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 // Note: aside from using windows headers to obtain the definitions of minidump | |
6 // structures, nothing here is windows specific. This seems like the best | |
7 // approach given this code is for temporary experimentation on Windows. | |
8 // Longer term, Crashpad will take over the minidump writing in this case as | |
9 // well. | |
10 | |
11 #include "components/browser_watcher/postmortem_minidump_writer.h" | |
12 | |
13 #include <windows.h> // NOLINT | |
14 #include <dbghelp.h> | |
15 | |
16 #include <map> | |
17 #include <type_traits> | |
18 #include <vector> | |
19 | |
20 #include "base/files/file_util.h" | |
21 #include "base/macros.h" | |
22 #include "base/numerics/safe_math.h" | |
23 #include "base/strings/string_piece.h" | |
24 #include "third_party/crashpad/crashpad/minidump/minidump_extensions.h" | |
25 | |
26 namespace browser_watcher { | |
27 | |
28 namespace { | |
29 | |
30 // The stream type assigned to the minidump stream that holds the serialized | |
31 // stability report. | |
32 // Note: the value was obtained by adding 1 to the stream type used for holding | |
33 // the SyzyAsan proto. | |
34 // TODO(manzagop): centralize the stream type definitions to avoid issues. | |
35 const uint32_t kStabilityReportStreamType = 0x4B6B0002; | |
36 | |
37 int64_t GetFileOffset(base::File* file) { | |
38 DCHECK(file); | |
39 return file->Seek(base::File::FROM_CURRENT, 0LL); | |
40 } | |
41 | |
42 // Returns true if the file is empty, and false if the file is not empty or if | |
43 // there is an error. | |
44 bool IsFileEmpty(base::File* file) { | |
45 DCHECK(file); | |
46 int64_t end = file->Seek(base::File::FROM_END, 0LL); | |
Sigurður Ásgeirsson
2016/09/14 15:11:48
nit: this is GetFileOffset?
manzagop (departed)
2016/09/14 15:43:49
No because we seek relative to the end instead of
| |
47 return end == 0ULL; | |
48 } | |
49 | |
50 // A class with functionality for writing minimal minidump containers to wrap | |
51 // postmortem stability reports. | |
52 // TODO(manzagop): remove this class once Crashpad takes over writing postmortem | |
53 // minidumps. | |
54 // TODO(manzagop): revisit where the module information should be transported, | |
55 // in the protocol buffer or in a module stream. | |
56 class PostmortemMinidumpWriter { | |
57 public: | |
58 PostmortemMinidumpWriter(); | |
59 ~PostmortemMinidumpWriter() = default; | |
Sigurður Ásgeirsson
2016/09/14 15:11:48
nit: DCHECK_EQ(nullptr, minidump_file_) in destruc
manzagop (departed)
2016/09/14 15:43:49
Done.
| |
60 | |
61 // Write to |minidump_file| a minimal minidump that wraps |report|. Returns | |
62 // true on success, false otherwise. | |
63 // Note: the caller owns |minidump_file| and is responsible for keeping it | |
64 // valid for this object's lifetime. |minidump_file| is expected to be empty | |
65 // and a binary stream. | |
66 bool WriteDump(base::PlatformFile minidump_file, | |
67 const StabilityReport& report, | |
68 const MinidumpInfo& minidump_info); | |
69 | |
70 private: | |
71 // An offset within a minidump file. Note: using this type to avoid including | |
72 // windows.h and relying on the RVA type. | |
73 using FilePosition = uint32_t; | |
74 | |
75 // The minidump header is always located at the head. | |
76 static const FilePosition kHeaderPos = 0U; | |
77 | |
78 bool WriteDumpImpl(const StabilityReport& report, | |
79 const MinidumpInfo& minidump_info); | |
80 | |
81 bool AppendCrashpadInfo(const crashpad::UUID& client_id, | |
82 const crashpad::UUID& report_id, | |
83 const std::map<std::string, std::string>& crash_keys); | |
84 | |
85 bool AppendCrashpadDictionaryEntry( | |
86 const std::string& key, | |
87 const std::string& value, | |
88 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry>* entries); | |
89 | |
90 // Allocate |size_bytes| within the minidump. On success, |pos| contains the | |
91 // location of the allocation. Returns true on success, false otherwise. | |
92 bool Allocate(size_t size_bytes, FilePosition* pos); | |
93 | |
94 // Seeks |cursor_|. The seek operation is kept separate from the write in | |
95 // order to make the call explicit. Seek operations can be costly and should | |
96 // be avoided. | |
97 bool SeekCursor(FilePosition destination); | |
98 | |
99 // Write to pre-allocated space. | |
100 // Note: |pos| must match |cursor_|. | |
101 template <class DataType> | |
102 bool Write(FilePosition pos, const DataType& data); | |
103 bool WriteBytes(FilePosition pos, size_t size_bytes, const char* data); | |
104 | |
105 // Allocate space for and write the contents of |data|. On success, |pos| | |
106 // contains the location of the write. Returns true on success, false | |
107 // otherwise. | |
108 template <class DataType> | |
109 bool Append(const DataType& data, FilePosition* pos); | |
110 template <class DataType> | |
111 bool AppendVec(const std::vector<DataType>& data, FilePosition* pos); | |
112 bool AppendUtf8String(base::StringPiece data, FilePosition* pos); | |
113 bool AppendBytes(base::StringPiece data, FilePosition* pos); | |
114 | |
115 void RegisterDirectoryEntry(uint32_t stream_type, | |
116 FilePosition pos, | |
117 uint32_t size); | |
118 | |
119 // The next allocatable FilePosition. | |
120 FilePosition next_available_byte_; | |
121 | |
122 // Storage for the directory during writes. | |
123 std::vector<MINIDUMP_DIRECTORY> directory_; | |
124 | |
125 // The file to write to. Only valid within the scope of a call to WriteDump. | |
126 base::File* minidump_file_; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(PostmortemMinidumpWriter); | |
129 }; | |
130 | |
131 PostmortemMinidumpWriter::PostmortemMinidumpWriter() | |
132 : next_available_byte_(0U), minidump_file_(nullptr) {} | |
133 | |
134 bool PostmortemMinidumpWriter::WriteDump( | |
135 base::PlatformFile minidump_platform_file, | |
136 const StabilityReport& report, | |
137 const MinidumpInfo& minidump_info) { | |
138 DCHECK_NE(base::kInvalidPlatformFile, minidump_platform_file); | |
139 | |
140 DCHECK_EQ(0U, next_available_byte_); | |
141 DCHECK(directory_.empty()); | |
142 DCHECK_EQ(nullptr, minidump_file_); | |
143 | |
144 // We do not own |minidump_platform_file|, but we want to rely on base::File's | |
145 // API, and so we need to duplicate it. | |
146 HANDLE duplicated_handle; | |
147 BOOL duplicate_success = ::DuplicateHandle( | |
148 ::GetCurrentProcess(), minidump_platform_file, ::GetCurrentProcess(), | |
149 &duplicated_handle, 0, FALSE, DUPLICATE_SAME_ACCESS); | |
150 if (!duplicate_success) | |
151 return false; | |
152 base::File minidump_file(duplicated_handle); | |
153 DCHECK(minidump_file.IsValid()); | |
154 minidump_file_ = &minidump_file; | |
155 DCHECK_EQ(0LL, GetFileOffset(minidump_file_)); | |
156 DCHECK(IsFileEmpty(minidump_file_)); | |
157 | |
158 // Write the minidump, then reset members. | |
159 bool success = WriteDumpImpl(report, minidump_info); | |
160 next_available_byte_ = 0U; | |
161 directory_.clear(); | |
162 minidump_file_ = nullptr; | |
163 | |
164 return success; | |
165 } | |
166 | |
167 bool PostmortemMinidumpWriter::WriteDumpImpl( | |
168 const StabilityReport& report, | |
169 const MinidumpInfo& minidump_info) { | |
170 // Allocate space for the header and seek the cursor. | |
171 FilePosition pos = 0U; | |
172 if (!Allocate(sizeof(MINIDUMP_HEADER), &pos)) | |
173 return false; | |
174 if (!SeekCursor(sizeof(MINIDUMP_HEADER))) | |
175 return false; | |
176 DCHECK_EQ(kHeaderPos, pos); | |
177 | |
178 // Write the proto to the file. | |
179 std::string serialized_report; | |
180 if (!report.SerializeToString(&serialized_report)) | |
181 return false; | |
182 FilePosition report_pos = 0U; | |
183 if (!AppendBytes(serialized_report, &report_pos)) | |
184 return false; | |
185 | |
186 // The directory entry for the stability report's stream. | |
187 RegisterDirectoryEntry(kStabilityReportStreamType, report_pos, | |
188 serialized_report.length()); | |
189 | |
190 // Write mandatory crash keys. These will be read by crashpad and used as | |
191 // http request parameters for the upload. Keys and values should match | |
192 // server side configuration. | |
193 // TODO(manzagop): use product and version from the stability report. The | |
194 // current executable's values are an (imperfect) proxy. | |
195 std::map<std::string, std::string> crash_keys = { | |
196 {"product", minidump_info.product_name + "_Postmortem"}, | |
197 {"version", minidump_info.version_number}}; | |
198 if (!AppendCrashpadInfo(minidump_info.client_id, minidump_info.report_id, | |
199 crash_keys)) | |
200 return false; | |
201 | |
202 // Write the directory. | |
203 FilePosition directory_pos = 0U; | |
204 if (!AppendVec(directory_, &directory_pos)) | |
205 return false; | |
206 | |
207 // Write the header. | |
208 MINIDUMP_HEADER header; | |
209 header.Signature = MINIDUMP_SIGNATURE; | |
210 header.Version = MINIDUMP_VERSION; | |
211 header.NumberOfStreams = directory_.size(); | |
212 header.StreamDirectoryRva = directory_pos; | |
213 if (!SeekCursor(0U)) | |
214 return false; | |
215 return Write(kHeaderPos, header); | |
216 } | |
217 | |
218 bool PostmortemMinidumpWriter::AppendCrashpadInfo( | |
219 const crashpad::UUID& client_id, | |
220 const crashpad::UUID& report_id, | |
221 const std::map<std::string, std::string>& crash_keys) { | |
222 // Write the crash keys as the contents of a crashpad dictionary. | |
223 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry> entries; | |
224 for (const auto& crash_key : crash_keys) { | |
225 if (!AppendCrashpadDictionaryEntry(crash_key.first, crash_key.second, | |
226 &entries)) { | |
227 return false; | |
228 } | |
229 } | |
230 | |
231 // Write the dictionary's index. | |
232 FilePosition dict_pos = 0U; | |
233 uint32_t entry_count = entries.size(); | |
234 if (entry_count > 0) { | |
235 if (!Append(entry_count, &dict_pos)) | |
236 return false; | |
237 FilePosition unused_pos = 0U; | |
238 if (!AppendVec(entries, &unused_pos)) | |
239 return false; | |
240 } | |
241 | |
242 MINIDUMP_LOCATION_DESCRIPTOR simple_annotations = {0}; | |
243 simple_annotations.DataSize = 0U; | |
244 if (entry_count > 0) | |
245 simple_annotations.DataSize = next_available_byte_ - dict_pos; | |
246 // Note: an RVA of 0 indicates the absence of a dictionary. | |
247 simple_annotations.Rva = dict_pos; | |
248 | |
249 // Write the crashpad info. | |
250 crashpad::MinidumpCrashpadInfo crashpad_info; | |
251 crashpad_info.version = crashpad::MinidumpCrashpadInfo::kVersion; | |
252 crashpad_info.report_id = report_id; | |
253 crashpad_info.client_id = client_id; | |
254 crashpad_info.simple_annotations = simple_annotations; | |
255 // Note: module_list is left at 0, which means none. | |
256 | |
257 FilePosition crashpad_pos = 0U; | |
258 if (!Append(crashpad_info, &crashpad_pos)) | |
259 return false; | |
260 | |
261 // Append a directory entry for the crashpad info stream. | |
262 RegisterDirectoryEntry(crashpad::kMinidumpStreamTypeCrashpadInfo, | |
263 crashpad_pos, sizeof(crashpad::MinidumpCrashpadInfo)); | |
264 | |
265 return true; | |
266 } | |
267 | |
268 bool PostmortemMinidumpWriter::AppendCrashpadDictionaryEntry( | |
269 const std::string& key, | |
270 const std::string& value, | |
271 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry>* entries) { | |
272 DCHECK_NE(nullptr, entries); | |
273 | |
274 FilePosition key_pos = 0U; | |
275 if (!AppendUtf8String(key, &key_pos)) | |
276 return false; | |
277 FilePosition value_pos = 0U; | |
278 if (!AppendUtf8String(value, &value_pos)) | |
279 return false; | |
280 | |
281 crashpad::MinidumpSimpleStringDictionaryEntry entry = {0}; | |
282 entry.key = key_pos; | |
283 entry.value = value_pos; | |
284 entries->push_back(entry); | |
285 | |
286 return true; | |
287 } | |
288 | |
289 bool PostmortemMinidumpWriter::Allocate(size_t size_bytes, FilePosition* pos) { | |
290 DCHECK(pos); | |
291 *pos = next_available_byte_; | |
292 | |
293 base::CheckedNumeric<FilePosition> next = next_available_byte_; | |
294 next += size_bytes; | |
295 if (!next.IsValid()) | |
296 return false; | |
297 | |
298 next_available_byte_ += size_bytes; | |
299 return true; | |
300 } | |
301 | |
302 bool PostmortemMinidumpWriter::SeekCursor(FilePosition destination) { | |
303 DCHECK_NE(nullptr, minidump_file_); | |
304 DCHECK(minidump_file_->IsValid()); | |
305 | |
306 // Validate the write does not extend past the allocated space. | |
307 if (destination > next_available_byte_) | |
308 return false; | |
309 | |
310 int64_t new_pos = minidump_file_->Seek(base::File::FROM_BEGIN, | |
311 static_cast<int64_t>(destination)); | |
312 return new_pos != -1; | |
313 } | |
314 | |
315 template <class DataType> | |
316 bool PostmortemMinidumpWriter::Write(FilePosition pos, const DataType& data) { | |
317 static_assert(std::is_trivially_copyable<DataType>::value, | |
318 "restricted to trivially copyable"); | |
319 return WriteBytes(pos, sizeof(data), reinterpret_cast<const char*>(&data)); | |
320 } | |
321 | |
322 bool PostmortemMinidumpWriter::WriteBytes(FilePosition pos, | |
323 size_t size_bytes, | |
324 const char* data) { | |
325 DCHECK(data); | |
326 DCHECK_NE(nullptr, minidump_file_); | |
327 DCHECK(minidump_file_->IsValid()); | |
328 DCHECK_EQ(static_cast<int64_t>(pos), GetFileOffset(minidump_file_)); | |
329 | |
330 // Validate the write does not extend past the next available byte. | |
331 base::CheckedNumeric<FilePosition> pos_end = pos; | |
332 pos_end += size_bytes; | |
333 if (!pos_end.IsValid() || pos_end.ValueOrDie() > next_available_byte_) | |
334 return false; | |
335 | |
336 int size_bytes_signed = static_cast<int>(size_bytes); | |
337 CHECK_LE(0, size_bytes_signed); | |
338 | |
339 int written_bytes = | |
340 minidump_file_->WriteAtCurrentPos(data, size_bytes_signed); | |
341 if (written_bytes < 0) | |
342 return false; | |
343 return static_cast<size_t>(written_bytes) == size_bytes; | |
344 } | |
345 | |
346 template <class DataType> | |
347 bool PostmortemMinidumpWriter::Append(const DataType& data, FilePosition* pos) { | |
348 static_assert(std::is_trivially_copyable<DataType>::value, | |
349 "restricted to trivially copyable"); | |
350 DCHECK(pos); | |
351 if (!Allocate(sizeof(data), pos)) | |
352 return false; | |
353 return Write(*pos, data); | |
354 } | |
355 | |
356 template <class DataType> | |
357 bool PostmortemMinidumpWriter::AppendVec(const std::vector<DataType>& data, | |
358 FilePosition* pos) { | |
359 static_assert(std::is_trivially_copyable<DataType>::value, | |
360 "restricted to trivially copyable"); | |
361 DCHECK(!data.empty()); | |
362 DCHECK(pos); | |
363 | |
364 size_t size_bytes = sizeof(DataType) * data.size(); | |
365 if (!Allocate(size_bytes, pos)) | |
366 return false; | |
367 return WriteBytes(*pos, size_bytes, | |
368 reinterpret_cast<const char*>(&data.at(0))); | |
369 } | |
370 | |
371 bool PostmortemMinidumpWriter::AppendUtf8String(base::StringPiece data, | |
372 FilePosition* pos) { | |
373 DCHECK(pos); | |
374 uint32_t string_size = data.size(); | |
375 if (!Append(string_size, pos)) | |
376 return false; | |
377 | |
378 FilePosition unused_pos = 0U; | |
379 return AppendBytes(data, &unused_pos); | |
380 } | |
381 | |
382 bool PostmortemMinidumpWriter::AppendBytes(base::StringPiece data, | |
383 FilePosition* pos) { | |
384 DCHECK(pos); | |
385 if (!Allocate(data.length(), pos)) | |
386 return false; | |
387 return WriteBytes(*pos, data.length(), data.data()); | |
388 } | |
389 | |
390 void PostmortemMinidumpWriter::RegisterDirectoryEntry(uint32_t stream_type, | |
391 FilePosition pos, | |
392 uint32_t size) { | |
393 MINIDUMP_DIRECTORY entry = {0}; | |
394 entry.StreamType = stream_type; | |
395 entry.Location.Rva = pos; | |
396 entry.Location.DataSize = size; | |
397 directory_.push_back(entry); | |
398 } | |
399 | |
400 } // namespace | |
401 | |
402 bool WritePostmortemDump(base::PlatformFile minidump_file, | |
403 const StabilityReport& report, | |
404 const MinidumpInfo& minidump_info) { | |
405 PostmortemMinidumpWriter writer; | |
406 return writer.WriteDump(minidump_file, report, minidump_info); | |
407 } | |
408 | |
409 } // namespace browser_watcher | |
OLD | NEW |