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