]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_common.cc
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_procmaps_common.cc
1 //===-- sanitizer_procmaps_common.cc --------------------------------------===//
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 // Information about the process mappings (common parts).
11 //===----------------------------------------------------------------------===//
12
13 #include "sanitizer_platform.h"
14
15 #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||                \
16     SANITIZER_OPENBSD || SANITIZER_SOLARIS
17
18 #include "sanitizer_common.h"
19 #include "sanitizer_placement_new.h"
20 #include "sanitizer_procmaps.h"
21
22 namespace __sanitizer {
23
24 static ProcSelfMapsBuff cached_proc_self_maps;
25 static StaticSpinMutex cache_lock;
26
27 static int TranslateDigit(char c) {
28   if (c >= '0' && c <= '9')
29     return c - '0';
30   if (c >= 'a' && c <= 'f')
31     return c - 'a' + 10;
32   if (c >= 'A' && c <= 'F')
33     return c - 'A' + 10;
34   return -1;
35 }
36
37 // Parse a number and promote 'p' up to the first non-digit character.
38 static uptr ParseNumber(const char **p, int base) {
39   uptr n = 0;
40   int d;
41   CHECK(base >= 2 && base <= 16);
42   while ((d = TranslateDigit(**p)) >= 0 && d < base) {
43     n = n * base + d;
44     (*p)++;
45   }
46   return n;
47 }
48
49 bool IsDecimal(char c) {
50   int d = TranslateDigit(c);
51   return d >= 0 && d < 10;
52 }
53
54 uptr ParseDecimal(const char **p) {
55   return ParseNumber(p, 10);
56 }
57
58 bool IsHex(char c) {
59   int d = TranslateDigit(c);
60   return d >= 0 && d < 16;
61 }
62
63 uptr ParseHex(const char **p) {
64   return ParseNumber(p, 16);
65 }
66
67 void MemoryMappedSegment::AddAddressRanges(LoadedModule *module) {
68   // data_ should be unused on this platform
69   CHECK(!data_);
70   module->addAddressRange(start, end, IsExecutable(), IsWritable());
71 }
72
73 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
74   // FIXME: in the future we may want to cache the mappings on demand only.
75   if (cache_enabled)
76     CacheMemoryMappings();
77
78   // Read maps after the cache update to capture the maps/unmaps happening in
79   // the process of updating.
80   ReadProcMaps(&data_.proc_self_maps);
81   if (cache_enabled && data_.proc_self_maps.mmaped_size == 0)
82     LoadFromCache();
83   CHECK_GT(data_.proc_self_maps.mmaped_size, 0);
84   CHECK_GT(data_.proc_self_maps.len, 0);
85
86   Reset();
87 }
88
89 MemoryMappingLayout::~MemoryMappingLayout() {
90   // Only unmap the buffer if it is different from the cached one. Otherwise
91   // it will be unmapped when the cache is refreshed.
92   if (data_.proc_self_maps.data != cached_proc_self_maps.data)
93     UnmapOrDie(data_.proc_self_maps.data, data_.proc_self_maps.mmaped_size);
94 }
95
96 void MemoryMappingLayout::Reset() {
97   data_.current = data_.proc_self_maps.data;
98 }
99
100 // static
101 void MemoryMappingLayout::CacheMemoryMappings() {
102   ProcSelfMapsBuff new_proc_self_maps;
103   ReadProcMaps(&new_proc_self_maps);
104   // Don't invalidate the cache if the mappings are unavailable.
105   if (new_proc_self_maps.mmaped_size == 0)
106     return;
107   SpinMutexLock l(&cache_lock);
108   if (cached_proc_self_maps.mmaped_size)
109     UnmapOrDie(cached_proc_self_maps.data, cached_proc_self_maps.mmaped_size);
110   cached_proc_self_maps = new_proc_self_maps;
111 }
112
113 void MemoryMappingLayout::LoadFromCache() {
114   SpinMutexLock l(&cache_lock);
115   if (cached_proc_self_maps.data)
116     data_.proc_self_maps = cached_proc_self_maps;
117 }
118
119 void MemoryMappingLayout::DumpListOfModules(
120     InternalMmapVectorNoCtor<LoadedModule> *modules) {
121   Reset();
122   InternalScopedString module_name(kMaxPathLength);
123   MemoryMappedSegment segment(module_name.data(), module_name.size());
124   for (uptr i = 0; Next(&segment); i++) {
125     const char *cur_name = segment.filename;
126     if (cur_name[0] == '\0')
127       continue;
128     // Don't subtract 'cur_beg' from the first entry:
129     // * If a binary is compiled w/o -pie, then the first entry in
130     //   process maps is likely the binary itself (all dynamic libs
131     //   are mapped higher in address space). For such a binary,
132     //   instruction offset in binary coincides with the actual
133     //   instruction address in virtual memory (as code section
134     //   is mapped to a fixed memory range).
135     // * If a binary is compiled with -pie, all the modules are
136     //   mapped high at address space (in particular, higher than
137     //   shadow memory of the tool), so the module can't be the
138     //   first entry.
139     uptr base_address = (i ? segment.start : 0) - segment.offset;
140     LoadedModule cur_module;
141     cur_module.set(cur_name, base_address);
142     segment.AddAddressRanges(&cur_module);
143     modules->push_back(cur_module);
144   }
145 }
146
147 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {
148   char *smaps = nullptr;
149   uptr smaps_cap = 0;
150   uptr smaps_len = 0;
151   if (!ReadFileToBuffer("/proc/self/smaps", &smaps, &smaps_cap, &smaps_len))
152     return;
153   uptr start = 0;
154   bool file = false;
155   const char *pos = smaps;
156   while (pos < smaps + smaps_len) {
157     if (IsHex(pos[0])) {
158       start = ParseHex(&pos);
159       for (; *pos != '/' && *pos > '\n'; pos++) {}
160       file = *pos == '/';
161     } else if (internal_strncmp(pos, "Rss:", 4) == 0) {
162       while (!IsDecimal(*pos)) pos++;
163       uptr rss = ParseDecimal(&pos) * 1024;
164       cb(start, rss, file, stats, stats_size);
165     }
166     while (*pos++ != '\n') {}
167   }
168   UnmapOrDie(smaps, smaps_cap);
169 }
170
171 } // namespace __sanitizer
172
173 #endif