Skip to content

Commit 66c5358

Browse files
committed
Binary: Add 'Type' to the IR.
1 parent 935eba2 commit 66c5358

11 files changed

Lines changed: 186 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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;
7+
private class TElement = @x86_instruction or @operand or @il_instruction or @method or @il_parameter or @type;
88

99
class Element extends TElement {
1010
final string toString() { none() }

binary/ql/lib/semmle/code/binary/ast/internal/CilInstructions.qll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
private import binary
22

3+
/**
4+
* A CIL type (class, struct, interface, etc.).
5+
*/
6+
class CilType extends @type {
7+
string toString() { result = this.getName() }
8+
9+
/** Gets the full name of this type (e.g., "System.Collections.Generic.List`1"). */
10+
string getFullName() { types(this, result, _, _) }
11+
12+
/** Gets the namespace of this type (e.g., "System.Collections.Generic"). */
13+
string getNamespace() { types(this, _, result, _) }
14+
15+
/** Gets the simple name of this type (e.g., "List`1"). */
16+
string getName() { types(this, _, _, result) }
17+
18+
/** Gets a method declared in this type. */
19+
CilMethod getAMethod() { result.getDeclaringType() = this }
20+
21+
Location getLocation() { none() } // TODO: Extract
22+
}
23+
324
/** A local variable defined in a CIL method body. */
425
class CilVariable extends @il_local_variable {
526
string toString() { result = "local_" + this.getIndex().toString() }
@@ -52,6 +73,8 @@ class CilMethod extends @method {
5273
result.getIndex() = i
5374
}
5475

76+
CilType getDeclaringType() { methods(this, _, _, result) }
77+
5578
Location getLocation() { none() } // TODO: Extract
5679
}
5780

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ private module FinalInstruction {
2020
Location getLocation() { result = super.getLocation() }
2121

2222
predicate isProgramEntryPoint() { super.isProgramEntryPoint() }
23+
24+
Type getDeclaringType() { result = super.getDeclaringType() }
25+
26+
predicate isPublic() { super.isPublic() }
27+
}
28+
29+
class Type instanceof Instruction::Type {
30+
Function getAFunction() { result = super.getAFunction() }
31+
32+
string toString() { result = super.toString() }
33+
34+
string getFullName() { result = super.getFullName() }
35+
36+
string getNamespace() { result = super.getNamespace() }
37+
38+
string getName() { result = super.getName() }
39+
40+
Location getLocation() { result = super.getLocation() }
2341
}
2442

2543
class Operand instanceof Instruction::Operand {

binary/ql/lib/semmle/code/binary/ast/ir/internal/Instruction0/Function.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ private import TranslatedFunction
22
private import Instruction
33
private import semmle.code.binary.ast.Location
44
private import BasicBlock
5+
private import Type
56

67
newtype TFunction = TMkFunction(TranslatedFunction f)
78

@@ -22,5 +23,7 @@ class Function extends TFunction {
2223

2324
predicate isProgramEntryPoint() { f.isProgramEntryPoint() }
2425

25-
predicate isExported() { f.isExported() }
26+
predicate isPublic() { f.isPublic() }
27+
28+
Type getDeclaringType() { result.getAFunction() = this }
2629
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import semmle.code.binary.ast.ir.internal.InstructionSig
33
module Instruction0 implements InstructionSig {
44
import Instruction
55
import Function
6+
import Type
67
import Operand
78
import Variable
89
import BasicBlock

binary/ql/lib/semmle/code/binary/ast/ir/internal/Instruction0/TranslatedElement.qll

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ private predicate shouldTranslateMethod(Raw::CilMethod m) { any() }
4444
*/
4545
private predicate shouldTranslateCilParameter(Raw::CilParameter p) { any() }
4646

47+
private predicate shouldTranslatedCilType(Raw::CilType t) { any() }
48+
4749
/**
4850
* The "base type" for all translated elements.
4951
*
@@ -124,8 +126,13 @@ newtype TTranslatedElement =
124126
TTranslatedCilLoadString(Raw::CilLdstr ldstr) { shouldTranslateCilInstr(ldstr) } or
125127
TTranslatedCilParameter(Raw::CilParameter param) { shouldTranslateCilParameter(param) } or
126128
TTranslatedCilLoadArg(Raw::CilLoadArgument ldstr) { shouldTranslateCilInstr(ldstr) } or
127-
TTranslatedCilLoadIndirect(Raw::CilLoadIndirectInstruction ldind) { shouldTranslateCilInstr(ldind) } or
128-
TTranslatedCilStoreIndirect(Raw::CilStoreIndirectInstruction stind) { shouldTranslateCilInstr(stind) }
129+
TTranslatedCilLoadIndirect(Raw::CilLoadIndirectInstruction ldind) {
130+
shouldTranslateCilInstr(ldind)
131+
} or
132+
TTranslatedCilStoreIndirect(Raw::CilStoreIndirectInstruction stind) {
133+
shouldTranslateCilInstr(stind)
134+
} or
135+
TTranslatedCilType(Raw::CilType type) { shouldTranslatedCilType(type) }
129136

130137
TranslatedElement getTranslatedElement(Raw::Element raw) {
131138
result.getRawElement() = raw and

binary/ql/lib/semmle/code/binary/ast/ir/internal/Instruction0/TranslatedFunction.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ abstract class TranslatedFunction extends TranslatedElement {
5151

5252
abstract predicate isProgramEntryPoint();
5353

54-
abstract predicate isExported();
54+
abstract predicate isPublic();
5555

5656
final override string getDumpId() { result = this.getName() }
5757

@@ -105,14 +105,14 @@ class TranslatedX86Function extends TranslatedFunction, TTranslatedX86Function {
105105
if this.isProgramEntryPoint()
106106
then result = "Program_entry_function"
107107
else
108-
if this.isExported()
108+
if this.isPublic()
109109
then result = "Exported_function_" + entry.getIndex()
110110
else result = "Function_" + entry.getIndex()
111111
}
112112

113113
final override predicate isProgramEntryPoint() { entry instanceof Raw::ProgramEntryInstruction }
114114

115-
final override predicate isExported() { entry instanceof Raw::ExportedEntryInstruction }
115+
final override predicate isPublic() { entry instanceof Raw::ExportedEntryInstruction }
116116

117117
final override predicate hasOrdering(LocalVariableTag tag, int ordering) {
118118
exists(Raw::X86Register r | tag = X86RegisterTag(r) |
@@ -217,7 +217,7 @@ class TranslatedCilMethod extends TranslatedFunction, TTranslatedCilMethod {
217217

218218
override predicate isProgramEntryPoint() { none() }
219219

220-
override predicate isExported() { none() }
220+
override predicate isPublic() { any() } // TODO: We need to extract this
221221

222222
override Instruction getBodyEntry() {
223223
result = this.getParameter(0).getEntry()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
private import semmle.code.binary.ast.Location
2+
private import semmle.code.binary.ast.instructions as Raw
3+
private import TranslatedElement
4+
private import codeql.util.Option
5+
private import semmle.code.binary.ast.ir.internal.Opcode as Opcode
6+
private import Variable
7+
private import Instruction
8+
private import TranslatedInstruction
9+
private import TranslatedFunction
10+
private import semmle.code.binary.ast.ir.internal.Tags
11+
private import InstructionTag
12+
private import codeql.controlflow.SuccessorType
13+
14+
abstract class TranslatedType extends TranslatedElement {
15+
final override predicate producesResult() { none() }
16+
17+
final override Variable getResultVariable() { none() }
18+
19+
final override Variable getVariableOperand(InstructionTag tag, OperandTag operandTag) { none() }
20+
21+
final FunEntryInstruction getEntry() { none() }
22+
23+
final override predicate hasInstruction(
24+
Opcode opcode, InstructionTag tag, Option<Variable>::Option v
25+
) {
26+
none()
27+
}
28+
29+
final override Instruction getSuccessor(InstructionTag tag, SuccessorType succType) { none() }
30+
31+
abstract string getName();
32+
33+
abstract string getNamespace();
34+
35+
abstract TranslatedFunction getAFunction();
36+
37+
final override string toString() { result = "Translation of " + this.getName() }
38+
39+
final override TranslatedFunction getEnclosingFunction() { none() }
40+
41+
final override Instruction getChildSuccessor(TranslatedElement child, SuccessorType succType) {
42+
none()
43+
}
44+
45+
final override string getDumpId() { result = this.getName() }
46+
}
47+
48+
class TranslatedCiLType extends TranslatedType, TTranslatedCilType {
49+
Raw::CilType type;
50+
51+
TranslatedCiLType() { this = TTranslatedCilType(type) }
52+
53+
final override Raw::Element getRawElement() { result = type }
54+
55+
final override string getName() { result = type.getName() }
56+
57+
final override string getNamespace() { result = type.getNamespace() }
58+
59+
final override TranslatedCilMethod getAFunction() {
60+
result = getTranslatedFunction(type.getAMethod())
61+
}
62+
63+
final override Location getLocation() { result = type.getLocation() }
64+
}
65+
66+
TranslatedType getTranslatedType(Raw::Element raw) { result.getRawElement() = raw }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
private import Function
2+
private import semmle.code.binary.ast.Location
3+
private import TranslatedType
4+
5+
newtype TType = TMkType(TranslatedType t)
6+
7+
class Type extends TType {
8+
TranslatedType t;
9+
10+
Type() { this = TMkType(t) }
11+
12+
Function getAFunction() { result = TMkFunction(t.getAFunction()) }
13+
14+
string toString() { result = this.getName() }
15+
16+
string getFullName() { result = this.getNamespace() + "." + this.getName() }
17+
18+
string getNamespace() { result = t.getNamespace() }
19+
20+
string getName() { result = t.getName() }
21+
22+
Location getLocation() { result = t.getLocation() }
23+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ private import codeql.controlflow.SuccessorType
44
private import semmle.code.binary.ast.Location
55

66
signature module InstructionSig {
7+
8+
class Type {
9+
Function getAFunction();
10+
11+
string toString();
12+
13+
string getFullName();
14+
15+
string getNamespace();
16+
17+
string getName();
18+
19+
Location getLocation();
20+
}
21+
722
class Function {
823
string getName();
924

@@ -16,6 +31,10 @@ signature module InstructionSig {
1631
Location getLocation();
1732

1833
predicate isProgramEntryPoint();
34+
35+
Type getDeclaringType();
36+
37+
predicate isPublic();
1938
}
2039

2140
class Operand {

0 commit comments

Comments
 (0)