[email protected] | 91f0755 | 2013-11-13 10:01:01 | [diff] [blame] | 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 | |
[email protected] | 91f0755 | 2013-11-13 10:01:01 | [diff] [blame] | 5 | #include "chrome_elf/chrome_elf_main.h" |
| 6 | |
ananta | 0a700e2 | 2016-04-21 03:00:46 | [diff] [blame] | 7 | #include <windows.h> |
avi | 4e989fa | 2016-08-15 20:08:21 | [diff] [blame] | 8 | #include <algorithm> |
ananta | 0a700e2 | 2016-04-21 03:00:46 | [diff] [blame] | 9 | |
avi | 4e989fa | 2016-08-15 20:08:21 | [diff] [blame] | 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" |
ananta | 0a700e2 | 2016-04-21 03:00:46 | [diff] [blame] | 15 | #include "chrome/install_static/install_util.h" |
[email protected] | cab3087 | 2013-12-19 23:15:59 | [diff] [blame] | 16 | #include "chrome_elf/blacklist/blacklist.h" |
avi | 4e989fa | 2016-08-15 20:08:21 | [diff] [blame] | 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" |
| 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 | #if !defined(ADDRESS_SANITIZER) |
| 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 | #endif // !defined(ADDRESS_SANITIZER) |
| 84 | |
| 85 | } // namespace |
[email protected] | 8bbb666 | 2013-12-09 06:15:24 | [diff] [blame] | 86 | |
[email protected] | cab3087 | 2013-12-19 23:15:59 | [diff] [blame] | 87 | void SignalChromeElf() { |
[email protected] | a461f91 | 2014-01-13 16:57:04 | [diff] [blame] | 88 | blacklist::ResetBeacon(); |
[email protected] | 91f0755 | 2013-11-13 10:01:01 | [diff] [blame] | 89 | } |
| 90 | |
avi | 4e989fa | 2016-08-15 20:08:21 | [diff] [blame] | 91 | // This helper is invoked by code in chrome.dll to retrieve the crash reports. |
| 92 | // See CrashUploadListCrashpad. Note that we do not pass an std::vector here, |
| 93 | // because we do not want to allocate/free in different modules. The returned |
| 94 | // pointer is read-only. |
| 95 | extern "C" __declspec(dllexport) void GetCrashReportsImpl( |
| 96 | const crash_reporter::Report** reports, |
| 97 | size_t* report_count) { |
| 98 | crash_reporter::GetReports(g_crash_reports.Pointer()); |
| 99 | *reports = g_crash_reports.Pointer()->data(); |
| 100 | *report_count = g_crash_reports.Pointer()->size(); |
| 101 | } |
| 102 | |
| 103 | // This helper is invoked by debugging code in chrome to register the client |
| 104 | // id. |
| 105 | extern "C" __declspec(dllexport) void SetMetricsClientId( |
| 106 | const char* client_id) { |
| 107 | if (client_id) |
| 108 | crash_keys::SetMetricsClientIdFromGUID(client_id); |
| 109 | } |
| 110 | |
[email protected] | 91f0755 | 2013-11-13 10:01:01 | [diff] [blame] | 111 | BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { |
[email protected] | cab3087 | 2013-12-19 23:15:59 | [diff] [blame] | 112 | if (reason == DLL_PROCESS_ATTACH) { |
avi | 4e989fa | 2016-08-15 20:08:21 | [diff] [blame] | 113 | InitializeCrashReportingForProcess(); |
| 114 | // CRT on initialization installs an exception filter which calls |
| 115 | // TerminateProcess. We need to hook CRT's attempt to set an exception |
| 116 | // handler and ignore it. Don't do this when ASan is present, or ASan will |
| 117 | // fail to install its own unhandled exception filter. |
inferno | 009fe04 | 2016-08-06 00:46:48 | [diff] [blame] | 118 | #if !defined(ADDRESS_SANITIZER) |
avi | 4e989fa | 2016-08-15 20:08:21 | [diff] [blame] | 119 | DisableSetUnhandledExceptionFilter(); |
| 120 | #endif |
ananta | 52c55bd | 2016-07-19 22:40:53 | [diff] [blame] | 121 | |
ananta | 69086d7 | 2016-05-12 23:29:04 | [diff] [blame] | 122 | install_static::InitializeProcessType(); |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 123 | |
| 124 | __try { |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 125 | blacklist::Initialize(false); // Don't force, abort if beacon is present. |
avi | 4e989fa | 2016-08-15 20:08:21 | [diff] [blame] | 126 | } __except(GenerateCrashDump(GetExceptionInformation())) { |
[email protected] | 5a54207 | 2014-02-24 02:12:09 | [diff] [blame] | 127 | } |
[email protected] | cab3087 | 2013-12-19 23:15:59 | [diff] [blame] | 128 | } |
[email protected] | 91f0755 | 2013-11-13 10:01:01 | [diff] [blame] | 129 | return TRUE; |
| 130 | } |