Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0f911bb
Migrate Rust to shared control-flow graph
hvitved Nov 2, 2026
11d44b9
wip
hvitved Nov 6, 2026
33e9ba3
Update Rust CFG baselines
hvitved Nov 6, 2026
aa0a198
Fix async block CFG value propagation
hvitved Nov 9, 2026
73bbb9a
Use shared switch CFG for Rust matches
hvitved Nov 16, 2026
bd40ad2
Propagate switch case values in shared CFG
hvitved Nov 16, 2026
be708bd
Remove redundant Rust parameter CFG step
hvitved Nov 16, 2026
5848dc5
Use shared foreach CFG for Rust loops
hvitved Nov 16, 2026
788cf80
simplify
hvitved Nov 16, 2026
2e74185
Rust: Use shared break and continue handling
hvitved Nov 16, 2026
0d2aa59
Rust: Simplify shared CFG modeling
hvitved Nov 16, 2026
6323ce7
Rust: Update variables CFG baseline
hvitved Nov 16, 2026
591125f
Shared: Move pattern handling into shared CFG
hvitved Nov 16, 2026
a127856
Shared: Add conditionless do-loop handling
hvitved Nov 16, 2026
0407e86
Shared: Refine pattern and conditional CFG modeling
hvitved Nov 17, 2026
3f30fe0
Actions: Add shared CFG pattern stubs
hvitved Nov 17, 2026
03f306f
Shared: Model expression patterns in shared CFG
hvitved Nov 17, 2026
b675348
wip
hvitved Nov 18, 2026
2e90ad7
Shared: Model binding pattern tests
hvitved Nov 18, 2026
0547363
Shared: Simplify conjunctive pattern handling
hvitved Nov 18, 2026
aaddfd5
Rust: Add fallthrough CFG stub
hvitved Nov 18, 2026
7a99177
Shared: Refine pattern CFG abstractions
hvitved Nov 18, 2026
928e9b0
Shared: Reuse default CFG traversal for patterns
hvitved Nov 19, 2026
e77e452
Shared: Add conjunctive pattern abstraction
hvitved Nov 19, 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
12 changes: 12 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,18 @@ module CfgImpl {
boolean getValue() { none() }
}

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ module Ast implements AstSig<Location> {
boolean getValue() { result = this.getBoolValue() }
}

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

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

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

final private class FinalIsExpr = CS::IsExpr;

