Skip to content

[DRAFT] [starvation] report deadlocks on global and C struct locks in per-file mode - #2189

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

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

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Stack: #2182 → #2189 (review only the top commit; lands after #2182)

Per-file mode searches for the other side of a lock-order inversion only among the methods of the
lock's root class, missing inversions on global, static and C struct locks. Deduplication also
dropped both sides of a deadlock locked at the same line of a helper, and global std::mutex locks
counted as recursive.

std::mutex m1, m2;
void take(std::mutex& m) { std::lock_guard<std::mutex> l(m); }
void f() {
  std::lock_guard<std::mutex> l(m1);
  take(m2);
}
void g() {
  std::lock_guard<std::mutex> l(m2);
  take(m1); // Infer missed: deadlock with f()
}

Starvation now also searches the current file's C/C++/ObjC procedures, indexed by held lock. Under
deduplication, a side reports a deadlock the other side cannot find, and location ties are broken
by the locks, also fixing Java and whole-program mode.

Limitations: cross-file inversions on global and C struct locks still need
--starvation-whole-program; global initializers are assumed to run before threads start, so only
their self deadlocks are reported.

Test plan

New c/starvation-per-file and cpp/starvation-dedup test directories, and new tests in
cpp/starvation/globals.cpp (including initializers) and java/starvation-dedup/Interproc.java.
The codetoanalyze tests pass.

@meta-cla meta-cla Bot added the CLA Signed label Oct 2, 2026
@VladimirMakaev
VladimirMakaev force-pushed the starvation-global-locks branch from 8fe7b2b to cf2f66b Compare October 2, 2026 23:34
Starvation only knows a fixed list of scoped guards. With any other, eg android::Mutex::Autolock,
the lock taken in the guard's constructor was read as a lock of the caller, 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 at every call depth, and dropped when the
caller cannot express them. 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. android::Mutex and
Autolock are modelled, tryLock and timedLock as trylocks returning zero on success; RacerD now
also reports races in classes using them.

Trade-off: 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,
substitution.cpp), cpp/racerd/android_mutex.cpp and java/starvation/Parameters.java, with FP_/FN_
tests for the limitations. The codetoanalyze tests pass.
…e mode

Per-file mode searches for the other side of a lock-order inversion only among the methods of the
lock's root class, missing inversions on global, static and C struct locks. Deduplication also
dropped both sides of a deadlock locked at the same line of a helper, and global `std::mutex` locks
counted as recursive.

```cpp
std::mutex m1, m2;
void take(std::mutex& m) { std::lock_guard<std::mutex> l(m); }
void f() {
  std::lock_guard<std::mutex> l(m1);
  take(m2);
}
void g() {
  std::lock_guard<std::mutex> l(m2);
  take(m1); // Infer missed: deadlock with f()
}
```

Starvation now also searches the current file's C/C++/ObjC procedures, indexed by held lock. Under
deduplication, a side reports a deadlock the other side cannot find, and location ties are broken
by the locks, also fixing Java and whole-program mode.

Limitations: cross-file inversions on global and C struct locks still need
`--starvation-whole-program`; global initializers are assumed to run before threads start, so only
their self deadlocks are reported.

## Test plan

New `c/starvation-per-file` and `cpp/starvation-dedup` test directories, and new tests in
`cpp/starvation/globals.cpp` (including initializers) and `java/starvation-dedup/Interproc.java`.
The codetoanalyze tests pass.
@VladimirMakaev
VladimirMakaev force-pushed the starvation-global-locks branch from cf2f66b to 76c4dac Compare October 4, 2026 00: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