brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · 5fd94a0 Raw
137 lines · c
1//===-- sanitizer_test_utils.h ----------------------------------*- C++ -*-===//2//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of *Sanitizer runtime.10// Common unit tests utilities.11//12//===----------------------------------------------------------------------===//13 14#ifndef SANITIZER_TEST_UTILS_H15#define SANITIZER_TEST_UTILS_H16 17#if defined(_WIN32)18// <windows.h> should always be the first include on Windows.19# include <windows.h>20// MSVS headers define max/min as macros, so std::max/min gets crazy.21# undef max22# undef min23#endif24 25#if !defined(SANITIZER_EXTERNAL_TEST_CONFIG)26# define INCLUDED_FROM_SANITIZER_TEST_UTILS_H27# include "sanitizer_test_config.h"28# undef INCLUDED_FROM_SANITIZER_TEST_UTILS_H29#endif30 31#include <stdint.h>32 33#if defined(_MSC_VER)34# define NOINLINE __declspec(noinline)35#else  // defined(_MSC_VER)36# define NOINLINE __attribute__((noinline))37#endif  // defined(_MSC_VER)38 39#if !defined(_MSC_VER) || defined(__clang__)40# define UNUSED __attribute__((unused))41# define USED __attribute__((used))42#else43# define UNUSED44# define USED45#endif46 47#if !defined(__has_feature)48#define __has_feature(x) 049#endif50 51#ifndef ATTRIBUTE_NO_SANITIZE_ADDRESS52# if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)53#  define ATTRIBUTE_NO_SANITIZE_ADDRESS \54    __attribute__((no_sanitize_address))55# else56#  define ATTRIBUTE_NO_SANITIZE_ADDRESS57# endif58#endif  // ATTRIBUTE_NO_SANITIZE_ADDRESS59 60#if __LP64__ || defined(_WIN64)61#  define SANITIZER_WORDSIZE 6462#else63#  define SANITIZER_WORDSIZE 3264#endif65 66// Make the compiler thinks that something is going on there.67inline void break_optimization(void *arg) {68#if !defined(_WIN32) || defined(__clang__)69  __asm__ __volatile__("" : : "r" (arg) : "memory");70#endif71}72 73// This function returns its parameter but in such a way that compiler74// can not prove it.75template<class T>76NOINLINE77static T Ident(T t) {78  T ret = t;79  break_optimization(&ret);80  return ret;81}82 83// Simple stand-alone pseudorandom number generator.84// Current algorithm is ANSI C linear congruential PRNG.85static inline uint32_t my_rand_r(uint32_t* state) {86  return (*state = *state * 1103515245 + 12345) >> 16;87}88 89static uint32_t global_seed = 0;90 91static inline uint32_t my_rand() {92  return my_rand_r(&global_seed);93}94 95// Set availability of platform-specific functions.96 97#if !defined(__APPLE__) && !defined(__ANDROID__) && !defined(_WIN32)98# define SANITIZER_TEST_HAS_POSIX_MEMALIGN 199#else100# define SANITIZER_TEST_HAS_POSIX_MEMALIGN 0101#endif102 103#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__ANDROID__) && \104    !defined(__NetBSD__) && !defined(_WIN32)105# define SANITIZER_TEST_HAS_MEMALIGN 1106#else107# define SANITIZER_TEST_HAS_MEMALIGN 0108#endif109 110#if defined(__GLIBC__)111# define SANITIZER_TEST_HAS_PVALLOC 1112# define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 1113#else114# define SANITIZER_TEST_HAS_PVALLOC 0115# define SANITIZER_TEST_HAS_MALLOC_USABLE_SIZE 0116#endif117 118#if !defined(__APPLE__)119# define SANITIZER_TEST_HAS_STRNLEN 1120#else121# define SANITIZER_TEST_HAS_STRNLEN 0122#endif123 124#if defined(__FreeBSD__) || defined(__NetBSD__)125# define SANITIZER_TEST_HAS_PRINTF_L 1126#else127# define SANITIZER_TEST_HAS_PRINTF_L 0128#endif129 130#if !defined(_WIN32)131#  define SANITIZER_TEST_HAS_STRNDUP 1132#else133# define SANITIZER_TEST_HAS_STRNDUP 0134#endif135 136#endif  // SANITIZER_TEST_UTILS_H137