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