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