Skip to content

Commit ee6624a

Browse files
authored
Merge pull request #157 from microsoft/dataflow-stack-cleanup
Shared: Refactor `DataFlowStack`
2 parents 81fa6fc + c825ca8 commit ee6624a

File tree

13 files changed

+1102
-1062
lines changed

13 files changed

+1102
-1062
lines changed

cpp/ql/lib/semmle/code/cpp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -255,27 +255,9 @@ class DataFlowCall extends Expr instanceof Call {
255255
*/
256256
Expr getArgument(int n) { result = super.getArgument(n) }
257257

258-
/** Gets an argument to this call. */
259-
Expr getAnArgument(){ result = super.getAnArgument() }
260-
261-
/** Gets an argument to this call as a Node. */
262-
ArgumentNode getAnArgumentNode(){ result = this.getNode() }
263-
264258
/** Gets the data flow node corresponding to this call. */
265259
ExprNode getNode() { result.getExpr() = this }
266260

267-
/** Gets the data flow node corresponding to this call. (Alias of `getNode()`) */
268-
ExprNode getDataFlowNode() { result = this.getNode() }
269-
270-
/** Gets the target of the call, as best as makes sense for this kind of call.
271-
*
272-
* The precise meaning depends on the kind of call it is:
273-
* - For a call to a function, it’s the function being called.
274-
* - For a C++ method call, it’s the statically resolved method.
275-
* - For an Objective C message expression, it’s the statically resolved method, and it might not exist.
276-
* - For a variable call, it never exists.
277-
*/
278-
DataFlowCallable getARuntimeTarget(){ result = super.getTarget() }
279261
/** Gets the enclosing callable of this call. */
280262
DataFlowCallable getEnclosingCallable() { result = this.getEnclosingFunction() }
281263
}

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,16 +1058,6 @@ class DataFlowCallable extends TDataFlowCallable {
10581058
result = this.asSummarizedCallable() or // SummarizedCallable = Function (in CPP)
10591059
result = this.asSourceCallable()
10601060
}
1061-
1062-
/** Gets a best-effort total ordering. */
1063-
int totalorder() {
1064-
this =
1065-
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
1066-
c.getLocation().hasLocationInfo(file, startline, startcolumn, _, _)
1067-
|
1068-
c order by file, startline, startcolumn
1069-
)
1070-
}
10711061
}
10721062

10731063
/**
@@ -1169,23 +1159,6 @@ class DataFlowCall extends TDataFlowCall {
11691159
* Gets the location of this call.
11701160
*/
11711161
Location getLocation() { none() }
1172-
1173-
// #43: Stub Implementation
1174-
/** Gets an argument to this call as a Node. */
1175-
ArgumentNode getAnArgumentNode(){ none() } // TODO: JB1 return an argument as a DataFlow ArgumentNode
1176-
1177-
// #43: Stub Implementation
1178-
/** Gets the target of the call, as a DataFlowCallable. */
1179-
DataFlowCallable getARuntimeTarget(){ none() } // TODO getCallTarget() returns `Instruction`
1180-
/** Gets a best-effort total ordering. */
1181-
int totalorder() {
1182-
this =
1183-
rank[result](DataFlowCall c, int startline, int startcolumn |
1184-
c.getLocation().hasLocationInfo(_, startline, startcolumn, _, _)
1185-
|
1186-
c order by startline, startcolumn
1187-
)
1188-
}
11891162
}
11901163

