[Sanitizer] Use common defines for ASan and TSan runtime. Split defines between interface defines (can be visible in user code that includes interface ASan/TSan headers) and internal defines.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@157998 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_interface.h b/lib/asan/asan_interface.h
index 9dc507c..167354e 100644
--- a/lib/asan/asan_interface.h
+++ b/lib/asan/asan_interface.h
@@ -15,20 +15,22 @@
#ifndef ASAN_INTERFACE_H
#define ASAN_INTERFACE_H
-#include "sanitizer_common/sanitizer_defs.h"
+#include "sanitizer_common/sanitizer_interface_defs.h"
// ----------- ATTENTION -------------
// This header should NOT include any other headers from ASan runtime.
// All functions in this header are extern "C" and start with __asan_.
+using __sanitizer::uptr;
+
extern "C" {
// This function should be called at the very beginning of the process,
// before any instrumented code is executed and before any call to malloc.
- void __asan_init() SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ void __asan_init() SANITIZER_INTERFACE_ATTRIBUTE;
// This function should be called by the instrumented code.
// 'addr' is the address of a global variable called 'name' of 'size' bytes.
void __asan_register_global(uptr addr, uptr size, const char *name)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// This structure describes an instrumented global variable.
struct __asan_global {
@@ -41,18 +43,18 @@
// These two functions should be called by the instrumented code.
// 'globals' is an array of structures describing 'n' globals.
void __asan_register_globals(__asan_global *globals, uptr n)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
void __asan_unregister_globals(__asan_global *globals, uptr n)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// These two functions are used by the instrumented code in the
// use-after-return mode. __asan_stack_malloc allocates size bytes of
// fake stack and __asan_stack_free poisons it. real_stack is a pointer to
// the real stack region.
uptr __asan_stack_malloc(uptr size, uptr real_stack)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
void __asan_stack_free(uptr ptr, uptr size, uptr real_stack)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Marks memory region [addr, addr+size) as unaddressable.
// This memory must be previously allocated by the user program. Accessing
@@ -63,7 +65,7 @@
// Method is NOT thread-safe in the sense that no two threads can
// (un)poison memory in the same memory region simultaneously.
void __asan_poison_memory_region(void const volatile *addr, uptr size)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Marks memory region [addr, addr+size) as addressable.
// This memory must be previously allocated by the user program. Accessing
// addresses in this region is allowed until this region is poisoned again.
@@ -72,16 +74,13 @@
// Method is NOT thread-safe in the sense that no two threads can
// (un)poison memory in the same memory region simultaneously.
void __asan_unpoison_memory_region(void const volatile *addr, uptr size)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Performs cleanup before a NoReturn function. Must be called before things
// like _exit and execl to avoid false positives on stack.
- void __asan_handle_no_return() SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ void __asan_handle_no_return() SANITIZER_INTERFACE_ATTRIBUTE;
// User code should use macro instead of functions.
-#if !defined(__has_feature)
-#define __has_feature(x) 0
-#endif
#if __has_feature(address_sanitizer)
#define ASAN_POISON_MEMORY_REGION(addr, size) \
__asan_poison_memory_region((addr), (size))
@@ -97,68 +96,67 @@
// Returns true iff addr is poisoned (i.e. 1-byte read/write access to this
// address will result in error report from AddressSanitizer).
bool __asan_address_is_poisoned(void const volatile *addr)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// This is an internal function that is called to report an error.
// However it is still a part of the interface because users may want to
// set a breakpoint on this function in a debugger.
void __asan_report_error(uptr pc, uptr bp, uptr sp,
uptr addr, bool is_write, uptr access_size)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Sets the exit code to use when reporting an error.
// Returns the old value.
int __asan_set_error_exit_code(int exit_code)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Sets the callback to be called right before death on error.
// Passing 0 will unset the callback.
void __asan_set_death_callback(void (*callback)(void))
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
void __asan_set_error_report_callback(void (*callback)(const char*))
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Returns the estimated number of bytes that will be reserved by allocator
// for request of "size" bytes. If ASan allocator can't allocate that much
// memory, returns the maximal possible allocation size, otherwise returns
// "size".
uptr __asan_get_estimated_allocated_size(uptr size)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Returns true if p was returned by the ASan allocator and
// is not yet freed.
bool __asan_get_ownership(const void *p)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Returns the number of bytes reserved for the pointer p.
// Requires (get_ownership(p) == true) or (p == 0).
uptr __asan_get_allocated_size(const void *p)
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Number of bytes, allocated and not yet freed by the application.
uptr __asan_get_current_allocated_bytes()
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Number of bytes, mmaped by asan allocator to fulfill allocation requests.
// Generally, for request of X bytes, allocator can reserve and add to free
// lists a large number of chunks of size X to use them for future requests.
// All these chunks count toward the heap size. Currently, allocator never
// releases memory to OS (instead, it just puts freed chunks to free lists).
uptr __asan_get_heap_size()
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Number of bytes, mmaped by asan allocator, which can be used to fulfill
// allocation requests. When a user program frees memory chunk, it can first
// fall into quarantine and will count toward __asan_get_free_bytes() later.
uptr __asan_get_free_bytes()
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Number of bytes in unmapped pages, that are released to OS. Currently,
// always returns 0.
uptr __asan_get_unmapped_bytes()
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
// Prints accumulated stats to stderr. Used for debugging.
void __asan_print_accumulated_stats()
- SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE;
+ SANITIZER_INTERFACE_ATTRIBUTE;
#if !defined(_WIN32)
// We do not need to redefine the defaults right now on Windows.
- char *__asan_default_options
- SANITIZER_WEAK_ATTRIBUTE;
+ char *__asan_default_options SANITIZER_WEAK_ATTRIBUTE;
#endif
} // namespace
diff --git a/lib/asan/asan_internal.h b/lib/asan/asan_internal.h
index 2df8729..a70a1e6 100644
--- a/lib/asan/asan_internal.h
+++ b/lib/asan/asan_internal.h
@@ -17,44 +17,17 @@
#include "sanitizer_common/sanitizer_defs.h"
#include "sanitizer_common/sanitizer_libc.h"
+using namespace __sanitizer; // NOLINT
+
#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
# error "This operating system is not supported by AddressSanitizer"
#endif
#if defined(_WIN32)
-
-typedef unsigned long DWORD; // NOLINT
-
extern "C" void* _ReturnAddress(void);
# pragma intrinsic(_ReturnAddress)
-
-# define ALIAS(x) // TODO(timurrrr): do we need this on Windows?
-# define ALIGNED(x) __declspec(align(x))
-# define NOINLINE __declspec(noinline)
-# define NORETURN __declspec(noreturn)
-
-# define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win?
-#else // defined(_WIN32)
-// #include <stdint.h>
-
-# define ALIAS(x) __attribute__((alias(x)))
-# define ALIGNED(x) __attribute__((aligned(x)))
-# define NOINLINE __attribute__((noinline))
-# define NORETURN __attribute__((noreturn))
-
-# define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
#endif // defined(_WIN32)
-// If __WORDSIZE was undefined by the platform, define it in terms of the
-// compiler built-ins __LP64__ and _WIN64.
-#ifndef __WORDSIZE
-#if __LP64__ || defined(_WIN64)
-#define __WORDSIZE 64
-#else
-#define __WORDSIZE 32
-#endif
-#endif
-
// Limits for integral types. We have to redefine it in case we don't
// have stdint.h (like in Visual Studio 9).
#if __WORDSIZE == 64
@@ -99,10 +72,6 @@
#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
-#if !defined(__has_feature)
-#define __has_feature(x) 0
-#endif
-
#if __has_feature(address_sanitizer)
# error "The AddressSanitizer run-time should not be"
" instrumented by AddressSanitizer"
diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc
index c668bec..af132e9 100644
--- a/lib/asan/asan_rtl.cc
+++ b/lib/asan/asan_rtl.cc
@@ -231,7 +231,7 @@
// -------------------------- Run-time entry ------------------- {{{1
// exported functions
#define ASAN_REPORT_ERROR(type, is_write, size) \
-extern "C" NOINLINE ASAN_INTERFACE_ATTRIBUTE \
+extern "C" NOINLINE INTERFACE_ATTRIBUTE \
void __asan_report_ ## type ## size(uptr addr); \
void __asan_report_ ## type ## size(uptr addr) { \
GET_CALLER_PC_BP_SP; \
diff --git a/lib/sanitizer_common/sanitizer_defs.h b/lib/sanitizer_common/sanitizer_defs.h
index d72bb52..ac22021 100644
--- a/lib/sanitizer_common/sanitizer_defs.h
+++ b/lib/sanitizer_common/sanitizer_defs.h
@@ -8,35 +8,55 @@
//===----------------------------------------------------------------------===//
//
// This file is shared between AddressSanitizer and ThreadSanitizer.
+// It contains macro used in run-time libraries code.
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_DEFS_H
#define SANITIZER_DEFS_H
+#include "sanitizer_interface_defs.h"
+using namespace __sanitizer; // NOLINT
// ----------- ATTENTION -------------
// This header should NOT include any other headers to avoid portability issues.
+// Common defs.
+#define INLINE static inline
+#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
+#define WEAK SANITIZER_WEAK_ATTRIBUTE
+
+// Platform-specific defs.
#if defined(_WIN32)
-// FIXME find out what we need on Windows. __declspec(dllexport) ?
-#define SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE
-#define SANITIZER_WEAK_ATTRIBUTE
-#else
-#define SANITIZER_INTERFACE_FUNCTION_ATTRIBUTE \
- __attribute__((visibility("default")))
-#define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak));
+typedef unsigned long DWORD; // NOLINT
+// FIXME(timurrrr): do we need this on Windows?
+# define ALIAS(x)
+# define ALIGNED(x) __declspec(align(x))
+# define NOINLINE __declspec(noinline)
+# define NORETURN __declspec(noreturn)
+# define THREADLOCAL __declspec(thread)
+#else // _WIN32
+# define ALIAS(x) __attribute__((alias(x)))
+# define ALIGNED(x) __attribute__((aligned(x)))
+# define NOINLINE __attribute__((noinline))
+# define NORETURN __attribute__((noreturn))
+# define THREADLOCAL __thread
+#endif // _WIN32
+
+// We have no equivalent of these on Windows.
+#ifndef _WIN32
+# define ALWAYS_INLINE __attribute__((always_inline))
+# define LIKELY(x) __builtin_expect(!!(x), 1)
+# define UNLIKELY(x) __builtin_expect(!!(x), 0)
+# define FORMAT(f, a) __attribute__((format(printf, f, a)))
+# define USED __attribute__((used))
#endif
-// For portability reasons we do not include stddef.h, stdint.h or any other
-// system header, but we do need some basic types that are not defined
-// in a portable way by the language itself.
-typedef unsigned long uptr; // NOLINT
-typedef signed long sptr; // NOLINT
-typedef unsigned char u8;
-typedef unsigned short u16; // NOLINT
-typedef unsigned int u32;
-typedef unsigned long long u64; // NOLINT
-typedef signed char s8;
-typedef signed short s16; // NOLINT
-typedef signed int s32;
-typedef signed long long s64; // NOLINT
+// If __WORDSIZE was undefined by the platform, define it in terms of the
+// compiler built-ins __LP64__ and _WIN64.
+#ifndef __WORDSIZE
+# if __LP64__ || defined(_WIN64)
+# define __WORDSIZE 64
+# else
+# define __WORDSIZE 32
+# endif
+#endif // __WORDSIZE
#endif // SANITIZER_DEFS_H
diff --git a/lib/sanitizer_common/sanitizer_interface_defs.h b/lib/sanitizer_common/sanitizer_interface_defs.h
new file mode 100644
index 0000000..ae53276
--- /dev/null
+++ b/lib/sanitizer_common/sanitizer_interface_defs.h
@@ -0,0 +1,53 @@
+//===-- sanitizer_interface_defs.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.
+// It contains basic macro and types.
+// NOTE: This file may be included into user code.
+//===----------------------------------------------------------------------===//
+
+#ifndef SANITIZER_INTERFACE_DEFS_H
+#define SANITIZER_INTERFACE_DEFS_H
+
+// ----------- ATTENTION -------------
+// This header should NOT include any other headers to avoid portability issues.
+
+#if defined(_WIN32)
+// FIXME find out what we need on Windows. __declspec(dllexport) ?
+# define SANITIZER_INTERFACE_ATTRIBUTE
+# define SANITIZER_WEAK_ATTRIBUTE
+#else // _WIN32
+# define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
+# define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
+#endif // _WIN32
+
+// __has_feature
+#if !defined(__has_feature)
+# define __has_feature(x) 0
+#endif
+
+// For portability reasons we do not include stddef.h, stdint.h or any other
+// system header, but we do need some basic types that are not defined
+// in a portable way by the language itself.
+namespace __sanitizer {
+
+typedef unsigned long uptr; // NOLINT
+typedef signed long sptr; // NOLINT
+typedef unsigned char u8;
+typedef unsigned short u16; // NOLINT
+typedef unsigned int u32;
+typedef unsigned long long u64; // NOLINT
+typedef signed char s8;
+typedef signed short s16; // NOLINT
+typedef signed int s32;
+typedef signed long long s64; // NOLINT
+
+} // namespace __sanitizer
+
+#endif // SANITIZER_INTERFACE_DEFS_H
diff --git a/lib/tsan/rtl/tsan_allocator.cc b/lib/tsan/rtl/tsan_allocator.cc
index 28cc2b3..1571b47 100644
--- a/lib/tsan/rtl/tsan_allocator.cc
+++ b/lib/tsan/rtl/tsan_allocator.cc
@@ -13,7 +13,7 @@
#include "tsan_allocator.h"
// Provisional implementation.
-extern "C" void *__libc_malloc(__tsan::uptr size);
+extern "C" void *__libc_malloc(__sanitizer::uptr size);
extern "C" void __libc_free(void *ptr);
namespace __tsan {
diff --git a/lib/tsan/rtl/tsan_compiler.h b/lib/tsan/rtl/tsan_compiler.h
deleted file mode 100644
index 6aab097..0000000
--- a/lib/tsan/rtl/tsan_compiler.h
+++ /dev/null
@@ -1,30 +0,0 @@
-//===-- tsan_rtl.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 a part of ThreadSanitizer (TSan), a race detector.
-//
-// Compiler-specific definitions.
-//===----------------------------------------------------------------------===//
-
-#ifndef TSAN_COMPILER_H
-#define TSAN_COMPILER_H
-
-#define INLINE static inline
-#define NOINLINE __attribute__((noinline))
-#define ALWAYS_INLINE __attribute__((always_inline))
-#define NORETURN __attribute__((noreturn))
-#define WEAK __attribute__((weak))
-#define ALIGN(n) __attribute__((aligned(n)))
-#define LIKELY(x) __builtin_expect(!!(x), 1)
-#define UNLIKELY(x) __builtin_expect(!!(x), 0)
-#define THREADLOCAL __thread
-#define FORMAT(f, a) __attribute__((format(printf, f, a)))
-#define USED __attribute__((used))
-
-#endif // TSAN_COMPILER_H
diff --git a/lib/tsan/rtl/tsan_defs.h b/lib/tsan/rtl/tsan_defs.h
index 608b58e..c25cb81 100644
--- a/lib/tsan/rtl/tsan_defs.h
+++ b/lib/tsan/rtl/tsan_defs.h
@@ -14,19 +14,16 @@
#ifndef TSAN_DEFS_H
#define TSAN_DEFS_H
-#include "tsan_compiler.h"
+#include "sanitizer_common/sanitizer_defs.h"
#include "tsan_stat.h"
#ifndef TSAN_DEBUG
#define TSAN_DEBUG 0
#endif // TSAN_DEBUG
-namespace __tsan {
+using namespace __sanitizer;
-typedef unsigned u32; // NOLINT
-typedef unsigned long long u64; // NOLINT
-typedef signed long long s64; // NOLINT
-typedef unsigned long uptr; // NOLINT
+namespace __tsan {
const uptr kPageSize = 4096;
const int kTidBits = 13;
@@ -60,8 +57,8 @@
#define CHECK_IMPL(c1, op, c2) \
do { \
- __tsan::u64 v1 = (u64)(c1); \
- __tsan::u64 v2 = (u64)(c2); \
+ __sanitizer::u64 v1 = (u64)(c1); \
+ __sanitizer::u64 v2 = (u64)(c2); \
if (!(v1 op v2)) \
__tsan::CheckFailed(__FILE__, __LINE__, \
"(" #c1 ") " #op " (" #c2 ")", v1, v2); \
diff --git a/lib/tsan/rtl/tsan_interceptors.cc b/lib/tsan/rtl/tsan_interceptors.cc
index 83bfa2c..3cc6f14 100644
--- a/lib/tsan/rtl/tsan_interceptors.cc
+++ b/lib/tsan/rtl/tsan_interceptors.cc
@@ -126,7 +126,7 @@
StatInc(thr, StatInterceptor); \
StatInc(thr, StatInt_##func); \
ScopedInterceptor si(thr, #func, \
- (__tsan::uptr)__builtin_return_address(0)); \
+ (__sanitizer::uptr)__builtin_return_address(0)); \
const uptr pc = (uptr)&func; \
(void)pc; \
/**/
diff --git a/lib/tsan/rtl/tsan_interface_ann.cc b/lib/tsan/rtl/tsan_interface_ann.cc
index b5c8f99..58be599 100644
--- a/lib/tsan/rtl/tsan_interface_ann.cc
+++ b/lib/tsan/rtl/tsan_interface_ann.cc
@@ -84,7 +84,7 @@
};
static DynamicAnnContext *dyn_ann_ctx;
-static char dyn_ann_ctx_placeholder[sizeof(DynamicAnnContext)] ALIGN(64);
+static char dyn_ann_ctx_placeholder[sizeof(DynamicAnnContext)] ALIGNED(64);
static void AddExpectRace(ExpectRace *list,
char *f, int l, uptr addr, uptr size, char *desc) {
diff --git a/lib/tsan/rtl/tsan_placement_new.h b/lib/tsan/rtl/tsan_placement_new.h
index 7b8ba03..e506b17 100644
--- a/lib/tsan/rtl/tsan_placement_new.h
+++ b/lib/tsan/rtl/tsan_placement_new.h
@@ -17,7 +17,7 @@
#include "tsan_defs.h"
-inline void *operator new(__tsan::uptr sz, void *p) {
+inline void *operator new(__sanitizer::uptr sz, void *p) {
return p;
}
diff --git a/lib/tsan/rtl/tsan_platform_linux.cc b/lib/tsan/rtl/tsan_platform_linux.cc
index 0525478..8f8f982 100644
--- a/lib/tsan/rtl/tsan_platform_linux.cc
+++ b/lib/tsan/rtl/tsan_platform_linux.cc
@@ -39,7 +39,7 @@
using namespace __sanitizer; // NOLINT
-extern "C" int arch_prctl(int code, __tsan::uptr *addr);
+extern "C" int arch_prctl(int code, __sanitizer::uptr *addr);
namespace __tsan {
diff --git a/lib/tsan/rtl/tsan_rtl.cc b/lib/tsan/rtl/tsan_rtl.cc
index 4b80504..c9ebcf7 100644
--- a/lib/tsan/rtl/tsan_rtl.cc
+++ b/lib/tsan/rtl/tsan_rtl.cc
@@ -31,8 +31,8 @@
namespace __tsan {
using namespace __sanitizer;
-THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGN(64);
-static char ctx_placeholder[sizeof(Context)] ALIGN(64);
+THREADLOCAL char cur_thread_placeholder[sizeof(ThreadState)] ALIGNED(64);
+static char ctx_placeholder[sizeof(Context)] ALIGNED(64);
static Context *ctx;
Context *CTX() {