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