Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: chrome/chrome_watcher/kasko_util.cc

Issue 1844023002: Capture a report on failed browser rendez-vous. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix consent validation at rdv Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/chrome_watcher/kasko_util.h"
6
7 #include <sddl.h>
8
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/base_paths.h"
14 #include "base/bind.h"
15 #include "base/callback_helpers.h"
16 #include "base/environment.h"
17 #include "base/files/file_path.h"
18 #include "base/macros.h"
19 #include "base/path_service.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/win/win_util.h"
22
23 #include "chrome/chrome_watcher/chrome_watcher_main_api.h"
24
25 #if BUILDFLAG(ENABLE_KASKO)
26 #include "components/crash/content/app/crashpad.h"
27 #include "syzygy/kasko/api/reporter.h"
28 #endif
29
30 #if BUILDFLAG(ENABLE_KASKO)
31
32 namespace {
33
34 // Helper function for determining the crash server to use. Defaults to the
35 // standard crash server, but can be overridden via an environment variable.
36 // Enables easy integration testing.
37 base::string16 GetKaskoCrashServerUrl() {
38 static const char kKaskoCrashServerUrl[] = "KASKO_CRASH_SERVER_URL";
39 static const wchar_t kDefaultKaskoCrashServerUrl[] =
40 L"https://clients2.google.com/cr/report";
41
42 std::unique_ptr<base::Environment> env(base::Environment::Create());
43 std::string env_var;
44 if (env->GetVar(kKaskoCrashServerUrl, &env_var)) {
45 return base::UTF8ToUTF16(env_var);
46 }
47 return kDefaultKaskoCrashServerUrl;
48 }
49
50 // Helper function for determining the crash reports directory to use. Defaults
51 // to the browser data directory, but can be overridden via an environment
52 // variable. Enables easy integration testing.
53 base::FilePath GetKaskoCrashReportsBaseDir(
54 const base::char16* browser_data_directory) {
55 static const char kKaskoCrashReportBaseDir[] = "KASKO_CRASH_REPORTS_BASE_DIR";
56 std::unique_ptr<base::Environment> env(base::Environment::Create());
57 std::string env_var;
58 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) {
59 return base::FilePath(base::UTF8ToUTF16(env_var));
60 }
61 return base::FilePath(browser_data_directory);
62 }
63
64 struct EventSourceDeregisterer {
65 using pointer = HANDLE;
66 void operator()(HANDLE event_source_handle) const {
67 if (!::DeregisterEventSource(event_source_handle))
68 DPLOG(ERROR) << "DeregisterEventSource";
69 }
70 };
71 using ScopedEventSourceHandle =
72 std::unique_ptr<HANDLE, EventSourceDeregisterer>;
73
74 struct SidDeleter {
75 using pointer = PSID;
76 void operator()(PSID sid) const {
77 if (::LocalFree(sid) != nullptr)
78 DPLOG(ERROR) << "LocalFree";
79 }
80 };
81 using ScopedSid = std::unique_ptr<PSID, SidDeleter>;
82
83 void OnCrashReportUpload(void* context,
84 const base::char16* report_id,
85 const base::char16* minidump_path,
86 const base::char16* const* keys,
87 const base::char16* const* values) {
88 // Open the event source.
89 ScopedEventSourceHandle event_source_handle(
90 ::RegisterEventSource(nullptr, L"Chrome"));
91 if (!event_source_handle) {
92 PLOG(ERROR) << "RegisterEventSource";
93 return;
94 }
95
96 // Get the user's SID for the log record.
97 base::string16 sid_string;
98 PSID sid = nullptr;
99 if (base::win::GetUserSidString(&sid_string) && !sid_string.empty()) {
100 if (!::ConvertStringSidToSid(sid_string.c_str(), &sid))
101 DPLOG(ERROR) << "ConvertStringSidToSid";
102 DCHECK(sid);
103 }
104 // Ensure cleanup on scope exit.
105 ScopedSid scoped_sid;
106 if (sid)
107 scoped_sid.reset(sid);
108
109 // Generate the message.
110 // Note that the format of this message must match the consumer in
111 // chrome/browser/crash_upload_list_win.cc.
112 base::string16 message =
113 L"Crash uploaded. Id=" + base::string16(report_id) + L".";
114
115 // Matches Omaha.
116 const int kCrashUploadEventId = 2;
117
118 // Report the event.
119 const base::char16* strings[] = {message.c_str()};
120 if (!::ReportEvent(event_source_handle.get(), EVENTLOG_INFORMATION_TYPE,
121 0, // category
122 kCrashUploadEventId, sid,
123 1, // count
124 0, strings, nullptr)) {
125 DPLOG(ERROR);
126 }
127 }
128
129 void AddCrashKey(const wchar_t *key, const wchar_t *value,
130 std::vector<kasko::api::CrashKey> *crash_keys) {
131 DCHECK(key);
132 DCHECK(value);
133 DCHECK(crash_keys);
134
135 kasko::api::CrashKey crash_key;
136 std::wcsncpy(crash_key.name, key, kasko::api::CrashKey::kNameMaxLength - 1);
137 std::wcsncpy(crash_key.value, value,
138 kasko::api::CrashKey::kValueMaxLength - 1);
139 crash_keys->push_back(crash_key);
140 }
141
142 } // namespace
143
144 bool InitializeKaskoReporter(const base::string16& endpoint,
145 const base::char16* browser_data_directory) {
146 base::string16 crash_server = GetKaskoCrashServerUrl();
147 base::FilePath crash_reports_base_dir =
148 GetKaskoCrashReportsBaseDir(browser_data_directory);
149
150 return kasko::api::InitializeReporter(
151 endpoint.c_str(),
152 crash_server.c_str(),
153 crash_reports_base_dir.Append(L"Crash Reports").value().c_str(),
154 crash_reports_base_dir.Append(kPermanentlyFailedReportsSubdir)
155 .value()
156 .c_str(),
157 &OnCrashReportUpload,
158 nullptr);
159 }
160
161 void ShutdownKaskoReporter() {
162 kasko::api::ShutdownReporter();
163 }
164
165 bool EnsureTargetProcessValidForCapture(const base::Process& process) {
166 // Ensure the target process shares the current process's executable name.
167 base::FilePath exe_self;
168 if (!PathService::Get(base::FILE_EXE, &exe_self))
169 return false;
170
171 wchar_t exe_name_other[MAX_PATH];
172 DWORD exe_name_other_len = arraysize(exe_name_other);
173 // Note: requesting the Win32 path format.
174 if (::QueryFullProcessImageName(process.Handle(), 0, exe_name_other,
175 &exe_name_other_len) == 0) {
176 DPLOG(ERROR) << "Failed to get executable name for other process";
177 return false;
178 }
179
180 // QueryFullProcessImageName's documentation does not specify behavior when
181 // the buffer is too small, but we know that GetModuleFileNameEx succeeds and
182 // truncates the returned name in such a case. Given that paths of arbitrary
183 // length may exist, the conservative approach is to reject names when
184 // the returned length is that of the buffer.
185 if (exe_name_other_len > 0 &&
186 exe_name_other_len < arraysize(exe_name_other)) {
187 return base::FilePath::CompareEqualIgnoreCase(exe_self.value(),
188 exe_name_other);
189 }
190 return false;
191 }
192
193 void DumpHungProcess(DWORD main_thread_id, const base::string16& channel,
194 const base::char16* key, const base::Process& process) {
195 // Read the Crashpad module annotations for the process.
196 std::vector<kasko::api::CrashKey> annotations;
197 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations);
198 AddCrashKey(key, L"1", &annotations);
199
200 std::vector<const base::char16*> key_buffers;
201 std::vector<const base::char16*> value_buffers;
202 for (const auto& crash_key : annotations) {
203 key_buffers.push_back(crash_key.name);
204 value_buffers.push_back(crash_key.value);
205 }
206 key_buffers.push_back(nullptr);
207 value_buffers.push_back(nullptr);
208
209 // Synthesize an exception for the main thread. Populate the record with the
210 // current context of the thread to get the stack trace bucketed on the crash
211 // backend.
212 CONTEXT thread_context = {};
213 EXCEPTION_RECORD exception_record = {};
214 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED;
215 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context};
216
217 base::win::ScopedHandle main_thread(::OpenThread(
218 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
219 FALSE, main_thread_id));
220
221 bool have_context = false;
222 if (main_thread.IsValid()) {
223 DWORD suspend_count = ::SuspendThread(main_thread.Get());
224 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
225 if (suspend_count != kSuspendFailed) {
226 // Best effort capture of the context.
227 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS |
228 CONTEXT_INTEGER | CONTEXT_CONTROL;
229 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE)
230 have_context = true;
231
232 ::ResumeThread(main_thread.Get());
233 }
234 }
235
236 // TODO(manzagop): consider making the dump-type channel-dependent.
237 if (have_context) {
238 kasko::api::SendReportForProcess(
239 process.Handle(), main_thread_id, &exception_pointers,
240 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data());
241 } else {
242 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr,
243 kasko::api::LARGER_DUMP_TYPE,
244 key_buffers.data(), value_buffers.data());
245 }
246 }
247
248 #endif // BUILDFLAG(ENABLE_KASKO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698