File: fuchsia_lock.c

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-20
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,436 kB
  • sloc: cpp: 5,593,990; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (104 lines) | stat: -rw-r--r-- 2,689 bytes parent folder | download | duplicates (8)
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
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.fuchsia.Lock -verify %s

typedef int spin_lock_t;
typedef int zx_status_t;
typedef int zx_time_t;

void spin_lock(spin_lock_t *lock);
int spin_trylock(spin_lock_t *lock);
void spin_unlock(spin_lock_t *lock);
void spin_lock_init(spin_lock_t *lock);

void spin_lock_save(spin_lock_t *lock, void *statep,
                    int flags);
void spin_unlock_restore(spin_lock_t *lock, void *old_state,
                         int flags);

spin_lock_t mtx1;
spin_lock_t mtx2;

void bad1(void)
{
	spin_lock(&mtx1);	// no-warning
	spin_lock(&mtx1);	// expected-warning{{This lock has already been acquired}}
}

void bad2(void) {
  spin_lock(&mtx1);
  spin_unlock(&mtx1);
  spin_unlock(&mtx1); // expected-warning {{This lock has already been unlocked}}
}

void bad3() {
  spin_lock_init(&mtx1);
  if (spin_trylock(&mtx1) != 0)
    spin_unlock(&mtx1); // expected-warning {{This lock has already been unlocked}}
}

void bad4(void) {
  spin_lock(&mtx1);
  spin_lock(&mtx2);
  spin_unlock(&mtx1); // expected-warning {{This was not the most recently acquired lock. Possible lock order reversal}}
  spin_unlock(&mtx2);
}

void good() {
  spin_lock_t mtx;
  spin_lock_init(&mtx);
  spin_lock_save(&mtx, 0, 0);
  spin_unlock_restore(&mtx, 0, 0);
}

void good2() {
  spin_lock_t mtx;
  spin_lock_init(&mtx);
  if (spin_trylock(&mtx) == 0)
    spin_unlock(&mtx);
}

typedef int sync_mutex_t;
void sync_mutex_lock(sync_mutex_t* mutex);
void sync_mutex_lock_with_waiter(sync_mutex_t* mutex);
zx_status_t sync_mutex_timedlock(sync_mutex_t* mutex, zx_time_t deadline);
zx_status_t sync_mutex_trylock(sync_mutex_t* mutex);
void sync_mutex_unlock(sync_mutex_t* mutex);

sync_mutex_t smtx1;
sync_mutex_t smtx2;

void bad11(void)
{
	sync_mutex_lock(&smtx1);	// no-warning
	sync_mutex_lock(&smtx1);	// expected-warning{{This lock has already been acquired}}
}

void bad12(void) {
  sync_mutex_lock_with_waiter(&smtx1);
  sync_mutex_unlock(&smtx1);
  sync_mutex_unlock(&smtx1); // expected-warning {{This lock has already been unlocked}}
}

void bad13() {
  sync_mutex_unlock(&smtx1);
  if (sync_mutex_trylock(&smtx1) != 0)
    sync_mutex_unlock(&smtx1); // expected-warning {{This lock has already been unlocked}}
}

void bad14(void) {
  sync_mutex_lock(&smtx1);
  sync_mutex_lock(&smtx2);
  sync_mutex_unlock(&smtx1); // expected-warning {{This was not the most recently acquired lock. Possible lock order reversal}}
  sync_mutex_unlock(&smtx2);
}

void good11() {
  sync_mutex_t mtx;
  if (sync_mutex_trylock(&mtx) == 0)
    sync_mutex_unlock(&mtx);
}

void good12() {
  sync_mutex_t mtx;
  if (sync_mutex_timedlock(&mtx, 0) == 0)
    sync_mutex_unlock(&mtx);
}