1*bb611c8fSApple OSS Distributions //
2*bb611c8fSApple OSS Distributions // Example of providing a malloc() wrapper that returns a `bounded_ptr`.
3*bb611c8fSApple OSS Distributions //
4*bb611c8fSApple OSS Distributions // This test serves as some kind of integration test, ensuring that we're
5*bb611c8fSApple OSS Distributions // able to convert existing code using raw pointers to using `bounded_ptr`s
6*bb611c8fSApple OSS Distributions // without too much hassle. This code was lifted from existing code in XNU,
7*bb611c8fSApple OSS Distributions // and the variable names were changed to make it more generic.
8*bb611c8fSApple OSS Distributions //
9*bb611c8fSApple OSS Distributions 
10*bb611c8fSApple OSS Distributions #include <libkern/c++/bounded_ptr.h>
11*bb611c8fSApple OSS Distributions #include <cstddef>
12*bb611c8fSApple OSS Distributions #include <cstdint>
13*bb611c8fSApple OSS Distributions #include <cstdlib>
14*bb611c8fSApple OSS Distributions #include <darwintest.h>
15*bb611c8fSApple OSS Distributions #include "test_utils.h"
16*bb611c8fSApple OSS Distributions 
17*bb611c8fSApple OSS Distributions test_bounded_ptr<void>
18*bb611c8fSApple OSS Distributions bounded_malloc(std::size_t size)
19*bb611c8fSApple OSS Distributions {
20*bb611c8fSApple OSS Distributions 	void* p = std::malloc(size);
21*bb611c8fSApple OSS Distributions 	void* end = static_cast<char*>(p) + size;
22*bb611c8fSApple OSS Distributions 	test_bounded_ptr<void> with_bounds(p, p, end);
23*bb611c8fSApple OSS Distributions 	return with_bounds;
24*bb611c8fSApple OSS Distributions }
25*bb611c8fSApple OSS Distributions 
26*bb611c8fSApple OSS Distributions void
27*bb611c8fSApple OSS Distributions bounded_free(test_bounded_ptr<void> ptr)
28*bb611c8fSApple OSS Distributions {
29*bb611c8fSApple OSS Distributions 	std::free(ptr.discard_bounds());
30*bb611c8fSApple OSS Distributions }
31*bb611c8fSApple OSS Distributions 
32*bb611c8fSApple OSS Distributions struct SomeType {
33*bb611c8fSApple OSS Distributions 	std::uint32_t idx;
34*bb611c8fSApple OSS Distributions };
35*bb611c8fSApple OSS Distributions 
36*bb611c8fSApple OSS Distributions // Pretend that those functions are already part of the code base being
37*bb611c8fSApple OSS Distributions // transitioned over to `bounded_ptr`s, and we can't change their signature.
38*bb611c8fSApple OSS Distributions // The purpose of having those functions is to make sure that we're able to
39*bb611c8fSApple OSS Distributions // integrate into existing code bases with decent ease.
40*bb611c8fSApple OSS Distributions void
41*bb611c8fSApple OSS Distributions use(SomeType*)
42*bb611c8fSApple OSS Distributions {
43*bb611c8fSApple OSS Distributions }
44*bb611c8fSApple OSS Distributions void
45*bb611c8fSApple OSS Distributions require(bool condition)
46*bb611c8fSApple OSS Distributions {
47*bb611c8fSApple OSS Distributions 	if (!condition) {
48*bb611c8fSApple OSS Distributions 		std::exit(EXIT_FAILURE);
49*bb611c8fSApple OSS Distributions 	}
50*bb611c8fSApple OSS Distributions }
51*bb611c8fSApple OSS Distributions 
52*bb611c8fSApple OSS Distributions T_DECL(example_malloc, "bounded_ptr.example.malloc") {
53*bb611c8fSApple OSS Distributions 	test_bounded_ptr<SomeType> array = nullptr;
54*bb611c8fSApple OSS Distributions 	std::uint32_t count = 100;
55*bb611c8fSApple OSS Distributions 	std::uint32_t alloc_size = count * sizeof(SomeType);
56*bb611c8fSApple OSS Distributions 
57*bb611c8fSApple OSS Distributions 	// (1) must use a bounded version of malloc
58*bb611c8fSApple OSS Distributions 	// (2) must use a reinterpret_pointer_cast to go from void* to SomeType*
59*bb611c8fSApple OSS Distributions 	array = libkern::reinterpret_pointer_cast<SomeType>(bounded_malloc(alloc_size));
60*bb611c8fSApple OSS Distributions 
61*bb611c8fSApple OSS Distributions 	require(array != nullptr); // use != nullptr instead of relying on implicit conversion to bool
62*bb611c8fSApple OSS Distributions 	use(array.discard_bounds()); // must manually discard bounds here
63*bb611c8fSApple OSS Distributions 
64*bb611c8fSApple OSS Distributions 	for (std::uint32_t i = 0; i < count; i++) {
65*bb611c8fSApple OSS Distributions 		std::uint32_t& idx = array[i].idx;
66*bb611c8fSApple OSS Distributions 		idx = i;
67*bb611c8fSApple OSS Distributions 		use(&array[idx]);
68*bb611c8fSApple OSS Distributions 	}
69*bb611c8fSApple OSS Distributions 
70*bb611c8fSApple OSS Distributions 	if (array) {
71*bb611c8fSApple OSS Distributions 		bounded_free(array); // must use a bounded version of free
72*bb611c8fSApple OSS Distributions 	}
73*bb611c8fSApple OSS Distributions 
74*bb611c8fSApple OSS Distributions 	T_PASS("bounded_ptr.example.malloc test done");
75*bb611c8fSApple OSS Distributions }
76