[DRAFT] [starvation] treat pthread mutexes as non-recursive - #2210
Draft
VladimirMakaev wants to merge 2 commits into
Draft
VladimirMakaev wants to merge 2 commits into
VladimirMakaev wants to merge 2 commits into
Conversation
VladimirMakaev
force-pushed
the
starvation-nonrecursive-locks
branch
2 times, most recently
from
October 5, 2026 02:41
ea479a0 to
29dc230
Compare
Starvation only knows a fixed list of scoped guards. With any other (eg android::Mutex::Autolock),
the lock taken by the guard's constructor was read as a caller lock, so all guards collapsed into
one lock: inversions were missed and self deadlocks reported.
```cpp
#include <mutex>
struct Guard {
explicit Guard(std::mutex& m) : m_(m) { m_.lock(); }
~Guard() { m_.unlock(); }
std::mutex& m_;
};
class C {
std::mutex mu1_, mu2_;
public:
void thread1() { Guard a(mu1_); Guard b(mu2_); } // false positive: self deadlock
void thread2() { Guard b(mu2_); Guard a(mu1_); } // same; Infer missed: deadlock with thread1
};
```
Callee locks are now substituted for every argument and call depth. A local object whose
constructor (or by-value return, if its destructor releases a lock) leaves one lock held becomes a
guard of that lock. A lock a callee released before an event is not held by its callers there (eg
`mu.unlock(); sleep(); mu.lock();`). android::Mutex and Autolock are modelled, tryLock and
timedLock as trylocks returning zero on success; RacerD now reports races in classes using them.
Limitations: callee locks and unlocks the caller cannot express are dropped, missing deadlocks
through objects returned by calls (eg Java singletons) and keeping locks released through a local
guard struct.
## Test plan
New tests in c/starvation/lock_wrappers.c, cpp/starvation (android_mutex.cpp, custom_guards.cpp,
release_in_callee.cpp, substitution.cpp), cpp/racerd/android_mutex.cpp and
java/starvation/Parameters.java, with FP_/FN_ tests for the limitations. The codetoanalyze tests
pass.
Starvation ignores re-acquisitions of recursive locks, but had no model for `pthread_mutex_t` and
looked up only C++ classes, so every pthread mutex counted as recursive and self deadlocks on
them went unreported.
```c
struct Device { pthread_mutex_t mutex; };
void reset(struct Device* d) {
pthread_mutex_lock(&d->mutex);
pthread_mutex_unlock(&d->mutex);
}
void close_device(struct Device* d) {
pthread_mutex_lock(&d->mutex);
reset(d); // Infer missed: self deadlock
pthread_mutex_unlock(&d->mutex);
}
```
Lock models now cover C structs and unions, `pthread_mutex_t` is non-recursive, boost's recursive
mutexes are modelled as recursive, and `"recursive"` in `--lock-model` overrides the built-in
setting. Global locks are matched through their extra pointer layer; file-static globals are
distinct per translation unit. Array elements (eg lock stripes) count as recursive, as indices are
not tracked, and recursive relocks that appear only once a callee's locks are substituted are
ignored too.
Limitations:
- recursive pthread mutexes are not recognised; a wrapper class needs a `--lock-model` listing its
methods with `"recursive": true`.
- relocking the same array element is missed.
## Test plan
New tests in c/starvation (self_deadlock.c, static_lock_{1,2}.c) and cpp/starvation (pthread.cpp,
recursive.cpp, custom_guards.cpp), with FP_/FN_ tests for the limitations. The codetoanalyze
tests pass.
VladimirMakaev
force-pushed
the
starvation-nonrecursive-locks
branch
from
October 5, 2026 06:55
29dc230 to
7315de1
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack: #2182 → #2210 (review only the top commit; lands after #2182)
Starvation ignores re-acquisitions of recursive locks, but had no model for
pthread_mutex_tandlooked up only C++ classes, so every pthread mutex counted as recursive and self deadlocks on
them went unreported.
Lock models now cover C structs and unions,
pthread_mutex_tis non-recursive, boost's recursivemutexes are modelled as recursive, and
"recursive"in--lock-modeloverrides the built-insetting. Global locks are matched through their extra pointer layer; file-static globals are
distinct per translation unit. Array elements (eg lock stripes) count as recursive, as indices are
not tracked, and recursive relocks that appear only once a callee's locks are substituted are
ignored too.
Limitations:
--lock-modellisting itsmethods with
"recursive": true.Test plan
New tests in c/starvation (self_deadlock.c, static_lock_{1,2}.c) and cpp/starvation (pthread.cpp,
recursive.cpp, custom_guards.cpp), with FP_/FN_ tests for the limitations. The codetoanalyze
tests pass.