asan_test_utils.h revision af5bd2ee59a9f7bea74f43fccc6ea48979b7a4b0
17da8503a90c7f84787aa1ba978e2223893fa7727Alexey Samsonov//===-- asan_test_utils.h ---------------------------------------*- C++ -*-===//
21e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
31e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//                     The LLVM Compiler Infrastructure
41e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
51e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// This file is distributed under the University of Illinois Open Source
61e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// License. See LICENSE.TXT for details.
71e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
81e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//===----------------------------------------------------------------------===//
91e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
101e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany// This file is a part of AddressSanitizer, an address sanity checker.
111e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//
121e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany//===----------------------------------------------------------------------===//
131e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
141e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#ifndef ASAN_TEST_UTILS_H
151e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#define ASAN_TEST_UTILS_H
161e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
177da8503a90c7f84787aa1ba978e2223893fa7727Alexey Samsonov#if !defined(ASAN_EXTERNAL_TEST_CONFIG)
187da8503a90c7f84787aa1ba978e2223893fa7727Alexey Samsonov# define INCLUDED_FROM_ASAN_TEST_UTILS_H
197da8503a90c7f84787aa1ba978e2223893fa7727Alexey Samsonov# include "asan_test_config.h"
207da8503a90c7f84787aa1ba978e2223893fa7727Alexey Samsonov# undef INCLUDED_FROM_ASAN_TEST_UTILS_H
217da8503a90c7f84787aa1ba978e2223893fa7727Alexey Samsonov#endif
227da8503a90c7f84787aa1ba978e2223893fa7727Alexey Samsonov
2348ddbef1d051875b2d0b204e8d78300b58d80a85Evgeniy Stepanov#include "sanitizer_common/tests/sanitizer_test_utils.h"
2438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <stdio.h>
2538db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <signal.h>
2638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <stdlib.h>
2738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <string.h>
2838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <strings.h>
2938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <pthread.h>
3038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <stdint.h>
3138db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <setjmp.h>
3238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <assert.h>
3338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <algorithm>
3438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <sys/mman.h>
3538db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
3638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#ifdef __linux__
3738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <sys/prctl.h>
3838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <sys/types.h>
3938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <sys/stat.h>
4038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <fcntl.h>
4138db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <unistd.h>
4238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#endif
4338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
4438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#if defined(__i386__) || defined(__x86_64__)
4538db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <emmintrin.h>
4638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#endif
4738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
4838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#ifndef __APPLE__
4938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <malloc.h>
5038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#endif
511e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
522697687059e64a4f1319dc23a0a3ca59982d53f6Kostya Serebryany// Check that pthread_create/pthread_join return success.
53e2770623379f403ea8c0247cac9ce8c75167a886Kostya Serebryany#define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
54e2770623379f403ea8c0247cac9ce8c75167a886Kostya Serebryany#define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
552697687059e64a4f1319dc23a0a3ca59982d53f6Kostya Serebryany
5638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#if ASAN_HAS_EXCEPTIONS
5738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# define ASAN_THROW(x) throw (x)
5838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#else
5938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# define ASAN_THROW(x)
6038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#endif
6138db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
6238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint8_t   U1;
6338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint16_t  U2;
6438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint32_t  U4;
6538db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint64_t  U8;
6638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
6738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanystatic const int kPageSize = 4096;
6838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
6938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanyconst size_t kLargeMalloc = 1 << 24;
7038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
7138db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanyextern void free_aaa(void *p);
7238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanyextern void *malloc_aaa(size_t size);
7338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
7438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytemplate<typename T>
7538db30686c5962f8b5c877e29c6669d72198d42bKostya SerebryanyNOINLINE void asan_write(T *a) {
7638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany  *a = 0;
7738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany}
7838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
796cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring RightOOBErrorMessage(int oob_distance, bool is_write);
806cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring RightOOBWriteMessage(int oob_distance);
816cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring RightOOBReadMessage(int oob_distance);
826cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBErrorMessage(int oob_distance, bool is_write);
836cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBWriteMessage(int oob_distance);
846cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBReadMessage(int oob_distance);
856cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBAccessMessage(int oob_distance);
866cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanychar* MallocAndMemsetString(size_t size, char ch);
876cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanychar* MallocAndMemsetString(size_t size);
886cbfae439b81221d2250ffe0331958f130259755Kostya Serebryany
89a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob1[1];
90a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob2[2];
91a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob3[3];
92a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob4[4];
93a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob5[5];
94a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob6[6];
95a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob7[7];
96a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob8[8];
97a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob9[9];
98a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob10[10];
99a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob11[11];
100a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob12[12];
101a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob13[13];
102a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob14[14];
103a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob15[15];
104a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob16[16];
105a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob17[17];
106af5bd2ee59a9f7bea74f43fccc6ea48979b7a4b0Kostya Serebryanyextern char glob1000[1000];
107af5bd2ee59a9f7bea74f43fccc6ea48979b7a4b0Kostya Serebryanyextern char glob10000[10000];
108af5bd2ee59a9f7bea74f43fccc6ea48979b7a4b0Kostya Serebryanyextern char glob100000[100000];
109a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern int GlobalsTest(int x);
110a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryany
1111e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#endif  // ASAN_TEST_UTILS_H
112