forked from github/securitylab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_ptr.qll
More file actions
133 lines (117 loc) · 3.61 KB
/
raw_ptr.qll
File metadata and controls
133 lines (117 loc) · 3.61 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
import cpp
import common
/**
* General type that contains raw pointers, e.g. raw pointer fields,
* pointers in collections etc.
*/
abstract class GeneralPointerType extends Type {
/**
* Gets the underlying raw pointer type.
*/
abstract Type getPointerType();
}
/**
* General field that are `GeneralPointerType`.
*/
abstract class GeneralPointerField extends Field {
/**
* Gets the underlying raw pointer type
*/
abstract Type getPointerType();
/**
* Gets a clean up that will reset the raw pointer.
*/
abstract Expr getACleanup();
}
/** Normal managed pointers. */
class RawPtr extends Class {
RawPtr() { this.getName().matches("raw_ptr%") }
Type getManagedType() { result = this.getTemplateArgument(0) }
}
/** Normal managed pointer fields. */
class RawPtrField extends GeneralPointerField {
RawPtrField() { this.getType().stripType() instanceof RawPtr }
override Type getPointerType() { result = getType().stripType().(RawPtr).getManagedType() }
override Expr getACleanup() {
exists(GeneralAssignment assign |
assign.getLValue() = this.getAnAccess() and
assign.getRValue() instanceof Zero and
result = assign.getLValue()
)
}
}
/**
* A normal raw pointer field.
*/
class PointerField extends GeneralPointerField {
PointerField() {
getType().getUnspecifiedType() instanceof PointerType and
//Needs to exclude some types that are less interesting (mostly generated code)
not getDeclaringType().getName().matches("ArrayDataViewImpl%") and
not getDeclaringType().getName().matches("AllocatedData") and
not getDeclaringType().getName().matches("AlignedUnion") and
not getDeclaringType().getName().matches("ArrayViewBase%") and
not getDeclaringType().getName().matches("AssociatedInterface%") and
not hasName("receiver_") and
(
//Only interested in certain area of the code. Can tweak
getFile().getAbsolutePath().matches("%/browser/%") or
getFile().getAbsolutePath().matches("%/media/%") or
getFile().getAbsolutePath().matches("%/components/%")
)
}
override Type getPointerType() { result = this.getType().stripType() }
override Expr getACleanup() {
exists(GeneralAssignment assign |
assign.getLValue() = this.getAnAccess() and
assign.getRValue() instanceof Zero and
result = assign.getLValue()
)
}
}
/**
* A field that stores raw pointers in map.
*/
class PointerMapField extends MapField, GeneralPointerField {
PointerMapField() { getType().(MapType).getComponentType() instanceof PointerType }
override Type getPointerType() {
exists(Type t |
t = getType().(MapType).getComponentType() and
t instanceof PointerType and
result = t.stripType()
)
}
override Expr getACleanup() {
exists(FunctionCall fc |
fc.getTarget().hasName("erase") or
fc.getTarget().hasName("clear") or
fc.getTarget().hasName("remove")
|
getQualifier(fc) = this.getAnAccess() and
result = fc
)
}
}
/**
* A field that stores raw pointers in list/vector.
*/
class PointerListField extends ListField, GeneralPointerField {
PointerListField() { getType().(MapType).getComponentType() instanceof PointerType }
override Type getPointerType() {
exists(Type t |
t = getType().(ListType).getComponentType() and
t instanceof PointerType and
result = t.stripType()
)
}
override Expr getACleanup() {
exists(FunctionCall fc |
fc.getTarget().hasName("erase") or
fc.getTarget().hasName("clear") or
fc.getTarget().hasName("remove")
|
getQualifier(fc) = this.getAnAccess() and
result = fc
)
}
}