read_leb128 overflow checking

There is a tiny error left in dwarf.c:read_leb128 after Nick fixed the
signed overflow problem in code I wrote.  It's to do with sleb128
values that have unnecessary excess bytes.  For example, -1 is
represented as 0x7f, the most efficient encoding, but also as
0xff,0x7f or 0xff,0xff,0x7f and so on.  None of these sequences
overflow any size signed value, but read_leb128 will report an
overflow given enough excess bytes.  This patch fixes that problem,
and since the proper test for signed values with excess bytes can
easily be adapted to also test a sleb byte with just some bits that
overflow the result, I changed the code to not use signed right
shifts.  (The C standard ISO/IEC 9899:1999 6.5.7 says signed right
shifts of negative values have an implementation defined value.  A
long time ago I even used a C compiler for a certain microprocessor
that always did unsigned right shifts.  Mind you, it is very unlikely
to be compiling binutils with such a compiler.)

bfd/
	* wasm-module.c: Guard include of limits.h.
	(CHAR_BIT): Provide backup define.
	(wasm_read_leb128): Use CHAR_BIT to size "result" in bits.
	Correct signed overflow checking.
opcodes/
	* wasm32-dis.c: Include limits.h.
	(CHAR_BIT): Provide backup define.
	(wasm_read_leb128): Use CHAR_BIT to size "result" in bits.
	Correct signed overflow checking.
binutils/
	* dwarf.c: Include limits.h.
	(CHAR_BIT): Provide backup define.
	(read_leb128): Use CHAR_BIT to size "result" in bits.  Correct
	signed overflow checking.
	* testsuite/binutils-all/pr26548.s,
	* testsuite/binutils-all/pr26548.d,
	* testsuite/binutils-all/pr26548e.d: New tests.
	* testsuite/binutils-all/readelf.exp: Run them.
	(readelf_test): Drop unused "xfails" parameter.  Update all uses.
This commit is contained in:
Alan Modra 2021-02-16 23:46:40 +10:30
parent 0d6aab7776
commit b9b204b311
10 changed files with 183 additions and 56 deletions

View file

@ -0,0 +1,13 @@
#source: pr26548.s
#as:
#readelf: -Wwi
#...
.*: Abbrev Number: 1 \(DW_TAG_variable\)
.* DW_AT_const_value : \(sdata\) 9223372036854775807
.*: Abbrev Number: 1 \(DW_TAG_variable\)
.* DW_AT_const_value : \(sdata\) -9223372036854775808
.*: Abbrev Number: 1 \(DW_TAG_variable\)
.* DW_AT_const_value : \(sdata\) -1
.*: Abbrev Number: 1 \(DW_TAG_variable\)
.* DW_AT_const_value : \(sdata\) 1

View file

@ -0,0 +1,40 @@
.section .debug_abbrev
1:
.uleb128 1
# DW_TAG_variable
.uleb128 0x34
# no children
.byte 0
# DW_AT_const_value
.uleb128 0x1c
# DW_FORM_sdata
.uleb128 0x0d
.byte 0,0
.byte 0
.section .debug_info
.4byte 9f-0f
0:
.2byte 4
.4byte 1b
.byte 8
.ifndef ERROR
.uleb128 1
.sleb128 0x7fffffffffffffff
.uleb128 1
.sleb128 -0x8000000000000000
.uleb128 1
# silly excess byte encoding of -1, no warning
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f
.uleb128 1
# silly excess byte encoding of 1, no warning
.byte 0x81,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0
.else
.uleb128 1
# encode +0x8000000000000000, readelf warning
.byte 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x01
.uleb128 1
# encode -0x8000000000000001, readelf warning
.byte 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3e
.endif
9:

View file

@ -0,0 +1,11 @@
#source: pr26548.s
#as: --defsym ERROR=1
#readelf: -Wwi
#...
.*: Abbrev Number: 1 \(DW_TAG_variable\)
.* DW_AT_const_value : \(sdata\).*LEB value.*
-9223372036854775808
.*: Abbrev Number: 1 \(DW_TAG_variable\)
.* DW_AT_const_value : \(sdata\).*LEB value.*
9223372036854775807

View file

