aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.arch/i386-mpx-call.exp
blob: 95b68cd30dfd1330dfef08a4c2ead033e525eb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Copyright (C) 2017-2023 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


require {is_any_target "i?86-*-*" "x86_64-*-*"}

standard_testfile

require supports_mpx_check_pointer_bounds have_mpx

set comp_flags "-mmpx -fcheck-pointer-bounds -I${srcdir}/../nat"

if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
    [list debug additional_flags=${comp_flags}]] } {
    return -1
}

if ![runto_main] {
    return -1
}

set bounds_table 0
gdb_test_multiple "disassemble upper" "" {
    -re -wrap "bndldx.*" {
	set bounds_table 1
    }
    -re -wrap "" {
    }
}

# Convenience for returning from an inferior call that causes a BND violation.
#
gdb_test_no_output "set confirm off"

# Convenience variable.
#
set bound_reg " = \\\{lbound = $hex, ubound = $hex\\\}.*"
set int_braw_reg " = \\\{lbound = 0x0, ubound_raw = 0x0\\\}.*"
set bndcfg_reg " = \\\{raw = $hex, config = \\\{base = $hex, reserved = $hex,\
               preserved = $hex, enabled = $hex\\\}\\\}"
set bndstatus_reg  " = \\\{raw = $hex, status = \\\{bde = $hex,\
                    error = $hex\\\}\\\}"
set u_fault [multi_line "Program received signal SIGSEGV, Segmentation fault" \
                        "Upper bound violation while accessing address $hex" \
                        "Bounds: \\\[lower = $hex, upper = $hex\\\]"]


# Simplify the tests below.
#
proc sanity_check_bndregs {arglist} {

    global int_braw_reg

    foreach a $arglist {
        gdb_test "p /x $a" "$int_braw_reg"\
            "$a"
    }
}

# Set bnd register to have no access to memory.
#
proc remove_memory_access {reg} {
    global hex

    sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}

    gdb_test "p /x $reg.lbound = $reg.ubound" "= $hex"\
        "$reg lower bound set"
    gdb_test "p /x $reg.ubound = 0" " = 0x0"\
        "$reg upper bound set"
}


# Prepare convenience variables for bndconfig and status
# for posterior comparison.
#
proc prepare_bndcfg_bndstatus {} {

    global bndcfg_reg
    global bndstatus_reg

    gdb_test "p /x \$temp_bndcfgu = \$bndcfgu" "$bndcfg_reg"\
        "bndcfgu should not change"

    gdb_test "p /x \$temp_bndstatus = \$bndstatus" "$bndstatus_reg"\
        "bndstatus should not change"
}

# Compare values set for convenience variables and actual values of bndconfig
# and bndstatus registers.
#
proc compare_bndstatus_with_convenience {} {

    gdb_test "p \$temp_bndcfgu == \$bndcfgu" "= 1"\
        "bndcfgu compare before and after"
    gdb_test "p \$temp_bndstatus == \$bndstatus" "= 1"\
        "bndstatus compare before and after"
}

# Perform an inferior call defined in func.
#
proc perform_a_call {func} {

    global inf_call_stopped
    global gdb_prompt

    gdb_test "p /x $func" [multi_line "The program being debugged\
                          stopped while in a function called from GDB." \
                          "Evaluation of the expression containing the\
                          function.*" \
                          ] "inferior call stopped"
}

# Perform an inferior call defined in func.
#
proc check_bound_violation {parm parm_type is_positive} {

    global u_fault bounds_table

    set have_bnd_violation 0
    gdb_test_multiple "continue" "continue to a bnd violation" {
	-re -wrap "Continuing\." {
	    if { $bounds_table } {
		pass $gdb_test_name
	    } else {
		fail $gdb_test_name
	    }
	}
	-re -wrap "$u_fault.*" {
	    pass $gdb_test_name
	    set have_bnd_violation 1
	}
    }
    if { ! $have_bnd_violation } {
	return
    }

    set message "access only one position"
    if {$is_positive == 1} {
        gdb_test "p (((void *)\$_siginfo._sifields._sigfault.si_addr\
                  - (void*)$parm))/sizeof($parm_type) == 1"\
                  " = 1" $message
    } else {
        gdb_test "p ((void*)$parm\
                  - (void *)\$_siginfo._sifields._sigfault.si_addr)\
                  /sizeof($parm_type) == 1"\
                  " = 1" $message
    }
    gdb_test "return" "\\\#.*main.*i386-mpx-call\\\.c:.*" "return from the fault"
}


# Start testing!
#

# Set up for stopping in the middle of main for calling a function in the
# inferior.
#
set break "bkpt 1."
gdb_breakpoint [gdb_get_line_number "${break}"]
gdb_continue_to_breakpoint "${break}" ".*${break}.*"


