Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 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,26 @@ module CfgImpl {
boolean getValue() { none() }
}

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

Pattern getPattern(int index) { none() }
}

class ExprPattern extends Expr, Pattern {
ExprPattern() { none() }
}

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

AstNode getVariable() { 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,26 @@ module Ast implements AstSig<Location> {
boolean getValue() { result = this.getBoolValue() }
}

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

Pattern getPattern(int index) { none() }
}

class ExprPattern extends Expr, Pattern {
ExprPattern() { none() }
}

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

AstNode getVariable() { none() }
}

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

final private class FinalIsExpr = CS::IsExpr;

class PatternMatchExpr extends FinalIsExpr {
Expand Down
20 changes: 20 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,26 @@ module CfgImpl {
AssignNullCoalescingExpr() { none() }
}

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

Pattern getPattern(int index) { none() }
}

class ExprPattern extends Expr, Pattern {
ExprPattern() { none() }
}

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

AstNode getVariable() { none() }
}

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

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

Expand Down
20 changes: 20 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,26 @@ private module Ast implements AstSig<Location> {
boolean getValue() { result = this.getBooleanValue() }
}

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

Pattern getPattern(int index) { none() }
}

class ExprPattern extends Expr, Pattern {
ExprPattern() { none() }
}

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

AstNode getVariable() { none() }
}

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

final private class FinalInstanceOfExpr = J::InstanceOfExpr;

class PatternMatchExpr extends FinalInstanceOfExpr {
Expand Down
66 changes: 52 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,44 @@ module Ast implements AstSig<Py::Location> {
}
}

final class Pattern = PatternImpl;

/** A pattern handled by the shared CFG. */
abstract private class PatternImpl extends AstNodeImpl {
abstract Pattern getPattern(int index);
}

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

override Pattern getPattern(int index) { none() }

override string toString() { none() }

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

override Callable getEnclosingCallable() { none() }
}

/** An expression pattern handled by the shared CFG. */
class ExprPattern extends Expr, PatternImpl {
ExprPattern() { none() }

override Pattern getPattern(int index) { none() }
}

/** A binding pattern handled by the shared CFG. */
class BindingPattern extends EmptyPattern {
BindingPattern() { none() }

AstNode getVariable() { 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
20 changes: 20 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,26 @@ private module Ast implements AstSig<Location> {
boolean getValue() { result = super.getValue() }
}

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

Pattern getPattern(int index) { none() }
}

class ExprPattern extends Expr, Pattern {
ExprPattern() { none() }
}

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

AstNode getVariable() { 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;
}
Loading
Loading