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