[ASan] Update sanitizer_common and asan test_util headers to support building on...
[blocksruntime.git] / lib / sanitizer_common / tests / sanitizer_test_utils.h
blobdabd61f1ddbe4f4671875156b845ae4a57ce76a3
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 #include <stdint.h>
20 #if defined(_WIN32)
21 # define NOINLINE __declspec(noinline)
22 # define USED
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
30 #endif
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))
36 # else
37 # define ATTRIBUTE_NO_SANITIZE_ADDRESS
38 # endif
39 #endif // ATTRIBUTE_NO_SANITIZE_ADDRESS
41 #if __LP64__ || defined(_WIN64)
42 # define SANITIZER_WORDSIZE 64
43 #else
44 # define SANITIZER_WORDSIZE 32
45 #endif
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");
51 #endif
54 // This function returns its parameter but in such a way that compiler
55 // can not prove it.
56 template<class T>
57 NOINLINE
58 static T Ident(T t) {
59 T ret = t;
60 break_optimization(&ret);
61 return 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
80 #else
81 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0
82 #endif
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
88 #else
89 # define SANITIZER_TEST_HAS_MEMALIGN 0
90 # define SANITIZER_TEST_HAS_PVALLOC 0
91 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0
92 #endif
94 #if !defined(__APPLE__)
95 # define SANITIZER_TEST_HAS_STRNLEN 1
96 #else
97 # define SANITIZER_TEST_HAS_STRNLEN 0
98 #endif
100 #endif // SANITIZER_TEST_UTILS_H