xref: /llvm-project/compiler-rt/test/hwasan/TestCases/Linux/pvalloc-overflow.cpp (revision 2f7d11be6f8d9d8658238889fb5b41e34e4ad7a2)
1 // RUN: %clangxx_hwasan -O0 %s -o %t
2 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t m1 2>&1 | FileCheck %s
3 // RUN: %env_hwasan_opts=allocator_may_return_null=1     %run %t m1 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
4 // RUN: %env_hwasan_opts=allocator_may_return_null=0 not %run %t psm1 2>&1 | FileCheck %s
5 // RUN: %env_hwasan_opts=allocator_may_return_null=1     %run %t psm1 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
6 
7 // UNSUPPORTED: android
8 
9 // REQUIRES: stable-runtime
10 
11 // Checks that pvalloc overflows are caught. If the allocator is allowed to
12 // return null, the errno should be set to ENOMEM.
13 
14 #include <assert.h>
15 #include <errno.h>
16 #include <malloc.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <unistd.h>
20 
21 int main(int argc, char *argv[]) {
22   assert(argc == 2);
23   const char *action = argv[1];
24 
25   const size_t page_size = sysconf(_SC_PAGESIZE);
26 
27   void *p = nullptr;
28   if (!strcmp(action, "m1")) {
29     p = pvalloc((uintptr_t)-1);
30   } else if (!strcmp(action, "psm1")) {
31     p = pvalloc((uintptr_t)-(page_size - 1));
32   } else {
33     assert(0);
34   }
35 
36   fprintf(stderr, "errno: %d\n", errno);
37 
38   return p != nullptr;
39 }
40 
41 // CHECK: {{ERROR: HWAddressSanitizer: pvalloc parameters overflow: size .* rounded up to system page size .* cannot be represented in type size_t}}
42 // CHECK: {{#0 0x.* in .*pvalloc}}
43 // CHECK: {{#1 0x.* in main .*pvalloc-overflow.cpp:}}
44 // CHECK: SUMMARY: HWAddressSanitizer: pvalloc-overflow
45 
46 // CHECK-NULL: errno: 12
47