Skip to content

Commit c1f1711

Browse files
committed
Binary: Add public-facing SSA predicates.
1 parent 62b17a4 commit c1f1711

3 files changed

Lines changed: 14 additions & 15 deletions

File tree

binary/ql/lib/semmle/code/binary/ast/ir/IR.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import codeql.controlflow.SuccessorType
33
private import semmle.code.binary.ast.ir.internal.Opcode
44
private import semmle.code.binary.ast.ir.internal.Tags
55
private import codeql.controlflow.BasicBlock as BB
6+
private import semmle.code.binary.dataflow.Ssa
67

78
private module FinalInstruction {
89
private import internal.Instruction2.Instruction2::Instruction2 as Instruction
@@ -33,6 +34,10 @@ private module FinalInstruction {
3334
OperandTag getOperandTag() { result = super.getOperandTag() }
3435

3536
Location getLocation() { result = super.getLocation() }
37+
38+
Ssa::Definition getDef() { result.getARead() = this }
39+
40+
Instruction getAnyDef() { result = this.getDef().getAnUltimateDefinition().asInstruction() }
3641
}
3742

3843
class StoreValueOperand extends Operand instanceof Instruction::StoreValueOperand { }
@@ -227,6 +232,8 @@ private module FinalInstruction {
227232
InstructionTag getInstructionTag() { result = super.getInstructionTag() }
228233

229234
Operand getFirstOperand() { result = super.getFirstOperand() }
235+
236+
Operand getAUse() { result.getDef().asInstruction() = this }
230237
}
231238

232239
class RetInstruction extends Instruction instanceof Instruction::RetInstruction { }

binary/ql/lib/semmle/code/binary/dataflow/Ssa.qll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ module Ssa {
99
private import semmle.code.binary.ast.ir.IR
1010
private import internal.SsaImpl as SsaImpl
1111

12-
class Variable = SsaImpl::SsaInput::SourceVariable;
13-
1412
/** A static single assignment (SSA) definition. */
1513
class Definition extends SsaImpl::Definition {
1614
final ControlFlowNode getControlFlowNode() {
@@ -28,6 +26,8 @@ module Ssa {
2826
not result instanceof PhiDefinition
2927
}
3028

29+
Instruction asInstruction() { result = this.getControlFlowNode().asInstruction() }
30+
3131
/** Gets the function of this SSA definition. */
3232
Function getFunction() { result = this.getBasicBlock().getEnclosingFunction() }
3333
}
@@ -45,9 +45,7 @@ module Ssa {
4545
/** Gets the underlying write access. */
4646
final Instruction getWriteAccess() { result = write }
4747

48-
predicate assigns(Operand value) {
49-
value = write.(CopyInstruction).getOperand()
50-
}
48+
predicate assigns(Operand value) { value = write.(CopyInstruction).getOperand() }
5149
}
5250

5351
class PhiDefinition extends Definition, SsaImpl::PhiDefinition {

binary/ql/lib/semmle/code/binary/dataflow/internal/SsaImpl.qll

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@ private predicate variableReadCertain(BasicBlock bb, int i, Operand va, Variable
66
va = v.getAnAccess()
77
}
88

9-
module SsaInput implements SsaImplCommon::InputSig<Location, BinaryCfg::BasicBlock> {
9+
private module SsaInput implements SsaImplCommon::InputSig<Location, BasicBlock> {
1010
class SourceVariable = Variable;
1111

12-
predicate variableWrite(BinaryCfg::BasicBlock bb, int i, SourceVariable v, boolean certain) {
13-
v = bb.getNode(i).asInstruction().getResultVariable() and
12+
predicate variableWrite(BasicBlock bb, int i, SourceVariable v, boolean certain) {
13+
bb.getNode(i).asInstruction().getResultVariable() = v and
1414
certain = true
15-
// or
16-
// certain = true and
17-
// bb.isFunctionEntryBasicBlock() and
18-
// i = -1 and
19-
// // TODO: Generalize beyond rsp
20-
// v.(RegisterVariable).getRegister().toString() = "rsp"
2115
}
2216

23-
predicate variableRead(BinaryCfg::BasicBlock bb, int i, SourceVariable v, boolean certain) {
17+
predicate variableRead(BasicBlock bb, int i, SourceVariable v, boolean certain) {
2418
variableReadCertain(bb, i, _, v) and certain = true
2519
}
2620
}

0 commit comments

Comments
 (0)