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