Skip to content

[DRAFT] [starvation] treat pthread mutexes as non-recursive - #2210

Draft
VladimirMakaev wants to merge 2 commits into
facebook:mainfrom
VladimirMakaev:starvation-nonrecursive-locks
Draft

VladimirMakaev wants to merge 2 commits into
facebook:mainfrom
VladimirMakaev:starvation-nonrecursive-locks

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

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_t and
looked up only C++ classes, so every pthread mutex counted as recursive and self deadlocks on
them went unreported.

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.

@meta-cla meta-cla Bot added the CLA Signed label Oct 3, 2026
@VladimirMakaev
VladimirMakaev force-pushed the starvation-nonrecursive-locks branch 2 times, most recently from ea479a0 to 29dc230 Compare October 5, 2026 02:41
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
VladimirMakaev force-pushed the starvation-nonrecursive-locks branch from 29dc230 to 7315de1 Compare October 5, 2026 06:55

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant