81 lines · cpp
1// Test that registers of running threads are included in the root set.2// RUN: %clangxx_lsan -pthread %s -o %t3// RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=0" not %run %t 2>&1 | FileCheck %s4// RUN: %env_lsan_opts="report_objects=1:use_stacks=0:use_registers=1" %run %t 2>&15// RUN: %env_lsan_opts="" %run %t 2>&16 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 14int sync = 0;15 16extern "C" void *registers_thread_func(void *) {17 void *p = malloc(1337);18 print_address("Test alloc: ", 1, p);19 fflush(stderr);20 21 while(true) {22 // To store the pointer, choose a register which is unlikely to be reused by23 // a function call.24#if defined(__i386__) || defined(__i686__)25 asm("mov %0, %%edi"26 :27 : "r"(p));28#elif defined(__x86_64__)29 asm("mov %0, %%r15"30 :31 : "r"(p));32#elif defined(__mips__)33 asm("move $16, %0"34 :35 : "r"(p));36#elif defined(__arm__)37 asm("mov r5, %0"38 :39 : "r"(p));40#elif defined(__aarch64__)41 // x9-10 are used. x11-12 are probably used.42 // So we pick x13 to be safe and x14 as a backup.43 // (x13 known to be used on Ubuntu Focal)44 asm("mov x13, %0\n"45 "mov x14, %0"46 :47 : "r"(p));48#elif defined(__loongarch_lp64)49 asm("move $s8, %0" : : "r"(p));50#elif defined(__powerpc__)51 asm("mr 30, %0"52 :53 : "r"(p));54#elif defined(__s390x__)55 asm("lgr %%r10, %0"56 :57 : "r"(p));58#elif defined(__riscv)59 asm("mv s11, %0"60 :61 : "r"(p));62#else63#error "Test is not supported on this architecture."64#endif65 __sync_fetch_and_xor(&sync, 1);66 }67}68 69int main() {70 pthread_t thread_id;71 int res = pthread_create(&thread_id, 0, registers_thread_func, nullptr);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 leaks79// CHECK: [[ADDR]] (1337 bytes)80// CHECK: SUMMARY: {{.*}}Sanitizer:81