@ -76,7 +76,7 @@ proc readelf_find_size { binary_file test_iteration } {
# Readelf's output is captured and then compared against the contents
# of the regexp_file-readelf_size if it exists, else regexp_file.
proc readelf_test { options binary_file regexp_file xfails } {
proc readelf_test { options binary_file regexp_file } {
global READELF
global READELFFLAGS
@ -89,10 +89,6 @@ proc readelf_test { options binary_file regexp_file xfails } {
send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
set got [remote_exec host "$READELF $READELFFLAGS $options $binary_file" "" "/dev/null" "readelf.out"]
foreach xfail $xfails {
setup_xfail $xfail
}
if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
fail "$testname (reason: unexpected output)"
send_log $got
@ -342,11 +338,11 @@ if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
readelf_find_size $tempfile 1
# Run the tests.
readelf_test -h $tempfile readelf.h {}
readelf_test -S $tempfile readelf.s {}
readelf_test -h $tempfile readelf.h
readelf_test -S $tempfile readelf.s
setup_xfail "mips-*-*irix*"
readelf_test -s $tempfile readelf.ss {}
readelf_test -r $tempfile readelf.r {}
readelf_test -s $tempfile readelf.ss
readelf_test -r $tempfile readelf.r
}
# Test demangling symbol names.
@ -361,7 +357,7 @@ if {![binutils_assemble $srcdir/$subdir/mangled.s tmpdir/mangled.o]} then {
}
# Run the test.
readelf_test {--syms --demangle --wide} $tempfile readelf.demangled {}
readelf_test {--syms --demangle --wide} $tempfile readelf.demangled
}
readelf_wi_test
@ -376,6 +372,19 @@ if {[which $AS] != 0} then {
run_dump_test "retain1b"
run_dump_test "readelf-maskos-1a"
run_dump_test "readelf-maskos-1b"
if {$readelf_size == 64 && ![istarget *-*-hpux*]} then {
run_dump_test pr26548
if {![binutils_assemble_flags $srcdir/$subdir/pr26548.s tmpdir/pr26548e.o {--defsym ERROR=1}]} then {
unsupported "pr26548e (failed to assemble)"
} else {
if ![is_remote host] {
set tempfile tmpdir/pr26548e.o
} else {
set tempfile [remote_download host tmpdir/pr26548e.o]
}
readelf_test -Wwi $tempfile pr26548e.d
}
}
}
# PR 13482 - Check for off-by-one errors when dumping .note sections.
@ -389,7 +398,7 @@ if {![binutils_assemble $srcdir/$subdir/version.s tmpdir/version.o]} then {
set tempfile [remote_download host tmpdir/version.o]
}
readelf_test -n $tempfile readelf.n {}
readelf_test -n $tempfile readelf.n
}
@ -405,7 +414,7 @@ if {![binutils_assemble $srcdir/$subdir/pr18374.s tmpdir/pr18374.o]} then {
set tempfile [remote_download host tmpdir/pr18374.o]
}
readelf_test --debug-dump=loc $tempfile readelf.pr18374 {}
readelf_test --debug-dump=loc $tempfile readelf.pr18374
}
@ -420,7 +429,7 @@ if {![binutils_assemble $srcdir/$subdir/locview-1.s tmpdir/locview-1.o]} then {
set tempfile [remote_download host tmpdir/locview-1.o]
}
readelf_test --debug-dump=loc $tempfile readelf.locview-1 {}
readelf_test --debug-dump=loc $tempfile readelf.locview-1
}
if {![binutils_assemble $srcdir/$subdir/locview-2.s tmpdir/locview-2.o]} then {
unsupported "readelf --debug-dump=loc locview-2 (failed to assemble)"
@ -432,7 +441,7 @@ if {![binutils_assemble $srcdir/$subdir/locview-2.s tmpdir/locview-2.o]} then {
set tempfile [remote_download host tmpdir/locview-2.o]
}
readelf_test --debug-dump=loc $tempfile readelf.locview-2 {}
readelf_test --debug-dump=loc $tempfile readelf.locview-2
}
@ -447,7 +456,7 @@ if {![binutils_assemble $srcdir/$subdir/z.s tmpdir/z.o]} then {
set tempfile [remote_download host tmpdir/z.o]
}
readelf_test {--decompress --hex-dump .debug_loc} $tempfile readelf.z {}
readelf_test {--decompress --hex-dump .debug_loc} $tempfile readelf.z
}
# Skip the next test for the RISCV architectures because they
@ -475,7 +484,7 @@ if ![istarget "riscv*-*-*"] then {
readelf_find_size $tempfile 2
# Make sure that readelf can decode the contents.
readelf_test -wiaoRlL $tempfile dw5.W {}
readelf_test -wiaoRlL $tempfile dw5.W
}
}
@ -494,7 +503,7 @@ if {![binutils_assemble_flags $srcdir/$subdir/dwarf-attributes.S tmpdir/dwarf-at
readelf_find_size $tempfile 3
# Make sure that readelf can decode the contents.
readelf_test -wi $tempfile dwarf-attributes.W {}
readelf_test -wi $tempfile dwarf-attributes.W
}
# Check that debug link sections can be dumped.
@ -507,7 +516,7 @@ if {![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o]} then {
set tempfile [remote_download host tmpdir/debuglink.o]
}
readelf_test {--debug-dump=links -wN} $tempfile readelf.k {}
readelf_test {--debug-dump=links -wN} $tempfile readelf.k
# Check that debug link sections can be followed.
if {![binutils_assemble $srcdir/$subdir/linkdebug.s tmpdir/linkdebug.debug]} then {
@ -517,7 +526,7 @@ if {![binutils_assemble $srcdir/$subdir/debuglink.s tmpdir/debuglink.o]} then {
set tempfile2 [remote_download host tmpdir/linkdebug.debug]
}
readelf_test {-wKis} $tempfile readelf.wKis {}
readelf_test {-wKis} $tempfile readelf.wKis
}
}
@ -530,7 +539,7 @@ if {![binutils_assemble $srcdir/$subdir/dwo.s tmpdir/dwo.o]} then {
set tempfile [remote_download host tmpdir/dwo.o]
}
readelf_test {--debug-dump=links --debug-dump=no-follow-links} $tempfile readelf.k2 {}
readelf_test {--debug-dump=links --debug-dump=no-follow-links} $tempfile readelf.k2
}
if {![binutils_assemble $srcdir/$subdir/zero-sec.s tmpdir/zero-sec.o]} then {
@ -542,7 +551,7 @@ if {![binutils_assemble $srcdir/$subdir/zero-sec.s tmpdir/zero-sec.o]} then {
set tempfile [remote_download host tmpdir/zero-sec.o]
}
readelf_test {--enable-checks --sections --wide} $tempfile zero-sec.r {}
readelf_test {--enable-checks --sections --wide} $tempfile zero-sec.r
}
if ![is_remote host] {
@ -555,6 +564,6 @@ if ![is_remote host] {
if {[catch "system \"bzip2 -dc $test > $tempfile\""] != 0} {
untested "bzip2 -dc ($testname)"
} else {
readelf_test {--debug-dump=macro -wN} $tempfile pr26112.r {}
readelf_test {--debug-dump=macro -wN} $tempfile pr26112.r
}
}