forked from github/securitylab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.qll
292 lines (261 loc) · 7.77 KB
/
collections.qll
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
282
283
284
285
286
287
288
289
290
291
292
import cpp
import common
import semmle.code.cpp.dataflow.DataFlow
//-------- Dataflow edges ------------
/**
* Edges that goes from some simple standard containers into their elements.
*/
predicate pairEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc |
fc.getTarget().getName().matches("tuple%") or
fc.getTarget().getName().matches("pair<%") or
fc.getTarget().getName().matches("value_type%")
|
node1.asExpr() = fc.getAnArgument() and node2.asExpr() = fc
)
}
/** Goes from an element into a collection via an overloaded index operator.*/
predicate indexSetEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().hasName("operator[]") |
node2.asExpr() = getQualifier(fc) and node1.asExpr() = fc
)
}
/** Goes from a collection to its element via an overloaded index operator. */
predicate indexGetEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget().hasName("operator[]") |
node1.asExpr() = getQualifier(fc) and node2.asExpr() = fc
)
}
/**
* Edge that goes from an a method that adds to a collection into a collection.
*/
predicate addToCollectionEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget() instanceof AddToCollection and
getQualifier(fc) = node2.asExpr() and node1.asExpr() = fc
)
}
/**
* Edge that goes from a collection to a the result of a method that takes elements from it.
*/
predicate takeFromCollectionEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FunctionCall fc | fc.getTarget() instanceof TakeFromCollection and
getQualifier(fc) = node1.asExpr() and node2.asExpr() = fc
)
}
/**
* To go from an iterator to iterator->second.
*/
predicate mapIteratorEdge(DataFlow::Node node1, DataFlow::Node node2) {
exists(FieldAccess fa | fa.getTarget().hasName("second") and
node1.asExpr() = getQualifier(fa) and
node2.asExpr() = fa
)
}
/**
* Combine edge that goes from a collection to an element.
*/
predicate collectionsGetEdge(DataFlow::Node node1, DataFlow::Node node2) {
indexGetEdge(node1, node2) or
takeFromCollectionEdge(node1, node2) or
mapIteratorEdge(node1, node2) or
pairEdge(node1, node2)
}
/**
* Combine edge that goes from an element into a collection.
*/
predicate collectionsSetEdge(DataFlow::Node node1, DataFlow::Node node2) {
indexSetEdge(node1, node2) or
addToCollectionEdge(node1, node2) or
pairEdge(node1, node2)
}
/**
* Combine edge that goes either from element to collection or vice versa.
*/
predicate collectionsEdge(DataFlow::Node node1, DataFlow::Node node2) {
indexGetEdge(node1, node2) or
indexSetEdge(node1, node2) or
addToCollectionEdge(node1, node2) or
takeFromCollectionEdge(node1, node2) or
mapIteratorEdge(node1, node2) or
pairEdge(node1, node2)
}
/**
* Various methods that adds to a collection.
*/
class AddToCollection extends Function {
AddToCollection() {
hasName("push_back") or
hasName("insert") or
hasName("emplace_back") or
hasName("emplace") or
hasName("push") or
hasName("push_front")
}
}
/**
* Various methods that takes from a collection.
*/
class TakeFromCollection extends Function {
TakeFromCollection() {
hasName("find") or
getName().matches("pop_%") or
hasName("pop") or
hasName("find_if") or
hasName("front")
}
}
/**
* Loose heuristics for collection types that are like vector, list etc.
*/
class ListType extends Class {
ListType() {
exists(Function f, Type t |
f.hasName("push_back") or f.hasName("front") or f.hasName("push") or f.hasName("pop")
| t = f.getDeclaringType() and
this = t.stripType().(ClassTemplateInstantiation)
)
}
/**
* Gets the type of the component in the container.
*/
Type getComponentType() {
result = getComponentType*(this.getTemplateArgument(0)) and
not collectionTemplateType(result)
}
}
/**
* General type that represents a set.
*/
class SetType extends Class {
SetType() {
this.getName().matches("set<%") or
this.getName().matches("flat_set<%")
}
Type getComponentType() {
result = getComponentType*(this.getTemplateArgument(0)) and
not collectionTemplateType(result)
}
}
/**
* Type that represents a tuple.
*/
class TupleType extends ClassTemplateInstantiation {
TupleType() {
getName().matches("pair%") or
getName().matches("tuple%")
}
}
class ValueType extends ClassTemplateInstantiation {
ValueType() {
this.getName().matches("__value_type%")
}
}
class HashValueType extends ClassTemplateInstantiation {
HashValueType() {
this.getName().matches("__has_value_type%")
}
}
/**
* General method to get the component type of different containers.
*/
Type getComponentType(Type t) {
if collectionTemplateType(t) then
result = t.(ListType).getComponentType() or
result = t.(TupleType).getATemplateArgument() or
result = t.(ValueType).getATemplateArgument() or
result = t.(HashValueType).getATemplateArgument() or
result = t.(MapType).getComponentType()
else
result = t
}
/** Holds if `t` is a collection type.*/
predicate collectionTemplateType(Type t) {
t instanceof ListType or
t instanceof TupleType or
t instanceof ValueType or
t instanceof HashValueType or
t instanceof MapType
}
/**
* Heuristics for the map type.
*/
class MapType extends Class {
MapType() {
(
exists(Function f, Type t |
f.hasName("find") or f.hasName("insert")|
t = f.getDeclaringType() and
this = t.stripType().(ClassTemplateInstantiation)
)
)
and
not this.getName().matches("__tuple_leaf%") and
not this.getName().matches("pair%") and
not this.getName().matches("union%")
}
/** Component 0 is the key type, component 1 is the value type.*/
Type getComponentTypeAt(int i) {
(
result = getComponentType*(this.getTemplateArgument(i)) and
not collectionTemplateType(result)
)
}
Type getComponentType() {
(
result = getComponentType*(this.getTemplateArgument(0)) and
not collectionTemplateType(result)
) or
(
result = getComponentType*(this.getTemplateArgument(1)) and
not collectionTemplateType(result)
)
}
}
/** A general field that is of `Collection` type.*/
abstract class CollectionField extends Field {
/** An expression that resets an element of the collection field. */
abstract Expr getAReset();
}
class MapField extends CollectionField {
MapField() {
getType() instanceof MapType
}
override Expr getAReset() {
exists(FunctionCall fc | fc.getTarget().hasName("erase") or
fc.getTarget().getName().matches("pop%") or fc.getTarget().hasName("clear") or
fc.getTarget().hasName("insert") |
fc.getQualifier() = this.getAnAccess() and
result = fc
)
or
exists(FunctionCall fc | fc.getTarget().hasName("operator[]") and
fc.getQualifier() = this.getAnAccess() and
not exists(FunctionCall assign | assign.getTarget().hasName("operator=") and
fc = assign.getArgument(0)
) and
not exists(AssignExpr assign | assign.getRValue() = fc) and
result = fc
)
}
}
class ListField extends CollectionField {
ListField() {
getType() instanceof ListType
}
override Expr getAReset() {
exists(FunctionCall fc | fc.getTarget().hasName("erase") or
fc.getTarget().getName().matches("pop%") or fc.getTarget().hasName("clear") |
fc.getQualifier() = this.getAnAccess() and
result = fc
)
or
exists(FunctionCall fc | fc.getTarget().hasName("operator[]") and
fc.getQualifier() = this.getAnAccess() and
not exists(FunctionCall assign | assign.getTarget().hasName("operator=") and
fc = assign.getArgument(0)
) and
not exists(AssignExpr assign | assign.getRValue() = fc) and
result = fc
)
}
}