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