1 | const int size = 5; |
2 | |
3 | #include <cstddef> |
4 | #include <cstdlib> |
5 | #include <sys/prctl.h> |
6 | |
7 | void func(int *ptr) { |
8 | int *tmp; |
9 | |
10 | #if defined __GNUC__ && !defined __INTEL_COMPILER |
11 | __builtin___bnd_store_ptr_bounds ((void**)&ptr, ptr); |
12 | #endif |
13 | tmp = ptr + size - 1; |
14 | #if defined __GNUC__ && !defined __INTEL_COMPILER |
15 | __builtin___bnd_store_ptr_bounds ((void**)&tmp, tmp); |
16 | #endif |
17 | tmp = (int*)0x2; // Break 2. |
18 | |
19 | return; // Break 3. |
20 | } |
21 | |
22 | int |
23 | main(int argc, char const *argv[]) |
24 | { |
25 | // This call returns 0 only if the CPU and the kernel support |
26 | // 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(nmemb: size, size: sizeof(int)); |
31 | #if defined __GNUC__ && !defined __INTEL_COMPILER |
32 | __builtin___bnd_store_ptr_bounds ((void**)&a, a); |
33 | #endif |
34 | func(ptr: a); // Break 1. |
35 | |
36 | free(ptr: a); // Break 4. |
37 | |
38 | return 0; |
39 | } |
40 | |