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