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