1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2022 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions */
4*1031c584SApple OSS Distributions
5*1031c584SApple OSS Distributions #define PRIVATE 1 /* Needed for some F_OFD_* definitions */
6*1031c584SApple OSS Distributions #include <fcntl.h>
7*1031c584SApple OSS Distributions #include <sys/fcntl.h>
8*1031c584SApple OSS Distributions #include <errno.h>
9*1031c584SApple OSS Distributions #include <darwintest.h>
10*1031c584SApple OSS Distributions #include <darwintest_utils.h>
11*1031c584SApple OSS Distributions #include <stdatomic.h>
12*1031c584SApple OSS Distributions
13*1031c584SApple OSS Distributions #ifndef O_CLOFORK
14*1031c584SApple OSS Distributions #define O_CLOFORK 0x08000000
15*1031c584SApple OSS Distributions #endif
16*1031c584SApple OSS Distributions
17*1031c584SApple OSS Distributions T_GLOBAL_META(
18*1031c584SApple OSS Distributions T_META_NAMESPACE("xnu.locks"),
19*1031c584SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
20*1031c584SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("locks"),
21*1031c584SApple OSS Distributions T_META_OWNER("tim_marsland"), // with thanks to Peter Rutenbar
22*1031c584SApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE));
23*1031c584SApple OSS Distributions
24*1031c584SApple OSS Distributions enum lock_flags {
25*1031c584SApple OSS Distributions EXCL = 1,
26*1031c584SApple OSS Distributions WAIT = 2,
27*1031c584SApple OSS Distributions UNLOCK = 4,
28*1031c584SApple OSS Distributions };
29*1031c584SApple OSS Distributions
30*1031c584SApple OSS Distributions enum lock_type {
31*1031c584SApple OSS Distributions TYPE_FLOCK = 0,
32*1031c584SApple OSS Distributions TYPE_POSIX = 1,
33*1031c584SApple OSS Distributions TYPE_OFD = 2,
34*1031c584SApple OSS Distributions };
35*1031c584SApple OSS Distributions
36*1031c584SApple OSS Distributions static int
ofd_get(struct flock * fl,int fd,off_t start,off_t len,pid_t pid,uint32_t flags)37*1031c584SApple OSS Distributions ofd_get(struct flock *fl,
38*1031c584SApple OSS Distributions int fd, off_t start, off_t len, pid_t pid, uint32_t flags)
39*1031c584SApple OSS Distributions {
40*1031c584SApple OSS Distributions fl->l_start = start;
41*1031c584SApple OSS Distributions fl->l_len = len;
42*1031c584SApple OSS Distributions fl->l_pid = pid;
43*1031c584SApple OSS Distributions fl->l_type = (flags & EXCL) ? F_WRLCK : F_RDLCK;
44*1031c584SApple OSS Distributions fl->l_whence = SEEK_SET;
45*1031c584SApple OSS Distributions return fcntl(fd, (pid == -1) ? F_OFD_GETLK : F_OFD_GETLKPID, fl);
46*1031c584SApple OSS Distributions }
47*1031c584SApple OSS Distributions
48*1031c584SApple OSS Distributions static int
posix_get(struct flock * fl,int fd,off_t start,off_t len,pid_t pid,uint32_t flags)49*1031c584SApple OSS Distributions posix_get(struct flock *fl,
50*1031c584SApple OSS Distributions int fd, off_t start, off_t len, pid_t pid, uint32_t flags)
51*1031c584SApple OSS Distributions {
52*1031c584SApple OSS Distributions fl->l_start = start;
53*1031c584SApple OSS Distributions fl->l_len = len;
54*1031c584SApple OSS Distributions fl->l_pid = pid;
55*1031c584SApple OSS Distributions fl->l_type = (flags & EXCL) ? F_WRLCK : F_RDLCK;
56*1031c584SApple OSS Distributions fl->l_whence = SEEK_SET;
57*1031c584SApple OSS Distributions return fcntl(fd, (pid == -1) ? F_GETLK : F_GETLKPID, fl);
58*1031c584SApple OSS Distributions }
59*1031c584SApple OSS Distributions
60*1031c584SApple OSS Distributions static int
posix_lock(int fd,off_t start,off_t len,uint32_t flags)61*1031c584SApple OSS Distributions posix_lock(int fd, off_t start, off_t len, uint32_t flags)
62*1031c584SApple OSS Distributions {
63*1031c584SApple OSS Distributions struct flock fl = {
64*1031c584SApple OSS Distributions .l_start = start,
65*1031c584SApple OSS Distributions .l_len = len,
66*1031c584SApple OSS Distributions .l_pid = -1,
67*1031c584SApple OSS Distributions .l_type = (flags & EXCL) ? F_WRLCK : F_RDLCK,
68*1031c584SApple OSS Distributions .l_whence = SEEK_SET,
69*1031c584SApple OSS Distributions };
70*1031c584SApple OSS Distributions return fcntl(fd, (flags & WAIT) ? F_SETLKW : F_SETLK, &fl);
71*1031c584SApple OSS Distributions }
72*1031c584SApple OSS Distributions
73*1031c584SApple OSS Distributions static int
ofd_lock(int fd,off_t start,off_t len,uint32_t flags)74*1031c584SApple OSS Distributions ofd_lock(int fd, off_t start, off_t len, uint32_t flags)
75*1031c584SApple OSS Distributions {
76*1031c584SApple OSS Distributions struct flock fl = {
77*1031c584SApple OSS Distributions .l_start = start,
78*1031c584SApple OSS Distributions .l_len = len,
79*1031c584SApple OSS Distributions .l_pid = -1,
80*1031c584SApple OSS Distributions .l_type = (flags & EXCL) ? F_WRLCK : F_RDLCK,
81*1031c584SApple OSS Distributions .l_whence = SEEK_SET,
82*1031c584SApple OSS Distributions };
83*1031c584SApple OSS Distributions return fcntl(fd, (flags & WAIT) ? F_OFD_SETLKW : F_OFD_SETLK, &fl);
84*1031c584SApple OSS Distributions }
85*1031c584SApple OSS Distributions
86*1031c584SApple OSS Distributions static int
posix_unlock(int fd,off_t start,off_t len)87*1031c584SApple OSS Distributions posix_unlock(int fd, off_t start, off_t len)
88*1031c584SApple OSS Distributions {
89*1031c584SApple OSS Distributions struct flock fl = {
90*1031c584SApple OSS Distributions .l_start = start,
91*1031c584SApple OSS Distributions .l_len = len,
92*1031c584SApple OSS Distributions .l_pid = -1,
93*1031c584SApple OSS Distributions .l_type = F_UNLCK,
94*1031c584SApple OSS Distributions .l_whence = SEEK_SET,
95*1031c584SApple OSS Distributions };
96*1031c584SApple OSS Distributions return fcntl(fd, F_SETLK, &fl);
97*1031c584SApple OSS Distributions }
98*1031c584SApple OSS Distributions
99*1031c584SApple OSS Distributions static int
ofd_unlock(int fd,off_t start,off_t len)100*1031c584SApple OSS Distributions ofd_unlock(int fd, off_t start, off_t len)
101*1031c584SApple OSS Distributions {
102*1031c584SApple OSS Distributions struct flock fl = {
103*1031c584SApple OSS Distributions .l_start = start,
104*1031c584SApple OSS Distributions .l_len = len,
105*1031c584SApple OSS Distributions .l_pid = -1,
106*1031c584SApple OSS Distributions .l_type = F_UNLCK,
107*1031c584SApple OSS Distributions .l_whence = SEEK_SET,
108*1031c584SApple OSS Distributions };
109*1031c584SApple OSS Distributions return fcntl(fd, F_OFD_SETLK, &fl);
110*1031c584SApple OSS Distributions }
111*1031c584SApple OSS Distributions
112*1031c584SApple OSS Distributions /* Is the given flock equal to the given arguments */
113*1031c584SApple OSS Distributions static bool
flequal(struct flock * fl,off_t start,off_t len,pid_t pid,int flags)114*1031c584SApple OSS Distributions flequal(struct flock *fl, off_t start, off_t len, pid_t pid, int flags)
115*1031c584SApple OSS Distributions {
116*1031c584SApple OSS Distributions if (start == fl->l_start && len == fl->l_len && pid == fl->l_pid) {
117*1031c584SApple OSS Distributions if (flags == EXCL && fl->l_type == F_WRLCK) {
118*1031c584SApple OSS Distributions return true;
119*1031c584SApple OSS Distributions } else if (flags == UNLOCK && fl->l_type == F_UNLCK) {
120*1031c584SApple OSS Distributions return true;
121*1031c584SApple OSS Distributions } else if (flags == 0 && fl->l_type == F_RDLCK) {
122*1031c584SApple OSS Distributions return true;
123*1031c584SApple OSS Distributions }
124*1031c584SApple OSS Distributions }
125*1031c584SApple OSS Distributions T_LOG("flequal: %lld %lld %d %x %x\n",
126*1031c584SApple OSS Distributions fl->l_start, fl->l_len, fl->l_pid, fl->l_type, fl->l_whence);
127*1031c584SApple OSS Distributions return false;
128*1031c584SApple OSS Distributions }
129*1031c584SApple OSS Distributions
130*1031c584SApple OSS Distributions typedef struct {
131*1031c584SApple OSS Distributions pthread_t thread;
132*1031c584SApple OSS Distributions int fd, err;
133*1031c584SApple OSS Distributions bool complete;
134*1031c584SApple OSS Distributions enum lock_type lock_type;
135*1031c584SApple OSS Distributions uint32_t flags;
136*1031c584SApple OSS Distributions off_t start, end;
137*1031c584SApple OSS Distributions } lock_thread_state_t;
138*1031c584SApple OSS Distributions
139*1031c584SApple OSS Distributions static void *
lock_thread(void * arg)140*1031c584SApple OSS Distributions lock_thread(
141*1031c584SApple OSS Distributions void *arg)
142*1031c584SApple OSS Distributions {
143*1031c584SApple OSS Distributions lock_thread_state_t *lts = (lock_thread_state_t *)arg;
144*1031c584SApple OSS Distributions
145*1031c584SApple OSS Distributions switch (lts->lock_type) {
146*1031c584SApple OSS Distributions case TYPE_FLOCK: {
147*1031c584SApple OSS Distributions int op = (lts->flags & EXCL) ? LOCK_EX : LOCK_SH;
148*1031c584SApple OSS Distributions op |= (lts->flags & WAIT) ? 0 : LOCK_NB;
149*1031c584SApple OSS Distributions lts->err = flock(lts->fd, op) ? errno : 0;
150*1031c584SApple OSS Distributions break;
151*1031c584SApple OSS Distributions }
152*1031c584SApple OSS Distributions case TYPE_POSIX:
153*1031c584SApple OSS Distributions lts->err = posix_lock(lts->fd,
154*1031c584SApple OSS Distributions lts->start, lts->end, lts->flags) ? errno : 0;
155*1031c584SApple OSS Distributions break;
156*1031c584SApple OSS Distributions case TYPE_OFD:
157*1031c584SApple OSS Distributions lts->err = ofd_lock(lts->fd,
158*1031c584SApple OSS Distributions lts->start, lts->end, lts->flags) ? errno : 0;
159*1031c584SApple OSS Distributions break;
160*1031c584SApple OSS Distributions }
161*1031c584SApple OSS Distributions
162*1031c584SApple OSS Distributions atomic_thread_fence(memory_order_acquire);
163*1031c584SApple OSS Distributions lts->complete = true;
164*1031c584SApple OSS Distributions atomic_thread_fence(memory_order_release);
165*1031c584SApple OSS Distributions
166*1031c584SApple OSS Distributions return NULL;
167*1031c584SApple OSS Distributions }
168*1031c584SApple OSS Distributions
169*1031c584SApple OSS Distributions static bool
has_completed(lock_thread_state_t * lts)170*1031c584SApple OSS Distributions has_completed(lock_thread_state_t *lts)
171*1031c584SApple OSS Distributions {
172*1031c584SApple OSS Distributions atomic_thread_fence(memory_order_acquire);
173*1031c584SApple OSS Distributions const bool r = lts->complete;
174*1031c584SApple OSS Distributions atomic_thread_fence(memory_order_release);
175*1031c584SApple OSS Distributions return r;
176*1031c584SApple OSS Distributions }
177*1031c584SApple OSS Distributions
178*1031c584SApple OSS Distributions static void
start_lock_thread(enum lock_type type,lock_thread_state_t * lts,int fd,off_t start,off_t end,uint32_t flags)179*1031c584SApple OSS Distributions start_lock_thread(enum lock_type type, lock_thread_state_t *lts,
180*1031c584SApple OSS Distributions int fd, off_t start, off_t end, uint32_t flags)
181*1031c584SApple OSS Distributions {
182*1031c584SApple OSS Distributions lts->fd = fd;
183*1031c584SApple OSS Distributions lts->err = 0;
184*1031c584SApple OSS Distributions lts->complete = false;
185*1031c584SApple OSS Distributions lts->lock_type = type;
186*1031c584SApple OSS Distributions lts->flags = flags;
187*1031c584SApple OSS Distributions lts->start = start;
188*1031c584SApple OSS Distributions lts->end = end;
189*1031c584SApple OSS Distributions
190*1031c584SApple OSS Distributions pthread_create(<s->thread, NULL, lock_thread, lts);
191*1031c584SApple OSS Distributions }
192*1031c584SApple OSS Distributions
193*1031c584SApple OSS Distributions static void
random_pause(void)194*1031c584SApple OSS Distributions random_pause(void)
195*1031c584SApple OSS Distributions {
196*1031c584SApple OSS Distributions const useconds_t usec = rand() & (16384 - 1);
197*1031c584SApple OSS Distributions usleep(usec + 1);
198*1031c584SApple OSS Distributions }
199*1031c584SApple OSS Distributions
200*1031c584SApple OSS Distributions #define GET_CHECK( \
201*1031c584SApple OSS Distributions str, fd, \
202*1031c584SApple OSS Distributions get_start, get_len, get_flags, get_type, \
203*1031c584SApple OSS Distributions chk_start, chk_len, chk_flags, chk_pid) \
204*1031c584SApple OSS Distributions do { \
205*1031c584SApple OSS Distributions struct flock _fl; \
206*1031c584SApple OSS Distributions if (get_type == TYPE_OFD) { \
207*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ofd_get(&_fl, fd, get_start, get_len, -1, get_flags), str " (ofd_get)"); \
208*1031c584SApple OSS Distributions } else { \
209*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_get(&_fl, fd, get_start, get_len, -1, get_flags), str " (posix_get)"); \
210*1031c584SApple OSS Distributions } \
211*1031c584SApple OSS Distributions T_ASSERT_TRUE(flequal(&_fl, chk_start, chk_len, chk_pid, chk_flags), str " (flequal)"); \
212*1031c584SApple OSS Distributions } while (0)
213*1031c584SApple OSS Distributions
214*1031c584SApple OSS Distributions #define LOCK_AND_CHECK( \
215*1031c584SApple OSS Distributions str, fd, \
216*1031c584SApple OSS Distributions lck_start, lck_len, lck_flags, lock_type, \
217*1031c584SApple OSS Distributions get_start, get_len, get_flags, get_type, \
218*1031c584SApple OSS Distributions chk_start, chk_len, chk_flags, chk_pid) \
219*1031c584SApple OSS Distributions do { \
220*1031c584SApple OSS Distributions if (lock_type == TYPE_OFD) { \
221*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ofd_lock(fd, lck_start, lck_len, lck_flags), str " (ofd_lock)"); \
222*1031c584SApple OSS Distributions } else { \
223*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_lock(fd, lck_start, lck_len, lck_flags), str " (posix_lock)"); \
224*1031c584SApple OSS Distributions } \
225*1031c584SApple OSS Distributions GET_CHECK(str, fd, get_start, get_len, get_flags, get_type, chk_start, chk_len, chk_flags, chk_pid); \
226*1031c584SApple OSS Distributions } while (0)
227*1031c584SApple OSS Distributions
228*1031c584SApple OSS Distributions #define UNLOCK_AND_CHECK( \
229*1031c584SApple OSS Distributions str, fd, \
230*1031c584SApple OSS Distributions lck_start, lck_len, unlock_type, \
231*1031c584SApple OSS Distributions get_start, get_len, get_flags, get_type, \
232*1031c584SApple OSS Distributions chk_start, chk_len, chk_flags, chk_pid) \
233*1031c584SApple OSS Distributions do { \
234*1031c584SApple OSS Distributions if (unlock_type == TYPE_OFD) { \
235*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ofd_unlock(fd, lck_start, lck_len), str " (ofd_unlock)"); \
236*1031c584SApple OSS Distributions } else { \
237*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_unlock(fd, lck_start, lck_len), str " (posix_unlock)"); \
238*1031c584SApple OSS Distributions } \
239*1031c584SApple OSS Distributions GET_CHECK(str, fd, get_start, get_len, get_flags, get_type, chk_start, chk_len, chk_flags, chk_pid); \
240*1031c584SApple OSS Distributions } while (0)
241*1031c584SApple OSS Distributions
242*1031c584SApple OSS Distributions #define A_PATH "basic_lockf_a"
243*1031c584SApple OSS Distributions #define B_PATH "basic_lockf_b"
244*1031c584SApple OSS Distributions
245*1031c584SApple OSS Distributions T_DECL(lockf_basic,
246*1031c584SApple OSS Distributions "Basic test of flock/POSIX/OFD advisory file locks",
247*1031c584SApple OSS Distributions T_META_CHECK_LEAKS(false))
248*1031c584SApple OSS Distributions {
249*1031c584SApple OSS Distributions const char *tmpdir = dt_tmpdir();
250*1031c584SApple OSS Distributions lock_thread_state_t lts[4];
251*1031c584SApple OSS Distributions pid_t pid = getpid();
252*1031c584SApple OSS Distributions int a, a_confined, b, b_confined;
253*1031c584SApple OSS Distributions const off_t file_len = 0x10000;
254*1031c584SApple OSS Distributions T_SETUPBEGIN;
255*1031c584SApple OSS Distributions
256*1031c584SApple OSS Distributions /* random sleeping to hunt for races */
257*1031c584SApple OSS Distributions
258*1031c584SApple OSS Distributions unsigned seed = (unsigned)pid;
259*1031c584SApple OSS Distributions const char *p = getenv("LOCKF_BASIC_SRAND_SEED");
260*1031c584SApple OSS Distributions if (p) {
261*1031c584SApple OSS Distributions seed = (unsigned)atol(p);
262*1031c584SApple OSS Distributions }
263*1031c584SApple OSS Distributions srand(seed);
264*1031c584SApple OSS Distributions
265*1031c584SApple OSS Distributions /* Create two test files, a and b */
266*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(chdir(tmpdir), "chdir(%s)", tmpdir);
267*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS((a = open(A_PATH, O_CREAT | O_RDWR, 0666)), "open(a)");
268*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS((b = open(B_PATH, O_CREAT | O_RDWR, 0666)), "open(b)");
269*1031c584SApple OSS Distributions
270*1031c584SApple OSS Distributions /* Give both files 64KiB */
271*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ftruncate(a, file_len), "truncate a");
272*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ftruncate(b, 0x10000), "truncate b");
273*1031c584SApple OSS Distributions
274*1031c584SApple OSS Distributions /* Open a/b again, but CONFINED this time */
275*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS((a_confined = open(A_PATH, O_CLOFORK | O_RDWR)), "open(a, O_CLOFORK)");
276*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(a_confined, F_SETCONFINED, 1), "F_SETCONFINED");
277*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS((b_confined = open(B_PATH, O_CLOFORK | O_RDWR)), "open(b, O_CLOFORK)");
278*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(b_confined, F_SETCONFINED, 1), "F_SETCONFINED");
279*1031c584SApple OSS Distributions
280*1031c584SApple OSS Distributions T_SETUPEND;
281*1031c584SApple OSS Distributions
282*1031c584SApple OSS Distributions /* Test all coalescence cases (non-upgrade/downgrade) */
283*1031c584SApple OSS Distributions
284*1031c584SApple OSS Distributions /*
285*1031c584SApple OSS Distributions * [ ]
286*1031c584SApple OSS Distributions * + [ ]
287*1031c584SApple OSS Distributions * = [ ]
288*1031c584SApple OSS Distributions */
289*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_lock(a, 130, 20, 0), "Coalesce: initial posix lock");
290*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: equal", a,
291*1031c584SApple OSS Distributions 130, 20, 0, TYPE_POSIX, /* POSIX-lock a, shared, from [100..199] */
292*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD, /* OFD-get the entire file, exclusively */
293*1031c584SApple OSS Distributions 130, 20, 0, pid); /* The result should be: [100..199] is locked shared by our PID */
294*1031c584SApple OSS Distributions
295*1031c584SApple OSS Distributions /*
296*1031c584SApple OSS Distributions * [ ]
297*1031c584SApple OSS Distributions * + [ ]
298*1031c584SApple OSS Distributions * = [ ]
299*1031c584SApple OSS Distributions */
300*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: adjacent high", a,
301*1031c584SApple OSS Distributions 150, 25, 0, TYPE_POSIX,
302*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
303*1031c584SApple OSS Distributions 130, 45, 0, pid);
304*1031c584SApple OSS Distributions
305*1031c584SApple OSS Distributions /*
306*1031c584SApple OSS Distributions * [ ]
307*1031c584SApple OSS Distributions * + [ ]
308*1031c584SApple OSS Distributions * = [ ]
309*1031c584SApple OSS Distributions */
310*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: adjacent low", a,
311*1031c584SApple OSS Distributions 125, 5, 0, TYPE_POSIX,
312*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
313*1031c584SApple OSS Distributions 125, 50, 0, pid);
314*1031c584SApple OSS Distributions
315*1031c584SApple OSS Distributions /*
316*1031c584SApple OSS Distributions * [ ]
317*1031c584SApple OSS Distributions * + [ ]
318*1031c584SApple OSS Distributions * = [ ]
319*1031c584SApple OSS Distributions */
320*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: subsume smaller", a,
321*1031c584SApple OSS Distributions 150, 10, 0, TYPE_POSIX,
322*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
323*1031c584SApple OSS Distributions 125, 50, 0, pid);
324*1031c584SApple OSS Distributions
325*1031c584SApple OSS Distributions /*
326*1031c584SApple OSS Distributions * [ ]
327*1031c584SApple OSS Distributions * + [ ]
328*1031c584SApple OSS Distributions * = [ ]
329*1031c584SApple OSS Distributions */
330*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: subsume larger", a,
331*1031c584SApple OSS Distributions 100, 100, 0, TYPE_POSIX,
332*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
333*1031c584SApple OSS Distributions 100, 100, 0, pid);
334*1031c584SApple OSS Distributions
335*1031c584SApple OSS Distributions /*
336*1031c584SApple OSS Distributions * [ ]
337*1031c584SApple OSS Distributions * + [ ]
338*1031c584SApple OSS Distributions * = [ ]
339*1031c584SApple OSS Distributions */
340*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: extend high", a,
341*1031c584SApple OSS Distributions 100, 125, 0, TYPE_POSIX,
342*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
343*1031c584SApple OSS Distributions 100, 125, 0, pid);
344*1031c584SApple OSS Distributions
345*1031c584SApple OSS Distributions /*
346*1031c584SApple OSS Distributions * [ ]
347*1031c584SApple OSS Distributions * + [ ]
348*1031c584SApple OSS Distributions * = [ ]
349*1031c584SApple OSS Distributions */
350*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: extend low", a,
351*1031c584SApple OSS Distributions 75, 150, 0, TYPE_POSIX,
352*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
353*1031c584SApple OSS Distributions 75, 150, 0, pid);
354*1031c584SApple OSS Distributions
355*1031c584SApple OSS Distributions /*
356*1031c584SApple OSS Distributions * [ ]
357*1031c584SApple OSS Distributions * + [ ]
358*1031c584SApple OSS Distributions * = [ ]
359*1031c584SApple OSS Distributions */
360*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: overlap start", a,
361*1031c584SApple OSS Distributions 50, 100, 0, TYPE_POSIX,
362*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
363*1031c584SApple OSS Distributions 50, 175, 0, pid);
364*1031c584SApple OSS Distributions
365*1031c584SApple OSS Distributions /*
366*1031c584SApple OSS Distributions * [ ]
367*1031c584SApple OSS Distributions * + [ ]
368*1031c584SApple OSS Distributions * = [ ]
369*1031c584SApple OSS Distributions */
370*1031c584SApple OSS Distributions LOCK_AND_CHECK("Coalesce: overlap end", a,
371*1031c584SApple OSS Distributions 150, 100, 0, TYPE_POSIX,
372*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
373*1031c584SApple OSS Distributions 50, 200, 0, pid);
374*1031c584SApple OSS Distributions
375*1031c584SApple OSS Distributions /* Test all upgrade cases */
376*1031c584SApple OSS Distributions
377*1031c584SApple OSS Distributions /*
378*1031c584SApple OSS Distributions * [ R ]
379*1031c584SApple OSS Distributions * + [ W ]
380*1031c584SApple OSS Distributions * = [ R | W ]
381*1031c584SApple OSS Distributions */
382*1031c584SApple OSS Distributions LOCK_AND_CHECK("Upgrade: adjacent high not-coalesced", a,
383*1031c584SApple OSS Distributions 250, 50, EXCL, TYPE_POSIX, /* Take the posix lock exclusively */
384*1031c584SApple OSS Distributions 50, 250, 0, TYPE_OFD, /* and OFD-get shared */
385*1031c584SApple OSS Distributions 250, 50, EXCL, pid);
386*1031c584SApple OSS Distributions
387*1031c584SApple OSS Distributions /*
388*1031c584SApple OSS Distributions * [ R | W ]
389*1031c584SApple OSS Distributions * + [ W ]
390*1031c584SApple OSS Distributions * = [ W | R | W ]
391*1031c584SApple OSS Distributions */
392*1031c584SApple OSS Distributions LOCK_AND_CHECK("Upgrade: adjacent low not-coalesced", a,
393*1031c584SApple OSS Distributions 25, 25, EXCL, TYPE_POSIX,
394*1031c584SApple OSS Distributions 25, 225, 0, TYPE_OFD,
395*1031c584SApple OSS Distributions 25, 25, EXCL, pid);
396*1031c584SApple OSS Distributions
397*1031c584SApple OSS Distributions /*
398*1031c584SApple OSS Distributions * 25 50 250 300
399*1031c584SApple OSS Distributions * [ W | R | W ]
400*1031c584SApple OSS Distributions * + [ W ]
401*1031c584SApple OSS Distributions * [ W | R | W ]
402*1031c584SApple OSS Distributions * 25 50 225 300
403*1031c584SApple OSS Distributions */
404*1031c584SApple OSS Distributions LOCK_AND_CHECK("Upgrade: truncate shared-end, grow excl-start", a,
405*1031c584SApple OSS Distributions 225, 50, EXCL, TYPE_POSIX,
406*1031c584SApple OSS Distributions 50, 250, 0, TYPE_OFD,
407*1031c584SApple OSS Distributions 225, 75, EXCL, pid);
408*1031c584SApple OSS Distributions
409*1031c584SApple OSS Distributions /*
410*1031c584SApple OSS Distributions * 25 50 225 300
411*1031c584SApple OSS Distributions * [ W | R | W ]
412*1031c584SApple OSS Distributions * + [ W ]
413*1031c584SApple OSS Distributions * = [ W | R | W ]
414*1031c584SApple OSS Distributions * 25 60 225 300
415*1031c584SApple OSS Distributions */
416*1031c584SApple OSS Distributions LOCK_AND_CHECK("Upgrade: truncate shared-start, grow excl-end", a,
417*1031c584SApple OSS Distributions 40, 20, EXCL, TYPE_POSIX,
418*1031c584SApple OSS Distributions 0, 225, 0, TYPE_OFD,
419*1031c584SApple OSS Distributions 25, 35, EXCL, pid);
420*1031c584SApple OSS Distributions
421*1031c584SApple OSS Distributions /*
422*1031c584SApple OSS Distributions * 25 60 225 300
423*1031c584SApple OSS Distributions * [ W | R | W ]
424*1031c584SApple OSS Distributions * + [ W ]
425*1031c584SApple OSS Distributions * = [ W | | W | | W ]
426*1031c584SApple OSS Distributions * 25 60 100 150 225 300
427*1031c584SApple OSS Distributions */
428*1031c584SApple OSS Distributions LOCK_AND_CHECK("Upgrade: 3-way split", a,
429*1031c584SApple OSS Distributions 100, 50, EXCL, TYPE_POSIX,
430*1031c584SApple OSS Distributions 60, 165, 0, TYPE_OFD,
431*1031c584SApple OSS Distributions 100, 50, EXCL, pid);
432*1031c584SApple OSS Distributions
433*1031c584SApple OSS Distributions /*
434*1031c584SApple OSS Distributions * 25 60 100 150 225 300
435*1031c584SApple OSS Distributions * [ W | | W | | W ]
436*1031c584SApple OSS Distributions * + [ W ]
437*1031c584SApple OSS Distributions * = [ W ]
438*1031c584SApple OSS Distributions * 25 300
439*1031c584SApple OSS Distributions */
440*1031c584SApple OSS Distributions LOCK_AND_CHECK("Upgrade: subsume multiple locks", a,
441*1031c584SApple OSS Distributions 25, 275, EXCL, TYPE_POSIX,
442*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
443*1031c584SApple OSS Distributions 25, 275, EXCL, pid);
444*1031c584SApple OSS Distributions
445*1031c584SApple OSS Distributions /* Unlock / waiter-wakeup cases */
446*1031c584SApple OSS Distributions
447*1031c584SApple OSS Distributions /*
448*1031c584SApple OSS Distributions * 25 300
449*1031c584SApple OSS Distributions * [ W ]
450*1031c584SApple OSS Distributions * + [W (wait)]
451*1031c584SApple OSS Distributions * 290 310
452*1031c584SApple OSS Distributions */
453*1031c584SApple OSS Distributions start_lock_thread(TYPE_OFD, <s[0], a, 290, 20, EXCL | WAIT); /* Wait on this lock in another thread */
454*1031c584SApple OSS Distributions random_pause();
455*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Unlock: created waiting lock");
456*1031c584SApple OSS Distributions
457*1031c584SApple OSS Distributions /*
458*1031c584SApple OSS Distributions * 25 300
459*1031c584SApple OSS Distributions * [ W ]
460*1031c584SApple OSS Distributions * - [ - ]
461*1031c584SApple OSS Distributions * = [ W ]
462*1031c584SApple OSS Distributions * 25 300
463*1031c584SApple OSS Distributions */
464*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: region with no locks", a,
465*1031c584SApple OSS Distributions 300, 100, TYPE_POSIX,
466*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
467*1031c584SApple OSS Distributions 25, 275, EXCL, pid);
468*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Unlock: waiter still waiting");
469*1031c584SApple OSS Distributions
470*1031c584SApple OSS Distributions /*
471*1031c584SApple OSS Distributions * 25 300
472*1031c584SApple OSS Distributions * [ W ]
473*1031c584SApple OSS Distributions * - [ - ]
474*1031c584SApple OSS Distributions * = [ W ]
475*1031c584SApple OSS Distributions * 25 250
476*1031c584SApple OSS Distributions */
477*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: overlap end", a,
478*1031c584SApple OSS Distributions 250, 100, TYPE_POSIX,
479*1031c584SApple OSS Distributions 25, 1, 0, TYPE_OFD,
480*1031c584SApple OSS Distributions 25, 225, EXCL, pid);
481*1031c584SApple OSS Distributions
482*1031c584SApple OSS Distributions /*
483*1031c584SApple OSS Distributions * 25 250
484*1031c584SApple OSS Distributions * [ W ]
485*1031c584SApple OSS Distributions * [ W ]
486*1031c584SApple OSS Distributions * 290 310
487*1031c584SApple OSS Distributions */
488*1031c584SApple OSS Distributions
489*1031c584SApple OSS Distributions while (!has_completed(<s[0])) {
490*1031c584SApple OSS Distributions random_pause();
491*1031c584SApple OSS Distributions }
492*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[0]), "Unlock: waiter woke up");
493*1031c584SApple OSS Distributions T_ASSERT_POSIX_ZERO(lts[0].err, "Unlock: no err granting waiter");
494*1031c584SApple OSS Distributions GET_CHECK("Unlock: waiter granted confirmation", a,
495*1031c584SApple OSS Distributions 250, 100, 0, TYPE_POSIX,
496*1031c584SApple OSS Distributions 290, 20, EXCL, -1); /* -1 because it's a non-CONFINED OFD lock (with no PID) */
497*1031c584SApple OSS Distributions
498*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ofd_unlock(a, 290, 20), "Unlock: unlock that now-granted waiter");
499*1031c584SApple OSS Distributions
500*1031c584SApple OSS Distributions /*
501*1031c584SApple OSS Distributions * 25 250
502*1031c584SApple OSS Distributions * [ W ] 290 310
503*1031c584SApple OSS Distributions * [ W ]
504*1031c584SApple OSS Distributions * - [ - ]
505*1031c584SApple OSS Distributions * = [ W ]
506*1031c584SApple OSS Distributions * 25 250
507*1031c584SApple OSS Distributions */
508*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: equal range", a,
509*1031c584SApple OSS Distributions 290, 20, TYPE_POSIX,
510*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
511*1031c584SApple OSS Distributions 25, 225, EXCL, pid);
512*1031c584SApple OSS Distributions
513*1031c584SApple OSS Distributions /*
514*1031c584SApple OSS Distributions * 25 250
515*1031c584SApple OSS Distributions * [ W ]
516*1031c584SApple OSS Distributions * - [ - ]
517*1031c584SApple OSS Distributions * = [ W ]
518*1031c584SApple OSS Distributions * 50 250
519*1031c584SApple OSS Distributions */
520*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: overlap start", a,
521*1031c584SApple OSS Distributions 0, 50, TYPE_POSIX,
522*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
523*1031c584SApple OSS Distributions 50, 200, EXCL, pid);
524*1031c584SApple OSS Distributions
525*1031c584SApple OSS Distributions /*
526*1031c584SApple OSS Distributions * 50 250
527*1031c584SApple OSS Distributions * [ W ]
528*1031c584SApple OSS Distributions * - [ ]
529*1031c584SApple OSS Distributions * = [ W ]
530*1031c584SApple OSS Distributions * 100 250
531*1031c584SApple OSS Distributions */
532*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: start-aligned", a,
533*1031c584SApple OSS Distributions 50, 50, TYPE_POSIX,
534*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
535*1031c584SApple OSS Distributions 100, 150, EXCL, pid);
536*1031c584SApple OSS Distributions
537*1031c584SApple OSS Distributions /*
538*1031c584SApple OSS Distributions * 100 250
539*1031c584SApple OSS Distributions * [ W ]
540*1031c584SApple OSS Distributions * - [ ]
541*1031c584SApple OSS Distributions * = [ W ]
542*1031c584SApple OSS Distributions * 100 200
543*1031c584SApple OSS Distributions */
544*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: end-aligned", a,
545*1031c584SApple OSS Distributions 200, 50, TYPE_POSIX,
546*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
547*1031c584SApple OSS Distributions 100, 100, EXCL, pid);
548*1031c584SApple OSS Distributions
549*1031c584SApple OSS Distributions /*
550*1031c584SApple OSS Distributions * 100 200
551*1031c584SApple OSS Distributions * [ W ]
552*1031c584SApple OSS Distributions * - [ ]
553*1031c584SApple OSS Distributions * = [ W ] [ W ]
554*1031c584SApple OSS Distributions * 100 125 175 200
555*1031c584SApple OSS Distributions */
556*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: split", a,
557*1031c584SApple OSS Distributions 125, 50, TYPE_POSIX,
558*1031c584SApple OSS Distributions 0, 150, 0, TYPE_OFD,
559*1031c584SApple OSS Distributions 100, 25, EXCL, pid);
560*1031c584SApple OSS Distributions
561*1031c584SApple OSS Distributions /* Check the tail-fragment too */
562*1031c584SApple OSS Distributions GET_CHECK("Unlock: split (tail-check)", a,
563*1031c584SApple OSS Distributions 125, 200, 0, TYPE_OFD,
564*1031c584SApple OSS Distributions 175, 25, EXCL, pid);
565*1031c584SApple OSS Distributions
566*1031c584SApple OSS Distributions /*
567*1031c584SApple OSS Distributions * 100 125 175 200
568*1031c584SApple OSS Distributions * [ W ] [ W ]
569*1031c584SApple OSS Distributions * - [ ]
570*1031c584SApple OSS Distributions * = (no locks)
571*1031c584SApple OSS Distributions */
572*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Unlock: multiple locks", a,
573*1031c584SApple OSS Distributions 0, 0, TYPE_POSIX,
574*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
575*1031c584SApple OSS Distributions 0, 0, UNLOCK, -1);
576*1031c584SApple OSS Distributions
577*1031c584SApple OSS Distributions
578*1031c584SApple OSS Distributions /*
579*1031c584SApple OSS Distributions * Downgrade / waiter-wakeup test:
580*1031c584SApple OSS Distributions * The only interesting different between this and upgrade is that waiting locks
581*1031c584SApple OSS Distributions * may now be granted. So let's test that...
582*1031c584SApple OSS Distributions */
583*1031c584SApple OSS Distributions
584*1031c584SApple OSS Distributions /*
585*1031c584SApple OSS Distributions * Create this lock layout
586*1031c584SApple OSS Distributions * 0 500
587*1031c584SApple OSS Distributions * [ W ]
588*1031c584SApple OSS Distributions *
589*1031c584SApple OSS Distributions * 0 100 200 300 400 600
590*1031c584SApple OSS Distributions * [R(wait)] [R(wait)] [ R(wait) ]
591*1031c584SApple OSS Distributions *
592*1031c584SApple OSS Distributions * 50 450
593*1031c584SApple OSS Distributions * [ R(wait) ]
594*1031c584SApple OSS Distributions */
595*1031c584SApple OSS Distributions
596*1031c584SApple OSS Distributions LOCK_AND_CHECK("Downgrade: first lock", a,
597*1031c584SApple OSS Distributions 0, 500, EXCL, TYPE_POSIX,
598*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
599*1031c584SApple OSS Distributions 0, 500, EXCL, pid);
600*1031c584SApple OSS Distributions
601*1031c584SApple OSS Distributions start_lock_thread(TYPE_OFD, <s[0], a, 0, 100, WAIT);
602*1031c584SApple OSS Distributions start_lock_thread(TYPE_OFD, <s[1], a, 200, 100, WAIT);
603*1031c584SApple OSS Distributions start_lock_thread(TYPE_OFD, <s[2], a, 400, 200, WAIT);
604*1031c584SApple OSS Distributions start_lock_thread(TYPE_OFD, <s[3], a, 50, 400, WAIT);
605*1031c584SApple OSS Distributions
606*1031c584SApple OSS Distributions random_pause(); /* wait a bit to allow the lock threads to run */
607*1031c584SApple OSS Distributions
608*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Downgrade: waiter 0 waiting");;
609*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[1]), "Downgrade: waiter 1 waiting");
610*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[2]), "Downgrade: waiter 2 waiting");
611*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[3]), "Downgrade: waiter 3 waiting");
612*1031c584SApple OSS Distributions
613*1031c584SApple OSS Distributions /* Open a gap just wide enough for the [200..300] lock to be granted */
614*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_lock(a, 200, 100, 0), "Downgrade [200..300]");
615*1031c584SApple OSS Distributions
616*1031c584SApple OSS Distributions /*
617*1031c584SApple OSS Distributions * (We can't use LOCK_AND_CHECK here, because there may be two shared locks
618*1031c584SApple OSS Distributions * with that range (our downgrade, plus waiter #1) - so ofd_get() could return
619*1031c584SApple OSS Distributions * either one non-deterministically.)
620*1031c584SApple OSS Distributions */
621*1031c584SApple OSS Distributions
622*1031c584SApple OSS Distributions while (!has_completed(<s[1])) {
623*1031c584SApple OSS Distributions random_pause(); /* wait for waiter #1 to complete */
624*1031c584SApple OSS Distributions }
625*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Downgrade: waiter 0 waiting");
626*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[1]), "Downgrade: waiter 1 awoken");
627*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[2]), "Downgrade: waiter 2 waiting");
628*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[3]), "Downgrade: waiter 3 waiting");
629*1031c584SApple OSS Distributions
630*1031c584SApple OSS Distributions /* Open a gap just wide enough for the [400..600] lock to be granted */
631*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_lock(a, 400, 100, 0), "Downgrade [400..500]");
632*1031c584SApple OSS Distributions
633*1031c584SApple OSS Distributions while (!has_completed(<s[2])) {
634*1031c584SApple OSS Distributions random_pause(); /* wait for waiter #2 to complete */
635*1031c584SApple OSS Distributions }
636*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Downgrade: waiter 0 waiting");
637*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[2]), "Downgrade: waiter 2 awoken");
638*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[3]), "Downgrade: waiter 3 waiting");
639*1031c584SApple OSS Distributions
640*1031c584SApple OSS Distributions /* Downgrade the remaining chunks */
641*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_lock(a, 0, 500, 0), "Downgrade [0..500]");
642*1031c584SApple OSS Distributions
643*1031c584SApple OSS Distributions while (!has_completed(<s[0]) || !has_completed(<s[3])) {
644*1031c584SApple OSS Distributions random_pause(); /* wait for waiters #0 and #3 to complete */
645*1031c584SApple OSS Distributions }
646*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[0]), "Downgrade: waiter 0 awoken");
647*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[3]), "Downgrade: waiter 3 awoken");
648*1031c584SApple OSS Distributions
649*1031c584SApple OSS Distributions /* Unlock the remaining OFD shared locks */
650*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Downgrade: cleanup (unlock all shared OFD locks)", a,
651*1031c584SApple OSS Distributions 0, 0, TYPE_OFD,
652*1031c584SApple OSS Distributions 0, 0, 0, TYPE_POSIX,
653*1031c584SApple OSS Distributions 0, 0, UNLOCK, -1);
654*1031c584SApple OSS Distributions
655*1031c584SApple OSS Distributions /* Unlock the remaining POSIX lock [0..500] */
656*1031c584SApple OSS Distributions UNLOCK_AND_CHECK("Downgrade: cleanup (unlock all shared posix locks)", a,
657*1031c584SApple OSS Distributions 0, 500, TYPE_POSIX,
658*1031c584SApple OSS Distributions 0, 0, 0, TYPE_OFD,
659*1031c584SApple OSS Distributions 0, 0, UNLOCK, -1);
660*1031c584SApple OSS Distributions
661*1031c584SApple OSS Distributions
662*1031c584SApple OSS Distributions /* Test SEEK_END flock range decoding */
663*1031c584SApple OSS Distributions
664*1031c584SApple OSS Distributions {
665*1031c584SApple OSS Distributions /* SEEK_END start=-10 len=10 */
666*1031c584SApple OSS Distributions struct flock fl = {.l_start = -10, .l_len = 10, .l_type = F_WRLCK, .l_whence = SEEK_END};
667*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(a, F_SETLK, &fl), "SEEK_END: -10, 10");
668*1031c584SApple OSS Distributions GET_CHECK("SEEK_END: -10, 10 ", a,
669*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
670*1031c584SApple OSS Distributions file_len - 10, 10, EXCL, pid);
671*1031c584SApple OSS Distributions }
672*1031c584SApple OSS Distributions
673*1031c584SApple OSS Distributions {
674*1031c584SApple OSS Distributions /* SEEK_END start=-10 len=10 */
675*1031c584SApple OSS Distributions struct flock fl = {.l_start = -file_len, .l_len = file_len, .l_type = F_WRLCK, .l_whence = SEEK_END};
676*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fcntl(a, F_SETLK, &fl), "SEEK_END: -file_len, file_len");
677*1031c584SApple OSS Distributions GET_CHECK("SEEK_END: -file_len, file_len", a,
678*1031c584SApple OSS Distributions 0, 0, EXCL, TYPE_OFD,
679*1031c584SApple OSS Distributions 0, file_len, EXCL, pid);
680*1031c584SApple OSS Distributions }
681*1031c584SApple OSS Distributions
682*1031c584SApple OSS Distributions {
683*1031c584SApple OSS Distributions /* Negative case: SEEK_END start=-(file_len + 10) len=20 */
684*1031c584SApple OSS Distributions struct flock fl = {.l_start = -(file_len + 10), .l_len = 20, .l_type = F_WRLCK, .l_whence = SEEK_END};
685*1031c584SApple OSS Distributions T_EXPECT_TRUE(fcntl(a, F_SETLK, &fl) && errno == EINVAL, "SEEK_END: -(file_len + 10), 20 => EINVAL");
686*1031c584SApple OSS Distributions }
687*1031c584SApple OSS Distributions
688*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_unlock(a, 0, 0), "SEEK_END: cleanup (release locks)");
689*1031c584SApple OSS Distributions
690*1031c584SApple OSS Distributions /* Test interactions between all 3 lock types */
691*1031c584SApple OSS Distributions
692*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(flock(a, LOCK_SH | LOCK_NB), "Interaction: flock(a, shared)");
693*1031c584SApple OSS Distributions
694*1031c584SApple OSS Distributions /* Take (waiting) exclusive locks in all 3 types on a_confined */
695*1031c584SApple OSS Distributions start_lock_thread(TYPE_FLOCK, <s[0], a_confined, 0, 0, EXCL | WAIT);
696*1031c584SApple OSS Distributions start_lock_thread(TYPE_POSIX, <s[1], a_confined, 0, 0, EXCL | WAIT);
697*1031c584SApple OSS Distributions start_lock_thread(TYPE_OFD, <s[2], a_confined, 0, 0, EXCL | WAIT);
698*1031c584SApple OSS Distributions
699*1031c584SApple OSS Distributions /* Yuck. Allow time for these threads to run -and- block on the flock lock */
700*1031c584SApple OSS Distributions sleep(1);
701*1031c584SApple OSS Distributions
702*1031c584SApple OSS Distributions /* Take shared locks in the remaining 2 types (posix/ofd) on a */
703*1031c584SApple OSS Distributions
704*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_lock(a, 0, 0, 0), "Interaction: posix_lock(a, 0, 0, 0)");
705*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ofd_lock(a, 0, 0, 0), "Interaction: ofd_lock(a, 0, 0, 0)");
706*1031c584SApple OSS Distributions
707*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Interaction: flock-waiter is starting or waiting");
708*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[1]), "Interaction: posix-waiter is starting or waiting");
709*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[2]), "Interaction: ofd-waiter is starting or waiting");
710*1031c584SApple OSS Distributions
711*1031c584SApple OSS Distributions T_EXPECT_POSIX_FAILURE(flock(a, LOCK_EX | LOCK_NB), EAGAIN, "Interaction: can't flock-upgrade");
712*1031c584SApple OSS Distributions T_EXPECT_POSIX_FAILURE(posix_lock(a, 0, 0, EXCL), EAGAIN, "Interaction: can't posix-upgrade");
713*1031c584SApple OSS Distributions T_EXPECT_POSIX_FAILURE(ofd_lock(a, 0, 0, EXCL), EAGAIN, "Interaction: can't ofd-upgrade");
714*1031c584SApple OSS Distributions
715*1031c584SApple OSS Distributions /*
716*1031c584SApple OSS Distributions * At this point:
717*1031c584SApple OSS Distributions * - 'a' owns a shared flock && a shared OFD lock
718*1031c584SApple OSS Distributions * - this process owns a shared POSIX lock
719*1031c584SApple OSS Distributions * - three threads are (hopefully) blocked waiting to take exclusive locks
720*1031c584SApple OSS Distributions * on 'a_confined.'
721*1031c584SApple OSS Distributions *
722*1031c584SApple OSS Distributions * Drop the POSIX lock ...
723*1031c584SApple OSS Distributions */
724*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_unlock(a, 0, 0), "Interaction: Unlock posix");
725*1031c584SApple OSS Distributions
726*1031c584SApple OSS Distributions random_pause(); /* wait a bit to see if there are consequences for the waiting locks */
727*1031c584SApple OSS Distributions
728*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Interaction: flock-waiter is still starting or waiting");
729*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[1]), "Interaction: posix-waiter is still starting or waiting");
730*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[2]), "Interaction: ofd-waiter is still starting or waiting");
731*1031c584SApple OSS Distributions
732*1031c584SApple OSS Distributions /*
733*1031c584SApple OSS Distributions * and drop the flock lock ...
734*1031c584SApple OSS Distributions */
735*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(flock(a, LOCK_UN), "Interaction: Unlock flock");
736*1031c584SApple OSS Distributions
737*1031c584SApple OSS Distributions random_pause(); /* wait a bit to see if there are consequences for the waiting locks */
738*1031c584SApple OSS Distributions
739*1031c584SApple OSS Distributions // Check that rdar://102160410 remains fixed.
740*1031c584SApple OSS Distributions // Before that, the LOCK_UN above would release the OFD lock too
741*1031c584SApple OSS Distributions
742*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[0]), "Interaction: flock-waiter is still starting or waiting");
743*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[1]), "Interaction: posix-waiter is still starting or waiting");
744*1031c584SApple OSS Distributions T_ASSERT_FALSE(has_completed(<s[2]), "Interaction: ofd-waiter is still starting or waiting");
745*1031c584SApple OSS Distributions
746*1031c584SApple OSS Distributions /*
747*1031c584SApple OSS Distributions * and finally drop the OFD lock, which should let one of the blocked threads
748*1031c584SApple OSS Distributions * acquire an exclusive lock. Work through them turn by turn.
749*1031c584SApple OSS Distributions */
750*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ofd_unlock(a, 0, 0), "Interaction: Unlock ofd");
751*1031c584SApple OSS Distributions
752*1031c584SApple OSS Distributions bool unlocked[3] = {
753*1031c584SApple OSS Distributions false, false, false
754*1031c584SApple OSS Distributions };
755*1031c584SApple OSS Distributions
756*1031c584SApple OSS Distributions for (uint32_t waiters = 3; waiters > 0; waiters--) {
757*1031c584SApple OSS Distributions uint32_t num_waiting = 0;
758*1031c584SApple OSS Distributions do {
759*1031c584SApple OSS Distributions random_pause(); /* wait for consequences for the waiting locks */
760*1031c584SApple OSS Distributions num_waiting = !has_completed(<s[0]) + !has_completed(<s[1]) + !has_completed(<s[2]);
761*1031c584SApple OSS Distributions } while (num_waiting == waiters);
762*1031c584SApple OSS Distributions
763*1031c584SApple OSS Distributions T_ASSERT_EQ(num_waiting, waiters - 1, "Interaction: 1 waiter awoke");
764*1031c584SApple OSS Distributions
765*1031c584SApple OSS Distributions if (has_completed(<s[0]) && !unlocked[0]) {
766*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(flock(a_confined, LOCK_UN), "Interaction: Flock awoke, unlocking");
767*1031c584SApple OSS Distributions unlocked[0] = true;
768*1031c584SApple OSS Distributions } else if (has_completed(<s[1]) && !unlocked[1]) {
769*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(posix_unlock(a_confined, 0, 0), "Interaction: posix awoke, unlocking");
770*1031c584SApple OSS Distributions unlocked[1] = true;
771*1031c584SApple OSS Distributions } else if (has_completed(<s[2]) && !unlocked[2]) {
772*1031c584SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ofd_unlock(a_confined, 0, 0), "Interaction: ofd awoke, unlocking");
773*1031c584SApple OSS Distributions unlocked[2] = true;
774*1031c584SApple OSS Distributions }
775*1031c584SApple OSS Distributions }
776*1031c584SApple OSS Distributions
777*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[0]), "Interaction: flock-waiter has completed");
778*1031c584SApple OSS Distributions T_ASSERT_TRUE(unlocked[0], "Interaction: flock-waiter was unlocked");
779*1031c584SApple OSS Distributions
780*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[1]), "Interaction: posix-waiter has completed");
781*1031c584SApple OSS Distributions T_ASSERT_TRUE(unlocked[1], "Interaction: posix-waiter was unlocked");
782*1031c584SApple OSS Distributions
783*1031c584SApple OSS Distributions T_ASSERT_TRUE(has_completed(<s[2]), "Interaction: ofd-waiter has completed");
784*1031c584SApple OSS Distributions T_ASSERT_TRUE(unlocked[2], "Interaction: ofd-waiter was unlocked");
785*1031c584SApple OSS Distributions }
786