Skip to content

Commit 9dbf7e8

Browse files
d10cMathiasVP
authored andcommitted
Swift: align definition of InputSig slightly closer to Java version
Though there is a regression in the tests, so more work is needed.
1 parent 9de3cc7 commit 9dbf7e8

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowPrivate.qll

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -843,17 +843,20 @@ private module CaptureInput implements VariableCapture::InputSig {
843843
BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.(B::BasicBlock).getASuccessor() }
844844

845845
//TODO: support capture of `this` in lambdas
846-
class CapturedVariable instanceof S::CapturedDecl {
846+
class CapturedVariable instanceof S::VarDecl {
847+
CapturedVariable() {
848+
any(S::CapturedDecl capturedDecl).getDecl() = this and
849+
exists(this.getEnclosingCallable())
850+
}
851+
847852
string toString() { result = super.toString() }
848853

849-
Callable getCallable() { result = super.getScope() }
854+
Callable getCallable() { result = super.getEnclosingCallable() }
850855

851856
Location getLocation() { result = super.getLocation() }
852857
}
853858

854-
class CapturedParameter extends CapturedVariable {
855-
CapturedParameter() { this.(S::CapturedDecl).getDecl() instanceof S::ParamDecl }
856-
}
859+
class CapturedParameter extends CapturedVariable instanceof S::ParamDecl { }
857860

858861
class Expr instanceof S::AstNode {
859862
string toString() { result = super.toString() }
@@ -870,23 +873,16 @@ private module CaptureInput implements VariableCapture::InputSig {
870873
Expr source;
871874

872875
VariableWrite() {
873-
exists(S::VarDecl varDecl |
874-
variable.(S::CapturedDecl).getDecl() = varDecl and
875-
variable.getCallable() = this.(S::AstNode).getEnclosingCallable()
876-
|
877-
exists(S::Assignment a | this = a |
878-
a.getDest().(DeclRefExpr).getDecl() = varDecl and
879-
source = a.getSource()
880-
)
881-
or
882-
exists(S::PatternBindingDecl pbd, S::NamedPattern np |
883-
this = pbd and pbd.getAPattern() = np
884-
|
885-
np.getVarDecl() = varDecl and
886-
source = np.getMatchingExpr()
887-
)
888-
// TODO: support multiple variables in LHS of =, in both of above cases.
876+
exists(S::Assignment a | this = a |
877+
a.getDest().(DeclRefExpr).getDecl() = variable and
878+
source = a.getSource()
879+
)
880+
or
881+
exists(S::PatternBindingDecl pbd, S::NamedPattern np | this = pbd and pbd.getAPattern() = np |
882+
np.getVarDecl() = variable and
883+
source = np.getMatchingExpr()
889884
)
885+
// TODO: support multiple variables in LHS of =, in both of above cases.
890886
}
891887

892888
CapturedVariable getVariable() { result = variable }
@@ -897,7 +893,7 @@ private module CaptureInput implements VariableCapture::InputSig {
897893
class VariableRead extends Expr instanceof S::DeclRefExpr {
898894
CapturedVariable v;
899895

900-
VariableRead() { this.getCapturedDecl() = v /* TODO: this should be an R-value only. */ }
896+
VariableRead() { this.getDecl() = v /* TODO: this should be an R-value only. */ }
901897

902898
CapturedVariable getVariable() { result = v }
903899
}
@@ -926,7 +922,7 @@ private CaptureFlow::ClosureNode asClosureNode(Node n) {
926922
result.(CaptureFlow::ExprNode).getExpr() = n.asExpr() or
927923
result.(CaptureFlow::ExprPostUpdateNode).getExpr() =
928924
n.(PostUpdateNode).getPreUpdateNode().asExpr() or
929-
result.(CaptureFlow::ParameterNode).getParameter().(CapturedDecl).getDecl() = n.getParameter() or
925+
result.(CaptureFlow::ParameterNode).getParameter() = n.getParameter() or
930926
result.(CaptureFlow::ThisParameterNode).getCallable().getSelfParam() = n.getParameter() or
931927
result.(CaptureFlow::MallocNode).getClosureExpr() = n.getCfgNode().getNode().asAstNode() // TODO: figure out why the java version had PostUpdateNode logic here
932928
}

swift/ql/test/library-tests/dataflow/capture/closures.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ func captureList() {
1515
}()
1616
}
1717

18+
func setAndCallEscape() {
19+
let x = source("setAndCallEscape", 0)
20+
21+
let escape = {
22+
sink(x) // $ MISSING: hasValueFlow=setAndCallEscape
23+
return x + 1
24+
}
25+
26+
sink(escape()) // $ MISSING: hasTaintFlow=setAndCallEscape
27+
}
28+
1829
var escape: (() -> Int)? = nil
1930

2031
func setEscape() {
@@ -132,7 +143,7 @@ func taintCollections(array: inout Array<Int>) {
132143
array.withContiguousStorageIfAvailable({
133144
buffer in
134145
sink(array)
135-
sink(array[0]) // $ hasValueFlow=array
146+
sink(array[0]) // $ MISSING: hasValueFlow=array
136147
})
137148
}
138149

0 commit comments

Comments
 (0)