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 2022-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 left and right bit shifting, in all languages that have such
# operator.
clean_restart
# Test a print command that prints out RESULT_RE. If WARNING_OR_ERROR
# is non-empty, it is expected that for languages other than Go, GDB
# prints this warning before the print result. For Go, this is an
# expected error. If WARNING_OR_ERROR is empty, it is expected that
# GDB prints no text other than the print result.
proc test_shift {lang cmd result_re {warning_or_error ""}} {
set cmd_re [string_to_regexp $cmd]
if {$lang == "go"} {
if {$warning_or_error != ""} {
set error_re "[string_to_regexp $warning_or_error]"
gdb_test_multiple $cmd "" {
-re -wrap "^$cmd_re\r\n$error_re" {
pass $gdb_test_name
}
}
} else {
gdb_test_multiple $cmd "" {
-re -wrap "^$cmd_re\r\n\\$$::decimal$result_re" {
pass $gdb_test_name
}
}
}
} else {
if {$warning_or_error != ""} {
set warning_re "warning: [string_to_regexp $warning_or_error]\r\n"
} else {
set warning_re ""
}
gdb_test_multiple $cmd "" {
-re -wrap "^$cmd_re\r\n$warning_re\\$$::decimal$result_re" {
pass $gdb_test_name
}
}
}
}
# Some warnings/errors GDB outputs.
set rs_negative_shift_count "right shift count is negative"
set rs_too_large_shift_count "right shift count >= width of type"
set ls_negative_shift_count "left shift count is negative"
set ls_too_large_shift_count "left shift count >= width of type"
# Test a left shift that results in a too-large shift count warning in
# all languages except Go.
proc test_lshift_tl {lang cmd result_re} {
if {$lang != "go"} {
test_shift $lang $cmd $result_re $::ls_too_large_shift_count
} else {
test_shift $lang $cmd $result_re
}
}
# Test a right shift that results in a too-large shift count warning
# in all languages except Go.
proc test_rshift_tl {lang cmd result_re} {
if {$lang != "go"} {
test_shift $lang $cmd $result_re $::rs_too_large_shift_count
} else {
test_shift $lang $cmd $result_re
}
}
# Return VAL, an integer value converted/cast to the right type for
# LANG. SIGNED indicates whether the type should be signed or
# unsigned. BITS indicates the bit width of the type. E.g., signed=0
# and bits=32 results in:
# Go => "uint($VAL)"
# D => "cast(uint) $VAL"
# Rust => "$VAL as i32"
# C/C++/others => "(unsigned int) $VAL"
proc make_val_cast {lang signed bits val} {
if {$lang == "go"} {
if {$signed} {
set sign_prefix ""
} else {
set sign_prefix "u"
}
return "${sign_prefix}int${bits}($val)"
} elseif {$lang == "d"} {
if {$signed} {
set sign_prefix ""
} else {
set sign_prefix "u"
}
if {$bits == 8} {
set type "byte"
} elseif {$bits == 16} {
set type "short"
} elseif {$bits == 32} {
set type "int"
} elseif {$bits == 64} {
set type "long"
} else {
error "$lang: unsupported bits"
}
return "cast(${sign_prefix}$type) $val"
} elseif {$lang == "rust"} {
if {$signed} {
set sign_prefix "i"
} else {
set sign_prefix "u"
}
return "$val as ${sign_prefix}$bits"
} else {
# C-like cast.
if {$signed} {
set sign_prefix ""
} else {
set sign_prefix "un"
}
if {$bits == 8} {
set type "char"
} elseif {$bits == 16} {
set type "short"
} elseif {$bits == 32} {
set type "int"
} elseif {$bits == 64} {
if {$lang == "opencl"} {
set type "long"
} else {
set type "long long"
}
} else {
error "$lang: unsupported bits"
}
return "(${sign_prefix}signed $type) $val"
}
}
# Generate make_int8 ... make_uint64 convenience procs, wrappers
# around make_val_cast.
foreach signed {0 1} {
if {$signed} {
set sign_prefix ""
} else {
set sign_prefix "u"
}
foreach bits {8 16 32 64} {
proc make_${sign_prefix}int${bits} {lang val} \
"make_val_cast \$lang $signed $bits \$val"
}
}
# Test bitshifting, particularly with negative shift counts and
# too-large-for-type shift counts. Exercises all C-like-ish
# languages.
proc test_shifts {} {
global ls_negative_shift_count rs_negative_shift_count
# Extract the set of all supported languages. We try all except
# languages we know wouldn't work. We do this instead of
# hardcoding the set of languages that we know work, so that if
# GDB gains a new language, it is automatically exercised.
set supported_langs [get_set_option_choices "set language"]
foreach_with_prefix lang $supported_langs {
set skip_langs {
"unknown" "ada" "modula-2" "pascal" "fortran"
}
if {[lsearch -exact $skip_langs $lang] >= 0} {
return
}
gdb_test_no_output "set language $lang"
# Make sure a signed left shift that overflows, i.e., whose
# result isn't representable in the signed type of the lhs,
# which is actually undefined, doesn't crash GDB when is it
# built with UBSan.
with_test_prefix "lsh overflow" {
test_shift $lang "print /x 0x0fffffffffffffff << 8" \
" = 0xffffffffffffff00"
test_shift $lang "print /x 0x0fffffff << 8" \
" = 0xffffff00"
# Make sure the result is still signed when the lhs was
# signed.
test_shift $lang "print 0x0fffffffffffffff << 8" " = -256"
test_shift $lang "print 0x0fffffff << 8" " = -256"
}
# 8-bit and 16-bit are promoted to int.
with_test_prefix "8-bit, promoted" {
foreach lhs \
[list \
[make_int8 $lang 0x0f] \
[make_uint8 $lang 0x0f]] \
{
test_shift $lang "print /x $lhs << 8" " = 0xf00"
test_shift $lang "print $lhs << 8" " = 3840"
}
}
with_test_prefix "16-bit, promoted" {
foreach lhs \
[list \
[make_int16 $lang 0x0fff] \
[make_uint16 $lang 0x0fff]] \
{
test_shift $lang "print /x $lhs << 8" " = 0xfff00"
test_shift $lang "print $lhs << 8" " = 1048320"
}
}
# Similarly, test shifting with both negative and too-large
# rhs. Both cases are undefined, but GDB lets them go through
# anyhow, similarly to how compilers don't error out. Try
# both signed and unsigned lhs.
# 8-bit lhs, signed and unsigned. These get promoted to
# 32-bit int.
with_test_prefix "8-bit, invalid" {
foreach lhs \
[list \
[make_int8 $lang 0x7f] \
[make_uint8 $lang 0xff]] \
{
test_shift $lang "print $lhs << -1" " = 0" \
$ls_negative_shift_count
test_shift $lang "print $lhs >> -1" " = 0" \
$rs_negative_shift_count
test_shift $lang "print/x $lhs << 8" " = 0x(7|f)f00"
test_shift $lang "print/x $lhs >> 8" " = 0x0"
test_lshift_tl $lang "print $lhs << 32" " = 0"
test_rshift_tl $lang "print $lhs >> 32" " = 0"
test_lshift_tl $lang "print $lhs << 33" " = 0"
test_rshift_tl $lang "print $lhs >> 33" " = 0"
}
}
# 16-bit lhs, signed and unsigned. These get promoted to 32-bit int.
with_test_prefix "16-bit, invalid" {
foreach {lhs res} \
[list \
[make_int16 $lang 0x7fff] 0x7fff \
[make_uint16 $lang 0xffff] 0xffff] \
{
test_shift $lang "print $lhs << -1" " = 0" \
$ls_negative_shift_count
test_shift $lang "print $lhs >> -1" " = 0" \
$rs_negative_shift_count
# Confirm shifting by 0 doesn't warn.
test_shift $lang "print/x $lhs << 0" " = $res"
test_shift $lang "print/x $lhs >> 0" " = $res"
# These don't overflow due to promotion.
test_shift $lang "print/x $lhs << 16" " = 0x(7|f)fff0000"
test_shift $lang "print/x $lhs >> 16" " = 0x0"
test_lshift_tl $lang "print $lhs << 32" " = 0"
test_rshift_tl $lang "print $lhs >> 32" " = 0"
test_lshift_tl $lang "print $lhs << 33" " = 0"
test_rshift_tl $lang "print $lhs >> 33" " = 0"
}
}
# 32-bit lhs, signed and unsigned.
with_test_prefix "32-bit, invalid" {
foreach {lhs res} \
[list \
[make_int32 $lang 0x7fffffff] 0x7fffffff \
[make_uint32 $lang 0xffffffff] 0xffffffff] \
{
test_shift $lang "print $lhs << -1" " = 0" \
$ls_negative_shift_count
test_shift $lang "print $lhs >> -1" " = 0" \
$rs_negative_shift_count
# Confirm shifting by 0 doesn't warn.
test_shift $lang "print/x $lhs << 0" " = $res"
test_shift $lang "print/x $lhs >> 0" " = $res"
test_lshift_tl $lang "print $lhs << 32" " = 0"
test_rshift_tl $lang "print $lhs >> 32" " = 0"
test_lshift_tl $lang "print $lhs << 33" " = 0"
test_rshift_tl $lang "print $lhs >> 33" " = 0"
}
}
# 64-bit lhs, signed and unsigned.
with_test_prefix "64-bit, invalid" {
foreach {lhs res} \
[list \
[make_int64 $lang 0x7fffffffffffffff] \
0x7fffffffffffffff \
\
[make_uint64 $lang 0xffffffffffffffff] \
0xffffffffffffffff] \
{
test_shift $lang "print $lhs << -1" " = 0" \
$ls_negative_shift_count
test_shift $lang "print $lhs >> -1" " = 0" \
$rs_negative_shift_count
# Confirm shifting by 0 doesn't warn.
test_shift $lang "print/x $lhs << 0" " = $res"
test_shift $lang "print/x $lhs >> 0" " = $res"
test_lshift_tl $lang "print $lhs << 64" " = 0"
test_rshift_tl $lang "print $lhs >> 64" " = 0"
test_lshift_tl $lang "print $lhs << 65" " = 0"
test_rshift_tl $lang "print $lhs >> 65" " = 0"
}
}
# Right shift a negative number by a negative amount.
with_test_prefix "neg lhs/rhs" {
test_shift $lang "print -1 >> -1" " = -1" $rs_negative_shift_count
test_shift $lang "print -4 >> -2" " = -1" $rs_negative_shift_count
}
# Check right shifting a negative value. For C++, this is
# implementation-defined, up until C++20. In most
# implementations, this performs an arithmetic right shift, so
# that the result remains negative. Currently, GDB does
# whatever the host's compiler does. If that turns out wrong
# for some host/target, then GDB should be taught to ask the
# target gdbarch what to do.
with_test_prefix "rsh neg lhs" {
test_shift $lang "print -1 >> 0" " = -1"
test_shift $lang "print -1 >> 1" " = -1"
test_shift $lang "print -8 >> 1" " = -4"
test_shift $lang "print [make_int64 $lang -8] >> 1" " = -4"
}
# Make sure an unsigned 64-bit value with high bit set isn't
# confused for a negative shift count in the warning messages.
with_test_prefix "max-uint64" {
test_lshift_tl $lang \
"print 1 << [make_uint64 $lang 0xffffffffffffffff]" " = 0"
test_rshift_tl $lang \
"print 1 >> [make_uint64 $lang 0xffffffffffffffff]" " = 0"
test_lshift_tl $lang \
"print -1 << [make_uint64 $lang 0xffffffffffffffff]" " = 0"
test_rshift_tl $lang \
"print -1 >> [make_uint64 $lang 0xffffffffffffffff]" " = -1"
}
}
}
test_shifts
|