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