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
6
7// Tests that valloc and pvalloc work as intended.
8
9#include <assert.h>
10#include <errno.h>
11#include <malloc.h>
12#include <stdint.h>
13#include <string.h>
14#include <unistd.h>
15
16size_t round_up_to(size_t size, size_t alignment) {
17 return (size + alignment - 1) & ~(alignment - 1);
18}
19
20int main(int argc, char **argv) {
21 void *p = NULL;
22 size_t size, page_size;
23
24 assert(argc == 2);
25
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);
29
30 if (!strcmp(s1: argv[1], s2: "valid")) {
31 for (int i = (sizeof(void *) == 4) ? 3 : 4; i < 21; i++) {
32 size = 1U << i;
33 p = valloc(size: size - (2 * sizeof(void *)));
34 assert(p);
35 assert(((uintptr_t)p & (page_size - 1)) == 0);
36 free(ptr: p);
37 p = pvalloc(size: size - (2 * sizeof(void *)));
38 assert(p);
39 assert(((uintptr_t)p & (page_size - 1)) == 0);
40 assert(malloc_usable_size(p) >= round_up_to(size, page_size));
41 free(ptr: p);
42 p = valloc(size: size);
43 assert(p);
44 assert(((uintptr_t)p & (page_size - 1)) == 0);
45 free(ptr: p);
46 p = pvalloc(size: size);
47 assert(p);
48 assert(((uintptr_t)p & (page_size - 1)) == 0);
49 assert(malloc_usable_size(p) >= round_up_to(size, page_size));
50 free(ptr: p);
51 }
52 }
53 if (!strcmp(s1: argv[1], s2: "invalid")) {
54 // Size passed to pvalloc overflows when rounded up.
55 p = pvalloc(size: (size_t)-1);
56 // CHECK: Scudo ERROR: pvalloc parameters overflow
57 assert(!p);
58 assert(errno == ENOMEM);
59 errno = 0;
60 p = pvalloc(size: (size_t)-page_size);
61 assert(!p);
62 assert(errno == ENOMEM);
63 }
64 return 0;
65}
66

source code of compiler-rt/test/scudo/valloc.c