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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of compiler-rt/test/lsan/TestCases/use_stacks_threaded.cpp