1 //===-- sanitizer_test_utils.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 // This file is a part of *Sanitizer runtime. 10 // Common unit tests utilities. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef SANITIZER_TEST_UTILS_H 15 #define SANITIZER_TEST_UTILS_H 16 17 #if defined(_WIN32) 18 // <windows.h> should always be the first include on Windows. 19 # include <windows.h> 20 // MSVS headers define max/min as macros, so std::max/min gets crazy. 21 # undef max 22 # undef min 23 #endif 24 25 #if !defined(SANITIZER_EXTERNAL_TEST_CONFIG) 26 # define INCLUDED_FROM_SANITIZER_TEST_UTILS_H 27 # include "sanitizer_test_config.h" 28 # undef INCLUDED_FROM_SANITIZER_TEST_UTILS_H 29 #endif 30 31 #include <stdint.h> 32 33 #if defined(_MSC_VER) 34 # define NOINLINE __declspec(noinline) 35 #else // defined(_MSC_VER) 36 # define NOINLINE __attribute__((noinline)) 37 #endif // defined(_MSC_VER) 38 39 #if !defined(_MSC_VER) || defined(__clang__) 40 # define UNUSED __attribute__((unused)) 41 # define USED __attribute__((used)) 42 #else 43 # define UNUSED 44 # define USED 45 #endif 46 47 #if !defined(__has_feature) 48 #define __has_feature(x) 0 49 #endif 50 51 #ifndef ATTRIBUTE_NO_SANITIZE_ADDRESS 52 # if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 53 # define ATTRIBUTE_NO_SANITIZE_ADDRESS \ 54 __attribute__((no_sanitize_address)) 55 # else 56 # define ATTRIBUTE_NO_SANITIZE_ADDRESS 57 # endif 58 #endif // ATTRIBUTE_NO_SANITIZE_ADDRESS 59 60 #if __LP64__ || defined(_WIN64) 61 # define SANITIZER_WORDSIZE 64 62 #else 63 # define SANITIZER_WORDSIZE 32 64 #endif 65 66 // Make the compiler thinks that something is going on there. 67 inline void break_optimization(void *arg) { 68 #if !defined(_WIN32) || defined(__clang__) 69 __asm__ __volatile__("" : : "r" (arg) : "memory"); 70 #endif 71 } 72 73 // This function returns its parameter but in such a way that compiler 74 // can not prove it. 75 template<class T> 76 NOINLINE 77 static T Ident(T t) { 78 T ret = t; 79 break_optimization(&ret); 80 return ret; 81 } 82 83 // Simple stand-alone pseudorandom number generator. 84 // Current algorithm is ANSI C linear congruential PRNG. 85 static inline uint32_t my_rand_r(uint32_t* state) { 86 return (*state = *state * 1103515245 + 12345) >> 16; 87 } 88 89 static uint32_t global_seed = 0; 90 91 static inline uint32_t my_rand() { 92 return my_rand_r(&global_seed); 93 } 94 95 // Set availability of platform-specific functions. 96 97 #if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(_WIN32) 98 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 1 99 #else 100 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0 101 #endif 102 103 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__ANDROID__) && \ 104 !defined(__NetBSD__) && !defined(_WIN32) 105 # define SANITIZER_TEST_HAS_MEMALIGN 1 106 #else 107 # define SANITIZER_TEST_HAS_MEMALIGN 0 108 #endif 109 110 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__ANDROID__) && \ 111 !defined(__NetBSD__) && !defined(_WIN32) && \ 112 !(defined(__sun__) && defined(__svr4__)) 113 # define SANITIZER_TEST_HAS_PVALLOC 1 114 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 1 115 #else 116 # define SANITIZER_TEST_HAS_PVALLOC 0 117 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0 118 #endif 119 120 #if !defined(__APPLE__) 121 # define SANITIZER_TEST_HAS_STRNLEN 1 122 #else 123 # define SANITIZER_TEST_HAS_STRNLEN 0 124 #endif 125 126 #if defined(__FreeBSD__) || defined(__NetBSD__) 127 # define SANITIZER_TEST_HAS_PRINTF_L 1 128 #else 129 # define SANITIZER_TEST_HAS_PRINTF_L 0 130 #endif 131 132 #if !defined(_MSC_VER) 133 # define SANITIZER_TEST_HAS_STRNDUP 1 134 #else 135 # define SANITIZER_TEST_HAS_STRNDUP 0 136 #endif 137 138 #endif // SANITIZER_TEST_UTILS_H 139