ROSE  0.11.51.0
MemoryMap.h
1 #ifndef ROSE_BinaryAnalysis_MemoryMap_H
2 #define ROSE_BinaryAnalysis_MemoryMap_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
5 
6 #include <ByteOrder.h>
7 #include <Combinatorics.h>
8 #include <Rose/Exception.h>
9 
10 #include <Sawyer/Access.h>
11 #include <Sawyer/AddressMap.h>
12 #include <Sawyer/AllocatingBuffer.h>
13 #include <Sawyer/MappedBuffer.h>
14 #include <Sawyer/NullBuffer.h>
15 #include <Sawyer/Optional.h>
16 #include <Sawyer/StaticBuffer.h>
17 
18 #include <boost/config.hpp>
19 #include <boost/utility/enable_if.hpp>
20 #include <boost/serialization/access.hpp>
21 #include <boost/serialization/base_object.hpp>
22 #include <boost/serialization/export.hpp>
23 #include <boost/type_traits/is_integral.hpp>
24 
25 namespace Rose {
26 namespace BinaryAnalysis {
27 
33 template<typename T, typename U>
34 typename boost::enable_if_c<boost::is_integral<T>::value && boost::is_integral<U>::value, T>::type
35 alignUp(T address, U alignment) {
36  ASSERT_require(alignment > 0);
37  T almt = static_cast<T>(alignment);
38  return ((address + almt - 1) / almt) * almt;
39 }
40 
46 template<typename T, typename U>
47 typename boost::enable_if_c<boost::is_integral<T>::value && boost::is_integral<U>::value, T>::type
48 alignDown(T address, U alignment) {
49  ASSERT_require(alignment > 0);
50  T almt = static_cast<T>(alignment);
51  return (address / almt) * almt;
52 }
53 
111 class MemoryMap: public Sawyer::Container::AddressMap<rose_addr_t, uint8_t>, public Sawyer::SharedObject {
112 public:
114  typedef rose_addr_t Address;
115  typedef uint8_t Value;
125 
127  struct Attach { // For consistency with other <Feature>::Boolean types
128  enum Boolean {
129  NO,
131  };
132  };
133 
134 private:
135  ByteOrder::Endianness endianness_;
136 
137 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
138 private:
139  friend class boost::serialization::access;
140 
141  template<class S>
142  void serialize(S &s, const unsigned /*version*/) {
143  s.template register_type<AllocatingBuffer>();
144  s.template register_type<MappedBuffer>();
145  s.template register_type<NullBuffer>();
146  s.template register_type<StaticBuffer>();
147  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
148  s & BOOST_SERIALIZATION_NVP(endianness_);
149  }
150 #endif
151 
152 public:
153 
154  // Whoever pollutes all namespaces with these common word preprocessor symbols is a lunatic!
155 # if defined(READABLE) || defined(WRITABLE) || defined(EXECUTABLE) || defined(IMMUTABLE) || defined(PRIVATE)
156 # ifdef _MSC_VER
157 # pragma message("Undefining common words from the global namespace: READABLE, WRITABLE, EXECUTABLE, IMMUTABLE, PRIVATE")
158 # else
159 # warning "Undefining common words from the global namespace: READABLE, WRITABLE, EXECUTABLE, IMMUTABLE, PRIVATE"
160 # endif
161 # undef READABLE
162 # undef WRITABLE
163 # undef EXECUTABLE
164 # undef IMMUTABLE
165 # undef PRIVATE
166 # endif
167 
168  // Accessibility flags
169  static const unsigned NO_ACCESS = 0;
170  static const unsigned READABLE = Sawyer::Access::READABLE;
171  static const unsigned WRITABLE = Sawyer::Access::WRITABLE;
172  static const unsigned EXECUTABLE = Sawyer::Access::EXECUTABLE;
173  static const unsigned IMMUTABLE = Sawyer::Access::IMMUTABLE;
174  static const unsigned PRIVATE = Sawyer::Access::PRIVATE;
175  static const unsigned INITIALIZED = 0x00000200; // Partitioner2: initialized memory even if writable
176 
177  // Aggregate accessibility flags
178  static const unsigned READ_WRITE = READABLE | WRITABLE;
179  static const unsigned READ_EXECUTE = READABLE | EXECUTABLE;
180  static const unsigned READ_WRITE_EXECUTE = READABLE | WRITABLE | EXECUTABLE;
181 
182  // These bits are reserved for use in ROSE
183  static const unsigned RESERVED_ACCESS_BITS = 0x0000ffff;
184 
185 
186 public:
188  class Exception: public Rose::Exception {
189  public:
190  Exception(const std::string &mesg, const MemoryMap::Ptr map): Rose::Exception(mesg), map(map) {}
191  virtual ~Exception() throw() {}
192  virtual std::string leader(std::string dflt="memory map problem") const;
193  virtual std::string details(bool) const;
194  virtual void print(std::ostream&, bool verbose=true) const;
195  friend std::ostream& operator<<(std::ostream&, const Exception&);
196  public:
198  };
199 
204  struct Inconsistent : public Exception {
205  Inconsistent(const std::string &mesg, const MemoryMap::Ptr &map,
206  const AddressInterval &new_range, const Segment &new_segment,
207  const AddressInterval &old_range, const Segment &old_segment)
208  : Exception(mesg, map),
209  new_range(new_range), old_range(old_range),
210  new_segment(new_segment), old_segment(old_segment) {}
211  virtual ~Inconsistent() throw() {}
212  virtual void print(std::ostream&, bool verbose=true) const;
213  friend std::ostream& operator<<(std::ostream&, const Inconsistent&);
214  AddressInterval new_range, old_range;
215  Segment new_segment, old_segment;
216  };
217 
219  struct NotMapped : public Exception {
220  NotMapped(const std::string &mesg, const MemoryMap::Ptr &map, rose_addr_t va)
221  : Exception(mesg, map), va(va) {}
222  virtual ~NotMapped() throw() {}
223  virtual void print(std::ostream&, bool verbose=true) const;
224  friend std::ostream& operator<<(std::ostream&, const NotMapped&);
225  rose_addr_t va;
226  };
227 
229  struct NoFreeSpace : public Exception {
230  NoFreeSpace(const std::string &mesg, const MemoryMap::Ptr &map, size_t size)
231  : Exception(mesg, map), size(size) {}
232  virtual ~NoFreeSpace() throw() {}
233  virtual void print(std::ostream&, bool verbose=true) const;
234  friend std::ostream& operator<<(std::ostream&, const NoFreeSpace&);
235  size_t size;
236  };
237 
239  struct SyntaxError: public Exception {
240  SyntaxError(const std::string &mesg, const MemoryMap::Ptr &map, const std::string &filename,
241  unsigned linenum, int colnum=-1)
242  : Exception(mesg, map), filename(filename), linenum(linenum), colnum(colnum) {}
243  virtual ~SyntaxError() throw() {}
244  virtual void print(std::ostream&, bool verbose=true) const;
245  friend std::ostream& operator<<(std::ostream&, const SyntaxError&);
246  std::string filename;
247  unsigned linenum;
248  int colnum;
249  };
250 
251 protected:
253  MemoryMap(): endianness_(ByteOrder::ORDER_UNSPECIFIED) {}
254 
255 public:
257  static Ptr instance() {
258  return Ptr(new MemoryMap);
259  }
260 
264  Ptr shallowCopy() {
265  return Ptr(new MemoryMap(*this));
266  }
267 
275  ByteOrder::Endianness byteOrder() const { return endianness_; }
276  void byteOrder(ByteOrder::Endianness order) { endianness_ = order; }
279  // Note that the order of the enum members is for backward compatibility with an older version of insertFile whose third
280  // argument was "bool writable = false" (MAP_RDONLY, but now intended to be MAP_PRIVATE) and when it was true was the same
281  // as MAP_READWRITE.
282  //
291  };
292 
297  size_t insertFile(const std::string &fileName, rose_addr_t va, InsertFileMapMode mode = MAP_PRIVATE,
298  std::string segmentName = "");
299 
375  AddressInterval insertFile(const std::string &locatorString);
376 
378  static std::string insertFileDocumentation();
379 
406  AddressInterval insertData(const std::string &locatorString);
407 
409  static std::string insertDataDocumentation();
410 
415  void adjustMap(const std::string &locatorString);
416 
418  static std::string adjustMapDocumentation();
419 
420 
423  AddressInterval interval;
424  unsigned accessibility;
425  rose_addr_t fileOffset;
426  std::string deviceName;
427  size_t inode;
428  std::string comment;
431  : accessibility(0), fileOffset(0), inode(0) {}
432  };
433 
437  static std::vector<ProcessMapRecord> readProcessMap(pid_t);
438 
439 #ifdef BOOST_WINDOWS
440  void insertProcess(int pid, Attach::Boolean attach);
441 #else
442 
443  void insertProcess(pid_t pid, Attach::Boolean attach);
444 #endif
445 
449  void insertProcess(const std::string &locatorString);
450 
454  bool insertProcessPid(pid_t, const AddressInterval &where, unsigned accessibility, const std::string &name);
455  void insertProcessPid(pid_t, const std::vector<ProcessMapRecord>&);
456  bool insertProcessMemory(int memFile, const AddressInterval &where, unsigned accessibility, std::string name);
460  static std::string insertProcessDocumentation();
461 
467  static std::pair<Buffer::Ptr, std::string> copyFromFile(int fd, const AddressInterval&);
468 
470  void eraseZeros(size_t minsize);
471 
482  bool shrinkUnshare();
483 
485  size_t readQuick(void *buf, rose_addr_t startVa, size_t desired) const {
486  return at(startVa).limit(desired).require(READABLE).read((uint8_t*)buf).size();
487  }
488 
501  std::string readString(rose_addr_t startVa, size_t desired, int(*validChar)(int)=NULL, int(*invalidChar)(int)=NULL,
502  unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0, char terminator='\0') const;
503 
509  template<typename U>
510  Sawyer::Optional<U> readUnsigned(rose_addr_t startVa) const {
511  U val = 0;
512  if (at(startVa).limit(sizeof val).read((uint8_t*)&val).size() != sizeof val)
513  return Sawyer::Nothing();
514  ByteOrder::convert((void*)&val, sizeof val, endianness_, ByteOrder::host_order());
515  return val;
516  }
517 
519  SgUnsignedCharList readVector(rose_addr_t startVa, size_t desired, unsigned requiredPerms=READABLE) const;
520 
522  size_t writeQuick(const void *buf, rose_addr_t startVa, size_t desired) {
523  return at(startVa).limit(desired).require(WRITABLE).write((const uint8_t*)buf).size();
524  }
525 
533  Sawyer::Optional<rose_addr_t> findAny(const Extent &limits, const std::vector<uint8_t> &bytesToFind,
534  unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0) const;
535  Sawyer::Optional<rose_addr_t> findAny(const AddressInterval &limits, const std::vector<uint8_t> &bytesToFind,
536  unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0) const;
544  Sawyer::Optional<rose_addr_t> findSequence(const AddressInterval &interval, const std::vector<uint8_t> &sequence) const;
545 
549  void dump(FILE*, const char *prefix="") const;
550  void dump(std::ostream&, std::string prefix="") const;
551  void print(std::ostream &o, std::string prefix="") const { dump(o, prefix); }
552  void dump() const; // mostly for calling from within GDB or similar
570 
572  static std::string segmentTitle(const Segment&);
573 
574  friend std::ostream& operator<<(std::ostream&, const MemoryMap&);
575 };
576 
577 } // namespace
578 } // namespace
579 
580 // Register the types needed for serialization since some of them are derived from polymorphic class templates.
581 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
583 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::MemoryMap::MappedBuffer);
584 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::MemoryMap::NullBuffer);
585 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::MemoryMap::StaticBuffer);
586 #endif
587 
588 #endif
589 #endif
Exception thrown by find_free() when there's not enough free space left.
Definition: MemoryMap.h:229
int colnum
Optional column number (0-origin; negative if unknown).
Definition: MemoryMap.h:248
Points to static data.
Definition: StaticBuffer.h:30
static std::string adjustMapDocumentation()
Documentation string for adjustMap.
Exception thrown by load() when there's a syntax error in the index file.
Definition: MemoryMap.h:239
virtual std::string leader(std::string dflt="memory map problem") const
Leading part of the error message.
static std::pair< Buffer::Ptr, std::string > copyFromFile(int fd, const AddressInterval &)
Copy part of a file into a buffer.
void print(std::ostream &o, std::string prefix="") const
Prints the contents of the map for debugging.
Definition: MemoryMap.h:551
virtual std::string details(bool) const
Details emitted on following lines, indented two spaces.
AddressMapConstraints< const AddressMap > at(Address x) const
Constraint: anchor point.
Definition: AddressMap.h:1115
static std::string insertFileDocumentation()
Documentation string for insertFile.
size_t insertFile(const std::string &fileName, rose_addr_t va, InsertFileMapMode mode=MAP_PRIVATE, std::string segmentName="")
Insert file contents into memory map.
A contiguous range of values.
Definition: rangemap.h:50
InsertFileMapMode
Mapping mode for insertFile.
Definition: MemoryMap.h:284
rose_addr_t fileOffset
The accessibility flags.
Definition: MemoryMap.h:425
MemoryMap()
Constructs an empty memory map.
Definition: MemoryMap.h:253
Holds a value or nothing.
Definition: Optional.h:49
MemoryMap::Ptr map
Map that caused the exception if available, null otherwise.
Definition: MemoryMap.h:197
Attach with ptrace, get memory, then detach.
Definition: MemoryMap.h:130
AddressSegment< rose_addr_t, uint8_t > Segment
Type of segments stored by this map.
Definition: AddressMap.h:1011
AddressInterval insertData(const std::string &locatorString)
Insert data into a memory map.
Main namespace for the ROSE library.
Information about a process map.
Definition: MemoryMap.h:422
Memory mapped file.
Definition: MappedBuffer.h:43
File is mapped with read-only permission.
Definition: MemoryMap.h:289
Allocates memory as needed.
std::string filename
Name of index file where error occurred.
Definition: MemoryMap.h:246
Combinatorics::Hasher & hash(Combinatorics::Hasher &) const
Compute a hash of the entire memory contents.
static Ptr instance()
Construct an empty memory map.
Definition: MemoryMap.h:257
void byteOrder(ByteOrder::Endianness order)
Property: byte order.
Definition: MemoryMap.h:276
Sawyer::Container::Interval< Address > read(Value *buf, const AddressMapConstraints< const AddressMap > &c, MatchFlags flags=0) const
Reads data into the supplied buffer.
Definition: AddressMap.h:1690
File is mapped with read and write permission.
Definition: MemoryMap.h:287
size_t readQuick(void *buf, rose_addr_t startVa, size_t desired) const
Read data into buffer.
Definition: MemoryMap.h:485
void insertProcess(pid_t pid, Attach::Boolean attach)
Insert the memory of some other process into this memory map.
size_t inode
The device from which the data is mapped, or "00:00".
Definition: MemoryMap.h:427
Exception for when we try to access a virtual address that isn't mapped.
Definition: MemoryMap.h:219
A mapping from address space to values.
Definition: AddressMap.h:1004
unsigned accessibility
Mapped virtual addresses.
Definition: MemoryMap.h:424
Buffer that has no data.
Definition: NullBuffer.h:26
Base class for all buffers.
Definition: Buffer.h:25
An efficient mapping from an address space to stored data.
Definition: MemoryMap.h:111
bool insertProcessMemory(int memFile, const AddressInterval &where, unsigned accessibility, std::string name)
Insert part of another process's memory into this memory map.
boost::enable_if_c< boost::is_integral< T >::value &&boost::is_integral< U >::value, T >::type alignDown(T address, U alignment)
Align address upward to boundary.
Definition: MemoryMap.h:48
boost::enable_if_c< boost::is_integral< T >::value &&boost::is_integral< U >::value, T >::type alignUp(T address, U alignment)
Align address downward to boundary.
Definition: MemoryMap.h:35
unsigned linenum
Line number (1 origin) where error occurred.
Definition: MemoryMap.h:247
Constraints are used to select addresses from a memory map.
Definition: AddressMap.h:80
Sawyer::Optional< rose_addr_t > findSequence(const AddressInterval &interval, const std::vector< uint8_t > &sequence) const
Search for a byte sequence.
void adjustMap(const std::string &locatorString)
Adjusts a memory map according to the locator string.
static std::vector< ProcessMapRecord > readProcessMap(pid_t)
Obtain the memory map information for a process.
std::string readString(rose_addr_t startVa, size_t desired, int(*validChar)(int)=NULL, int(*invalidChar)(int)=NULL, unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0, char terminator='\0') const
Reads a NUL-terminated string from the memory map.
Base class for reference counted objects.
Definition: SharedObject.h:64
Exception for an inconsistent mapping.
Definition: MemoryMap.h:204
std::string deviceName
Starting byte offset in the file.
Definition: MemoryMap.h:426
void eraseZeros(size_t minsize)
Erases regions of zero bytes that are executable and readable and at least minsize in size...
bool insertProcessPid(pid_t, const AddressInterval &where, unsigned accessibility, const std::string &name)
Insert part of another process's memory into this memory map.
SgUnsignedCharList readVector(rose_addr_t startVa, size_t desired, unsigned requiredPerms=READABLE) const
Read quickly into a vector.
Attach with ptrace first when reading a process?
Definition: MemoryMap.h:127
std::string comment
Inode on the device, or zero.
Definition: MemoryMap.h:428
bool shrinkUnshare()
Shrink buffers and remove sharing.
Represents no value.
Definition: Optional.h:32
ByteOrder::Endianness byteOrder() const
Property: byte order.
Definition: MemoryMap.h:275
void dump() const
Prints the contents of the map for debugging.
static std::string insertDataDocumentation()
Documentation string for insertData.
Sawyer::Optional< rose_addr_t > findAny(const Extent &limits, const std::vector< uint8_t > &bytesToFind, unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0) const
Search for any byte.
static std::string segmentTitle(const Segment &)
Title of a segment when printing the map.
Base class for all ROSE exceptions.
Definition: Rose/Exception.h:9
static std::string insertProcessDocumentation()
Documentation string for insertProcess.
AddressMapConstraints< const AddressMap > limit(size_t x) const
Constraint: limit matched size.
Definition: AddressMap.h:1156
size_t writeQuick(const void *buf, rose_addr_t startVa, size_t desired)
Write data from buffer.
Definition: MemoryMap.h:522
Sawyer::Optional< U > readUnsigned(rose_addr_t startVa) const
Read an unsigned value.
Definition: MemoryMap.h:510
Exception for MemoryMap operations.
Definition: MemoryMap.h:188
Assume ptrace is attached and process is stopped.
Definition: MemoryMap.h:129
Ptr shallowCopy()
Create a new copy of the memory map.
Definition: MemoryMap.h:264