Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // Test that LargeAllocator unpoisons memory before releasing it to the OS. |
| 2 | // RUN: %clangxx_asan %s -o %t |
| 3 | // The memory is released only when the deallocated chunk leaves the quarantine, |
| 4 | // otherwise the mmap(p, ...) call overwrites the malloc header. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 5 | // RUN: %env_asan_opts=quarantine_size_mb=0 %run %t |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 6 | |
| 7 | #include <assert.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/mman.h> |
| 10 | #include <stdlib.h> |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 11 | #include <unistd.h> |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 12 | |
| 13 | #ifdef __ANDROID__ |
| 14 | #include <malloc.h> |
| 15 | void *my_memalign(size_t boundary, size_t size) { |
| 16 | return memalign(boundary, size); |
| 17 | } |
| 18 | #else |
| 19 | void *my_memalign(size_t boundary, size_t size) { |
| 20 | void *p; |
| 21 | posix_memalign(&p, boundary, size); |
| 22 | return p; |
| 23 | } |
| 24 | #endif |
| 25 | |
| 26 | int main() { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 27 | const long kPageSize = sysconf(_SC_PAGESIZE); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 28 | void *p = my_memalign(kPageSize, 1024 * 1024); |
| 29 | free(p); |
| 30 | |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 31 | char *q = (char *)mmap(p, kPageSize, PROT_READ | PROT_WRITE, |
| 32 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 33 | assert(q == p); |
| 34 | |
| 35 | memset(q, 42, kPageSize); |
| 36 | |
| 37 | munmap(q, kPageSize); |
| 38 | return 0; |
| 39 | } |