ROSE  0.11.83.2
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 
110 class MemoryMap: public Sawyer::Container::AddressMap<rose_addr_t, uint8_t>, public Sawyer::SharedObject {
111 public:
113  typedef rose_addr_t Address;
114  typedef uint8_t Value;
124 
126  struct Attach { // For consistency with other <Feature>::Boolean types
127  enum Boolean {
128  NO,
130  };
131  };
132 
133 private:
134  ByteOrder::Endianness endianness_;
135 
136 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
137 private:
138  friend class boost::serialization::access;
139 
140  template<class S>
141  void serialize(S &s, const unsigned /*version*/) {
142  s.template register_type<AllocatingBuffer>();
143  s.template register_type<MappedBuffer>();
144  s.template register_type<NullBuffer>();
145  s.template register_type<StaticBuffer>();
146  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
147  s & BOOST_SERIALIZATION_NVP(endianness_);
148  }
149 #endif
150 
151 public:
152 
153  // Whoever pollutes all namespaces with these common word preprocessor symbols is a lunatic!
154 # if defined(READABLE) || defined(WRITABLE) || defined(EXECUTABLE) || defined(IMMUTABLE) || defined(PRIVATE)
155 # ifdef _MSC_VER
156 # pragma message("Undefining common words from the global namespace: READABLE, WRITABLE, EXECUTABLE, IMMUTABLE, PRIVATE")
157 # else
158 # warning "Undefining common words from the global namespace: READABLE, WRITABLE, EXECUTABLE, IMMUTABLE, PRIVATE"
159 # endif
160 # undef READABLE
161 # undef WRITABLE
162 # undef EXECUTABLE
163 # undef IMMUTABLE
164 # undef PRIVATE
165 # endif
166 
167  // Accessibility flags
168  static const unsigned NO_ACCESS = 0;
169  static const unsigned READABLE = Sawyer::Access::READABLE;
170  static const unsigned WRITABLE = Sawyer::Access::WRITABLE;
171  static const unsigned EXECUTABLE = Sawyer::Access::EXECUTABLE;
172  static const unsigned IMMUTABLE = Sawyer::Access::IMMUTABLE;
173  static const unsigned PRIVATE = Sawyer::Access::PRIVATE;
174  static const unsigned INITIALIZED = 0x00000200; // Partitioner2: initialized memory even if writable
175 
176  // Aggregate accessibility flags
177  static const unsigned READ_WRITE = READABLE | WRITABLE;
178  static const unsigned READ_EXECUTE = READABLE | EXECUTABLE;
179  static const unsigned READ_WRITE_EXECUTE = READABLE | WRITABLE | EXECUTABLE;
180 
181  // These bits are reserved for use in ROSE
182  static const unsigned RESERVED_ACCESS_BITS = 0x0000ffff;
183 
184 
185 public:
187  class Exception: public Rose::Exception {
188  public:
189  Exception(const std::string &mesg, const MemoryMap::Ptr map): Rose::Exception(mesg), map(map) {}
190  virtual ~Exception() throw() {}
191  virtual std::string leader(std::string dflt="memory map problem") const;
192  virtual std::string details(bool) const;
193  virtual void print(std::ostream&, bool verbose=true) const;
194  friend std::ostream& operator<<(std::ostream&, const Exception&);
195  public:
197  };
198 
203  struct Inconsistent : public Exception {
204  Inconsistent(const std::string &mesg, const MemoryMap::Ptr &map,
205  const AddressInterval &new_range, const Segment &new_segment,
206  const AddressInterval &old_range, const Segment &old_segment)
207  : Exception(mesg, map),
208  new_range(new_range), old_range(old_range),
209  new_segment(new_segment), old_segment(old_segment) {}
210  virtual ~Inconsistent() throw() {}
211  virtual void print(std::ostream&, bool verbose=true) const;
212  friend std::ostream& operator<<(std::ostream&, const Inconsistent&);
213  AddressInterval new_range, old_range;
214  Segment new_segment, old_segment;
215  };
216 
218  struct NotMapped : public Exception {
219  NotMapped(const std::string &mesg, const MemoryMap::Ptr &map, rose_addr_t va)
220  : Exception(mesg, map), va(va) {}
221  virtual ~NotMapped() throw() {}
222  virtual void print(std::ostream&, bool verbose=true) const;
223  friend std::ostream& operator<<(std::ostream&, const NotMapped&);
224  rose_addr_t va;
225  };
226 
228  struct NoFreeSpace : public Exception {
229  NoFreeSpace(const std::string &mesg, const MemoryMap::Ptr &map, size_t size)
230  : Exception(mesg, map), size(size) {}
231  virtual ~NoFreeSpace() throw() {}
232  virtual void print(std::ostream&, bool verbose=true) const;
233  friend std::ostream& operator<<(std::ostream&, const NoFreeSpace&);
234  size_t size;
235  };
236 
238  struct SyntaxError: public Exception {
239  SyntaxError(const std::string &mesg, const MemoryMap::Ptr &map, const std::string &filename,
240  unsigned linenum, int colnum=-1)
241  : Exception(mesg, map), filename(filename), linenum(linenum), colnum(colnum) {}
242  virtual ~SyntaxError() throw() {}
243  virtual void print(std::ostream&, bool verbose=true) const;
244  friend std::ostream& operator<<(std::ostream&, const SyntaxError&);
245  std::string filename;
246  unsigned linenum;
247  int colnum;
248  };
249 
250 protected:
252  MemoryMap(): endianness_(ByteOrder::ORDER_UNSPECIFIED) {}
253 
254 public:
256  static Ptr instance() {
257  return Ptr(new MemoryMap);
258  }
259 
263  Ptr shallowCopy() {
264  return Ptr(new MemoryMap(*this));
265  }
266 
274  ByteOrder::Endianness byteOrder() const { return endianness_; }
275  void byteOrder(ByteOrder::Endianness order) { endianness_ = order; }
278  // Note that the order of the enum members is for backward compatibility with an older version of insertFile whose third
279  // argument was "bool writable = false" (MAP_RDONLY, but now intended to be MAP_PRIVATE) and when it was true was the same
280  // as MAP_READWRITE.
281  //
290  };
291 
296  size_t insertFile(const std::string &fileName, rose_addr_t va, InsertFileMapMode mode = MAP_PRIVATE,
297  std::string segmentName = "");
298 
374  AddressInterval insertFile(const std::string &locatorString);
375 
377  static std::string insertFileDocumentation();
378 
405  AddressInterval insertData(const std::string &locatorString);
406 
408  static std::string insertDataDocumentation();
409 
414  void adjustMap(const std::string &locatorString);
415 
417  static std::string adjustMapDocumentation();
418 
419 
422  AddressInterval interval;
423  unsigned accessibility;
424  rose_addr_t fileOffset;
425  std::string deviceName;
426  size_t inode;
427  std::string comment;
430  : accessibility(0), fileOffset(0), inode(0) {}
431  };
432 
436  static std::vector<ProcessMapRecord> readProcessMap(pid_t);
437 
438 #ifdef BOOST_WINDOWS
439  void insertProcess(int pid, Attach::Boolean attach);
440 #else
441 
442  void insertProcess(pid_t pid, Attach::Boolean attach);
443 #endif
444 
448  void insertProcess(const std::string &locatorString);
449 
453  bool insertProcessPid(pid_t, const AddressInterval &where, unsigned accessibility, const std::string &name);
454  void insertProcessPid(pid_t, const std::vector<ProcessMapRecord>&);
455  bool insertProcessMemory(int memFile, const AddressInterval &where, unsigned accessibility, std::string name);
459  static std::string insertProcessDocumentation();
460 
464  void linkTo(const MemoryMap::Ptr &other, const AddressIntervalSet &parts);
465 
471  static std::pair<Buffer::Ptr, std::string> copyFromFile(int fd, const AddressInterval&);
472 
474  void eraseZeros(size_t minsize);
475 
486  bool shrinkUnshare();
487 
489  size_t readQuick(void *buf, rose_addr_t startVa, size_t desired) const {
490  return at(startVa).limit(desired).require(READABLE).read((uint8_t*)buf).size();
491  }
492 
505  std::string readString(rose_addr_t startVa, size_t desired, int(*validChar)(int)=NULL, int(*invalidChar)(int)=NULL,
506  unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0, char terminator='\0') const;
507 
513  template<typename U>
514  Sawyer::Optional<U> readUnsigned(rose_addr_t startVa) const {
515  U val = 0;
516  if (at(startVa).limit(sizeof val).read((uint8_t*)&val).size() != sizeof val)
517  return Sawyer::Nothing();
518  ByteOrder::convert((void*)&val, sizeof val, endianness_, ByteOrder::host_order());
519  return val;
520  }
521 
527  Sawyer::Optional<uint64_t> readLongUnsinged(rose_addr_t startVa) const {
528  uint64_t val = 0;
529  if (at(startVa).limit(sizeof val).read((uint8_t*)&val).size() != sizeof val)
530  return Sawyer::Nothing();
531  ByteOrder::convert((void*)&val, sizeof val, endianness_, ByteOrder::host_order());
532  return val;
533  }
534 
539  size_t writeUnsigned(uint32_t value, rose_addr_t startVa) {
540  return at(startVa).limit(sizeof(uint32_t)).write((const uint8_t*)(&value)).size();
541  }
542 
547  size_t writeUnsigned(uint64_t value, rose_addr_t startVa) {
548  return at(startVa).limit(sizeof(uint64_t)).write((const uint8_t*)(&value)).size();
549  }
550 
554  Sawyer::Optional<uint8_t> readByte(rose_addr_t) const;
555 
557  SgUnsignedCharList readVector(rose_addr_t startVa, size_t desired, unsigned requiredPerms=READABLE) const;
558 
560  size_t writeQuick(const void *buf, rose_addr_t startVa, size_t desired) {
561  return at(startVa).limit(desired).require(WRITABLE).write((const uint8_t*)buf).size();
562  }
563 
571  Sawyer::Optional<rose_addr_t> findAny(const Extent &limits, const std::vector<uint8_t> &bytesToFind,
572  unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0) const;
573  Sawyer::Optional<rose_addr_t> findAny(const AddressInterval &limits, const std::vector<uint8_t> &bytesToFind,
574  unsigned requiredPerms=READABLE, unsigned prohibitedPerms=0) const;
582  Sawyer::Optional<rose_addr_t> findSequence(const AddressInterval &interval, const std::vector<uint8_t> &sequence) const;
583 
587  void dump(FILE*, const char *prefix="") const;
588  void dump(std::ostream&, std::string prefix="") const;
589  void print(std::ostream &o, std::string prefix="") const { dump(o, prefix); }
590  void dump() const; // mostly for calling from within GDB or similar
608 
610  static std::string segmentTitle(const Segment&);
611 
612  friend std::ostream& operator<<(std::ostream&, const MemoryMap&);
613 };
614 
615 } // namespace
616 } // namespace
617 
618 // Register the types needed for serialization since some of them are derived from polymorphic class templates.
619 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
621 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::MemoryMap::MappedBuffer);
622 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::MemoryMap::NullBuffer);
623 BOOST_CLASS_EXPORT_KEY(Rose::BinaryAnalysis::MemoryMap::StaticBuffer);
624 #endif
625 
626 #endif
627 #endif
Exception thrown by find_free() when there's not enough free space left.
Definition: MemoryMap.h:228
int colnum
Optional column number (0-origin; negative if unknown).
Definition: MemoryMap.h:247
Sawyer::Optional< uint8_t > readByte(rose_addr_t) const
Read a byte from memory.
Sawyer::Optional< uint64_t > readLongUnsinged(rose_addr_t startVa) const
Read a long unsigned value.
Definition: MemoryMap.h:527
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:238
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:589
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:283
rose_addr_t fileOffset
The accessibility flags.
Definition: MemoryMap.h:424
MemoryMap()
Constructs an empty memory map.
Definition: MemoryMap.h:252
Holds a value or nothing.
Definition: Optional.h:49
MemoryMap::Ptr map
Map that caused the exception if available, null otherwise.
Definition: MemoryMap.h:196
Attach with ptrace, get memory, then detach.
Definition: MemoryMap.h:129
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:421
Memory mapped file.
Definition: MappedBuffer.h:43
File is mapped with read-only permission.
Definition: MemoryMap.h:288
Allocates memory as needed.
std::string filename
Name of index file where error occurred.
Definition: MemoryMap.h:245
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:256
void byteOrder(ByteOrder::Endianness order)
Property: byte order.
Definition: MemoryMap.h:275
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:286
size_t readQuick(void *buf, rose_addr_t startVa, size_t desired) const
Read data into buffer.
Definition: MemoryMap.h:489
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:426
Exception for when we try to access a virtual address that isn't mapped.
Definition: MemoryMap.h:218
A mapping from address space to values.
Definition: AddressMap.h:1004
unsigned accessibility
Mapped virtual addresses.
Definition: MemoryMap.h:423
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:110
size_t writeUnsigned(uint32_t value, rose_addr_t startVa)
Write an unsigned value.
Definition: MemoryMap.h:539
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:246
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.
void linkTo(const MemoryMap::Ptr &other, const AddressIntervalSet &parts)
Insert part of another map by reference.
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:203
std::string deviceName
Starting byte offset in the file.
Definition: MemoryMap.h:425
size_t writeUnsigned(uint64_t value, rose_addr_t startVa)
Write a long unsigned value.
Definition: MemoryMap.h:547
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:126
std::string comment
Inode on the device, or zero.
Definition: MemoryMap.h:427
bool shrinkUnshare()
Shrink buffers and remove sharing.
Represents no value.
Definition: Optional.h:32
ByteOrder::Endianness byteOrder() const
Property: byte order.
Definition: MemoryMap.h:274
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:560
Sawyer::Optional< U > readUnsigned(rose_addr_t startVa) const
Read an unsigned value.
Definition: MemoryMap.h:514
Exception for MemoryMap operations.
Definition: MemoryMap.h:187
Assume ptrace is attached and process is stopped.
Definition: MemoryMap.h:128
Ptr shallowCopy()
Create a new copy of the memory map.
Definition: MemoryMap.h:263