xref: /llvm-project/compiler-rt/test/asan/TestCases/Posix/large_allocator_unpoisons_on_free.cpp (revision 673dc3d4a0b0fbb3b9b34ae2ecbfa522627fe582)
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>
my_memalign(size_t boundary,size_t size)15 void *my_memalign(size_t boundary, size_t size) {
16   return memalign(boundary, size);
17 }
18 #else
my_memalign(size_t boundary,size_t size)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 
main()26 int main() {
27   const long kPageSize = sysconf(_SC_PAGESIZE);
28   void *p = my_memalign(kPageSize, 1024 * 1024);
29   free(p);
30 
31   char *q = (char *)mmap(p, kPageSize, PROT_READ | PROT_WRITE,
32                          MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
33   assert(q == p);
34 
35   memset(q, 42, kPageSize);
36 
37   munmap(q, kPageSize);
38   return 0;
39 }
40