From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2205) id A74E23839C68; Sat, 4 Jun 2022 11:17:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A74E23839C68 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom de Vries To: gdb-cvs@sourceware.org Subject: [binutils-gdb] [gdb/testsuite] Test more values in gdb.base/parse_numbers.exp X-Act-Checkin: binutils-gdb X-Git-Author: Tom de Vries X-Git-Refname: refs/heads/master X-Git-Oldrev: 0c05610450e5f19f03245ac646587e35cab53e7b X-Git-Newrev: 1b4633f812b329863a523c4d798c2b55417b5144 Message-Id: <20220604111738.A74E23839C68@sourceware.org> Date: Sat, 4 Jun 2022 11:17:38 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Jun 2022 11:17:38 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D1b4633f812b3= 29863a523c4d798c2b55417b5144 commit 1b4633f812b329863a523c4d798c2b55417b5144 Author: Tom de Vries Date: Sat Jun 4 13:17:32 2022 +0200 [gdb/testsuite] Test more values in gdb.base/parse_numbers.exp =20 Currently we only test value 0xffffffffffffffff in test-case gdb.base/parse_numbers.exp. =20 Test more interesting values, both in decimal and hex format, as well as negative decimals for language modula-2. =20 This results in an increase in total tests from 15572 to 847448 (55 tim= es more tests). =20 Balance out the increase in runtime by reducing the number of architect= ures tested: only test one architecture per sizeof longlong/long/int/short combination, while keeping the possibility intact to run with all architectures (through setting a variable in the test-case) =20 Results in slight reduction of total tests: 15572 -> 13853. =20 Document interesting cases in the expected results: - wrapping from unsigned to signed - truncation - PR16377: using unsigned types to represent decimal constants in C =20 Running the test-case with a gdb build with -fsanitize=3Dundefined, we = trigger two UB errors in the modula-2 parser, filed as PR29163. =20 Tested on x86_64-linux with --enable-targets=3Dall. Diff: --- gdb/testsuite/gdb.base/parse_number.exp | 302 +++++++++++++++++++++++++++-= ---- 1 file changed, 260 insertions(+), 42 deletions(-) diff --git a/gdb/testsuite/gdb.base/parse_number.exp b/gdb/testsuite/gdb.ba= se/parse_number.exp index 197e27a8e9e..7c259e0a8a0 100644 --- a/gdb/testsuite/gdb.base/parse_number.exp +++ b/gdb/testsuite/gdb.base/parse_number.exp @@ -16,13 +16,179 @@ # Format hex value VAL for language LANG. =20 proc hex_for_lang { lang val } { - set val [regsub ^0x $val ""] + set neg_p [regexp ^- $val] + set val [regsub ^-?0x $val ""] if { $lang =3D=3D "modula-2" } { set val 0[string toupper $val]H } else { set val 0x$val } - return $val + if { $neg_p } { + return -$val + } else { + return $val + } +} + +# Determine whether N fits in type with TYPE_BITS and TYPE_SIGNEDNESS. + +proc fits_in_type { n type_bits type_signedness } { + if { $type_signedness =3D=3D "s" } { + set type_signed_p 1 + } elseif { $type_signedness =3D=3D "u" } { + set type_signed_p 0 + } else { + error "unreachable" + } + + if { $n < 0 && !$type_signed_p } { + # Can't fit a negative number in an unsigned type. + return 0 + } + + if { $n < 0} { + set n_sign -1 + set n [expr -$n] + } else { + set n_sign 1 + } + + set smax [expr 1 << ($type_bits - 1)]; + if { $n_sign =3D=3D -1 } { + # Negative number, signed type. + return [expr ($n <=3D $smax)] + } elseif { $n_sign =3D=3D 1 && $type_signed_p } { + # Positive number, signed type. + return [expr ($n < $smax)] + } elseif { $n_sign =3D=3D 1 && !$type_signed_p } { + # Positive number, unsigned type. + return [expr ($n >> $type_bits) =3D=3D 0] + } else { + error "unreachable" + } +} + +# Parse number N for LANG, and return a list of expected type and value. + +proc parse_number { lang n } { + global re_overflow + + set hex_p [regexp ^-?0x $n] + + global hex decimal + if { $hex_p } { + set any $hex + } else { + set any $decimal + } + + global sizeof_long_long sizeof_long sizeof_int + set long_long_bits [expr $sizeof_long_long * 8] + set long_bits [expr $sizeof_long * 8] + set int_bits [expr $sizeof_int * 8] + + if { $lang =3D=3D "rust" } { + if { [fits_in_type $n 32 s] } { + return [list "i32" $n] + } elseif { [fits_in_type $n 64 s] } { + return [list "i64" $n] + } elseif { [fits_in_type $n 64 u] } { + # Note: Interprets MAX_U64 as -1. + return [list "i64" $n] + } else { + # Overflow. + # Some truncated value, should be re_overflow. + return [list i64 $any] + } + } elseif { $lang =3D=3D "d" } { + if { [fits_in_type $n 32 s] } { + return [list int $n] + } elseif { [fits_in_type $n 32 u] } { + if { $hex_p } { + return [list uint $n] + } else { + return [list long $n] + } + } elseif { [fits_in_type $n 64 s] } { + return [list long $n] + } elseif { [fits_in_type $n 64 u] } { + return [list ulong $n] + } else { + # Overflow. + return [list $re_overflow $re_overflow] + } + } elseif { $lang =3D=3D "ada" } { + if { [fits_in_type $n $int_bits s] } { + return [list "<$sizeof_int-byte integer>" $n] + } elseif { [fits_in_type $n $long_bits s] } { + return [list "<$sizeof_long-byte integer>" $n] + } elseif { [fits_in_type $n $long_bits u] } { + return [list "<$sizeof_long-byte integer>" $n] + } elseif { [fits_in_type $n $long_long_bits s] } { + return [list "<$sizeof_long_long-byte integer>" $n] + } elseif { [fits_in_type $n $long_long_bits u] } { + # Note: Interprets ULLONG_MAX as -1. + return [list "<$sizeof_long_long-byte integer>" $n] + } else { + # Overflow. + # Some truncated value or re_overflow, should be re_overflow. + return [list "($re_overflow|<$decimal-byte integer>)" \ + ($re_overflow|$any)] + } + } elseif { $lang =3D=3D "modula-2" } { + if { [string equal $n -0] } { + # Note: 0 is CARDINAL, but -0 is an INTEGER. + return [list "INTEGER" 0] + } + if { $n < 0 && [fits_in_type $n $int_bits s] } { + return [list "INTEGER" $n] + } elseif { [fits_in_type $n $int_bits u] } { + return [list "CARDINAL" $n] + } else { + # Overflow. + # Some truncated value or re_overflow, should be re_overflow. + return [list ($re_overflow|CARDINAL|INTEGER) ($re_overflow|$any)] + } + } elseif { $lang =3D=3D "fortran" } { + if { [fits_in_type $n $int_bits s] } { + return [list int $n] + } elseif { [fits_in_type $n $int_bits u] } { + return [list "unsigned int" $n] + } elseif { [fits_in_type $n $long_bits s] } { + return [list long $n] + } elseif { [fits_in_type $n $long_bits u] } { + return [list "unsigned long" $n] + } else { + # Overflow. + # Some truncated value or re_overflow, should be re_overflow. + return [list "((unsigned )?(int|long)|$re_overflow)" \ + ($any|$re_overflow)] + } + } else { + # This is wrong for c-like languages. For the decimal case, we + # shouldn't use unsigned. + # See PR 16377. + if { [fits_in_type $n $int_bits s] } { + return [list int $n] + } elseif { [fits_in_type $n $int_bits u] } { + return [list "unsigned int" $n] + } elseif { [fits_in_type $n $long_bits s] } { + return [list long $n] + } elseif { [fits_in_type $n $long_bits u] } { + return [list "unsigned long" $n] + } elseif { [fits_in_type $n $long_long_bits s] } { + return [list "long long" $n] + } elseif { [fits_in_type $n $long_long_bits u] } { + return [list "unsigned long long" $n] + } else { + # Overflow. + # Some truncated value or re_overflow, should be re_overflow. + return [list "((unsigned )?(int|long)|$re_overflow)" \ + ($any|$re_overflow)] + } + } + + error "unreachable" } =20 # Test parsing numbers. Several language parsers had the same bug @@ -32,6 +198,10 @@ proc hex_for_lang { lang val } { # that GDB doesn't crash. ARCH is the architecture to test with. =20 proc test_parse_numbers {arch} { + global full_arch_testing + global tested_archs + global verbose + set arch_re [string_to_regexp $arch] gdb_test "set architecture $arch" "The target architecture is set to \= "$arch_re\"." =20 @@ -41,24 +211,21 @@ proc test_parse_numbers {arch} { # Figure out type sizes before matching patterns in the upcoming # tests. =20 + global sizeof_long_long sizeof_long sizeof_int sizeof_short set sizeof_long_long [get_sizeof "long long" -1] set sizeof_long [get_sizeof "long" -1] set sizeof_int [get_sizeof "int" -1] + set sizeof_short [get_sizeof "short" -1] =20 - if {$sizeof_long_long =3D=3D 8 && $sizeof_long =3D=3D 8} { - set 8B_type "unsigned long" - set fortran_type "unsigned long" - set fortran_value "0xffffffffffffffff" - } elseif {$sizeof_long_long =3D=3D 8 && $sizeof_long =3D=3D 4 && $size= of_int =3D=3D 4} { - set 8B_type "unsigned long long" - set fortran_type "unsigned int" - set fortran_value "0xffffffff" - } elseif {$sizeof_long =3D=3D 4 && $sizeof_int =3D=3D 2} { - set 8B_type "unsigned long long" - set fortran_type "unsigned long" - set fortran_value "0xffffffff" - } else { - error "missing case for long long =3D $sizeof_long_long, long =3D $sizeof= _long, int =3D $sizeof_int" + if { ! $full_arch_testing } { + set arch_id \ + [list $sizeof_long_long $sizeof_long $sizeof_long $sizeof_int \ + $sizeof_short] + if { [lsearch $tested_archs $arch_id] =3D=3D -1 } { + lappend tested_archs $arch_id + } else { + return + } } =20 foreach_with_prefix lang $::all_languages { @@ -72,34 +239,78 @@ proc test_parse_numbers {arch} { =20 gdb_test_no_output "set language $lang" =20 - set val "0xffffffffffffffff" - set val [hex_for_lang $lang $val] - if {$lang =3D=3D "fortran"} { - gdb_test "p/x $val" " =3D $fortran_value" - gdb_test "ptype $val" " =3D $fortran_type" - } elseif {$lang =3D=3D "modula-2"} { - gdb_test "p/x $val" "Overflow on numeric constant\\." + global re_overflow + if { $lang =3D=3D "modula-2" || $lang =3D=3D "fortran" } { + set re_overflow "Overflow on numeric constant\\." + } elseif { $lang =3D=3D "ada" } { + set re_overflow "Integer literal out of range" } else { - # D and Rust define their own built-in 64-bit types, and - # are thus always able to parse/print 64-bit values. - if {$sizeof_long_long =3D=3D 4 && $lang !=3D "d" && $lang !=3D "rust"= } { - set out "0xffffffff" - } else { - set out $val - } - gdb_test "p/x $val" " =3D $out" - if {$lang =3D=3D "ada"} { - if {$sizeof_long_long =3D=3D 4} { - gdb_test "ptype $val" " =3D <4-byte integer>" - } else { - gdb_test "ptype $val" " =3D <8-byte integer>" + set re_overflow "Numeric constant too large\\." + } + + set basevals { + 0xffffffffffffffff + 0x7fffffffffffffff + 0xffffffff + 0x7fffffff + 0xffff + 0x7fff + 0xff + 0x7f + 0x0 + } + + if { $lang =3D=3D "modula-2" } { + # Modula-2 is the only language that changes the type of an + # integral literal based on whether it's prefixed with "-", + # so test both scenarios. + set prefixes { "" "-" } + } else { + # For all the other languages, we'd just be testing the + # parsing twice, so just test the basic scenario of no prefix. + set prefixes { "" } + } + + foreach_with_prefix prefix $prefixes { + foreach baseval $basevals { + foreach offset { -2 -1 0 1 2 } { + set dec_val [expr $baseval + $offset] + set hex_val [format "0x%llx" $dec_val] + if { $dec_val < 0 } { + continue + } + + set dec_val $prefix$dec_val + lassign [parse_number $lang $dec_val] type out + if { $verbose >=3D 1 } { verbose -log "EXPECTED: $out" 2 } + if { $prefix =3D=3D "" } { + gdb_test "p/u $dec_val" "$out" + } else { + gdb_test "p/d $dec_val" "$out" + } + if { $verbose >=3D 1 } { verbose -log "EXPECTED: $type" 2 } + gdb_test "ptype $dec_val" "$type" + + if { $prefix =3D=3D "-" } { + # Printing with /x below means negative numbers are + # converted to unsigned representation. We could + # support this by updating the expected patterns. + # Possibly, we could print with /u and /d instead of + # /x here as well (which would also require updating + # expected patterns). + # For now, this doesn't seem worth the trouble, + # so skip. + continue + } + + set hex_val $prefix$hex_val + lassign [parse_number $lang $hex_val] type out + set hex_val [hex_for_lang $lang $hex_val] + if { $verbose >=3D 1 } { verbose -log "EXPECTED: $out" 2 } + gdb_test "p/x $hex_val" "$out" + if { $verbose >=3D 1 } { verbose -log "EXPECTED: $type" 2 } + gdb_test "ptype $hex_val" "$type" } - } elseif {$lang =3D=3D "d"} { - gdb_test "ptype $val" " =3D ulong" - } elseif {$lang =3D=3D "rust"} { - gdb_test "ptype $val" " =3D i64" - } else { - gdb_test "ptype $val" " =3D $8B_type" } } } @@ -119,6 +330,13 @@ gdb_assert {[llength $supported_archs] > 1} "at least = one architecture" =20 set all_languages [get_set_option_choices "set language"] =20 +# If 1, test each arch. If 0, test one arch for each sizeof +# short/int/long/longlong configuration. +# For a build with --enable-targets=3Dall, full_arch_testing =3D=3D 0 take= s 15s, +# while full_arch_testing =3D=3D 1 takes 9m20s. +set full_arch_testing 0 + +set tested_archs {} foreach_with_prefix arch $supported_archs { if {$arch =3D=3D "auto"} { # Avoid duplicate testing.