Skip to content

Commit ab5c1d6

Browse files
author
Benjamin Muskalla
committed
Rework filter to exclude simple constructors
1 parent 9ed14b4 commit ab5c1d6

File tree

9 files changed

+57
-67
lines changed

9 files changed

+57
-67
lines changed

java/ql/src/Telemetry/APIUsage.qll

Lines changed: 0 additions & 48 deletions
This file was deleted.

java/ql/src/Telemetry/ExternalAPI.qll

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
/** Provides classes and predicates related to handling APIs from external libraries. */
22

33
private import java
4-
private import APIUsage
4+
private import semmle.code.java.dataflow.DataFlow
5+
private import semmle.code.java.dataflow.DataFlow
56
private import semmle.code.java.dataflow.ExternalFlow
7+
private import semmle.code.java.dataflow.FlowSources
8+
private import semmle.code.java.dataflow.FlowSummary
9+
private import semmle.code.java.dataflow.internal.DataFlowPrivate
10+
private import semmle.code.java.dataflow.TaintTracking
611

712
/**
813
* An external API from either the Java Standard Library or a 3rd party library.
914
*/
1015
class ExternalAPI extends Callable {
1116
ExternalAPI() { not this.fromSource() }
1217

18+
/** Holds if this API is a candidate worth supporting */
19+
predicate isWorthSupporting() { not isTestLibrary() and not isParameterlessConstructor() }
20+
21+
/** Holds if this API is is a constructor without parameters */
22+
predicate isParameterlessConstructor() {
23+
this instanceof Constructor and this.getNumberOfParameters() = 0
24+
}
25+
1326
/** Holds if this API is part of a common testing library or framework */
14-
predicate isTestLibrary() { getDeclaringType() instanceof TestLibrary }
27+
private predicate isTestLibrary() { getDeclaringType() instanceof TestLibrary }
1528

1629
/**
1730
* Gets information about the external API in the form expected by the CSV modeling framework.
@@ -22,9 +35,6 @@ class ExternalAPI extends Callable {
2235
"#" + api.getName() + paramsString(api)
2336
}
2437

25-
/** Holds if this API is not yet supported by existing CodeQL libraries */
26-
predicate isSupported() { not supportKind(this) = "?" }
27-
2838
/**
2939
* Gets the jar file containing this API. Normalizes the Java Runtime to "rt.jar" despite the presence of modules.
3040
*/
@@ -33,6 +43,39 @@ class ExternalAPI extends Callable {
3343
private string containerAsJar(Container container) {
3444
if container instanceof JarFile then result = container.getBaseName() else result = "rt.jar"
3545
}
46+
47+
/** Gets a node that is an input to a call to this API. */
48+
private DataFlow::Node getAnInput() {
49+
exists(Call call | call.getCallee().getSourceDeclaration() = this |
50+
result.asExpr().(Argument).getCall() = call or
51+
result.(ArgumentNode).getCall() = call
52+
)
53+
}
54+
55+
/** Gets a node that is an output from a call to this API. */
56+
private DataFlow::Node getAnOutput() {
57+
exists(Call call | call.getCallee().getSourceDeclaration() = this |
58+
result.asExpr() = call or
59+
result.(DataFlow::PostUpdateNode).getPreUpdateNode().(ArgumentNode).getCall() = call
60+
)
61+
}
62+
63+
/** Holds if this API has a supported summary. */
64+
predicate hasSummary() {
65+
this instanceof SummarizedCallable or
66+
TaintTracking::localAdditionalTaintStep(this.getAnInput(), _)
67+
}
68+
69+
/** Holds if this API is a known source. */
70+
predicate isSource() {
71+
this.getAnOutput() instanceof RemoteFlowSource or sourceNode(this.getAnOutput(), _)
72+
}
73+
74+
/** Holds if this API is a known sink. */
75+
predicate isSink() { sinkNode(this.getAnInput(), _) }
76+
77+
/** Holds if this API is supported by existing CodeQL libraries, that is, it is either a recognized source or sink or has a flow summary. */
78+
predicate isSupported() { hasSummary() or isSource() or isSink() }
3679
}
3780

3881
private class TestLibrary extends RefType {

java/ql/src/Telemetry/ExternalLibraryUsage.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ where
1616
c.getCallee() = a and
1717
not c.getFile() instanceof GeneratedFile and
1818
a.jarContainer() = jarname and
19-
not a.isTestLibrary()
19+
a.isWorthSupporting()
2020
)
2121
select jarname, usages order by usages desc

java/ql/src/Telemetry/SupportedExternalSinks.ql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
*/
88

99
import java
10-
import APIUsage
1110
import ExternalAPI
1211
import semmle.code.java.GeneratedFiles
1312

1413
from ExternalAPI api, int usages
1514
where
16-
not api.isTestLibrary() and
17-
supportKind(api) = "sink" and
15+
api.isWorthSupporting() and
16+
api.isSink() and
1817
usages =
1918
strictcount(Call c |
2019
c.getCallee().getSourceDeclaration() = api and

java/ql/src/Telemetry/SupportedExternalSources.ql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
*/
88

99
import java
10-
import APIUsage
1110
import ExternalAPI
1211
import semmle.code.java.GeneratedFiles
1312

1413
from ExternalAPI api, int usages
1514
where
16-
not api.isTestLibrary() and
17-
supportKind(api) = "source" and
15+
api.isWorthSupporting() and
16+
api.isSource() and
1817
usages =
1918
strictcount(Call c |
2019
c.getCallee().getSourceDeclaration() = api and

java/ql/src/Telemetry/SupportedExternalTaint.ql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
*/
88

99
import java
10-
import APIUsage
1110
import ExternalAPI
1211
import semmle.code.java.GeneratedFiles
1312

1413
from ExternalAPI api, int usages
1514
where
16-
not api.isTestLibrary() and
17-
supportKind(api) = ["summary", "taint-preserving"] and
15+
api.isWorthSupporting() and
16+
api.hasSummary() and
1817
usages =
1918
strictcount(Call c |
2019
c.getCallee().getSourceDeclaration() = api and

java/ql/src/Telemetry/UnsupportedExternalAPIs.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
*/
88

99
import java
10-
import APIUsage
1110
import ExternalAPI
1211
import semmle.code.java.GeneratedFiles
1312

1413
from ExternalAPI api, int usages
1514
where
16-
not api.isTestLibrary() and
15+
api.isWorthSupporting() and
1716
not api.isSupported() and
1817
usages =
1918
strictcount(Call c |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| rt.jar | 3 |
1+
| rt.jar | 1 |
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
| java.io.PrintStream#println(Object) | 3 |
2-
| java.lang.Object#Object() | 2 |
32
| java.lang.String#length() | 1 |
43
| java.time.Duration#ofMillis(long) | 1 |

0 commit comments

Comments
 (0)