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
21 # define NOINLINE __declspec(noinline)
23 #else // defined(_WIN32)
24 # define NOINLINE __attribute__((noinline))
25 # define USED __attribute__((used))
26 #endif // defined(_WIN32)
28 #if !defined(__has_feature)
29 #define __has_feature(x) 0
32 #ifndef ATTRIBUTE_NO_SANITIZE_ADDRESS
33 # if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
34 # define ATTRIBUTE_NO_SANITIZE_ADDRESS \
35 __attribute__((no_sanitize_address))
37 # define ATTRIBUTE_NO_SANITIZE_ADDRESS
39 #endif // ATTRIBUTE_NO_SANITIZE_ADDRESS
41 #if __LP64__ || defined(_WIN64)
42 # define SANITIZER_WORDSIZE 64
44 # define SANITIZER_WORDSIZE 32
47 // Make the compiler thinks that something is going on there.
48 inline void break_optimization(void *arg
) {
49 #if !defined(_WIN32) || defined(__clang__)
50 __asm__
__volatile__("" : : "r" (arg
) : "memory");
54 // This function returns its parameter but in such a way that compiler
60 break_optimization(&ret
);
64 // Simple stand-alone pseudorandom number generator.
65 // Current algorithm is ANSI C linear congruential PRNG.
66 static inline uint32_t my_rand_r(uint32_t* state
) {
67 return (*state
= *state
* 1103515245 + 12345) >> 16;
70 static uint32_t global_seed
= 0;
72 static inline uint32_t my_rand() {
73 return my_rand_r(&global_seed
);
76 // Set availability of platform-specific functions.
78 #if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__) && !defined(_WIN32)
79 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 1
81 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0
84 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(_WIN32)
85 # define SANITIZER_TEST_HAS_MEMALIGN 1
86 # define SANITIZER_TEST_HAS_PVALLOC 1
87 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 1
89 # define SANITIZER_TEST_HAS_MEMALIGN 0
90 # define SANITIZER_TEST_HAS_PVALLOC 0
91 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0
94 #if !defined(__APPLE__)
95 # define SANITIZER_TEST_HAS_STRNLEN 1
97 # define SANITIZER_TEST_HAS_STRNLEN 0
100 #endif // SANITIZER_TEST_UTILS_H