1 // RUN: %clangxx %s -o %t && %run %t 2>&1 | FileCheck %s
2 // UNSUPPORTED: android, 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 
check_heap()15 void check_heap() {
16   void *p = malloc(1000);
17   int res = mprobe(p);
18   if (res == MCHECK_OK)
19     printf("Success!\n");
20   free(p);
21 }
22 
main(int argc,char * argv[])23 int main(int argc, char *argv[]) {
24   void *p;
25   if (mcheck(NULL) != 0) {
26     fprintf(stderr, "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, "mcheck_pedantic() failed\n");
35     exit(EXIT_FAILURE);
36   }
37 
38   check_heap();
39   // CHECK: Success!
40 
41   return 0;
42 }
43