1 //===-- sanitizer_test_utils.h ----------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of *Sanitizer runtime.
10 // Common unit tests utilities.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_TEST_UTILS_H
15 #define SANITIZER_TEST_UTILS_H
18 // <windows.h> should always be the first include on Windows.
20 // MSVS headers define max/min as macros, so std::max/min gets crazy.
25 #if !defined(SANITIZER_EXTERNAL_TEST_CONFIG)
26 # define INCLUDED_FROM_SANITIZER_TEST_UTILS_H
27 # include "sanitizer_test_config.h"
28 # undef INCLUDED_FROM_SANITIZER_TEST_UTILS_H
34 # define NOINLINE __declspec(noinline)
35 #else // defined(_MSC_VER)
36 # define NOINLINE __attribute__((noinline))
37 #endif // defined(_MSC_VER)
39 #if !defined(_MSC_VER) || defined(__clang__)
40 # define UNUSED __attribute__((unused))
41 # define USED __attribute__((used))
47 #if !defined(__has_feature)
48 #define __has_feature(x) 0
51 #ifndef ATTRIBUTE_NO_SANITIZE_ADDRESS
52 # if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
53 # define ATTRIBUTE_NO_SANITIZE_ADDRESS \
54 __attribute__((no_sanitize_address))
56 # define ATTRIBUTE_NO_SANITIZE_ADDRESS
58 #endif // ATTRIBUTE_NO_SANITIZE_ADDRESS
60 #if __LP64__ || defined(_WIN64)
61 # define SANITIZER_WORDSIZE 64
63 # define SANITIZER_WORDSIZE 32
66 // Make the compiler thinks that something is going on there.
67 inline void break_optimization(void *arg
) {
68 #if !defined(_WIN32) || defined(__clang__)
69 __asm__
__volatile__("" : : "r" (arg
) : "memory");
73 // This function returns its parameter but in such a way that compiler
79 break_optimization(&ret
);
83 // Simple stand-alone pseudorandom number generator.
84 // Current algorithm is ANSI C linear congruential PRNG.
85 static inline uint32_t my_rand_r(uint32_t* state
) {
86 return (*state
= *state
* 1103515245 + 12345) >> 16;
89 static uint32_t global_seed
= 0;
91 static inline uint32_t my_rand() {
92 return my_rand_r(&global_seed
);
95 // Set availability of platform-specific functions.
97 #if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(_WIN32)
98 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 1
100 # define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0
103 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__ANDROID__) && \
104 !defined(__NetBSD__) && !defined(_WIN32)
105 # define SANITIZER_TEST_HAS_MEMALIGN 1
107 # define SANITIZER_TEST_HAS_MEMALIGN 0
110 #if defined(__GLIBC__)
111 # define SANITIZER_TEST_HAS_PVALLOC 1
112 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 1
114 # define SANITIZER_TEST_HAS_PVALLOC 0
115 # define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0
118 #if !defined(__APPLE__)
119 # define SANITIZER_TEST_HAS_STRNLEN 1
121 # define SANITIZER_TEST_HAS_STRNLEN 0
124 #if defined(__FreeBSD__) || defined(__NetBSD__)
125 # define SANITIZER_TEST_HAS_PRINTF_L 1
127 # define SANITIZER_TEST_HAS_PRINTF_L 0
130 #if !defined(_MSC_VER)
131 # define SANITIZER_TEST_HAS_STRNDUP 1
133 # define SANITIZER_TEST_HAS_STRNDUP 0
136 #endif // SANITIZER_TEST_UTILS_H