Skip to content

Commit 94a72ec

Browse files
committed
Java: Refactor SummarizedCallable.
1 parent 73802cb commit 94a72ec

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private module DispatchImpl {
1010
DataFlowCallable viableCallable(DataFlowCall c) {
1111
result.asCallable() = VirtualDispatch::viableCallable(c.asCall())
1212
or
13-
result.(SummarizedCallable).asCallable() = c.asCall().getCallee().getSourceDeclaration()
13+
result.asCallable().(SummarizedCallable) = c.asCall().getCallee().getSourceDeclaration()
1414
}
1515

1616
/**
@@ -118,7 +118,7 @@ private module DispatchImpl {
118118
not failsUnification(t, t2)
119119
)
120120
or
121-
result.asCallable() = def and result instanceof SummarizedCallable
121+
result.asCallable() = def and def instanceof SummarizedCallable
122122
)
123123
}
124124

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ newtype TNode =
1414
not e.getParent*() instanceof Annotation
1515
} or
1616
TExplicitParameterNode(Parameter p) {
17-
exists(p.getCallable().getBody()) or p.getCallable() = any(SummarizedCallable sc).asCallable()
17+
exists(p.getCallable().getBody()) or p.getCallable() instanceof SummarizedCallable
1818
} or
1919
TImplicitVarargsArray(Call c) {
2020
c.getCallee().isVarargs() and
2121
not exists(Argument arg | arg.getCall() = c and arg.isExplicitVarargsArray())
2222
} or
2323
TInstanceParameterNode(Callable c) {
24-
(exists(c.getBody()) or c = any(SummarizedCallable sc).asCallable()) and
24+
(exists(c.getBody()) or c instanceof SummarizedCallable) and
2525
not c.isStatic()
2626
} or
2727
TImplicitInstanceAccess(InstanceAccessExt ia) { not ia.isExplicit(_) } or
@@ -336,7 +336,7 @@ module Private {
336336
result.asCallable() = n.(ImplicitInstanceAccess).getInstanceAccess().getEnclosingCallable() or
337337
result.asCallable() = n.(MallocNode).getClassInstanceExpr().getEnclosingCallable() or
338338
result = nodeGetEnclosingCallable(n.(ImplicitPostUpdateNode).getPreUpdateNode()) or
339-
n = TSummaryInternalNode(result, _) or
339+
n = TSummaryInternalNode(result.asCallable(), _) or
340340
result.asFieldScope() = n.(FieldValueNode).getField()
341341
}
342342

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ class SummaryCall extends DataFlowCall, TSummaryCall {
317317
/** Gets the data flow node that this call targets. */
318318
Node getReceiver() { result = receiver }
319319

320-
override DataFlowCallable getEnclosingCallable() { result = c }
320+
override DataFlowCallable getEnclosingCallable() { result.asCallable() = c }
321321

322322
override string toString() { result = "[summary] call to " + receiver + " in " + c }
323323

@@ -378,7 +378,7 @@ predicate forceHighPrecision(Content c) {
378378
predicate nodeIsHidden(Node n) {
379379
n instanceof SummaryNode
380380
or
381-
n.(ParameterNode).isParameterOf(any(SummarizedCallable c).asCallable(), _)
381+
n.(ParameterNode).isParameterOf(any(SummarizedCallable c), _)
382382
}
383383

384384
class LambdaCallKind = Method; // the "apply" method in the functional interface

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ module Public {
195195
}
196196

197197
/** A callable with a flow summary. */
198-
abstract class SummarizedCallable extends DataFlowCallable {
198+
abstract class SummarizedCallable extends SummarizedCallableBase {
199+
bindingset[this]
200+
SummarizedCallable() { any() }
201+
199202
/**
200203
* Holds if data may flow from `input` to `output` through this callable.
201204
*
@@ -493,7 +496,7 @@ module Private {
493496
or
494497
exists(ParameterPosition pos |
495498
parameterReadState(c, state, pos) and
496-
result.(ParamNode).isParameterOf(c, pos)
499+
result.(ParamNode).isParameterOf(inject(c), pos)
497500
)
498501
)
499502
}
@@ -621,7 +624,7 @@ module Private {
621624
predicate summaryPostUpdateNode(Node post, Node pre) {
622625
exists(SummarizedCallable c, ParameterPosition pos |
623626
isParameterPostUpdate(post, c, pos) and
624-
pre.(ParamNode).isParameterOf(c, pos)
627+
pre.(ParamNode).isParameterOf(inject(c), pos)
625628
)
626629
or
627630
exists(SummarizedCallable callable, SummaryComponentStack s |
@@ -644,7 +647,7 @@ module Private {
644647
* node, and back out to `p`.
645648
*/
646649
predicate summaryAllowParameterReturnInSelf(ParamNode p) {
647-
exists(SummarizedCallable c, ParameterPosition ppos | p.isParameterOf(c, ppos) |
650+
exists(SummarizedCallable c, ParameterPosition ppos | p.isParameterOf(inject(c), ppos) |
648651
exists(SummaryComponentStack inputContents, SummaryComponentStack outputContents |
649652
summary(c, inputContents, outputContents, _) and
650653
inputContents.bottom() = pragma[only_bind_into](TArgumentSummaryComponent(ppos)) and
@@ -748,8 +751,11 @@ module Private {
748751
private predicate viableParam(
749752
DataFlowCall call, SummarizedCallable sc, ParameterPosition ppos, ParamNode p
750753
) {
751-
p.isParameterOf(sc, ppos) and
752-
sc = viableCallable(call)
754+
exists(DataFlowCallable c |
755+
c = inject(sc) and
756+
p.isParameterOf(c, ppos) and
757+
c = viableCallable(call)
758+
)
753759
}
754760

755761
pragma[nomagic]
@@ -1067,16 +1073,18 @@ module Private {
10671073
/** Provides a query predicate for outputting a set of relevant flow summaries. */
10681074
module TestOutput {
10691075
/** A flow summary to include in the `summary/3` query predicate. */
1070-
abstract class RelevantSummarizedCallable extends SummarizedCallable {
1076+
abstract class RelevantSummarizedCallable instanceof SummarizedCallable {
10711077
/** Gets the string representation of this callable used by `summary/1`. */
10721078
abstract string getCallableCsv();
10731079

10741080
/** Holds if flow is propagated between `input` and `output`. */
10751081
predicate relevantSummary(
10761082
SummaryComponentStack input, SummaryComponentStack output, boolean preservesValue
10771083
) {
1078-
this.propagatesFlow(input, output, preservesValue)
1084+
super.propagatesFlow(input, output, preservesValue)
10791085
}
1086+
1087+
string toString() { result = super.toString() }
10801088
}
10811089

10821090
/** Render the kind in the format used in flow summaries. */
@@ -1087,7 +1095,7 @@ module Private {
10871095
}
10881096

10891097
private string renderGenerated(RelevantSummarizedCallable c) {
1090-
if c.isAutoGenerated() then result = "generated:" else result = ""
1098+
if c.(SummarizedCallable).isAutoGenerated() then result = "generated:" else result = ""
10911099
}
10921100

10931101
/**
@@ -1117,19 +1125,21 @@ module Private {
11171125
*/
11181126
module RenderSummarizedCallable {
11191127
/** A summarized callable to include in the graph. */
1120-
abstract class RelevantSummarizedCallable extends SummarizedCallable { }
1128+
abstract class RelevantSummarizedCallable instanceof SummarizedCallable {
1129+
string toString() { result = super.toString() }
1130+
}
11211131

11221132
private newtype TNodeOrCall =
11231133
MkNode(Node n) {
11241134
exists(RelevantSummarizedCallable c |
11251135
n = summaryNode(c, _)
11261136
or
1127-
n.(ParamNode).isParameterOf(c, _)
1137+
n.(ParamNode).isParameterOf(inject(c), _)
11281138
)
11291139
} or
11301140
MkCall(DataFlowCall call) {
11311141
call = summaryDataFlowCall(_) and
1132-
call.getEnclosingCallable() instanceof RelevantSummarizedCallable
1142+
call.getEnclosingCallable() = inject(any(RelevantSummarizedCallable c))
11331143
}
11341144

11351145
private class NodeOrCall extends TNodeOrCall {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ private module FlowSummaries {
1414
private import semmle.code.java.dataflow.FlowSummary as F
1515
}
1616

17+
class SummarizedCallableBase = Callable;
18+
19+
DataFlowCallable inject(SummarizedCallable c) { result.asCallable() = c }
20+
1721
/** Gets the parameter position of the instance parameter. */
1822
int instanceParameterPosition() { result = -1 }
1923

@@ -28,7 +32,7 @@ DataFlowType getContentType(Content c) { result = c.getType() }
2832

2933
/** Gets the return type of kind `rk` for callable `c`. */
3034
DataFlowType getReturnType(SummarizedCallable c, ReturnKind rk) {
31-
result = getErasedRepr(c.asCallable().getReturnType()) and
35+
result = getErasedRepr(c.getReturnType()) and
3236
exists(rk)
3337
}
3438

@@ -56,14 +60,12 @@ DataFlowType getCallbackReturnType(DataFlowType t, ReturnKind rk) {
5660
* `input`, output specification `output`, kind `kind`, and a flag `generated`
5761
* stating whether the summary is autogenerated.
5862
*/
59-
predicate summaryElement(
60-
DataFlowCallable c, string input, string output, string kind, boolean generated
61-
) {
63+
predicate summaryElement(Callable c, string input, string output, string kind, boolean generated) {
6264
exists(
6365
string namespace, string type, boolean subtypes, string name, string signature, string ext
6466
|
6567
summaryModel(namespace, type, subtypes, name, signature, ext, input, output, kind, generated) and
66-
c.asCallable() = interpretElement(namespace, type, subtypes, name, signature, ext)
68+
c = interpretElement(namespace, type, subtypes, name, signature, ext)
6769
)
6870
}
6971

0 commit comments

Comments
 (0)