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