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