File: crash_handler_unittest.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 (596 lines) | stat: -rw-r--r-- 23,517 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

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

#include <array>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/page_size.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/test/gtest_util.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "components/gwp_asan/client/guarded_page_allocator.h"
#include "components/gwp_asan/client/gwp_asan.h"
#include "components/gwp_asan/client/lightweight_detector/poison_metadata_recorder.h"
#include "components/gwp_asan/common/crash_key_name.h"
#include "components/gwp_asan/common/lightweight_detector_state.h"
#include "components/gwp_asan/crash_handler/crash.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
#include "third_party/crashpad/crashpad/client/annotation.h"
#include "third_party/crashpad/crashpad/client/crash_report_database.h"
#include "third_party/crashpad/crashpad/client/crashpad_client.h"
#include "third_party/crashpad/crashpad/client/crashpad_info.h"
#include "third_party/crashpad/crashpad/handler/handler_main.h"
#include "third_party/crashpad/crashpad/snapshot/minidump/process_snapshot_minidump.h"
#include "third_party/crashpad/crashpad/tools/tool_support.h"

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
#include "third_party/crashpad/crashpad/snapshot/sanitized/sanitization_information.h"
#endif

namespace gwp_asan {
namespace internal {

namespace {

constexpr size_t kAllocationSize = 902;
constexpr int kSuccess = 0;

static constexpr size_t kMaxMetadata = 2048;
static constexpr size_t kTotalPages = 8192;

#if !BUILDFLAG(IS_ANDROID)
int HandlerMainAdaptor(int argc, char* argv[]) {
  crashpad::UserStreamDataSources user_stream_data_sources;
  user_stream_data_sources.push_back(
      std::make_unique<gwp_asan::UserStreamDataSource>());
  return crashpad::HandlerMain(argc, argv, &user_stream_data_sources);
}

// Child process that runs the crashpad handler.
MULTIPROCESS_TEST_MAIN(CrashpadHandler) {
  base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();

  // Remove the --test-child-process argument from argv and launch crashpad.
#if BUILDFLAG(IS_WIN)
  std::vector<wchar_t*> argv;
  for (auto& arg : cmd_line->argv()) {
    if (arg.find(L"test-child-process") == std::string::npos)
      argv.push_back(const_cast<wchar_t*>(arg.c_str()));
  }

  crashpad::ToolSupport::Wmain(argv.size(), argv.data(), HandlerMainAdaptor);
#else
  std::vector<char*> argv;
  for (auto& arg : cmd_line->argv()) {
    if (arg.find("test-child-process") == std::string::npos)
      argv.push_back(const_cast<char*>(arg.c_str()));
  }

  HandlerMainAdaptor(argv.size(), argv.data());
#endif

  return 0;
}
#endif  // !BUILDFLAG(IS_ANDROID)

// Child process that launches the crashpad handler and then crashes.
MULTIPROCESS_TEST_MAIN(CrashingProcess) {
#if BUILDFLAG(IS_APPLE)
  // Disable the system crash reporter from inspecting this crash (it is slow
  // and causes test timeouts.)
  crashpad::CrashpadInfo::GetCrashpadInfo()
      ->set_system_crash_reporter_forwarding(crashpad::TriState::kDisabled);
#endif

  base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
  base::FilePath directory = cmd_line->GetSwitchValuePath("directory");
  CHECK(!directory.empty());
  std::string test_name = cmd_line->GetSwitchValueASCII("test-name");
  CHECK(!test_name.empty());
  std::string allocator = cmd_line->GetSwitchValueASCII("allocator");

  const char* annotation_name;
  if (allocator == "malloc") {
    annotation_name = kMallocCrashKey;
  } else if (allocator == "partitionalloc") {
    annotation_name = kPartitionAllocCrashKey;
  } else {
    LOG(ERROR) << "Unknown allocator";
    return kSuccess;
  }

  base::NoDestructor<GuardedPageAllocator> gpa;
  CHECK(gpa->Init(
      AllocatorSettings{
          .max_allocated_pages = kMaxMetadata,
          .num_metadata = kMaxMetadata,
          .total_pages = kTotalPages,
          .sampling_frequency = 0u,
      },
      base::DoNothing(), allocator == "partitionalloc"));

  static crashpad::StringAnnotation<24> gpa_annotation(annotation_name);
  gpa_annotation.Set(gpa->GetCrashKey());

  if (cmd_line->HasSwitch("enable-lightweight-detector")) {
    lud::PoisonMetadataRecorder::Init(LightweightDetectorMode::kBrpQuarantine,
                                      1);
    static crashpad::StringAnnotation<24> lightweight_detector_annotation(
        kLightweightDetectorCrashKey);
    lightweight_detector_annotation.Set(
        lud::PoisonMetadataRecorder::Get()->GetCrashKey());
  }

  base::FilePath metrics_dir(FILE_PATH_LITERAL(""));
  std::map<std::string, std::string> annotations;
  std::vector<std::string> arguments;

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
  static crashpad::SanitizationInformation sanitization_info = {};
  static crashpad::SanitizationAllowedMemoryRanges allowed_memory_ranges;
  if (cmd_line->HasSwitch("sanitize")) {
    auto memory_ranges = gpa->GetInternalMemoryRegions();
    if (cmd_line->HasSwitch("enable-lightweight-detector")) {
      auto detector_memory_ranges =
          lud::PoisonMetadataRecorder::Get()->GetInternalMemoryRegions();
      memory_ranges.insert(memory_ranges.end(), detector_memory_ranges.begin(),
                           detector_memory_ranges.end());
    }
    auto* range_array =
        new crashpad::SanitizationAllowedMemoryRanges::Range[memory_ranges
                                                                 .size()];
    for (size_t i = 0; i < memory_ranges.size(); i++) {
      range_array[i].base =
          reinterpret_cast<crashpad::VMAddress>(memory_ranges[i].first);
      range_array[i].length = memory_ranges[i].second;
    }
    allowed_memory_ranges.size = memory_ranges.size();
    allowed_memory_ranges.entries =
        reinterpret_cast<crashpad::VMAddress>(range_array);
    sanitization_info.allowed_memory_ranges_address =
        reinterpret_cast<crashpad::VMAddress>(&allowed_memory_ranges);
    arguments.push_back(base::StringPrintf("--sanitization-information=%p",
                                           &sanitization_info));
  }
#endif

#if !BUILDFLAG(IS_ANDROID)
  arguments.push_back("--test-child-process=CrashpadHandler");
#endif

  crashpad::CrashpadClient* client = new crashpad::CrashpadClient();
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  bool handler =
      client->StartHandlerAtCrash(/* handler */ cmd_line->GetProgram(),
                                  /* database */ directory,
                                  /* metrics_dir */ metrics_dir,
                                  /* url */ "",
                                  /* annotations */ annotations,
                                  /* arguments */ arguments);
#elif BUILDFLAG(IS_ANDROID)
  // TODO: Once the minSdkVersion is >= Q define a CrashpadHandlerMain() and
  // use the /system/bin/linker approach instead of using
  // libchrome_crashpad_handler.so
  base::FilePath modules;
  if (!base::PathService::Get(base::DIR_MODULE, &modules)) {
    LOG(ERROR) << "Failed to read DIR_MODULE";
    return kSuccess;
  }

  base::FilePath executable_path =
      modules.AppendASCII("libchrome_crashpad_handler.so");

  std::unique_ptr<base::Environment> env(base::Environment::Create());
  std::string library_path = env->GetVar("LD_LIBRARY_PATH").value_or("");
  env->SetVar("LD_LIBRARY_PATH", library_path + ":" + modules.value());

  bool handler = client->StartHandlerAtCrash(
      executable_path, directory, metrics_dir, "", annotations, arguments);
#else
  bool handler = client->StartHandler(/* handler */ cmd_line->GetProgram(),
                                      /* database */ directory,
                                      /* metrics_dir */ metrics_dir,
                                      /* url */ "",
                                      /* annotations */ annotations,
                                      /* arguments */ arguments,
                                      /* restartable */ false,
                                      /* asynchronous_start */ false);
#endif

  if (!handler) {
    LOG(ERROR) << "Crash handler failed to launch";
    return kSuccess;
  }

  if (test_name == "UseAfterFree") {
    void* ptr = gpa->Allocate(kAllocationSize);
    gpa->Deallocate(ptr);
    *(int*)ptr = 0;
  } else if (test_name == "DoubleFree") {
    void* ptr = gpa->Allocate(kAllocationSize);
    gpa->Deallocate(ptr);
    gpa->Deallocate(ptr);
  } else if (test_name == "Underflow") {
    void* ptr = gpa->Allocate(kAllocationSize);
    for (size_t i = 0; i < base::GetPageSize(); i++) {
      // Cast to `ptrdiff_t` so that the offset is actually negative, rather
      // than a very large unsigned value. With a very large unsigned value,
      // UBSan flags the error without any information about allocation sizes,
      // which impacts the crash handling.
      //
      // The compiler could also, in principle, see that `ptr[-size_t{1}]` is
      // always UB because no allocation can be that large, and then optimize
      // this code to assume `base::GetPageSize()` returns one, suppressing the
      // crash. (Though, as of writing, it does not do this.)
      //
      // Avoid these issues by underflowing with an actual negative value. This
      // is still UB (thus the crash), but requires knowledge of `ptr` to
      // observe, so a non-ASan compiler does not interfere with it in practice.
      ((unsigned char*)ptr)[-static_cast<ptrdiff_t>(i)] = 0;
    }
  } else if (test_name == "Overflow") {
    void* ptr = gpa->Allocate(kAllocationSize);
    for (size_t i = 0; i <= base::GetPageSize(); i++) {
      ((unsigned char*)ptr)[i] = 0;
    }
  } else if (test_name == "UnrelatedException") {
    __builtin_trap();
  } else if (test_name == "FreeInvalidAddress") {
    void* ptr = gpa->Allocate(kAllocationSize);
    uintptr_t bad_address = reinterpret_cast<uintptr_t>(ptr) + 1;
    gpa->Deallocate(reinterpret_cast<void*>(bad_address));
  } else if (test_name == "MissingMetadata") {
    // Consume all allocations/metadata
    std::array<void*, kMaxMetadata> ptrs;
    for (size_t i = 0; i < kMaxMetadata; i++) {
      ptrs[i] = gpa->Allocate(1);
    }

    gpa->Deallocate(ptrs[0]);

    // Take the remaining metadata slot with an allocation on a different page.
    while (true) {
      void* new_alloc = gpa->Allocate(1);
      if (new_alloc != ptrs[0])
        break;
      gpa->Deallocate(new_alloc);
    }

    // Cause a crash accessing an allocation that no longer has metadata
    // associated with it.
    *(uint8_t*)(ptrs[0]) = 0;
  } else if (test_name == "LightweightDetectorUseAfterFree") {
    uint8_t fake_alloc[kAllocationSize];
    lud::PoisonMetadataRecorder::Get()->RecordAndZap(&fake_alloc,
                                                     sizeof(fake_alloc));
    **(int**)fake_alloc = 0;
  } else {
    LOG(ERROR) << "Unknown test name " << test_name;
  }

  LOG(ERROR) << "This return should never be reached.";
  return kSuccess;
}

enum class ShouldSanitize : bool { kNo, kYes };
enum class EnableLightweightDetector : bool { kNo, kYes };
enum class HasAllocation : bool { kNo, kYes };
enum class HasDeallocation : bool { kNo, kYes };

struct TestParams {
  TestParams(const char* allocator,
             ShouldSanitize sanitize,
             EnableLightweightDetector enable_lightweight_detector)
      : allocator(allocator),
        sanitize(sanitize),
        enable_lightweight_detector(enable_lightweight_detector) {}

