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 <assert.h>
9 #include <pthread.h>
10 #include <sched.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include "sanitizer_common/print_address.h"
14 
15 extern "C"
16 void *registers_thread_func(void *arg) {
17   int *sync = reinterpret_cast<int *>(arg);
18   void *p = malloc(1337);
19   // To store the pointer, choose a register which is unlikely to be reused by
20   // a function call.
21 #if defined(__i386__)
22   asm ( "mov %0, %%esi"
23       :
24       : "r" (p)
25       );
26 #elif defined(__x86_64__)
27   asm ( "mov %0, %%r15"
28       :
29       : "r" (p)
30       );
31 #elif defined(__mips__)
32   asm ( "move $16, %0"
33       :
34       : "r" (p)
35       );
36 #elif defined(__arm__)
37   asm ( "mov r5, %0"
38       :
39       : "r" (p)
40       );
41 #elif defined(__powerpc__)
42   asm ( "mr 30, %0"
43       :
44       : "r" (p)
45       );
46 #else
47 #error "Test is not supported on this architecture."
48 #endif
49   print_address("Test alloc: ", 1, p);
50   fflush(stderr);
51   __sync_fetch_and_xor(sync, 1);
52   while (true)
53     sched_yield();
54 }
55 
56 int main() {
57   int sync = 0;
58   pthread_t thread_id;
59   int res = pthread_create(&thread_id, 0, registers_thread_func, &sync);
60   assert(res == 0);
61   while (!__sync_fetch_and_xor(&sync, 0))
62     sched_yield();
63   return 0;
64 }
65 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
66 // CHECK: LeakSanitizer: detected memory leaks
67 // CHECK: [[ADDR]] (1337 bytes)
68 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
69