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: LSAN_OPTIONS=$LSAN_BASE:"use_registers=0" not %run %t 2>&1 | FileCheck %s
5 // RUN: LSAN_OPTIONS=$LSAN_BASE:"use_registers=1" %run %t 2>&1
6 // RUN: LSAN_OPTIONS="" %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
14 extern "C"
registers_thread_func(void * arg)15 void *registers_thread_func(void *arg) {
16 int *sync = reinterpret_cast<int *>(arg);
17 void *p = malloc(1337);
18 // To store the pointer, choose a register which is unlikely to be reused by
19 // a function call.
20 #if defined(__i386__)
21 asm ( "mov %0, %%esi"
22 :
23 : "r" (p)
24 );
25 #elif defined(__x86_64__)
26 asm ( "mov %0, %%r15"
27 :
28 : "r" (p)
29 );
30 #else
31 #error "Test is not supported on this architecture."
32 #endif
33 fprintf(stderr, "Test alloc: %p.\n", p);
34 fflush(stderr);
35 __sync_fetch_and_xor(sync, 1);
36 while (true)
37 sched_yield();
38 }
39
main()40 int main() {
41 int sync = 0;
42 pthread_t thread_id;
43 int res = pthread_create(&thread_id, 0, registers_thread_func, &sync);
44 assert(res == 0);
45 while (!__sync_fetch_and_xor(&sync, 0))
46 sched_yield();
47 return 0;
48 }
49 // CHECK: Test alloc: [[ADDR:.*]].
50 // CHECK: LeakSanitizer: detected memory leaks
51 // CHECK: [[ADDR]] (1337 bytes)
52 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
53