43 lines · cpp
1// RUN: %clangxx %s -o %t && %run %t 2>&1 | FileCheck %s2// UNSUPPORTED: android, hwasan, ubsan3 4#include <stdio.h>5#include <stdlib.h>6#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 2)7#include <mcheck.h>8#else9#define MCHECK_OK 010extern "C" int mcheck(void (*abortfunc)(int mstatus));11extern "C" int mcheck_pedantic(void (*abortfunc)(int mstatus));12extern "C" int mprobe(void *ptr);13#endif14 15void 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 23int 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