ROSE  0.11.145.0
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/Partitioner2/BasicTypes.h>
7 #include <Sawyer/Map.h>
8 
9 namespace Rose {
10 namespace BinaryAnalysis {
11 
13 namespace Commit {
14 enum Boolean {
15  NO,
16  YES
17 };
18 } // namespace
19 
21 class CodeInserter {
22 public:
25  AGGREGATE_PREDECESSORS = 0x00000001,
26  AGGREGATE_SUCCESSORS = 0x00000002
27  };
28 
30  enum NopPadding {
34  };
35 
54  enum RelocType {
100  };
101 
111  struct Relocation {
112  size_t offset;
114  rose_addr_t value;
122  Relocation(size_t offset, RelocType type, rose_addr_t value)
123  : offset(offset), type(type), value(value) {}
124  };
125 
128  rose_addr_t originalVa;
131  explicit InstructionInfo(SgAsmInstruction *insn)
132  : originalVa(insn->get_address()) {}
133  };
134 
142 
143 protected:
144  Partitioner2::PartitionerConstPtr partitioner_; // not null
145  AddressInterval chunkAllocationRegion_; // where chunks can be allocated
146  size_t minChunkAllocationSize_; // size of each chunk in bytes (also the alignment)
147  size_t chunkAllocationAlignment_; // alignment for allocating large chunks
148  std::string chunkAllocationName_; // name to give new areas of the memory map
149  AddressIntervalSet allocatedChunks_; // large allocated chunks that populate freeSpace_
150  AddressIntervalSet freeSpace_; // parts of mapped memory serving as free space for allocations
151  unsigned aggregationDirection_; // AggregationDirection bits
152  NopPadding nopPadding_; // where to add no-op padding
153 
154 public:
157 
158 public:
160  ~CodeInserter();
161 
165  static void initDiagnostics();
166 
174  const AddressInterval& chunkAllocationRegion() const { return chunkAllocationRegion_; }
175  void chunkAllocationRegion(const AddressInterval& i) { chunkAllocationRegion_ = i; }
181  const AddressIntervalSet& allocatedChunks() const { return allocatedChunks_; }
182 
189  size_t minChunkAllocationSize() const { return minChunkAllocationSize_; }
190  void minChunkAllocationSize(size_t n) { minChunkAllocationSize_ = n; }
196  size_t chunkAllocationAlignment() const { return chunkAllocationAlignment_; }
197  void chunkAllocationAlignment(size_t n);
203  const std::string& chunkAllocationName() const { return chunkAllocationName_; }
204  void chunkAllocationName(const std::string &s) { chunkAllocationName_ = s; }
215  unsigned aggregationDirection() const { return aggregationDirection_; }
216  void aggregationDirection(unsigned d) { aggregationDirection_ = d; }
225  NopPadding nopPadding() const { return nopPadding_; }
226  void nopPadding(NopPadding p) { nopPadding_ = p; }
255  size_t startIdx, size_t nInsns, std::vector<uint8_t> replacement,
256  const std::vector<Relocation> &relocations = std::vector<Relocation>());
257 
263  bool replaceInsnsAtFront(const Partitioner2::BasicBlockPtr&, size_t nInsns, const std::vector<uint8_t> &replacement,
264  const std::vector<Relocation> &relocations = std::vector<Relocation>());
265 
271  virtual bool replaceInsnsAtBack(const Partitioner2::BasicBlockPtr&, size_t nInsns, const std::vector<uint8_t> &replacement,
272  const std::vector<Relocation> &relocations = std::vector<Relocation>());
273 
280  virtual bool prependInsns(const Partitioner2::BasicBlockPtr&, const std::vector<uint8_t> &replacement,
281  const std::vector<Relocation> &relocations = std::vector<Relocation>());
282 
289  virtual bool appendInsns(const Partitioner2::BasicBlockPtr&, const std::vector<uint8_t> &replacement,
290  const std::vector<Relocation> &relocations = std::vector<Relocation>());
291 
308  virtual bool replaceInsns(const std::vector<SgAsmInstruction*> &toReplace, const std::vector<uint8_t> &replacement,
309  const std::vector<Relocation> &relocations = std::vector<Relocation>());
310 
312  virtual void fillWithNops(const AddressIntervalSet &where);
313 
315  virtual void fillWithRandom(const AddressIntervalSet &where);
316 
322  virtual std::vector<uint8_t> encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa);
323 
329  virtual std::vector<uint8_t> applyRelocations(rose_addr_t startVa, std::vector<uint8_t> replacement,
330  const std::vector<Relocation> &relocations, size_t relocStart,
331  const InstructionInfoMap &insnInfoMap);
332 
342  virtual AddressInterval allocateMemory(size_t nBytes, rose_addr_t jmpTargetVa, Commit::Boolean commit = Commit::YES);
343 
351  void commitAllocation(const AddressInterval &where, Commit::Boolean commit = Commit::YES);
352 
354  AddressIntervalSet instructionLocations(const std::vector<SgAsmInstruction*>&);
355 
364  virtual bool replaceByOverwrite(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval,
365  const std::vector<uint8_t> &replacement, const std::vector<Relocation> &relocations,
366  size_t relocStart, const InstructionInfoMap &insnInfoMap);
367 
377  virtual bool replaceByTransfer(const AddressIntervalSet &toReplaceVas, const AddressInterval &entryInterval,
378  const std::vector<SgAsmInstruction*> &toReplace, const std::vector<uint8_t> &replacement,
379  const std::vector<Relocation> &relocations, size_t relocStart,
380  const InstructionInfoMap &insnInfoMap);
381 
387  InstructionInfoMap computeInstructionInfoMap(const Partitioner2::BasicBlockPtr&,
388  size_t startIdx, size_t nDeleted);
389 };
390 
391 } // namespace
392 } // namespace
393 
394 #endif
395 #endif
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
Definition: CodeInserter.h:61
RelocType type
Relocation algorithm.
Definition: CodeInserter.h:113
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.
rose_addr_t value
Argument for relocation algorithm.
Definition: CodeInserter.h:114
void minChunkAllocationSize(size_t n)
Property: Minimum size of allocated chunks.
Definition: CodeInserter.h:190
Base class for machine instructions.
AddressIntervalSet instructionLocations(const std::vector< SgAsmInstruction * > &)
Given a list of functions, return all addresses that the instructions occupy.
Collection of streams.
Definition: Message.h:1606
virtual void fillWithRandom(const AddressIntervalSet &where)
Fill the specified memory with random data.
Only query an allocation.
Definition: CodeInserter.h:15
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
Definition: CodeInserter.h:81
virtual void fillWithNops(const AddressIntervalSet &where)
Fill the specified memory with no-op instructions.
size_t offset
Location of relocation in replacement code.
Definition: CodeInserter.h:112
virtual AddressInterval allocateMemory(size_t nBytes, rose_addr_t jmpTargetVa, Commit::Boolean commit=Commit::YES)
Allocate virtual memory in the partitioner memory map.
rose_addr_t originalVa
Original address of instruction.
Definition: CodeInserter.h:128
unsigned aggregationDirection() const
Property: Whether additional instructions can be moved.
Definition: CodeInserter.h:215
AggregationDirection
What other instructions can be moved to make room.
Definition: CodeInserter.h:24
void chunkAllocationRegion(const AddressInterval &i)
Property: Where chunks are allocated.
Definition: CodeInserter.h:175
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.
static void initDiagnostics()
Initialize diagnostic streams.
Add random data to the end of replacements.
Definition: CodeInserter.h:33
const std::string & chunkAllocationName() const
Property: Name for newly allocated regions of memory.
Definition: CodeInserter.h:203
Main namespace for the ROSE library.
Sawyer::Container::Map< int, InstructionInfo > InstructionInfoMap
Information about instructions within the basic block being modified.
Definition: CodeInserter.h:141
Relocation(size_t offset, RelocType type, rose_addr_t value)
Constructor.
Definition: CodeInserter.h:122
Add no-ops to the end of replacements.
Definition: CodeInserter.h:31
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.
size_t minChunkAllocationSize() const
Property: Minimum size of allocated chunks.
Definition: CodeInserter.h:189
void commitAllocation(const AddressInterval &where, Commit::Boolean commit=Commit::YES)
Commit previous allocation.
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
Definition: CodeInserter.h:91
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.
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
Definition: CodeInserter.h:58
virtual std::vector< uint8_t > encodeJump(rose_addr_t srcVa, rose_addr_t tgtVa)
Encode an unconditional branch.
Move succeeding instructions in CFG.
Definition: CodeInserter.h:26
const AddressIntervalSet & allocatedChunks() const
Returns the parts of the virtual address space that were allocated for new instructions.
Definition: CodeInserter.h:181
Interprets the reloc_value as an instruction relative index for some instruction of the original basi...
Definition: CodeInserter.h:72
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.
size_t chunkAllocationAlignment() const
Property: Alignment for large allocated chunks.
Definition: CodeInserter.h:196
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
Definition: CodeInserter.h:68
void chunkAllocationName(const std::string &s)
Property: Name for newly allocated regions of memory.
Definition: CodeInserter.h:204
Insert new code in place of existing instructions.
Definition: CodeInserter.h:21
Interprets the reloc_value as an index of some byte in the input, and computes that byte's virtual ad...
Definition: CodeInserter.h:55
Allocate memory for real.
Definition: CodeInserter.h:16
void nopPadding(NopPadding p)
Property: Where to add no-ops when padding.
Definition: CodeInserter.h:226
RelocType
Type of relocation to perform.
Definition: CodeInserter.h:54
Information about an instruction within the basic block being modified.
Definition: CodeInserter.h:127
Add no-ops to the front of replacements.
Definition: CodeInserter.h:32
NopPadding nopPadding() const
Property: Where to add no-ops when padding.
Definition: CodeInserter.h:225
static Diagnostics::Facility mlog
Facility for emitting diagnostics.
Definition: CodeInserter.h:156
Sawyer::Optional< rose_addr_t > newVaOffset
Offset of instruction from front of encoded insn vector.
Definition: CodeInserter.h:129
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.
void aggregationDirection(unsigned d)
Property: Whether additional instructions can be moved.
Definition: CodeInserter.h:216
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 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.
InstructionInfoMap computeInstructionInfoMap(const Partitioner2::BasicBlockPtr &, size_t startIdx, size_t nDeleted)
Obtain info about instructions for the basic block being modified.
Interprets the reloc_value as a virtual address and computes the offset from the output virtual addre...
Definition: CodeInserter.h:64
NopPadding
How to pad with no-ops.
Definition: CodeInserter.h:30
Container associating values with keys.
Definition: Sawyer/Map.h:66
const AddressInterval & chunkAllocationRegion() const
Property: Where chunks are allocated.
Definition: CodeInserter.h:174
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.