aboutsummaryrefslogtreecommitdiff
path: root/test/sanitizer_common
diff options
context:
space:
mode:
Diffstat (limited to 'test/sanitizer_common')
-rw-r--r--test/sanitizer_common/TestCases/Linux/hard_rss_limit_mb_test.cc37
-rw-r--r--test/sanitizer_common/TestCases/Linux/sanitizer_set_death_callback_test.cc49
-rw-r--r--test/sanitizer_common/TestCases/Linux/sched_getparam.cc13
-rw-r--r--test/sanitizer_common/TestCases/Linux/soft_rss_limit_mb_test.cc66
-rw-r--r--test/sanitizer_common/TestCases/options-help.cc8
-rw-r--r--test/sanitizer_common/TestCases/options-include.cc21
-rw-r--r--test/sanitizer_common/TestCases/options-invalid.cc15
7 files changed, 209 insertions, 0 deletions
diff --git a/test/sanitizer_common/TestCases/Linux/hard_rss_limit_mb_test.cc b/test/sanitizer_common/TestCases/Linux/hard_rss_limit_mb_test.cc
new file mode 100644
index 000000000000..225c44e25cd0
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Linux/hard_rss_limit_mb_test.cc
@@ -0,0 +1,37 @@
+// Check hard_rss_limit_mb. Not all sanitizers implement it yet.
+// RUN: %clangxx -O2 %s -o %t
+//
+// Run with limit should fail:
+// RUN: %tool_options=hard_rss_limit_mb=100 not %run %t 2>&1 | FileCheck %s
+// This run uses getrusage:
+// RUN: %tool_options=hard_rss_limit_mb=100:can_use_proc_maps_statm=0 not %run %t 2>&1 | FileCheck %s
+//
+// Run w/o limit or with a large enough limit should pass:
+// RUN: %tool_options=hard_rss_limit_mb=1000 %run %t
+// RUN: %run %t
+//
+// FIXME: make it work for other sanitizers.
+// XFAIL: lsan
+// XFAIL: tsan
+// XFAIL: msan
+
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+
+const int kNumAllocs = 200 * 1000;
+const int kAllocSize = 1000;
+volatile char *sink[kNumAllocs];
+
+int main(int argc, char **argv) {
+ for (int i = 0; i < kNumAllocs; i++) {
+ if ((i % 1000) == 0) {
+ fprintf(stderr, "[%d]\n", i);
+ }
+ char *x = new char[kAllocSize];
+ memset(x, 0, kAllocSize);
+ sink[i] = x;
+ }
+ sleep(1); // Make sure the background thread has time to kill the process.
+// CHECK: hard rss limit exhausted
+}
diff --git a/test/sanitizer_common/TestCases/Linux/sanitizer_set_death_callback_test.cc b/test/sanitizer_common/TestCases/Linux/sanitizer_set_death_callback_test.cc
new file mode 100644
index 000000000000..ee3d59c72172
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Linux/sanitizer_set_death_callback_test.cc
@@ -0,0 +1,49 @@
+// RUN: %clangxx -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
+// Check __sanitizer_set_death_callback. Not all sanitizers implement it yet.
+// XFAIL: lsan
+// XFAIL: tsan
+
+#include <sanitizer/common_interface_defs.h>
+#include <stdio.h>
+#include <pthread.h>
+
+volatile char *zero = 0;
+
+void Death() {
+ fprintf(stderr, "DEATH CALLBACK EXECUTED\n");
+}
+// CHECK: DEATH CALLBACK EXECUTED
+
+int global[10];
+volatile char *sink;
+
+void *Thread(void *x) {
+ global[0]++;
+ return x;
+}
+
+__attribute__((noinline))
+void MaybeInit(int *uninitialized) {
+ if (zero)
+ *uninitialized = 1;
+}
+
+__attribute__((noinline))
+void Leak() {
+ sink = new char[100]; // trigger lsan report.
+}
+
+int main(int argc, char **argv) {
+ int uninitialized;
+ __sanitizer_set_death_callback(Death);
+ MaybeInit(&uninitialized);
+ if (uninitialized) // trigger msan report.
+ global[0] = 77;
+ pthread_t t;
+ pthread_create(&t, 0, Thread, 0);
+ global[0]++; // trigger tsan report.
+ pthread_join(t, 0);
+ global[argc + 10]++; // trigger asan report.
+ Leak();
+ sink = 0;
+}
diff --git a/test/sanitizer_common/TestCases/Linux/sched_getparam.cc b/test/sanitizer_common/TestCases/Linux/sched_getparam.cc
new file mode 100644
index 000000000000..390c656fe209
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Linux/sched_getparam.cc
@@ -0,0 +1,13 @@
+// RUN: %clangxx -O0 %s -o %t && %run %t
+
+#include <assert.h>
+#include <sched.h>
+#include <stdio.h>
+
+int main(void) {
+ struct sched_param param;
+ int res = sched_getparam(0, &param);
+ assert(res == 0);
+ if (param.sched_priority == 42) printf(".\n");
+ return 0;
+}
diff --git a/test/sanitizer_common/TestCases/Linux/soft_rss_limit_mb_test.cc b/test/sanitizer_common/TestCases/Linux/soft_rss_limit_mb_test.cc
new file mode 100644
index 000000000000..145cc5d05769
--- /dev/null
+++ b/test/sanitizer_common/TestCases/Linux/soft_rss_limit_mb_test.cc
@@ -0,0 +1,66 @@
+// Check soft_rss_limit_mb. Not all sanitizers implement it yet.
+// RUN: %clangxx -O2 %s -o %t
+//
+// Run with limit should fail:
+// RUN: %tool_options=soft_rss_limit_mb=220:quarantine_size=1:allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s -check-prefix=CHECK_MAY_RETURN_1
+// RUN: %tool_options=soft_rss_limit_mb=220:quarantine_size=1:allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s -check-prefix=CHECK_MAY_RETURN_0
+
+// This run uses getrusage. We can only test getrusage when allocator_may_return_null=0
+// because getrusage gives us max-rss, not current-rss.
+// RUN: %tool_options=soft_rss_limit_mb=220:quarantine_size=1:allocator_may_return_null=0:can_use_proc_maps_statm=0 not %run %t 2>&1 | FileCheck %s -check-prefix=CHECK_MAY_RETURN_0
+
+// FIXME: make it work for other sanitizers.
+// XFAIL: lsan
+// XFAIL: tsan
+// XFAIL: msan
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+static const int kMaxNumAllocs = 1 << 9;
+static const int kAllocSize = 1 << 20; // Large enough to go via mmap.
+
+static char *allocs[kMaxNumAllocs];
+
+int main() {
+ int num_allocs = kMaxNumAllocs / 4;
+ for (int i = 0; i < 3; i++, num_allocs *= 2) {
+ fprintf(stderr, "[%d] allocating %d times\n", i, num_allocs);
+ int zero_results = 0;
+ for (int j = 0; j < num_allocs; j++) {
+ if ((j % (num_allocs / 8)) == 0) {
+ usleep(100000);
+ fprintf(stderr, " [%d]\n", j);
+ }
+ allocs[j] = (char*)malloc(kAllocSize);
+ if (allocs[j])
+ memset(allocs[j], -1, kAllocSize);
+ else
+ zero_results++;
+ }
+ if (zero_results)
+ fprintf(stderr, "Some of the malloc calls returned null: %d\n",
+ zero_results);
+ if (zero_results != num_allocs)
+ fprintf(stderr, "Some of the malloc calls returned non-null: %d\n",
+ num_allocs - zero_results);
+ for (int j = 0; j < num_allocs; j++) {
+ free(allocs[j]);
+ }
+ }
+}
+
+// CHECK_MAY_RETURN_1: allocating 128 times
+// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null: 128
+// CHECK_MAY_RETURN_1: allocating 256 times
+// CHECK_MAY_RETURN_1: Some of the malloc calls returned null:
+// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
+// CHECK_MAY_RETURN_1: allocating 512 times
+// CHECK_MAY_RETURN_1: Some of the malloc calls returned null:
+// CHECK_MAY_RETURN_1: Some of the malloc calls returned non-null:
+
+// CHECK_MAY_RETURN_0: allocating 128 times
+// CHECK_MAY_RETURN_0: Some of the malloc calls returned non-null: 128
+// CHECK_MAY_RETURN_0: allocating 256 times
+// CHECK_MAY_RETURN_0: allocator is terminating the process instead of returning
diff --git a/test/sanitizer_common/TestCases/options-help.cc b/test/sanitizer_common/TestCases/options-help.cc
new file mode 100644
index 000000000000..eaa04a494be4
--- /dev/null
+++ b/test/sanitizer_common/TestCases/options-help.cc
@@ -0,0 +1,8 @@
+// RUN: %clangxx -O0 %s -o %t
+// RUN: %tool_options=help=1 %run %t 2>&1 | FileCheck %s
+
+int main() {
+}
+
+// CHECK: Available flags for {{.*}}Sanitizer:
+// CHECK: handle_segv
diff --git a/test/sanitizer_common/TestCases/options-include.cc b/test/sanitizer_common/TestCases/options-include.cc
new file mode 100644
index 000000000000..ae8995164853
--- /dev/null
+++ b/test/sanitizer_common/TestCases/options-include.cc
@@ -0,0 +1,21 @@
+// RUN: %clangxx -O0 %s -o %t
+// RUN: echo -e "symbolize=1\ninclude='%t.options2.txt'" >%t.options1.txt
+// RUN: echo -e "help=1\n" >%t.options2.txt
+// RUN: cat %t.options1.txt
+// RUN: cat %t.options2.txt
+// RUN: %tool_options="help=0:include='%t.options1.txt'" %run %t 2>&1 | tee %t.out
+// RUN: FileCheck %s --check-prefix=CHECK-VERBOSITY1 <%t.out
+// RUN: %tool_options="include='%t.options1.txt',help=0" %run %t 2>&1 | tee %t.out
+// RUN: FileCheck %s --check-prefix=CHECK-VERBOSITY0 <%t.out
+// RUN: %tool_options="include='%t.options-not-found.txt',help=1" not %run %t 2>&1 | tee %t.out
+// RUN: FileCheck %s --check-prefix=CHECK-NOT-FOUND < %t.out
+
+#include <stdio.h>
+
+int main() {
+ fprintf(stderr, "done\n");
+}
+
+// CHECK-VERBOSITY1: Available flags for
+// CHECK-VERBOSITY0-NOT: Available flags for
+// CHECK-NOT-FOUND: Failed to read options from
diff --git a/test/sanitizer_common/TestCases/options-invalid.cc b/test/sanitizer_common/TestCases/options-invalid.cc
new file mode 100644
index 000000000000..3c261405c992
--- /dev/null
+++ b/test/sanitizer_common/TestCases/options-invalid.cc
@@ -0,0 +1,15 @@
+// RUN: %clangxx -O0 %s -o %t
+// RUN: %tool_options=invalid_option_name=10,verbosity=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-V1
+// RUN: %tool_options=invalid_option_name=10 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-V0
+
+#include <stdio.h>
+
+int main() {
+ fprintf(stderr, "done\n");
+}
+
+// CHECK-V1: WARNING: found 1 unrecognized
+// CHECK-V1: invalid_option_name
+// CHECK-V0-NOT: WARNING: found 1 unrecognized
+// CHECK-V0-NOT: invalid_option_name
+// CHECK: done