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