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