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 #include "../utils.h"
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[]) {
24   assert(argc == 2);
25   const char *action = argv[1];
26 
27   const size_t page_size = sysconf(_SC_PAGESIZE);
28 
29   void *p = nullptr;
30   if (!untag_strcmp(action, "m1")) {
31     p = pvalloc((uintptr_t)-1);
32   } else if (!untag_strcmp(action, "psm1")) {
33     p = pvalloc((uintptr_t)-(page_size - 1));
34   } else {
35     assert(0);
36   }
37 
38   untag_fprintf(stderr, "errno: %d\n", errno);
39 
40   return p != nullptr;
41 }
42 
43 // CHECK: {{ERROR: HWAddressSanitizer: pvalloc parameters overflow: size .* rounded up to system page size .* cannot be represented in type size_t}}
44 // CHECK: {{#0 0x.* in .*pvalloc}}
45 // CHECK: {{#1 0x.* in main .*pvalloc-overflow.cpp:}}
46 // CHECK: SUMMARY: HWAddressSanitizer: pvalloc-overflow
47 
48 // CHECK-NULL: errno: 12
49