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