1 //===-- internal_defs.h -----------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef SCUDO_INTERNAL_DEFS_H_ 10 #define SCUDO_INTERNAL_DEFS_H_ 11 12 #include "platform.h" 13 14 #include <stdint.h> 15 16 #ifndef SCUDO_DEBUG 17 #define SCUDO_DEBUG 0 18 #endif 19 20 #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) 21 22 // String related macros. 23 24 #define STRINGIFY_(S) #S 25 #define STRINGIFY(S) STRINGIFY_(S) 26 #define CONCATENATE_(S, C) S##C 27 #define CONCATENATE(S, C) CONCATENATE_(S, C) 28 29 // Attributes & builtins related macros. 30 31 #define INTERFACE __attribute__((visibility("default"))) 32 #define HIDDEN __attribute__((visibility("hidden"))) 33 #define WEAK __attribute__((weak)) 34 #define ALWAYS_INLINE inline __attribute__((always_inline)) 35 #define ALIAS(X) __attribute__((alias(X))) 36 #define FORMAT(F, A) __attribute__((format(printf, F, A))) 37 #define NOINLINE __attribute__((noinline)) 38 #define NORETURN __attribute__((noreturn)) 39 #define THREADLOCAL __thread 40 #define LIKELY(X) __builtin_expect(!!(X), 1) 41 #define UNLIKELY(X) __builtin_expect(!!(X), 0) 42 #if defined(__i386__) || defined(__x86_64__) 43 // __builtin_prefetch(X) generates prefetchnt0 on x86 44 #define PREFETCH(X) __asm__("prefetchnta (%0)" : : "r"(X)) 45 #else 46 #define PREFETCH(X) __builtin_prefetch(X) 47 #endif 48 #define UNUSED __attribute__((unused)) 49 #define USED __attribute__((used)) 50 #define NOEXCEPT noexcept 51 52 namespace scudo { 53 54 typedef unsigned long uptr; 55 typedef unsigned char u8; 56 typedef unsigned short u16; 57 typedef unsigned int u32; 58 typedef unsigned long long u64; 59 typedef signed long sptr; 60 typedef signed char s8; 61 typedef signed short s16; 62 typedef signed int s32; 63 typedef signed long long s64; 64 65 // The following two functions have platform specific implementations. 66 void outputRaw(const char *Buffer); 67 void NORETURN die(); 68 69 #define RAW_CHECK_MSG(Expr, Msg) \ 70 do { \ 71 if (UNLIKELY(!(Expr))) { \ 72 outputRaw(Msg); \ 73 die(); \ 74 } \ 75 } while (false) 76 77 #define RAW_CHECK(Expr) RAW_CHECK_MSG(Expr, #Expr) 78 79 void NORETURN reportCheckFailed(const char *File, int Line, 80 const char *Condition, u64 Value1, u64 Value2); 81 82 #define CHECK_IMPL(C1, Op, C2) \ 83 do { \ 84 scudo::u64 V1 = (scudo::u64)(C1); \ 85 scudo::u64 V2 = (scudo::u64)(C2); \ 86 if (UNLIKELY(!(V1 Op V2))) { \ 87 scudo::reportCheckFailed(__FILE__, __LINE__, \ 88 "(" #C1 ") " #Op " (" #C2 ")", V1, V2); \ 89 scudo::die(); \ 90 } \ 91 } while (false) 92 93 #define CHECK(A) CHECK_IMPL((A), !=, 0) 94 #define CHECK_EQ(A, B) CHECK_IMPL((A), ==, (B)) 95 #define CHECK_NE(A, B) CHECK_IMPL((A), !=, (B)) 96 #define CHECK_LT(A, B) CHECK_IMPL((A), <, (B)) 97 #define CHECK_LE(A, B) CHECK_IMPL((A), <=, (B)) 98 #define CHECK_GT(A, B) CHECK_IMPL((A), >, (B)) 99 #define CHECK_GE(A, B) CHECK_IMPL((A), >=, (B)) 100 101 #if SCUDO_DEBUG 102 #define DCHECK(A) CHECK(A) 103 #define DCHECK_EQ(A, B) CHECK_EQ(A, B) 104 #define DCHECK_NE(A, B) CHECK_NE(A, B) 105 #define DCHECK_LT(A, B) CHECK_LT(A, B) 106 #define DCHECK_LE(A, B) CHECK_LE(A, B) 107 #define DCHECK_GT(A, B) CHECK_GT(A, B) 108 #define DCHECK_GE(A, B) CHECK_GE(A, B) 109 #else 110 #define DCHECK(A) 111 #define DCHECK_EQ(A, B) 112 #define DCHECK_NE(A, B) 113 #define DCHECK_LT(A, B) 114 #define DCHECK_LE(A, B) 115 #define DCHECK_GT(A, B) 116 #define DCHECK_GE(A, B) 117 #endif 118 119 // The superfluous die() call effectively makes this macro NORETURN. 120 #define UNREACHABLE(Msg) \ 121 do { \ 122 CHECK(0 && Msg); \ 123 die(); \ 124 } while (0) 125 126 } // namespace scudo 127 128 #endif // SCUDO_INTERNAL_DEFS_H_ 129