summaryrefslogtreecommitdiff
path: root/test/sanitizer_common
diff options
context:
space:
mode:
authorAlex Shlyapnikov <alekseys@google.com>2018-06-18 20:03:31 +0000
committerAlex Shlyapnikov <alekseys@google.com>2018-06-18 20:03:31 +0000
commit65e9e301b969895edfeadc2fedec84b15430ad42 (patch)
treeaea9d240244bd18f1349999105d36df4927c5dd4 /test/sanitizer_common
parent3555ac91fdb74c57b2dcc7cfc8ba37ae99dfba67 (diff)
[TSan] Report proper error on allocator failures instead of CHECK(0)-ing
Summary: Following up on and complementing D44404 and other sanitizer allocators. Currently many allocator specific errors (OOM, for example) are reported as a text message and CHECK(0) termination, no stack, no details, not too helpful nor informative. To improve the situation, detailed and structured common errors were defined and reported under the appropriate conditions. Common tests were generalized a bit to cover a slightly different TSan stack reporting format, extended to verify errno value and returned pointer value check is now explicit to facilitate debugging. Reviewers: dvyukov Subscribers: srhines, kubamracek, delcypher, #sanitizers, llvm-commits Differential Revision: https://reviews.llvm.org/D48087 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@334975 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/sanitizer_common')
-rw-r--r--test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc21
-rw-r--r--test/sanitizer_common/TestCases/Linux/pvalloc-overflow.cc21
-rw-r--r--test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc28
-rw-r--r--test/sanitizer_common/TestCases/allocator_returns_null.cc24
4 files changed, 59 insertions, 35 deletions
diff --git a/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc b/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc
index 035b5ca7f..1a340edec 100644
--- a/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc
+++ b/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cc
@@ -1,14 +1,23 @@
// RUN: %clangxx %collect_stack_traces -O0 %s -o %t
+
+// Alignment is not a power of 2:
// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 17 2>&1 | FileCheck %s
+// Size is not a multiple of alignment:
+// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 8 2>&1 | FileCheck %s
+// Alignment is 0:
// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 0 2>&1 | FileCheck %s
+
+// The same for allocator_may_return_null=1:
// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 17 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 8 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
// REQUIRES: stable-runtime
-// UNSUPPORTED: android, tsan, ubsan
+// UNSUPPORTED: android, ubsan
#include <assert.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -21,12 +30,14 @@ int main(int argc, char **argv) {
void *p = aligned_alloc(alignment, 100);
// CHECK: {{ERROR: .*Sanitizer: invalid alignment requested in aligned_alloc}}
// Handle a case when aligned_alloc is aliased by memalign.
- // CHECK: {{#0 0x.* in .*}}{{aligned_alloc|memalign}}
- // CHECK: {{#1 0x.* in main .*aligned_alloc-alignment.cc:}}[[@LINE-4]]
+ // CHECK: {{#0 .*}}{{aligned_alloc|memalign}}
+ // CHECK: {{#1 .*main .*aligned_alloc-alignment.cc:}}[[@LINE-4]]
// CHECK: {{SUMMARY: .*Sanitizer: invalid-aligned-alloc-alignment}}
- printf("pointer after failed aligned_alloc: %zd\n", (size_t)p);
- // CHECK-NULL: pointer after failed aligned_alloc: 0
+ // The NULL pointer is printed differently on different systems, while (long)0
+ // is always the same.
+ fprintf(stderr, "errno: %d, p: %lx\n", errno, (long)p);
+ // CHECK-NULL: errno: 22, p: 0
return 0;
}
diff --git a/test/sanitizer_common/TestCases/Linux/pvalloc-overflow.cc b/test/sanitizer_common/TestCases/Linux/pvalloc-overflow.cc
index 93421af19..537c57e1a 100644
--- a/test/sanitizer_common/TestCases/Linux/pvalloc-overflow.cc
+++ b/test/sanitizer_common/TestCases/Linux/pvalloc-overflow.cc
@@ -6,7 +6,7 @@
// REQUIRES: stable-runtime
-// UNSUPPORTED: android, freebsd, netbsd, tsan, ubsan
+// UNSUPPORTED: android, freebsd, netbsd, ubsan
// Checks that pvalloc overflows are caught. If the allocator is allowed to
// return null, the errno should be set to ENOMEM.
@@ -14,6 +14,7 @@
#include <assert.h>
#include <errno.h>
#include <malloc.h>
+#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
@@ -32,15 +33,15 @@ int main(int argc, char *argv[]) {
} else {
assert(0);
}
+ // CHECK: {{ERROR: .*Sanitizer: pvalloc parameters overflow: size .* rounded up to system page size .* cannot be represented in type size_t}}
+ // CHECK: {{#0 .*pvalloc}}
+ // CHECK: {{#1 .*main .*pvalloc-overflow.cc:}}
+ // CHECK: {{SUMMARY: .*Sanitizer: pvalloc-overflow}}
- fprintf(stderr, "errno: %d\n", errno);
+ // The NULL pointer is printed differently on different systems, while (long)0
+ // is always the same.
+ fprintf(stderr, "errno: %d, p: %lx\n", errno, (long)p);
+ // CHECK-NULL: errno: 12, p: 0
- return p != nullptr;
+ return 0;
}
-
-// CHECK: {{ERROR: .*Sanitizer: pvalloc parameters overflow: size .* rounded up to system page size .* cannot be represented in type size_t}}
-// CHECK: {{#0 0x.* in .*pvalloc}}
-// CHECK: {{#1 0x.* in main .*pvalloc-overflow.cc:}}
-// CHECK: {{SUMMARY: .*Sanitizer: pvalloc-overflow}}
-
-// CHECK-NULL: errno: 12
diff --git a/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc b/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc
index c46540b6b..7729057d2 100644
--- a/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc
+++ b/test/sanitizer_common/TestCases/Posix/posix_memalign-alignment.cc
@@ -1,14 +1,26 @@
// RUN: %clangxx %collect_stack_traces -O0 %s -o %t
+
+// Alignment is not a power of two:
// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 17 2>&1 | FileCheck %s
+// Alignment is not a power of two, although is a multiple of sizeof(void*):
+// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 24 2>&1 | FileCheck %s
+// Alignment is not a multiple of sizeof(void*), although is a power of 2:
+// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 2 2>&1 | FileCheck %s
+// Alignment is 0:
// RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 0 2>&1 | FileCheck %s
+
+// The same for allocator_may_return_null=1:
// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 17 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 24 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
+// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
// RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
// REQUIRES: stable-runtime
-// UNSUPPORTED: tsan, ubsan
+// UNSUPPORTED: ubsan
#include <assert.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -16,16 +28,20 @@ int main(int argc, char **argv) {
assert(argc == 2);
const int alignment = atoi(argv[1]);
- void *p = reinterpret_cast<void*>(42);
+ void* const kInitialPtrValue = reinterpret_cast<void*>(0x2a);
+ void *p = kInitialPtrValue;
+ errno = 0;
int res = posix_memalign(&p, alignment, 100);
// CHECK: {{ERROR: .*Sanitizer: invalid alignment requested in posix_memalign}}
- // CHECK: {{#0 0x.* in .*posix_memalign}}
- // CHECK: {{#1 0x.* in main .*posix_memalign-alignment.cc:}}[[@LINE-3]]
+ // CHECK: {{#0 .*posix_memalign}}
+ // CHECK: {{#1 .*main .*posix_memalign-alignment.cc:}}[[@LINE-3]]
// CHECK: {{SUMMARY: .*Sanitizer: invalid-posix-memalign-alignment}}
- printf("pointer after failed posix_memalign: %zd\n", (size_t)p);
- // CHECK-NULL: pointer after failed posix_memalign: 42
+ // The NULL pointer is printed differently on different systems, while (long)0
+ // is always the same.
+ fprintf(stderr, "errno: %d, res: %d, p: %lx\n", errno, res, (long)p);
+ // CHECK-NULL: errno: 0, res: 22, p: 2a
return 0;
}
diff --git a/test/sanitizer_common/TestCases/allocator_returns_null.cc b/test/sanitizer_common/TestCases/allocator_returns_null.cc
index 425e7f316..9ecdfef9f 100644
--- a/test/sanitizer_common/TestCases/allocator_returns_null.cc
+++ b/test/sanitizer_common/TestCases/allocator_returns_null.cc
@@ -1,8 +1,8 @@
// Test the behavior of malloc/calloc/realloc/new when the allocation size
// exceeds the sanitizer's allocator 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 and set errno to
-// the appropriate error code.
+// allocator_may_return_null=1 the allocator should return nullptr and set errno
+// to the appropriate error code.
//
// RUN: %clangxx -O0 %s -o %t
// RUN: not %run %t malloc 2>&1 | FileCheck %s --check-prefix=CHECK-mCRASH
@@ -36,7 +36,7 @@
// RUN: | FileCheck %s --check-prefix=CHECK-NULL
// TODO(alekseyshl): win32 is disabled due to failing errno tests, fix it there.
-// UNSUPPORTED: tsan, ubsan, win32
+// UNSUPPORTED: ubsan, win32
#include <assert.h>
#include <errno.h>
@@ -51,12 +51,8 @@ int main(int argc, char **argv) {
const char *action = argv[1];
fprintf(stderr, "%s:\n", action);
- // The maximum value of all supported sanitizers:
- // ASan: asan_allocator.cc, search for kMaxAllowedMallocSize.
- // LSan: lsan_allocator.cc, search for kMaxAllowedMallocSize.
- // ASan + LSan: ASan limit is used.
- // MSan: msan_allocator.cc, search for kMaxAllowedMallocSize.
- // TSan: tsan_mman.cc, user_alloc_internal function.
+ // The maximum value of all supported sanitizers (search for
+ // kMaxAllowedMallocSize). For ASan + LSan, ASan limit is used.
static const size_t kMaxAllowedMallocSizePlusOne =
#if __LP64__ || defined(_WIN64)
(1ULL << 40) + 1;
@@ -90,11 +86,11 @@ int main(int argc, char **argv) {
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, "errno: %d, x: %lx\n", errno, (long)x);
- free(x);
-
- return x != nullptr;
+ return 0;
}
// CHECK-mCRASH: malloc:
@@ -115,4 +111,4 @@ int main(int argc, char **argv) {
// CHECK-nnCRASH: {{SUMMARY: .*Sanitizer: allocation-size-too-big}}
// CHECK-NULL: {{malloc|calloc|calloc-overflow|realloc|realloc-after-malloc|new-nothrow}}
-// CHECK-NULL: errno: 12
+// CHECK-NULL: errno: 12, x: 0