Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8f646fe
Shared: Add pattern support to the control-flow graph
hvitved Nov 23, 2026
22e5c80
Actions: Add shared CFG pattern stubs
hvitved Nov 23, 2026
92a984b
C#: Add pattern CFG support and defining accesses
hvitved Nov 23, 2026
e55f9e7
Go: Add shared CFG pattern stubs
hvitved Nov 23, 2026
1c128dc
Java: Model binding patterns in the shared CFG
hvitved Nov 23, 2026
8b754f6
Python: Add shared CFG pattern stubs
hvitved Nov 23, 2026
818259c
Ruby: Add shared CFG pattern stubs
hvitved Nov 23, 2026
eb11b9a
Unified: Add shared CFG pattern stubs
hvitved Nov 23, 2026
16e7191
Shared: Update CFG pattern guard expectations
hvitved Nov 25, 2026
2d9e78e
Migrate Rust to shared control-flow graph
hvitved Nov 2, 2026
79ff9cf
wip
hvitved Nov 6, 2026
d0925f1
Update Rust CFG baselines
hvitved Nov 6, 2026
436a74a
Fix async block CFG value propagation
hvitved Nov 9, 2026
59ad349
Use shared switch CFG for Rust matches
hvitved Nov 16, 2026
f061c07
Propagate switch case values in shared CFG
hvitved Nov 16, 2026
e4fae24
Remove redundant Rust parameter CFG step
hvitved Nov 16, 2026
fd8d993
Use shared foreach CFG for Rust loops
hvitved Nov 16, 2026
272fd7e
simplify
hvitved Nov 16, 2026
84d6055
Rust: Use shared break and continue handling
hvitved Nov 16, 2026
29f8178
Rust: Simplify shared CFG modeling
hvitved Nov 16, 2026
53d8ab9
Rust: Update variables CFG baseline
hvitved Nov 16, 2026
fbf2517
Shared: Move pattern handling into shared CFG
hvitved Nov 16, 2026
d3dbcc9
Shared: Add conditionless do-loop handling
hvitved Nov 16, 2026
1b6ccfb
Shared: Refine pattern and conditional CFG modeling
hvitved Nov 17, 2026
3e3d669
Shared: Model expression patterns in shared CFG
hvitved Nov 17, 2026
63f46f0
wip
hvitved Nov 18, 2026
92fe5c1
Shared: Model binding pattern tests
hvitved Nov 18, 2026
3e58042
Shared: Simplify conjunctive pattern handling
hvitved Nov 18, 2026
d33b2df
Rust: Add fallthrough CFG stub
hvitved Nov 18, 2026
3be6a06
Shared: Refine pattern CFG abstractions
hvitved Nov 18, 2026
a4d3fcb
Shared: Reuse default CFG traversal for patterns
hvitved Nov 19, 2026
473574d
Shared: Add conjunctive pattern abstraction
hvitved Nov 19, 2026
1815122
Rust: Model try expressions as optional returns
hvitved Nov 25, 2026
2481a80
Rust: Model let-else divergence as abrupt completion
hvitved Nov 25, 2026
8f9637c
Shared: Treat loops as pre-order control flow
hvitved Nov 25, 2026
20910ca
Rust: Remove redundant try expression classification
hvitved Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions actions/ql/lib/codeql/actions/controlflow/internal/Cfg.qll
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,28 @@ module CfgImpl {
boolean getValue() { none() }
}

class Pattern extends AstNode {
Pattern() { none() }
}

class AndPattern extends Pattern {
AndPattern() { none() }
}

class OrPattern extends Pattern {
OrPattern() { none() }
}

class NotPattern extends Pattern {
NotPattern() { none() }

Pattern getNegatedPattern() { none() }
}

class BindingPattern extends Pattern {
BindingPattern() { none() }
}

