asan_test.cc revision c311105fd64219bb96b6467c3707ebda355be87b
17ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen//===-- asan_test.cc ------------------------------------------------------===// 27ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// 37ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// The LLVM Compiler Infrastructure 47ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// 57ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// This file is distributed under the University of Illinois Open Source 67ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// License. See LICENSE.TXT for details. 77ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// 87ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen//===----------------------------------------------------------------------===// 97ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// 107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// This file is a part of AddressSanitizer, an address sanity checker. 117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// 127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen//===----------------------------------------------------------------------===// 137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <stdio.h> 147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <signal.h> 157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <stdlib.h> 167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <string.h> 177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <strings.h> 187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <pthread.h> 197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <stdint.h> 207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <setjmp.h> 217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <assert.h> 227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef __linux__ 247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen# include <sys/prctl.h> 257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen# include <sys/types.h> 267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen# include <sys/stat.h> 277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen# include <fcntl.h> 287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if defined(__i386__) || defined(__x86_64__) 317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <emmintrin.h> 327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include "asan_test_utils.h" 357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef __APPLE__ 377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <malloc.h> 387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#else 397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <malloc/malloc.h> 407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <AvailabilityMacros.h> // For MAC_OS_X_VERSION_* 417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <CoreFoundation/CFString.h> 427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif // __APPLE__ 437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if ASAN_HAS_EXCEPTIONS 457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen# define ASAN_THROW(x) throw (x) 467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#else 477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen# define ASAN_THROW(x) 487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#include <sys/mman.h> 517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentypedef uint8_t U1; 537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentypedef uint16_t U2; 547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentypedef uint32_t U4; 557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentypedef uint64_t U8; 567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic const int kPageSize = 4096; 587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Simple stand-alone pseudorandom number generator. 607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Current algorithm is ANSI C linear congruential PRNG. 617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic inline uint32_t my_rand(uint32_t* state) { 627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return (*state = *state * 1103515245 + 12345) >> 16; 637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic uint32_t global_seed = 0; 667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenconst size_t kLargeMalloc = 1 << 24; 687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentemplate<typename T> 707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void asan_write(T *a) { 717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *a = 0; 727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void asan_write_sized_aligned(uint8_t *p, size_t size) { 757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0U, ((uintptr_t)p % size)); 767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (size == 1) asan_write((uint8_t*)p); 777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen else if (size == 2) asan_write((uint16_t*)p); 787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen else if (size == 4) asan_write((uint32_t*)p); 797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen else if (size == 8) asan_write((uint64_t*)p); 807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *malloc_fff(size_t size) { 837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = malloc/**/(size); break_optimization(0); return res;} 847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *malloc_eee(size_t size) { 857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = malloc_fff(size); break_optimization(0); return res;} 867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *malloc_ddd(size_t size) { 877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = malloc_eee(size); break_optimization(0); return res;} 887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *malloc_ccc(size_t size) { 897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = malloc_ddd(size); break_optimization(0); return res;} 907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *malloc_bbb(size_t size) { 917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = malloc_ccc(size); break_optimization(0); return res;} 927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *malloc_aaa(size_t size) { 937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = malloc_bbb(size); break_optimization(0); return res;} 947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef __APPLE__ 967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *memalign_fff(size_t alignment, size_t size) { 977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = memalign/**/(alignment, size); break_optimization(0); return res;} 987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *memalign_eee(size_t alignment, size_t size) { 997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = memalign_fff(alignment, size); break_optimization(0); return res;} 1007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *memalign_ddd(size_t alignment, size_t size) { 1017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = memalign_eee(alignment, size); break_optimization(0); return res;} 1027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *memalign_ccc(size_t alignment, size_t size) { 1037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = memalign_ddd(alignment, size); break_optimization(0); return res;} 1047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *memalign_bbb(size_t alignment, size_t size) { 1057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = memalign_ccc(alignment, size); break_optimization(0); return res;} 1067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void *memalign_aaa(size_t alignment, size_t size) { 1077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *res = memalign_bbb(alignment, size); break_optimization(0); return res;} 1087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif // __APPLE__ 1097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void free_ccc(void *p) { free(p); break_optimization(0);} 1127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void free_bbb(void *p) { free_ccc(p); break_optimization(0);} 1137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void free_aaa(void *p) { free_bbb(p); break_optimization(0);} 1147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentemplate<typename T> 1167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void oob_test(int size, int off) { 1177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *p = (char*)malloc_aaa(size); 1187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // fprintf(stderr, "writing %d byte(s) into [%p,%p) with offset %d\n", 1197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // sizeof(T), p, p + size, off); 1207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen asan_write((T*)(p + off)); 1217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free_aaa(p); 1227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 1237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentemplate<typename T> 1267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void uaf_test(int size, int off) { 1277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *p = (char *)malloc_aaa(size); 1287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free_aaa(p); 1297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = 1; i < 100; i++) 1307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free_aaa(malloc_aaa(i)); 1317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen fprintf(stderr, "writing %ld byte(s) at %p with offset %d\n", 1327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen (long)sizeof(T), p, off); 1337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen asan_write((T*)(p + off)); 1347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 1357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, HasFeatureAddressSanitizerTest) { 1377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if defined(__has_feature) && __has_feature(address_sanitizer) 1387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen bool asan = 1; 1397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#elif defined(__SANITIZE_ADDRESS__) 1407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen bool asan = 1; 1417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#else 1427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen bool asan = 0; 1437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 1447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(true, asan); 1457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 1467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, SimpleDeathTest) { 1487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(exit(1), ""); 1497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 1507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, VariousMallocsTest) { 1527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *a = (int*)malloc(100 * sizeof(int)); 1537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen a[50] = 0; 1547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(a); 1557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *r = (int*)malloc(10); 1577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen r = (int*)realloc(r, 2000 * sizeof(int)); 1587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen r[1000] = 0; 1597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(r); 1607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *b = new int[100]; 1627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen b[50] = 0; 1637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen delete [] b; 1647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *c = new int; 1667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *c = 0; 1677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen delete c; 1687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if !defined(__APPLE__) && !defined(ANDROID) && !defined(__ANDROID__) 1707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *pm; 1717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int pm_res = posix_memalign((void**)&pm, kPageSize, kPageSize); 1727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, pm_res); 1737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(pm); 1747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 1757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if !defined(__APPLE__) 1777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *ma = (int*)memalign(kPageSize, kPageSize); 1787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0U, (uintptr_t)ma % kPageSize); 1797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ma[123] = 0; 1807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(ma); 1817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif // __APPLE__ 1827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 1837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, CallocTest) { 1857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *a = (int*)calloc(100, sizeof(int)); 1867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, a[10]); 1877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(a); 1887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 1897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, VallocTest) { 1917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *a = valloc(100); 1927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 1937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(a); 1947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 1957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 1967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef __APPLE__ 1977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, PvallocTest) { 1987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *a = (char*)pvalloc(kPageSize + 100); 1997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 2007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen a[kPageSize + 101] = 1; // we should not report an error here. 2017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(a); 2027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen a = (char*)pvalloc(0); // pvalloc(0) should allocate at least one page. 2047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0U, (uintptr_t)a % kPageSize); 2057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen a[101] = 1; // we should not report an error here. 2067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(a); 2077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 2087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif // __APPLE__ 2097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid *TSDWorker(void *test_key) { 2117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (test_key) { 2127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_setspecific(*(pthread_key_t*)test_key, (void*)0xfeedface); 2137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 2147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return NULL; 2157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 2167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid TSDDestructor(void *tsd) { 2187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Spawning a thread will check that the current thread id is not -1. 2197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_t th; 2207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_CREATE(&th, NULL, TSDWorker, NULL); 2217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_JOIN(th, NULL); 2227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 2237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// This tests triggers the thread-specific data destruction fiasco which occurs 2257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// if we don't manage the TSD destructors ourselves. We create a new pthread 2267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// key with a non-NULL destructor which is likely to be put after the destructor 2277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// of AsanThread in the list of destructors. 2287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// In this case the TSD for AsanThread will be destroyed before TSDDestructor 2297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// is called for the child thread, and a CHECK will fail when we call 2307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// pthread_create() to spawn the grandchild. 2317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, DISABLED_TSDTest) { 2327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_t th; 2337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_key_t test_key; 2347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_key_create(&test_key, TSDDestructor); 2357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_CREATE(&th, NULL, TSDWorker, &test_key); 2367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_JOIN(th, NULL); 2377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_key_delete(test_key); 2387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 2397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentemplate<typename T> 2417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid OOBTest() { 2427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char expected_str[100]; 2437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int size = sizeof(T); size < 20; size += 5) { 2447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = -5; i < 0; i++) { 2457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const char *str = 2467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "is located.*%d byte.*to the left"; 2477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sprintf(expected_str, str, abs(i)); 2487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(oob_test<T>(size, i), expected_str); 2497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 2507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = 0; i < (int)(size - sizeof(T) + 1); i++) 2527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen oob_test<T>(size, i); 2537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = size - sizeof(T) + 1; i <= (int)(size + 3 * sizeof(T)); i++) { 2557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const char *str = 2567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "is located.*%d byte.*to the right"; 2577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int off = i >= size ? (i - size) : 0; 2587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // we don't catch unaligned partially OOB accesses. 2597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (i % sizeof(T)) continue; 2607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sprintf(expected_str, str, off); 2617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(oob_test<T>(size, i), expected_str); 2627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 2637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 2647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(oob_test<T>(kLargeMalloc, -1), 2667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "is located.*1 byte.*to the left"); 2677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(oob_test<T>(kLargeMalloc, kLargeMalloc), 2687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "is located.*0 byte.*to the right"); 2697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 2707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// TODO(glider): the following tests are EXTREMELY slow on Darwin: 2727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// AddressSanitizer.OOB_char (125503 ms) 2737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// AddressSanitizer.OOB_int (126890 ms) 2747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// AddressSanitizer.OOBRightTest (315605 ms) 2757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// AddressSanitizer.SimpleStackTest (366559 ms) 2767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, OOB_char) { 2787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen OOBTest<U1>(); 2797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 2807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, OOB_int) { 2827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen OOBTest<U4>(); 2837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 2847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 2857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, OOBRightTest) { 2867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t access_size = 1; access_size <= 8; access_size *= 2) { 2877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t alloc_size = 1; alloc_size <= 8; alloc_size++) { 2887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t offset = 0; offset <= 8; offset += access_size) { 2897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *p = malloc(alloc_size); 2907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // allocated: [p, p + alloc_size) 2917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // accessed: [p + offset, p + offset + access_size) 2927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen uint8_t *addr = (uint8_t*)p + offset; 2937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (offset + access_size <= alloc_size) { 2947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen asan_write_sized_aligned(addr, access_size); 2957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } else { 2967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int outside_bytes = offset > alloc_size ? (offset - alloc_size) : 0; 2977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const char *str = 2987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "is located.%d *byte.*to the right"; 2997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char expected_str[100]; 3007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sprintf(expected_str, str, outside_bytes); 3017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(asan_write_sized_aligned(addr, access_size), 3027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen expected_str); 3037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 3047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(p); 3057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 3067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 3077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 3087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, UAF_char) { 3117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const char *uaf_string = "AddressSanitizer:.*heap-use-after-free"; 3127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(uaf_test<U1>(1, 0), uaf_string); 3137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(uaf_test<U1>(10, 0), uaf_string); 3147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(uaf_test<U1>(10, 10), uaf_string); 3157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, 0), uaf_string); 3167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(uaf_test<U1>(kLargeMalloc, kLargeMalloc / 2), uaf_string); 3177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if ASAN_HAS_BLACKLIST 3207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, IgnoreTest) { 3217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *x = Ident(new int); 3227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen delete Ident(x); 3237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *x = 0; 3247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif // ASAN_HAS_BLACKLIST 3267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstruct StructWithBitField { 3287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int bf1:1; 3297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int bf2:1; 3307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int bf3:1; 3317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int bf4:29; 3327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen}; 3337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, BitFieldPositiveTest) { 3357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen StructWithBitField *x = new StructWithBitField; 3367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen delete Ident(x); 3377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(x->bf1 = 0, "use-after-free"); 3387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(x->bf2 = 0, "use-after-free"); 3397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(x->bf3 = 0, "use-after-free"); 3407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(x->bf4 = 0, "use-after-free"); 3417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstruct StructWithBitFields_8_24 { 3447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a:8; 3457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int b:24; 3467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen}; 3477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, BitFieldNegativeTest) { 3497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen StructWithBitFields_8_24 *x = Ident(new StructWithBitFields_8_24); 3507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen x->a = 0; 3517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen x->b = 0; 3527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen delete Ident(x); 3537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, OutOfMemoryTest) { 3567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 48) : (0xf0000000); 3577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, realloc(0, size)); 3587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, realloc(0, ~Ident(0))); 3597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, malloc(size)); 3607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, malloc(~Ident(0))); 3617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, calloc(1, size)); 3627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, calloc(1, ~Ident(0))); 3637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if ASAN_NEEDS_SEGV 3667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohennamespace { 3677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenconst char kUnknownCrash[] = "AddressSanitizer: SEGV on unknown address"; 3697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenconst char kOverriddenHandler[] = "ASan signal handler has been overridden\n"; 3707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, WildAddressTest) { 3727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *c = (char*)0x123; 3737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(*c = 0, kUnknownCrash); 3747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid my_sigaction_sighandler(int, siginfo_t*, void*) { 3777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen fprintf(stderr, kOverriddenHandler); 3787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen exit(1); 3797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid my_signal_sighandler(int signum) { 3827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen fprintf(stderr, kOverriddenHandler); 3837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen exit(1); 3847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 3857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 3867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, SignalTest) { 3877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen struct sigaction sigact; 3887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(&sigact, 0, sizeof(sigact)); 3897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sigact.sa_sigaction = my_sigaction_sighandler; 3907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sigact.sa_flags = SA_SIGINFO; 3917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // ASan should silently ignore sigaction()... 3927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, sigaction(SIGSEGV, &sigact, 0)); 3937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef __APPLE__ 3947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, sigaction(SIGBUS, &sigact, 0)); 3957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 3967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *c = (char*)0x123; 3977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(*c = 0, kUnknownCrash); 3987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // ... and signal(). 3997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0, signal(SIGSEGV, my_signal_sighandler)); 4007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(*c = 0, kUnknownCrash); 4017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} // namespace 4037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 4047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic void MallocStress(size_t n) { 4067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen uint32_t seed = my_rand(&global_seed); 4077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t iter = 0; iter < 10; iter++) { 4087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen vector<void *> vec; 4097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t i = 0; i < n; i++) { 4107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if ((i % 3) == 0) { 4117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (vec.empty()) continue; 4127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t idx = my_rand(&seed) % vec.size(); 4137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen void *ptr = vec[idx]; 4147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen vec[idx] = vec.back(); 4157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen vec.pop_back(); 4167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free_aaa(ptr); 4177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } else { 4187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = my_rand(&seed) % 1000 + 1; 4197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef __APPLE__ 4207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t alignment = 1 << (my_rand(&seed) % 7 + 3); 4217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *ptr = (char*)memalign_aaa(alignment, size); 4227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#else 4237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *ptr = (char*) malloc_aaa(size); 4247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 4257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen vec.push_back(ptr); 4267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ptr[0] = 0; 4277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ptr[size-1] = 0; 4287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ptr[size/2] = 0; 4297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 4307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 4317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t i = 0; i < vec.size(); i++) 4327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free_aaa(vec[i]); 4337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 4347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, MallocStressTest) { 4377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MallocStress((ASAN_LOW_MEMORY) ? 20000 : 200000); 4387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic void TestLargeMalloc(size_t size) { 4417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char buff[1024]; 4427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sprintf(buff, "is located 1 bytes to the left of %lu-byte", (long)size); 4437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident((char*)malloc(size))[-1] = 0, buff); 4447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, LargeMallocTest) { 4477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = 113; i < (1 << 28); i = i * 2 + 13) { 4487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TestLargeMalloc(i); 4497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 4507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if ASAN_LOW_MEMORY != 1 4537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, HugeMallocTest) { 4547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef __APPLE__ 4557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // It was empirically found out that 1215 megabytes is the maximum amount of 4567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // memory available to the process under AddressSanitizer on 32-bit Mac 10.6. 4577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // 32-bit Mac 10.7 gives even less (< 1G). 4587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // (the libSystem malloc() allows allocating up to 2300 megabytes without 4597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // ASan). 4607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t n_megs = SANITIZER_WORDSIZE == 32 ? 500 : 4100; 4617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#else 4627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t n_megs = SANITIZER_WORDSIZE == 32 ? 2600 : 4100; 4637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 4647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TestLargeMalloc(n_megs << 20); 4657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 4677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef __APPLE__ 4697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid MemalignRun(size_t align, size_t size, int idx) { 4707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen fprintf(stderr, "align %ld\n", align); 4717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *p = (char *)memalign(align, size); 4727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(p)[idx] = 0; 4737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(p); 4747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, memalign) { 4777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int align = 16; align <= (1 << 23); align *= 2) { 4787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = align * 5; 4797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(MemalignRun(align, size, -1), 4807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "is located 1 bytes to the left"); 4817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(MemalignRun(align, size, size + 1), 4827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "is located 1 bytes to the right"); 4837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 4847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 4867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 4877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, ThreadedMallocStressTest) { 4887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const int kNumThreads = 4; 4897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const int kNumIterations = (ASAN_LOW_MEMORY) ? 10000 : 100000; 4907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_t t[kNumThreads]; 4917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = 0; i < kNumThreads; i++) { 4927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))MallocStress, 4937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen (void*)kNumIterations); 4947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 4957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = 0; i < kNumThreads; i++) { 4967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_JOIN(t[i], 0); 4977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 4987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 4997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid *ManyThreadsWorker(void *a) { 5017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int iter = 0; iter < 100; iter++) { 5027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t size = 100; size < 2000; size *= 2) { 5037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(Ident(malloc(size))); 5047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 5057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 5067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return 0; 5077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, ManyThreadsTest) { 5107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const size_t kNumThreads = 5117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen (SANITIZER_WORDSIZE == 32 || ASAN_AVOID_EXPENSIVE_TESTS) ? 30 : 1000; 5127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_t t[kNumThreads]; 5137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t i = 0; i < kNumThreads; i++) { 5147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_CREATE(&t[i], 0, ManyThreadsWorker, (void*)i); 5157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 5167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t i = 0; i < kNumThreads; i++) { 5177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_JOIN(t[i], 0); 5187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 5197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, ReallocTest) { 5227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const int kMinElem = 5; 5237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *ptr = (int*)malloc(sizeof(int) * kMinElem); 5247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ptr[3] = 3; 5257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = 0; i < 10000; i++) { 5267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ptr = (int*)realloc(ptr, 5277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen (my_rand(&global_seed) % 1000 + kMinElem) * sizeof(int)); 5287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(3, ptr[3]); 5297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 5307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef __APPLE__ 5337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic const char *kMallocUsableSizeErrorMsg = 5347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "AddressSanitizer: attempting to call malloc_usable_size()"; 5357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, MallocUsableSizeTest) { 5377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen const size_t kArraySize = 100; 5387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *array = Ident((char*)malloc(kArraySize)); 5397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *int_ptr = Ident(new int); 5407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0U, malloc_usable_size(NULL)); 5417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(kArraySize, malloc_usable_size(array)); 5427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(sizeof(int), malloc_usable_size(int_ptr)); 5437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(malloc_usable_size((void*)0x123), kMallocUsableSizeErrorMsg); 5447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(malloc_usable_size(array + kArraySize / 2), 5457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen kMallocUsableSizeErrorMsg); 5467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(array); 5477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(malloc_usable_size(array), kMallocUsableSizeErrorMsg); 5487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 5507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid WrongFree() { 5527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *x = (int*)malloc(100 * sizeof(int)); 5537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Use the allocated memory, otherwise Clang will optimize it out. 5547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(x); 5557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(x + 1); 5567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, WrongFreeTest) { 5597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(WrongFree(), 5607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "ERROR: AddressSanitizer: attempting free.*not malloc"); 5617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid DoubleFree() { 5647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *x = (int*)malloc(100 * sizeof(int)); 5657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen fprintf(stderr, "DoubleFree: x=%p\n", x); 5667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(x); 5677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(x); 5687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen fprintf(stderr, "should have failed in the second free(%p)\n", x); 5697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen abort(); 5707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, DoubleFreeTest) { 5737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(DoubleFree(), ASAN_PCRE_DOTALL 5747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "ERROR: AddressSanitizer: attempting double-free" 5757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ".*is located 0 bytes inside of 400-byte region" 5767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ".*freed by thread T0 here" 5777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ".*previously allocated by thread T0 here"); 5787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentemplate<int kSize> 5817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void SizedStackTest() { 5827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char a[kSize]; 5837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *A = Ident((char*)&a); 5847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t i = 0; i < kSize; i++) 5857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen A[i] = i; 5867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(A[-1] = 0, ""); 5877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(A[-20] = 0, ""); 5887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(A[-31] = 0, ""); 5897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(A[kSize] = 0, ""); 5907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(A[kSize + 1] = 0, ""); 5917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(A[kSize + 10] = 0, ""); 5927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(A[kSize + 31] = 0, ""); 5937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 5947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 5957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, SimpleStackTest) { 5967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<1>(); 5977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<2>(); 5987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<3>(); 5997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<4>(); 6007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<5>(); 6017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<6>(); 6027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<7>(); 6037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<16>(); 6047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<25>(); 6057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<34>(); 6067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<43>(); 6077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<51>(); 6087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<62>(); 6097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<64>(); 6107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SizedStackTest<128>(); 6117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, ManyStackObjectsTest) { 6147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char XXX[10]; 6157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char YYY[20]; 6167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char ZZZ[30]; 6177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(XXX); 6187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(YYY); 6197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(ZZZ)[-1] = 0, ASAN_PCRE_DOTALL "XXX.*YYY.*ZZZ"); 6207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE static void Frame0(int frame, char *a, char *b, char *c) { 6237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char d[4] = {0}; 6247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *D = Ident(d); 6257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen switch (frame) { 6267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen case 3: a[5]++; break; 6277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen case 2: b[5]++; break; 6287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen case 1: c[5]++; break; 6297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen case 0: D[5]++; break; 6307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 6317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE static void Frame1(int frame, char *a, char *b) { 6337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char c[4] = {0}; Frame0(frame, a, b, c); 6347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen break_optimization(0); 6357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE static void Frame2(int frame, char *a) { 6377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char b[4] = {0}; Frame1(frame, a, b); 6387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen break_optimization(0); 6397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE static void Frame3(int frame) { 6417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char a[4] = {0}; Frame2(frame, a); 6427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen break_optimization(0); 6437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, GuiltyStackFrame0Test) { 6467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Frame3(0), "located .*in frame <.*Frame0"); 6477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, GuiltyStackFrame1Test) { 6497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Frame3(1), "located .*in frame <.*Frame1"); 6507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, GuiltyStackFrame2Test) { 6527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Frame3(2), "located .*in frame <.*Frame2"); 6537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, GuiltyStackFrame3Test) { 6557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Frame3(3), "located .*in frame <.*Frame3"); 6567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void LongJmpFunc1(jmp_buf buf) { 6597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // create three red zones for these two stack objects. 6607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a; 6617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int b; 6627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *A = Ident(&a); 6647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *B = Ident(&b); 6657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *A = *B; 6667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen longjmp(buf, 1); 6677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void BuiltinLongJmpFunc1(jmp_buf buf) { 6707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // create three red zones for these two stack objects. 6717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a; 6727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int b; 6737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *A = Ident(&a); 6757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *B = Ident(&b); 6767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *A = *B; 6777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen __builtin_longjmp((void**)buf, 1); 6787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void UnderscopeLongJmpFunc1(jmp_buf buf) { 6817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // create three red zones for these two stack objects. 6827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a; 6837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int b; 6847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *A = Ident(&a); 6867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *B = Ident(&b); 6877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *A = *B; 6887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen _longjmp(buf, 1); 6897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 6907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void SigLongJmpFunc1(sigjmp_buf buf) { 6927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // create three red zones for these two stack objects. 6937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a; 6947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int b; 6957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 6967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *A = Ident(&a); 6977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *B = Ident(&b); 6987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *A = *B; 6997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen siglongjmp(buf, 1); 7007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void TouchStackFunc() { 7047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a[100]; // long array will intersect with redzones from LongJmpFunc1. 7057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *A = Ident(a); 7067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (int i = 0; i < 100; i++) 7077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen A[i] = i*i; 7087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Test that we handle longjmp and do not report fals positives on stack. 7117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, LongJmpTest) { 7127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen static jmp_buf buf; 7137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (!setjmp(buf)) { 7147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LongJmpFunc1(buf); 7157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } else { 7167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TouchStackFunc(); 7177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 7187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if not defined(__ANDROID__) 7217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, BuiltinLongJmpTest) { 7227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen static jmp_buf buf; 7237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (!__builtin_setjmp((void**)buf)) { 7247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen BuiltinLongJmpFunc1(buf); 7257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } else { 7267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TouchStackFunc(); 7277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 7287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif // not defined(__ANDROID__) 7307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, UnderscopeLongJmpTest) { 7327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen static jmp_buf buf; 7337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (!_setjmp(buf)) { 7347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen UnderscopeLongJmpFunc1(buf); 7357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } else { 7367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TouchStackFunc(); 7377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 7387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, SigLongJmpTest) { 7417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen static sigjmp_buf buf; 7427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (!sigsetjmp(buf, 1)) { 7437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen SigLongJmpFunc1(buf); 7447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } else { 7457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TouchStackFunc(); 7467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 7477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifdef __EXCEPTIONS 7507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenNOINLINE void ThrowFunc() { 7517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // create three red zones for these two stack objects. 7527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a; 7537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int b; 7547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *A = Ident(&a); 7567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *B = Ident(&b); 7577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *A = *B; 7587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ASAN_THROW(1); 7597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, CxxExceptionTest) { 7627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (ASAN_UAR) return; 7637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // TODO(kcc): this test crashes on 32-bit for some reason... 7647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (SANITIZER_WORDSIZE == 32) return; 7657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen try { 7667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen ThrowFunc(); 7677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } catch(...) {} 7687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TouchStackFunc(); 7697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 7717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid *ThreadStackReuseFunc1(void *unused) { 7737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // create three red zones for these two stack objects. 7747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int a; 7757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int b; 7767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *A = Ident(&a); 7787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int *B = Ident(&b); 7797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen *A = *B; 7807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_exit(0); 7817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return 0; 7827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid *ThreadStackReuseFunc2(void *unused) { 7857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen TouchStackFunc(); 7867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return 0; 7877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, ThreadStackReuseTest) { 7907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen pthread_t t; 7917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc1, 0); 7927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_JOIN(t, 0); 7937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_CREATE(&t, 0, ThreadStackReuseFunc2, 0); 7947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen PTHREAD_JOIN(t, 0); 7957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 7967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 7977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#if defined(__i386__) || defined(__x86_64__) 7987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, Store128Test) { 7997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *a = Ident((char*)malloc(Ident(12))); 8007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *p = a; 8017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (((uintptr_t)a % 16) != 0) 8027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen p = a + 8; 8037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen assert(((uintptr_t)p % 16) == 0); 8047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen __m128i value_wide = _mm_set1_epi16(0x1234); 8057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 8067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "AddressSanitizer: heap-buffer-overflow"); 8077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 8087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "WRITE of size 16"); 8097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(_mm_store_si128((__m128i*)p, value_wide), 8107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen "located 0 bytes to the right of 12-byte"); 8117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(a); 8127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 8147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic string RightOOBErrorMessage(int oob_distance, bool is_write) { 8167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen assert(oob_distance >= 0); 8177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char expected_str[100]; 8187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sprintf(expected_str, ASAN_PCRE_DOTALL "%s.*located %d bytes to the right", 8197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen is_write ? "WRITE" : "READ", oob_distance); 8207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return string(expected_str); 8217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic string RightOOBWriteMessage(int oob_distance) { 8247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return RightOOBErrorMessage(oob_distance, /*is_write*/true); 8257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic string RightOOBReadMessage(int oob_distance) { 8287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return RightOOBErrorMessage(oob_distance, /*is_write*/false); 8297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic string LeftOOBErrorMessage(int oob_distance, bool is_write) { 8327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen assert(oob_distance > 0); 8337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char expected_str[100]; 8347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sprintf(expected_str, ASAN_PCRE_DOTALL "%s.*located %d bytes to the left", 8357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen is_write ? "WRITE" : "READ", oob_distance); 8367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return string(expected_str); 8377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic string LeftOOBWriteMessage(int oob_distance) { 8407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return LeftOOBErrorMessage(oob_distance, /*is_write*/true); 8417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic string LeftOOBReadMessage(int oob_distance) { 8447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return LeftOOBErrorMessage(oob_distance, /*is_write*/false); 8457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic string LeftOOBAccessMessage(int oob_distance) { 8487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen assert(oob_distance > 0); 8497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char expected_str[100]; 8507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen sprintf(expected_str, "located %d bytes to the left", oob_distance); 8517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return string(expected_str); 8527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 8537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentemplate<typename T> 8557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid MemSetOOBTestTemplate(size_t length) { 8567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (length == 0) return; 8577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = Ident(sizeof(T) * length); 8587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen T *array = Ident((T*)malloc(size)); 8597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int element = Ident(42); 8607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int zero = Ident(0); 8617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // memset interval inside array 8627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array, element, size); 8637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array, element, size - 1); 8647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array + length - 1, element, sizeof(T)); 8657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array, element, 1); 8667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // memset 0 bytes 8687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array - 10, element, zero); 8697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array - 1, element, zero); 8707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array, element, zero); 8717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array + length, 0, zero); 8727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(array + length + 1, 0, zero); 8737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to memset bytes to the right of array 8757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset(array, 0, size + 1), 8767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(0)); 8777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset((char*)(array + length) - 1, element, 6), 8787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(4)); 8797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset(array + 1, element, size + sizeof(T)), 8807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(2 * sizeof(T) - 1)); 8817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // whole interval is to the right 8827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset(array + length + 1, 0, 10), 8837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(sizeof(T))); 8847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to memset bytes to the left of array 8867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset((char*)array - 1, element, size), 8877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(1)); 8887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset((char*)array - 5, 0, 6), 8897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(5)); 8907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset(array - 5, element, size + 5 * sizeof(T)), 8917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(5 * sizeof(T))); 8927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // whole interval is to the left 8937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset(array - 2, 0, sizeof(T)), 8947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(2 * sizeof(T))); 8957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 8967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to memset bytes both to the left & to the right 8977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(memset((char*)array - 2, element, size + 4), 8987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(2)); 8997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(array); 9017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 9027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, MemSetOOBTest) { 9047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MemSetOOBTestTemplate<char>(100); 9057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MemSetOOBTestTemplate<int>(5); 9067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MemSetOOBTestTemplate<double>(256); 9077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // We can test arrays of structres/classes here, but what for? 9087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 9097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Same test for memcpy and memmove functions 9117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentemplate <typename T, class M> 9127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid MemTransferOOBTestTemplate(size_t length) { 9137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (length == 0) return; 9147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = Ident(sizeof(T) * length); 9157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen T *src = Ident((T*)malloc(size)); 9167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen T *dest = Ident((T*)malloc(size)); 9177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen int zero = Ident(0); 9187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // valid transfer of bytes between arrays 9207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest, src, size); 9217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest + 1, src, size - sizeof(T)); 9227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest, src + length - 1, sizeof(T)); 9237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest, src, 1); 9247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // transfer zero bytes 9267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest - 1, src, 0); 9277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest + length, src, zero); 9287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest, src - 1, zero); 9297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen M::transfer(dest, src, zero); 9307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to change mem to the right of dest 9327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(dest + 1, src, size), 9337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(sizeof(T) - 1)); 9347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer((char*)(dest + length) - 1, src, 5), 9357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(3)); 9367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to change mem to the left of dest 9387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(dest - 2, src, size), 9397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(2 * sizeof(T))); 9407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer((char*)dest - 3, src, 4), 9417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(3)); 9427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to access mem to the right of src 9447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(dest, src + 2, size), 9457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBReadMessage(2 * sizeof(T) - 1)); 9467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(dest, (char*)(src + length) - 3, 6), 9477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBReadMessage(2)); 9487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to access mem to the left of src 9507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(dest, src - 1, size), 9517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBReadMessage(sizeof(T))); 9527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(dest, (char*)src - 6, 7), 9537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBReadMessage(6)); 9547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Generally we don't need to test cases where both accessing src and writing 9567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // to dest address to poisoned memory. 9577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen T *big_src = Ident((T*)malloc(size * 2)); 9597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen T *big_dest = Ident((T*)malloc(size * 2)); 9607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to change mem to both sides of dest 9617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(dest - 1, big_src, size * 2), 9627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(sizeof(T))); 9637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // try to access mem to both sides of src 9647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(M::transfer(big_dest, src - 2, size * 2), 9657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBReadMessage(2 * sizeof(T))); 9667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(src); 9687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(dest); 9697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(big_src); 9707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(big_dest); 9717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 9727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenclass MemCpyWrapper { 9747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen public: 9757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen static void* transfer(void *to, const void *from, size_t size) { 9767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return memcpy(to, from, size); 9777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 9787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen}; 9797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, MemCpyOOBTest) { 9807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MemTransferOOBTestTemplate<char, MemCpyWrapper>(100); 9817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MemTransferOOBTestTemplate<int, MemCpyWrapper>(1024); 9827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 9837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenclass MemMoveWrapper { 9857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen public: 9867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen static void* transfer(void *to, const void *from, size_t size) { 9877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return memmove(to, from, size); 9887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 9897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen}; 9907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, MemMoveOOBTest) { 9917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MemTransferOOBTestTemplate<char, MemMoveWrapper>(100); 9927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen MemTransferOOBTestTemplate<int, MemMoveWrapper>(1024); 9937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 9947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Tests for string functions 9967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 9977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Used for string functions tests 9987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic char global_string[] = "global"; 9997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic size_t global_string_length = 6; 10007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 10017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Input to a test is a zero-terminated string str with given length 10027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Accesses to the bytes to the left and to the right of str 10037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// are presumed to produce OOB errors 10047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenvoid StrLenOOBTestTemplate(char *str, size_t length, bool is_global) { 10057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Normal strlen calls 10067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(strlen(str), length); 10077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (length > 0) { 10087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(length - 1, strlen(str + 1)); 10097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(0U, strlen(str + length)); 10107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 10117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Arg of strlen is not malloced, OOB access 10127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen if (!is_global) { 10137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // We don't insert RedZones to the left of global variables 10147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strlen(str - 1)), LeftOOBReadMessage(1)); 10157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strlen(str - 5)), LeftOOBReadMessage(5)); 10167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 10177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strlen(str + length + 1)), RightOOBReadMessage(0)); 10187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Overwrite terminator 10197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[length] = 'a'; 10207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // String is not zero-terminated, strlen will lead to OOB access 10217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strlen(str)), RightOOBReadMessage(0)); 10227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strlen(str + length)), RightOOBReadMessage(0)); 10237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Restore terminator 10247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[length] = 0; 10257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 10267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, StrLenOOBTest) { 10277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Check heap-allocated string 10287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t length = Ident(10); 10297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *heap_string = Ident((char*)malloc(length + 1)); 10307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char stack_string[10 + 1]; 10317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen break_optimization(&stack_string); 10327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen for (size_t i = 0; i < length; i++) { 10337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen heap_string[i] = 'a'; 10347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen stack_string[i] = 'b'; 10357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen } 10367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen heap_string[length] = 0; 10377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen stack_string[length] = 0; 10387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen StrLenOOBTestTemplate(heap_string, length, false); 10397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // TODO(samsonov): Fix expected messages in StrLenOOBTestTemplate to 10407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // make test for stack_string work. Or move it to output tests. 10417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // StrLenOOBTestTemplate(stack_string, length, false); 10427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen StrLenOOBTestTemplate(global_string, global_string_length, true); 10437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(heap_string); 10447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 10457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 10467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic inline char* MallocAndMemsetString(size_t size, char ch) { 10477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *s = Ident((char*)malloc(size)); 10487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen memset(s, ch, size); 10497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return s; 10507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 10517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohenstatic inline char* MallocAndMemsetString(size_t size) { 10527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen return MallocAndMemsetString(size, 'z'); 10537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 10547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 10557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#ifndef __APPLE__ 10567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, StrNLenOOBTest) { 10577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = Ident(123); 10587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *str = MallocAndMemsetString(size); 10597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Normal strnlen calls. 10607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(strnlen(str - 1, 0)); 10617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(strnlen(str, size)); 10627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(strnlen(str + size - 1, 1)); 10637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[size - 1] = '\0'; 10647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen Ident(strnlen(str, 2 * size)); 10657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Argument points to not allocated memory. 10667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strnlen(str - 1, 1)), LeftOOBReadMessage(1)); 10677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strnlen(str + size, 1)), RightOOBReadMessage(0)); 10687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Overwrite the terminating '\0' and hit unallocated memory. 10697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[size - 1] = 'z'; 10707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strnlen(str, size + 1)), RightOOBReadMessage(0)); 10717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(str); 10727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 10737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen#endif 10747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 10757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, StrDupOOBTest) { 10767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = Ident(42); 10777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *str = MallocAndMemsetString(size); 10787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *new_str; 10797ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Normal strdup calls. 10807ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[size - 1] = '\0'; 10817ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen new_str = strdup(str); 10827ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(new_str); 10837ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen new_str = strdup(str + size - 1); 10847ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(new_str); 10857ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Argument points to not allocated memory. 10867ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strdup(str - 1)), LeftOOBReadMessage(1)); 10877ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strdup(str + size)), RightOOBReadMessage(0)); 10887ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Overwrite the terminating '\0' and hit unallocated memory. 10897ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[size - 1] = 'z'; 10907ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strdup(str)), RightOOBReadMessage(0)); 10917ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(str); 10927ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 10937ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 10947ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, StrCpyOOBTest) { 10957ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t to_size = Ident(30); 10967ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t from_size = Ident(6); // less than to_size 10977ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *to = Ident((char*)malloc(to_size)); 10987ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *from = Ident((char*)malloc(from_size)); 10997ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Normal strcpy calls. 11007ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strcpy(from, "hello"); 11017ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strcpy(to, from); 11027ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strcpy(to + to_size - from_size, from); 11037ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Length of "from" is too small. 11047ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strcpy(from, "hello2")), RightOOBWriteMessage(0)); 11057ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // "to" or "from" points to not allocated memory. 11067ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strcpy(to - 1, from)), LeftOOBWriteMessage(1)); 11077ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strcpy(to, from - 1)), LeftOOBReadMessage(1)); 11087ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strcpy(to, from + from_size)), RightOOBReadMessage(0)); 11097ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strcpy(to + to_size, from)), RightOOBWriteMessage(0)); 11107ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Overwrite the terminating '\0' character and hit unallocated memory. 11117ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen from[from_size - 1] = '!'; 11127ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strcpy(to, from)), RightOOBReadMessage(0)); 11137ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(to); 11147ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(from); 11157ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 11167ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 11177ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenTEST(AddressSanitizer, StrNCpyOOBTest) { 11187ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t to_size = Ident(20); 11197ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t from_size = Ident(6); // less than to_size 11207ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *to = Ident((char*)malloc(to_size)); 11217ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // From is a zero-terminated string "hello\0" of length 6 11227ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *from = Ident((char*)malloc(from_size)); 11237ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strcpy(from, "hello"); 11247ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // copy 0 bytes 11257ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strncpy(to, from, 0); 11267ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strncpy(to - 1, from - 1, 0); 11277ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // normal strncpy calls 11287ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strncpy(to, from, from_size); 11297ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strncpy(to, from, to_size); 11307ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strncpy(to, from + from_size - 1, to_size); 11317ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strncpy(to + to_size - 1, from, 1); 11327ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // One of {to, from} points to not allocated memory 11337ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strncpy(to, from - 1, from_size)), 11347ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBReadMessage(1)); 11357ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strncpy(to - 1, from, from_size)), 11367ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen LeftOOBWriteMessage(1)); 11377ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strncpy(to, from + from_size, 1)), 11387ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBReadMessage(0)); 11397ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strncpy(to + to_size, from, 1)), 11407ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(0)); 11417ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Length of "to" is too small 11427ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strncpy(to + to_size - from_size + 1, from, from_size)), 11437ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(0)); 11447ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strncpy(to + 1, from, to_size)), 11457ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBWriteMessage(0)); 11467ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Overwrite terminator in from 11477ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen from[from_size - 1] = '!'; 11487ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // normal strncpy call 11497ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen strncpy(to, from, from_size); 11507ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Length of "from" is too small 11517ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(strncpy(to, from, to_size)), 11527ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen RightOOBReadMessage(0)); 11537ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(to); 11547ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(from); 11557ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen} 11567ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 11577ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// Users may have different definitions of "strchr" and "index", so provide 11587ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// function pointer typedefs and overload RunStrChrTest implementation. 11597ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// We can't use macro for RunStrChrTest body here, as this macro would 11607ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen// confuse EXPECT_DEATH gtest macro. 11617ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentypedef char*(*PointerToStrChr1)(const char*, int); 11627ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohentypedef char*(*PointerToStrChr2)(char*, int); 11637ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen 11647ef855e462b9a18b7d330e4b40f350164a6ad9daEtan CohenUSED static void RunStrChrTest(PointerToStrChr1 StrChr) { 11657ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen size_t size = Ident(100); 11667ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen char *str = MallocAndMemsetString(size); 11677ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[10] = 'q'; 11687ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[11] = '\0'; 11697ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(str, StrChr(str, 'z')); 11707ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(str + 10, StrChr(str, 'q')); 11717ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_EQ(NULL, StrChr(str, 'a')); 11727ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // StrChr argument points to not allocated memory. 11737ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(StrChr(str - 1, 'z')), LeftOOBReadMessage(1)); 11747ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(StrChr(str + size, 'z')), RightOOBReadMessage(0)); 11757ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen // Overwrite the terminator and hit not allocated memory. 11767ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen str[11] = 'z'; 11777ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBReadMessage(0)); 11787ef855e462b9a18b7d330e4b40f350164a6ad9daEtan Cohen free(str); 1179} 1180USED static void RunStrChrTest(PointerToStrChr2 StrChr) { 1181 size_t size = Ident(100); 1182 char *str = MallocAndMemsetString(size); 1183 str[10] = 'q'; 1184 str[11] = '\0'; 1185 EXPECT_EQ(str, StrChr(str, 'z')); 1186 EXPECT_EQ(str + 10, StrChr(str, 'q')); 1187 EXPECT_EQ(NULL, StrChr(str, 'a')); 1188 // StrChr argument points to not allocated memory. 1189 EXPECT_DEATH(Ident(StrChr(str - 1, 'z')), LeftOOBReadMessage(1)); 1190 EXPECT_DEATH(Ident(StrChr(str + size, 'z')), RightOOBReadMessage(0)); 1191 // Overwrite the terminator and hit not allocated memory. 1192 str[11] = 'z'; 1193 EXPECT_DEATH(Ident(StrChr(str, 'a')), RightOOBReadMessage(0)); 1194 free(str); 1195} 1196 1197TEST(AddressSanitizer, StrChrAndIndexOOBTest) { 1198 RunStrChrTest(&strchr); 1199 RunStrChrTest(&index); 1200} 1201 1202TEST(AddressSanitizer, StrCmpAndFriendsLogicTest) { 1203 // strcmp 1204 EXPECT_EQ(0, strcmp("", "")); 1205 EXPECT_EQ(0, strcmp("abcd", "abcd")); 1206 EXPECT_GT(0, strcmp("ab", "ac")); 1207 EXPECT_GT(0, strcmp("abc", "abcd")); 1208 EXPECT_LT(0, strcmp("acc", "abc")); 1209 EXPECT_LT(0, strcmp("abcd", "abc")); 1210 1211 // strncmp 1212 EXPECT_EQ(0, strncmp("a", "b", 0)); 1213 EXPECT_EQ(0, strncmp("abcd", "abcd", 10)); 1214 EXPECT_EQ(0, strncmp("abcd", "abcef", 3)); 1215 EXPECT_GT(0, strncmp("abcde", "abcfa", 4)); 1216 EXPECT_GT(0, strncmp("a", "b", 5)); 1217 EXPECT_GT(0, strncmp("bc", "bcde", 4)); 1218 EXPECT_LT(0, strncmp("xyz", "xyy", 10)); 1219 EXPECT_LT(0, strncmp("baa", "aaa", 1)); 1220 EXPECT_LT(0, strncmp("zyx", "", 2)); 1221 1222 // strcasecmp 1223 EXPECT_EQ(0, strcasecmp("", "")); 1224 EXPECT_EQ(0, strcasecmp("zzz", "zzz")); 1225 EXPECT_EQ(0, strcasecmp("abCD", "ABcd")); 1226 EXPECT_GT(0, strcasecmp("aB", "Ac")); 1227 EXPECT_GT(0, strcasecmp("ABC", "ABCd")); 1228 EXPECT_LT(0, strcasecmp("acc", "abc")); 1229 EXPECT_LT(0, strcasecmp("ABCd", "abc")); 1230 1231 // strncasecmp 1232 EXPECT_EQ(0, strncasecmp("a", "b", 0)); 1233 EXPECT_EQ(0, strncasecmp("abCD", "ABcd", 10)); 1234 EXPECT_EQ(0, strncasecmp("abCd", "ABcef", 3)); 1235 EXPECT_GT(0, strncasecmp("abcde", "ABCfa", 4)); 1236 EXPECT_GT(0, strncasecmp("a", "B", 5)); 1237 EXPECT_GT(0, strncasecmp("bc", "BCde", 4)); 1238 EXPECT_LT(0, strncasecmp("xyz", "xyy", 10)); 1239 EXPECT_LT(0, strncasecmp("Baa", "aaa", 1)); 1240 EXPECT_LT(0, strncasecmp("zyx", "", 2)); 1241 1242 // memcmp 1243 EXPECT_EQ(0, memcmp("a", "b", 0)); 1244 EXPECT_EQ(0, memcmp("ab\0c", "ab\0c", 4)); 1245 EXPECT_GT(0, memcmp("\0ab", "\0ac", 3)); 1246 EXPECT_GT(0, memcmp("abb\0", "abba", 4)); 1247 EXPECT_LT(0, memcmp("ab\0cd", "ab\0c\0", 5)); 1248 EXPECT_LT(0, memcmp("zza", "zyx", 3)); 1249} 1250 1251typedef int(*PointerToStrCmp)(const char*, const char*); 1252void RunStrCmpTest(PointerToStrCmp StrCmp) { 1253 size_t size = Ident(100); 1254 char *s1 = MallocAndMemsetString(size); 1255 char *s2 = MallocAndMemsetString(size); 1256 s1[size - 1] = '\0'; 1257 s2[size - 1] = '\0'; 1258 // Normal StrCmp calls 1259 Ident(StrCmp(s1, s2)); 1260 Ident(StrCmp(s1, s2 + size - 1)); 1261 Ident(StrCmp(s1 + size - 1, s2 + size - 1)); 1262 s1[size - 1] = 'z'; 1263 s2[size - 1] = 'x'; 1264 Ident(StrCmp(s1, s2)); 1265 // One of arguments points to not allocated memory. 1266 EXPECT_DEATH(Ident(StrCmp)(s1 - 1, s2), LeftOOBReadMessage(1)); 1267 EXPECT_DEATH(Ident(StrCmp)(s1, s2 - 1), LeftOOBReadMessage(1)); 1268 EXPECT_DEATH(Ident(StrCmp)(s1 + size, s2), RightOOBReadMessage(0)); 1269 EXPECT_DEATH(Ident(StrCmp)(s1, s2 + size), RightOOBReadMessage(0)); 1270 // Hit unallocated memory and die. 1271 s2[size - 1] = 'z'; 1272 EXPECT_DEATH(Ident(StrCmp)(s1, s1), RightOOBReadMessage(0)); 1273 EXPECT_DEATH(Ident(StrCmp)(s1 + size - 1, s2), RightOOBReadMessage(0)); 1274 free(s1); 1275 free(s2); 1276} 1277 1278TEST(AddressSanitizer, StrCmpOOBTest) { 1279 RunStrCmpTest(&strcmp); 1280} 1281 1282TEST(AddressSanitizer, StrCaseCmpOOBTest) { 1283 RunStrCmpTest(&strcasecmp); 1284} 1285 1286typedef int(*PointerToStrNCmp)(const char*, const char*, size_t); 1287void RunStrNCmpTest(PointerToStrNCmp StrNCmp) { 1288 size_t size = Ident(100); 1289 char *s1 = MallocAndMemsetString(size); 1290 char *s2 = MallocAndMemsetString(size); 1291 s1[size - 1] = '\0'; 1292 s2[size - 1] = '\0'; 1293 // Normal StrNCmp calls 1294 Ident(StrNCmp(s1, s2, size + 2)); 1295 s1[size - 1] = 'z'; 1296 s2[size - 1] = 'x'; 1297 Ident(StrNCmp(s1 + size - 2, s2 + size - 2, size)); 1298 s2[size - 1] = 'z'; 1299 Ident(StrNCmp(s1 - 1, s2 - 1, 0)); 1300 Ident(StrNCmp(s1 + size - 1, s2 + size - 1, 1)); 1301 // One of arguments points to not allocated memory. 1302 EXPECT_DEATH(Ident(StrNCmp)(s1 - 1, s2, 1), LeftOOBReadMessage(1)); 1303 EXPECT_DEATH(Ident(StrNCmp)(s1, s2 - 1, 1), LeftOOBReadMessage(1)); 1304 EXPECT_DEATH(Ident(StrNCmp)(s1 + size, s2, 1), RightOOBReadMessage(0)); 1305 EXPECT_DEATH(Ident(StrNCmp)(s1, s2 + size, 1), RightOOBReadMessage(0)); 1306 // Hit unallocated memory and die. 1307 EXPECT_DEATH(Ident(StrNCmp)(s1 + 1, s2 + 1, size), RightOOBReadMessage(0)); 1308 EXPECT_DEATH(Ident(StrNCmp)(s1 + size - 1, s2, 2), RightOOBReadMessage(0)); 1309 free(s1); 1310 free(s2); 1311} 1312 1313TEST(AddressSanitizer, StrNCmpOOBTest) { 1314 RunStrNCmpTest(&strncmp); 1315} 1316 1317TEST(AddressSanitizer, StrNCaseCmpOOBTest) { 1318 RunStrNCmpTest(&strncasecmp); 1319} 1320 1321TEST(AddressSanitizer, MemCmpOOBTest) { 1322 size_t size = Ident(100); 1323 char *s1 = MallocAndMemsetString(size); 1324 char *s2 = MallocAndMemsetString(size); 1325 // Normal memcmp calls. 1326 Ident(memcmp(s1, s2, size)); 1327 Ident(memcmp(s1 + size - 1, s2 + size - 1, 1)); 1328 Ident(memcmp(s1 - 1, s2 - 1, 0)); 1329 // One of arguments points to not allocated memory. 1330 EXPECT_DEATH(Ident(memcmp)(s1 - 1, s2, 1), LeftOOBReadMessage(1)); 1331 EXPECT_DEATH(Ident(memcmp)(s1, s2 - 1, 1), LeftOOBReadMessage(1)); 1332 EXPECT_DEATH(Ident(memcmp)(s1 + size, s2, 1), RightOOBReadMessage(0)); 1333 EXPECT_DEATH(Ident(memcmp)(s1, s2 + size, 1), RightOOBReadMessage(0)); 1334 // Hit unallocated memory and die. 1335 EXPECT_DEATH(Ident(memcmp)(s1 + 1, s2 + 1, size), RightOOBReadMessage(0)); 1336 EXPECT_DEATH(Ident(memcmp)(s1 + size - 1, s2, 2), RightOOBReadMessage(0)); 1337 // Zero bytes are not terminators and don't prevent from OOB. 1338 s1[size - 1] = '\0'; 1339 s2[size - 1] = '\0'; 1340 EXPECT_DEATH(Ident(memcmp)(s1, s2, size + 1), RightOOBReadMessage(0)); 1341 free(s1); 1342 free(s2); 1343} 1344 1345TEST(AddressSanitizer, StrCatOOBTest) { 1346 // strcat() reads strlen(to) bytes from |to| before concatenating. 1347 size_t to_size = Ident(100); 1348 char *to = MallocAndMemsetString(to_size); 1349 to[0] = '\0'; 1350 size_t from_size = Ident(20); 1351 char *from = MallocAndMemsetString(from_size); 1352 from[from_size - 1] = '\0'; 1353 // Normal strcat calls. 1354 strcat(to, from); 1355 strcat(to, from); 1356 strcat(to + from_size, from + from_size - 2); 1357 // Passing an invalid pointer is an error even when concatenating an empty 1358 // string. 1359 EXPECT_DEATH(strcat(to - 1, from + from_size - 1), LeftOOBAccessMessage(1)); 1360 // One of arguments points to not allocated memory. 1361 EXPECT_DEATH(strcat(to - 1, from), LeftOOBAccessMessage(1)); 1362 EXPECT_DEATH(strcat(to, from - 1), LeftOOBReadMessage(1)); 1363 EXPECT_DEATH(strcat(to + to_size, from), RightOOBWriteMessage(0)); 1364 EXPECT_DEATH(strcat(to, from + from_size), RightOOBReadMessage(0)); 1365 1366 // "from" is not zero-terminated. 1367 from[from_size - 1] = 'z'; 1368 EXPECT_DEATH(strcat(to, from), RightOOBReadMessage(0)); 1369 from[from_size - 1] = '\0'; 1370 // "to" is not zero-terminated. 1371 memset(to, 'z', to_size); 1372 EXPECT_DEATH(strcat(to, from), RightOOBWriteMessage(0)); 1373 // "to" is too short to fit "from". 1374 to[to_size - from_size + 1] = '\0'; 1375 EXPECT_DEATH(strcat(to, from), RightOOBWriteMessage(0)); 1376 // length of "to" is just enough. 1377 strcat(to, from + 1); 1378 1379 free(to); 1380 free(from); 1381} 1382 1383TEST(AddressSanitizer, StrNCatOOBTest) { 1384 // strncat() reads strlen(to) bytes from |to| before concatenating. 1385 size_t to_size = Ident(100); 1386 char *to = MallocAndMemsetString(to_size); 1387 to[0] = '\0'; 1388 size_t from_size = Ident(20); 1389 char *from = MallocAndMemsetString(from_size); 1390 // Normal strncat calls. 1391 strncat(to, from, 0); 1392 strncat(to, from, from_size); 1393 from[from_size - 1] = '\0'; 1394 strncat(to, from, 2 * from_size); 1395 // Catenating empty string with an invalid string is still an error. 1396 EXPECT_DEATH(strncat(to - 1, from, 0), LeftOOBAccessMessage(1)); 1397 strncat(to, from + from_size - 1, 10); 1398 // One of arguments points to not allocated memory. 1399 EXPECT_DEATH(strncat(to - 1, from, 2), LeftOOBAccessMessage(1)); 1400 EXPECT_DEATH(strncat(to, from - 1, 2), LeftOOBReadMessage(1)); 1401 EXPECT_DEATH(strncat(to + to_size, from, 2), RightOOBWriteMessage(0)); 1402 EXPECT_DEATH(strncat(to, from + from_size, 2), RightOOBReadMessage(0)); 1403 1404 memset(from, 'z', from_size); 1405 memset(to, 'z', to_size); 1406 to[0] = '\0'; 1407 // "from" is too short. 1408 EXPECT_DEATH(strncat(to, from, from_size + 1), RightOOBReadMessage(0)); 1409 // "to" is not zero-terminated. 1410 EXPECT_DEATH(strncat(to + 1, from, 1), RightOOBWriteMessage(0)); 1411 // "to" is too short to fit "from". 1412 to[0] = 'z'; 1413 to[to_size - from_size + 1] = '\0'; 1414 EXPECT_DEATH(strncat(to, from, from_size - 1), RightOOBWriteMessage(0)); 1415 // "to" is just enough. 1416 strncat(to, from, from_size - 2); 1417 1418 free(to); 1419 free(from); 1420} 1421 1422static string OverlapErrorMessage(const string &func) { 1423 return func + "-param-overlap"; 1424} 1425 1426TEST(AddressSanitizer, StrArgsOverlapTest) { 1427 size_t size = Ident(100); 1428 char *str = Ident((char*)malloc(size)); 1429 1430// Do not check memcpy() on OS X 10.7 and later, where it actually aliases 1431// memmove(). 1432#if !defined(__APPLE__) || !defined(MAC_OS_X_VERSION_10_7) || \ 1433 (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7) 1434 // Check "memcpy". Use Ident() to avoid inlining. 1435 memset(str, 'z', size); 1436 Ident(memcpy)(str + 1, str + 11, 10); 1437 Ident(memcpy)(str, str, 0); 1438 EXPECT_DEATH(Ident(memcpy)(str, str + 14, 15), OverlapErrorMessage("memcpy")); 1439 EXPECT_DEATH(Ident(memcpy)(str + 14, str, 15), OverlapErrorMessage("memcpy")); 1440#endif 1441 1442 // We do not treat memcpy with to==from as a bug. 1443 // See http://llvm.org/bugs/show_bug.cgi?id=11763. 1444 // EXPECT_DEATH(Ident(memcpy)(str + 20, str + 20, 1), 1445 // OverlapErrorMessage("memcpy")); 1446 1447 // Check "strcpy". 1448 memset(str, 'z', size); 1449 str[9] = '\0'; 1450 strcpy(str + 10, str); 1451 EXPECT_DEATH(strcpy(str + 9, str), OverlapErrorMessage("strcpy")); 1452 EXPECT_DEATH(strcpy(str, str + 4), OverlapErrorMessage("strcpy")); 1453 strcpy(str, str + 5); 1454 1455 // Check "strncpy". 1456 memset(str, 'z', size); 1457 strncpy(str, str + 10, 10); 1458 EXPECT_DEATH(strncpy(str, str + 9, 10), OverlapErrorMessage("strncpy")); 1459 EXPECT_DEATH(strncpy(str + 9, str, 10), OverlapErrorMessage("strncpy")); 1460 str[10] = '\0'; 1461 strncpy(str + 11, str, 20); 1462 EXPECT_DEATH(strncpy(str + 10, str, 20), OverlapErrorMessage("strncpy")); 1463 1464 // Check "strcat". 1465 memset(str, 'z', size); 1466 str[10] = '\0'; 1467 str[20] = '\0'; 1468 strcat(str, str + 10); 1469 EXPECT_DEATH(strcat(str, str + 11), OverlapErrorMessage("strcat")); 1470 str[10] = '\0'; 1471 strcat(str + 11, str); 1472 EXPECT_DEATH(strcat(str, str + 9), OverlapErrorMessage("strcat")); 1473 EXPECT_DEATH(strcat(str + 9, str), OverlapErrorMessage("strcat")); 1474 EXPECT_DEATH(strcat(str + 10, str), OverlapErrorMessage("strcat")); 1475 1476 // Check "strncat". 1477 memset(str, 'z', size); 1478 str[10] = '\0'; 1479 strncat(str, str + 10, 10); // from is empty 1480 EXPECT_DEATH(strncat(str, str + 11, 10), OverlapErrorMessage("strncat")); 1481 str[10] = '\0'; 1482 str[20] = '\0'; 1483 strncat(str + 5, str, 5); 1484 str[10] = '\0'; 1485 EXPECT_DEATH(strncat(str + 5, str, 6), OverlapErrorMessage("strncat")); 1486 EXPECT_DEATH(strncat(str, str + 9, 10), OverlapErrorMessage("strncat")); 1487 1488 free(str); 1489} 1490 1491void CallAtoi(const char *nptr) { 1492 Ident(atoi(nptr)); 1493} 1494void CallAtol(const char *nptr) { 1495 Ident(atol(nptr)); 1496} 1497void CallAtoll(const char *nptr) { 1498 Ident(atoll(nptr)); 1499} 1500typedef void(*PointerToCallAtoi)(const char*); 1501 1502void RunAtoiOOBTest(PointerToCallAtoi Atoi) { 1503 char *array = MallocAndMemsetString(10, '1'); 1504 // Invalid pointer to the string. 1505 EXPECT_DEATH(Atoi(array + 11), RightOOBReadMessage(1)); 1506 EXPECT_DEATH(Atoi(array - 1), LeftOOBReadMessage(1)); 1507 // Die if a buffer doesn't have terminating NULL. 1508 EXPECT_DEATH(Atoi(array), RightOOBReadMessage(0)); 1509 // Make last symbol a terminating NULL or other non-digit. 1510 array[9] = '\0'; 1511 Atoi(array); 1512 array[9] = 'a'; 1513 Atoi(array); 1514 Atoi(array + 9); 1515 // Sometimes we need to detect overflow if no digits are found. 1516 memset(array, ' ', 10); 1517 EXPECT_DEATH(Atoi(array), RightOOBReadMessage(0)); 1518 array[9] = '-'; 1519 EXPECT_DEATH(Atoi(array), RightOOBReadMessage(0)); 1520 EXPECT_DEATH(Atoi(array + 9), RightOOBReadMessage(0)); 1521 array[8] = '-'; 1522 Atoi(array); 1523 delete array; 1524} 1525 1526TEST(AddressSanitizer, AtoiAndFriendsOOBTest) { 1527 RunAtoiOOBTest(&CallAtoi); 1528 RunAtoiOOBTest(&CallAtol); 1529 RunAtoiOOBTest(&CallAtoll); 1530} 1531 1532void CallStrtol(const char *nptr, char **endptr, int base) { 1533 Ident(strtol(nptr, endptr, base)); 1534} 1535void CallStrtoll(const char *nptr, char **endptr, int base) { 1536 Ident(strtoll(nptr, endptr, base)); 1537} 1538typedef void(*PointerToCallStrtol)(const char*, char**, int); 1539 1540void RunStrtolOOBTest(PointerToCallStrtol Strtol) { 1541 char *array = MallocAndMemsetString(3); 1542 char *endptr = NULL; 1543 array[0] = '1'; 1544 array[1] = '2'; 1545 array[2] = '3'; 1546 // Invalid pointer to the string. 1547 EXPECT_DEATH(Strtol(array + 3, NULL, 0), RightOOBReadMessage(0)); 1548 EXPECT_DEATH(Strtol(array - 1, NULL, 0), LeftOOBReadMessage(1)); 1549 // Buffer overflow if there is no terminating null (depends on base). 1550 Strtol(array, &endptr, 3); 1551 EXPECT_EQ(array + 2, endptr); 1552 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBReadMessage(0)); 1553 array[2] = 'z'; 1554 Strtol(array, &endptr, 35); 1555 EXPECT_EQ(array + 2, endptr); 1556 EXPECT_DEATH(Strtol(array, NULL, 36), RightOOBReadMessage(0)); 1557 // Add terminating zero to get rid of overflow. 1558 array[2] = '\0'; 1559 Strtol(array, NULL, 36); 1560 // Don't check for overflow if base is invalid. 1561 Strtol(array - 1, NULL, -1); 1562 Strtol(array + 3, NULL, 1); 1563 // Sometimes we need to detect overflow if no digits are found. 1564 array[0] = array[1] = array[2] = ' '; 1565 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBReadMessage(0)); 1566 array[2] = '+'; 1567 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBReadMessage(0)); 1568 array[2] = '-'; 1569 EXPECT_DEATH(Strtol(array, NULL, 0), RightOOBReadMessage(0)); 1570 array[1] = '+'; 1571 Strtol(array, NULL, 0); 1572 array[1] = array[2] = 'z'; 1573 Strtol(array, &endptr, 0); 1574 EXPECT_EQ(array, endptr); 1575 Strtol(array + 2, NULL, 0); 1576 EXPECT_EQ(array, endptr); 1577 delete array; 1578} 1579 1580TEST(AddressSanitizer, StrtollOOBTest) { 1581 RunStrtolOOBTest(&CallStrtoll); 1582} 1583TEST(AddressSanitizer, StrtolOOBTest) { 1584 RunStrtolOOBTest(&CallStrtol); 1585} 1586 1587// At the moment we instrument memcpy/memove/memset calls at compile time so we 1588// can't handle OOB error if these functions are called by pointer, see disabled 1589// MemIntrinsicCallByPointerTest below 1590typedef void*(*PointerToMemTransfer)(void*, const void*, size_t); 1591typedef void*(*PointerToMemSet)(void*, int, size_t); 1592 1593void CallMemSetByPointer(PointerToMemSet MemSet) { 1594 size_t size = Ident(100); 1595 char *array = Ident((char*)malloc(size)); 1596 EXPECT_DEATH(MemSet(array, 0, 101), RightOOBWriteMessage(0)); 1597 free(array); 1598} 1599 1600void CallMemTransferByPointer(PointerToMemTransfer MemTransfer) { 1601 size_t size = Ident(100); 1602 char *src = Ident((char*)malloc(size)); 1603 char *dst = Ident((char*)malloc(size)); 1604 EXPECT_DEATH(MemTransfer(dst, src, 101), RightOOBWriteMessage(0)); 1605 free(src); 1606 free(dst); 1607} 1608 1609TEST(AddressSanitizer, DISABLED_MemIntrinsicCallByPointerTest) { 1610 CallMemSetByPointer(&memset); 1611 CallMemTransferByPointer(&memcpy); 1612 CallMemTransferByPointer(&memmove); 1613} 1614 1615#if defined(__linux__) && !defined(ANDROID) && !defined(__ANDROID__) 1616TEST(AddressSanitizer, pread) { 1617 char *x = new char[10]; 1618 int fd = open("/proc/self/stat", O_RDONLY); 1619 ASSERT_GT(fd, 0); 1620 EXPECT_DEATH(pread(fd, x, 15, 0), 1621 ASAN_PCRE_DOTALL 1622 "AddressSanitizer: heap-buffer-overflow" 1623 ".* is located 4 bytes to the right of 10-byte region"); 1624 close(fd); 1625 delete x; 1626} 1627 1628TEST(AddressSanitizer, pread64) { 1629 char *x = new char[10]; 1630 int fd = open("/proc/self/stat", O_RDONLY); 1631 ASSERT_GT(fd, 0); 1632 EXPECT_DEATH(pread64(fd, x, 15, 0), 1633 ASAN_PCRE_DOTALL 1634 "AddressSanitizer: heap-buffer-overflow" 1635 ".* is located 4 bytes to the right of 10-byte region"); 1636 close(fd); 1637 delete x; 1638} 1639 1640TEST(AddressSanitizer, read) { 1641 char *x = new char[10]; 1642 int fd = open("/proc/self/stat", O_RDONLY); 1643 ASSERT_GT(fd, 0); 1644 EXPECT_DEATH(read(fd, x, 15), 1645 ASAN_PCRE_DOTALL 1646 "AddressSanitizer: heap-buffer-overflow" 1647 ".* is located 4 bytes to the right of 10-byte region"); 1648 close(fd); 1649 delete x; 1650} 1651 1652#endif // defined(__linux__) && !defined(ANDROID) && !defined(__ANDROID__) 1653 1654// This test case fails 1655// Clang optimizes memcpy/memset calls which lead to unaligned access 1656TEST(AddressSanitizer, DISABLED_MemIntrinsicUnalignedAccessTest) { 1657 int size = Ident(4096); 1658 char *s = Ident((char*)malloc(size)); 1659 EXPECT_DEATH(memset(s + size - 1, 0, 2), RightOOBWriteMessage(0)); 1660 free(s); 1661} 1662 1663// TODO(samsonov): Add a test with malloc(0) 1664// TODO(samsonov): Add tests for str* and mem* functions. 1665 1666NOINLINE static int LargeFunction(bool do_bad_access) { 1667 int *x = new int[100]; 1668 x[0]++; 1669 x[1]++; 1670 x[2]++; 1671 x[3]++; 1672 x[4]++; 1673 x[5]++; 1674 x[6]++; 1675 x[7]++; 1676 x[8]++; 1677 x[9]++; 1678 1679 x[do_bad_access ? 100 : 0]++; int res = __LINE__; 1680 1681 x[10]++; 1682 x[11]++; 1683 x[12]++; 1684 x[13]++; 1685 x[14]++; 1686 x[15]++; 1687 x[16]++; 1688 x[17]++; 1689 x[18]++; 1690 x[19]++; 1691 1692 delete x; 1693 return res; 1694} 1695 1696// Test the we have correct debug info for the failing instruction. 1697// This test requires the in-process symbolizer to be enabled by default. 1698TEST(AddressSanitizer, DISABLED_LargeFunctionSymbolizeTest) { 1699 int failing_line = LargeFunction(false); 1700 char expected_warning[128]; 1701 sprintf(expected_warning, "LargeFunction.*asan_test.*:%d", failing_line); 1702 EXPECT_DEATH(LargeFunction(true), expected_warning); 1703} 1704 1705// Check that we unwind and symbolize correctly. 1706TEST(AddressSanitizer, DISABLED_MallocFreeUnwindAndSymbolizeTest) { 1707 int *a = (int*)malloc_aaa(sizeof(int)); 1708 *a = 1; 1709 free_aaa(a); 1710 EXPECT_DEATH(*a = 1, "free_ccc.*free_bbb.*free_aaa.*" 1711 "malloc_fff.*malloc_eee.*malloc_ddd"); 1712} 1713 1714static void TryToSetThreadName(const char *name) { 1715#ifdef __linux__ 1716 prctl(PR_SET_NAME, (unsigned long)name, 0, 0, 0); 1717#endif 1718} 1719 1720void *ThreadedTestAlloc(void *a) { 1721 TryToSetThreadName("AllocThr"); 1722 int **p = (int**)a; 1723 *p = new int; 1724 return 0; 1725} 1726 1727void *ThreadedTestFree(void *a) { 1728 TryToSetThreadName("FreeThr"); 1729 int **p = (int**)a; 1730 delete *p; 1731 return 0; 1732} 1733 1734void *ThreadedTestUse(void *a) { 1735 TryToSetThreadName("UseThr"); 1736 int **p = (int**)a; 1737 **p = 1; 1738 return 0; 1739} 1740 1741void ThreadedTestSpawn() { 1742 pthread_t t; 1743 int *x; 1744 PTHREAD_CREATE(&t, 0, ThreadedTestAlloc, &x); 1745 PTHREAD_JOIN(t, 0); 1746 PTHREAD_CREATE(&t, 0, ThreadedTestFree, &x); 1747 PTHREAD_JOIN(t, 0); 1748 PTHREAD_CREATE(&t, 0, ThreadedTestUse, &x); 1749 PTHREAD_JOIN(t, 0); 1750} 1751 1752TEST(AddressSanitizer, ThreadedTest) { 1753 EXPECT_DEATH(ThreadedTestSpawn(), 1754 ASAN_PCRE_DOTALL 1755 "Thread T.*created" 1756 ".*Thread T.*created" 1757 ".*Thread T.*created"); 1758} 1759 1760#ifdef __linux__ 1761TEST(AddressSanitizer, ThreadNamesTest) { 1762 // ThreadedTestSpawn(); 1763 EXPECT_DEATH(ThreadedTestSpawn(), 1764 ASAN_PCRE_DOTALL 1765 "WRITE .*thread T. .UseThr." 1766 ".*freed by thread T. .FreeThr. here:" 1767 ".*previously allocated by thread T. .AllocThr. here:" 1768 ".*Thread T. .UseThr. created by T. here:" 1769 ".*Thread T. .FreeThr. created by T. here:" 1770 ".*Thread T. .AllocThr. created by T. here:" 1771 ""); 1772} 1773#endif 1774 1775#if ASAN_NEEDS_SEGV 1776TEST(AddressSanitizer, ShadowGapTest) { 1777#if SANITIZER_WORDSIZE == 32 1778 char *addr = (char*)0x22000000; 1779#else 1780 char *addr = (char*)0x0000100000080000; 1781#endif 1782 EXPECT_DEATH(*addr = 1, "AddressSanitizer: SEGV on unknown"); 1783} 1784#endif // ASAN_NEEDS_SEGV 1785 1786extern "C" { 1787NOINLINE static void UseThenFreeThenUse() { 1788 char *x = Ident((char*)malloc(8)); 1789 *x = 1; 1790 free_aaa(x); 1791 *x = 2; 1792} 1793} 1794 1795TEST(AddressSanitizer, UseThenFreeThenUseTest) { 1796 EXPECT_DEATH(UseThenFreeThenUse(), "freed by thread"); 1797} 1798 1799TEST(AddressSanitizer, StrDupTest) { 1800 free(strdup(Ident("123"))); 1801} 1802 1803// Currently we create and poison redzone at right of global variables. 1804char glob5[5]; 1805static char static110[110]; 1806const char ConstGlob[7] = {1, 2, 3, 4, 5, 6, 7}; 1807static const char StaticConstGlob[3] = {9, 8, 7}; 1808extern int GlobalsTest(int x); 1809 1810TEST(AddressSanitizer, GlobalTest) { 1811 static char func_static15[15]; 1812 1813 static char fs1[10]; 1814 static char fs2[10]; 1815 static char fs3[10]; 1816 1817 glob5[Ident(0)] = 0; 1818 glob5[Ident(1)] = 0; 1819 glob5[Ident(2)] = 0; 1820 glob5[Ident(3)] = 0; 1821 glob5[Ident(4)] = 0; 1822 1823 EXPECT_DEATH(glob5[Ident(5)] = 0, 1824 "0 bytes to the right of global variable.*glob5.* size 5"); 1825 EXPECT_DEATH(glob5[Ident(5+6)] = 0, 1826 "6 bytes to the right of global variable.*glob5.* size 5"); 1827 Ident(static110); // avoid optimizations 1828 static110[Ident(0)] = 0; 1829 static110[Ident(109)] = 0; 1830 EXPECT_DEATH(static110[Ident(110)] = 0, 1831 "0 bytes to the right of global variable"); 1832 EXPECT_DEATH(static110[Ident(110+7)] = 0, 1833 "7 bytes to the right of global variable"); 1834 1835 Ident(func_static15); // avoid optimizations 1836 func_static15[Ident(0)] = 0; 1837 EXPECT_DEATH(func_static15[Ident(15)] = 0, 1838 "0 bytes to the right of global variable"); 1839 EXPECT_DEATH(func_static15[Ident(15 + 9)] = 0, 1840 "9 bytes to the right of global variable"); 1841 1842 Ident(fs1); 1843 Ident(fs2); 1844 Ident(fs3); 1845 1846 // We don't create left redzones, so this is not 100% guaranteed to fail. 1847 // But most likely will. 1848 EXPECT_DEATH(fs2[Ident(-1)] = 0, "is located.*of global variable"); 1849 1850 EXPECT_DEATH(Ident(Ident(ConstGlob)[8]), 1851 "is located 1 bytes to the right of .*ConstGlob"); 1852 EXPECT_DEATH(Ident(Ident(StaticConstGlob)[5]), 1853 "is located 2 bytes to the right of .*StaticConstGlob"); 1854 1855 // call stuff from another file. 1856 GlobalsTest(0); 1857} 1858 1859TEST(AddressSanitizer, GlobalStringConstTest) { 1860 static const char *zoo = "FOOBAR123"; 1861 const char *p = Ident(zoo); 1862 EXPECT_DEATH(Ident(p[15]), "is ascii string 'FOOBAR123'"); 1863} 1864 1865TEST(AddressSanitizer, FileNameInGlobalReportTest) { 1866 static char zoo[10]; 1867 const char *p = Ident(zoo); 1868 // The file name should be present in the report. 1869 EXPECT_DEATH(Ident(p[15]), "zoo.*asan_test."); 1870} 1871 1872int *ReturnsPointerToALocalObject() { 1873 int a = 0; 1874 return Ident(&a); 1875} 1876 1877#if ASAN_UAR == 1 1878TEST(AddressSanitizer, LocalReferenceReturnTest) { 1879 int *(*f)() = Ident(ReturnsPointerToALocalObject); 1880 int *p = f(); 1881 // Call 'f' a few more times, 'p' should still be poisoned. 1882 for (int i = 0; i < 32; i++) 1883 f(); 1884 EXPECT_DEATH(*p = 1, "AddressSanitizer: stack-use-after-return"); 1885 EXPECT_DEATH(*p = 1, "is located.*in frame .*ReturnsPointerToALocal"); 1886} 1887#endif 1888 1889template <int kSize> 1890NOINLINE static void FuncWithStack() { 1891 char x[kSize]; 1892 Ident(x)[0] = 0; 1893 Ident(x)[kSize-1] = 0; 1894} 1895 1896static void LotsOfStackReuse() { 1897 int LargeStack[10000]; 1898 Ident(LargeStack)[0] = 0; 1899 for (int i = 0; i < 10000; i++) { 1900 FuncWithStack<128 * 1>(); 1901 FuncWithStack<128 * 2>(); 1902 FuncWithStack<128 * 4>(); 1903 FuncWithStack<128 * 8>(); 1904 FuncWithStack<128 * 16>(); 1905 FuncWithStack<128 * 32>(); 1906 FuncWithStack<128 * 64>(); 1907 FuncWithStack<128 * 128>(); 1908 FuncWithStack<128 * 256>(); 1909 FuncWithStack<128 * 512>(); 1910 Ident(LargeStack)[0] = 0; 1911 } 1912} 1913 1914TEST(AddressSanitizer, StressStackReuseTest) { 1915 LotsOfStackReuse(); 1916} 1917 1918TEST(AddressSanitizer, ThreadedStressStackReuseTest) { 1919 const int kNumThreads = 20; 1920 pthread_t t[kNumThreads]; 1921 for (int i = 0; i < kNumThreads; i++) { 1922 PTHREAD_CREATE(&t[i], 0, (void* (*)(void *x))LotsOfStackReuse, 0); 1923 } 1924 for (int i = 0; i < kNumThreads; i++) { 1925 PTHREAD_JOIN(t[i], 0); 1926 } 1927} 1928 1929static void *PthreadExit(void *a) { 1930 pthread_exit(0); 1931 return 0; 1932} 1933 1934TEST(AddressSanitizer, PthreadExitTest) { 1935 pthread_t t; 1936 for (int i = 0; i < 1000; i++) { 1937 PTHREAD_CREATE(&t, 0, PthreadExit, 0); 1938 PTHREAD_JOIN(t, 0); 1939 } 1940} 1941 1942#ifdef __EXCEPTIONS 1943NOINLINE static void StackReuseAndException() { 1944 int large_stack[1000]; 1945 Ident(large_stack); 1946 ASAN_THROW(1); 1947} 1948 1949// TODO(kcc): support exceptions with use-after-return. 1950TEST(AddressSanitizer, DISABLED_StressStackReuseAndExceptionsTest) { 1951 for (int i = 0; i < 10000; i++) { 1952 try { 1953 StackReuseAndException(); 1954 } catch(...) { 1955 } 1956 } 1957} 1958#endif 1959 1960TEST(AddressSanitizer, MlockTest) { 1961 EXPECT_EQ(0, mlockall(MCL_CURRENT)); 1962 EXPECT_EQ(0, mlock((void*)0x12345, 0x5678)); 1963 EXPECT_EQ(0, munlockall()); 1964 EXPECT_EQ(0, munlock((void*)0x987, 0x654)); 1965} 1966 1967struct LargeStruct { 1968 int foo[100]; 1969}; 1970 1971// Test for bug http://llvm.org/bugs/show_bug.cgi?id=11763. 1972// Struct copy should not cause asan warning even if lhs == rhs. 1973TEST(AddressSanitizer, LargeStructCopyTest) { 1974 LargeStruct a; 1975 *Ident(&a) = *Ident(&a); 1976} 1977 1978ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS 1979static void NoAddressSafety() { 1980 char *foo = new char[10]; 1981 Ident(foo)[10] = 0; 1982 delete [] foo; 1983} 1984 1985TEST(AddressSanitizer, AttributeNoAddressSafetyTest) { 1986 Ident(NoAddressSafety)(); 1987} 1988 1989// ------------------ demo tests; run each one-by-one ------------- 1990// e.g. --gtest_filter=*DemoOOBLeftHigh --gtest_also_run_disabled_tests 1991TEST(AddressSanitizer, DISABLED_DemoThreadedTest) { 1992 ThreadedTestSpawn(); 1993} 1994 1995void *SimpleBugOnSTack(void *x = 0) { 1996 char a[20]; 1997 Ident(a)[20] = 0; 1998 return 0; 1999} 2000 2001TEST(AddressSanitizer, DISABLED_DemoStackTest) { 2002 SimpleBugOnSTack(); 2003} 2004 2005TEST(AddressSanitizer, DISABLED_DemoThreadStackTest) { 2006 pthread_t t; 2007 PTHREAD_CREATE(&t, 0, SimpleBugOnSTack, 0); 2008 PTHREAD_JOIN(t, 0); 2009} 2010 2011TEST(AddressSanitizer, DISABLED_DemoUAFLowIn) { 2012 uaf_test<U1>(10, 0); 2013} 2014TEST(AddressSanitizer, DISABLED_DemoUAFLowLeft) { 2015 uaf_test<U1>(10, -2); 2016} 2017TEST(AddressSanitizer, DISABLED_DemoUAFLowRight) { 2018 uaf_test<U1>(10, 10); 2019} 2020 2021TEST(AddressSanitizer, DISABLED_DemoUAFHigh) { 2022 uaf_test<U1>(kLargeMalloc, 0); 2023} 2024 2025TEST(AddressSanitizer, DISABLED_DemoOOBLeftLow) { 2026 oob_test<U1>(10, -1); 2027} 2028 2029TEST(AddressSanitizer, DISABLED_DemoOOBLeftHigh) { 2030 oob_test<U1>(kLargeMalloc, -1); 2031} 2032 2033TEST(AddressSanitizer, DISABLED_DemoOOBRightLow) { 2034 oob_test<U1>(10, 10); 2035} 2036 2037TEST(AddressSanitizer, DISABLED_DemoOOBRightHigh) { 2038 oob_test<U1>(kLargeMalloc, kLargeMalloc); 2039} 2040 2041TEST(AddressSanitizer, DISABLED_DemoOOM) { 2042 size_t size = SANITIZER_WORDSIZE == 64 ? (size_t)(1ULL << 40) : (0xf0000000); 2043 printf("%p\n", malloc(size)); 2044} 2045 2046TEST(AddressSanitizer, DISABLED_DemoDoubleFreeTest) { 2047 DoubleFree(); 2048} 2049 2050TEST(AddressSanitizer, DISABLED_DemoNullDerefTest) { 2051 int *a = 0; 2052 Ident(a)[10] = 0; 2053} 2054 2055TEST(AddressSanitizer, DISABLED_DemoFunctionStaticTest) { 2056 static char a[100]; 2057 static char b[100]; 2058 static char c[100]; 2059 Ident(a); 2060 Ident(b); 2061 Ident(c); 2062 Ident(a)[5] = 0; 2063 Ident(b)[105] = 0; 2064 Ident(a)[5] = 0; 2065} 2066 2067TEST(AddressSanitizer, DISABLED_DemoTooMuchMemoryTest) { 2068 const size_t kAllocSize = (1 << 28) - 1024; 2069 size_t total_size = 0; 2070 while (true) { 2071 char *x = (char*)malloc(kAllocSize); 2072 memset(x, 0, kAllocSize); 2073 total_size += kAllocSize; 2074 fprintf(stderr, "total: %ldM %p\n", (long)total_size >> 20, x); 2075 } 2076} 2077 2078// http://code.google.com/p/address-sanitizer/issues/detail?id=66 2079TEST(AddressSanitizer, BufferOverflowAfterManyFrees) { 2080 for (int i = 0; i < 1000000; i++) { 2081 delete [] (Ident(new char [8644])); 2082 } 2083 char *x = new char[8192]; 2084 EXPECT_DEATH(x[Ident(8192)] = 0, "AddressSanitizer: heap-buffer-overflow"); 2085 delete [] Ident(x); 2086} 2087 2088#ifdef __APPLE__ 2089#include "asan_mac_test.h" 2090TEST(AddressSanitizerMac, CFAllocatorDefaultDoubleFree) { 2091 EXPECT_DEATH( 2092 CFAllocatorDefaultDoubleFree(NULL), 2093 "attempting double-free"); 2094} 2095 2096void CFAllocator_DoubleFreeOnPthread() { 2097 pthread_t child; 2098 PTHREAD_CREATE(&child, NULL, CFAllocatorDefaultDoubleFree, NULL); 2099 PTHREAD_JOIN(child, NULL); // Shouldn't be reached. 2100} 2101 2102TEST(AddressSanitizerMac, CFAllocatorDefaultDoubleFree_ChildPhread) { 2103 EXPECT_DEATH(CFAllocator_DoubleFreeOnPthread(), "attempting double-free"); 2104} 2105 2106namespace { 2107 2108void *GLOB; 2109 2110void *CFAllocatorAllocateToGlob(void *unused) { 2111 GLOB = CFAllocatorAllocate(NULL, 100, /*hint*/0); 2112 return NULL; 2113} 2114 2115void *CFAllocatorDeallocateFromGlob(void *unused) { 2116 char *p = (char*)GLOB; 2117 p[100] = 'A'; // ASan should report an error here. 2118 CFAllocatorDeallocate(NULL, GLOB); 2119 return NULL; 2120} 2121 2122void CFAllocator_PassMemoryToAnotherThread() { 2123 pthread_t th1, th2; 2124 PTHREAD_CREATE(&th1, NULL, CFAllocatorAllocateToGlob, NULL); 2125 PTHREAD_JOIN(th1, NULL); 2126 PTHREAD_CREATE(&th2, NULL, CFAllocatorDeallocateFromGlob, NULL); 2127 PTHREAD_JOIN(th2, NULL); 2128} 2129 2130TEST(AddressSanitizerMac, CFAllocator_PassMemoryToAnotherThread) { 2131 EXPECT_DEATH(CFAllocator_PassMemoryToAnotherThread(), 2132 "heap-buffer-overflow"); 2133} 2134 2135} // namespace 2136 2137// TODO(glider): figure out whether we still need these tests. Is it correct 2138// to intercept the non-default CFAllocators? 2139TEST(AddressSanitizerMac, DISABLED_CFAllocatorSystemDefaultDoubleFree) { 2140 EXPECT_DEATH( 2141 CFAllocatorSystemDefaultDoubleFree(), 2142 "attempting double-free"); 2143} 2144 2145// We're intercepting malloc, so kCFAllocatorMalloc is routed to ASan. 2146TEST(AddressSanitizerMac, CFAllocatorMallocDoubleFree) { 2147 EXPECT_DEATH(CFAllocatorMallocDoubleFree(), "attempting double-free"); 2148} 2149 2150TEST(AddressSanitizerMac, DISABLED_CFAllocatorMallocZoneDoubleFree) { 2151 EXPECT_DEATH(CFAllocatorMallocZoneDoubleFree(), "attempting double-free"); 2152} 2153 2154// For libdispatch tests below we check that ASan got to the shadow byte 2155// legend, i.e. managed to print the thread stacks (this almost certainly 2156// means that the libdispatch task creation has been intercepted correctly). 2157TEST(AddressSanitizerMac, GCDDispatchAsync) { 2158 // Make sure the whole ASan report is printed, i.e. that we don't die 2159 // on a CHECK. 2160 EXPECT_DEATH(TestGCDDispatchAsync(), "Shadow byte legend"); 2161} 2162 2163TEST(AddressSanitizerMac, GCDDispatchSync) { 2164 // Make sure the whole ASan report is printed, i.e. that we don't die 2165 // on a CHECK. 2166 EXPECT_DEATH(TestGCDDispatchSync(), "Shadow byte legend"); 2167} 2168 2169 2170TEST(AddressSanitizerMac, GCDReuseWqthreadsAsync) { 2171 // Make sure the whole ASan report is printed, i.e. that we don't die 2172 // on a CHECK. 2173 EXPECT_DEATH(TestGCDReuseWqthreadsAsync(), "Shadow byte legend"); 2174} 2175 2176TEST(AddressSanitizerMac, GCDReuseWqthreadsSync) { 2177 // Make sure the whole ASan report is printed, i.e. that we don't die 2178 // on a CHECK. 2179 EXPECT_DEATH(TestGCDReuseWqthreadsSync(), "Shadow byte legend"); 2180} 2181 2182TEST(AddressSanitizerMac, GCDDispatchAfter) { 2183 // Make sure the whole ASan report is printed, i.e. that we don't die 2184 // on a CHECK. 2185 EXPECT_DEATH(TestGCDDispatchAfter(), "Shadow byte legend"); 2186} 2187 2188TEST(AddressSanitizerMac, GCDSourceEvent) { 2189 // Make sure the whole ASan report is printed, i.e. that we don't die 2190 // on a CHECK. 2191 EXPECT_DEATH(TestGCDSourceEvent(), "Shadow byte legend"); 2192} 2193 2194TEST(AddressSanitizerMac, GCDSourceCancel) { 2195 // Make sure the whole ASan report is printed, i.e. that we don't die 2196 // on a CHECK. 2197 EXPECT_DEATH(TestGCDSourceCancel(), "Shadow byte legend"); 2198} 2199 2200TEST(AddressSanitizerMac, GCDGroupAsync) { 2201 // Make sure the whole ASan report is printed, i.e. that we don't die 2202 // on a CHECK. 2203 EXPECT_DEATH(TestGCDGroupAsync(), "Shadow byte legend"); 2204} 2205 2206void *MallocIntrospectionLockWorker(void *_) { 2207 const int kNumPointers = 100; 2208 int i; 2209 void *pointers[kNumPointers]; 2210 for (i = 0; i < kNumPointers; i++) { 2211 pointers[i] = malloc(i + 1); 2212 } 2213 for (i = 0; i < kNumPointers; i++) { 2214 free(pointers[i]); 2215 } 2216 2217 return NULL; 2218} 2219 2220void *MallocIntrospectionLockForker(void *_) { 2221 pid_t result = fork(); 2222 if (result == -1) { 2223 perror("fork"); 2224 } 2225 assert(result != -1); 2226 if (result == 0) { 2227 // Call malloc in the child process to make sure we won't deadlock. 2228 void *ptr = malloc(42); 2229 free(ptr); 2230 exit(0); 2231 } else { 2232 // Return in the parent process. 2233 return NULL; 2234 } 2235} 2236 2237TEST(AddressSanitizerMac, MallocIntrospectionLock) { 2238 // Incorrect implementation of force_lock and force_unlock in our malloc zone 2239 // will cause forked processes to deadlock. 2240 // TODO(glider): need to detect that none of the child processes deadlocked. 2241 const int kNumWorkers = 5, kNumIterations = 100; 2242 int i, iter; 2243 for (iter = 0; iter < kNumIterations; iter++) { 2244 pthread_t workers[kNumWorkers], forker; 2245 for (i = 0; i < kNumWorkers; i++) { 2246 PTHREAD_CREATE(&workers[i], 0, MallocIntrospectionLockWorker, 0); 2247 } 2248 PTHREAD_CREATE(&forker, 0, MallocIntrospectionLockForker, 0); 2249 for (i = 0; i < kNumWorkers; i++) { 2250 PTHREAD_JOIN(workers[i], 0); 2251 } 2252 PTHREAD_JOIN(forker, 0); 2253 } 2254} 2255 2256void *TSDAllocWorker(void *test_key) { 2257 if (test_key) { 2258 void *mem = malloc(10); 2259 pthread_setspecific(*(pthread_key_t*)test_key, mem); 2260 } 2261 return NULL; 2262} 2263 2264TEST(AddressSanitizerMac, DISABLED_TSDWorkqueueTest) { 2265 pthread_t th; 2266 pthread_key_t test_key; 2267 pthread_key_create(&test_key, CallFreeOnWorkqueue); 2268 PTHREAD_CREATE(&th, NULL, TSDAllocWorker, &test_key); 2269 PTHREAD_JOIN(th, NULL); 2270 pthread_key_delete(test_key); 2271} 2272 2273// Test that CFStringCreateCopy does not copy constant strings. 2274TEST(AddressSanitizerMac, CFStringCreateCopy) { 2275 CFStringRef str = CFSTR("Hello world!\n"); 2276 CFStringRef str2 = CFStringCreateCopy(0, str); 2277 EXPECT_EQ(str, str2); 2278} 2279 2280TEST(AddressSanitizerMac, NSObjectOOB) { 2281 // Make sure that our allocators are used for NSObjects. 2282 EXPECT_DEATH(TestOOBNSObjects(), "heap-buffer-overflow"); 2283} 2284 2285// Make sure that correct pointer is passed to free() when deallocating a 2286// NSURL object. 2287// See http://code.google.com/p/address-sanitizer/issues/detail?id=70. 2288TEST(AddressSanitizerMac, NSURLDeallocation) { 2289 TestNSURLDeallocation(); 2290} 2291 2292// See http://code.google.com/p/address-sanitizer/issues/detail?id=109. 2293TEST(AddressSanitizerMac, Mstats) { 2294 malloc_statistics_t stats1, stats2; 2295 malloc_zone_statistics(/*all zones*/NULL, &stats1); 2296 const int kMallocSize = 100000; 2297 void *alloc = Ident(malloc(kMallocSize)); 2298 malloc_zone_statistics(/*all zones*/NULL, &stats2); 2299 EXPECT_GT(stats2.blocks_in_use, stats1.blocks_in_use); 2300 EXPECT_GE(stats2.size_in_use - stats1.size_in_use, kMallocSize); 2301 free(alloc); 2302 // Even the default OSX allocator may not change the stats after free(). 2303} 2304#endif // __APPLE__ 2305 2306// Test that instrumentation of stack allocations takes into account 2307// AllocSize of a type, and not its StoreSize (16 vs 10 bytes for long double). 2308// See http://llvm.org/bugs/show_bug.cgi?id=12047 for more details. 2309TEST(AddressSanitizer, LongDoubleNegativeTest) { 2310 long double a, b; 2311 static long double c; 2312 memcpy(Ident(&a), Ident(&b), sizeof(long double)); 2313 memcpy(Ident(&c), Ident(&b), sizeof(long double)); 2314} 2315