1 //===-- sanitizer_procmaps_common.cc --------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Information about the process mappings (common parts).
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_platform.h"
14 #if SANITIZER_FREEBSD || SANITIZER_LINUX
15 #include "sanitizer_common.h"
16 #include "sanitizer_placement_new.h"
17 #include "sanitizer_procmaps.h"
19 namespace __sanitizer {
21 // Linker initialized.
22 ProcSelfMapsBuff MemoryMappingLayout::cached_proc_self_maps_;
23 StaticSpinMutex MemoryMappingLayout::cache_lock_; // Linker initialized.
25 static int TranslateDigit(char c) {
26 if (c >= '0' && c <= '9')
28 if (c >= 'a' && c <= 'f')
30 if (c >= 'A' && c <= 'F')
35 // Parse a number and promote 'p' up to the first non-digit character.
36 static uptr ParseNumber(const char **p, int base) {
39 CHECK(base >= 2 && base <= 16);
40 while ((d = TranslateDigit(**p)) >= 0 && d < base) {
47 bool IsDecimal(char c) {
48 int d = TranslateDigit(c);
49 return d >= 0 && d < 10;
52 uptr ParseDecimal(const char **p) {
53 return ParseNumber(p, 10);
57 int d = TranslateDigit(c);
58 return d >= 0 && d < 16;
61 uptr ParseHex(const char **p) {
62 return ParseNumber(p, 16);
65 MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
66 ReadProcMaps(&proc_self_maps_);
68 if (proc_self_maps_.mmaped_size == 0) {
70 CHECK_GT(proc_self_maps_.len, 0);
73 CHECK_GT(proc_self_maps_.mmaped_size, 0);
76 // FIXME: in the future we may want to cache the mappings on demand only.
78 CacheMemoryMappings();
81 MemoryMappingLayout::~MemoryMappingLayout() {
82 // Only unmap the buffer if it is different from the cached one. Otherwise
83 // it will be unmapped when the cache is refreshed.
84 if (proc_self_maps_.data != cached_proc_self_maps_.data) {
85 UnmapOrDie(proc_self_maps_.data, proc_self_maps_.mmaped_size);
89 void MemoryMappingLayout::Reset() {
90 current_ = proc_self_maps_.data;
94 void MemoryMappingLayout::CacheMemoryMappings() {
95 SpinMutexLock l(&cache_lock_);
96 // Don't invalidate the cache if the mappings are unavailable.
97 ProcSelfMapsBuff old_proc_self_maps;
98 old_proc_self_maps = cached_proc_self_maps_;
99 ReadProcMaps(&cached_proc_self_maps_);
100 if (cached_proc_self_maps_.mmaped_size == 0) {
101 cached_proc_self_maps_ = old_proc_self_maps;
103 if (old_proc_self_maps.mmaped_size) {
104 UnmapOrDie(old_proc_self_maps.data,
105 old_proc_self_maps.mmaped_size);
110 void MemoryMappingLayout::LoadFromCache() {
111 SpinMutexLock l(&cache_lock_);
112 if (cached_proc_self_maps_.data) {
113 proc_self_maps_ = cached_proc_self_maps_;
117 uptr MemoryMappingLayout::DumpListOfModules(LoadedModule *modules,
119 string_predicate_t filter) {
121 uptr cur_beg, cur_end, cur_offset, prot;
122 InternalScopedString module_name(kMaxPathLength);
124 for (uptr i = 0; n_modules < max_modules &&
125 Next(&cur_beg, &cur_end, &cur_offset, module_name.data(),
126 module_name.size(), &prot);
128 const char *cur_name = module_name.data();
129 if (cur_name[0] == '\0')
131 if (filter && !filter(cur_name))
133 void *mem = &modules[n_modules];
134 // Don't subtract 'cur_beg' from the first entry:
135 // * If a binary is compiled w/o -pie, then the first entry in
136 // process maps is likely the binary itself (all dynamic libs
137 // are mapped higher in address space). For such a binary,
138 // instruction offset in binary coincides with the actual
139 // instruction address in virtual memory (as code section
140 // is mapped to a fixed memory range).
141 // * If a binary is compiled with -pie, all the modules are
142 // mapped high at address space (in particular, higher than
143 // shadow memory of the tool), so the module can't be the
145 uptr base_address = (i ? cur_beg : 0) - cur_offset;
146 LoadedModule *cur_module = new(mem) LoadedModule(cur_name, base_address);
147 cur_module->addAddressRange(cur_beg, cur_end, prot & kProtectionExecute);
153 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {
156 uptr smaps_len = ReadFileToBuffer("/proc/self/smaps",
157 &smaps, &smaps_cap, 64<<20);
160 const char *pos = smaps;
161 while (pos < smaps + smaps_len) {
163 start = ParseHex(&pos);
164 for (; *pos != '/' && *pos > '\n'; pos++) {}
166 } else if (internal_strncmp(pos, "Rss:", 4) == 0) {
167 while (!IsDecimal(*pos)) pos++;
168 uptr rss = ParseDecimal(&pos) * 1024;
169 cb(start, rss, file, stats, stats_size);
171 while (*pos++ != '\n') {}
173 UnmapOrDie(smaps, smaps_cap);
176 } // namespace __sanitizer
178 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX