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