crash_handler.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2018 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. #include "components/gwp_asan/crash_handler/crash_handler.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include <string>
  8. #include "base/logging.h"
  9. #include "components/gwp_asan/crash_handler/crash.pb.h"
  10. #include "components/gwp_asan/crash_handler/crash_analyzer.h"
  11. #include "third_party/crashpad/crashpad/minidump/minidump_user_extension_stream_data_source.h"
  12. #include "third_party/crashpad/crashpad/snapshot/process_snapshot.h"
  13. namespace gwp_asan {
  14. namespace internal {
  15. namespace {
  16. // Return a serialized protobuf using a wrapper interface that
  17. // crashpad::UserStreamDataSource expects us to return.
  18. class BufferExtensionStreamDataSource final
  19. : public crashpad::MinidumpUserExtensionStreamDataSource {
  20. public:
  21. BufferExtensionStreamDataSource(uint32_t stream_type, const Crash& crash);
  22. BufferExtensionStreamDataSource(const BufferExtensionStreamDataSource&) =
  23. delete;
  24. BufferExtensionStreamDataSource& operator=(
  25. const BufferExtensionStreamDataSource&) = delete;
  26. size_t StreamDataSize() override;
  27. bool ReadStreamData(Delegate* delegate) override;
  28. private:
  29. std::string data_;
  30. };
  31. BufferExtensionStreamDataSource::BufferExtensionStreamDataSource(
  32. uint32_t stream_type,
  33. const Crash& crash)
  34. : crashpad::MinidumpUserExtensionStreamDataSource(stream_type) {
  35. [[maybe_unused]] bool result = crash.SerializeToString(&data_);
  36. DCHECK(result);
  37. }
  38. size_t BufferExtensionStreamDataSource::StreamDataSize() {
  39. DCHECK(!data_.empty());
  40. return data_.size();
  41. }
  42. bool BufferExtensionStreamDataSource::ReadStreamData(Delegate* delegate) {
  43. DCHECK(!data_.empty());
  44. return delegate->ExtensionStreamDataSourceRead(data_.data(), data_.size());
  45. }
  46. const char* ErrorToString(Crash_ErrorType type) {
  47. switch (type) {
  48. case Crash::USE_AFTER_FREE:
  49. return "heap-use-after-free";
  50. case Crash::BUFFER_UNDERFLOW:
  51. return "heap-buffer-underflow";
  52. case Crash::BUFFER_OVERFLOW:
  53. return "heap-buffer-overflow";
  54. case Crash::DOUBLE_FREE:
  55. return "double-free";
  56. case Crash::UNKNOWN:
  57. return "unknown";
  58. case Crash::FREE_INVALID_ADDRESS:
  59. return "free-invalid-address";
  60. default:
  61. return "unexpected error type";
  62. }
  63. }
  64. const char* AllocatorToString(Crash_Allocator allocator) {
  65. switch (allocator) {
  66. case Crash::MALLOC:
  67. return "malloc";
  68. case Crash::PARTITIONALLOC:
  69. return "partitionalloc";
  70. default:
  71. return "unexpected allocator type";
  72. }
  73. }
  74. std::unique_ptr<crashpad::MinidumpUserExtensionStreamDataSource>
  75. HandleException(const crashpad::ProcessSnapshot& snapshot) {
  76. gwp_asan::Crash proto;
  77. CrashAnalyzer::GetExceptionInfo(snapshot, &proto);
  78. // The missing_metadata field is always set for all exceptions.
  79. if (!proto.has_missing_metadata())
  80. return nullptr;
  81. if (proto.missing_metadata()) {
  82. LOG(ERROR) << "Detected GWP-ASan crash with missing metadata.";
  83. } else {
  84. LOG(ERROR) << "Detected GWP-ASan crash for allocation at 0x" << std::hex
  85. << proto.allocation_address() << std::dec << " ("
  86. << AllocatorToString(proto.allocator()) << ") of type "
  87. << ErrorToString(proto.error_type());
  88. }
  89. if (proto.has_free_invalid_address()) {
  90. LOG(ERROR) << "Invalid address passed to free() is " << std::hex
  91. << proto.free_invalid_address() << std::dec;
  92. }
  93. if (proto.has_internal_error())
  94. LOG(ERROR) << "Experienced internal error: " << proto.internal_error();
  95. return std::make_unique<BufferExtensionStreamDataSource>(
  96. kGwpAsanMinidumpStreamType, proto);
  97. }
  98. } // namespace
  99. } // namespace internal
  100. std::unique_ptr<crashpad::MinidumpUserExtensionStreamDataSource>
  101. UserStreamDataSource::ProduceStreamData(crashpad::ProcessSnapshot* snapshot) {
  102. if (!snapshot) {
  103. DLOG(ERROR) << "Null process snapshot is unexpected.";
  104. return nullptr;
  105. }
  106. return internal::HandleException(*snapshot);
  107. }
  108. } // namespace gwp_asan