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