blob: 07c940ee3dbf5f07bc9292a2ab76a80b2730592c [file] [log] [blame]
Alexey Samsonov5bbf8292012-06-05 14:25:27 +00001//===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
Kostya Serebryany1b712072012-05-31 14:11:07 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is shared between AddressSanitizer and ThreadSanitizer.
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000011// It contains macro used in run-time libraries code.
Kostya Serebryany1b712072012-05-31 14:11:07 +000012//===----------------------------------------------------------------------===//
13#ifndef SANITIZER_DEFS_H
14#define SANITIZER_DEFS_H
15
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000016#include "sanitizer_interface_defs.h"
17using namespace __sanitizer; // NOLINT
Kostya Serebryany1b712072012-05-31 14:11:07 +000018// ----------- ATTENTION -------------
19// This header should NOT include any other headers to avoid portability issues.
20
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000021// Common defs.
22#define INLINE static inline
23#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
24#define WEAK SANITIZER_WEAK_ATTRIBUTE
25
26// Platform-specific defs.
Kostya Serebryany1b712072012-05-31 14:11:07 +000027#if defined(_WIN32)
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000028typedef unsigned long DWORD; // NOLINT
29// FIXME(timurrrr): do we need this on Windows?
30# define ALIAS(x)
31# define ALIGNED(x) __declspec(align(x))
Alexey Samsonov8bafdd02012-06-06 13:37:02 +000032# define FORMAT(f, a)
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000033# define NOINLINE __declspec(noinline)
34# define NORETURN __declspec(noreturn)
35# define THREADLOCAL __declspec(thread)
36#else // _WIN32
37# define ALIAS(x) __attribute__((alias(x)))
38# define ALIGNED(x) __attribute__((aligned(x)))
Alexey Samsonov8bafdd02012-06-06 13:37:02 +000039# define FORMAT(f, a) __attribute__((format(printf, f, a)))
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000040# define NOINLINE __attribute__((noinline))
41# define NORETURN __attribute__((noreturn))
42# define THREADLOCAL __thread
43#endif // _WIN32
44
45// We have no equivalent of these on Windows.
46#ifndef _WIN32
47# define ALWAYS_INLINE __attribute__((always_inline))
48# define LIKELY(x) __builtin_expect(!!(x), 1)
49# define UNLIKELY(x) __builtin_expect(!!(x), 0)
Alexey Samsonovaac5d0c2012-06-14 14:02:32 +000050# define UNUSED __attribute__((unused))
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000051# define USED __attribute__((used))
Kostya Serebryany1b712072012-05-31 14:11:07 +000052#endif
53
Alexey Samsonovef2e2cf2012-06-05 13:50:57 +000054// If __WORDSIZE was undefined by the platform, define it in terms of the
55// compiler built-ins __LP64__ and _WIN64.
56#ifndef __WORDSIZE
57# if __LP64__ || defined(_WIN64)
58# define __WORDSIZE 64
59# else
60# define __WORDSIZE 32
61# endif
62#endif // __WORDSIZE
Kostya Serebryany1b712072012-05-31 14:11:07 +000063
Alexey Samsonove4287792012-06-06 15:22:20 +000064// NOTE: Functions below must be defined in each run-time.
65namespace __sanitizer {
66void NORETURN Die();
67void NORETURN CheckFailed(const char *file, int line, const char *cond,
68 u64 v1, u64 v2);
69} // namespace __sanitizer
70
71// Check macro
Alexey Samsonovee072902012-06-06 09:26:25 +000072#define RAW_CHECK_MSG(expr, msg) do { \
73 if (!(expr)) { \
74 RawWrite(msg); \
75 Die(); \
76 } \
77} while (0)
78
79#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
80
Alexey Samsonove4287792012-06-06 15:22:20 +000081#define CHECK_IMPL(c1, op, c2) \
82 do { \
83 __sanitizer::u64 v1 = (u64)(c1); \
84 __sanitizer::u64 v2 = (u64)(c2); \
85 if (!(v1 op v2)) \
86 __sanitizer::CheckFailed(__FILE__, __LINE__, \
87 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
88 } while (false) \
89/**/
90
91#define CHECK(a) CHECK_IMPL((a), !=, 0)
92#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
93#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
94#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
95#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
96#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
97#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
98
Alexey Samsonove95e29c2012-06-06 15:47:40 +000099#define UNIMPLEMENTED() CHECK("unimplemented" && 0)
100
Kostya Serebryany1b712072012-05-31 14:11:07 +0000101#endif // SANITIZER_DEFS_H