Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ ANALYZER_OPTION(bool, MayInlineCXXStandardLibrary, "c++-stdlib-inlining",
"considered for inlining.",
true)

ANALYZER_OPTION(bool, ModelPthreads, "model-pthreads",
"Model Pthreads if enabled - default is disabled ",
false
)

ANALYZER_OPTION(bool, MayInlineCXXAllocator, "c++-allocator-inlining",
"Whether or not allocator and deallocator calls may be "
"considered for inlining.",
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,7 @@ static bool isTrivialObjectAssignment(const CallEvent &Call) {
void ExprEngine::defaultEvalCall(NodeBuilder &Bldr, ExplodedNode *Pred,
const CallEvent &CallTemplate,
const EvalCallOptions &CallOpts) {
AnalyzerOptions &Opts = AMgr.getAnalyzerOptions();
// Make sure we have the most recent state attached to the call.
ProgramStateRef State = Pred->getState();
CallEventRef<> Call = CallTemplate.cloneWithState(State);
Expand All @@ -1322,7 +1323,7 @@ void ExprEngine::defaultEvalCall(NodeBuilder &Bldr, ExplodedNode *Pred,

// TODO: make this a proper mode
// Special case thread creation
if (isThread(*Call)) {
if (isThread(*Call) && Opts.ModelPthreads) {
llvm::errs() << "Hijacking pthread_create(3)\n";
threadBifurcate(*Call, D, Bldr, Pred, State);
return;
Expand Down
37 changes: 37 additions & 0 deletions clang/test/Analysis/SD-tests/taint-thread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core,optin.taint.GenericTaint -DPTHREAD_MODEL=1 \
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core,optin.taint.GenericTaint -DNO_PTHREAD_MODEL=1 \
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=false

#define NULL ((void*) 0)
typedef unsigned long int pthread_t;
typedef struct __pthread_attr pthread_attr_t;
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

char *strcat( char *dest, const char *src );
int scanf(const char*, ...);
int system(const char *command);

void *thread_func(void *arg) {
#ifdef PTHREAD_MODEL
system( (char *) arg); // expected-warning {{Untrusted data is passed to a system call (CERT/STR02-C. Sanitize data passed to complex subsystems)}}
#endif
#ifdef NO_PTHREAD_MODEL
system( (char *) arg); // expected-no-diagnostics
#endif
return NULL;
}

// Command Injection Vulnerability Example
void test(void) {
char cmd[2048] = "/bin/cat ";
char filename[1024];
scanf (" %1023[^\n]", filename); // The attacker can inject a shell escape here
strcat(cmd, filename);
pthread_t p1;
pthread_create(&p1, NULL, &thread_func, &cmd);
}

2 changes: 1 addition & 1 deletion clang/test/Analysis/SD-tests/thread-modeling-inline.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=debug.ExprInspection
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

#define NULL ((void*) 0)
enum bool {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/SD-tests/thread-modeling-inline2.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=debug.ExprInspection
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

#define NULL ((void*) 0)
enum bool {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/SD-tests/thread-modeling-leak.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=unix \
// RUN: -analyzer-checker=debug.ExprInspection
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

#define NULL ((void*) 0)

Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/SD-tests/thread-modeling-leak2.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=unix \
// RUN: -analyzer-checker=debug.ExprInspection
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

#define NULL ((void*) 0)

Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/SD-tests/thread-modeling-null-deref.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=debug.ExprInspection
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

#define NULL ((void*) 0)
enum bool {
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/SD-tests/thread-modeling-null-deref2.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=debug.ExprInspection
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

#define NULL ((void*) 0)
enum bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=unix \
// RUN: -analyzer-checker=debug.ExprInspection
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true

#define NULL ((void*) 0)

Expand Down
42 changes: 42 additions & 0 deletions clang/test/Analysis/SD-tests/thread-modeling-ptr-deref.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core -DPTHREAD_MODEL=1 \
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=true
//
// RUN: %clang_analyze_cc1 -Wno-strict-prototypes -Wno-error=implicit-int -verify %s \
// RUN: -analyzer-checker=core -DNO_PTHREAD_MODEL=1 \
// RUN: -analyzer-checker=debug.ExprInspection -analyzer-config model-pthreads=false

#define NULL ((void*) 0)

typedef unsigned long int pthread_t;
typedef struct __pthread_attr pthread_attr_t;

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

void clang_analyzer_checkInlined(int);
void clang_analyzer_dump(int);

void* thread_function(void* arg)
{
// should expect to fail the test at this line if you set the checkInlined to true
#ifdef PTHREAD_MODEL
clang_analyzer_checkInlined(1); // expected-warning{{TRUE}}
#endif
int *ptr = (int *)arg;
#ifdef PTHREAD_MODEL
clang_analyzer_dump(*ptr); // expected-warning{{1 S32b}}
#endif
#ifdef NO_PTHREAD_MODEL
clang_analyzer_dump(*ptr); // expected-warning-re{{reg_${{[[:digit:]]+}}<int Element{SymRegion{reg_${{[[:digit:]]+}}<void * arg>},0 S64b,int}}}
#endif
return NULL;
}

int test()
{
int i = 1;
pthread_t p1;
pthread_create(&p1, NULL, &thread_function, &i);
return 0;
}

2 changes: 2 additions & 0 deletions clang/test/Analysis/analyzer-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
// CHECK-NEXT: min-cfg-size-treat-functions-as-large = 14
// CHECK-NEXT: mode = deep
// CHECK-NEXT: model-path = ""
// CHECK-NEXT: model-pthreads = false
// CHECK-NEXT: notes-as-events = false
// CHECK-NEXT: nullability:NoDiagnoseCallsToSystemHeaders = false
// CHECK-NEXT: objc-inlining = true
Expand Down Expand Up @@ -126,6 +127,7 @@
// CHECK-NEXT: suppress-c++-stdlib = true
// CHECK-NEXT: suppress-inlined-defensive-checks = true
// CHECK-NEXT: suppress-null-return-paths = true
// CHECK-NEXT: thread-aware = true
// CHECK-NEXT: track-conditions = true
// CHECK-NEXT: track-conditions-debug = false
// CHECK-NEXT: unix.DynamicMemoryModeling:AddNoOwnershipChangeNotes = true
Expand Down
1 change: 1 addition & 0 deletions clang/test/Analysis/analyzer-enabled-checkers.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// CHECK: OVERVIEW: Clang Static Analyzer Enabled Checkers List
// CHECK-EMPTY:
// CHECK-NEXT: apiModeling.Errno
// CHECK-NEXT: apiModeling.Thread
// CHECK-NEXT: apiModeling.TrustNonnull
// CHECK-NEXT: apiModeling.TrustReturnsNonnull
// CHECK-NEXT: apiModeling.llvm.CastValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// CHECK: OVERVIEW: Clang Static Analyzer Enabled Checkers List
// CHECK-EMPTY:
// CHECK-NEXT: apiModeling.Errno
// CHECK-NEXT: apiModeling.Thread
// CHECK-NEXT: apiModeling.TrustNonnull
// CHECK-NEXT: apiModeling.TrustReturnsNonnull
// CHECK-NEXT: apiModeling.llvm.CastValue
Expand Down