Skip to content

Commit 9ef61bd

Browse files
criemenaschackmull
authored andcommitted
Address more parts of Anders review.
1 parent 812a0bc commit 9ef61bd

File tree

13 files changed

+185
-90
lines changed

13 files changed

+185
-90
lines changed

java/ql/src/semmle/code/java/dataflow/internal/DataFlowImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ private class PathNodeSink extends PathNode, TPathNodeSink {
17341734
* a callable is recorded by `cc`.
17351735
*/
17361736
private predicate pathStep(PathNodeMid mid, Node node, CallContext cc, AccessPath ap) {
1737-
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc) |
1737+
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc, node) |
17381738
localFlowBigStep(mid.getNode(), node, true, mid.getConfiguration(), localCC) and
17391739
cc = mid.getCallContext() and
17401740
ap = mid.getAp()

java/ql/src/semmle/code/java/dataflow/internal/DataFlowImpl2.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ private class PathNodeSink extends PathNode, TPathNodeSink {
17341734
* a callable is recorded by `cc`.
17351735
*/
17361736
private predicate pathStep(PathNodeMid mid, Node node, CallContext cc, AccessPath ap) {
1737-
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc) |
1737+
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc, node) |
17381738
localFlowBigStep(mid.getNode(), node, true, mid.getConfiguration(), localCC) and
17391739
cc = mid.getCallContext() and
17401740
ap = mid.getAp()

java/ql/src/semmle/code/java/dataflow/internal/DataFlowImpl3.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ private class PathNodeSink extends PathNode, TPathNodeSink {
17341734
* a callable is recorded by `cc`.
17351735
*/
17361736
private predicate pathStep(PathNodeMid mid, Node node, CallContext cc, AccessPath ap) {
1737-
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc) |
1737+
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc, node) |
17381738
localFlowBigStep(mid.getNode(), node, true, mid.getConfiguration(), localCC) and
17391739
cc = mid.getCallContext() and
17401740
ap = mid.getAp()

java/ql/src/semmle/code/java/dataflow/internal/DataFlowImpl4.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ private class PathNodeSink extends PathNode, TPathNodeSink {
17341734
* a callable is recorded by `cc`.
17351735
*/
17361736
private predicate pathStep(PathNodeMid mid, Node node, CallContext cc, AccessPath ap) {
1737-
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc) |
1737+
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc, node) |
17381738
localFlowBigStep(mid.getNode(), node, true, mid.getConfiguration(), localCC) and
17391739
cc = mid.getCallContext() and
17401740
ap = mid.getAp()

