2012-06-05 22:25:27 +08:00
|
|
|
//===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
|
2012-05-31 22:11:07 +08:00
|
|
|
//
|
|
|
|
// 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.
|
2012-06-05 21:50:57 +08:00
|
|
|
// It contains macro used in run-time libraries code.
|
2012-05-31 22:11:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_DEFS_H
|
|
|
|
#define SANITIZER_DEFS_H
|
|
|
|
|
2012-08-29 10:27:54 +08:00
|
|
|
#include "sanitizer/common_interface_defs.h"
|
2012-06-05 21:50:57 +08:00
|
|
|
using namespace __sanitizer; // NOLINT
|
2012-05-31 22:11:07 +08:00
|
|
|
// ----------- ATTENTION -------------
|
|
|
|
// This header should NOT include any other headers to avoid portability issues.
|
|
|
|
|
2012-06-05 21:50:57 +08:00
|
|
|
// Common defs.
|
|
|
|
#define INLINE static inline
|
|
|
|
#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
#define WEAK SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
|
|
|
|
// Platform-specific defs.
|
2012-11-06 20:47:42 +08:00
|
|
|
#if defined(_MSC_VER)
|
2012-06-28 05:00:23 +08:00
|
|
|
# define ALWAYS_INLINE __declspec(forceinline)
|
2012-06-05 21:50:57 +08:00
|
|
|
// FIXME(timurrrr): do we need this on Windows?
|
|
|
|
# define ALIAS(x)
|
|
|
|
# define ALIGNED(x) __declspec(align(x))
|
2012-06-06 21:37:02 +08:00
|
|
|
# define FORMAT(f, a)
|
2012-06-05 21:50:57 +08:00
|
|
|
# define NOINLINE __declspec(noinline)
|
|
|
|
# define NORETURN __declspec(noreturn)
|
|
|
|
# define THREADLOCAL __declspec(thread)
|
2012-06-28 05:00:23 +08:00
|
|
|
# define NOTHROW
|
2012-11-06 20:54:16 +08:00
|
|
|
# define LIKELY(x) (x)
|
|
|
|
# define UNLIKELY(x) (x)
|
|
|
|
# define UNUSED
|
|
|
|
# define USED
|
2012-11-06 20:47:42 +08:00
|
|
|
#else // _MSC_VER
|
2012-06-28 05:00:23 +08:00
|
|
|
# define ALWAYS_INLINE __attribute__((always_inline))
|
2012-06-05 21:50:57 +08:00
|
|
|
# define ALIAS(x) __attribute__((alias(x)))
|
|
|
|
# define ALIGNED(x) __attribute__((aligned(x)))
|
2012-06-06 21:37:02 +08:00
|
|
|
# define FORMAT(f, a) __attribute__((format(printf, f, a)))
|
2012-06-05 21:50:57 +08:00
|
|
|
# define NOINLINE __attribute__((noinline))
|
|
|
|
# define NORETURN __attribute__((noreturn))
|
|
|
|
# define THREADLOCAL __thread
|
2012-11-06 20:49:22 +08:00
|
|
|
# define NOTHROW throw()
|
2012-06-05 21:50:57 +08:00
|
|
|
# define LIKELY(x) __builtin_expect(!!(x), 1)
|
|
|
|
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
|
2012-06-14 22:02:32 +08:00
|
|
|
# define UNUSED __attribute__((unused))
|
2012-06-05 21:50:57 +08:00
|
|
|
# define USED __attribute__((used))
|
2012-11-06 20:54:16 +08:00
|
|
|
#endif // _MSC_VER
|
2012-05-31 22:11:07 +08:00
|
|
|
|
2012-06-15 15:29:14 +08:00
|
|
|
#if defined(_WIN32)
|
2012-11-06 20:50:13 +08:00
|
|
|
typedef unsigned long DWORD; // NOLINT
|
2012-06-15 15:29:14 +08:00
|
|
|
typedef DWORD thread_return_t;
|
|
|
|
# define THREAD_CALLING_CONV __stdcall
|
|
|
|
#else // _WIN32
|
|
|
|
typedef void* thread_return_t;
|
|
|
|
# define THREAD_CALLING_CONV
|
|
|
|
#endif // _WIN32
|
|
|
|
typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
|
|
|
|
|
2012-11-21 20:38:58 +08:00
|
|
|
#if __LP64__ || defined(_WIN64)
|
|
|
|
# define SANITIZER_WORDSIZE 64
|
|
|
|
#else
|
|
|
|
# define SANITIZER_WORDSIZE 32
|
|
|
|
#endif
|
2012-05-31 22:11:07 +08:00
|
|
|
|
2012-06-06 23:22:20 +08:00
|
|
|
// NOTE: Functions below must be defined in each run-time.
|
|
|
|
namespace __sanitizer {
|
|
|
|
void NORETURN Die();
|
|
|
|
void NORETURN CheckFailed(const char *file, int line, const char *cond,
|
|
|
|
u64 v1, u64 v2);
|
|
|
|
} // namespace __sanitizer
|
|
|
|
|
|
|
|
// Check macro
|
2012-06-06 17:26:25 +08:00
|
|
|
#define RAW_CHECK_MSG(expr, msg) do { \
|
|
|
|
if (!(expr)) { \
|
|
|
|
RawWrite(msg); \
|
|
|
|
Die(); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
|
|
|
|
|
2012-06-06 23:22:20 +08:00
|
|
|
#define CHECK_IMPL(c1, op, c2) \
|
|
|
|
do { \
|
|
|
|
__sanitizer::u64 v1 = (u64)(c1); \
|
|
|
|
__sanitizer::u64 v2 = (u64)(c2); \
|
|
|
|
if (!(v1 op v2)) \
|
|
|
|
__sanitizer::CheckFailed(__FILE__, __LINE__, \
|
|
|
|
"(" #c1 ") " #op " (" #c2 ")", v1, v2); \
|
|
|
|
} while (false) \
|
|
|
|
/**/
|
|
|
|
|
|
|
|
#define CHECK(a) CHECK_IMPL((a), !=, 0)
|
|
|
|
#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
|
|
|
|
#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
|
|
|
|
#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
|
|
|
|
#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
|
|
|
|
#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
|
|
|
|
#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
|
|
|
|
|
2012-06-30 00:58:33 +08:00
|
|
|
#if TSAN_DEBUG
|
|
|
|
#define DCHECK(a) CHECK(a)
|
|
|
|
#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
|
|
|
|
#define DCHECK_NE(a, b) CHECK_NE(a, b)
|
|
|
|
#define DCHECK_LT(a, b) CHECK_LT(a, b)
|
|
|
|
#define DCHECK_LE(a, b) CHECK_LE(a, b)
|
|
|
|
#define DCHECK_GT(a, b) CHECK_GT(a, b)
|
|
|
|
#define DCHECK_GE(a, b) CHECK_GE(a, b)
|
|
|
|
#else
|
|
|
|
#define DCHECK(a)
|
|
|
|
#define DCHECK_EQ(a, b)
|
|
|
|
#define DCHECK_NE(a, b)
|
|
|
|
#define DCHECK_LT(a, b)
|
|
|
|
#define DCHECK_LE(a, b)
|
|
|
|
#define DCHECK_GT(a, b)
|
|
|
|
#define DCHECK_GE(a, b)
|
|
|
|
#endif
|
|
|
|
|
2012-10-09 16:42:07 +08:00
|
|
|
#define UNREACHABLE(msg) do { \
|
|
|
|
CHECK(0 && msg); \
|
|
|
|
Die(); \
|
2012-10-16 12:50:32 +08:00
|
|
|
} while (0)
|
2012-10-09 16:42:07 +08:00
|
|
|
|
|
|
|
#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
|
2012-06-06 23:47:40 +08:00
|
|
|
|
2012-06-21 18:04:36 +08:00
|
|
|
#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
|
|
|
|
|
2012-08-28 19:54:51 +08:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
|
|
|
|
|
2012-06-21 18:04:36 +08:00
|
|
|
#define IMPL_PASTE(a, b) a##b
|
|
|
|
#define IMPL_COMPILER_ASSERT(pred, line) \
|
2012-09-11 18:31:28 +08:00
|
|
|
typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
|
2012-06-21 18:04:36 +08:00
|
|
|
|
2012-06-15 21:09:52 +08:00
|
|
|
// Limits for integral types. We have to redefine it in case we don't
|
|
|
|
// have stdint.h (like in Visual Studio 9).
|
2012-09-24 19:43:40 +08:00
|
|
|
#undef __INT64_C
|
|
|
|
#undef __UINT64_C
|
2012-11-21 20:38:58 +08:00
|
|
|
#if SANITIZER_WORDSIZE == 64
|
2012-06-15 21:09:52 +08:00
|
|
|
# define __INT64_C(c) c ## L
|
|
|
|
# define __UINT64_C(c) c ## UL
|
|
|
|
#else
|
|
|
|
# define __INT64_C(c) c ## LL
|
|
|
|
# define __UINT64_C(c) c ## ULL
|
2012-11-21 20:38:58 +08:00
|
|
|
#endif // SANITIZER_WORDSIZE == 64
|
2012-06-15 21:09:52 +08:00
|
|
|
#undef INT32_MIN
|
|
|
|
#define INT32_MIN (-2147483647-1)
|
|
|
|
#undef INT32_MAX
|
|
|
|
#define INT32_MAX (2147483647)
|
|
|
|
#undef UINT32_MAX
|
|
|
|
#define UINT32_MAX (4294967295U)
|
|
|
|
#undef INT64_MIN
|
|
|
|
#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
|
|
|
|
#undef INT64_MAX
|
|
|
|
#define INT64_MAX (__INT64_C(9223372036854775807))
|
|
|
|
#undef UINT64_MAX
|
|
|
|
#define UINT64_MAX (__UINT64_C(18446744073709551615))
|
|
|
|
|
2012-08-27 17:30:58 +08:00
|
|
|
enum LinkerInitialized { LINKER_INITIALIZED = 0 };
|
|
|
|
|
2012-09-24 19:43:40 +08:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__)
|
2012-08-28 22:11:57 +08:00
|
|
|
# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
|
|
|
|
# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
|
|
|
|
#else
|
2012-08-28 23:25:07 +08:00
|
|
|
extern "C" void* _ReturnAddress(void);
|
|
|
|
# pragma intrinsic(_ReturnAddress)
|
2012-08-28 22:11:57 +08:00
|
|
|
# define GET_CALLER_PC() (uptr)_ReturnAddress()
|
|
|
|
// CaptureStackBackTrace doesn't need to know BP on Windows.
|
|
|
|
// FIXME: This macro is still used when printing error reports though it's not
|
|
|
|
// clear if the BP value is needed in the ASan reports on Windows.
|
|
|
|
# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
|
|
|
|
#endif
|
|
|
|
|
2012-10-02 21:41:40 +08:00
|
|
|
#define HANDLE_EINTR(res, f) { \
|
|
|
|
do { \
|
|
|
|
res = (f); \
|
|
|
|
} while (res == -1 && errno == EINTR); \
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:11:07 +08:00
|
|
|
#endif // SANITIZER_DEFS_H
|