brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · 0a423df Raw
61 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// FIXME: Support more platforms.8// REQUIRES: x86-target-arch && linux9 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 17extern "C" void *registers_thread_func(void *arg) {18  int *sync = reinterpret_cast<int *>(arg);19  void *p = malloc(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 by24  // a function call.25#if defined(__i386__)26  asm(R"(27    movd %0, %%xmm028    mov $0, %029  )"30      :31      : "r"(p));32#elif defined(__x86_64__)33  asm(R"(34    movq %0, %%xmm035    mov $0, %036  )"37      :38      : "r"(p));39#else40#error "Test is not supported on this architecture."41#endif42 43  __sync_fetch_and_xor(sync, 1);44  while (true)45    sched_yield();46}47 48int main() {49  int sync = 0;50  pthread_t thread_id;51  int res = pthread_create(&thread_id, 0, registers_thread_func, &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 leaks59// CHECK: [[ADDR]] (1337 bytes)60// CHECK: SUMMARY: {{.*}}Sanitizer:61