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

Skip to content

Commit 1001dd8

Browse files
committed
Java: Switch array steps and one containerstep.
1 parent ce509eb commit 1001dd8

5 files changed

Lines changed: 60 additions & 24 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import java
22
import semmle.code.java.Collections
33
import semmle.code.java.Maps
4+
private import semmle.code.java.dataflow.SSA
5+
private import DataFlowUtil
46

57
private class EntryType extends RefType {
68
EntryType() {
@@ -426,3 +428,44 @@ predicate containerStep(Expr n1, Expr n2) {
426428
containerReturnValueStep(n1, n2) or
427429
containerUpdateStep(n1, n2)
428430
}
431+
432+
predicate arrayStoreStep(Node node1, Node node2) {
433+
exists(Argument arg |
434+
node1.asExpr() = arg and
435+
arg.isVararg() and
436+
node2.(ImplicitVarargsArray).getCall() = arg.getCall()
437+
)
438+
or
439+
node2.asExpr().(ArrayInit).getAnInit() = node1.asExpr()
440+
or
441+
exists(Assignment assign | assign.getSource() = node1.asExpr() |
442+
node2.(PostUpdateNode).getPreUpdateNode().asExpr() = assign.getDest().(ArrayAccess).getArray()
443+
)
444+
}
445+
446+
private predicate enhancedForStmtStep(Node node1, Node node2, Type containerType) {
447+
exists(EnhancedForStmt for, Expr e, SsaExplicitUpdate v |
448+
for.getExpr() = e and
449+
node1.asExpr() = e and
450+
containerType = e.getType() and
451+
v.getDefiningExpr() = for.getVariable() and
452+
v.getAFirstUse() = node2.asExpr()
453+
)
454+
}
455+
456+
predicate arrayReadStep(Node node1, Node node2, Type elemType) {
457+
exists(ArrayAccess aa |
458+
aa.getArray() = node1.asExpr() and
459+
aa.getType() = elemType and
460+
node2.asExpr() = aa
461+
)
462+
or
463+
exists(Array arr |
464+
enhancedForStmtStep(node1, node2, arr) and
465+
arr.getComponentType() = elemType
466+
)
467+
}
468+
469+
predicate collectionReadStep(Node node1, Node node2) {
470+
enhancedForStmtStep(node1, node2, any(Type t | not t instanceof Array))
471+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import DataFlowImplCommon
44
private import DataFlowDispatch
55
private import semmle.code.java.controlflow.Guards
66
private import semmle.code.java.dataflow.SSA
7+
private import ContainerFlow
78
private import FlowSummaryImpl as FlowSummaryImpl
89
import DataFlowNodes::Private
910

@@ -137,13 +138,15 @@ class MapValueContent extends Content, TMapValueContent {
137138
* Thus, `node2` references an object with a field `f` that contains the
138139
* value of `node1`.
139140
*/
140-
predicate storeStep(Node node1, Content f, PostUpdateNode node2) {
141+
predicate storeStep(Node node1, Content f, Node node2) {
141142
exists(FieldAccess fa |
142143
instanceFieldAssign(node1.asExpr(), fa) and
143-
node2.getPreUpdateNode() = getFieldQualifier(fa) and
144+
node2.(PostUpdateNode).getPreUpdateNode() = getFieldQualifier(fa) and
144145
f.(FieldContent).getField() = fa.getField()
145146
)
146147
or
148+
f instanceof ArrayContent and arrayStoreStep(node1, node2)
149+
or
147150
FlowSummaryImpl::Private::Steps::summaryStoreStep(node1, f, node2)
148151
}
149152

@@ -171,6 +174,10 @@ predicate readStep(Node node1, Content f, Node node2) {
171174
node2.asExpr() = get
172175
)
173176
or
177+
f instanceof ArrayContent and arrayReadStep(node1, node2, _)
178+
or
179+
f instanceof CollectionContent and collectionReadStep(node1, node2)
180+
or
174181
FlowSummaryImpl::Private::Steps::summaryReadStep(node1, f, node2)
175182
}
176183

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ predicate simpleLocalFlowStep(Node node1, Node node2) {
144144
or
145145
node2.asExpr().(AssignExpr).getSource() = node1.asExpr()
146146
or
147+
node2.asExpr().(ArrayCreationExpr).getInit() = node1.asExpr()
148+
or
147149
exists(MethodAccess ma, ValuePreservingMethod m, int argNo |
148150
ma.getCallee().getSourceDeclaration() = m and m.returnsValue(argNo)
149151
|

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ DataFlowType getContentType(Content c) {
3030
or
3131
c instanceof ArrayContent and
3232
result instanceof TypeObject
33+
or
34+
c instanceof MapKeyContent and
35+
result instanceof TypeObject
36+
or
37+
c instanceof MapValueContent and
38+
result instanceof TypeObject
3339
}
3440

3541
/** Gets the return type of kind `rk` for callable `c`. */

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ private module Cached {
6060
localAdditionalTaintUpdateStep(src.asExpr(),
6161
sink.(DataFlow::PostUpdateNode).getPreUpdateNode().asExpr())
6262
or
63-
exists(Argument arg |
64-
src.asExpr() = arg and
65-
arg.isVararg() and
66-
sink.(DataFlow::ImplicitVarargsArray).getCall() = arg.getCall()
67-
)
68-
or
6963
FlowSummaryImpl::Private::Steps::summaryLocalStep(src, sink, false)
7064
}
7165

@@ -103,20 +97,8 @@ private predicate localAdditionalTaintExprStep(Expr src, Expr sink) {
10397
or
10498
sink.(AssignAddExpr).getSource() = src and sink.getType() instanceof TypeString
10599
or
106-
sink.(ArrayCreationExpr).getInit() = src
107-
or
108-
sink.(ArrayInit).getAnInit() = src
109-
or
110-
sink.(ArrayAccess).getArray() = src
111-
or
112100
sink.(LogicExpr).getAnOperand() = src
113101
or
114-
exists(EnhancedForStmt for, SsaExplicitUpdate v |
115-
for.getExpr() = src and
116-
v.getDefiningExpr() = for.getVariable() and
117-
v.getAFirstUse() = sink
118-
)
119-
or
120102
containerReturnValueStep(src, sink)
121103
or
122104
constructorStep(src, sink)
@@ -141,10 +123,6 @@ private predicate localAdditionalTaintExprStep(Expr src, Expr sink) {
141123
* This is restricted to cases where the step updates the value of `sink`.
142124
*/
143125
private predicate localAdditionalTaintUpdateStep(Expr src, Expr sink) {
144-
exists(Assignment assign | assign.getSource() = src |
145-
sink = assign.getDest().(ArrayAccess).getArray()
146-
)
147-
or
148126
containerUpdateStep(src, sink)
149127
or
150128
qualifierToArgumentStep(src, sink)

0 commit comments

Comments
 (0)