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