1/* Test for inferior function calls MPX context.
2
3   Copyright (C) 2017-2020 Free Software Foundation, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18#include <stdlib.h>
19#include <string.h>
20#include "x86-cpuid.h"
21
22/* Defined size for arrays.  */
23#define ARRAY_LENGTH    5
24
25unsigned int
26have_mpx (void)
27{
28  unsigned int eax, ebx, ecx, edx;
29
30  if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
31    return 0;
32
33  if ((ecx & bit_OSXSAVE) == bit_OSXSAVE)
34    {
35      if (__get_cpuid_max (0, NULL) < 7)
36	return 0;
37
38      __cpuid_count (7, 0, eax, ebx, ecx, edx);
39
40      if ((ebx & bit_MPX) == bit_MPX)
41	return 1;
42      else
43	return 0;
44    }
45  return 0;
46}
47
48
49int
50upper (int *a, int *b, int *c, int *d, int len)
51{
52  int value;
53
54  value = *(a + len);
55  value = *(b + len);
56  value = *(c + len);
57  value = *(d + len);
58
59  value = value - *a + 1;
60  return value;
61}
62
63
64int
65lower (int *a, int *b, int *c, int *d, int len)
66{
67  int value;
68
69  value = *(a - len);
70  value = *(b - len);
71  value = *(c - len);
72  value = *(d - len);
73
74  value = value - *a + 1;
75  return value;
76}
77
78
79char
80char_upper (char *str, int length)
81{
82  char ch;
83  ch = *(str + length);
84
85  return ch;
86}
87
88
89char
90char_lower (char *str, int length)
91{
92  char ch;
93  ch = *(str - length);
94
95  return ch;
96}
97
98
99int
100main (void)
101{
102  if (have_mpx ())
103    {
104      int sa[ARRAY_LENGTH];
105      int sb[ARRAY_LENGTH];
106      int sc[ARRAY_LENGTH];
107      int sd[ARRAY_LENGTH];
108      int *x, *a, *b, *c, *d;
109      char mchar;
110      char hello[] = "Hello";
111
112      x = malloc (sizeof (int) * ARRAY_LENGTH);
113      a = malloc (sizeof (int) * ARRAY_LENGTH);
114      b = malloc (sizeof (int) * ARRAY_LENGTH);
115      c = malloc (sizeof (int) * ARRAY_LENGTH);
116      d = malloc (sizeof (int) * ARRAY_LENGTH);
117
118      *x = upper (sa, sb, sc, sd, 0);  /* bkpt 1.  */
119      *x = lower (a, b, c, d, 0);
120
121      mchar = char_upper (hello, 10);
122      mchar = char_lower (hello, 10);
123
124      free (x);
125      free (a);
126      free (b);
127      free (c);
128      free (d);
129    }
130  return 0;
131}
132