-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathSsa.qll
More file actions
361 lines (333 loc) · 11 KB
/
Ssa.qll
File metadata and controls
361 lines (333 loc) · 11 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* Provides the module `Ssa` for working with static single assignment (SSA) form.
*/
/**
* Provides classes for working with static single assignment (SSA) form.
*/
module Ssa {
private import rust
private import codeql.rust.controlflow.BasicBlocks
private import codeql.rust.controlflow.ControlFlowGraph
private import codeql.rust.controlflow.internal.ControlFlowGraphImpl as CfgImpl
private import internal.SsaImpl as SsaImpl
class Variable = SsaImpl::SsaInput::SourceVariable;
/** A static single assignment (SSA) definition. */
class Definition extends SsaImpl::Definition {
/**
* Gets the control flow node of this SSA definition, if any. Phi nodes are
* examples of SSA definitions without a control flow node, as they are
* modeled at index `-1` in the relevant basic block.
*/
final CfgNode getControlFlowNode() {
exists(BasicBlock bb, int i | this.definesAt(_, bb, i) | result = bb.getNode(i))
}
/**
* Gets a control flow node that reads the value of this SSA definition.
*
* Example:
*
* ```rust
* fn phi(b : bool) { // defines b_0
* let mut x = 1; // defines x_0
* println!("{}", x); // reads x_0
* println!("{}", x + 1); // reads x_0
*
* if b { // reads b_0
* x = 2; // defines x_1
* println!("{}", x); // reads x_1
* println!("{}", x + 1); // reads x_1
* } else {
* x = 3; // defines x_2
* println!("{}", x); // reads x_2
* println!("{}", x + 1); // reads x_2
* }
* // defines x_3 = phi(x_1, x_2)
* println!("{}", x); // reads x_3
* }
* ```
*/
final Expr getARead() { result = SsaImpl::getARead(this) }
/**
* Gets a first control flow node that reads the value of this SSA definition.
* That is, a read that can be reached from this definition without passing
* through other reads.
*
* Example:
*
* ```rust
* fn phi(b : bool) { // defines b_0
* let mut x = 1; // defines x_0
* println!("{}", x); // first read of x_0
* println!("{}", x + 1);
*
* if b { // first read of b_0
* x = 2; // defines x_1
* println!("{}", x); // first read of x_1
* println!("{}", x + 1);
* } else {
* x = 3; // defines x_2
* println!("{}", x); // first read of x_2
* println!("{}", x + 1);
* }
* // defines x_3 = phi(x_1, x_2)
* println!("{}", x); // first read of x_3
* }
* ```
*/
final Expr getAFirstRead() { SsaImpl::firstRead(this, result) }
/**
* Holds if `read1` and `read2` are adjacent reads of this SSA definition.
* That is, `read2` can be reached from `read1` without passing through
* another read.
*
* Example:
*
* ```rust
* fn phi(b : bool) {
* let mut x = 1; // defines x_0
* println!("{}", x); // reads x_0 (read1)
* println!("{}", x + 1); // reads x_0 (read2)
*
* if b {
* x = 2; // defines x_1
* println!("{}", x); // reads x_1 (read1)
* println!("{}", x + 1); // reads x_1 (read2)
* } else {
* x = 3; // defines x_2
* println!("{}", x); // reads x_2 (read1)
* println!("{}", x + 1); // reads x_2 (read2)
* }
* println!("{}", x);
* }
* ```
*/
final predicate hasAdjacentReads(Expr read1, Expr read2) {
SsaImpl::adjacentReadPair(this, read1, read2)
}
/**
* Gets an SSA definition whose value can flow to this one in one step. This
* includes inputs to phi nodes and the prior definitions of uncertain writes.
*/
private Definition getAPhiInputOrPriorDefinition() {
result = this.(PhiDefinition).getAnInput()
}
/**
* Gets a definition that ultimately defines this SSA definition and is
* not itself a phi node.
*
* Example:
*
* ```rust
* fn phi(b : bool) {
* let mut x = 1; // defines x_0
* println!("{}", x);
* println!("{}", x + 1);
*
* if b {
* x = 2; // defines x_1
* println!("{}", x);
* println!("{}", x + 1);
* } else {
* x = 3; // defines x_2
* println!("{}", x);
* println!("{}", x + 1);
* }
* // defines x_3 = phi(x_1, x_2); ultimate definitions are x_1 and x_2
* println!("{}", x);
* }
* ```
*/
final Definition getAnUltimateDefinition() {
result = this.getAPhiInputOrPriorDefinition*() and
not result instanceof PhiDefinition
}
override string toString() { result = this.getControlFlowNode().toString() }
/** Gets the scope of this SSA definition. */
CfgScope getScope() { result = this.getBasicBlock().getScope() }
}
/**
* An SSA definition that corresponds to a write. Example:
*
* ```rust
* fn m(i : i64) { // writes `i`
* let mut x = i; // writes `x`
* x = 11; // writes `x`
* }
* ```
*/
class WriteDefinition extends Definition, SsaImpl::WriteDefinition {
private AstNode write;
WriteDefinition() {
exists(BasicBlock bb, int i, Variable v, AstNode n |
this.definesAt(v, bb, i) and
SsaImpl::variableWriteActual(bb, i, v, n.getACfgNode())
|
write.(VariableAccess).getVariable() = v and
(
write = n.(AssignmentExpr).getAWriteAccess()
or
write = n.(CompoundAssignmentExpr).getLhs()
)
or
not n instanceof AssignmentExpr and
not n instanceof CompoundAssignmentExpr and
write = n
)
}
/** Gets the underlying write access. */
final AstNode getWriteAccess() { result = write }
/**
* Holds if this SSA definition assigns `value` to the underlying
* variable.
*
* This is either the value in a direct assignment, `x = value`, or in a
* `let` statement, `let x = value`. Note that patterns on the lhs. are
* currently not supported.
*/
predicate assigns(Expr value) {
exists(AssignmentExpr ae |
ae.getLhs() = write and
ae.getRhs() = value
)
or
exists(IdentPat pat | pat.getName() = write |
exists(LetStmt ls |
pat = ls.getPat() and
ls.getInitializer() = value
)
or
exists(LetExpr le |
pat = le.getPat() and
le.getScrutinee() = value
)
)
}
final override string toString() { result = write.toString() }
final override Location getLocation() { result = write.getLocation() }
}
/**
* A phi definition. For example, in
*
* ```rust
* if b {
* x = 0
* } else {
* x = 1
* }
* println!("{}", x);
* ```
*
* a phi definition for `x` is inserted just before the call to `println!`.
*/
class PhiDefinition extends Definition, SsaImpl::PhiDefinition {
/**
* Gets an input of this phi definition.
*
* Example:
*
* ```rust
* fn phi(b : bool) {
* let mut x = 1; // defines x_0
* println!("{}", x);
* println!("{}", x + 1);
*
* if b {
* x = 2; // defines x_1
* println!("{}", x);
* println!("{}", x + 1);
* } else {
* x = 3; // defines x_2
* println!("{}", x);
* println!("{}", x + 1);
* }
* // defines x_3 = phi(x_1, x_2); inputs are x_1 and x_2
* println!("{}", x);
* }
* ```
*/
final Definition getAnInput() { this.hasInputFromBlock(result, _) }
/** Holds if `inp` is an input to this phi definition along the edge originating in `bb`. */
predicate hasInputFromBlock(Definition inp, BasicBlock bb) {
inp = SsaImpl::phiHasInputFromBlock(this, bb)
}
private string getSplitString() {
result = this.getBasicBlock().getFirstNode().(CfgImpl::AstCfgNode).getSplitsString()
}
override string toString() {
exists(string prefix |
prefix = "[" + this.getSplitString() + "] "
or
not exists(this.getSplitString()) and
prefix = ""
|
result = prefix + SsaImpl::PhiDefinition.super.toString()
)
}
/*
* The location of a phi definition is the same as the location of the first node
* in the basic block in which it is defined.
*
* Strictly speaking, the node is *before* the first node, but such a location
* does not exist in the source program.
*/
final override Location getLocation() {
result = this.getBasicBlock().getFirstNode().getLocation()
}
}
/**
* An SSA definition inserted at the beginning of a scope to represent a
* captured local variable. For example, in
*
* ```rust
* fn capture_immut() {
* let x = 100;
* let mut cap = || {
* println!("{}", x);
* };
* cap();
* }
* ```
*
* an entry definition for `x` is inserted at the start of the CFG for `cap`.
*/
class CapturedEntryDefinition extends Definition, SsaImpl::WriteDefinition {
CapturedEntryDefinition() {
exists(BasicBlock bb, int i, Variable v |
this.definesAt(v, bb, i) and
SsaImpl::capturedEntryWrite(bb, i, v)
)
}
final override string toString() { result = "<captured entry> " + this.getSourceVariable() }
override Location getLocation() { result = this.getBasicBlock().getLocation() }
}
/**
* An SSA definition inserted at a call that may update the value of a captured
* variable. For example, in
*
* ```rust
* fn capture_mut() {
* let mut y = 0;
* (0..5).for_each(|x| {
* y += x
* });
* y
* }
* ```
*
* a definition for `y` is inserted at the call to `for_each`.
*/
private class CapturedCallDefinition extends Definition, SsaImpl::UncertainWriteDefinition {
CapturedCallDefinition() {
exists(Variable v, BasicBlock bb, int i |
this.definesAt(v, bb, i) and
SsaImpl::capturedCallWrite(_, bb, i, v)
)
}
/**
* Gets the immediately preceding definition. Since this update is uncertain,
* the value from the preceding definition might still be valid.
*/
final Definition getPriorDefinition() { result = SsaImpl::uncertainWriteDefinitionInput(this) }
override string toString() { result = "<captured exit> " + this.getSourceVariable() }
}
}