From 0b5270979dad0be78b2d34559b67f554195d599a Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Mon, 10 Feb 2025 14:36:18 +0100 Subject: [PATCH] SSA: Remove the need for ExitBasicBlock in SSA. --- .../cpp/ir/dataflow/internal/SsaInternalsCommon.qll | 4 ---- .../code/csharp/controlflow/internal/PreSsa.qll | 2 +- .../code/csharp/dataflow/internal/BaseSSA.qll | 2 -- .../code/csharp/dataflow/internal/SsaImpl.qll | 2 -- .../dataflow/internal/VariableCapture.qll | 2 -- .../javascript/dataflow/internal/sharedlib/Ssa.qll | 4 ---- .../lib/codeql/ruby/dataflow/internal/SsaImpl.qll | 2 -- .../lib/codeql/rust/dataflow/internal/SsaImpl.qll | 2 -- shared/dataflow/codeql/dataflow/VariableCapture.qll | 7 ------- shared/ssa/codeql/ssa/Ssa.qll | 13 +++++-------- swift/ql/lib/codeql/swift/dataflow/Ssa.qll | 2 -- 11 files changed, 6 insertions(+), 36 deletions(-) diff --git a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternalsCommon.qll b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternalsCommon.qll index 52bb16b1f3c..5d8a9ebf9a6 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternalsCommon.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternalsCommon.qll @@ -769,8 +769,4 @@ module InputSigCommon { BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result.immediatelyDominates(bb) } BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() } - - class ExitBasicBlock extends BasicBlock { - ExitBasicBlock() { this.getLastInstruction() instanceof ExitFunctionInstruction } - } } diff --git a/csharp/ql/lib/semmle/code/csharp/controlflow/internal/PreSsa.qll b/csharp/ql/lib/semmle/code/csharp/controlflow/internal/PreSsa.qll index 0984d0ac41b..184317b07bd 100644 --- a/csharp/ql/lib/semmle/code/csharp/controlflow/internal/PreSsa.qll +++ b/csharp/ql/lib/semmle/code/csharp/controlflow/internal/PreSsa.qll @@ -90,7 +90,7 @@ module PreSsa { BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() } - class ExitBasicBlock extends BasicBlock { + private class ExitBasicBlock extends BasicBlock { ExitBasicBlock() { scopeLast(_, this.getLastElement(), _) } } diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/BaseSSA.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/BaseSSA.qll index e39d26d80b2..ec1b5a0188e 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/BaseSSA.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/BaseSSA.qll @@ -55,8 +55,6 @@ module BaseSsa { BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() } - class ExitBasicBlock extends BasicBlock, ControlFlow::BasicBlocks::ExitBlock { } - class SourceVariable = PreSsa::SimpleLocalScopeVariable; predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) { diff --git a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll index 0093f3f4246..399a9ef2a2c 100644 --- a/csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll +++ b/csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll @@ -17,8 +17,6 @@ private module SsaInput implements SsaImplCommon::InputSig { BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() } - class ExitBasicBlock extends BasicBlock, ControlFlow::BasicBlocks::ExitBlock { } - class SourceVariable = Ssa::SourceVariable; /** diff --git a/javascript/ql/lib/semmle/javascript/dataflow/internal/VariableCapture.qll b/javascript/ql/lib/semmle/javascript/dataflow/internal/VariableCapture.qll index 32ce88169e6..75f21bab38a 100644 --- a/javascript/ql/lib/semmle/javascript/dataflow/internal/VariableCapture.qll +++ b/javascript/ql/lib/semmle/javascript/dataflow/internal/VariableCapture.qll @@ -240,8 +240,6 @@ module VariableCaptureConfig implements InputSig { BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result = bb.getImmediateDominator() } predicate entryBlock(BasicBlock bb) { bb instanceof js::EntryBasicBlock } - - predicate exitBlock(BasicBlock bb) { bb.getLastNode() instanceof js::ControlFlowExitNode } } module VariableCaptureOutput = Flow; diff --git a/javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib/Ssa.qll b/javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib/Ssa.qll index 04607a5bd5b..4fee3f98b17 100644 --- a/javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib/Ssa.qll +++ b/javascript/ql/lib/semmle/javascript/dataflow/internal/sharedlib/Ssa.qll @@ -14,10 +14,6 @@ module SsaConfig implements InputSig { class BasicBlock = js::BasicBlock; - class ExitBasicBlock extends BasicBlock { - ExitBasicBlock() { this.isExitBlock() } - } - class SourceVariable extends LocalVariableOrThis { SourceVariable() { not this.isCaptured() } } diff --git a/ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImpl.qll b/ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImpl.qll index 643ff0d3276..77c0d0e82b0 100644 --- a/ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImpl.qll +++ b/ruby/ql/lib/codeql/ruby/dataflow/internal/SsaImpl.qll @@ -18,8 +18,6 @@ module SsaInput implements SsaImplCommon::InputSig { BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() } - class ExitBasicBlock extends BasicBlock, BasicBlocks::ExitBasicBlock { } - class SourceVariable = LocalVariable; /** diff --git a/rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll b/rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll index 99d2be77cc5..009efed92fe 100644 --- a/rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll +++ b/rust/ql/lib/codeql/rust/dataflow/internal/SsaImpl.qll @@ -47,8 +47,6 @@ module SsaInput implements SsaImplCommon::InputSig { BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() } - class ExitBasicBlock = BasicBlocks::ExitBasicBlock; - /** * A variable amenable to SSA construction. * diff --git a/shared/dataflow/codeql/dataflow/VariableCapture.qll b/shared/dataflow/codeql/dataflow/VariableCapture.qll index 8aa1804792b..3077739338e 100644 --- a/shared/dataflow/codeql/dataflow/VariableCapture.qll +++ b/shared/dataflow/codeql/dataflow/VariableCapture.qll @@ -67,9 +67,6 @@ signature module InputSig { /** Holds if `bb` is a control-flow entry point. */ default predicate entryBlock(BasicBlock bb) { not exists(getImmediateBasicBlockDominator(bb)) } - /** Holds if `bb` is a control-flow exit point. */ - default predicate exitBlock(BasicBlock bb) { not exists(getABasicBlockSuccessor(bb)) } - /** A variable that is captured in a closure. */ class CapturedVariable { /** Gets a textual representation of this variable. */ @@ -699,10 +696,6 @@ module Flow Input> implements OutputSig result = Input::getABasicBlockSuccessor(bb) } - class ExitBasicBlock extends BasicBlock { - ExitBasicBlock() { exitBlock(this) } - } - class SourceVariable = CaptureContainer; predicate variableWrite(BasicBlock bb, int i, SourceVariable cc, boolean certain) { diff --git a/shared/ssa/codeql/ssa/Ssa.qll b/shared/ssa/codeql/ssa/Ssa.qll index bb9acb7fbda..dacc323f43e 100644 --- a/shared/ssa/codeql/ssa/Ssa.qll +++ b/shared/ssa/codeql/ssa/Ssa.qll @@ -60,12 +60,6 @@ signature module InputSig { /** Gets an immediate successor of basic block `bb`, if any. */ BasicBlock getABasicBlockSuccessor(BasicBlock bb); - /** - * An exit basic block, that is, a basic block whose last node is - * an exit node. - */ - class ExitBasicBlock extends BasicBlock; - /** A variable that can be SSA converted. */ class SourceVariable { /** Gets a textual representation of this variable. */ @@ -855,6 +849,9 @@ module Make Input> { lastRefRedef(inp, _, _, def) } + /** Holds if `bb` is a control-flow exit point. */ + private predicate exitBlock(BasicBlock bb) { not exists(getABasicBlockSuccessor(bb)) } + /** * NB: If this predicate is exposed, it should be cached. * @@ -873,7 +870,7 @@ module Make Input> { lastSsaRefExt(def, _, bb, i) and ( // Can reach exit directly - bb instanceof ExitBasicBlock + exitBlock(bb) or // Can reach a block using one or more steps, where `def` is no longer live varBlockReachesExitExt(def, bb) @@ -893,7 +890,7 @@ module Make Input> { lastSsaRef(def, _, bb, i) and ( // Can reach exit directly - bb instanceof ExitBasicBlock + exitBlock(bb) or // Can reach a block using one or more steps, where `def` is no longer live varBlockReachesExit(def, bb) diff --git a/swift/ql/lib/codeql/swift/dataflow/Ssa.qll b/swift/ql/lib/codeql/swift/dataflow/Ssa.qll index e8ad8cbdceb..94ba657e94d 100644 --- a/swift/ql/lib/codeql/swift/dataflow/Ssa.qll +++ b/swift/ql/lib/codeql/swift/dataflow/Ssa.qll @@ -21,8 +21,6 @@ module Ssa { BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() } - class ExitBasicBlock = BasicBlocks::ExitBasicBlock; - private newtype TSourceVariable = TNormalSourceVariable(VarDecl v) or TKeyPathSourceVariable(EntryNode entry) { entry.getScope() instanceof KeyPathExpr }