11911164
/**
@@ -1280,15 +1253,6 @@ module IsUnreachableInCall {
12801253
string toString() { result = "NodeRegion" }
12811254

12821255
predicate contains(Node n) { this = n.getBasicBlock() }
1283-
1284-
int totalOrder() {
1285-
this =
1286-
rank[result](IRBlock b, int startline, int startcolumn |
1287-
b.getLocation().hasLocationInfo(_, startline, startcolumn, _, _)
1288-
|
1289-
b order by startline, startcolumn
1290-
)
1291-
}
12921256
}
12931257

12941258
predicate isUnreachableInCall(NodeRegion block, DataFlowCall call) {
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
21
import csharp
32
private import codeql.dataflow.DataFlow
43
private import semmle.code.csharp.dataflow.internal.DataFlowImplSpecific
5-
64
private import codeql.dataflowstack.DataFlowStack as DFS
75
private import DFS::DataFlowStackMake<Location, CsharpDataFlow> as DataFlowStackFactory
86

9-
module DataFlowStackMake<DataFlowStackFactory::DataFlow::GlobalFlowSig Flow>{
10-
import DataFlowStackFactory::FlowStack<Flow>
7+
private module DataFlowStackInput<DataFlowStackFactory::DataFlow::ConfigSig Config> implements
8+
DFS::DataFlowStackSig<Location, CsharpDataFlow, Config>
9+
{
10+
private module Flow = DataFlow::Global<Config>;
11+
12+
CsharpDataFlow::Node getNode(Flow::PathNode n) { result = n.getNode() }
13+
14+
predicate isSource(Flow::PathNode n) { n.isSource() }
15+
16+
Flow::PathNode getASuccessor(Flow::PathNode n) { result = n.getASuccessor() }
17+
18+
CsharpDataFlow::DataFlowCallable getARuntimeTarget(CsharpDataFlow::DataFlowCall call) {
19+
result = call.getARuntimeTarget()
20+
}
21+
22+
CsharpDataFlow::Node getAnArgumentNode(CsharpDataFlow::DataFlowCall call) {
23+
result = call.getArgument(_)
24+
}
1125
}
1226

13-
module BiStackAnalysisMake<DataFlowStackFactory::DataFlow::GlobalFlowSig FlowA, DataFlowStackFactory::DataFlow::GlobalFlowSig FlowB>{
14-
import DataFlowStackFactory::BiStackAnalysis<FlowA, FlowB>
15-
}
27+
module DataFlowStackMake<DataFlowStackFactory::DataFlow::ConfigSig Config> {
28+
import DataFlowStackFactory::FlowStack<Config, DataFlowStackInput<Config>>
29+
}
30+
31+
module BiStackAnalysisMake<
32+
DataFlowStackFactory::DataFlow::ConfigSig ConfigA,
33+
DataFlowStackFactory::DataFlow::ConfigSig ConfigB>
34+
{
35+
import DataFlowStackFactory::BiStackAnalysis<ConfigA, DataFlowStackInput<ConfigA>, ConfigB, DataFlowStackInput<ConfigB>>
36+
}

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowDispatch.qll

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,6 @@ class DataFlowCallable extends TDataFlowCallable {
288288
or
289289
result = this.asCapturedVariable().getLocation()
290290
}
291-
292-
/** Gets a best-effort total ordering. */
293-
int totalorder() {
294-
this =
295-
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
296-
c.getLocation().hasLocationInfo(file, startline, startcolumn, _, _)
297-
|
298-
c order by file, startline, startcolumn
299-
)
300-
}
301291
}
302292

303293
/** A call relevant for data flow. */
@@ -323,9 +313,6 @@ abstract class DataFlowCall extends TDataFlowCall {
323313
/** Gets the argument at position `pos` of this call. */
324314
final ArgumentNode getArgument(ArgumentPosition pos) { result.argumentOf(this, pos) }
325315

326-
/** Gets an argument of this call. */
327-
final ArgumentNode getAnArgumentNode() { result.argumentOf(this, _) }
328-
329316
/** Gets a textual representation of this call. */
330317
abstract string toString();
331318

@@ -344,16 +331,6 @@ abstract class DataFlowCall extends TDataFlowCall {
344331
) {
345332
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
346333
}
347-
348-
/** Gets a best-effort total ordering. */
349-
int totalorder() {
350-
this =
351-
rank[result](DataFlowCall c, int startline, int startcolumn |
352-
c.hasLocationInfo(_, startline, startcolumn, _, _)
353-
|
354-
c order by startline, startcolumn
355-
)
356-
}
357334
}
358335

