Skip to content

Commit f9c8926

Browse files
committed
Swift: Fill in some easy TODOs in 'FlowSummaryImplSpecific' and implement a source model for 'String(contentsOf:)'.
1 parent 184371f commit f9c8926

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ class ParameterPosition extends TParameterPosition {
181181

182182
class PositionalParameterPosition extends ParameterPosition, TPositionalParameter {
183183
int getIndex() { this = TPositionalParameter(result) }
184+
185+
override string toString() { result = this.getIndex().toString() }
186+
}
187+
188+
class ThisParameterPosition extends ParameterPosition, TThisParameter {
189+
override string toString() { result = "this" }
184190
}
185191

186192
/** An argument position. */
@@ -191,6 +197,12 @@ class ArgumentPosition extends TArgumentPosition {
191197

192198
class PositionalArgumentPosition extends ArgumentPosition, TPositionalArgument {
193199
int getIndex() { this = TPositionalArgument(result) }
200+
201+
override string toString() { result = this.getIndex().toString() }
202+
}
203+
204+
class ThisArgumentPosition extends ArgumentPosition, TThisArgument {
205+
override string toString() { result = "this" }
194206
}
195207

196208
/** Holds if arguments at position `apos` match parameters at position `ppos`. */

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SummarizedCallableBase = AbstractFunctionDecl;
1717
DataFlowCallable inject(SummarizedCallable c) { result = TDataFlowFunc(c) }
1818

1919
/** Gets the parameter position of the instance parameter. */
20-
ArgumentPosition instanceParameterPosition() { none() } // disables implicit summary flow to `this` for callbacks
20+
ArgumentPosition instanceParameterPosition() { result instanceof ThisArgumentPosition }
2121

2222
/** Gets the synthesized summary data-flow node for the given values. */
2323
Node summaryNode(SummarizedCallable c, SummaryNodeState state) { result = TSummaryNode(c, state) }
@@ -31,23 +31,23 @@ DataFlowType getContentType(Content c) { any() }
3131
/** Gets the return type of kind `rk` for callable `c`. */
3232
bindingset[c]
3333
DataFlowType getReturnType(SummarizedCallable c, ReturnKind rk) {
34-
any() // TODO
34+
any() // TODO once we have type pruning
3535
}
3636

3737
/**
3838
* Gets the type of the parameter matching arguments at position `pos` in a
3939
* synthesized call that targets a callback of type `t`.
4040
*/
4141
DataFlowType getCallbackParameterType(DataFlowType t, ArgumentPosition pos) {
42-
none() // TODO
42+
any() // TODO once we have type pruning
4343
}
4444

4545
/**
4646
* Gets the return type of kind `rk` in a synthesized call that targets a
4747
* callback of type `t`.
4848
*/
4949
DataFlowType getCallbackReturnType(DataFlowType t, ReturnKind rk) {
50-
none() // TODO
50+
any() // TODO once we have type pruning
5151
}
5252

5353
/**
@@ -97,12 +97,12 @@ predicate sinkElement(Element e, string input, string kind, boolean generated) {
9797
/** Gets the summary component for specification component `c`, if any. */
9898
bindingset[c]
9999
SummaryComponent interpretComponentSpecific(AccessPathToken c) {
100-
none() // TODO
100+
none() // TODO once we have field flow
101101
}
102102

103103
/** Gets the textual representation of the content in the format used for flow summaries. */
104104
private string getContentSpecificCsv(Content c) {
105-
none() // TODO
105+
none() // TODO once we have field flow
106106
}
107107

108108
/** Gets the textual representation of a summary component in the format used for flow summaries. */
@@ -117,14 +117,10 @@ string getComponentSpecificCsv(SummaryComponent sc) {
117117
}
118118

119119
/** Gets the textual representation of a parameter position in the format used for flow summaries. */
120-
string getParameterPositionCsv(ParameterPosition pos) {
121-
none() // TODO
122-
}
120+
string getParameterPositionCsv(ParameterPosition pos) { result = pos.toString() }
123121

124122
/** Gets the textual representation of an argument position in the format used for flow summaries. */
125-
string getArgumentPositionCsv(ArgumentPosition pos) {
126-
none() // TODO
127-
}
123+
string getArgumentPositionCsv(ArgumentPosition pos) { result = pos.toString() }
128124

129125
/** Holds if input specification component `c` needs a reference. */
130126
predicate inputNeedsReferenceSpecific(string c) { none() }
@@ -187,11 +183,21 @@ predicate interpretInputSpecific(string c, InterpretNode mid, InterpretNode n) {
187183
/** Gets the argument position obtained by parsing `X` in `Parameter[X]`. */
188184
bindingset[s]
189185
ArgumentPosition parseParamBody(string s) {
190-
none() // TODO
186+
exists(int index | index = AccessPath::parseInt(s) |
187+
result.(PositionalArgumentPosition).getIndex() = index
188+
or
189+
index = -1 and
190+
result instanceof ThisArgumentPosition
191+
)
191192
}
192193

193194
/** Gets the parameter position obtained by parsing `X` in `Argument[X]`. */
194195
bindingset[s]
195196
ParameterPosition parseArgBody(string s) {
196-
none() // TODO
197+
exists(int index | index = AccessPath::parseInt(s) |
198+
result.(PositionalParameterPosition).getIndex() = index
199+
or
200+
index = -1 and
201+
result instanceof ThisParameterPosition
202+
)
197203
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import swift
2+
private import codeql.swift.dataflow.ExternalFlow
3+
4+
private class StringSource extends SourceModelCsv {
5+
override predicate row(string row) {
6+
row =
7+
[
8+
// String(contentsOf:) is a remote flow source
9+
";String;true;init(contentsOf:);(URL);;ReturnValue;remote"
10+
]
11+
}
12+
}

0 commit comments

Comments
 (0)