2017-07-26 05:18:02 +08:00
|
|
|
// RUN: %clang_scudo %s -o %t
|
2017-09-18 23:40:53 +08:00
|
|
|
// RUN: %run %t valid 2>&1
|
2018-06-16 00:45:19 +08:00
|
|
|
// RUN: not %run %t invalid 2>&1 | FileCheck %s
|
2017-09-18 23:40:53 +08:00
|
|
|
// RUN: %env_scudo_opts=allocator_may_return_null=1 %run %t invalid 2>&1
|
2018-01-05 02:35:28 +08:00
|
|
|
// UNSUPPORTED: android
|
2017-07-26 05:18:02 +08:00
|
|
|
|
|
|
|
// Tests that valloc and pvalloc work as intended.
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
size_t round_up_to(size_t size, size_t alignment) {
|
|
|
|
return (size + alignment - 1) & ~(alignment - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
[scudo] Implement stricter separation of C vs C++
Summary:
Initially, Scudo had a monolithic design where both C and C++ functions were
living in the same library. This was not necessarily ideal, and with the work
on -fsanitize=scudo, it became more apparent that this needed to change.
We are splitting the new/delete interceptor in their own C++ library. This
allows more flexibility, notably with regard to std::bad_alloc when the work is
done. This also allows us to not link new & delete when using pure C.
Additionally, we add the UBSan runtimes with Scudo, in order to be able to have
a -fsanitize=scudo,undefined in Clang (see work in D39334).
The changes in this patch:
- split the cxx specific code in the scudo cmake file into a new library;
(remove the spurious foreach loop, that was not necessary)
- add the UBSan runtimes (both C and C++);
- change the test cmake file to allow for specific C & C++ tests;
- make C tests pure C, rename their extension accordingly.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, mgorny, llvm-commits
Differential Revision: https://reviews.llvm.org/D39461
llvm-svn: 317097
2017-11-01 23:28:20 +08:00
|
|
|
void *p = NULL;
|
2017-07-26 05:18:02 +08:00
|
|
|
size_t size, page_size;
|
|
|
|
|
|
|
|
assert(argc == 2);
|
|
|
|
|
|
|
|
page_size = sysconf(_SC_PAGESIZE);
|
|
|
|
// Check that the page size is a power of two.
|
|
|
|
assert((page_size & (page_size - 1)) == 0);
|
|
|
|
|
|
|
|
if (!strcmp(argv[1], "valid")) {
|
|
|
|
for (int i = (sizeof(void *) == 4) ? 3 : 4; i < 21; i++) {
|
|
|
|
size = 1U << i;
|
|
|
|
p = valloc(size - (2 * sizeof(void *)));
|
|
|
|
assert(p);
|
|
|
|
assert(((uintptr_t)p & (page_size - 1)) == 0);
|
|
|
|
free(p);
|
|
|
|
p = pvalloc(size - (2 * sizeof(void *)));
|
|
|
|
assert(p);
|
|
|
|
assert(((uintptr_t)p & (page_size - 1)) == 0);
|
|
|
|
assert(malloc_usable_size(p) >= round_up_to(size, page_size));
|
|
|
|
free(p);
|
|
|
|
p = valloc(size);
|
|
|
|
assert(p);
|
|
|
|
assert(((uintptr_t)p & (page_size - 1)) == 0);
|
|
|
|
free(p);
|
|
|
|
p = pvalloc(size);
|
|
|
|
assert(p);
|
|
|
|
assert(((uintptr_t)p & (page_size - 1)) == 0);
|
|
|
|
assert(malloc_usable_size(p) >= round_up_to(size, page_size));
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "invalid")) {
|
|
|
|
// Size passed to pvalloc overflows when rounded up.
|
|
|
|
p = pvalloc((size_t)-1);
|
2018-06-16 00:45:19 +08:00
|
|
|
// CHECK: Scudo ERROR: pvalloc parameters overflow
|
2017-07-26 05:18:02 +08:00
|
|
|
assert(!p);
|
|
|
|
assert(errno == ENOMEM);
|
|
|
|
errno = 0;
|
|
|
|
p = pvalloc((size_t)-page_size);
|
|
|
|
assert(!p);
|
|
|
|
assert(errno == ENOMEM);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|