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

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: Merge 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 // Ensures a unique_ptr-wrapped base::File is deleted this goes out of scope.
27 class ScopedFileCloser {
Sigurður Ásgeirsson 2016/09/13 14:57:09 this looks pretty contrived. A simpler thing to do
manzagop (departed) 2016/09/13 16:22:44 Omg! The perils of iterative changes. Done.
28 public:
29 explicit ScopedFileCloser(std::unique_ptr<base::File>* file) : file_(file) {
30 DCHECK(file);
31 }
32 ~ScopedFileCloser() { file_->reset(); }
33
34 private:
35 std::unique_ptr<base::File>* file_;
36
37 DISALLOW_COPY_AND_ASSIGN(ScopedFileCloser);
38 };
39
40 bool IsFileAtOffset(base::File* file, uint32_t expected_offset) {
41 DCHECK(file);
42 int64_t actual_offset = file->Seek(base::File::FROM_CURRENT, 0ULL);
43 return actual_offset == static_cast<int64_t>(expected_offset);
44 }
45
46 // Returns true if the file is empty, and false if the file is not empty or if
47 // there is an error.
48 bool IsFileEmpty(base::File* file) {
49 DCHECK(file);
50 // Get the current offset to be able to restore if not empty.
51 int64_t offset = file->Seek(base::File::FROM_CURRENT, 0ULL);
Sigurður Ásgeirsson 2016/09/13 14:57:09 I wouldn't bother with the gold plating here - jus
manzagop (departed) 2016/09/13 16:22:44 Done.
52 if (offset == -1)
53 return false;
54
55 int64_t end = file->Seek(base::File::FROM_END, 0ULL);
56 if (end == -1)
57 return false;
58 if (end != 0U) {
59 // Attempt to restore the original position.
60 file->Seek(base::File::FROM_BEGIN, offset);
61 return false;
62 }
63 return true;
64 }
65
66 } // namespace
67
68 // The stream type assigned to the minidump stream that holds the serialized
69 // stability report.
70 // Note: the value was obtained by adding 1 to the stream type used for holding
71 // the SyzyAsan proto.
72 // TODO(manzagop): centralize the stream type definitions to avoid issues.
73 const uint32_t kStabilityReportStreamType = 0x4B6B0002;
74
75 void PostmortemMinidumpWriter::Initialize() {
76 next_available_byte_ = 0U;
77 directory_.clear();
78 }
79
80 bool PostmortemMinidumpWriter::WriteDump(base::PlatformFile minidump_file,
81 const StabilityReport& report,
82 const ReportInfo& report_info) {
83 DCHECK_NE(base::kInvalidPlatformFile, minidump_file);
84
85 Initialize();
86 DCHECK_EQ(0U, next_available_byte_);
87 DCHECK(directory_.empty());
88 DCHECK_EQ(nullptr, minidump_file_.get());
89
90 // We do not own |minidump_file|, but we want to rely on base::File's API,
91 // and so we need to duplicate it.
92 HANDLE duplicated_handle;
93 BOOL success = ::DuplicateHandle(::GetCurrentProcess(), minidump_file,
94 GetCurrentProcess(), &duplicated_handle, 0,
95 FALSE, DUPLICATE_SAME_ACCESS);
96 if (!success)
97 return false;
98
99 minidump_file_.reset(new base::File(duplicated_handle));
100 // Ensure the file is closed on exitting the function.
101 ScopedFileCloser closer(&minidump_file_);
102 DCHECK(minidump_file_->IsValid());
103
104 // Allocate space for the header and seek the cursor.
105 FilePosition pos = 0U;
106 if (!Allocate(sizeof(MINIDUMP_HEADER), &pos))
107 return false;
108 if (!SeekCursor(sizeof(MINIDUMP_HEADER)))
109 return false;
110 DCHECK_EQ(kHeaderPos, pos);
111 DCHECK(IsFileAtOffset(minidump_file_.get(), 0U));
112 DCHECK(IsFileEmpty(minidump_file_.get()));
113
114 // Write the proto to the file.
115 std::string serialized_report;
116 if (!report.SerializeToString(&serialized_report))
117 return false;
118 FilePosition report_pos = 0U;
119 if (!AppendBytes(serialized_report, &report_pos))
120 return false;
121
122 // The directory entry for the stability report's stream.
123 RegisterDirectoryEntry(kStabilityReportStreamType, report_pos,
124 serialized_report.length());
125
126 // Write mandatory crash keys. These will be read by crashpad and used as
127 // http request parameters for the upload. Keys and values should match
128 // server side configuration.
129 // TODO(manzagop): use product and version from the stability report. The
130 // current executable's values are an (imperfect) proxy.
131 std::map<std::string, std::string> crash_keys = {
132 {"product", report_info.product_name + "_Postmortem"},
133 {"version", report_info.version_number}};
134 if (!AppendCrashpadInfo(report_info.client_id, report_info.report_id,
135 crash_keys))
136 return false;
137
138 // Write the directory.
139 FilePosition directory_pos = 0U;
140 if (!AppendVec(directory_, &directory_pos))
141 return false;
142
143 // Write the header.
144 MINIDUMP_HEADER header;
145 header.Signature = MINIDUMP_SIGNATURE;
146 header.Version = MINIDUMP_VERSION;
147 header.NumberOfStreams = directory_.size();
148 header.StreamDirectoryRva = directory_pos;
149 if (!SeekCursor(0U))
150 return false;
151 return Write(kHeaderPos, header);
152 }
153
154 bool PostmortemMinidumpWriter::AppendCrashpadInfo(
155 const crashpad::UUID& client_id,
156 const crashpad::UUID& report_id,
157 const std::map<std::string, std::string>& crash_keys) {
158 // Write the crash keys as the contents of a crashpad dictionary.
159 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry> entries;
160 for (const auto& crash_key : crash_keys) {
161 if (!AppendCrashpadDictionaryEntry(crash_key.first, crash_key.second,
162 &entries)) {
163 return false;
164 }
165 }
166
167 // Write the dictionary's index.
168 FilePosition dict_pos = 0U;
169 uint32_t entry_count = entries.size();
170 if (entry_count > 0) {
171 if (!Append(entry_count, &dict_pos))
172 return false;
173 FilePosition unused_pos = 0U;
174 if (!AppendVec(entries, &unused_pos))
175 return false;
176 }
177
178 MINIDUMP_LOCATION_DESCRIPTOR simple_annotations = {0};
179 simple_annotations.DataSize = 0U;
180 if (entry_count > 0)
181 simple_annotations.DataSize = next_available_byte_ - dict_pos;
182 // Note: an RVA of 0 indicates the absence of a dictionary.
183 simple_annotations.Rva = dict_pos;
184
185 // Write the crashpad info.
186 crashpad::MinidumpCrashpadInfo crashpad_info;
187 crashpad_info.version = crashpad::MinidumpCrashpadInfo::kVersion;
188 crashpad_info.report_id = report_id;
189 crashpad_info.client_id = client_id;
190 crashpad_info.simple_annotations = simple_annotations;
191 // Note: module_list is left at 0, which means none.
192
193 FilePosition crashpad_pos = 0U;
194 if (!Append(crashpad_info, &crashpad_pos))
195 return false;
196
197 // Append a directory entry for the crashpad info stream.
198 RegisterDirectoryEntry(crashpad::kMinidumpStreamTypeCrashpadInfo,
199 crashpad_pos, sizeof(crashpad::MinidumpCrashpadInfo));
200
201 return true;
202 }
203
204 bool PostmortemMinidumpWriter::AppendCrashpadDictionaryEntry(
205 const std::string& key,
206 const std::string& value,
207 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry>* entries) {
208 DCHECK_NE(nullptr, entries);
209
210 FilePosition key_pos = 0U;
211 if (!AppendUtf8String(key, &key_pos))
212 return false;
213 FilePosition value_pos = 0U;
214 if (!AppendUtf8String(value, &value_pos))
215 return false;
216
217 crashpad::MinidumpSimpleStringDictionaryEntry entry = {0};
218 entry.key = key_pos;
219 entry.value = value_pos;
220 entries->push_back(entry);
221
222 return true;
223 }
224
225 bool PostmortemMinidumpWriter::Allocate(size_t size_bytes, FilePosition* pos) {
226 DCHECK(pos);
227 *pos = next_available_byte_;
228
229 base::CheckedNumeric<FilePosition> next = next_available_byte_;
230 next += size_bytes;
231 if (!next.IsValid())
232 return false;
233
234 next_available_byte_ += size_bytes;
235 return true;
236 }
237
238 bool PostmortemMinidumpWriter::SeekCursor(FilePosition destination) {
239 DCHECK_NE(nullptr, minidump_file_.get());
240 DCHECK(minidump_file_->IsValid());
241
242 // Validate the write does not extend past the allocated space.
243 if (destination > next_available_byte_)
244 return false;
245
246 int64_t new_pos = minidump_file_->Seek(base::File::FROM_BEGIN,
247 static_cast<int64_t>(destination));
248 return new_pos != -1;
249 }
250
251 bool PostmortemMinidumpWriter::WriteBytes(FilePosition pos,
252 size_t size_bytes,
253 const char* data) {
254 DCHECK(data);
255 DCHECK_NE(nullptr, minidump_file_.get());
256 DCHECK(minidump_file_->IsValid());
257 DCHECK(IsFileAtOffset(minidump_file_.get(), pos));
258
259 // Validate the write does not extend past the next available byte.
260 base::CheckedNumeric<FilePosition> pos_end = pos;
261 pos_end += size_bytes;
262 if (!pos_end.IsValid() || pos_end.ValueOrDie() > next_available_byte_)
263 return false;
264
265 int written_bytes = minidump_file_->WriteAtCurrentPos(data, size_bytes);
266 return written_bytes == size_bytes;
267 }
268
269 bool PostmortemMinidumpWriter::AppendUtf8String(base::StringPiece data,
270 FilePosition* pos) {
271 DCHECK(pos);
272 uint32_t string_size = data.size();
273 if (!Append(string_size, pos))
274 return false;
275
276 FilePosition unused_pos = 0U;
277 return AppendBytes(data, &unused_pos);
278 }
279
280 bool PostmortemMinidumpWriter::AppendBytes(base::StringPiece data,
281 FilePosition* pos) {
282 DCHECK(pos);
283 if (!Allocate(data.length(), pos))
284 return false;
285 return WriteBytes(*pos, data.length(), data.data());
286 }
287
288 void PostmortemMinidumpWriter::RegisterDirectoryEntry(uint32_t stream_type,
289 FilePosition pos,
290 uint32_t size) {
291 MINIDUMP_DIRECTORY entry = {0};
292 entry.StreamType = stream_type;
293 entry.Location.Rva = pos;
294 entry.Location.DataSize = size;
295 directory_.push_back(entry);
296 }
297
298 } // namespace browser_watcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698