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