From 93c1b73a09a52d4a265f683bf1954b08bb430049 Mon Sep 17 00:00:00 2001
From: Dimitry Andric <dim@FreeBSD.org>
Date: Sat, 28 Jul 2018 11:06:48 +0000
Subject: Vendor import of compiler-rt trunk r338150:
 https://llvm.org/svn/llvm-project/compiler-rt/trunk@338150

---
 test/lsan/TestCases/Linux/fork_with_threads.cc |  35 +++++++
 test/lsan/TestCases/Linux/log-path_test.cc     |  26 +++++
 test/lsan/TestCases/Linux/use_tls_dynamic.cc   |   2 +-
 test/lsan/TestCases/Posix/lit.local.cfg        |   9 ++
 test/lsan/TestCases/allocator_returns_null.cc  | 131 -------------------------
 5 files changed, 71 insertions(+), 132 deletions(-)
 create mode 100644 test/lsan/TestCases/Linux/fork_with_threads.cc
 create mode 100644 test/lsan/TestCases/Linux/log-path_test.cc
 create mode 100644 test/lsan/TestCases/Posix/lit.local.cfg
 delete mode 100644 test/lsan/TestCases/allocator_returns_null.cc

(limited to 'test/lsan')

diff --git a/test/lsan/TestCases/Linux/fork_with_threads.cc b/test/lsan/TestCases/Linux/fork_with_threads.cc
new file mode 100644
index 000000000000..221c5d249d77
--- /dev/null
+++ b/test/lsan/TestCases/Linux/fork_with_threads.cc
@@ -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
diff --git a/test/lsan/TestCases/Linux/log-path_test.cc b/test/lsan/TestCases/Linux/log-path_test.cc
new file mode 100644
index 000000000000..a31b4f64acc5
--- /dev/null
+++ b/test/lsan/TestCases/Linux/log-path_test.cc
@@ -0,0 +1,26 @@
+// RUN: %clangxx_lsan %s -o %t
+// The globs below do not work in the lit shell.
+
+// Regular run.
+// RUN: %env_lsan_opts="use_stacks=0" not %run %t > %t.out 2>&1
+// RUN: FileCheck %s --check-prefix=CHECK-ERROR < %t.out
+
+// Good log_path.
+// RUN: rm -f %t.log.*
+// RUN: %env_lsan_opts="use_stacks=0:log_path='"%t.log"'" not %run %t > %t.out 2>&1
+// RUN: FileCheck %s --check-prefix=CHECK-ERROR < %t.log.*
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "sanitizer_common/print_address.h"
+
+int main() {
+  void *stack_var = malloc(1337);
+  print_address("Test alloc: ", 1, stack_var);
+  // Do not return from main to prevent the pointer from going out of scope.
+  exit(0);
+}
+
+// CHECK-ERROR: LeakSanitizer: detected memory leaks
+// CHECK-ERROR: Direct leak of 1337 byte(s) in 1 object(s) allocated from
+// CHECK-ERROR: SUMMARY: {{(Leak|Address)}}Sanitizer:
diff --git a/test/lsan/TestCases/Linux/use_tls_dynamic.cc b/test/lsan/TestCases/Linux/use_tls_dynamic.cc
index f5df231ba9a6..4d70a46f8183 100644
--- a/test/lsan/TestCases/Linux/use_tls_dynamic.cc
+++ b/test/lsan/TestCases/Linux/use_tls_dynamic.cc
@@ -5,7 +5,7 @@
 // RUN: %env_lsan_opts=$LSAN_BASE:"use_tls=0" not %run %t 2>&1 | FileCheck %s
 // RUN: %env_lsan_opts=$LSAN_BASE:"use_tls=1" %run %t 2>&1
 // RUN: %env_lsan_opts="" %run %t 2>&1
-// UNSUPPORTED: i386-linux,arm
+// UNSUPPORTED: i386-linux,arm,powerpc
 
 #ifndef BUILD_DSO
 #include <assert.h>
