Files
TheRock/compiler/amd-llvm/compiler-rt/lib/gwp_asan/tests/harness.h
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

143 lines
4.8 KiB
C++

//===-- harness.h -----------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef GWP_ASAN_TESTS_HARNESS_H_
#define GWP_ASAN_TESTS_HARNESS_H_
#include <stdarg.h>
#if defined(__Fuchsia__)
#ifndef ZXTEST_USE_STREAMABLE_MACROS
#define ZXTEST_USE_STREAMABLE_MACROS
#endif
#include <zxtest/zxtest.h>
namespace testing = zxtest;
// zxtest defines a different ASSERT_DEATH, taking a lambda and an error message
// if death didn't occur, versus gtest taking a statement and a string to search
// for in the dying process. zxtest doesn't define an EXPECT_DEATH, so we use
// that in the tests below (which works as intended for gtest), and we define
// EXPECT_DEATH as a wrapper for zxtest's ASSERT_DEATH. Note that zxtest drops
// the functionality for checking for a particular message in death.
#define EXPECT_DEATH(X, Y) ASSERT_DEATH(([&] { X; }), "")
#else
#include "gtest/gtest.h"
#endif
#include "gwp_asan/guarded_pool_allocator.h"
#include "gwp_asan/optional/backtrace.h"
#include "gwp_asan/optional/printf.h"
#include "gwp_asan/optional/segv_handler.h"
#include "gwp_asan/options.h"
namespace gwp_asan {
namespace test {
// This printf-function getter allows other platforms (e.g. Android) to define
// their own signal-safe Printf function. In LLVM, we use
// `optional/printf_sanitizer_common.cpp` which supplies the __sanitizer::Printf
// for this purpose.
Printf_t getPrintfFunction();
}; // namespace test
}; // namespace gwp_asan
char *AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA);
void DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr);
void DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr);
void TouchMemory(void *Ptr);
void CheckOnlyOneGwpAsanCrash(const std::string &OutputBuffer);
class DefaultGuardedPoolAllocator : public ::testing::Test {
public:
void SetUp() override {
gwp_asan::options::Options Opts;
MaxSimultaneousAllocations = Opts.MaxSimultaneousAllocations;
GPA.init(Opts);
}
void TearDown() override { GPA.uninitTestOnly(); }
protected:
gwp_asan::GuardedPoolAllocator GPA;
decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
MaxSimultaneousAllocations;
};
class CustomGuardedPoolAllocator : public ::testing::Test {
public:
void
InitNumSlots(decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
MaxSimultaneousAllocationsArg) {
gwp_asan::options::Options Opts;
Opts.MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
GPA.init(Opts);
}
void TearDown() override { GPA.uninitTestOnly(); }
protected:
gwp_asan::GuardedPoolAllocator GPA;
decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
MaxSimultaneousAllocations;
};
class BacktraceGuardedPoolAllocator
: public ::testing::TestWithParam</* Recoverable */ bool> {
public:
void SetUp() override {
gwp_asan::options::Options Opts;
Opts.Backtrace = gwp_asan::backtrace::getBacktraceFunction();
GPA.init(Opts);
// In recoverable mode, capture GWP-ASan logs to an internal buffer so that
// we can search it in unit tests. For non-recoverable tests, the default
// buffer is fine, as any tests should be EXPECT_DEATH()'d.
Recoverable = GetParam();
gwp_asan::Printf_t PrintfFunction = PrintfToBuffer;
GetOutputBuffer().clear();
if (!Recoverable)
PrintfFunction = gwp_asan::test::getPrintfFunction();
gwp_asan::segv_handler::installSignalHandlers(
&GPA, PrintfFunction, gwp_asan::backtrace::getPrintBacktraceFunction(),
gwp_asan::backtrace::getSegvBacktraceFunction(),
/* Recoverable */ Recoverable);
}
void TearDown() override {
GPA.uninitTestOnly();
gwp_asan::segv_handler::uninstallSignalHandlers();
}
protected:
static std::string &GetOutputBuffer() {
static std::string Buffer;
return Buffer;
}
__attribute__((format(printf, 1, 2))) static void
PrintfToBuffer(const char *Format, ...) {
va_list AP;
va_start(AP, Format);
char Buffer[8192];
vsnprintf(Buffer, sizeof(Buffer), Format, AP);
GetOutputBuffer() += Buffer;
va_end(AP);
}
gwp_asan::GuardedPoolAllocator GPA;
bool Recoverable;
};
// https://github.com/google/googletest/blob/master/docs/advanced.md#death-tests-and-threads
using DefaultGuardedPoolAllocatorDeathTest = DefaultGuardedPoolAllocator;
using CustomGuardedPoolAllocatorDeathTest = CustomGuardedPoolAllocator;
using BacktraceGuardedPoolAllocatorDeathTest = BacktraceGuardedPoolAllocator;
#endif // GWP_ASAN_TESTS_HARNESS_H_