2012-06-07 14:15:12 +08:00
|
|
|
//===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer.
|
|
|
|
//
|
|
|
|
// Information about the process mappings.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_PROCMAPS_H
|
|
|
|
#define SANITIZER_PROCMAPS_H
|
|
|
|
|
|
|
|
#include "sanitizer_internal_defs.h"
|
2012-12-01 10:39:45 +08:00
|
|
|
#include "sanitizer_mutex.h"
|
2012-06-07 14:15:12 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2013-03-19 22:54:17 +08:00
|
|
|
#if SANITIZER_WINDOWS
|
2012-08-27 21:48:48 +08:00
|
|
|
class MemoryMappingLayout {
|
2012-08-27 22:08:53 +08:00
|
|
|
public:
|
2013-03-28 01:57:58 +08:00
|
|
|
explicit MemoryMappingLayout(bool cache_enabled) {
|
2013-03-26 21:02:11 +08:00
|
|
|
(void)cache_enabled;
|
|
|
|
}
|
2012-08-28 15:22:24 +08:00
|
|
|
bool GetObjectNameAndOffset(uptr addr, uptr *offset,
|
2013-03-13 15:39:25 +08:00
|
|
|
char filename[], uptr filename_size,
|
|
|
|
uptr *protection) {
|
2012-08-28 15:22:24 +08:00
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
2012-08-27 21:48:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#else // _WIN32
|
2013-03-19 22:33:38 +08:00
|
|
|
#if SANITIZER_LINUX
|
2012-12-04 05:21:22 +08:00
|
|
|
struct ProcSelfMapsBuff {
|
|
|
|
char *data;
|
|
|
|
uptr mmaped_size;
|
|
|
|
uptr len;
|
|
|
|
};
|
2013-03-19 22:33:38 +08:00
|
|
|
#endif // SANITIZER_LINUX
|
2012-12-04 05:21:22 +08:00
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
class MemoryMappingLayout {
|
2012-06-07 14:15:12 +08:00
|
|
|
public:
|
2013-03-26 18:34:37 +08:00
|
|
|
explicit MemoryMappingLayout(bool cache_enabled);
|
2012-06-07 14:15:12 +08:00
|
|
|
bool Next(uptr *start, uptr *end, uptr *offset,
|
2013-03-13 14:51:02 +08:00
|
|
|
char filename[], uptr filename_size, uptr *protection);
|
2012-06-07 14:15:12 +08:00
|
|
|
void Reset();
|
|
|
|
// Gets the object file name and the offset in that object for a given
|
|
|
|
// address 'addr'. Returns true on success.
|
|
|
|
bool GetObjectNameAndOffset(uptr addr, uptr *offset,
|
2013-03-13 14:51:02 +08:00
|
|
|
char filename[], uptr filename_size,
|
|
|
|
uptr *protection);
|
2012-12-01 10:39:45 +08:00
|
|
|
// In some cases, e.g. when running under a sandbox on Linux, ASan is unable
|
|
|
|
// to obtain the memory mappings. It should fall back to pre-cached data
|
|
|
|
// instead of aborting.
|
|
|
|
static void CacheMemoryMappings();
|
2012-08-27 21:48:48 +08:00
|
|
|
~MemoryMappingLayout();
|
2012-06-20 23:19:17 +08:00
|
|
|
|
2013-03-13 14:51:02 +08:00
|
|
|
// Memory protection masks.
|
|
|
|
static const uptr kProtectionRead = 1;
|
|
|
|
static const uptr kProtectionWrite = 2;
|
|
|
|
static const uptr kProtectionExecute = 4;
|
|
|
|
static const uptr kProtectionShared = 8;
|
|
|
|
|
2012-06-07 14:15:12 +08:00
|
|
|
private:
|
2012-12-01 10:39:45 +08:00
|
|
|
void LoadFromCache();
|
2012-06-07 14:15:12 +08:00
|
|
|
// Default implementation of GetObjectNameAndOffset.
|
|
|
|
// Quite slow, because it iterates through the whole process map for each
|
|
|
|
// lookup.
|
|
|
|
bool IterateForObjectNameAndOffset(uptr addr, uptr *offset,
|
2013-03-13 14:51:02 +08:00
|
|
|
char filename[], uptr filename_size,
|
|
|
|
uptr *protection) {
|
2012-06-07 14:15:12 +08:00
|
|
|
Reset();
|
|
|
|
uptr start, end, file_offset;
|
2013-03-13 14:51:02 +08:00
|
|
|
for (int i = 0; Next(&start, &end, &file_offset, filename, filename_size,
|
|
|
|
protection);
|
2012-06-07 14:15:12 +08:00
|
|
|
i++) {
|
|
|
|
if (addr >= start && addr < end) {
|
2012-07-03 16:24:14 +08:00
|
|
|
// Don't subtract 'start' for 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.
|
2012-06-07 14:15:12 +08:00
|
|
|
*offset = (addr - (i ? start : 0)) + file_offset;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (filename_size)
|
|
|
|
filename[0] = '\0';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-03 15:24:35 +08:00
|
|
|
# if SANITIZER_LINUX
|
2012-12-04 05:21:22 +08:00
|
|
|
ProcSelfMapsBuff proc_self_maps_;
|
2012-06-07 14:15:12 +08:00
|
|
|
char *current_;
|
2012-12-01 10:39:45 +08:00
|
|
|
|
|
|
|
// Static mappings cache.
|
2012-12-04 05:21:22 +08:00
|
|
|
static ProcSelfMapsBuff cached_proc_self_maps_;
|
|
|
|
static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_.
|
2013-04-03 15:24:35 +08:00
|
|
|
# elif SANITIZER_MAC
|
2012-06-07 14:15:12 +08:00
|
|
|
template<u32 kLCSegment, typename SegmentCommand>
|
|
|
|
bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
|
2013-03-13 15:39:25 +08:00
|
|
|
char filename[], uptr filename_size,
|
|
|
|
uptr *protection);
|
2012-06-07 14:15:12 +08:00
|
|
|
int current_image_;
|
|
|
|
u32 current_magic_;
|
2012-10-02 23:42:24 +08:00
|
|
|
u32 current_filetype_;
|
2012-06-07 14:15:12 +08:00
|
|
|
int current_load_cmd_count_;
|
|
|
|
char *current_load_cmd_addr_;
|
2012-08-28 15:22:24 +08:00
|
|
|
# endif
|
2012-06-07 14:15:12 +08:00
|
|
|
};
|
|
|
|
|
2012-08-27 21:48:48 +08:00
|
|
|
#endif // _WIN32
|
|
|
|
|
2012-06-07 14:15:12 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
#endif // SANITIZER_PROCMAPS_H
|