OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "components/crash/content/app/fallback_crash_handler_win.h" | |
6 | |
7 #include <dbghelp.h> | |
8 | |
9 #include <algorithm> | |
10 #include <map> | |
11 #include <vector> | |
12 | |
13 #include "base/command_line.h" | |
14 #include "base/files/file.h" | |
15 #include "base/files/file_util.h" | |
16 #include "base/numerics/safe_conversions.h" | |
17 #include "base/process/process_handle.h" | |
18 #include "base/strings/string_number_conversions.h" | |
19 #include "base/win/scoped_handle.h" | |
20 #include "base/win/win_util.h" | |
21 #include "third_party/crashpad/crashpad/client/crash_report_database.h" | |
22 #include "third_party/crashpad/crashpad/client/settings.h" | |
23 #include "third_party/crashpad/crashpad/minidump/minidump_extensions.h" | |
24 | |
25 namespace crash_reporter { | |
26 | |
27 namespace { | |
28 | |
29 using StringStringMap = std::map<std::string, std::string>; | |
30 | |
31 // This class is a helper to edit minidump files written by MinidumpWriteDump. | |
scottmg
2017/01/10 21:33:39
MiniDumpWriteDump
Sigurður Ásgeirsson
2017/01/10 21:56:12
Good lord - I though I'd global-replaced those. Th
| |
32 // It assumes the minidump file it operates on has a directory entry pointing to | |
33 // a CrashpadInfo entry, which it updates to point to the SimpleDictionary data | |
34 // it appends to the file contents. | |
35 class MinidumpUpdater { | |
36 public: | |
37 using FilePosition = uint32_t; | |
38 | |
39 MinidumpUpdater(); | |
40 | |
41 // Reads the existing directory from |file|. | |
42 bool Initialize(base::File* file); | |
43 | |
44 // Appends the simple dictionary with |crash_keys| to the file, and updates | |
45 // the CrashpadInfo with its location. | |
46 bool AppendSimpleDictionary(const StringStringMap& crash_keys); | |
47 | |
48 private: | |
49 // Writes |data_len| bytes from |data| to the file at the current location. | |
50 bool WriteData(const void* data, size_t data_len); | |
51 bool WriteAndAdvance(const void* data, | |
52 size_t data_len, | |
53 FilePosition* position); | |
54 | |
55 base::File* file_; | |
56 std::vector<MINIDUMP_DIRECTORY> directory_; | |
57 }; | |
58 | |
59 MinidumpUpdater::MinidumpUpdater() : file_(nullptr) {} | |
60 | |
61 bool MinidumpUpdater::Initialize(base::File* file) { | |
62 DCHECK(file && file->IsValid()); | |
63 DCHECK(!file_); | |
64 | |
65 // Read the file header. | |
66 MINIDUMP_HEADER header = {}; | |
67 int bytes_read = | |
68 file->Read(0, reinterpret_cast<char*>(&header), sizeof(header)); | |
69 if (bytes_read != sizeof(header)) | |
70 return false; | |
71 if (header.Signature != MINIDUMP_SIGNATURE || header.NumberOfStreams == 0) | |
72 return false; | |
73 | |
74 // Read the stream directory. | |
75 directory_.resize(header.NumberOfStreams); | |
76 int bytes_to_read = header.NumberOfStreams * sizeof(directory_[0]); | |
77 bytes_read = | |
78 file->Read(header.StreamDirectoryRva, | |
79 reinterpret_cast<char*>(&directory_[0]), bytes_to_read); | |
80 if (bytes_read != bytes_to_read) | |
81 return false; | |
82 | |
83 // Crashpad has some fairly unreasonable checking on the minidump header and | |
84 // directory. Match with those checks for now to allow Crashpad to read the | |
85 // CrashpadInfo and upload these dumps. | |
86 | |
87 // Start by removing any unused directory entries. | |
88 // TODO(siggi): Fix Crashpad to ignore unused streams. | |
89 directory_.erase(std::remove_if(directory_.begin(), directory_.end(), | |
90 [](const MINIDUMP_DIRECTORY& entry) { | |
91 return entry.StreamType == UnusedStream; | |
92 })); | |
93 | |
94 // Update the header. | |
95 // TODO(siggi): Fix Crashpad's version checking. | |
96 header.Version = MINIDUMP_VERSION; | |
97 header.NumberOfStreams = base::saturated_cast<ULONG32>(directory_.size()); | |
98 | |
99 // Write back the potentially shortened and packed dictionary. | |
100 int bytes_to_write = header.NumberOfStreams * sizeof(directory_[0]); | |
101 int bytes_written = | |
102 file->Write(header.StreamDirectoryRva, | |
103 reinterpret_cast<char*>(&directory_[0]), bytes_to_write); | |
104 if (bytes_written != bytes_to_write) | |
105 return false; | |
106 | |
107 // Write back the header. | |
108 bytes_written = | |
109 file->Write(0, reinterpret_cast<char*>(&header), sizeof(header)); | |
110 if (bytes_written != sizeof(header)) | |
111 return false; | |
112 | |
113 // Success, stash the file. | |
114 file_ = file; | |
115 | |
116 return true; | |
117 } | |
118 | |
119 bool MinidumpUpdater::AppendSimpleDictionary( | |
120 const StringStringMap& crash_keys) { | |
121 DCHECK(file_); | |
122 | |
123 // Start by finding the Crashpad directory entry and reading the CrashpadInfo. | |
124 FilePosition crashpad_info_pos = 0; | |
125 crashpad::MinidumpCrashpadInfo crashpad_info; | |
126 for (const auto& entry : directory_) { | |
127 if (entry.StreamType == crashpad::kMinidumpStreamTypeCrashpadInfo) { | |
128 // This file is freshly written, so it must contain the same version | |
129 // CrashpadInfo structure this code compiled against. | |
130 if (entry.Location.DataSize != sizeof(crashpad_info)) | |
131 return false; | |
132 | |
133 crashpad_info_pos = entry.Location.Rva; | |
134 break; | |
135 } | |
136 } | |
137 | |
138 // No CrashpadInfo directory entry found. | |
139 if (crashpad_info_pos == 0) | |
140 return false; | |
141 | |
142 int bytes_read = | |
143 file_->Read(crashpad_info_pos, reinterpret_cast<char*>(&crashpad_info), | |
144 sizeof(crashpad_info)); | |
145 if (bytes_read != sizeof(crashpad_info)) | |
146 return false; | |
147 | |
148 if (crashpad_info.version != crashpad::MinidumpCrashpadInfo::kVersion) | |
149 return false; | |
150 | |
151 // Seek to the tail of the file, where we're going to extend it. | |
152 FilePosition next_available_byte = file_->Seek(base::File::FROM_END, 0); | |
153 if (next_available_byte == -1) | |
154 return false; | |
155 | |
156 // Write the key/value pairs and collect their locations. | |
157 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry> entries; | |
158 for (const auto& kv : crash_keys) { | |
159 crashpad::MinidumpSimpleStringDictionaryEntry entry = {0}; | |
160 | |
161 entry.key = next_available_byte; | |
162 uint32_t key_len = base::saturated_cast<uint32_t>(kv.first.size()); | |
163 if (!WriteAndAdvance(&key_len, sizeof(key_len), &next_available_byte) || | |
164 !WriteAndAdvance(&kv.first[0], key_len, &next_available_byte)) { | |
165 return false; | |
166 } | |
167 | |
168 entry.value = next_available_byte; | |
169 uint32_t value_len = base::saturated_cast<uint32_t>(kv.second.size()); | |
170 if (!WriteAndAdvance(&value_len, sizeof(value_len), &next_available_byte) || | |
171 !WriteAndAdvance(&kv.second[0], value_len, &next_available_byte)) { | |
172 return false; | |
173 } | |
174 | |
175 entries.push_back(entry); | |
176 } | |
177 | |
178 // Write the dictionary array itself - note the array is count-prefixed. | |
179 FilePosition dict_pos = next_available_byte; | |
180 uint32_t entry_count = base::saturated_cast<uint32_t>(entries.size()); | |
181 if (!WriteAndAdvance(&entry_count, sizeof(entry_count), | |
182 &next_available_byte) || | |
183 !WriteAndAdvance(&entries[0], entry_count * sizeof(entries[0]), | |
184 &next_available_byte)) { | |
185 return false; | |
186 } | |
187 | |
188 // Touch up the CrashpadInfo and write it back to the file. | |
189 crashpad_info.simple_annotations.DataSize = next_available_byte - dict_pos; | |
190 crashpad_info.simple_annotations.Rva = dict_pos; | |
191 | |
192 int bytes_written = file_->Write( | |
193 crashpad_info_pos, reinterpret_cast<const char*>(&crashpad_info), | |
194 sizeof(crashpad_info)); | |
195 if (bytes_written != sizeof(crashpad_info)) | |
196 return false; | |
197 | |
198 return true; | |
199 } | |
200 | |
201 bool MinidumpUpdater::WriteData(const void* data, size_t data_len) { | |
202 DCHECK(file_); | |
203 DCHECK(data); | |
204 DCHECK_NE(0U, data_len); | |
205 | |
206 if (data_len > INT_MAX) | |
207 return false; | |
208 | |
209 int bytes_to_write = static_cast<int>(data_len); | |
210 int written_bytes = file_->WriteAtCurrentPos( | |
211 reinterpret_cast<const char*>(data), bytes_to_write); | |
212 if (written_bytes == -1) | |
213 return false; | |
214 | |
215 return true; | |
216 } | |
217 | |
218 bool MinidumpUpdater::WriteAndAdvance(const void* data, | |
219 size_t data_len, | |
220 FilePosition* position) { | |
221 DCHECK(position); | |
222 DCHECK_EQ(file_->Seek(base::File::FROM_CURRENT, 0), *position); | |
223 | |
224 if (!WriteData(data, data_len)) | |
225 return false; | |
226 | |
227 *position += base::saturated_cast<FilePosition>(data_len); | |
228 return true; | |
229 } | |
230 | |
231 // Writes a minidump file for |process| to |dump_file| with embedded | |
232 // CrashpadInfo, containing |crash_keys|, |client_id| and |report_id|. | |
233 // The |dump_file| must be open for read as well as write. | |
234 bool MiniDumpWriteDumpWithCrashpadInfo(const base::Process& process, | |
235 uint32_t minidump_type, | |
236 MINIDUMP_EXCEPTION_INFORMATION* exc_info, | |
237 const StringStringMap& crash_keys, | |
238 const crashpad::UUID& client_id, | |
239 const crashpad::UUID& report_id, | |
240 base::File* dump_file) { | |
241 DCHECK(process.IsValid()); | |
242 DCHECK(exc_info); | |
243 DCHECK(dump_file && dump_file->IsValid()); | |
244 | |
245 // The CrashpadInfo structure and its associated directory entry are injected | |
246 // into the minidump, to minimize the work to patching up the dump. | |
247 crashpad::MinidumpCrashpadInfo crashpad_info; | |
248 crashpad_info.version = crashpad::MinidumpCrashpadInfo::kVersion; | |
249 crashpad_info.client_id = client_id; | |
250 crashpad_info.report_id = report_id; | |
251 | |
252 MINIDUMP_USER_STREAM crashpad_info_stream = { | |
253 crashpad::kMinidumpStreamTypeCrashpadInfo, // Type | |
254 sizeof(crashpad_info), // BufferSize | |
255 &crashpad_info // Buffer | |
256 }; | |
257 MINIDUMP_USER_STREAM_INFORMATION user_stream_info = { | |
258 1, // UserStreamCount | |
259 &crashpad_info_stream // UserStreamArray | |
260 }; | |
261 | |
262 // Write the minidump to the provided dump file. | |
263 if (!MiniDumpWriteDump( | |
264 process.Handle(), // Process handle. | |
265 process.Pid(), // Process Id. | |
266 dump_file->GetPlatformFile(), // File handle. | |
267 static_cast<MINIDUMP_TYPE>(minidump_type), // Minidump type. | |
268 exc_info, // Exception Param | |
269 &user_stream_info, // UserStreamParam, | |
270 nullptr)) { // CallbackParam | |
271 return false; | |
272 } | |
273 | |
274 // Retouch the minidump to make it Crashpad compatible. | |
275 MinidumpUpdater updater; | |
276 if (!updater.Initialize(dump_file)) | |
277 return false; | |
278 if (!updater.AppendSimpleDictionary(crash_keys)) | |
279 return false; | |
280 | |
281 return true; | |
282 } | |
283 | |
284 // Appends the full contents of |source| to |dest| from the current position | |
285 // of |dest|. | |
286 bool AppendFileContents(base::File* source, base::PlatformFile dest) { | |
287 DCHECK(source && source->IsValid()); | |
288 DCHECK_NE(base::kInvalidPlatformFile, dest); | |
289 | |
290 // Rewind the source. | |
291 if (source->Seek(base::File::FROM_BEGIN, 0) == -1) | |
292 return false; | |
293 | |
294 std::vector<char> buf; | |
295 buf.resize(1024); | |
296 while (true) { | |
297 int bytes_read = | |
298 source->ReadAtCurrentPos(&buf[0], static_cast<int>(buf.size())); | |
299 if (bytes_read == -1) | |
300 return false; | |
301 if (bytes_read == 0) | |
302 break; | |
303 | |
304 DWORD bytes_written = 0; | |
305 // Due to handle instrumentation, the destination can't be wrapped in | |
306 // a base::File, so we go basic Win32 API here. | |
307 if (!WriteFile(dest, &buf[0], bytes_read, &bytes_written, nullptr) || | |
308 static_cast<int>(bytes_written) != bytes_read) { | |
309 return false; | |
310 } | |
311 } | |
312 | |
313 return true; | |
314 } | |
315 | |
316 } // namespace | |
317 | |
318 FallbackCrashHandler::FallbackCrashHandler() | |
319 : thread_id_(base::kInvalidThreadId), exc_ptrs_(0UL) {} | |
320 | |
321 FallbackCrashHandler::~FallbackCrashHandler() {} | |
322 | |
323 bool FallbackCrashHandler::ParseCommandLine(const base::CommandLine& cmd_line) { | |
324 // Retrieve the handle to the process to dump. | |
325 unsigned int uint_process; | |
326 if (!base::StringToUint(cmd_line.GetSwitchValueASCII("process"), | |
327 &uint_process)) { | |
328 return false; | |
329 } | |
330 | |
331 // Before taking ownership of the supposed handle, see whether it's really | |
332 // a process handle. | |
333 base::ProcessHandle process_handle = base::win::Uint32ToHandle(uint_process); | |
334 if (base::GetProcId(process_handle) == base::kNullProcessId) | |
335 return false; | |
336 | |
337 // Retrieve the thread id argument. | |
338 unsigned thread_id = 0; | |
339 if (!base::StringToUint(cmd_line.GetSwitchValueASCII("thread"), &thread_id)) { | |
340 return false; | |
341 } | |
342 | |
343 // Retrieve the "exception-pointers" argument. | |
344 uint64_t uint_exc_ptrs = 0; | |
345 if (!base::StringToUint64(cmd_line.GetSwitchValueASCII("exception-pointers"), | |
346 &uint_exc_ptrs)) { | |
347 return false; | |
348 } | |
349 exc_ptrs_ = static_cast<uintptr_t>(uint_exc_ptrs); | |
350 | |
351 // Retrieve the "database" argument. | |
352 database_dir_ = cmd_line.GetSwitchValuePath("database"); | |
353 if (database_dir_.empty()) | |
354 return false; | |
355 | |
356 // Everything checks out, take ownership of the process handle. | |
357 process_ = base::Process(process_handle); | |
358 | |
359 return true; | |
360 } | |
361 | |
362 bool FallbackCrashHandler::GenerateCrashDump(const std::string& product, | |
363 const std::string& version, | |
364 const std::string& channel, | |
365 const std::string& process_type) { | |
366 std::unique_ptr<crashpad::CrashReportDatabase> database = | |
367 crashpad::CrashReportDatabase::InitializeWithoutCreating(database_dir_); | |
368 | |
369 if (!database) | |
370 return false; | |
371 | |
372 crashpad::CrashReportDatabase::NewReport* report = nullptr; | |
373 crashpad::CrashReportDatabase::OperationStatus status = | |
374 database->PrepareNewCrashReport(&report); | |
375 if (status != crashpad::CrashReportDatabase::kNoError) | |
376 return false; | |
377 | |
378 // Make sure we release the report on early exit. | |
379 crashpad::CrashReportDatabase::CallErrorWritingCrashReport on_error( | |
380 database.get(), report); | |
381 | |
382 // TODO(siggi): Go big on the detail here for Canary/Dev channels. | |
383 const uint32_t kMinidumpType = MiniDumpWithUnloadedModules | | |
384 MiniDumpWithProcessThreadData | | |
385 MiniDumpWithThreadInfo; | |
386 | |
387 MINIDUMP_EXCEPTION_INFORMATION exc_info = {}; | |
388 exc_info.ThreadId = thread_id_; | |
389 exc_info.ExceptionPointers = reinterpret_cast<EXCEPTION_POINTERS*>(exc_ptrs_); | |
390 exc_info.ClientPointers = TRUE; // ExceptionPointers in client. | |
391 | |
392 // Mandatory crash keys. These will be read by Crashpad and used as | |
393 // http request parameters for the upload. Keys and values need to match | |
394 // server side configuration. | |
395 #if defined(ARCH_CPU_64_BITS) | |
396 const char* platform = "Win64"; | |
397 #else | |
398 const char* platform = "Win32"; | |
399 #endif | |
400 std::map<std::string, std::string> crash_keys = {{"prod", product}, | |
401 {"ver", version}, | |
402 {"channel", channel}, | |
403 {"plat", platform}, | |
404 {"ptype", process_type}}; | |
405 | |
406 crashpad::UUID client_id; | |
407 crashpad::Settings* settings = database->GetSettings(); | |
408 if (settings) { | |
409 // If GetSettings() or GetClientID() fails client_id will be left at its | |
410 // default value, all zeroes, which is appropriate. | |
411 settings->GetClientID(&client_id); | |
412 } | |
413 | |
414 base::FilePath dump_file_path; | |
415 if (!base::CreateTemporaryFile(&dump_file_path)) | |
416 return false; | |
417 | |
418 // Open the file with delete on close, to try and ensure it's cleaned up on | |
419 // any kind of failure. | |
420 base::File dump_file(dump_file_path, base::File::FLAG_OPEN | | |
421 base::File::FLAG_READ | | |
422 base::File::FLAG_WRITE | | |
423 base::File::FLAG_DELETE_ON_CLOSE); | |
424 if (!dump_file.IsValid()) | |
425 return false; | |
426 | |
427 // Write the minidump to the temp file, and then copy the data to the | |
428 // Crashpad-provided handle, as the latter is only open for write. | |
429 if (!MiniDumpWriteDumpWithCrashpadInfo(process_, kMinidumpType, &exc_info, | |
430 crash_keys, client_id, report->uuid, | |
431 &dump_file) || | |
432 !AppendFileContents(&dump_file, report->handle)) { | |
433 return false; | |
434 } | |
435 | |
436 on_error.Disarm(); | |
437 | |
438 crashpad::UUID report_id = {}; | |
439 status = database->FinishedWritingCrashReport(report, &report_id); | |
scottmg
2017/01/10 21:33:39
These crashes are going to show up in the UMA, e.g
Sigurður Ásgeirsson
2017/01/10 21:56:12
Oh - really? Who takes care of setting up the pers
| |
440 if (status != crashpad::CrashReportDatabase::kNoError) | |
441 return false; | |
442 | |
443 return true; | |
444 } | |
445 | |
446 } // namespace crash_reporter | |
OLD | NEW |