Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: chrome_elf/crash/crash_helper.cc

Issue 2183263003: [chrome_elf] Big ELF cleanup. Part 1. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix up tests for no chrash handling. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Gets the exe name from the full path of the exe.
28 std::wstring GetExeName() {
29 std::wstring file_name;
30 file_name.resize(MAX_PATH);
31 if (!::GetModuleFileNameW(nullptr, &file_name[0], MAX_PATH)) {
32 assert(false);
33 return std::wstring();
34 }
35 size_t last_slash_pos = file_name.find_last_of(L'\\');
36 if (last_slash_pos != std::wstring::npos) {
37 file_name = file_name.substr(last_slash_pos + 1,
38 file_name.length() - last_slash_pos);
39 }
40 std::transform(file_name.begin(), file_name.end(), file_name.begin(),
41 ::tolower);
42 return file_name;
43 }
44
45 // Global pointer to a vector of crash reports.
46 // This structure will be initialized in InitializeCrashReportingForProcess()
47 // and cleaned up in DllDetachCrashReportingCleanup().
48 std::vector<crash_reporter::Report>* g_crash_reports = nullptr;
49
50 // chrome_elf loads early in the process and initializes Crashpad. That in turn
51 // uses the SetUnhandledExceptionFilter API to set a top level exception
52 // handler for the process. When the process eventually initializes, CRT sets
53 // an exception handler which calls TerminateProcess which effectively bypasses
54 // us. Ideally we want to be at the top of the unhandled exception filter
55 // chain. However we don't have a good way of intercepting the
56 // SetUnhandledExceptionFilter API in the sandbox. EAT patching kernel32 or
57 // kernelbase should ideally work. However the kernel32 kernelbase dlls are
58 // prebound which causes EAT patching to not work. Sidestep works. However it
59 // is only supported for 32 bit. For now we use IAT patching for the
60 // executable.
61 // TODO(ananta).
62 // Check if it is possible to fix EAT patching or use sidestep patching for
63 // 32 bit and 64 bit for this purpose.
64 elf_hook::IATHook* g_set_unhandled_exception_filter = nullptr;
65
66 // Hook function, which ignores the request to set an unhandled-exception
67 // filter.
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.
71 return nullptr;
72 }
73
74 } // namespace
75
76 //------------------------------------------------------------------------------
77 // Public chrome_elf crash APIs
78 //------------------------------------------------------------------------------
79
80 namespace elf_crash {
81
82 // NOTE: This function will be called from DllMain during DLL_PROCESS_ATTACH
83 // (while we have the loader lock), so do not misbehave.
84 void InitializeCrashReporting() {
85 // We want to initialize crash reporting only in chrome.exe
86 if (GetExeName() != L"chrome.exe") {
87 #ifdef _DEBUG
88 assert(false);
89 #endif // _DEBUG
90 return;
91 }
92
93 #ifdef _DEBUG
94 assert(g_crash_reports == nullptr);
95 assert(g_set_unhandled_exception_filter == nullptr);
96 #endif // _DEBUG
97
98 // No global objects with destructors, so using global pointers.
99 // DllMain on detach will clean these up.
100 g_crash_reports = new std::vector<crash_reporter::Report>;
101 g_set_unhandled_exception_filter = new elf_hook::IATHook();
102
103 ChromeCrashReporterClient::InitializeCrashReportingForProcess();
104
105 g_crash_helper_enabled = true;
106 }
107
108 // NOTE: This function will be called from DllMain during DLL_PROCESS_DETACH
109 // (while we have the loader lock), so do not misbehave.
110 void ShutdownCrashReporting() {
111 if (g_crash_reports != nullptr) {
112 g_crash_reports->clear();
113 delete g_crash_reports;
114 }
115 if (g_set_unhandled_exception_filter != nullptr) {
116 delete g_set_unhandled_exception_filter;
117 }
118 }
119
120 // Please refer to the comment on g_set_unhandled_exception_filter for more
121 // information about why we intercept the SetUnhandledExceptionFilter API.
122 void DisableSetUnhandledExceptionFilter() {
123 if (!g_crash_helper_enabled)
124 return;
125 if (g_set_unhandled_exception_filter->Hook(
126 ::GetModuleHandle(nullptr), "kernel32.dll",
127 "SetUnhandledExceptionFilter",
128 SetUnhandledExceptionFilterPatch) != NO_ERROR) {
129 #ifdef _DEBUG
130 assert(false);
131 #endif //_DEBUG
132 }
133 }
134
135 int GenerateCrashDump(EXCEPTION_POINTERS* exception_pointers) {
136 if (g_crash_helper_enabled)
137 crashpad::CrashpadClient::DumpWithoutCrash(
138 *(exception_pointers->ContextRecord));
139 return EXCEPTION_CONTINUE_SEARCH;
140 }
141
142 } // namespace elf_crash
143
144 //------------------------------------------------------------------------------
145 // Exported crash APIs for the rest of the process.
146 //------------------------------------------------------------------------------
147
148 // This helper is invoked by code in chrome.dll to retrieve the crash reports.
149 // See CrashUploadListCrashpad. Note that we do not pass a std::vector here,
150 // because we do not want to allocate/free in different modules. The returned
151 // pointer is read-only.
152 //
153 // NOTE: Since the returned pointer references read-only memory that will be
154 // cleaned up when this DLL unloads, be careful not to reference the memory
155 // beyond that point (e.g. during tests).
156 extern "C" __declspec(dllexport) void GetCrashReportsImpl(
157 const crash_reporter::Report** reports,
158 size_t* report_count) {
159 if (!g_crash_helper_enabled)
160 return;
161 crash_reporter::GetReports(g_crash_reports);
162 *reports = g_crash_reports->data();
163 *report_count = g_crash_reports->size();
164 }
165
166 // This helper is invoked by debugging code in chrome to register the client
167 // id.
168 extern "C" __declspec(dllexport) void SetMetricsClientId(
169 const char* client_id) {
170 if (!g_crash_helper_enabled)
171 return;
172 if (client_id)
173 crash_keys::SetMetricsClientIdFromGUID(client_id);
174 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698