File: crash_handler.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rw-r--r-- 4,061 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/gwp_asan/crash_handler/crash_handler.h"

#include <stddef.h>
#include <memory>
#include <string>

#include "base/logging.h"
#include "components/gwp_asan/crash_handler/crash.pb.h"
#include "components/gwp_asan/crash_handler/crash_analyzer.h"
#include "third_party/crashpad/crashpad/minidump/minidump_user_extension_stream_data_source.h"
#include "third_party/crashpad/crashpad/snapshot/process_snapshot.h"

namespace gwp_asan {
namespace internal {
namespace {

// Return a serialized protobuf using a wrapper interface that
// crashpad::UserStreamDataSource expects us to return.
class BufferExtensionStreamDataSource final
    : public crashpad::MinidumpUserExtensionStreamDataSource {
 public:
  BufferExtensionStreamDataSource(uint32_t stream_type, const Crash& crash);

  BufferExtensionStreamDataSource(const BufferExtensionStreamDataSource&) =
      delete;
  BufferExtensionStreamDataSource& operator=(
      const BufferExtensionStreamDataSource&) = delete;

  size_t StreamDataSize() override;
  bool ReadStreamData(Delegate* delegate) override;

 private:
  std::string data_;
};

BufferExtensionStreamDataSource::BufferExtensionStreamDataSource(
    uint32_t stream_type,
    const Crash& crash)
    : crashpad::MinidumpUserExtensionStreamDataSource(stream_type) {
  [[maybe_unused]] bool result = crash.SerializeToString(&data_);
  DCHECK(result);
}

size_t BufferExtensionStreamDataSource::StreamDataSize() {
  DCHECK(!data_.empty());
  return data_.size();
}

bool BufferExtensionStreamDataSource::ReadStreamData(Delegate* delegate) {
  DCHECK(!data_.empty());
  return delegate->ExtensionStreamDataSourceRead(data_.data(), data_.size());
}

const char* ErrorToString(Crash_ErrorType type) {
  switch (type) {
    case Crash::USE_AFTER_FREE:
      return "heap-use-after-free";
    case Crash::BUFFER_UNDERFLOW:
      return "heap-buffer-underflow";
    case Crash::BUFFER_OVERFLOW:
      return "heap-buffer-overflow";
    case Crash::DOUBLE_FREE:
      return "double-free";
    case Crash::UNKNOWN:
      return "unknown";
    case Crash::FREE_INVALID_ADDRESS:
      return "free-invalid-address";
    default:
      return "unexpected error type";
  }
}

const char* AllocatorToString(Crash_Allocator allocator) {
  switch (allocator) {
    case Crash::MALLOC:
      return "malloc";
    case Crash::PARTITIONALLOC:
      return "partitionalloc";
    default:
      return "unexpected allocator type";
  }
}

std::unique_ptr<crashpad::MinidumpUserExtensionStreamDataSource>
HandleException(const crashpad::ProcessSnapshot& snapshot) {
  gwp_asan::Crash proto;
  CrashAnalyzer::GetExceptionInfo(snapshot, &proto);
  // The missing_metadata field is always set for all exceptions.
  if (!proto.has_missing_metadata())
    return nullptr;

  if (proto.missing_metadata()) {
    LOG(ERROR) << "Detected GWP-ASan crash with missing metadata.";
  } else {
    LOG(ERROR) << "Detected GWP-ASan crash for allocation at 0x" << std::hex
               << proto.allocation_address() << std::dec << " ("
               << AllocatorToString(proto.allocator()) << ") of type "
               << ErrorToString(proto.error_type());
  }

  if (proto.has_free_invalid_address()) {
    LOG(ERROR) << "Invalid address passed to free() is " << std::hex
               << proto.free_invalid_address() << std::dec;
  }

  if (proto.has_internal_error())
    LOG(ERROR) << "Experienced internal error: " << proto.internal_error();

  return std::make_unique<BufferExtensionStreamDataSource>(
      kGwpAsanMinidumpStreamType, proto);
}

}  // namespace
}  // namespace internal

std::unique_ptr<crashpad::MinidumpUserExtensionStreamDataSource>
UserStreamDataSource::ProduceStreamData(crashpad::ProcessSnapshot* snapshot) {
  if (!snapshot) {
    DLOG(ERROR) << "Null process snapshot is unexpected.";
    return nullptr;
  }

  return internal::HandleException(*snapshot);
}

}  // namespace gwp_asan