xref: /xnu-12377.81.4/tests/lockf_EOF_77264182.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions #include <fcntl.h>
2*043036a2SApple OSS Distributions #include <sys/fcntl.h>
3*043036a2SApple OSS Distributions #include <darwintest.h>
4*043036a2SApple OSS Distributions #include <darwintest_utils.h>
5*043036a2SApple OSS Distributions 
6*043036a2SApple OSS Distributions 
7*043036a2SApple OSS Distributions T_GLOBAL_META(
8*043036a2SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
9*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
10*043036a2SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
11*043036a2SApple OSS Distributions 	T_META_OWNER("jonathan_w_adams"),
12*043036a2SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE));
13*043036a2SApple OSS Distributions 
14*043036a2SApple OSS Distributions /*
15*043036a2SApple OSS Distributions  * See rdar://77264182: xnu's lockf implementation had trouble
16*043036a2SApple OSS Distributions  * with l_len = 0 (e.g. go to EOF) being treated differently
17*043036a2SApple OSS Distributions  * than (l_start + l_len - 1) == OFF_MAX, even though they are
18*043036a2SApple OSS Distributions  * effectively the same thing.  ~25 loops of this test was enough
19*043036a2SApple OSS Distributions  * to get an Intel mac into an infinite loop in the kernel.
20*043036a2SApple OSS Distributions  */
21*043036a2SApple OSS Distributions T_DECL(lockf_EOF_77264182,
22*043036a2SApple OSS Distributions     "try to stress out lockf requests around OFF_MAX/EOF",
23*043036a2SApple OSS Distributions     T_META_CHECK_LEAKS(false), T_META_TAG_VM_PREFERRED)
24*043036a2SApple OSS Distributions {
25*043036a2SApple OSS Distributions 	const char *dir = dt_tmpdir();
26*043036a2SApple OSS Distributions 	int fd;
27*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(chdir(dir), "chdir(%s)", dir);
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS((fd = open("lockf_EOF_test", O_CREAT | O_RDWR, 0666)), "open(lockf_EOF_test)");
30*043036a2SApple OSS Distributions 
31*043036a2SApple OSS Distributions 	/*
32*043036a2SApple OSS Distributions 	 * At each loop, we do:
33*043036a2SApple OSS Distributions 	 *	write lock [OFF_MAX - loop, EOF)
34*043036a2SApple OSS Distributions 	 *	unlock     [OFF_MAX - loop, OFF_MAX)
35*043036a2SApple OSS Distributions 	 *	write lock [OFF_MAX - loop - 1, OFF_MAX)
36*043036a2SApple OSS Distributions 	 */
37*043036a2SApple OSS Distributions 	int loops;
38*043036a2SApple OSS Distributions 	for (loops = 0; loops < 100; loops++) {
39*043036a2SApple OSS Distributions 		struct flock fl = {
40*043036a2SApple OSS Distributions 			.l_start = OFF_MAX - loops,
41*043036a2SApple OSS Distributions 			.l_len = 0,
42*043036a2SApple OSS Distributions 			.l_pid = getpid(),
43*043036a2SApple OSS Distributions 			.l_type = F_WRLCK,
44*043036a2SApple OSS Distributions 			.l_whence = SEEK_SET
45*043036a2SApple OSS Distributions 		};
46*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_SETLK, &fl), "wrlock");
47*043036a2SApple OSS Distributions 		fl.l_len = OFF_MAX - fl.l_start + 1;
48*043036a2SApple OSS Distributions 		fl.l_type = F_UNLCK;
49*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_SETLK, &fl), "unlock");
50*043036a2SApple OSS Distributions 		fl.l_start--;
51*043036a2SApple OSS Distributions 		fl.l_len++;
52*043036a2SApple OSS Distributions 		fl.l_type = F_WRLCK;
53*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_SETLK, &fl), "wrlock 2");
54*043036a2SApple OSS Distributions 	}
55*043036a2SApple OSS Distributions 	T_PASS("did %d loops", loops);
56*043036a2SApple OSS Distributions }
57