1 // Test for __lsan_(un)register_root_region().
2 // RUN: LSAN_BASE="use_stacks=0:use_registers=0"
3 // RUN: %clangxx_lsan %s -o %t
4 // RUN: LSAN_OPTIONS=$LSAN_BASE %run %t
5 // RUN: LSAN_OPTIONS=$LSAN_BASE not %run %t foo 2>&1 | FileCheck %s
6 // RUN: LSAN_OPTIONS=$LSAN_BASE:use_root_regions=0 not %run %t 2>&1 | FileCheck %s
7 
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 
14 #include <sanitizer/lsan_interface.h>
15 
main(int argc,char * argv[])16 int main(int argc, char *argv[]) {
17   size_t size = getpagesize() * 2;
18   void *p =
19       mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
20   assert(p);
21   // Make half of the memory inaccessible. LSan must not crash trying to read it.
22   assert(0 == mprotect((char *)p + size / 2, size / 2, PROT_NONE));
23 
24   __lsan_register_root_region(p, size);
25   *((void **)p) = malloc(1337);
26   fprintf(stderr, "Test alloc: %p.\n", p);
27   if (argc > 1)
28     __lsan_unregister_root_region(p, size);
29   return 0;
30 }
31 // CHECK: Test alloc: [[ADDR:.*]].
32 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: 1337 byte(s) leaked in 1 allocation(s)
33