-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjc-lockdebug.mm
More file actions
622 lines (501 loc) · 15.5 KB
/
objc-lockdebug.mm
File metadata and controls
622 lines (501 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
* Copyright (c) 2007 Apple Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/***********************************************************************
* objc-lock.m
* Error-checking locks for debugging.
**********************************************************************/
#include "objc-private.h"
#if LOCKDEBUG && !TARGET_OS_WIN32
#include <unordered_map>
/***********************************************************************
* Thread-local bool set during _objc_atfork_prepare().
* That function is allowed to break some lock ordering rules.
**********************************************************************/
static tls_key_t fork_prepare_tls;
void
lockdebug_setInForkPrepare(bool inForkPrepare)
{
INIT_ONCE_PTR(fork_prepare_tls, tls_create(nil), (void)0);
tls_set(fork_prepare_tls, (void*)inForkPrepare);
}
static bool
inForkPrepare()
{
INIT_ONCE_PTR(fork_prepare_tls, tls_create(nil), (void)0);
return (bool)tls_get(fork_prepare_tls);
}
/***********************************************************************
* Lock order graph.
* "lock X precedes lock Y" means that X must be acquired first.
* This property is transitive.
**********************************************************************/
struct lockorder {
const void *l;
std::vector<const lockorder *> predecessors;
mutable std::unordered_map<const lockorder *, bool> memo;
lockorder(const void *newl) : l(newl) { }
};
static std::unordered_map<const void*, lockorder *> lockOrderList;
// not mutex_t because we don't want lock debugging on this lock
static mutex_tt<false> lockOrderLock;
static bool
lockPrecedesLock(const lockorder *oldlock, const lockorder *newlock)
{
auto memoed = newlock->memo.find(oldlock);
if (memoed != newlock->memo.end()) {
return memoed->second;
}
bool result = false;
for (const auto *pre : newlock->predecessors) {
if (oldlock == pre || lockPrecedesLock(oldlock, pre)) {
result = true;
break;
}
}
newlock->memo[oldlock] = result;
return result;
}
static bool
lockPrecedesLock(const void *oldlock, const void *newlock)
{
mutex_tt<false>::locker lock(lockOrderLock);
auto oldorder = lockOrderList.find(oldlock);
auto neworder = lockOrderList.find(newlock);
if (neworder == lockOrderList.end() || oldorder == lockOrderList.end()) {
return false;
}
return lockPrecedesLock(oldorder->second, neworder->second);
}
static bool
lockUnorderedWithLock(const void *oldlock, const void *newlock)
{
mutex_tt<false>::locker lock(lockOrderLock);
auto oldorder = lockOrderList.find(oldlock);
auto neworder = lockOrderList.find(newlock);
if (neworder == lockOrderList.end() || oldorder == lockOrderList.end()) {
return true;
}
if (lockPrecedesLock(oldorder->second, neworder->second) ||
lockPrecedesLock(neworder->second, oldorder->second))
{
return false;
}
return true;
}
void lockdebug_lock_precedes_lock(const void *oldlock, const void *newlock)
{
if (lockPrecedesLock(newlock, oldlock)) {
_objc_fatal("contradiction in lock order declaration");
}
mutex_tt<false>::locker lock(lockOrderLock);
auto oldorder = lockOrderList.find(oldlock);
auto neworder = lockOrderList.find(newlock);
if (oldorder == lockOrderList.end()) {
lockOrderList[oldlock] = new lockorder(oldlock);
oldorder = lockOrderList.find(oldlock);
}
if (neworder == lockOrderList.end()) {
lockOrderList[newlock] = new lockorder(newlock);
neworder = lockOrderList.find(newlock);
}
neworder->second->predecessors.push_back(oldorder->second);
}
/***********************************************************************
* Recording - per-thread list of mutexes and monitors held
**********************************************************************/
enum class lockkind {
MUTEX = 1, MONITOR = 2, RDLOCK = 3, WRLOCK = 4, RECURSIVE = 5
};
#define MUTEX lockkind::MUTEX
#define MONITOR lockkind::MONITOR
#define RDLOCK lockkind::RDLOCK
#define WRLOCK lockkind::WRLOCK
#define RECURSIVE lockkind::RECURSIVE
struct lockcount {
lockkind k; // the kind of lock it is (MUTEX, MONITOR, etc)
int i; // the lock's nest count
};
using objc_lock_list = std::unordered_map<const void *, lockcount>;
// Thread-local list of locks owned by a thread.
// Used by lock ownership checks.
static tls_key_t lock_tls;
// Global list of all locks.
// Used by fork() safety check.
// This can't be a static struct because of C++ initialization order problems.
static objc_lock_list& AllLocks() {
static objc_lock_list *locks;
INIT_ONCE_PTR(locks, new objc_lock_list, (void)0);
return *locks;
}
static void
destroyLocks(void *value)
{
auto locks = (objc_lock_list *)value;
// fixme complain about any still-held locks?
if (locks) delete locks;
}
static objc_lock_list&
ownedLocks()
{
// Use a dedicated tls key to prevent differences vs non-debug in
// usage of objc's other tls keys (required for some unit tests).
INIT_ONCE_PTR(lock_tls, tls_create(&destroyLocks), (void)0);
auto locks = (objc_lock_list *)tls_get(lock_tls);
if (!locks) {
locks = new objc_lock_list;
tls_set(lock_tls, locks);
}
return *locks;
}
static bool
hasLock(objc_lock_list& locks, const void *lock, lockkind kind)
{
auto iter = locks.find(lock);
if (iter != locks.end() && iter->second.k == kind) return true;
return false;
}
static const char *sym(const void *lock)
{
Dl_info info;
int ok = dladdr(lock, &info);
if (ok && info.dli_sname && info.dli_sname[0]) return info.dli_sname;
else return "??";
}
static void
setLock(objc_lock_list& locks, const void *lock, lockkind kind)
{
// Check if we already own this lock.
auto iter = locks.find(lock);
if (iter != locks.end() && iter->second.k == kind) {
iter->second.i++;
return;
}
// Newly-acquired lock. Verify lock ordering.
// Locks not in AllLocks are exempt (i.e. @synchronize locks)
if (&locks != &AllLocks() && AllLocks().find(lock) != AllLocks().end()) {
for (auto& oldlock : locks) {
if (AllLocks().find(oldlock.first) == AllLocks().end()) {
// oldlock is exempt
continue;
}
if (lockPrecedesLock(lock, oldlock.first)) {
_objc_fatal("lock %p (%s) incorrectly acquired before %p (%s)",
oldlock.first, sym(oldlock.first), lock, sym(lock));
}
if (!inForkPrepare() &&
lockUnorderedWithLock(lock, oldlock.first))
{
// _objc_atfork_prepare is allowed to acquire
// otherwise-unordered locks, but nothing else may.
_objc_fatal("lock %p (%s) acquired before %p (%s) "
"with no defined lock order",
oldlock.first, sym(oldlock.first), lock, sym(lock));
}
}
}
locks[lock] = lockcount{kind, 1};
}
static void
clearLock(objc_lock_list& locks, const void *lock, lockkind kind)
{
auto iter = locks.find(lock);
if (iter != locks.end()) {
auto& l = iter->second;
if (l.k == kind) {
if (--l.i == 0) {
locks.erase(iter);
}
return;
}
}
_objc_fatal("lock not found!");
}
/***********************************************************************
* fork() safety checking
**********************************************************************/
void
lockdebug_remember_mutex(mutex_t *lock)
{
setLock(AllLocks(), lock, MUTEX);
}
void
lockdebug_remember_recursive_mutex(recursive_mutex_t *lock)
{
setLock(AllLocks(), lock, RECURSIVE);
}
void
lockdebug_remember_monitor(monitor_t *lock)
{
setLock(AllLocks(), lock, MONITOR);
}
void
lockdebug_remember_rwlock(rwlock_t *lock)
{
setLock(AllLocks(), lock, WRLOCK);
}
void
lockdebug_assert_all_locks_locked()
{
auto& owned = ownedLocks();
for (const auto& l : AllLocks()) {
if (!hasLock(owned, l.first, l.second.k)) {
_objc_fatal("lock %p:%d is incorrectly not owned",
l.first, l.second.k);
}
}
}
void
lockdebug_assert_no_locks_locked()
{
auto& owned = ownedLocks();
for (const auto& l : AllLocks()) {
if (hasLock(owned, l.first, l.second.k)) {
_objc_fatal("lock %p:%d is incorrectly owned", l.first, l.second.k);
}
}
}
/***********************************************************************
* Mutex checking
**********************************************************************/
void
lockdebug_mutex_lock(mutex_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, MUTEX)) {
_objc_fatal("deadlock: relocking mutex");
}
setLock(locks, lock, MUTEX);
}
// try-lock success is the only case with lockdebug effects.
// try-lock when already locked is OK (will fail)
// try-lock failure does nothing.
void
lockdebug_mutex_try_lock_success(mutex_t *lock)
{
auto& locks = ownedLocks();
setLock(locks, lock, MUTEX);
}
void
lockdebug_mutex_unlock(mutex_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, MUTEX)) {
_objc_fatal("unlocking unowned mutex");
}
clearLock(locks, lock, MUTEX);
}
void
lockdebug_mutex_assert_locked(mutex_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, MUTEX)) {
_objc_fatal("mutex incorrectly not locked");
}
}
void
lockdebug_mutex_assert_unlocked(mutex_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, MUTEX)) {
_objc_fatal("mutex incorrectly locked");
}
}
/***********************************************************************
* Recursive mutex checking
**********************************************************************/
void
lockdebug_recursive_mutex_lock(recursive_mutex_t *lock)
{
auto& locks = ownedLocks();
setLock(locks, lock, RECURSIVE);
}
void
lockdebug_recursive_mutex_unlock(recursive_mutex_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, RECURSIVE)) {
_objc_fatal("unlocking unowned recursive mutex");
}
clearLock(locks, lock, RECURSIVE);
}
void
lockdebug_recursive_mutex_assert_locked(recursive_mutex_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, RECURSIVE)) {
_objc_fatal("recursive mutex incorrectly not locked");
}
}
void
lockdebug_recursive_mutex_assert_unlocked(recursive_mutex_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, RECURSIVE)) {
_objc_fatal("recursive mutex incorrectly locked");
}
}
/***********************************************************************
* Monitor checking
**********************************************************************/
void
lockdebug_monitor_enter(monitor_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, MONITOR)) {
_objc_fatal("deadlock: relocking monitor");
}
setLock(locks, lock, MONITOR);
}
void
lockdebug_monitor_leave(monitor_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, MONITOR)) {
_objc_fatal("unlocking unowned monitor");
}
clearLock(locks, lock, MONITOR);
}
void
lockdebug_monitor_wait(monitor_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, MONITOR)) {
_objc_fatal("waiting in unowned monitor");
}
}
void
lockdebug_monitor_assert_locked(monitor_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, MONITOR)) {
_objc_fatal("monitor incorrectly not locked");
}
}
void
lockdebug_monitor_assert_unlocked(monitor_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, MONITOR)) {
_objc_fatal("monitor incorrectly held");
}
}
/***********************************************************************
* rwlock checking
**********************************************************************/
void
lockdebug_rwlock_read(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, RDLOCK)) {
// Recursive rwlock read is bad (may deadlock vs pending writer)
_objc_fatal("recursive rwlock read");
}
if (hasLock(locks, lock, WRLOCK)) {
_objc_fatal("deadlock: read after write for rwlock");
}
setLock(locks, lock, RDLOCK);
}
// try-read success is the only case with lockdebug effects.
// try-read when already reading is OK (won't deadlock)
// try-read when already writing is OK (will fail)
// try-read failure does nothing.
void
lockdebug_rwlock_try_read_success(rwlock_t *lock)
{
auto& locks = ownedLocks();
setLock(locks, lock, RDLOCK);
}
void
lockdebug_rwlock_unlock_read(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, RDLOCK)) {
_objc_fatal("un-reading unowned rwlock");
}
clearLock(locks, lock, RDLOCK);
}
void
lockdebug_rwlock_write(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, RDLOCK)) {
// Lock promotion not allowed (may deadlock)
_objc_fatal("deadlock: write after read for rwlock");
}
if (hasLock(locks, lock, WRLOCK)) {
_objc_fatal("recursive rwlock write");
}
setLock(locks, lock, WRLOCK);
}
// try-write success is the only case with lockdebug effects.
// try-write when already reading is OK (will fail)
// try-write when already writing is OK (will fail)
// try-write failure does nothing.
void
lockdebug_rwlock_try_write_success(rwlock_t *lock)
{
auto& locks = ownedLocks();
setLock(locks, lock, WRLOCK);
}
void
lockdebug_rwlock_unlock_write(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, WRLOCK)) {
_objc_fatal("un-writing unowned rwlock");
}
clearLock(locks, lock, WRLOCK);
}
void
lockdebug_rwlock_assert_reading(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, RDLOCK)) {
_objc_fatal("rwlock incorrectly not reading");
}
}
void
lockdebug_rwlock_assert_writing(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, WRLOCK)) {
_objc_fatal("rwlock incorrectly not writing");
}
}
void
lockdebug_rwlock_assert_locked(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (!hasLock(locks, lock, RDLOCK) && !hasLock(locks, lock, WRLOCK)) {
_objc_fatal("rwlock incorrectly neither reading nor writing");
}
}
void
lockdebug_rwlock_assert_unlocked(rwlock_t *lock)
{
auto& locks = ownedLocks();
if (hasLock(locks, lock, RDLOCK) || hasLock(locks, lock, WRLOCK)) {
_objc_fatal("rwlock incorrectly not unlocked");
}
}
#endif