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 <sched.h>
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 
     14 extern "C"
     15 void *registers_thread_func(void *arg) {
     16   int *sync = reinterpret_cast<int *>(arg);
     17   void *p = malloc(1337);
     18   // To store the pointer, choose a register which is unlikely to be reused by
     19   // a function call.
     20 #if defined(__i386__)
     21   asm ( "mov %0, %%esi"
     22       :
     23       : "r" (p)
     24       );
     25 #elif defined(__x86_64__)
     26   asm ( "mov %0, %%r15"
     27       :
     28       : "r" (p)
     29       );
     30 #elif defined(__mips__)
     31   asm ( "move $16, %0"
     32       :
     33       : "r" (p)
     34       );
     35 #else
     36 #error "Test is not supported on this architecture."
     37 #endif
     38   fprintf(stderr, "Test alloc: %p.\n", p);
     39   fflush(stderr);
     40   __sync_fetch_and_xor(sync, 1);
     41   while (true)
     42     sched_yield();
     43 }
     44 
     45 int main() {
     46   int sync = 0;
     47   pthread_t thread_id;
     48   int res = pthread_create(&thread_id, 0, registers_thread_func, &sync);
     49   assert(res == 0);
     50   while (!__sync_fetch_and_xor(&sync, 0))
     51     sched_yield();
     52   return 0;
     53 }
     54 // CHECK: Test alloc: [[ADDR:.*]].
     55 // CHECK: LeakSanitizer: detected memory leaks
     56 // CHECK: [[ADDR]] (1337 bytes)
     57 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
     58