-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathConcurrency.qll
More file actions
155 lines (133 loc) · 3.22 KB
/
Concurrency.qll
File metadata and controls
155 lines (133 loc) · 3.22 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Various utilities for writing concurrency queries.
import csharp
class WaitCall extends MethodCall
{
WaitCall()
{
getTarget().hasName("Wait") and
getTarget().getDeclaringType().hasQualifiedName("System.Threading.Monitor")
}
Expr getExpr()
{
result = getArgument(0)
}
}
class WaitStmt extends ExprStmt
{
WaitStmt()
{
getExpr() instanceof WaitCall
}
Expr getLock()
{
result = ((WaitCall)getExpr()).getExpr()
}
// If we are waiting on a variable
Variable getWaitVariable()
{
result.getAnAccess() = getLock()
}
// If we are waiting on 'this'
predicate isWaitThis()
{
getLock() instanceof ThisAccess
}
// If we are waiting on a typeof()
Type getWaitTypeObject()
{
result = ((TypeofExpr)getLock()).getTypeAccess().getTarget()
}
}
class SynchronizedMethodAttribute extends Attribute
{
SynchronizedMethodAttribute()
{
getType().hasQualifiedName("System.Runtime.CompilerServices.MethodImplAttribute") and
exists( MemberConstantAccess a, MemberConstant mc |
a=getArgument(0) and
a.getTarget()=mc and
mc.hasName("Synchronized") and
mc.getDeclaringType().hasQualifiedName("System.Runtime.CompilerServices.MethodImplOptions")
)
}
}
// A method with attribute [MethodImpl(MethodImplOptions.Synchronized)]
class SynchronizedMethod extends Method
{
SynchronizedMethod()
{
getAnAttribute() instanceof SynchronizedMethodAttribute
}
predicate isLockThis()
{
not isStatic()
}
Type getLockTypeObject()
{
isStatic() and result=getDeclaringType()
}
}
abstract class LockedBlock extends BlockStmt
{
abstract predicate isLockThis();
abstract Variable getLockVariable();
abstract Type getLockTypeObject();
Stmt getALockedStmt()
{
// Do this instead of getParent+, because we don't want to escape
// delegates and lambdas
result.getParent() = this
or exists( Stmt mid | mid=getALockedStmt() and result.getParent()=mid )
}
}
class LockStmtBlock extends LockedBlock
{
LockStmtBlock()
{
exists( LockStmt s | this=s.getBlock() )
}
predicate isLockThis()
{
exists( LockStmt s | this=s.getBlock() and s.isLockThis() )
}
Variable getLockVariable()
{
exists( LockStmt s | this=s.getBlock() and result=s.getLockVariable() )
}
Type getLockTypeObject()
{
exists( LockStmt s | this=s.getBlock() and result=s.getLockTypeObject() )
}
}
/**
* A call which may take a lock using one of the standard library classes.
*/
class LockingCall extends MethodCall {
LockingCall() {
this.getTarget() = any(Method m |
m.getDeclaringType().hasQualifiedName("System.Threading", "Monitor") and
m.getName().matches("%Enter%")
) or
this.getTarget().hasName("EnterReadLock") or
this.getTarget().hasName("EnterWriteLock")
}
}
class SynchronizedMethodBlock extends LockedBlock
{
SynchronizedMethodBlock()
{
exists( SynchronizedMethod m | this=m.getStatementBody() )
}
predicate isLockThis()
{
exists( SynchronizedMethod m | this=m.getStatementBody() and m.isLockThis() )
}
Variable getLockVariable()
{
none()
}
Type getLockTypeObject()
{
exists( SynchronizedMethod m | this=m.getStatementBody() and result=m.getLockTypeObject() )
}
}