1*0f4c859eSApple OSS Distributions #ifndef TESTS_SAFE_ALLOCATION_TEST_UTILS_H 2*0f4c859eSApple OSS Distributions #define TESTS_SAFE_ALLOCATION_TEST_UTILS_H 3*0f4c859eSApple OSS Distributions 4*0f4c859eSApple OSS Distributions #include <libkern/c++/bounded_ptr.h> 5*0f4c859eSApple OSS Distributions #include <libkern/c++/safe_allocation.h> 6*0f4c859eSApple OSS Distributions #include <darwintest_utils.h> 7*0f4c859eSApple OSS Distributions #include <cassert> 8*0f4c859eSApple OSS Distributions #include <cstddef> 9*0f4c859eSApple OSS Distributions #include <cstdlib> 10*0f4c859eSApple OSS Distributions #include <cstring> 11*0f4c859eSApple OSS Distributions 12*0f4c859eSApple OSS Distributions namespace { 13*0f4c859eSApple OSS Distributions struct assert_trapping_policy { 14*0f4c859eSApple OSS Distributions static void trapassert_trapping_policy15*0f4c859eSApple OSS Distributions trap(char const*) 16*0f4c859eSApple OSS Distributions { 17*0f4c859eSApple OSS Distributions assert(false); 18*0f4c859eSApple OSS Distributions } 19*0f4c859eSApple OSS Distributions }; 20*0f4c859eSApple OSS Distributions 21*0f4c859eSApple OSS Distributions struct malloc_allocator { 22*0f4c859eSApple OSS Distributions static void* allocatemalloc_allocator23*0f4c859eSApple OSS Distributions allocate(size_t n) 24*0f4c859eSApple OSS Distributions { 25*0f4c859eSApple OSS Distributions return std::malloc(n); 26*0f4c859eSApple OSS Distributions } 27*0f4c859eSApple OSS Distributions 28*0f4c859eSApple OSS Distributions static void* allocate_zeromalloc_allocator29*0f4c859eSApple OSS Distributions allocate_zero(size_t n) 30*0f4c859eSApple OSS Distributions { 31*0f4c859eSApple OSS Distributions const auto result = malloc_allocator::allocate(n); 32*0f4c859eSApple OSS Distributions std::memset(result, 0, n); 33*0f4c859eSApple OSS Distributions return result; 34*0f4c859eSApple OSS Distributions } 35*0f4c859eSApple OSS Distributions 36*0f4c859eSApple OSS Distributions static void deallocatemalloc_allocator37*0f4c859eSApple OSS Distributions deallocate(void* p, size_t n) 38*0f4c859eSApple OSS Distributions { 39*0f4c859eSApple OSS Distributions std::free(p); 40*0f4c859eSApple OSS Distributions } 41*0f4c859eSApple OSS Distributions }; 42*0f4c859eSApple OSS Distributions 43*0f4c859eSApple OSS Distributions struct tracking_allocator { 44*0f4c859eSApple OSS Distributions static void resettracking_allocator45*0f4c859eSApple OSS Distributions reset() 46*0f4c859eSApple OSS Distributions { 47*0f4c859eSApple OSS Distributions allocated_size = 0; 48*0f4c859eSApple OSS Distributions deallocated_size = 0; 49*0f4c859eSApple OSS Distributions did_allocate = false; 50*0f4c859eSApple OSS Distributions did_deallocate = false; 51*0f4c859eSApple OSS Distributions } 52*0f4c859eSApple OSS Distributions static std::size_t allocated_size; 53*0f4c859eSApple OSS Distributions static std::size_t deallocated_size; 54*0f4c859eSApple OSS Distributions static bool did_allocate; 55*0f4c859eSApple OSS Distributions static bool did_deallocate; 56*0f4c859eSApple OSS Distributions 57*0f4c859eSApple OSS Distributions static void* allocatetracking_allocator58*0f4c859eSApple OSS Distributions allocate(std::size_t n) 59*0f4c859eSApple OSS Distributions { 60*0f4c859eSApple OSS Distributions did_allocate = true; 61*0f4c859eSApple OSS Distributions allocated_size = n; 62*0f4c859eSApple OSS Distributions return std::malloc(n); 63*0f4c859eSApple OSS Distributions } 64*0f4c859eSApple OSS Distributions 65*0f4c859eSApple OSS Distributions static void* allocate_zerotracking_allocator66*0f4c859eSApple OSS Distributions allocate_zero(std::size_t n) 67*0f4c859eSApple OSS Distributions { 68*0f4c859eSApple OSS Distributions const auto result = tracking_allocator::allocate(n); 69*0f4c859eSApple OSS Distributions std::memset(result, 0, n); 70*0f4c859eSApple OSS Distributions return result; 71*0f4c859eSApple OSS Distributions } 72*0f4c859eSApple OSS Distributions 73*0f4c859eSApple OSS Distributions static void deallocatetracking_allocator74*0f4c859eSApple OSS Distributions deallocate(void* p, std::size_t n) 75*0f4c859eSApple OSS Distributions { 76*0f4c859eSApple OSS Distributions did_deallocate = true; 77*0f4c859eSApple OSS Distributions deallocated_size = n; 78*0f4c859eSApple OSS Distributions std::free(p); 79*0f4c859eSApple OSS Distributions } 80*0f4c859eSApple OSS Distributions }; 81*0f4c859eSApple OSS Distributions 82*0f4c859eSApple OSS Distributions std::size_t tracking_allocator::allocated_size = 0; 83*0f4c859eSApple OSS Distributions std::size_t tracking_allocator::deallocated_size = 0; 84*0f4c859eSApple OSS Distributions bool tracking_allocator::did_allocate = false; 85*0f4c859eSApple OSS Distributions bool tracking_allocator::did_deallocate = false; 86*0f4c859eSApple OSS Distributions 87*0f4c859eSApple OSS Distributions struct tracking_trapping_policy { 88*0f4c859eSApple OSS Distributions static void resettracking_trapping_policy89*0f4c859eSApple OSS Distributions reset() 90*0f4c859eSApple OSS Distributions { 91*0f4c859eSApple OSS Distributions did_trap = false; 92*0f4c859eSApple OSS Distributions } 93*0f4c859eSApple OSS Distributions static bool did_trap; 94*0f4c859eSApple OSS Distributions static void traptracking_trapping_policy95*0f4c859eSApple OSS Distributions trap(char const*) 96*0f4c859eSApple OSS Distributions { 97*0f4c859eSApple OSS Distributions did_trap = true; 98*0f4c859eSApple OSS Distributions } 99*0f4c859eSApple OSS Distributions }; 100*0f4c859eSApple OSS Distributions bool tracking_trapping_policy::did_trap = false; 101*0f4c859eSApple OSS Distributions 102*0f4c859eSApple OSS Distributions template <typename T> 103*0f4c859eSApple OSS Distributions using test_safe_allocation = libkern::safe_allocation<T, malloc_allocator, assert_trapping_policy>; 104*0f4c859eSApple OSS Distributions 105*0f4c859eSApple OSS Distributions template <typename T> 106*0f4c859eSApple OSS Distributions using tracked_safe_allocation = libkern::safe_allocation<T, tracking_allocator, assert_trapping_policy>; 107*0f4c859eSApple OSS Distributions 108*0f4c859eSApple OSS Distributions template <typename T> 109*0f4c859eSApple OSS Distributions using test_bounded_ptr = libkern::bounded_ptr<T, assert_trapping_policy>; 110*0f4c859eSApple OSS Distributions } // end anonymous namespace 111*0f4c859eSApple OSS Distributions 112*0f4c859eSApple OSS Distributions #define CHECK(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__) 113*0f4c859eSApple OSS Distributions 114*0f4c859eSApple OSS Distributions #endif // !TESTS_SAFE_ALLOCATION_TEST_UTILS_H 115