about summary refs log tree commit diff homepage
path: root/unittests/KDAlloc
diff options
context:
space:
mode:
authorDaniel Schemmel <daniel@schemmel.net>2023-06-28 23:21:39 +0000
committerCristian Cadar <c.cadar@imperial.ac.uk>2023-07-10 16:53:14 +0200
commit125fb26ceae09da92d17505071cad881ad31ba88 (patch)
tree9230b368910a6e271e554ea0944fbca4f15216fd /unittests/KDAlloc
parent3034ae5878f2b1f3216ab1a7d2706edf27c8ae4c (diff)
downloadklee-125fb26ceae09da92d17505071cad881ad31ba88.tar.gz
Simplify KDAlloc tests
Diffstat (limited to 'unittests/KDAlloc')
-rw-r--r--unittests/KDAlloc/CMakeLists.txt2
-rw-r--r--unittests/KDAlloc/allocate.cpp8
-rw-r--r--unittests/KDAlloc/randomtest.cpp43
-rw-r--r--unittests/KDAlloc/reuse.cpp8
-rw-r--r--unittests/KDAlloc/sample.cpp8
-rw-r--r--unittests/KDAlloc/stacktest.cpp43
6 files changed, 22 insertions, 90 deletions
diff --git a/unittests/KDAlloc/CMakeLists.txt b/unittests/KDAlloc/CMakeLists.txt
index 87d016d7..0d19891b 100644
--- a/unittests/KDAlloc/CMakeLists.txt
+++ b/unittests/KDAlloc/CMakeLists.txt
@@ -5,7 +5,7 @@ add_klee_unit_test(KDAllocTest
   rusage.cpp
   sample.cpp
   stacktest.cpp)
-target_compile_definitions(KDAllocTest PRIVATE KDALLOC_CHECKED USE_KDALLOC USE_GTEST_INSTEAD_OF_MAIN)
+target_compile_definitions(KDAllocTest PRIVATE USE_GTEST_INSTEAD_OF_MAIN)
 target_compile_definitions(KDAllocTest PRIVATE ${KLEE_COMPONENT_CXX_DEFINES})
 target_compile_options(KDAllocTest PRIVATE ${KLEE_COMPONENT_CXX_FLAGS})
 target_include_directories(KDAllocTest PRIVATE ${KLEE_INCLUDE_DIRS})
\ No newline at end of file
diff --git a/unittests/KDAlloc/allocate.cpp b/unittests/KDAlloc/allocate.cpp
index c691e114..eb13d5e1 100644
--- a/unittests/KDAlloc/allocate.cpp
+++ b/unittests/KDAlloc/allocate.cpp
@@ -18,11 +18,7 @@
 #include <iomanip>
 #include <iostream>
 
