summaryrefslogtreecommitdiff
path: root/gnu/llvm/compiler-rt/lib/gwp_asan/tests
diff options
context:
space:
mode:
authorPatrick Wildt <patrick@cvs.openbsd.org>2021-12-17 12:26:56 +0000
committerPatrick Wildt <patrick@cvs.openbsd.org>2021-12-17 12:26:56 +0000
commit62c6e032fae72fcce51d30b40fad9b3be39763de (patch)
tree5d4bb2f9d723fb838757ad5d92688ffcd2cf86cf /gnu/llvm/compiler-rt/lib/gwp_asan/tests
parentac454a928e5fd4511477b6e325e6b0690519290a (diff)
Import LLVM 13.0.0 release.
Diffstat (limited to 'gnu/llvm/compiler-rt/lib/gwp_asan/tests')
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/CMakeLists.txt9
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/alignment.cpp126
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/backtrace.cpp53
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/basic.cpp31
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/compression.cpp2
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/crash_handler_api.cpp7
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/driver.cpp2
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/enable_disable.cpp2
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.cpp10
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.h29
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/iterate.cpp3
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/late_init.cpp2
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/mutex_test.cpp2
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/options.cpp63
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/platform_specific/printf_sanitizer_common.cpp19
-rw-r--r--gnu/llvm/compiler-rt/lib/gwp_asan/tests/slot_reuse.cpp2
16 files changed, 309 insertions, 53 deletions
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/CMakeLists.txt b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/CMakeLists.txt
index feac23df9fe..abc02a49637 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/CMakeLists.txt
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/CMakeLists.txt
@@ -5,11 +5,12 @@ set(GWP_ASAN_UNITTEST_CFLAGS
${COMPILER_RT_GTEST_CFLAGS}
-I${COMPILER_RT_SOURCE_DIR}/lib/
-O2
- -g)
+ -g
+ -fno-omit-frame-pointer)
file(GLOB GWP_ASAN_HEADERS ../*.h)
set(GWP_ASAN_UNITTESTS
- optional/printf_sanitizer_common.cpp
+ platform_specific/printf_sanitizer_common.cpp
alignment.cpp
backtrace.cpp
basic.cpp
@@ -22,7 +23,8 @@ set(GWP_ASAN_UNITTESTS
thread_contention.cpp
harness.cpp
enable_disable.cpp
- late_init.cpp)
+ late_init.cpp
+ options.cpp)
set(GWP_ASAN_UNIT_TEST_HEADERS
${GWP_ASAN_HEADERS}
@@ -47,6 +49,7 @@ if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST GWP_ASAN_SUPPORTED_ARCH)
$<TARGET_OBJECTS:RTGwpAsan.${arch}>
$<TARGET_OBJECTS:RTGwpAsanBacktraceSanitizerCommon.${arch}>
$<TARGET_OBJECTS:RTGwpAsanSegvHandler.${arch}>
+ $<TARGET_OBJECTS:RTGwpAsanOptionsParser.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommonSymbolizer.${arch}>)
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/alignment.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/alignment.cpp
index bf98f1f5833..5f24a9a1bd8 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/alignment.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/alignment.cpp
@@ -6,39 +6,109 @@
//
//===----------------------------------------------------------------------===//
+#include "gwp_asan/guarded_pool_allocator.h"
#include "gwp_asan/tests/harness.h"
-#include "gwp_asan/utilities.h"
-
-TEST(AlignmentTest, PowerOfTwo) {
- std::vector<std::pair<size_t, size_t>> AskedSizeToAlignedSize = {
- {1, 1}, {2, 2}, {3, 4}, {4, 4}, {5, 8}, {7, 8},
- {8, 8}, {9, 16}, {15, 16}, {16, 16}, {17, 32}, {31, 32},
- {32, 32}, {33, 48}, {4095, 4096}, {4096, 4096},
- };
-
- for (const auto &KV : AskedSizeToAlignedSize) {
- EXPECT_EQ(KV.second,
- gwp_asan::rightAlignedAllocationSize(
- KV.first, gwp_asan::AlignmentStrategy::POWER_OF_TWO));
+
+class AlignmentTestGPA : public gwp_asan::GuardedPoolAllocator {
+public:
+ static size_t getRequiredBackingSize(size_t Size, size_t Alignment,
+ size_t PageSize) {
+ return GuardedPoolAllocator::getRequiredBackingSize(Size, Alignment,
+ PageSize);
+ }
+ static uintptr_t alignUp(uintptr_t Ptr, size_t Alignment) {
+ return GuardedPoolAllocator::alignUp(Ptr, Alignment);
}
+ static uintptr_t alignDown(uintptr_t Ptr, size_t Alignment) {
+ return GuardedPoolAllocator::alignDown(Ptr, Alignment);
+ }
+};
+
+// Global assumptions for these tests:
+// 1. Page size is 0x1000.
+// 2. All tests assume a slot is multipage, between 0x4000 - 0x8000. While we
+// don't use multipage slots right now, this tests more boundary conditions
+// and allows us to add this feature at a later date without rewriting the
+// alignment functionality.
+// These aren't actual requirements of the allocator - but just simplifies the
+// numerics of the testing.
+TEST(AlignmentTest, LeftAlignedAllocs) {
+ // Alignment < Page Size.
+ EXPECT_EQ(0x4000, AlignmentTestGPA::alignUp(
+ /* Ptr */ 0x4000, /* Alignment */ 0x1));
+ // Alignment == Page Size.
+ EXPECT_EQ(0x4000, AlignmentTestGPA::alignUp(
+ /* Ptr */ 0x4000, /* Alignment */ 0x1000));
+ // Alignment > Page Size.
+ EXPECT_EQ(0x4000, AlignmentTestGPA::alignUp(
+ /* Ptr */ 0x4000, /* Alignment */ 0x4000));
}
-TEST(AlignmentTest, AlignBionic) {
- std::vector<std::pair<size_t, size_t>> AskedSizeToAlignedSize = {
- {1, 8}, {2, 8}, {3, 8}, {4, 8}, {5, 8}, {7, 8},
- {8, 8}, {9, 16}, {15, 16}, {16, 16}, {17, 24}, {31, 32},
- {32, 32}, {33, 40}, {4095, 4096}, {4096, 4096},
- };
+TEST(AlignmentTest, SingleByteAllocs) {
+ // Alignment < Page Size.
+ EXPECT_EQ(0x1,
+ AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x1, /* Alignment */ 0x1, /* PageSize */ 0x1000));
+ EXPECT_EQ(0x7fff, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1));
- for (const auto &KV : AskedSizeToAlignedSize) {
- EXPECT_EQ(KV.second, gwp_asan::rightAlignedAllocationSize(
- KV.first, gwp_asan::AlignmentStrategy::BIONIC));
- }
+ // Alignment == Page Size.
+ EXPECT_EQ(0x1,
+ AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x1, /* Alignment */ 0x1000, /* PageSize */ 0x1000));
+ EXPECT_EQ(0x7000, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1000));
+
+ // Alignment > Page Size.
+ EXPECT_EQ(0x3001,
+ AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x1, /* Alignment */ 0x4000, /* PageSize */ 0x1000));
+ EXPECT_EQ(0x4000, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x4000));
}
-TEST(AlignmentTest, PerfectAlignment) {
- for (size_t i = 1; i <= 4096; ++i) {
- EXPECT_EQ(i, gwp_asan::rightAlignedAllocationSize(
- i, gwp_asan::AlignmentStrategy::PERFECT));
- }
+TEST(AlignmentTest, PageSizedAllocs) {
+ // Alignment < Page Size.
+ EXPECT_EQ(0x1000,
+ AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x1000, /* Alignment */ 0x1, /* PageSize */ 0x1000));
+ EXPECT_EQ(0x7000, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1));
+
+ // Alignment == Page Size.
+ EXPECT_EQ(0x1000, AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x1000, /* Alignment */ 0x1000,
+ /* PageSize */ 0x1000));
+ EXPECT_EQ(0x7000, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1000));
+
+ // Alignment > Page Size.
+ EXPECT_EQ(0x4000, AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x1000, /* Alignment */ 0x4000,
+ /* PageSize */ 0x1000));
+ EXPECT_EQ(0x4000, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x4000));
+}
+
+TEST(AlignmentTest, MoreThanPageAllocs) {
+ // Alignment < Page Size.
+ EXPECT_EQ(0x2fff,
+ AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x2fff, /* Alignment */ 0x1, /* PageSize */ 0x1000));
+ EXPECT_EQ(0x5001, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1));
+
+ // Alignment == Page Size.
+ EXPECT_EQ(0x2fff, AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x2fff, /* Alignment */ 0x1000,
+ /* PageSize */ 0x1000));
+ EXPECT_EQ(0x5000, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1000));
+
+ // Alignment > Page Size.
+ EXPECT_EQ(0x5fff, AlignmentTestGPA::getRequiredBackingSize(
+ /* Size */ 0x2fff, /* Alignment */ 0x4000,
+ /* PageSize */ 0x1000));
+ EXPECT_EQ(0x4000, AlignmentTestGPA::alignDown(
+ /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x4000));
}
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/backtrace.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/backtrace.cpp
index b3d44270bb2..a4eb8eb9b21 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/backtrace.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/backtrace.cpp
@@ -8,6 +8,7 @@
#include <string>
+#include "gwp_asan/common.h"
#include "gwp_asan/crash_handler.h"
#include "gwp_asan/tests/harness.h"
@@ -29,7 +30,7 @@ __attribute__((optnone)) void TouchMemory(void *Ptr) {
*(reinterpret_cast<volatile char *>(Ptr)) = 7;
}
-TEST_F(BacktraceGuardedPoolAllocator, DoubleFree) {
+TEST_F(BacktraceGuardedPoolAllocatorDeathTest, DoubleFree) {
void *Ptr = AllocateMemory(GPA);
DeallocateMemory(GPA, Ptr);
@@ -44,7 +45,12 @@ TEST_F(BacktraceGuardedPoolAllocator, DoubleFree) {
ASSERT_DEATH(DeallocateMemory2(GPA, Ptr), DeathRegex);
}
-TEST_F(BacktraceGuardedPoolAllocator, UseAfterFree) {
+TEST_F(BacktraceGuardedPoolAllocatorDeathTest, UseAfterFree) {
+#if defined(__linux__) && __ARM_ARCH == 7
+ // Incomplete backtrace on Armv7 Linux
+ GTEST_SKIP();
+#endif
+
void *Ptr = AllocateMemory(GPA);
DeallocateMemory(GPA, Ptr);
@@ -76,9 +82,46 @@ TEST(Backtrace, Short) {
TEST(Backtrace, ExceedsStorableLength) {
gwp_asan::AllocationMetadata Meta;
Meta.AllocationTrace.RecordBacktrace(
- [](uintptr_t * /* TraceBuffer */, size_t /* Size */) -> size_t {
- return SIZE_MAX; // Wow, that's big!
+ [](uintptr_t *TraceBuffer, size_t Size) -> size_t {
+ // Need to inintialise the elements that will be packed.
+ memset(TraceBuffer, 0u, Size * sizeof(*TraceBuffer));
+
+ // Indicate that there were more frames, and we just didn't have enough
+ // room to store them.
+ return Size * 2;
+ });
+ // Retrieve a frame from the collected backtrace, make sure it works E2E.
+ uintptr_t TraceOutput;
+ EXPECT_EQ(gwp_asan::AllocationMetadata::kMaxTraceLengthToCollect,
+ __gwp_asan_get_allocation_trace(&Meta, &TraceOutput, 1));
+}
+
+TEST(Backtrace, ExceedsRetrievableAllocLength) {
+ gwp_asan::AllocationMetadata Meta;
+ constexpr size_t kNumFramesToStore = 3u;
+ Meta.AllocationTrace.RecordBacktrace(
+ [](uintptr_t *TraceBuffer, size_t /* Size */) -> size_t {
+ memset(TraceBuffer, kNumFramesToStore,
+ kNumFramesToStore * sizeof(*TraceBuffer));
+ return kNumFramesToStore;
+ });
+ uintptr_t TraceOutput;
+ // Ask for one element, get told that there's `kNumFramesToStore` available.
+ EXPECT_EQ(kNumFramesToStore,
+ __gwp_asan_get_allocation_trace(&Meta, &TraceOutput, 1));
+}
+
+TEST(Backtrace, ExceedsRetrievableDeallocLength) {
+ gwp_asan::AllocationMetadata Meta;
+ constexpr size_t kNumFramesToStore = 3u;
+ Meta.DeallocationTrace.RecordBacktrace(
+ [](uintptr_t *TraceBuffer, size_t /* Size */) -> size_t {
+ memset(TraceBuffer, kNumFramesToStore,
+ kNumFramesToStore * sizeof(*TraceBuffer));
+ return kNumFramesToStore;
});
uintptr_t TraceOutput;
- EXPECT_EQ(1u, __gwp_asan_get_allocation_trace(&Meta, &TraceOutput, 1));
+ // Ask for one element, get told that there's `kNumFramesToStore` available.
+ EXPECT_EQ(kNumFramesToStore,
+ __gwp_asan_get_deallocation_trace(&Meta, &TraceOutput, 1));
}
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/basic.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/basic.cpp
index 29f420d3027..88e7ed14a5c 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/basic.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/basic.cpp
@@ -39,6 +39,37 @@ TEST_F(CustomGuardedPoolAllocator, SizedAllocations) {
TEST_F(DefaultGuardedPoolAllocator, TooLargeAllocation) {
EXPECT_EQ(nullptr,
GPA.allocate(GPA.getAllocatorState()->maximumAllocationSize() + 1));
+ EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, 0));
+ EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, 1));
+ EXPECT_EQ(nullptr, GPA.allocate(0, SIZE_MAX / 2));
+ EXPECT_EQ(nullptr, GPA.allocate(1, SIZE_MAX / 2));
+ EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, SIZE_MAX / 2));
+}
+
+TEST_F(DefaultGuardedPoolAllocator, ZeroSizeAndAlignmentAllocations) {
+ void *P;
+ EXPECT_NE(nullptr, (P = GPA.allocate(0, 0)));
+ GPA.deallocate(P);
+ EXPECT_NE(nullptr, (P = GPA.allocate(1, 0)));
+ GPA.deallocate(P);
+ EXPECT_NE(nullptr, (P = GPA.allocate(0, 1)));
+ GPA.deallocate(P);
+}
+
+TEST_F(DefaultGuardedPoolAllocator, NonPowerOfTwoAlignment) {
+ EXPECT_EQ(nullptr, GPA.allocate(0, 3));
+ EXPECT_EQ(nullptr, GPA.allocate(1, 3));
+ EXPECT_EQ(nullptr, GPA.allocate(0, SIZE_MAX));
+ EXPECT_EQ(nullptr, GPA.allocate(1, SIZE_MAX));
+}
+
+// Added multi-page slots? You'll need to expand this test.
+TEST_F(DefaultGuardedPoolAllocator, TooBigForSinglePageSlots) {
+ EXPECT_EQ(nullptr, GPA.allocate(0x1001, 0));
+ EXPECT_EQ(nullptr, GPA.allocate(0x1001, 1));
+ EXPECT_EQ(nullptr, GPA.allocate(0x1001, 0x1000));
+ EXPECT_EQ(nullptr, GPA.allocate(1, 0x2000));
+ EXPECT_EQ(nullptr, GPA.allocate(0, 0x2000));
}
TEST_F(CustomGuardedPoolAllocator, AllocAllSlots) {
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/compression.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/compression.cpp
index 7a5894de125..2423c866a53 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/compression.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/compression.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
#include "gwp_asan/stack_trace_compressor.h"
-#include "gtest/gtest.h"
+#include "gwp_asan/tests/harness.h"
namespace gwp_asan {
namespace compression {
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/crash_handler_api.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/crash_handler_api.cpp
index 10a014ecd4e..4cdb5694842 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/crash_handler_api.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/crash_handler_api.cpp
@@ -16,7 +16,7 @@ using GuardedPoolAllocator = gwp_asan::GuardedPoolAllocator;
using AllocationMetadata = gwp_asan::AllocationMetadata;
using AllocatorState = gwp_asan::AllocatorState;
-class CrashHandlerAPITest : public ::testing::Test {
+class CrashHandlerAPITest : public Test {
public:
void SetUp() override { setupState(); }
@@ -29,7 +29,7 @@ protected:
size_t Slot = State.getNearestSlot(Addr);
Metadata[Slot].Addr = Addr;
- Metadata[Slot].Size = Size;
+ Metadata[Slot].RequestedSize = Size;
Metadata[Slot].IsDeallocated = IsDeallocated;
Metadata[Slot].AllocationTrace.ThreadID = 123;
Metadata[Slot].DeallocationTrace.ThreadID = 321;
@@ -80,7 +80,8 @@ protected:
__gwp_asan_get_metadata(&State, Metadata, ErrorPtr);
EXPECT_NE(nullptr, Meta);
EXPECT_EQ(Metadata[Index].Addr, __gwp_asan_get_allocation_address(Meta));
- EXPECT_EQ(Metadata[Index].Size, __gwp_asan_get_allocation_size(Meta));
+ EXPECT_EQ(Metadata[Index].RequestedSize,
+ __gwp_asan_get_allocation_size(Meta));
EXPECT_EQ(Metadata[Index].AllocationTrace.ThreadID,
__gwp_asan_get_allocation_thread_id(Meta));
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/driver.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/driver.cpp
index b402cec1126..02ab36001c7 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/driver.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/driver.cpp
@@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//
-#include "gtest/gtest.h"
+#include "gwp_asan/tests/harness.h"
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/enable_disable.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/enable_disable.cpp
index 2c6ba514f49..98da591c40d 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/enable_disable.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/enable_disable.cpp
@@ -10,7 +10,7 @@
constexpr size_t Size = 100;
-TEST_F(DefaultGuardedPoolAllocator, Fork) {
+TEST_F(DefaultGuardedPoolAllocatorDeathTest, Fork) {
void *P;
pid_t Pid = fork();
EXPECT_GE(Pid, 0);
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.cpp
index 77c25ee5a6e..e668c73057f 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.cpp
@@ -1,4 +1,12 @@
-#include "harness.h"
+//===-- harness.cpp ---------------------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "gwp_asan/tests/harness.h"
namespace gwp_asan {
namespace test {
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.h b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.h
index e47254e13c4..ed91e642de7 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.h
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.h
@@ -11,10 +11,17 @@
#include <stdarg.h>
+#if defined(__Fuchsia__)
+#include <zxtest/zxtest.h>
+using Test = ::zxtest::Test;
+#else
#include "gtest/gtest.h"
+using Test = ::testing::Test;
+#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"
@@ -24,7 +31,7 @@ namespace test {
// their own signal-safe Printf function. In LLVM, we use
// `optional/printf_sanitizer_common.cpp` which supplies the __sanitizer::Printf
// for this purpose.
-crash_handler::Printf_t getPrintfFunction();
+Printf_t getPrintfFunction();
// First call returns true, all the following calls return false.
bool OnlyOnce();
@@ -32,7 +39,7 @@ bool OnlyOnce();
}; // namespace test
}; // namespace gwp_asan
-class DefaultGuardedPoolAllocator : public ::testing::Test {
+class DefaultGuardedPoolAllocator : public Test {
public:
void SetUp() override {
gwp_asan::options::Options Opts;
@@ -51,7 +58,7 @@ protected:
MaxSimultaneousAllocations;
};
-class CustomGuardedPoolAllocator : public ::testing::Test {
+class CustomGuardedPoolAllocator : public Test {
public:
void
InitNumSlots(decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
@@ -74,28 +81,34 @@ protected:
MaxSimultaneousAllocations;
};
-class BacktraceGuardedPoolAllocator : public ::testing::Test {
+class BacktraceGuardedPoolAllocator : public Test {
public:
void SetUp() override {
gwp_asan::options::Options Opts;
Opts.setDefaults();
- Opts.Backtrace = gwp_asan::options::getBacktraceFunction();
+ Opts.Backtrace = gwp_asan::backtrace::getBacktraceFunction();
Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce();
GPA.init(Opts);
- gwp_asan::crash_handler::installSignalHandlers(
+ gwp_asan::segv_handler::installSignalHandlers(
&GPA, gwp_asan::test::getPrintfFunction(),
- gwp_asan::options::getPrintBacktraceFunction(), Opts.Backtrace);
+ gwp_asan::backtrace::getPrintBacktraceFunction(),
+ gwp_asan::backtrace::getSegvBacktraceFunction());
}
void TearDown() override {
GPA.uninitTestOnly();
- gwp_asan::crash_handler::uninstallSignalHandlers();
+ gwp_asan::segv_handler::uninstallSignalHandlers();
}
protected:
gwp_asan::GuardedPoolAllocator GPA;
};
+// 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_
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/iterate.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/iterate.cpp
index c40df15e09c..2b8635d5b36 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/iterate.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/iterate.cpp
@@ -8,6 +8,9 @@
#include "gwp_asan/tests/harness.h"
+#include <set>
+#include <vector>
+
TEST_F(CustomGuardedPoolAllocator, Iterate) {
InitNumSlots(7);
std::vector<std::pair<void *, size_t>> Allocated;
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/late_init.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/late_init.cpp
index c7d62c8f3c8..8a947270c2d 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/late_init.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/late_init.cpp
@@ -8,7 +8,7 @@
#include "gwp_asan/guarded_pool_allocator.h"
#include "gwp_asan/options.h"
-#include "gtest/gtest.h"
+#include "gwp_asan/tests/harness.h"
TEST(LateInit, CheckLateInitIsOK) {
gwp_asan::GuardedPoolAllocator GPA;
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/mutex_test.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/mutex_test.cpp
index 5bc53b90218..f68619cb01c 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/mutex_test.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/mutex_test.cpp
@@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//
#include "gwp_asan/mutex.h"
-#include "gtest/gtest.h"
+#include "gwp_asan/tests/harness.h"
#include <atomic>
#include <mutex>
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/options.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/options.cpp
new file mode 100644
index 00000000000..ffa4bca5a09
--- /dev/null
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/options.cpp
@@ -0,0 +1,63 @@
+//===-- options.cpp ---------------------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "gwp_asan/tests/harness.h"
+
+#include "gwp_asan/optional/options_parser.h"
+#include "gwp_asan/options.h"
+
+#include <stdarg.h>
+
+static char Message[1024];
+void MessageRecorder(const char *Format, ...) {
+ va_list Args;
+ va_start(Args, Format);
+ vsprintf(Message + strlen(Message), Format, Args);
+ va_end(Args);
+}
+
+TEST(GwpAsanOptionsTest, Basic) {
+ Message[0] = '\0';
+ gwp_asan::options::initOptions("Enabled=0:SampleRate=4:"
+ "InstallSignalHandlers=false",
+ MessageRecorder);
+ gwp_asan::options::Options Opts = gwp_asan::options::getOptions();
+ EXPECT_EQ('\0', Message[0]);
+ EXPECT_FALSE(Opts.Enabled);
+ EXPECT_FALSE(Opts.InstallSignalHandlers);
+ EXPECT_EQ(4, Opts.SampleRate);
+}
+
+void RunErrorTest(const char *OptionsStr, const char *ErrorNeedle) {
+ Message[0] = '\0';
+ gwp_asan::options::initOptions(OptionsStr, MessageRecorder);
+ EXPECT_NE('\0', Message[0])
+ << "Options string \"" << OptionsStr << "\" didn't generate an error.";
+ EXPECT_NE(nullptr, strstr(Message, ErrorNeedle))
+ << "Couldn't find error needle \"" << ErrorNeedle
+ << "\" in haystack created by options string \"" << OptionsStr
+ << "\". Error was: \"" << Message << "\".";
+}
+
+TEST(GwpAsanOptionsTest, FailureModes) {
+ RunErrorTest("Enabled=2", "Invalid boolean value '2' for option 'Enabled'");
+ RunErrorTest("Enabled=1:MaxSimultaneousAllocations=0",
+ "MaxSimultaneousAllocations must be > 0");
+ RunErrorTest("Enabled=1:MaxSimultaneousAllocations=-1",
+ "MaxSimultaneousAllocations must be > 0");
+ RunErrorTest("Enabled=1:SampleRate=0", "SampleRate must be > 0");
+ RunErrorTest("Enabled=1:SampleRate=-1", "SampleRate must be > 0");
+ RunErrorTest("Enabled=", "Invalid boolean value '' for option 'Enabled'");
+ RunErrorTest("==", "Unknown option '=='");
+ RunErrorTest("Enabled==0", "Invalid boolean value '=0' for option 'Enabled'");
+ RunErrorTest("Enabled:", "Expected '=' when parsing option 'Enabled:'");
+ RunErrorTest("Enabled:=", "Expected '=' when parsing option 'Enabled:='");
+ RunErrorTest("SampleRate=NOT_A_NUMBER",
+ "Invalid integer value 'NOT_A_NUMBER' for option 'SampleRate'");
+ RunErrorTest("NOT_A_VALUE=0", "Unknown option 'NOT_A_VALUE");
+}
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/platform_specific/printf_sanitizer_common.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/platform_specific/printf_sanitizer_common.cpp
new file mode 100644
index 00000000000..102b1dbfe51
--- /dev/null
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/platform_specific/printf_sanitizer_common.cpp
@@ -0,0 +1,19 @@
+//===-- printf_sanitizer_common.cpp -----------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "gwp_asan/optional/printf.h"
+
+#include "sanitizer_common/sanitizer_common.h"
+
+namespace gwp_asan {
+namespace test {
+
+Printf_t getPrintfFunction() { return __sanitizer::Printf; }
+
+} // namespace test
+} // namespace gwp_asan
diff --git a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/slot_reuse.cpp b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/slot_reuse.cpp
index ee4b6713696..f2a77b094a8 100644
--- a/gnu/llvm/compiler-rt/lib/gwp_asan/tests/slot_reuse.cpp
+++ b/gnu/llvm/compiler-rt/lib/gwp_asan/tests/slot_reuse.cpp
@@ -8,6 +8,8 @@
#include "gwp_asan/tests/harness.h"
+#include <set>
+
void singleByteGoodAllocDealloc(gwp_asan::GuardedPoolAllocator *GPA) {
void *Ptr = GPA->allocate(1);
EXPECT_NE(nullptr, Ptr);