Skip to content

Commit 5f80c4a

Browse files
committed
Update tests for MutexModeling
This patch updates the existing tests to reflect the changes made to themutex modeling system. It modifies the expected output for various testcases to match the new MutexModeling API and the updated checker behavior.
1 parent 9595f37 commit 5f80c4a

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

clang/test/Analysis/analyzer-enabled-checkers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// CHECK-NEXT: core.CallAndMessage
1717
// CHECK-NEXT: core.DivideZero
1818
// CHECK-NEXT: core.DynamicTypePropagation
19+
// CHECK-NEXT: core.MutexModeling
1920
// CHECK-NEXT: core.NonNullParamChecker
2021
// CHECK-NEXT: core.NonnilStringConstants
2122
// CHECK-NEXT: core.NullDereference

clang/test/Analysis/block-in-critical-section.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ int pthread_mutex_trylock(pthread_mutex_t *mutex);
4141
int pthread_mutex_unlock(pthread_mutex_t *mutex);
4242

4343
struct mtx_t;
44+
struct timespec;
4445
int mtx_lock(mtx_t *mutex);
45-
int mtx_timedlock(mtx_t *mutex);
46+
int mtx_timedlock(mtx_t *mutex, const struct timespec *ts);
4647
int mtx_trylock(mtx_t *mutex);
4748
int mtx_unlock(mtx_t *mutex);
4849

@@ -100,7 +101,7 @@ void testBlockInCriticalSectionWithPthreadMutex(pthread_mutex_t *mutex) {
100101
pthread_mutex_unlock(mutex);
101102
}
102103

