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