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