1*a1e26a70SApple OSS Distributions #include <stdlib.h>
2*a1e26a70SApple OSS Distributions #include <unistd.h>
3*a1e26a70SApple OSS Distributions #include <sys/mman.h>
4*a1e26a70SApple OSS Distributions #include <fcntl.h>
5*a1e26a70SApple OSS Distributions #include <sys/types.h>
6*a1e26a70SApple OSS Distributions #include <sys/sysctl.h>
7*a1e26a70SApple OSS Distributions #include <stdatomic.h>
8*a1e26a70SApple OSS Distributions #include <TargetConditionals.h>
9*a1e26a70SApple OSS Distributions
10*a1e26a70SApple OSS Distributions #include <darwintest.h>
11*a1e26a70SApple OSS Distributions
12*a1e26a70SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
13*a1e26a70SApple OSS Distributions
14*a1e26a70SApple OSS Distributions static int nthreads = 0;
15*a1e26a70SApple OSS Distributions static int fd;
16*a1e26a70SApple OSS Distributions static _Atomic int phase = 0;
17*a1e26a70SApple OSS Distributions static _Atomic int pass_count = 0;
18*a1e26a70SApple OSS Distributions static _Atomic int fail_count = 0;
19*a1e26a70SApple OSS Distributions
20*a1e26a70SApple OSS Distributions static void *
worker_thread_func(__unused void * arg)21*a1e26a70SApple OSS Distributions worker_thread_func(__unused void *arg)
22*a1e26a70SApple OSS Distributions {
23*a1e26a70SApple OSS Distributions int myfd;
24*a1e26a70SApple OSS Distributions int error;
25*a1e26a70SApple OSS Distributions
26*a1e26a70SApple OSS Distributions /* test racing shm_open */
27*a1e26a70SApple OSS Distributions while (atomic_load(&phase) == 0) {
28*a1e26a70SApple OSS Distributions ;
29*a1e26a70SApple OSS Distributions }
30*a1e26a70SApple OSS Distributions myfd = shm_open("abcd", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
31*a1e26a70SApple OSS Distributions if (myfd == -1) {
32*a1e26a70SApple OSS Distributions T_QUIET; T_EXPECT_EQ(errno, EEXIST, "Expected EEXIST");
33*a1e26a70SApple OSS Distributions atomic_fetch_add(&fail_count, 1);
34*a1e26a70SApple OSS Distributions } else {
35*a1e26a70SApple OSS Distributions fd = myfd;
36*a1e26a70SApple OSS Distributions atomic_fetch_add(&pass_count, 1);
37*a1e26a70SApple OSS Distributions }
38*a1e26a70SApple OSS Distributions
39*a1e26a70SApple OSS Distributions /* test racing ftruncate */
40*a1e26a70SApple OSS Distributions while (atomic_load(&phase) == 1) {
41*a1e26a70SApple OSS Distributions ;
42*a1e26a70SApple OSS Distributions }
43*a1e26a70SApple OSS Distributions error = ftruncate(fd, 8 * 1024);
44*a1e26a70SApple OSS Distributions if (error == -1) {
45*a1e26a70SApple OSS Distributions T_QUIET; T_EXPECT_EQ(errno, EINVAL, "Expected EINVAL");
46*a1e26a70SApple OSS Distributions atomic_fetch_add(&fail_count, 1);
47*a1e26a70SApple OSS Distributions } else {
48*a1e26a70SApple OSS Distributions atomic_fetch_add(&pass_count, 1);
49*a1e26a70SApple OSS Distributions }
50*a1e26a70SApple OSS Distributions
51*a1e26a70SApple OSS Distributions /* test racing close */
52*a1e26a70SApple OSS Distributions while (atomic_load(&phase) == 2) {
53*a1e26a70SApple OSS Distributions ;
54*a1e26a70SApple OSS Distributions }
55*a1e26a70SApple OSS Distributions error = close(fd);
56*a1e26a70SApple OSS Distributions if (error == -1) {
57*a1e26a70SApple OSS Distributions T_QUIET; T_EXPECT_EQ(errno, EBADF, "Expected EBADF");
58*a1e26a70SApple OSS Distributions atomic_fetch_add(&fail_count, 1);
59*a1e26a70SApple OSS Distributions } else {
60*a1e26a70SApple OSS Distributions atomic_fetch_add(&pass_count, 1);
61*a1e26a70SApple OSS Distributions }
62*a1e26a70SApple OSS Distributions
63*a1e26a70SApple OSS Distributions /* test racing shm_unlink() */
64*a1e26a70SApple OSS Distributions while (atomic_load(&phase) == 3) {
65*a1e26a70SApple OSS Distributions ;
66*a1e26a70SApple OSS Distributions }
67*a1e26a70SApple OSS Distributions error = shm_unlink("abcd");
68*a1e26a70SApple OSS Distributions if (error == -1) {
69*a1e26a70SApple OSS Distributions T_QUIET; T_EXPECT_EQ(errno, ENOENT, "Expected ENOENT");
70*a1e26a70SApple OSS Distributions atomic_fetch_add(&fail_count, 1);
71*a1e26a70SApple OSS Distributions } else {
72*a1e26a70SApple OSS Distributions atomic_fetch_add(&pass_count, 1);
73*a1e26a70SApple OSS Distributions }
74*a1e26a70SApple OSS Distributions return NULL;
75*a1e26a70SApple OSS Distributions }
76*a1e26a70SApple OSS Distributions
77*a1e26a70SApple OSS Distributions static void
create_threads(void)78*a1e26a70SApple OSS Distributions create_threads(void)
79*a1e26a70SApple OSS Distributions {
80*a1e26a70SApple OSS Distributions int ret;
81*a1e26a70SApple OSS Distributions int ncpu;
82*a1e26a70SApple OSS Distributions size_t ncpu_size = sizeof(ncpu);
83*a1e26a70SApple OSS Distributions int i;
84*a1e26a70SApple OSS Distributions pthread_attr_t attr;
85*a1e26a70SApple OSS Distributions
86*a1e26a70SApple OSS Distributions ret = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0);
87*a1e26a70SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname(hw.ncpu)");
88*a1e26a70SApple OSS Distributions
89*a1e26a70SApple OSS Distributions T_QUIET; T_LOG("%s: Detected %d CPUs\n", __FUNCTION__, ncpu);
90*a1e26a70SApple OSS Distributions
91*a1e26a70SApple OSS Distributions nthreads = ncpu;
92*a1e26a70SApple OSS Distributions T_QUIET; T_LOG("%s: Will create %d threads\n", __FUNCTION__, nthreads);
93*a1e26a70SApple OSS Distributions
94*a1e26a70SApple OSS Distributions ret = pthread_attr_init(&attr);
95*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(ret, "pthread_attr_init");
96*a1e26a70SApple OSS Distributions
97*a1e26a70SApple OSS Distributions for (i = 0; i < nthreads; i++) {
98*a1e26a70SApple OSS Distributions pthread_t thread;
99*a1e26a70SApple OSS Distributions ret = pthread_create(&thread, &attr, worker_thread_func, NULL);
100*a1e26a70SApple OSS Distributions T_QUIET; T_ASSERT_POSIX_ZERO(ret, "pthread_create");
101*a1e26a70SApple OSS Distributions }
102*a1e26a70SApple OSS Distributions }
103*a1e26a70SApple OSS Distributions
104*a1e26a70SApple OSS Distributions
105*a1e26a70SApple OSS Distributions T_DECL(testposixshm, "Posix Shared Memory tests")
106*a1e26a70SApple OSS Distributions {
107*a1e26a70SApple OSS Distributions int fd1;
108*a1e26a70SApple OSS Distributions int fd2;
109*a1e26a70SApple OSS Distributions int *addr;
110*a1e26a70SApple OSS Distributions char *noname = "";
111*a1e26a70SApple OSS Distributions char *toolong = "12345678901234567890123456789012";
112*a1e26a70SApple OSS Distributions char *maxname = "1234567890123456789012345678901";
113*a1e26a70SApple OSS Distributions
114*a1e26a70SApple OSS Distributions /* must have O_CREAT */
115*a1e26a70SApple OSS Distributions fd1 = shm_open(maxname, O_RDWR, S_IRUSR | S_IWUSR);
116*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fd1, -1, "shm_open() missing O_CREAT");
117*a1e26a70SApple OSS Distributions T_WITH_ERRNO;
118*a1e26a70SApple OSS Distributions T_EXPECT_EQ(errno, ENOENT, "Expected ENOENT");
119*a1e26a70SApple OSS Distributions
120*a1e26a70SApple OSS Distributions /* name too long */
121*a1e26a70SApple OSS Distributions fd1 = shm_open(toolong, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
122*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fd1, -1, "shm_open() name too long");
123*a1e26a70SApple OSS Distributions T_WITH_ERRNO;
124*a1e26a70SApple OSS Distributions T_EXPECT_EQ(errno, ENAMETOOLONG, "Expected ENAMETOOLONG");
125*a1e26a70SApple OSS Distributions
126*a1e26a70SApple OSS Distributions /* invalid name */
127*a1e26a70SApple OSS Distributions fd1 = shm_open(noname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
128*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fd1, -1, "shm_open() invalid name");
129*a1e26a70SApple OSS Distributions T_WITH_ERRNO;
130*a1e26a70SApple OSS Distributions T_EXPECT_EQ(errno, EINVAL, "Expected EINVAL");
131*a1e26a70SApple OSS Distributions
132*a1e26a70SApple OSS Distributions /* valid open */
133*a1e26a70SApple OSS Distributions fd1 = shm_open(maxname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
134*a1e26a70SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(fd1, "valid shm_open() result");
135*a1e26a70SApple OSS Distributions
136*a1e26a70SApple OSS Distributions /* O_CREAT, but not O_EXCL should work */
137*a1e26a70SApple OSS Distributions fd2 = shm_open(maxname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
138*a1e26a70SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(fd2, "shm_open() no O_EXCL");
139*a1e26a70SApple OSS Distributions
140*a1e26a70SApple OSS Distributions /* close should work */
141*a1e26a70SApple OSS Distributions T_EXPECT_POSIX_ZERO(close(fd2), "close()");
142*a1e26a70SApple OSS Distributions
143*a1e26a70SApple OSS Distributions /* O_CREAT | O_EXCL should fail */
144*a1e26a70SApple OSS Distributions fd2 = shm_open(maxname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
145*a1e26a70SApple OSS Distributions T_WITH_ERRNO;
146*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fd2, -1, "shm_open() existing but O_EXCL");
147*a1e26a70SApple OSS Distributions T_EXPECT_EQ(errno, EEXIST, "Expected EEXIST");
148*a1e26a70SApple OSS Distributions
149*a1e26a70SApple OSS Distributions /* use ftruncate to create the memory */
150*a1e26a70SApple OSS Distributions T_EXPECT_POSIX_ZERO(ftruncate(fd1, 16 * 1024), NULL);
151*a1e26a70SApple OSS Distributions
152*a1e26a70SApple OSS Distributions /* a second ftruncate should fail */
153*a1e26a70SApple OSS Distributions T_WITH_ERRNO;
154*a1e26a70SApple OSS Distributions T_EXPECT_EQ(ftruncate(fd1, 8 * 1024), -1, "second ftruncate() should fail");
155*a1e26a70SApple OSS Distributions T_EXPECT_EQ(errno, EINVAL, "Expected EINVAL");
156*a1e26a70SApple OSS Distributions
157*a1e26a70SApple OSS Distributions /* Map the memory object */
158*a1e26a70SApple OSS Distributions addr = mmap(0, 4 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
159*a1e26a70SApple OSS Distributions T_WITH_ERRNO;
160*a1e26a70SApple OSS Distributions T_EXPECT_NE((void *)addr, MAP_FAILED, "mmap() should work");
161*a1e26a70SApple OSS Distributions
162*a1e26a70SApple OSS Distributions /* close should work */
163*a1e26a70SApple OSS Distributions T_EXPECT_POSIX_ZERO(close(fd1), "close()");
164*a1e26a70SApple OSS Distributions
165*a1e26a70SApple OSS Distributions /* unlink should work */
166*a1e26a70SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(shm_unlink(maxname), "shm_unlink()");
167*a1e26a70SApple OSS Distributions
168*a1e26a70SApple OSS Distributions /* shm_open() after unlink/close should fail */
169*a1e26a70SApple OSS Distributions fd2 = shm_open(maxname, O_RDWR, S_IRUSR | S_IWUSR);
170*a1e26a70SApple OSS Distributions T_WITH_ERRNO;
171*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fd2, -1, "shm_open() but removed");
172*a1e26a70SApple OSS Distributions T_EXPECT_EQ(errno, ENOENT, "Expected ENOENT");
173*a1e26a70SApple OSS Distributions
174*a1e26a70SApple OSS Distributions /*
175*a1e26a70SApple OSS Distributions * second phase of tests, try to create race conditions for
176*a1e26a70SApple OSS Distributions * shm_open() - multiple threads do shm_open(, ... O_EXCL, ...)
177*a1e26a70SApple OSS Distributions * ftruncate() - multiple threads, only 1 should succeed.
178*a1e26a70SApple OSS Distributions * fclose() - multiple threads, only 1 should succeed.
179*a1e26a70SApple OSS Distributions * shm_unlink() - multiple threads, only 1 should succeed.
180*a1e26a70SApple OSS Distributions */
181*a1e26a70SApple OSS Distributions create_threads();
182*a1e26a70SApple OSS Distributions sleep(1);
183*a1e26a70SApple OSS Distributions T_LOG("Race testing shm_open");
184*a1e26a70SApple OSS Distributions atomic_fetch_add(&phase, 1);
185*a1e26a70SApple OSS Distributions while (pass_count + fail_count < nthreads) {
186*a1e26a70SApple OSS Distributions sleep(1);
187*a1e26a70SApple OSS Distributions }
188*a1e26a70SApple OSS Distributions T_EXPECT_EQ(pass_count, 1, "racing shm_open()");
189*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fail_count, nthreads - 1, "racing shm_open()");
190*a1e26a70SApple OSS Distributions
191*a1e26a70SApple OSS Distributions atomic_store(&pass_count, 0);
192*a1e26a70SApple OSS Distributions atomic_store(&fail_count, 0);
193*a1e26a70SApple OSS Distributions T_LOG("Race testing ftruncate\n");
194*a1e26a70SApple OSS Distributions atomic_fetch_add(&phase, 1);
195*a1e26a70SApple OSS Distributions while (pass_count + fail_count < nthreads) {
196*a1e26a70SApple OSS Distributions sleep(1);
197*a1e26a70SApple OSS Distributions }
198*a1e26a70SApple OSS Distributions T_EXPECT_EQ(pass_count, 1, "racing ftruncate()");
199*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fail_count, nthreads - 1, "racing ftruncate()");
200*a1e26a70SApple OSS Distributions
201*a1e26a70SApple OSS Distributions atomic_store(&pass_count, 0);
202*a1e26a70SApple OSS Distributions atomic_store(&fail_count, 0);
203*a1e26a70SApple OSS Distributions T_LOG("Race testing fclose\n");
204*a1e26a70SApple OSS Distributions atomic_fetch_add(&phase, 1);
205*a1e26a70SApple OSS Distributions while (pass_count + fail_count < nthreads) {
206*a1e26a70SApple OSS Distributions sleep(1);
207*a1e26a70SApple OSS Distributions }
208*a1e26a70SApple OSS Distributions T_EXPECT_EQ(pass_count, 1, "racing fclose()");
209*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fail_count, nthreads - 1, "racing fclose()");
210*a1e26a70SApple OSS Distributions
211*a1e26a70SApple OSS Distributions atomic_store(&pass_count, 0);
212*a1e26a70SApple OSS Distributions atomic_store(&fail_count, 0);
213*a1e26a70SApple OSS Distributions T_LOG("Race testing shm_unlink\n");
214*a1e26a70SApple OSS Distributions atomic_fetch_add(&phase, 1);
215*a1e26a70SApple OSS Distributions while (pass_count + fail_count < nthreads) {
216*a1e26a70SApple OSS Distributions sleep(1);
217*a1e26a70SApple OSS Distributions }
218*a1e26a70SApple OSS Distributions T_EXPECT_EQ(pass_count, 1, "racing shm_unlink()");
219*a1e26a70SApple OSS Distributions T_EXPECT_EQ(fail_count, nthreads - 1, "racing shm_unlink()");
220*a1e26a70SApple OSS Distributions }
221