-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathmanaged_ptr.qll
More file actions
281 lines (233 loc) · 7.3 KB
/
managed_ptr.qll
File metadata and controls
281 lines (233 loc) · 7.3 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import cpp
import semmle.code.cpp.dataflow.DataFlow
import collections
/**
* General library code to deal with managed pointer types.
*/
//------------ dataflow edges
/**
* `x -> x.get();`
* `x` managed pointer
*/
predicate getEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget() instanceof PointerGet and
node1.asExpr() = fc.getQualifier() and node2.asExpr() = fc
)
}
/**
* `x -> std::move(x);`
* `x` managed pointer.
*/
predicate stdMoveEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget() instanceof StdMove and
fc.getAnArgument() = node1.asExpr() and
fc = node2.asExpr()
)
}
/**
* `x -> x.release
*/
predicate stdReleaseEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget() instanceof StdRelease and
fc.getQualifier() = node1.asExpr() and
fc = node2.asExpr()
)
}
/**
* `x -> std::forward(x);`
*/
predicate stdForwardEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget() instanceof StdForward and
fc.getAnArgument() = node1.asExpr() and
fc = node2.asExpr()
)
}
predicate wrapUniqueEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getName().matches("WrapUnique%") and
fc = node2.asExpr() and fc.getArgument(0) = node1.asExpr()
)
}
predicate makeUniqueEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getName().matches("make_unique%") and
fc = node2.asExpr() and fc.getArgument(0) = node1.asExpr()
)
}
predicate makeRefEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getName().matches("MakeRefCounted%") and
fc = node2.asExpr() and fc.getArgument(0) = node1.asExpr()
)
}
predicate wrapRefCountedEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().getName() = "WrapRefCounted" and
fc = node2.asExpr() and fc.getArgument(0) = node1.asExpr()
)
}
predicate pointerWrapperEdge(DataFlow::Node node1, DataFlow::Node node2) {
makeRefEdge(node1, node2) or
makeUniqueEdge(node1, node2) or
wrapUniqueEdge(node1, node2) or
wrapRefCountedEdge(node1, node2)
}
predicate pointerTransferEdge(DataFlow::Node node1, DataFlow::Node node2) {
stdMoveEdge(node1, node2) or
stdForwardEdge(node1, node2) or
stdReleaseEdge(node1, node2)
}
//-------------dataflow ends
/**
* General managed pointer type. Only includes `scoped_refptr` and `unique_ptr`
* at the moment. (`WeakPtr` not too interesting for memory corruption problem)
* Includes normal type or container types.
*/
abstract class GeneralManagedType extends Class {
/**
* Gets the underlying managed type.
*/
abstract Type getManagedType();
/**
* Holds if the type is owned completely, i.e. `unique_ptr`.
*/
abstract predicate isOwner();
}
/**
* General managed pointer field. Excludes `WeakPtr` as it is less interesting.
* Includes normal field or collection field.
*/
abstract class GeneralManagedField extends Field {
/**
* Gets the underlying type that is being managed.
*/
abstract Type getManagedType();
/**
* Gets an expression that resets the pointer, which can free the underlying object.
*/
abstract Expr getAManagedReset();
/**
* Holds if the field is owned completely, i.e. `unique_ptr`.
*/
abstract predicate isOwner();
}
/** Function that transfers ownership of a managed pointer.*/
abstract class PointerTransferFunction extends Function {
}
class StdMove extends PointerTransferFunction {
StdMove() {
this.getQualifiedName() = "std::__Cr::move"
}
}
class StdForward extends PointerTransferFunction {
StdForward() {
this.getQualifiedName() = "std::__Cr::forward"
}
}
class StdRelease extends PointerTransferFunction {
StdRelease() {
this.getName() = "release" and
this.getDeclaringType() instanceof ManagedPtr
}
}
/** The `get` function from a managed pointer that returns the underlying raw pointer.*/
class PointerGet extends Function {
PointerGet() {
this.hasName("get") and
this.getDeclaringType().stripType().getName().matches("%_ptr%")
}
}
/** Normal managed pointers.*/
class ManagedPtr extends GeneralManagedType {
ManagedPtr() {
this.getName().matches("unique_ptr%") or
this.getName().matches("scoped_refptr%")
}
override Type getManagedType() {result = this.getTemplateArgument(0)}
override predicate isOwner() {getName().matches("unique_ptr%")}
}
/** Normal managed pointer fields.*/
class ManagedPtrField extends GeneralManagedField {
ManagedPtrField() {
this.getType().stripType() instanceof ManagedPtr
}
override Type getManagedType() {result = getType().stripType().(ManagedPtr).getManagedType()}
override Expr getAManagedReset() {
result.(FunctionCall).getTarget() instanceof ManagedPtrSetFunction and
getQualifier(result) = this.getAnAccess()
}
override predicate isOwner() {
this.getType().stripType().(ManagedPtr).isOwner()
}
}
/** A function that resets a managed pointer.*/
class ManagedPtrSetFunction extends Function {
ManagedPtrSetFunction() {
hasName("operator=") or
hasName("reset")
}
}
/**
* A managed pointer that is inside a collection.
*/
abstract class ManagedCollectionType extends GeneralManagedType {
ManagedCollectionType() {
this.stripType().getName().matches("%unique_ptr%") or
this.stripType().getName().matches("%scoped_refptr%")
}
override predicate isOwner() {
this.stripType().getName().matches("%unique_ptr%")
}
}
/**
* A managed pointer inside a map.
*/
class ManagedMapType extends ManagedCollectionType, MapType {
ManagedMapType() {
getComponentType() instanceof ManagedPtr
}
override Type getManagedType() {
exists(Type t |
t = getComponentType() and
result = t.(ManagedPtr).getManagedType()
)
}
}
/** A managed pointer inside a list type. */
class ManagedListType extends ManagedCollectionType, ListType {
ManagedListType() {
getComponentType() instanceof ManagedPtr
}
override Type getManagedType() {
exists(Type t |
t = getComponentType() and
result = t.(ManagedPtr).getManagedType()
)
}
}
/**
* A field that stores managed pointers in a map.
*/
class ManagedMapField extends MapField, GeneralManagedField {
ManagedMapField() {
// binding set is not likely to have problem.
not (getName() = "bindings_" and getDeclaringType().getName().matches("BindingSet%")) and
getType() instanceof ManagedMapType
}
override Type getManagedType() {
result = getType().(ManagedMapType).getManagedType()
}
override Expr getAManagedReset() {result = getAReset()}
override predicate isOwner() {getType().(ManagedMapType).isOwner()}
}
/**
* A field that stores managed pointers in a list/vector type.
*/
class ManagedListField extends ListField, GeneralManagedField {
ManagedListField() {
// binding set is not likely to have problem.
not (getName() = "bindings_" and getDeclaringType().getName().matches("BindingSet%")) and
getType() instanceof ManagedListType
}
override Type getManagedType() {
result = getType().(ManagedListType).getManagedType()
}
override Expr getAManagedReset() {result = getAReset()}
override predicate isOwner() {getType().(ManagedListType).isOwner()}
}