1 # Copyright (C) 2017-2020 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"
52 # Convenience for returning from an inferior call that causes a BND violation.
54 gdb_test_no_output "set confirm off"
56 # Convenience variable.
58 set bound_reg " = \\\{lbound = $hex, ubound = $hex\\\}.*"
59 set int_braw_reg " = \\\{lbound = 0x0, ubound_raw = 0x0\\\}.*"
60 set bndcfg_reg " = \\\{raw = $hex, config = \\\{base = $hex, reserved = $hex,\
61 preserved = $hex, enabled = $hex\\\}\\\}"
62 set bndstatus_reg " = \\\{raw = $hex, status = \\\{bde = $hex,\
64 set u_fault [multi_line "Program received signal SIGSEGV, Segmentation fault" \
65 "Upper bound violation while accessing address $hex" \
66 "Bounds: \\\[lower = $hex, upper = $hex\\\]"]
69 # Simplify the tests below.
71 proc sanity_check_bndregs {arglist} {
76 gdb_test "p /x $a" "$int_braw_reg"\
81 # Set bnd register to have no access to memory.
83 proc remove_memory_access {reg} {
86 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
88 gdb_test "p /x $reg.lbound = $reg.ubound" "= $hex"\
89 "$reg lower bound set"
90 gdb_test "p /x $reg.ubound = 0" " = 0x0"\
91 "$reg upper bound set"
95 # Prepare convenience variables for bndconfig and status
96 # for posterior comparison.
98 proc prepare_bndcfg_bndstatus {} {
103 gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
104 "bndcfgu should not change"
106 gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
107 "bndstatus should not change"
110 # Compare values set for convenience variables and actual values of bndconfig
111 # and bndstatus registers.
113 proc compare_bndstatus_with_convenience {} {
115 gdb_test "p \$temp_bndcfgu == \$bndcfgu" "= 1"\
116 "bndcfgu compare before and after"
117 gdb_test "p \$temp_bndstatus == \$bndstatus" "= 1"\
118 "bndstatus compare before and after"
121 # Perform an inferior call defined in func.
123 proc perform_a_call {func} {
125 global inf_call_stopped
128 gdb_test "p /x $func" [multi_line "The program being debugged\
129 stopped while in a function called from GDB." \
130 "Evaluation of the expression containing the\
132 ] "inferior call stopped"
135 # Perform an inferior call defined in func.
137 proc check_bound_violation {parm parm_type is_positive} {
141 gdb_test "continue" "$u_fault.*" "continue to a bnd violation"
143 set message "access only one position"
144 if {$is_positive == 1} {
145 gdb_test "p (((void *)\$_siginfo._sifields._sigfault.si_addr\
146 - (void*)$parm))/sizeof($parm_type) == 1"\
149 gdb_test "p ((void*)$parm\
150 - (void *)\$_siginfo._sifields._sigfault.si_addr)\
151 /sizeof($parm_type) == 1"\
154 gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
161 # Set up for stopping in the middle of main for calling a function in the
165 gdb_breakpoint [gdb_get_line_number "${break}"]
166 gdb_continue_to_breakpoint "${break}" ".*${break}.*"
170 # default run execution of call should succeed without violations.
172 with_test_prefix "default_run" {
174 gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
175 "store bnd0 register in a convenience variable"
177 gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
178 "default inferior call"
180 gdb_test "p ((\$bnd0.lbound==\$keep_bnd0_value.lbound) &&\
181 (\$bnd0.ubound==\$keep_bnd0_value.ubound))" "= 1" \
182 "bnd register value after and before call"
185 # Consistency: Examine bnd registers values before and after the call.
188 with_test_prefix "verify_default_values" {
190 prepare_bndcfg_bndstatus
192 gdb_breakpoint "*upper"
193 perform_a_call "upper (a, b, c, d, 1)"
195 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
197 compare_bndstatus_with_convenience
199 gdb_test_multiple "continue" "inferior call test" {
200 -re ".*Continuing.\r\n$gdb_prompt " {
201 pass "inferior call performed"
206 # Examine: Cause an upper bound violation changing BND0.
209 with_test_prefix "upper_bnd0" {
211 prepare_bndcfg_bndstatus
213 gdb_breakpoint "*upper"
214 perform_a_call "upper (a, b, c, d, 1)"
216 remove_memory_access "\$bnd0"
218 compare_bndstatus_with_convenience
220 check_bound_violation "a" "int" 1
223 # Examine: Cause an upper bound violation changing BND1.
226 with_test_prefix "upper_bnd1" {
228 prepare_bndcfg_bndstatus
230 gdb_breakpoint "*upper"
231 perform_a_call "upper (a, b, c, d, 1)"
233 remove_memory_access "\$bnd1"
235 compare_bndstatus_with_convenience
237 check_bound_violation "b" "int" 1
240 # Examine: Cause an upper bound violation changing BND2.
243 with_test_prefix "upper_bnd2" {
245 prepare_bndcfg_bndstatus
247 gdb_breakpoint "*upper"
248 perform_a_call "upper (a, b, c, d, 1)"
250 remove_memory_access "\$bnd2"
252 compare_bndstatus_with_convenience
254 check_bound_violation "c" "int" 1
257 # Examine: Cause an upper bound violation changing BND3.
260 with_test_prefix "upper_bnd3" {
261 prepare_bndcfg_bndstatus
263 gdb_breakpoint "*upper"
264 perform_a_call "upper (a, b, c, d, 1)"
266 remove_memory_access "\$bnd3"
268 compare_bndstatus_with_convenience
270 check_bound_violation "d" "int" 1
273 # Examine: Cause a lower bound violation changing BND0.
276 with_test_prefix "lower_bnd0" {
278 prepare_bndcfg_bndstatus
280 gdb_breakpoint "*lower"
281 perform_a_call "lower (a, b, c, d, 1)"
283 remove_memory_access "\$bnd0"
285 compare_bndstatus_with_convenience
287 check_bound_violation "a" "int" 0
290 # Examine: Cause a lower bound violation changing BND1.
293 with_test_prefix "lower_bnd1" {
295 prepare_bndcfg_bndstatus
297 gdb_breakpoint "*lower"
298 perform_a_call "lower (a, b, c, d, 1)"
300 remove_memory_access "\$bnd1"
302 compare_bndstatus_with_convenience
304 check_bound_violation "b" "int" 0
307 # Examine: Cause a lower bound violation changing BND2.
310 with_test_prefix "lower_bnd2" {
312 prepare_bndcfg_bndstatus
314 gdb_breakpoint "*lower"
315 perform_a_call "lower (a, b, c, d, 1)"
317 remove_memory_access "\$bnd2"
319 compare_bndstatus_with_convenience
321 check_bound_violation "c" "int" 0
324 # Examine: Cause a lower bound violation changing BND3.
327 with_test_prefix "lower_bnd3" {
329 prepare_bndcfg_bndstatus
331 gdb_breakpoint "*lower"
332 perform_a_call "lower (a, b, c, d, 1)"
334 remove_memory_access "\$bnd3"
336 compare_bndstatus_with_convenience
338 check_bound_violation "d" "int" 0
341 # Examine: String causing a upper bound violation changing BND0.
344 with_test_prefix "chars_up" {
346 prepare_bndcfg_bndstatus
348 gdb_breakpoint "*char_upper"
349 perform_a_call "char_upper (hello, 1)"
351 remove_memory_access "\$bnd0"
353 compare_bndstatus_with_convenience
355 check_bound_violation "str" "char" 1
359 # Examine: String causing an lower bound violation changing BND0.
362 with_test_prefix "chars_low" {
364 prepare_bndcfg_bndstatus
366 gdb_breakpoint "*char_lower"
367 perform_a_call "char_lower (hello, 1)"
369 remove_memory_access "\$bnd0"
371 compare_bndstatus_with_convenience
373 check_bound_violation "str" "char" 0
376 # Examine: String causing an lower bound violation changing BND0.
379 with_test_prefix "chars_low_adhoc_parm" {
381 prepare_bndcfg_bndstatus
383 gdb_breakpoint "*char_lower"
384 perform_a_call "char_lower (\"tryme\", 1)"
386 remove_memory_access "\$bnd0"
388 compare_bndstatus_with_convenience
390 check_bound_violation "str" "char" 0