| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 "components/breakpad/breakpad_win.h" | |
| 6 | |
| 7 #include <windows.h> | |
| 8 #include <shellapi.h> | |
| 9 #include <tchar.h> | |
| 10 #include <userenv.h> | |
| 11 #include <winnt.h> | |
| 12 | |
| 13 #include <algorithm> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/base_switches.h" | |
| 17 #include "base/basictypes.h" | |
| 18 #include "base/command_line.h" | |
| 19 #include "base/debug/crash_logging.h" | |
| 20 #include "base/environment.h" | |
| 21 #include "base/memory/scoped_ptr.h" | |
| 22 #include "base/strings/string16.h" | |
| 23 #include "base/strings/string_split.h" | |
| 24 #include "base/strings/string_util.h" | |
| 25 #include "base/strings/stringprintf.h" | |
| 26 #include "base/strings/utf_string_conversions.h" | |
| 27 #include "base/win/metro.h" | |
| 28 #include "base/win/pe_image.h" | |
| 29 #include "base/win/registry.h" | |
| 30 #include "base/win/win_util.h" | |
| 31 #include "breakpad/src/client/windows/handler/exception_handler.h" | |
| 32 #include "components/breakpad/breakpad_client.h" | |
| 33 #include "components/breakpad/hard_error_handler_win.h" | |
| 34 #include "content/public/common/content_switches.h" | |
| 35 #include "content/public/common/result_codes.h" | |
| 36 #include "sandbox/win/src/nt_internals.h" | |
| 37 #include "sandbox/win/src/sidestep/preamble_patcher.h" | |
| 38 | |
| 39 // userenv.dll is required for GetProfileType(). | |
| 40 #pragma comment(lib, "userenv.lib") | |
| 41 | |
| 42 #pragma intrinsic(_AddressOfReturnAddress) | |
| 43 #pragma intrinsic(_ReturnAddress) | |
| 44 | |
| 45 namespace breakpad { | |
| 46 | |
| 47 // TODO(raymes): Modify the way custom crash info is stored. g_custom_entries | |
| 48 // is way too too fragile. See | |
| 49 // https://code.google.com/p/chromium/issues/detail?id=137062. | |
| 50 std::vector<google_breakpad::CustomInfoEntry>* g_custom_entries = NULL; | |
| 51 bool g_deferred_crash_uploads = false; | |
| 52 | |
| 53 namespace { | |
| 54 | |
| 55 // Minidump with stacks, PEB, TEB, and unloaded module list. | |
| 56 const MINIDUMP_TYPE kSmallDumpType = static_cast<MINIDUMP_TYPE>( | |
| 57 MiniDumpWithProcessThreadData | // Get PEB and TEB. | |
| 58 MiniDumpWithUnloadedModules); // Get unloaded modules when available. | |
| 59 | |
| 60 // Minidump with all of the above, plus memory referenced from stack. | |
| 61 const MINIDUMP_TYPE kLargerDumpType = static_cast<MINIDUMP_TYPE>( | |
| 62 MiniDumpWithProcessThreadData | // Get PEB and TEB. | |
| 63 MiniDumpWithUnloadedModules | // Get unloaded modules when available. | |
| 64 MiniDumpWithIndirectlyReferencedMemory); // Get memory referenced by stack. | |
| 65 | |
| 66 // Large dump with all process memory. | |
| 67 const MINIDUMP_TYPE kFullDumpType = static_cast<MINIDUMP_TYPE>( | |
| 68 MiniDumpWithFullMemory | // Full memory from process. | |
| 69 MiniDumpWithProcessThreadData | // Get PEB and TEB. | |
| 70 MiniDumpWithHandleData | // Get all handle information. | |
| 71 MiniDumpWithUnloadedModules); // Get unloaded modules when available. | |
| 72 | |
| 73 const char kPipeNameVar[] = "CHROME_BREAKPAD_PIPE_NAME"; | |
| 74 | |
| 75 const wchar_t kGoogleUpdatePipeName[] = L"\\\\.\\pipe\\GoogleCrashServices\\"; | |
| 76 const wchar_t kChromePipeName[] = L"\\\\.\\pipe\\ChromeCrashServices"; | |
| 77 | |
| 78 // This is the well known SID for the system principal. | |
| 79 const wchar_t kSystemPrincipalSid[] =L"S-1-5-18"; | |
| 80 | |
| 81 google_breakpad::ExceptionHandler* g_breakpad = NULL; | |
| 82 google_breakpad::ExceptionHandler* g_dumphandler_no_crash = NULL; | |
| 83 | |
| 84 EXCEPTION_POINTERS g_surrogate_exception_pointers = {0}; | |
| 85 EXCEPTION_RECORD g_surrogate_exception_record = {0}; | |
| 86 CONTEXT g_surrogate_context = {0}; | |
| 87 | |
| 88 typedef NTSTATUS (WINAPI* NtTerminateProcessPtr)(HANDLE ProcessHandle, | |
| 89 NTSTATUS ExitStatus); | |
| 90 char* g_real_terminate_process_stub = NULL; | |
| 91 | |
| 92 static size_t g_dynamic_keys_offset = 0; | |
| 93 typedef std::map<std::wstring, google_breakpad::CustomInfoEntry*> | |
| 94 DynamicEntriesMap; | |
| 95 DynamicEntriesMap* g_dynamic_entries = NULL; | |
| 96 // Allow for 128 entries. POSIX uses 64 entries of 256 bytes, so Windows needs | |
| 97 // 256 entries of 64 bytes to match. See CustomInfoEntry::kValueMaxLength in | |
| 98 // Breakpad. | |
| 99 const size_t kMaxDynamicEntries = 256; | |
| 100 | |
| 101 // Maximum length for plugin path to include in plugin crash reports. | |
| 102 const size_t kMaxPluginPathLength = 256; | |
| 103 | |
| 104 // Dumps the current process memory. | |
| 105 extern "C" void __declspec(dllexport) __cdecl DumpProcess() { | |
| 106 if (g_breakpad) { | |
| 107 g_breakpad->WriteMinidump(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 // Used for dumping a process state when there is no crash. | |
| 112 extern "C" void __declspec(dllexport) __cdecl DumpProcessWithoutCrash() { | |
| 113 if (g_dumphandler_no_crash) { | |
| 114 g_dumphandler_no_crash->WriteMinidump(); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 // We need to prevent ICF from folding DumpForHangDebuggingThread() and | |
| 119 // DumpProcessWithoutCrashThread() together, since that makes them | |
| 120 // indistinguishable in crash dumps. We do this by making the function | |
| 121 // bodies unique, and prevent optimization from shuffling things around. | |
| 122 MSVC_DISABLE_OPTIMIZE() | |
| 123 MSVC_PUSH_DISABLE_WARNING(4748) | |
| 124 | |
| 125 DWORD WINAPI DumpProcessWithoutCrashThread(void*) { | |
| 126 DumpProcessWithoutCrash(); | |
| 127 return 0; | |
| 128 } | |
| 129 | |
| 130 // The following two functions do exactly the same thing as the two above. But | |
| 131 // we want the signatures to be different so that we can easily track them in | |
| 132 // crash reports. | |
| 133 // TODO(yzshen): Remove when enough information is collected and the hang rate | |
| 134 // of pepper/renderer processes is reduced. | |
| 135 DWORD WINAPI DumpForHangDebuggingThread(void*) { | |
| 136 DumpProcessWithoutCrash(); | |
| 137 LOG(INFO) << "dumped for hang debugging"; | |
| 138 return 0; | |
| 139 } | |
| 140 | |
| 141 MSVC_POP_WARNING() | |
| 142 MSVC_ENABLE_OPTIMIZE() | |
| 143 | |
| 144 // Injects a thread into a remote process to dump state when there is no crash. | |
| 145 extern "C" HANDLE __declspec(dllexport) __cdecl | |
| 146 InjectDumpProcessWithoutCrash(HANDLE process) { | |
| 147 return CreateRemoteThread(process, NULL, 0, DumpProcessWithoutCrashThread, | |
| 148 0, 0, NULL); | |
| 149 } | |
| 150 | |
| 151 extern "C" HANDLE __declspec(dllexport) __cdecl | |
| 152 InjectDumpForHangDebugging(HANDLE process) { | |
| 153 return CreateRemoteThread(process, NULL, 0, DumpForHangDebuggingThread, | |
| 154 0, 0, NULL); | |
| 155 } | |
| 156 | |
| 157 extern "C" void DumpProcessAbnormalSignature() { | |
| 158 if (!g_breakpad) | |
| 159 return; | |
| 160 g_custom_entries->push_back( | |
| 161 google_breakpad::CustomInfoEntry(L"unusual-crash-signature", L"")); | |
| 162 g_breakpad->WriteMinidump(); | |
| 163 } | |
| 164 | |
| 165 // Reduces the size of the string |str| to a max of 64 chars. Required because | |
| 166 // breakpad's CustomInfoEntry raises an invalid_parameter error if the string | |
| 167 // we want to set is longer. | |
| 168 std::wstring TrimToBreakpadMax(const std::wstring& str) { | |
| 169 std::wstring shorter(str); | |
| 170 return shorter.substr(0, | |
| 171 google_breakpad::CustomInfoEntry::kValueMaxLength - 1); | |
| 172 } | |
| 173 | |
| 174 static void SetIntegerValue(size_t offset, int value) { | |
| 175 if (!g_custom_entries) | |
| 176 return; | |
| 177 | |
| 178 base::wcslcpy((*g_custom_entries)[offset].value, | |
| 179 base::StringPrintf(L"%d", value).c_str(), | |
| 180 google_breakpad::CustomInfoEntry::kValueMaxLength); | |
| 181 } | |
| 182 | |
| 183 // Appends the plugin path to |g_custom_entries|. | |
| 184 void SetPluginPath(const std::wstring& path) { | |
| 185 DCHECK(g_custom_entries); | |
| 186 | |
| 187 if (path.size() > kMaxPluginPathLength) { | |
| 188 // If the path is too long, truncate from the start rather than the end, | |
| 189 // since we want to be able to recover the DLL name. | |
| 190 SetPluginPath(path.substr(path.size() - kMaxPluginPathLength)); | |
| 191 return; | |
| 192 } | |
| 193 | |
| 194 // The chunk size without terminator. | |
| 195 const size_t kChunkSize = static_cast<size_t>( | |
| 196 google_breakpad::CustomInfoEntry::kValueMaxLength - 1); | |
| 197 | |
| 198 int chunk_index = 0; | |
| 199 size_t chunk_start = 0; // Current position inside |path| | |
| 200 | |
| 201 for (chunk_start = 0; chunk_start < path.size(); chunk_index++) { | |
| 202 size_t chunk_length = std::min(kChunkSize, path.size() - chunk_start); | |
| 203 | |
| 204 g_custom_entries->push_back(google_breakpad::CustomInfoEntry( | |
| 205 base::StringPrintf(L"plugin-path-chunk-%i", chunk_index + 1).c_str(), | |
| 206 path.substr(chunk_start, chunk_length).c_str())); | |
| 207 | |
| 208 chunk_start += chunk_length; | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 // Appends the breakpad dump path to |g_custom_entries|. | |
| 213 void SetBreakpadDumpPath() { | |
| 214 DCHECK(g_custom_entries); | |
| 215 base::FilePath crash_dumps_dir_path; | |
| 216 if (GetBreakpadClient()->GetAlternativeCrashDumpLocation( | |
| 217 &crash_dumps_dir_path)) { | |
| 218 g_custom_entries->push_back(google_breakpad::CustomInfoEntry( | |
| 219 L"breakpad-dump-location", crash_dumps_dir_path.value().c_str())); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 // Returns a string containing a list of all modifiers for the loaded profile. | |
| 224 std::wstring GetProfileType() { | |
| 225 std::wstring profile_type; | |
| 226 DWORD profile_bits = 0; | |
| 227 if (::GetProfileType(&profile_bits)) { | |
| 228 static const struct { | |
| 229 DWORD bit; | |
| 230 const wchar_t* name; | |
| 231 } kBitNames[] = { | |
| 232 { PT_MANDATORY, L"mandatory" }, | |
| 233 { PT_ROAMING, L"roaming" }, | |
| 234 { PT_TEMPORARY, L"temporary" }, | |
| 235 }; | |
| 236 for (size_t i = 0; i < arraysize(kBitNames); ++i) { | |
| 237 const DWORD this_bit = kBitNames[i].bit; | |
| 238 if ((profile_bits & this_bit) != 0) { | |
| 239 profile_type.append(kBitNames[i].name); | |
| 240 profile_bits &= ~this_bit; | |
| 241 if (profile_bits != 0) | |
| 242 profile_type.append(L", "); | |
| 243 } | |
| 244 } | |
| 245 } else { | |
| 246 DWORD last_error = ::GetLastError(); | |
| 247 base::SStringPrintf(&profile_type, L"error %u", last_error); | |
| 248 } | |
| 249 return profile_type; | |
| 250 } | |
| 251 | |
| 252 // Returns the custom info structure based on the dll in parameter and the | |
| 253 // process type. | |
| 254 google_breakpad::CustomClientInfo* GetCustomInfo(const std::wstring& exe_path, | |
| 255 const std::wstring& type) { | |
| 256 base::string16 version, product; | |
| 257 base::string16 special_build; | |
| 258 base::string16 channel_name; | |
| 259 GetBreakpadClient()->GetProductNameAndVersion( | |
| 260 base::FilePath(exe_path), | |
| 261 &product, | |
| 262 &version, | |
| 263 &special_build, | |
| 264 &channel_name); | |
| 265 | |
| 266 // We only expect this method to be called once per process. | |
| 267 DCHECK(!g_custom_entries); | |
| 268 g_custom_entries = new std::vector<google_breakpad::CustomInfoEntry>; | |
| 269 | |
| 270 // Common g_custom_entries. | |
| 271 g_custom_entries->push_back( | |
| 272 google_breakpad::CustomInfoEntry(L"ver", UTF16ToWide(version).c_str())); | |
| 273 g_custom_entries->push_back( | |
| 274 google_breakpad::CustomInfoEntry(L"prod", UTF16ToWide(product).c_str())); | |
| 275 g_custom_entries->push_back( | |
| 276 google_breakpad::CustomInfoEntry(L"plat", L"Win32")); | |
| 277 g_custom_entries->push_back( | |
| 278 google_breakpad::CustomInfoEntry(L"ptype", type.c_str())); | |
| 279 g_custom_entries->push_back(google_breakpad::CustomInfoEntry( | |
| 280 L"channel", base::UTF16ToWide(channel_name).c_str())); | |
| 281 g_custom_entries->push_back(google_breakpad::CustomInfoEntry( | |
| 282 L"profile-type", GetProfileType().c_str())); | |
| 283 | |
| 284 if (g_deferred_crash_uploads) | |
| 285 g_custom_entries->push_back( | |
| 286 google_breakpad::CustomInfoEntry(L"deferred-upload", L"true")); | |
| 287 | |
| 288 if (!special_build.empty()) | |
| 289 g_custom_entries->push_back(google_breakpad::CustomInfoEntry( | |
| 290 L"special", UTF16ToWide(special_build).c_str())); | |
| 291 | |
| 292 if (type == L"plugin" || type == L"ppapi") { | |
| 293 std::wstring plugin_path = | |
| 294 CommandLine::ForCurrentProcess()->GetSwitchValueNative("plugin-path"); | |
| 295 if (!plugin_path.empty()) | |
| 296 SetPluginPath(plugin_path); | |
| 297 } | |
| 298 | |
| 299 // Check whether configuration management controls crash reporting. | |
| 300 bool crash_reporting_enabled = true; | |
| 301 bool controlled_by_policy = GetBreakpadClient()->ReportingIsEnforcedByPolicy( | |
| 302 &crash_reporting_enabled); | |
| 303 const CommandLine& command = *CommandLine::ForCurrentProcess(); | |
| 304 bool use_crash_service = | |
| 305 !controlled_by_policy && (command.HasSwitch(switches::kNoErrorDialogs) || | |
| 306 GetBreakpadClient()->IsRunningUnattended()); | |
| 307 if (use_crash_service) | |
| 308 SetBreakpadDumpPath(); | |
| 309 | |
| 310 // Create space for dynamic ad-hoc keys. The names and values are set using | |
| 311 // the API defined in base/debug/crash_logging.h. | |
| 312 g_dynamic_keys_offset = g_custom_entries->size(); | |
| 313 for (size_t i = 0; i < kMaxDynamicEntries; ++i) { | |
| 314 // The names will be mutated as they are set. Un-numbered since these are | |
| 315 // merely placeholders. The name cannot be empty because Breakpad's | |
| 316 // HTTPUpload will interpret that as an invalid parameter. | |
| 317 g_custom_entries->push_back( | |
| 318 google_breakpad::CustomInfoEntry(L"unspecified-crash-key", L"")); | |
| 319 } | |
| 320 g_dynamic_entries = new DynamicEntriesMap; | |
| 321 | |
| 322 static google_breakpad::CustomClientInfo custom_client_info; | |
| 323 custom_client_info.entries = &g_custom_entries->front(); | |
| 324 custom_client_info.count = g_custom_entries->size(); | |
| 325 | |
| 326 return &custom_client_info; | |
| 327 } | |
| 328 | |
| 329 // This callback is used when we want to get a dump without crashing the | |
| 330 // process. | |
| 331 bool DumpDoneCallbackWhenNoCrash(const wchar_t*, const wchar_t*, void*, | |
| 332 EXCEPTION_POINTERS* ex_info, | |
| 333 MDRawAssertionInfo*, bool) { | |
| 334 return true; | |
| 335 } | |
| 336 | |
| 337 // This callback is executed when the browser process has crashed, after | |
| 338 // the crash dump has been created. We need to minimize the amount of work | |
| 339 // done here since we have potentially corrupted process. Our job is to | |
| 340 // spawn another instance of chrome which will show a 'chrome has crashed' | |
| 341 // dialog. This code needs to live in the exe and thus has no access to | |
| 342 // facilities such as the i18n helpers. | |
| 343 bool DumpDoneCallback(const wchar_t*, const wchar_t*, void*, | |
| 344 EXCEPTION_POINTERS* ex_info, | |
| 345 MDRawAssertionInfo*, bool) { | |
| 346 // Check if the exception is one of the kind which would not be solved | |
| 347 // by simply restarting chrome. In this case we show a message box with | |
| 348 // and exit silently. Remember that chrome is in a crashed state so we | |
| 349 // can't show our own UI from this process. | |
| 350 if (HardErrorHandler(ex_info)) | |
| 351 return true; | |
| 352 | |
| 353 if (!GetBreakpadClient()->AboutToRestart()) | |
| 354 return true; | |
| 355 | |
| 356 // Now we just start chrome browser with the same command line. | |
| 357 STARTUPINFOW si = {sizeof(si)}; | |
| 358 PROCESS_INFORMATION pi; | |
| 359 if (::CreateProcessW(NULL, ::GetCommandLineW(), NULL, NULL, FALSE, | |
| 360 CREATE_UNICODE_ENVIRONMENT, NULL, NULL, &si, &pi)) { | |
| 361 ::CloseHandle(pi.hProcess); | |
| 362 ::CloseHandle(pi.hThread); | |
| 363 } | |
| 364 // After this return we will be terminated. The actual return value is | |
| 365 // not used at all. | |
| 366 return true; | |
| 367 } | |
| 368 | |
| 369 // flag to indicate that we are already handling an exception. | |
| 370 volatile LONG handling_exception = 0; | |
| 371 | |
| 372 // This callback is used when there is no crash. Note: Unlike the | |
| 373 // |FilterCallback| below this does not do dupe detection. It is upto the caller | |
| 374 // to implement it. | |
| 375 bool FilterCallbackWhenNoCrash( | |
| 376 void*, EXCEPTION_POINTERS*, MDRawAssertionInfo*) { | |
| 377 GetBreakpadClient()->RecordCrashDumpAttempt(false); | |
| 378 return true; | |
| 379 } | |
| 380 | |
| 381 // This callback is executed when the Chrome process has crashed and *before* | |
| 382 // the crash dump is created. To prevent duplicate crash reports we | |
| 383 // make every thread calling this method, except the very first one, | |
| 384 // go to sleep. | |
| 385 bool FilterCallback(void*, EXCEPTION_POINTERS*, MDRawAssertionInfo*) { | |
| 386 // Capture every thread except the first one in the sleep. We don't | |
| 387 // want multiple threads to concurrently report exceptions. | |
| 388 if (::InterlockedCompareExchange(&handling_exception, 1, 0) == 1) { | |
| 389 ::Sleep(INFINITE); | |
| 390 } | |
| 391 GetBreakpadClient()->RecordCrashDumpAttempt(true); | |
| 392 return true; | |
| 393 } | |
| 394 | |
| 395 // Previous unhandled filter. Will be called if not null when we | |
| 396 // intercept a crash. | |
| 397 LPTOP_LEVEL_EXCEPTION_FILTER previous_filter = NULL; | |
| 398 | |
| 399 // Exception filter used when breakpad is not enabled. We just display | |
| 400 // the "Do you want to restart" message and then we call the previous filter. | |
| 401 long WINAPI ChromeExceptionFilter(EXCEPTION_POINTERS* info) { | |
| 402 DumpDoneCallback(NULL, NULL, NULL, info, NULL, false); | |
| 403 | |
| 404 if (previous_filter) | |
| 405 return previous_filter(info); | |
| 406 | |
| 407 return EXCEPTION_EXECUTE_HANDLER; | |
| 408 } | |
| 409 | |
| 410 // Exception filter for the service process used when breakpad is not enabled. | |
| 411 // We just display the "Do you want to restart" message and then die | |
| 412 // (without calling the previous filter). | |
| 413 long WINAPI ServiceExceptionFilter(EXCEPTION_POINTERS* info) { | |
| 414 DumpDoneCallback(NULL, NULL, NULL, info, NULL, false); | |
| 415 return EXCEPTION_EXECUTE_HANDLER; | |
| 416 } | |
| 417 | |
| 418 // NOTE: This function is used by SyzyASAN to annotate crash reports. If you | |
| 419 // change the name or signature of this function you will break SyzyASAN | |
| 420 // instrumented releases of Chrome. Please contact syzygy-team@chromium.org | |
| 421 // before doing so! | |
| 422 extern "C" void __declspec(dllexport) __cdecl SetCrashKeyValueImpl( | |
| 423 const wchar_t* key, const wchar_t* value) { | |
| 424 // CustomInfoEntry limits the length of key and value. If they exceed | |
| 425 // their maximum length the underlying string handling functions raise | |
| 426 // an exception and prematurely trigger a crash. Truncate here. | |
| 427 std::wstring safe_key(std::wstring(key).substr( | |
| 428 0, google_breakpad::CustomInfoEntry::kNameMaxLength - 1)); | |
| 429 std::wstring safe_value(std::wstring(value).substr( | |
| 430 0, google_breakpad::CustomInfoEntry::kValueMaxLength - 1)); | |
| 431 | |
| 432 // If we already have a value for this key, update it; otherwise, insert | |
| 433 // the new value if we have not exhausted the pre-allocated slots for dynamic | |
| 434 // entries. | |
| 435 DynamicEntriesMap::iterator it = g_dynamic_entries->find(safe_key); | |
| 436 google_breakpad::CustomInfoEntry* entry = NULL; | |
| 437 if (it == g_dynamic_entries->end()) { | |
| 438 if (g_dynamic_entries->size() >= kMaxDynamicEntries) | |
| 439 return; | |
| 440 entry = &(*g_custom_entries)[g_dynamic_keys_offset++]; | |
| 441 g_dynamic_entries->insert(std::make_pair(safe_key, entry)); | |
| 442 } else { | |
| 443 entry = it->second; | |
| 444 } | |
| 445 | |
| 446 entry->set(safe_key.data(), safe_value.data()); | |
| 447 } | |
| 448 | |
| 449 extern "C" void __declspec(dllexport) __cdecl ClearCrashKeyValueImpl( | |
| 450 const wchar_t* key) { | |
| 451 std::wstring key_string(key); | |
| 452 DynamicEntriesMap::iterator it = g_dynamic_entries->find(key_string); | |
| 453 if (it == g_dynamic_entries->end()) | |
| 454 return; | |
| 455 | |
| 456 it->second->set_value(NULL); | |
| 457 } | |
| 458 | |
| 459 } // namespace | |
| 460 | |
| 461 bool WrapMessageBoxWithSEH(const wchar_t* text, const wchar_t* caption, | |
| 462 UINT flags, bool* exit_now) { | |
| 463 // We wrap the call to MessageBoxW with a SEH handler because it some | |
| 464 // machines with CursorXP, PeaDict or with FontExplorer installed it crashes | |
| 465 // uncontrollably here. Being this a best effort deal we better go away. | |
| 466 __try { | |
| 467 *exit_now = (IDOK != ::MessageBoxW(NULL, text, caption, flags)); | |
| 468 } __except(EXCEPTION_EXECUTE_HANDLER) { | |
| 469 // Its not safe to continue executing, exit silently here. | |
| 470 ::TerminateProcess(::GetCurrentProcess(), | |
| 471 GetBreakpadClient()->GetResultCodeRespawnFailed()); | |
| 472 } | |
| 473 | |
| 474 return true; | |
| 475 } | |
| 476 | |
| 477 // This function is executed by the child process that DumpDoneCallback() | |
| 478 // spawned and basically just shows the 'chrome has crashed' dialog if | |
| 479 // the CHROME_CRASHED environment variable is present. | |
| 480 bool ShowRestartDialogIfCrashed(bool* exit_now) { | |
| 481 // If we are being launched in metro mode don't try to show the dialog. | |
| 482 if (base::win::IsMetroProcess()) | |
| 483 return false; | |
| 484 | |
| 485 // Only show this for the browser process. See crbug.com/132119. | |
| 486 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 487 std::string process_type = | |
| 488 command_line.GetSwitchValueASCII(switches::kProcessType); | |
| 489 if (!process_type.empty()) | |
| 490 return false; | |
| 491 | |
| 492 base::string16 message; | |
| 493 base::string16 title; | |
| 494 bool is_rtl_locale; | |
| 495 if (!GetBreakpadClient()->ShouldShowRestartDialog( | |
| 496 &title, &message, &is_rtl_locale)) { | |
| 497 return false; | |
| 498 } | |
| 499 | |
| 500 // If the UI layout is right-to-left, we need to pass the appropriate MB_XXX | |
| 501 // flags so that an RTL message box is displayed. | |
| 502 UINT flags = MB_OKCANCEL | MB_ICONWARNING; | |
| 503 if (is_rtl_locale) | |
| 504 flags |= MB_RIGHT | MB_RTLREADING; | |
| 505 | |
| 506 return WrapMessageBoxWithSEH(base::UTF16ToWide(message).c_str(), | |
| 507 base::UTF16ToWide(title).c_str(), | |
| 508 flags, | |
| 509 exit_now); | |
| 510 } | |
| 511 | |
| 512 // Crashes the process after generating a dump for the provided exception. Note | |
| 513 // that the crash reporter should be initialized before calling this function | |
| 514 // for it to do anything. | |
| 515 // NOTE: This function is used by SyzyASAN to invoke a crash. If you change the | |
| 516 // the name or signature of this function you will break SyzyASAN instrumented | |
| 517 // releases of Chrome. Please contact syzygy-team@chromium.org before doing so! | |
| 518 extern "C" int __declspec(dllexport) CrashForException( | |
| 519 EXCEPTION_POINTERS* info) { | |
| 520 if (g_breakpad) { | |
| 521 g_breakpad->WriteMinidumpForException(info); | |
| 522 // Patched stub exists based on conditions (See InitCrashReporter). | |
| 523 // As a side note this function also gets called from | |
| 524 // WindowProcExceptionFilter. | |
| 525 if (g_real_terminate_process_stub == NULL) { | |
| 526 ::TerminateProcess(::GetCurrentProcess(), content::RESULT_CODE_KILLED); | |
| 527 } else { | |
| 528 NtTerminateProcessPtr real_terminate_proc = | |
| 529 reinterpret_cast<NtTerminateProcessPtr>( | |
| 530 static_cast<char*>(g_real_terminate_process_stub)); | |
| 531 real_terminate_proc(::GetCurrentProcess(), content::RESULT_CODE_KILLED); | |
| 532 } | |
| 533 } | |
| 534 return EXCEPTION_CONTINUE_SEARCH; | |
| 535 } | |
| 536 | |
| 537 NTSTATUS WINAPI HookNtTerminateProcess(HANDLE ProcessHandle, | |
| 538 NTSTATUS ExitStatus) { | |
| 539 if (g_breakpad && | |
| 540 (ProcessHandle == ::GetCurrentProcess() || ProcessHandle == NULL)) { | |
| 541 NT_TIB* tib = reinterpret_cast<NT_TIB*>(NtCurrentTeb()); | |
| 542 void* address_on_stack = _AddressOfReturnAddress(); | |
| 543 if (address_on_stack < tib->StackLimit || | |
| 544 address_on_stack > tib->StackBase) { | |
| 545 g_surrogate_exception_record.ExceptionAddress = _ReturnAddress(); | |
| 546 g_surrogate_exception_record.ExceptionCode = DBG_TERMINATE_PROCESS; | |
| 547 g_surrogate_exception_record.ExceptionFlags = EXCEPTION_NONCONTINUABLE; | |
| 548 CrashForException(&g_surrogate_exception_pointers); | |
| 549 } | |
| 550 } | |
| 551 | |
| 552 NtTerminateProcessPtr real_proc = | |
| 553 reinterpret_cast<NtTerminateProcessPtr>( | |
| 554 static_cast<char*>(g_real_terminate_process_stub)); | |
| 555 return real_proc(ProcessHandle, ExitStatus); | |
| 556 } | |
| 557 | |
| 558 static void InitTerminateProcessHooks() { | |
| 559 NtTerminateProcessPtr terminate_process_func_address = | |
| 560 reinterpret_cast<NtTerminateProcessPtr>(::GetProcAddress( | |
| 561 ::GetModuleHandle(L"ntdll.dll"), "NtTerminateProcess")); | |
| 562 if (terminate_process_func_address == NULL) | |
| 563 return; | |
| 564 | |
| 565 DWORD old_protect = 0; | |
| 566 if (!::VirtualProtect(terminate_process_func_address, 5, | |
| 567 PAGE_EXECUTE_READWRITE, &old_protect)) | |
| 568 return; | |
| 569 | |
| 570 g_real_terminate_process_stub = reinterpret_cast<char*>(VirtualAllocEx( | |
| 571 ::GetCurrentProcess(), NULL, sidestep::kMaxPreambleStubSize, | |
| 572 MEM_COMMIT, PAGE_EXECUTE_READWRITE)); | |
| 573 if (g_real_terminate_process_stub == NULL) | |
| 574 return; | |
| 575 | |
| 576 g_surrogate_exception_pointers.ContextRecord = &g_surrogate_context; | |
| 577 g_surrogate_exception_pointers.ExceptionRecord = | |
| 578 &g_surrogate_exception_record; | |
| 579 | |
| 580 sidestep::SideStepError patch_result = | |
| 581 sidestep::PreamblePatcher::Patch( | |
| 582 terminate_process_func_address, HookNtTerminateProcess, | |
| 583 g_real_terminate_process_stub, sidestep::kMaxPreambleStubSize); | |
| 584 if (patch_result != sidestep::SIDESTEP_SUCCESS) { | |
| 585 CHECK(::VirtualFreeEx(::GetCurrentProcess(), g_real_terminate_process_stub, | |
| 586 0, MEM_RELEASE)); | |
| 587 CHECK(::VirtualProtect(terminate_process_func_address, 5, old_protect, | |
| 588 &old_protect)); | |
| 589 return; | |
| 590 } | |
| 591 | |
| 592 DWORD dummy = 0; | |
| 593 CHECK(::VirtualProtect(terminate_process_func_address, | |
| 594 5, | |
| 595 old_protect, | |
| 596 &dummy)); | |
| 597 CHECK(::VirtualProtect(g_real_terminate_process_stub, | |
| 598 sidestep::kMaxPreambleStubSize, | |
| 599 old_protect, | |
| 600 &old_protect)); | |
| 601 } | |
| 602 | |
| 603 static void InitPipeNameEnvVar(bool is_per_user_install) { | |
| 604 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 605 if (env->HasVar(kPipeNameVar)) { | |
| 606 // The Breakpad pipe name is already configured: nothing to do. | |
| 607 return; | |
| 608 } | |
| 609 | |
| 610 // Check whether configuration management controls crash reporting. | |
| 611 bool crash_reporting_enabled = true; | |
| 612 bool controlled_by_policy = GetBreakpadClient()->ReportingIsEnforcedByPolicy( | |
| 613 &crash_reporting_enabled); | |
| 614 | |
| 615 const CommandLine& command = *CommandLine::ForCurrentProcess(); | |
| 616 bool use_crash_service = | |
| 617 !controlled_by_policy && (command.HasSwitch(switches::kNoErrorDialogs) || | |
| 618 GetBreakpadClient()->IsRunningUnattended()); | |
| 619 | |
| 620 std::wstring pipe_name; | |
| 621 if (use_crash_service) { | |
| 622 // Crash reporting is done by crash_service.exe. | |
| 623 pipe_name = kChromePipeName; | |
| 624 } else { | |
| 625 // We want to use the Google Update crash reporting. We need to check if the | |
| 626 // user allows it first (in case the administrator didn't already decide | |
| 627 // via policy). | |
| 628 if (!controlled_by_policy) | |
| 629 crash_reporting_enabled = GetBreakpadClient()->GetCollectStatsConsent(); | |
| 630 | |
| 631 if (!crash_reporting_enabled) { | |
| 632 if (!controlled_by_policy && | |
| 633 GetBreakpadClient()->GetDeferredUploadsSupported( | |
| 634 is_per_user_install)) { | |
| 635 g_deferred_crash_uploads = true; | |
| 636 } else { | |
| 637 return; | |
| 638 } | |
| 639 } | |
| 640 | |
| 641 // Build the pipe name. It can be either: | |
| 642 // System-wide install: "NamedPipe\GoogleCrashServices\S-1-5-18" | |
| 643 // Per-user install: "NamedPipe\GoogleCrashServices\<user SID>" | |
| 644 std::wstring user_sid; | |
| 645 if (is_per_user_install) { | |
| 646 if (!base::win::GetUserSidString(&user_sid)) { | |
| 647 return; | |
| 648 } | |
| 649 } else { | |
| 650 user_sid = kSystemPrincipalSid; | |
| 651 } | |
| 652 | |
| 653 pipe_name = kGoogleUpdatePipeName; | |
| 654 pipe_name += user_sid; | |
| 655 } | |
| 656 env->SetVar(kPipeNameVar, WideToASCII(pipe_name)); | |
| 657 } | |
| 658 | |
| 659 void InitDefaultCrashCallback(LPTOP_LEVEL_EXCEPTION_FILTER filter) { | |
| 660 previous_filter = SetUnhandledExceptionFilter(filter); | |
| 661 } | |
| 662 | |
| 663 void InitCrashReporter() { | |
| 664 const CommandLine& command = *CommandLine::ForCurrentProcess(); | |
| 665 if (command.HasSwitch(switches::kDisableBreakpad)) | |
| 666 return; | |
| 667 | |
| 668 // Disable the message box for assertions. | |
| 669 _CrtSetReportMode(_CRT_ASSERT, 0); | |
| 670 | |
| 671 std::wstring process_type = | |
| 672 command.GetSwitchValueNative(switches::kProcessType); | |
| 673 if (process_type.empty()) | |
| 674 process_type = L"browser"; | |
| 675 | |
| 676 wchar_t exe_path[MAX_PATH]; | |
| 677 exe_path[0] = 0; | |
| 678 GetModuleFileNameW(NULL, exe_path, MAX_PATH); | |
| 679 | |
| 680 bool is_per_user_install = | |
| 681 GetBreakpadClient()->GetIsPerUserInstall(base::FilePath(exe_path)); | |
| 682 | |
| 683 google_breakpad::CustomClientInfo* custom_info = | |
| 684 GetCustomInfo(exe_path, process_type); | |
| 685 | |
| 686 google_breakpad::ExceptionHandler::MinidumpCallback callback = NULL; | |
| 687 LPTOP_LEVEL_EXCEPTION_FILTER default_filter = NULL; | |
| 688 // We install the post-dump callback only for the browser and service | |
| 689 // processes. It spawns a new browser/service process. | |
| 690 if (process_type == L"browser") { | |
| 691 callback = &DumpDoneCallback; | |
| 692 default_filter = &ChromeExceptionFilter; | |
| 693 } else if (process_type == L"service") { | |
| 694 callback = &DumpDoneCallback; | |
| 695 default_filter = &ServiceExceptionFilter; | |
| 696 } | |
| 697 | |
| 698 if (process_type == L"browser") { | |
| 699 InitPipeNameEnvVar(is_per_user_install); | |
| 700 GetBreakpadClient()->InitBrowserCrashDumpsRegKey(); | |
| 701 } | |
| 702 | |
| 703 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 704 std::string pipe_name_ascii; | |
| 705 if (!env->GetVar(kPipeNameVar, &pipe_name_ascii)) { | |
| 706 // Breakpad is not enabled. Configuration is managed or the user | |
| 707 // did not allow Google Update to send crashes. We need to use | |
| 708 // our default crash handler instead, but only for the | |
| 709 // browser/service processes. | |
| 710 if (default_filter) | |
| 711 InitDefaultCrashCallback(default_filter); | |
| 712 return; | |
| 713 } | |
| 714 std::wstring pipe_name = ASCIIToWide(pipe_name_ascii); | |
| 715 | |
| 716 #ifdef _WIN64 | |
| 717 // The protocol for connecting to the out-of-process Breakpad crash | |
| 718 // reporter is different for x86-32 and x86-64: the message sizes | |
| 719 // are different because the message struct contains a pointer. As | |
| 720 // a result, there are two different named pipes to connect to. The | |
| 721 // 64-bit one is distinguished with an "-x64" suffix. | |
| 722 pipe_name += L"-x64"; | |
| 723 #endif | |
| 724 | |
| 725 // Get the alternate dump directory. We use the temp path. | |
| 726 wchar_t temp_dir[MAX_PATH] = {0}; | |
| 727 ::GetTempPathW(MAX_PATH, temp_dir); | |
| 728 | |
| 729 MINIDUMP_TYPE dump_type = kSmallDumpType; | |
| 730 // Capture full memory if explicitly instructed to. | |
| 731 if (command.HasSwitch(switches::kFullMemoryCrashReport)) | |
| 732 dump_type = kFullDumpType; | |
| 733 else if (GetBreakpadClient()->GetShouldDumpLargerDumps(is_per_user_install)) | |
| 734 dump_type = kLargerDumpType; | |
| 735 | |
| 736 g_breakpad = new google_breakpad::ExceptionHandler(temp_dir, &FilterCallback, | |
| 737 callback, NULL, | |
| 738 google_breakpad::ExceptionHandler::HANDLER_ALL, | |
| 739 dump_type, pipe_name.c_str(), custom_info); | |
| 740 | |
| 741 // Now initialize the non crash dump handler. | |
| 742 g_dumphandler_no_crash = new google_breakpad::ExceptionHandler(temp_dir, | |
| 743 &FilterCallbackWhenNoCrash, | |
| 744 &DumpDoneCallbackWhenNoCrash, | |
| 745 NULL, | |
| 746 // Set the handler to none so this handler would not be added to | |
| 747 // |handler_stack_| in |ExceptionHandler| which is a list of exception | |
| 748 // handlers. | |
| 749 google_breakpad::ExceptionHandler::HANDLER_NONE, | |
| 750 dump_type, pipe_name.c_str(), custom_info); | |
| 751 | |
| 752 if (g_breakpad->IsOutOfProcess()) { | |
| 753 // Tells breakpad to handle breakpoint and single step exceptions. | |
| 754 // This might break JIT debuggers, but at least it will always | |
| 755 // generate a crashdump for these exceptions. | |
| 756 g_breakpad->set_handle_debug_exceptions(true); | |
| 757 | |
| 758 #ifndef _WIN64 | |
| 759 if (process_type != L"browser" && | |
| 760 !GetBreakpadClient()->IsRunningUnattended()) { | |
| 761 // Initialize the hook TerminateProcess to catch unexpected exits. | |
| 762 InitTerminateProcessHooks(); | |
| 763 } | |
| 764 #endif | |
| 765 } | |
| 766 } | |
| 767 | |
| 768 } // namespace breakpad | |
| OLD | NEW |