1 # Copyright (C) 2017 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 if { ![istarget i?86-*-*] && ![istarget x86_64-*-* ] } {
18 untested "skipping x86 MPX tests."
24 set comp_flags "-mmpx -fcheck-pointer-bounds -I${srcdir}/../nat"
26 if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
27 [list debug additional_flags=${comp_flags}]] } {
32 untested "could not run to main"
36 set test "check whether processor supports MPX"
37 gdb_test_multiple "print have_mpx ()" $test {
38 -re ".*= 1\r\n$gdb_prompt " {
41 -re ".*= 0\r\n$gdb_prompt " {
43 untested "processor does not support MPX; skipping tests"
48 # Convenience for returning from an inferior call that causes a BND violation.
50 gdb_test_no_output "set confirm off"
52 # Convenience variable.
54 set bound_reg " = \\\{lbound = $hex, ubound = $hex\\\}.*"
55 set int_braw_reg " = \\\{lbound = 0x0, ubound_raw = 0x0\\\}.*"
56 set bndcfg_reg " = \\\{raw = $hex, config = \\\{base = $hex, reserved = $hex,\
57 preserved = $hex, enabled = $hex\\\}\\\}"
58 set bndstatus_reg " = \\\{raw = $hex, status = \\\{bde = $hex,\
60 set u_fault [multi_line "Program received signal SIGSEGV, Segmentation fault" \
61 "Upper bound violation while accessing address $hex" \
62 "Bounds: \\\[lower = $hex, upper = $hex\\\]"]
65 # Simplify the tests below.
67 proc sanity_check_bndregs {arglist} {
72 gdb_test "p /x $a" "$int_braw_reg"\
77 # Set bnd register to have no access to memory.
79 proc remove_memory_access {reg} {
82 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
84 gdb_test "p /x $reg.lbound = $reg.ubound" "= $hex"\
85 "$reg lower bound set"
86 gdb_test "p /x $reg.ubound = 0" " = 0x0"\
87 "$reg upper bound set"
91 # Prepare convenience variables for bndconfig and status
92 # for posterior comparison.
94 proc prepare_bndcfg_bndstatus {} {
99 gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
100 "bndcfgu should not change"
102 gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
103 "bndstatus should not change"
106 # Compare values set for convenience variables and actual values of bndconfig
107 # and bndstatus registers.
109 proc compare_bndstatus_with_convenience {} {
111 gdb_test "p \$temp_bndcfgu == \$bndcfgu" "= 1"\
112 "bndcfgu compare before and after"
113 gdb_test "p \$temp_bndstatus == \$bndstatus" "= 1"\
114 "bndstatus compare before and after"
117 # Perform an inferior call defined in func.
119 proc perform_a_call {func} {
121 global inf_call_stopped
124 gdb_test "p /x $func" [multi_line "The program being debugged\
125 stopped while in a function called from GDB." \
126 "Evaluation of the expression containing the\
128 ] "inferior call stopped"
131 # Perform an inferior call defined in func.
133 proc check_bound_violation {parm parm_type is_positive} {
137 gdb_test "continue" "$u_fault.*" "continue to a bnd violation"
139 set message "access only one position"
140 if {$is_positive == 1} {
141 gdb_test "p (((void *)\$_siginfo._sifields._sigfault.si_addr\
142 - (void*)$parm))/sizeof($parm_type) == 1"\
145 gdb_test "p ((void*)$parm\
146 - (void *)\$_siginfo._sifields._sigfault.si_addr)\
147 /sizeof($parm_type) == 1"\
150 gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
157 # Set up for stopping in the middle of main for calling a function in the
161 gdb_breakpoint [gdb_get_line_number "${break}"]
162 gdb_continue_to_breakpoint "${break}" ".*${break}.*"
166 # default run execution of call should succeed without violations.
168 with_test_prefix "default_run" {
170 gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
171 "store bnd0 register in a convenience variable"
173 gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
174 "default inferior call"
176 gdb_test "p ((\$bnd0.lbound==\$keep_bnd0_value.lbound) &&\
177 (\$bnd0.ubound==\$keep_bnd0_value.ubound))" "= 1" \
178 "bnd register value after and before call"
181 # Consistency: Examine bnd registers values before and after the call.
184 with_test_prefix "verify_default_values" {
186 prepare_bndcfg_bndstatus
188 gdb_breakpoint "*upper"
189 perform_a_call "upper (a, b, c, d, 1)"
191 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
193 compare_bndstatus_with_convenience
195 gdb_test_multiple "continue" "inferior call test" {
196 -re ".*Continuing.\r\n$gdb_prompt " {
197 pass "inferior call performed"
202 # Examine: Cause an upper bound violation changing BND0.
205 with_test_prefix "upper_bnd0" {
207 prepare_bndcfg_bndstatus
209 gdb_breakpoint "*upper"
210 perform_a_call "upper (a, b, c, d, 1)"
212 remove_memory_access "\$bnd0"
214 compare_bndstatus_with_convenience
216 check_bound_violation "a" "int" 1
219 # Examine: Cause an upper bound violation changing BND1.
222 with_test_prefix "upper_bnd1" {
224 prepare_bndcfg_bndstatus
226 gdb_breakpoint "*upper"
227 perform_a_call "upper (a, b, c, d, 1)"
229 remove_memory_access "\$bnd1"
231 compare_bndstatus_with_convenience
233 check_bound_violation "b" "int" 1
236 # Examine: Cause an upper bound violation changing BND2.
239 with_test_prefix "upper_bnd2" {
241 prepare_bndcfg_bndstatus
243 gdb_breakpoint "*upper"
244 perform_a_call "upper (a, b, c, d, 1)"
246 remove_memory_access "\$bnd2"
248 compare_bndstatus_with_convenience
250 check_bound_violation "c" "int" 1
253 # Examine: Cause an upper bound violation changing BND3.
256 with_test_prefix "upper_bnd3" {
257 prepare_bndcfg_bndstatus
259 gdb_breakpoint "*upper"
260 perform_a_call "upper (a, b, c, d, 1)"
262 remove_memory_access "\$bnd3"
264 compare_bndstatus_with_convenience
266 check_bound_violation "d" "int" 1
269 # Examine: Cause a lower bound violation changing BND0.
272 with_test_prefix "lower_bnd0" {
274 prepare_bndcfg_bndstatus
276 gdb_breakpoint "*lower"
277 perform_a_call "lower (a, b, c, d, 1)"
279 remove_memory_access "\$bnd0"
281 compare_bndstatus_with_convenience
283 check_bound_violation "a" "int" 0
286 # Examine: Cause a lower bound violation changing BND1.
289 with_test_prefix "lower_bnd1" {
291 prepare_bndcfg_bndstatus
293 gdb_breakpoint "*lower"
294 perform_a_call "lower (a, b, c, d, 1)"
296 remove_memory_access "\$bnd1"
298 compare_bndstatus_with_convenience
300 check_bound_violation "b" "int" 0
303 # Examine: Cause a lower bound violation changing BND2.
306 with_test_prefix "lower_bnd2" {
308 prepare_bndcfg_bndstatus
310 gdb_breakpoint "*lower"
311 perform_a_call "lower (a, b, c, d, 1)"
313 remove_memory_access "\$bnd2"
315 compare_bndstatus_with_convenience
317 check_bound_violation "c" "int" 0
320 # Examine: Cause a lower bound violation changing BND3.
323 with_test_prefix "lower_bnd3" {
325 prepare_bndcfg_bndstatus
327 gdb_breakpoint "*lower"
328 perform_a_call "lower (a, b, c, d, 1)"
330 remove_memory_access "\$bnd3"
332 compare_bndstatus_with_convenience
334 check_bound_violation "d" "int" 0
337 # Examine: String causing a upper bound violation changing BND0.
340 with_test_prefix "chars_up" {
342 prepare_bndcfg_bndstatus
344 gdb_breakpoint "*char_upper"
345 perform_a_call "char_upper (hello, 1)"
347 remove_memory_access "\$bnd0"
349 compare_bndstatus_with_convenience
351 check_bound_violation "str" "char" 1
355 # Examine: String causing an lower bound violation changing BND0.
358 with_test_prefix "chars_low" {
360 prepare_bndcfg_bndstatus
362 gdb_breakpoint "*char_lower"
363 perform_a_call "char_lower (hello, 1)"
365 remove_memory_access "\$bnd0"
367 compare_bndstatus_with_convenience
369 check_bound_violation "str" "char" 0
372 # Examine: String causing an lower bound violation changing BND0.
375 with_test_prefix "chars_low_adhoc_parm" {
377 prepare_bndcfg_bndstatus
379 gdb_breakpoint "*char_lower"
380 perform_a_call "char_lower (\"tryme\", 1)"
382 remove_memory_access "\$bnd0"
384 compare_bndstatus_with_convenience
386 check_bound_violation "str" "char" 0