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); |
| 47 return end == 0LL; |
| 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(); |
| 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 PostmortemMinidumpWriter::~PostmortemMinidumpWriter() { |
| 135 DCHECK_EQ(nullptr, minidump_file_); |
| 136 } |
| 137 |
| 138 bool PostmortemMinidumpWriter::WriteDump( |
| 139 base::PlatformFile minidump_platform_file, |
| 140 const StabilityReport& report, |
| 141 const MinidumpInfo& minidump_info) { |
| 142 DCHECK_NE(base::kInvalidPlatformFile, minidump_platform_file); |
| 143 |
| 144 DCHECK_EQ(0U, next_available_byte_); |
| 145 DCHECK(directory_.empty()); |
| 146 DCHECK_EQ(nullptr, minidump_file_); |
| 147 |
| 148 // We do not own |minidump_platform_file|, but we want to rely on base::File's |
| 149 // API, and so we need to duplicate it. |
| 150 HANDLE duplicated_handle; |
| 151 BOOL duplicate_success = ::DuplicateHandle( |
| 152 ::GetCurrentProcess(), minidump_platform_file, ::GetCurrentProcess(), |
| 153 &duplicated_handle, 0, FALSE, DUPLICATE_SAME_ACCESS); |
| 154 if (!duplicate_success) |
| 155 return false; |
| 156 base::File minidump_file(duplicated_handle); |
| 157 DCHECK(minidump_file.IsValid()); |
| 158 minidump_file_ = &minidump_file; |
| 159 DCHECK_EQ(0LL, GetFileOffset(minidump_file_)); |
| 160 DCHECK(IsFileEmpty(minidump_file_)); |
| 161 |
| 162 // Write the minidump, then reset members. |
| 163 bool success = WriteDumpImpl(report, minidump_info); |
| 164 next_available_byte_ = 0U; |
| 165 directory_.clear(); |
| 166 minidump_file_ = nullptr; |
| 167 |
| 168 return success; |
| 169 } |
| 170 |
| 171 bool PostmortemMinidumpWriter::WriteDumpImpl( |
| 172 const StabilityReport& report, |
| 173 const MinidumpInfo& minidump_info) { |
| 174 // Allocate space for the header and seek the cursor. |
| 175 FilePosition pos = 0U; |
| 176 if (!Allocate(sizeof(MINIDUMP_HEADER), &pos)) |
| 177 return false; |
| 178 if (!SeekCursor(sizeof(MINIDUMP_HEADER))) |
| 179 return false; |
| 180 DCHECK_EQ(kHeaderPos, pos); |
| 181 |
| 182 // Write the proto to the file. |
| 183 std::string serialized_report; |
| 184 if (!report.SerializeToString(&serialized_report)) |
| 185 return false; |
| 186 FilePosition report_pos = 0U; |
| 187 if (!AppendBytes(serialized_report, &report_pos)) |
| 188 return false; |
| 189 |
| 190 // The directory entry for the stability report's stream. |
| 191 RegisterDirectoryEntry(kStabilityReportStreamType, report_pos, |
| 192 serialized_report.length()); |
| 193 |
| 194 // Write mandatory crash keys. These will be read by crashpad and used as |
| 195 // http request parameters for the upload. Keys and values should match |
| 196 // server side configuration. |
| 197 // TODO(manzagop): use product and version from the stability report. The |
| 198 // current executable's values are an (imperfect) proxy. |
| 199 std::map<std::string, std::string> crash_keys = { |
| 200 {"product", minidump_info.product_name + "_Postmortem"}, |
| 201 {"version", minidump_info.version_number}}; |
| 202 if (!AppendCrashpadInfo(minidump_info.client_id, minidump_info.report_id, |
| 203 crash_keys)) |
| 204 return false; |
| 205 |
| 206 // Write the directory. |
| 207 FilePosition directory_pos = 0U; |
| 208 if (!AppendVec(directory_, &directory_pos)) |
| 209 return false; |
| 210 |
| 211 // Write the header. |
| 212 MINIDUMP_HEADER header; |
| 213 header.Signature = MINIDUMP_SIGNATURE; |
| 214 header.Version = MINIDUMP_VERSION; |
| 215 header.NumberOfStreams = directory_.size(); |
| 216 header.StreamDirectoryRva = directory_pos; |
| 217 if (!SeekCursor(0U)) |
| 218 return false; |
| 219 return Write(kHeaderPos, header); |
| 220 } |
| 221 |
| 222 bool PostmortemMinidumpWriter::AppendCrashpadInfo( |
| 223 const crashpad::UUID& client_id, |
| 224 const crashpad::UUID& report_id, |
| 225 const std::map<std::string, std::string>& crash_keys) { |
| 226 // Write the crash keys as the contents of a crashpad dictionary. |
| 227 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry> entries; |
| 228 for (const auto& crash_key : crash_keys) { |
| 229 if (!AppendCrashpadDictionaryEntry(crash_key.first, crash_key.second, |
| 230 &entries)) { |
| 231 return false; |
| 232 } |
| 233 } |
| 234 |
| 235 // Write the dictionary's index. |
| 236 FilePosition dict_pos = 0U; |
| 237 uint32_t entry_count = entries.size(); |
| 238 if (entry_count > 0) { |
| 239 if (!Append(entry_count, &dict_pos)) |
| 240 return false; |
| 241 FilePosition unused_pos = 0U; |
| 242 if (!AppendVec(entries, &unused_pos)) |
| 243 return false; |
| 244 } |
| 245 |
| 246 MINIDUMP_LOCATION_DESCRIPTOR simple_annotations = {0}; |
| 247 simple_annotations.DataSize = 0U; |
| 248 if (entry_count > 0) |
| 249 simple_annotations.DataSize = next_available_byte_ - dict_pos; |
| 250 // Note: an RVA of 0 indicates the absence of a dictionary. |
| 251 simple_annotations.Rva = dict_pos; |
| 252 |
| 253 // Write the crashpad info. |
| 254 crashpad::MinidumpCrashpadInfo crashpad_info; |
| 255 crashpad_info.version = crashpad::MinidumpCrashpadInfo::kVersion; |
| 256 crashpad_info.report_id = report_id; |
| 257 crashpad_info.client_id = client_id; |
| 258 crashpad_info.simple_annotations = simple_annotations; |
| 259 // Note: module_list is left at 0, which means none. |
| 260 |
| 261 FilePosition crashpad_pos = 0U; |
| 262 if (!Append(crashpad_info, &crashpad_pos)) |
| 263 return false; |
| 264 |
| 265 // Append a directory entry for the crashpad info stream. |
| 266 RegisterDirectoryEntry(crashpad::kMinidumpStreamTypeCrashpadInfo, |
| 267 crashpad_pos, sizeof(crashpad::MinidumpCrashpadInfo)); |
| 268 |
| 269 return true; |
| 270 } |
| 271 |
| 272 bool PostmortemMinidumpWriter::AppendCrashpadDictionaryEntry( |
| 273 const std::string& key, |
| 274 const std::string& value, |
| 275 std::vector<crashpad::MinidumpSimpleStringDictionaryEntry>* entries) { |
| 276 DCHECK_NE(nullptr, entries); |
| 277 |
| 278 FilePosition key_pos = 0U; |
| 279 if (!AppendUtf8String(key, &key_pos)) |
| 280 return false; |
| 281 FilePosition value_pos = 0U; |
| 282 if (!AppendUtf8String(value, &value_pos)) |
| 283 return false; |
| 284 |
| 285 crashpad::MinidumpSimpleStringDictionaryEntry entry = {0}; |
| 286 entry.key = key_pos; |
| 287 entry.value = value_pos; |
| 288 entries->push_back(entry); |
| 289 |
| 290 return true; |
| 291 } |
| 292 |
| 293 bool PostmortemMinidumpWriter::Allocate(size_t size_bytes, FilePosition* pos) { |
| 294 DCHECK(pos); |
| 295 *pos = next_available_byte_; |
| 296 |
| 297 base::CheckedNumeric<FilePosition> next = next_available_byte_; |
| 298 next += size_bytes; |
| 299 if (!next.IsValid()) |
| 300 return false; |
| 301 |
| 302 next_available_byte_ += size_bytes; |
| 303 return true; |
| 304 } |
| 305 |
| 306 bool PostmortemMinidumpWriter::SeekCursor(FilePosition destination) { |
| 307 DCHECK_NE(nullptr, minidump_file_); |
| 308 DCHECK(minidump_file_->IsValid()); |
| 309 |
| 310 // Validate the write does not extend past the allocated space. |
| 311 if (destination > next_available_byte_) |
| 312 return false; |
| 313 |
| 314 int64_t new_pos = minidump_file_->Seek(base::File::FROM_BEGIN, |
| 315 static_cast<int64_t>(destination)); |
| 316 return new_pos != -1; |
| 317 } |
| 318 |
| 319 template <class DataType> |
| 320 bool PostmortemMinidumpWriter::Write(FilePosition pos, const DataType& data) { |
| 321 static_assert(std::is_trivially_copyable<DataType>::value, |
| 322 "restricted to trivially copyable"); |
| 323 return WriteBytes(pos, sizeof(data), reinterpret_cast<const char*>(&data)); |
| 324 } |
| 325 |
| 326 bool PostmortemMinidumpWriter::WriteBytes(FilePosition pos, |
| 327 size_t size_bytes, |
| 328 const char* data) { |
| 329 DCHECK(data); |
| 330 DCHECK_NE(nullptr, minidump_file_); |
| 331 DCHECK(minidump_file_->IsValid()); |
| 332 DCHECK_EQ(static_cast<int64_t>(pos), GetFileOffset(minidump_file_)); |
| 333 |
| 334 // Validate the write does not extend past the next available byte. |
| 335 base::CheckedNumeric<FilePosition> pos_end = pos; |
| 336 pos_end += size_bytes; |
| 337 if (!pos_end.IsValid() || pos_end.ValueOrDie() > next_available_byte_) |
| 338 return false; |
| 339 |
| 340 int size_bytes_signed = static_cast<int>(size_bytes); |
| 341 CHECK_LE(0, size_bytes_signed); |
| 342 |
| 343 int written_bytes = |
| 344 minidump_file_->WriteAtCurrentPos(data, size_bytes_signed); |
| 345 if (written_bytes < 0) |
| 346 return false; |
| 347 return static_cast<size_t>(written_bytes) == size_bytes; |
| 348 } |
| 349 |
| 350 template <class DataType> |
| 351 bool PostmortemMinidumpWriter::Append(const DataType& data, FilePosition* pos) { |
| 352 static_assert(std::is_trivially_copyable<DataType>::value, |
| 353 "restricted to trivially copyable"); |
| 354 DCHECK(pos); |
| 355 if (!Allocate(sizeof(data), pos)) |
| 356 return false; |
| 357 return Write(*pos, data); |
| 358 } |
| 359 |
| 360 template <class DataType> |
| 361 bool PostmortemMinidumpWriter::AppendVec(const std::vector<DataType>& data, |
| 362 FilePosition* pos) { |
| 363 static_assert(std::is_trivially_copyable<DataType>::value, |
| 364 "restricted to trivially copyable"); |
| 365 DCHECK(!data.empty()); |
| 366 DCHECK(pos); |
| 367 |
| 368 size_t size_bytes = sizeof(DataType) * data.size(); |
| 369 if (!Allocate(size_bytes, pos)) |
| 370 return false; |
| 371 return WriteBytes(*pos, size_bytes, |
| 372 reinterpret_cast<const char*>(&data.at(0))); |
| 373 } |
| 374 |
| 375 bool PostmortemMinidumpWriter::AppendUtf8String(base::StringPiece data, |
| 376 FilePosition* pos) { |
| 377 DCHECK(pos); |
| 378 uint32_t string_size = data.size(); |
| 379 if (!Append(string_size, pos)) |
| 380 return false; |
| 381 |
| 382 FilePosition unused_pos = 0U; |
| 383 return AppendBytes(data, &unused_pos); |
| 384 } |
| 385 |
| 386 bool PostmortemMinidumpWriter::AppendBytes(base::StringPiece data, |
| 387 FilePosition* pos) { |
| 388 DCHECK(pos); |
| 389 if (!Allocate(data.length(), pos)) |
| 390 return false; |
| 391 return WriteBytes(*pos, data.length(), data.data()); |
| 392 } |
| 393 |
| 394 void PostmortemMinidumpWriter::RegisterDirectoryEntry(uint32_t stream_type, |
| 395 FilePosition pos, |
| 396 uint32_t size) { |
| 397 MINIDUMP_DIRECTORY entry = {0}; |
| 398 entry.StreamType = stream_type; |
| 399 entry.Location.Rva = pos; |
| 400 entry.Location.DataSize = size; |
| 401 directory_.push_back(entry); |
| 402 } |
| 403 |
| 404 } // namespace |
| 405 |
| 406 bool WritePostmortemDump(base::PlatformFile minidump_file, |
| 407 const StabilityReport& report, |
| 408 const MinidumpInfo& minidump_info) { |
| 409 PostmortemMinidumpWriter writer; |
| 410 return writer.WriteDump(minidump_file, report, minidump_info); |
| 411 } |
| 412 |
| 413 } // namespace browser_watcher |
OLD | NEW |