xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.arch/i386-mpx-map.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* Test program for MPX map allocated bounds.
2 
3    Copyright 2015-2020 Free Software Foundation, Inc.
4 
5    Contributed by Intel Corp. <walfred.tedeschi@intel.com>
6 			      <mircea.gherzan@intel.com>
7 
8    This file is part of GDB.
9 
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22 
23 #include <stdlib.h>
24 #include "x86-cpuid.h"
25 
26 #ifndef NOINLINE
27 #define NOINLINE __attribute__ ((noinline))
28 #endif
29 
30 #define SIZE  5
31 
32 typedef int T;
33 
34 unsigned int have_mpx (void) NOINLINE;
35 
36 unsigned int NOINLINE
37 have_mpx (void)
38 {
39   unsigned int eax, ebx, ecx, edx;
40 
41   if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
42     return 0;
43 
44   if ((ecx & bit_OSXSAVE) == bit_OSXSAVE)
45     {
46       if (__get_cpuid_max (0, NULL) < 7)
47 	return 0;
48 
49       __cpuid_count (7, 0, eax, ebx, ecx, edx);
50 
51       if ((ebx & bit_MPX) == bit_MPX)
52 	return 1;
53       else
54 	return 0;
55     }
56   return 0;
57 }
58 
59 void
60 foo (T *p)
61 {
62   T *x;
63 
64 #if defined  __GNUC__ && !defined __INTEL_COMPILER
65   __bnd_store_ptr_bounds (p, &p);
66 #endif
67 
68   x = p + SIZE - 1;
69 
70 #if defined  __GNUC__ && !defined __INTEL_COMPILER
71   __bnd_store_ptr_bounds (x, &x);
72 #endif
73   /* Dummy assign.  */
74   x = x + 1;			/* after-assign */
75   return;
76 }
77 
78 int
79 main (void)
80 {
81   if (have_mpx ())
82     {
83       T *a = NULL;
84 
85       a = calloc (SIZE, sizeof (T));	/* after-decl */
86 #if defined  __GNUC__ && !defined __INTEL_COMPILER
87       __bnd_store_ptr_bounds (a, &a);
88 #endif
89 
90       foo (a);				/* after-alloc */
91       free (a);
92     }
93   return 0;
94 }
95