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 } // namespace |
| 127 |
| 128 bool InitializeKaskoReporter(const base::string16& endpoint, |
| 129 const base::char16* browser_data_directory) { |
| 130 base::string16 crash_server; |
| 131 GetKaskoCrashServerUrl(&crash_server); |
| 132 base::FilePath crash_reports_base_dir; |
| 133 GetKaskoCrashReportsBaseDir(browser_data_directory, &crash_reports_base_dir); |
| 134 |
| 135 return kasko::api::InitializeReporter( |
| 136 endpoint.c_str(), |
| 137 crash_server.c_str(), |
| 138 crash_reports_base_dir.Append(L"Crash Reports").value().c_str(), |
| 139 crash_reports_base_dir.Append(kPermanentlyFailedReportsSubdir) |
| 140 .value() |
| 141 .c_str(), |
| 142 &OnCrashReportUpload, |
| 143 nullptr); |
| 144 } |
| 145 |
| 146 void ShutdownKaskoReporter() { |
| 147 kasko::api::ShutdownReporter(); |
| 148 } |
| 149 |
| 150 void DumpHungProcess(DWORD main_thread_id, const base::string16 &channel, |
| 151 const base::Process &process) { |
| 152 // Read the Crashpad module annotations for the process. |
| 153 std::vector<kasko::api::CrashKey> annotations; |
| 154 crash_reporter::ReadMainModuleAnnotationsForKasko(process, &annotations); |
| 155 |
| 156 // Add a special crash key to distinguish reports generated for a hung |
| 157 // process. |
| 158 annotations.push_back(kasko::api::CrashKey{L"hung-process", L"1"}); |
| 159 |
| 160 std::vector<const base::char16*> key_buffers; |
| 161 std::vector<const base::char16*> value_buffers; |
| 162 for (const auto& crash_key : annotations) { |
| 163 key_buffers.push_back(crash_key.name); |
| 164 value_buffers.push_back(crash_key.value); |
| 165 } |
| 166 key_buffers.push_back(nullptr); |
| 167 value_buffers.push_back(nullptr); |
| 168 |
| 169 // Synthesize an exception for the main thread. Populate the record with the |
| 170 // current context of the thread to get the stack trace bucketed on the crash |
| 171 // backend. |
| 172 CONTEXT thread_context = {}; |
| 173 EXCEPTION_RECORD exception_record = {}; |
| 174 exception_record.ExceptionCode = EXCEPTION_ARRAY_BOUNDS_EXCEEDED; |
| 175 EXCEPTION_POINTERS exception_pointers = {&exception_record, &thread_context}; |
| 176 |
| 177 base::win::ScopedHandle main_thread(::OpenThread( |
| 178 THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION, |
| 179 FALSE, main_thread_id)); |
| 180 |
| 181 bool have_context = false; |
| 182 if (main_thread.IsValid()) { |
| 183 DWORD suspend_count = ::SuspendThread(main_thread.Get()); |
| 184 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| 185 if (suspend_count != kSuspendFailed) { |
| 186 // Best effort capture of the context. |
| 187 thread_context.ContextFlags = CONTEXT_FLOATING_POINT | CONTEXT_SEGMENTS | |
| 188 CONTEXT_INTEGER | CONTEXT_CONTROL; |
| 189 if (::GetThreadContext(main_thread.Get(), &thread_context) == TRUE) |
| 190 have_context = true; |
| 191 |
| 192 ::ResumeThread(main_thread.Get()); |
| 193 } |
| 194 } |
| 195 |
| 196 // TODO(erikwright): Make the dump-type channel-dependent. |
| 197 if (have_context) { |
| 198 kasko::api::SendReportForProcess( |
| 199 process.Handle(), main_thread_id, &exception_pointers, |
| 200 kasko::api::LARGER_DUMP_TYPE, key_buffers.data(), value_buffers.data()); |
| 201 } else { |
| 202 kasko::api::SendReportForProcess(process.Handle(), 0, nullptr, |
| 203 kasko::api::LARGER_DUMP_TYPE, |
| 204 key_buffers.data(), value_buffers.data()); |
| 205 } |
| 206 } |
| 207 |
| 208 #endif // BUILDFLAG(ENABLE_KASKO) |
OLD | NEW |