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.
5// RUN: %env_asan_opts=quarantine_size_mb=0 %run %t
6
7#include <assert.h>
8#include <string.h>
9#include <sys/mman.h>
10#include <stdlib.h>
11#include <unistd.h>
12
13#ifdef __ANDROID__
14#include <malloc.h>
15void *my_memalign(size_t boundary, size_t size) {
16 return memalign(boundary, size);
17}
18#else
19void *my_memalign(size_t boundary, size_t size) {
20 void *p;
21 posix_memalign(memptr: &p, alignment: boundary, size: size);
22 return p;
23}
24#endif
25
26int main() {
27 const long kPageSize = sysconf(_SC_PAGESIZE);
28 void *p = my_memalign(boundary: kPageSize, size: 1024 * 1024);
29 free(ptr: p);
30
31 char *q = (char *)mmap(addr: p, len: kPageSize, PROT_READ | PROT_WRITE,
32 MAP_PRIVATE | MAP_ANON | MAP_FIXED, fd: -1, offset: 0);
33 assert(q == p);
34
35 memset(s: q, c: 42, n: kPageSize);
36
37 munmap(addr: q, len: kPageSize);
38 return 0;
39}
40

source code of compiler-rt/test/asan/TestCases/Posix/large_allocator_unpoisons_on_free.cpp