1 //===-- sanitizer_test_utils.h ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of *Sanitizer runtime.
11 // Common unit tests utilities.
13 //===----------------------------------------------------------------------===//
15 #ifndef SANITIZER_TEST_UTILS_H
16 #define SANITIZER_TEST_UTILS_H
19 // <windows.h> should always be the first include on Windows.
21 // MSVS headers define max/min as macros, so std::max/min gets crazy.
29 # define NOINLINE __declspec(noinline)
30 #else // defined(_MSC_VER)
31 # define NOINLINE __attribute__((noinline))
32 #endif // defined(_MSC_VER)
34 #if !defined(_MSC_VER) || defined(__clang__)
35 # define UNUSED __attribute__((unused))
36 # define USED __attribute__((used))
42 #if !defined(__has_feature)
43 #define __has_feature(x) 0
46 #ifndef ATTRIBUTE_NO_SANITIZE_ADDRESS
47 # if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
48 # define ATTRIBUTE_NO_SANITIZE_ADDRESS \
49 __attribute__((no_sanitize_address))
51 # define ATTRIBUTE_NO_SANITIZE_ADDRESS
53 #endif // ATTRIBUTE_NO_SANITIZE_ADDRESS
55 #if __LP64__ || defined(_WIN64)
56 # define SANITIZER_WORDSIZE 64
58 # define SANITIZER_WORDSIZE 32
61 // Make the compiler thinks that something is going on there.
62 inline void break_optimization(void *arg
) {
63 #if !defined(_WIN32) || defined(__clang__)
64 __asm__
__volatile__("" : : "r" (arg
) : "memory");
68 // This function returns its parameter but in such a way that compiler
74 break_optimization(&ret
);
78 // Simple stand-alone pseudorandom number generator.
79 // Current algorithm is ANSI C linear congruential PRNG.
80 static inline uint32_t my_rand_r(uint32_t* state
) {
81 return (*state
= *state
* 1103515245 + 12345) >> 16;
84 static uint32_t global_seed
= 0;
86 static inline uint32_t my_rand() {
87 return my_rand_r(&global_seed
);
90 // Set availability of platform-specific functions.
92 #if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__) && !defined(_WIN32)
93 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 1
95 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0
98 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(_WIN32)
99 # define SANITIZER_TEST_HAS_MEMALIGN 1
100 # define SANITIZER_TEST_HAS_PVALLOC 1
101 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 1
103 # define SANITIZER_TEST_HAS_MEMALIGN 0
104 # define SANITIZER_TEST_HAS_PVALLOC 0
105 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0
108 #if !defined(__APPLE__)
109 # define SANITIZER_TEST_HAS_STRNLEN 1
111 # define SANITIZER_TEST_HAS_STRNLEN 0
114 #endif // SANITIZER_TEST_UTILS_H