Skip to content

Commit 01490f9

Browse files
committed
Binary: Make the instruction transformation framework skip dead instructions.
1 parent 51707bc commit 01490f9

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

binary/ql/lib/semmle/code/binary/ast/ir/internal/Instruction2/Instruction2.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ module InstructionInput implements Transform<Instruction1>::TransformInputSig {
152152
}
153153

154154
private predicate isStackPointerVariable(Instruction1::Variable v) {
155-
v.toString() = "rsp" // TODO: Something else here
155+
v instanceof Instruction1::StackPointer
156156
}
157157

158158
/** Holds if `def2 = def1 + k`. */
@@ -242,6 +242,12 @@ module InstructionInput implements Transform<Instruction1>::TransformInputSig {
242242
exists(TTranslatedLoad(instr))
243243
or
244244
exists(TTranslatedStore(instr))
245+
or
246+
exists(Ssa::Definition def |
247+
def.getInstruction() = instr and
248+
def.getSourceVariable() instanceof Instruction1::TempVariable and
249+
forex(Instruction1::Operand op | op = def.getARead() | isRemovedInstruction(op.getUse())) // TODO: Recursion through forex is bad for performance
250+
)
245251
}
246252

247253
abstract class TranslatedElement extends TTranslatedElement {

binary/ql/lib/semmle/code/binary/ast/ir/internal/InstructionSig.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ signature module InstructionSig {
5454
Operand getAnAccess();
5555
}
5656

57+
class StackPointer extends Variable;
58+
59+
class FramePointer extends Variable;
60+
61+
class TempVariable extends Variable;
62+
5763
class BasicBlock {
5864
ControlFlowNode getNode(int index);
5965

binary/ql/lib/semmle/code/binary/ast/ir/internal/TransformInstruction/TransformInstruction.qll

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ module Transform<InstructionSig Input> {
156156
Operand getAnAccess() { result.getVariable() = this }
157157
}
158158

159+
class StackPointer extends Variable {
160+
StackPointer() { this.asOldVariable() instanceof Input::StackPointer }
161+
}
162+
163+
class FramePointer extends Variable {
164+
FramePointer() { this.asOldVariable() instanceof Input::FramePointer }
165+
}
166+
167+
class TempVariable extends Variable {
168+
TempVariable() {
169+
this.asOldVariable() instanceof Input::TempVariable
170+
or
171+
this.isNewVariable(_, _)
172+
}
173+
}
174+
159175
final private class FinalTranslatedElement = TransformInput::TranslatedElement;
160176

161177
private class TranslatedElement extends FinalTranslatedElement {
@@ -574,6 +590,22 @@ module Transform<InstructionSig Input> {
574590
result = any(TranslatedElement te).getInstructionSuccessor(old, succType)
575591
}
576592

593+
private Input::Instruction getASuccessorIfRemoved(Input::Instruction i) {
594+
TransformInput::isRemovedInstruction(i) and
595+
result = i.getASuccessor()
596+
}
597+
598+
private Input::Instruction getSuccessorFromNonRemoved(Input::Instruction i, SuccessorType t) {
599+
result = i.getSuccessor(t) and
600+
not TransformInput::isRemovedInstruction(i)
601+
or
602+
result = getASuccessorIfRemoved(getSuccessorFromNonRemoved(i, t))
603+
}
604+
605+
private Input::Instruction getNonRemovedSuccessor(Input::Instruction i, SuccessorType t) {
606+
result = getSuccessorFromNonRemoved(i, t) and not TransformInput::isRemovedInstruction(result)
607+
}
608+
577609
private class OldInstruction extends TOldInstruction, Instruction {
578610
Input::Instruction old;
579611

@@ -590,7 +622,7 @@ module Transform<InstructionSig Input> {
590622
override Instruction getSuccessor(SuccessorType succType) {
591623
exists(Input::Instruction oldSucc |
592624
not exists(getInstructionSuccessor(old, _)) and
593-
oldSucc = old.getSuccessor(succType) and
625+
oldSucc = getNonRemovedSuccessor(old, succType) and
594626
result = getNewInstruction(oldSucc)
595627
)
596628
or

0 commit comments

Comments
 (0)