Skip to content

Commit

Permalink
Removed platform-specific ifdefs from sanitizer_procmaps.h
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fjricci committed Sep 22, 2017
1 parent a1e7ecc commit fbccb0a
Showing 7 changed files with 132 additions and 132 deletions.
13 changes: 13 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux.h
Original file line number Diff line number Diff line change
@@ -28,6 +28,19 @@ namespace __sanitizer {
// the one in <dirent.h>, which is used by readdir().
struct linux_dirent;

struct ProcSelfMapsBuff {
char *data;
uptr mmaped_size;
uptr len;
};

struct MemoryMappingLayoutData {
ProcSelfMapsBuff proc_self_maps;
const char *current;
};

void ReadProcMaps(ProcSelfMapsBuff *proc_maps);

// Syscall wrappers.
uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count);
uptr internal_sigaltstack(const void* ss, void* oss);
11 changes: 11 additions & 0 deletions compiler-rt/lib/sanitizer_common/sanitizer_mac.h
Original file line number Diff line number Diff line change
@@ -20,6 +20,17 @@

namespace __sanitizer {

struct MemoryMappingLayoutData {
int current_image;
u32 current_magic;
u32 current_filetype;
ModuleArch current_arch;
u8 current_uuid[kModuleUUIDSize];
int current_load_cmd_count;
char *current_load_cmd_addr;
bool current_instrumented;
};

enum MacosVersion {
MACOS_VERSION_UNINITIALIZED = 0,
MACOS_VERSION_UNKNOWN,
32 changes: 3 additions & 29 deletions compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h
Original file line number Diff line number Diff line change
@@ -16,20 +16,12 @@

#include "sanitizer_common.h"
#include "sanitizer_internal_defs.h"
#include "sanitizer_linux.h"
#include "sanitizer_mac.h"
#include "sanitizer_mutex.h"

namespace __sanitizer {

#if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
struct ProcSelfMapsBuff {
char *data;
uptr mmaped_size;
uptr len;
};

// Reads process memory map in an OS-specific way.
void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD

// Memory protection masks.
static const uptr kProtectionRead = 1;
@@ -87,25 +79,7 @@ class MemoryMappingLayout {

// FIXME: Hide implementation details for different platforms in
// platform-specific files.
# if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD
ProcSelfMapsBuff proc_self_maps_;
const char *current_;

// Static mappings cache.
static ProcSelfMapsBuff cached_proc_self_maps_;
static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_.
# elif SANITIZER_MAC
template <u32 kLCSegment, typename SegmentCommand>
bool NextSegmentLoad(MemoryMappedSegment *segment);
int current_image_;
u32 current_magic_;
u32 current_filetype_;
ModuleArch current_arch_;
u8 current_uuid_[kModuleUUIDSize];
int current_load_cmd_count_;
char *current_load_cmd_addr_;
bool current_instrumented_;
# endif
MemoryMappingLayoutData data_;
};

typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
37 changes: 17 additions & 20 deletions compiler-rt/lib/sanitizer_common/sanitizer_procmaps_common.cc
Original file line number Diff line number Diff line change
@@ -20,9 +20,8 @@

namespace __sanitizer {

// Linker initialized.
ProcSelfMapsBuff MemoryMappingLayout::cached_proc_self_maps_;
StaticSpinMutex MemoryMappingLayout::cache_lock_; // Linker initialized.
static ProcSelfMapsBuff cached_proc_self_maps;
static StaticSpinMutex cache_lock;

static int TranslateDigit(char c) {
if (c >= '0' && c <= '9')
@@ -71,14 +70,14 @@ void MemoryMappedSegment::AddAddressRanges(LoadedModule *module) {
}

MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
ReadProcMaps(&proc_self_maps_);
ReadProcMaps(&data_.proc_self_maps);
if (cache_enabled) {
if (proc_self_maps_.mmaped_size == 0) {
if (data_.proc_self_maps.mmaped_size == 0) {
LoadFromCache();
CHECK_GT(proc_self_maps_.len, 0);
CHECK_GT(data_.proc_self_maps.len, 0);
}
} else {
CHECK_GT(proc_self_maps_.mmaped_size, 0);
CHECK_GT(data_.proc_self_maps.mmaped_size, 0);
}
Reset();
// FIXME: in the future we may want to cache the mappings on demand only.
@@ -89,24 +88,22 @@ MemoryMappingLayout::MemoryMappingLayout(bool cache_enabled) {
MemoryMappingLayout::~MemoryMappingLayout() {
// Only unmap the buffer if it is different from the cached one. Otherwise
// it will be unmapped when the cache is refreshed.
if (proc_self_maps_.data != cached_proc_self_maps_.data) {
UnmapOrDie(proc_self_maps_.data, proc_self_maps_.mmaped_size);
if (data_.proc_self_maps.data != cached_proc_self_maps.data) {
UnmapOrDie(data_.proc_self_maps.data, data_.proc_self_maps.mmaped_size);
}
}

void MemoryMappingLayout::Reset() {
current_ = proc_self_maps_.data;
}
void MemoryMappingLayout::Reset() { data_.current = data_.proc_self_maps.data; }

// static
void MemoryMappingLayout::CacheMemoryMappings() {
SpinMutexLock l(&cache_lock_);
SpinMutexLock l(&cache_lock);
// Don't invalidate the cache if the mappings are unavailable.
ProcSelfMapsBuff old_proc_self_maps;
old_proc_self_maps = cached_proc_self_maps_;
ReadProcMaps(&cached_proc_self_maps_);
if (cached_proc_self_maps_.mmaped_size == 0) {
cached_proc_self_maps_ = old_proc_self_maps;
old_proc_self_maps = cached_proc_self_maps;
ReadProcMaps(&cached_proc_self_maps);
if (cached_proc_self_maps.mmaped_size == 0) {
cached_proc_self_maps = old_proc_self_maps;
} else {
if (old_proc_self_maps.mmaped_size) {
UnmapOrDie(old_proc_self_maps.data,
@@ -116,9 +113,9 @@ void MemoryMappingLayout::CacheMemoryMappings() {
}

void MemoryMappingLayout::LoadFromCache() {
SpinMutexLock l(&cache_lock_);
if (cached_proc_self_maps_.data) {
proc_self_maps_ = cached_proc_self_maps_;
SpinMutexLock l(&cache_lock);
if (cached_proc_self_maps.data) {
data_.proc_self_maps = cached_proc_self_maps;
}
}

10 changes: 5 additions & 5 deletions compiler-rt/lib/sanitizer_common/sanitizer_procmaps_freebsd.cc
Original file line number Diff line number Diff line change
@@ -67,9 +67,9 @@ void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
}

bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
char *last = proc_self_maps_.data + proc_self_maps_.len;
if (current_ >= last) return false;
struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry*)current_;
char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
if (data_.current >= last) return false;
struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry *)data_.current;

segment->start = (uptr)VmEntry->kve_start;
segment->end = (uptr)VmEntry->kve_end;
@@ -90,9 +90,9 @@ bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
}

#if SANITIZER_FREEBSD
current_ += VmEntry->kve_structsize;
data_.current += VmEntry->kve_structsize;
#else
current_ += sizeof(*VmEntry);
data_.current += sizeof(*VmEntry);
#endif

return true;
60 changes: 30 additions & 30 deletions compiler-rt/lib/sanitizer_common/sanitizer_procmaps_linux.cc
Original file line number Diff line number Diff line change
@@ -27,48 +27,48 @@ static bool IsOneOf(char c, char c1, char c2) {
}

bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
char *last = proc_self_maps_.data + proc_self_maps_.len;
if (current_ >= last) return false;
char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
if (data_.current >= last) return false;
char *next_line =
(char *)internal_memchr(data_.current, '\n', last - data_.current);
if (next_line == 0)
next_line = last;
// Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
segment->start = ParseHex(&current_);
CHECK_EQ(*current_++, '-');
segment->end = ParseHex(&current_);
CHECK_EQ(*current_++, ' ');
CHECK(IsOneOf(*current_, '-', 'r'));
segment->start = ParseHex(&data_.current);
CHECK_EQ(*data_.current++, '-');
segment->end = ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ' ');
CHECK(IsOneOf(*data_.current, '-', 'r'));
segment->protection = 0;
if (*current_++ == 'r') segment->protection |= kProtectionRead;
CHECK(IsOneOf(*current_, '-', 'w'));
if (*current_++ == 'w') segment->protection |= kProtectionWrite;
CHECK(IsOneOf(*current_, '-', 'x'));
if (*current_++ == 'x') segment->protection |= kProtectionExecute;
CHECK(IsOneOf(*current_, 's', 'p'));
if (*current_++ == 's') segment->protection |= kProtectionShared;
CHECK_EQ(*current_++, ' ');
segment->offset = ParseHex(&current_);
CHECK_EQ(*current_++, ' ');
ParseHex(&current_);
CHECK_EQ(*current_++, ':');
ParseHex(&current_);
CHECK_EQ(*current_++, ' ');
while (IsDecimal(*current_))
current_++;
if (*data_.current++ == 'r') segment->protection |= kProtectionRead;
CHECK(IsOneOf(*data_.current, '-', 'w'));
if (*data_.current++ == 'w') segment->protection |= kProtectionWrite;
CHECK(IsOneOf(*data_.current, '-', 'x'));
if (*data_.current++ == 'x') segment->protection |= kProtectionExecute;
CHECK(IsOneOf(*data_.current, 's', 'p'));
if (*data_.current++ == 's') segment->protection |= kProtectionShared;
CHECK_EQ(*data_.current++, ' ');
segment->offset = ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ' ');
ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ':');
ParseHex(&data_.current);
CHECK_EQ(*data_.current++, ' ');
while (IsDecimal(*data_.current)) data_.current++;
// Qemu may lack the trailing space.
// https://github.com/google/sanitizers/issues/160
// CHECK_EQ(*current_++, ' ');
// CHECK_EQ(*data_.current++, ' ');
// Skip spaces.
while (current_ < next_line && *current_ == ' ')
current_++;
while (data_.current < next_line && *data_.current == ' ') data_.current++;
// Fill in the filename.
if (segment->filename) {
uptr len = Min((uptr)(next_line - current_), segment->filename_size - 1);
internal_strncpy(segment->filename, current_, len);
uptr len =
Min((uptr)(next_line - data_.current), segment->filename_size - 1);
internal_strncpy(segment->filename, data_.current, len);
segment->filename[len] = 0;
}

current_ = next_line + 1;
data_.current = next_line + 1;
return true;
}

Loading

0 comments on commit fbccb0a

Please sign in to comment.