 Chromium Code Reviews
 Chromium Code Reviews Issue 2183263003:
  [chrome_elf] Big ELF cleanup.  Part 1.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 2183263003:
  [chrome_elf] Big ELF cleanup.  Part 1.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| OLD | NEW | 
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/crash/crash_helper.h" | 
| 6 | 6 | 
| 7 #include <assert.h> | |
| 7 #include <windows.h> | 8 #include <windows.h> | 
| 9 | |
| 8 #include <algorithm> | 10 #include <algorithm> | 
| 11 #include <string> | |
| 12 #include <vector> | |
| 9 | 13 | 
| 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" | 14 #include "chrome/app/chrome_crash_reporter_client_win.h" | 
| 15 #include "chrome/install_static/install_util.h" | 15 #include "chrome_elf/hook_util/hook_util.h" | 
| 16 #include "chrome_elf/blacklist/blacklist.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" | 16 #include "components/crash/content/app/crashpad.h" | 
| 20 #include "components/crash/core/common/crash_keys.h" | 17 #include "components/crash/core/common/crash_keys.h" | 
| 18 #include "third_party/crashpad/crashpad/client/crashpad_client.h" | |
| 21 | 19 | 
| 22 namespace { | 20 namespace { | 
| 23 | 21 | 
| 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. | 22 // Gets the exe name from the full path of the exe. | 
| 28 base::string16 GetExeName() { | 23 std::wstring GetExeName() { | 
| 29 wchar_t file_path[MAX_PATH] = {}; | 24 wchar_t file_path[MAX_PATH] = {}; | 
| 30 if (!::GetModuleFileName(nullptr, file_path, arraysize(file_path))) { | 25 if (!::GetModuleFileNameW(nullptr, file_path, MAX_PATH)) { | 
| 31 assert(false); | 26 assert(false); | 
| 32 return base::string16(); | 27 return std::wstring(); | 
| 33 } | 28 } | 
| 34 base::string16 file_name_string = file_path; | 29 std::wstring file_name_string = file_path; | 
| 
robertshield
2016/08/02 02:38:40
You can save a copy here like this:
std::wstring
 
penny
2016/08/02 21:07:43
Done.
 | |
| 35 size_t last_slash_pos = file_name_string.find_last_of(L'\\'); | 30 size_t last_slash_pos = file_name_string.find_last_of(L'\\'); | 
| 36 if (last_slash_pos != base::string16::npos) { | 31 if (last_slash_pos != std::wstring::npos) { | 
| 37 file_name_string = file_name_string.substr( | 32 file_name_string = file_name_string.substr( | 
| 38 last_slash_pos + 1, file_name_string.length() - last_slash_pos); | 33 last_slash_pos + 1, file_name_string.length() - last_slash_pos); | 
| 39 } | 34 } | 
| 40 std::transform(file_name_string.begin(), file_name_string.end(), | 35 std::transform(file_name_string.begin(), file_name_string.end(), | 
| 41 file_name_string.begin(), ::tolower); | 36 file_name_string.begin(), ::tolower); | 
| 42 return file_name_string; | 37 return file_name_string; | 
| 43 } | 38 } | 
| 44 | 39 | 
| 45 void InitializeCrashReportingForProcess() { | 40 // Global pointer to a vector of crash reports. | 
| 46 // We want to initialize crash reporting only in chrome.exe | 41 // This structure will be initialized in InitializeCrashReportingForProcess() | 
| 47 if (GetExeName() != L"chrome.exe") | 42 // and cleaned up in DllDetachCrashReportingCleanup(). | 
| 48 return; | 43 std::vector<crash_reporter::Report>* g_crash_reports = nullptr; | 
| 49 ChromeCrashReporterClient::InitializeCrashReportingForProcess(); | |
| 50 } | |
| 51 | 44 | 
| 52 // chrome_elf loads early in the process and initializes Crashpad. That in turn | 45 // chrome_elf loads early in the process and initializes Crashpad. That in turn | 
| 53 // uses the SetUnhandledExceptionFilter API to set a top level exception | 46 // uses the SetUnhandledExceptionFilter API to set a top level exception | 
| 54 // handler for the process. When the process eventually initializes, CRT sets | 47 // handler for the process. When the process eventually initializes, CRT sets | 
| 55 // an exception handler which calls TerminateProcess which effectively bypasses | 48 // an exception handler which calls TerminateProcess which effectively bypasses | 
| 56 // us. Ideally we want to be at the top of the unhandled exception filter | 49 // 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 | 50 // chain. However we don't have a good way of intercepting the | 
| 58 // SetUnhandledExceptionFilter API in the sandbox. EAT patching kernel32 or | 51 // SetUnhandledExceptionFilter API in the sandbox. EAT patching kernel32 or | 
| 59 // kernelbase should ideally work. However the kernel32 kernelbase dlls are | 52 // kernelbase should ideally work. However the kernel32 kernelbase dlls are | 
| 60 // prebound which causes EAT patching to not work. Sidestep works. However it | 53 // 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 | 54 // is only supported for 32 bit. For now we use IAT patching for the | 
| 62 // executable. | 55 // executable. | 
| 63 // TODO(ananta). | 56 // TODO(ananta). | 
| 64 // Check if it is possible to fix EAT patching or use sidestep patching for | 57 // Check if it is possible to fix EAT patching or use sidestep patching for | 
| 65 // 32 bit and 64 bit for this purpose. | 58 // 32 bit and 64 bit for this purpose. | 
| 66 base::win::IATPatchFunction g_set_unhandled_exception_filter; | 59 elf_hook::IATHook g_set_unhandled_exception_filter; | 
| 67 | 60 | 
| 61 // Hook function, which ignores the request to set an unhandled-exception | |
| 62 // filter. | |
| 68 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI | 63 LPTOP_LEVEL_EXCEPTION_FILTER WINAPI | 
| 69 SetUnhandledExceptionFilterPatch(LPTOP_LEVEL_EXCEPTION_FILTER filter) { | 64 SetUnhandledExceptionFilterPatch(LPTOP_LEVEL_EXCEPTION_FILTER filter) { | 
| 70 // Don't set the exception filter. Please see above for comments. | 65 // Don't set the exception filter. Please see above for comments. | 
| 71 return nullptr; | 66 return nullptr; | 
| 72 } | 67 } | 
| 73 | 68 | 
| 69 } // namespace | |
| 70 | |
| 71 //------------------------------------------------------------------------------ | |
| 72 // Public chrome_elf crash APIs | |
| 73 //------------------------------------------------------------------------------ | |
| 74 | |
| 75 namespace elf_crash { | |
| 76 | |
| 77 void InitializeCrashReportingForProcess() { | |
| 78 // We want to initialize crash reporting only in chrome.exe | |
| 79 if (GetExeName() != L"chrome.exe") { | |
| 80 #ifdef _DEBUG | |
| 81 assert(false); | |
| 82 #endif // _DEBUG | |
| 83 return; | |
| 84 } | |
| 85 | |
| 
robertshield
2016/08/02 02:38:40
Could you also assert in debug mode that g_crash_r
 
penny
2016/08/02 21:07:43
Done.
 | |
| 86 // No global objects with destructors, so using a global pointer. | |
| 87 // DllMain on detach will clean this up. | |
| 88 g_crash_reports = new std::vector<crash_reporter::Report>; | |
| 89 | |
| 90 ChromeCrashReporterClient::InitializeCrashReportingForProcess(); | |
| 91 } | |
| 92 | |
| 93 // NOTE: This function will be called from DllMain during DLL_PROCESS_DETACH | |
| 94 // (while we have the loader lock), so do not misbehave. | |
| 
robertshield
2016/08/02 02:38:40
This comment also applies to InitializeCrashReport
 
penny
2016/08/02 21:07:43
Done.
 | |
| 95 void DllDetachCrashReportingCleanup() { | |
| 96 if (g_crash_reports != nullptr) { | |
| 97 g_crash_reports->clear(); | |
| 98 delete g_crash_reports; | |
| 99 } | |
| 100 } | |
| 101 | |
| 74 // Please refer above to more information about why we intercept the | 102 // Please refer above to more information about why we intercept the | 
| 
robertshield
2016/08/02 02:38:40
specify to refer to the comment on g_set_unhandled
 
penny
2016/08/02 21:07:43
Done.
 | |
| 75 // SetUnhandledExceptionFilter API. | 103 // SetUnhandledExceptionFilter API. | 
| 76 void DisableSetUnhandledExceptionFilter() { | 104 void DisableSetUnhandledExceptionFilter() { | 
| 77 DWORD patched = g_set_unhandled_exception_filter.PatchFromModule( | 105 if (g_set_unhandled_exception_filter.Hook( | 
| 78 GetModuleHandle(nullptr), "kernel32.dll", "SetUnhandledExceptionFilter", | 106 GetModuleHandle(nullptr), "kernel32.dll", | 
| 79 SetUnhandledExceptionFilterPatch); | 107 "SetUnhandledExceptionFilter", | 
| 80 CHECK(patched == 0); | 108 SetUnhandledExceptionFilterPatch) != NO_ERROR) { | 
| 109 #ifdef _DEBUG | |
| 110 assert(false); | |
| 111 #endif //_DEBUG | |
| 112 } | |
| 81 } | 113 } | 
| 82 | 114 | 
| 83 } // namespace | 115 int GenerateCrashDump(EXCEPTION_POINTERS* exception_pointers) { | 
| 84 | 116 crashpad::CrashpadClient::DumpWithoutCrash( | 
| 85 void SignalChromeElf() { | 117 *(exception_pointers->ContextRecord)); | 
| 86 blacklist::ResetBeacon(); | 118 return EXCEPTION_CONTINUE_SEARCH; | 
| 87 } | 119 } | 
| 88 | 120 | 
| 121 } // namespace elf_crash | |
| 122 | |
| 123 //------------------------------------------------------------------------------ | |
| 124 // Exported crash APIs for the rest of the process. | |
| 125 //------------------------------------------------------------------------------ | |
| 126 | |
| 89 // This helper is invoked by code in chrome.dll to retrieve the crash reports. | 127 // This helper is invoked by code in chrome.dll to retrieve the crash reports. | 
| 90 // See CrashUploadListCrashpad. Note that we do not pass an std::vector here, | 128 // See CrashUploadListCrashpad. Note that we do not pass a std::vector here, | 
| 91 // because we do not want to allocate/free in different modules. The returned | 129 // because we do not want to allocate/free in different modules. The returned | 
| 92 // pointer is read-only. | 130 // pointer is read-only. | 
| 93 extern "C" __declspec(dllexport) void GetCrashReportsImpl( | 131 extern "C" __declspec(dllexport) void GetCrashReportsImpl( | 
| 94 const crash_reporter::Report** reports, | 132 const crash_reporter::Report** reports, | 
| 95 size_t* report_count) { | 133 size_t* report_count) { | 
| 96 crash_reporter::GetReports(g_crash_reports.Pointer()); | 134 crash_reporter::GetReports(g_crash_reports); | 
| 97 *reports = g_crash_reports.Pointer()->data(); | 135 *reports = g_crash_reports->data(); | 
| 98 *report_count = g_crash_reports.Pointer()->size(); | 136 *report_count = g_crash_reports->size(); | 
| 99 } | 137 } | 
| 100 | 138 | 
| 101 // This helper is invoked by debugging code in chrome to register the client | 139 // This helper is invoked by debugging code in chrome to register the client | 
| 102 // id. | 140 // id. | 
| 103 extern "C" __declspec(dllexport) void SetMetricsClientId( | 141 extern "C" __declspec(dllexport) void SetMetricsClientId( | 
| 104 const char* client_id) { | 142 const char* client_id) { | 
| 105 if (client_id) | 143 if (client_id) | 
| 106 crash_keys::SetMetricsClientIdFromGUID(client_id); | 144 crash_keys::SetMetricsClientIdFromGUID(client_id); | 
| 107 } | 145 } | 
| 108 | |
| 109 BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved) { | |
| 110 if (reason == DLL_PROCESS_ATTACH) { | |
| 111 InitializeCrashReportingForProcess(); | |
| 112 // CRT on initialization installs an exception filter which calls | |
| 113 // TerminateProcess. We need to hook CRT's attempt to set an exception | |
| 114 // handler and ignore it. | |
| 115 DisableSetUnhandledExceptionFilter(); | |
| 116 | |
| 117 install_static::InitializeProcessType(); | |
| 118 | |
| 119 __try { | |
| 120 blacklist::Initialize(false); // Don't force, abort if beacon is present. | |
| 121 } __except(GenerateCrashDump(GetExceptionInformation())) { | |
| 122 } | |
| 123 } | |
| 124 return TRUE; | |
| 125 } | |
| OLD | NEW |