40 lines · cpp
1const int size = 5;2 3#include <cstddef>4#include <cstdlib>5#include <sys/prctl.h>6 7void func(int *ptr) {8 int *tmp;9 10#if defined __GNUC__ && !defined __INTEL_COMPILER11 __builtin___bnd_store_ptr_bounds ((void**)&ptr, ptr);12#endif13 tmp = ptr + size - 1;14#if defined __GNUC__ && !defined __INTEL_COMPILER15 __builtin___bnd_store_ptr_bounds ((void**)&tmp, tmp);16#endif17 tmp = (int*)0x2; // Break 2.18 19 return; // Break 3.20}21 22int23main(int argc, char const *argv[])24{25 // This call returns 0 only if the CPU and the kernel support26 // Intel(R) Memory Protection Extensions (Intel(R) MPX).27 if (prctl(PR_MPX_ENABLE_MANAGEMENT, 0, 0, 0, 0) != 0)28 return -1;29 30 int* a = (int *) calloc(size, sizeof(int));31#if defined __GNUC__ && !defined __INTEL_COMPILER32 __builtin___bnd_store_ptr_bounds ((void**)&a, a);33#endif34 func(a); // Break 1.35 36 free(a); // Break 4.37 38 return 0;39}40