[Sanitizer tests] Make simple pthread tests compile and pass on Windows
[blocksruntime.git] / lib / sanitizer_common / tests / sanitizer_test_utils.h
bloba0c75c921ce4fc8f52a0e4a698b51fdebba43ae9
1 //===-- sanitizer_test_utils.h ----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
18 #if defined(_WIN32)
19 // <windows.h> should always be the first include on Windows.
20 # include <windows.h>
21 // MSVS headers define max/min as macros, so std::max/min gets crazy.
22 # undef max
23 # undef min
24 #endif
26 #include <stdint.h>
28 #if defined(_MSC_VER)
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))
37 #else
38 # define UNUSED
39 # define USED
40 #endif
42 #if !defined(__has_feature)
43 #define __has_feature(x) 0
44 #endif
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))
50 # else
51 # define ATTRIBUTE_NO_SANITIZE_ADDRESS
52 # endif
53 #endif // ATTRIBUTE_NO_SANITIZE_ADDRESS
55 #if __LP64__ || defined(_WIN64)
56 # define SANITIZER_WORDSIZE 64
57 #else
58 # define SANITIZER_WORDSIZE 32
59 #endif
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");
65 #endif
68 // This function returns its parameter but in such a way that compiler
69 // can not prove it.
70 template<class T>
71 NOINLINE
72 static T Ident(T t) {
73 T ret = t;
74 break_optimization(&ret);
75 return 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
94 #else
95 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0
96 #endif
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
102 #else
103 # define SANITIZER_TEST_HAS_MEMALIGN 0
104 # define SANITIZER_TEST_HAS_PVALLOC 0
105 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0
106 #endif
108 #if !defined(__APPLE__)
109 # define SANITIZER_TEST_HAS_STRNLEN 1
110 #else
111 # define SANITIZER_TEST_HAS_STRNLEN 0
112 #endif
114 #endif // SANITIZER_TEST_UTILS_H