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