diff options
| author | Alexey Samsonov <samsonov@google.com> | 2012-06-06 09:26:25 +0000 |
|---|---|---|
| committer | Alexey Samsonov <samsonov@google.com> | 2012-06-06 09:26:25 +0000 |
| commit | ee072906285faf0528dc36df64411a95b680437b (patch) | |
| tree | b33be0f5de7d417ae567332c02411b3e47c9883b | |
| parent | f1ef87ddbbc1a62111adcc7ef31f4a28892ebcb9 (diff) | |
| download | bcm5719-llvm-ee072906285faf0528dc36df64411a95b680437b.tar.gz bcm5719-llvm-ee072906285faf0528dc36df64411a95b680437b.zip | |
[Sanitizer] Move more functions/constants to sanitizer_common.
llvm-svn: 158056
18 files changed, 119 insertions, 46 deletions
diff --git a/compiler-rt/lib/asan/asan_interceptors.cc b/compiler-rt/lib/asan/asan_interceptors.cc index 7797f99aee4..37a0c065164 100644 --- a/compiler-rt/lib/asan/asan_interceptors.cc +++ b/compiler-rt/lib/asan/asan_interceptors.cc @@ -220,12 +220,6 @@ s64 internal_atoll(const char *nptr) { return internal_simple_strtoll(nptr, (char**)0, 10); } -uptr internal_strlen(const char *s) { - uptr i = 0; - while (s[i]) i++; - return i; -} - uptr internal_strnlen(const char *s, uptr maxlen) { #if ASAN_INTERCEPT_STRNLEN if (REAL(strnlen) != 0) { diff --git a/compiler-rt/lib/asan/asan_interceptors.h b/compiler-rt/lib/asan/asan_interceptors.h index 975187f53e4..e0dab6a07bf 100644 --- a/compiler-rt/lib/asan/asan_interceptors.h +++ b/compiler-rt/lib/asan/asan_interceptors.h @@ -32,7 +32,6 @@ namespace __asan { // __asan::internal_X() is the implementation of X() for use in RTL. s64 internal_atoll(const char *nptr); -uptr internal_strlen(const char *s); uptr internal_strnlen(const char *s, uptr maxlen); char* internal_strchr(const char *s, int c); void* internal_memset(void *s, int c, uptr n); diff --git a/compiler-rt/lib/asan/asan_internal.h b/compiler-rt/lib/asan/asan_internal.h index 0d1d208bfd5..d234a78757a 100644 --- a/compiler-rt/lib/asan/asan_internal.h +++ b/compiler-rt/lib/asan/asan_internal.h @@ -141,7 +141,6 @@ bool AsanInterceptsSignal(int signum); void SetAlternateSignalStack(); void UnsetAlternateSignalStack(); void InstallSignalHandlers(); -int GetPid(); uptr GetThreadSelf(); int AtomicInc(int *a); u16 AtomicExchange(u16 *a, u16 new_val); @@ -242,11 +241,6 @@ int Atexit(void (*function)(void)); #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) -const uptr kWordSize = __WORDSIZE / 8; -const uptr kWordSizeInBits = 8 * kWordSize; -const uptr kPageSizeBits = 12; -const uptr kPageSize = 1UL << kPageSizeBits; - #if !defined(_WIN32) || defined(__clang__) # define GET_CALLER_PC() (uptr)__builtin_return_address(0) # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) @@ -291,16 +285,6 @@ const int kAsanInternalHeapMagic = 0xfe; static const uptr kCurrentStackFrameMagic = 0x41B58AB3; static const uptr kRetiredStackFrameMagic = 0x45E0360E; -// --------------------------- Bit twiddling ------- {{{1 -inline bool IsPowerOfTwo(uptr x) { - return (x & (x - 1)) == 0; -} - -inline uptr RoundUpTo(uptr size, uptr boundary) { - CHECK(IsPowerOfTwo(boundary)); - return (size + boundary - 1) & ~(boundary - 1); -} - // -------------------------- LowLevelAllocator ----- {{{1 // A simple low-level memory allocator for internal use. class LowLevelAllocator { diff --git a/compiler-rt/lib/asan/asan_posix.cc b/compiler-rt/lib/asan/asan_posix.cc index 762c340e631..48de79395c5 100644 --- a/compiler-rt/lib/asan/asan_posix.cc +++ b/compiler-rt/lib/asan/asan_posix.cc @@ -154,10 +154,6 @@ void AsanDumpProcessMap() { Report("End of process memory map.\n"); } -int GetPid() { - return getpid(); -} - uptr GetThreadSelf() { return (uptr)pthread_self(); } diff --git a/compiler-rt/lib/asan/asan_win.cc b/compiler-rt/lib/asan/asan_win.cc index 001f4719d3a..609f3d4322f 100644 --- a/compiler-rt/lib/asan/asan_win.cc +++ b/compiler-rt/lib/asan/asan_win.cc @@ -239,10 +239,6 @@ void AsanDumpProcessMap() { UNIMPLEMENTED(); } -int GetPid() { - return GetProcessId(GetCurrentProcess()); -} - uptr GetThreadSelf() { return GetCurrentThreadId(); } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc new file mode 100644 index 00000000000..d8878e1e331 --- /dev/null +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc @@ -0,0 +1,28 @@ +//===-- sanitizer_common.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. +//===----------------------------------------------------------------------===// + +#include "sanitizer_common.h" +#include "sanitizer_libc.h" + +namespace __sanitizer { + +void RawWrite(const char *buffer) { + static const char *kRawWriteError = "RawWrite can't output requested buffer!"; + uptr length = (uptr)internal_strlen(buffer); + if (length != internal_write(2, buffer, length)) { + internal_write(2, kRawWriteError, internal_strlen(kRawWriteError)); + Die(); + } +} + +} // namespace __sanitizer diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 8e18d3f8867..8c93d69ad0d 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -9,7 +9,7 @@ // // This file is shared between AddressSanitizer and ThreadSanitizer // run-time libraries. -// It defines common functions and classes that are used in both runtimes. +// It declares common functions and classes that are used in both runtimes. // Implementation of some functions are provided in sanitizer_common, while // others must be defined by run-time library itself. //===----------------------------------------------------------------------===// @@ -20,8 +20,30 @@ namespace __sanitizer { -// NOTE: Functions below must be defined in each run-time. +// NOTE: Functions below must be defined in each run-time. {{{ void NORETURN Die(); +// }}} + +// Constants. +const uptr kWordSize = __WORDSIZE / 8; +const uptr kWordSizeInBits = 8 * kWordSize; +const uptr kPageSizeBits = 12; +const uptr kPageSize = 1UL << kPageSizeBits; + +int GetPid(); +void RawWrite(const char *buffer); +void *MmapOrDie(uptr size); +void UnmapOrDie(void *addr, uptr size); + +// Bit twiddling. +inline bool IsPowerOfTwo(uptr x) { + return (x & (x - 1)) == 0; +} +inline uptr RoundUpTo(uptr size, uptr boundary) { + // FIXME: Use CHECK here. + RAW_CHECK(IsPowerOfTwo(boundary)); + return (size + boundary - 1) & ~(boundary - 1); +} } // namespace __sanitizer diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h index ed6ef6364f4..f29045f381a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h @@ -59,4 +59,14 @@ typedef unsigned long DWORD; // NOLINT # endif #endif // __WORDSIZE +// Raw check. +#define RAW_CHECK_MSG(expr, msg) do { \ + if (!(expr)) { \ + RawWrite(msg); \ + Die(); \ + } \ +} while (0) + +#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) + #endif // SANITIZER_DEFS_H diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.cc b/compiler-rt/lib/sanitizer_common/sanitizer_libc.cc index fe91c984478..4191cda42eb 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.cc @@ -38,6 +38,12 @@ int internal_strcmp(const char *s1, const char *s2) { return 0; } +uptr internal_strlen(const char *s) { + uptr i = 0; + while (s[i]) i++; + return i; +} + char *internal_strncpy(char *dst, const char *src, uptr n) { uptr i; for (i = 0; i < n && src[i]; i++) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h index 8472d0b82a7..e3813ccca9e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h @@ -26,6 +26,7 @@ void MiniLibcStub(); // String functions void *internal_memchr(const void *s, int c, uptr n); int internal_strcmp(const char *s1, const char *s2); +uptr internal_strlen(const char *s); char *internal_strncpy(char *dst, const char *src, uptr n); // Memory diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc index ca5c6fc1d95..1bc08a4bbab 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc @@ -13,14 +13,42 @@ //===----------------------------------------------------------------------===// #if defined(__linux__) || defined(__APPLE__) -#include "sanitizer_internal_defs.h" +#include "sanitizer_common.h" #include "sanitizer_libc.h" #include <stdarg.h> #include <stdio.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <unistd.h> namespace __sanitizer { +int GetPid() { + return getpid(); +} + +void *MmapOrDie(uptr size) { + size = RoundUpTo(size, kPageSize); + void *res = internal_mmap(0, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, -1, 0); + if (res == (void*)-1) { + RawWrite("Failed to map!\n"); + Die(); + } + return res; +} + +void UnmapOrDie(void *addr, uptr size) { + if (!addr || !size) return; + int res = internal_munmap(addr, size); + if (res != 0) { + RawWrite("Failed to unmap!\n"); + Die(); + } +} + int internal_sscanf(const char *str, const char *format, ...) { va_list args; va_start(args, format); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc index 39459078774..4ad50ab3b7a 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc @@ -23,6 +23,23 @@ namespace __sanitizer { +int GetPid() { + return GetProcessId(GetCurrentProcess()); +} + +void *MmapOrDie(uptr size) { + void *rv = VirtualAlloc(0, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + if (rv == 0) + RawWrite("Failed to map!\n"); + Die(); + return rv; +} + +void UnmapOrDie(void *addr, uptr size) { + // FIXME: Use CHECK here. + RAW_CHECK(VirtualFree(addr, size, MEM_DECOMMIT)); +} + void *internal_mmap(void *addr, uptr length, int prot, int flags, int fd, u64 offset) { UNIMPLEMENTED_WIN(); diff --git a/compiler-rt/lib/tsan/rtl/tsan_defs.h b/compiler-rt/lib/tsan/rtl/tsan_defs.h index e761c787e61..d3c748bd57c 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_defs.h +++ b/compiler-rt/lib/tsan/rtl/tsan_defs.h @@ -23,7 +23,6 @@ namespace __tsan { -const uptr kPageSize = 4096; const int kTidBits = 13; const unsigned kMaxTid = 1 << kTidBits; const unsigned kMaxTidInClock = kMaxTid * 2; // This includes msb 'freed' bit. @@ -159,7 +158,6 @@ void internal_memcpy(void *dst, const void *src, uptr size); int internal_memcmp(const void *s1, const void *s2, uptr size); int internal_strncmp(const char *s1, const char *s2, uptr size); void internal_strcpy(char *s1, const char *s2); -uptr internal_strlen(const char *s); char* internal_strdup(const char *s); const char *internal_strstr(const char *where, const char *what); const char *internal_strchr(const char *where, char what); diff --git a/compiler-rt/lib/tsan/rtl/tsan_flags.cc b/compiler-rt/lib/tsan/rtl/tsan_flags.cc index c805e12542a..7224f4b6723 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_flags.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_flags.cc @@ -11,6 +11,7 @@ // //===----------------------------------------------------------------------===// +#include "sanitizer_common/sanitizer_libc.h" #include "tsan_flags.h" #include "tsan_rtl.h" #include "tsan_mman.h" diff --git a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc index 3cc6f14d9af..58c1d616a3d 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "interception/interception.h" +#include "sanitizer_common/sanitizer_libc.h" #include "tsan_rtl.h" #include "tsan_interface.h" #include "tsan_atomic.h" @@ -1540,10 +1541,6 @@ void internal_strcpy(char *s1, const char *s2) { REAL(strcpy)(s1, s2); // NOLINT } -uptr internal_strlen(const char *s) { - return REAL(strlen)(s); -} - char* internal_strdup(const char *s) { uptr len = internal_strlen(s); char *s2 = (char*)internal_alloc(MBlockString, len + 1); diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform.h b/compiler-rt/lib/tsan/rtl/tsan_platform.h index 94dc1ccb4b4..f82d846f96c 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_platform.h +++ b/compiler-rt/lib/tsan/rtl/tsan_platform.h @@ -69,7 +69,6 @@ void FlushShadowMemory(); const char *InitializePlatform(); void FinalizePlatform(); -int GetPid(); void internal_yield(); void internal_sleep_ms(u32 ms); diff --git a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cc b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cc index a38eb88df39..2d744433696 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_platform_linux.cc @@ -273,8 +273,4 @@ void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size, } } -int GetPid() { - return getpid(); -} - } // namespace __tsan diff --git a/compiler-rt/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc b/compiler-rt/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc index 79b6cf6d53f..f7366423730 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc +++ b/compiler-rt/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc @@ -10,6 +10,7 @@ // This file is a part of ThreadSanitizer (TSan), a race detector. // //===----------------------------------------------------------------------===// +#include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_libc.h" #include "tsan_symbolize.h" #include "tsan_mman.h" @@ -84,7 +85,7 @@ static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) { DlIteratePhdrCtx *ctx = (DlIteratePhdrCtx*)arg; InternalScopedBuf<char> tmp(128); if (ctx->is_first) { - Snprintf(tmp.Ptr(), tmp.Size(), "/proc/%d/exe", (int)getpid()); + Snprintf(tmp.Ptr(), tmp.Size(), "/proc/%d/exe", GetPid()); info->dlpi_name = tmp.Ptr(); } ctx->is_first = false; |

