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