ROSE 0.11.145.192
CodeInserter.h
1#ifndef ROSE_BinaryAnalysis_CodeInserter_H
2#define ROSE_BinaryAnalysis_CodeInserter_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/AddressIntervalSet.h>
7#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
8
9#include <Sawyer/Map.h>
10#include <Sawyer/Message.h>
11
13
14namespace Rose {
15namespace BinaryAnalysis {
16
18namespace Commit {
19enum Boolean {
21 YES
22};
23} // namespace
24
27public:
33
40
106
116 struct Relocation {
117 size_t offset;
119 rose_addr_t value;
127 Relocation(size_t offset, RelocType type, rose_addr_t value)
128 : offset(offset), type(type), value(value) {}
129 };
130
138
146
147protected:
148 Partitioner2::PartitionerConstPtr partitioner_; // not null
149 AddressInterval chunkAllocationRegion_; // restricts addresses where new free-space chunks can be mapped
150 size_t minChunkAllocationSize_; // size of each chunk in bytes (also the alignment)
151 size_t chunkAllocationAlignment_; // alignment for allocating large chunks
152 std::string chunkAllocationName_; // name to give new areas of the memory map
153 AddressIntervalSet allocatedChunks_; // large allocated chunks that populate freeSpace_
154 AddressIntervalSet freeSpace_; // parts of mapped memory serving as free space for allocations
155 unsigned aggregationDirection_; // AggregationDirection bits
156 NopPadding nopPadding_; // where to add no-op padding
157
158public:
161
162public:
165
169 static void initDiagnostics();
170
178 const AddressInterval& chunkAllocationRegion() const { return chunkAllocationRegion_; }
179 void chunkAllocationRegion(const AddressInterval& i) { chunkAllocationRegion_ = i; }
188 const AddressIntervalSet& mappedFreeSpace() const { return freeSpace_; }
189 AddressIntervalSet& mappedFreeSpace() { return freeSpace_; }
190 void mappedFreeSpace(const AddressIntervalSet &x) { freeSpace_ = x; }
196 const AddressIntervalSet& allocatedChunks() const { return allocatedChunks_; }
197
204 size_t minChunkAllocationSize() const { return minChunkAllocationSize_; }
205 void minChunkAllocationSize(size_t n) { minChunkAllocationSize_ = n; }
211 size_t chunkAllocationAlignment() const { return chunkAllocationAlignment_; }
218 const std::string& chunkAllocationName() const { return chunkAllocationName_; }
219 void chunkAllocationName(const std::string &s) { chunkAllocationName_ = s; }
230 unsigned aggregationDirection() const { return aggregationDirection_; }
231 void aggregationDirection(unsigned d) { aggregationDirection_ = d; }
240 NopPadding nopPadding() const { return nopPadding_; }
241 void nopPadding(NopPadding p) { nopPadding_ = p; }
270 size_t startIdx, size_t nInsns, std::vector<uint8_t> replacement,
271 const std::vector<Relocation> &relocations = std::vector<Relocation>());
272
278 bool replaceInsnsAtFront(const Partitioner2::BasicBlockPtr&, size_t nInsns, const std::vector<uint8_t> &replacement,
279 const std::vector<Relocation> &relocations = std::vector<Relocation>());
280
286 virtual bool replaceInsnsAtBack(const Partitioner2::BasicBlockPtr&, size_t nInsns, const std::vector<uint8_t> &replacement,
287 const std::vector<Relocation> &relocations = std::vector<Relocation>());
288
295 virtual bool prependInsns(const Partitioner2::BasicBlockPtr&, const std::vector<uint8_t> &replacement,
296 const std::vector<Relocation> &relocations = std::vector<Relocation>());
297
304 virtual bool appendInsns(const Partitioner2::BasicBlockPtr&, const std::vector<uint8_t> &replacement,
305 const std::vector<Relocation> &relocations = std::vector<Relocation>());
306
323 virtual bool replaceInsns(const std::vector<SgAsmInstruction*> &toReplace, const std::vector<uint8_t> &replacement,
324 const std::vector<Relocation> &relocations = std::vector<Relocation>());
325
327 virtual void fillWithNops(const AddressIntervalSet &where);
328
330 virtual void fillWithRandom(const AddressIntervalSet &where);
331
337 virtual std::vector<uint8_t> encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa);
338
344 virtual std::vector<uint8_t> applyRelocations(rose_addr_t startVa, std::vector<uint8_t> replacement,
345 const std::vector<Relocation> &relocations, size_t relocStart,
346 const InstructionInfoMap &insnInfoMap);
347
357 virtual AddressInterval allocateMemory(size_t nBytes, rose_addr_t jmpTargetVa, Commit::Boolean commit = Commit::YES);
358
367
369 AddressIntervalSet instructionLocations(const std::vector<SgAsmInstruction*>&);
370
379 virtual bool replaceByOverwrite(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval,
380 const std::vector<uint8_t> &replacement, const std::vector<Relocation> &relocations,
381 size_t relocStart, const InstructionInfoMap &insnInfoMap);
382
392 virtual bool replaceByTransfer(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval,
393 const std::vector<SgAsmInstruction*> &toReplace, const std::vector<uint8_t> &replacement,
394 const std::vector<Relocation> &relocations, size_t relocStart,
395 const InstructionInfoMap &insnInfoMap);
396
403 size_t startIdx, size_t nDeleted);
404};
405
406} // namespace
407} // namespace
408
409#endif
410#endif
Insert new code in place of existing instructions.
void aggregationDirection(unsigned d)
Property: Whether additional instructions can be moved.
const AddressIntervalSet & allocatedChunks() const
Returns the parts of the virtual address space that were allocated for new instructions.
virtual bool replaceByTransfer(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval, const std::vector< SgAsmInstruction * > &toReplace, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations, size_t relocStart, const InstructionInfoMap &insnInfoMap)
Insert new code in allocated area.
Sawyer::Container::Map< int, InstructionInfo > InstructionInfoMap
Information about instructions within the basic block being modified.
void nopPadding(NopPadding p)
Property: Where to add no-ops when padding.
unsigned aggregationDirection() const
Property: Whether additional instructions can be moved.
static Sawyer::Message::Facility mlog
Facility for emitting diagnostics.
void chunkAllocationAlignment(size_t n)
Property: Alignment for large allocated chunks.
static void initDiagnostics()
Initialize diagnostic streams.
virtual bool appendInsns(const Partitioner2::BasicBlockPtr &, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Append code to a basic block.
virtual std::vector< uint8_t > encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa)
Encode an unconditional branch.
virtual void fillWithNops(const AddressIntervalSet &where)
Fill the specified memory with no-op instructions.
virtual void fillWithRandom(const AddressIntervalSet &where)
Fill the specified memory with random data.
void commitAllocation(const AddressInterval &where, Commit::Boolean commit=Commit::YES)
Commit previous allocation.
AggregationDirection
What other instructions can be moved to make room.
@ AGGREGATE_SUCCESSORS
Move succeeding instructions in CFG.
@ AGGREGATE_PREDECESSORS
Move preceding instructions in CFG.
void minChunkAllocationSize(size_t n)
Property: Minimum size of allocated chunks.
void chunkAllocationRegion(const AddressInterval &i)
Property: Where chunks are allocated.
const AddressInterval & chunkAllocationRegion() const
Property: Where chunks are allocated.
NopPadding
How to pad with no-ops.
@ PAD_RANDOM_BACK
Add random data to the end of replacements.
@ PAD_NOP_FRONT
Add no-ops to the front of replacements.
@ PAD_NOP_BACK
Add no-ops to the end of replacements.
void mappedFreeSpace(const AddressIntervalSet &x)
Property: Mapped free-space chunks.
const AddressIntervalSet & mappedFreeSpace() const
Property: Mapped free-space chunks.
AddressIntervalSet & mappedFreeSpace()
Property: Mapped free-space chunks.
virtual AddressInterval allocateMemory(size_t nBytes, rose_addr_t jmpTargetVa, Commit::Boolean commit=Commit::YES)
Allocate virtual memory in the partitioner memory map.
AddressIntervalSet instructionLocations(const std::vector< SgAsmInstruction * > &)
Given a list of functions, return all addresses that the instructions occupy.
virtual bool replaceInsnsAtBack(const Partitioner2::BasicBlockPtr &, size_t nInsns, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace instructions at back of basic block.
NopPadding nopPadding() const
Property: Where to add no-ops when padding.
const std::string & chunkAllocationName() const
Property: Name for newly allocated regions of memory.
void chunkAllocationName(const std::string &s)
Property: Name for newly allocated regions of memory.
InstructionInfoMap computeInstructionInfoMap(const Partitioner2::BasicBlockPtr &, size_t startIdx, size_t nDeleted)
Obtain info about instructions for the basic block being modified.
RelocType
Type of relocation to perform.
@ RELOC_INDEX_ABS_LE32HI
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
@ RELOC_INDEX_ABS_LE32
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
@ RELOC_ADDR_REL_BE32
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
@ RELOC_ADDR_REL_LE32
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
@ RELOC_INDEX_ABS_BE32
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
@ RELOC_INSN_ABS_LE32
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
@ RELOC_INSN_REL_LE32
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
@ RELOC_INSN_REL_BE32
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
virtual std::vector< uint8_t > applyRelocations(rose_addr_t startVa, std::vector< uint8_t > replacement, const std::vector< Relocation > &relocations, size_t relocStart, const InstructionInfoMap &insnInfoMap)
Apply relocations to create a new encoding.
virtual bool prependInsns(const Partitioner2::BasicBlockPtr &, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Prepend code to a basic block.
virtual bool replaceBlockInsns(const Partitioner2::BasicBlockPtr &, size_t startIdx, size_t nInsns, std::vector< uint8_t > replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace instructions in basic block.
size_t chunkAllocationAlignment() const
Property: Alignment for large allocated chunks.
virtual bool replaceInsns(const std::vector< SgAsmInstruction * > &toReplace, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace exactly the specified instructions with some other encoding.
bool replaceInsnsAtFront(const Partitioner2::BasicBlockPtr &, size_t nInsns, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations=std::vector< Relocation >())
Replace instructions at front of basic block.
virtual bool replaceByOverwrite(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval, const std::vector< uint8_t > &replacement, const std::vector< Relocation > &relocations, size_t relocStart, const InstructionInfoMap &insnInfoMap)
Insert new code by overwriting existing instructions.
size_t minChunkAllocationSize() const
Property: Minimum size of allocated chunks.
Container associating values with keys.
Definition Sawyer/Map.h:72
Collection of streams.
Definition Message.h:1606
Holds a value or nothing.
Definition Optional.h:56
Base class for machine instructions.
@ YES
Allocate memory for real.
@ NO
Only query an allocation.
The ROSE library.
Information about an instruction within the basic block being modified.
rose_addr_t originalVa
Original address of instruction.
Sawyer::Optional< rose_addr_t > newVaOffset
Offset of instruction from front of encoded insn vector.
size_t offset
Location of relocation in replacement code.
Relocation(size_t offset, RelocType type, rose_addr_t value)
Constructor.
rose_addr_t value
Argument for relocation algorithm.