class PatternMatchExpr extends FinalIsExpr {
Expand Down
12 changes: 12 additions & 0 deletions go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,18 @@ module CfgImpl {
AssignNullCoalescingExpr() { none() }
}

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

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

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

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

Expand Down
12 changes: 12 additions & 0 deletions java/ql/lib/semmle/code/java/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ private module Ast implements AstSig<Location> {
boolean getValue() { result = this.getBooleanValue() }
}

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

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

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

final private class FinalInstanceOfExpr = J::InstanceOfExpr;

class PatternMatchExpr extends FinalInstanceOfExpr {
Expand Down
53 changes: 39 additions & 14 deletions python/ql/lib/semmle/python/controlflow/internal/AstNodeImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ module Ast implements AstSig<Py::Location> {
}

/** A pattern in a `match` statement. */
additional class Pattern extends AstNodeImpl, TPattern {
additional class PythonPattern extends AstNodeImpl, TPattern {
private Py::Pattern p;

Pattern() { this = TPattern(p) }
PythonPattern() { this = TPattern(p) }

override string toString() { result = p.toString() }

Expand All @@ -264,7 +264,7 @@ module Ast implements AstSig<Py::Location> {
/**
* A `case x` pattern that binds `x` to the matched value.
*/
additional class MatchCapturePattern extends Pattern {
additional class MatchCapturePattern extends PythonPattern {
private Py::MatchCapturePattern cap;

MatchCapturePattern() { this = TPattern(cap) }
Expand All @@ -278,7 +278,7 @@ module Ast implements AstSig<Py::Location> {
/**
* A `case pattern as name` pattern.
*/
additional class MatchAsPattern extends Pattern {
additional class MatchAsPattern extends PythonPattern {
private Py::MatchAsPattern asp;

MatchAsPattern() { this = TPattern(asp) }
Expand All @@ -300,7 +300,7 @@ module Ast implements AstSig<Py::Location> {
* A `case [a, b, *rest]` star pattern. Binds `rest` to the remaining
* elements of the sequence.
*/
additional class MatchStarPattern extends Pattern {
additional class MatchStarPattern extends PythonPattern {
private Py::MatchStarPattern starp;

MatchStarPattern() { this = TPattern(starp) }
Expand All @@ -314,7 +314,7 @@ module Ast implements AstSig<Py::Location> {
/**
* A `case [a, b, ...]` sequence pattern. Recurses into the sub-patterns.
*/
additional class MatchSequencePattern extends Pattern {
additional class MatchSequencePattern extends PythonPattern {
private Py::MatchSequencePattern seqp;

MatchSequencePattern() { this = TPattern(seqp) }
Expand All @@ -328,7 +328,7 @@ module Ast implements AstSig<Py::Location> {
/**
* A `case Cls(a, b, x=y)` class pattern.
*/
additional class MatchClassPattern extends Pattern {
additional class MatchClassPattern extends PythonPattern {
private Py::MatchClassPattern clsp;

MatchClassPattern() { this = TPattern(clsp) }
Expand Down Expand Up @@ -357,7 +357,7 @@ module Ast implements AstSig<Py::Location> {
/**
* A `case {k: v}` mapping pattern.
*/
additional class MatchMappingPattern extends Pattern {
additional class MatchMappingPattern extends PythonPattern {
private Py::MatchMappingPattern mapp;

MatchMappingPattern() { this = TPattern(mapp) }
Expand All @@ -370,7 +370,7 @@ module Ast implements AstSig<Py::Location> {
/**
* A key-value pair inside a `case {k: v}` mapping pattern.
*/
additional class MatchKeyValuePattern extends Pattern {
additional class MatchKeyValuePattern extends PythonPattern {
private Py::MatchKeyValuePattern kvp;

MatchKeyValuePattern() { this = TPattern(kvp) }
Expand All @@ -389,7 +389,7 @@ module Ast implements AstSig<Py::Location> {
/**
* A `case Cls(name=value)` keyword sub-pattern.
*/
additional class MatchKeywordPattern extends Pattern {
additional class MatchKeywordPattern extends PythonPattern {
private Py::MatchKeywordPattern kwp;

MatchKeywordPattern() { this = TPattern(kwp) }
Expand All @@ -406,7 +406,7 @@ module Ast implements AstSig<Py::Location> {
}

/** A `case **rest` double-star mapping sub-pattern. */
additional class MatchDoubleStarPattern extends Pattern {
additional class MatchDoubleStarPattern extends PythonPattern {
private Py::MatchDoubleStarPattern dsp;

MatchDoubleStarPattern() { this = TPattern(dsp) }
Expand All @@ -417,7 +417,7 @@ module Ast implements AstSig<Py::Location> {
}

/** A `case p1 | p2 | …` or-pattern. */
additional class MatchOrPattern extends Pattern {
additional class MatchOrPattern extends PythonPattern {
private Py::MatchOrPattern orp;

MatchOrPattern() { this = TPattern(orp) }
Expand All @@ -428,7 +428,7 @@ module Ast implements AstSig<Py::Location> {
}

/** A `case 1` literal pattern. */
additional class MatchLiteralPattern extends Pattern {
additional class MatchLiteralPattern extends PythonPattern {
private Py::MatchLiteralPattern litp;

MatchLiteralPattern() { this = TPattern(litp) }
Expand All @@ -439,7 +439,7 @@ module Ast implements AstSig<Py::Location> {
}

/** A `case Cls.NAME` value pattern. */
additional class MatchValuePattern extends Pattern {
additional class MatchValuePattern extends PythonPattern {
private Py::MatchValuePattern vp;

MatchValuePattern() { this = TPattern(vp) }
Expand Down Expand Up @@ -1155,6 +1155,31 @@ module Ast implements AstSig<Py::Location> {
}
}

final class Pattern = PatternImpl;

/** A pattern handled by the shared CFG. */
abstract private class PatternImpl extends AstNodeImpl { }

private class EmptyPattern extends PatternImpl {
EmptyPattern() { none() }

override string toString() { none() }

override Py::Location getLocation() { none() }

override Callable getEnclosingCallable() { none() }
}

/** An and-pattern handled by the shared CFG. */
class AndPattern extends EmptyPattern {
AndPattern() { none() }
}

/** An or-pattern handled by the shared CFG. */
class OrPattern extends EmptyPattern {
OrPattern() { none() }
}

/** A pattern match expression. Python has no `instanceof`-style pattern match expression. */
class PatternMatchExpr extends Expr {
PatternMatchExpr() { none() }
Expand Down
12 changes: 12 additions & 0 deletions ruby/ql/lib/codeql/ruby/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,18 @@ private module Ast implements AstSig<Location> {
boolean getValue() { result = super.getValue() }
}

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

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

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

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

Expand Down
66 changes: 54 additions & 12 deletions rust/ql/lib/codeql/rust/controlflow/BasicBlocks.qll
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
private import codeql.controlflow.BasicBlock as BB
private import codeql.Locations
private import codeql.rust.elements.internal.generated.Raw
private import codeql.rust.elements.internal.generated.Synth
private import codeql.rust.controlflow.ControlFlowGraph as ControlFlowGraph
private import internal.ControlFlowGraphImpl as CfgImpl
private import CfgImpl::BasicBlocks as BasicBlocksImpl

class BasicBlock = BasicBlocksImpl::BasicBlock;
class BasicBlock = CfgImpl::Cfg::BasicBlock;

final class EntryBasicBlock = BasicBlocksImpl::EntryBasicBlock;
/** A basic block containing the entry node of a callable. */
final class EntryBasicBlock extends BasicBlock {
EntryBasicBlock() { this.getFirstNode() instanceof CfgImpl::ControlFlow::EntryNode }
}

/** A basic block containing the final exit node of a callable. */
final class ExitBasicBlock extends BasicBlock {
ExitBasicBlock() { this.getLastNode() instanceof CfgImpl::ControlFlow::ExitNode }
}

/** A basic block containing an annotated normal or exceptional exit node. */
final class AnnotatedExitBasicBlock extends BasicBlock {
AnnotatedExitBasicBlock() { this.getANode() instanceof CfgImpl::ControlFlow::AnnotatedExitNode }

/** Holds if this block represents normal exit. */
predicate isNormal() { this.getANode() instanceof CfgImpl::ControlFlow::NormalExitNode }
}

final class ExitBasicBlock = BasicBlocksImpl::ExitBasicBlock;
/** A basic block whose last node is a condition. */
final class ConditionBasicBlock extends BasicBlock {
ConditionBasicBlock() { this.getLastNode().(ControlFlowGraph::CfgNode).isCondition() }
}

/** A basic block whose first node joins multiple predecessors. */
final class JoinBasicBlock extends BasicBlock {
JoinBasicBlock() { this.getFirstNode().(ControlFlowGraph::CfgNode).isJoin() }

/** Gets the `i`th predecessor of this join block in a stable arbitrary order. */
JoinPredecessorBasicBlock getJoinBlockPredecessor(int i) {
result =
rank[i + 1](JoinPredecessorBasicBlock pred, int id, string tag |
pred = this.getAPredecessor() and
id = getNodeId(pred.getFirstNode()) and
tag = pred.getFirstNode().getIdTag()
|
pred order by id, tag
)
}
}

final class AnnotatedExitBasicBlock = BasicBlocksImpl::AnnotatedExitBasicBlock;
/** A predecessor basic block of a join block. */
final class JoinPredecessorBasicBlock extends BasicBlock {
JoinPredecessorBasicBlock() { this.getASuccessor() instanceof JoinBasicBlock }
}

final class ConditionBasicBlock = BasicBlocksImpl::ConditionBasicBlock;
private predicate id(Raw::AstNode x, Raw::AstNode y) { x = y }

final class JoinBasicBlock = BasicBlocksImpl::JoinBasicBlock;
private predicate idOfDbAstNode(Raw::AstNode x, int y) = equivalenceRelation(id/2)(x, y)

final class JoinPredecessorBasicBlock = BasicBlocksImpl::JoinPredecessorBasicBlock;
private int getNodeId(CfgImpl::ControlFlowNode node) {
idOfDbAstNode(Synth::convertAstNodeToRaw(node.getAstNode()), result)
}

module Cfg implements BB::CfgSig<Location> {
class ControlFlowNode = ControlFlowGraph::CfgNode;
class ControlFlowNode = CfgImpl::Cfg::ControlFlowNode;

class BasicBlock = BasicBlocksImpl::BasicBlock;
class BasicBlock = CfgImpl::Cfg::BasicBlock;

class EntryBasicBlock = BasicBlocksImpl::EntryBasicBlock;
class EntryBasicBlock = CfgImpl::Cfg::EntryBasicBlock;

predicate dominatingEdge = BasicBlocksImpl::dominatingEdge/2;
predicate dominatingEdge = CfgImpl::Cfg::dominatingEdge/2;
}
17 changes: 13 additions & 4 deletions rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@

private import rust
private import ControlFlowGraph
private import internal.ControlFlowGraphImpl as CfgImpl
private import internal.CfgNodes
import Nodes

class AstCfgNode = CfgImpl::AstCfgNode;
/**
* A CFG node that uniquely represents an AST node.
*/
class AstCfgNode extends CfgNode {
AstCfgNode() { this.injects(_) }

/** Gets the represented AST node. */
AstNode getAstNode() { this.injects(result) }
}

class ExitCfgNode = CfgImpl::ExitNode;
/** A CFG node representing the final exit from a callable. */
class ExitCfgNode extends CfgNode instanceof ControlFlow::ExitNode { }

class AnnotatedExitCfgNode = CfgImpl::AnnotatedExitNode;
/** A CFG node representing a normal or exceptional exit from a callable. */
class AnnotatedExitCfgNode extends CfgNode instanceof ControlFlow::AnnotatedExitNode { }

/** A variable access. */
final class VariableAccessCfgNode extends PathExprBaseCfgNode {
Expand Down
24 changes: 20 additions & 4 deletions rust/ql/lib/codeql/rust/controlflow/ControlFlowGraph.qll
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
/** Provides classes representing the control flow graph. */

private import internal.ControlFlowGraphImpl
private import internal.Scope as Scope
private import internal.ControlFlowGraphImpl as CfgImpl
import codeql.controlflow.SuccessorType

final class CfgScope = Scope::CfgScope;
final class CfgScope = CfgImpl::CfgScope;

final class CfgNode = Node;
/**
* A node in the control flow graph.
*/
class CfgNode extends CfgImpl::ControlFlowNode {
/** Gets the CFG scope containing this node. */
CfgScope getScope() { result = this.getEnclosingCallable() }

/** Holds if this node has conditional successors. */
predicate isCondition() { exists(this.getASuccessor(any(ConditionalSuccessor t))) }

/** Holds if this node has more than one predecessor. */
predicate isJoin() { strictcount(this.getAPredecessor()) > 1 }

/** Holds if this node has more than one successor. */
predicate isBranch() { strictcount(this.getASuccessor()) > 1 }
}

module ControlFlow = CfgImpl::ControlFlow;
Loading
Loading