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