Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 157f56f

Browse files
author
Benjamin Muskalla
committed
Capture model for defining interface
Instead of modeling individual implementations, take a more general approach of reuse dataflows for interfaces defined by a library. This allows tracking flows across all implementations and aligns better with how we manually model frameworks. This may have some FPs given all possible flows are modeled for a specific interface but also covers more scenarios where we don't know which implementation of an interface is used.
1 parent f36bb8b commit 157f56f

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

java/ql/src/utils/model-generator/ModelGeneratorUtils.qll

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,27 @@ string asSourceModel(Callable api, string output, string kind) {
6969
*/
7070
private string asPartialModel(Callable api) {
7171
result =
72-
api.getCompilationUnit().getPackage().getName() + ";" //
73-
+ api.getDeclaringType().nestedName() + ";" //
72+
asModelName(api) + ";" //
7473
+ isExtensible(api.getDeclaringType()).toString() + ";" //
7574
+ api.getName() + ";" //
7675
+ paramsString(api) + ";" //
7776
+ /* ext + */ ";" //
7877
}
7978

79+
/**
80+
* Returns the appropriate type name for the model. Either the type
81+
* declaring the method or the supertype introducing the method.
82+
*/
83+
private string asModelName(Callable api) {
84+
if api.(Method).getASourceOverriddenMethod().fromSource()
85+
then result = typeAsModel(api.(Method).getASourceOverriddenMethod().getDeclaringType())
86+
else result = typeAsModel(api.getDeclaringType())
87+
}
88+
89+
private string typeAsModel(RefType type) {
90+
result = type.getCompilationUnit().getPackage().getName() + ";" + type.nestedName()
91+
}
92+
8093
string parameterAccess(Parameter p) {
8194
if p.getType() instanceof Array
8295
then result = "ArrayElement of Argument[" + p.getPosition() + "]"

java/ql/test/utils/model-generator/CaptureSummaryModels.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
| p;Joiner;false;setEmptyValue;(CharSequence);;Argument[-1];ReturnValue;value; |
1818
| p;Joiner;false;setEmptyValue;(CharSequence);;Argument[0];Argument[-1];taint; |
1919
| p;Joiner;false;toString;();;Argument[-1];ReturnValue;taint; |
20+
| p;MultipleImpls$Strat2;true;getValue;();;Argument[-1];ReturnValue;taint; |
21+
| p;MultipleImpls$Strategy;true;doSomething;(String);;Argument[0];Argument[-1];taint; |
22+
| p;MultipleImpls$Strategy;true;doSomething;(String);;Argument[0];ReturnValue;taint; |
2023
| p;ParamFlow;true;addTo;(String,List);;Argument[0];Element of Argument[1];taint; |
2124
| p;ParamFlow;true;returnArrayElement;(String[]);;ArrayElement of Argument[0];ReturnValue;taint; |
2225
| p;ParamFlow;true;returnCollectionElement;(List);;Element of Argument[0];ReturnValue;taint; |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package p;
2+
3+
import java.util.concurrent.Callable;
4+
5+
public class MultipleImpls {
6+
7+
public static interface Strategy {
8+
String doSomething(String value);
9+
}
10+
11+
public static class Strat1 implements Strategy {
12+
public String doSomething(String value) {
13+
return value;
14+
}
15+
}
16+
17+
// implements in different library should not count as impl
18+
public static class Strat3 implements Callable<String> {
19+
20+
@Override
21+
public String call() throws Exception {
22+
return null;
23+
}
24+
25+
}
26+
public static class Strat2 implements Strategy {
27+
private String foo;
28+
29+
public String doSomething(String value) {
30+
this.foo = value;
31+
return "none";
32+
}
33+
34+
public String getValue() {
35+
return this.foo;
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)