summaryrefslogtreecommitdiff
path: root/test/scudo
diff options
context:
space:
mode:
authorKostya Kortchinsky <kostyak@google.com>2018-02-26 17:14:44 +0000
committerKostya Kortchinsky <kostyak@google.com>2018-02-26 17:14:44 +0000
commitfbef9e077c6fab0a50b1ce91d7264a271b208ef1 (patch)
treea36dab5f5b5e9a462cf9139ca26e1aec74ee7605 /test/scudo
parent2c3261fe1289845bac95a8f2e72fd01909105278 (diff)
[scudo] Make some tests less Linux-y
Summary: Start making the Scudo tests less Linux-y: - `malloc_usable_size` doesn't exist everywhere, so replace them with `__sanitizer_get_allocated_size` which we provide; - move all the `memalign` related tests into `memalign.c` since it's also not available everywhere. I also noticed that the `memalign.c` was missing a line in one of the loops. Reviewers: alekseyshl Reviewed By: alekseyshl Subscribers: delcypher, #sanitizers, llvm-commits Differential Revision: https://reviews.llvm.org/D43393 git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@326100 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/scudo')
-rw-r--r--test/scudo/double-free.cpp8
-rw-r--r--test/scudo/memalign.c27
-rw-r--r--test/scudo/mismatch.cpp24
-rw-r--r--test/scudo/realloc.cpp10
-rw-r--r--test/scudo/sizes.cpp18
5 files changed, 43 insertions, 44 deletions
diff --git a/test/scudo/double-free.cpp b/test/scudo/double-free.cpp
index 56118038c..de6c90f1c 100644
--- a/test/scudo/double-free.cpp
+++ b/test/scudo/double-free.cpp
@@ -2,7 +2,6 @@
// RUN: not %run %t malloc 2>&1 | FileCheck %s
// RUN: not %run %t new 2>&1 | FileCheck %s
// RUN: not %run %t newarray 2>&1 | FileCheck %s
-// RUN: not %run %t memalign 2>&1 | FileCheck %s
// Tests double-free error on pointers allocated with different allocation
// functions.
@@ -32,13 +31,6 @@ int main(int argc, char **argv)
delete[] p;
delete[] p;
}
- if (!strcmp(argv[1], "memalign")) {
- void *p = nullptr;
- posix_memalign(&p, 0x100, sizeof(int));
- assert(p);
- free(p);
- free(p);
- }
return 0;
}
diff --git a/test/scudo/memalign.c b/test/scudo/memalign.c
index 1fe6e3ec7..694a4dad6 100644
--- a/test/scudo/memalign.c
+++ b/test/scudo/memalign.c
@@ -1,7 +1,10 @@
// RUN: %clang_scudo %s -o %t
-// RUN: %run %t valid 2>&1
-// RUN: not %run %t invalid 2>&1
-// RUN: %env_scudo_opts=allocator_may_return_null=1 %run %t invalid 2>&1
+// RUN: %run %t valid 2>&1
+// RUN: not %run %t invalid 2>&1
+// RUN: %env_scudo_opts=allocator_may_return_null=1 %run %t invalid 2>&1
+// RUN: not %run %t double-free 2>&1 | FileCheck --check-prefix=CHECK-double-free %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t realloc 2>&1 | FileCheck --check-prefix=CHECK-realloc %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t realloc 2>&1
// Tests that the various aligned allocation functions work as intended. Also
// tests for the condition where the alignment is not a power of 2.
@@ -51,6 +54,7 @@ int main(int argc, char **argv)
// For larger alignment, reduce the number of allocations to avoid running
// out of potential addresses (on 32-bit).
for (int i = 19; i <= 24; i++) {
+ alignment = 1U << i;
for (int k = 0; k < 3; k++) {
p = memalign(alignment, 0x1000 - (2 * sizeof(void *) * k));
assert(p);
@@ -77,5 +81,22 @@ int main(int argc, char **argv)
assert(p == p_unchanged);
assert(err == EINVAL);
}
+ if (!strcmp(argv[1], "double-free")) {
+ void *p = NULL;
+ posix_memalign(&p, 0x100, sizeof(int));
+ assert(p);
+ free(p);
+ free(p);
+ }
+ if (!strcmp(argv[1], "realloc")) {
+ // We cannot reallocate a memalign'd chunk.
+ void *p = memalign(16, 16);
+ assert(p);
+ p = realloc(p, 32);
+ free(p);
+ }
return 0;
}
+
+// CHECK-double-free: ERROR: invalid chunk state
+// CHECK-realloc: ERROR: allocation type mismatch when reallocating address
diff --git a/test/scudo/mismatch.cpp b/test/scudo/mismatch.cpp
index b49e0ea46..b794f66d8 100644
--- a/test/scudo/mismatch.cpp
+++ b/test/scudo/mismatch.cpp
@@ -1,18 +1,13 @@
// RUN: %clangxx_scudo %s -o %t
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t mallocdel 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t mallocdel 2>&1
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t newfree 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t newfree 2>&1
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t memaligndel 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t memaligndel 2>&1
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t memalignrealloc 2>&1 | FileCheck --check-prefix=CHECK-realloc %s
-// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t memalignrealloc 2>&1
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t mallocdel 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t mallocdel 2>&1
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t newfree 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
+// RUN: %env_scudo_opts=DeallocationTypeMismatch=0 %run %t newfree 2>&1
// Tests that type mismatches between allocation and deallocation functions are
// caught when the related option is set.
#include <assert.h>
-#include <malloc.h>
#include <stdlib.h>
#include <string.h>
@@ -29,17 +24,6 @@ int main(int argc, char **argv)
assert(p);
free((void *)p);
}
- if (!strcmp(argv[1], "memaligndel")) {
- int *p = (int *)memalign(16, 16);
- assert(p);
- delete p;
- }
- if (!strcmp(argv[1], "memalignrealloc")) {
- void *p = memalign(16, 16);
- assert(p);
- p = realloc(p, 32);
- free(p);
- }
return 0;
}
diff --git a/test/scudo/realloc.cpp b/test/scudo/realloc.cpp
index 254c67a2c..26f6373b9 100644
--- a/test/scudo/realloc.cpp
+++ b/test/scudo/realloc.cpp
@@ -1,6 +1,6 @@
// RUN: %clangxx_scudo %s -lstdc++ -o %t
-// RUN: %run %t pointers 2>&1
-// RUN: %run %t contents 2>&1
+// RUN: %run %t pointers 2>&1
+// RUN: %run %t contents 2>&1
// RUN: %run %t usablesize 2>&1
// Tests that our reallocation function returns the same pointer when the
@@ -15,6 +15,8 @@
#include <vector>
+#include <sanitizer/allocator_interface.h>
+
int main(int argc, char **argv)
{
void *p, *old_p;
@@ -35,7 +37,7 @@ int main(int argc, char **argv)
if (p) free(p);
size += 16;
p = malloc(size);
- usable_size = malloc_usable_size(p);
+ usable_size = __sanitizer_get_allocated_size(p);
assert(usable_size >= size);
} while (usable_size == size);
for (int i = 0; i < usable_size; i++)
@@ -56,7 +58,7 @@ int main(int argc, char **argv)
if (!strcmp(argv[1], "pointers")) {
old_p = p = realloc(nullptr, size);
assert(p);
- size = malloc_usable_size(p);
+ size = __sanitizer_get_allocated_size(p);
// Our realloc implementation will return the same pointer if the size
// requested is lower than or equal to the usable size of the associated
// chunk.
diff --git a/test/scudo/sizes.cpp b/test/scudo/sizes.cpp
index 73fc71f25..a5e48a250 100644
--- a/test/scudo/sizes.cpp
+++ b/test/scudo/sizes.cpp
@@ -21,10 +21,10 @@
#include <limits>
#include <new>
+#include <sanitizer/allocator_interface.h>
+
int main(int argc, char **argv) {
assert(argc == 2);
- const char *action = argv[1];
- fprintf(stderr, "%s:\n", action);
#if __LP64__ || defined(_WIN64)
static const size_t kMaxAllowedMallocSize = 1ULL << 40;
@@ -34,32 +34,32 @@ int main(int argc, char **argv) {
static const size_t kChunkHeaderSize = 8;
#endif
- if (!strcmp(action, "malloc")) {
+ if (!strcmp(argv[1], "malloc")) {
void *p = malloc(kMaxAllowedMallocSize);
assert(!p);
p = malloc(kMaxAllowedMallocSize - kChunkHeaderSize);
assert(!p);
- } else if (!strcmp(action, "calloc")) {
+ } else if (!strcmp(argv[1], "calloc")) {
// Trigger an overflow in calloc.
size_t size = std::numeric_limits<size_t>::max();
void *p = calloc((size / 0x1000) + 1, 0x1000);
assert(!p);
- } else if (!strcmp(action, "new")) {
+ } else if (!strcmp(argv[1], "new")) {
void *p = operator new(kMaxAllowedMallocSize);
assert(!p);
- } else if (!strcmp(action, "new-nothrow")) {
+ } else if (!strcmp(argv[1], "new-nothrow")) {
void *p = operator new(kMaxAllowedMallocSize, std::nothrow);
assert(!p);
- } else if (!strcmp(action, "usable")) {
+ } else if (!strcmp(argv[1], "usable")) {
// Playing with the actual usable size of a chunk.
void *p = malloc(1007);
assert(p);
- size_t size = malloc_usable_size(p);
+ size_t size = __sanitizer_get_allocated_size(p);
assert(size >= 1007);
memset(p, 'A', size);
p = realloc(p, 2014);
assert(p);
- size = malloc_usable_size(p);
+ size = __sanitizer_get_allocated_size(p);
assert(size >= 2014);
memset(p, 'B', size);
free(p);