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