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
5 // Tests that the various aligned allocation functions work as intended. Also
6 // tests for the condition where the alignment is not a power of 2.
7
8 #include <assert.h>
9 #include <malloc.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 // Sometimes the headers may not have this...
14 extern "C" void *aligned_alloc (size_t alignment, size_t size);
15
main(int argc,char ** argv)16 int main(int argc, char **argv)
17 {
18 void *p;
19 size_t alignment = 1U << 12;
20 size_t size = alignment;
21
22 assert(argc == 2);
23 if (!strcmp(argv[1], "valid")) {
24 p = memalign(alignment, size);
25 if (!p)
26 return 1;
27 free(p);
28 p = nullptr;
29 posix_memalign(&p, alignment, size);
30 if (!p)
31 return 1;
32 free(p);
33 p = aligned_alloc(alignment, size);
34 if (!p)
35 return 1;
36 free(p);
37 }
38 if (!strcmp(argv[1], "invalid")) {
39 p = memalign(alignment - 1, size);
40 free(p);
41 }
42 return 0;
43 }
44
45 // CHECK: ERROR: malloc alignment is not a power of 2
46