-#if defined(USE_GTEST_INSTEAD_OF_MAIN)
-int allocate_sample_test() {
-#else
-int main() {
-#endif
+void allocate_sample_test() {
   // initialize a factory and an associated allocator (1 MiB and no quarantine)
   klee::kdalloc::AllocatorFactory factory(static_cast<std::size_t>(1) << 20, 0);
   klee::kdalloc::Allocator allocator = factory.makeAllocator();
@@ -62,4 +58,6 @@ int main() {
 TEST(KDAllocDeathTest, AllocateSample) {
   ASSERT_EXIT(allocate_sample_test(), ::testing::ExitedWithCode(0), "");
 }
+#else
+int main() { allocate_sample_test(); }
 #endif
diff --git a/unittests/KDAlloc/randomtest.cpp b/unittests/KDAlloc/randomtest.cpp
index 4d2eca9c..c926fb84 100644
--- a/unittests/KDAlloc/randomtest.cpp
+++ b/unittests/KDAlloc/randomtest.cpp
@@ -29,9 +29,7 @@ namespace {
 class RandomTest {
   xoshiro512 rng;
 
-#if defined(USE_KDALLOC)
   klee::kdalloc::Allocator allocator;
-#endif
 
   std::vector<std::pair<void *, std::size_t>> allocations;
 
@@ -44,16 +42,10 @@ public:
   std::uint64_t deallocation_count = 0;
 
   RandomTest(std::uint64_t seed = 0x31337)
-      : rng(seed)
-#if defined(USE_KDALLOC)
-        ,
-        allocator(klee::kdalloc::AllocatorFactory(
-            static_cast<std::size_t>(1) << 42, 0))
-#endif
-        ,
+      : rng(seed), allocator(klee::kdalloc::AllocatorFactory(
+                       static_cast<std::size_t>(1) << 42, 0)),
         allocation_bin_distribution(0.3),
-        large_allocation_distribution(0.00003) {
-  }
+        large_allocation_distribution(0.00003) {}
 
   void run(std::uint64_t const iterations) {
     std::uniform_int_distribution<std::uint32_t> choice(0, 999);
@@ -77,7 +69,6 @@ public:
     while (!allocations.empty()) {
       auto choice = std::uniform_int_distribution<std::size_t>(
           0, allocations.size() - 1)(rng);
-#if defined(USE_KDALLOC)
       assert(allocator.locationInfo(allocations[choice].first, 1) ==
              klee::kdalloc::LocationInfo::LI_AllocatedOrQuarantined);
       assert(allocator.locationInfo(allocations[choice].first,
@@ -89,9 +80,6 @@ public:
       assert(allocator.locationInfo(allocations[choice].first,
                                     allocations[choice].second) ==
              klee::kdalloc::LocationInfo::LI_Unallocated);
-#else
-      free(allocations[choice].first);
-#endif
       allocations[choice] = allocations.back();
       allocations.pop_back();
     }
@@ -106,11 +94,7 @@ public:
     auto max = static_cast<std::size_t>(1) << (bin + 2);
     auto size = std::uniform_int_distribution<std::size_t>(min, max)(rng);
 
-#if defined(USE_KDALLOC)
     allocations.emplace_back(allocator.allocate(size), size);
-#else
-    allocations.emplace_back(std::malloc(size), size);
-#endif
     if (allocations.size() > maximum_concurrent_allocations) {
       maximum_concurrent_allocations = allocations.size();
     }
@@ -122,11 +106,7 @@ public:
       size = large_allocation_distribution(rng) + 4097;
     }
 
-#if defined(USE_KDALLOC)
     allocations.emplace_back(allocator.allocate(size), size);
-#else
-    allocations.emplace_back(std::malloc(size), size);
-#endif
     if (allocations.size() > maximum_concurrent_allocations) {
       maximum_concurrent_allocations = allocations.size();
     }
@@ -138,27 +118,14 @@ public:
     }
     auto choice = std::uniform_int_distribution<std::size_t>(
         0, allocations.size() - 1)(rng);
-#if defined(USE_KDALLOC)
     allocator.free(allocations[choice].first, allocations[choice].second);
-#else
-    free(allocations[choice].first);
-#endif
     allocations[choice] = allocations.back();
     allocations.pop_back();
   }
 };
 } // namespace
 
-#if defined(USE_GTEST_INSTEAD_OF_MAIN)
-int random_test() {
-#else
-int main() {
-#endif
-#if defined(USE_KDALLOC)
-  std::cout << "Using kdalloc\n";
-#else
-  std::cout << "Using std::malloc\n";
-#endif
+void random_test() {
   auto start = std::chrono::steady_clock::now();
 
   RandomTest tester;
@@ -184,4 +151,6 @@ int main() {
 TEST(KDAllocDeathTest, Random) {
   ASSERT_EXIT(random_test(), ::testing::ExitedWithCode(0), "");
 }
+#else
+int main() { random_test(); }
 #endif
diff --git a/unittests/KDAlloc/reuse.cpp b/unittests/KDAlloc/reuse.cpp
index d886a9d6..e6c79ddc 100644
--- a/unittests/KDAlloc/reuse.cpp
+++ b/unittests/KDAlloc/reuse.cpp
@@ -19,11 +19,7 @@
 #include <iostream>
 #include <unordered_set>
 
-#if defined(USE_GTEST_INSTEAD_OF_MAIN)
-int reuse_test() {
-#else
-int main() {
-#endif
+void reuse_test() {
   {
     static const std::size_t size = 8;
     static const std::uint32_t quarantine = 0;
@@ -131,4 +127,6 @@ int main() {
 TEST(KDAllocDeathTest, Reuse) {
   ASSERT_EXIT(reuse_test(), ::testing::ExitedWithCode(0), "");
 }
+#else
+int main() { reuse_test(); }
 #endif
diff --git a/unittests/KDAlloc/sample.cpp b/unittests/KDAlloc/sample.cpp
index 365e7e21..ddf8b75d 100644
--- a/unittests/KDAlloc/sample.cpp
+++ b/unittests/KDAlloc/sample.cpp
@@ -15,11 +15,7 @@
 
 #include <cassert>
 
-#if defined(USE_GTEST_INSTEAD_OF_MAIN)
-int sample_test() {
-#else
-int main() {
-#endif
+void sample_test() {
   // initialize a factory and an associated allocator (1 TiB and no quarantine)
   klee::kdalloc::AllocatorFactory factory(static_cast<std::size_t>(1) << 40, 0);
   klee::kdalloc::Allocator allocator = factory.makeAllocator();
@@ -64,4 +60,6 @@ int main() {
 TEST(KDAllocDeathTest, Sample) {
   ASSERT_EXIT(sample_test(), ::testing::ExitedWithCode(0), "");
 }
+#else
+int main() { sample_test(); }
 #endif
diff --git a/unittests/KDAlloc/stacktest.cpp b/unittests/KDAlloc/stacktest.cpp
index 2844bf11..e2251b76 100644
--- a/unittests/KDAlloc/stacktest.cpp
+++ b/unittests/KDAlloc/stacktest.cpp
@@ -29,9 +29,7 @@ namespace {
 class RandomTest {
   xoshiro512 rng;
 
-#if defined(USE_KDALLOC)
   klee::kdalloc::StackAllocator allocator;
-#endif
 
   std::vector<std::pair<void *, std::size_t>> allocations;
 
@@ -44,16 +42,10 @@ public:
   std::uint64_t deallocation_count = 0;
 
   RandomTest(std::mt19937_64::result_type seed = 0x31337)
-      : rng(seed)
-#if defined(USE_KDALLOC)
-        ,
-        allocator(klee::kdalloc::StackAllocatorFactory(
-            static_cast<std::size_t>(1) << 42, 0))
-#endif
-        ,
+      : rng(seed), allocator(klee::kdalloc::StackAllocatorFactory(
+                       static_cast<std::size_t>(1) << 42, 0)),
         allocation_bin_distribution(0.3),
-        large_allocation_distribution(0.00003) {
-  }
+        large_allocation_distribution(0.00003) {}
 
   void run(std::uint64_t const iterations) {
     allocations.reserve((iterations * 7) / 10);
@@ -76,11 +68,7 @@ public:
 
   void cleanup() {
     while (!allocations.empty()) {
-#if defined(USE_KDALLOC)
       allocator.free(allocations.back().first, allocations.back().second);
-#else
-      free(allocations.back().first);
-#endif
       allocations.pop_back();
     }
   }
@@ -94,11 +82,7 @@ public:
     auto max = static_cast<std::size_t>(1) << (bin + 2);
     auto size = std::uniform_int_distribution<std::size_t>(min, max)(rng);
 
-#if defined(USE_KDALLOC)
     allocations.emplace_back(allocator.allocate(size), size);
-#else
-    allocations.emplace_back(std::malloc(size), size);
-#endif
     if (allocations.size() > maximum_concurrent_allocations) {
       maximum_concurrent_allocations = allocations.size();
     }
@@ -110,11 +94,7 @@ public:
       size = large_allocation_distribution(rng) + 4097;
     }
 
-#if defined(USE_KDALLOC)
     allocations.emplace_back(allocator.allocate(size), size);
-#else
-    allocations.emplace_back(std::malloc(size), size);
-#endif
     if (allocations.size() > maximum_concurrent_allocations) {
       maximum_concurrent_allocations = allocations.size();
     }
@@ -124,26 +104,13 @@ public:
     if (allocations.empty()) {
       return;
     }
-#if defined(USE_KDALLOC)
     allocator.free(allocations.back().first, allocations.back().second);
-#else
-    free(allocations.back().first);
-#endif
     allocations.pop_back();
   }
 };
 } // namespace
 
-#if defined(USE_GTEST_INSTEAD_OF_MAIN)
-int stack_test() {
-#else
-int main() {
-#endif
-#if defined(USE_KDALLOC)
-  std::cout << "Using kdalloc\n";
-#else
-  std::cout << "Using std::malloc\n";
-#endif
+void stack_test() {
   auto start = std::chrono::steady_clock::now();
 
   RandomTest tester;
@@ -169,4 +136,6 @@ int main() {
 TEST(KDAllocDeathTest, Stack) {
   ASSERT_EXIT(stack_test(), ::testing::ExitedWithCode(0), "");
 }
+#else
+int main() { stack_test(); }
 #endif