Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Jul 1, 2024
1 parent 7fdc09c commit 6d9de86
Show file tree
Hide file tree
Showing 19 changed files with 565 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,7 @@ module IteratorFlow {
* Holds if `(bb, i)` contains a write to an iterator that may have been obtained
* by calling `begin` (or related functions) on the variable `v`.
*/
predicate variableWrite(IRBlock bb, int i, SourceVariable v, boolean certain) {
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
certain = false and
exists(GetsIteratorCall beginCall, Instruction writeToDeref, IRBlock bbQual, int iQual |
isIteratorStoreInstruction(beginCall, writeToDeref) and
Expand All @@ -1820,7 +1820,7 @@ module IteratorFlow {
}

/** Holds if `(bb, i)` reads the container variable `v`. */
predicate variableRead(IRBlock bb, int i, SourceVariable v, boolean certain) {
predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain) {
Ssa::variableRead(bb, i, v, certain)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
* Holds if the `i`'th write in block `bb` writes to the variable `v`.
* `certain` is `true` if the write is guaranteed to overwrite the entire variable.
*/
predicate variableWrite(IRBlock bb, int i, SourceVariable v, boolean certain) {
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
DataFlowImplCommon::forceCachingInSameStage() and
(
exists(DefImpl def | def.hasIndexInBlock(bb, i, v) |
Expand All @@ -999,7 +999,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
* Holds if the `i`'th read in block `bb` reads to the variable `v`.
* `certain` is `true` if the read is guaranteed. For C++, this is always the case.
*/
predicate variableRead(IRBlock bb, int i, SourceVariable v, boolean certain) {
predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain) {
exists(UseImpl use | use.hasIndexInBlock(bb, i, v) |
if use.isCertain() then certain = true else certain = false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,13 +757,17 @@ import Cached
* between the SSA pruning stage, and the final SSA stage.
*/
module InputSigCommon {
class BasicBlock = IRBlock;
class BasicBlock extends IRBlock {
Location getLocation(int i) { result = this.getInstruction(i).getLocation() }

int length() { result = this.getInstructionCount() }
}

BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result.immediatelyDominates(bb) }

BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }

class ExitBasicBlock extends IRBlock {
class ExitBasicBlock extends BasicBlock {
ExitBasicBlock() { this.getLastInstruction() instanceof ExitFunctionInstruction }
}
}
7 changes: 5 additions & 2 deletions csharp/ql/lib/semmle/code/cil/internal/SsaImpl.qll
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
private import cil
private import CIL
private import codeql.ssa.Ssa as SsaImplCommon

deprecated private module SsaInput implements SsaImplCommon::InputSig<CIL::Location> {
class BasicBlock = CIL::BasicBlock;
class BasicBlock extends CIL::BasicBlock {
Location getLocation(int i) { result = this.getNode(i).getLocation() }
}

BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result = bb.getImmediateDominator() }

BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }

class ExitBasicBlock = CIL::ExitBasicBlock;
class ExitBasicBlock extends BasicBlock, CIL::ExitBasicBlock { }

class SourceVariable = CIL::StackVariable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ module PreSsa {
}

module SsaInput implements SsaImplCommon::InputSig<Location> {
class BasicBlock = PreBasicBlocks::PreBasicBlock;
class BasicBlock extends PreBasicBlocks::PreBasicBlock {
Location getLocation(int i) { result = this.getElement(i).getLocation() }
}

BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result.immediatelyDominates(bb) }

Expand Down Expand Up @@ -192,7 +194,7 @@ module PreSsa {
SsaImpl::ssaDefReachesEndOfBlock(bb, this, _)
}

Location getLocation() {
override Location getLocation() {
result = this.getDefinition().getLocation()
or
exists(Callable c, SsaInput::BasicBlock bb, SsaInput::SourceVariable v |
Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/lib/semmle/code/csharp/dataflow/SSA.qll
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ module Ssa {
}

/** Gets the location of this SSA definition. */
Location getLocation() { none() }
override Location getLocation() { none() }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ module BaseSsa {
private module SsaInput implements SsaImplCommon::InputSig<Location> {
private import semmle.code.csharp.controlflow.internal.PreSsa

class BasicBlock = ControlFlow::BasicBlock;
class BasicBlock extends ControlFlow::BasicBlock {
Location getLocation(int i) { result = this.getNode(i).getLocation() }
}

BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) {
result = bb.getImmediateDominator()
}

BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }

class ExitBasicBlock = ControlFlow::BasicBlocks::ExitBlock;
class ExitBasicBlock extends BasicBlock, ControlFlow::BasicBlocks::ExitBlock { }

class SourceVariable = PreSsa::SimpleLocalScopeVariable;

Expand Down Expand Up @@ -93,7 +95,7 @@ module BaseSsa {
not result instanceof PhiNode
}

Location getLocation() {
override Location getLocation() {
result = this.getDefinition().getLocation()
or
exists(Callable c, SsaInput::BasicBlock bb, SsaInput::SourceVariable v |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ module VariableCapture {
private import TaintTrackingPrivate as TaintTrackingPrivate

class BasicBlock extends BasicBlocks::BasicBlock {
Location getLocation(int i) { result = this.getNode(i).getLocation() }

Callable getEnclosingCallable() { result = super.getCallable() }
}

Expand Down
12 changes: 7 additions & 5 deletions csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ private import AssignableDefinitions
private import semmle.code.csharp.controlflow.internal.PreSsa

private module SsaInput implements SsaImplCommon::InputSig<Location> {
class BasicBlock = ControlFlow::BasicBlock;
class BasicBlock extends ControlFlow::BasicBlock {
Location getLocation(int i) { result = this.getNode(i).getLocation() }
}

BasicBlock getImmediateBasicBlockDominator(BasicBlock bb) { result = bb.getImmediateDominator() }

BasicBlock getABasicBlockSuccessor(BasicBlock bb) { result = bb.getASuccessor() }

class ExitBasicBlock = ControlFlow::BasicBlocks::ExitBlock;
class ExitBasicBlock extends BasicBlock, ControlFlow::BasicBlocks::ExitBlock { }

class SourceVariable = Ssa::SourceVariable;

Expand All @@ -24,7 +26,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
*
* This includes implicit writes via calls.
*/
predicate variableWrite(ControlFlow::BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
predicate variableWrite(BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
variableWriteDirect(bb, i, v, certain)
or
variableWriteQualifier(bb, i, v, certain)
Expand All @@ -38,7 +40,7 @@ private module SsaInput implements SsaImplCommon::InputSig<Location> {
*
* This includes implicit reads via calls.
*/
predicate variableRead(ControlFlow::BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
predicate variableRead(BasicBlock bb, int i, Ssa::SourceVariable v, boolean certain) {
variableReadActual(bb, i, v) and
certain = true
or
Expand Down Expand Up @@ -1089,7 +1091,7 @@ class DefinitionExt extends Impl::DefinitionExt {
override string toString() { result = this.(Ssa::Definition).toString() }

/** Gets the location of this definition. */
Location getLocation() { result = this.(Ssa::Definition).getLocation() }
override Location getLocation() { result = this.(Ssa::Definition).getLocation() }

/** Gets the enclosing callable of this definition. */
Callable getEnclosingCallable() { result = this.(Ssa::Definition).getEnclosingCallable() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ private module CaptureInput implements VariableCapture::InputSig<Location> {
class BasicBlock instanceof J::BasicBlock {
string toString() { result = super.toString() }

Location getLocation(int i) { result = super.getNode(i).getLocation() }

int length() { result = super.length() }

Callable getEnclosingCallable() { result = super.getEnclosingCallable() }

Location getLocation() { result = super.getLocation() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ private module CaptureInput implements Shared::InputSig<Location> {
}

class BasicBlock extends PY::BasicBlock {
Location getLocation(int i) { result = this.getNode(i).getLocation() }

int length() { result = count(int i | exists(this.getNode(i))) }

Callable getEnclosingCallable() { result = this.getScope() }

// Note `PY:BasicBlock` does not have a `getLocation`.
Expand Down
3 changes: 0 additions & 3 deletions ruby/ql/lib/codeql/ruby/dataflow/SSA.qll
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ module Ssa {

override string toString() { result = this.getControlFlowNode().toString() }

/** Gets the location of this SSA definition. */
Location getLocation() { result = this.getControlFlowNode().getLocation() }

/** Gets the scope of this SSA definition. */
CfgScope getScope() { result = this.getBasicBlock().getScope() }
}
Expand Down
Loading

0 comments on commit 6d9de86

Please sign in to comment.