Index: test/scudo/double-free.cpp =================================================================== --- test/scudo/double-free.cpp +++ 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 @@ 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; } Index: test/scudo/memalign.c =================================================================== --- test/scudo/memalign.c +++ 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 @@ // 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 @@ 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 Index: test/scudo/mismatch.cpp =================================================================== --- test/scudo/mismatch.cpp +++ 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 -#include #include #include @@ -29,17 +24,6 @@ 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; } Index: test/scudo/realloc.cpp =================================================================== --- test/scudo/realloc.cpp +++ 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 +#include + int main(int argc, char **argv) { void *p, *old_p; @@ -35,7 +37,7 @@ 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 @@ 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. Index: test/scudo/sizes.cpp =================================================================== --- test/scudo/sizes.cpp +++ test/scudo/sizes.cpp @@ -21,10 +21,10 @@ #include #include +#include + 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 @@ 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::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);