forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crash_handler.cc
129 lines (107 loc) · 3.91 KB
/
crash_handler.cc
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// 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/compiler_specific.h"
#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);
size_t StreamDataSize() override;
bool ReadStreamData(Delegate* delegate) override;
private:
std::string data_;
DISALLOW_COPY_AND_ASSIGN(BufferExtensionStreamDataSource);
};
BufferExtensionStreamDataSource::BufferExtensionStreamDataSource(
uint32_t stream_type,
const Crash& crash)
: crashpad::MinidumpUserExtensionStreamDataSource(stream_type) {
bool result = crash.SerializeToString(&data_);
DCHECK(result);
ALLOW_UNUSED_LOCAL(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