xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_procmaps_common.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
168d75effSDimitry Andric //===-- sanitizer_procmaps_common.cpp -------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // Information about the process mappings (common parts).
1068d75effSDimitry Andric //===----------------------------------------------------------------------===//
1168d75effSDimitry Andric 
1268d75effSDimitry Andric #include "sanitizer_platform.h"
1368d75effSDimitry Andric 
1468d75effSDimitry Andric #if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||                \
15e8d8bef9SDimitry Andric     SANITIZER_SOLARIS
1668d75effSDimitry Andric 
1768d75effSDimitry Andric #include "sanitizer_common.h"
1868d75effSDimitry Andric #include "sanitizer_placement_new.h"
1968d75effSDimitry Andric #include "sanitizer_procmaps.h"
2068d75effSDimitry Andric 
2168d75effSDimitry Andric namespace __sanitizer {
2268d75effSDimitry Andric 
2368d75effSDimitry Andric static ProcSelfMapsBuff cached_proc_self_maps;
2468d75effSDimitry Andric static StaticSpinMutex cache_lock;
2568d75effSDimitry Andric 
TranslateDigit(char c)2668d75effSDimitry Andric static int TranslateDigit(char c) {
2768d75effSDimitry Andric   if (c >= '0' && c <= '9')
2868d75effSDimitry Andric     return c - '0';
2968d75effSDimitry Andric   if (c >= 'a' && c <= 'f')
3068d75effSDimitry Andric     return c - 'a' + 10;
3168d75effSDimitry Andric   if (c >= 'A' && c <= 'F')
3268d75effSDimitry Andric     return c - 'A' + 10;
3368d75effSDimitry Andric   return -1;
3468d75effSDimitry Andric }
3568d75effSDimitry Andric 
3668d75effSDimitry Andric // Parse a number and promote 'p' up to the first non-digit character.
ParseNumber(const char ** p,int base)3768d75effSDimitry Andric static uptr ParseNumber(const char **p, int base) {
3868d75effSDimitry Andric   uptr n = 0;
3968d75effSDimitry Andric   int d;
4068d75effSDimitry Andric   CHECK(base >= 2 && base <= 16);
4168d75effSDimitry Andric   while ((d = TranslateDigit(**p)) >= 0 && d < base) {
4268d75effSDimitry Andric     n = n * base + d;
4368d75effSDimitry Andric     (*p)++;
4468d75effSDimitry Andric   }
4568d75effSDimitry Andric   return n;
4668d75effSDimitry Andric }
4768d75effSDimitry Andric 
IsDecimal(char c)4868d75effSDimitry Andric bool IsDecimal(char c) {
4968d75effSDimitry Andric   int d = TranslateDigit(c);
5068d75effSDimitry Andric   return d >= 0 && d < 10;
5168d75effSDimitry Andric }
5268d75effSDimitry Andric 
ParseDecimal(const char ** p)5368d75effSDimitry Andric uptr ParseDecimal(const char **p) {
5468d75effSDimitry Andric   return ParseNumber(p, 10);
5568d75effSDimitry Andric }
5668d75effSDimitry Andric 
IsHex(char c)5768d75effSDimitry Andric bool IsHex(char c) {
5868d75effSDimitry Andric   int d = TranslateDigit(c);
5968d75effSDimitry Andric   return d >= 0 && d < 16;
6068d75effSDimitry Andric }
6168d75effSDimitry Andric 
ParseHex(const char ** p)6268d75effSDimitry Andric uptr ParseHex(const char **p) {
6368d75effSDimitry Andric   return ParseNumber(p, 16);
6468d75effSDimitry Andric }
6568d75effSDimitry Andric 
AddAddressRanges(LoadedModule * module)6668d75effSDimitry Andric void MemoryMappedSegment::AddAddressRanges(LoadedModule *module) {
6768d75effSDimitry Andric   // data_ should be unused on this platform
6868d75effSDimitry Andric   CHECK(!data_);
6968d75effSDimitry Andric   module->addAddressRange(start, end, IsExecutable(), IsWritable());
7068d75effSDimitry Andric }
7168d75effSDimitry Andric 
MemoryMappingLayout(bool cache_enabled)7268d75effSDimitry Andric MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
7368d75effSDimitry Andric   // FIXME: in the future we may want to cache the mappings on demand only.
7468d75effSDimitry Andric   if (cache_enabled)
7568d75effSDimitry Andric     CacheMemoryMappings();
7668d75effSDimitry Andric 
7768d75effSDimitry Andric   // Read maps after the cache update to capture the maps/unmaps happening in
7868d75effSDimitry Andric   // the process of updating.
7968d75effSDimitry Andric   ReadProcMaps(&data_.proc_self_maps);
8068d75effSDimitry Andric   if (cache_enabled && data_.proc_self_maps.mmaped_size == 0)
8168d75effSDimitry Andric     LoadFromCache();
8268d75effSDimitry Andric 
8368d75effSDimitry Andric   Reset();
8468d75effSDimitry Andric }
8568d75effSDimitry Andric 
Error() const8668d75effSDimitry Andric bool MemoryMappingLayout::Error() const {
8768d75effSDimitry Andric   return data_.current == nullptr;
8868d75effSDimitry Andric }
8968d75effSDimitry Andric 
~MemoryMappingLayout()9068d75effSDimitry Andric MemoryMappingLayout::~MemoryMappingLayout() {
9168d75effSDimitry Andric   // Only unmap the buffer if it is different from the cached one. Otherwise
9268d75effSDimitry Andric   // it will be unmapped when the cache is refreshed.
9368d75effSDimitry Andric   if (data_.proc_self_maps.data != cached_proc_self_maps.data)
9468d75effSDimitry Andric     UnmapOrDie(data_.proc_self_maps.data, data_.proc_self_maps.mmaped_size);
9568d75effSDimitry Andric }
9668d75effSDimitry Andric 
Reset()9768d75effSDimitry Andric void MemoryMappingLayout::Reset() {
9868d75effSDimitry Andric   data_.current = data_.proc_self_maps.data;
9968d75effSDimitry Andric }
10068d75effSDimitry Andric 
10168d75effSDimitry Andric // static
CacheMemoryMappings()10268d75effSDimitry Andric void MemoryMappingLayout::CacheMemoryMappings() {
10368d75effSDimitry Andric   ProcSelfMapsBuff new_proc_self_maps;
10468d75effSDimitry Andric   ReadProcMaps(&new_proc_self_maps);
10568d75effSDimitry Andric   // Don't invalidate the cache if the mappings are unavailable.
10668d75effSDimitry Andric   if (new_proc_self_maps.mmaped_size == 0)
10768d75effSDimitry Andric     return;
10868d75effSDimitry Andric   SpinMutexLock l(&cache_lock);
10968d75effSDimitry Andric   if (cached_proc_self_maps.mmaped_size)
11068d75effSDimitry Andric     UnmapOrDie(cached_proc_self_maps.data, cached_proc_self_maps.mmaped_size);
11168d75effSDimitry Andric   cached_proc_self_maps = new_proc_self_maps;
11268d75effSDimitry Andric }
11368d75effSDimitry Andric 
LoadFromCache()11468d75effSDimitry Andric void MemoryMappingLayout::LoadFromCache() {
11568d75effSDimitry Andric   SpinMutexLock l(&cache_lock);
11668d75effSDimitry Andric   if (cached_proc_self_maps.data)
11768d75effSDimitry Andric     data_.proc_self_maps = cached_proc_self_maps;
11868d75effSDimitry Andric }
11968d75effSDimitry Andric 
DumpListOfModules(InternalMmapVectorNoCtor<LoadedModule> * modules)12068d75effSDimitry Andric void MemoryMappingLayout::DumpListOfModules(
12168d75effSDimitry Andric     InternalMmapVectorNoCtor<LoadedModule> *modules) {
12268d75effSDimitry Andric   Reset();
123fe6060f1SDimitry Andric   InternalMmapVector<char> module_name(kMaxPathLength);
12468d75effSDimitry Andric   MemoryMappedSegment segment(module_name.data(), module_name.size());
12568d75effSDimitry Andric   for (uptr i = 0; Next(&segment); i++) {
12668d75effSDimitry Andric     const char *cur_name = segment.filename;
12768d75effSDimitry Andric     if (cur_name[0] == '\0')
12868d75effSDimitry Andric       continue;
12968d75effSDimitry Andric     // Don't subtract 'cur_beg' from the first entry:
13068d75effSDimitry Andric     // * If a binary is compiled w/o -pie, then the first entry in
13168d75effSDimitry Andric     //   process maps is likely the binary itself (all dynamic libs
13268d75effSDimitry Andric     //   are mapped higher in address space). For such a binary,
13368d75effSDimitry Andric     //   instruction offset in binary coincides with the actual
13468d75effSDimitry Andric     //   instruction address in virtual memory (as code section
13568d75effSDimitry Andric     //   is mapped to a fixed memory range).
13668d75effSDimitry Andric     // * If a binary is compiled with -pie, all the modules are
13768d75effSDimitry Andric     //   mapped high at address space (in particular, higher than
13868d75effSDimitry Andric     //   shadow memory of the tool), so the module can't be the
13968d75effSDimitry Andric     //   first entry.
14068d75effSDimitry Andric     uptr base_address = (i ? segment.start : 0) - segment.offset;
14168d75effSDimitry Andric     LoadedModule cur_module;
14268d75effSDimitry Andric     cur_module.set(cur_name, base_address);
14368d75effSDimitry Andric     segment.AddAddressRanges(&cur_module);
14468d75effSDimitry Andric     modules->push_back(cur_module);
14568d75effSDimitry Andric   }
14668d75effSDimitry Andric }
14768d75effSDimitry Andric 
148*0fca6ea1SDimitry Andric #if SANITIZER_LINUX || SANITIZER_ANDROID || SANITIZER_SOLARIS
GetMemoryProfile(fill_profile_f cb,uptr * stats)149349cc55cSDimitry Andric void GetMemoryProfile(fill_profile_f cb, uptr *stats) {
15068d75effSDimitry Andric   char *smaps = nullptr;
15168d75effSDimitry Andric   uptr smaps_cap = 0;
15268d75effSDimitry Andric   uptr smaps_len = 0;
15368d75effSDimitry Andric   if (!ReadFileToBuffer("/proc/self/smaps", &smaps, &smaps_cap, &smaps_len))
15468d75effSDimitry Andric     return;
155349cc55cSDimitry Andric   ParseUnixMemoryProfile(cb, stats, smaps, smaps_len);
156349cc55cSDimitry Andric   UnmapOrDie(smaps, smaps_cap);
157349cc55cSDimitry Andric }
158349cc55cSDimitry Andric 
ParseUnixMemoryProfile(fill_profile_f cb,uptr * stats,char * smaps,uptr smaps_len)159349cc55cSDimitry Andric void ParseUnixMemoryProfile(fill_profile_f cb, uptr *stats, char *smaps,
160349cc55cSDimitry Andric                             uptr smaps_len) {
16168d75effSDimitry Andric   uptr start = 0;
16268d75effSDimitry Andric   bool file = false;
16368d75effSDimitry Andric   const char *pos = smaps;
164349cc55cSDimitry Andric   char *end = smaps + smaps_len;
165349cc55cSDimitry Andric   if (smaps_len < 2)
166349cc55cSDimitry Andric     return;
167349cc55cSDimitry Andric   // The following parsing can crash on almost every line
168349cc55cSDimitry Andric   // in the case of malformed/truncated input.
169349cc55cSDimitry Andric   // Fixing that is hard b/c e.g. ParseDecimal does not
170349cc55cSDimitry Andric   // even accept end of the buffer and assumes well-formed input.
171349cc55cSDimitry Andric   // So instead we patch end of the input a bit,
172349cc55cSDimitry Andric   // it does not affect well-formed complete inputs.
173349cc55cSDimitry Andric   *--end = 0;
174349cc55cSDimitry Andric   *--end = '\n';
175349cc55cSDimitry Andric   while (pos < end) {
17668d75effSDimitry Andric     if (IsHex(pos[0])) {
17768d75effSDimitry Andric       start = ParseHex(&pos);
17868d75effSDimitry Andric       for (; *pos != '/' && *pos > '\n'; pos++) {}
17968d75effSDimitry Andric       file = *pos == '/';
18068d75effSDimitry Andric     } else if (internal_strncmp(pos, "Rss:", 4) == 0) {
181349cc55cSDimitry Andric       while (pos < end && !IsDecimal(*pos)) pos++;
18268d75effSDimitry Andric       uptr rss = ParseDecimal(&pos) * 1024;
183349cc55cSDimitry Andric       cb(start, rss, file, stats);
18468d75effSDimitry Andric     }
18568d75effSDimitry Andric     while (*pos++ != '\n') {}
18668d75effSDimitry Andric   }
18768d75effSDimitry Andric }
188bdd1243dSDimitry Andric #endif
18968d75effSDimitry Andric 
19068d75effSDimitry Andric } // namespace __sanitizer
19168d75effSDimitry Andric 
19268d75effSDimitry Andric #endif
193