class PatternMatchExpr extends Expr {
PatternMatchExpr() { none() }

Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/lib/semmle/code/csharp/Assignable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ module AssignableInternal {
or
def = TAddressOfDefinition(result)
or
def = TPatternDefinition(result)
def = TPatternDefinition(any(LocalVariablePatternDecl decl | decl.getAccess() = result))
or
def = TAssignOperationDefinition(result)
or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ private module Input implements InputSig1, InputSig2 {

predicate matchAll(Ast::Case c) { c.(SwitchCaseExpr).matchesAll() }

predicate patternPerformsTest(Ast::Pattern pattern) {
not pattern instanceof RecursivePatternExpr
}

predicate patternIsExhaustive(Ast::Pattern pattern) { pattern instanceof DiscardPatternExpr }

private newtype TLabel =
TLblGoto(string label) { any(GotoLabelStmt goto).getLabel() = label } or
TLblSwitchCase(string value) { any(GotoCaseStmt goto).getLabel() = value } or
Expand Down
62 changes: 28 additions & 34 deletions csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private module GuardsInput implements SharedGuards::InputSig<Location, ControlFl
ct.getComparisonKind().isInequality() and polarity = false
)
or
// todo: handled in shared lib?
exists(IsExpr ie, PatternExpr pat |
ie = eqtest and
ie.getExpr() = left and
Expand Down Expand Up @@ -190,6 +191,16 @@ private module GuardsImpl = SharedGuards::Make<Location, Cfg, GuardsInput>;

class GuardValue = GuardsImpl::GuardValue;

private PatternExpr getSameSubPattern(PatternExpr pattern) {
result = pattern
or
result = getSameSubPattern(pattern).(AndPatternExpr).getAnOperand()
or
result = getSameSubPattern(pattern).(OrPatternExpr).getAnOperand()
or
result = getSameSubPattern(pattern).(NotPatternExpr).getPattern()
}

private module LogicInput implements GuardsImpl::LogicInputSig {
import Ssa

Expand Down Expand Up @@ -217,22 +228,23 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
isNull = false
)
or
guard =
any(PatternMatch pm |
e instanceof DereferenceableExpr and
e = pm.getExpr() and
(
val.asBooleanValue().booleanNot() = patternMatchesNull(pm.getPattern()) and
isNull = false
or
exists(TypePatternExpr tpe |
// E.g. `x is string` where `x` has type `string`
typePattern(guard, tpe, tpe.getCheckedType()) and
val.asBooleanValue() = false and
isNull = true
)
)
)
exists(PatternMatch pm |
e instanceof DereferenceableExpr and
e = pm.getExpr() and
guard = getSameSubPattern(pm.getPattern())
|
val.asMatchValue() = true and
isNull = patternMatchesNull(guard)
or
val.asMatchValue() = false and
guard instanceof NullLiteral and
isNull = false
or
// E.g. `x is string` where `x` has type `string`
guard.(TypePatternExpr).getCheckedType() = e.getType() and
val.asMatchValue() = false and
isNull = true
)
or
e.(DereferenceableExpr).hasNullableType() and
guard =
Expand Down Expand Up @@ -355,24 +367,6 @@ private boolean patternMatchesNull(PatternExpr pat) {
not pat instanceof OrPatternExpr and
not pat instanceof AndPatternExpr and
result = false
or
result = patternMatchesNull(pat.(NotPatternExpr).getPattern()).booleanNot()
or
exists(OrPatternExpr ope | pat = ope |
result =
patternMatchesNull(ope.getLeftOperand()).booleanOr(patternMatchesNull(ope.getRightOperand()))
)
or
exists(AndPatternExpr ape | pat = ape |
result =
patternMatchesNull(ape.getLeftOperand()).booleanAnd(patternMatchesNull(ape.getRightOperand()))
)
}

pragma[nomagic]
private predicate typePattern(PatternMatch pm, TypePatternExpr tpe, Type t) {
tpe = pm.getPattern() and
t = pm.getExpr().getType()
}

pragma[nomagic]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Ast implements AstSig<Location> {

additional predicate skipControlFlow(AstNode e) {
e instanceof TypeAccess and
not e instanceof TypeAccessPatternExpr and
// not e instanceof TypeAccessPatternExpr and
not any(CS::SpecificCatchClause sc).getTypeAccess() = e
or
not e.getFile().fromSource()
Expand Down Expand Up @@ -295,6 +295,24 @@ module Ast implements AstSig<Location> {
boolean getValue() { result = this.getBoolValue() }
}

final class Pattern = PatternImpl;

abstract private class PatternImpl extends AstNode { }

private class PatternExprPattern extends PatternImpl, CS::PatternExpr { }

class AndPattern extends Pattern instanceof CS::AndPatternExpr { }

class OrPattern extends Pattern instanceof CS::OrPatternExpr { }

class NotPattern extends Pattern instanceof CS::NotPatternExpr {
Pattern getNegatedPattern() { result = super.getPattern() }
}

class BindingPattern extends PatternImpl {
BindingPattern() { this = any(CS::VariablePatternExpr vp).getAccess() }
}

final private class FinalIsExpr = CS::IsExpr;

class PatternMatchExpr extends FinalIsExpr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ private module Impl {
* true.
*/
predicate isEquality(ExprNode e1, ExprNode e2, boolean polarity) {
exists(Expr e1_, Expr e2_ |
e1 = unique(ExprNode cfn | hasChild(this, e1_, _, cfn) | cfn) and
e2 = unique(ExprNode cfn | hasChild(this, e2_, _, cfn) | cfn) and
super.isEquality(e1_, e2_, any(G::GuardValue gv | gv.asBooleanValue() = polarity))
)
super
.isEquality(e1.getExpr(), e2.getExpr(),
any(G::GuardValue gv | gv.asBooleanValue() = polarity))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
| Finally.cs:7:13:7:13 | i | Finally.cs:7:13:7:17 | Int32 i = ... | Finally.cs:7:13:7:13 | access to local variable i | Finally.cs:7:17:7:17 | 0 | certain |
| Finally.cs:7:13:7:13 | i | Finally.cs:15:13:15:17 | ... = ... | Finally.cs:15:13:15:13 | access to local variable i | Finally.cs:15:17:15:17 | 1 | certain |
| Patterns.cs:7:16:7:16 | o | Patterns.cs:7:16:7:23 | Object o = ... | Patterns.cs:7:16:7:16 | access to local variable o | Patterns.cs:7:20:7:23 | null | certain |
| Patterns.cs:8:22:8:23 | i1 | Patterns.cs:8:18:8:23 | Int32 i1 | Patterns.cs:8:22:8:23 | access to local variable i1 | Patterns.cs:8:13:8:13 | access to local variable o | certain |
| Patterns.cs:12:30:12:31 | s1 | Patterns.cs:12:23:12:31 | String s1 | Patterns.cs:12:30:12:31 | access to local variable s1 | Patterns.cs:12:18:12:18 | access to local variable o | certain |
| Patterns.cs:16:27:16:28 | v1 | Patterns.cs:16:23:16:28 | Object v1 | Patterns.cs:16:27:16:28 | access to local variable v1 | Patterns.cs:16:18:16:18 | access to local variable o | certain |
| Patterns.cs:24:22:24:23 | i2 | Patterns.cs:24:18:24:23 | Int32 i2 | Patterns.cs:24:22:24:23 | access to local variable i2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:27:22:27:23 | i3 | Patterns.cs:27:18:27:23 | Int32 i3 | Patterns.cs:27:22:27:23 | access to local variable i3 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:30:25:30:26 | s2 | Patterns.cs:30:18:30:26 | String s2 | Patterns.cs:30:25:30:26 | access to local variable s2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:33:22:33:23 | v2 | Patterns.cs:33:18:33:23 | Object v2 | Patterns.cs:33:22:33:23 | access to local variable v2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:8:22:8:23 | i1 | Patterns.cs:8:22:8:23 | Int32 i1 | Patterns.cs:8:22:8:23 | access to local variable i1 | Patterns.cs:8:13:8:13 | access to local variable o | certain |
| Patterns.cs:12:30:12:31 | s1 | Patterns.cs:12:30:12:31 | String s1 | Patterns.cs:12:30:12:31 | access to local variable s1 | Patterns.cs:12:18:12:18 | access to local variable o | certain |
| Patterns.cs:16:27:16:28 | v1 | Patterns.cs:16:27:16:28 | Object v1 | Patterns.cs:16:27:16:28 | access to local variable v1 | Patterns.cs:16:18:16:18 | access to local variable o | certain |
| Patterns.cs:24:22:24:23 | i2 | Patterns.cs:24:22:24:23 | Int32 i2 | Patterns.cs:24:22:24:23 | access to local variable i2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:27:22:27:23 | i3 | Patterns.cs:27:22:27:23 | Int32 i3 | Patterns.cs:27:22:27:23 | access to local variable i3 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:30:25:30:26 | s2 | Patterns.cs:30:25:30:26 | String s2 | Patterns.cs:30:25:30:26 | access to local variable s2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
| Patterns.cs:33:22:33:23 | v2 | Patterns.cs:33:22:33:23 | Object v2 | Patterns.cs:33:22:33:23 | access to local variable v2 | Patterns.cs:20:17:20:17 | access to local variable o | certain |
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@
| Finally.cs:7:13:7:17 | Int32 i = ... | Finally.cs:7:13:7:17 | Int32 i = ... |
| Finally.cs:15:13:15:17 | ... = ... | Finally.cs:15:13:15:17 | ... = ... |
| Patterns.cs:7:16:7:23 | Object o = ... | Patterns.cs:7:16:7:23 | Object o = ... |
| Patterns.cs:8:18:8:23 | Int32 i1 | Patterns.cs:8:18:8:23 | Int32 i1 |
| Patterns.cs:12:23:12:31 | String s1 | Patterns.cs:12:23:12:31 | String s1 |
| Patterns.cs:16:23:16:28 | Object v1 | Patterns.cs:16:23:16:28 | Object v1 |
| Patterns.cs:24:18:24:23 | Int32 i2 | Patterns.cs:24:18:24:23 | Int32 i2 |
| Patterns.cs:27:18:27:23 | Int32 i3 | Patterns.cs:27:18:27:23 | Int32 i3 |
| Patterns.cs:30:18:30:26 | String s2 | Patterns.cs:30:18:30:26 | String s2 |
| Patterns.cs:33:18:33:23 | Object v2 | Patterns.cs:33:18:33:23 | Object v2 |
| Patterns.cs:8:22:8:23 | Int32 i1 | Patterns.cs:8:22:8:23 | access to local variable i1 |
| Patterns.cs:12:30:12:31 | String s1 | Patterns.cs:12:30:12:31 | access to local variable s1 |
| Patterns.cs:16:27:16:28 | Object v1 | Patterns.cs:16:27:16:28 | access to local variable v1 |
| Patterns.cs:24:22:24:23 | Int32 i2 | Patterns.cs:24:22:24:23 | access to local variable i2 |
| Patterns.cs:27:22:27:23 | Int32 i3 | Patterns.cs:27:22:27:23 | access to local variable i3 |
| Patterns.cs:30:25:30:26 | String s2 | Patterns.cs:30:25:30:26 | access to local variable s2 |
| Patterns.cs:33:22:33:23 | Object v2 | Patterns.cs:33:22:33:23 | access to local variable v2 |
Loading
Loading