Skip to content

Commit 990bdc2

Browse files
committed
Move value-preserving callable class into FlowSteps
1 parent da0a7f3 commit 990bdc2

File tree

3 files changed

+57
-56
lines changed

3 files changed

+57
-56
lines changed

java/ql/src/semmle/code/java/dataflow/FlowSteps.qll

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,60 @@ private module Frameworks {
2020
private import semmle.code.java.frameworks.ApacheHttp
2121
}
2222

23+
/**
24+
* A method or constructor that returns the exact value of one of its parameters or the qualifier.
25+
*
26+
* Extend this class and override `returnsValue` to add additional value-preserving steps through a
27+
* method that should be added to the basic local flow step relation.
28+
*
29+
* These steps will be visible for all global data-flow purposes, as well as via
30+
* `DataFlow::Node.getASuccessor` and other related functions exposing intraprocedural dataflow.
31+
*/
32+
abstract class ValuePreservingCallable extends Callable {
33+
/**
34+
* Holds if this callable returns precisely the value passed into argument `arg`.
35+
* `arg` is a parameter index, or is -1 to indicate the qualifier.
36+
*/
37+
abstract predicate returnsValue(int arg);
38+
}
39+
40+
/**
41+
* A method or constructor that returns the exact value of its qualifier (e.g., `return this;`)
42+
*
43+
* Extend this class and override `returnsValue` to add additional value-preserving steps through a
44+
* method that should be added to the basic local flow step relation.
45+
*
46+
* These steps will be visible for all global data-flow purposes, as well as via
47+
* `DataFlow::Node.getASuccessor` and other related functions exposing intraprocedural dataflow.
48+
*/
49+
abstract class FluentMethod extends ValuePreservingCallable {
50+
override predicate returnsValue(int arg) { arg = -1 }
51+
}
52+
53+
private class StandardLibraryValuePreservingCallable extends ValuePreservingCallable {
54+
int returnsArgNo;
55+
56+
StandardLibraryValuePreservingCallable() {
57+
this.getDeclaringType().hasQualifiedName("java.util", "Objects") and
58+
(
59+
this.hasName(["requireNonNull", "requireNonNullElseGet"]) and returnsArgNo = 0
60+
or
61+
this.hasName("requireNonNullElse") and returnsArgNo = [0 .. this.getNumberOfParameters() - 1]
62+
or
63+
this.hasName("toString") and returnsArgNo = 1
64+
)
65+
or
66+
this.getDeclaringType()
67+
.getSourceDeclaration()
68+
.getASourceSupertype*()
69+
.hasQualifiedName("java.util", "Stack") and
70+
this.hasName("push") and
71+
returnsArgNo = 0
72+
}
73+
74+
override predicate returnsValue(int argNo) { argNo = returnsArgNo }
75+
}
76+
2377
/**
2478
* A unit class for adding additional taint steps.
2579
*

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

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ private import semmle.code.java.dataflow.SSA
88
private import semmle.code.java.dataflow.TypeFlow
99
private import semmle.code.java.controlflow.Guards
1010
private import semmle.code.java.dataflow.ExternalFlow
11+
private import semmle.code.java.dataflow.FlowSteps
1112
import semmle.code.java.dataflow.InstanceAccess
1213

1314
cached
@@ -368,60 +369,6 @@ predicate hasNonlocalValue(FieldRead fr) {
368369
*/
369370
predicate localFlowStep(Node node1, Node node2) { simpleLocalFlowStep(node1, node2) }
370371

371-
/**
372-
* A method or constructor that returns the exact value of one of its parameters or the qualifier.
373-
*
374-
* Extend this class and override `returnsValue` to add additional value-preserving steps through a
375-
* method that should be added to the basic local flow step relation.
376-
*
377-
* These steps will be visible for all global data-flow purposes, as well as via
378-
* `DataFlow::Node.getASuccessor` and other related functions exposing intraprocedural dataflow.
379-
*/
380-
abstract class ValuePreservingCallable extends Callable {
381-
/**
382-
* Holds if this callable returns precisely the value passed into argument `arg`.
383-
* `arg` is a parameter index, or is -1 to indicate the qualifier.
384-
*/
385-
abstract predicate returnsValue(int arg);
386-
}
387-
388-
/**
389-
* A method or constructor that returns the exact value of its qualifier (e.g., `return this;`)
390-
*
391-
* Extend this class and override `returnsValue` to add additional value-preserving steps through a
392-
* method that should be added to the basic local flow step relation.
393-
*
394-
* These steps will be visible for all global data-flow purposes, as well as via
395-
* `DataFlow::Node.getASuccessor` and other related functions exposing intraprocedural dataflow.
396-
*/
397-
abstract class FluentMethod extends ValuePreservingCallable {
398-
override predicate returnsValue(int arg) { arg = -1 }
399-
}
400-
401-
private class StandardLibraryValuePreservingCallable extends ValuePreservingCallable {
402-
int returnsArgNo;
403-
404-
StandardLibraryValuePreservingCallable() {
405-
this.getDeclaringType().hasQualifiedName("java.util", "Objects") and
406-
(
407-
this.hasName(["requireNonNull", "requireNonNullElseGet"]) and returnsArgNo = 0
408-
or
409-
this.hasName("requireNonNullElse") and returnsArgNo = [0 .. this.getNumberOfParameters() - 1]
410-
or
411-
this.hasName("toString") and returnsArgNo = 1
412-
)
413-
or
414-
this.getDeclaringType()
415-
.getSourceDeclaration()
416-
.getASourceSupertype*()
417-
.hasQualifiedName("java.util", "Stack") and
418-
this.hasName("push") and
419-
returnsArgNo = 0
420-
}
421-
422-
override predicate returnsValue(int argNo) { argNo = returnsArgNo }
423-
}
424-
425372
/**
426373
* INTERNAL: do not use.
427374
*

java/ql/test/library-tests/dataflow/fluent-methods/flow.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class Conf extends DataFlow::Configuration {
1515
}
1616
}
1717

18-
class Model extends DataFlow::FluentMethod {
18+
class Model extends FluentMethod {
1919
Model() { this.getName() = "modelledFluentMethod" }
2020
}
2121

22-
class IdentityModel extends DataFlow::ValuePreservingCallable {
22+
class IdentityModel extends ValuePreservingCallable {
2323
IdentityModel() { this.getName() = "modelledIdentity" }
2424

2525
override predicate returnsValue(int arg) { arg = 0 }

0 commit comments

Comments
 (0)