From 2c5fc3bb111c291cc21b27b2068db4f035727a08 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Mon, 4 Jun 2012 14:27:50 +0000 Subject: [PATCH] Created files sanitizer_linux.cc and sanitizer_mac.cc for platform-specific implementations of common functions. Turned asan_mmap into __sanitizer::internal_mmap. llvm-svn: 157930 --- compiler-rt/lib/asan/asan_linux.cc | 34 +++++++---------- compiler-rt/lib/asan/asan_mac.cc | 38 +++++++++---------- .../lib/sanitizer_common/sanitizer_libc.h | 5 +++ .../lib/sanitizer_common/sanitizer_linux.cc | 37 ++++++++++++++++++ .../lib/sanitizer_common/sanitizer_mac.cc | 31 +++++++++++++++ 5 files changed, 105 insertions(+), 40 deletions(-) create mode 100644 compiler-rt/lib/sanitizer_common/sanitizer_linux.cc create mode 100644 compiler-rt/lib/sanitizer_common/sanitizer_mac.cc diff --git a/compiler-rt/lib/asan/asan_linux.cc b/compiler-rt/lib/asan/asan_linux.cc index 040b8b711f76..1577d2f5214b 100644 --- a/compiler-rt/lib/asan/asan_linux.cc +++ b/compiler-rt/lib/asan/asan_linux.cc @@ -18,6 +18,7 @@ #include "asan_lock.h" #include "asan_procmaps.h" #include "asan_thread.h" +#include "sanitizer_common/sanitizer_libc.h" #include #include @@ -35,6 +36,8 @@ #include #endif +using namespace __sanitizer; // NOLINT + extern "C" void* _DYNAMIC; namespace __asan { @@ -73,20 +76,11 @@ bool AsanInterceptsSignal(int signum) { return signum == SIGSEGV && FLAG_handle_segv; } -static void *asan_mmap(void *addr, uptr length, int prot, int flags, - int fd, u64 offset) { -# if __WORDSIZE == 64 - return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); -# else - return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); -# endif -} - void *AsanMmapSomewhereOrDie(uptr size, const char *mem_type) { size = RoundUpTo(size, kPageSize); - void *res = asan_mmap(0, size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, -1, 0); + void *res = internal_mmap(0, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, -1, 0); if (res == (void*)-1) { OutOfMemoryMessageAndDie(mem_type, size); } @@ -94,17 +88,17 @@ void *AsanMmapSomewhereOrDie(uptr size, const char *mem_type) { } void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size) { - return asan_mmap((void*)fixed_addr, size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, - 0, 0); + return internal_mmap((void*)fixed_addr, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, + 0, 0); } void *AsanMprotect(uptr fixed_addr, uptr size) { - return asan_mmap((void*)fixed_addr, size, - PROT_NONE, - MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, - 0, 0); + return internal_mmap((void*)fixed_addr, size, + PROT_NONE, + MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, + 0, 0); } void AsanUnmapOrDie(void *addr, uptr size) { diff --git a/compiler-rt/lib/asan/asan_mac.cc b/compiler-rt/lib/asan/asan_mac.cc index 21e97954d2e7..cd87a2d311a1 100644 --- a/compiler-rt/lib/asan/asan_mac.cc +++ b/compiler-rt/lib/asan/asan_mac.cc @@ -21,6 +21,7 @@ #include "asan_stack.h" #include "asan_thread.h" #include "asan_thread_registry.h" +#include "sanitizer_common/sanitizer_libc.h" #include // for _NSGetEnviron #include @@ -35,6 +36,8 @@ #include #include +using namespace __sanitizer; // NOLINT + namespace __asan { void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) { @@ -97,20 +100,15 @@ bool AsanInterceptsSignal(int signum) { return (signum == SIGSEGV || signum == SIGBUS) && FLAG_handle_segv; } -static void *asan_mmap(void *addr, size_t length, int prot, int flags, - int fd, u64 offset) { - return mmap(addr, length, prot, flags, fd, offset); -} - size_t AsanWrite(int fd, const void *buf, size_t count) { return write(fd, buf, count); } void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) { size = RoundUpTo(size, kPageSize); - void *res = asan_mmap(0, size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, -1, 0); + void *res = internal_mmap(0, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, -1, 0); if (res == (void*)-1) { OutOfMemoryMessageAndDie(mem_type, size); } @@ -118,17 +116,17 @@ void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) { } void *AsanMmapFixedNoReserve(uptr fixed_addr, size_t size) { - return asan_mmap((void*)fixed_addr, size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, - 0, 0); + return internal_mmap((void*)fixed_addr, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, + 0, 0); } void *AsanMprotect(uptr fixed_addr, size_t size) { - return asan_mmap((void*)fixed_addr, size, - PROT_NONE, - MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, - 0, 0); + return internal_mmap((void*)fixed_addr, size, + PROT_NONE, + MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, + 0, 0); } void AsanUnmapOrDie(void *addr, size_t size) { @@ -351,10 +349,10 @@ mach_error_t __interception_allocate_island(void **ptr, void *unused_hint) { if (!island_allocator_pos) { island_allocator_pos = - asan_mmap((void*)kIslandBeg, kIslandEnd - kIslandBeg, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_PRIVATE | MAP_ANON | MAP_FIXED, - -1, 0); + internal_mmap((void*)kIslandBeg, kIslandEnd - kIslandBeg, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANON | MAP_FIXED, + -1, 0); if (island_allocator_pos != (void*)kIslandBeg) { return KERN_NO_SPACE; } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h index 65e0305eee47..fd24b65e506f 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h @@ -29,6 +29,11 @@ void MiniLibcStub(); int internal_strcmp(const char *s1, const char *s2); char *internal_strncpy(char *dst, const char *src, uptr n); +#ifndef _WIN32 +void *internal_mmap(void *addr, uptr length, int prot, int flags, + int fd, u64 offset); +#endif // _WIN32 + } // namespace __sanitizer #endif // SANITIZER_LIBC_H diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc new file mode 100644 index 000000000000..f105ff040cbc --- /dev/null +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc @@ -0,0 +1,37 @@ +//===-- sanitizer_linux.cc ------------------------------------------------===// +// +// 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 +// run-time libraries and implements linux-specific functions from +// sanitizer_libc.h. +//===----------------------------------------------------------------------===// +#ifdef __linux__ + +#include "sanitizer_defs.h" +#include "sanitizer_libc.h" + +#include +#include +#include +#include + +namespace __sanitizer { + +void *internal_mmap(void *addr, uptr length, int prot, int flags, + int fd, u64 offset) { +#if __WORDSIZE == 64 + return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); +#else + return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); +#endif +} + +} // namespace __sanitizer + +#endif // __linux__ diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc new file mode 100644 index 000000000000..432abed1ecfd --- /dev/null +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc @@ -0,0 +1,31 @@ +//===-- sanitizer_mac.cc --------------------------------------------------===// +// +// 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 +// run-time libraries and implements mac-specific functions from +// sanitizer_libc.h. +//===----------------------------------------------------------------------===// + +#ifdef __APPLE__ + +#include "sanitizer_defs.h" +#include "sanitizer_libc.h" + +#include + +namespace __sanitizer { + +void *internal_mmap(void *addr, size_t length, int prot, int flags, + int fd, u64 offset) { + return mmap(addr, length, prot, flags, fd, offset); +} + +} // namespace __sanitizer + +#endif // __APPLE__