| 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> | |
| 9 | 8 |
| 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" | |
| 15 #include "chrome/install_static/install_util.h" | 9 #include "chrome/install_static/install_util.h" |
| 16 #include "chrome_elf/blacklist/blacklist.h" | 10 #include "chrome_elf/blacklist/blacklist.h" |
| 17 #include "chrome_elf/blacklist/crashpad_helper.h" | |
| 18 #include "chrome_elf/chrome_elf_constants.h" | |
| 19 #include "chrome_elf/chrome_elf_security.h" | 11 #include "chrome_elf/chrome_elf_security.h" |
| 20 #include "components/crash/content/app/crashpad.h" | 12 #include "chrome_elf/crash/crash_helper.h" |
| 21 #include "components/crash/core/common/crash_keys.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 base::LazyInstance<std::vector<crash_reporter::Report>>::Leaky g_crash_reports = | |
| 26 LAZY_INSTANCE_INITIALIZER; | |
| 27 | |
| 28 // Gets the exe name from the full path of the exe. | |
| 29 base::string16 GetExeName() { | |
| 30 wchar_t file_path[MAX_PATH] = {}; | |
| 31 if (!::GetModuleFileName(nullptr, file_path, arraysize(file_path))) { | |
| 32 assert(false); | |
| 33 return base::string16(); | |
| 34 } | |
| 35 base::string16 file_name_string = file_path; | |
| 36 size_t last_slash_pos = file_name_string.find_last_of(L'\\'); | |
| 37 if (last_slash_pos != base::string16::npos) { | |
| 38 file_name_string = file_name_string.substr( | |
| 39 last_slash_pos + 1, file_name_string.length() - last_slash_pos); | |
| 40 } | |
| 41 std::transform(file_name_string.begin(), file_name_string.end(), | |
| 42 file_name_string.begin(), ::tolower); | |
| 43 return file_name_string; | |
| 44 } | |
| 45 | |
| 46 void InitializeCrashReportingForProcess() { | |
| 47 // We want to initialize crash reporting only in chrome.exe | |
| 48 if (GetExeName() != L"chrome.exe") | |
| 49 return; | |
| 50 ChromeCrashReporterClient::InitializeCrashReportingForProcess(); | |
| 51 } | |
| 52 | |
| 53 // chrome_elf loads early in the process and initializes Crashpad. That in turn | |
| 54 // uses the SetUnhandledExceptionFilter API to set a top level exception | |
| 55 // handler for the process. When the process eventually initializes, CRT sets | |
| 56 // an exception handler which calls TerminateProcess which effectively bypasses | |
| 57 // us. Ideally we want to be at the top of the unhandled exception filter | |
| 58 // chain. However we don't have a good way of intercepting the | |
| 59 // SetUnhandledExceptionFilter API in the sandbox. EAT patching kernel32 or | |
| 60 // kernelbase should ideally work. However the kernel32 kernelbase dlls are | |
| 61 // prebound which causes EAT patching to not work. Sidestep works. However it | |
| 62 // is only supported for 32 bit. For now we use IAT patching for the | |
| 63 // executable. | |
| 64 // TODO(ananta). | |
| 65 // Check if it is possible to fix EAT patching or use sidestep patching for | |
| 66 // 32 bit and 64 bit for this purpose. | |
| 67 base::win::IATPatchFunction g_set_unhandled_exception_filter; | |
| 68 | |
| 69 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI | |
| 70 SetUnhandledExceptionFilterPatch(LPTOP_LEVEL_EXCEPTION_FILTER filter) { | |
| 71 // Don't set the exception filter. Please see above for comments. | |
| 72 return nullptr; | |
| 73 } | |
| 74 | |
| 75 // Please refer above to more information about why we intercept the | |
| 76 // SetUnhandledExceptionFilter API. | |
| 77 void DisableSetUnhandledExceptionFilter() { | |
| 78 DWORD patched = g_set_unhandled_exception_filter.PatchFromModule( | |
| 79 GetModuleHandle(nullptr), "kernel32.dll", "SetUnhandledExceptionFilter", | |
| 80 SetUnhandledExceptionFilterPatch); | |
| 81 CHECK(patched == 0); | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | 13 |
| 86 void SignalChromeElf() { | 14 void SignalChromeElf() { |
| 87 blacklist::ResetBeacon(); | 15 blacklist::ResetBeacon(); |
| 88 } | 16 } |
| 89 | 17 |
| 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 the client | |
| 103 // id. | |
| 104 extern "C" __declspec(dllexport) void SetMetricsClientId( | |
| 105 const char* client_id) { | |
| 106 if (client_id) | |
| 107 crash_keys::SetMetricsClientIdFromGUID(client_id); | |
| 108 } | |
| 109 | |
| 110 BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { | 18 BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { |
| 111 if (reason == DLL_PROCESS_ATTACH) { | 19 if (reason == DLL_PROCESS_ATTACH) { |
| 112 InitializeCrashReportingForProcess(); | 20 elf_crash::InitializeCrashReporting(); |
| 113 // CRT on initialization installs an exception filter which calls | 21 // CRT on initialization installs an exception filter which calls |
| 114 // TerminateProcess. We need to hook CRT's attempt to set an exception | 22 // TerminateProcess. We need to hook CRT's attempt to set an exception |
| 115 // handler and ignore it. | 23 // handler and ignore it. |
| 116 DisableSetUnhandledExceptionFilter(); | 24 elf_crash::DisableSetUnhandledExceptionFilter(); |
| 117 | 25 |
| 118 install_static::InitializeProcessType(); | 26 install_static::InitializeProcessType(); |
| 119 if (install_static::g_process_type == | 27 if (install_static::g_process_type == |
| 120 install_static::ProcessType::BROWSER_PROCESS) | 28 install_static::ProcessType::BROWSER_PROCESS) |
| 121 EarlyBrowserSecurity(); | 29 elf_security::EarlyBrowserSecurity(); |
| 122 | 30 |
| 123 __try { | 31 __try { |
| 124 blacklist::Initialize(false); // Don't force, abort if beacon is present. | 32 blacklist::Initialize(false); // Don't force, abort if beacon is present. |
| 125 } __except(GenerateCrashDump(GetExceptionInformation())) { | 33 } __except (elf_crash::GenerateCrashDump(GetExceptionInformation())) { |
| 126 } | 34 } |
| 35 } else if (reason == DLL_PROCESS_DETACH) { |
| 36 elf_crash::ShutdownCrashReporting(); |
| 127 } | 37 } |
| 128 return TRUE; | 38 return TRUE; |
| 129 } | 39 } |
| OLD | NEW |