blob: b39fa74a31499da514336083b7ab1138976da9a2 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// 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 Nainar799172d2016-03-03 15:50:30 -08005// RUN: %env_asan_opts=quarantine_size_mb=0 %run %t
Stephen Hines2d1fdb22014-05-28 23:58:16 -07006
7#include <assert.h>
8#include <string.h>
9#include <sys/mman.h>
10#include <stdlib.h>
Stephen Hines6d186232014-11-26 17:56:19 -080011#include <unistd.h>
Stephen Hines2d1fdb22014-05-28 23:58:16 -070012
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(&p, boundary, size);
22 return p;
23}
24#endif
25
26int main() {
Stephen Hines6d186232014-11-26 17:56:19 -080027 const long kPageSize = sysconf(_SC_PAGESIZE);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070028 void *p = my_memalign(kPageSize, 1024 * 1024);
29 free(p);
30
Stephen Hines6d186232014-11-26 17:56:19 -080031 char *q = (char *)mmap(p, kPageSize, PROT_READ | PROT_WRITE,
32 MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070033 assert(q == p);
34
35 memset(q, 42, kPageSize);
36
37 munmap(q, kPageSize);
38 return 0;
39}