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