1 // RUN: %clang_scudo %s -o %t
2 // RUN: %run %t valid 2>&1
3 // RUN: not %run %t invalid 2>&1 | FileCheck %s
4 // RUN: %env_scudo_opts=allocator_may_return_null=1 %run %t invalid 2>&1
5 // UNSUPPORTED: android
7 // Tests that valloc and pvalloc work as intended.
16 size_t round_up_to(size_t size
, size_t alignment
) {
17 return (size
+ alignment
- 1) & ~(alignment
- 1);
20 int main(int argc
, char **argv
) {
22 size_t size
, page_size
;
26 page_size
= sysconf(_SC_PAGESIZE
);
27 // Check that the page size is a power of two.
28 assert((page_size
& (page_size
- 1)) == 0);
30 if (!strcmp(argv
[1], "valid")) {
31 for (int i
= (sizeof(void *) == 4) ? 3 : 4; i
< 21; i
++) {
33 p
= valloc(size
- (2 * sizeof(void *)));
35 assert(((uintptr_t)p
& (page_size
- 1)) == 0);
37 p
= pvalloc(size
- (2 * sizeof(void *)));
39 assert(((uintptr_t)p
& (page_size
- 1)) == 0);
40 assert(malloc_usable_size(p
) >= round_up_to(size
, page_size
));
44 assert(((uintptr_t)p
& (page_size
- 1)) == 0);
48 assert(((uintptr_t)p
& (page_size
- 1)) == 0);
49 assert(malloc_usable_size(p
) >= round_up_to(size
, page_size
));
53 if (!strcmp(argv
[1], "invalid")) {
54 // Size passed to pvalloc overflows when rounded up.
55 p
= pvalloc((size_t)-1);
56 // CHECK: Scudo ERROR: pvalloc parameters overflow
58 assert(errno
== ENOMEM
);
60 p
= pvalloc((size_t)-page_size
);
62 assert(errno
== ENOMEM
);