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