]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.arch/i386-mpx-call.exp
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.arch / i386-mpx-call.exp
1 # Copyright (C) 2017-2022 Free Software Foundation, Inc.
2 #
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.
7 #
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.
12 #
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/>.
15
16
17 if { ![istarget i?86-*-*] && ![istarget x86_64-*-* ] } {
18 untested "skipping x86 MPX tests."
19 return
20 }
21
22 standard_testfile
23
24 if { ![supports_mpx_check_pointer_bounds] } {
25 return -1
26 }
27
28 if { ![have_mpx] } {
29 unsupported "processor does not support MPX"
30 return -1
31 }
32
33 set comp_flags "-mmpx -fcheck-pointer-bounds -I${srcdir}/../nat"
34
35 if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
36 [list debug additional_flags=${comp_flags}]] } {
37 return -1
38 }
39
40 if ![runto_main] {
41 return -1
42 }
43
44 set bounds_table 0
45 gdb_test_multiple "disassemble upper" "" {
46 -re -wrap "bndldx.*" {
47 set bounds_table 1
48 }
49 -re -wrap "" {
50 }
51 }
52
53 # Convenience for returning from an inferior call that causes a BND violation.
54 #
55 gdb_test_no_output "set confirm off"
56
57 # Convenience variable.
58 #
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,\
64 error = $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\\\]"]
68
69
70 # Simplify the tests below.
71 #
72 proc sanity_check_bndregs {arglist} {
73
74 global int_braw_reg
75
76 foreach a $arglist {
77 gdb_test "p /x $a" "$int_braw_reg"\
78 "$a"
79 }
80 }
81
82 # Set bnd register to have no access to memory.
83 #
84 proc remove_memory_access {reg} {
85 global hex
86
87 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
88
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"
93 }
94
95
96 # Prepare convenience variables for bndconfig and status
97 # for posterior comparison.
98 #
99 proc prepare_bndcfg_bndstatus {} {
100
101 global bndcfg_reg
102 global bndstatus_reg
103
104 gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
105 "bndcfgu should not change"
106
107 gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
108 "bndstatus should not change"
109 }
110
111 # Compare values set for convenience variables and actual values of bndconfig
112 # and bndstatus registers.
113 #
114 proc compare_bndstatus_with_convenience {} {
115
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"
120 }
121
122 # Perform an inferior call defined in func.
123 #
124 proc perform_a_call {func} {
125
126 global inf_call_stopped
127 global gdb_prompt
128
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\
132 function.*" \
133 ] "inferior call stopped"
134 }
135
136 # Perform an inferior call defined in func.
137 #
138 proc check_bound_violation {parm parm_type is_positive} {
139
140 global u_fault bounds_table
141
142 set have_bnd_violation 0
143 gdb_test_multiple "continue" "continue to a bnd violation" {
144 -re -wrap "Continuing\." {
145 if { $bounds_table } {
146 pass $gdb_test_name
147 } else {
148 fail $gdb_test_name
149 }
150 }
151 -re -wrap "$u_fault.*" {
152 pass $gdb_test_name
153 set have_bnd_violation 1
154 }
155 }
156 if { ! $have_bnd_violation } {
157 return
158 }
159
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"\
164 " = 1" $message
165 } else {
166 gdb_test "p ((void*)$parm\
167 - (void *)\$_siginfo._sifields._sigfault.si_addr)\
168 /sizeof($parm_type) == 1"\
169 " = 1" $message
170 }
171 gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
172 }
173
174
175 # Start testing!
176 #
177
178 # Set up for stopping in the middle of main for calling a function in the
179 # inferior.
180 #
181 set break "bkpt 1."
182 gdb_breakpoint [gdb_get_line_number "${break}"]
183 gdb_continue_to_breakpoint "${break}" ".*${break}.*"
184
185
186 # Consistency:
187 # default run execution of call should succeed without violations.
188 #
189 with_test_prefix "default_run" {
190
191 gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
192 "store bnd0 register in a convenience variable"
193
194 gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
195 "default inferior call"
196
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"
200 }
201
202 # Consistency: Examine bnd registers values before and after the call.
203 #
204 #
205 with_test_prefix "verify_default_values" {
206
207 prepare_bndcfg_bndstatus
208
209 gdb_breakpoint "*upper"
210 perform_a_call "upper (a, b, c, d, 1)"
211
212 sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}
213
214 compare_bndstatus_with_convenience
215
216 gdb_test_multiple "continue" "inferior call test" {
217 -re ".*Continuing.\r\n$gdb_prompt " {
218 pass "inferior call performed"
219 }
220 }
221 }
222
223 # Examine: Cause an upper bound violation changing BND0.
224 #
225 #
226 with_test_prefix "upper_bnd0" {
227
228 prepare_bndcfg_bndstatus
229
230 gdb_breakpoint "*upper"
231 perform_a_call "upper (a, b, c, d, 1)"
232
233 remove_memory_access "\$bnd0"
234
235 compare_bndstatus_with_convenience
236
237 check_bound_violation "a" "int" 1
238 }
239
240 # Examine: Cause an upper bound violation changing BND1.
241 #
242 #
243 with_test_prefix "upper_bnd1" {
244
245 prepare_bndcfg_bndstatus
246
247 gdb_breakpoint "*upper"
248 perform_a_call "upper (a, b, c, d, 1)"
249
250 remove_memory_access "\$bnd1"
251
252 compare_bndstatus_with_convenience
253
254 check_bound_violation "b" "int" 1
255 }
256
257 # Examine: Cause an upper bound violation changing BND2.
258 #
259 #
260 with_test_prefix "upper_bnd2" {
261
262 prepare_bndcfg_bndstatus
263
264 gdb_breakpoint "*upper"
265 perform_a_call "upper (a, b, c, d, 1)"
266
267 remove_memory_access "\$bnd2"
268
269 compare_bndstatus_with_convenience
270
271 check_bound_violation "c" "int" 1
272 }
273
274 # Examine: Cause an upper bound violation changing BND3.
275 #
276 #
277 with_test_prefix "upper_bnd3" {
278 prepare_bndcfg_bndstatus
279
280 gdb_breakpoint "*upper"
281 perform_a_call "upper (a, b, c, d, 1)"
282
283 remove_memory_access "\$bnd3"
284
285 compare_bndstatus_with_convenience
286
287 check_bound_violation "d" "int" 1
288 }
289
290 # Examine: Cause a lower bound violation changing BND0.
291 #
292 #
293 with_test_prefix "lower_bnd0" {
294
295 prepare_bndcfg_bndstatus
296
297 gdb_breakpoint "*lower"
298 perform_a_call "lower (a, b, c, d, 1)"
299
300 remove_memory_access "\$bnd0"
301
302 compare_bndstatus_with_convenience
303
304 check_bound_violation "a" "int" 0
305 }
306
307 # Examine: Cause a lower bound violation changing BND1.
308 #
309 #
310 with_test_prefix "lower_bnd1" {
311
312 prepare_bndcfg_bndstatus
313
314 gdb_breakpoint "*lower"
315 perform_a_call "lower (a, b, c, d, 1)"
316
317 remove_memory_access "\$bnd1"
318
319 compare_bndstatus_with_convenience
320
321 check_bound_violation "b" "int" 0
322 }
323
324 # Examine: Cause a lower bound violation changing BND2.
325 #
326 #
327 with_test_prefix "lower_bnd2" {
328
329 prepare_bndcfg_bndstatus
330
331 gdb_breakpoint "*lower"
332 perform_a_call "lower (a, b, c, d, 1)"
333
334 remove_memory_access "\$bnd2"
335
336 compare_bndstatus_with_convenience
337
338 check_bound_violation "c" "int" 0
339 }
340
341 # Examine: Cause a lower bound violation changing BND3.
342 #
343 #
344 with_test_prefix "lower_bnd3" {
345
346 prepare_bndcfg_bndstatus
347
348 gdb_breakpoint "*lower"
349 perform_a_call "lower (a, b, c, d, 1)"
350
351 remove_memory_access "\$bnd3"
352
353 compare_bndstatus_with_convenience
354
355 check_bound_violation "d" "int" 0
356 }
357
358 # Examine: String causing a upper bound violation changing BND0.
359 #
360 #
361 with_test_prefix "chars_up" {
362
363 prepare_bndcfg_bndstatus
364
365 gdb_breakpoint "*char_upper"
366 perform_a_call "char_upper (hello, 1)"
367
368 remove_memory_access "\$bnd0"
369
370 compare_bndstatus_with_convenience
371
372 check_bound_violation "str" "char" 1
373 }
374
375
376 # Examine: String causing an lower bound violation changing BND0.
377 #
378 #
379 with_test_prefix "chars_low" {
380
381 prepare_bndcfg_bndstatus
382
383 gdb_breakpoint "*char_lower"
384 perform_a_call "char_lower (hello, 1)"
385
386 remove_memory_access "\$bnd0"
387
388 compare_bndstatus_with_convenience
389
390 check_bound_violation "str" "char" 0
391 }
392
393 # Examine: String causing an lower bound violation changing BND0.
394 #
395 #
396 with_test_prefix "chars_low_adhoc_parm" {
397
398 prepare_bndcfg_bndstatus
399
400 gdb_breakpoint "*char_lower"
401 perform_a_call "char_lower (\"tryme\", 1)"
402
403 remove_memory_access "\$bnd0"
404
405 compare_bndstatus_with_convenience
406
407 check_bound_violation "str" "char" 0
408 }