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 14 extern "C" void *registers_thread_func(void *arg) { 15 int *sync = reinterpret_cast<int *>(arg); 16 void *p = malloc(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(__powerpc__) 47 asm("mr 30, %0" 48 : 49 : "r"(p)); 50 #elif defined(__s390x__) 51 asm("lgr %%r10, %0" 52 : 53 : "r"(p)); 54 #elif defined(__riscv) 55 asm("mv s11, %0" 56 : 57 : "r"(p)); 58 #else 59 #error "Test is not supported on this architecture." 60 #endif 61 __sync_fetch_and_xor(sync, 1); 62 while (true) 63 sched_yield(); 64 } 65 66 int main() { 67 int sync = 0; 68 pthread_t thread_id; 69 int res = pthread_create(&thread_id, 0, registers_thread_func, &sync); 70 assert(res == 0); 71 while (!__sync_fetch_and_xor(&sync, 0)) 72 sched_yield(); 73 return 0; 74 } 75 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]] 76 // CHECK: LeakSanitizer: detected memory leaks 77 // CHECK: [[ADDR]] (1337 bytes) 78 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer: 79