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