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
|
# Copyright (C) 2021-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/>.
# Test a binary that uses MTE and exercise various MTE-related scenarios.
global hex
global decimal
# Return TAG in hex format with no leading zeroes.
proc get_hex_tag { tag } {
return [format "%x" $tag]
}
# Return TAG in the NN format where N is 4 bits of the byte.
proc get_tag_nn { tag } {
return [format "%02x" $tag]
}
# Return the address of PTR with a tag of TAG.
proc get_tagged_ptr { tag ptr } {
set addr [get_hexadecimal_valueof $ptr -1]
return [get_valueof "/x" \
"${addr} & (0xf0ffffffffffffff) | ((unsigned long) ${tag} << 56)" \
"0" "fetch pointer ${ptr} with tag ${tag}"]
}
# Return the logical TAG from PTR.
proc get_ltag_from_ptr { ptr } {
set addr [get_hexadecimal_valueof $ptr -1]
return [get_valueof "/x" "${addr} >> 56 & 0xf" -1 \
"fetch tag from pointer ${ptr}"]
}
if {![is_aarch64_target]} {
verbose "Skipping ${gdb_test_file_name}."
return
}
standard_testfile
if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
return -1
}
if ![runto_main] {
return -1
}
# Targets that don't support memory tagging should not execute the
# runtime memory tagging tests.
if {![supports_memtag]} {
unsupported "memory tagging unsupported"
return -1
}
gdb_breakpoint "access_memory"
if [gdb_continue "access_memory"] {
return -1
}
# Fetch a known pointer to an area mapped with PROT_MTE.
set tagged_ptr_symbol "tagged_ptr"
set tagged_ptr_addr [get_hexadecimal_valueof $tagged_ptr_symbol -1]
if {$tagged_ptr_addr == -1} {
unresolved "unexpected pointer or tag value"
return -1
}
# Fetch a known pointer to an area not mapped with PROT_MTE.
set untagged_ptr_symbol "untagged_ptr"
set untagged_ptr_addr [get_hexadecimal_valueof $untagged_ptr_symbol -1]
if {$untagged_ptr_addr == -1} {
unresolved "unexpected pointer or tag value"
return -1
}
with_test_prefix "literals" {
# Test inspecting an allocation tag from a pointer to a memory area that
# is not mapped with PROT_MTE.
set msg "Address ${untagged_ptr_addr} not in a region mapped with a memory tagging flag\."
gdb_test "memory-tag print-allocation-tag ${untagged_ptr_addr}" $msg \
"memory-tag print-allocation-tag with an untagged address"
gdb_test "memory-tag set-allocation-tag ${untagged_ptr_addr} 1 00" $msg \
"memory-tag set-allocation-tag with an untagged address"
set addr_tagged 0
set addr_tagged_valid 0
# Test setting and showing the logical tags for a literal address.
for {set i 0} {$i < 32} {incr i} {
with_test_prefix "tag ${i}" {
set addr_tagged [get_tagged_ptr $i ${tagged_ptr_addr}]
}
set tag_hexnz [get_hex_tag [expr $i % 16]]
gdb_test "memory-tag print-logical-tag ${addr_tagged}" \
" = 0x${tag_hexnz}" \
"print-logical-tag with tag ${i}"
set tag_hexnn [get_tag_nn $i]
gdb_test "memory-tag with-logical-tag ${addr_tagged} ${tag_hexnn}" \
" = \\(void \\*\\) ${addr_tagged}" \
"with-logical-tag with tag ${i}"
}
set atag_msg "Allocation tag\\(s\\) updated successfully\."
# Test setting and showing the allocation tags.
for {set i 0} {$i < 32} {incr i} {
set tag_hexnn [get_tag_nn $i]
gdb_test "memory-tag set-allocation-tag ${tagged_ptr_addr} 1 ${tag_hexnn}" \
$atag_msg \
"set-allocation-tag with tag ${i}"
set tag_hexnz [get_hex_tag [expr $i % 16]]
gdb_test "memory-tag print-allocation-tag ${tagged_ptr_addr}" " = 0x${tag_hexnz}" \
"print-allocation-tag with tag ${i}"
}
# Test tag mismatches.
with_test_prefix "tag mismatches" {
for {set i 0} {$i < 32} {incr i} {
# Set the allocation tag to a known value.
set tag_hexnn [get_tag_nn $i]
gdb_test "memory-tag set-allocation-tag ${tagged_ptr_addr} 1 ${tag_hexnn}" \
$atag_msg \
"set-allocation-tag with tag ${i}"
set atag_hexnz [get_hex_tag [expr $i % 16]]
# Validate that the logical tag matches the allocation tag.
with_test_prefix "tag ${i}" {
set addr_tagged [get_tagged_ptr $i ${tagged_ptr_addr}]
}
gdb_test "memory-tag check ${addr_tagged}" \
"Memory tags for address $hex match \\(0x${atag_hexnz}\\)\." \
"check match with tag ${i}"
# Get a pointer with the logical tag that does not match the
# allocation tag.
set ltag [expr $i + 1]
with_test_prefix "fetch mismatch tag ${i}" {
set addr_tagged [get_tagged_ptr $ltag ${tagged_ptr_addr}]
}
# Validate that the logical tag does not match the allocation
# tag.
set ltag_hexnz [get_hex_tag [expr [expr $i + 1]% 16]]
gdb_test "memory-tag check ${addr_tagged}" \
"Logical tag \\(0x${ltag_hexnz}\\) does not match the allocation tag \\(0x${atag_hexnz}\\) for address $hex\." \
"check mismatch with tag ${i}"
}
}
}
with_test_prefix "symbolic" {
# Test inspecting an allocation tag from a pointer to a memory area that
# is not mapped with PROT_MTE.
set msg "Address ${untagged_ptr_addr} not in a region mapped with a memory tagging flag\."
gdb_test "memory-tag print-allocation-tag ${untagged_ptr_symbol}" $msg \
"memory-tag print-allocation-tag with an untagged address"
gdb_test "memory-tag set-allocation-tag ${untagged_ptr_symbol} 1 00" $msg \
"memory-tag set-allocation-tag with an untagged address"
# Test setting and showing the logical tags for a literal address.
for {set i 0} {$i < 32} {incr i} {
set addr_tagged 0
with_test_prefix "tag ${i}" {
set addr_tagged [get_tagged_ptr $i ${tagged_ptr_addr}]
gdb_test_no_output "set variable ${tagged_ptr_symbol} = ${addr_tagged}" \
"update value of symbol ${tagged_ptr_symbol}"
}
set tag_hexnz [get_hex_tag [expr $i % 16]]
gdb_test "memory-tag print-logical-tag ${tagged_ptr_symbol}" \
" = 0x${tag_hexnz}" \
"print-logical-tag with tag ${i}"
set tag_hexnn [get_tag_nn $i]
gdb_test "memory-tag with-logical-tag ${tagged_ptr_symbol} ${tag_hexnn}" \
" = \\(void \\*\\) ${addr_tagged}" \
"with-logical-tag with tag ${i}"
}
# Reset the tagged ptr to its original value
gdb_test_no_output "set variable ${tagged_ptr_symbol} = ${tagged_ptr_addr}" \
"reset ${tagged_ptr_symbol} to ${tagged_ptr_addr}"
set atag_msg "Allocation tag\\(s\\) updated successfully\."
# Test setting and showing the allocation tags.
for {set i 0} {$i < 32} {incr i} {
set tag_hexnn [get_tag_nn $i]
gdb_test "memory-tag set-allocation-tag ${tagged_ptr_symbol} 1 ${tag_hexnn}" \
$atag_msg \
"set-allocation-tag with tag ${i}"
set tag_hexnz [get_hex_tag [expr $i % 16]]
gdb_test "memory-tag print-allocation-tag ${tagged_ptr_symbol}" \
" = 0x${tag_hexnz}" \
"print-allocation-tag with tag ${i}"
}
# Test tag mismatches.
with_test_prefix "tag mismatches" {
for {set i 0} {$i < 32} {incr i} {
# Set the allocation tag to a known value (0).
set tag_hexnn [get_tag_nn $i]
gdb_test "memory-tag set-allocation-tag ${tagged_ptr_symbol} 1 ${tag_hexnn}" \
$atag_msg \
"set-allocation-tag with tag ${i}"
set atag_hexnz [get_hex_tag [expr $i % 16]]
# Validate that the logical tag matches the allocation tag.
with_test_prefix "tag ${i}" {
set addr_tagged [get_tagged_ptr $i ${tagged_ptr_addr}]
}
with_test_prefix "tag ${i}" {
gdb_test_no_output "set variable ${tagged_ptr_symbol} = ${addr_tagged}" \
"set ${tagged_ptr_symbol} to a matching logical tag"
}
gdb_test "memory-tag check ${tagged_ptr_symbol}" \
"Memory tags for address $hex match \\(0x${atag_hexnz}\\)\." \
"check match with tag ${i}"
# Get a pointer with the logical tag that does not match the
# allocation tag.
set ltag [expr $i + 1]
with_test_prefix "fetch mismatch tag ${i}" {
set addr_tagged [get_tagged_ptr $ltag ${tagged_ptr_addr}]
}
with_test_prefix "tag ${i}" {
gdb_test_no_output "set variable ${tagged_ptr_symbol} = ${addr_tagged}" \
"set ${tagged_ptr_symbol} to a mismatching logical tag"
}
# Validate that the logical tag does not match the allocation
# tag.
set ltag_hexnz [get_hex_tag [expr [expr $i + 1]% 16]]
gdb_test "memory-tag check ${tagged_ptr_symbol}" \
"Logical tag \\(0x${ltag_hexnz}\\) does not match the allocation tag \\(0x${atag_hexnz}\\) for address $hex\." \
"check mismatch with tag ${i}"
}
# Reset the tagged ptr to its original value
gdb_test_no_output "set variable ${tagged_ptr_symbol} = ${tagged_ptr_addr}" \
"reset ${tagged_ptr_symbol} to ${tagged_ptr_addr}"
}
}
# Test the memory tagging extensions for the "print" command.
with_test_prefix "print command" {
set untagged_ptr [get_tagged_ptr 0 ${tagged_ptr_addr}]
with_test_prefix "fetch ltag" {
set ltag [get_ltag_from_ptr ${tagged_ptr_addr}]
}
if {$ltag == -1} {
unresolved "unexpected tag value"
return -1
}
set atag [expr [expr $ltag + 1] % 16]
set atag_hexnn [get_tag_nn $atag]
gdb_test "memory-tag set-allocation-tag ${tagged_ptr_symbol} 1 ${atag_hexnn}" \
$atag_msg \
"make atag and ltag different"
set atag_hexnz [get_hex_tag $atag]
gdb_test "p/x ${tagged_ptr_symbol}" \
[multi_line \
"Logical tag \\(${ltag}\\) does not match the allocation tag \\(0x${atag_hexnz}\\)\." \
"\\\$\[0-9\]+ = ${untagged_ptr}"] \
"show tag mismatch"
}
# Test the memory tagging extensions for the "x" command.
with_test_prefix "x command" {
# Check if the allocation tags match what we expect.
gdb_test "x/gxm ${tagged_ptr_symbol}" \
[multi_line \
"<Allocation Tag $hex for range \\\[$hex,$hex\\)>" \
"$hex:\[ \t\]+$hex"] \
"outputs tag information"
# Also make sure no tag information is output for memory areas without
# PROT_MTE mappings.
gdb_test "x/gxm ${untagged_ptr_symbol}" \
"$hex:\[ \t\]+$hex" \
"does not output tag information"
}
# Validate the presence of the MTE registers.
foreach reg {"tag_ctl" } {
gdb_test "info registers $reg" \
"$reg\[ \t\]+$hex\[ \t\]+$decimal" \
"register $reg available"
}
# Run until a crash and confirm GDB displays memory tag violation
# information.
gdb_test "continue" \
[multi_line \
"Program received signal SIGSEGV, Segmentation fault" \
"Memory tag violation while accessing address $hex" \
"Allocation tag $hex" \
"Logical tag $hex\." \
"$hex in access_memory \\(.*\\) at .*" \
".*tagged_ptr\\\[0\\\] = 'a';"] \
"display tag violation information"
# Restart to execute the async tag fault test.
with_test_prefix "async" {
if ![runto_main] {
return -1
}
gdb_breakpoint "access_memory"
if [gdb_continue "access_memory"] {
fail "could not run to tagged memory test function"
return -1
}
# Force a tag fault.
gdb_test "memory-tag set-allocation-tag tagged_ptr 1 05" \
$atag_msg \
"make atag and ltag different"
# Force the tag fault to be async.
gdb_test_no_output "set \$tag_ctl=0x7fff5" "set tag_ctl to async"
# Run until a crash and confirm GDB displays memory tag violation
# information for async mode
gdb_test "continue" \
[multi_line \
"Program received signal SIGSEGV, Segmentation fault" \
"Memory tag violation" \
"Fault address unavailable\." \
"$hex in .* \\(.*\\) .*"] \
"display tag violation information"
}
|