sanitizer_test_utils.h revision 31a5cd83406be83bcdb91df03e16816e3af8c615
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.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef SANITIZER_TEST_UTILS_H
16#define SANITIZER_TEST_UTILS_H
17
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
25
26#if !defined(SANITIZER_EXTERNAL_TEST_CONFIG)
27# define INCLUDED_FROM_SANITIZER_TEST_UTILS_H
28# include "sanitizer_test_config.h"
29# undef INCLUDED_FROM_SANITIZER_TEST_UTILS_H
30#endif
31
32#include <stdint.h>
33
34#if defined(_MSC_VER)
35# define NOINLINE __declspec(noinline)
36#else  // defined(_MSC_VER)
37# define NOINLINE __attribute__((noinline))
38#endif  // defined(_MSC_VER)
39
40#if !defined(_MSC_VER) || defined(__clang__)
41# define UNUSED __attribute__((unused))
42# define USED __attribute__((used))
43#else
44# define UNUSED
45# define USED
46#endif
47
48#if !defined(__has_feature)
49#define __has_feature(x) 0
50#endif
51
52#ifndef ATTRIBUTE_NO_SANITIZE_ADDRESS
53# if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
54#  define ATTRIBUTE_NO_SANITIZE_ADDRESS \
55    __attribute__((no_sanitize_address))
56# else
57#  define ATTRIBUTE_NO_SANITIZE_ADDRESS
58# endif
59#endif  // ATTRIBUTE_NO_SANITIZE_ADDRESS
60
61#if __LP64__ || defined(_WIN64)
62#  define SANITIZER_WORDSIZE 64
63#else
64#  define SANITIZER_WORDSIZE 32
65#endif
66
67// Make the compiler thinks that something is going on there.
68inline void break_optimization(void *arg) {
69#if !defined(_WIN32) || defined(__clang__)
70  __asm__ __volatile__("" : : "r" (arg) : "memory");
71#endif
72}
73
74// This function returns its parameter but in such a way that compiler
75// can not prove it.
76template<class T>
77NOINLINE
78static T Ident(T t) {
79  T ret = t;
80  break_optimization(&ret);
81  return ret;
82}
83
84// Simple stand-alone pseudorandom number generator.
85// Current algorithm is ANSI C linear congruential PRNG.
86static inline uint32_t my_rand_r(uint32_t* state) {
87  return (*state = *state * 1103515245 + 12345) >> 16;
88}
89
90static uint32_t global_seed = 0;
91
92static inline uint32_t my_rand() {
93  return my_rand_r(&global_seed);
94}
95
96// Set availability of platform-specific functions.
97
98#if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__) && !defined(_WIN32)
99# define SANITIZER_TEST_HAS_POSIX_MEMALIGN 1
100#else
101# define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0
102#endif
103
104#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(ANDROID) && \
105    !defined(__ANDROID__) && !defined(_WIN32)
106# define SANITIZER_TEST_HAS_MEMALIGN 1
107# define SANITIZER_TEST_HAS_PVALLOC 1
108# define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 1
109#else
110# define SANITIZER_TEST_HAS_MEMALIGN 0
111# define SANITIZER_TEST_HAS_PVALLOC 0
112# define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0
113#endif
114
115#if !defined(__APPLE__)
116# define SANITIZER_TEST_HAS_STRNLEN 1
117#else
118# define SANITIZER_TEST_HAS_STRNLEN 0
119#endif
120
121#endif  // SANITIZER_TEST_UTILS_H
122