java/ql/src/semmle/code/java/dataflow/internal/DataFlowImpl5.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ private class PathNodeSink extends PathNode, TPathNodeSink {
17341734
* a callable is recorded by `cc`.
17351735
*/
17361736
private predicate pathStep(PathNodeMid mid, Node node, CallContext cc, AccessPath ap) {
1737-
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc) |
1737+
exists(LocalCallContext localCC | localCC = getMatchingLocalCallContext(cc, node) |
17381738
localFlowBigStep(mid.getNode(), node, true, mid.getConfiguration(), localCC) and
17391739
cc = mid.getCallContext() and
17401740
ap = mid.getAp()

java/ql/src/semmle/code/java/dataflow/internal/DataFlowImplCommon.qll

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,16 @@ private module ImplCommon {
120120
int i, ArgumentNode arg, CallContext outercc, DataFlowCall call
121121
) {
122122
exists(DataFlowCallable c | argumentOf(call, i, arg, c) |
123+
(
123124
outercc = TAnyCallContext()
124125
or
125126
outercc = TSomeCall(getAParameter(c), _)
126127
or
127128
exists(DataFlowCall other | outercc = TSpecificCall(other, _, _) |
128129
recordDataFlowCallSite(other, c)
129130
)
131+
) and
132+
not isUnreachableInCall(arg, outercc.(CallContextSpecificCall).getCall())
130133
)
131134
}
132135

@@ -180,14 +183,16 @@ private module ImplCommon {
180183
exists(Node mid |
181184
parameterValueFlow(p, mid, cc) and
182185
step(mid, node) and
183-
compatibleTypes(p.getType(), node.getType())
186+
compatibleTypes(p.getType(), node.getType()) and
187+
not isUnreachableInCall(node, cc.(CallContextSpecificCall).getCall())
184188
)
185189
or
186190
// flow through a callable
187191
exists(Node arg |
188192
parameterValueFlow(p, arg, cc) and
189193
argumentValueFlowsThrough(arg, node, cc) and
190-
compatibleTypes(p.getType(), node.getType())
194+
compatibleTypes(p.getType(), node.getType()) and
195+
not isUnreachableInCall(node, cc.(CallContextSpecificCall).getCall())
191196
)
192197
}
193198

@@ -220,6 +225,7 @@ private module ImplCommon {
220225
argumentValueFlowsThrough0(call, arg, kind, cc)
221226
|
222227
out = getAnOutNode(call, kind) and
228+
not isUnreachableInCall(out, cc.(CallContextSpecificCall).getCall()) and
223229
compatibleTypes(arg.getType(), out.getType())
224230
)
225231
}
@@ -675,16 +681,14 @@ private module ImplCommon {
675681
abstract predicate validFor(Node n);
676682
}
677683

678-
LocalCallContext getMatchingLocalCallContext(CallContext ctx) {
679-
(
680-
not ctx instanceof CallContextSpecificCall or
681-
not exists(TSpecificLocalCall(ctx.(CallContextSpecificCall).getCall()))
682-
) and
683-
exists(LocalCallContextAny l | result = l)
684-
or
685-
exists(LocalCallContextSpecificCall l |
686-
ctx.(CallContextSpecificCall).getCall() = l.getCall() and result = l
687-
)
684+
/**
685+
* Gets a matching local call context given the call context and a node which is in
686+
* the callable the call is targeting.
687+
*/
688+
LocalCallContext getMatchingLocalCallContext(CallContext ctx, Node n) {
689+
if hasUnreachableNode(ctx.(CallContextSpecificCall).getCall(), n.getEnclosingCallable())
690+
then result.(LocalCallContextSpecificCall).getCall() = ctx.(CallContextSpecificCall).getCall()
691+
else result instanceof LocalCallContextAny
688692
}
689693

690694
class LocalCallContextAny extends LocalCallContext, TAnyLocalCall {

java/ql/test/library-tests/dataflow/call-sensitivity/A.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ public class A {
33
public static void sink(Object o) {
44
}
55

6+
public Object flowThrough(Object o, boolean cond) {
7+
if (cond) {
8+
return o;
9+
} else {
10+
return null;
11+
}
12+
}
13+
614
public void callSinkIfTrue(Object o, boolean cond) {
715
if (cond) {
816
sink(o);
@@ -49,6 +57,7 @@ public void f1() {
4957
callSinkIfFalse(new Integer(2), true);
5058
callSinkFromLoop(new Integer(3), false);
5159
localCallSensitivity(new Integer(4), false);
60+
sink(flowThrough(new Integer(4), false));
5261
// should exhibit flow
5362
callSinkIfTrue(new Integer(1), true);
5463
callSinkIfFalse(new Integer(2), false);
@@ -57,6 +66,7 @@ public void f1() {
5766
localCallSensitivity2(new Integer(4), true, true);
5867
localCallSensitivity2(new Integer(4), false, true);
5968
localCallSensitivity2(new Integer(4), true, false);
69+
sink(flowThrough(new Integer(4), true));
6070
// expected false positive
6171
localCallSensitivity2(new Integer(4), false, false);
6272
}
@@ -69,11 +79,13 @@ public void f2() {
6979
callSinkIfFalse(new Integer(5), t);
7080
callSinkFromLoop(new Integer(6), f);
7181
localCallSensitivity(new Integer(4), f);
82+
sink(flowThrough(new Integer(4), f));
7283
// should exhibit flow
7384
callSinkIfTrue(new Integer(4), t);
7485
callSinkIfFalse(new Integer(5), f);
7586
callSinkFromLoop(new Integer(6), t);
7687
localCallSensitivity(new Integer(4), t);
88+
sink(flowThrough(new Integer(4), t));
7789
}
7890

7991
public void f3(InterfaceA b) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
public class A2 {
2+
3+
public static void sink(Object o) {
4+
}
5+
6+
public void m() {
7+
8+
}
9+
10+
public void callsite(InterfaceB intF) {
11+
B b = new B();
12+
// in both possible implementations of foo, this callsite is relevant
13+
// in IntA, it improves virtual dispatch,
14+
// and in IntB, it improves the dataflow analysis.
15+
intF.foo(b, new Integer(5), false);
16+
}
17+
18+
private class B extends A2 {
19+
@Override
20+
public void m() {
21+
22+
}
23+
}
24+
25+
private class IntA implements InterfaceB {
26+
@Override
27+
public void foo(A2 obj, Object o, boolean cond) {
28+
obj.m();
29+
sink(o);
30+
}
31+
}
32+
33+
private class IntB implements InterfaceB {
34+
@Override
35+
public void foo(A2 obj, Object o, boolean cond) {
36+
if (cond) {
37+
sink(o);
38+
}
39+
}
40+
}
41+
42+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface InterfaceB {
2+
public void foo(A2 a, Object o, boolean cond);
3+
}

0 commit comments

Comments
 (0)