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 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 // Don't subtract 'cur_beg' from the first entry:
134 // * If a binary is compiled w/o -pie, then the first entry in
135 // process maps is likely the binary itself (all dynamic libs
136 // are mapped higher in address space). For such a binary,
137 // instruction offset in binary coincides with the actual
138 // instruction address in virtual memory (as code section
139 // is mapped to a fixed memory range).
140 // * If a binary is compiled with -pie, all the modules are
141 // mapped high at address space (in particular, higher than
142 // shadow memory of the tool), so the module can't be the
144 uptr base_address
= (i
? cur_beg
: 0) - cur_offset
;
145 LoadedModule
*cur_module
= &modules
[n_modules
];
146 cur_module
->set(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
) {
154 char *smaps
= nullptr;
157 if (!ReadFileToBuffer("/proc/self/smaps", &smaps
, &smaps_cap
, &smaps_len
))
161 const char *pos
= smaps
;
162 while (pos
< smaps
+ smaps_len
) {
164 start
= ParseHex(&pos
);
165 for (; *pos
!= '/' && *pos
> '\n'; pos
++) {}
167 } else if (internal_strncmp(pos
, "Rss:", 4) == 0) {
168 while (!IsDecimal(*pos
)) pos
++;
169 uptr rss
= ParseDecimal(&pos
) * 1024;
170 cb(start
, rss
, file
, stats
, stats_size
);
172 while (*pos
++ != '\n') {}
174 UnmapOrDie(smaps
, smaps_cap
);
177 } // namespace __sanitizer
179 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX