Skip to content

Commit 5a25984

Browse files
committed
Dataflow: Switch call context to a set representation.
1 parent 972b81b commit 5a25984

File tree

4 files changed

+361
-167
lines changed

4 files changed

+361
-167
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,21 @@ class CastNode extends ExprNode {
399399
}
400400
}
401401

402+
private predicate id_member(Member x, Member y) { x = y }
403+
404+
private predicate idOf_member(Member x, int y) = equivalenceRelation(id_member/2)(x, y)
405+
406+
private int summarizedCallableId(SummarizedCallable c) {
407+
c =
408+
rank[result](SummarizedCallable c0, int b, int i, string s |
409+
b = 0 and idOf_member(c0.asCallable(), i) and s = ""
410+
or
411+
b = 1 and i = 0 and s = c0.asSyntheticCallable()
412+
|
413+
c0 order by b, i, s
414+
)
415+
}
416+
402417
private newtype TDataFlowCallable =
403418
TSrcCallable(Callable c) or
404419
TSummarizedCallable(SummarizedCallable c) or
@@ -432,10 +447,28 @@ class DataFlowCallable extends TDataFlowCallable {
432447
result = this.asSummarizedCallable().getLocation() or
433448
result = this.asFieldScope().getLocation()
434449
}
450+
451+
/** Gets a best-effort total ordering. */
452+
int totalorder() {
453+
this =
454+
rank[result](DataFlowCallable c, int b, int i |
455+
b = 0 and idOf_member(c.asCallable(), i)
456+
or
457+
b = 1 and i = summarizedCallableId(c.asSummarizedCallable())
458+
or
459+
b = 2 and idOf_member(c.asFieldScope(), i)
460+
|
461+
c order by b, i
462+
)
463+
}
435464
}
436465

437466
class DataFlowExpr = Expr;
438467

468+
private predicate id_call(Call x, Call y) { x = y }
469+
470+
private predicate idOf_call(Call x, int y) = equivalenceRelation(id_call/2)(x, y)
471+
439472
private newtype TDataFlowCall =
440473
TCall(Call c) or
441474
TSummaryCall(SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver) {
@@ -468,6 +501,19 @@ class DataFlowCall extends TDataFlowCall {
468501
) {
469502
this.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
470503
}
504+
505+
/** Gets a best-effort total ordering. */
506+
int totalorder() {
507+
this =
508+
rank[result](DataFlowCall c, int b, int i |
509+
b = 0 and idOf_call(c.asCall(), i)
510+
or
511+
b = 1 and // not guaranteed to be total
512+
exists(SummarizedCallable sc | c = TSummaryCall(sc, _) and i = summarizedCallableId(sc))
513+
|
514+
c order by b, i
515+
)
516+
}
471517
}
472518

473519
/** A source call, that is, a `Call`. */

shared/dataflow/codeql/dataflow/DataFlow.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,17 @@ signature module InputSig<LocationSig Location> {
7373
string toString();
7474

7575
DataFlowCallable getEnclosingCallable();
76+
77+
/** Gets a best-effort total ordering. */
78+
int totalorder();
7679
}
7780

7881
class DataFlowCallable {
7982
/** Gets a textual representation of this element. */
8083
string toString();
84+
85+
/** Gets a best-effort total ordering. */
86+
int totalorder();
8187
}
8288

8389
class ReturnKind {

shared/dataflow/codeql/dataflow/internal/DataFlowImpl.qll

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,12 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
13691369

13701370
CcCall ccSomeCall();
13711371

1372+
predicate instanceofCc(Cc cc);
1373+
1374+
predicate instanceofCcCall(CcCall cc);
1375+
1376+
predicate instanceofCcNoCall(CcNoCall cc);
1377+
13721378
class LocalCc;
13731379

13741380
DataFlowCallable viableImplCallContextReduced(DataFlowCall call, CcCall ctx);
@@ -1386,8 +1392,8 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
13861392
bindingset[call, c]
13871393
CcNoCall getCallContextReturn(DataFlowCallable c, DataFlowCall call);
13881394

1389-
bindingset[c, cc]
1390-
LocalCc getLocalCc(DataFlowCallable c, Cc cc);
1395+
bindingset[cc]
1396+
LocalCc getLocalCc(Cc cc);
13911397

13921398
bindingset[node1, state1]
13931399
bindingset[node2, state2]
@@ -1476,7 +1482,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
14761482
or
14771483
exists(NodeEx mid, FlowState state0, Typ t0, LocalCc localCc |
14781484
fwdFlow(mid, state0, cc, summaryCtx, argT, argAp, t0, ap, apa) and
1479-
localCc = getLocalCc(mid.getEnclosingCallable(), cc)
1485+
localCc = getLocalCc(cc)
14801486
|
14811487
localStep(mid, state0, node, state, true, _, localCc) and
14821488
t = t0
@@ -1613,7 +1619,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
16131619
ApOption argAp, Typ t, Ap ap, boolean emptyAp, ApApprox apa, boolean cc
16141620
) {
16151621
fwdFlow(arg, state, outercc, summaryCtx, argT, argAp, t, ap, apa) and
1616-
(if outercc instanceof CcCall then cc = true else cc = false) and
1622+
(if instanceofCcCall(outercc) then cc = true else cc = false) and
16171623
if ap instanceof ApNil then emptyAp = true else emptyAp = false
16181624
}
16191625

@@ -1669,6 +1675,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
16691675
DataFlowCall call, ArgNodeEx arg, CcCall ctx
16701676
) {
16711677
callEdgeArgParamRestricted(call, _, arg, _, _, _) and
1678+
instanceofCcCall(ctx) and
16721679
result = viableImplCallContextReducedInlineLate(call, ctx)
16731680
}
16741681

