1 //===-- sanitizer_test_utils.h ----------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of *Sanitizer runtime.
9 // Common unit tests utilities.
11 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_TEST_UTILS_H
14 #define SANITIZER_TEST_UTILS_H
17 // <windows.h> should always be the first include on Windows.
19 // MSVS headers define max/min as macros, so std::max/min gets crazy.
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
33 # define NOINLINE __declspec(noinline)
34 #else // defined(_MSC_VER)
35 # define NOINLINE __attribute__((noinline))
36 #endif // defined(_MSC_VER)
38 #if !defined(_MSC_VER) || defined(__clang__)
39 # define UNUSED __attribute__((unused))
40 # define USED __attribute__((used))
46 #if !defined(__has_feature)
47 #define __has_feature(x) 0
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))
55 # define ATTRIBUTE_NO_SANITIZE_ADDRESS
57 #endif // ATTRIBUTE_NO_SANITIZE_ADDRESS
59 #if __LP64__ || defined(_WIN64)
60 # define SANITIZER_WORDSIZE 64
62 # define SANITIZER_WORDSIZE 32
65 // Make the compiler thinks that something is going on there.
66 inline void break_optimization(void *arg
) {
67 #if !defined(_WIN32) || defined(__clang__)
68 __asm__
__volatile__("" : : "r" (arg
) : "memory");
72 // This function returns its parameter but in such a way that compiler
78 break_optimization(&ret
);
82 // Simple stand-alone pseudorandom number generator.
83 // Current algorithm is ANSI C linear congruential PRNG.
84 static inline uint32_t my_rand_r(uint32_t* state
) {
85 return (*state
= *state
* 1103515245 + 12345) >> 16;
88 static uint32_t global_seed
= 0;
90 static inline uint32_t my_rand() {
91 return my_rand_r(&global_seed
);
94 // Set availability of platform-specific functions.
96 #if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__) && !defined(_WIN32)
97 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 1
99 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0
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
107 # define SANITIZER_TEST_HAS_MEMALIGN 0
108 # define SANITIZER_TEST_HAS_PVALLOC 0
109 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0
112 #if !defined(__APPLE__)
113 # define SANITIZER_TEST_HAS_STRNLEN 1
115 # define SANITIZER_TEST_HAS_STRNLEN 0
118 #endif // SANITIZER_TEST_UTILS_H