Skip to content

Commit ba28175

Browse files
committed
Binary: Small cleanup by moving some x86 stuff into another file like we do for CIL and JVM instructions.
1 parent 0fd33da commit ba28175

File tree

4 files changed

+260
-235
lines changed

4 files changed

+260
-235
lines changed

binary/ql/lib/semmle/code/binary/ast/instructions.qll

Lines changed: 16 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ private import Headers
44
private import Sections
55
private import codeql.util.Unit
66

7-
private class TElement = @x86_instruction or @operand or @il_instruction or @method or @il_parameter or @type or @jvm_instruction or @jvm_parameter;
7+
private class TElement =
8+
@x86_instruction or @operand or @il_instruction or @method or @il_parameter or @type or
9+
@jvm_instruction or @jvm_parameter;
810

911
class Element extends TElement {
1012
final string toString() { none() }
1113
}
1214

13-
private class X86InstructionElement extends Element {
15+
private class X86InstructionElement extends Element, @x86_instruction {
1416
final string toString() {
1517
exists(string sInstr |
1618
instruction_string(this, sInstr) and
@@ -25,245 +27,34 @@ private class X86InstructionElement extends Element {
2527
}
2628
}
2729

28-
private class X86OperandElement extends Element {
30+
private class X86OperandElement extends Element, @operand {
2931
final string toString() { operand_string(this, result) }
3032
}
3133

32-
private class IlInstructionElement extends Element {
34+
private class IlInstructionElement extends Element, @il_instruction {
3335
final string toString() { instruction_string(this, result) }
3436
}
3537

36-
private class CilMethodElement extends Element {
38+
private class CilMethodElement extends Element, @method {
3739
final string toString() { methods(this, result, _, _) }
3840
}
3941

40-
private module Pre {
41-
module PreInput implements Internal::InstructionInputSig {
42-
class BaseX86Instruction extends Internal::X86Instruction {
43-
private string toString0() { instruction_string(this, result) }
44-
45-
override string toString() {
46-
if exists(this.getAnOperand())
47-
then
48-
result =
49-
this.toString0() + " " +
50-
strictconcat(int i, string s | s = this.getOperand(i).toString() | s, ", " order by i)
51-
else result = this.toString0()
52-
}
53-
}
54-
55-
class BaseX86Register extends Internal::X86Register {
56-
BaseX86Register getASubRegister() { result = super.getASubRegister() }
57-
}
58-
59-
class BaseRipRegister extends BaseX86Register instanceof Internal::RipRegister { }
60-
61-
class BaseRspRegister extends BaseX86Register instanceof Internal::RspRegister { }
62-
63-
class BaseRbpRegister extends BaseX86Register instanceof Internal::RbpRegister { }
64-
65-
class BaseRcxRegister extends BaseX86Register instanceof Internal::RcxRegister { }
66-
67-
class BaseRdxRegister extends BaseX86Register instanceof Internal::RdxRegister { }
68-
69-
class BaseR8Register extends BaseX86Register instanceof Internal::R8Register { }
70-
71-
class BaseR9Register extends BaseX86Register instanceof Internal::R9Register { }
72-
73-
class BaseX86Operand extends Internal::X86Operand { }
74-
75-
class BaseX86RegisterAccess extends Internal::X86RegisterAccess {
76-
BaseX86Register getTarget() { result = super.getTarget() }
77-
}
78-
79-
class BaseX86UnusedOperand extends BaseX86Operand, Internal::X86UnusedOperand { }
80-
81-
class BaseX86RegisterOperand extends BaseX86Operand, Internal::X86RegisterOperand {
82-
BaseX86RegisterAccess getAccess() { result = super.getAccess() }
83-
}
84-
85-
class BaseX86PointerOperand extends BaseX86Operand, Internal::X86PointerOperand { }
86-
87-
class BaseX86ImmediateOperand extends BaseX86Operand, Internal::X86ImmediateOperand { }
88-
89-
abstract private class MyCall extends BaseX86Instruction instanceof Internal::X86Call {
90-
Internal::X86Operand op;
91-
92-
MyCall() { op = this.getOperand(0) }
93-
94-
abstract Internal::X86Instruction getTarget();
95-
}
96-
97-
private class CallImmediate extends MyCall {
98-
override Internal::X86ImmediateOperand op;
99-
BaseX86Instruction target;
100-
101-
CallImmediate() {
102-
op.isRelative() and
103-
op.getValue().toBigInt() + this.getIndex() + this.getLength().toBigInt() = target.getIndex()
104-
}
105-
106-
override Internal::X86Instruction getTarget() { result = target }
107-
}
108-
109-
class BaseX86MemoryOperand extends X86Operand instanceof Internal::X86MemoryOperand {
110-
predicate hasDisplacement() { super.hasDisplacement() }
111-
112-
BaseX86RegisterAccess getSegmentRegister() { result = super.getSegmentRegister() }
113-
114-
BaseX86RegisterAccess getBaseRegister() { result = super.getBaseRegister() }
115-
116-
BaseX86RegisterAccess getIndexRegister() { result = super.getIndexRegister() }
117-
118-
int getScaleFactor() { result = super.getScaleFactor() }
119-
120-
int getDisplacementValue() { result = super.getDisplacementValue() }
121-
}
122-
123-
private class CallConstantMemoryOperand extends MyCall {
124-
override Internal::X86MemoryOperand op;
125-
int displacement;
126-
127-
CallConstantMemoryOperand() {
128-
op.getBaseRegister().getTarget() instanceof Internal::RipRegister and
129-
not exists(op.getIndexRegister()) and
130-
displacement = op.getDisplacementValue()
131-
}
132-
133-
final override BaseX86Instruction getTarget() {
134-
exists(
135-
QlBuiltins::BigInt rip, QlBuiltins::BigInt effectiveVA,
136-
QlBuiltins::BigInt offsetWithinSection, RDataSection rdata, QlBuiltins::BigInt address
137-
|
138-
rip = this.getVirtualAddress() + this.getLength().toBigInt() and
139-
effectiveVA = rip + displacement.toBigInt() and
140-
offsetWithinSection = effectiveVA - rdata.getVirtualAddress().toBigInt() and
141-
address = rdata.read8Bytes(offsetWithinSection) - any(OptionalHeader h).getImageBase() and
142-
result.getVirtualAddress() = address
143-
)
144-
}
145-
}
146-
147-
BaseX86Instruction getCallTarget(BaseX86Instruction b) { result = b.(MyCall).getTarget() }
148-
149-
abstract private class MyJumping extends BaseX86Instruction instanceof Internal::X86JumpingInstruction
150-
{
151-
abstract BaseX86Instruction getTarget();
152-
}
153-
154-
private class ImmediateRelativeJumping extends MyJumping {
155-
X86ImmediateOperand op;
156-
157-
ImmediateRelativeJumping() { op = this.getOperand(0) and op.isRelative() }
158-
159-
final override BaseX86Instruction getTarget() {
160-
op.getValue().toBigInt() + this.getIndex() + this.getLength().toBigInt() = result.getIndex()
161-
}
162-
}
163-
164-
BaseX86Instruction getJumpTarget(BaseX86Instruction b) { result = b.(MyJumping).getTarget() }
165-
}
166-
167-
import Internal::MakeInstructions<PreInput> as Instructions
168-
}
169-
170-
private int getOffsetOfEntryPoint() {
171-
result = any(OptionalHeader x).getEntryPoint() - any(TextSection s).getVirtualAddress()
42+
private class CilParameterElement extends Element, @il_parameter {
43+
final string toString() { il_parameter(this, _, _, result) }
17244
}
17345

174-
private int getOffsetOfAnExportedFunction() {
175-
result = any(ExportTableEntry e).getAddress() - any(TextSection s).getVirtualAddress()
46+
private class TypeElement extends Element, @type {
47+
final string toString() { types(this, result, _, _) }
17648
}
17749

178-
private module Input implements Internal::InstructionInputSig {
179-
private class ProgramEntryInstruction0 extends Pre::Instructions::X86Instruction {
180-
ProgramEntryInstruction0() { this.getIndex() = getOffsetOfEntryPoint().toBigInt() }
181-
}
182-
183-
private class ExportedInstruction0 extends Pre::Instructions::X86Instruction {
184-
ExportedInstruction0() { this.getIndex() = getOffsetOfAnExportedFunction().toBigInt() }
185-
}
186-
187-
private predicate fwd(Pre::Instructions::X86Instruction i) {
188-
i instanceof ProgramEntryInstruction0
189-
or
190-
i instanceof ExportedInstruction0
191-
or
192-
exists(Pre::Instructions::X86Instruction i0 | fwd(i0) |
193-
i0.getASuccessor() = i
194-
or
195-
Pre::PreInput::getCallTarget(i0) = i
196-
)
197-
}
198-
199-
class BaseX86Instruction extends Pre::Instructions::X86Instruction {
200-
BaseX86Instruction() { fwd(this) }
201-
}
202-
203-
BaseX86Instruction getCallTarget(BaseX86Instruction b) {
204-
result = Pre::PreInput::getCallTarget(b)
205-
}
206-
207-
BaseX86Instruction getJumpTarget(BaseX86Instruction b) {
208-
result = Pre::PreInput::getJumpTarget(b)
209-
}
210-
211-
class BaseX86Register extends Pre::Instructions::X86Register {
212-
BaseX86Register getASubRegister() { result = super.getASubRegister() }
213-
}
214-
215-
class BaseRipRegister extends BaseX86Register instanceof Pre::Instructions::RipRegister { }
216-
217-
class BaseRspRegister extends BaseX86Register instanceof Pre::Instructions::RspRegister { }
218-
219-
class BaseRbpRegister extends BaseX86Register instanceof Pre::Instructions::RbpRegister { }
220-
221-
class BaseRcxRegister extends BaseX86Register instanceof Pre::Instructions::RcxRegister { }
222-
223-
class BaseRdxRegister extends BaseX86Register instanceof Pre::Instructions::RdxRegister { }
224-
225-
class BaseR8Register extends BaseX86Register instanceof Pre::Instructions::R8Register { }
226-
227-
class BaseR9Register extends BaseX86Register instanceof Pre::Instructions::R9Register { }
228-
229-
class BaseX86Operand extends Pre::Instructions::X86Operand {
230-
BaseX86Operand() { this.getUse() instanceof BaseX86Instruction }
231-
}
232-
233-
class BaseX86RegisterAccess extends Pre::Instructions::X86RegisterAccess {
234-
BaseX86Register getTarget() { result = super.getTarget() }
235-
}
236-
237-
class BaseX86UnusedOperand extends BaseX86Operand, Pre::Instructions::X86UnusedOperand { }
238-
239-
class BaseX86RegisterOperand extends BaseX86Operand, Pre::Instructions::X86RegisterOperand {
240-
BaseX86RegisterAccess getAccess() { result = super.getAccess() }
241-
}
242-
243-
final private class FinalBaseX86Operand = BaseX86Operand;
244-
245-
class BaseX86MemoryOperand extends FinalBaseX86Operand, Pre::Instructions::X86MemoryOperand {
246-
BaseX86RegisterAccess getSegmentRegister() { result = super.getSegmentRegister() }
247-
248-
BaseX86RegisterAccess getBaseRegister() { result = super.getBaseRegister() }
249-
250-
BaseX86RegisterAccess getIndexRegister() { result = super.getIndexRegister() }
251-
}
252-
253-
class BaseX86PointerOperand extends BaseX86Operand, Pre::Instructions::X86PointerOperand { }
254-
255-
class BaseX86ImmediateOperand extends BaseX86Operand, Pre::Instructions::X86ImmediateOperand { }
256-
}
257-
258-
import Internal::MakeInstructions<Input>
259-
260-
class ProgramEntryInstruction extends X86Instruction {
261-
ProgramEntryInstruction() { this.getIndex() = getOffsetOfEntryPoint().toBigInt() }
50+
private class JvmInstructionElement extends Element, @jvm_instruction {
51+
final string toString() { instruction_string(this, result) }
26252
}
26353

264-
class ExportedEntryInstruction extends X86Instruction {
265-
ExportedEntryInstruction() { this.getIndex() = getOffsetOfAnExportedFunction().toBigInt() }
54+
private class JvmParameterElement extends Element, @jvm_parameter {
55+
final string toString() { jvm_parameter(this, _, _, result, _) }
26656
}
26757

58+
import internal.X86Instructions
26859
import internal.CilInstructions
26960
import internal.JvmInstructions

0 commit comments

Comments
 (0)