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