sanitizer_test_utils.h revision 00b8a34250e9f6c1c0211c33f4fe4da15d0fb24a
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)
19typedef unsigned __int8  uint8_t;
20typedef unsigned __int16 uint16_t;
21typedef unsigned __int32 uint32_t;
22typedef unsigned __int64 uint64_t;
23typedef __int8           int8_t;
24typedef __int16          int16_t;
25typedef __int32          int32_t;
26typedef __int64          int64_t;
27# define NOINLINE __declspec(noinline)
28# define USED
29#else  // defined(_WIN32)
30# define NOINLINE __attribute__((noinline))
31# define USED __attribute__((used))
32#include <stdint.h>
33#endif  // defined(_WIN32)
34
35#if !defined(__has_feature)
36#define __has_feature(x) 0
37#endif
38
39#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
40# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \
41    __attribute__((no_sanitize_address))
42#else
43# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
44#endif
45
46#if __LP64__ || defined(_WIN64)
47#  define SANITIZER_WORDSIZE 64
48#else
49#  define SANITIZER_WORDSIZE 32
50#endif
51
52// Make the compiler thinks that something is going on there.
53inline void break_optimization(void *arg) {
54  __asm__ __volatile__("" : : "r" (arg) : "memory");
55}
56
57// This function returns its parameter but in such a way that compiler
58// can not prove it.
59template<class T>
60NOINLINE
61static T Ident(T t) {
62  T ret = t;
63  break_optimization(&ret);
64  return ret;
65}
66
67// Simple stand-alone pseudorandom number generator.
68// Current algorithm is ANSI C linear congruential PRNG.
69static inline uint32_t my_rand_r(uint32_t* state) {
70  return (*state = *state * 1103515245 + 12345) >> 16;
71}
72
73static uint32_t global_seed = 0;
74
75static inline uint32_t my_rand() {
76  return my_rand_r(&global_seed);
77}
78
79
80#endif  // SANITIZER_TEST_UTILS_H
81