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