# Consistency:
#    default run execution of call should succeed without violations.
#
with_test_prefix "default_run" {

    gdb_test "p \$keep_bnd0_value=\$bnd0" $bound_reg\
        "store bnd0 register in a convenience variable"

    gdb_test "p /x upper (a, b, c, d, 0)" " = $hex"\
        "default inferior call"

    gdb_test "p ((\$bnd0.lbound==\$keep_bnd0_value.lbound) &&\
        (\$bnd0.ubound==\$keep_bnd0_value.ubound))" "= 1" \
        "bnd register value after and before call"
}

# Consistency:  Examine bnd registers values before and after the call.
#
#
with_test_prefix "verify_default_values" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*upper"
    perform_a_call "upper (a, b, c, d, 1)"

    sanity_check_bndregs {"\$bnd0raw" "\$bnd1raw" "\$bnd2raw" "\$bnd3raw"}

    compare_bndstatus_with_convenience

    gdb_test_multiple "continue" "inferior call test" {
        -re ".*Continuing.\r\n$gdb_prompt " {
            pass "inferior call performed"
        }
    }
}

# Examine:  Cause an upper bound violation changing BND0.
#
#
with_test_prefix "upper_bnd0" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*upper"
    perform_a_call "upper (a, b, c, d, 1)"

    remove_memory_access "\$bnd0"

    compare_bndstatus_with_convenience

    check_bound_violation "a" "int" 1
}

# Examine:  Cause an upper bound violation changing BND1.
#
#
with_test_prefix "upper_bnd1" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*upper"
    perform_a_call "upper (a, b, c, d, 1)"

    remove_memory_access "\$bnd1"

    compare_bndstatus_with_convenience

    check_bound_violation "b" "int" 1
}

# Examine:  Cause an upper bound violation changing BND2.
#
#
with_test_prefix "upper_bnd2" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*upper"
    perform_a_call "upper (a, b, c, d, 1)"

    remove_memory_access "\$bnd2"

    compare_bndstatus_with_convenience

    check_bound_violation "c" "int" 1
}

# Examine:  Cause an upper bound violation changing BND3.
#
#
with_test_prefix "upper_bnd3" {
    prepare_bndcfg_bndstatus

    gdb_breakpoint "*upper"
    perform_a_call "upper (a, b, c, d, 1)"

    remove_memory_access "\$bnd3"

    compare_bndstatus_with_convenience

    check_bound_violation "d" "int" 1
}

# Examine:  Cause a lower bound violation changing BND0.
#
#
with_test_prefix "lower_bnd0" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*lower"
    perform_a_call "lower (a, b, c, d, 1)"

    remove_memory_access "\$bnd0"

    compare_bndstatus_with_convenience

    check_bound_violation "a" "int" 0
}

# Examine:  Cause a lower bound violation changing BND1.
#
#
with_test_prefix "lower_bnd1" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*lower"
    perform_a_call "lower (a, b, c, d, 1)"

    remove_memory_access "\$bnd1"

    compare_bndstatus_with_convenience

    check_bound_violation "b" "int" 0
}

# Examine:  Cause a lower bound violation changing BND2.
#
#
with_test_prefix "lower_bnd2" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*lower"
    perform_a_call "lower (a, b, c, d, 1)"

    remove_memory_access "\$bnd2"

    compare_bndstatus_with_convenience

    check_bound_violation "c" "int" 0
}

# Examine:  Cause a lower bound violation changing BND3.
#
#
with_test_prefix "lower_bnd3" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*lower"
    perform_a_call "lower (a, b, c, d, 1)"

    remove_memory_access "\$bnd3"

    compare_bndstatus_with_convenience

    check_bound_violation "d" "int" 0
}

# Examine:  String causing a upper bound violation changing BND0.
#
#
with_test_prefix "chars_up" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*char_upper"
    perform_a_call "char_upper (hello, 1)"

    remove_memory_access "\$bnd0"

    compare_bndstatus_with_convenience

    check_bound_violation "str" "char" 1
}


# Examine:  String causing an lower bound violation changing BND0.
#
#
with_test_prefix "chars_low" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*char_lower"
    perform_a_call "char_lower (hello, 1)"

    remove_memory_access "\$bnd0"

    compare_bndstatus_with_convenience

    check_bound_violation "str" "char" 0
}

# Examine:  String causing an lower bound violation changing BND0.
#
#
with_test_prefix "chars_low_adhoc_parm" {

    prepare_bndcfg_bndstatus

    gdb_breakpoint "*char_lower"
    perform_a_call "char_lower (\"tryme\", 1)"

    remove_memory_access "\$bnd0"

    compare_bndstatus_with_convenience

    check_bound_violation "str" "char" 0
}