1 //===-- sanitizer_procmaps_common.cc --------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Information about the process mappings (common parts).
9 //===----------------------------------------------------------------------===//
11 #include "sanitizer_platform.h"
13 #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 void MemoryMappingLayout::DumpListOfModules(
118 InternalMmapVector
<LoadedModule
> *modules
) {
120 uptr cur_beg
, cur_end
, cur_offset
, prot
;
121 InternalScopedString
module_name(kMaxPathLength
);
122 for (uptr i
= 0; Next(&cur_beg
, &cur_end
, &cur_offset
, module_name
.data(),
123 module_name
.size(), &prot
);
125 const char *cur_name
= module_name
.data();
126 if (cur_name
[0] == '\0')
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
139 uptr base_address
= (i
? cur_beg
: 0) - cur_offset
;
140 LoadedModule cur_module
;
141 cur_module
.set(cur_name
, base_address
);
142 cur_module
.addAddressRange(cur_beg
, cur_end
, prot
& kProtectionExecute
);
143 modules
->push_back(cur_module
);
147 void GetMemoryProfile(fill_profile_f cb
, uptr
*stats
, uptr stats_size
) {
148 char *smaps
= nullptr;
151 if (!ReadFileToBuffer("/proc/self/smaps", &smaps
, &smaps_cap
, &smaps_len
))
155 const char *pos
= smaps
;
156 while (pos
< smaps
+ smaps_len
) {
158 start
= ParseHex(&pos
);
159 for (; *pos
!= '/' && *pos
> '\n'; 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
);
166 while (*pos
++ != '\n') {}
168 UnmapOrDie(smaps
, smaps_cap
);
171 } // namespace __sanitizer
173 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX