1 //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer.
9 //
10 // Information about the process mappings.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_PROCMAPS_H
13 #define SANITIZER_PROCMAPS_H
14 
15 #include "sanitizer_platform.h"
16 
17 #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD ||                \
18     SANITIZER_OPENBSD || SANITIZER_MAC || SANITIZER_SOLARIS
19 
20 #include "sanitizer_common.h"
21 #include "sanitizer_internal_defs.h"
22 #include "sanitizer_linux.h"
23 #include "sanitizer_mac.h"
24 #include "sanitizer_mutex.h"
25 
26 namespace __sanitizer {
27 
28 
29 // Memory protection masks.
30 static const uptr kProtectionRead = 1;
31 static const uptr kProtectionWrite = 2;
32 static const uptr kProtectionExecute = 4;
33 static const uptr kProtectionShared = 8;
34 
35 struct MemoryMappedSegmentData;
36 
37 class MemoryMappedSegment {
38  public:
39   MemoryMappedSegment(char *buff = nullptr, uptr size = 0)
filename(buff)40       : filename(buff), filename_size(size), data_(nullptr) {}
~MemoryMappedSegment()41   ~MemoryMappedSegment() {}
42 
IsReadable()43   bool IsReadable() const { return protection & kProtectionRead; }
IsWritable()44   bool IsWritable() const { return protection & kProtectionWrite; }
IsExecutable()45   bool IsExecutable() const { return protection & kProtectionExecute; }
IsShared()46   bool IsShared() const { return protection & kProtectionShared; }
47 
48   void AddAddressRanges(LoadedModule *module);
49 
50   uptr start;
51   uptr end;
52   uptr offset;
53   char *filename;  // owned by caller
54   uptr filename_size;
55   uptr protection;
56   ModuleArch arch;
57   u8 uuid[kModuleUUIDSize];
58 
59  private:
60   friend class MemoryMappingLayout;
61 
62   // This field is assigned and owned by MemoryMappingLayout if needed
63   MemoryMappedSegmentData *data_;
64 };
65 
66 class MemoryMappingLayout {
67  public:
68   explicit MemoryMappingLayout(bool cache_enabled);
69   ~MemoryMappingLayout();
70   bool Next(MemoryMappedSegment *segment);
71   void Reset();
72   // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
73   // to obtain the memory mappings. It should fall back to pre-cached data
74   // instead of aborting.
75   static void CacheMemoryMappings();
76 
77   // Adds all mapped objects into a vector.
78   void DumpListOfModules(InternalMmapVectorNoCtor<LoadedModule> *modules);
79 
80  private:
81   void LoadFromCache();
82 
83   MemoryMappingLayoutData data_;
84 };
85 
86 // Returns code range for the specified module.
87 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
88 
89 bool IsDecimal(char c);
90 uptr ParseDecimal(const char **p);
91 bool IsHex(char c);
92 uptr ParseHex(const char **p);
93 
94 }  // namespace __sanitizer
95 
96 #endif
97 #endif  // SANITIZER_PROCMAPS_H
98