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}]] } {
41 untested "could not run to main"
46 gdb_test_multiple "disassemble upper" "" {
47 -re -wrap "bndldx.*" {
54 # Convenience for returning from an inferior call that causes a BND violation.
56 gdb_test_no_output "set confirm off"
58 # Convenience variable.
60 set bound_reg " = \\\{lbound = $hex, ubound = $hex\\\}.*"
61 set int_braw_reg " = \\\{lbound = 0x0, ubound_raw = 0x0\\\}.*"
62 set bndcfg_reg " = \\\{raw = $hex, config = \\\{base = $hex, reserved = $hex,\
63 preserved = $hex, enabled = $hex\\\}\\\}"
64 set bndstatus_reg " = \\\{raw = $hex, status = \\\{bde = $hex,\
66 set u_fault [multi_line "Program received signal SIGSEGV, Segmentation fault" \
67 "Upper bound violation while accessing address $hex" \
68 "Bounds: \\\[lower = $hex, upper = $hex\\\]"]
71 # Simplify the tests below.
73 proc sanity_check_bndregs {arglist} {
78 gdb_test "p /x $a" "$int_braw_reg"\
83 # Set bnd register to have no access to memory.
85 proc remove_memory_access {reg} {
88 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
90 gdb_test "p /x $reg.lbound = $reg.ubound" "= $hex"\
91 "$reg lower bound set"
92 gdb_test "p /x $reg.ubound = 0" " = 0x0"\
93 "$reg upper bound set"
97 # Prepare convenience variables for bndconfig and status
98 # for posterior comparison.
100 proc prepare_bndcfg_bndstatus {} {
105 gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
106 "bndcfgu should not change"
108 gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
109 "bndstatus should not change"
112 # Compare values set for convenience variables and actual values of bndconfig
113 # and bndstatus registers.
115 proc compare_bndstatus_with_convenience {} {
117 gdb_test "p \$temp_bndcfgu == \$bndcfgu" "= 1"\
118 "bndcfgu compare before and after"
119 gdb_test "p \$temp_bndstatus == \$bndstatus" "= 1"\
120 "bndstatus compare before and after"
123 # Perform an inferior call defined in func.
125 proc perform_a_call {func} {
127 global inf_call_stopped
130 gdb_test "p /x $func" [multi_line "The program being debugged\
131 stopped while in a function called from GDB." \
132 "Evaluation of the expression containing the\
134 ] "inferior call stopped"
137 # Perform an inferior call defined in func.
139 proc check_bound_violation {parm parm_type is_positive} {
141 global u_fault bounds_table
143 set have_bnd_violation 0
144 gdb_test_multiple "continue" "continue to a bnd violation" {
145 -re -wrap "Continuing\." {
146 if { $bounds_table } {
152 -re -wrap "$u_fault.*" {
154 set have_bnd_violation 1
157 if { ! $have_bnd_violation } {
161 set message "access only one position"
162 if {$is_positive == 1} {
163 gdb_test "p (((void *)\$_siginfo._sifields._sigfault.si_addr\
164 - (void*)$parm))/sizeof($parm_type) == 1"\
167 gdb_test "p ((void*)$parm\
168 - (void *)\$_siginfo._sifields._sigfault.si_addr)\
169 /sizeof($parm_type) == 1"\
172 gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
179 # Set up for stopping in the middle of main for calling a function in the
183 gdb_breakpoint [gdb_get_line_number "${break}"]
184 gdb_continue_to_breakpoint "${break}" ".*${break}.*"
188 # default run execution of call should succeed without violations.
190 with_test_prefix "default_run" {
192 gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
193 "store bnd0 register in a convenience variable"
195 gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
196 "default inferior call"
198 gdb_test "p ((\$bnd0.lbound==\$keep_bnd0_value.lbound) &&\
199 (\$bnd0.ubound==\$keep_bnd0_value.ubound))" "= 1" \
200 "bnd register value after and before call"
203 # Consistency: Examine bnd registers values before and after the call.
206 with_test_prefix "verify_default_values" {
208 prepare_bndcfg_bndstatus
210 gdb_breakpoint "*upper"
211 perform_a_call "upper (a, b, c, d, 1)"
213 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
215 compare_bndstatus_with_convenience
217 gdb_test_multiple "continue" "inferior call test" {
218 -re ".*Continuing.\r\n$gdb_prompt " {
219 pass "inferior call performed"
224 # Examine: Cause an upper bound violation changing BND0.
227 with_test_prefix "upper_bnd0" {
229 prepare_bndcfg_bndstatus
231 gdb_breakpoint "*upper"
232 perform_a_call "upper (a, b, c, d, 1)"
234 remove_memory_access "\$bnd0"
236 compare_bndstatus_with_convenience
238 check_bound_violation "a" "int" 1
241 # Examine: Cause an upper bound violation changing BND1.
244 with_test_prefix "upper_bnd1" {
246 prepare_bndcfg_bndstatus
248 gdb_breakpoint "*upper"
249 perform_a_call "upper (a, b, c, d, 1)"
251 remove_memory_access "\$bnd1"
253 compare_bndstatus_with_convenience
255 check_bound_violation "b" "int" 1
258 # Examine: Cause an upper bound violation changing BND2.
261 with_test_prefix "upper_bnd2" {
263 prepare_bndcfg_bndstatus
265 gdb_breakpoint "*upper"
266 perform_a_call "upper (a, b, c, d, 1)"
268 remove_memory_access "\$bnd2"
270 compare_bndstatus_with_convenience
272 check_bound_violation "c" "int" 1
275 # Examine: Cause an upper bound violation changing BND3.
278 with_test_prefix "upper_bnd3" {
279 prepare_bndcfg_bndstatus
281 gdb_breakpoint "*upper"
282 perform_a_call "upper (a, b, c, d, 1)"
284 remove_memory_access "\$bnd3"
286 compare_bndstatus_with_convenience
288 check_bound_violation "d" "int" 1
291 # Examine: Cause a lower bound violation changing BND0.
294 with_test_prefix "lower_bnd0" {
296 prepare_bndcfg_bndstatus
298 gdb_breakpoint "*lower"
299 perform_a_call "lower (a, b, c, d, 1)"
301 remove_memory_access "\$bnd0"
303 compare_bndstatus_with_convenience
305 check_bound_violation "a" "int" 0
308 # Examine: Cause a lower bound violation changing BND1.
311 with_test_prefix "lower_bnd1" {
313 prepare_bndcfg_bndstatus
315 gdb_breakpoint "*lower"
316 perform_a_call "lower (a, b, c, d, 1)"
318 remove_memory_access "\$bnd1"
320 compare_bndstatus_with_convenience
322 check_bound_violation "b" "int" 0
325 # Examine: Cause a lower bound violation changing BND2.
328 with_test_prefix "lower_bnd2" {
330 prepare_bndcfg_bndstatus
332 gdb_breakpoint "*lower"
333 perform_a_call "lower (a, b, c, d, 1)"
335 remove_memory_access "\$bnd2"
337 compare_bndstatus_with_convenience
339 check_bound_violation "c" "int" 0
342 # Examine: Cause a lower bound violation changing BND3.
345 with_test_prefix "lower_bnd3" {
347 prepare_bndcfg_bndstatus
349 gdb_breakpoint "*lower"
350 perform_a_call "lower (a, b, c, d, 1)"
352 remove_memory_access "\$bnd3"
354 compare_bndstatus_with_convenience
356 check_bound_violation "d" "int" 0
359 # Examine: String causing a upper bound violation changing BND0.
362 with_test_prefix "chars_up" {
364 prepare_bndcfg_bndstatus
366 gdb_breakpoint "*char_upper"
367 perform_a_call "char_upper (hello, 1)"
369 remove_memory_access "\$bnd0"
371 compare_bndstatus_with_convenience
373 check_bound_violation "str" "char" 1
377 # Examine: String causing an lower bound violation changing BND0.
380 with_test_prefix "chars_low" {
382 prepare_bndcfg_bndstatus
384 gdb_breakpoint "*char_lower"
385 perform_a_call "char_lower (hello, 1)"
387 remove_memory_access "\$bnd0"
389 compare_bndstatus_with_convenience
391 check_bound_violation "str" "char" 0
394 # Examine: String causing an lower bound violation changing BND0.
397 with_test_prefix "chars_low_adhoc_parm" {
399 prepare_bndcfg_bndstatus
401 gdb_breakpoint "*char_lower"
402 perform_a_call "char_lower (\"tryme\", 1)"
404 remove_memory_access "\$bnd0"
406 compare_bndstatus_with_convenience
408 check_bound_violation "str" "char" 0