ROSE 0.11.145.272
ModulesX86.h
1#ifndef ROSE_BinaryAnalysis_Partitioner2_ModulesX86_H
2#define ROSE_BinaryAnalysis_Partitioner2_ModulesX86_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
6
7#include <Rose/BinaryAnalysis/Partitioner2/JumpTable.h>
8#include <Rose/BinaryAnalysis/Partitioner2/Modules.h>
9
10namespace Rose {
11namespace BinaryAnalysis {
12namespace Partitioner2 {
13
15namespace ModulesX86 {
16
18// Classes for matching function prologues
20
33protected:
34 FunctionPtr function_;
35protected:
37public:
39
40public:
41 static Ptr instance();
42 virtual std::vector<FunctionPtr> functions() const override;
43 virtual bool match(const PartitionerConstPtr&, Address anchor) override;
44};
45
55public:
56 static Ptr instance() { return Ptr(new MatchHotPatchPrologue); }
57 virtual std::vector<FunctionPtr> functions() const override { return std::vector<FunctionPtr>(1, function_); }
58 virtual bool match(const PartitionerConstPtr&, Address anchor) override;
59};
60
63protected:
64 FunctionPtr function_;
65protected:
67public:
69
70public:
71 static Ptr instance();
72 virtual std::vector<FunctionPtr> functions() const override;
73 virtual bool match(const PartitionerConstPtr&, Address anchor) override;
74};
75
78protected:
79 FunctionPtr function_;
80public:
81 static Ptr instance() { return Ptr(new MatchEnterPrologue); }
82 virtual std::vector<FunctionPtr> functions() const override { return std::vector<FunctionPtr>(1, function_); }
83 virtual bool match(const PartitionerConstPtr&, Address anchor) override;
84};
85
88protected:
89 FunctionPtr function_;
90public:
91 static Ptr instance() { return Ptr(new MatchRetPadPush); }
92 virtual std::vector<FunctionPtr> functions() const override { return std::vector<FunctionPtr>(1, function_); }
93 virtual bool match(const PartitionerConstPtr&, Address anchor) override;
94};
95
97// FunctionReturnDetector for CFG edges at a function return
99
107public:
108 static Ptr instance() { return Ptr(new FunctionReturnDetector); }
109 virtual bool operator()(bool chain, const Args&) override;
110};
111
113// SwitchSuccessors for CFG edges from a C `switch` statement or similar
115
124public:
125private:
126 // These get filled in during the pattern matching phase when we're looking at the content of the basic block in question to try
127 // to figure out if it has a jump table and if so, some characteristics of that table. */
128 std::string matcherName_; // pattern matcher that matched
129 Sawyer::Optional<Address> tableVa_; // possible address for jump table
131 size_t entrySizeBytes_ = 4; // size of each table entry
132 Address entryOffset_ = 0; // value added to every table entry
133
134 // These members are initialized after the jump table is loaded and are used to update the successors and attach basic blocks.
135 JumpTable::Ptr mainTable_;
136 std::vector<uint8_t> indexes_; // indexes into the main table immediately following the main table
137
138public:
141 static Ptr instance();
142 virtual bool operator()(bool chain, const Args&) override;
143
144 // Adjust successors and for the basic block, create a data block, and add everything to the partitioner.
145 void addToPartitioner(const Args&, const std::set<Address> &successors) const;
146
147private:
148 // Pattern matchers that look at
149 bool matchPattern1(SgAsmExpression *jmpArg);
150 bool matchPattern2(const BasicBlockPtr&, SgAsmInstruction *jmp);
151 bool matchPattern3(const PartitionerConstPtr&, const BasicBlockPtr&, SgAsmInstruction *jmp);
152 bool matchPattern4(const PartitionerConstPtr&, const BasicBlockPtr&);
153 bool matchPattern5(const PartitionerConstPtr&, const BasicBlockPtr&);
154 bool matchPatterns(const PartitionerConstPtr&, const BasicBlockPtr&);
155
156 // Try to parse the jump table. If successful, create a `table` in this object.
157 void parseJumpTable(const Args&);
158};
159
161// Supporting functions used by this module
163
166
171
174
177
180
183
186
189
201
202
203} // namespace
204} // namespace
205} // namespace
206} // namespace
207
208#endif
209#endif
Base class for adjusting basic blocks during discovery.
Definition Modules.h:39
BasicBlockCallbackPtr Ptr
Shared-ownership pointer to a BasicBlockCallback.
Definition Modules.h:42
Base class for matching function prologues.
Definition Modules.h:108
FunctionPrologueMatcherPtr Ptr
Shared-ownership pointer to a FunctionPrologueMatcher.
Definition Modules.h:111
JumpTablePtr Ptr
Shared ownership pointer.
Definition JumpTable.h:20
EntryType
How target addresses are computed from entries.
Definition JumpTable.h:25
@ ABSOLUTE
Target is entry plus (nominally zero) constant offset.
Basic block callback to detect function returns.
Definition ModulesX86.h:106
virtual bool operator()(bool chain, const Args &) override
Callback method.
Matches an x86 MOV EDI,EDI; PUSH ESI function prologe.
Definition ModulesX86.h:62
virtual bool match(const PartitionerConstPtr &, Address anchor) override
Attempt to match an instruction pattern.
virtual std::vector< FunctionPtr > functions() const override
Returns the function(s) for the previous successful match.
virtual std::vector< FunctionPtr > functions() const override
Returns the function(s) for the previous successful match.
Definition ModulesX86.h:82
virtual bool match(const PartitionerConstPtr &, Address anchor) override
Attempt to match an instruction pattern.
Matches an x86 function prologue with hot patch.
Definition ModulesX86.h:54
virtual bool match(const PartitionerConstPtr &, Address anchor) override
Attempt to match an instruction pattern.
virtual std::vector< FunctionPtr > functions() const override
Returns the function(s) for the previous successful match.
Definition ModulesX86.h:57
Match RET followed by PUSH with intervening no-op padding.
Definition ModulesX86.h:87
virtual bool match(const PartitionerConstPtr &, Address anchor) override
Attempt to match an instruction pattern.
virtual std::vector< FunctionPtr > functions() const override
Returns the function(s) for the previous successful match.
Definition ModulesX86.h:92
virtual std::vector< FunctionPtr > functions() const override
Returns the function(s) for the previous successful match.
virtual bool match(const PartitionerConstPtr &, Address anchor) override
Attempt to match an instruction pattern.
Basic block callback to detect "switch" statements.
Definition ModulesX86.h:123
virtual bool operator()(bool chain, const Args &) override
Callback method.
Holds a value or nothing.
Definition Optional.h:56
Base class for expressions.
Base class for machine instructions.
Represents one Intel x86 machine instruction.
Sawyer::Optional< Address > findTableBase(SgAsmExpression *)
Try to match a base+offset expression.
bool matchMovBpSp(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "MOV EBP, ESP" or variant.
bool matchEnterAnyZero(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "ENTER x, 0".
bool matchPushBp(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "PUSH EBP" or variant.
bool matchMovDiDi(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "MOV EDI, EDI" or variant.
bool matchPushSi(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "PUSH SI" or variant.
Sawyer::Optional< Address > matchJmpConst(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "JMP constant".
bool matchJmpMem(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "JMP [address]" or variant.
bool matchLeaCxMemBpConst(const PartitionerConstPtr &, SgAsmX86Instruction *)
Matches "LEA ECX, [EBP + constant]" or variant.
std::uint64_t Address
Address.
Definition Address.h:11
The ROSE library.