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
14extern "C" void *registers_thread_func(void *arg) {
15 int *sync = reinterpret_cast<int *>(arg);
16 void *p = malloc(size: 1337);
17 print_address("Test alloc: ", 1, p);
18 fflush(stderr);
19
20 // To store the pointer, choose a register which is unlikely to be reused by
21 // a function call.
22#if defined(__i386__) || defined(__i686__)
23 asm("mov %0, %%edi"
24 :
25 : "r"(p));
26#elif defined(__x86_64__)
27 asm("mov %0, %%r15"
28 :
29 : "r"(p));
30#elif defined(__mips__)
31 asm("move $16, %0"
32 :
33 : "r"(p));
34#elif defined(__arm__)
35 asm("mov r5, %0"
36 :
37 : "r"(p));
38#elif defined(__aarch64__)
39 // x9-10 are used. x11-12 are probably used.
40 // So we pick x13 to be safe and x14 as a backup.
41 // (x13 known to be used on Ubuntu Focal)
42 asm("mov x13, %0\n"
43 "mov x14, %0"
44 :
45 : "r"(p));
46#elif defined(__loongarch_lp64)
47 asm("move $s8, %0" : : "r"(p));
48#elif defined(__powerpc__)
49 asm("mr 30, %0"
50 :
51 : "r"(p));
52#elif defined(__s390x__)
53 asm("lgr %%r10, %0"
54 :
55 : "r"(p));
56#elif defined(__riscv)
57 asm("mv s11, %0"
58 :
59 : "r"(p));
60#else
61#error "Test is not supported on this architecture."
62#endif
63 __sync_fetch_and_xor(sync, 1);
64 while (true)
65 sched_yield();
66}
67
68int main() {
69 int sync = 0;
70 pthread_t thread_id;
71 int res = pthread_create(newthread: &thread_id, attr: 0, start_routine: registers_thread_func, arg: &sync);
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

source code of compiler-rt/test/lsan/TestCases/use_registers.cpp