asan_test.cc revision a390ece58317a25ef26866cef6753df4b06a3e65
1b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//===-- asan_test.cc ------------------------------------------------------===// 2b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// 3b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// The LLVM Compiler Infrastructure 4b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// 5b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// This file is distributed under the University of Illinois Open Source 6b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// License. See LICENSE.TXT for details. 7b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// 8b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//===----------------------------------------------------------------------===// 9b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// 10b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// This file is a part of AddressSanitizer, an address sanity checker. 11eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov// 12b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov//===----------------------------------------------------------------------===// 13b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <stdio.h> 14b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <signal.h> 15eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov#include <stdlib.h> 16b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <string.h> 17b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <strings.h> 18b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <pthread.h> 19b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <stdint.h> 20b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <setjmp.h> 21b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <assert.h> 22b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 23b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifdef __linux__ 24b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov# include <sys/prctl.h> 25b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif 26b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 27b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#if defined(__i386__) || defined(__x86_64__) 28b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <emmintrin.h> 29b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif 30b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 31b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include "asan_test_utils.h" 32b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 33b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef __APPLE__ 34b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <malloc.h> 35b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#else 36b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <malloc/malloc.h> 37b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <AvailabilityMacros.h> // For MAC_OS_X_VERSION_* 38b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <CoreFoundation/CFString.h> 39b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif // __APPLE__ 40b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 41b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#if ASAN_HAS_EXCEPTIONS 42b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov# define ASAN_THROW(x) throw (x) 43b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#else 44b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov# define ASAN_THROW(x) 45b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif 46b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 47b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#include <sys/mman.h> 48b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 49b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef uint8_t U1; 50b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef uint16_t U2; 51b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef uint32_t U4; 52b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtypedef uint64_t U8; 53b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 54b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstatic const int kPageSize = 4096; 55b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 56b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// Simple stand-alone pseudorandom number generator. 57b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov// Current algorithm is ANSI C linear congruential PRNG. 58b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstatic inline uint32_t my_rand(uint32_t* state) { 59b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov return (*state = *state * 1103515245 + 12345) >> 16; 60b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 61b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 62b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovstatic uint32_t global_seed = 0; 63b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 64b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovconst size_t kLargeMalloc = 1 << 24; 65b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 66b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtemplate<typename T> 67b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void asan_write(T *a) { 68b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov *a = 0; 69b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 70b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 71b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void asan_write_sized_aligned(uint8_t *p, size_t size) { 72b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_EQ(0U, ((uintptr_t)p % size)); 73b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov if (size == 1) asan_write((uint8_t*)p); 74b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov else if (size == 2) asan_write((uint16_t*)p); 75b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov else if (size == 4) asan_write((uint32_t*)p); 76b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov else if (size == 8) asan_write((uint64_t*)p); 77b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 78b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 79b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *malloc_fff(size_t size) { 80b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = malloc/**/(size); break_optimization(0); return res;} 81b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *malloc_eee(size_t size) { 82b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = malloc_fff(size); break_optimization(0); return res;} 83b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *malloc_ddd(size_t size) { 84b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = malloc_eee(size); break_optimization(0); return res;} 85b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *malloc_ccc(size_t size) { 86b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = malloc_ddd(size); break_optimization(0); return res;} 87b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *malloc_bbb(size_t size) { 88b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = malloc_ccc(size); break_optimization(0); return res;} 89b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *malloc_aaa(size_t size) { 90b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = malloc_bbb(size); break_optimization(0); return res;} 91b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 92b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef __APPLE__ 93b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *memalign_fff(size_t alignment, size_t size) { 94b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = memalign/**/(alignment, size); break_optimization(0); return res;} 95b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *memalign_eee(size_t alignment, size_t size) { 96b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = memalign_fff(alignment, size); break_optimization(0); return res;} 97b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *memalign_ddd(size_t alignment, size_t size) { 98b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = memalign_eee(alignment, size); break_optimization(0); return res;} 99b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *memalign_ccc(size_t alignment, size_t size) { 100b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = memalign_ddd(alignment, size); break_optimization(0); return res;} 101b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *memalign_bbb(size_t alignment, size_t size) { 102b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = memalign_ccc(alignment, size); break_optimization(0); return res;} 103b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void *memalign_aaa(size_t alignment, size_t size) { 104b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *res = memalign_bbb(alignment, size); break_optimization(0); return res;} 105b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif // __APPLE__ 106b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 107b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 108b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void free_ccc(void *p) { free(p); break_optimization(0);} 109b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void free_bbb(void *p) { free_ccc(p); break_optimization(0);} 110b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void free_aaa(void *p) { free_bbb(p); break_optimization(0);} 111b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 112b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtemplate<typename T> 113b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void oob_test(int size, int off) { 114b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov char *p = (char*)malloc_aaa(size); 115b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov // fprintf(stderr, "writing %d byte(s) into [%p,%p) with offset %d\n", 116b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov // sizeof(T), p, p + size, off); 117b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov asan_write((T*)(p + off)); 118b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free_aaa(p); 119b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 120b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 121b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 122b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanovtemplate<typename T> 123b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovNOINLINE void uaf_test(int size, int off) { 124b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov char *p = (char *)malloc_aaa(size); 125b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free_aaa(p); 126b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov for (int i = 1; i < 100; i++) 127b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free_aaa(malloc_aaa(i)); 128b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov fprintf(stderr, "writing %ld byte(s) at %p with offset %d\n", 129b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov (long)sizeof(T), p, off); 130b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov asan_write((T*)(p + off)); 131b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 132b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 133b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovTEST(AddressSanitizer, HasFeatureAddressSanitizerTest) { 134b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#if defined(__has_feature) && __has_feature(address_sanitizer) 135b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov bool asan = 1; 136eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov#elif defined(__SANITIZE_ADDRESS__) 137b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov bool asan = 1; 138b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#else 139b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov bool asan = 0; 140b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif 141b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_EQ(true, asan); 142b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 143b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 144b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovTEST(AddressSanitizer, SimpleDeathTest) { 145b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_DEATH(exit(1), ""); 146b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 147b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 148b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovTEST(AddressSanitizer, VariousMallocsTest) { 149b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int *a = (int*)malloc(100 * sizeof(int)); 150b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov a[50] = 0; 151b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free(a); 152b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 153b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int *r = (int*)malloc(10); 154b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov r = (int*)realloc(r, 2000 * sizeof(int)); 155b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov r[1000] = 0; 156b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free(r); 157b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 158b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int *b = new int[100]; 159b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov b[50] = 0; 160b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov delete [] b; 161b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 162b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int *c = new int; 163b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov *c = 0; 164b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov delete c; 165b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 166b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__) 167b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int *pm; 168b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int pm_res = posix_memalign((void**)&pm, kPageSize, kPageSize); 169b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_EQ(0, pm_res); 170b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free(pm); 171b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif 172b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 173b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#if !defined(__APPLE__) 174b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int *ma = (int*)memalign(kPageSize, kPageSize); 175b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_EQ(0U, (uintptr_t)ma % kPageSize); 176b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov ma[123] = 0; 177b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free(ma); 178b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#endif // __APPLE__ 179b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 180b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 181b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovTEST(AddressSanitizer, CallocTest) { 182b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov int *a = (int*)calloc(100, sizeof(int)); 183b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_EQ(0, a[10]); 184b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free(a); 185b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 186b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 187b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovTEST(AddressSanitizer, VallocTest) { 188b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov void *a = valloc(100); 189b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 190b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov free(a); 191b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov} 192b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov 193b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov#ifndef __APPLE__ 194b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy StepanovTEST(AddressSanitizer, PvallocTest) { 195b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov char *a = (char*)pvalloc(kPageSize + 100); 196b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 197eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov a[kPageSize + 101] = 1; // we should not report an error here. 198eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov free(a); 199eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov 200eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov a = (char*)pvalloc(0); // pvalloc(0) should allocate at least one page. 201eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 202eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov a[101] = 1; // we should not report an error here. 203eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov free(a); 204eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov} 205eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov#endif // __APPLE__ 206eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov 207eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanovvoid *TSDWorker(void *test_key) { 208eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov if (test_key) { 209eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov pthread_setspecific(*(pthread_key_t*)test_key, (void*)0xfeedface); 210eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov } 211eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov return NULL; 212eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov} 213eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov 214eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanovvoid TSDDestructor(void *tsd) { 215eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov // Spawning a thread will check that the current thread id is not -1. 216eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov pthread_t th; 217eb0bae136f4eeaaf29761dddb148b118fb824632Dmitriy Ivanov PTHREAD_CREATE(&th, NULL, TSDWorker, NULL); 218 PTHREAD_JOIN(th, NULL); 219} 220 221// This tests triggers the thread-specific data destruction fiasco which occurs 222// if we don't manage the TSD destructors ourselves. We create a new pthread 223// key with a non-NULL destructor which is likely to be put after the destructor 224// of AsanThread in the list of destructors. 225// In this case the TSD for AsanThread will be destroyed before TSDDestructor 226// is called for the child thread, and a CHECK will fail when we call 227// pthread_create() to spawn the grandchild. 228TEST(AddressSanitizer, DISABLED_TSDTest) { 229 pthread_t th; 230 pthread_key_t test_key; 231 pthread_key_create(&test_key, TSDDestructor); 232 PTHREAD_CREATE(&th, NULL, TSDWorker, &test_key); 233 PTHREAD_JOIN(th, NULL); 234 pthread_key_delete(test_key); 235} 236 237template<typename T> 238void OOBTest() { 239 char expected_str[100]; 240 for (int size = sizeof(T); size < 20; size += 5) { 241 for (int i = -5; i < 0; i++) { 242 const char *str = 243 "is located.*%d byte.*to the left"; 244 sprintf(expected_str, str, abs(i)); 245 EXPECT_DEATH(oob_test<T>(size, i), expected_str); 246 } 247 248 for (int i = 0; i < (int)(size - sizeof(T) + 1); i++) 249 oob_test<T>(size, i); 250 251 for (int i = size - sizeof(T) + 1; i <= (int)(size + 3 * sizeof(T)); i++) { 252 const char *str = 253 "is located.*%d byte.*to the right"; 254 int off = i >= size ? (i - size) : 0; 255 // we don't catch unaligned partially OOB accesses. 256 if (i % sizeof(T)) continue; 257 sprintf(expected_str, str, off); 258 EXPECT_DEATH(oob_test<T>(size, i), expected_str); 259 } 260 } 261 262 EXPECT_DEATH(oob_test<T>(kLargeMalloc, -1), 263 "is located.*1 byte.*to the left"); 264 EXPECT_DEATH(oob_test<T>(kLargeMalloc, kLargeMalloc), 265 "is located.*0 byte.*to the right"); 266} 267 268// TODO(glider): the following tests are EXTREMELY slow on Darwin: 269// AddressSanitizer.OOB_char (125503 ms) 270// AddressSanitizer.OOB_int (126890 ms) 271// AddressSanitizer.OOBRightTest (315605 ms) 272// AddressSanitizer.SimpleStackTest (366559 ms) 273 274TEST(AddressSanitizer, OOB_char) { 275 OOBTest<U1>(); 276} 277 278TEST(AddressSanitizer, OOB_int) { 279 OOBTest<U4>(); 280} 281 282TEST(AddressSanitizer, OOBRightTest) { 283 for (size_t access_size = 1; access_size <= 8; access_size *= 2) { 284 for (size_t alloc_size = 1; alloc_size <= 8; alloc_size++) { 285 for (size_t offset = 0; offset <= 8; offset += access_size) { 286 void *p = malloc(alloc_size); 287 // allocated: [p, p + alloc_size) 288 // accessed: [p + offset, p + offset + access_size) 289 uint8_t *addr = (uint8_t*)p + offset; 290 if (offset + access_size <= alloc_size) { 291 asan_write_sized_aligned(addr, access_size); 292 } else { 293 int outside_bytes = offset > alloc_size ? (offset - alloc_size) : 0; 294 const char *str = 295 "is located.%d *byte.*to the right"; 296 char expected_str[100]; 297 sprintf(expected_str, str, outside_bytes); 298 EXPECT_DEATH(asan_write_sized_aligned(addr, access_size), 299 expected_str); 300 } 301 free(p); 302 } 303 } 304 } 305} 306 307TEST(AddressSanitizer, UAF_char) { 308 const char *uaf_string = "AddressSanitizer:.*heap-use-after-free"; 309 EXPECT_DEATH(uaf_test<U1>(1, 0), uaf_string); 310 EXPECT_DEATH(uaf_test<U1>(10, 0), uaf_string); 311 EXPECT_DEATH(uaf_test<U1>(10, 10), uaf_string); 312 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, 0), uaf_string); 313 EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, kLargeMalloc / 2), uaf_string); 314} 315 316#if ASAN_HAS_BLACKLIST 317TEST(AddressSanitizer, IgnoreTest) { 318 int *x = Ident(new int); 319 delete Ident(x); 320 *x = 0; 321} 322#endif // ASAN_HAS_BLACKLIST 323 324struct StructWithBitField { 325 int bf1:1; 326 int bf2:1; 327 int bf3:1; 328 int bf4:29; 329}; 330 331TEST(AddressSanitizer, BitFieldPositiveTest) { 332 StructWithBitField *x = new StructWithBitField; 333 delete Ident(x); 334 EXPECT_DEATH(x->bf1 = 0, "use-after-free"); 335 EXPECT_DEATH(x->bf2 = 0, "use-after-free"); 336 EXPECT_DEATH(x->bf3 = 0, "use-after-free"); 337 EXPECT_DEATH(x->bf4 = 0, "use-after-free"); 338} 339 340struct StructWithBitFields_8_24 { 341 int a:8; 342 int b:24; 343}; 344 345TEST(AddressSanitizer, BitFieldNegativeTest) { 346 StructWithBitFields_8_24 *x = Ident(new StructWithBitFields_8_24); 347 x->a = 0; 348 x->b = 0; 349 delete Ident(x); 350} 351 352TEST(AddressSanitizer, OutOfMemoryTest) { 353 size_t size = SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 48) : (0xf0000000); 354 EXPECT_EQ(0, realloc(0, size)); 355 EXPECT_EQ(0, realloc(0, ~Ident(0))); 356 EXPECT_EQ(0, malloc(size)); 357 EXPECT_EQ(0, malloc(~Ident(0))); 358 EXPECT_EQ(0, calloc(1, size)); 359 EXPECT_EQ(0, calloc(1, ~Ident(0))); 360} 361 362#if ASAN_NEEDS_SEGV 363namespace { 364 365const char kUnknownCrash[] = "AddressSanitizer: SEGV on unknown address"; 366const char kOverriddenHandler[] = "ASan signal handler has been overridden\n"; 367 368TEST(AddressSanitizer, WildAddressTest) { 369 char *c = (char*)0x123; 370 EXPECT_DEATH(*c = 0, kUnknownCrash); 371} 372 373void my_sigaction_sighandler(int, siginfo_t*, void*) { 374 fprintf(stderr, kOverriddenHandler); 375 exit(1); 376} 377 378void my_signal_sighandler(int signum) { 379 fprintf(stderr, kOverriddenHandler); 380 exit(1); 381} 382 383TEST(AddressSanitizer, SignalTest) { 384 struct sigaction sigact; 385 memset(&sigact, 0, sizeof(sigact)); 386 sigact.sa_sigaction = my_sigaction_sighandler; 387 sigact.sa_flags = SA_SIGINFO; 388 // ASan should silently ignore sigaction()... 389 EXPECT_EQ(0, sigaction(SIGSEGV, &sigact, 0)); 390#ifdef __APPLE__ 391 EXPECT_EQ(0, sigaction(SIGBUS, &sigact, 0)); 392#endif 393 char *c = (char*)0x123; 394 EXPECT_DEATH(*c = 0, kUnknownCrash); 395 // ... and signal(). 396 EXPECT_EQ(0, signal(SIGSEGV, my_signal_sighandler)); 397 EXPECT_DEATH(*c = 0, kUnknownCrash); 398} 399} // namespace 400#endif 401 402static void MallocStress(size_t n) { 403 uint32_t seed = my_rand(&global_seed); 404 for (size_t iter = 0; iter < 10; iter++) { 405 vector<void *> vec; 406 for (size_t i = 0; i < n; i++) { 407 if ((i % 3) == 0) { 408 if (vec.empty()) continue; 409 size_t idx = my_rand(&seed) % vec.size(); 410 void *ptr = vec[idx]; 411 vec[idx] = vec.back(); 412 vec.pop_back(); 413 free_aaa(ptr); 414 } else { 415 size_t size = my_rand(&seed) % 1000 + 1; 416#ifndef __APPLE__ 417 size_t alignment = 1 << (my_rand(&seed) % 7 + 3); 418 char *ptr = (char*)memalign_aaa(alignment, size); 419#else 420 char *ptr = (char*) malloc_aaa(size); 421#endif 422 vec.push_back(ptr); 423 ptr[0] = 0; 424 ptr[size-1] = 0; 425 ptr[size/2] = 0; 426 } 427 } 428 for (size_t i = 0; i < vec.size(); i++) 429 free_aaa(vec[i]); 430 } 431} 432 433TEST(AddressSanitizer, MallocStressTest) { 434 MallocStress((ASAN_LOW_MEMORY) ? 20000 : 200000); 435} 436 437static void TestLargeMalloc(size_t size) { 438 char buff[1024]; 439 sprintf(buff, "is located 1 bytes to the left of %lu-byte", (long)size); 440 EXPECT_DEATH(Ident((char*)malloc(size))[-1] = 0, buff); 441} 442 443TEST(AddressSanitizer, LargeMallocTest) { 444 for (int i = 113; i < (1 << 28); i = i * 2 + 13) { 445 TestLargeMalloc(i); 446 } 447} 448 449#if ASAN_LOW_MEMORY != 1 450TEST(AddressSanitizer, HugeMallocTest) { 451#ifdef __APPLE__ 452 // It was empirically found out that 1215 megabytes is the maximum amount of 453 // memory available to the process under AddressSanitizer on 32-bit Mac 10.6. 454 // 32-bit Mac 10.7 gives even less (< 1G). 455 // (the libSystem malloc() allows allocating up to 2300 megabytes without 456 // ASan). 457 size_t n_megs = SANITIZER_WORDSIZE == 32 ? 500 : 4100; 458#else 459 size_t n_megs = SANITIZER_WORDSIZE == 32 ? 2600 : 4100; 460#endif 461 TestLargeMalloc(n_megs << 20); 462} 463#endif 464 465TEST(AddressSanitizer, ThreadedMallocStressTest) { 466 const int kNumThreads = 4; 467 const int kNumIterations = (ASAN_LOW_MEMORY) ? 10000 : 100000; 468 pthread_t t[kNumThreads]; 469 for (int i = 0; i < kNumThreads; i++) { 470 PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))MallocStress, 471 (void*)kNumIterations); 472 } 473 for (int i = 0; i < kNumThreads; i++) { 474 PTHREAD_JOIN(t[i], 0); 475 } 476} 477 478void *ManyThreadsWorker(void *a) { 479 for (int iter = 0; iter < 100; iter++) { 480 for (size_t size = 100; size < 2000; size *= 2) { 481 free(Ident(malloc(size))); 482 } 483 } 484 return 0; 485} 486 487TEST(AddressSanitizer, ManyThreadsTest) { 488 const size_t kNumThreads = 489 (SANITIZER_WORDSIZE == 32 || ASAN_AVOID_EXPENSIVE_TESTS) ? 30 : 1000; 490 pthread_t t[kNumThreads]; 491 for (size_t i = 0; i < kNumThreads; i++) { 492 PTHREAD_CREATE(&t[i], 0, ManyThreadsWorker, (void*)i); 493 } 494 for (size_t i = 0; i < kNumThreads; i++) { 495 PTHREAD_JOIN(t[i], 0); 496 } 497} 498 499TEST(AddressSanitizer, ReallocTest) { 500 const int kMinElem = 5; 501 int *ptr = (int*)malloc(sizeof(int) * kMinElem); 502 ptr[3] = 3; 503 for (int i = 0; i < 10000; i++) { 504 ptr = (int*)realloc(ptr, 505 (my_rand(&global_seed) % 1000 + kMinElem) * sizeof(int)); 506 EXPECT_EQ(3, ptr[3]); 507 } 508} 509 510#ifndef __APPLE__ 511static const char *kMallocUsableSizeErrorMsg = 512 "AddressSanitizer: attempting to call malloc_usable_size()"; 513 514TEST(AddressSanitizer, MallocUsableSizeTest) { 515 const size_t kArraySize = 100; 516 char *array = Ident((char*)malloc(kArraySize)); 517 int *int_ptr = Ident(new int); 518 EXPECT_EQ(0U, malloc_usable_size(NULL)); 519 EXPECT_EQ(kArraySize, malloc_usable_size(array)); 520 EXPECT_EQ(sizeof(int), malloc_usable_size(int_ptr)); 521 EXPECT_DEATH(malloc_usable_size((void*)0x123), kMallocUsableSizeErrorMsg); 522 EXPECT_DEATH(malloc_usable_size(array + kArraySize / 2), 523 kMallocUsableSizeErrorMsg); 524 free(array); 525 EXPECT_DEATH(malloc_usable_size(array), kMallocUsableSizeErrorMsg); 526} 527#endif 528 529void WrongFree() { 530 int *x = (int*)malloc(100 * sizeof(int)); 531 // Use the allocated memory, otherwise Clang will optimize it out. 532 Ident(x); 533 free(x + 1); 534} 535 536TEST(AddressSanitizer, WrongFreeTest) { 537 EXPECT_DEATH(WrongFree(), 538 "ERROR: AddressSanitizer: attempting free.*not malloc"); 539} 540 541void DoubleFree() { 542 int *x = (int*)malloc(100 * sizeof(int)); 543 fprintf(stderr, "DoubleFree: x=%p\n", x); 544 free(x); 545 free(x); 546 fprintf(stderr, "should have failed in the second free(%p)\n", x); 547 abort(); 548} 549 550TEST(AddressSanitizer, DoubleFreeTest) { 551 EXPECT_DEATH(DoubleFree(), ASAN_PCRE_DOTALL 552 "ERROR: AddressSanitizer: attempting double-free" 553 ".*is located 0 bytes inside of 400-byte region" 554 ".*freed by thread T0 here" 555 ".*previously allocated by thread T0 here"); 556} 557 558template<int kSize> 559NOINLINE void SizedStackTest() { 560 char a[kSize]; 561 char *A = Ident((char*)&a); 562 for (size_t i = 0; i < kSize; i++) 563 A[i] = i; 564 EXPECT_DEATH(A[-1] = 0, ""); 565 EXPECT_DEATH(A[-20] = 0, ""); 566 EXPECT_DEATH(A[-31] = 0, ""); 567 EXPECT_DEATH(A[kSize] = 0, ""); 568 EXPECT_DEATH(A[kSize + 1] = 0, ""); 569 EXPECT_DEATH(A[kSize + 10] = 0, ""); 570 EXPECT_DEATH(A[kSize + 31] = 0, ""); 571} 572 573TEST(AddressSanitizer, SimpleStackTest) { 574 SizedStackTest<1>(); 575 SizedStackTest<2>(); 576 SizedStackTest<3>(); 577 SizedStackTest<4>(); 578 SizedStackTest<5>(); 579 SizedStackTest<6>(); 580 SizedStackTest<7>(); 581 SizedStackTest<16>(); 582 SizedStackTest<25>(); 583 SizedStackTest<34>(); 584 SizedStackTest<43>(); 585 SizedStackTest<51>(); 586 SizedStackTest<62>(); 587 SizedStackTest<64>(); 588 SizedStackTest<128>(); 589} 590 591TEST(AddressSanitizer, ManyStackObjectsTest) { 592 char XXX[10]; 593 char YYY[20]; 594 char ZZZ[30]; 595 Ident(XXX); 596 Ident(YYY); 597 EXPECT_DEATH(Ident(ZZZ)[-1] = 0, ASAN_PCRE_DOTALL "XXX.*YYY.*ZZZ"); 598} 599 600NOINLINE static void Frame0(int frame, char *a, char *b, char *c) { 601 char d[4] = {0}; 602 char *D = Ident(d); 603 switch (frame) { 604 case 3: a[5]++; break; 605 case 2: b[5]++; break; 606 case 1: c[5]++; break; 607 case 0: D[5]++; break; 608 } 609} 610NOINLINE static void Frame1(int frame, char *a, char *b) { 611 char c[4] = {0}; Frame0(frame, a, b, c); 612 break_optimization(0); 613} 614NOINLINE static void Frame2(int frame, char *a) { 615 char b[4] = {0}; Frame1(frame, a, b); 616 break_optimization(0); 617} 618NOINLINE static void Frame3(int frame) { 619 char a[4] = {0}; Frame2(frame, a); 620 break_optimization(0); 621} 622 623TEST(AddressSanitizer, GuiltyStackFrame0Test) { 624 EXPECT_DEATH(Frame3(0), "located .*in frame <.*Frame0"); 625} 626TEST(AddressSanitizer, GuiltyStackFrame1Test) { 627 EXPECT_DEATH(Frame3(1), "located .*in frame <.*Frame1"); 628} 629TEST(AddressSanitizer, GuiltyStackFrame2Test) { 630 EXPECT_DEATH(Frame3(2), "located .*in frame <.*Frame2"); 631} 632TEST(AddressSanitizer, GuiltyStackFrame3Test) { 633 EXPECT_DEATH(Frame3(3), "located .*in frame <.*Frame3"); 634} 635 636NOINLINE void LongJmpFunc1(jmp_buf buf) { 637 // create three red zones for these two stack objects. 638 int a; 639 int b; 640 641 int *A = Ident(&a); 642 int *B = Ident(&b); 643 *A = *B; 644 longjmp(buf, 1); 645} 646 647NOINLINE void BuiltinLongJmpFunc1(jmp_buf buf) { 648 // create three red zones for these two stack objects. 649 int a; 650 int b; 651 652 int *A = Ident(&a); 653 int *B = Ident(&b); 654 *A = *B; 655 __builtin_longjmp((void**)buf, 1); 656} 657 658NOINLINE void UnderscopeLongJmpFunc1(jmp_buf buf) { 659 // create three red zones for these two stack objects. 660 int a; 661 int b; 662 663 int *A = Ident(&a); 664 int *B = Ident(&b); 665 *A = *B; 666 _longjmp(buf, 1); 667} 668 669NOINLINE void SigLongJmpFunc1(sigjmp_buf buf) { 670 // create three red zones for these two stack objects. 671 int a; 672 int b; 673 674 int *A = Ident(&a); 675 int *B = Ident(&b); 676 *A = *B; 677 siglongjmp(buf, 1); 678} 679 680 681NOINLINE void TouchStackFunc() { 682 int a[100]; // long array will intersect with redzones from LongJmpFunc1. 683 int *A = Ident(a); 684 for (int i = 0; i < 100; i++) 685 A[i] = i*i; 686} 687 688// Test that we handle longjmp and do not report fals positives on stack. 689TEST(AddressSanitizer, LongJmpTest) { 690 static jmp_buf buf; 691 if (!setjmp(buf)) { 692 LongJmpFunc1(buf); 693 } else { 694 TouchStackFunc(); 695 } 696} 697 698#if not defined(__ANDROID__) 699TEST(AddressSanitizer, BuiltinLongJmpTest) { 700 static jmp_buf buf; 701 if (!__builtin_setjmp((void**)buf)) { 702 BuiltinLongJmpFunc1(buf); 703 } else { 704 TouchStackFunc(); 705 } 706} 707#endif // not defined(__ANDROID__) 708 709TEST(AddressSanitizer, UnderscopeLongJmpTest) { 710 static jmp_buf buf; 711 if (!_setjmp(buf)) { 712 UnderscopeLongJmpFunc1(buf); 713 } else { 714 TouchStackFunc(); 715 } 716} 717 718TEST(AddressSanitizer, SigLongJmpTest) { 719 static sigjmp_buf buf; 720 if (!sigsetjmp(buf, 1)) { 721 SigLongJmpFunc1(buf); 722 } else { 723 TouchStackFunc(); 724 } 725} 726 727#ifdef __EXCEPTIONS 728NOINLINE void ThrowFunc() { 729 // create three red zones for these two stack objects. 730 int a; 731 int b; 732 733 int *A = Ident(&a); 734 int *B = Ident(&b); 735 *A = *B; 736 ASAN_THROW(1); 737} 738 739TEST(AddressSanitizer, CxxExceptionTest) { 740 if (ASAN_UAR) return; 741 // TODO(kcc): this test crashes on 32-bit for some reason... 742 if (SANITIZER_WORDSIZE == 32) return; 743 try { 744 ThrowFunc(); 745 } catch(...) {} 746 TouchStackFunc(); 747} 748#endif 749 750void *ThreadStackReuseFunc1(void *unused) { 751 // create three red zones for these two stack objects. 752 int a; 753 int b; 754 755 int *A = Ident(&a); 756 int *B = Ident(&b); 757 *A = *B; 758 pthread_exit(0); 759 return 0; 760} 761 762void *ThreadStackReuseFunc2(void *unused) { 763 TouchStackFunc(); 764 return 0; 765} 766 767TEST(AddressSanitizer, ThreadStackReuseTest) { 768 pthread_t t; 769 PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc1, 0); 770 PTHREAD_JOIN(t, 0); 771 PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc2, 0); 772 PTHREAD_JOIN(t, 0); 773} 774 775#if defined(__i386__) || defined(__x86_64__) 776TEST(AddressSanitizer, Store128Test) { 777 char *a = Ident((char*)malloc(Ident(12))); 778 char *p = a; 779 if (((uintptr_t)a % 16) != 0) 780 p = a + 8; 781 assert(((uintptr_t)p % 16) == 0); 782 __m128i value_wide = _mm_set1_epi16(0x1234); 783 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 784 "AddressSanitizer: heap-buffer-overflow"); 785 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 786 "WRITE of size 16"); 787 EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 788 "located 0 bytes to the right of 12-byte"); 789 free(a); 790} 791#endif 792 793static string RightOOBErrorMessage(int oob_distance) { 794 assert(oob_distance >= 0); 795 char expected_str[100]; 796 sprintf(expected_str, "located %d bytes to the right", oob_distance); 797 return string(expected_str); 798} 799 800static string LeftOOBErrorMessage(int oob_distance) { 801 assert(oob_distance > 0); 802 char expected_str[100]; 803 sprintf(expected_str, "located %d bytes to the left", oob_distance); 804 return string(expected_str); 805} 806 807template<typename T> 808void MemSetOOBTestTemplate(size_t length) { 809 if (length == 0) return; 810 size_t size = Ident(sizeof(T) * length); 811 T *array = Ident((T*)malloc(size)); 812 int element = Ident(42); 813 int zero = Ident(0); 814 // memset interval inside array 815 memset(array, element, size); 816 memset(array, element, size - 1); 817 memset(array + length - 1, element, sizeof(T)); 818 memset(array, element, 1); 819 820 // memset 0 bytes 821 memset(array - 10, element, zero); 822 memset(array - 1, element, zero); 823 memset(array, element, zero); 824 memset(array + length, 0, zero); 825 memset(array + length + 1, 0, zero); 826 827 // try to memset bytes to the right of array 828 EXPECT_DEATH(memset(array, 0, size + 1), 829 RightOOBErrorMessage(0)); 830 EXPECT_DEATH(memset((char*)(array + length) - 1, element, 6), 831 RightOOBErrorMessage(4)); 832 EXPECT_DEATH(memset(array + 1, element, size + sizeof(T)), 833 RightOOBErrorMessage(2 * sizeof(T) - 1)); 834 // whole interval is to the right 835 EXPECT_DEATH(memset(array + length + 1, 0, 10), 836 RightOOBErrorMessage(sizeof(T))); 837 838 // try to memset bytes to the left of array 839 EXPECT_DEATH(memset((char*)array - 1, element, size), 840 LeftOOBErrorMessage(1)); 841 EXPECT_DEATH(memset((char*)array - 5, 0, 6), 842 LeftOOBErrorMessage(5)); 843 EXPECT_DEATH(memset(array - 5, element, size + 5 * sizeof(T)), 844 LeftOOBErrorMessage(5 * sizeof(T))); 845 // whole interval is to the left 846 EXPECT_DEATH(memset(array - 2, 0, sizeof(T)), 847 LeftOOBErrorMessage(2 * sizeof(T))); 848 849 // try to memset bytes both to the left & to the right 850 EXPECT_DEATH(memset((char*)array - 2, element, size + 4), 851 LeftOOBErrorMessage(2)); 852 853 free(array); 854} 855 856TEST(AddressSanitizer, MemSetOOBTest) { 857 MemSetOOBTestTemplate<char>(100); 858 MemSetOOBTestTemplate<int>(5); 859 MemSetOOBTestTemplate<double>(256); 860 // We can test arrays of structres/classes here, but what for? 861} 862 863// Same test for memcpy and memmove functions 864template <typename T, class M> 865void MemTransferOOBTestTemplate(size_t length) { 866 if (length == 0) return; 867 size_t size = Ident(sizeof(T) * length); 868 T *src = Ident((T*)malloc(size)); 869 T *dest = Ident((T*)malloc(size)); 870 int zero = Ident(0); 871 872 // valid transfer of bytes between arrays 873 M::transfer(dest, src, size); 874 M::transfer(dest + 1, src, size - sizeof(T)); 875 M::transfer(dest, src + length - 1, sizeof(T)); 876 M::transfer(dest, src, 1); 877 878 // transfer zero bytes 879 M::transfer(dest - 1, src, 0); 880 M::transfer(dest + length, src, zero); 881 M::transfer(dest, src - 1, zero); 882 M::transfer(dest, src, zero); 883 884 // try to change mem to the right of dest 885 EXPECT_DEATH(M::transfer(dest + 1, src, size), 886 RightOOBErrorMessage(sizeof(T) - 1)); 887 EXPECT_DEATH(M::transfer((char*)(dest + length) - 1, src, 5), 888 RightOOBErrorMessage(3)); 889 890 // try to change mem to the left of dest 891 EXPECT_DEATH(M::transfer(dest - 2, src, size), 892 LeftOOBErrorMessage(2 * sizeof(T))); 893 EXPECT_DEATH(M::transfer((char*)dest - 3, src, 4), 894 LeftOOBErrorMessage(3)); 895 896 // try to access mem to the right of src 897 EXPECT_DEATH(M::transfer(dest, src + 2, size), 898 RightOOBErrorMessage(2 * sizeof(T) - 1)); 899 EXPECT_DEATH(M::transfer(dest, (char*)(src + length) - 3, 6), 900 RightOOBErrorMessage(2)); 901 902 // try to access mem to the left of src 903 EXPECT_DEATH(M::transfer(dest, src - 1, size), 904 LeftOOBErrorMessage(sizeof(T))); 905 EXPECT_DEATH(M::transfer(dest, (char*)src - 6, 7), 906 LeftOOBErrorMessage(6)); 907 908 // Generally we don't need to test cases where both accessing src and writing 909 // to dest address to poisoned memory. 910 911 T *big_src = Ident((T*)malloc(size * 2)); 912 T *big_dest = Ident((T*)malloc(size * 2)); 913 // try to change mem to both sides of dest 914 EXPECT_DEATH(M::transfer(dest - 1, big_src, size * 2), 915 LeftOOBErrorMessage(sizeof(T))); 916 // try to access mem to both sides of src 917 EXPECT_DEATH(M::transfer(big_dest, src - 2, size * 2), 918 LeftOOBErrorMessage(2 * sizeof(T))); 919 920 free(src); 921 free(dest); 922 free(big_src); 923 free(big_dest); 924} 925 926class MemCpyWrapper { 927 public: 928 static void* transfer(void *to, const void *from, size_t size) { 929 return memcpy(to, from, size); 930 } 931}; 932TEST(AddressSanitizer, MemCpyOOBTest) { 933 MemTransferOOBTestTemplate<char, MemCpyWrapper>(100); 934 MemTransferOOBTestTemplate<int, MemCpyWrapper>(1024); 935} 936 937class MemMoveWrapper { 938 public: 939 static void* transfer(void *to, const void *from, size_t size) { 940 return memmove(to, from, size); 941 } 942}; 943TEST(AddressSanitizer, MemMoveOOBTest) { 944 MemTransferOOBTestTemplate<char, MemMoveWrapper>(100); 945 MemTransferOOBTestTemplate<int, MemMoveWrapper>(1024); 946} 947 948// Tests for string functions 949 950// Used for string functions tests 951static char global_string[] = "global"; 952static size_t global_string_length = 6; 953 954// Input to a test is a zero-terminated string str with given length 955// Accesses to the bytes to the left and to the right of str 956// are presumed to produce OOB errors 957void StrLenOOBTestTemplate(char *str, size_t length, bool is_global) { 958 // Normal strlen calls 959 EXPECT_EQ(strlen(str), length); 960 if (length > 0) { 961 EXPECT_EQ(length - 1, strlen(str + 1)); 962 EXPECT_EQ(0U, strlen(str + length)); 963 } 964 // Arg of strlen is not malloced, OOB access 965 if (!is_global) { 966 // We don't insert RedZones to the left of global variables 967 EXPECT_DEATH(Ident(strlen(str - 1)), LeftOOBErrorMessage(1)); 968 EXPECT_DEATH(Ident(strlen(str - 5)), LeftOOBErrorMessage(5)); 969 } 970 EXPECT_DEATH(Ident(strlen(str + length + 1)), RightOOBErrorMessage(0)); 971 // Overwrite terminator 972 str[length] = 'a'; 973 // String is not zero-terminated, strlen will lead to OOB access 974 EXPECT_DEATH(Ident(strlen(str)), RightOOBErrorMessage(0)); 975 EXPECT_DEATH(Ident(strlen(str + length)), RightOOBErrorMessage(0)); 976 // Restore terminator 977 str[length] = 0; 978} 979TEST(AddressSanitizer, StrLenOOBTest) { 980 // Check heap-allocated string 981 size_t length = Ident(10); 982 char *heap_string = Ident((char*)malloc(length + 1)); 983 char stack_string[10 + 1]; 984 break_optimization(&stack_string); 985 for (size_t i = 0; i < length; i++) { 986 heap_string[i] = 'a'; 987 stack_string[i] = 'b'; 988 } 989 heap_string[length] = 0; 990 stack_string[length] = 0; 991 StrLenOOBTestTemplate(heap_string, length, false); 992 // TODO(samsonov): Fix expected messages in StrLenOOBTestTemplate to 993 // make test for stack_string work. Or move it to output tests. 994 // StrLenOOBTestTemplate(stack_string, length, false); 995 StrLenOOBTestTemplate(global_string, global_string_length, true); 996 free(heap_string); 997} 998 999static inline char* MallocAndMemsetString(size_t size, char ch) { 1000 char *s = Ident((char*)malloc(size)); 1001 memset(s, ch, size); 1002 return s; 1003} 1004static inline char* MallocAndMemsetString(size_t size) { 1005 return MallocAndMemsetString(size, 'z'); 1006} 1007 1008#ifndef __APPLE__ 1009TEST(AddressSanitizer, StrNLenOOBTest) { 1010 size_t size = Ident(123); 1011 char *str = MallocAndMemsetString(size); 1012 // Normal strnlen calls. 1013 Ident(strnlen(str - 1, 0)); 1014 Ident(strnlen(str, size)); 1015 Ident(strnlen(str + size - 1, 1)); 1016 str[size - 1] = '\0'; 1017 Ident(strnlen(str, 2 * size)); 1018 // Argument points to not allocated memory. 1019 EXPECT_DEATH(Ident(strnlen(str - 1, 1)), LeftOOBErrorMessage(1)); 1020 EXPECT_DEATH(Ident(strnlen(str + size, 1)), RightOOBErrorMessage(0)); 1021 // Overwrite the terminating '\0' and hit unallocated memory. 1022 str[size - 1] = 'z'; 1023 EXPECT_DEATH(Ident(strnlen(str, size + 1)), RightOOBErrorMessage(0)); 1024 free(str); 1025} 1026#endif 1027 1028TEST(AddressSanitizer, StrDupOOBTest) { 1029 size_t size = Ident(42); 1030 char *str = MallocAndMemsetString(size); 1031 char *new_str; 1032 // Normal strdup calls. 1033 str[size - 1] = '\0'; 1034 new_str = strdup(str); 1035 free(new_str); 1036 new_str = strdup(str + size - 1); 1037 free(new_str); 1038 // Argument points to not allocated memory. 1039 EXPECT_DEATH(Ident(strdup(str - 1)), LeftOOBErrorMessage(1)); 1040 EXPECT_DEATH(Ident(strdup(str + size)), RightOOBErrorMessage(0)); 1041 // Overwrite the terminating '\0' and hit unallocated memory. 1042 str[size - 1] = 'z'; 1043 EXPECT_DEATH(Ident(strdup(str)), RightOOBErrorMessage(0)); 1044 free(str); 1045} 1046 1047TEST(AddressSanitizer, StrCpyOOBTest) { 1048 size_t to_size = Ident(30); 1049 size_t from_size = Ident(6); // less than to_size 1050 char *to = Ident((char*)malloc(to_size)); 1051 char *from = Ident((char*)malloc(from_size)); 1052 // Normal strcpy calls. 1053 strcpy(from, "hello"); 1054 strcpy(to, from); 1055 strcpy(to + to_size - from_size, from); 1056 // Length of "from" is too small. 1057 EXPECT_DEATH(Ident(strcpy(from, "hello2")), RightOOBErrorMessage(0)); 1058 // "to" or "from" points to not allocated memory. 1059 EXPECT_DEATH(Ident(strcpy(to - 1, from)), LeftOOBErrorMessage(1)); 1060 EXPECT_DEATH(Ident(strcpy(to, from - 1)), LeftOOBErrorMessage(1)); 1061 EXPECT_DEATH(Ident(strcpy(to, from + from_size)), RightOOBErrorMessage(0)); 1062 EXPECT_DEATH(Ident(strcpy(to + to_size, from)), RightOOBErrorMessage(0)); 1063 // Overwrite the terminating '\0' character and hit unallocated memory. 1064 from[from_size - 1] = '!'; 1065 EXPECT_DEATH(Ident(strcpy(to, from)), RightOOBErrorMessage(0)); 1066 free(to); 1067 free(from); 1068} 1069 1070TEST(AddressSanitizer, StrNCpyOOBTest) { 1071 size_t to_size = Ident(20); 1072 size_t from_size = Ident(6); // less than to_size 1073 char *to = Ident((char*)malloc(to_size)); 1074 // From is a zero-terminated string "hello\0" of length 6 1075 char *from = Ident((char*)malloc(from_size)); 1076 strcpy(from, "hello"); 1077 // copy 0 bytes 1078 strncpy(to, from, 0); 1079 strncpy(to - 1, from - 1, 0); 1080 // normal strncpy calls 1081 strncpy(to, from, from_size); 1082 strncpy(to, from, to_size); 1083 strncpy(to, from + from_size - 1, to_size); 1084 strncpy(to + to_size - 1, from, 1); 1085 // One of {to, from} points to not allocated memory 1086 EXPECT_DEATH(Ident(strncpy(to, from - 1, from_size)), 1087 LeftOOBErrorMessage(1)); 1088 EXPECT_DEATH(Ident(strncpy(to - 1, from, from_size)), 1089 LeftOOBErrorMessage(1)); 1090 EXPECT_DEATH(Ident(strncpy(to, from + from_size, 1)), 1091 RightOOBErrorMessage(0)); 1092 EXPECT_DEATH(Ident(strncpy(to + to_size, from, 1)), 1093 RightOOBErrorMessage(0)); 1094 // Length of "to" is too small 1095 EXPECT_DEATH(Ident(strncpy(to + to_size - from_size + 1, from, from_size)), 1096 RightOOBErrorMessage(0)); 1097 EXPECT_DEATH(Ident(strncpy(to + 1, from, to_size)), 1098 RightOOBErrorMessage(0)); 1099 // Overwrite terminator in from 1100 from[from_size - 1] = '!'; 1101 // normal strncpy call 1102 strncpy(to, from, from_size); 1103 // Length of "from" is too small 1104 EXPECT_DEATH(Ident(strncpy(to, from, to_size)), 1105 RightOOBErrorMessage(0)); 1106 free(to); 1107 free(from); 1108} 1109 1110// Users may have different definitions of "strchr" and "index", so provide 1111// function pointer typedefs and overload RunStrChrTest implementation. 1112// We can't use macro for RunStrChrTest body here, as this macro would 1113// confuse EXPECT_DEATH gtest macro. 1114typedef char*(*PointerToStrChr1)(const char*, int); 1115typedef char*(*PointerToStrChr2)(char*, int); 1116 1117USED static void RunStrChrTest(PointerToStrChr1 StrChr) { 1118 size_t size = Ident(100); 1119 char *str = MallocAndMemsetString(size); 1120 str[10] = 'q'; 1121 str[11] = '\0'; 1122 EXPECT_EQ(str, StrChr(str, 'z')); 1123 EXPECT_EQ(str + 10, StrChr(str, 'q')); 1124 EXPECT_EQ(NULL, StrChr(str, 'a')); 1125 // StrChr argument points to not allocated memory. 1126 EXPECT_DEATH(Ident(StrChr(str - 1, 'z')), LeftOOBErrorMessage(1)); 1127 EXPECT_DEATH(Ident(StrChr(str + size, 'z')), RightOOBErrorMessage(0)); 1128 // Overwrite the terminator and hit not allocated memory. 1129 str[11] = 'z'; 1130 EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBErrorMessage(0)); 1131 free(str); 1132} 1133USED static void RunStrChrTest(PointerToStrChr2 StrChr) { 1134 size_t size = Ident(100); 1135 char *str = MallocAndMemsetString(size); 1136 str[10] = 'q'; 1137 str[11] = '\0'; 1138 EXPECT_EQ(str, StrChr(str, 'z')); 1139 EXPECT_EQ(str + 10, StrChr(str, 'q')); 1140 EXPECT_EQ(NULL, StrChr(str, 'a')); 1141 // StrChr argument points to not allocated memory. 1142 EXPECT_DEATH(Ident(StrChr(str - 1, 'z')), LeftOOBErrorMessage(1)); 1143 EXPECT_DEATH(Ident(StrChr(str + size, 'z')), RightOOBErrorMessage(0)); 1144 // Overwrite the terminator and hit not allocated memory. 1145 str[11] = 'z'; 1146 EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBErrorMessage(0)); 1147 free(str); 1148} 1149 1150TEST(AddressSanitizer, StrChrAndIndexOOBTest) { 1151 RunStrChrTest(&strchr); 1152 RunStrChrTest(&index); 1153} 1154 1155TEST(AddressSanitizer, StrCmpAndFriendsLogicTest) { 1156 // strcmp 1157 EXPECT_EQ(0, strcmp("", "")); 1158 EXPECT_EQ(0, strcmp("abcd", "abcd")); 1159 EXPECT_GT(0, strcmp("ab", "ac")); 1160 EXPECT_GT(0, strcmp("abc", "abcd")); 1161 EXPECT_LT(0, strcmp("acc", "abc")); 1162 EXPECT_LT(0, strcmp("abcd", "abc")); 1163 1164 // strncmp 1165 EXPECT_EQ(0, strncmp("a", "b", 0)); 1166 EXPECT_EQ(0, strncmp("abcd", "abcd", 10)); 1167 EXPECT_EQ(0, strncmp("abcd", "abcef", 3)); 1168 EXPECT_GT(0, strncmp("abcde", "abcfa", 4)); 1169 EXPECT_GT(0, strncmp("a", "b", 5)); 1170 EXPECT_GT(0, strncmp("bc", "bcde", 4)); 1171 EXPECT_LT(0, strncmp("xyz", "xyy", 10)); 1172 EXPECT_LT(0, strncmp("baa", "aaa", 1)); 1173 EXPECT_LT(0, strncmp("zyx", "", 2)); 1174 1175 // strcasecmp 1176 EXPECT_EQ(0, strcasecmp("", "")); 1177 EXPECT_EQ(0, strcasecmp("zzz", "zzz")); 1178 EXPECT_EQ(0, strcasecmp("abCD", "ABcd")); 1179 EXPECT_GT(0, strcasecmp("aB", "Ac")); 1180 EXPECT_GT(0, strcasecmp("ABC", "ABCd")); 1181 EXPECT_LT(0, strcasecmp("acc", "abc")); 1182 EXPECT_LT(0, strcasecmp("ABCd", "abc")); 1183 1184 // strncasecmp 1185 EXPECT_EQ(0, strncasecmp("a", "b", 0)); 1186 EXPECT_EQ(0, strncasecmp("abCD", "ABcd", 10)); 1187 EXPECT_EQ(0, strncasecmp("abCd", "ABcef", 3)); 1188 EXPECT_GT(0, strncasecmp("abcde", "ABCfa", 4)); 1189 EXPECT_GT(0, strncasecmp("a", "B", 5)); 1190 EXPECT_GT(0, strncasecmp("bc", "BCde", 4)); 1191 EXPECT_LT(0, strncasecmp("xyz", "xyy", 10)); 1192 EXPECT_LT(0, strncasecmp("Baa", "aaa", 1)); 1193 EXPECT_LT(0, strncasecmp("zyx", "", 2)); 1194 1195 // memcmp 1196 EXPECT_EQ(0, memcmp("a", "b", 0)); 1197 EXPECT_EQ(0, memcmp("ab\0c", "ab\0c", 4)); 1198 EXPECT_GT(0, memcmp("\0ab", "\0ac", 3)); 1199 EXPECT_GT(0, memcmp("abb\0", "abba", 4)); 1200 EXPECT_LT(0, memcmp("ab\0cd", "ab\0c\0", 5)); 1201 EXPECT_LT(0, memcmp("zza", "zyx", 3)); 1202} 1203 1204typedef int(*PointerToStrCmp)(const char*, const char*); 1205void RunStrCmpTest(PointerToStrCmp StrCmp) { 1206 size_t size = Ident(100); 1207 char *s1 = MallocAndMemsetString(size); 1208 char *s2 = MallocAndMemsetString(size); 1209 s1[size - 1] = '\0'; 1210 s2[size - 1] = '\0'; 1211 // Normal StrCmp calls 1212 Ident(StrCmp(s1, s2)); 1213 Ident(StrCmp(s1, s2 + size - 1)); 1214 Ident(StrCmp(s1 + size - 1, s2 + size - 1)); 1215 s1[size - 1] = 'z'; 1216 s2[size - 1] = 'x'; 1217 Ident(StrCmp(s1, s2)); 1218 // One of arguments points to not allocated memory. 1219 EXPECT_DEATH(Ident(StrCmp)(s1 - 1, s2), LeftOOBErrorMessage(1)); 1220 EXPECT_DEATH(Ident(StrCmp)(s1, s2 - 1), LeftOOBErrorMessage(1)); 1221 EXPECT_DEATH(Ident(StrCmp)(s1 + size, s2), RightOOBErrorMessage(0)); 1222 EXPECT_DEATH(Ident(StrCmp)(s1, s2 + size), RightOOBErrorMessage(0)); 1223 // Hit unallocated memory and die. 1224 s2[size - 1] = 'z'; 1225 EXPECT_DEATH(Ident(StrCmp)(s1, s1), RightOOBErrorMessage(0)); 1226 EXPECT_DEATH(Ident(StrCmp)(s1 + size - 1, s2), RightOOBErrorMessage(0)); 1227 free(s1); 1228 free(s2); 1229} 1230 1231TEST(AddressSanitizer, StrCmpOOBTest) { 1232 RunStrCmpTest(&strcmp); 1233} 1234 1235TEST(AddressSanitizer, StrCaseCmpOOBTest) { 1236 RunStrCmpTest(&strcasecmp); 1237} 1238 1239typedef int(*PointerToStrNCmp)(const char*, const char*, size_t); 1240void RunStrNCmpTest(PointerToStrNCmp StrNCmp) { 1241 size_t size = Ident(100); 1242 char *s1 = MallocAndMemsetString(size); 1243 char *s2 = MallocAndMemsetString(size); 1244 s1[size - 1] = '\0'; 1245 s2[size - 1] = '\0'; 1246 // Normal StrNCmp calls 1247 Ident(StrNCmp(s1, s2, size + 2)); 1248 s1[size - 1] = 'z'; 1249 s2[size - 1] = 'x'; 1250 Ident(StrNCmp(s1 + size - 2, s2 + size - 2, size)); 1251 s2[size - 1] = 'z'; 1252 Ident(StrNCmp(s1 - 1, s2 - 1, 0)); 1253 Ident(StrNCmp(s1 + size - 1, s2 + size - 1, 1)); 1254 // One of arguments points to not allocated memory. 1255 EXPECT_DEATH(Ident(StrNCmp)(s1 - 1, s2, 1), LeftOOBErrorMessage(1)); 1256 EXPECT_DEATH(Ident(StrNCmp)(s1, s2 - 1, 1), LeftOOBErrorMessage(1)); 1257 EXPECT_DEATH(Ident(StrNCmp)(s1 + size, s2, 1), RightOOBErrorMessage(0)); 1258 EXPECT_DEATH(Ident(StrNCmp)(s1, s2 + size, 1), RightOOBErrorMessage(0)); 1259 // Hit unallocated memory and die. 1260 EXPECT_DEATH(Ident(StrNCmp)(s1 + 1, s2 + 1, size), RightOOBErrorMessage(0)); 1261 EXPECT_DEATH(Ident(StrNCmp)(s1 + size - 1, s2, 2), RightOOBErrorMessage(0)); 1262 free(s1); 1263 free(s2); 1264} 1265 1266TEST(AddressSanitizer, StrNCmpOOBTest) { 1267 RunStrNCmpTest(&strncmp); 1268} 1269 1270TEST(AddressSanitizer, StrNCaseCmpOOBTest) { 1271 RunStrNCmpTest(&strncasecmp); 1272} 1273 1274TEST(AddressSanitizer, MemCmpOOBTest) { 1275 size_t size = Ident(100); 1276 char *s1 = MallocAndMemsetString(size); 1277 char *s2 = MallocAndMemsetString(size); 1278 // Normal memcmp calls. 1279 Ident(memcmp(s1, s2, size)); 1280 Ident(memcmp(s1 + size - 1, s2 + size - 1, 1)); 1281 Ident(memcmp(s1 - 1, s2 - 1, 0)); 1282 // One of arguments points to not allocated memory. 1283 EXPECT_DEATH(Ident(memcmp)(s1 - 1, s2, 1), LeftOOBErrorMessage(1)); 1284 EXPECT_DEATH(Ident(memcmp)(s1, s2 - 1, 1), LeftOOBErrorMessage(1)); 1285 EXPECT_DEATH(Ident(memcmp)(s1 + size, s2, 1), RightOOBErrorMessage(0)); 1286 EXPECT_DEATH(Ident(memcmp)(s1, s2 + size, 1), RightOOBErrorMessage(0)); 1287 // Hit unallocated memory and die. 1288 EXPECT_DEATH(Ident(memcmp)(s1 + 1, s2 + 1, size), RightOOBErrorMessage(0)); 1289 EXPECT_DEATH(Ident(memcmp)(s1 + size - 1, s2, 2), RightOOBErrorMessage(0)); 1290 // Zero bytes are not terminators and don't prevent from OOB. 1291 s1[size - 1] = '\0'; 1292 s2[size - 1] = '\0'; 1293 EXPECT_DEATH(Ident(memcmp)(s1, s2, size + 1), RightOOBErrorMessage(0)); 1294 free(s1); 1295 free(s2); 1296} 1297 1298TEST(AddressSanitizer, StrCatOOBTest) { 1299 size_t to_size = Ident(100); 1300 char *to = MallocAndMemsetString(to_size); 1301 to[0] = '\0'; 1302 size_t from_size = Ident(20); 1303 char *from = MallocAndMemsetString(from_size); 1304 from[from_size - 1] = '\0'; 1305 // Normal strcat calls. 1306 strcat(to, from); 1307 strcat(to, from); 1308 strcat(to + from_size, from + from_size - 2); 1309 // Passing an invalid pointer is an error even when concatenating an empty 1310 // string. 1311 EXPECT_DEATH(strcat(to - 1, from + from_size - 1), LeftOOBErrorMessage(1)); 1312 // One of arguments points to not allocated memory. 1313 EXPECT_DEATH(strcat(to - 1, from), LeftOOBErrorMessage(1)); 1314 EXPECT_DEATH(strcat(to, from - 1), LeftOOBErrorMessage(1)); 1315 EXPECT_DEATH(strcat(to + to_size, from), RightOOBErrorMessage(0)); 1316 EXPECT_DEATH(strcat(to, from + from_size), RightOOBErrorMessage(0)); 1317 1318 // "from" is not zero-terminated. 1319 from[from_size - 1] = 'z'; 1320 EXPECT_DEATH(strcat(to, from), RightOOBErrorMessage(0)); 1321 from[from_size - 1] = '\0'; 1322 // "to" is not zero-terminated. 1323 memset(to, 'z', to_size); 1324 EXPECT_DEATH(strcat(to, from), RightOOBErrorMessage(0)); 1325 // "to" is too short to fit "from". 1326 to[to_size - from_size + 1] = '\0'; 1327 EXPECT_DEATH(strcat(to, from), RightOOBErrorMessage(0)); 1328 // length of "to" is just enough. 1329 strcat(to, from + 1); 1330 1331 free(to); 1332 free(from); 1333} 1334 1335TEST(AddressSanitizer, StrNCatOOBTest) { 1336 size_t to_size = Ident(100); 1337 char *to = MallocAndMemsetString(to_size); 1338 to[0] = '\0'; 1339 size_t from_size = Ident(20); 1340 char *from = MallocAndMemsetString(from_size); 1341 // Normal strncat calls. 1342 strncat(to, from, 0); 1343 strncat(to, from, from_size); 1344 from[from_size - 1] = '\0'; 1345 strncat(to, from, 2 * from_size); 1346 // Catenating empty string with an invalid string is still an error. 1347 EXPECT_DEATH(strncat(to - 1, from, 0), LeftOOBErrorMessage(1)); 1348 strncat(to, from + from_size - 1, 10); 1349 // One of arguments points to not allocated memory. 1350 EXPECT_DEATH(strncat(to - 1, from, 2), LeftOOBErrorMessage(1)); 1351 EXPECT_DEATH(strncat(to, from - 1, 2), LeftOOBErrorMessage(1)); 1352 EXPECT_DEATH(strncat(to + to_size, from, 2), RightOOBErrorMessage(0)); 1353 EXPECT_DEATH(strncat(to, from + from_size, 2), RightOOBErrorMessage(0)); 1354 1355 memset(from, 'z', from_size); 1356 memset(to, 'z', to_size); 1357 to[0] = '\0'; 1358 // "from" is too short. 1359 EXPECT_DEATH(strncat(to, from, from_size + 1), RightOOBErrorMessage(0)); 1360 // "to" is not zero-terminated. 1361 EXPECT_DEATH(strncat(to + 1, from, 1), RightOOBErrorMessage(0)); 1362 // "to" is too short to fit "from". 1363 to[0] = 'z'; 1364 to[to_size - from_size + 1] = '\0'; 1365 EXPECT_DEATH(strncat(to, from, from_size - 1), RightOOBErrorMessage(0)); 1366 // "to" is just enough. 1367 strncat(to, from, from_size - 2); 1368 1369 free(to); 1370 free(from); 1371} 1372 1373static string OverlapErrorMessage(const string &func) { 1374 return func + "-param-overlap"; 1375} 1376 1377TEST(AddressSanitizer, StrArgsOverlapTest) { 1378 size_t size = Ident(100); 1379 char *str = Ident((char*)malloc(size)); 1380 1381// Do not check memcpy() on OS X 10.7 and later, where it actually aliases 1382// memmove(). 1383#if !defined(__APPLE__) || !defined(MAC_OS_X_VERSION_10_7) || \ 1384 (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7) 1385 // Check "memcpy". Use Ident() to avoid inlining. 1386 memset(str, 'z', size); 1387 Ident(memcpy)(str + 1, str + 11, 10); 1388 Ident(memcpy)(str, str, 0); 1389 EXPECT_DEATH(Ident(memcpy)(str, str + 14, 15), OverlapErrorMessage("memcpy")); 1390 EXPECT_DEATH(Ident(memcpy)(str + 14, str, 15), OverlapErrorMessage("memcpy")); 1391#endif 1392 1393 // We do not treat memcpy with to==from as a bug. 1394 // See http://llvm.org/bugs/show_bug.cgi?id=11763. 1395 // EXPECT_DEATH(Ident(memcpy)(str + 20, str + 20, 1), 1396 // OverlapErrorMessage("memcpy")); 1397 1398 // Check "strcpy". 1399 memset(str, 'z', size); 1400 str[9] = '\0'; 1401 strcpy(str + 10, str); 1402 EXPECT_DEATH(strcpy(str + 9, str), OverlapErrorMessage("strcpy")); 1403 EXPECT_DEATH(strcpy(str, str + 4), OverlapErrorMessage("strcpy")); 1404 strcpy(str, str + 5); 1405 1406 // Check "strncpy". 1407 memset(str, 'z', size); 1408 strncpy(str, str + 10, 10); 1409 EXPECT_DEATH(strncpy(str, str + 9, 10), OverlapErrorMessage("strncpy")); 1410 EXPECT_DEATH(strncpy(str + 9, str, 10), OverlapErrorMessage("strncpy")); 1411 str[10] = '\0'; 1412 strncpy(str + 11, str, 20); 1413 EXPECT_DEATH(strncpy(str + 10, str, 20), OverlapErrorMessage("strncpy")); 1414 1415 // Check "strcat". 1416 memset(str, 'z', size); 1417 str[10] = '\0'; 1418 str[20] = '\0'; 1419 strcat(str, str + 10); 1420 EXPECT_DEATH(strcat(str, str + 11), OverlapErrorMessage("strcat")); 1421 str[10] = '\0'; 1422 strcat(str + 11, str); 1423 EXPECT_DEATH(strcat(str, str + 9), OverlapErrorMessage("strcat")); 1424 EXPECT_DEATH(strcat(str + 9, str), OverlapErrorMessage("strcat")); 1425 EXPECT_DEATH(strcat(str + 10, str), OverlapErrorMessage("strcat")); 1426 1427 // Check "strncat". 1428 memset(str, 'z', size); 1429 str[10] = '\0'; 1430 strncat(str, str + 10, 10); // from is empty 1431 EXPECT_DEATH(strncat(str, str + 11, 10), OverlapErrorMessage("strncat")); 1432 str[10] = '\0'; 1433 str[20] = '\0'; 1434 strncat(str + 5, str, 5); 1435 str[10] = '\0'; 1436 EXPECT_DEATH(strncat(str + 5, str, 6), OverlapErrorMessage("strncat")); 1437 EXPECT_DEATH(strncat(str, str + 9, 10), OverlapErrorMessage("strncat")); 1438 1439 free(str); 1440} 1441 1442void CallAtoi(const char *nptr) { 1443 Ident(atoi(nptr)); 1444} 1445void CallAtol(const char *nptr) { 1446 Ident(atol(nptr)); 1447} 1448void CallAtoll(const char *nptr) { 1449 Ident(atoll(nptr)); 1450} 1451typedef void(*PointerToCallAtoi)(const char*); 1452 1453void RunAtoiOOBTest(PointerToCallAtoi Atoi) { 1454 char *array = MallocAndMemsetString(10, '1'); 1455 // Invalid pointer to the string. 1456 EXPECT_DEATH(Atoi(array + 11), RightOOBErrorMessage(1)); 1457 EXPECT_DEATH(Atoi(array - 1), LeftOOBErrorMessage(1)); 1458 // Die if a buffer doesn't have terminating NULL. 1459 EXPECT_DEATH(Atoi(array), RightOOBErrorMessage(0)); 1460 // Make last symbol a terminating NULL or other non-digit. 1461 array[9] = '\0'; 1462 Atoi(array); 1463 array[9] = 'a'; 1464 Atoi(array); 1465 Atoi(array + 9); 1466 // Sometimes we need to detect overflow if no digits are found. 1467 memset(array, ' ', 10); 1468 EXPECT_DEATH(Atoi(array), RightOOBErrorMessage(0)); 1469 array[9] = '-'; 1470 EXPECT_DEATH(Atoi(array), RightOOBErrorMessage(0)); 1471 EXPECT_DEATH(Atoi(array + 9), RightOOBErrorMessage(0)); 1472 array[8] = '-'; 1473 Atoi(array); 1474 delete array; 1475} 1476 1477TEST(AddressSanitizer, AtoiAndFriendsOOBTest) { 1478 RunAtoiOOBTest(&CallAtoi); 1479 RunAtoiOOBTest(&CallAtol); 1480 RunAtoiOOBTest(&CallAtoll); 1481} 1482 1483void CallStrtol(const char *nptr, char **endptr, int base) { 1484 Ident(strtol(nptr, endptr, base)); 1485} 1486void CallStrtoll(const char *nptr, char **endptr, int base) { 1487 Ident(strtoll(nptr, endptr, base)); 1488} 1489typedef void(*PointerToCallStrtol)(const char*, char**, int); 1490 1491void RunStrtolOOBTest(PointerToCallStrtol Strtol) { 1492 char *array = MallocAndMemsetString(3); 1493 char *endptr = NULL; 1494 array[0] = '1'; 1495 array[1] = '2'; 1496 array[2] = '3'; 1497 // Invalid pointer to the string. 1498 EXPECT_DEATH(Strtol(array + 3, NULL, 0), RightOOBErrorMessage(0)); 1499 EXPECT_DEATH(Strtol(array - 1, NULL, 0), LeftOOBErrorMessage(1)); 1500 // Buffer overflow if there is no terminating null (depends on base). 1501 Strtol(array, &endptr, 3); 1502 EXPECT_EQ(array + 2, endptr); 1503 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBErrorMessage(0)); 1504 array[2] = 'z'; 1505 Strtol(array, &endptr, 35); 1506 EXPECT_EQ(array + 2, endptr); 1507 EXPECT_DEATH(Strtol(array, NULL, 36), RightOOBErrorMessage(0)); 1508 // Add terminating zero to get rid of overflow. 1509 array[2] = '\0'; 1510 Strtol(array, NULL, 36); 1511 // Don't check for overflow if base is invalid. 1512 Strtol(array - 1, NULL, -1); 1513 Strtol(array + 3, NULL, 1); 1514 // Sometimes we need to detect overflow if no digits are found. 1515 array[0] = array[1] = array[2] = ' '; 1516 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBErrorMessage(0)); 1517 array[2] = '+'; 1518 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBErrorMessage(0)); 1519 array[2] = '-'; 1520 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBErrorMessage(0)); 1521 array[1] = '+'; 1522 Strtol(array, NULL, 0); 1523 array[1] = array[2] = 'z'; 1524 Strtol(array, &endptr, 0); 1525 EXPECT_EQ(array, endptr); 1526 Strtol(array + 2, NULL, 0); 1527 EXPECT_EQ(array, endptr); 1528 delete array; 1529} 1530 1531TEST(AddressSanitizer, StrtollOOBTest) { 1532 RunStrtolOOBTest(&CallStrtoll); 1533} 1534TEST(AddressSanitizer, StrtolOOBTest) { 1535 RunStrtolOOBTest(&CallStrtol); 1536} 1537 1538// At the moment we instrument memcpy/memove/memset calls at compile time so we 1539// can't handle OOB error if these functions are called by pointer, see disabled 1540// MemIntrinsicCallByPointerTest below 1541typedef void*(*PointerToMemTransfer)(void*, const void*, size_t); 1542typedef void*(*PointerToMemSet)(void*, int, size_t); 1543 1544void CallMemSetByPointer(PointerToMemSet MemSet) { 1545 size_t size = Ident(100); 1546 char *array = Ident((char*)malloc(size)); 1547 EXPECT_DEATH(MemSet(array, 0, 101), RightOOBErrorMessage(0)); 1548 free(array); 1549} 1550 1551void CallMemTransferByPointer(PointerToMemTransfer MemTransfer) { 1552 size_t size = Ident(100); 1553 char *src = Ident((char*)malloc(size)); 1554 char *dst = Ident((char*)malloc(size)); 1555 EXPECT_DEATH(MemTransfer(dst, src, 101), RightOOBErrorMessage(0)); 1556 free(src); 1557 free(dst); 1558} 1559 1560TEST(AddressSanitizer, DISABLED_MemIntrinsicCallByPointerTest) { 1561 CallMemSetByPointer(&memset); 1562 CallMemTransferByPointer(&memcpy); 1563 CallMemTransferByPointer(&memmove); 1564} 1565 1566// This test case fails 1567// Clang optimizes memcpy/memset calls which lead to unaligned access 1568TEST(AddressSanitizer, DISABLED_MemIntrinsicUnalignedAccessTest) { 1569 int size = Ident(4096); 1570 char *s = Ident((char*)malloc(size)); 1571 EXPECT_DEATH(memset(s + size - 1, 0, 2), RightOOBErrorMessage(0)); 1572 free(s); 1573} 1574 1575// TODO(samsonov): Add a test with malloc(0) 1576// TODO(samsonov): Add tests for str* and mem* functions. 1577 1578NOINLINE static int LargeFunction(bool do_bad_access) { 1579 int *x = new int[100]; 1580 x[0]++; 1581 x[1]++; 1582 x[2]++; 1583 x[3]++; 1584 x[4]++; 1585 x[5]++; 1586 x[6]++; 1587 x[7]++; 1588 x[8]++; 1589 x[9]++; 1590 1591 x[do_bad_access ? 100 : 0]++; int res = __LINE__; 1592 1593 x[10]++; 1594 x[11]++; 1595 x[12]++; 1596 x[13]++; 1597 x[14]++; 1598 x[15]++; 1599 x[16]++; 1600 x[17]++; 1601 x[18]++; 1602 x[19]++; 1603 1604 delete x; 1605 return res; 1606} 1607 1608// Test the we have correct debug info for the failing instruction. 1609// This test requires the in-process symbolizer to be enabled by default. 1610TEST(AddressSanitizer, DISABLED_LargeFunctionSymbolizeTest) { 1611 int failing_line = LargeFunction(false); 1612 char expected_warning[128]; 1613 sprintf(expected_warning, "LargeFunction.*asan_test.*:%d", failing_line); 1614 EXPECT_DEATH(LargeFunction(true), expected_warning); 1615} 1616 1617// Check that we unwind and symbolize correctly. 1618TEST(AddressSanitizer, DISABLED_MallocFreeUnwindAndSymbolizeTest) { 1619 int *a = (int*)malloc_aaa(sizeof(int)); 1620 *a = 1; 1621 free_aaa(a); 1622 EXPECT_DEATH(*a = 1, "free_ccc.*free_bbb.*free_aaa.*" 1623 "malloc_fff.*malloc_eee.*malloc_ddd"); 1624} 1625 1626static void TryToSetThreadName(const char *name) { 1627#ifdef __linux__ 1628 prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); 1629#endif 1630} 1631 1632void *ThreadedTestAlloc(void *a) { 1633 TryToSetThreadName("AllocThr"); 1634 int **p = (int**)a; 1635 *p = new int; 1636 return 0; 1637} 1638 1639void *ThreadedTestFree(void *a) { 1640 TryToSetThreadName("FreeThr"); 1641 int **p = (int**)a; 1642 delete *p; 1643 return 0; 1644} 1645 1646void *ThreadedTestUse(void *a) { 1647 TryToSetThreadName("UseThr"); 1648 int **p = (int**)a; 1649 **p = 1; 1650 return 0; 1651} 1652 1653void ThreadedTestSpawn() { 1654 pthread_t t; 1655 int *x; 1656 PTHREAD_CREATE(&t, 0, ThreadedTestAlloc, &x); 1657 PTHREAD_JOIN(t, 0); 1658 PTHREAD_CREATE(&t, 0, ThreadedTestFree, &x); 1659 PTHREAD_JOIN(t, 0); 1660 PTHREAD_CREATE(&t, 0, ThreadedTestUse, &x); 1661 PTHREAD_JOIN(t, 0); 1662} 1663 1664TEST(AddressSanitizer, ThreadedTest) { 1665 EXPECT_DEATH(ThreadedTestSpawn(), 1666 ASAN_PCRE_DOTALL 1667 "Thread T.*created" 1668 ".*Thread T.*created" 1669 ".*Thread T.*created"); 1670} 1671 1672#ifdef __linux__ 1673TEST(AddressSanitizer, ThreadNamesTest) { 1674 // ThreadedTestSpawn(); 1675 EXPECT_DEATH(ThreadedTestSpawn(), 1676 ASAN_PCRE_DOTALL 1677 "WRITE .*thread T. .UseThr." 1678 ".*freed by thread T. .FreeThr. here:" 1679 ".*previously allocated by thread T. .AllocThr. here:" 1680 ".*Thread T. .UseThr. created by T. here:" 1681 ".*Thread T. .FreeThr. created by T. here:" 1682 ".*Thread T. .AllocThr. created by T. here:" 1683 ""); 1684} 1685#endif 1686 1687#if ASAN_NEEDS_SEGV 1688TEST(AddressSanitizer, ShadowGapTest) { 1689#if SANITIZER_WORDSIZE == 32 1690 char *addr = (char*)0x22000000; 1691#else 1692 char *addr = (char*)0x0000100000080000; 1693#endif 1694 EXPECT_DEATH(*addr = 1, "AddressSanitizer: SEGV on unknown"); 1695} 1696#endif // ASAN_NEEDS_SEGV 1697 1698extern "C" { 1699NOINLINE static void UseThenFreeThenUse() { 1700 char *x = Ident((char*)malloc(8)); 1701 *x = 1; 1702 free_aaa(x); 1703 *x = 2; 1704} 1705} 1706 1707TEST(AddressSanitizer, UseThenFreeThenUseTest) { 1708 EXPECT_DEATH(UseThenFreeThenUse(), "freed by thread"); 1709} 1710 1711TEST(AddressSanitizer, StrDupTest) { 1712 free(strdup(Ident("123"))); 1713} 1714 1715// Currently we create and poison redzone at right of global variables. 1716char glob5[5]; 1717static char static110[110]; 1718const char ConstGlob[7] = {1, 2, 3, 4, 5, 6, 7}; 1719static const char StaticConstGlob[3] = {9, 8, 7}; 1720extern int GlobalsTest(int x); 1721 1722TEST(AddressSanitizer, GlobalTest) { 1723 static char func_static15[15]; 1724 1725 static char fs1[10]; 1726 static char fs2[10]; 1727 static char fs3[10]; 1728 1729 glob5[Ident(0)] = 0; 1730 glob5[Ident(1)] = 0; 1731 glob5[Ident(2)] = 0; 1732 glob5[Ident(3)] = 0; 1733 glob5[Ident(4)] = 0; 1734 1735 EXPECT_DEATH(glob5[Ident(5)] = 0, 1736 "0 bytes to the right of global variable.*glob5.* size 5"); 1737 EXPECT_DEATH(glob5[Ident(5+6)] = 0, 1738 "6 bytes to the right of global variable.*glob5.* size 5"); 1739 Ident(static110); // avoid optimizations 1740 static110[Ident(0)] = 0; 1741 static110[Ident(109)] = 0; 1742 EXPECT_DEATH(static110[Ident(110)] = 0, 1743 "0 bytes to the right of global variable"); 1744 EXPECT_DEATH(static110[Ident(110+7)] = 0, 1745 "7 bytes to the right of global variable"); 1746 1747 Ident(func_static15); // avoid optimizations 1748 func_static15[Ident(0)] = 0; 1749 EXPECT_DEATH(func_static15[Ident(15)] = 0, 1750 "0 bytes to the right of global variable"); 1751 EXPECT_DEATH(func_static15[Ident(15 + 9)] = 0, 1752 "9 bytes to the right of global variable"); 1753 1754 Ident(fs1); 1755 Ident(fs2); 1756 Ident(fs3); 1757 1758 // We don't create left redzones, so this is not 100% guaranteed to fail. 1759 // But most likely will. 1760 EXPECT_DEATH(fs2[Ident(-1)] = 0, "is located.*of global variable"); 1761 1762 EXPECT_DEATH(Ident(Ident(ConstGlob)[8]), 1763 "is located 1 bytes to the right of .*ConstGlob"); 1764 EXPECT_DEATH(Ident(Ident(StaticConstGlob)[5]), 1765 "is located 2 bytes to the right of .*StaticConstGlob"); 1766 1767 // call stuff from another file. 1768 GlobalsTest(0); 1769} 1770 1771TEST(AddressSanitizer, GlobalStringConstTest) { 1772 static const char *zoo = "FOOBAR123"; 1773 const char *p = Ident(zoo); 1774 EXPECT_DEATH(Ident(p[15]), "is ascii string 'FOOBAR123'"); 1775} 1776 1777TEST(AddressSanitizer, FileNameInGlobalReportTest) { 1778 static char zoo[10]; 1779 const char *p = Ident(zoo); 1780 // The file name should be present in the report. 1781 EXPECT_DEATH(Ident(p[15]), "zoo.*asan_test."); 1782} 1783 1784int *ReturnsPointerToALocalObject() { 1785 int a = 0; 1786 return Ident(&a); 1787} 1788 1789#if ASAN_UAR == 1 1790TEST(AddressSanitizer, LocalReferenceReturnTest) { 1791 int *(*f)() = Ident(ReturnsPointerToALocalObject); 1792 int *p = f(); 1793 // Call 'f' a few more times, 'p' should still be poisoned. 1794 for (int i = 0; i < 32; i++) 1795 f(); 1796 EXPECT_DEATH(*p = 1, "AddressSanitizer: stack-use-after-return"); 1797 EXPECT_DEATH(*p = 1, "is located.*in frame .*ReturnsPointerToALocal"); 1798} 1799#endif 1800 1801template <int kSize> 1802NOINLINE static void FuncWithStack() { 1803 char x[kSize]; 1804 Ident(x)[0] = 0; 1805 Ident(x)[kSize-1] = 0; 1806} 1807 1808static void LotsOfStackReuse() { 1809 int LargeStack[10000]; 1810 Ident(LargeStack)[0] = 0; 1811 for (int i = 0; i < 10000; i++) { 1812 FuncWithStack<128 * 1>(); 1813 FuncWithStack<128 * 2>(); 1814 FuncWithStack<128 * 4>(); 1815 FuncWithStack<128 * 8>(); 1816 FuncWithStack<128 * 16>(); 1817 FuncWithStack<128 * 32>(); 1818 FuncWithStack<128 * 64>(); 1819 FuncWithStack<128 * 128>(); 1820 FuncWithStack<128 * 256>(); 1821 FuncWithStack<128 * 512>(); 1822 Ident(LargeStack)[0] = 0; 1823 } 1824} 1825 1826TEST(AddressSanitizer, StressStackReuseTest) { 1827 LotsOfStackReuse(); 1828} 1829 1830TEST(AddressSanitizer, ThreadedStressStackReuseTest) { 1831 const int kNumThreads = 20; 1832 pthread_t t[kNumThreads]; 1833 for (int i = 0; i < kNumThreads; i++) { 1834 PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0); 1835 } 1836 for (int i = 0; i < kNumThreads; i++) { 1837 PTHREAD_JOIN(t[i], 0); 1838 } 1839} 1840 1841static void *PthreadExit(void *a) { 1842 pthread_exit(0); 1843 return 0; 1844} 1845 1846TEST(AddressSanitizer, PthreadExitTest) { 1847 pthread_t t; 1848 for (int i = 0; i < 1000; i++) { 1849 PTHREAD_CREATE(&t, 0, PthreadExit, 0); 1850 PTHREAD_JOIN(t, 0); 1851 } 1852} 1853 1854#ifdef __EXCEPTIONS 1855NOINLINE static void StackReuseAndException() { 1856 int large_stack[1000]; 1857 Ident(large_stack); 1858 ASAN_THROW(1); 1859} 1860 1861// TODO(kcc): support exceptions with use-after-return. 1862TEST(AddressSanitizer, DISABLED_StressStackReuseAndExceptionsTest) { 1863 for (int i = 0; i < 10000; i++) { 1864 try { 1865 StackReuseAndException(); 1866 } catch(...) { 1867 } 1868 } 1869} 1870#endif 1871 1872TEST(AddressSanitizer, MlockTest) { 1873 EXPECT_EQ(0, mlockall(MCL_CURRENT)); 1874 EXPECT_EQ(0, mlock((void*)0x12345, 0x5678)); 1875 EXPECT_EQ(0, munlockall()); 1876 EXPECT_EQ(0, munlock((void*)0x987, 0x654)); 1877} 1878 1879struct LargeStruct { 1880 int foo[100]; 1881}; 1882 1883// Test for bug http://llvm.org/bugs/show_bug.cgi?id=11763. 1884// Struct copy should not cause asan warning even if lhs == rhs. 1885TEST(AddressSanitizer, LargeStructCopyTest) { 1886 LargeStruct a; 1887 *Ident(&a) = *Ident(&a); 1888} 1889 1890ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS 1891static void NoAddressSafety() { 1892 char *foo = new char[10]; 1893 Ident(foo)[10] = 0; 1894 delete [] foo; 1895} 1896 1897TEST(AddressSanitizer, AttributeNoAddressSafetyTest) { 1898 Ident(NoAddressSafety)(); 1899} 1900 1901// ------------------ demo tests; run each one-by-one ------------- 1902// e.g. --gtest_filter=*DemoOOBLeftHigh --gtest_also_run_disabled_tests 1903TEST(AddressSanitizer, DISABLED_DemoThreadedTest) { 1904 ThreadedTestSpawn(); 1905} 1906 1907void *SimpleBugOnSTack(void *x = 0) { 1908 char a[20]; 1909 Ident(a)[20] = 0; 1910 return 0; 1911} 1912 1913TEST(AddressSanitizer, DISABLED_DemoStackTest) { 1914 SimpleBugOnSTack(); 1915} 1916 1917TEST(AddressSanitizer, DISABLED_DemoThreadStackTest) { 1918 pthread_t t; 1919 PTHREAD_CREATE(&t, 0, SimpleBugOnSTack, 0); 1920 PTHREAD_JOIN(t, 0); 1921} 1922 1923TEST(AddressSanitizer, DISABLED_DemoUAFLowIn) { 1924 uaf_test<U1>(10, 0); 1925} 1926TEST(AddressSanitizer, DISABLED_DemoUAFLowLeft) { 1927 uaf_test<U1>(10, -2); 1928} 1929TEST(AddressSanitizer, DISABLED_DemoUAFLowRight) { 1930 uaf_test<U1>(10, 10); 1931} 1932 1933TEST(AddressSanitizer, DISABLED_DemoUAFHigh) { 1934 uaf_test<U1>(kLargeMalloc, 0); 1935} 1936 1937TEST(AddressSanitizer, DISABLED_DemoOOBLeftLow) { 1938 oob_test<U1>(10, -1); 1939} 1940 1941TEST(AddressSanitizer, DISABLED_DemoOOBLeftHigh) { 1942 oob_test<U1>(kLargeMalloc, -1); 1943} 1944 1945TEST(AddressSanitizer, DISABLED_DemoOOBRightLow) { 1946 oob_test<U1>(10, 10); 1947} 1948 1949TEST(AddressSanitizer, DISABLED_DemoOOBRightHigh) { 1950 oob_test<U1>(kLargeMalloc, kLargeMalloc); 1951} 1952 1953TEST(AddressSanitizer, DISABLED_DemoOOM) { 1954 size_t size = SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 40) : (0xf0000000); 1955 printf("%p\n", malloc(size)); 1956} 1957 1958TEST(AddressSanitizer, DISABLED_DemoDoubleFreeTest) { 1959 DoubleFree(); 1960} 1961 1962TEST(AddressSanitizer, DISABLED_DemoNullDerefTest) { 1963 int *a = 0; 1964 Ident(a)[10] = 0; 1965} 1966 1967TEST(AddressSanitizer, DISABLED_DemoFunctionStaticTest) { 1968 static char a[100]; 1969 static char b[100]; 1970 static char c[100]; 1971 Ident(a); 1972 Ident(b); 1973 Ident(c); 1974 Ident(a)[5] = 0; 1975 Ident(b)[105] = 0; 1976 Ident(a)[5] = 0; 1977} 1978 1979TEST(AddressSanitizer, DISABLED_DemoTooMuchMemoryTest) { 1980 const size_t kAllocSize = (1 << 28) - 1024; 1981 size_t total_size = 0; 1982 while (true) { 1983 char *x = (char*)malloc(kAllocSize); 1984 memset(x, 0, kAllocSize); 1985 total_size += kAllocSize; 1986 fprintf(stderr, "total: %ldM %p\n", (long)total_size >> 20, x); 1987 } 1988} 1989 1990// http://code.google.com/p/address-sanitizer/issues/detail?id=66 1991TEST(AddressSanitizer, BufferOverflowAfterManyFrees) { 1992 for (int i = 0; i < 1000000; i++) { 1993 delete [] (Ident(new char [8644])); 1994 } 1995 char *x = new char[8192]; 1996 EXPECT_DEATH(x[Ident(8192)] = 0, "AddressSanitizer: heap-buffer-overflow"); 1997 delete [] Ident(x); 1998} 1999 2000#ifdef __APPLE__ 2001#include "asan_mac_test.h" 2002TEST(AddressSanitizerMac, CFAllocatorDefaultDoubleFree) { 2003 EXPECT_DEATH( 2004 CFAllocatorDefaultDoubleFree(NULL), 2005 "attempting double-free"); 2006} 2007 2008void CFAllocator_DoubleFreeOnPthread() { 2009 pthread_t child; 2010 PTHREAD_CREATE(&child, NULL, CFAllocatorDefaultDoubleFree, NULL); 2011 PTHREAD_JOIN(child, NULL); // Shouldn't be reached. 2012} 2013 2014TEST(AddressSanitizerMac, CFAllocatorDefaultDoubleFree_ChildPhread) { 2015 EXPECT_DEATH(CFAllocator_DoubleFreeOnPthread(), "attempting double-free"); 2016} 2017 2018namespace { 2019 2020void *GLOB; 2021 2022void *CFAllocatorAllocateToGlob(void *unused) { 2023 GLOB = CFAllocatorAllocate(NULL, 100, /*hint*/0); 2024 return NULL; 2025} 2026 2027void *CFAllocatorDeallocateFromGlob(void *unused) { 2028 char *p = (char*)GLOB; 2029 p[100] = 'A'; // ASan should report an error here. 2030 CFAllocatorDeallocate(NULL, GLOB); 2031 return NULL; 2032} 2033 2034void CFAllocator_PassMemoryToAnotherThread() { 2035 pthread_t th1, th2; 2036 PTHREAD_CREATE(&th1, NULL, CFAllocatorAllocateToGlob, NULL); 2037 PTHREAD_JOIN(th1, NULL); 2038 PTHREAD_CREATE(&th2, NULL, CFAllocatorDeallocateFromGlob, NULL); 2039 PTHREAD_JOIN(th2, NULL); 2040} 2041 2042TEST(AddressSanitizerMac, CFAllocator_PassMemoryToAnotherThread) { 2043 EXPECT_DEATH(CFAllocator_PassMemoryToAnotherThread(), 2044 "heap-buffer-overflow"); 2045} 2046 2047} // namespace 2048 2049// TODO(glider): figure out whether we still need these tests. Is it correct 2050// to intercept the non-default CFAllocators? 2051TEST(AddressSanitizerMac, DISABLED_CFAllocatorSystemDefaultDoubleFree) { 2052 EXPECT_DEATH( 2053 CFAllocatorSystemDefaultDoubleFree(), 2054 "attempting double-free"); 2055} 2056 2057// We're intercepting malloc, so kCFAllocatorMalloc is routed to ASan. 2058TEST(AddressSanitizerMac, CFAllocatorMallocDoubleFree) { 2059 EXPECT_DEATH(CFAllocatorMallocDoubleFree(), "attempting double-free"); 2060} 2061 2062TEST(AddressSanitizerMac, DISABLED_CFAllocatorMallocZoneDoubleFree) { 2063 EXPECT_DEATH(CFAllocatorMallocZoneDoubleFree(), "attempting double-free"); 2064} 2065 2066TEST(AddressSanitizerMac, GCDDispatchAsync) { 2067 // Make sure the whole ASan report is printed, i.e. that we don't die 2068 // on a CHECK. 2069 EXPECT_DEATH(TestGCDDispatchAsync(), "Shadow byte and word"); 2070} 2071 2072TEST(AddressSanitizerMac, GCDDispatchSync) { 2073 // Make sure the whole ASan report is printed, i.e. that we don't die 2074 // on a CHECK. 2075 EXPECT_DEATH(TestGCDDispatchSync(), "Shadow byte and word"); 2076} 2077 2078 2079TEST(AddressSanitizerMac, GCDReuseWqthreadsAsync) { 2080 // Make sure the whole ASan report is printed, i.e. that we don't die 2081 // on a CHECK. 2082 EXPECT_DEATH(TestGCDReuseWqthreadsAsync(), "Shadow byte and word"); 2083} 2084 2085TEST(AddressSanitizerMac, GCDReuseWqthreadsSync) { 2086 // Make sure the whole ASan report is printed, i.e. that we don't die 2087 // on a CHECK. 2088 EXPECT_DEATH(TestGCDReuseWqthreadsSync(), "Shadow byte and word"); 2089} 2090 2091TEST(AddressSanitizerMac, GCDDispatchAfter) { 2092 // Make sure the whole ASan report is printed, i.e. that we don't die 2093 // on a CHECK. 2094 EXPECT_DEATH(TestGCDDispatchAfter(), "Shadow byte and word"); 2095} 2096 2097TEST(AddressSanitizerMac, GCDSourceEvent) { 2098 // Make sure the whole ASan report is printed, i.e. that we don't die 2099 // on a CHECK. 2100 EXPECT_DEATH(TestGCDSourceEvent(), "Shadow byte and word"); 2101} 2102 2103TEST(AddressSanitizerMac, GCDSourceCancel) { 2104 // Make sure the whole ASan report is printed, i.e. that we don't die 2105 // on a CHECK. 2106 EXPECT_DEATH(TestGCDSourceCancel(), "Shadow byte and word"); 2107} 2108 2109TEST(AddressSanitizerMac, GCDGroupAsync) { 2110 // Make sure the whole ASan report is printed, i.e. that we don't die 2111 // on a CHECK. 2112 EXPECT_DEATH(TestGCDGroupAsync(), "Shadow byte and word"); 2113} 2114 2115void *MallocIntrospectionLockWorker(void *_) { 2116 const int kNumPointers = 100; 2117 int i; 2118 void *pointers[kNumPointers]; 2119 for (i = 0; i < kNumPointers; i++) { 2120 pointers[i] = malloc(i + 1); 2121 } 2122 for (i = 0; i < kNumPointers; i++) { 2123 free(pointers[i]); 2124 } 2125 2126 return NULL; 2127} 2128 2129void *MallocIntrospectionLockForker(void *_) { 2130 pid_t result = fork(); 2131 if (result == -1) { 2132 perror("fork"); 2133 } 2134 assert(result != -1); 2135 if (result == 0) { 2136 // Call malloc in the child process to make sure we won't deadlock. 2137 void *ptr = malloc(42); 2138 free(ptr); 2139 exit(0); 2140 } else { 2141 // Return in the parent process. 2142 return NULL; 2143 } 2144} 2145 2146TEST(AddressSanitizerMac, MallocIntrospectionLock) { 2147 // Incorrect implementation of force_lock and force_unlock in our malloc zone 2148 // will cause forked processes to deadlock. 2149 // TODO(glider): need to detect that none of the child processes deadlocked. 2150 const int kNumWorkers = 5, kNumIterations = 100; 2151 int i, iter; 2152 for (iter = 0; iter < kNumIterations; iter++) { 2153 pthread_t workers[kNumWorkers], forker; 2154 for (i = 0; i < kNumWorkers; i++) { 2155 PTHREAD_CREATE(&workers[i], 0, MallocIntrospectionLockWorker, 0); 2156 } 2157 PTHREAD_CREATE(&forker, 0, MallocIntrospectionLockForker, 0); 2158 for (i = 0; i < kNumWorkers; i++) { 2159 PTHREAD_JOIN(workers[i], 0); 2160 } 2161 PTHREAD_JOIN(forker, 0); 2162 } 2163} 2164 2165void *TSDAllocWorker(void *test_key) { 2166 if (test_key) { 2167 void *mem = malloc(10); 2168 pthread_setspecific(*(pthread_key_t*)test_key, mem); 2169 } 2170 return NULL; 2171} 2172 2173TEST(AddressSanitizerMac, DISABLED_TSDWorkqueueTest) { 2174 pthread_t th; 2175 pthread_key_t test_key; 2176 pthread_key_create(&test_key, CallFreeOnWorkqueue); 2177 PTHREAD_CREATE(&th, NULL, TSDAllocWorker, &test_key); 2178 PTHREAD_JOIN(th, NULL); 2179 pthread_key_delete(test_key); 2180} 2181 2182// Test that CFStringCreateCopy does not copy constant strings. 2183TEST(AddressSanitizerMac, CFStringCreateCopy) { 2184 CFStringRef str = CFSTR("Hello world!\n"); 2185 CFStringRef str2 = CFStringCreateCopy(0, str); 2186 EXPECT_EQ(str, str2); 2187} 2188 2189TEST(AddressSanitizerMac, NSObjectOOB) { 2190 // Make sure that our allocators are used for NSObjects. 2191 EXPECT_DEATH(TestOOBNSObjects(), "heap-buffer-overflow"); 2192} 2193 2194// Make sure that correct pointer is passed to free() when deallocating a 2195// NSURL object. 2196// See http://code.google.com/p/address-sanitizer/issues/detail?id=70. 2197TEST(AddressSanitizerMac, NSURLDeallocation) { 2198 TestNSURLDeallocation(); 2199} 2200 2201// See http://code.google.com/p/address-sanitizer/issues/detail?id=109. 2202TEST(AddressSanitizerMac, Mstats) { 2203 malloc_statistics_t stats1, stats2; 2204 malloc_zone_statistics(/*all zones*/NULL, &stats1); 2205 const int kMallocSize = 100000; 2206 void *alloc = Ident(malloc(kMallocSize)); 2207 malloc_zone_statistics(/*all zones*/NULL, &stats2); 2208 EXPECT_GT(stats2.blocks_in_use, stats1.blocks_in_use); 2209 EXPECT_GE(stats2.size_in_use - stats1.size_in_use, kMallocSize); 2210 free(alloc); 2211 // Even the default OSX allocator may not change the stats after free(). 2212} 2213#endif // __APPLE__ 2214 2215// Test that instrumentation of stack allocations takes into account 2216// AllocSize of a type, and not its StoreSize (16 vs 10 bytes for long double). 2217// See http://llvm.org/bugs/show_bug.cgi?id=12047 for more details. 2218TEST(AddressSanitizer, LongDoubleNegativeTest) { 2219 long double a, b; 2220 static long double c; 2221 memcpy(Ident(&a), Ident(&b), sizeof(long double)); 2222 memcpy(Ident(&c), Ident(&b), sizeof(long double)); 2223} 2224