summaryrefslogtreecommitdiff
path: root/gwp_asan/tests
diff options
context:
space:
mode:
Diffstat (limited to 'gwp_asan/tests')
-rw-r--r--gwp_asan/tests/alignment.cpp124
-rw-r--r--gwp_asan/tests/basic.cpp31
-rw-r--r--gwp_asan/tests/crash_handler_api.cpp5
3 files changed, 130 insertions, 30 deletions
diff --git a/gwp_asan/tests/alignment.cpp b/gwp_asan/tests/alignment.cpp
index 2489ff8..5f24a9a 100644
--- a/gwp_asan/tests/alignment.cpp
+++ b/gwp_asan/tests/alignment.cpp
@@ -6,41 +6,109 @@
//
//===----------------------------------------------------------------------===//
+#include "gwp_asan/guarded_pool_allocator.h"
#include "gwp_asan/tests/harness.h"
-#include "gwp_asan/utilities.h"
-#include <vector>
+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);
+ }
+};
-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},
- };
+// 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));
+}
- for (const auto &KV : AskedSizeToAlignedSize) {
- EXPECT_EQ(KV.second,
- gwp_asan::rightAlignedAllocationSize(
- KV.first, gwp_asan::AlignmentStrategy::POWER_OF_TWO));
- }
+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));
+
+ // 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, 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, 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));
- for (const auto &KV : AskedSizeToAlignedSize) {
- EXPECT_EQ(KV.second, gwp_asan::rightAlignedAllocationSize(
- KV.first, gwp_asan::AlignmentStrategy::BIONIC));
- }
+ // 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, PerfectAlignment) {
- for (size_t i = 1; i <= 4096; ++i) {
- EXPECT_EQ(i, gwp_asan::rightAlignedAllocationSize(
- i, gwp_asan::AlignmentStrategy::PERFECT));
- }
+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/gwp_asan/tests/basic.cpp b/gwp_asan/tests/basic.cpp
index 29f420d..88e7ed1 100644
--- a/gwp_asan/tests/basic.cpp
+++ b/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/gwp_asan/tests/crash_handler_api.cpp b/gwp_asan/tests/crash_handler_api.cpp
index cb30636..4cdb569 100644
--- a/gwp_asan/tests/crash_handler_api.cpp
+++ b/gwp_asan/tests/crash_handler_api.cpp
@@ -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));