File: dump_gwp_asan.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (176 lines) | stat: -rw-r--r-- 5,352 bytes parent folder | download | duplicates (5)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <iostream>
#include <sstream>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/to_string.h"
#include "components/gwp_asan/crash_handler/crash.pb.h"
#include "components/gwp_asan/crash_handler/crash_handler.h"
#include "third_party/crashpad/crashpad/minidump/minidump_extensions.h"
#include "third_party/crashpad/crashpad/snapshot/minidump/process_snapshot_minidump.h"
#include "third_party/crashpad/crashpad/util/file/file_reader.h"

namespace gwp_asan {

std::string ErrorTypeToString(Crash::ErrorType error_type) {
  switch (error_type) {
    case Crash::USE_AFTER_FREE:
      return "USE_AFTER_FREE";
    case Crash::BUFFER_UNDERFLOW:
      return "BUFFER_UNDERFLOW";
    case Crash::BUFFER_OVERFLOW:
      return "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 "UNKNOWN";
  }
}

std::string AllocatorToString(Crash::Allocator allocator) {
  switch (allocator) {
    case Crash::MALLOC:
      return "MALLOC";
    case Crash::PARTITIONALLOC:
      return "PARTITIONALLOC";
    default:
      return "UNKNOWN";
  }
}

std::string ModeToString(Crash::Mode mode) {
  switch (mode) {
    case Crash::CLASSIC:
      return "CLASSIC";
    case Crash::LIGHTWEIGHT_DETECTOR_BRP:
      return "LIGHTWEIGHT_DETECTOR_BRP";
    case Crash::LIGHTWEIGHT_DETECTOR_RANDOM:
      return "LIGHTWEIGHT_DETECTOR_RANDOM";
    case Crash::EXTREME_LIGHTWEIGHT_DETECTOR:
      return "EXTREME_LIGHTWEIGHT_DETECTOR";
    default:
      return "UNKNOWN";
  }
}

std::string AllocationInfoToString(const Crash::AllocationInfo& info) {
  std::stringstream ss;
  ss << "AllocationInfo {" << std::endl;
  ss << "    stack_trace: [" << std::endl;
  for (int i = 0; i < info.stack_trace_size(); ++i) {
    ss << "      " << info.stack_trace(i)
       << (i < info.stack_trace_size() - 1 ? ", " : "") << std::endl;
  }
  ss << "    ]" << std::endl;
  ss << "    thread_id: " << info.thread_id() << std::endl;
  ss << "  }";
  return ss.str();
}

std::string CrashToString(const Crash& crash) {
  std::stringstream ss;
  ss << "Crash {" << std::endl;
  if (crash.has_error_type()) {
    ss << "  error_type: " << ErrorTypeToString(crash.error_type())
       << std::endl;
  }
  if (crash.has_allocation_address()) {
    ss << "  allocation_address: " << crash.allocation_address() << std::endl;
  }
  if (crash.has_allocation_size()) {
    ss << "  allocation_size: " << crash.allocation_size() << std::endl;
  }
  if (crash.has_allocation()) {
    ss << "  allocation: " << AllocationInfoToString(crash.allocation())
       << std::endl;
  }
  if (crash.has_deallocation()) {
    ss << "  deallocation: " << AllocationInfoToString(crash.deallocation())
       << std::endl;
  }
  if (crash.has_region_start()) {
    ss << "  region_start: " << crash.region_start() << std::endl;
  }
  if (crash.has_region_size()) {
    ss << "  region_size: " << crash.region_size() << std::endl;
  }
  if (crash.has_free_invalid_address()) {
    ss << "  free_invalid_address: " << crash.free_invalid_address()
       << std::endl;
  }
  if (crash.has_missing_metadata()) {
    ss << "  missing_metadata: " << base::ToString(crash.missing_metadata())
       << std::endl;
  }
  if (crash.has_internal_error()) {
    ss << "  internal_error: " << crash.internal_error() << std::endl;
  }
  if (crash.has_allocator()) {
    ss << "  allocator: " << AllocatorToString(crash.allocator()) << std::endl;
  }
  if (crash.has_mode()) {
    ss << "  mode: " << ModeToString(crash.mode()) << std::endl;
  }
  ss << "}";
  return ss.str();
}

}  // namespace gwp_asan

int main(int argc, const char* argv[]) {
  base::CommandLine::Init(argc, argv);
  const base::CommandLine& command_line =
      *base::CommandLine::ForCurrentProcess();

  base::CommandLine::StringVector args = command_line.GetArgs();
  if (args.size() != 1) {
    std::cerr << "Usage: dump_gwp_asan <minidump_path>" << std::endl;
    return 1;
  }

  base::FilePath path(args[0]);

  crashpad::FileReader minidump_file_reader;
  if (!minidump_file_reader.Open(path)) {
    std::cerr << "Can't open the minidump." << std::endl;
    return 1;
  }

  crashpad::ProcessSnapshotMinidump minidump_process_snapshot;
  if (!minidump_process_snapshot.Initialize(&minidump_file_reader)) {
    std::cerr << "Can't initialize the process snapshot." << std::endl;
    return 1;
  }

  gwp_asan::Crash proto;
  bool found = false;
  auto custom_streams = minidump_process_snapshot.CustomMinidumpStreams();
  for (auto* stream : custom_streams) {
    if (stream->stream_type() ==
        static_cast<crashpad::MinidumpStreamType>(
            gwp_asan::internal::kGwpAsanMinidumpStreamType)) {
      if (!proto.ParseFromArray(stream->data().data(), stream->data().size())) {
        std::cerr << "Couldn't parse the GWP-ASan stream." << std::endl;
        return 1;
      }
      found = true;
      break;
    }
  }

  if (!found) {
    std::cerr << "Couldn't find the GWP-ASan stream." << std::endl;
  }

  std::cout << gwp_asan::CrashToString(proto) << std::endl;

  return 0;
}