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

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/aligned_alloc-alignment.cpp