Skip to content

Commit

Permalink
[test][lsan] For thread args/result leak
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalybuka committed May 5, 2023
1 parent 81f9c43 commit 43de9cb
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions compiler-rt/test/lsan/TestCases/create_thread_leak.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Check that sanitizer does not leak args when passing to a child thread.

// RUN: %clangxx_lsan -pthread %s -o %t -DLEAK_ARG && %run not %t 10 2>&1 | FileCheck %s --check-prefixes=LEAK,LEAK123
// RUN: %clangxx_lsan -pthread %s -o %t -DLEAK_RES && %run not %t 10 2>&1 | FileCheck %s --check-prefixes=LEAK,LEAK234
// RUN: %clangxx_lsan -pthread %s -o %t -DLEAK_DETACH && %run not %t 10 2>&1 | FileCheck %s --check-prefixes=LEAK,LEAK234

// FIXME: Remove "not". There is no leak.
// False LEAK123 is broken for HWASAN.
// False LEAK234 is broken for ASAN, HWASAN, LSAN.
// RUN: %clangxx_lsan -pthread %s -o %t && %run not %t 10

#include <pthread.h>
#include <stdlib.h>
#include <vector>

#include <sanitizer/lsan_interface.h>

static void *thread_free(void *args) {
#ifndef LEAK_ARG
free(args);
#endif
return malloc(234);
}

int main(int argc, char **argv) {
int n = atoi(argv[1]);
for (int i = 0; i < n; ++i) {
std::vector<pthread_t> threads(10);

for (auto &thread : threads) {
pthread_create(&thread, 0, thread_free, malloc(123));
if (__lsan_do_recoverable_leak_check())
return 1;
}

for (auto &thread : threads) {
#ifdef LEAK_DETACH
pthread_detach(thread);
continue;
#endif
void *retval = 0;
pthread_join(thread, &retval);
#ifndef LEAK_RES
free(retval);
#endif
}
}
return 0;
}

// LEAK: LeakSanitizer: detected memory leaks
// LEAK123: in main
// LEAK234: in thread_free

0 comments on commit 43de9cb

Please sign in to comment.