1 // RUN: %clangxx %collect_stack_traces -O0 %s -o %t
2 // RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t m1 2>&1 | FileCheck %s
3 // RUN: %env_tool_opts=allocator_may_return_null=1 %run %t m1 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
4 // RUN: %env_tool_opts=allocator_may_return_null=0 not %run %t psm1 2>&1 | FileCheck %s
5 // RUN: %env_tool_opts=allocator_may_return_null=1 %run %t psm1 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
7 // REQUIRES: stable-runtime
9 // UNSUPPORTED: android, target={{.*(freebsd|netbsd).*}}, ubsan
11 // Checks that pvalloc overflows are caught. If the allocator is allowed to
12 // return null, the errno should be set to ENOMEM.
22 int main(int argc
, char *argv
[]) {
24 const char *action
= argv
[1];
26 const size_t page_size
= sysconf(_SC_PAGESIZE
);
29 if (!strcmp(action
, "m1")) {
30 p
= pvalloc((uintptr_t)-1);
31 } else if (!strcmp(action
, "psm1")) {
32 p
= pvalloc((uintptr_t)-(page_size
- 1));
36 // CHECK: {{ERROR: .*Sanitizer: pvalloc parameters overflow: size .* rounded up to system page size .* cannot be represented in type size_t}}
37 // CHECK: {{#0 .*pvalloc}}
38 // CHECK: {{#[12] .*main .*pvalloc-overflow.cpp:}}
39 // CHECK: {{SUMMARY: .*Sanitizer: pvalloc-overflow}}
41 // The NULL pointer is printed differently on different systems, while (long)0
42 // is always the same.
43 fprintf(stderr
, "errno: %d, p: %lx\n", errno
, (long)p
);
44 // CHECK-NULL: errno: 12, p: 0