xref: /llvm-project/compiler-rt/test/lsan/TestCases/use_registers.cpp (revision 6b67f79f5f851bfcd5a5861910aedc9e31f377b9)
1 // Test that registers of running threads are included in the root set.
2 // RUN: %clangxx_lsan -pthread %s -o %t
3 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0" not %run %t 2>&1 | FileCheck %s
4 // RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=1" %run %t 2>&1
5 // RUN: %env_lsan_opts="" %run %t 2>&1
6 
7 #include "sanitizer_common/print_address.h"
8 #include <assert.h>
9 #include <pthread.h>
10 #include <sched.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 int sync = 0;
15 
16 extern "C" void *registers_thread_func(void *) {
17   void *p = malloc(1337);
18   print_address("Test alloc: ", 1, p);
19   fflush(stderr);
20 
21   while(true) {
22   // To store the pointer, choose a register which is unlikely to be reused by
23   // a function call.
24 #if defined(__i386__) || defined(__i686__)
25   asm("mov %0, %%edi"
26       :
27       : "r"(p));
28 #elif defined(__x86_64__)
29   asm("mov %0, %%r15"
30       :
31       : "r"(p));
32 #elif defined(__mips__)
33   asm("move $16, %0"
34       :
35       : "r"(p));
36 #elif defined(__arm__)
37   asm("mov r5, %0"
38       :
39       : "r"(p));
40 #elif defined(__aarch64__)
41   // x9-10 are used. x11-12 are probably used.
42   // So we pick x13 to be safe and x14 as a backup.
43   // (x13 known to be used on Ubuntu Focal)
44   asm("mov x13, %0\n"
45       "mov x14, %0"
46       :
47       : "r"(p));
48 #elif defined(__loongarch_lp64)
49   asm("move $s8, %0" : : "r"(p));
50 #elif defined(__powerpc__)
51   asm("mr 30, %0"
52       :
53       : "r"(p));
54 #elif defined(__s390x__)
55   asm("lgr %%r10, %0"
56       :
57       : "r"(p));
58 #elif defined(__riscv)
59   asm("mv s11, %0"
60       :
61       : "r"(p));
62 #else
63 #error "Test is not supported on this architecture."
64 #endif
65   __sync_fetch_and_xor(&sync, 1);
66   }
67 }
68 
69 int main() {
70   pthread_t thread_id;
71   int res = pthread_create(&thread_id, 0, registers_thread_func, nullptr);
72   assert(res == 0);
73   while (!__sync_fetch_and_xor(&sync, 0))
74     sched_yield();
75   return 0;
76 }
77 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
78 // CHECK: LeakSanitizer: detected memory leaks
79 // CHECK: [[ADDR]] (1337 bytes)
80 // CHECK: SUMMARY: {{.*}}Sanitizer:
81