chromium/components/gwp_asan/crash_handler/crash_handler_unittest.cc

// 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 <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 =;
constexpr int kSuccess =;
constexpr size_t kTotalPages =;

#if !BUILDFLAG(IS_ANDROID)
int HandlerMainAdaptor(int argc, char* argv[]) {}

// Child process that runs the crashpad handler.
MULTIPROCESS_TEST_MAIN(CrashpadHandler) {}
#endif  // !BUILDFLAG(IS_ANDROID)

// Child process that launches the crashpad handler and then crashes.
MULTIPROCESS_TEST_MAIN(CrashingProcess) {}

enum class ShouldSanitize : bool {};
enum class EnableLightweightDetector : bool {};
enum class HasAllocation : bool {};
enum class HasDeallocation : bool {};

struct TestParams {};

class BaseCrashHandlerTest : public base::MultiProcessTest,
                             public testing::WithParamInterface<TestParams> {};

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
#else
#define MAYBE_DISABLED(name)
#endif

TEST_P(CrashHandlerTest, MAYBE_DISABLED(UseAfterFree)) {}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(DoubleFree)) {}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(Underflow)) {}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(Overflow)) {}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(FreeInvalidAddress)) {}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(MissingMetadata)) {}

TEST_P(CrashHandlerTest, MAYBE_DISABLED(UnrelatedException)) {}

INSTANTIATE_TEST_SUITE_P();

// 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) {}

INSTANTIATE_TEST_SUITE_P();
#endif  // !defined(ADDRESS_SANITIZER) && defined(ARCH_CPU_64_BITS)

}  // namespace

}  // namespace internal
}  // namespace gwp_asan