[llvm-commits] [compiler-rt] r158056 - in /compiler-rt/trunk/lib: asan/ sanitizer_common/ tsan/rtl/

Alexey Samsonov samsonov at google.com
Wed Jun 6 02:26:25 PDT 2012


Author: samsonov
Date: Wed Jun  6 04:26:25 2012
New Revision: 158056

URL: http://llvm.org/viewvc/llvm-project?rev=158056&view=rev
Log:
[Sanitizer] Move more functions/constants to sanitizer_common.

Added:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
Modified:
    compiler-rt/trunk/lib/asan/asan_interceptors.cc
    compiler-rt/trunk/lib/asan/asan_interceptors.h
    compiler-rt/trunk/lib/asan/asan_internal.h
    compiler-rt/trunk/lib/asan/asan_posix.cc
    compiler-rt/trunk/lib/asan/asan_win.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_defs.h
    compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_platform.h
    compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc

Modified: compiler-rt/trunk/lib/asan/asan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_interceptors.cc Wed Jun  6 04:26:25 2012
@@ -220,12 +220,6 @@
   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) {

Modified: compiler-rt/trunk/lib/asan/asan_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.h?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors.h (original)
+++ compiler-rt/trunk/lib/asan/asan_interceptors.h Wed Jun  6 04:26:25 2012
@@ -32,7 +32,6 @@
 
 // __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);

Modified: compiler-rt/trunk/lib/asan/asan_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_internal.h?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_internal.h Wed Jun  6 04:26:25 2012
@@ -141,7 +141,6 @@
 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 @@
 
 #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 @@
 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 {

Modified: compiler-rt/trunk/lib/asan/asan_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_posix.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_posix.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_posix.cc Wed Jun  6 04:26:25 2012
@@ -154,10 +154,6 @@
   Report("End of process memory map.\n");
 }
 
-int GetPid() {
-  return getpid();
-}
-
 uptr GetThreadSelf() {
   return (uptr)pthread_self();
 }

Modified: compiler-rt/trunk/lib/asan/asan_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_win.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_win.cc Wed Jun  6 04:26:25 2012
@@ -239,10 +239,6 @@
   UNIMPLEMENTED();
 }
 
-int GetPid() {
-  return GetProcessId(GetCurrentProcess());
-}
-
 uptr GetThreadSelf() {
   return GetCurrentThreadId();
 }

Added: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=158056&view=auto
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc (added)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc Wed Jun  6 04:26:25 2012
@@ -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

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Wed Jun  6 04:26:25 2012
@@ -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
 

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h Wed Jun  6 04:26:25 2012
@@ -59,4 +59,14 @@
 #  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

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.cc Wed Jun  6 04:26:25 2012
@@ -38,6 +38,12 @@
   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++)

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h Wed Jun  6 04:26:25 2012
@@ -26,6 +26,7 @@
 // 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

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Wed Jun  6 04:26:25 2012
@@ -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);

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Wed Jun  6 04:26:25 2012
@@ -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();

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_defs.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_defs.h?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_defs.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_defs.h Wed Jun  6 04:26:25 2012
@@ -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 @@
 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);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_flags.cc Wed Jun  6 04:26:25 2012
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "sanitizer_common/sanitizer_libc.h"
 #include "tsan_flags.h"
 #include "tsan_rtl.h"
 #include "tsan_mman.h"

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_interceptors.cc Wed Jun  6 04:26:25 2012
@@ -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 @@
   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);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_platform.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_platform.h?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_platform.h (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_platform.h Wed Jun  6 04:26:25 2012
@@ -69,7 +69,6 @@
 
 const char *InitializePlatform();
 void FinalizePlatform();
-int GetPid();
 
 void internal_yield();
 void internal_sleep_ms(u32 ms);

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_platform_linux.cc Wed Jun  6 04:26:25 2012
@@ -273,8 +273,4 @@
   }
 }
 
-int GetPid() {
-  return getpid();
-}
-
 }  // namespace __tsan

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc?rev=158056&r1=158055&r2=158056&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_symbolize_addr2line_linux.cc Wed Jun  6 04:26:25 2012
@@ -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 @@
   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;





More information about the llvm-commits mailing list