2014-08-06 18:16:52 +08:00
|
|
|
//===-- sanitizer_procmaps_common.cc --------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Information about the process mappings (common parts).
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "sanitizer_platform.h"
|
2015-10-01 07:52:54 +08:00
|
|
|
|
[Sanitizers] Basic sanitizer Solaris support (PR 33274)
Summary:
This is the first mostly working version of the Sanitizer port to 32-bit Solaris/x86.
It is currently based on Solaris 11.4 Beta.
This part was initially developed inside libsanitizer in the GCC tree and should apply to
both. Subsequent parts will address changes to clang, the compiler-rt build system
and testsuite.
I'm not yet sure what the right patch granularity is: if it's profitable to split the patch
up, I'd like to get guidance on how to do so.
Most of the changes are probably straightforward with a few exceptions:
* The Solaris syscall interface isn't stable, undocumented and can change within an
OS release. The stable interface is the libc interface, which I'm using here, if possible
using the internal _-prefixed names.
* While the patch primarily target 32-bit x86, I've left a few sparc changes in. They
cannot currently be used with clang due to a backend limitation, but have worked
fine inside the gcc tree.
* Some functions (e.g. largefile versions of functions like open64) only exist in 32-bit
Solaris, so I've introduced a separate SANITIZER_SOLARIS32 to check for that.
The patch (with the subsequent ones to be submitted shortly) was tested
on i386-pc-solaris2.11. Only a few failures remain, some of them analyzed, some
still TBD:
AddressSanitizer-i386-sunos :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos :: TestCases/malloc-no-intercept.c
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/start-deactivated.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/default_options.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/malloc-no-intercept.c
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/MemoryMappingLayout.DumpListOfModules
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/SanitizerCommon.PthreadDestructorIterations
Maybe this is good enough the get the ball rolling.
Reviewers: kcc, alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, jyknight, kubamracek, krytarowski, fedor.sergeev, llvm-commits, #sanitizers
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D40898
llvm-svn: 320740
2017-12-15 04:14:29 +08:00
|
|
|
#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \
|
|
|
|
SANITIZER_SOLARIS
|
2015-10-01 07:52:54 +08:00
|
|
|
|
2014-08-06 18:16:52 +08:00
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_placement_new.h"
|
|
|
|
#include "sanitizer_procmaps.h"
|
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
Removed platform-specific ifdefs from sanitizer_procmaps.h
Summary: Removed platform-specific ifdefs for linux, mac, freebsd and netbsd from sanitizer_procmaps.h
Patch by Yicheng Wang <yichengfb@fb.com>
Reviewers: kcc, kubamracek, alekseyshl, fjricci, vitalybuka
Reviewed By: fjricci, vitalybuka
Subscribers: vitalybuka, emaste, krytarowski, llvm-commits
Differential Revision: https://reviews.llvm.org/D38098
llvm-svn: 313999
2017-09-23 01:48:24 +08:00
|
|
|
static ProcSelfMapsBuff cached_proc_self_maps;
|
|
|
|
static StaticSpinMutex cache_lock;
|
2014-08-06 18:16:52 +08:00
|
|
|
|
|
|
|
static int TranslateDigit(char c) {
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
if (c >= 'a' && c <= 'f')
|
|
|
|
return c - 'a' + 10;
|
|
|
|
if (c >= 'A' && c <= 'F')
|
|
|
|
return c - 'A' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a number and promote 'p' up to the first non-digit character.
|
|
|
|
static uptr ParseNumber(const char **p, int base) {
|
|
|
|
uptr n = 0;
|
|
|
|
int d;
|
|
|
|
CHECK(base >= 2 && base <= 16);
|
|
|
|
while ((d = TranslateDigit(**p)) >= 0 && d < base) {
|
|
|
|
n = n * base + d;
|
|
|
|
(*p)++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDecimal(char c) {
|
|
|
|
int d = TranslateDigit(c);
|
|
|
|
return d >= 0 && d < 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr ParseDecimal(const char **p) {
|
|
|
|
return ParseNumber(p, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsHex(char c) {
|
|
|
|
int d = TranslateDigit(c);
|
|
|
|
return d >= 0 && d < 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr ParseHex(const char **p) {
|
|
|
|
return ParseNumber(p, 16);
|
|
|
|
}
|
|
|
|
|
2017-07-25 23:27:32 +08:00
|
|
|
void MemoryMappedSegment::AddAddressRanges(LoadedModule *module) {
|
2017-07-26 01:28:41 +08:00
|
|
|
// data_ should be unused on this platform
|
|
|
|
CHECK(!data_);
|
2017-07-25 23:27:32 +08:00
|
|
|
module->addAddressRange(start, end, IsExecutable(), IsWritable());
|
|
|
|
}
|
|
|
|
|
2014-08-06 18:16:52 +08:00
|
|
|
MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
|
|
|
|
// FIXME: in the future we may want to cache the mappings on demand only.
|
|
|
|
if (cache_enabled)
|
|
|
|
CacheMemoryMappings();
|
2017-11-29 06:15:27 +08:00
|
|
|
|
|
|
|
// Read maps after the cache update to capture the maps/unmaps happening in
|
|
|
|
// the process of updating.
|
|
|
|
ReadProcMaps(&data_.proc_self_maps);
|
|
|
|
if (cache_enabled && data_.proc_self_maps.mmaped_size == 0)
|
|
|
|
LoadFromCache();
|
|
|
|
CHECK_GT(data_.proc_self_maps.mmaped_size, 0);
|
|
|
|
CHECK_GT(data_.proc_self_maps.len, 0);
|
|
|
|
|
|
|
|
Reset();
|
2014-08-06 18:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryMappingLayout::~MemoryMappingLayout() {
|
|
|
|
// Only unmap the buffer if it is different from the cached one. Otherwise
|
|
|
|
// it will be unmapped when the cache is refreshed.
|
2017-11-29 06:15:27 +08:00
|
|
|
if (data_.proc_self_maps.data != cached_proc_self_maps.data)
|
Removed platform-specific ifdefs from sanitizer_procmaps.h
Summary: Removed platform-specific ifdefs for linux, mac, freebsd and netbsd from sanitizer_procmaps.h
Patch by Yicheng Wang <yichengfb@fb.com>
Reviewers: kcc, kubamracek, alekseyshl, fjricci, vitalybuka
Reviewed By: fjricci, vitalybuka
Subscribers: vitalybuka, emaste, krytarowski, llvm-commits
Differential Revision: https://reviews.llvm.org/D38098
llvm-svn: 313999
2017-09-23 01:48:24 +08:00
|
|
|
UnmapOrDie(data_.proc_self_maps.data, data_.proc_self_maps.mmaped_size);
|
2014-08-06 18:16:52 +08:00
|
|
|
}
|
|
|
|
|
2017-11-29 06:15:27 +08:00
|
|
|
void MemoryMappingLayout::Reset() {
|
|
|
|
data_.current = data_.proc_self_maps.data;
|
|
|
|
}
|
2014-08-06 18:16:52 +08:00
|
|
|
|
|
|
|
// static
|
|
|
|
void MemoryMappingLayout::CacheMemoryMappings() {
|
2017-11-29 06:15:27 +08:00
|
|
|
ProcSelfMapsBuff new_proc_self_maps;
|
|
|
|
ReadProcMaps(&new_proc_self_maps);
|
2014-08-06 18:16:52 +08:00
|
|
|
// Don't invalidate the cache if the mappings are unavailable.
|
2017-11-29 06:15:27 +08:00
|
|
|
if (new_proc_self_maps.mmaped_size == 0)
|
|
|
|
return;
|
|
|
|
SpinMutexLock l(&cache_lock);
|
|
|
|
if (cached_proc_self_maps.mmaped_size)
|
|
|
|
UnmapOrDie(cached_proc_self_maps.data, cached_proc_self_maps.mmaped_size);
|
|
|
|
cached_proc_self_maps = new_proc_self_maps;
|
2014-08-06 18:16:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryMappingLayout::LoadFromCache() {
|
Removed platform-specific ifdefs from sanitizer_procmaps.h
Summary: Removed platform-specific ifdefs for linux, mac, freebsd and netbsd from sanitizer_procmaps.h
Patch by Yicheng Wang <yichengfb@fb.com>
Reviewers: kcc, kubamracek, alekseyshl, fjricci, vitalybuka
Reviewed By: fjricci, vitalybuka
Subscribers: vitalybuka, emaste, krytarowski, llvm-commits
Differential Revision: https://reviews.llvm.org/D38098
llvm-svn: 313999
2017-09-23 01:48:24 +08:00
|
|
|
SpinMutexLock l(&cache_lock);
|
2017-11-29 06:15:27 +08:00
|
|
|
if (cached_proc_self_maps.data)
|
Removed platform-specific ifdefs from sanitizer_procmaps.h
Summary: Removed platform-specific ifdefs for linux, mac, freebsd and netbsd from sanitizer_procmaps.h
Patch by Yicheng Wang <yichengfb@fb.com>
Reviewers: kcc, kubamracek, alekseyshl, fjricci, vitalybuka
Reviewed By: fjricci, vitalybuka
Subscribers: vitalybuka, emaste, krytarowski, llvm-commits
Differential Revision: https://reviews.llvm.org/D38098
llvm-svn: 313999
2017-09-23 01:48:24 +08:00
|
|
|
data_.proc_self_maps = cached_proc_self_maps;
|
2014-08-06 18:16:52 +08:00
|
|
|
}
|
|
|
|
|
2016-02-23 02:52:51 +08:00
|
|
|
void MemoryMappingLayout::DumpListOfModules(
|
2017-09-30 04:55:06 +08:00
|
|
|
InternalMmapVectorNoCtor<LoadedModule> *modules) {
|
2014-08-06 18:16:52 +08:00
|
|
|
Reset();
|
2014-12-03 06:20:11 +08:00
|
|
|
InternalScopedString module_name(kMaxPathLength);
|
2017-07-12 02:54:00 +08:00
|
|
|
MemoryMappedSegment segment(module_name.data(), module_name.size());
|
|
|
|
for (uptr i = 0; Next(&segment); i++) {
|
|
|
|
const char *cur_name = segment.filename;
|
2014-08-06 18:16:52 +08:00
|
|
|
if (cur_name[0] == '\0')
|
|
|
|
continue;
|
|
|
|
// Don't subtract 'cur_beg' from the first entry:
|
|
|
|
// * If a binary is compiled w/o -pie, then the first entry in
|
|
|
|
// process maps is likely the binary itself (all dynamic libs
|
|
|
|
// are mapped higher in address space). For such a binary,
|
|
|
|
// instruction offset in binary coincides with the actual
|
|
|
|
// instruction address in virtual memory (as code section
|
|
|
|
// is mapped to a fixed memory range).
|
|
|
|
// * If a binary is compiled with -pie, all the modules are
|
|
|
|
// mapped high at address space (in particular, higher than
|
|
|
|
// shadow memory of the tool), so the module can't be the
|
|
|
|
// first entry.
|
2017-07-12 02:54:00 +08:00
|
|
|
uptr base_address = (i ? segment.start : 0) - segment.offset;
|
2016-02-23 02:52:51 +08:00
|
|
|
LoadedModule cur_module;
|
|
|
|
cur_module.set(cur_name, base_address);
|
2017-07-25 23:27:32 +08:00
|
|
|
segment.AddAddressRanges(&cur_module);
|
2016-02-23 02:52:51 +08:00
|
|
|
modules->push_back(cur_module);
|
2014-08-06 18:16:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {
|
2015-10-01 07:52:54 +08:00
|
|
|
char *smaps = nullptr;
|
2014-08-06 18:16:52 +08:00
|
|
|
uptr smaps_cap = 0;
|
2015-07-18 07:50:08 +08:00
|
|
|
uptr smaps_len = 0;
|
|
|
|
if (!ReadFileToBuffer("/proc/self/smaps", &smaps, &smaps_cap, &smaps_len))
|
|
|
|
return;
|
2014-08-06 18:16:52 +08:00
|
|
|
uptr start = 0;
|
|
|
|
bool file = false;
|
|
|
|
const char *pos = smaps;
|
|
|
|
while (pos < smaps + smaps_len) {
|
|
|
|
if (IsHex(pos[0])) {
|
|
|
|
start = ParseHex(&pos);
|
|
|
|
for (; *pos != '/' && *pos > '\n'; pos++) {}
|
|
|
|
file = *pos == '/';
|
|
|
|
} else if (internal_strncmp(pos, "Rss:", 4) == 0) {
|
|
|
|
while (!IsDecimal(*pos)) pos++;
|
|
|
|
uptr rss = ParseDecimal(&pos) * 1024;
|
|
|
|
cb(start, rss, file, stats, stats_size);
|
|
|
|
}
|
|
|
|
while (*pos++ != '\n') {}
|
|
|
|
}
|
|
|
|
UnmapOrDie(smaps, smaps_cap);
|
|
|
|
}
|
|
|
|
|
2015-10-01 07:52:54 +08:00
|
|
|
} // namespace __sanitizer
|
2014-08-06 18:16:52 +08:00
|
|
|
|
[Sanitizers] Basic sanitizer Solaris support (PR 33274)
Summary:
This is the first mostly working version of the Sanitizer port to 32-bit Solaris/x86.
It is currently based on Solaris 11.4 Beta.
This part was initially developed inside libsanitizer in the GCC tree and should apply to
both. Subsequent parts will address changes to clang, the compiler-rt build system
and testsuite.
I'm not yet sure what the right patch granularity is: if it's profitable to split the patch
up, I'd like to get guidance on how to do so.
Most of the changes are probably straightforward with a few exceptions:
* The Solaris syscall interface isn't stable, undocumented and can change within an
OS release. The stable interface is the libc interface, which I'm using here, if possible
using the internal _-prefixed names.
* While the patch primarily target 32-bit x86, I've left a few sparc changes in. They
cannot currently be used with clang due to a backend limitation, but have worked
fine inside the gcc tree.
* Some functions (e.g. largefile versions of functions like open64) only exist in 32-bit
Solaris, so I've introduced a separate SANITIZER_SOLARIS32 to check for that.
The patch (with the subsequent ones to be submitted shortly) was tested
on i386-pc-solaris2.11. Only a few failures remain, some of them analyzed, some
still TBD:
AddressSanitizer-i386-sunos :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos :: TestCases/malloc-no-intercept.c
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/start-deactivated.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/default_options.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/malloc-no-intercept.c
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/MemoryMappingLayout.DumpListOfModules
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/SanitizerCommon.PthreadDestructorIterations
Maybe this is good enough the get the ball rolling.
Reviewers: kcc, alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, jyknight, kubamracek, krytarowski, fedor.sergeev, llvm-commits, #sanitizers
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D40898
llvm-svn: 320740
2017-12-15 04:14:29 +08:00
|
|
|
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD ||
|
|
|
|
// SANITIZER_SOLARIS
|