Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome_elf/chrome_elf_main.h" | 5 #include "chrome_elf/chrome_elf_main.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <algorithm> | |
| 8 | 9 |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/strings/string16.h" | |
| 12 #include "base/win/iat_patch_function.h" | |
| 13 #include "build/build_config.h" | |
| 14 #include "chrome/app/chrome_crash_reporter_client_win.h" | |
| 9 #include "chrome/install_static/install_util.h" | 15 #include "chrome/install_static/install_util.h" |
| 10 #include "chrome_elf/blacklist/blacklist.h" | 16 #include "chrome_elf/blacklist/blacklist.h" |
| 11 #include "chrome_elf/breakpad.h" | 17 #include "chrome_elf/blacklist/crashpad_helper.h" |
| 18 #include "chrome_elf/chrome_elf_constants.h" | |
| 19 #include "components/crash/content/app/crashpad.h" | |
| 20 #include "components/crash/core/common/crash_keys.h" | |
| 12 | 21 |
| 22 namespace { | |
| 23 | |
| 24 base::LazyInstance<std::vector<crash_reporter::Report>>::Leaky g_crash_reports = | |
| 25 LAZY_INSTANCE_INITIALIZER; | |
| 26 | |
| 27 // Gets the exe name from the full path of the exe. | |
| 28 base::string16 GetExeName() { | |
| 29 wchar_t file_path[MAX_PATH] = {}; | |
| 30 if (!::GetModuleFileName(nullptr, file_path, arraysize(file_path))) { | |
| 31 assert(false); | |
| 32 return base::string16(); | |
| 33 } | |
| 34 base::string16 file_name_string = file_path; | |
| 35 size_t last_slash_pos = file_name_string.find_last_of(L'\\'); | |
| 36 if (last_slash_pos != base::string16::npos) { | |
| 37 file_name_string = file_name_string.substr( | |
| 38 last_slash_pos + 1, file_name_string.length() - last_slash_pos); | |
| 39 } | |
| 40 std::transform(file_name_string.begin(), file_name_string.end(), | |
| 41 file_name_string.begin(), ::tolower); | |
| 42 return file_name_string; | |
| 43 } | |
| 44 | |
| 45 void InitializeCrashReportingForProcess() { | |
| 46 // We want to initialize crash reporting only in chrome.exe | |
| 47 if (GetExeName() != L"chrome.exe") | |
| 48 return; | |
| 49 ChromeCrashReporterClient::InitializeCrashReportingForProcess(); | |
| 50 } | |
| 51 | |
| 52 // chrome_elf loads early in the process and initializes CrashPad. That in turn | |
|
scottmg
2016/06/27 19:37:46
CrashPad -> Crashpad
ananta
2016/06/27 20:00:27
Done.
| |
| 53 // uses the SetUnhandledExceptionFilter API to set a top level exception | |
| 54 // handler for the process. When the process eventually initializes, CRT sets | |
| 55 // an exception handler which calls TerminateProcess which effectively bypasses | |
| 56 // us. Ideally we want to be at the top of the unhandled exception filter | |
| 57 // chain. However we don't have a good way of intercepting the | |
| 58 // SetUnhandledExceptionFilter API in the sandbox. EAT patching kernel32 or | |
| 59 // kernelbase should ideally work. However the kernel32 kernelbase dlls are | |
| 60 // prebound which causes EAT patching to not work. Sidestep works. However it | |
| 61 // is only supported for 32 bit. For now we use IAT patching for the | |
| 62 // executable. | |
| 63 // TODO(ananta). | |
| 64 // Check if it is possible to fix EAT patching or use sidestep patching for | |
| 65 // 32 bit and 64 bit for this purpose. | |
| 66 base::win::IATPatchFunction g_set_unhandled_exception_filter; | |
| 67 | |
| 68 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI | |
| 69 SetUnhandledExceptionFilterPatch(LPTOP_LEVEL_EXCEPTION_FILTER filter) { | |
| 70 // Don't set the exception filter. Please see above for comments. | |
|
scottmg
2016/06/27 19:37:46
Add ",crashpad:106" to the BUG= line, since you fi
ananta
2016/06/27 20:00:26
Done.
| |
| 71 return nullptr; | |
| 72 } | |
| 73 | |
| 74 // Please refer above to more information about why we intercept the | |
| 75 // SetUnhandledExceptionFilter API. | |
| 76 bool DisableSetUnhandledExceptionFilter() { | |
| 77 DWORD patched = g_set_unhandled_exception_filter.PatchFromModule( | |
| 78 GetModuleHandle(nullptr), "kernel32.dll", "SetUnhandledExceptionFilter", | |
| 79 SetUnhandledExceptionFilterPatch); | |
| 80 assert(patched == 0); | |
| 81 return patched == 0; | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 13 | 85 |
| 14 void SignalChromeElf() { | 86 void SignalChromeElf() { |
| 15 blacklist::ResetBeacon(); | 87 blacklist::ResetBeacon(); |
| 16 } | 88 } |
| 17 | 89 |
| 90 // This helper is invoked by code in chrome.dll to retrieve the crash reports. | |
| 91 // See CrashUploadListCrashpad. Note that we do not pass an std::vector here, | |
| 92 // because we do not want to allocate/free in different modules. The returned | |
| 93 // pointer is read-only. | |
| 94 extern "C" __declspec(dllexport) void GetCrashReportsImpl( | |
| 95 const crash_reporter::Report** reports, | |
| 96 size_t* report_count) { | |
| 97 crash_reporter::GetReports(g_crash_reports.Pointer()); | |
| 98 *reports = g_crash_reports.Pointer()->data(); | |
| 99 *report_count = g_crash_reports.Pointer()->size(); | |
| 100 } | |
| 101 | |
| 102 // This helper is invoked by debugging code in chrome to register crash keys | |
| 103 // for chrome_elf. We need this now because chrome_elf and chrome.exe and other | |
| 104 // executables have their own copies of base. | |
| 105 // TODO(ananta) | |
| 106 // Remove this when the change to not require crash key registration lands. | |
| 107 extern "C" __declspec(dllexport) void RegisterCrashKeysImpl( | |
| 108 const char* client_id) { | |
| 109 ChromeCrashReporterClient::RegisterCrashKeysForDebugging(); | |
| 110 if (client_id) | |
| 111 crash_keys::SetMetricsClientIdFromGUID(client_id); | |
| 112 } | |
| 113 | |
| 18 BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { | 114 BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { |
| 19 if (reason == DLL_PROCESS_ATTACH) { | 115 if (reason == DLL_PROCESS_ATTACH) { |
| 116 InitializeCrashReportingForProcess(); | |
| 20 install_static::InitializeProcessType(); | 117 install_static::InitializeProcessType(); |
| 21 InitializeCrashReporting(); | |
| 22 | 118 |
| 23 __try { | 119 __try { |
| 24 blacklist::Initialize(false); // Don't force, abort if beacon is present. | 120 blacklist::Initialize(false); // Don't force, abort if beacon is present. |
| 121 DisableSetUnhandledExceptionFilter(); | |
| 25 } __except(GenerateCrashDump(GetExceptionInformation())) { | 122 } __except(GenerateCrashDump(GetExceptionInformation())) { |
| 26 } | 123 } |
| 27 } | 124 } |
| 28 | |
| 29 return TRUE; | 125 return TRUE; |
| 30 } | 126 } |
| OLD | NEW |