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