Skip to content

Commit 33cf96c

Browse files
committed
Python: Address review comments
1 parent d201eb2 commit 33cf96c

File tree

3 files changed

+177
-228
lines changed

3 files changed

+177
-228
lines changed

python/ql/src/experimental/dataflow/internal/DataFlowPrivate.qll

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ private import DataFlowPublic
44
//--------
55
// Data flow graph
66
//--------
7-
87
//--------
98
// Nodes
109
//--------
11-
1210
/**
1311
* A node associated with an object after an operation that might have
1412
* changed its state.
@@ -40,7 +38,8 @@ module EssaFlow {
4038
// `x = f(42)`
4139
// nodeFrom is `f(42)`, cfg node
4240
// nodeTo is `x`, essa var
43-
nodeFrom.(CfgNode).getNode() = nodeTo.(EssaNode).getVar().getDefinition().(AssignmentDefinition).getValue()
41+
nodeFrom.(CfgNode).getNode() =
42+
nodeTo.(EssaNode).getVar().getDefinition().(AssignmentDefinition).getValue()
4443
or
4544
// With definition
4645
// `with f(42) as x:`
@@ -49,7 +48,7 @@ module EssaFlow {
4948
exists(With with, ControlFlowNode contextManager, ControlFlowNode var |
5049
nodeFrom.(CfgNode).getNode() = contextManager and
5150
nodeTo.(EssaNode).getVar().getDefinition().(WithDefinition).getDefiningNode() = var and
52-
// see `with_flow`
51+
// see `with_flow` in `python/ql/src/semmle/python/dataflow/Implementation.qll`
5352
with.getContextExpr() = contextManager.getNode() and
5453
with.getOptionalVars() = var.getNode() and
5554
contextManager.strictlyDominates(var)
@@ -83,7 +82,6 @@ module EssaFlow {
8382
//--------
8483
// Local flow
8584
//--------
86-
8785
/**
8886
* This is the local flow predicate that is used as a building block in global
8987
* data flow. It is a strict subset of the `localFlowStep` predicate, as it
@@ -99,48 +97,35 @@ predicate simpleLocalFlowStep(Node nodeFrom, Node nodeTo) {
9997
//--------
10098
// Global flow
10199
//--------
102-
103100
/** Represents a callable */
104101
class DataFlowCallable = CallableValue;
105102

106103
/** Represents a call to a callable */
107104
class DataFlowCall extends CallNode {
108105
DataFlowCallable callable;
109106

110-
DataFlowCall() {
111-
this = callable.getACall()
112-
}
107+
DataFlowCall() { this = callable.getACall() }
113108

114109
/** Get the callable to which this call goes. */
115110
DataFlowCallable getCallable() { result = callable }
116111

117112
/** Gets the enclosing callable of this call. */
118-
DataFlowCallable getEnclosingCallable() {
119-
result.getScope() = this.getNode().getScope()
120-
}
113+
DataFlowCallable getEnclosingCallable() { result.getScope() = this.getNode().getScope() }
121114
}
122115

123116
/** A data flow node that represents a call argument. */
124117
class ArgumentNode extends CfgNode {
125-
ArgumentNode() {
126-
exists(DataFlowCall call, int pos |
127-
node = call.getArg(pos)
128-
)
129-
}
118+
ArgumentNode() { exists(DataFlowCall call, int pos | node = call.getArg(pos)) }
130119

131-
/** Holds if this argument occurs at the given position in the given call. */
132-
predicate argumentOf(DataFlowCall call, int pos) {
133-
node = call.getArg(pos)
134-
}
120+
/** Holds if this argument occurs at the given position in the given call. */
121+
predicate argumentOf(DataFlowCall call, int pos) { node = call.getArg(pos) }
135122

136-
/** Gets the call in which this node is an argument. */
123+
/** Gets the call in which this node is an argument. */
137124
final DataFlowCall getCall() { this.argumentOf(result, _) }
138125
}
139126

140127
/** Gets a viable run-time target for the call `call`. */
141-
DataFlowCallable viableCallable(DataFlowCall call) {
142-
result = call.getCallable()
143-
}
128+
DataFlowCallable viableCallable(DataFlowCall call) { result = call.getCallable() }
144129

145130
private newtype TReturnKind = TNormalReturnKind()
146131

