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 
28 // Memory protection masks.
29 static const uptr kProtectionRead = 1;
30 static const uptr kProtectionWrite = 2;
31 static const uptr kProtectionExecute = 4;
32 static const uptr kProtectionShared = 8;
33 
34 struct MemoryMappedSegmentData;
35 
36 class MemoryMappedSegment {
37  public:
38   MemoryMappedSegment(char *buff = nullptr, uptr size = 0)
filename(buff)39       : filename(buff), filename_size(size), data_(nullptr) {}
~MemoryMappedSegment()40   ~MemoryMappedSegment() {}
41 
IsReadable()42   bool IsReadable() const { return protection & kProtectionRead; }
IsWritable()43   bool IsWritable() const { return protection & kProtectionWrite; }
IsExecutable()44   bool IsExecutable() const { return protection & kProtectionExecute; }
IsShared()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 
65 class 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.
86 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
87 
88 bool IsDecimal(char c);
89 uptr ParseDecimal(const char **p);
90 bool IsHex(char c);
91 uptr ParseHex(const char **p);
92 
93 }  // namespace __sanitizer
94 
95 #endif  // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD ||
96         // SANITIZER_MAC
97 #endif  // SANITIZER_PROCMAPS_H
98