xref: /xnu-8020.140.41/tests/safe_allocation_src/operator.subscript.cpp (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions //
2*27b03b36SApple OSS Distributions // Tests for
3*27b03b36SApple OSS Distributions //  T& operator[](std::ptrdiff_t n);
4*27b03b36SApple OSS Distributions //  T const& operator[](std::ptrdiff_t n) const;
5*27b03b36SApple OSS Distributions //
6*27b03b36SApple OSS Distributions 
7*27b03b36SApple OSS Distributions #include <libkern/c++/safe_allocation.h>
8*27b03b36SApple OSS Distributions #include <darwintest.h>
9*27b03b36SApple OSS Distributions #include "test_utils.h"
10*27b03b36SApple OSS Distributions #include <cstddef>
11*27b03b36SApple OSS Distributions #include <limits>
12*27b03b36SApple OSS Distributions 
13*27b03b36SApple OSS Distributions struct T {
14*27b03b36SApple OSS Distributions 	long i;
15*27b03b36SApple OSS Distributions };
16*27b03b36SApple OSS Distributions 
17*27b03b36SApple OSS Distributions template <typename RawT, typename QualT>
18*27b03b36SApple OSS Distributions static void
tests()19*27b03b36SApple OSS Distributions tests()
20*27b03b36SApple OSS Distributions {
21*27b03b36SApple OSS Distributions 	// Test the non-const version
22*27b03b36SApple OSS Distributions 	{
23*27b03b36SApple OSS Distributions 		RawT* memory = reinterpret_cast<RawT*>(malloc_allocator::allocate(10 * sizeof(RawT)));
24*27b03b36SApple OSS Distributions 		for (RawT* ptr = memory; ptr != memory + 10; ++ptr) {
25*27b03b36SApple OSS Distributions 			*ptr = RawT{ptr - memory}; // number from 0 to 9
26*27b03b36SApple OSS Distributions 		}
27*27b03b36SApple OSS Distributions 
28*27b03b36SApple OSS Distributions 		test_safe_allocation<QualT> array(memory, 10, libkern::adopt_memory);
29*27b03b36SApple OSS Distributions 		for (std::ptrdiff_t n = 0; n != 10; ++n) {
30*27b03b36SApple OSS Distributions 			QualT& element = array[n];
31*27b03b36SApple OSS Distributions 			CHECK(&element == memory + n);
32*27b03b36SApple OSS Distributions 		}
33*27b03b36SApple OSS Distributions 	}
34*27b03b36SApple OSS Distributions 
35*27b03b36SApple OSS Distributions 	// Test the const version
36*27b03b36SApple OSS Distributions 	{
37*27b03b36SApple OSS Distributions 		RawT* memory = reinterpret_cast<RawT*>(malloc_allocator::allocate(10 * sizeof(RawT)));
38*27b03b36SApple OSS Distributions 		for (RawT* ptr = memory; ptr != memory + 10; ++ptr) {
39*27b03b36SApple OSS Distributions 			*ptr = RawT{ptr - memory}; // number from 0 to 9
40*27b03b36SApple OSS Distributions 		}
41*27b03b36SApple OSS Distributions 
42*27b03b36SApple OSS Distributions 		test_safe_allocation<QualT> const array(memory, 10, libkern::adopt_memory);
43*27b03b36SApple OSS Distributions 		for (std::ptrdiff_t n = 0; n != 10; ++n) {
44*27b03b36SApple OSS Distributions 			QualT const& element = array[n];
45*27b03b36SApple OSS Distributions 			CHECK(&element == memory + n);
46*27b03b36SApple OSS Distributions 		}
47*27b03b36SApple OSS Distributions 	}
48*27b03b36SApple OSS Distributions 
49*27b03b36SApple OSS Distributions 	// Test with OOB offsets (should trap)
50*27b03b36SApple OSS Distributions 	{
51*27b03b36SApple OSS Distributions 		using Alloc = libkern::safe_allocation<RawT, malloc_allocator, tracking_trapping_policy>;
52*27b03b36SApple OSS Distributions 		RawT* memory = reinterpret_cast<RawT*>(malloc_allocator::allocate(10 * sizeof(RawT)));
53*27b03b36SApple OSS Distributions 		Alloc const array(memory, 10, libkern::adopt_memory);
54*27b03b36SApple OSS Distributions 
55*27b03b36SApple OSS Distributions 		// Negative offsets
56*27b03b36SApple OSS Distributions 		{
57*27b03b36SApple OSS Distributions 			tracking_trapping_policy::reset();
58*27b03b36SApple OSS Distributions 			(void)array[-1];
59*27b03b36SApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
60*27b03b36SApple OSS Distributions 		}
61*27b03b36SApple OSS Distributions 		{
62*27b03b36SApple OSS Distributions 			tracking_trapping_policy::reset();
63*27b03b36SApple OSS Distributions 			(void)array[-10];
64*27b03b36SApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
65*27b03b36SApple OSS Distributions 		}
66*27b03b36SApple OSS Distributions 
67*27b03b36SApple OSS Distributions 		// Too large offsets
68*27b03b36SApple OSS Distributions 		{
69*27b03b36SApple OSS Distributions 			tracking_trapping_policy::reset();
70*27b03b36SApple OSS Distributions 			(void)array[10];
71*27b03b36SApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
72*27b03b36SApple OSS Distributions 		}
73*27b03b36SApple OSS Distributions 		{
74*27b03b36SApple OSS Distributions 			tracking_trapping_policy::reset();
75*27b03b36SApple OSS Distributions 			(void)array[11];
76*27b03b36SApple OSS Distributions 			CHECK(tracking_trapping_policy::did_trap);
77*27b03b36SApple OSS Distributions 		}
78*27b03b36SApple OSS Distributions 	}
79*27b03b36SApple OSS Distributions }
80*27b03b36SApple OSS Distributions 
81*27b03b36SApple OSS Distributions T_DECL(operator_subscript, "safe_allocation.operator.subscript") {
82*27b03b36SApple OSS Distributions 	tests<T, T>();
83*27b03b36SApple OSS Distributions 	tests<T, T const>();
84*27b03b36SApple OSS Distributions }
85