Repository navigation
Unified: Add Guards instantiation. - #23350
aschackmull wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The guard analysis can infer false nil states from ordinary null identifiers and conditional casts, and incorrectly transfer checks between parameters with repeated labels.
3 open issues
What changed in this PR
Adds Unified’s integration with the shared guard-analysis library to reason about conditions and the control flow they govern.
Changes:
- Instantiates guard analysis with Unified expressions, control flow, SSA, and wrapper calls.
- Adds AST helpers for bitwise operators, compound assignments, and equality tests.
| File | Description |
|---|---|
unified/ql/lib/codeql/unified/internal/controlflow/Guards.qll |
Connects Unified to shared guard analysis. |
unified/ql/lib/codeql/unified/internal/AstExtra.qll |
Adds operator classifications used by guard analysis. |
Review effort: Balanced (adaptive)
63904ba to
d8d1f86
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Multiple critical Swift guard-inference errors can falsely establish non-null and Boolean facts, making analysis results unsound.
8 open issues
Overloaded Boolean operators can produce unsound guard facts · New Optional-to-Any cast falsely implies the source is non-null · New Nullable Boolean comparison derives an incorrect true fact · New Overloaded equality operators can produce unsound nullness facts · New Delegated failable initializers can cause incorrect wrapper inference · New Repeated external labels can mark unchecked parameters non-null · New String escape spellings are mistaken for distinct values · New Single-statement returns produce an untracked extra return expression · New
3 resolved since last review
Review effort: Max (adaptive)
| class AndExpr extends BinaryExpr { | ||
| AndExpr() { | ||
| this instanceof BitwiseAndExpr or | ||
| this instanceof LogicalAndExpr or | ||
| this instanceof AssignBitwiseAndExpr or | ||
| this instanceof AssignLogicalAndExpr |
| IdExpr() { | ||
| this instanceof AssignExpr | ||
| or | ||
| this instanceof TypeCastExpr and | ||
| // TODO: This is Swift-specific and needs to go through a plugin | ||
| not this.(TypeCastExpr).getOperator().getValue() = "as?" | ||
| } | ||
|
|
||
| Expr getEqualChildExpr() { | ||
| result = this.(AssignExpr).getValue() | ||
| or | ||
| result = this.(TypeCastExpr).getExpr() | ||
| } |
| exists(U::EqualityTest eq | eq = eqtest | | ||
| eq.getLeft() = left and eq.getRight() = right and eq.getPolarity() = polarity |
| predicate equalityTest(Expr eqtest, Expr left, Expr right, boolean polarity) { | ||
| exists(U::EqualityTest eq | eq = eqtest | | ||
| eq.getLeft() = left and eq.getRight() = right and eq.getPolarity() = polarity | ||
| ) |
| this instanceof ConstructorDeclaration or | ||
| this.(FunctionDeclaration).hasModifier("static") or |
| result.getDeclaringCallable() = this and | ||
| ppos = getParameterPosition(result) |
| private class StringConstant extends ConstantExpr { | ||
| StringConstant() { exists(this.getStringValue()) } | ||
|
|
||
| override ConstantValue asConstantValue() { result = TStringValue(this.getStringValue()) } |
| ppos = getParameterPosition(result) | ||
| } | ||
|
|
||
| Expr getAReturnExpr() { EP::isReturnedExpr(this, result) } |
Also, extend the test to cover more Swift cases. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
d8d1f86 to
625face
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The guard model treats optional-to-Any boxing as value-preserving, allowing a non-nil box to incorrectly prove its nil source non-null. Separately, repeated parameter labels transfer wrapper checks to unchecked arguments, variadic mapping can mark nil elements non-null, and failable initializer inference can misattribute delegated initialization failures to input nullness. Overloaded comparisons, logical operators, and expression patterns are also assigned built-in semantics they need not have. These paths need conservative modeling and regression coverage to prevent incorrect guard guarantees.
12 open issues
Avoid equality reasoning for overloaded expression patterns · New Restrict disjunction guards to built-in Boolean semantics · New Exclude variadic parameters from scalar wrapper guards · New Repeated external labels can mark unchecked parameters non-null Delegated failable initializers can cause incorrect wrapper inference Overloaded equality operators can produce unsound nullness facts Nullable Boolean comparison derives an incorrect true fact Optional-to-Any cast falsely implies the source is non-null Overloaded Boolean operators can produce unsound guard facts Parse Swift radix prefixes and digit separators in integer guards · New Single-statement returns produce an untracked extra return expression String escape spellings are mistaken for distinct values
Review effort: Max
| class Case extends AstNode instanceof U::SwitchCase { | ||
| Expr getSwitchExpr() { exists(U::SwitchExpr s | s.getValue() = result and s.getACase() = this) } | ||
|
|
||
| AstNode getAPattern() { result = super.getPattern() } |
| class OrExpr extends BinaryExpr { | ||
| OrExpr() { | ||
| this instanceof BitwiseOrExpr or | ||
| this instanceof LogicalOrExpr or |
| arg.getValue() = result and | ||
| apos = getArgumentPosition(arg) |
| private class IntegerConstant extends ConstantExpr instanceof U::IntLiteral { | ||
| override int asIntegerValue() { result = super.getValue().toInt() } | ||
| } |


This instantiates the Guards library for unified.