Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/path_service.h" | |
| 19 #include "base/strings/utf_string_conversions.h" | |
| 20 #include "base/win/win_util.h" | |
| 21 | |
| 22 #include "chrome/chrome_watcher/chrome_watcher_main_api.h" | |
| 23 | |
| 24 #if BUILDFLAG(ENABLE_KASKO) | |
| 25 #include "components/crash/content/app/crashpad.h" | |
| 26 #include "syzygy/kasko/api/reporter.h" | |
| 27 #endif | |
| 28 | |
| 29 #if BUILDFLAG(ENABLE_KASKO) | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Helper function for determining the crash server to use. Defaults to the | |
| 34 // standard crash server, but can be overridden via an environment variable. | |
| 35 // Enables easy integration testing. | |
| 36 base::string16 GetKaskoCrashServerUrl() { | |
| 37 static const char kKaskoCrashServerUrl[] = "KASKO_CRASH_SERVER_URL"; | |
| 38 static const wchar_t kDefaultKaskoCrashServerUrl[] = | |
| 39 L"https://clients2.google.com/cr/report"; | |
| 40 | |
| 41 std::unique_ptr<base::Environment> env(base::Environment::Create()); | |
| 42 std::string env_var; | |
| 43 if (env->GetVar(kKaskoCrashServerUrl, &env_var)) { | |
| 44 return base::UTF8ToUTF16(env_var); | |
| 45 } | |
| 46 return kDefaultKaskoCrashServerUrl; | |
| 47 } | |
| 48 | |
| 49 // Helper function for determining the crash reports directory to use. Defaults | |
| 50 // to the browser data directory, but can be overridden via an environment | |
| 51 // variable. Enables easy integration testing. | |
| 52 base::FilePath GetKaskoCrashReportsBaseDir( | |
| 53 const base::char16* browser_data_directory) { | |
| 54 static const char kKaskoCrashReportBaseDir[] = "KASKO_CRASH_REPORTS_BASE_DIR"; | |
| 55 std::unique_ptr<base::Environment> env(base::Environment::Create()); | |
| 56 std::string env_var; | |
| 57 if (env->GetVar(kKaskoCrashReportBaseDir, &env_var)) { | |
| 58 return base::FilePath(base::UTF8ToUTF16(env_var)); | |
| 59 } | |
| 60 return base::FilePath(browser_data_directory); | |
| 61 } | |
| 62 | |
| 63 struct EventSourceDeregisterer { | |
| 64 using pointer = HANDLE; | |
| 65 void operator()(HANDLE event_source_handle) { | |
|
grt (UTC plus 2)
2016/04/06 19:50:36
const method
manzagop (departed)
2016/04/06 20:44:54
Done.
| |
| 66 if (!::DeregisterEventSource(event_source_handle)) | |
| 67 DPLOG(ERROR) << "DeregisterEventSource"; | |
| 68 } | |
| 69 }; | |
| 70 using ScopedEventSourceHandle = | |
| 71 std::unique_ptr<HANDLE, EventSourceDeregisterer>; | |
| 72 | |
| 73 struct SidDeleter { | |
| 74 using pointer = PSID; | |
| 75 void operator()(PSID sid) { | |
|
grt (UTC plus 2)
2016/04/06 19:50:35
const method
manzagop (departed)
2016/04/06 20:44:53
Done.
| |
| 76 if (::LocalFree(sid) != nullptr) | |
| 77 DPLOG(ERROR) << "LocalFree"; | |
| 78 } | |
| 79 }; | |
| 80 using ScopedSid = std::unique_ptr<PSID, SidDeleter>; | |
| 81 | |
| 82 void OnCrashReportUpload(void* context, | |
| 83 const base::char16* report_id, | |
| 84 const base::char16* minidump_path, | |
| 85 const base::char16* const* keys, | |
| 86 const base::char16* const* values) { | |
| 87 // Open the event source. | |
| 88 ScopedEventSourceHandle event_source_handle( | |
| 89 ::RegisterEventSource(nullptr, L"Chrome")); | |
| 90 if (!event_source_handle) { | |
| 91 PLOG(ERROR) << "RegisterEventSource"; | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 // Get the user's SID for the log record. | |
| 96 base::string16 sid_string; | |
| 97 PSID sid = nullptr; | |
| 98 if (base::win::GetUserSidString(&sid_string) && !sid_string.empty()) { | |
| 99 if (!::ConvertStringSidToSid(sid_string.c_str(), &sid)) | |
| 100 DPLOG(ERROR) << "ConvertStringSidToSid"; | |
| 101 DCHECK(sid); | |
| 102 } | |
| 103 // Ensure cleanup on scope exit. | |
| 104 ScopedSid scoped_sid; | |
| 105 if (sid) | |
| 106 scoped_sid.reset(sid); | |
| 107 | |
| 108 // Generate the message. | |
| 109 // Note that the format of this message must match the consumer in | |
| 110 // chrome/browser/crash_upload_list_win.cc. | |
| 111 base::string16 message = | |
| 112 L"Crash uploaded. Id=" + base::string16(report_id) + L"."; | |
| 113 | |
| 114 // Matches Omaha. | |
| 115 const int kCrashUploadEventId = 2; | |
| 116 | |
| 117 // Report the event. | |
| 118 const base::char16* strings[] = {message.c_str()}; | |
| 119 if (!::ReportEvent(event_source_handle.get(), EVENTLOG_INFORMATION_TYPE, | |
| 120 0, // category | |
| 121 kCrashUploadEventId, sid, | |
| 122 1, // count | |
| 123 0, strings, nullptr)) { | |
| 124 DPLOG(ERROR); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void AddCrashKey(const wchar_t *key, const wchar_t *value, | |
| 129 std::vector<kasko::api::CrashKey> *crash_keys) { | |
| 130 DCHECK(key); | |
| 131 DCHECK(value); | |
| 132 DCHECK(crash_keys); | |
| 133 | |
| 134 kasko::api::CrashKey crash_key; | |
| 135 std::wcsncpy(crash_key.name, key, kasko::api::CrashKey::kNameMaxLength - 1); | |
| 136 std::wcsncpy(crash_key.value, value, | |
| 137 kasko::api::CrashKey::kValueMaxLength - 1); | |
| 138 crash_keys->push_back(crash_key); | |
| 139 } | |
| 140 | |
| 141 } // namespace | |
| 142 | |
| 143 bool InitializeKaskoReporter(const base::string16& endpoint, | |
| 144 const base::char16* browser_data_directory) { | |
| 145 base::string16 crash_server = GetKaskoCrashServerUrl(); | |
| 146 base::FilePath crash_reports_base_dir = | |
| 147 GetKaskoCrashReportsBaseDir(browser_data_directory); | |
| 148 | |
| 149 return kasko::api::InitializeReporter( | |
| 150 endpoint.c_str(), | |
| 151 crash_server.c_str(), | |
| 152 crash_reports_base_dir.Append(L"Crash Reports").value().c_str(), | |
| 153 crash_reports_base_dir.Append(kPermanentlyFailedReportsSubdir) | |
| 154 .value() | |
| 155 .c_str(), | |
| 156 &OnCrashReportUpload, | |
| 157 nullptr); | |
| 158 } | |
| 159 | |
| 160 void ShutdownKaskoReporter() { | |
| 161 kasko::api::ShutdownReporter(); | |
| 162 } | |
| 163 | |
| 164 bool EnsureTargetProcessValidForCapture(const base::Process& process) { | |
| 165 // Ensure the target process shares the current process's executable name. | |
| 166 base::FilePath exe_self; | |
| 167 if (!PathService::Get(base::FILE_EXE, &exe_self)) | |
| 168 return false; | |
| 169 wchar_t exe_name_other[MAX_PATH]; | |
| 170 DWORD exe_name_other_len = MAX_PATH; | |
| 171 if (QueryFullProcessImageName(process.Handle(), 0, exe_name_other, | |
| 172 &exe_name_other_len) == 0) { | |
|
grt (UTC plus 2)
2016/04/06 19:50:36
do you need to check exe_name_other_len after the
manzagop (departed)
2016/04/06 20:44:54
I'm not sure. I see that paths can sometimes be lo
| |
| 173 DPLOG(ERROR) << "Failed to get executable name for other process"; | |
| 174 return false; | |
| 175 } | |
| 176 return base::FilePath::CompareEqualIgnoreCase(exe_self.value(), | |
| 177 exe_name_other); | |
| 178 } | |
| 179 | |
| 180 void DumpHungProcess(DWORD main_thread_id, const base::string16& channel, | |
| 181 const base::char16* key, const base::Process& process) { | |
| 182 // Read the Crashpad module annotations for the process. | |
| 183 std::vector<kasko::api::CrashKey> annotations; | |
| 184 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations); | |
| 185 AddCrashKey(key, L"1", &annotations); | |
| 186 | |
| 187 std::vector<const base::char16*> key_buffers; | |
| 188 std::vector<const base::char16*> value_buffers; | |
| 189 for (const auto& crash_key : annotations) { | |
| 190 key_buffers.push_back(crash_key.name); | |
| 191 value_buffers.push_back(crash_key.value); | |
| 192 } | |
| 193 key_buffers.push_back(nullptr); | |
| 194 value_buffers.push_back(nullptr); | |
| 195 | |
| 196 // Synthesize an exception for the main thread. Populate the record with the | |
| 197 // current context of the thread to get the stack trace bucketed on the crash | |
| 198 // backend. | |
| 199 CONTEXT thread_context = {}; | |
| 200 EXCEPTION_RECORD exception_record = {}; | |
| 201 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED; | |
| 202 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context}; | |
| 203 | |
| 204 base::win::ScopedHandle main_thread(::OpenThread( | |
| 205 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, | |
| 206 FALSE, main_thread_id)); | |
| 207 | |
| 208 bool have_context = false; | |
| 209 if (main_thread.IsValid()) { | |
| 210 DWORD suspend_count = ::SuspendThread(main_thread.Get()); | |
| 211 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | |
| 212 if (suspend_count != kSuspendFailed) { | |
| 213 // Best effort capture of the context. | |
| 214 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS | | |
| 215 CONTEXT_INTEGER | CONTEXT_CONTROL; | |
| 216 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE) | |
| 217 have_context = true; | |
| 218 | |
| 219 ::ResumeThread(main_thread.Get()); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 // TODO(manzagop): consider making the dump-type channel-dependent. | |
| 224 if (have_context) { | |
| 225 kasko::api::SendReportForProcess( | |
| 226 process.Handle(), main_thread_id, &exception_pointers, | |
| 227 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data()); | |
| 228 } else { | |
| 229 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr, | |
| 230 kasko::api::LARGER_DUMP_TYPE, | |
| 231 key_buffers.data(), value_buffers.data()); | |
| 232 } | |
| 233 } | |
| 234 | |
| 235 #endif // BUILDFLAG(ENABLE_KASKO) | |
| OLD | NEW |