1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions * Tests for reinterpret_span_cast template in cxx_safe_buffers.h
3*43a90889SApple OSS Distributions */
4*43a90889SApple OSS Distributions #include <os/cxx_safe_buffers.h>
5*43a90889SApple OSS Distributions #include <vector>
6*43a90889SApple OSS Distributions #include <darwintest.h>
7*43a90889SApple OSS Distributions #include <darwintest_utils.h>
8*43a90889SApple OSS Distributions #define CHECK(...) T_ASSERT_TRUE((__VA_ARGS__), # __VA_ARGS__)
9*43a90889SApple OSS Distributions
10*43a90889SApple OSS Distributions struct A {
11*43a90889SApple OSS Distributions int a[2];
12*43a90889SApple OSS Distributions };
13*43a90889SApple OSS Distributions
14*43a90889SApple OSS Distributions struct B {
15*43a90889SApple OSS Distributions int b[3];
16*43a90889SApple OSS Distributions };
17*43a90889SApple OSS Distributions
18*43a90889SApple OSS Distributions struct C : B {
19*43a90889SApple OSS Distributions int c[3];
20*43a90889SApple OSS Distributions };
21*43a90889SApple OSS Distributions
22*43a90889SApple OSS Distributions struct D {
23*43a90889SApple OSS Distributions std::uint8_t a[2];
24*43a90889SApple OSS Distributions };
25*43a90889SApple OSS Distributions
26*43a90889SApple OSS Distributions struct NoPadding {
27*43a90889SApple OSS Distributions char a;
28*43a90889SApple OSS Distributions char b;
29*43a90889SApple OSS Distributions
30*43a90889SApple OSS Distributions int c;
31*43a90889SApple OSS Distributions };
32*43a90889SApple OSS Distributions
33*43a90889SApple OSS Distributions struct WithPaddingMiddle {
34*43a90889SApple OSS Distributions char a;
35*43a90889SApple OSS Distributions int b;
36*43a90889SApple OSS Distributions
37*43a90889SApple OSS Distributions char c;
38*43a90889SApple OSS Distributions };
39*43a90889SApple OSS Distributions
40*43a90889SApple OSS Distributions struct PaddingEnd {
41*43a90889SApple OSS Distributions int a;
42*43a90889SApple OSS Distributions char c;
43*43a90889SApple OSS Distributions };
44*43a90889SApple OSS Distributions
45*43a90889SApple OSS Distributions
46*43a90889SApple OSS Distributions static void
tests()47*43a90889SApple OSS Distributions tests()
48*43a90889SApple OSS Distributions {
49*43a90889SApple OSS Distributions {
50*43a90889SApple OSS Distributions // convert span<int> to span<byte>
51*43a90889SApple OSS Distributions std::array<int, 3> a1{{1, 2, 3}};
52*43a90889SApple OSS Distributions std::span<int> sp{a1}; // static-extent std::span
53*43a90889SApple OSS Distributions std::span<std::byte> writable_sp = os::reinterpret_span_cast<std::byte>(sp);
54*43a90889SApple OSS Distributions std::span<const std::byte> nonwritable_sp = os::reinterpret_span_cast<const std::byte>(sp);
55*43a90889SApple OSS Distributions CHECK(writable_sp.size() == sp.size_bytes() && nonwritable_sp.size() == sp.size_bytes());
56*43a90889SApple OSS Distributions }
57*43a90889SApple OSS Distributions
58*43a90889SApple OSS Distributions {
59*43a90889SApple OSS Distributions // convert span<byte> to span<A>
60*43a90889SApple OSS Distributions std::vector<std::byte> vec {std::byte{0}, std::byte{1}, std::byte{2}, std::byte{3},
61*43a90889SApple OSS Distributions std::byte{4}, std::byte{5}, std::byte{6}, std::byte{7}};
62*43a90889SApple OSS Distributions std::span<std::byte> sp{vec}; // dynamic-extent std::span
63*43a90889SApple OSS Distributions std::span<A> span_a = os::reinterpret_span_cast<A>(sp);
64*43a90889SApple OSS Distributions CHECK(sp.size() == span_a.size_bytes());
65*43a90889SApple OSS Distributions }
66*43a90889SApple OSS Distributions
67*43a90889SApple OSS Distributions {
68*43a90889SApple OSS Distributions // convert to a span of unrelated type
69*43a90889SApple OSS Distributions std::array<A, 3> arr;
70*43a90889SApple OSS Distributions std::span<A> span_a = arr;
71*43a90889SApple OSS Distributions std::span<B> span_b = os::reinterpret_span_cast<B>(span_a);
72*43a90889SApple OSS Distributions CHECK(span_b.size() == 2);
73*43a90889SApple OSS Distributions }
74*43a90889SApple OSS Distributions
75*43a90889SApple OSS Distributions {
76*43a90889SApple OSS Distributions // convert to a span of extended type
77*43a90889SApple OSS Distributions B array[4];
78*43a90889SApple OSS Distributions std::span<B> span_b = array;
79*43a90889SApple OSS Distributions std::span<C> span_c = os::reinterpret_span_cast<C>(span_b);
80*43a90889SApple OSS Distributions CHECK(2 * span_c.size() == span_b.size());
81*43a90889SApple OSS Distributions }
82*43a90889SApple OSS Distributions
83*43a90889SApple OSS Distributions {
84*43a90889SApple OSS Distributions //convert to a span of base type
85*43a90889SApple OSS Distributions C array[4];
86*43a90889SApple OSS Distributions std::span<C> span_c = array;
87*43a90889SApple OSS Distributions std::span<B> span_b = os::reinterpret_span_cast<B>(span_c);
88*43a90889SApple OSS Distributions CHECK(2 * span_c.size() == span_b.size());
89*43a90889SApple OSS Distributions }
90*43a90889SApple OSS Distributions {
91*43a90889SApple OSS Distributions std::array<std::uint8_t, 12> buf;
92*43a90889SApple OSS Distributions std::span<std::uint8_t> sp = buf;
93*43a90889SApple OSS Distributions std::span<D> span_d = os::reinterpret_span_cast<D>(sp);
94*43a90889SApple OSS Distributions CHECK(span_d.size() == 6);
95*43a90889SApple OSS Distributions }
96*43a90889SApple OSS Distributions }
97*43a90889SApple OSS Distributions
98*43a90889SApple OSS Distributions static void
trapping_test()99*43a90889SApple OSS Distributions trapping_test()
100*43a90889SApple OSS Distributions {
101*43a90889SApple OSS Distributions pid_t pid = fork(); // Fork a new process
102*43a90889SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pid, "forked %d", pid);
103*43a90889SApple OSS Distributions
104*43a90889SApple OSS Distributions if (pid == 0) {
105*43a90889SApple OSS Distributions // convert to a span of unrelated type
106*43a90889SApple OSS Distributions A array[2];
107*43a90889SApple OSS Distributions std::span<A> span_a = {array, 2};
108*43a90889SApple OSS Distributions // This invocation will cause a run time trap in child process.
109*43a90889SApple OSS Distributions std::span<B> span_b = os::reinterpret_span_cast<B>(span_a);
110*43a90889SApple OSS Distributions
111*43a90889SApple OSS Distributions exit(0); // Exit child process
112*43a90889SApple OSS Distributions }
113*43a90889SApple OSS Distributions
114*43a90889SApple OSS Distributions int status = 0, signal = 0;
115*43a90889SApple OSS Distributions
116*43a90889SApple OSS Distributions // wait for the child process to finish
117*43a90889SApple OSS Distributions T_ASSERT_FALSE(dt_waitpid(pid, &status, &signal, 0), "wait for child (%d) complete with signal %d", pid, signal);
118*43a90889SApple OSS Distributions // child process must trigger an execution trap
119*43a90889SApple OSS Distributions T_ASSERT_TRUE(WIFSIGNALED(signal), "Child process successfully triggered an execution trap");
120*43a90889SApple OSS Distributions }
121*43a90889SApple OSS Distributions
122*43a90889SApple OSS Distributions
123*43a90889SApple OSS Distributions T_DECL(reinterpret_span_cast, "cxx_safe_buffers.reinterpret_span_cast")
124*43a90889SApple OSS Distributions {
125*43a90889SApple OSS Distributions tests();
126*43a90889SApple OSS Distributions trapping_test();
127*43a90889SApple OSS Distributions }
128