Skip to content

Commit 63904ba

Browse files
aschackmullCopilot
andauthored
Unified: Improve Swift guard reasoning
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 84c8b2a commit 63904ba

3 files changed

Lines changed: 190 additions & 18 deletions

File tree

‎unified/ql/lib/codeql/unified/internal/controlflow/Guards.qll‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ private import unified as U
1010
private import codeql.controlflow.Guards as SharedGuards
1111
private import codeql.controlflow.SuccessorType
1212
private import codeql.unified.internal.typeinference.TypeInference as T
13+
private import codeql.unified.internal.typeinference.TypeInferencePluginSwift as SwiftTypes
14+
private import codeql.unified.internal.typeinference.TypeMention as TM
1315
private import codeql.unified.internal.ExprPositions as EP
1416

1517
private module GuardsInput implements SharedGuards::InputSig<Location, ControlFlowNode, BasicBlock> {
@@ -68,7 +70,23 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
6870

6971
class NonNullExpr extends Expr {
7072
NonNullExpr() {
71-
none() // TODO
73+
this instanceof U::BooleanLiteral
74+
or
75+
this instanceof U::IntLiteral
76+
or
77+
this instanceof U::FloatLiteral
78+
or
79+
this instanceof U::StringLiteral
80+
or
81+
// Explicit or inherited Swift initializers may be failable.
82+
exists(CallExpr call, ConstructorDeclaration constructor, ClassLikeDeclaration cls |
83+
call = this and
84+
constructor = unique(Callable c | c = T::resolveCallTarget(call)) and
85+
constructor.hasModifier("generated") and
86+
cls.getAMember() = constructor and
87+
cls.hasModifier("class") and
88+
not exists(cls.getABaseType())
89+
)
7290
}
7391
}
7492

@@ -131,10 +149,17 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
131149
NonOverridableMethod() {
132150
// TODO: The concept of non-overridability likely belongs in a language plugin
133151
// TODO: This logic isn't quite right, but just a placeholder for now
134-
this instanceof ConstructorDeclaration or
135-
this.(FunctionDeclaration).hasModifier("static") or
136-
this.(FunctionDeclaration).hasModifier("private") or
137-
this.(FunctionDeclaration).hasModifier("final")
152+
// Swift `dynamic` functions can be replaced at runtime.
153+
not this.hasModifier("dynamic") and
154+
(
155+
this instanceof ConstructorDeclaration or
156+
this.(FunctionDeclaration).hasModifier("static") or
157+
this.(FunctionDeclaration).hasModifier("private") or
158+
this.(FunctionDeclaration).hasModifier("final") or
159+
this instanceof LocalFunctionDeclaration or
160+
// Top-level functions are classified as static members, not local functions.
161+
this.(FunctionDeclaration) instanceof TopLevelStmt
162+
)
138163
}
139164

140165
Parameter getParameter(ParameterPosition ppos) {
@@ -173,6 +198,22 @@ private module GuardsImpl = SharedGuards::Make<Location, Cfg, GuardsInput>;
173198

174199
private module LogicInput_v1 implements GuardsImpl::LogicInputSig {
175200
import Ssa
201+
202+
predicate additionalNullCheck(
203+
GuardsImpl::PreGuard guard, GuardsImpl::GuardValue val, Expr e, boolean isNull
204+
) {
205+
exists(TypeTestExpr test, T::Type testedType |
206+
guard = test and
207+
test.getOperator().getValue() = "is" and
208+
e = test.getExpr() and
209+
testedType = unique(T::Type t | t = test.getType().(TM::TypeMention).getType()) and
210+
// In Swift, `nil is Any` and `nil is T?` can both be true.
211+
testedType.asClass().hasModifier(["class", "actor", "struct", "enum"]) and
212+
not testedType instanceof SwiftTypes::OptionalType and
213+
val.asBooleanValue() = true and
214+
isNull = false
215+
)
216+
}
176217
}
177218

178219
class GuardValue = GuardsImpl::GuardValue;

‎unified/ql/test/library-tests/guards/Guards.swift‎

Lines changed: 105 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func t1(_ a: [Int]?, _ s0: String) {
2828
if b != false {
2929
chk() // $ guarded=IfExpr:true guarded='b != false:true' guarded=b:true
3030
} else {
31-
chk() // $ guarded=IfExpr:false guarded='b != false:false' guarded=b:false guarded=g(1):true guarded=g(2):false
31+
chk() // $ guarded=IfExpr:false guarded='IfExpr:not null' guarded='b != false:false' guarded=b:false guarded='b:not null' guarded=g(1):true guarded=g(2):false
3232
}
3333
let sz = a != nil ? a!.count : 0
3434
var i = 0
@@ -54,14 +54,14 @@ func t1(_ a: [Int]?, _ s0: String) {
5454

5555
let o: Any? = g(4) ? nil : s
5656
if o is String {
57-
chk() // $ guarded=TypeTestExpr:true MISSING: guarded='o:not null' guarded='IfExpr:not null' guarded=g(4):false guarded='s:not null'
57+
chk() // $ guarded=TypeTestExpr:true guarded='o:not null' guarded='IfExpr:not null' guarded=g(4):false guarded='s:not null'
5858
}
5959
}
6060

6161
func t2() throws {
6262
try checkTrue(g(1), "A")
6363
try checkFalse(g(2), "B")
64-
chk() // $ guarded='checkTrue(...):no exception' guarded='checkFalse(...):no exception' SPURIOUS: guarded='g(...):no exception' MISSING: guarded=g(1):true guarded=g(2):false
64+
chk() // $ guarded='checkTrue(...):no exception' guarded='checkFalse(...):no exception' guarded=g(1):true guarded=g(2):false SPURIOUS: guarded='g(...):no exception'
6565
}
6666

6767
func t3() {
@@ -106,9 +106,9 @@ func t4() {
106106
}
107107
let o: C? = g(4) ? C() : nil
108108
if o == nil {
109-
chk() // $ guarded='o == nil:true' guarded='o:null' guarded='IfExpr:null' MISSING: guarded=g(4):false
109+
chk() // $ guarded='o == nil:true' guarded='o:null' guarded='IfExpr:null' guarded=g(4):false
110110
} else {
111-
chk() // $ guarded='o == nil:false' guarded='o:not null' guarded='IfExpr:not null' guarded='C(...):not null' guarded=g(4):true
111+
chk() // $ guarded='o == nil:false' guarded='o:not null' guarded='IfExpr:not null' guarded=g(4):true
112112
}
113113
}
114114

@@ -129,9 +129,9 @@ func t6() {
129129
if g(2) { }
130130
}
131131
if o != nil {
132-
chk() // $ guarded='o != nil:true' guarded='o:not null' guarded='C(...):not null' guarded=g(1):true
132+
chk() // $ guarded='o != nil:true' guarded='o:not null' guarded=g(1):true
133133
} else {
134-
chk() // $ guarded='o != nil:false' guarded='o:null' MISSING: guarded=g(1):false
134+
chk() // $ guarded='o != nil:false' guarded='o:null' guarded=g(1):false
135135
}
136136
}
137137

@@ -185,21 +185,21 @@ func testWrappers(_ s: String?, _ i: Int?) {
185185
}
186186

187187
if testNotNull2(s) {
188-
chk() // $ guarded=testNotNull2(...):true MISSING: guarded='s:not null'
188+
chk() // $ guarded=testNotNull2(...):true guarded='s:not null'
189189
} else {
190190
chk() // $ guarded=testNotNull2(...):false
191191
}
192192

193193
if 0 == getNumOrDefault(i) {
194-
chk() // $ guarded='0 == getNumOrDefault(...):true' guarded='getNumOrDefault(...):0'
194+
chk() // $ guarded='0 == getNumOrDefault(...):true' guarded='getNumOrDefault(...):0' guarded='getNumOrDefault(...):not null'
195195
} else {
196196
chk() // $ guarded='0 == getNumOrDefault(...):false' guarded='getNumOrDefault(...):not 0' MISSING: guarded='i:not 0' guarded='i:not null'
197197
}
198198

199199
if nil == concatNonNull(s, "suffix") {
200200
chk() // $ guarded='concatNonNull(...):null' guarded='nil == concatNonNull(...):true'
201201
} else {
202-
chk() // $ guarded='concatNonNull(...):not null' guarded='nil == concatNonNull(...):false' MISSING: guarded='s:not null'
202+
chk() // $ guarded='concatNonNull(...):not null' guarded='nil == concatNonNull(...):false' guarded='s:not null'
203203
}
204204

205205
switch testEnumWrapper(g(1)) {
@@ -217,5 +217,99 @@ func ensureNotNull(_ o: Any?) throws {
217217
func testExceptionWrapper(_ s: String?) throws {
218218
chk() // nothing guards here
219219
try ensureNotNull(s)
220-
chk() // $ guarded='ensureNotNull(...):no exception' MISSING: guarded='s:not null'
220+
chk() // $ guarded='ensureNotNull(...):no exception' guarded='s:not null'
221+
}
222+
223+
func namedCheck(_ other: String?, value: String?) -> Bool {
224+
if value == nil { return false }
225+
return true
226+
}
227+
228+
func testFunctionWrappers(_ first: String?, _ second: String?) {
229+
if namedCheck(first, value: second) {
230+
chk() // $ guarded=namedCheck(...):true guarded='second:not null'
231+
}
232+
233+
func localCheck(_ value: String?) -> Bool {
234+
if value == nil { return false }
235+
return true
236+
}
237+
if localCheck(first) {
238+
chk() // $ guarded=localCheck(...):true guarded='first:not null'
239+
}
240+
}
241+
242+
class OverridableCheck {
243+
func accepts(_ value: String?) -> Bool {
244+
if value == nil { return false }
245+
return true
246+
}
247+
}
248+
249+
class AlwaysAccept: OverridableCheck {
250+
override func accepts(_ value: String?) -> Bool {
251+
return true
252+
}
253+
}
254+
255+
private dynamic func dynamicCheck(_ value: String?) -> Bool {
256+
if value == nil { return false }
257+
return true
258+
}
259+
260+
func testDynamicDispatch(_ checker: OverridableCheck, _ value: String?) {
261+
if checker.accepts(value) {
262+
chk() // $ guarded='... .accepts(...):true'
263+
}
264+
if dynamicCheck(value) {
265+
chk() // $ guarded=dynamicCheck(...):true
266+
}
267+
}
268+
269+
class Failable {
270+
init?() { return nil }
271+
}
272+
273+
class InheritedFailable: Failable {}
274+
275+
func testFailableInitializers(_ flag: Bool) {
276+
let first = flag ? Failable() : nil
277+
if first == nil {
278+
chk() // $ guarded='first == nil:true' guarded='first:null' guarded='IfExpr:null'
279+
}
280+
let second = flag ? InheritedFailable() : nil
281+
if second == nil {
282+
chk() // $ guarded='second == nil:true' guarded='second:null' guarded='IfExpr:null'
283+
}
284+
}
285+
286+
typealias Text = String
287+
typealias MaybeText = String?
288+
289+
func testTypeTests(_ value: Any?) {
290+
if value is String {
291+
chk() // $ guarded=TypeTestExpr:true guarded='value:not null'
292+
} else {
293+
chk() // $ guarded=TypeTestExpr:false
294+
}
295+
if value is Text {
296+
chk() // $ guarded=TypeTestExpr:true guarded='value:not null'
297+
}
298+
if value is String? {
299+
chk() // $ guarded=TypeTestExpr:true
300+
}
301+
if value is MaybeText {
302+
chk() // $ guarded=TypeTestExpr:true
303+
}
304+
if value is Any {
305+
chk() // $ guarded=TypeTestExpr:true
306+
}
307+
}
308+
309+
func testLiteralEquality(_ value: String?) {
310+
if value == "safe" {
311+
chk() // $ guarded='value == "safe":true' guarded='value:safe' guarded='value:not null'
312+
} else {
313+
chk() // $ guarded='value == "safe":false' guarded='value:not safe'
314+
}
221315
}

‎unified/ql/test/library-tests/guards/GuardsInline.expected‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
| Guards.swift:29:5:29:9 | chk(...) | 'b != false:true' |
44
| Guards.swift:29:5:29:9 | chk(...) | IfExpr:true |
55
| Guards.swift:29:5:29:9 | chk(...) | b:true |
6+
| Guards.swift:31:5:31:9 | chk(...) | 'IfExpr:not null' |
67
| Guards.swift:31:5:31:9 | chk(...) | 'b != false:false' |
8+
| Guards.swift:31:5:31:9 | chk(...) | 'b:not null' |
79
| Guards.swift:31:5:31:9 | chk(...) | IfExpr:false |
810
| Guards.swift:31:5:31:9 | chk(...) | b:false |
911
| Guards.swift:31:5:31:9 | chk(...) | g(1):true |
@@ -24,10 +26,16 @@
2426
| Guards.swift:52:5:52:9 | chk(...) | 's:not bar' |
2527
| Guards.swift:52:5:52:9 | chk(...) | 's:not foo' |
2628
| Guards.swift:52:5:52:9 | chk(...) | g(3):false |
29+
| Guards.swift:57:5:57:9 | chk(...) | 'IfExpr:not null' |
30+
| Guards.swift:57:5:57:9 | chk(...) | 'o:not null' |
31+
| Guards.swift:57:5:57:9 | chk(...) | 's:not null' |
2732
| Guards.swift:57:5:57:9 | chk(...) | TypeTestExpr:true |
33+
| Guards.swift:57:5:57:9 | chk(...) | g(4):false |
2834
| Guards.swift:64:3:64:7 | chk(...) | 'checkFalse(...):no exception' |
2935
| Guards.swift:64:3:64:7 | chk(...) | 'checkTrue(...):no exception' |
3036
| Guards.swift:64:3:64:7 | chk(...) | 'g(...):no exception' |
37+
| Guards.swift:64:3:64:7 | chk(...) | g(1):true |
38+
| Guards.swift:64:3:64:7 | chk(...) | g(2):false |
3139
| Guards.swift:70:5:70:9 | chk(...) | 'g(...) && ... \|\| ...:true' |
3240
| Guards.swift:70:5:70:9 | chk(...) | 'g(...) \|\| g(...):true' |
3341
| Guards.swift:70:5:70:9 | chk(...) | b:true |
@@ -52,7 +60,7 @@
5260
| Guards.swift:109:5:109:9 | chk(...) | 'IfExpr:null' |
5361
| Guards.swift:109:5:109:9 | chk(...) | 'o == nil:true' |
5462
| Guards.swift:109:5:109:9 | chk(...) | 'o:null' |
55-
| Guards.swift:111:5:111:9 | chk(...) | 'C(...):not null' |
63+
| Guards.swift:109:5:109:9 | chk(...) | g(4):false |
5664
| Guards.swift:111:5:111:9 | chk(...) | 'IfExpr:not null' |
5765
| Guards.swift:111:5:111:9 | chk(...) | 'o == nil:false' |
5866
| Guards.swift:111:5:111:9 | chk(...) | 'o:not null' |
@@ -63,28 +71,57 @@
6371
| Guards.swift:121:5:121:9 | chk(...) | 'base:not null' |
6472
| Guards.swift:121:5:121:9 | chk(...) | 'foo:/' |
6573
| Guards.swift:121:5:121:9 | chk(...) | 'foo:not null' |
66-
| Guards.swift:132:5:132:9 | chk(...) | 'C(...):not null' |
6774
| Guards.swift:132:5:132:9 | chk(...) | 'o != nil:true' |
6875
| Guards.swift:132:5:132:9 | chk(...) | 'o:not null' |
6976
| Guards.swift:132:5:132:9 | chk(...) | g(1):true |
7077
| Guards.swift:134:5:134:9 | chk(...) | 'o != nil:false' |
7178
| Guards.swift:134:5:134:9 | chk(...) | 'o:null' |
79+
| Guards.swift:134:5:134:9 | chk(...) | g(1):false |
7280
| Guards.swift:147:7:147:11 | chk(...) | 'i < ... .count:true' |
7381
| Guards.swift:147:7:147:11 | chk(...) | found:true |
7482
| Guards.swift:152:5:152:9 | chk(...) | 'i < ... .count:false' |
7583
| Guards.swift:152:5:152:9 | chk(...) | found:true |
7684
| Guards.swift:182:5:182:9 | chk(...) | testNotNull1(...):true |
7785
| Guards.swift:184:5:184:9 | chk(...) | testNotNull1(...):false |
86+
| Guards.swift:188:5:188:9 | chk(...) | 's:not null' |
7887
| Guards.swift:188:5:188:9 | chk(...) | testNotNull2(...):true |
7988
| Guards.swift:190:5:190:9 | chk(...) | testNotNull2(...):false |
8089
| Guards.swift:194:5:194:9 | chk(...) | '0 == getNumOrDefault(...):true' |
8190
| Guards.swift:194:5:194:9 | chk(...) | 'getNumOrDefault(...):0' |
91+
| Guards.swift:194:5:194:9 | chk(...) | 'getNumOrDefault(...):not null' |
8292
| Guards.swift:196:5:196:9 | chk(...) | '0 == getNumOrDefault(...):false' |
8393
| Guards.swift:196:5:196:9 | chk(...) | 'getNumOrDefault(...):not 0' |
8494
| Guards.swift:200:5:200:9 | chk(...) | 'concatNonNull(...):null' |
8595
| Guards.swift:200:5:200:9 | chk(...) | 'nil == concatNonNull(...):true' |
8696
| Guards.swift:202:5:202:9 | chk(...) | 'concatNonNull(...):not null' |
8797
| Guards.swift:202:5:202:9 | chk(...) | 'nil == concatNonNull(...):false' |
98+
| Guards.swift:202:5:202:9 | chk(...) | 's:not null' |
8899
| Guards.swift:207:5:207:9 | chk(...) | '... .SUCCESS:match' |
89100
| Guards.swift:209:5:209:9 | chk(...) | '... .SUCCESS:no match' |
90101
| Guards.swift:220:3:220:7 | chk(...) | 'ensureNotNull(...):no exception' |
102+
| Guards.swift:220:3:220:7 | chk(...) | 's:not null' |
103+
| Guards.swift:230:5:230:9 | chk(...) | 'second:not null' |
104+
| Guards.swift:230:5:230:9 | chk(...) | namedCheck(...):true |
105+
| Guards.swift:238:5:238:9 | chk(...) | 'first:not null' |
106+
| Guards.swift:238:5:238:9 | chk(...) | localCheck(...):true |
107+
| Guards.swift:262:5:262:9 | chk(...) | '... .accepts(...):true' |
108+
| Guards.swift:265:5:265:9 | chk(...) | dynamicCheck(...):true |
109+
| Guards.swift:278:5:278:9 | chk(...) | 'IfExpr:null' |
110+
| Guards.swift:278:5:278:9 | chk(...) | 'first == nil:true' |
111+
| Guards.swift:278:5:278:9 | chk(...) | 'first:null' |
112+
| Guards.swift:282:5:282:9 | chk(...) | 'IfExpr:null' |
113+
| Guards.swift:282:5:282:9 | chk(...) | 'second == nil:true' |
114+
| Guards.swift:282:5:282:9 | chk(...) | 'second:null' |
115+
| Guards.swift:291:5:291:9 | chk(...) | 'value:not null' |
116+
| Guards.swift:291:5:291:9 | chk(...) | TypeTestExpr:true |
117+
| Guards.swift:293:5:293:9 | chk(...) | TypeTestExpr:false |
118+
| Guards.swift:296:5:296:9 | chk(...) | 'value:not null' |
119+
| Guards.swift:296:5:296:9 | chk(...) | TypeTestExpr:true |
120+
| Guards.swift:299:5:299:9 | chk(...) | TypeTestExpr:true |
121+
| Guards.swift:302:5:302:9 | chk(...) | TypeTestExpr:true |
122+
| Guards.swift:305:5:305:9 | chk(...) | TypeTestExpr:true |
123+
| Guards.swift:311:5:311:9 | chk(...) | 'value == "safe":true' |
124+
| Guards.swift:311:5:311:9 | chk(...) | 'value:not null' |
125+
| Guards.swift:311:5:311:9 | chk(...) | 'value:safe' |
126+
| Guards.swift:313:5:313:9 | chk(...) | 'value == "safe":false' |
127+
| Guards.swift:313:5:313:9 | chk(...) | 'value:not safe' |

0 commit comments

Comments
 (0)