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