1 //===-- main.cpp ------------------------------------------------*- C++ -*-===//
2 ////
3 ////                     The LLVM Compiler Infrastructure
4 ////
5 //// This file is distributed under the University of Illinois Open Source
6 //// License. See LICENSE.TXT for details.
7 ////
8 ////===----------------------------------------------------------------------===//
9 //
10 
11 const int size = 5;
12 
13 #include <cstddef>
14 #include <cstdlib>
15 #include <sys/prctl.h>
16 
17 void func(int *ptr) {
18   int *tmp;
19 
20 #if defined  __GNUC__ && !defined __INTEL_COMPILER
21   __builtin___bnd_store_ptr_bounds ((void**)&ptr, ptr);
22 #endif
23   tmp = ptr + size - 1;
24 #if defined  __GNUC__ && !defined __INTEL_COMPILER
25   __builtin___bnd_store_ptr_bounds ((void**)&tmp, tmp);
26 #endif
27   tmp = (int*)0x2; // Break 2.
28 
29   return; // Break 3.
30 }
31 
32 int
33 main(int argc, char const *argv[])
34 {
35   // This call returns 0 only if the CPU and the kernel support
36   // Intel(R) Memory Protection Extensions (Intel(R) MPX).
37   if (prctl(PR_MPX_ENABLE_MANAGEMENT, 0, 0, 0, 0) != 0)
38         return -1;
39 
40   int*  a = (int *) calloc(size, sizeof(int));
41 #if defined  __GNUC__ && !defined __INTEL_COMPILER
42   __builtin___bnd_store_ptr_bounds ((void**)&a, a);
43 #endif
44   func(a); // Break 1.
45 
46   free(a); // Break 4.
47 
48   return 0;
49 }
50