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