Skip to content

Commit

Permalink
[scudo] Make some tests less Linux-y
Browse files Browse the repository at this point in the history
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

llvm-svn: 326100
  • Loading branch information
Kostya Kortchinsky committed Feb 26, 2018
1 parent 39ceac1 commit 0c8ecea
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
8 changes: 0 additions & 8 deletions compiler-rt/test/scudo/double-free.cpp
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
27 changes: 24 additions & 3 deletions compiler-rt/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.
Expand Down Expand Up @@ -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);
Expand All @@ -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
24 changes: 4 additions & 20 deletions compiler-rt/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>

Expand All @@ -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;
}

Expand Down
10 changes: 6 additions & 4 deletions compiler-rt/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
Expand All @@ -15,6 +15,8 @@

#include <vector>

#include <sanitizer/allocator_interface.h>

int main(int argc, char **argv)
{
void *p, *old_p;
Expand All @@ -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++)
Expand All @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions compiler-rt/test/scudo/sizes.cpp
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 0c8ecea

Please sign in to comment.