]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h
Upgrade our copies of clang, llvm, lld and libc++ to r311219 from the
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_procmaps.h
1 //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer.
11 //
12 // Information about the process mappings.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_PROCMAPS_H
15 #define SANITIZER_PROCMAPS_H
16
17 #include "sanitizer_common.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_mutex.h"
20
21 namespace __sanitizer {
22
23 #if SANITIZER_FREEBSD || SANITIZER_LINUX
24 struct ProcSelfMapsBuff {
25   char *data;
26   uptr mmaped_size;
27   uptr len;
28 };
29
30 // Reads process memory map in an OS-specific way.
31 void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
32 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX
33
34 // Memory protection masks.
35 static const uptr kProtectionRead = 1;
36 static const uptr kProtectionWrite = 2;
37 static const uptr kProtectionExecute = 4;
38 static const uptr kProtectionShared = 8;
39
40 struct MemoryMappedSegment {
41   MemoryMappedSegment(char *buff = nullptr, uptr size = 0)
42       : filename(buff), filename_size(size) {}
43   ~MemoryMappedSegment() {}
44
45   bool IsReadable() { return protection & kProtectionRead; }
46   bool IsWritable() { return protection & kProtectionWrite; }
47   bool IsExecutable() { return protection & kProtectionExecute; }
48   bool IsShared() { return protection & kProtectionShared; }
49
50   uptr start;
51   uptr end;
52   uptr offset;
53   char *filename;  // owned by caller
54   uptr filename_size;
55   uptr protection;
56   ModuleArch arch;
57   u8 uuid[kModuleUUIDSize];
58 };
59
60 class MemoryMappingLayout {
61  public:
62   explicit MemoryMappingLayout(bool cache_enabled);
63   ~MemoryMappingLayout();
64   bool Next(MemoryMappedSegment *segment);
65   void Reset();
66   // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
67   // to obtain the memory mappings. It should fall back to pre-cached data
68   // instead of aborting.
69   static void CacheMemoryMappings();
70
71   // Adds all mapped objects into a vector.
72   void DumpListOfModules(InternalMmapVector<LoadedModule> *modules);
73
74  private:
75   void LoadFromCache();
76
77   // FIXME: Hide implementation details for different platforms in
78   // platform-specific files.
79 # if SANITIZER_FREEBSD || SANITIZER_LINUX
80   ProcSelfMapsBuff proc_self_maps_;
81   const char *current_;
82
83   // Static mappings cache.
84   static ProcSelfMapsBuff cached_proc_self_maps_;
85   static StaticSpinMutex cache_lock_;  // protects cached_proc_self_maps_.
86 # elif SANITIZER_MAC
87   template <u32 kLCSegment, typename SegmentCommand>
88   bool NextSegmentLoad(MemoryMappedSegment *segment);
89   int current_image_;
90   u32 current_magic_;
91   u32 current_filetype_;
92   ModuleArch current_arch_;
93   u8 current_uuid_[kModuleUUIDSize];
94   int current_load_cmd_count_;
95   char *current_load_cmd_addr_;
96   bool current_instrumented_;
97 # endif
98 };
99
100 typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
101                                /*out*/uptr *stats, uptr stats_size);
102
103 // Parse the contents of /proc/self/smaps and generate a memory profile.
104 // |cb| is a tool-specific callback that fills the |stats| array containing
105 // |stats_size| elements.
106 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
107
108 // Returns code range for the specified module.
109 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
110
111 bool IsDecimal(char c);
112 uptr ParseDecimal(const char **p);
113 bool IsHex(char c);
114 uptr ParseHex(const char **p);
115
116 }  // namespace __sanitizer
117
118 #endif  // SANITIZER_PROCMAPS_H