1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 2020 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions *
4*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions *
6*5e3eaea3SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions *
15*5e3eaea3SApple OSS Distributions * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions *
18*5e3eaea3SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions * limitations under the License.
25*5e3eaea3SApple OSS Distributions *
26*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions */
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions
30*5e3eaea3SApple OSS Distributions #include <System/machine/cpu_capabilities.h>
31*5e3eaea3SApple OSS Distributions
32*5e3eaea3SApple OSS Distributions #include <darwintest.h>
33*5e3eaea3SApple OSS Distributions
34*5e3eaea3SApple OSS Distributions #include <stdio.h>
35*5e3eaea3SApple OSS Distributions #include <stdint.h>
36*5e3eaea3SApple OSS Distributions #include <unistd.h>
37*5e3eaea3SApple OSS Distributions #include <sys/sysctl.h>
38*5e3eaea3SApple OSS Distributions #include <sys/wait.h>
39*5e3eaea3SApple OSS Distributions #include <ptrauth.h>
40*5e3eaea3SApple OSS Distributions #include <dispatch/dispatch.h>
41*5e3eaea3SApple OSS Distributions #include <libkern/OSAtomic.h>
42*5e3eaea3SApple OSS Distributions
43*5e3eaea3SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
44*5e3eaea3SApple OSS Distributions
45*5e3eaea3SApple OSS Distributions #if TARGET_OS_OSX && defined(_COMM_PAGE_TEXT_ATOMIC_ENQUEUE)
46*5e3eaea3SApple OSS Distributions
47*5e3eaea3SApple OSS Distributions /* Keys and discriminators */
48*5e3eaea3SApple OSS Distributions #define COMMPAGE_PFZ_BASE_AUTH_KEY ptrauth_key_process_independent_code
49*5e3eaea3SApple OSS Distributions #define COMMPAGE_PFZ_FN_AUTH_KEY ptrauth_key_function_pointer
50*5e3eaea3SApple OSS Distributions #define COMMPAGE_PFZ_BASE_DISCRIMINATOR ptrauth_string_discriminator("pfz")
51*5e3eaea3SApple OSS Distributions
52*5e3eaea3SApple OSS Distributions /* Auth and sign macros */
53*5e3eaea3SApple OSS Distributions #define SIGN_COMMPAGE_PFZ_BASE_PTR(ptr) \
54*5e3eaea3SApple OSS Distributions ptrauth_sign_unauthenticated(ptr, COMMPAGE_PFZ_BASE_AUTH_KEY, COMMPAGE_PFZ_BASE_DISCRIMINATOR)
55*5e3eaea3SApple OSS Distributions #define AUTH_COMMPAGE_PFZ_BASE_PTR(ptr) \
56*5e3eaea3SApple OSS Distributions ptrauth_auth_data(ptr, COMMPAGE_PFZ_BASE_AUTH_KEY, COMMPAGE_PFZ_BASE_DISCRIMINATOR)
57*5e3eaea3SApple OSS Distributions #define SIGN_COMMPAGE_PFZ_FUNCTION_PTR(ptr) \
58*5e3eaea3SApple OSS Distributions ptrauth_sign_unauthenticated(ptr, COMMPAGE_PFZ_FN_AUTH_KEY, 0)
59*5e3eaea3SApple OSS Distributions
60*5e3eaea3SApple OSS Distributions static void *commpage_pfz_base = NULL;
61*5e3eaea3SApple OSS Distributions
62*5e3eaea3SApple OSS Distributions static void *
get_pfz_base(void)63*5e3eaea3SApple OSS Distributions get_pfz_base(void)
64*5e3eaea3SApple OSS Distributions {
65*5e3eaea3SApple OSS Distributions void *pfz_base = NULL;
66*5e3eaea3SApple OSS Distributions size_t s = sizeof(void *);
67*5e3eaea3SApple OSS Distributions
68*5e3eaea3SApple OSS Distributions int ret = sysctlbyname("kern.pfz", &pfz_base, &s, NULL, 0);
69*5e3eaea3SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname(kern.pfz)");
70*5e3eaea3SApple OSS Distributions
71*5e3eaea3SApple OSS Distributions commpage_pfz_base = SIGN_COMMPAGE_PFZ_BASE_PTR(pfz_base);
72*5e3eaea3SApple OSS Distributions T_LOG("pfz base = 0x%llx\n", commpage_pfz_base);
73*5e3eaea3SApple OSS Distributions }
74*5e3eaea3SApple OSS Distributions
75*5e3eaea3SApple OSS Distributions static void
undefined_function(void)76*5e3eaea3SApple OSS Distributions undefined_function(void)
77*5e3eaea3SApple OSS Distributions {
78*5e3eaea3SApple OSS Distributions // We can use the same commpage_pfz_base as parent since the PFZ is slide
79*5e3eaea3SApple OSS Distributions // once per boot and is same across all processes
80*5e3eaea3SApple OSS Distributions void (*undefined)(void);
81*5e3eaea3SApple OSS Distributions uintptr_t addr = (uintptr_t) (void *) AUTH_COMMPAGE_PFZ_BASE_PTR(commpage_pfz_base);
82*5e3eaea3SApple OSS Distributions addr += _COMM_PAGE_TEXT_ATOMIC_DEQUEUE;
83*5e3eaea3SApple OSS Distributions addr += 4; // Jump ahead
84*5e3eaea3SApple OSS Distributions undefined = SIGN_COMMPAGE_PFZ_FUNCTION_PTR((void *)addr);
85*5e3eaea3SApple OSS Distributions
86*5e3eaea3SApple OSS Distributions return undefined();
87*5e3eaea3SApple OSS Distributions }
88*5e3eaea3SApple OSS Distributions
89*5e3eaea3SApple OSS Distributions typedef struct {
90*5e3eaea3SApple OSS Distributions void *next;
91*5e3eaea3SApple OSS Distributions char *str;
92*5e3eaea3SApple OSS Distributions } QueueNode;
93*5e3eaea3SApple OSS Distributions
94*5e3eaea3SApple OSS Distributions T_DECL(test_arm_pfz, "Validate that ARM PFZ is mapped in",
95*5e3eaea3SApple OSS Distributions T_META_CHECK_LEAKS(false), T_META_IGNORECRASHES(".*undefined_function*"),
96*5e3eaea3SApple OSS Distributions T_META_ENABLED(false) /* rdar://62615792 */)
97*5e3eaea3SApple OSS Distributions {
98*5e3eaea3SApple OSS Distributions static dispatch_once_t pred;
99*5e3eaea3SApple OSS Distributions dispatch_once(&pred, ^{
100*5e3eaea3SApple OSS Distributions commpage_pfz_base = get_pfz_base();
101*5e3eaea3SApple OSS Distributions });
102*5e3eaea3SApple OSS Distributions
103*5e3eaea3SApple OSS Distributions OSFifoQueueHead head = OS_ATOMIC_FIFO_QUEUE_INIT;
104*5e3eaea3SApple OSS Distributions char *str1 = "String 1", *str2 = "String 2";
105*5e3eaea3SApple OSS Distributions QueueNode node1 = { 0, str1 };
106*5e3eaea3SApple OSS Distributions QueueNode node2 = { 0, str2 };
107*5e3eaea3SApple OSS Distributions
108*5e3eaea3SApple OSS Distributions OSAtomicFifoEnqueue(&head, &node1, 0);
109*5e3eaea3SApple OSS Distributions OSAtomicFifoEnqueue(&head, &node2, 0);
110*5e3eaea3SApple OSS Distributions QueueNode *node_ptr = OSAtomicFifoDequeue(&head, 0);
111*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(strcmp(node_ptr->str, str1), 0, "Dequeued first node correctly");
112*5e3eaea3SApple OSS Distributions
113*5e3eaea3SApple OSS Distributions node_ptr = OSAtomicFifoDequeue(&head, 0);
114*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(strcmp(node_ptr->str, str2), 0, "Dequeued second node correctly");
115*5e3eaea3SApple OSS Distributions
116*5e3eaea3SApple OSS Distributions node_ptr = OSAtomicFifoDequeue(&head, 0);
117*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(node_ptr, NULL, "Dequeuing from empty list correctly");
118*5e3eaea3SApple OSS Distributions
119*5e3eaea3SApple OSS Distributions int child_pid = 0;
120*5e3eaea3SApple OSS Distributions if ((child_pid = fork()) == 0) { // Child should call undefined function
121*5e3eaea3SApple OSS Distributions return undefined_function();
122*5e3eaea3SApple OSS Distributions } else {
123*5e3eaea3SApple OSS Distributions int status = 0;
124*5e3eaea3SApple OSS Distributions wait(&status);
125*5e3eaea3SApple OSS Distributions
126*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(!WIFEXITED(status), true, "Did not exit cleanly");
127*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(WIFSIGNALED(status), true, "Exited due to signal");
128*5e3eaea3SApple OSS Distributions T_LOG("Signal number = %d\n", WTERMSIG(status));
129*5e3eaea3SApple OSS Distributions }
130*5e3eaea3SApple OSS Distributions }
131*5e3eaea3SApple OSS Distributions
132*5e3eaea3SApple OSS Distributions T_DECL(test_rdar_65270017, "Testing for rdar 65270017",
133*5e3eaea3SApple OSS Distributions T_META_CHECK_LEAKS(false), T_META_ENABLED(false) /* rdar://65270017 */)
134*5e3eaea3SApple OSS Distributions {
135*5e3eaea3SApple OSS Distributions static dispatch_once_t pred;
136*5e3eaea3SApple OSS Distributions dispatch_once(&pred, ^{
137*5e3eaea3SApple OSS Distributions commpage_pfz_base = get_pfz_base();
138*5e3eaea3SApple OSS Distributions });
139*5e3eaea3SApple OSS Distributions
140*5e3eaea3SApple OSS Distributions struct OSAtomicFifoHeadWrapper {
141*5e3eaea3SApple OSS Distributions // Embedded OSFifoQueueHead structure inside the structure
142*5e3eaea3SApple OSS Distributions void *first;
143*5e3eaea3SApple OSS Distributions void *last;
144*5e3eaea3SApple OSS Distributions int opaque;
145*5e3eaea3SApple OSS Distributions
146*5e3eaea3SApple OSS Distributions int data;
147*5e3eaea3SApple OSS Distributions } wrapped_head = {
148*5e3eaea3SApple OSS Distributions .first = NULL,
149*5e3eaea3SApple OSS Distributions .last = NULL,
150*5e3eaea3SApple OSS Distributions .opaque = 0,
151*5e3eaea3SApple OSS Distributions .data = 0xfeed
152*5e3eaea3SApple OSS Distributions };
153*5e3eaea3SApple OSS Distributions
154*5e3eaea3SApple OSS Distributions char *str1 = "String 1", *str2 = "String 2";
155*5e3eaea3SApple OSS Distributions QueueNode node1 = { 0, str1 };
156*5e3eaea3SApple OSS Distributions QueueNode node2 = { 0, str2 };
157*5e3eaea3SApple OSS Distributions
158*5e3eaea3SApple OSS Distributions OSAtomicFifoEnqueue(&wrapped_head, &node1, 0);
159*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(wrapped_head.data, 0xfeed, "data is valid");
160*5e3eaea3SApple OSS Distributions
161*5e3eaea3SApple OSS Distributions OSAtomicFifoEnqueue(&wrapped_head, &node2, 0);
162*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(wrapped_head.data, 0xfeed, "data is valid");
163*5e3eaea3SApple OSS Distributions
164*5e3eaea3SApple OSS Distributions QueueNode *node_ptr = OSAtomicFifoDequeue(&wrapped_head, 0);
165*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(strcmp(node_ptr->str, str1), 0, "Dequeued first node correctly");
166*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(wrapped_head.data, 0xfeed, "data is valid");
167*5e3eaea3SApple OSS Distributions
168*5e3eaea3SApple OSS Distributions node_ptr = OSAtomicFifoDequeue(&wrapped_head, 0);
169*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(strcmp(node_ptr->str, str2), 0, "Dequeued second node correctly");
170*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(wrapped_head.data, 0xfeed, "data is valid");
171*5e3eaea3SApple OSS Distributions
172*5e3eaea3SApple OSS Distributions node_ptr = OSAtomicFifoDequeue(&wrapped_head, 0);
173*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(node_ptr, NULL, "Dequeuing from empty list correctly");
174*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(wrapped_head.data, 0xfeed, "data is valid");
175*5e3eaea3SApple OSS Distributions }
176*5e3eaea3SApple OSS Distributions
177*5e3eaea3SApple OSS Distributions #define WIDE 50ll
178*5e3eaea3SApple OSS Distributions #define SMALL 2000ll
179*5e3eaea3SApple OSS Distributions
180*5e3eaea3SApple OSS Distributions void
preheat(dispatch_queue_t dq)181*5e3eaea3SApple OSS Distributions preheat(dispatch_queue_t dq)
182*5e3eaea3SApple OSS Distributions {
183*5e3eaea3SApple OSS Distributions dispatch_apply(WIDE, dq, ^(size_t i) {
184*5e3eaea3SApple OSS Distributions sleep(1);
185*5e3eaea3SApple OSS Distributions });
186*5e3eaea3SApple OSS Distributions }
187*5e3eaea3SApple OSS Distributions
188*5e3eaea3SApple OSS Distributions typedef struct elem {
189*5e3eaea3SApple OSS Distributions long data1;
190*5e3eaea3SApple OSS Distributions struct elem *link;
191*5e3eaea3SApple OSS Distributions int data2;
192*5e3eaea3SApple OSS Distributions } elem_t;
193*5e3eaea3SApple OSS Distributions
194*5e3eaea3SApple OSS Distributions static size_t offset = offsetof(elem_t, link);
195*5e3eaea3SApple OSS Distributions static elem_t elements[WIDE][SMALL];
196*5e3eaea3SApple OSS Distributions
197*5e3eaea3SApple OSS Distributions T_DECL(test_65270017_contended, "multithreaded testing for radar 65270017")
198*5e3eaea3SApple OSS Distributions {
199*5e3eaea3SApple OSS Distributions dispatch_queue_t global_q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
200*5e3eaea3SApple OSS Distributions dispatch_queue_t queue = dispatch_queue_create("com.apple.libctests.threaded", 0);
201*5e3eaea3SApple OSS Distributions uint64_t __block t = 0;
202*5e3eaea3SApple OSS Distributions
203*5e3eaea3SApple OSS Distributions struct OSAtomicFifoHeadWrapper {
204*5e3eaea3SApple OSS Distributions // Embedded OSFifoQueueHead structure inside the structure
205*5e3eaea3SApple OSS Distributions void *first;
206*5e3eaea3SApple OSS Distributions void *last;
207*5e3eaea3SApple OSS Distributions int opaque;
208*5e3eaea3SApple OSS Distributions
209*5e3eaea3SApple OSS Distributions int data;
210*5e3eaea3SApple OSS Distributions };
211*5e3eaea3SApple OSS Distributions
212*5e3eaea3SApple OSS Distributions struct OSAtomicFifoHeadWrapper wrapped_q_head1 = {
213*5e3eaea3SApple OSS Distributions .first = NULL,
214*5e3eaea3SApple OSS Distributions .last = NULL,
215*5e3eaea3SApple OSS Distributions .opaque = 0,
216*5e3eaea3SApple OSS Distributions .data = 0xfeed
217*5e3eaea3SApple OSS Distributions };
218*5e3eaea3SApple OSS Distributions OSFifoQueueHead *q1 = (OSFifoQueueHead *) &wrapped_q_head1;
219*5e3eaea3SApple OSS Distributions
220*5e3eaea3SApple OSS Distributions struct OSAtomicFifoHeadWrapper wrapped_q_head2 = {
221*5e3eaea3SApple OSS Distributions .first = NULL,
222*5e3eaea3SApple OSS Distributions .last = NULL,
223*5e3eaea3SApple OSS Distributions .opaque = 0,
224*5e3eaea3SApple OSS Distributions .data = 0xdead
225*5e3eaea3SApple OSS Distributions };
226*5e3eaea3SApple OSS Distributions OSFifoQueueHead *q2 = (OSFifoQueueHead *) &wrapped_q_head2;
227*5e3eaea3SApple OSS Distributions
228*5e3eaea3SApple OSS Distributions t = 0;
229*5e3eaea3SApple OSS Distributions T_LOG("Preheating thread pool");
230*5e3eaea3SApple OSS Distributions
231*5e3eaea3SApple OSS Distributions preheat(global_q);
232*5e3eaea3SApple OSS Distributions
233*5e3eaea3SApple OSS Distributions T_LOG("Starting contended pfz test");
234*5e3eaea3SApple OSS Distributions
235*5e3eaea3SApple OSS Distributions dispatch_apply(WIDE, global_q, ^(size_t i) {
236*5e3eaea3SApple OSS Distributions dispatch_apply(SMALL, global_q, ^(size_t idx) {
237*5e3eaea3SApple OSS Distributions OSAtomicFifoEnqueue(q1, &(elements[i][idx]), offset); // contended enqueue on q1
238*5e3eaea3SApple OSS Distributions });
239*5e3eaea3SApple OSS Distributions
240*5e3eaea3SApple OSS Distributions uint32_t count = 0;
241*5e3eaea3SApple OSS Distributions elem_t *p = NULL;
242*5e3eaea3SApple OSS Distributions do {
243*5e3eaea3SApple OSS Distributions p = OSAtomicFifoDequeue(q1, offset);
244*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_EQ(wrapped_q_head1.data, 0xfeed, "q1 data is valid");
245*5e3eaea3SApple OSS Distributions if (p) {
246*5e3eaea3SApple OSS Distributions OSAtomicFifoEnqueue(q2, p, offset);
247*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_EQ(wrapped_q_head2.data, 0xdead, "q2 data is valid");
248*5e3eaea3SApple OSS Distributions count++;
249*5e3eaea3SApple OSS Distributions }
250*5e3eaea3SApple OSS Distributions } while (p != NULL);
251*5e3eaea3SApple OSS Distributions
252*5e3eaea3SApple OSS Distributions dispatch_sync(queue, ^{
253*5e3eaea3SApple OSS Distributions t += count;
254*5e3eaea3SApple OSS Distributions });
255*5e3eaea3SApple OSS Distributions });
256*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(t, ((uint64_t)WIDE * (uint64_t)SMALL), "OSAtomicFifoEnqueue");
257*5e3eaea3SApple OSS Distributions
258*5e3eaea3SApple OSS Distributions t = 0;
259*5e3eaea3SApple OSS Distributions dispatch_apply(WIDE, global_q, ^(size_t i) {
260*5e3eaea3SApple OSS Distributions uint32_t count = 0;
261*5e3eaea3SApple OSS Distributions elem_t *p = NULL;
262*5e3eaea3SApple OSS Distributions do {
263*5e3eaea3SApple OSS Distributions p = OSAtomicFifoDequeue(q2, offset);
264*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_EQ(wrapped_q_head2.data, 0xdead, "q2 data is valid");
265*5e3eaea3SApple OSS Distributions if (p) {
266*5e3eaea3SApple OSS Distributions count++;
267*5e3eaea3SApple OSS Distributions }
268*5e3eaea3SApple OSS Distributions } while (p != NULL);
269*5e3eaea3SApple OSS Distributions dispatch_sync(queue, ^{
270*5e3eaea3SApple OSS Distributions t += count;
271*5e3eaea3SApple OSS Distributions });
272*5e3eaea3SApple OSS Distributions });
273*5e3eaea3SApple OSS Distributions
274*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(t, ((uint64_t)WIDE * (uint64_t)SMALL), "OSAtomicFifoDequeue");
275*5e3eaea3SApple OSS Distributions
276*5e3eaea3SApple OSS Distributions dispatch_release(queue);
277*5e3eaea3SApple OSS Distributions }
278*5e3eaea3SApple OSS Distributions
279*5e3eaea3SApple OSS Distributions #else
280*5e3eaea3SApple OSS Distributions
281*5e3eaea3SApple OSS Distributions T_DECL(test_arm_pfz, "Validate that ARM PFZ is mapped in",
282*5e3eaea3SApple OSS Distributions T_META_CHECK_LEAKS(false))
283*5e3eaea3SApple OSS Distributions {
284*5e3eaea3SApple OSS Distributions T_SKIP("No PFZ, _COMM_PAGE_TEXT_ATOMIC_ENQUEUE doesn't exist");
285*5e3eaea3SApple OSS Distributions }
286*5e3eaea3SApple OSS Distributions
287*5e3eaea3SApple OSS Distributions #endif
288