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