• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Test that stacks of non-main threads are included in the root set.
2 // RUN: LSAN_BASE="report_objects=1:use_registers=0"
3 // RUN: %clangxx_lsan -pthread %s -o %t
4 // RUN: %env_lsan_opts=$LSAN_BASE:"use_stacks=0" not %run %t 2>&1 | FileCheck %s
5 // RUN: %env_lsan_opts=$LSAN_BASE:"use_stacks=1" %run %t 2>&1
6 // RUN: %env_lsan_opts="" %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 #include "sanitizer_common/print_address.h"
14 
15 extern "C"
stacks_thread_func(void * arg)16 void *stacks_thread_func(void *arg) {
17   int *sync = reinterpret_cast<int *>(arg);
18   void *p = malloc(1337);
19   print_address("Test alloc: ", 1, p);
20   fflush(stderr);
21   __sync_fetch_and_xor(sync, 1);
22   while (true)
23     sched_yield();
24 }
25 
main()26 int main() {
27   int sync = 0;
28   pthread_t thread_id;
29   int res = pthread_create(&thread_id, 0, stacks_thread_func, &sync);
30   assert(res == 0);
31   while (!__sync_fetch_and_xor(&sync, 0))
32     sched_yield();
33   return 0;
34 }
35 // CHECK: Test alloc: [[ADDR:0x[0-9,a-f]+]]
36 // CHECK: LeakSanitizer: detected memory leaks
37 // CHECK: [[ADDR]] (1337 bytes)
38 // CHECK: SUMMARY: {{(Leak|Address)}}Sanitizer:
39