[PATCH] D47751: [lsan] Do not check for leaks in the forked process
Vitaly Buka via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 4 18:13:05 PDT 2018
vitalybuka created this revision.
vitalybuka added a reviewer: alekseyshl.
If calling process had threads forked process will fail to detect references
from them.
Fixes https://github.com/google/sanitizers/issues/836
Repository:
rL LLVM
https://reviews.llvm.org/D47751
Files:
compiler-rt/lib/lsan/lsan_common.cc
compiler-rt/test/lsan/TestCases/Linux/fork_with_threads.cc
Index: compiler-rt/test/lsan/TestCases/Linux/fork_with_threads.cc
===================================================================
--- /dev/null
+++ compiler-rt/test/lsan/TestCases/Linux/fork_with_threads.cc
@@ -0,0 +1,32 @@
+// Test forked thread does not run lsan.
+// RUN: %clangxx_lsan %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static pthread_barrier_t barrier;
+
+// CHECK-NOT: SUMMARY: {{(Leak|Address)}}Sanitizer:
+void *thread_func(void *arg) {
+ void *buffer = malloc(1337);
+ pthread_barrier_wait(&barrier);
+ for (;;)
+ pthread_yield();
+ return 0;
+}
+
+int main() {
+ pthread_barrier_init(&barrier, 0, 2);
+ pthread_t tid;
+ int res = pthread_create(&tid, 0, thread_func, 0);
+ pthread_barrier_wait(&barrier);
+ pid_t pid = fork();
+ pthread_barrier_destroy(&barrier);
+ printf("OK\n");
+ return 0;
+}
+// CHECK: OK
Index: compiler-rt/lib/lsan/lsan_common.cc
===================================================================
--- compiler-rt/lib/lsan/lsan_common.cc
+++ compiler-rt/lib/lsan/lsan_common.cc
@@ -100,6 +100,8 @@
static InternalMmapVector<RootRegion> *root_regions;
+static uptr initialized_for_pid;
+
InternalMmapVector<RootRegion> const *GetRootRegions() { return root_regions; }
void InitializeRootRegions() {
@@ -113,6 +115,7 @@
}
void InitCommonLsan() {
+ initialized_for_pid = internal_getpid();
InitializeRootRegions();
if (common_flags()->detect_leaks) {
// Initialization which can fail or print warnings should only be done if
@@ -568,6 +571,9 @@
static bool CheckForLeaks() {
if (&__lsan_is_turned_off && __lsan_is_turned_off())
return false;
+ // If process was forked and it had threads we do meaningfull leak checks.
+ if (initialized_for_pid != internal_getpid())
+ return false;
EnsureMainThreadIDIsCorrect();
CheckForLeaksParam param;
param.success = false;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47751.149885.patch
Type: text/x-patch
Size: 2001 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180605/924fb08c/attachment.bin>
More information about the llvm-commits
mailing list