Skip to content

Commit da72823

Browse files
committed
Add more tests
1 parent 6daf88a commit da72823

File tree

6 files changed

+218
-12
lines changed

6 files changed

+218
-12
lines changed

clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,18 +583,22 @@ void ExprEngine::threadBifurcate(CallEvent const &Call, Decl const *D,
583583
StartRoutine->getType(),
584584
VK_LValue);
585585

586+
auto *null_expr = new (SRR->getContext()) CXXNullPtrLiteralExpr(QualType(), SourceLocation());
587+
586588
CallExpr *srcall = CallExpr::Create(
587589
SRR->getContext(),
588590
srexpr,
589-
{const_cast<Expr*>(SRInit)},
591+
{const_cast<Expr*>(SRInit) /*null_expr*/},
590592
StartRoutine->getType(),
591593
VK_LValue,
592594
SourceLocation(),
593595
FPOptionsOverride());
594596

597+
// TODO: bind the arguments?
595598
auto call = CEMgr.getSimpleCall(srcall, State, LC, getCFGElementRef());
596599

597600
inlineCall(Engine.getWorkList(), *call, StartRoutine, Bldr, Pred, State);
601+
conservativeEvalCall(*call, Bldr, Pred, State);
598602
}
599603
#pragma clang optimize on
600604

clang/test/Analysis/SD-tests/thread-modeling-2.c renamed to clang/test/Analysis/SD-tests/thread-modeling-inline.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,18 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_
1616

1717
int pthread_join(pthread_t thread, void **retval);
1818

19-
void clang_analyzer_checkInlined(enum bool);
19+
void clang_analyzer_checkInlined(int);
2020

2121
void* thread_function(void* arg){
2222
// should expect to fail the test at this line if you set the checkInlined to true
2323
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
2424
return NULL;
2525
}
2626

