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