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