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