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_elf/crash/crash_helper.h" |
| 6 |
| 7 #include <assert.h> |
| 8 #include <windows.h> |
| 9 |
| 10 #include <algorithm> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "chrome/app/chrome_crash_reporter_client_win.h" |
| 15 #include "chrome_elf/hook_util/hook_util.h" |
| 16 #include "components/crash/content/app/crashpad.h" |
| 17 #include "components/crash/core/common/crash_keys.h" |
| 18 #include "third_party/crashpad/crashpad/client/crashpad_client.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Crash handling from elf is only enabled for the chrome.exe process. |
| 23 // Use this global to safely handle the rare case where elf may not be in that |
| 24 // process (e.g. tests). |
| 25 bool g_crash_helper_enabled = false; |
| 26 |
| 27 // Global pointer to a vector of crash reports. |
| 28 // This structure will be initialized in InitializeCrashReportingForProcess() |
| 29 // and cleaned up in DllDetachCrashReportingCleanup(). |
| 30 std::vector<crash_reporter::Report>* g_crash_reports = nullptr; |
| 31 |
| 32 // chrome_elf loads early in the process and initializes Crashpad. That in turn |
| 33 // uses the SetUnhandledExceptionFilter API to set a top level exception |
| 34 // handler for the process. When the process eventually initializes, CRT sets |
| 35 // an exception handler which calls TerminateProcess which effectively bypasses |
| 36 // us. Ideally we want to be at the top of the unhandled exception filter |
| 37 // chain. However we don't have a good way of intercepting the |
| 38 // SetUnhandledExceptionFilter API in the sandbox. EAT patching kernel32 or |
| 39 // kernelbase should ideally work. However the kernel32 kernelbase dlls are |
| 40 // prebound which causes EAT patching to not work. Sidestep works. However it |
| 41 // is only supported for 32 bit. For now we use IAT patching for the |
| 42 // executable. |
| 43 // TODO(ananta). |
| 44 // Check if it is possible to fix EAT patching or use sidestep patching for |
| 45 // 32 bit and 64 bit for this purpose. |
| 46 elf_hook::IATHook* g_set_unhandled_exception_filter = nullptr; |
| 47 |
| 48 // Hook function, which ignores the request to set an unhandled-exception |
| 49 // filter. |
| 50 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI |
| 51 SetUnhandledExceptionFilterPatch(LPTOP_LEVEL_EXCEPTION_FILTER filter) { |
| 52 // Don't set the exception filter. Please see above for comments. |
| 53 return nullptr; |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
| 58 //------------------------------------------------------------------------------ |
| 59 // Public chrome_elf crash APIs |
| 60 //------------------------------------------------------------------------------ |
| 61 |
| 62 namespace elf_crash { |
| 63 |
| 64 // NOTE: This function will be called from DllMain during DLL_PROCESS_ATTACH |
| 65 // (while we have the loader lock), so do not misbehave. |
| 66 bool InitializeCrashReporting() { |
| 67 #ifdef _DEBUG |
| 68 assert(g_crash_reports == nullptr); |
| 69 assert(g_set_unhandled_exception_filter == nullptr); |
| 70 #endif // _DEBUG |
| 71 |
| 72 // No global objects with destructors, so using global pointers. |
| 73 // DllMain on detach will clean these up. |
| 74 g_crash_reports = new std::vector<crash_reporter::Report>; |
| 75 g_set_unhandled_exception_filter = new elf_hook::IATHook(); |
| 76 |
| 77 ChromeCrashReporterClient::InitializeCrashReportingForProcess(); |
| 78 |
| 79 g_crash_helper_enabled = true; |
| 80 return true; |
| 81 } |
| 82 |
| 83 // NOTE: This function will be called from DllMain during DLL_PROCESS_DETACH |
| 84 // (while we have the loader lock), so do not misbehave. |
| 85 void ShutdownCrashReporting() { |
| 86 if (g_crash_reports != nullptr) { |
| 87 g_crash_reports->clear(); |
| 88 delete g_crash_reports; |
| 89 } |
| 90 if (g_set_unhandled_exception_filter != nullptr) { |
| 91 delete g_set_unhandled_exception_filter; |
| 92 } |
| 93 } |
| 94 |
| 95 // Please refer to the comment on g_set_unhandled_exception_filter for more |
| 96 // information about why we intercept the SetUnhandledExceptionFilter API. |
| 97 void DisableSetUnhandledExceptionFilter() { |
| 98 if (!g_crash_helper_enabled) |
| 99 return; |
| 100 if (g_set_unhandled_exception_filter->Hook( |
| 101 ::GetModuleHandle(nullptr), "kernel32.dll", |
| 102 "SetUnhandledExceptionFilter", |
| 103 SetUnhandledExceptionFilterPatch) != NO_ERROR) { |
| 104 #ifdef _DEBUG |
| 105 assert(false); |
| 106 #endif //_DEBUG |
| 107 } |
| 108 } |
| 109 |
| 110 int GenerateCrashDump(EXCEPTION_POINTERS* exception_pointers) { |
| 111 if (g_crash_helper_enabled) |
| 112 crashpad::CrashpadClient::DumpWithoutCrash( |
| 113 *(exception_pointers->ContextRecord)); |
| 114 return EXCEPTION_CONTINUE_SEARCH; |
| 115 } |
| 116 |
| 117 } // namespace elf_crash |
| 118 |
| 119 //------------------------------------------------------------------------------ |
| 120 // Exported crash APIs for the rest of the process. |
| 121 //------------------------------------------------------------------------------ |
| 122 |
| 123 // This helper is invoked by code in chrome.dll to retrieve the crash reports. |
| 124 // See CrashUploadListCrashpad. Note that we do not pass a std::vector here, |
| 125 // because we do not want to allocate/free in different modules. The returned |
| 126 // pointer is read-only. |
| 127 // |
| 128 // NOTE: Since the returned pointer references read-only memory that will be |
| 129 // cleaned up when this DLL unloads, be careful not to reference the memory |
| 130 // beyond that point (e.g. during tests). |
| 131 extern "C" __declspec(dllexport) void GetCrashReportsImpl( |
| 132 const crash_reporter::Report** reports, |
| 133 size_t* report_count) { |
| 134 if (!g_crash_helper_enabled) |
| 135 return; |
| 136 crash_reporter::GetReports(g_crash_reports); |
| 137 *reports = g_crash_reports->data(); |
| 138 *report_count = g_crash_reports->size(); |
| 139 } |
| 140 |
| 141 // This helper is invoked by debugging code in chrome to register the client |
| 142 // id. |
| 143 extern "C" __declspec(dllexport) void SetMetricsClientId( |
| 144 const char* client_id) { |
| 145 if (!g_crash_helper_enabled) |
| 146 return; |
| 147 if (client_id) |
| 148 crash_keys::SetMetricsClientIdFromGUID(client_id); |
| 149 } |
OLD | NEW |