-
Notifications
You must be signed in to change notification settings - Fork 63
/
flows.dl
321 lines (246 loc) · 9.24 KB
/
flows.dl
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
#pragma once
#include "dominators.dl"
//
// *** Data flow and dependency analysis ***
//
.comp GlobalFlowAnalysis {
// Inputs
.decl TransferStmt(stmt: Statement)
.decl InitialFlowVar(var: Variable)
.decl TransferBoundary(block: Block)
.decl Uses(stmt: Statement, use: Variable) overridable
.decl Defines(stmt: Statement, def: Variable) overridable
// Intermediate
.decl FlowsBase(from: Variable, to: Variable)
.decl VarOfInterest(var: Variable)
// Outputs
.decl Flows(from: Variable, to: Variable)
Uses(stmt, use):-
Statement_Uses(stmt, use, _).
Defines(stmt, def):-
Statement_Defines(stmt, def, _).
VarOfInterest(var) :-
Flows(_, var).
// Although FlowsBase is mutually recursive with Flows, it represents an attempt to break
// up the transitive closure into smaller relations, so that the same final tuples aren't
// computed in many different ways.
FlowsBase(x, x) :- InitialFlowVar(x).
// Flows(x, y) :- Flows(_, x), PHI(_, x, y).
/// This is commented out on purpose and will be eliminated later!
/// Please don't put it back in without consulting--it will break
/// the type inference client. It will make no difference for most
/// other clients, since their TransferOpcodes include PHIs anyway.
FlowsBase(x, y) :-
VarOfInterest(x),
Uses(stmt, x),
Defines(stmt, y),
TransferStmt(stmt),
Statement_Block(stmt, block),
TransferBoundary(block).
// Case: Flows from formal to formal return
FlowsBase(actual, actualReturn) :-
FunctionFlowSummary(fn, formal, m),
FunctionCallReturn(block, fn, _),
FormalArgs(fn, formal, n),
ActualArgs(block, actual, n),
ActualReturnArgs(block, actualReturn, m),
TransferBoundary(block).
// Case: Flows from local variable to formal return
FlowsBase(variable, actualReturn) :-
FunctionFlowSummary(fn, variable, m),
FunctionCallReturn(block, fn, _),
!FormalArgs(fn, variable, _),
ActualReturnArgs(block, actualReturn, m),
TransferBoundary(block).
// Recursive Case
Flows(x, y) :-
FlowsBase(x, y).
Flows(x, z) :-
Flows(x, y),
FlowsBase(y, z).
.plan 1:(2,1)
// Flows from variable to return argument
.decl FunctionFlowSummary(fn: Function, from:Variable, n:number)
// Flow from local or formal to formal return
FunctionFlowSummary(fn, from, n) :-
Flows(from, to),
FormalReturnArgs(fn, to, n).
.decl InterFunctionActualArgFlow(actual:Variable, formal:Variable)
InterFunctionActualArgFlow(actual, inFunctionVar):-
ActualArgs(caller, actual, n),
TransferBoundary(caller),
CallGraphEdge(caller, fn),
FormalArgs(fn, formal, n),
Flows(formal, inFunctionVar),
Variable_Function(inFunctionVar, fn). // Make it local. Global transitivity restored later with separate rule.
// Case: flow within the same function, or back to local
.decl GlobalFlows(from:Variable, to:Variable)
GlobalFlows(x, y) :- Flows(x, y).
// Case: forward inter-procedural assignment only
GlobalFlows(from, to) :-
GlobalFlows(from, actual),
InterFunctionActualArgFlow(actual, to).
GlobalFlows(from, to) :-
GlobalFlows(from, interm),
FlowsBase(interm, to).
}
.init standardflowanalysis = GlobalFlowAnalysis
standardflowanalysis.TransferStmt(stmt) :- FlowOp(op), Statement_Opcode(stmt, op).
standardflowanalysis.InitialFlowVar(v) :- isVariable(v).
standardflowanalysis.TransferBoundary(b) :- IsBlock(b).
#define DataFlows standardflowanalysis.GlobalFlows
.init constantOpFlows = GlobalFlowAnalysis
constantOpFlows.TransferStmt(stmt):-
UnaryArith(op),
Statement_Opcode(stmt, op).
constantOpFlows.TransferStmt(stmt):-
BinArith(op),
Statement_Opcode(stmt, op),
Statement_Uses(stmt, var, _),
Variable_Value(var, _). // Maybe replace with BasicVariable_Value
constantOpFlows.TransferStmt(stmt):-
Statement_Opcode(stmt, "PHI").
constantOpFlows.InitialFlowVar(v):- isVariable(v).
constantOpFlows.TransferBoundary(b):- IsBlock(b).
#define DataFlowsThroughConstOps constantOpFlows.GlobalFlows
// Need a custom data flow analysis? Of course you do!
// Initialise this component and indicate with opcodes to use as transfer functions.
// Also indicate the transfer boundary (e.g. Loop blocks for induction variables)
.comp LocalFlowAnalysis {
.decl TransferStmt(stmt: Statement)
.decl TransferBoundary(block: Block)
.decl Uses(stmt: Statement, use: Variable) overridable
.decl Defines(stmt: Statement, def: Variable) overridable
// Output
.decl Flows(x: Variable, y: Variable)
.decl NonTransitiveFlows(x: Variable, y: Variable)
Uses(stmt, use):-
Statement_Uses(stmt, use, _).
Defines(stmt, def):-
Statement_Defines(stmt, def, _).
NonTransitiveFlows(x, x),
Flows(x, x) :- isVariable(x).
// NonTransitiveFlows(x, y),
// Flows(x, y) :-
// TransferOpcodeArgument(op, n),
// Statement_Opcode(stmt, op),
// Statement_Block(stmt, block),
// TransferBoundary(block),
// Defines(stmt, y, _),
// Uses(stmt, x, n).
NonTransitiveFlows(x, y),
Flows(x, y) :-
TransferStmt(stmt),
Statement_Block(stmt, block),
TransferBoundary(block),
Defines(stmt, y),
Uses(stmt, x).
// Case: Flows from formal to formal return
NonTransitiveFlows(actual, actualReturn),
Flows(actual, actualReturn) :-
FunctionFlowSummary(fn, formal, m),
FunctionCallReturn(block, fn, _),
FormalArgs(fn, formal, n),
ActualArgs(block, actual, n),
ActualReturnArgs(block, actualReturn, m),
TransferBoundary(block).
// Flows from variable to return argument
.decl FunctionFlowSummary(fn: Function, from:Variable, n:number)
// Flow from local or formal to formal return
FunctionFlowSummary(fn, from, n) :-
Flows(from, to),
FormalReturnArgs(fn, to, n).
Flows(x, z) :-
Flows(x, y),
Flows(y, z).
.plan 1:(2,1)
// SL: Why is this here? remove it?
.decl TransferOpcodeArgument(op: Opcode, argument: number)
TransferOpcodeArgument(op, n) :- TransferOpcodeArgument(op, n). // suppress warning
}
// Note that this is an inter-procedural analysis with no context sensitivity
// The value of x depends on the value of y
// I.e. y is used to calculate x, either directly as an input of the operation
// that defined x, or transitively.
.init dependencyAnalysis = LocalFlowAnalysis
dependencyAnalysis.TransferStmt(stmt) :- Statement_Opcode(stmt, _).
dependencyAnalysis.TransferBoundary(block) :- IsBlock(block).
#define DependsOn dependencyAnalysis.Flows
.init localFlowAnalysis = LocalFlowAnalysis
localFlowAnalysis.TransferStmt(stmt) :- FlowOp(op), Statement_Opcode(stmt, op).
localFlowAnalysis.TransferBoundary(block) :- IsBlock(block).
#define LocalFlows localFlowAnalysis.Flows
/**
Local flows excluding PHIs
To be used when more precise, local inferrences are needed
*/
.init altLocalFlowAnalysis = LocalFlowAnalysis
altLocalFlowAnalysis.TransferStmt(stmt) :- FlowOp(op), Statement_Opcode(stmt, op), op != "PHI".
altLocalFlowAnalysis.TransferBoundary(block) :- IsBlock(block).
#define LocalFlowsExclPHI altLocalFlowAnalysis.Flows
// x controls whether y is executed.
.decl Controls(x:Statement, y:Block)
.decl DecisionEdge(jmpi: Statement, w1: Block, w2: Block)
DecisionEdge(jmpi, w1, w2) :-
LocalBlockEdge(x, w1),
FallthroughEdge(x, w2),
w1 != w2,
Block_Tail(x, jmpi),
JUMPI(jmpi, _, _).
Controls(jmpi, y) :-
(DecisionEdge(jmpi, w1, w2) ; DecisionEdge(jmpi, w2, w1)),
Dominates(w2, y),
!Dominates(w1, y).
Controls(x, z) :- Controls(x, y), Statement_Block(yjmp, y), Controls(yjmp, z). // Transitivity
// var is the guard variable of some conditional jump or throw stmt
.decl ConditionVar(var: Variable, stmt:Statement)
ConditionVar(var, stmt) :- JUMPI(stmt, _, var).
// x controls whether y executes by the value in condVar
.decl ControlsWith(x:Statement, y:Block, condVar:Variable)
ControlsWith(x, yHead, condVar) :-
Controls(x, yHead),
ConditionVar(condVar, x).
.decl ThrowBlock(block: Block)
ThrowBlock(revertblock) :-
Block_Tail(revertblock, revert),
(THROW(revert) ; REVERT(revert, _, _)).
.decl NonLinearEdge(block: Block)
NonLinearEdge(block) :-
GlobalBlockEdge(block, next),
GlobalBlockEdge(block, next2),
next2 != next.
ThrowBlock(block) :-
GlobalBlockEdge(block, next),
!NonLinearEdge(block),
ThrowBlock(next).
.decl AddResult(b: Variable, d: Variable)
AddResult(b, d) :-
(ADD(_, b, c, d) ; ADD(_, c, b, d)),
!NOT(_, _, c).
// calls
.decl CallSuccessControlsException(callStmt:Statement, throwStmt: Statement)
// The return value of a call (callStmt) controls
// whether an exception is thrown by throwStmt
// Either through a control dependency
CallSuccessControlsException(callStmt, throwStmt) :-
CallResult(resVar, callStmt),
DataFlows(resVar, condVar),
JUMPI(throwStmt, _, condVar),
ControlsWith(_, throwBlock, condVar),
ThrowBlock(throwBlock).
.decl CallFailureControls(call: Statement, to: Block)
CallFailureControls(call, to) :-
CallResult(res, call),
ISZERO(_, res, notres),
JUMPI(jmp, _, notres),
Statement_Block(jmp, from),
LocalBlockEdge(from, to),
!FallthroughEdge(from, to).
CallFailureControls(call, to) :-
CallResult(res, call),
JUMPI(jmp, _, res),
Statement_Block(jmp, from),
FallthroughEdge(from, to).
CallFailureControls(call, to) :-
CallFailureControls(call, from),
Dominates(from, to).