xref: /aosp_15_r20/external/compiler-rt/test/lsan/TestCases/use_registers.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Test that registers of running threads are included in the root set.
2*7c3d14c8STreehugger Robot // RUN: LSAN_BASE="report_objects=1:use_stacks=0"
3*7c3d14c8STreehugger Robot // RUN: %clangxx_lsan -pthread %s -o %t
4*7c3d14c8STreehugger Robot // RUN: LSAN_OPTIONS=$LSAN_BASE:"use_registers=0" not %run %t 2>&1 | FileCheck %s
5*7c3d14c8STreehugger Robot // RUN: LSAN_OPTIONS=$LSAN_BASE:"use_registers=1" %run %t 2>&1
6*7c3d14c8STreehugger Robot // RUN: LSAN_OPTIONS="" %run %t 2>&1
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot #include <assert.h>
9*7c3d14c8STreehugger Robot #include <pthread.h>
10*7c3d14c8STreehugger Robot #include <sched.h>
11*7c3d14c8STreehugger Robot #include <stdio.h>
12*7c3d14c8STreehugger Robot #include <stdlib.h>
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot extern "C"
registers_thread_func(void * arg)15*7c3d14c8STreehugger Robot void *registers_thread_func(void *arg) {
16*7c3d14c8STreehugger Robot   int *sync = reinterpret_cast<int *>(arg);
17*7c3d14c8STreehugger Robot   void *p = malloc(1337);
18*7c3d14c8STreehugger Robot   // To store the pointer, choose a register which is unlikely to be reused by
19*7c3d14c8STreehugger Robot   // a function call.
20*7c3d14c8STreehugger Robot #if defined(__i386__)
21*7c3d14c8STreehugger Robot   asm ( "mov %0, %%esi"
22*7c3d14c8STreehugger Robot       :
23*7c3d14c8STreehugger Robot       : "r" (p)
24*7c3d14c8STreehugger Robot       );
25*7c3d14c8STreehugger Robot #elif defined(__x86_64__)
26*7c3d14c8STreehugger Robot   asm ( "mov %0, %%r15"
27*7c3d14c8STreehugger Robot       :
28*7c3d14c8STreehugger Robot       : "r" (p)
29*7c3d14c8STreehugger Robot       );
30*7c3d14c8STreehugger Robot #elif defined(__mips__)
31*7c3d14c8STreehugger Robot   asm ( "move $16, %0"
32*7c3d14c8STreehugger Robot       :
33*7c3d14c8STreehugger Robot       : "r" (p)
34*7c3d14c8STreehugger Robot       );
35*7c3d14c8STreehugger Robot #else
36*7c3d14c8STreehugger Robot #error "Test is not supported on this architecture."
37*7c3d14c8STreehugger Robot #endif
38*7c3d14c8STreehugger Robot   fprintf(stderr, "Test alloc: %p.\n", p);
39*7c3d14c8STreehugger Robot   fflush(stderr);
40*7c3d14c8STreehugger Robot   __sync_fetch_and_xor(sync, 1);
41*7c3d14c8STreehugger Robot   while (true)
42*7c3d14c8STreehugger Robot     sched_yield();
43*7c3d14c8STreehugger Robot }
44*7c3d14c8STreehugger Robot 
main()45*7c3d14c8STreehugger Robot int main() {
46*7c3d14c8STreehugger Robot   int sync = 0;
47*7c3d14c8STreehugger Robot   pthread_t thread_id;
48*7c3d14c8STreehugger Robot   int res = pthread_create(&thread_id, 0, registers_thread_func, &sync);
49*7c3d14c8STreehugger Robot   assert(res == 0);
50*7c3d14c8STreehugger Robot   while (!__sync_fetch_and_xor(&sync, 0))
51*7c3d14c8STreehugger Robot     sched_yield();
52*7c3d14c8STreehugger Robot   return 0;
53*7c3d14c8STreehugger Robot }
54*7c3d14c8STreehugger Robot // CHECK: Test alloc: [[ADDR:.*]].
55*7c3d14c8STreehugger Robot // CHECK: LeakSanitizer: detected memory leaks
56*7c3d14c8STreehugger Robot // CHECK: [[ADDR]] (1337 bytes)
57*7c3d14c8STreehugger Robot // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
58