359336
private predicate relevantFolder(Folder f) {

go/ql/lib/semmle/go/dataflow/internal/DataFlowPrivate.qll

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,6 @@ class DataFlowCallable extends TDataFlowCallable {
315315
result = this.asFileScope().getLocation() or
316316
result = getCallableLocation(this.asSummarizedCallable())
317317
}
318-
319-
/** Gets a best-effort total ordering. */
320-
int totalorder() {
321-
this =
322-
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
323-
c.hasLocationInfo(file, startline, startcolumn, _, _)
324-
|
325-
c order by file, startline, startcolumn
326-
)
327-
}
328318
}
329319

330320
private Location getCallableLocation(Callable c) {
@@ -358,23 +348,6 @@ class DataFlowCall extends Expr {
358348

359349
/** Gets the location of this call. */
360350
Location getLocation() { result = super.getLocation() }
361-
362-
// #45 - Stub Implementation
363-
/** Gets an argument to this call as a Node. */
364-
ArgumentNode getAnArgumentNode(){ result = this.getArgument(_) }
365-
366-
/** Gets the target of the call, as a DataFlowCallable. */
367-
DataFlowCallable getARuntimeTarget(){ result.asCallable() = call.getACalleeIncludingExternals() }
368-
369-
/** Gets a best-effort total ordering. */
370-
int totalorder() {
371-
this =
372-
rank[result](DataFlowCall c, int startline, int startcolumn |
373-
c.getLocation().hasLocationInfo(_, startline, startcolumn, _, _)
374-
|
375-
c order by startline, startcolumn
376-
)
377-
}
378351
}
379352

380353
/** Holds if `e` is an expression that always has the same Boolean value `val`. */
@@ -417,15 +390,6 @@ class NodeRegion instanceof BasicBlock {
417390
string toString() { result = "NodeRegion" }
418391

419392
predicate contains(Node n) { n.getBasicBlock() = this }
420-
421-
int totalOrder() {
422-
this =
423-
rank[result](BasicBlock b, int startline, int startcolumn |
424-
b.hasLocationInfo(_, startline, startcolumn, _, _)
425-
|
426-
b order by startline, startcolumn
427-
)
428-
}
429393
}
430394

431395
/**

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowPrivate.qll

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -423,21 +423,6 @@ predicate cloneStep(Node n1, Node n2) {
423423
bindingset[node1, node2]
424424
predicate validParameterAliasStep(Node node1, Node node2) { not cloneStep(node1, node2) }
425425

426-
private predicate id_member(Member x, Member y) { x = y }
427-
428-
private predicate idOf_member(Member x, int y) = equivalenceRelation(id_member/2)(x, y)
429-
430-
private int summarizedCallableId(SummarizedCallable c) {
431-
c =
432-
rank[result](SummarizedCallable c0, int b, int i, string s |
433-
b = 0 and idOf_member(c0.asCallable(), i) and s = ""
434-
or
435-
b = 1 and i = 0 and s = c0.asSyntheticCallable()
436-
|
437-
c0 order by b, i, s
438-
)
439-
}
440-
441426
private newtype TDataFlowCallable =
442427
TSrcCallable(Callable c) or
443428
TSummarizedCallable(SummarizedCallable c) or
@@ -471,28 +456,10 @@ class DataFlowCallable extends TDataFlowCallable {
471456
result = this.asSummarizedCallable().getLocation() or
472457
result = this.asFieldScope().getLocation()
473458
}
474-
475-
/** Gets a best-effort total ordering. */
476-
int totalorder() {
477-
this =
478-
rank[result](DataFlowCallable c, int b, int i |
479-
b = 0 and idOf_member(c.asCallable(), i)
480-
or
481-
b = 1 and i = summarizedCallableId(c.asSummarizedCallable())
482-
or
483-
b = 2 and idOf_member(c.asFieldScope(), i)
484-
|
485-
c order by b, i
486-
)
487-
}
488459
}
489460