@@ -157,49 +142,41 @@ class ReturnKind extends TReturnKind {
157142
class ReturnNode extends CfgNode {
158143
Return ret;
159144

160-
// See `TaintTrackingImplementation::returnFlowStep`
161-
ReturnNode() {
162-
node = ret.getValue().getAFlowNode()
163-
}
145+
// See `TaintTrackingImplementation::returnFlowStep`
146+
ReturnNode() { node = ret.getValue().getAFlowNode() }
164147

165-
/** Gets the kind of this return node. */
166-
ReturnKind getKind() { result = TNormalReturnKind() }
148+
/** Gets the kind of this return node. */
149+
ReturnKind getKind() { any() }
167150

168-
override DataFlowCallable getEnclosingCallable() {
151+
override DataFlowCallable getEnclosingCallable() {
169152
result.getScope().getAStmt() = ret // TODO: check nested function definitions
170153
}
171154
}
172155

173156
/** A data flow node that represents the output of a call. */
174157
class OutNode extends CfgNode {
175158
OutNode() { node instanceof CallNode }
176-
177-
/** Gets the underlying call, where this node is a corresponding output of kind `kind`. */
178-
cached
179-
DataFlowCall getCall(ReturnKind kind) {
180-
kind = TNormalReturnKind() and
181-
result = node
182-
}
183159
}
184160

185161
/**
186162
* Gets a node that can read the value returned from `call` with return kind
187163
* `kind`.
188164
*/
189-
OutNode getAnOutNode(DataFlowCall call, ReturnKind kind) { call = result.getCall(kind) }
165+
OutNode getAnOutNode(DataFlowCall call, ReturnKind kind) {
166+
call = result.getNode() and
167+
kind = TNormalReturnKind()
168+
}
190169

191170
//--------
192171
// Type pruning
193172
//--------
194-
195-
newtype TDataFlowType =
196-
TStringFlow()
173+
newtype TDataFlowType = TAnyFlow()
197174

198175
class DataFlowType extends TDataFlowType {
199176
/**
200177
* Gets a string representation of the data flow type.
201178
*/
202-
string toString() { result = "DataFlowType" }
179+
string toString() { result = "DataFlowType" }
203180
}
204181

205182
/** A node that performs a type cast. */
@@ -212,22 +189,19 @@ class CastNode extends Node {
212189
* a node of type `t1` to a node of type `t2`.
213190
*/
214191
pragma[inline]
215-
predicate compatibleTypes(DataFlowType t1, DataFlowType t2) {
216-
any()
217-
}
192+
predicate compatibleTypes(DataFlowType t1, DataFlowType t2) { any() }
218193

219194
/**
220195
* Gets the type of `node`.
221196
*/
222-
DataFlowType getNodeType(Node node) { result = TStringFlow() }
197+
DataFlowType getNodeType(Node node) { result = TAnyFlow() }
223198

224199
/** Gets a string representation of a type returned by `getErasedRepr`. */
225200
string ppReprType(DataFlowType t) { none() }
226201

227202
//--------
228203
// Extra flow
229204
//--------
230-
231205
/**
232206
* Holds if `pred` can flow to `succ`, by jumping from one callable to
233207
* another. Additional steps specified by the configuration are *not*
@@ -247,68 +221,52 @@ predicate jumpStep(Node pred, Node succ) {
247221
//--------
248222
// Field flow
249223
//--------
250-
251224
/**
252225
* Holds if data can flow from `node1` to `node2` via an assignment to
253226
* content `c`.
254227
*/
255-
predicate storeStep(Node node1, Content c, Node node2) {
256-
none()
257-
}
228+
predicate storeStep(Node node1, Content c, Node node2) { none() }
258229

259230
/**
260231
* Holds if data can flow from `node1` to `node2` via a read of content `c`.
261232
*/
262-
predicate readStep(Node node1, Content c, Node node2) {
263-
none()
264-
}
233+
predicate readStep(Node node1, Content c, Node node2) { none() }
265234

266235
/**
267236
* Holds if values stored inside content `c` are cleared at node `n`. For example,
268237
* any value stored inside `f` is cleared at the pre-update node associated with `x`
269238
* in `x.f = newValue`.
270239
*/
271240
cached
272-
predicate clearsContent(Node n, Content c) {
273-
none()
274-
}
241+
predicate clearsContent(Node n, Content c) { none() }
275242

276243
//--------
277244
// Fancy context-sensitive guards
278245
//--------
279-
280246
/**
281247
* Holds if the node `n` is unreachable when the call context is `call`.
282248
*/
283-
predicate isUnreachableInCall(Node n, DataFlowCall call) {
284-
none()
285-
}
249+
predicate isUnreachableInCall(Node n, DataFlowCall call) { none() }
286250

287251
//--------
288252
// Virtual dispatch with call context
289253
//--------
290-
291254
/**
292255
* Gets a viable dispatch target of `call` in the context `ctx`. This is
293256
* restricted to those `call`s for which a context might make a difference.
294257
*/
295-
DataFlowCallable viableImplInCallContext(DataFlowCall call, DataFlowCall ctx) {
296-
none()
297-
}
258+
DataFlowCallable viableImplInCallContext(DataFlowCall call, DataFlowCall ctx) { none() }
298259

299260
/**
300261
* Holds if the set of viable implementations that can be called by `call`
301262
* might be improved by knowing the call context. This is the case if the qualifier accesses a parameter of
302263
* the enclosing callable `c` (including the implicit `this` parameter).
303264
*/
304-
predicate mayBenefitFromCallContext(DataFlowCall call, DataFlowCallable c) {
305-
none()
306-
}
265+
predicate mayBenefitFromCallContext(DataFlowCall call, DataFlowCallable c) { none() }
307266

308267
//--------
309268
// Misc
310269
//--------
311-
312270
/**
313271
* Holds if `n` does not require a `PostUpdateNode` as it either cannot be
314272
* modified or its modification cannot be observed, for example if it is a

0 commit comments

Comments
 (0)