  const char* allocator;
  ShouldSanitize sanitize;
  EnableLightweightDetector enable_lightweight_detector;
};

class BaseCrashHandlerTest : public base::MultiProcessTest,
                             public testing::WithParamInterface<TestParams> {
 protected:
  BaseCrashHandlerTest() : params_(GetParam()) {}

  // Launch a child process and wait for it to crash. Set |gwp_asan_found_| if a
  // GWP-ASan data was found and if so, read it into |proto_|.
  void SetUp() final {
    base::ScopedTempDir database_dir;
    ASSERT_TRUE(database_dir.CreateUniqueTempDir());

    // Remove the parameterized test suffix from the test name.
    std::string test_name(
        testing::UnitTest::GetInstance()->current_test_info()->name());
    size_t separator = test_name.find("/");
    ASSERT_NE(separator, std::string::npos);
    test_name.erase(separator);

    ASSERT_TRUE(runTestProcess(database_dir.GetPath(), test_name.c_str()));

    bool minidump_found;
    readGwpAsanStreamFromCrash(database_dir.GetPath(), &minidump_found,
                               &gwp_asan_found_, &proto_);
    ASSERT_TRUE(minidump_found);
  }

  // Launch a second process that installs a crashpad handler and causes an
  // exception of type test_name, then validate that it exited successfully.
  // crashpad is initialized to write to the given database directory.
  bool runTestProcess(const base::FilePath& database_dir,
                      const char* test_name) {
    base::CommandLine cmd_line =
        base::GetMultiProcessTestChildBaseCommandLine();
    cmd_line.AppendSwitchPath("directory", database_dir);
    cmd_line.AppendSwitchASCII("test-name", test_name);
    cmd_line.AppendSwitchASCII("allocator", params_.allocator);

    if (params_.sanitize == ShouldSanitize::kYes) {
      cmd_line.AppendSwitch("sanitize");
    }

    if (params_.enable_lightweight_detector ==
        EnableLightweightDetector::kYes) {
      cmd_line.AppendSwitch("enable-lightweight-detector");
    }

    base::LaunchOptions options;
#if BUILDFLAG(IS_WIN)
    options.start_hidden = true;
#endif  // BUILDFLAG(IS_WIN)
    base::Process process =
        base::SpawnMultiProcessTestChild("CrashingProcess", cmd_line, options);

#if !BUILDFLAG(IS_ANDROID)
    int exit_code = -1;
    EXPECT_TRUE(WaitForMultiprocessTestChildExit(
        process, TestTimeouts::action_max_timeout(), &exit_code));
    EXPECT_NE(exit_code, kSuccess);
    return (exit_code != kSuccess);
#else
    // TODO(crbug.com/40632533): Android's implementation of
    // WaitForMultiprocessTestChildExit can't detect child process crashes, this
    // can be fixed after minSdkVersion >= Q.
    for (int i = 0; i < TestTimeouts::action_max_timeout().InSeconds(); i++) {
      if (kill(process.Pid(), 0) && errno == ESRCH)
        return true;
      sleep(1);
    }
    return false;
#endif
  }

  // Given a directory with a single crashpad exception, read and parse the
  // minidump and identify whether it has a GWP-ASan stream. If it successfully
  // found a minidump, it writes true to minidump_found. If it sucessfully found
  // a GWP-ASan stream in the minidump, it writes true to gwp_asan_found and
  // parses the protobuf into into proto_out.
  void readGwpAsanStreamFromCrash(const base::FilePath& database_dir,
                                  bool* minidump_found,
                                  bool* gwp_asan_found,
                                  gwp_asan::Crash* proto_out) {
    *minidump_found = *gwp_asan_found = false;
    auto database =
        crashpad::CrashReportDatabase::InitializeWithoutCreating(database_dir);

    std::vector<crashpad::CrashReportDatabase::Report> reports;
    ASSERT_EQ(database->GetPendingReports(&reports),
              crashpad::CrashReportDatabase::kNoError);
    ASSERT_EQ(reports.size(), 1U);

    crashpad::FileReader minidump_file_reader;
    ASSERT_TRUE(minidump_file_reader.Open(reports[0].file_path));

    crashpad::ProcessSnapshotMinidump minidump_process_snapshot;
    ASSERT_TRUE(minidump_process_snapshot.Initialize(&minidump_file_reader));
    *minidump_found = true;

    auto custom_streams = minidump_process_snapshot.CustomMinidumpStreams();
    for (auto* stream : custom_streams) {
      if (stream->stream_type() == static_cast<crashpad::MinidumpStreamType>(
                                       kGwpAsanMinidumpStreamType)) {
        ASSERT_TRUE(proto_out->ParseFromArray(stream->data().data(),
                                              stream->data().size()));
        *gwp_asan_found = true;
        return;
      }
    }
  }

  void checkProto(Crash_Mode mode,
                  Crash_ErrorType error_type,
                  HasAllocation has_allocation,
                  HasDeallocation has_deallocation) {
    EXPECT_TRUE(proto_.has_mode());
    EXPECT_EQ(proto_.mode(), mode);

    EXPECT_TRUE(proto_.has_error_type());
    EXPECT_EQ(proto_.error_type(), error_type);

    EXPECT_TRUE(proto_.has_allocation_address());

    EXPECT_TRUE(proto_.has_allocation_size());
    EXPECT_EQ(proto_.allocation_size(), kAllocationSize);

    EXPECT_EQ(proto_.has_allocation(), has_allocation == HasAllocation::kYes);
    if (proto_.has_allocation()) {
      EXPECT_TRUE(proto_.allocation().has_thread_id());
      EXPECT_NE(proto_.allocation().thread_id(),
                static_cast<uint64_t>(base::kInvalidThreadId));
      EXPECT_GT(proto_.allocation().stack_trace_size(), 0);
    }

    EXPECT_EQ(proto_.has_deallocation(),
              has_deallocation == HasDeallocation::kYes);
    if (proto_.has_deallocation()) {
      EXPECT_TRUE(proto_.deallocation().has_thread_id());
      EXPECT_NE(proto_.deallocation().thread_id(),
                static_cast<uint64_t>(base::kInvalidThreadId));
      EXPECT_GT(proto_.deallocation().stack_trace_size(), 0);
    }

    if (proto_.has_allocation() && proto_.has_deallocation()) {
      EXPECT_EQ(proto_.allocation().thread_id(),
                proto_.deallocation().thread_id());
    }

    if (proto_.has_region_start() || proto_.has_region_size()) {
      EXPECT_TRUE(proto_.has_region_start());
      EXPECT_TRUE(proto_.has_region_size());
      EXPECT_EQ(proto_.region_start() & (base::GetPageSize() - 1), 0U);
      // We can't have a more precise check of the region size because it
      // depends on the PartitionAlloc metadata layout.
      EXPECT_GE(proto_.region_size(),
                base::GetPageSize() * (2 * kTotalPages + 1));
      // Upper bound for number of pages reserved when requesting kTotalPages
      // worth of allocatable slots.
      constexpr size_t kTotalPagesReserved = 2 * kTotalPages;
      EXPECT_LE(proto_.region_size(),
                base::GetPageSize() * (2 * kTotalPagesReserved + 1));
    }

    EXPECT_TRUE(proto_.has_missing_metadata());
    EXPECT_FALSE(proto_.missing_metadata());

    EXPECT_TRUE(proto_.has_allocator());
    if (!strcmp(params_.allocator, "malloc"))
      EXPECT_EQ(proto_.allocator(), Crash_Allocator_MALLOC);
    else if (!strcmp(params_.allocator, "partitionalloc"))
      EXPECT_EQ(proto_.allocator(), Crash_Allocator_PARTITIONALLOC);
    else
      ASSERT_TRUE(false) << "Unknown allocator name";
  }

  gwp_asan::Crash proto_;
  TestParams params_;
  bool gwp_asan_found_;
};

class CrashHandlerTest : public BaseCrashHandlerTest {};

#if defined(ADDRESS_SANITIZER) && (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID))
// ASan intercepts crashes and crashpad doesn't have a chance to see them.
#define MAYBE_DISABLED(name) DISABLED_ ##name
#else
#define MAYBE_DISABLED(name) name
#endif

TEST_P(CrashHandlerTest, MAYBE_DISABLED(UseAfterFree)) {
  ASSERT_TRUE(gwp_asan_found_);
  checkProto(Crash_Mode_CLASSIC, Crash_ErrorType_USE_AFTER_FREE,
             HasAllocation::kYes, HasDeallocation::kYes);
}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(DoubleFree)) {
  ASSERT_TRUE(gwp_asan_found_);
  checkProto(Crash_Mode_CLASSIC, Crash_ErrorType_DOUBLE_FREE,
             HasAllocation::kYes, HasDeallocation::kYes);
}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(Underflow)) {
  ASSERT_TRUE(gwp_asan_found_);
  checkProto(Crash_Mode_CLASSIC, Crash_ErrorType_BUFFER_UNDERFLOW,
             HasAllocation::kYes, HasDeallocation::kNo);
}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(Overflow)) {
  ASSERT_TRUE(gwp_asan_found_);
  checkProto(Crash_Mode_CLASSIC, Crash_ErrorType_BUFFER_OVERFLOW,
             HasAllocation::kYes, HasDeallocation::kNo);
}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(FreeInvalidAddress)) {
  ASSERT_TRUE(gwp_asan_found_);
  checkProto(Crash_Mode_CLASSIC, Crash_ErrorType_FREE_INVALID_ADDRESS,
             HasAllocation::kYes, HasDeallocation::kNo);
  EXPECT_TRUE(proto_.has_free_invalid_address());
}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(MissingMetadata)) {
  ASSERT_TRUE(gwp_asan_found_);

  ASSERT_TRUE(proto_.has_missing_metadata());
  EXPECT_TRUE(proto_.missing_metadata());

  EXPECT_FALSE(proto_.has_error_type());
  EXPECT_FALSE(proto_.has_allocation_address());
  EXPECT_FALSE(proto_.has_allocation_size());
  EXPECT_FALSE(proto_.has_allocation());
  EXPECT_FALSE(proto_.has_deallocation());
  EXPECT_FALSE(proto_.has_free_invalid_address());
  EXPECT_TRUE(proto_.has_region_start());
  EXPECT_TRUE(proto_.has_region_size());
}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(UnrelatedException)) {
  ASSERT_FALSE(gwp_asan_found_);
}

