Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / scudo / valloc.c
blob7f3a38ec500731dfeee48b25d1d3e458cf83aadf
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.
9 #include <assert.h>
10 #include <errno.h>
11 #include <malloc.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <unistd.h>
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) {
21 void *p = NULL;
22 size_t size, page_size;
24 assert(argc == 2);
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++) {
32 size = 1U << i;
33 p = valloc(size - (2 * sizeof(void *)));
34 assert(p);
35 assert(((uintptr_t)p & (page_size - 1)) == 0);
36 free(p);
37 p = pvalloc(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(p);
42 p = valloc(size);
43 assert(p);
44 assert(((uintptr_t)p & (page_size - 1)) == 0);
45 free(p);
46 p = pvalloc(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(p);
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
57 assert(!p);
58 assert(errno == ENOMEM);
59 errno = 0;
60 p = pvalloc((size_t)-page_size);
61 assert(!p);
62 assert(errno == ENOMEM);
64 return 0;