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