1 # Copyright (C) 2017-2024 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 require {is_any_target "i?86-*-*" "x86_64-*-*"}
21 require supports_mpx_check_pointer_bounds have_mpx
23 set comp_flags "-mmpx -fcheck-pointer-bounds -I${srcdir}/../nat"
25 if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
26 [list debug additional_flags=${comp_flags}]] } {
35 gdb_test_multiple "disassemble upper" "" {
36 -re -wrap "bndldx.*" {
43 # Convenience for returning from an inferior call that causes a BND violation.
45 gdb_test_no_output "set confirm off"
47 # Convenience variable.
49 set bound_reg " = \\\{lbound = $hex, ubound = $hex\\\}.*"
50 set int_braw_reg " = \\\{lbound = 0x0, ubound_raw = 0x0\\\}.*"
51 set bndcfg_reg " = \\\{raw = $hex, config = \\\{base = $hex, reserved = $hex,\
52 preserved = $hex, enabled = $hex\\\}\\\}"
53 set bndstatus_reg " = \\\{raw = $hex, status = \\\{bde = $hex,\
55 set u_fault [multi_line "Program received signal SIGSEGV, Segmentation fault" \
56 "Upper bound violation while accessing address $hex" \
57 "Bounds: \\\[lower = $hex, upper = $hex\\\]"]
60 # Simplify the tests below.
62 proc sanity_check_bndregs {arglist} {
67 gdb_test "p /x $a" "$int_braw_reg"\
72 # Set bnd register to have no access to memory.
74 proc remove_memory_access {reg} {
77 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
79 gdb_test "p /x $reg.lbound = $reg.ubound" "= $hex"\
80 "$reg lower bound set"
81 gdb_test "p /x $reg.ubound = 0" " = 0x0"\
82 "$reg upper bound set"
86 # Prepare convenience variables for bndconfig and status
87 # for posterior comparison.
89 proc prepare_bndcfg_bndstatus {} {
94 gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
95 "bndcfgu should not change"
97 gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
98 "bndstatus should not change"
101 # Compare values set for convenience variables and actual values of bndconfig
102 # and bndstatus registers.
104 proc compare_bndstatus_with_convenience {} {
106 gdb_test "p \$temp_bndcfgu == \$bndcfgu" "= 1"\
107 "bndcfgu compare before and after"
108 gdb_test "p \$temp_bndstatus == \$bndstatus" "= 1"\
109 "bndstatus compare before and after"
112 # Perform an inferior call defined in func.
114 proc perform_a_call {func} {
116 global inf_call_stopped
119 gdb_test "p /x $func" [multi_line "The program being debugged\
120 stopped while in a function called from GDB." \
121 "Evaluation of the expression containing the\
123 ] "inferior call stopped"
126 # Perform an inferior call defined in func.
128 proc check_bound_violation {parm parm_type is_positive} {
130 global u_fault bounds_table
132 set have_bnd_violation 0
133 gdb_test_multiple "continue" "continue to a bnd violation" {
134 -re -wrap "Continuing\." {
135 if { $bounds_table } {
141 -re -wrap "$u_fault.*" {
143 set have_bnd_violation 1
146 if { ! $have_bnd_violation } {
150 set message "access only one position"
151 if {$is_positive == 1} {
152 gdb_test "p (((void *)\$_siginfo._sifields._sigfault.si_addr\
153 - (void*)$parm))/sizeof($parm_type) == 1"\
156 gdb_test "p ((void*)$parm\
157 - (void *)\$_siginfo._sifields._sigfault.si_addr)\
158 /sizeof($parm_type) == 1"\
161 gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
168 # Set up for stopping in the middle of main for calling a function in the
172 gdb_breakpoint [gdb_get_line_number "${break}"]
173 gdb_continue_to_breakpoint "${break}" ".*${break}.*"
177 # default run execution of call should succeed without violations.
179 with_test_prefix "default_run" {
181 gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
182 "store bnd0 register in a convenience variable"
184 gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
185 "default inferior call"
187 gdb_test "p ((\$bnd0.lbound==\$keep_bnd0_value.lbound) &&\
188 (\$bnd0.ubound==\$keep_bnd0_value.ubound))" "= 1" \
189 "bnd register value after and before call"
192 # Consistency: Examine bnd registers values before and after the call.
195 with_test_prefix "verify_default_values" {
197 prepare_bndcfg_bndstatus
199 gdb_breakpoint "*upper"
200 perform_a_call "upper (a, b, c, d, 1)"
202 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
204 compare_bndstatus_with_convenience
206 gdb_test_multiple "continue" "inferior call test" {
207 -re ".*Continuing.\r\n$gdb_prompt " {
208 pass "inferior call performed"
213 # Examine: Cause an upper bound violation changing BND0.
216 with_test_prefix "upper_bnd0" {
218 prepare_bndcfg_bndstatus
220 gdb_breakpoint "*upper"
221 perform_a_call "upper (a, b, c, d, 1)"
223 remove_memory_access "\$bnd0"
225 compare_bndstatus_with_convenience
227 check_bound_violation "a" "int" 1
230 # Examine: Cause an upper bound violation changing BND1.
233 with_test_prefix "upper_bnd1" {
235 prepare_bndcfg_bndstatus
237 gdb_breakpoint "*upper"
238 perform_a_call "upper (a, b, c, d, 1)"
240 remove_memory_access "\$bnd1"
242 compare_bndstatus_with_convenience
244 check_bound_violation "b" "int" 1
247 # Examine: Cause an upper bound violation changing BND2.
250 with_test_prefix "upper_bnd2" {
252 prepare_bndcfg_bndstatus
254 gdb_breakpoint "*upper"
255 perform_a_call "upper (a, b, c, d, 1)"
257 remove_memory_access "\$bnd2"
259 compare_bndstatus_with_convenience
261 check_bound_violation "c" "int" 1
264 # Examine: Cause an upper bound violation changing BND3.
267 with_test_prefix "upper_bnd3" {
268 prepare_bndcfg_bndstatus
270 gdb_breakpoint "*upper"
271 perform_a_call "upper (a, b, c, d, 1)"
273 remove_memory_access "\$bnd3"
275 compare_bndstatus_with_convenience
277 check_bound_violation "d" "int" 1
280 # Examine: Cause a lower bound violation changing BND0.
283 with_test_prefix "lower_bnd0" {
285 prepare_bndcfg_bndstatus
287 gdb_breakpoint "*lower"
288 perform_a_call "lower (a, b, c, d, 1)"
290 remove_memory_access "\$bnd0"
292 compare_bndstatus_with_convenience
294 check_bound_violation "a" "int" 0
297 # Examine: Cause a lower bound violation changing BND1.
300 with_test_prefix "lower_bnd1" {
302 prepare_bndcfg_bndstatus
304 gdb_breakpoint "*lower"
305 perform_a_call "lower (a, b, c, d, 1)"
307 remove_memory_access "\$bnd1"
309 compare_bndstatus_with_convenience
311 check_bound_violation "b" "int" 0
314 # Examine: Cause a lower bound violation changing BND2.
317 with_test_prefix "lower_bnd2" {
319 prepare_bndcfg_bndstatus
321 gdb_breakpoint "*lower"
322 perform_a_call "lower (a, b, c, d, 1)"
324 remove_memory_access "\$bnd2"
326 compare_bndstatus_with_convenience
328 check_bound_violation "c" "int" 0
331 # Examine: Cause a lower bound violation changing BND3.
334 with_test_prefix "lower_bnd3" {
336 prepare_bndcfg_bndstatus
338 gdb_breakpoint "*lower"
339 perform_a_call "lower (a, b, c, d, 1)"
341 remove_memory_access "\$bnd3"
343 compare_bndstatus_with_convenience
345 check_bound_violation "d" "int" 0
348 # Examine: String causing a upper bound violation changing BND0.
351 with_test_prefix "chars_up" {
353 prepare_bndcfg_bndstatus
355 gdb_breakpoint "*char_upper"
356 perform_a_call "char_upper (hello, 1)"
358 remove_memory_access "\$bnd0"
360 compare_bndstatus_with_convenience
362 check_bound_violation "str" "char" 1
366 # Examine: String causing an lower bound violation changing BND0.
369 with_test_prefix "chars_low" {
371 prepare_bndcfg_bndstatus
373 gdb_breakpoint "*char_lower"
374 perform_a_call "char_lower (hello, 1)"
376 remove_memory_access "\$bnd0"
378 compare_bndstatus_with_convenience
380 check_bound_violation "str" "char" 0
383 # Examine: String causing an lower bound violation changing BND0.
386 with_test_prefix "chars_low_adhoc_parm" {
388 prepare_bndcfg_bndstatus
390 gdb_breakpoint "*char_lower"
391 perform_a_call "char_lower (\"tryme\", 1)"
393 remove_memory_access "\$bnd0"
395 compare_bndstatus_with_convenience
397 check_bound_violation "str" "char" 0