27-
int foo_no_pthread(void)
27+
int ok()
2828
{
29-
// should expect to pass at this line
30-
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
31-
return 0;
32-
}
33-
34-
int main(){
3529
pthread_t p1;
3630
pthread_create(&p1, NULL, &thread_function, NULL);
37-
pthread_join(p1, NULL);
38-
foo_no_pthread();
39-
return 1;
31+
return 0;
4032
}
4133

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=debug.ExprInspection
3+
4+
#define NULL ((void*) 0)
5+
enum bool {
6+
false,
7+
true
8+
};
9+
10+
11+
typedef unsigned long int pthread_t;
12+
typedef struct __pthread_attr pthread_attr_t;
13+
14+
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
15+
16+
17+
int pthread_join(pthread_t thread, void **retval);
18+
19+
void clang_analyzer_checkInlined(int);
20+
21+
void* thread_function1(void* arg){
22+
// should expect to fail the test at this line if you set the checkInlined to true
23+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
24+
return NULL;
25+
}
26+
27+
void* thread_function2(void* arg){
28+
// should expect to fail the test at this line if you set the checkInlined to true
29+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
30+
return NULL;
31+
}
32+
33+
int ok()
34+
{
35+
pthread_t p1, p2;
36+
pthread_create(&p1, NULL, &thread_function1, NULL);
37+
pthread_create(&p2, NULL, &thread_function2, NULL);
38+
return 0;
39+
}
40+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=core \
3+
// RUN: -analyzer-checker=unix \
4+
// RUN: -analyzer-checker=debug.ExprInspection
5+
6+
#define NULL ((void*) 0)
7+
8+
enum bool {
9+
false,
10+
true
11+
};
12+
13+
typedef struct __pthread_attr pthread_attr_t;
14+
15+
typedef __typeof(sizeof(int)) size_t;
16+
17+
void free (void* ptr);
18+
19+
void *malloc(size_t sz);
20+
21+
void clang_analyzer_checkInlined(int);
22+
void clang_analyzer_warnIfReached();
23+
void clang_analyzer_printState();
24+
25+
typedef unsigned long int pthread_t;
26+
27+
28+
int pthread_create(pthread_t *restrict thread,
29+
const pthread_attr_t *restrict attr,
30+
void *(*start_routine)(void*), void *restrict arg);
31+
32+
33+
int pthread_join(pthread_t thread, void **retval);
34+
35+
36+
void *worker(void *);
37+
int foo(void);
38+
39+
void *worker(void *data)
40+
41+
{
42+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
43+
// clang_analyzer_printState();
44+
45+
int *pdata = (int *)data;
46+
// switch (*pdata) {
47+
// case 0:
48+
// case 2:
49+
// free(data);
50+
// break;
51+
// default:
52+
// break;
53+
// }
54+
55+
return NULL;
56+
}
57+
58+
59+
60+
int foo(void)
61+
{
62+
pthread_t th1, th2;
63+
64+
int *pdata1 = (int *)malloc(sizeof(int));
65+
// int *pdata2 = (int *)malloc(sizeof(int));
66+
67+
68+
*pdata1 = 0;
69+
// *pdata2 = 1;
70+
71+
// clang_analyzer_printState();
72+
pthread_create(&th1, NULL, worker, pdata1);
73+
// pthread_create(&th2, NULL, worker, pdata2);
74+
75+
//
76+
// pthread_join(th1, NULL);
77+
// pthread_join(th2, NULL);
78+
79+
return 0; // expected-warning {{Potential leak of memory pointed to by 'pdata1'}}
80+
}
81+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=core \
3+
// RUN: -analyzer-checker=debug.ExprInspection
4+
5+
#define NULL ((void*) 0)
6+
enum bool {
7+
false,
8+
true
9+
};
10+
11+
12+
typedef unsigned long int pthread_t;
13+
typedef struct __pthread_attr pthread_attr_t;
14+
15+
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
16+
17+
int pthread_join(pthread_t thread, void **retval);
18+
19+
void clang_analyzer_checkInlined(int);
20+
21+
void* thread_function(void* arg)
22+
{
23+
// should expect to fail the test at this line if you set the checkInlined to true
24+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
25+
int i = *(int*)arg; // expected-warning{{Dereference of null pointer}}
26+
return NULL;
27+
}
28+
//
29+
//int fine()
30+
//{
31+
// int i = 1;
32+
// pthread_t p1;
33+
// pthread_create(&p1, NULL, &thread_function, &i);
34+
//// pthread_join(p1, NULL);
35+
// return 0;
36+
//}
37+
38+
int bad()
39+
{
40+
int i = 1;
41+
pthread_t p1;
42+
pthread_create(&p1, NULL, &thread_function, NULL);
43+
// pthread_join(p1, NULL);
44+
return 0;
45+
}
46+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
2+
// RUN: -analyzer-checker=core \
3+
// RUN: -analyzer-checker=debug.ExprInspection
4+
5+
#define NULL ((void*) 0)
6+
enum bool {
7+
false,
8+
true
9+
};
10+
11+
12+
typedef unsigned long int pthread_t;
13+
typedef struct __pthread_attr pthread_attr_t;
14+
15+
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
16+
17+
18+
int pthread_join(pthread_t thread, void **retval);
19+
20+
void clang_analyzer_checkInlined(int);
21+
22+
void* thread_function1(void* arg){
23+
// should expect to fail the test at this line if you set the checkInlined to true
24+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
25+
int i = *(int *)arg; // expected-warning{{Dereference of null pointer}}
26+
return NULL;
27+
}
28+
29+
void* thread_function2(void* arg){
30+
// should expect to fail the test at this line if you set the checkInlined to true
31+
clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
32+
int i = *(int *)arg; // expected-warning{{Dereference of null pointer}}
33+
return NULL;
34+
}
35+
36+
int ok()
37+
{
38+
pthread_t p1, p2;
39+
pthread_create(&p1, NULL, &thread_function1, NULL);
40+
pthread_create(&p2, NULL, &thread_function2, NULL);
41+
return 0;
42+
}
43+

0 commit comments

Comments
 (0)