1 # Copyright (C) 2017-2021 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 if { ![supports_mpx_check_pointer_bounds] } {
28 set comp_flags "-mmpx -fcheck-pointer-bounds -I${srcdir}/../nat"
30 if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
31 [list debug additional_flags=${comp_flags}]] } {
36 untested "could not run to main"
40 set test "check whether processor supports MPX"
41 gdb_test_multiple "print have_mpx ()" $test {
42 -re ".*= 1\r\n$gdb_prompt " {
45 -re ".*= 0\r\n$gdb_prompt " {
47 untested "processor does not support MPX; skipping tests"
53 gdb_test_multiple "disassemble upper" "" {
54 -re -wrap "bndldx.*" {
61 # Convenience for returning from an inferior call that causes a BND violation.
63 gdb_test_no_output "set confirm off"
65 # Convenience variable.
67 set bound_reg " = \\\{lbound = $hex, ubound = $hex\\\}.*"
68 set int_braw_reg " = \\\{lbound = 0x0, ubound_raw = 0x0\\\}.*"
69 set bndcfg_reg " = \\\{raw = $hex, config = \\\{base = $hex, reserved = $hex,\
70 preserved = $hex, enabled = $hex\\\}\\\}"
71 set bndstatus_reg " = \\\{raw = $hex, status = \\\{bde = $hex,\
73 set u_fault [multi_line "Program received signal SIGSEGV, Segmentation fault" \
74 "Upper bound violation while accessing address $hex" \
75 "Bounds: \\\[lower = $hex, upper = $hex\\\]"]
78 # Simplify the tests below.
80 proc sanity_check_bndregs {arglist} {
85 gdb_test "p /x $a" "$int_braw_reg"\
90 # Set bnd register to have no access to memory.
92 proc remove_memory_access {reg} {
95 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
97 gdb_test "p /x $reg.lbound = $reg.ubound" "= $hex"\
98 "$reg lower bound set"
99 gdb_test "p /x $reg.ubound = 0" " = 0x0"\
100 "$reg upper bound set"
104 # Prepare convenience variables for bndconfig and status
105 # for posterior comparison.
107 proc prepare_bndcfg_bndstatus {} {
112 gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
113 "bndcfgu should not change"
115 gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
116 "bndstatus should not change"
119 # Compare values set for convenience variables and actual values of bndconfig
120 # and bndstatus registers.
122 proc compare_bndstatus_with_convenience {} {
124 gdb_test "p \$temp_bndcfgu == \$bndcfgu" "= 1"\
125 "bndcfgu compare before and after"
126 gdb_test "p \$temp_bndstatus == \$bndstatus" "= 1"\
127 "bndstatus compare before and after"
130 # Perform an inferior call defined in func.
132 proc perform_a_call {func} {
134 global inf_call_stopped
137 gdb_test "p /x $func" [multi_line "The program being debugged\
138 stopped while in a function called from GDB." \
139 "Evaluation of the expression containing the\
141 ] "inferior call stopped"
144 # Perform an inferior call defined in func.
146 proc check_bound_violation {parm parm_type is_positive} {
148 global u_fault bounds_table
150 set have_bnd_violation 0
151 gdb_test_multiple "continue" "continue to a bnd violation" {
152 -re -wrap "Continuing\." {
153 if { $bounds_table } {
159 -re -wrap "$u_fault.*" {
161 set have_bnd_violation 1
164 if { ! $have_bnd_violation } {
168 set message "access only one position"
169 if {$is_positive == 1} {
170 gdb_test "p (((void *)\$_siginfo._sifields._sigfault.si_addr\
171 - (void*)$parm))/sizeof($parm_type) == 1"\
174 gdb_test "p ((void*)$parm\
175 - (void *)\$_siginfo._sifields._sigfault.si_addr)\
176 /sizeof($parm_type) == 1"\
179 gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
186 # Set up for stopping in the middle of main for calling a function in the
190 gdb_breakpoint [gdb_get_line_number "${break}"]
191 gdb_continue_to_breakpoint "${break}" ".*${break}.*"
195 # default run execution of call should succeed without violations.
197 with_test_prefix "default_run" {
199 gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
200 "store bnd0 register in a convenience variable"
202 gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
203 "default inferior call"
205 gdb_test "p ((\$bnd0.lbound==\$keep_bnd0_value.lbound) &&\
206 (\$bnd0.ubound==\$keep_bnd0_value.ubound))" "= 1" \
207 "bnd register value after and before call"
210 # Consistency: Examine bnd registers values before and after the call.
213 with_test_prefix "verify_default_values" {
215 prepare_bndcfg_bndstatus
217 gdb_breakpoint "*upper"
218 perform_a_call "upper (a, b, c, d, 1)"
220 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
222 compare_bndstatus_with_convenience
224 gdb_test_multiple "continue" "inferior call test" {
225 -re ".*Continuing.\r\n$gdb_prompt " {
226 pass "inferior call performed"
231 # Examine: Cause an upper bound violation changing BND0.
234 with_test_prefix "upper_bnd0" {
236 prepare_bndcfg_bndstatus
238 gdb_breakpoint "*upper"
239 perform_a_call "upper (a, b, c, d, 1)"
241 remove_memory_access "\$bnd0"
243 compare_bndstatus_with_convenience
245 check_bound_violation "a" "int" 1
248 # Examine: Cause an upper bound violation changing BND1.
251 with_test_prefix "upper_bnd1" {
253 prepare_bndcfg_bndstatus
255 gdb_breakpoint "*upper"
256 perform_a_call "upper (a, b, c, d, 1)"
258 remove_memory_access "\$bnd1"
260 compare_bndstatus_with_convenience
262 check_bound_violation "b" "int" 1
265 # Examine: Cause an upper bound violation changing BND2.
268 with_test_prefix "upper_bnd2" {
270 prepare_bndcfg_bndstatus
272 gdb_breakpoint "*upper"
273 perform_a_call "upper (a, b, c, d, 1)"
275 remove_memory_access "\$bnd2"
277 compare_bndstatus_with_convenience
279 check_bound_violation "c" "int" 1
282 # Examine: Cause an upper bound violation changing BND3.
285 with_test_prefix "upper_bnd3" {
286 prepare_bndcfg_bndstatus
288 gdb_breakpoint "*upper"
289 perform_a_call "upper (a, b, c, d, 1)"
291 remove_memory_access "\$bnd3"
293 compare_bndstatus_with_convenience
295 check_bound_violation "d" "int" 1
298 # Examine: Cause a lower bound violation changing BND0.
301 with_test_prefix "lower_bnd0" {
303 prepare_bndcfg_bndstatus
305 gdb_breakpoint "*lower"
306 perform_a_call "lower (a, b, c, d, 1)"
308 remove_memory_access "\$bnd0"
310 compare_bndstatus_with_convenience
312 check_bound_violation "a" "int" 0
315 # Examine: Cause a lower bound violation changing BND1.
318 with_test_prefix "lower_bnd1" {
320 prepare_bndcfg_bndstatus
322 gdb_breakpoint "*lower"
323 perform_a_call "lower (a, b, c, d, 1)"
325 remove_memory_access "\$bnd1"
327 compare_bndstatus_with_convenience
329 check_bound_violation "b" "int" 0
332 # Examine: Cause a lower bound violation changing BND2.
335 with_test_prefix "lower_bnd2" {
337 prepare_bndcfg_bndstatus
339 gdb_breakpoint "*lower"
340 perform_a_call "lower (a, b, c, d, 1)"
342 remove_memory_access "\$bnd2"
344 compare_bndstatus_with_convenience
346 check_bound_violation "c" "int" 0
349 # Examine: Cause a lower bound violation changing BND3.
352 with_test_prefix "lower_bnd3" {
354 prepare_bndcfg_bndstatus
356 gdb_breakpoint "*lower"
357 perform_a_call "lower (a, b, c, d, 1)"
359 remove_memory_access "\$bnd3"
361 compare_bndstatus_with_convenience
363 check_bound_violation "d" "int" 0
366 # Examine: String causing a upper bound violation changing BND0.
369 with_test_prefix "chars_up" {
371 prepare_bndcfg_bndstatus
373 gdb_breakpoint "*char_upper"
374 perform_a_call "char_upper (hello, 1)"
376 remove_memory_access "\$bnd0"
378 compare_bndstatus_with_convenience
380 check_bound_violation "str" "char" 1
384 # Examine: String causing an lower bound violation changing BND0.
387 with_test_prefix "chars_low" {
389 prepare_bndcfg_bndstatus
391 gdb_breakpoint "*char_lower"
392 perform_a_call "char_lower (hello, 1)"
394 remove_memory_access "\$bnd0"
396 compare_bndstatus_with_convenience
398 check_bound_violation "str" "char" 0
401 # Examine: String causing an lower bound violation changing BND0.
404 with_test_prefix "chars_low_adhoc_parm" {
406 prepare_bndcfg_bndstatus
408 gdb_breakpoint "*char_lower"
409 perform_a_call "char_lower (\"tryme\", 1)"
411 remove_memory_access "\$bnd0"
413 compare_bndstatus_with_convenience
415 check_bound_violation "str" "char" 0