| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_allocator.cc ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Implementation of ASan's memory allocator. |
| 13 | // Evey piece of memory (AsanChunk) allocated by the allocator |
| 14 | // has a left redzone of REDZONE bytes and |
| 15 | // a right redzone such that the end of the chunk is aligned by REDZONE |
| 16 | // (i.e. the right redzone is between 0 and REDZONE-1). |
| 17 | // The left redzone is always poisoned. |
| 18 | // The right redzone is poisoned on malloc, the body is poisoned on free. |
| 19 | // Once freed, a chunk is moved to a quarantine (fifo list). |
| 20 | // After quarantine, a chunk is returned to freelists. |
| 21 | // |
| 22 | // The left redzone contains ASan's internal data and the stack trace of |
| 23 | // the malloc call. |
| 24 | // Once freed, the body of the chunk contains the stack trace of the free call. |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "asan_allocator.h" |
| 29 | #include "asan_interceptors.h" |
| 30 | #include "asan_interface.h" |
| 31 | #include "asan_internal.h" |
| 32 | #include "asan_lock.h" |
| 33 | #include "asan_mapping.h" |
| 34 | #include "asan_stats.h" |
| 35 | #include "asan_thread.h" |
| 36 | #include "asan_thread_registry.h" |
| 37 | |
| Kostya Serebryany | 8582208 | 2012-01-30 20:55:02 +0000 | [diff] [blame] | 38 | #ifdef _WIN32 |
| 39 | #include <intrin.h> |
| 40 | #endif |
| 41 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 42 | namespace __asan { |
| 43 | |
| 44 | #define REDZONE FLAG_redzone |
| 45 | static const size_t kMinAllocSize = REDZONE * 2; |
| 46 | static const size_t kMinMmapSize = 4UL << 20; // 4M |
| 47 | static const uint64_t kMaxAvailableRam = 128ULL << 30; // 128G |
| 48 | static const size_t kMaxThreadLocalQuarantine = 1 << 20; // 1M |
| 49 | static const size_t kMaxSizeForThreadLocalFreeList = 1 << 17; |
| 50 | |
| 51 | // Size classes less than kMallocSizeClassStep are powers of two. |
| 52 | // All other size classes are multiples of kMallocSizeClassStep. |
| 53 | static const size_t kMallocSizeClassStepLog = 26; |
| 54 | static const size_t kMallocSizeClassStep = 1UL << kMallocSizeClassStepLog; |
| 55 | |
| 56 | #if __WORDSIZE == 32 |
| 57 | static const size_t kMaxAllowedMallocSize = 3UL << 30; // 3G |
| 58 | #else |
| 59 | static const size_t kMaxAllowedMallocSize = 8UL << 30; // 8G |
| 60 | #endif |
| 61 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 62 | static inline bool IsAligned(uintptr_t a, uintptr_t alignment) { |
| 63 | return (a & (alignment - 1)) == 0; |
| 64 | } |
| 65 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 66 | static inline size_t Log2(size_t x) { |
| 67 | CHECK(IsPowerOfTwo(x)); |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 68 | #if defined(_WIN64) |
| 69 | unsigned long ret; // NOLINT |
| 70 | _BitScanForward64(&ret, x); |
| 71 | return ret; |
| 72 | #elif defined(_WIN32) |
| 73 | unsigned long ret; // NOLINT |
| 74 | _BitScanForward(&ret, x); |
| 75 | return ret; |
| 76 | #else |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 77 | return __builtin_ctzl(x); |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 78 | #endif |
| 79 | } |
| 80 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 81 | static inline size_t RoundUpToPowerOfTwo(size_t size) { |
| 82 | CHECK(size); |
| 83 | if (IsPowerOfTwo(size)) return size; |
| Alexander Potapenko | 6f04529 | 2012-01-27 15:15:04 +0000 | [diff] [blame] | 84 | |
| Alexey Samsonov | f927ddc | 2012-02-03 08:50:16 +0000 | [diff] [blame] | 85 | unsigned long up; // NOLINT |
| 86 | #if defined(_WIN64) |
| 87 | _BitScanReverse64(&up, size); |
| 88 | #elif defined(_WIN32) |
| 89 | _BitScanReverse(&up, size); |
| 90 | #else |
| 91 | up = __WORDSIZE - 1 - __builtin_clzl(size); |
| 92 | #endif |
| 93 | CHECK(size < (1ULL << (up + 1))); |
| 94 | CHECK(size > (1ULL << up)); |
| 95 | return 1UL << (up + 1); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static inline size_t SizeClassToSize(uint8_t size_class) { |
| 99 | CHECK(size_class < kNumberOfSizeClasses); |
| 100 | if (size_class <= kMallocSizeClassStepLog) { |
| 101 | return 1UL << size_class; |
| 102 | } else { |
| 103 | return (size_class - kMallocSizeClassStepLog) * kMallocSizeClassStep; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static inline uint8_t SizeToSizeClass(size_t size) { |
| 108 | uint8_t res = 0; |
| 109 | if (size <= kMallocSizeClassStep) { |
| 110 | size_t rounded = RoundUpToPowerOfTwo(size); |
| 111 | res = Log2(rounded); |
| 112 | } else { |
| 113 | res = ((size + kMallocSizeClassStep - 1) / kMallocSizeClassStep) |
| 114 | + kMallocSizeClassStepLog; |
| 115 | } |
| 116 | CHECK(res < kNumberOfSizeClasses); |
| 117 | CHECK(size <= SizeClassToSize(res)); |
| 118 | return res; |
| 119 | } |
| 120 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 121 | // Given REDZONE bytes, we need to mark first size bytes |
| 122 | // as addressable and the rest REDZONE-size bytes as unaddressable. |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 123 | static void PoisonHeapPartialRightRedzone(uintptr_t mem, size_t size) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 124 | CHECK(size <= REDZONE); |
| 125 | CHECK(IsAligned(mem, REDZONE)); |
| 126 | CHECK(IsPowerOfTwo(SHADOW_GRANULARITY)); |
| 127 | CHECK(IsPowerOfTwo(REDZONE)); |
| 128 | CHECK(REDZONE >= SHADOW_GRANULARITY); |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 129 | PoisonShadowPartialRightRedzone(mem, size, REDZONE, |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 130 | kAsanHeapRightRedzoneMagic); |
| 131 | } |
| 132 | |
| 133 | static uint8_t *MmapNewPagesAndPoisonShadow(size_t size) { |
| 134 | CHECK(IsAligned(size, kPageSize)); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 135 | uint8_t *res = (uint8_t*)AsanMmapSomewhereOrDie(size, __FUNCTION__); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 136 | PoisonShadow((uintptr_t)res, size, kAsanHeapLeftRedzoneMagic); |
| 137 | if (FLAG_debug) { |
| 138 | Printf("ASAN_MMAP: [%p, %p)\n", res, res + size); |
| 139 | } |
| 140 | return res; |
| 141 | } |
| 142 | |
| 143 | // Every chunk of memory allocated by this allocator can be in one of 3 states: |
| 144 | // CHUNK_AVAILABLE: the chunk is in the free list and ready to be allocated. |
| 145 | // CHUNK_ALLOCATED: the chunk is allocated and not yet freed. |
| 146 | // CHUNK_QUARANTINE: the chunk was freed and put into quarantine zone. |
| 147 | // |
| 148 | // The pseudo state CHUNK_MEMALIGN is used to mark that the address is not |
| 149 | // the beginning of a AsanChunk (in which case 'next' contains the address |
| 150 | // of the AsanChunk). |
| 151 | // |
| 152 | // The magic numbers for the enum values are taken randomly. |
| 153 | enum { |
| 154 | CHUNK_AVAILABLE = 0x573B, |
| 155 | CHUNK_ALLOCATED = 0x3204, |
| 156 | CHUNK_QUARANTINE = 0x1978, |
| 157 | CHUNK_MEMALIGN = 0xDC68, |
| 158 | }; |
| 159 | |
| 160 | struct ChunkBase { |
| 161 | uint16_t chunk_state; |
| 162 | uint8_t size_class; |
| 163 | uint32_t offset; // User-visible memory starts at this+offset (beg()). |
| 164 | int32_t alloc_tid; |
| 165 | int32_t free_tid; |
| 166 | size_t used_size; // Size requested by the user. |
| 167 | AsanChunk *next; |
| 168 | |
| 169 | uintptr_t beg() { return (uintptr_t)this + offset; } |
| 170 | size_t Size() { return SizeClassToSize(size_class); } |
| 171 | uint8_t SizeClass() { return size_class; } |
| 172 | }; |
| 173 | |
| 174 | struct AsanChunk: public ChunkBase { |
| 175 | uint32_t *compressed_alloc_stack() { |
| 176 | CHECK(REDZONE >= sizeof(ChunkBase)); |
| 177 | return (uint32_t*)((uintptr_t)this + sizeof(ChunkBase)); |
| 178 | } |
| 179 | uint32_t *compressed_free_stack() { |
| 180 | CHECK(REDZONE >= sizeof(ChunkBase)); |
| 181 | return (uint32_t*)((uintptr_t)this + REDZONE); |
| 182 | } |
| 183 | |
| 184 | // The left redzone after the ChunkBase is given to the alloc stack trace. |
| 185 | size_t compressed_alloc_stack_size() { |
| 186 | return (REDZONE - sizeof(ChunkBase)) / sizeof(uint32_t); |
| 187 | } |
| 188 | size_t compressed_free_stack_size() { |
| 189 | return (REDZONE) / sizeof(uint32_t); |
| 190 | } |
| 191 | |
| 192 | bool AddrIsInside(uintptr_t addr, size_t access_size, size_t *offset) { |
| 193 | if (addr >= beg() && (addr + access_size) <= (beg() + used_size)) { |
| 194 | *offset = addr - beg(); |
| 195 | return true; |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | bool AddrIsAtLeft(uintptr_t addr, size_t access_size, size_t *offset) { |
| 201 | if (addr < beg()) { |
| 202 | *offset = beg() - addr; |
| 203 | return true; |
| 204 | } |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | bool AddrIsAtRight(uintptr_t addr, size_t access_size, size_t *offset) { |
| 209 | if (addr + access_size >= beg() + used_size) { |
| 210 | if (addr <= beg() + used_size) |
| 211 | *offset = 0; |
| 212 | else |
| 213 | *offset = addr - (beg() + used_size); |
| 214 | return true; |
| 215 | } |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | void DescribeAddress(uintptr_t addr, size_t access_size) { |
| 220 | size_t offset; |
| 221 | Printf("%p is located ", addr); |
| 222 | if (AddrIsInside(addr, access_size, &offset)) { |
| 223 | Printf("%ld bytes inside of", offset); |
| 224 | } else if (AddrIsAtLeft(addr, access_size, &offset)) { |
| 225 | Printf("%ld bytes to the left of", offset); |
| 226 | } else if (AddrIsAtRight(addr, access_size, &offset)) { |
| 227 | Printf("%ld bytes to the right of", offset); |
| 228 | } else { |
| 229 | Printf(" somewhere around (this is AddressSanitizer bug!)"); |
| 230 | } |
| 231 | Printf(" %lu-byte region [%p,%p)\n", |
| 232 | used_size, beg(), beg() + used_size); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | static AsanChunk *PtrToChunk(uintptr_t ptr) { |
| 237 | AsanChunk *m = (AsanChunk*)(ptr - REDZONE); |
| 238 | if (m->chunk_state == CHUNK_MEMALIGN) { |
| 239 | m = m->next; |
| 240 | } |
| 241 | return m; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | void AsanChunkFifoList::PushList(AsanChunkFifoList *q) { |
| 246 | if (last_) { |
| 247 | CHECK(first_); |
| 248 | CHECK(!last_->next); |
| 249 | last_->next = q->first_; |
| 250 | last_ = q->last_; |
| 251 | } else { |
| 252 | CHECK(!first_); |
| 253 | last_ = q->last_; |
| 254 | first_ = q->first_; |
| 255 | } |
| 256 | size_ += q->size(); |
| 257 | q->clear(); |
| 258 | } |
| 259 | |
| 260 | void AsanChunkFifoList::Push(AsanChunk *n) { |
| 261 | CHECK(n->next == NULL); |
| 262 | if (last_) { |
| 263 | CHECK(first_); |
| 264 | CHECK(!last_->next); |
| 265 | last_->next = n; |
| 266 | last_ = n; |
| 267 | } else { |
| 268 | CHECK(!first_); |
| 269 | last_ = first_ = n; |
| 270 | } |
| 271 | size_ += n->Size(); |
| 272 | } |
| 273 | |
| 274 | // Interesting performance observation: this function takes up to 15% of overal |
| 275 | // allocator time. That's because *first_ has been evicted from cache long time |
| 276 | // ago. Not sure if we can or want to do anything with this. |
| 277 | AsanChunk *AsanChunkFifoList::Pop() { |
| 278 | CHECK(first_); |
| 279 | AsanChunk *res = first_; |
| 280 | first_ = first_->next; |
| 281 | if (first_ == NULL) |
| 282 | last_ = NULL; |
| 283 | CHECK(size_ >= res->Size()); |
| 284 | size_ -= res->Size(); |
| 285 | if (last_) { |
| 286 | CHECK(!last_->next); |
| 287 | } |
| 288 | return res; |
| 289 | } |
| 290 | |
| 291 | // All pages we ever allocated. |
| 292 | struct PageGroup { |
| 293 | uintptr_t beg; |
| 294 | uintptr_t end; |
| 295 | size_t size_of_chunk; |
| 296 | uintptr_t last_chunk; |
| 297 | bool InRange(uintptr_t addr) { |
| 298 | return addr >= beg && addr < end; |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | class MallocInfo { |
| 303 | public: |
| 304 | |
| 305 | explicit MallocInfo(LinkerInitialized x) : mu_(x) { } |
| 306 | |
| 307 | AsanChunk *AllocateChunks(uint8_t size_class, size_t n_chunks) { |
| 308 | AsanChunk *m = NULL; |
| 309 | AsanChunk **fl = &free_lists_[size_class]; |
| 310 | { |
| 311 | ScopedLock lock(&mu_); |
| 312 | for (size_t i = 0; i < n_chunks; i++) { |
| 313 | if (!(*fl)) { |
| 314 | *fl = GetNewChunks(size_class); |
| 315 | } |
| 316 | AsanChunk *t = *fl; |
| 317 | *fl = t->next; |
| 318 | t->next = m; |
| 319 | CHECK(t->chunk_state == CHUNK_AVAILABLE); |
| 320 | m = t; |
| 321 | } |
| 322 | } |
| 323 | return m; |
| 324 | } |
| 325 | |
| 326 | void SwallowThreadLocalMallocStorage(AsanThreadLocalMallocStorage *x, |
| 327 | bool eat_free_lists) { |
| 328 | CHECK(FLAG_quarantine_size > 0); |
| 329 | ScopedLock lock(&mu_); |
| 330 | AsanChunkFifoList *q = &x->quarantine_; |
| 331 | if (q->size() > 0) { |
| 332 | quarantine_.PushList(q); |
| 333 | while (quarantine_.size() > FLAG_quarantine_size) { |
| 334 | QuarantinePop(); |
| 335 | } |
| 336 | } |
| 337 | if (eat_free_lists) { |
| 338 | for (size_t size_class = 0; size_class < kNumberOfSizeClasses; |
| 339 | size_class++) { |
| 340 | AsanChunk *m = x->free_lists_[size_class]; |
| 341 | while (m) { |
| 342 | AsanChunk *t = m->next; |
| 343 | m->next = free_lists_[size_class]; |
| 344 | free_lists_[size_class] = m; |
| 345 | m = t; |
| 346 | } |
| 347 | x->free_lists_[size_class] = 0; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void BypassThreadLocalQuarantine(AsanChunk *chunk) { |
| 353 | ScopedLock lock(&mu_); |
| 354 | quarantine_.Push(chunk); |
| 355 | } |
| 356 | |
| 357 | AsanChunk *FindMallocedOrFreed(uintptr_t addr, size_t access_size) { |
| 358 | ScopedLock lock(&mu_); |
| 359 | return FindChunkByAddr(addr); |
| 360 | } |
| 361 | |
| 362 | // TODO(glider): AllocationSize() may become very slow if the size of |
| 363 | // page_groups_ grows. This can be fixed by increasing kMinMmapSize, |
| 364 | // but a better solution is to speed up the search somehow. |
| 365 | size_t AllocationSize(uintptr_t ptr) { |
| Alexey Samsonov | ca2278d | 2012-01-18 15:26:55 +0000 | [diff] [blame] | 366 | if (!ptr) return 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 367 | ScopedLock lock(&mu_); |
| 368 | |
| 369 | // first, check if this is our memory |
| 370 | PageGroup *g = FindPageGroupUnlocked(ptr); |
| 371 | if (!g) return 0; |
| 372 | AsanChunk *m = PtrToChunk(ptr); |
| 373 | if (m->chunk_state == CHUNK_ALLOCATED) { |
| 374 | return m->used_size; |
| 375 | } else { |
| 376 | return 0; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void ForceLock() { |
| 381 | mu_.Lock(); |
| 382 | } |
| 383 | |
| 384 | void ForceUnlock() { |
| 385 | mu_.Unlock(); |
| 386 | } |
| 387 | |
| 388 | void PrintStatus() { |
| 389 | ScopedLock lock(&mu_); |
| 390 | size_t malloced = 0; |
| 391 | |
| 392 | Printf(" MallocInfo: in quarantine: %ld malloced: %ld; ", |
| 393 | quarantine_.size() >> 20, malloced >> 20); |
| 394 | for (size_t j = 1; j < kNumberOfSizeClasses; j++) { |
| 395 | AsanChunk *i = free_lists_[j]; |
| 396 | if (!i) continue; |
| 397 | size_t t = 0; |
| 398 | for (; i; i = i->next) { |
| 399 | t += i->Size(); |
| 400 | } |
| 401 | Printf("%ld:%ld ", j, t >> 20); |
| 402 | } |
| 403 | Printf("\n"); |
| 404 | } |
| 405 | |
| 406 | PageGroup *FindPageGroup(uintptr_t addr) { |
| 407 | ScopedLock lock(&mu_); |
| 408 | return FindPageGroupUnlocked(addr); |
| 409 | } |
| 410 | |
| 411 | private: |
| 412 | PageGroup *FindPageGroupUnlocked(uintptr_t addr) { |
| 413 | for (int i = 0; i < n_page_groups_; i++) { |
| 414 | PageGroup *g = page_groups_[i]; |
| 415 | if (g->InRange(addr)) { |
| 416 | return g; |
| 417 | } |
| 418 | } |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | // We have an address between two chunks, and we want to report just one. |
| 423 | AsanChunk *ChooseChunk(uintptr_t addr, |
| 424 | AsanChunk *left_chunk, AsanChunk *right_chunk) { |
| 425 | // Prefer an allocated chunk or a chunk from quarantine. |
| 426 | if (left_chunk->chunk_state == CHUNK_AVAILABLE && |
| 427 | right_chunk->chunk_state != CHUNK_AVAILABLE) |
| 428 | return right_chunk; |
| 429 | if (right_chunk->chunk_state == CHUNK_AVAILABLE && |
| 430 | left_chunk->chunk_state != CHUNK_AVAILABLE) |
| 431 | return left_chunk; |
| 432 | // Choose based on offset. |
| Daniel Dunbar | 514cebb | 2011-12-02 01:36:38 +0000 | [diff] [blame] | 433 | size_t l_offset = 0, r_offset = 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 434 | CHECK(left_chunk->AddrIsAtRight(addr, 1, &l_offset)); |
| 435 | CHECK(right_chunk->AddrIsAtLeft(addr, 1, &r_offset)); |
| 436 | if (l_offset < r_offset) |
| 437 | return left_chunk; |
| 438 | return right_chunk; |
| 439 | } |
| 440 | |
| 441 | AsanChunk *FindChunkByAddr(uintptr_t addr) { |
| 442 | PageGroup *g = FindPageGroupUnlocked(addr); |
| 443 | if (!g) return 0; |
| 444 | CHECK(g->size_of_chunk); |
| 445 | uintptr_t offset_from_beg = addr - g->beg; |
| 446 | uintptr_t this_chunk_addr = g->beg + |
| 447 | (offset_from_beg / g->size_of_chunk) * g->size_of_chunk; |
| 448 | CHECK(g->InRange(this_chunk_addr)); |
| 449 | AsanChunk *m = (AsanChunk*)this_chunk_addr; |
| 450 | CHECK(m->chunk_state == CHUNK_ALLOCATED || |
| 451 | m->chunk_state == CHUNK_AVAILABLE || |
| 452 | m->chunk_state == CHUNK_QUARANTINE); |
| Daniel Dunbar | 514cebb | 2011-12-02 01:36:38 +0000 | [diff] [blame] | 453 | size_t offset = 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 454 | if (m->AddrIsInside(addr, 1, &offset)) |
| 455 | return m; |
| 456 | |
| 457 | if (m->AddrIsAtRight(addr, 1, &offset)) { |
| 458 | if (this_chunk_addr == g->last_chunk) // rightmost chunk |
| 459 | return m; |
| 460 | uintptr_t right_chunk_addr = this_chunk_addr + g->size_of_chunk; |
| 461 | CHECK(g->InRange(right_chunk_addr)); |
| 462 | return ChooseChunk(addr, m, (AsanChunk*)right_chunk_addr); |
| 463 | } else { |
| 464 | CHECK(m->AddrIsAtLeft(addr, 1, &offset)); |
| 465 | if (this_chunk_addr == g->beg) // leftmost chunk |
| 466 | return m; |
| 467 | uintptr_t left_chunk_addr = this_chunk_addr - g->size_of_chunk; |
| 468 | CHECK(g->InRange(left_chunk_addr)); |
| 469 | return ChooseChunk(addr, (AsanChunk*)left_chunk_addr, m); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | void QuarantinePop() { |
| 474 | CHECK(quarantine_.size() > 0); |
| 475 | AsanChunk *m = quarantine_.Pop(); |
| 476 | CHECK(m); |
| 477 | // if (F_v >= 2) Printf("MallocInfo::pop %p\n", m); |
| 478 | |
| 479 | CHECK(m->chunk_state == CHUNK_QUARANTINE); |
| 480 | m->chunk_state = CHUNK_AVAILABLE; |
| 481 | CHECK(m->alloc_tid >= 0); |
| 482 | CHECK(m->free_tid >= 0); |
| 483 | |
| 484 | size_t size_class = m->SizeClass(); |
| 485 | m->next = free_lists_[size_class]; |
| 486 | free_lists_[size_class] = m; |
| 487 | |
| Kostya Serebryany | 3074314 | 2011-12-05 19:17:53 +0000 | [diff] [blame] | 488 | // Statistics. |
| 489 | AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats(); |
| 490 | thread_stats.real_frees++; |
| 491 | thread_stats.really_freed += m->used_size; |
| 492 | thread_stats.really_freed_redzones += m->Size() - m->used_size; |
| 493 | thread_stats.really_freed_by_size[m->SizeClass()]++; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Get a list of newly allocated chunks. |
| 497 | AsanChunk *GetNewChunks(uint8_t size_class) { |
| 498 | size_t size = SizeClassToSize(size_class); |
| 499 | CHECK(IsPowerOfTwo(kMinMmapSize)); |
| 500 | CHECK(size < kMinMmapSize || (size % kMinMmapSize) == 0); |
| Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 501 | size_t mmap_size = Max(size, kMinMmapSize); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 502 | size_t n_chunks = mmap_size / size; |
| 503 | CHECK(n_chunks * size == mmap_size); |
| 504 | if (size < kPageSize) { |
| 505 | // Size is small, just poison the last chunk. |
| 506 | n_chunks--; |
| 507 | } else { |
| 508 | // Size is large, allocate an extra page at right and poison it. |
| 509 | mmap_size += kPageSize; |
| 510 | } |
| 511 | CHECK(n_chunks > 0); |
| 512 | uint8_t *mem = MmapNewPagesAndPoisonShadow(mmap_size); |
| Kostya Serebryany | 3074314 | 2011-12-05 19:17:53 +0000 | [diff] [blame] | 513 | |
| 514 | // Statistics. |
| 515 | AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats(); |
| 516 | thread_stats.mmaps++; |
| 517 | thread_stats.mmaped += mmap_size; |
| 518 | thread_stats.mmaped_by_size[size_class] += n_chunks; |
| 519 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 520 | AsanChunk *res = NULL; |
| 521 | for (size_t i = 0; i < n_chunks; i++) { |
| 522 | AsanChunk *m = (AsanChunk*)(mem + i * size); |
| 523 | m->chunk_state = CHUNK_AVAILABLE; |
| 524 | m->size_class = size_class; |
| 525 | m->next = res; |
| 526 | res = m; |
| 527 | } |
| 528 | PageGroup *pg = (PageGroup*)(mem + n_chunks * size); |
| 529 | // This memory is already poisoned, no need to poison it again. |
| 530 | pg->beg = (uintptr_t)mem; |
| 531 | pg->end = pg->beg + mmap_size; |
| 532 | pg->size_of_chunk = size; |
| 533 | pg->last_chunk = (uintptr_t)(mem + size * (n_chunks - 1)); |
| 534 | int page_group_idx = AtomicInc(&n_page_groups_) - 1; |
| 535 | CHECK(page_group_idx < (int)ASAN_ARRAY_SIZE(page_groups_)); |
| 536 | page_groups_[page_group_idx] = pg; |
| 537 | return res; |
| 538 | } |
| 539 | |
| 540 | AsanChunk *free_lists_[kNumberOfSizeClasses]; |
| 541 | AsanChunkFifoList quarantine_; |
| 542 | AsanLock mu_; |
| 543 | |
| 544 | PageGroup *page_groups_[kMaxAvailableRam / kMinMmapSize]; |
| 545 | int n_page_groups_; // atomic |
| 546 | }; |
| 547 | |
| 548 | static MallocInfo malloc_info(LINKER_INITIALIZED); |
| 549 | |
| 550 | void AsanThreadLocalMallocStorage::CommitBack() { |
| 551 | malloc_info.SwallowThreadLocalMallocStorage(this, true); |
| 552 | } |
| 553 | |
| 554 | static void Describe(uintptr_t addr, size_t access_size) { |
| 555 | AsanChunk *m = malloc_info.FindMallocedOrFreed(addr, access_size); |
| 556 | if (!m) return; |
| 557 | m->DescribeAddress(addr, access_size); |
| 558 | CHECK(m->alloc_tid >= 0); |
| 559 | AsanThreadSummary *alloc_thread = |
| 560 | asanThreadRegistry().FindByTid(m->alloc_tid); |
| 561 | AsanStackTrace alloc_stack; |
| 562 | AsanStackTrace::UncompressStack(&alloc_stack, m->compressed_alloc_stack(), |
| 563 | m->compressed_alloc_stack_size()); |
| 564 | AsanThread *t = asanThreadRegistry().GetCurrent(); |
| 565 | CHECK(t); |
| 566 | if (m->free_tid >= 0) { |
| 567 | AsanThreadSummary *free_thread = |
| 568 | asanThreadRegistry().FindByTid(m->free_tid); |
| 569 | Printf("freed by thread T%d here:\n", free_thread->tid()); |
| 570 | AsanStackTrace free_stack; |
| 571 | AsanStackTrace::UncompressStack(&free_stack, m->compressed_free_stack(), |
| 572 | m->compressed_free_stack_size()); |
| 573 | free_stack.PrintStack(); |
| 574 | Printf("previously allocated by thread T%d here:\n", |
| 575 | alloc_thread->tid()); |
| 576 | |
| 577 | alloc_stack.PrintStack(); |
| 578 | t->summary()->Announce(); |
| 579 | free_thread->Announce(); |
| 580 | alloc_thread->Announce(); |
| 581 | } else { |
| 582 | Printf("allocated by thread T%d here:\n", alloc_thread->tid()); |
| 583 | alloc_stack.PrintStack(); |
| 584 | t->summary()->Announce(); |
| 585 | alloc_thread->Announce(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static uint8_t *Allocate(size_t alignment, size_t size, AsanStackTrace *stack) { |
| 590 | __asan_init(); |
| 591 | CHECK(stack); |
| 592 | if (size == 0) { |
| 593 | size = 1; // TODO(kcc): do something smarter |
| 594 | } |
| 595 | CHECK(IsPowerOfTwo(alignment)); |
| 596 | size_t rounded_size = RoundUpTo(size, REDZONE); |
| 597 | size_t needed_size = rounded_size + REDZONE; |
| 598 | if (alignment > REDZONE) { |
| 599 | needed_size += alignment; |
| 600 | } |
| 601 | CHECK(IsAligned(needed_size, REDZONE)); |
| 602 | if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize) { |
| 603 | Report("WARNING: AddressSanitizer failed to allocate %p bytes\n", size); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | uint8_t size_class = SizeToSizeClass(needed_size); |
| 608 | size_t size_to_allocate = SizeClassToSize(size_class); |
| 609 | CHECK(size_to_allocate >= kMinAllocSize); |
| 610 | CHECK(size_to_allocate >= needed_size); |
| 611 | CHECK(IsAligned(size_to_allocate, REDZONE)); |
| 612 | |
| 613 | if (FLAG_v >= 2) { |
| 614 | Printf("Allocate align: %ld size: %ld class: %d real: %ld\n", |
| 615 | alignment, size, size_class, size_to_allocate); |
| 616 | } |
| 617 | |
| 618 | AsanThread *t = asanThreadRegistry().GetCurrent(); |
| 619 | AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats(); |
| Kostya Serebryany | 3074314 | 2011-12-05 19:17:53 +0000 | [diff] [blame] | 620 | // Statistics |
| 621 | thread_stats.mallocs++; |
| 622 | thread_stats.malloced += size; |
| 623 | thread_stats.malloced_redzones += size_to_allocate - size; |
| 624 | thread_stats.malloced_by_size[size_class]++; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 625 | |
| 626 | AsanChunk *m = NULL; |
| 627 | if (!t || size_to_allocate >= kMaxSizeForThreadLocalFreeList) { |
| 628 | // get directly from global storage. |
| 629 | m = malloc_info.AllocateChunks(size_class, 1); |
| Kostya Serebryany | 3074314 | 2011-12-05 19:17:53 +0000 | [diff] [blame] | 630 | thread_stats.malloc_large++; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 631 | } else { |
| 632 | // get from the thread-local storage. |
| 633 | AsanChunk **fl = &t->malloc_storage().free_lists_[size_class]; |
| 634 | if (!*fl) { |
| 635 | size_t n_new_chunks = kMaxSizeForThreadLocalFreeList / size_to_allocate; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 636 | *fl = malloc_info.AllocateChunks(size_class, n_new_chunks); |
| Kostya Serebryany | 3074314 | 2011-12-05 19:17:53 +0000 | [diff] [blame] | 637 | thread_stats.malloc_small_slow++; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 638 | } |
| 639 | m = *fl; |
| 640 | *fl = (*fl)->next; |
| 641 | } |
| 642 | CHECK(m); |
| 643 | CHECK(m->chunk_state == CHUNK_AVAILABLE); |
| 644 | m->chunk_state = CHUNK_ALLOCATED; |
| 645 | m->next = NULL; |
| 646 | CHECK(m->Size() == size_to_allocate); |
| 647 | uintptr_t addr = (uintptr_t)m + REDZONE; |
| 648 | CHECK(addr == (uintptr_t)m->compressed_free_stack()); |
| 649 | |
| 650 | if (alignment > REDZONE && (addr & (alignment - 1))) { |
| 651 | addr = RoundUpTo(addr, alignment); |
| 652 | CHECK((addr & (alignment - 1)) == 0); |
| 653 | AsanChunk *p = (AsanChunk*)(addr - REDZONE); |
| 654 | p->chunk_state = CHUNK_MEMALIGN; |
| 655 | p->next = m; |
| 656 | } |
| 657 | CHECK(m == PtrToChunk(addr)); |
| 658 | m->used_size = size; |
| 659 | m->offset = addr - (uintptr_t)m; |
| 660 | CHECK(m->beg() == addr); |
| 661 | m->alloc_tid = t ? t->tid() : 0; |
| 662 | m->free_tid = AsanThread::kInvalidTid; |
| 663 | AsanStackTrace::CompressStack(stack, m->compressed_alloc_stack(), |
| 664 | m->compressed_alloc_stack_size()); |
| 665 | PoisonShadow(addr, rounded_size, 0); |
| 666 | if (size < rounded_size) { |
| Kostya Serebryany | 218a9b7 | 2011-11-30 18:50:23 +0000 | [diff] [blame] | 667 | PoisonHeapPartialRightRedzone(addr + rounded_size - REDZONE, |
| 668 | size & (REDZONE - 1)); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 669 | } |
| 670 | if (size <= FLAG_max_malloc_fill_size) { |
| 671 | real_memset((void*)addr, 0, rounded_size); |
| 672 | } |
| 673 | return (uint8_t*)addr; |
| 674 | } |
| 675 | |
| 676 | static void Deallocate(uint8_t *ptr, AsanStackTrace *stack) { |
| 677 | if (!ptr) return; |
| 678 | CHECK(stack); |
| 679 | |
| 680 | if (FLAG_debug) { |
| 681 | CHECK(malloc_info.FindPageGroup((uintptr_t)ptr)); |
| 682 | } |
| 683 | |
| 684 | // Printf("Deallocate %p\n", ptr); |
| 685 | AsanChunk *m = PtrToChunk((uintptr_t)ptr); |
| 686 | if (m->chunk_state == CHUNK_QUARANTINE) { |
| Kostya Serebryany | c162013 | 2011-12-13 19:16:36 +0000 | [diff] [blame] | 687 | Report("ERROR: AddressSanitizer attempting double-free on %p:\n", ptr); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 688 | stack->PrintStack(); |
| 689 | m->DescribeAddress((uintptr_t)ptr, 1); |
| 690 | ShowStatsAndAbort(); |
| 691 | } else if (m->chunk_state != CHUNK_ALLOCATED) { |
| Kostya Serebryany | c162013 | 2011-12-13 19:16:36 +0000 | [diff] [blame] | 692 | Report("ERROR: AddressSanitizer attempting free on address which was not" |
| 693 | " malloc()-ed: %p\n", ptr); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 694 | stack->PrintStack(); |
| 695 | ShowStatsAndAbort(); |
| 696 | } |
| 697 | CHECK(m->chunk_state == CHUNK_ALLOCATED); |
| 698 | CHECK(m->free_tid == AsanThread::kInvalidTid); |
| 699 | CHECK(m->alloc_tid >= 0); |
| 700 | AsanThread *t = asanThreadRegistry().GetCurrent(); |
| 701 | m->free_tid = t ? t->tid() : 0; |
| 702 | AsanStackTrace::CompressStack(stack, m->compressed_free_stack(), |
| 703 | m->compressed_free_stack_size()); |
| 704 | size_t rounded_size = RoundUpTo(m->used_size, REDZONE); |
| 705 | PoisonShadow((uintptr_t)ptr, rounded_size, kAsanHeapFreeMagic); |
| 706 | |
| Kostya Serebryany | 3074314 | 2011-12-05 19:17:53 +0000 | [diff] [blame] | 707 | // Statistics. |
| 708 | AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats(); |
| 709 | thread_stats.frees++; |
| 710 | thread_stats.freed += m->used_size; |
| 711 | thread_stats.freed_by_size[m->SizeClass()]++; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 712 | |
| 713 | m->chunk_state = CHUNK_QUARANTINE; |
| 714 | if (t) { |
| 715 | AsanThreadLocalMallocStorage *ms = &t->malloc_storage(); |
| 716 | CHECK(!m->next); |
| 717 | ms->quarantine_.Push(m); |
| 718 | |
| 719 | if (ms->quarantine_.size() > kMaxThreadLocalQuarantine) { |
| 720 | malloc_info.SwallowThreadLocalMallocStorage(ms, false); |
| 721 | } |
| 722 | } else { |
| 723 | CHECK(!m->next); |
| 724 | malloc_info.BypassThreadLocalQuarantine(m); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | static uint8_t *Reallocate(uint8_t *old_ptr, size_t new_size, |
| 729 | AsanStackTrace *stack) { |
| 730 | CHECK(old_ptr && new_size); |
| Kostya Serebryany | 3074314 | 2011-12-05 19:17:53 +0000 | [diff] [blame] | 731 | |
| 732 | // Statistics. |
| 733 | AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats(); |
| 734 | thread_stats.reallocs++; |
| 735 | thread_stats.realloced += new_size; |
| 736 | |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 737 | AsanChunk *m = PtrToChunk((uintptr_t)old_ptr); |
| 738 | CHECK(m->chunk_state == CHUNK_ALLOCATED); |
| 739 | size_t old_size = m->used_size; |
| Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 740 | size_t memcpy_size = Min(new_size, old_size); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 741 | uint8_t *new_ptr = Allocate(0, new_size, stack); |
| 742 | if (new_ptr) { |
| Alexey Samsonov | f927ddc | 2012-02-03 08:50:16 +0000 | [diff] [blame] | 743 | CHECK(real_memcpy != NULL); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 744 | real_memcpy(new_ptr, old_ptr, memcpy_size); |
| 745 | Deallocate(old_ptr, stack); |
| 746 | } |
| 747 | return new_ptr; |
| 748 | } |
| 749 | |
| 750 | } // namespace __asan |
| 751 | |
| 752 | // Malloc hooks declaration. |
| 753 | // ASAN_NEW_HOOK(ptr, size) is called immediately after |
| 754 | // allocation of "size" bytes, which returned "ptr". |
| 755 | // ASAN_DELETE_HOOK(ptr) is called immediately before |
| 756 | // deallocation of "ptr". |
| 757 | // If ASAN_NEW_HOOK or ASAN_DELETE_HOOK is defined, user |
| 758 | // program must provide implementation of this hook. |
| 759 | // If macro is undefined, the hook is no-op. |
| 760 | #ifdef ASAN_NEW_HOOK |
| 761 | extern "C" void ASAN_NEW_HOOK(void *ptr, size_t size); |
| 762 | #else |
| 763 | static inline void ASAN_NEW_HOOK(void *ptr, size_t size) { } |
| 764 | #endif |
| 765 | |
| 766 | #ifdef ASAN_DELETE_HOOK |
| 767 | extern "C" void ASAN_DELETE_HOOK(void *ptr); |
| 768 | #else |
| 769 | static inline void ASAN_DELETE_HOOK(void *ptr) { } |
| 770 | #endif |
| 771 | |
| 772 | namespace __asan { |
| 773 | |
| 774 | void *asan_memalign(size_t alignment, size_t size, AsanStackTrace *stack) { |
| 775 | void *ptr = (void*)Allocate(alignment, size, stack); |
| 776 | ASAN_NEW_HOOK(ptr, size); |
| 777 | return ptr; |
| 778 | } |
| 779 | |
| 780 | void asan_free(void *ptr, AsanStackTrace *stack) { |
| 781 | ASAN_DELETE_HOOK(ptr); |
| 782 | Deallocate((uint8_t*)ptr, stack); |
| 783 | } |
| 784 | |
| 785 | void *asan_malloc(size_t size, AsanStackTrace *stack) { |
| 786 | void *ptr = (void*)Allocate(0, size, stack); |
| 787 | ASAN_NEW_HOOK(ptr, size); |
| 788 | return ptr; |
| 789 | } |
| 790 | |
| 791 | void *asan_calloc(size_t nmemb, size_t size, AsanStackTrace *stack) { |
| 792 | void *ptr = (void*)Allocate(0, nmemb * size, stack); |
| 793 | if (ptr) |
| 794 | real_memset(ptr, 0, nmemb * size); |
| 795 | ASAN_NEW_HOOK(ptr, nmemb * size); |
| 796 | return ptr; |
| 797 | } |
| 798 | |
| 799 | void *asan_realloc(void *p, size_t size, AsanStackTrace *stack) { |
| 800 | if (p == NULL) { |
| 801 | void *ptr = (void*)Allocate(0, size, stack); |
| 802 | ASAN_NEW_HOOK(ptr, size); |
| 803 | return ptr; |
| 804 | } else if (size == 0) { |
| 805 | ASAN_DELETE_HOOK(p); |
| 806 | Deallocate((uint8_t*)p, stack); |
| 807 | return NULL; |
| 808 | } |
| 809 | return Reallocate((uint8_t*)p, size, stack); |
| 810 | } |
| 811 | |
| 812 | void *asan_valloc(size_t size, AsanStackTrace *stack) { |
| 813 | void *ptr = (void*)Allocate(kPageSize, size, stack); |
| 814 | ASAN_NEW_HOOK(ptr, size); |
| 815 | return ptr; |
| 816 | } |
| 817 | |
| 818 | void *asan_pvalloc(size_t size, AsanStackTrace *stack) { |
| 819 | size = RoundUpTo(size, kPageSize); |
| 820 | if (size == 0) { |
| 821 | // pvalloc(0) should allocate one page. |
| 822 | size = kPageSize; |
| 823 | } |
| 824 | void *ptr = (void*)Allocate(kPageSize, size, stack); |
| 825 | ASAN_NEW_HOOK(ptr, size); |
| 826 | return ptr; |
| 827 | } |
| 828 | |
| 829 | int asan_posix_memalign(void **memptr, size_t alignment, size_t size, |
| 830 | AsanStackTrace *stack) { |
| 831 | void *ptr = Allocate(alignment, size, stack); |
| 832 | CHECK(IsAligned((uintptr_t)ptr, alignment)); |
| 833 | ASAN_NEW_HOOK(ptr, size); |
| 834 | *memptr = ptr; |
| 835 | return 0; |
| 836 | } |
| 837 | |
| Alexey Samsonov | 4fd95f1 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 838 | size_t asan_malloc_usable_size(void *ptr, AsanStackTrace *stack) { |
| Alexey Samsonov | ca2278d | 2012-01-18 15:26:55 +0000 | [diff] [blame] | 839 | CHECK(stack); |
| 840 | if (ptr == NULL) return 0; |
| 841 | size_t usable_size = malloc_info.AllocationSize((uintptr_t)ptr); |
| 842 | if (usable_size == 0) { |
| Alexey Samsonov | 4fd95f1 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 843 | Report("ERROR: AddressSanitizer attempting to call malloc_usable_size() " |
| 844 | "for pointer which is not owned: %p\n", ptr); |
| 845 | stack->PrintStack(); |
| 846 | Describe((uintptr_t)ptr, 1); |
| 847 | ShowStatsAndAbort(); |
| 848 | } |
| 849 | return usable_size; |
| 850 | } |
| 851 | |
| 852 | size_t asan_mz_size(const void *ptr) { |
| Alexey Samsonov | ca2278d | 2012-01-18 15:26:55 +0000 | [diff] [blame] | 853 | return malloc_info.AllocationSize((uintptr_t)ptr); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | void DescribeHeapAddress(uintptr_t addr, uintptr_t access_size) { |
| 857 | Describe(addr, access_size); |
| 858 | } |
| 859 | |
| Alexey Samsonov | 4fd95f1 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 860 | void asan_mz_force_lock() { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 861 | malloc_info.ForceLock(); |
| 862 | } |
| 863 | |
| Alexey Samsonov | 4fd95f1 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 864 | void asan_mz_force_unlock() { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 865 | malloc_info.ForceUnlock(); |
| 866 | } |
| 867 | |
| 868 | // ---------------------- Fake stack-------------------- {{{1 |
| 869 | FakeStack::FakeStack() { |
| Alexey Samsonov | f927ddc | 2012-02-03 08:50:16 +0000 | [diff] [blame] | 870 | CHECK(real_memset != NULL); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 871 | real_memset(this, 0, sizeof(*this)); |
| 872 | } |
| 873 | |
| 874 | bool FakeStack::AddrIsInSizeClass(uintptr_t addr, size_t size_class) { |
| 875 | uintptr_t mem = allocated_size_classes_[size_class]; |
| 876 | uintptr_t size = ClassMmapSize(size_class); |
| 877 | bool res = mem && addr >= mem && addr < mem + size; |
| 878 | return res; |
| 879 | } |
| 880 | |
| 881 | uintptr_t FakeStack::AddrIsInFakeStack(uintptr_t addr) { |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 882 | for (size_t i = 0; i < kNumberOfSizeClasses; i++) { |
| 883 | if (AddrIsInSizeClass(addr, i)) return allocated_size_classes_[i]; |
| 884 | } |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | // We may want to compute this during compilation. |
| 889 | inline size_t FakeStack::ComputeSizeClass(size_t alloc_size) { |
| 890 | size_t rounded_size = RoundUpToPowerOfTwo(alloc_size); |
| 891 | size_t log = Log2(rounded_size); |
| 892 | CHECK(alloc_size <= (1UL << log)); |
| 893 | if (!(alloc_size > (1UL << (log-1)))) { |
| 894 | Printf("alloc_size %ld log %ld\n", alloc_size, log); |
| 895 | } |
| 896 | CHECK(alloc_size > (1UL << (log-1))); |
| 897 | size_t res = log < kMinStackFrameSizeLog ? 0 : log - kMinStackFrameSizeLog; |
| 898 | CHECK(res < kNumberOfSizeClasses); |
| 899 | CHECK(ClassSize(res) >= rounded_size); |
| 900 | return res; |
| 901 | } |
| 902 | |
| 903 | void FakeFrameFifo::FifoPush(FakeFrame *node) { |
| 904 | CHECK(node); |
| 905 | node->next = 0; |
| 906 | if (first_ == 0 && last_ == 0) { |
| 907 | first_ = last_ = node; |
| 908 | } else { |
| 909 | CHECK(first_); |
| 910 | CHECK(last_); |
| 911 | last_->next = node; |
| 912 | last_ = node; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | FakeFrame *FakeFrameFifo::FifoPop() { |
| 917 | CHECK(first_ && last_ && "Exhausted fake stack"); |
| 918 | FakeFrame *res = 0; |
| 919 | if (first_ == last_) { |
| 920 | res = first_; |
| 921 | first_ = last_ = 0; |
| 922 | } else { |
| 923 | res = first_; |
| 924 | first_ = first_->next; |
| 925 | } |
| 926 | return res; |
| 927 | } |
| 928 | |
| 929 | void FakeStack::Init(size_t stack_size) { |
| 930 | stack_size_ = stack_size; |
| 931 | alive_ = true; |
| 932 | } |
| 933 | |
| 934 | void FakeStack::Cleanup() { |
| 935 | alive_ = false; |
| 936 | for (size_t i = 0; i < kNumberOfSizeClasses; i++) { |
| 937 | uintptr_t mem = allocated_size_classes_[i]; |
| 938 | if (mem) { |
| 939 | PoisonShadow(mem, ClassMmapSize(i), 0); |
| 940 | allocated_size_classes_[i] = 0; |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 941 | AsanUnmapOrDie((void*)mem, ClassMmapSize(i)); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | size_t FakeStack::ClassMmapSize(size_t size_class) { |
| 947 | return RoundUpToPowerOfTwo(stack_size_); |
| 948 | } |
| 949 | |
| 950 | void FakeStack::AllocateOneSizeClass(size_t size_class) { |
| 951 | CHECK(ClassMmapSize(size_class) >= kPageSize); |
| Kostya Serebryany | de496f4 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 952 | uintptr_t new_mem = (uintptr_t)AsanMmapSomewhereOrDie( |
| 953 | ClassMmapSize(size_class), __FUNCTION__); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 954 | // Printf("T%d new_mem[%ld]: %p-%p mmap %ld\n", |
| 955 | // asanThreadRegistry().GetCurrent()->tid(), |
| 956 | // size_class, new_mem, new_mem + ClassMmapSize(size_class), |
| 957 | // ClassMmapSize(size_class)); |
| 958 | size_t i; |
| 959 | for (i = 0; i < ClassMmapSize(size_class); |
| 960 | i += ClassSize(size_class)) { |
| 961 | size_classes_[size_class].FifoPush((FakeFrame*)(new_mem + i)); |
| 962 | } |
| 963 | CHECK(i == ClassMmapSize(size_class)); |
| 964 | allocated_size_classes_[size_class] = new_mem; |
| 965 | } |
| 966 | |
| 967 | uintptr_t FakeStack::AllocateStack(size_t size, size_t real_stack) { |
| Kostya Serebryany | c4b34d9 | 2011-12-09 01:49:31 +0000 | [diff] [blame] | 968 | if (!alive_) return real_stack; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 969 | CHECK(size <= kMaxStackMallocSize && size > 1); |
| 970 | size_t size_class = ComputeSizeClass(size); |
| 971 | if (!allocated_size_classes_[size_class]) { |
| 972 | AllocateOneSizeClass(size_class); |
| 973 | } |
| 974 | FakeFrame *fake_frame = size_classes_[size_class].FifoPop(); |
| 975 | CHECK(fake_frame); |
| 976 | fake_frame->size_minus_one = size - 1; |
| 977 | fake_frame->real_stack = real_stack; |
| 978 | while (FakeFrame *top = call_stack_.top()) { |
| 979 | if (top->real_stack > real_stack) break; |
| 980 | call_stack_.LifoPop(); |
| 981 | DeallocateFrame(top); |
| 982 | } |
| 983 | call_stack_.LifoPush(fake_frame); |
| 984 | uintptr_t ptr = (uintptr_t)fake_frame; |
| 985 | PoisonShadow(ptr, size, 0); |
| 986 | return ptr; |
| 987 | } |
| 988 | |
| 989 | void FakeStack::DeallocateFrame(FakeFrame *fake_frame) { |
| 990 | CHECK(alive_); |
| 991 | size_t size = fake_frame->size_minus_one + 1; |
| 992 | size_t size_class = ComputeSizeClass(size); |
| 993 | CHECK(allocated_size_classes_[size_class]); |
| 994 | uintptr_t ptr = (uintptr_t)fake_frame; |
| 995 | CHECK(AddrIsInSizeClass(ptr, size_class)); |
| 996 | CHECK(AddrIsInSizeClass(ptr + size - 1, size_class)); |
| 997 | size_classes_[size_class].FifoPush(fake_frame); |
| 998 | } |
| 999 | |
| 1000 | void FakeStack::OnFree(size_t ptr, size_t size, size_t real_stack) { |
| 1001 | FakeFrame *fake_frame = (FakeFrame*)ptr; |
| 1002 | CHECK(fake_frame->magic = kRetiredStackFrameMagic); |
| 1003 | CHECK(fake_frame->descr != 0); |
| 1004 | CHECK(fake_frame->size_minus_one == size - 1); |
| 1005 | PoisonShadow(ptr, size, kAsanStackAfterReturnMagic); |
| 1006 | } |
| 1007 | |
| 1008 | } // namespace __asan |
| 1009 | |
| 1010 | // ---------------------- Interface ---------------- {{{1 |
| 1011 | using namespace __asan; // NOLINT |
| 1012 | |
| 1013 | size_t __asan_stack_malloc(size_t size, size_t real_stack) { |
| 1014 | if (!FLAG_use_fake_stack) return real_stack; |
| 1015 | AsanThread *t = asanThreadRegistry().GetCurrent(); |
| 1016 | if (!t) { |
| 1017 | // TSD is gone, use the real stack. |
| 1018 | return real_stack; |
| 1019 | } |
| 1020 | size_t ptr = t->fake_stack().AllocateStack(size, real_stack); |
| 1021 | // Printf("__asan_stack_malloc %p %ld %p\n", ptr, size, real_stack); |
| 1022 | return ptr; |
| 1023 | } |
| 1024 | |
| 1025 | void __asan_stack_free(size_t ptr, size_t size, size_t real_stack) { |
| 1026 | if (!FLAG_use_fake_stack) return; |
| 1027 | if (ptr != real_stack) { |
| 1028 | FakeStack::OnFree(ptr, size, real_stack); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | // ASan allocator doesn't reserve extra bytes, so normally we would |
| 1033 | // just return "size". |
| 1034 | size_t __asan_get_estimated_allocated_size(size_t size) { |
| 1035 | if (size == 0) return 1; |
| Kostya Serebryany | 2d8b3bd | 2011-12-02 18:42:04 +0000 | [diff] [blame] | 1036 | return Min(size, kMaxAllowedMallocSize); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | bool __asan_get_ownership(const void *p) { |
| Alexey Samsonov | ca2278d | 2012-01-18 15:26:55 +0000 | [diff] [blame] | 1040 | return malloc_info.AllocationSize((uintptr_t)p) > 0; |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | size_t __asan_get_allocated_size(const void *p) { |
| Alexey Samsonov | ca2278d | 2012-01-18 15:26:55 +0000 | [diff] [blame] | 1044 | if (p == NULL) return 0; |
| 1045 | size_t allocated_size = malloc_info.AllocationSize((uintptr_t)p); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1046 | // Die if p is not malloced or if it is already freed. |
| Alexey Samsonov | ca2278d | 2012-01-18 15:26:55 +0000 | [diff] [blame] | 1047 | if (allocated_size == 0) { |
| Alexey Samsonov | 4fd95f1 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 1048 | Report("ERROR: AddressSanitizer attempting to call " |
| 1049 | "__asan_get_allocated_size() for pointer which is " |
| 1050 | "not owned: %p\n", p); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1051 | PRINT_CURRENT_STACK(); |
| Alexey Samsonov | 4fd95f1 | 2012-01-17 06:39:10 +0000 | [diff] [blame] | 1052 | Describe((uintptr_t)p, 1); |
| Kostya Serebryany | 1e172b4 | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1053 | ShowStatsAndAbort(); |
| 1054 | } |
| 1055 | return allocated_size; |
| 1056 | } |