| 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_APPLE || SANITIZER_SOLARIS || SANITIZER_HAIKU || \ |
| 20 | SANITIZER_FUCHSIA |
| 21 | |
| 22 | #include "sanitizer_common.h" |
| 23 | #include "sanitizer_internal_defs.h" |
| 24 | #include "sanitizer_fuchsia.h" |
| 25 | #include "sanitizer_linux.h" |
| 26 | #include "sanitizer_mac.h" |
| 27 | #include "sanitizer_mutex.h" |
| 28 | |
| 29 | namespace __sanitizer { |
| 30 | |
| 31 | // Memory protection masks. |
| 32 | static const uptr kProtectionRead = 1; |
| 33 | static const uptr kProtectionWrite = 2; |
| 34 | static const uptr kProtectionExecute = 4; |
| 35 | static const uptr kProtectionShared = 8; |
| 36 | |
| 37 | struct MemoryMappedSegmentData; |
| 38 | |
| 39 | class MemoryMappedSegment { |
| 40 | public: |
| 41 | explicit MemoryMappedSegment(char *buff = nullptr, uptr size = 0) |
| 42 | : filename(buff), filename_size(size), data_(nullptr) {} |
| 43 | ~MemoryMappedSegment() {} |
| 44 | |
| 45 | bool IsReadable() const { return protection & kProtectionRead; } |
| 46 | bool IsWritable() const { return protection & kProtectionWrite; } |
| 47 | bool IsExecutable() const { return protection & kProtectionExecute; } |
| 48 | bool IsShared() const { return protection & kProtectionShared; } |
| 49 | |
| 50 | void AddAddressRanges(LoadedModule *module); |
| 51 | |
| 52 | uptr start; |
| 53 | uptr end; |
| 54 | uptr offset; |
| 55 | char *filename; // owned by caller |
| 56 | uptr filename_size; |
| 57 | uptr protection; |
| 58 | ModuleArch arch; |
| 59 | u8 uuid[kModuleUUIDSize]; |
| 60 | |
| 61 | private: |
| 62 | friend class MemoryMappingLayout; |
| 63 | |
| 64 | // This field is assigned and owned by MemoryMappingLayout if needed |
| 65 | MemoryMappedSegmentData *data_; |
| 66 | }; |
| 67 | |
| 68 | struct ; |
| 69 | |
| 70 | class MemoryMappingLayoutBase { |
| 71 | public: |
| 72 | virtual bool Next(MemoryMappedSegment *segment) { UNIMPLEMENTED(); } |
| 73 | virtual bool Error() const { UNIMPLEMENTED(); }; |
| 74 | virtual void Reset() { UNIMPLEMENTED(); } |
| 75 | |
| 76 | protected: |
| 77 | ~MemoryMappingLayoutBase() {} |
| 78 | }; |
| 79 | |
| 80 | class MemoryMappingLayout : public MemoryMappingLayoutBase { |
| 81 | public: |
| 82 | explicit MemoryMappingLayout(bool cache_enabled); |
| 83 | |
| 84 | // This destructor cannot be virtual, as it would cause an operator new() linking |
| 85 | // failures in hwasan test cases. However non-virtual destructors emit warnings |
| 86 | // in macOS build, hence disabling those |
| 87 | #ifdef __clang__ |
| 88 | #pragma clang diagnostic push |
| 89 | #pragma clang diagnostic ignored "-Wnon-virtual-dtor" |
| 90 | #endif |
| 91 | ~MemoryMappingLayout(); |
| 92 | #ifdef __clang__ |
| 93 | #pragma clang diagnostic pop |
| 94 | #endif |
| 95 | |
| 96 | virtual bool Next(MemoryMappedSegment *segment) override; |
| 97 | virtual bool Error() const override; |
| 98 | virtual void Reset() override; |
| 99 | // In some cases, e.g. when running under a sandbox on Linux, ASan is unable |
| 100 | // to obtain the memory mappings. It should fall back to pre-cached data |
| 101 | // instead of aborting. |
| 102 | static void CacheMemoryMappings(); |
| 103 | |
| 104 | // Adds all mapped objects into a vector. |
| 105 | void DumpListOfModules(InternalMmapVectorNoCtor<LoadedModule> *modules); |
| 106 | |
| 107 | protected: |
| 108 | #if SANITIZER_APPLE |
| 109 | virtual const ImageHeader *CurrentImageHeader(); |
| 110 | #endif |
| 111 | MemoryMappingLayoutData data_; |
| 112 | |
| 113 | private: |
| 114 | void LoadFromCache(); |
| 115 | }; |
| 116 | |
| 117 | // Returns code range for the specified module. |
| 118 | bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end); |
| 119 | |
| 120 | bool IsDecimal(char c); |
| 121 | uptr ParseDecimal(const char **p); |
| 122 | bool IsHex(char c); |
| 123 | uptr ParseHex(const char **p); |
| 124 | |
| 125 | } // namespace __sanitizer |
| 126 | |
| 127 | #endif |
| 128 | #endif // SANITIZER_PROCMAPS_H |
| 129 | |