-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSsa.qll
More file actions
222 lines (194 loc) · 6.74 KB
/
Ssa.qll
File metadata and controls
222 lines (194 loc) · 6.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
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
cached
module Ssa {
private import swift
private import codeql.ssa.Ssa as SsaImplCommon
private import codeql.swift.controlflow.CfgNodes
private import codeql.swift.controlflow.ControlFlowGraph
private import codeql.swift.controlflow.BasicBlocks as BasicBlocks
private module SsaInput implements SsaImplCommon::InputSig<Location, BasicBlocks::BasicBlock> {
private import internal.DataFlowPrivate
private import codeql.swift.controlflow.CfgNodes
private newtype TSourceVariable =
TNormalSourceVariable(VarDecl v) or
TKeyPathSourceVariable(EntryNode entry) { entry.getScope() instanceof KeyPathExpr }
abstract class SourceVariable extends TSourceVariable {
abstract string toString();
VarDecl asVarDecl() { none() }
EntryNode asKeyPath() { none() }
DeclRefExpr getAnAccess() { result.getDecl() = this.asVarDecl() }
Location getLocation() {
result = this.asVarDecl().getLocation()
or
result = this.asKeyPath().getLocation()
}
}
private class NormalSourceVariable extends SourceVariable, TNormalSourceVariable {
VarDecl v;
NormalSourceVariable() { this = TNormalSourceVariable(v) }
override string toString() { result = v.toString() }
override VarDecl asVarDecl() { result = v }
}
private class KeyPathSourceVariable extends SourceVariable, TKeyPathSourceVariable {
EntryNode enter;
KeyPathSourceVariable() { this = TKeyPathSourceVariable(enter) }
override string toString() { result = enter.toString() }
override EntryNode asKeyPath() { result = enter }
}
predicate variableWrite(BasicBlocks::BasicBlock bb, int i, SourceVariable v, boolean certain) {
exists(AssignExpr assign |
bb.getNode(i).getNode().asAstNode() = assign and
assign.getDest() = v.getAnAccess() and
certain = true
)
or
// Any variable initialization through pattern matching. For example each `x*` in:
// ```
// var x1 = v
// let x2 = v
// let (x3, x4) = tuple
// if let x5 = optional { ... }
// guard let x6 = optional else { ... }
// ```
exists(NamedPattern pattern |
bb.getNode(i).getNode().asAstNode() = pattern and
v.asVarDecl() = pattern.getVarDecl() and
certain = true
)
or
exists(ParamDecl p |
p = v.asVarDecl() and
bb.getNode(i).getNode().asAstNode() = p and
certain = true
)
or
bb.getNode(i) = v.asKeyPath() and
certain = true
or
// Mark the subexpression as a write of the local variable declared in the `TapExpr`.
exists(TapExpr tap |
v.asVarDecl() = tap.getVar() and
bb.getNode(i).getNode().asAstNode() = tap.getSubExpr() and
certain = true
)
}
predicate variableRead(BasicBlocks::BasicBlock bb, int i, SourceVariable v, boolean certain) {
exists(DeclRefExpr ref |
not isLValue(ref) and
bb.getNode(i).getNode().asAstNode() = ref and
v.asVarDecl() = ref.getDecl() and
certain = true
)
or
exists(InOutExpr expr |
bb.getNode(i).getNode().asAstNode() = expr and
expr.getSubExpr() = v.getAnAccess() and
certain = true
)
or
exists(ExitNode exit, Function func |
[func.getAParam(), func.getSelfParam()] = v.asVarDecl() and
bb.getNode(i) = exit and
modifiableParam(v.asVarDecl()) and
bb.getScope() = func and
certain = true
)
or
// Mark the `TapExpr` as a read of the of the local variable.
exists(TapExpr tap |
v.asVarDecl() = tap.getVar() and
bb.getNode(i).getNode().asAstNode() = tap and
certain = true
)
}
}
/**
* INTERNAL: Do not use.
*/
module SsaImpl = SsaImplCommon::Make<Location, BasicBlocks::Cfg, SsaInput>;
cached
class Definition extends SsaImpl::Definition {
cached
override Location getLocation() { none() }
cached
ControlFlowNode getARead() {
exists(SsaInput::SourceVariable v, BasicBlocks::BasicBlock bb, int i |
SsaImpl::ssaDefReachesRead(v, this, bb, i) and
SsaInput::variableRead(bb, i, v, true) and
result = bb.getNode(i)
)
}
cached
ControlFlowNode getAFirstRead() {
exists(BasicBlocks::BasicBlock bb, int i |
SsaImpl::firstUse(this, bb, i, true) and
result = bb.getNode(i)
)
}
cached
predicate adjacentReadPair(ControlFlowNode read1, ControlFlowNode read2) {
read1 = this.getARead() and
exists(BasicBlocks::BasicBlock bb1, int i1, BasicBlocks::BasicBlock bb2, int i2 |
read1 = bb1.getNode(i1) and
SsaImpl::adjacentUseUse(bb1, i1, bb2, i2, _, true) and
read2 = bb2.getNode(i2)
)
}
cached
deprecated predicate lastRefRedef(BasicBlocks::BasicBlock bb, int i, Definition next) {
SsaImpl::lastRefRedef(this, bb, i, next)
}
}
cached
class WriteDefinition extends Definition, SsaImpl::WriteDefinition {
cached
override Location getLocation() {
exists(BasicBlocks::BasicBlock bb, int i |
this.definesAt(_, bb, i) and
result = bb.getNode(i).getLocation()
)
}
/**
* Holds if this SSA definition represents a direct assignment of `value`
* to the underlying variable.
*/
cached
predicate assigns(CfgNode value) {
exists(AssignExpr a, BasicBlocks::BasicBlock bb, int i |
this.definesAt(_, bb, i) and
a = bb.getNode(i).getNode().asAstNode() and
value.getNode().asAstNode() = a.getSource()
)
or
exists(BasicBlocks::BasicBlock bb, int blockIndex, NamedPattern np |
this.definesAt(_, bb, blockIndex) and
np = bb.getNode(blockIndex).getNode().asAstNode() and
value.getNode().asAstNode() = np
)
or
exists(BasicBlocks::BasicBlock bb, int blockIndex, ConditionElement ce, Expr init |
this.definesAt(_, bb, blockIndex) and
ce.getPattern() = bb.getNode(blockIndex).getNode().asAstNode() and
init = ce.getInitializer() and
strictcount(Ssa::WriteDefinition alt | alt.definesAt(_, bb, blockIndex)) = 1 // exclude cases where there are multiple writes from the same pattern, this is at best taint flow.
|
value.getAst() = init
)
}
}
cached
class PhiDefinition extends Definition, SsaImpl::PhiNode {
cached
override Location getLocation() {
exists(BasicBlocks::BasicBlock bb |
this.definesAt(_, bb, _) and
result = bb.getLocation()
)
}
cached
Definition getPhiInput(BasicBlocks::BasicBlock bb) {
SsaImpl::phiHasInputFromBlock(this, result, bb)
}
cached
Definition getAPhiInput() { result = this.getPhiInput(_) }
}
}