490461
class DataFlowExpr = Expr;
491462

492-
private predicate id_call(Call x, Call y) { x = y }
493-
494-
private predicate idOf_call(Call x, int y) = equivalenceRelation(id_call/2)(x, y)
495-
496463
private newtype TDataFlowCall =
497464
TCall(Call c) or
498465
TSummaryCall(SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver) {
@@ -525,29 +492,6 @@ class DataFlowCall extends TDataFlowCall {
525492
) {
526493
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
527494
}
528-
529-
/** Gets an argument to this call as a Node. */
530-
ArgumentNode getAnArgumentNode(){
531-
result = exprNode(this.asCall().getAnArgument())
532-
}
533-
534-
/** Gets the target of the call, as a DataFlowCallable. */
535-
DataFlowCallable getARuntimeTarget(){
536-
result.asCallable() = this.asCall().getCallee()
537-
}
538-
539-
/** Gets a best-effort total ordering. */
540-
int totalorder() {
541-
this =
542-
rank[result](DataFlowCall c, int b, int i |
543-
b = 0 and idOf_call(c.asCall(), i)
544-
or
545-
b = 1 and // not guaranteed to be total
546-
exists(SummarizedCallable sc | c = TSummaryCall(sc, _) and i = summarizedCallableId(sc))
547-
|
548-
c order by b, i
549-
)
550-
}
551495
}
552496

553497
/** A source call, that is, a `Call`. */
@@ -582,16 +526,10 @@ class SummaryCall extends DataFlowCall, TSummaryCall {
582526
override Location getLocation() { result = c.getLocation() }
583527
}
584528

585-
private predicate id(BasicBlock x, BasicBlock y) { x = y }
586-
587-
private predicate idOf(BasicBlock x, int y) = equivalenceRelation(id/2)(x, y)
588-
589529
class NodeRegion instanceof BasicBlock {
590530
string toString() { result = "NodeRegion" }
591531

592532
predicate contains(Node n) { n.asExpr().getBasicBlock() = this }
593-
594-
int totalOrder() { idOf(this, result) }
595533
}
596534

597535
/** Holds if `e` is an expression that always has the same Boolean value `val`. */

python/ql/lib/semmle/python/dataflow/new/internal/DataFlowDispatch.qll

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,6 @@ abstract class DataFlowCallable extends TDataFlowCallable {
347347

348348
/** Gets the location of this dataflow callable. */
349349
abstract Location getLocation();
350-
351-
/** Gets a best-effort total ordering. */
352-
int totalorder() {
353-
this =
354-
rank[result](DataFlowCallable c, string file, int startline, int startcolumn |
355-
c.getLocation().hasLocationInfo(file, startline, startcolumn, _, _)
356-
|
357-
c order by file, startline, startcolumn
358-
)
359-
}
360350
}
361351

362352
/** A callable function. */
@@ -1439,23 +1429,6 @@ abstract class DataFlowCall extends TDataFlowCall {
14391429
) {
14401430
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
14411431
}
1442-
1443-
// #47: Stubs below
1444-
/** Gets an argument to this call as a Node. */
1445-
ArgumentNode getAnArgumentNode(){ none() } // TODO: JB1 return an argument as a DataFlow ArgumentNode
1446-
1447-
/** Gets the target of the call, as a DataFlowCallable. */
1448-
DataFlowCallable getARuntimeTarget(){ none() } // TODO
1449-
1450-
/** Gets a best-effort total ordering. */
1451-
int totalorder() {
1452-
this =
1453-
rank[result](DataFlowCall c, int startline, int startcolumn |
1454-
c.hasLocationInfo(_, startline, startcolumn, _, _)
1455-
|
1456-
c order by startline, startcolumn
1457-
)
1458-
}
14591432
}
14601433

14611434
/** A call found in the program source (as opposed to a synthesised call). */

0 commit comments

Comments
 (0)