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