@@ -1684,6 +1691,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
16841691
bindingset[call, ctx]
16851692
pragma[inline_late]
16861693
private predicate viableImplNotCallContextReducedInlineLate(DataFlowCall call, Cc ctx) {
1694+
instanceofCc(ctx) and
16871695
viableImplNotCallContextReduced(call, ctx)
16881696
}
16891697

@@ -1693,6 +1701,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
16931701
DataFlowCall call, ArgNodeEx arg, Cc outercc
16941702
) {
16951703
callEdgeArgParamRestricted(call, _, arg, _, _, _) and
1704+
instanceofCc(outercc) and
16961705
viableImplNotCallContextReducedInlineLate(call, outercc)
16971706
}
16981707

@@ -1837,6 +1846,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
18371846
RetNodeEx ret, FlowState state, CcNoCall cc, ParamNodeOption summaryCtx, TypOption argT,
18381847
ApOption argAp, Typ t, Ap ap, ApApprox apa
18391848
) {
1849+
instanceofCcNoCall(cc) and
18401850
fwdFlow(ret, state, cc, summaryCtx, argT, argAp, t, ap, apa)
18411851
}
18421852

@@ -1896,6 +1906,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
18961906

18971907
pragma[nomagic]
18981908
private predicate fwdFlow1Param(ParamNodeEx p, FlowState state, CcCall cc, Typ t0, Ap ap) {
1909+
instanceofCcCall(cc) and
18991910
fwdFlow1(p, state, cc, _, _, _, t0, _, ap, _)
19001911
}
19011912

@@ -1962,6 +1973,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
19621973
ApApprox argApa, Typ t, Ap ap, ApApprox apa
19631974
) {
19641975
exists(ReturnKindExt kind |
1976+
instanceofCcCall(ccc) and
19651977
fwdFlow(pragma[only_bind_into](ret), state, ccc,
19661978
TParamNodeSome(pragma[only_bind_into](summaryCtx.asNode())), TypOption::some(argT),
19671979
pragma[only_bind_into](apSome(argAp)), t, ap, pragma[only_bind_into](apa)) and
@@ -2527,10 +2539,16 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
25272539

25282540
CcCall ccSomeCall() { result = true }
25292541

2542+
predicate instanceofCc(Cc cc) { any() }
2543+
2544+
predicate instanceofCcCall(CcCall cc) { any() }
2545+
2546+
predicate instanceofCcNoCall(CcNoCall cc) { any() }
2547+
25302548
class LocalCc = Unit;
25312549

2532-
bindingset[c, cc]
2533-
LocalCc getLocalCc(DataFlowCallable c, Cc cc) { any() }
2550+
bindingset[cc]
2551+
LocalCc getLocalCc(Cc cc) { any() }
25342552

25352553
DataFlowCallable viableImplCallContextReduced(DataFlowCall call, CcCall ctx) { none() }
25362554

@@ -4081,7 +4099,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
40814099
state = mid.getState() and
40824100
cc = mid.getCallContext() and
40834101
sc = mid.getSummaryCtx() and
4084-
localCC = PrunedCallContextSensitivityStage5::getLocalCc(midnode.getEnclosingCallable(), cc) and
4102+
localCC = PrunedCallContextSensitivityStage5::getLocalCc(cc) and
40854103
t = mid.getType() and
40864104
ap = mid.getAp() and
40874105
summaryLabel = mid.getSummaryLabel()
@@ -5144,7 +5162,7 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
51445162
boolean isStoreStep
51455163
) {
51465164
not isUnreachableInCall1(node,
5147-
CachedCallContextSensitivity::LocalCallContext::getLocalCc(node.getEnclosingCallable(), cc)) and
5165+
CachedCallContextSensitivity::LocalCallContext::getLocalCc(cc)) and
51485166
(
51495167
localFlowStepEx(mid.getNodeEx(), node, _) and
51505168
state = mid.getState() and

0 commit comments

Comments
 (0)