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