INSTANTIATE_TEST_SUITE_P(VaryAllocator,
                         CrashHandlerTest,
                         testing::Values(
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
                             TestParams("malloc",
                                        ShouldSanitize::kYes,
                                        EnableLightweightDetector::kNo),
#endif
                             TestParams("malloc",
                                        ShouldSanitize::kNo,
                                        EnableLightweightDetector::kNo),
                             TestParams("partitionalloc",
                                        ShouldSanitize::kNo,
                                        EnableLightweightDetector::kNo)));

// ASan hides the fault address from the analyzer.
// The detector is not used on 32-bit systems because pointers there aren't big
// enough to safely store metadata IDs.
#if !defined(ADDRESS_SANITIZER) && defined(ARCH_CPU_64_BITS)
class LightweightDetectorCrashHandlerTest : public BaseCrashHandlerTest {};

TEST_P(LightweightDetectorCrashHandlerTest, LightweightDetectorUseAfterFree) {
  ASSERT_TRUE(gwp_asan_found_);

  checkProto(Crash_Mode_LIGHTWEIGHT_DETECTOR_BRP,
             Crash_ErrorType_USE_AFTER_FREE, HasAllocation::kNo,
             HasDeallocation::kYes);
}

INSTANTIATE_TEST_SUITE_P(VarySanitization,
                         LightweightDetectorCrashHandlerTest,
                         testing::Values(
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
                             TestParams("partitionalloc",
                                        ShouldSanitize::kYes,
                                        EnableLightweightDetector::kYes),
#endif
                             TestParams("partitionalloc",
                                        ShouldSanitize::kNo,
                                        EnableLightweightDetector::kYes)));
#endif  // !defined(ADDRESS_SANITIZER) && defined(ARCH_CPU_64_BITS)

}  // namespace

}  // namespace internal
}  // namespace gwp_asan