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

Side by Side Diff: components/browser_watcher/postmortem_minidump_writer_win.cc

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

Powered by Google App Engine
This is Rietveld 408576698