1 // RUN: %clangxx %collect_stack_traces -O0 %s -o %t
2 
3 // Alignment is not a power of two:
4 // RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 17 2>&1 | FileCheck %s
5 // Alignment is not a power of two, although is a multiple of sizeof(void*):
6 // RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 24 2>&1 | FileCheck %s
7 // Alignment is not a multiple of sizeof(void*), although is a power of 2:
8 // RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 2 2>&1 | FileCheck %s
9 // Alignment is 0:
10 // RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t 0 2>&1 | FileCheck %s
11 
12 // The same for allocator_may_return_null=1:
13 // RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 17 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
14 // RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 24 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
15 // RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 2 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
16 // RUN: %env_tool_opts=allocator_may_return_null=1 %run %t 0 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
17 
18 // REQUIRES: stable-runtime
19 
20 // UNSUPPORTED: ubsan
21 
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 
main(int argc,char ** argv)27 int main(int argc, char **argv) {
28   assert(argc == 2);
29   const int alignment = atoi(argv[1]);
30 
31   void* const kInitialPtrValue = reinterpret_cast<void*>(0x2a);
32   void *p = kInitialPtrValue;
33 
34   errno = 0;
35   int res = posix_memalign(&p, alignment, 100);
36   // CHECK: {{ERROR: .*Sanitizer: invalid alignment requested in posix_memalign}}
37   // CHECK: {{#0 .*posix_memalign}}
38   // CHECK: {{#[12] .*main .*posix_memalign-alignment.cpp:}}[[@LINE-3]]
39   // CHECK: {{SUMMARY: .*Sanitizer: invalid-posix-memalign-alignment}}
40 
41   // The NULL pointer is printed differently on different systems, while (long)0
42   // is always the same.
43   fprintf(stderr, "errno: %d, res: %d, p: %lx\n", errno, res, (long)p);
44   // CHECK-NULL: errno: 0, res: 22, p: 2a
45 
46   return 0;
47 }
48