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
172d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#if !defined(SANITIZER_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
23c74b2ccbc0cf40bb401ebf3758e8e964b22acb3aKostya Serebryany#include "sanitizer_test_utils.h"
242d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#include "sanitizer_pthread_wrappers.h"
252d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
2638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <stdio.h>
2738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <signal.h>
2838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <stdlib.h>
2938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <string.h>
3038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <stdint.h>
3138db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <assert.h>
3238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <algorithm>
332d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines
342d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#if !defined(_WIN32)
352d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines# include <strings.h>
362d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines# include <sys/mman.h>
372d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines# include <setjmp.h>
382d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#endif
3938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
4038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#ifdef __linux__
4138db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <sys/prctl.h>
4238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <sys/types.h>
4338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <sys/stat.h>
4438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# include <fcntl.h>
4538db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <unistd.h>
4638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#endif
4738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
482d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines#if !defined(__APPLE__) && !defined(__FreeBSD__)
4938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#include <malloc.h>
5038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#endif
511e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany
5238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#if ASAN_HAS_EXCEPTIONS
5338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# define ASAN_THROW(x) throw (x)
5438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#else
5538db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany# define ASAN_THROW(x)
5638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany#endif
5738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
5838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint8_t   U1;
5938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint16_t  U2;
6038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint32_t  U4;
6138db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytypedef uint64_t  U8;
6238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
6338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanystatic const int kPageSize = 4096;
6438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
6538db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanyconst size_t kLargeMalloc = 1 << 24;
6638db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
6738db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanyextern void free_aaa(void *p);
6838db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanyextern void *malloc_aaa(size_t size);
6938db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
7038db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryanytemplate<typename T>
7138db30686c5962f8b5c877e29c6669d72198d42bKostya SerebryanyNOINLINE void asan_write(T *a) {
7238db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany  *a = 0;
7338db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany}
7438db30686c5962f8b5c877e29c6669d72198d42bKostya Serebryany
756cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring RightOOBErrorMessage(int oob_distance, bool is_write);
766cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring RightOOBWriteMessage(int oob_distance);
776cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring RightOOBReadMessage(int oob_distance);
786cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBErrorMessage(int oob_distance, bool is_write);
796cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBWriteMessage(int oob_distance);
806cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBReadMessage(int oob_distance);
816cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanystring LeftOOBAccessMessage(int oob_distance);
826cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanychar* MallocAndMemsetString(size_t size, char ch);
836cbfae439b81221d2250ffe0331958f130259755Kostya Serebryanychar* MallocAndMemsetString(size_t size);
846cbfae439b81221d2250ffe0331958f130259755Kostya Serebryany
85a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob1[1];
86a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob2[2];
87a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob3[3];
88a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob4[4];
89a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob5[5];
90a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob6[6];
91a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob7[7];
92a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob8[8];
93a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob9[9];
94a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob10[10];
95a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob11[11];
96a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob12[12];
97a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob13[13];
98a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob14[14];
99a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob15[15];
100a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob16[16];
101a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern char glob17[17];
102af5bd2ee59a9f7bea74f43fccc6ea48979b7a4b0Kostya Serebryanyextern char glob1000[1000];
103af5bd2ee59a9f7bea74f43fccc6ea48979b7a4b0Kostya Serebryanyextern char glob10000[10000];
104af5bd2ee59a9f7bea74f43fccc6ea48979b7a4b0Kostya Serebryanyextern char glob100000[100000];
105a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryanyextern int GlobalsTest(int x);
106a3b0e5e4f9f48b2ed0baee10c0236eda7c21c660Kostya Serebryany
1071e172b4bdec57329bf904f063a29f99cddf2d85fKostya Serebryany#endif  // ASAN_TEST_UTILS_H
108