[compiler-rt] r334036 - [lsan] Do not check for leaks in the forked process
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 5 11:15:57 PDT 2018
Author: vitalybuka
Date: Tue Jun 5 11:15:57 2018
New Revision: 334036
URL: http://llvm.org/viewvc/llvm-project?rev=334036&view=rev
Log:
[lsan] Do not check for leaks in the forked process
Summary:
If calling process had threads then forked process will fail to detect
references from them.
Fixes https://github.com/google/sanitizers/issues/836
Reviewers: alekseyshl
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D47751
Added:
compiler-rt/trunk/test/lsan/TestCases/Linux/fork_with_threads.cc
Modified:
compiler-rt/trunk/lib/lsan/lsan_common.cc
Modified: compiler-rt/trunk/lib/lsan/lsan_common.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common.cc?rev=334036&r1=334035&r2=334036&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common.cc Tue Jun 5 11:15:57 2018
@@ -100,6 +100,8 @@ static SuppressionContext *GetSuppressio
static InternalMmapVector<RootRegion> *root_regions;
+static uptr initialized_for_pid;
+
InternalMmapVector<RootRegion> const *GetRootRegions() { return root_regions; }
void InitializeRootRegions() {
@@ -113,6 +115,7 @@ const char *MaybeCallLsanDefaultOptions(
}
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,12 @@ static void CheckForLeaksCallback(const
static bool CheckForLeaks() {
if (&__lsan_is_turned_off && __lsan_is_turned_off())
return false;
+ if (initialized_for_pid != internal_getpid()) {
+ // If process was forked and it had threads we fail to detect references
+ // from other threads.
+ Report("WARNING: LeakSanitizer is disabled in forked process.\n");
+ return false;
+ }
EnsureMainThreadIDIsCorrect();
CheckForLeaksParam param;
param.success = false;
Added: compiler-rt/trunk/test/lsan/TestCases/Linux/fork_with_threads.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/lsan/TestCases/Linux/fork_with_threads.cc?rev=334036&view=auto
==============================================================================
--- compiler-rt/trunk/test/lsan/TestCases/Linux/fork_with_threads.cc (added)
+++ compiler-rt/trunk/test/lsan/TestCases/Linux/fork_with_threads.cc Tue Jun 5 11:15:57 2018
@@ -0,0 +1,35 @@
+// Test forked process does not run lsan.
+// RUN: %clangxx_lsan %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <pthread.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+static pthread_barrier_t barrier;
+
+// CHECK-NOT: SUMMARY: {{(Leak|Address)}}Sanitizer:
+static 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);
+ pthread_barrier_destroy(&barrier);
+
+ pid_t pid = fork();
+ if (pid > 0) {
+ int status = 0;
+ waitpid(pid, &status, 0);
+ }
+ return 0;
+}
+
+// CHECK: WARNING: LeakSanitizer is disabled in forked process
More information about the llvm-commits
mailing list