-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathMutexObjectsNotAlwaysUnlocked.ql
More file actions
52 lines (47 loc) · 1.74 KB
/
MutexObjectsNotAlwaysUnlocked.ql
File metadata and controls
52 lines (47 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @id c/misra/mutex-objects-not-always-unlocked
* @name RULE-22-16: All mutex objects locked by a thread shall be explicitly unlocked by the same thread
* @description Mutex not unlocked by thread on all execution paths in current thread after being
* locked.
* @kind problem
* @precision high
* @problem.severity error
* @tags external/misra/id/rule-22-16
* correctness
* concurrency
* external/misra/c/2012/amendment4
* external/misra/obligation/required
*/
import cpp
import codingstandards.c.misra
import codingstandards.cpp.Concurrency
import codingstandards.cpp.resources.ResourceLeakAnalysis
module MutexLeakConfig implements ResourceLeakConfigSig {
predicate isAllocate(ControlFlowNode allocPoint, DataFlow::Node node) {
exists(MutexFunctionCall lock |
allocPoint = lock and
lock.isLock() and
node.asDefiningArgument() = lock.getLockExpr()
)
}
predicate isFree(ControlFlowNode node, DataFlow::Node resource) {
exists(MutexFunctionCall mfc |
node = mfc and
mfc.isUnlock() and
mfc.getLockExpr() = resource.asExpr()
)
}
}
string describeMutex(Expr mutexExpr) {
if mutexExpr instanceof AddressOfExpr
then result = mutexExpr.(AddressOfExpr).getOperand().toString()
else result = mutexExpr.toString()
}
from MutexFunctionCall lockCall, string mutexDescription
where
not isExcluded(lockCall, Concurrency8Package::mutexObjectsNotAlwaysUnlockedQuery()) and
//lockCall.getLockExpr() = mutexNode.asDefiningArgument() and
exists(ResourceLeak<MutexLeakConfig>::getALeak(lockCall)) and
mutexDescription = describeMutex(lockCall.getLockExpr())
select lockCall,
"Mutex '" + mutexDescription + "' is locked here and may not always be subsequently unlocked."