1 # Copyright (C) 2017-2022 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] } {
29 unsupported "processor does not support MPX"
33 set comp_flags "-mmpx -fcheck-pointer-bounds -I${srcdir}/../nat"
35 if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
36 [list debug additional_flags=${comp_flags}]] } {
45 gdb_test_multiple "disassemble upper" "" {
46 -re -wrap "bndldx.*" {
53 # Convenience for returning from an inferior call that causes a BND violation.
55 gdb_test_no_output "set confirm off"
57 # Convenience variable.
59 set bound_reg " = \\\{lbound = $hex, ubound = $hex\\\}.*"
60 set int_braw_reg " = \\\{lbound = 0x0, ubound_raw = 0x0\\\}.*"
61 set bndcfg_reg " = \\\{raw = $hex, config = \\\{base = $hex, reserved = $hex,\
62 preserved = $hex, enabled = $hex\\\}\\\}"
63 set bndstatus_reg " = \\\{raw = $hex, status = \\\{bde = $hex,\
65 set u_fault [multi_line "Program received signal SIGSEGV, Segmentation fault" \
66 "Upper bound violation while accessing address $hex" \
67 "Bounds: \\\[lower = $hex, upper = $hex\\\]"]
70 # Simplify the tests below.
72 proc sanity_check_bndregs {arglist} {
77 gdb_test "p /x $a" "$int_braw_reg"\
82 # Set bnd register to have no access to memory.
84 proc remove_memory_access {reg} {
87 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
89 gdb_test "p /x $reg.lbound = $reg.ubound" "= $hex"\
90 "$reg lower bound set"
91 gdb_test "p /x $reg.ubound = 0" " = 0x0"\
92 "$reg upper bound set"
96 # Prepare convenience variables for bndconfig and status
97 # for posterior comparison.
99 proc prepare_bndcfg_bndstatus {} {
104 gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
105 "bndcfgu should not change"
107 gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
108 "bndstatus should not change"
111 # Compare values set for convenience variables and actual values of bndconfig
112 # and bndstatus registers.
114 proc compare_bndstatus_with_convenience {} {
116 gdb_test "p \$temp_bndcfgu == \$bndcfgu" "= 1"\
117 "bndcfgu compare before and after"
118 gdb_test "p \$temp_bndstatus == \$bndstatus" "= 1"\
119 "bndstatus compare before and after"
122 # Perform an inferior call defined in func.
124 proc perform_a_call {func} {
126 global inf_call_stopped
129 gdb_test "p /x $func" [multi_line "The program being debugged\
130 stopped while in a function called from GDB." \
131 "Evaluation of the expression containing the\
133 ] "inferior call stopped"
136 # Perform an inferior call defined in func.
138 proc check_bound_violation {parm parm_type is_positive} {
140 global u_fault bounds_table
142 set have_bnd_violation 0
143 gdb_test_multiple "continue" "continue to a bnd violation" {
144 -re -wrap "Continuing\." {
145 if { $bounds_table } {
151 -re -wrap "$u_fault.*" {
153 set have_bnd_violation 1
156 if { ! $have_bnd_violation } {
160 set message "access only one position"
161 if {$is_positive == 1} {
162 gdb_test "p (((void *)\$_siginfo._sifields._sigfault.si_addr\
163 - (void*)$parm))/sizeof($parm_type) == 1"\
166 gdb_test "p ((void*)$parm\
167 - (void *)\$_siginfo._sifields._sigfault.si_addr)\
168 /sizeof($parm_type) == 1"\
171 gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
178 # Set up for stopping in the middle of main for calling a function in the
182 gdb_breakpoint [gdb_get_line_number "${break}"]
183 gdb_continue_to_breakpoint "${break}" ".*${break}.*"
187 # default run execution of call should succeed without violations.
189 with_test_prefix "default_run" {
191 gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
192 "store bnd0 register in a convenience variable"
194 gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
195 "default inferior call"
197 gdb_test "p ((\$bnd0.lbound==\$keep_bnd0_value.lbound) &&\
198 (\$bnd0.ubound==\$keep_bnd0_value.ubound))" "= 1" \
199 "bnd register value after and before call"
202 # Consistency: Examine bnd registers values before and after the call.
205 with_test_prefix "verify_default_values" {
207 prepare_bndcfg_bndstatus
209 gdb_breakpoint "*upper"
210 perform_a_call "upper (a, b, c, d, 1)"
212 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
214 compare_bndstatus_with_convenience
216 gdb_test_multiple "continue" "inferior call test" {
217 -re ".*Continuing.\r\n$gdb_prompt " {
218 pass "inferior call performed"
223 # Examine: Cause an upper bound violation changing BND0.
226 with_test_prefix "upper_bnd0" {
228 prepare_bndcfg_bndstatus
230 gdb_breakpoint "*upper"
231 perform_a_call "upper (a, b, c, d, 1)"
233 remove_memory_access "\$bnd0"
235 compare_bndstatus_with_convenience
237 check_bound_violation "a" "int" 1
240 # Examine: Cause an upper bound violation changing BND1.
243 with_test_prefix "upper_bnd1" {
245 prepare_bndcfg_bndstatus
247 gdb_breakpoint "*upper"
248 perform_a_call "upper (a, b, c, d, 1)"
250 remove_memory_access "\$bnd1"
252 compare_bndstatus_with_convenience
254 check_bound_violation "b" "int" 1
257 # Examine: Cause an upper bound violation changing BND2.
260 with_test_prefix "upper_bnd2" {
262 prepare_bndcfg_bndstatus
264 gdb_breakpoint "*upper"
265 perform_a_call "upper (a, b, c, d, 1)"
267 remove_memory_access "\$bnd2"
269 compare_bndstatus_with_convenience
271 check_bound_violation "c" "int" 1
274 # Examine: Cause an upper bound violation changing BND3.
277 with_test_prefix "upper_bnd3" {
278 prepare_bndcfg_bndstatus
280 gdb_breakpoint "*upper"
281 perform_a_call "upper (a, b, c, d, 1)"
283 remove_memory_access "\$bnd3"
285 compare_bndstatus_with_convenience
287 check_bound_violation "d" "int" 1
290 # Examine: Cause a lower bound violation changing BND0.
293 with_test_prefix "lower_bnd0" {
295 prepare_bndcfg_bndstatus
297 gdb_breakpoint "*lower"
298 perform_a_call "lower (a, b, c, d, 1)"
300 remove_memory_access "\$bnd0"
302 compare_bndstatus_with_convenience
304 check_bound_violation "a" "int" 0
307 # Examine: Cause a lower bound violation changing BND1.
310 with_test_prefix "lower_bnd1" {
312 prepare_bndcfg_bndstatus
314 gdb_breakpoint "*lower"
315 perform_a_call "lower (a, b, c, d, 1)"
317 remove_memory_access "\$bnd1"
319 compare_bndstatus_with_convenience
321 check_bound_violation "b" "int" 0
324 # Examine: Cause a lower bound violation changing BND2.
327 with_test_prefix "lower_bnd2" {
329 prepare_bndcfg_bndstatus
331 gdb_breakpoint "*lower"
332 perform_a_call "lower (a, b, c, d, 1)"
334 remove_memory_access "\$bnd2"
336 compare_bndstatus_with_convenience
338 check_bound_violation "c" "int" 0
341 # Examine: Cause a lower bound violation changing BND3.
344 with_test_prefix "lower_bnd3" {
346 prepare_bndcfg_bndstatus
348 gdb_breakpoint "*lower"
349 perform_a_call "lower (a, b, c, d, 1)"
351 remove_memory_access "\$bnd3"
353 compare_bndstatus_with_convenience
355 check_bound_violation "d" "int" 0
358 # Examine: String causing a upper bound violation changing BND0.
361 with_test_prefix "chars_up" {
363 prepare_bndcfg_bndstatus
365 gdb_breakpoint "*char_upper"
366 perform_a_call "char_upper (hello, 1)"
368 remove_memory_access "\$bnd0"
370 compare_bndstatus_with_convenience
372 check_bound_violation "str" "char" 1
376 # Examine: String causing an lower bound violation changing BND0.
379 with_test_prefix "chars_low" {
381 prepare_bndcfg_bndstatus
383 gdb_breakpoint "*char_lower"
384 perform_a_call "char_lower (hello, 1)"
386 remove_memory_access "\$bnd0"
388 compare_bndstatus_with_convenience
390 check_bound_violation "str" "char" 0
393 # Examine: String causing an lower bound violation changing BND0.
396 with_test_prefix "chars_low_adhoc_parm" {
398 prepare_bndcfg_bndstatus
400 gdb_breakpoint "*char_lower"
401 perform_a_call "char_lower (\"tryme\", 1)"
403 remove_memory_access "\$bnd0"
405 compare_bndstatus_with_convenience
407 check_bound_violation "str" "char" 0