103-
void testBlockInCriticalSectionC11Locks(mtx_t *mutex) {
104+
void testBlockInCriticalSectionC11Locks(mtx_t *mutex, timespec *ts) {
104105
mtx_lock(mutex); // expected-note 5{{Entering critical section here}}
105106
sleep(3); // expected-warning {{Call to blocking function 'sleep' inside of critical section}}
106107
// expected-note@-1 {{Call to blocking function 'sleep' inside of critical section}}
@@ -114,7 +115,7 @@ void testBlockInCriticalSectionC11Locks(mtx_t *mutex) {
114115
// expected-note@-1 {{Call to blocking function 'recv' inside of critical section}}
115116
mtx_unlock(mutex);
116117

117-
mtx_timedlock(mutex); // expected-note 5{{Entering critical section here}}
118+
mtx_timedlock(mutex, ts); // expected-note 5{{Entering critical section here}}
118119
sleep(3); // expected-warning {{Call to blocking function 'sleep' inside of critical section}}
119120
// expected-note@-1 {{Call to blocking function 'sleep' inside of critical section}}
120121
getc(stream); // expected-warning {{Call to blocking function 'getc' inside of critical section}}
@@ -293,19 +294,13 @@ void testBlockInCriticalSectionUniqueLockNested() {
293294
sleep(1); // no-warning
294295
}
295296

296-
void testTrylockCurrentlyFalsePositive(pthread_mutex_t *m) {
297-
// expected-note@+4 {{Assuming the condition is true}}
298-
// expected-note@+3 {{Taking true branch}}
299-
// expected-note@+2 {{Assuming the condition is false}}
300-
// expected-note@+1 {{Taking false branch}}
301-
if (pthread_mutex_trylock(m) == 0) { // expected-note 2 {{Entering critical section here}}
302-
// FIXME: we are entering the critical section only in the true branch
297+
void testTrylockStateSplitting(pthread_mutex_t *m) {
298+
// expected-note@+1 {{Taking true branch}}
299+
if (pthread_mutex_trylock(m) == 0) { // expected-note {{Entering critical section here}}
303300
sleep(10); // expected-warning {{Call to blocking function 'sleep' inside of critical section}}
304301
// expected-note@-1 {{Call to blocking function 'sleep' inside of critical section}}
305302
pthread_mutex_unlock(m);
306303
} else {
307-
sleep(10); // expected-warning {{Call to blocking function 'sleep' inside of critical section}}
308-
// expected-note@-1 {{Call to blocking function 'sleep' inside of critical section}}
309-
// FIXME: this is a false positive, the lock was not acquired
304+
sleep(10); // no-warning
310305
}
311306
}

clang/test/Analysis/pthreadlock_state.c

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,49 @@ void test(void) {
1515
pthread_mutex_init(&mtx, NULL);
1616
clang_analyzer_printState();
1717
// CHECK: { "checker": "alpha.core.PthreadLockBase", "messages": [
18-
// CHECK-NEXT: "Mutex states:",
19-
// CHECK-NEXT: "mtx: unlocked",
18+
// CHECK-NEXT: "Mutex event:",
19+
// CHECK-NEXT: "Kind: : Init",
20+
// CHECK-NEXT: "Semantics: NotApplicable",
21+
// CHECK-NEXT: "Library: Pthread",
22+
// CHECK-NEXT: "Mutex region: mtx",
23+
// CHECK-NEXT: "Mutex states:",
24+
// CHECK-NEXT: "mtx: unlocked",
2025
// CHECK-NEXT: ""
2126
// CHECK-NEXT: ]}
2227

2328
pthread_mutex_lock(&mtx);
2429
clang_analyzer_printState();
2530
// CHECK: { "checker": "alpha.core.PthreadLockBase", "messages": [
31+
// CHECK-NEXT: "Mutex event:",
32+
// CHECK-NEXT: "Kind: : Acquire",
33+
// CHECK-NEXT: "Semantics: PthreadSemantics",
34+
// CHECK-NEXT: "Library: Pthread",
35+
// CHECK-NEXT: "Mutex region: mtx",
36+
// CHECK-NEXT: "Kind: : Init",
37+
// CHECK-NEXT: "Semantics: NotApplicable",
38+
// CHECK-NEXT: "Library: Pthread",
39+
// CHECK-NEXT: "Mutex region: mtx",
2640
// CHECK-NEXT: "Mutex states:",
2741
// CHECK-NEXT: "mtx: locked",
28-
// CHECK-NEXT: "Mutex lock order:",
29-
// CHECK-NEXT: "mtx",
3042
// CHECK-NEXT: ""
3143
// CHECK-NEXT: ]}
3244

3345
pthread_mutex_unlock(&mtx);
3446
clang_analyzer_printState();
3547
// CHECK: { "checker": "alpha.core.PthreadLockBase", "messages": [
48+
// CHECK-NEXT: "Mutex event:",
49+
// CHECK-NEXT: "Kind: : Release",
50+
// CHECK-NEXT: "Semantics: NotApplicable",
51+
// CHECK-NEXT: "Library: Pthread",
52+
// CHECK-NEXT: "Mutex region: mtx",
53+
// CHECK-NEXT: "Kind: : Acquire",
54+
// CHECK-NEXT: "Semantics: PthreadSemantics",
55+
// CHECK-NEXT: "Library: Pthread",
56+
// CHECK-NEXT: "Mutex region: mtx",
57+
// CHECK-NEXT: "Kind: : Init",
58+
// CHECK-NEXT: "Semantics: NotApplicable",
59+
// CHECK-NEXT: "Library: Pthread",
60+
// CHECK-NEXT: "Mutex region: mtx",
3661
// CHECK-NEXT: "Mutex states:",
3762
// CHECK-NEXT: "mtx: unlocked",
3863
// CHECK-NEXT: ""
@@ -41,6 +66,23 @@ void test(void) {
4166
int ret = pthread_mutex_destroy(&mtx);
4267
clang_analyzer_printState();
4368
// CHECK: { "checker": "alpha.core.PthreadLockBase", "messages": [
69+
// CHECK-NEXT: "Mutex event:",
70+
// CHECK-NEXT: "Kind: : Destroy",
71+
// CHECK-NEXT: "Semantics: PthreadSemantics",
72+
// CHECK-NEXT: "Library: Pthread",
73+
// CHECK-NEXT: "Mutex region: mtx",
74+
// CHECK-NEXT: "Kind: : Release",
75+
// CHECK-NEXT: "Semantics: NotApplicable",
76+
// CHECK-NEXT: "Library: Pthread",
77+
// CHECK-NEXT: "Mutex region: mtx",
78+
// CHECK-NEXT: "Kind: : Acquire",
79+
// CHECK-NEXT: "Semantics: PthreadSemantics",
80+
// CHECK-NEXT: "Library: Pthread",
81+
// CHECK-NEXT: "Mutex region: mtx",
82+
// CHECK-NEXT: "Kind: : Init",
83+
// CHECK-NEXT: "Semantics: NotApplicable",
84+
// CHECK-NEXT: "Library: Pthread",
85+
// CHECK-NEXT: "Mutex region: mtx",
4486
// CHECK-NEXT: "Mutex states:",
4587
// CHECK-NEXT: "mtx: unlocked, possibly destroyed",
4688
// CHECK-NEXT: "Mutexes in unresolved possibly destroyed state:",
@@ -51,9 +93,10 @@ void test(void) {
5193
if (ret)
5294
return;
5395

96+
// Assert that the 'possibly destroyed' state is now resolved to 'destroyed'.
5497
clang_analyzer_printState();
5598
// CHECK: { "checker": "alpha.core.PthreadLockBase", "messages": [
56-
// CHECK-NEXT: "Mutex states:",
99+
// CHECK: "Mutex states:",
57100
// CHECK-NEXT: "mtx: destroyed",
58101
// CHECK-NEXT: ""
59102
// CHECK-NEXT: ]}

clang/test/Analysis/pthreadlock_state_nottracked.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ void test(pthread_mutex_t *mtx) {
1010
int ret = pthread_mutex_destroy(mtx);
1111
clang_analyzer_printState();
1212
// CHECK: { "checker": "alpha.core.PthreadLockBase", "messages": [
13+
// CHECK-NEXT: "Mutex event:",
14+
// CHECK-NEXT: "Kind: : Destroy",
15+
// CHECK-NEXT: "Semantics: PthreadSemantics",
16+
// CHECK-NEXT: "Library: Pthread",
17+
// CHECK-NEXT: "Mutex region: SymRegion{reg_$[[REG:[0-9]+]]<pthread_mutex_t * mtx>}",
1318
// CHECK-NEXT: "Mutex states:",
1419
// CHECK-NEXT: "SymRegion{reg_$[[REG:[0-9]+]]<pthread_mutex_t * mtx>}: not tracked, possibly destroyed",
1520
// CHECK-NEXT: "Mutexes in unresolved possibly destroyed state:",

clang/test/Analysis/std-c-library-functions-arg-enabled-checkers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// CHECK-NEXT: core.CallAndMessage
2525
// CHECK-NEXT: core.DivideZero
2626
// CHECK-NEXT: core.DynamicTypePropagation
27+
// CHECK-NEXT: core.MutexModeling
2728
// CHECK-NEXT: core.NonNullParamChecker
2829
// CHECK-NEXT: core.NonnilStringConstants
2930
// CHECK-NEXT: core.NullDereference

0 commit comments

Comments
 (0)