llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.32k stars 11.69k forks source link

pthread_mutex_lock() modeling error #8767

Open llvmbot opened 13 years ago

llvmbot commented 13 years ago
Bugzilla Link 8395
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @belkadan

Extended Description

$ clang -v clang version 2.9 (trunk 116687) Target: x86_64-unknown-linux-gnu Thread model: posix s$ cat pthread_mutex_lock.c

include

static pthread_mutex_t m;

void foo() { int err = 0; err = pthread_mutex_lock(&m); if(err) return; pthread_mutex_unlock(&m); } $ clang -cc1 pthread_mutex_lock.c -analyze -analyzer-check-objc-mem -analyzer-experimental-checks pthread_mutex_lock.c:10:2: warning: This statement is never executed return; ^~ 1 warning generated.

belkadan commented 11 years ago

This passed because we turned off the analyzer's dead code checker for now. It still fails with

% clang -cc1 -analyze -analyzer-checker=alpha.unix.PthreadLock,alpha.deadcode pthread_mutex_lock.c

Arguably there's a policy decision here: many people do not expect pthread locks to fail. We probably need to do something similar to realloc -- most people just assume it succeeds, but if someone was defensive enough to test it then we can offer leak warnings on the original buffer.

llvmbot commented 11 years ago

$ clang --version clang version 3.2 (trunk 165709) Target: x86_64-unknown-linux-gnu Thread model: posix

PASS for me. Thanks!

llvmbot commented 13 years ago

Both functions -- pthread_mutex_lock() and pthread_mutex_trylock() can:

Then we can process them equally. And the following naive patch will solve PR

$ clang --version clang version 2.9 (trunk 120936) Target: x86_64-unknown-linux-gnu Thread model: posix

Checker$ svn diff Index: PthreadLockChecker.cpp

--- PthreadLockChecker.cpp (revision 120936) +++ PthreadLockChecker.cpp (working copy) @@ -32,8 +32,7 @@ } void PostVisitCallExpr(CheckerContext &C, const CallExpr *CE);

llvmbot commented 13 years ago

assigned to @tkremenek