Skip to content

Fix for PreventDeadlockByLockingInPredefinedOrder #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
17 changes: 16 additions & 1 deletion cpp/common/src/codingstandards/cpp/Concurrency.qll
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ class CPPMutexFunctionCall extends MutexFunctionCall {
/**
* Holds if this `CPPMutexFunctionCall` is a lock.
*/
override predicate isLock() { getTarget().getName() = "lock" }
override predicate isLock() {
not isLockingOperationWithinLockingOperation(this) and
getTarget().getName() = "lock"
}

/**
* Holds if this `CPPMutexFunctionCall` is a speculative lock, defined as calling
Expand Down Expand Up @@ -172,6 +175,7 @@ class CMutexFunctionCall extends MutexFunctionCall {
* Holds if this `CMutexFunctionCall` is a lock.
*/
override predicate isLock() {
not isLockingOperationWithinLockingOperation(this) and
getTarget().getName() = ["mtx_lock", "mtx_timedlock", "mtx_trylock"]
}

Expand Down Expand Up @@ -296,6 +300,16 @@ abstract class LockingOperation extends FunctionCall {
* Holds if this is an unlock operation
*/
abstract predicate isUnlock();

/**
* Holds if this locking operation is really a locking operation within a
* designated locking operation. This library assumes the underlying locking
* operations are implemented correctly in that calling a `LockingOperation`
* results in the creation of a singular lock.
*/
predicate isLockingOperationWithinLockingOperation(LockingOperation inner) {
exists(LockingOperation outer | outer.getTarget() = inner.getEnclosingFunction())
}
}

/**
Expand All @@ -317,6 +331,7 @@ class RAIIStyleLock extends LockingOperation {
* Holds if this is a lock operation
*/
override predicate isLock() {
not isLockingOperationWithinLockingOperation(this) and
this instanceof ConstructorCall and
lock = getArgument(0).getAChild*() and
// defer_locks don't cause a lock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ predicate getAnOrderedLockPair(
lock1 = node.coveredByLock() and
lock2 = node.coveredByLock() and
not lock1 = lock2 and
lock1.getEnclosingFunction() = lock2.getEnclosingFunction() and
exists(Function f |
lock1.getEnclosingFunction() = f and
lock2.getEnclosingFunction() = f and
node.getBasicBlock().getEnclosingFunction() = f
) and
exists(Location l1Loc, Location l2Loc |
l1Loc = lock1.getLocation() and
l2Loc = lock2.getLocation()
Expand Down