diff --git a/test/lsan/TestCases/Posix/lit.local.cfg b/test/lsan/TestCases/Posix/lit.local.cfg
new file mode 100644
index 000000000000..60a9460820a6
--- /dev/null
+++ b/test/lsan/TestCases/Posix/lit.local.cfg
@@ -0,0 +1,9 @@
+def getRoot(config):
+  if not config.parent:
+    return config
+  return getRoot(config.parent)
+
+root = getRoot(config)
+
+if root.host_os in ['Windows']:
+  config.unsupported = True
diff --git a/test/lsan/TestCases/allocator_returns_null.cc b/test/lsan/TestCases/allocator_returns_null.cc
deleted file mode 100644
index 28dd696dc673..000000000000
--- a/test/lsan/TestCases/allocator_returns_null.cc
+++ /dev/null
@@ -1,131 +0,0 @@
-// Test the behavior of malloc/calloc/realloc/new when the allocation size is
-// more than LSan allocator's max allowed one.
-// By default (allocator_may_return_null=0) the process should crash.
-// With allocator_may_return_null=1 the allocator should return 0, except the
-// operator new(), which should crash anyway (operator new(std::nothrow) should
-// return nullptr, indeed).
-//
-// RUN: %clangxx_lsan -O0 %s -o %t
-// RUN: not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t malloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-mCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=1     %run %t malloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-mNULL
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t calloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-cCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=1     %run %t calloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-cNULL
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t calloc-overflow 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-coCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=1     %run %t calloc-overflow 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-coNULL
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t realloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-rCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=1     %run %t realloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-rNULL
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t realloc-after-malloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-mrCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=1     %run %t realloc-after-malloc 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-mrNULL
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t new 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-nCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=1 not %run %t new 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-nCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=0 not %run %t new-nothrow 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-nnCRASH
-// RUN: %env_lsan_opts=allocator_may_return_null=1     %run %t new-nothrow 2>&1 \
-// RUN:   | FileCheck %s --check-prefix=CHECK-nnNULL
-
-#include <assert.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <limits>
-#include <new>
-
-int main(int argc, char **argv) {
-  // Disable stderr buffering. Needed on Windows.
-  setvbuf(stderr, NULL, _IONBF, 0);
-
-  assert(argc == 2);
-  const char *action = argv[1];
-  fprintf(stderr, "%s:\n", action);
-
-  // Use max of ASan and LSan allocator limits to cover both "lsan" and
-  // "lsan + asan" configs.
-  static const size_t kMaxAllowedMallocSizePlusOne =
-#if __LP64__ || defined(_WIN64)
-      (1ULL << 40) + 1;
-#else
-      (3UL << 30) + 1;
-#endif
-
-  void *x = 0;
-  if (!strcmp(action, "malloc")) {
-    x = malloc(kMaxAllowedMallocSizePlusOne);
-  } else if (!strcmp(action, "calloc")) {
-    x = calloc((kMaxAllowedMallocSizePlusOne / 4) + 1, 4);
-  } else if (!strcmp(action, "calloc-overflow")) {
-    volatile size_t kMaxSizeT = std::numeric_limits<size_t>::max();
-    size_t kArraySize = 4096;
-    volatile size_t kArraySize2 = kMaxSizeT / kArraySize + 10;
-    x = calloc(kArraySize, kArraySize2);
-  } else if (!strcmp(action, "realloc")) {
-    x = realloc(0, kMaxAllowedMallocSizePlusOne);
-  } else if (!strcmp(action, "realloc-after-malloc")) {
-    char *t = (char*)malloc(100);
-    *t = 42;
-    x = realloc(t, kMaxAllowedMallocSizePlusOne);
-    assert(*t == 42);
-    free(t);
-  } else if (!strcmp(action, "new")) {
-    x = operator new(kMaxAllowedMallocSizePlusOne);
-  } else if (!strcmp(action, "new-nothrow")) {
-    x = operator new(kMaxAllowedMallocSizePlusOne, std::nothrow);
-  } else {
-    assert(0);
-  }
-
-  fprintf(stderr, "errno: %d\n", errno);
-
-  // The NULL pointer is printed differently on different systems, while (long)0
-  // is always the same.
-  fprintf(stderr, "x: %zu\n", (size_t)x);
-  free(x);
-
-  return x != 0;
-}
-
-// CHECK-mCRASH: malloc:
-// CHECK-mCRASH: Sanitizer's allocator is terminating the process
-// CHECK-cCRASH: calloc:
-// CHECK-cCRASH: Sanitizer's allocator is terminating the process
-// CHECK-coCRASH: calloc-overflow:
-// CHECK-coCRASH: Sanitizer's allocator is terminating the process
-// CHECK-rCRASH: realloc:
-// CHECK-rCRASH: Sanitizer's allocator is terminating the process
-// CHECK-mrCRASH: realloc-after-malloc:
-// CHECK-mrCRASH: Sanitizer's allocator is terminating the process
-// CHECK-nCRASH: new:
-// CHECK-nCRASH: Sanitizer's allocator is terminating the process
-// CHECK-nnCRASH: new-nothrow:
-// CHECK-nnCRASH: Sanitizer's allocator is terminating the process
-
-// CHECK-mNULL: malloc:
-// CHECK-mNULL: errno: 12
-// CHECK-mNULL: x: 0
-// CHECK-cNULL: calloc:
-// CHECK-cNULL: errno: 12
-// CHECK-cNULL: x: 0
-// CHECK-coNULL: calloc-overflow:
-// CHECK-coNULL: errno: 12
-// CHECK-coNULL: x: 0
-// CHECK-rNULL: realloc:
-// CHECK-rNULL: errno: 12
-// CHECK-rNULL: x: 0
-// CHECK-mrNULL: realloc-after-malloc:
-// CHECK-mrNULL: errno: 12
-// CHECK-mrNULL: x: 0
-// CHECK-nnNULL: new-nothrow:
-// CHECK-nnNULL: x: 0
-- 
cgit v1.2.3