1 | // RUN: %clangxx %s -o %t && %run %t 2>&1 | FileCheck %s |
2 | // UNSUPPORTED: android, hwasan, ubsan |
3 | |
4 | #include <stdio.h> |
5 | #include <stdlib.h> |
6 | #if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 2) |
7 | #include <mcheck.h> |
8 | #else |
9 | #define MCHECK_OK 0 |
10 | extern "C" int mcheck(void (*abortfunc)(int mstatus)); |
11 | extern "C" int mcheck_pedantic(void (*abortfunc)(int mstatus)); |
12 | extern "C" int mprobe(void *ptr); |
13 | #endif |
14 | |
15 | void check_heap() { |
16 | void *p = malloc(size: 1000); |
17 | int res = mprobe(ptr: p); |
18 | if (res == MCHECK_OK) |
19 | printf(format: "Success!\n" ); |
20 | free(ptr: p); |
21 | } |
22 | |
23 | int main(int argc, char *argv[]) { |
24 | void *p; |
25 | if (mcheck(NULL) != 0) { |
26 | fprintf(stderr, format: "mcheck() failed\n" ); |
27 | exit(EXIT_FAILURE); |
28 | } |
29 | |
30 | check_heap(); |
31 | // CHECK: Success! |
32 | |
33 | if (mcheck_pedantic(NULL) != 0) { |
34 | fprintf(stderr, format: "mcheck_pedantic() failed\n" ); |
35 | exit(EXIT_FAILURE); |
36 | } |
37 | |
38 | check_heap(); |
39 | // CHECK: Success! |
40 | |
41 | return 0; |
42 | } |
43 | |