sanitizer_internal_defs.h revision c375657778e8a72d566e29951060b7eadc749b0d
194b5036ee6ba866e1702848855b6d687d1e70afaAlexey Samsonov//===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===// 29aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// 39aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// The LLVM Compiler Infrastructure 49aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// 59aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// This file is distributed under the University of Illinois Open Source 69aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// License. See LICENSE.TXT for details. 79aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// 89aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany//===----------------------------------------------------------------------===// 99aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// 109aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// This file is shared between AddressSanitizer and ThreadSanitizer. 110a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov// It contains macro used in run-time libraries code. 129aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany//===----------------------------------------------------------------------===// 139aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#ifndef SANITIZER_DEFS_H 149aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#define SANITIZER_DEFS_H 159aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany 160a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#include "sanitizer_interface_defs.h" 170a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonovusing namespace __sanitizer; // NOLINT 189aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// ----------- ATTENTION ------------- 199aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany// This header should NOT include any other headers to avoid portability issues. 209aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany 210a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov// Common defs. 220a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#define INLINE static inline 230a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE 240a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#define WEAK SANITIZER_WEAK_ATTRIBUTE 250a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov 260a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov// Platform-specific defs. 279aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#if defined(_WIN32) 280a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonovtypedef unsigned long DWORD; // NOLINT 290a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov// FIXME(timurrrr): do we need this on Windows? 300a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define ALIAS(x) 310a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define ALIGNED(x) __declspec(align(x)) 3215503b0a4331c7f27f9cebc25e25c2e494f61cb9Alexey Samsonov# define FORMAT(f, a) 330a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define NOINLINE __declspec(noinline) 340a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define NORETURN __declspec(noreturn) 350a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define THREADLOCAL __declspec(thread) 360a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#else // _WIN32 370a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define ALIAS(x) __attribute__((alias(x))) 380a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define ALIGNED(x) __attribute__((aligned(x))) 3915503b0a4331c7f27f9cebc25e25c2e494f61cb9Alexey Samsonov# define FORMAT(f, a) __attribute__((format(printf, f, a))) 400a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define NOINLINE __attribute__((noinline)) 410a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define NORETURN __attribute__((noreturn)) 420a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define THREADLOCAL __thread 430a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#endif // _WIN32 440a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov 450a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov// We have no equivalent of these on Windows. 460a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#ifndef _WIN32 470a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define ALWAYS_INLINE __attribute__((always_inline)) 480a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define LIKELY(x) __builtin_expect(!!(x), 1) 490a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define UNLIKELY(x) __builtin_expect(!!(x), 0) 502ea978704a794e536d2801affcc7f301092d75daAlexey Samsonov# define UNUSED __attribute__((unused)) 510a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define USED __attribute__((used)) 529aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#endif 539aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany 54dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonov#if defined(_WIN32) 55dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonovtypedef DWORD thread_return_t; 56dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonov# define THREAD_CALLING_CONV __stdcall 57dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonov#else // _WIN32 58dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonovtypedef void* thread_return_t; 59dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonov# define THREAD_CALLING_CONV 60dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonov#endif // _WIN32 61dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonovtypedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); 62dd3a911e46b3f0416d60d9be5c84ccfc4b1c3aa8Alexey Samsonov 630a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov// If __WORDSIZE was undefined by the platform, define it in terms of the 640a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov// compiler built-ins __LP64__ and _WIN64. 650a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#ifndef __WORDSIZE 660a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# if __LP64__ || defined(_WIN64) 670a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define __WORDSIZE 64 680a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# else 690a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# define __WORDSIZE 32 700a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov# endif 710a4c906dbc8f150657ddd4f19a7192b779f1d605Alexey Samsonov#endif // __WORDSIZE 729aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany 7315a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov// NOTE: Functions below must be defined in each run-time. 7415a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonovnamespace __sanitizer { 7515a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonovvoid NORETURN Die(); 7615a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonovvoid NORETURN CheckFailed(const char *file, int line, const char *cond, 7715a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov u64 v1, u64 v2); 7815a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov} // namespace __sanitizer 7915a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov 8015a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov// Check macro 81230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov#define RAW_CHECK_MSG(expr, msg) do { \ 82230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov if (!(expr)) { \ 83230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov RawWrite(msg); \ 84230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov Die(); \ 85230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov } \ 86230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov} while (0) 87230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov 88230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) 89230c3be6cdd094a187f48e27ba0961dbeee70344Alexey Samsonov 9015a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK_IMPL(c1, op, c2) \ 9115a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov do { \ 9215a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov __sanitizer::u64 v1 = (u64)(c1); \ 9315a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov __sanitizer::u64 v2 = (u64)(c2); \ 9415a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov if (!(v1 op v2)) \ 9515a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov __sanitizer::CheckFailed(__FILE__, __LINE__, \ 9615a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ 9715a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov } while (false) \ 9815a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov/**/ 9915a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov 10015a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK(a) CHECK_IMPL((a), !=, 0) 10115a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) 10215a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) 10315a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) 10415a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) 10515a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) 10615a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) 10715a77612e0a89c1df444a2034e531c8968d0cedfAlexey Samsonov 1088c53e54ef9e713953ec9495e82e5c330b96e49f3Alexey Samsonov#define UNIMPLEMENTED() CHECK("unimplemented" && 0) 1098c53e54ef9e713953ec9495e82e5c330b96e49f3Alexey Samsonov 110c375657778e8a72d566e29951060b7eadc749b0dKostya Serebryany#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__) 111c375657778e8a72d566e29951060b7eadc749b0dKostya Serebryany 112c375657778e8a72d566e29951060b7eadc749b0dKostya Serebryany#define IMPL_PASTE(a, b) a##b 113c375657778e8a72d566e29951060b7eadc749b0dKostya Serebryany#define IMPL_COMPILER_ASSERT(pred, line) \ 114c375657778e8a72d566e29951060b7eadc749b0dKostya Serebryany typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]; 115c375657778e8a72d566e29951060b7eadc749b0dKostya Serebryany 116c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov// Limits for integral types. We have to redefine it in case we don't 117c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov// have stdint.h (like in Visual Studio 9). 118c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#if __WORDSIZE == 64 119c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov# define __INT64_C(c) c ## L 120c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov# define __UINT64_C(c) c ## UL 121c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#else 122c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov# define __INT64_C(c) c ## LL 123c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov# define __UINT64_C(c) c ## ULL 124c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#endif // __WORDSIZE == 64 125c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#undef INT32_MIN 126c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#define INT32_MIN (-2147483647-1) 127c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#undef INT32_MAX 128c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#define INT32_MAX (2147483647) 129c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#undef UINT32_MAX 130c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#define UINT32_MAX (4294967295U) 131c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#undef INT64_MIN 132c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#define INT64_MIN (-__INT64_C(9223372036854775807)-1) 133c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#undef INT64_MAX 134c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#define INT64_MAX (__INT64_C(9223372036854775807)) 135c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#undef UINT64_MAX 136c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov#define UINT64_MAX (__UINT64_C(18446744073709551615)) 137c925697df6626bb0ea27ea96539bf0580f8f3d3dAlexey Samsonov 1389aead37421a6e4bf43265e5195c6ac31fc519982Kostya Serebryany#endif // SANITIZER_DEFS_H 139