Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests run-pass/backtrace.rs and run-pass/backtrace-debuginfo.rs fail on Windows (MinGW 64) #28447

Closed
petrochenkov opened this issue Sep 16, 2015 · 7 comments
Labels
O-windows Operating system: Windows O-windows-gnu Toolchain: GNU, Operating system: Windows

Comments

@petrochenkov
Copy link
Contributor

cc @Diggsey

Configure:

./configure

Build:

$ make check-notidy TESTNAME=backtrace.rs
cfg: version 1.5.0-dev (355daea40 2015-09-16)
cfg: build triple x86_64-pc-windows-gnu
cfg: host triples x86_64-pc-windows-gnu
cfg: target triples x86_64-pc-windows-gnu
cfg: host for x86_64-pc-windows-gnu is x86_64
cfg: os for x86_64-pc-windows-gnu is pc-windows-gnu
cfg: good valgrind for x86_64-pc-windows-gnu is
cfg: using CC=gcc (CFG_CC)
cfg: disabling valgrind run-pass tests
cfg: including test rules
cfg: javac not available, skipping lexer test...
run rpass [x86_64-pc-windows-gnu]: x86_64-pc-windows-gnu/stage2/bin/compiletest.exe
Could not extract GDB version from line 'GNU gdb (GDB) 7.10'

running 1 test
test [run-pass] run-pass/backtrace.rs ... FAILED

failures:

---- [run-pass] run-pass/backtrace.rs stdout ----

error: test run failed!
status: exit code: 101
command: PATH="x86_64-pc-windows-gnu/stage2/bin/rustlib/x86_64-pc-windows-gnu/lib;C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage2\bin;C:\msys64\mingw64\bin;C:\msys64\usr\local\bin;C:\msys64\usr\bin;C:\msys64\usr\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Skype\Phone;C:\Program Files\kdiff3;C:\msys64\usr\bin\site_perl;C:\msys64\usr\bin\vendor_perl;C:\msys64\usr\bin\core_perl" x86_64-pc-windows-gnu/test/run-pass\backtrace.stage2-x86_64-pc-windows-gnu.exe
stdout:
------------------------------------------

------------------------------------------
stderr:
------------------------------------------
thread '<main>' panicked at 'bad output: thread '<main>' panicked at 'explicit panic', C:/msys64/home/we/rust/src/test/run-pass/backtrace.rs:24
stack backtrace:
   0:         0x67f4154e - <unknown>
   1:         0x67f3e132 - <unknown>
   2:         0x67f0576e - <unknown>
   3:           0x401693 - <unknown>
   4:           0x40162f - <unknown>
   5:           0x4027bc - <unknown>
   6:         0x67f48488 - <unknown>
   7:         0x67f31693 - <unknown>
   8:         0x67f48397 - <unknown>
   9:           0x4013ec - <unknown>
  10:           0x40152a - <unknown>
  11:     0x7ffa0cb913d1 - <unknown>
', C:/msys64/home/we/rust/src/test/run-pass/backtrace.rs:55

------------------------------------------

thread '[run-pass] run-pass/backtrace.rs' panicked at 'explicit panic', C:/msys64/home/we/rust/src/compiletest\runtest.rs:1501



failures:
    [run-pass] run-pass/backtrace.rs

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured

thread '<main>' panicked at 'Some tests failed', C:/msys64/home/we/rust/src/compiletest/compiletest.rs:248
/home/we/rust/mk/tests.mk:764: recipe for target 'tmp/check-stage2-T-x86_64-pc-windows-gnu-H-x86_64-pc-windows-gnu-rpass.ok' failed
make: *** [tmp/check-stage2-T-x86_64-pc-windows-gnu-H-x86_64-pc-windows-gnu-rpass.ok] Error 101
@Diggsey
Copy link
Contributor

Diggsey commented Sep 17, 2015

There must be more to it: both of these tests pass on all of the buildbot machines, and for me locally. What version of windows are you running? Is your msys2 up-to-date? What's the value of PATH? Is this a fresh clone + compile of rust? Is there anything unusual about your build environment?

@petrochenkov
Copy link
Contributor Author

@Diggsey
Windows 8.1 Update 1, packages are up-to-date and msys2 distribution was from May, PATH is

$ echo $PATH
/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/ProgramData/chocolatey/bin:/c/Program Files (x86)/Skype/Phone:/c/Program Files/kdiff3:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

, not fresh but clean rebuild (make clean-all + directory x86_64-pc-windows-gnu removed), nothing unusual about my build environment as far as I know.

The line Could not extract GDB version from line 'GNU gdb (GDB) 7.10' bothers me though. What does your version of msys2 gdb says?

@petrochenkov
Copy link
Contributor Author

It still happens with a newly installed MSYS2 and a fresh clone of Rust.
(The GDB version is indeed parsed incorrectly and 7.10 can't be parsed because it has too many digits, but it is unrelated to the test failures)

@steveklabnik steveklabnik added O-windows Operating system: Windows O-windows-gnu Toolchain: GNU, Operating system: Windows labels Sep 28, 2015
@petrochenkov
Copy link
Contributor Author

Backtraces have been broken somewhere between 1.3 and 1.4.
I'll try to bisect the commit history, but it'll require a lot of time.

1.3:

we@we-pc MINGW64 ~
$ rustc --version --verbose
rustc 1.3.0 (9a92aaf19 2015-09-15)
binary: rustc
commit-hash: 9a92aaf19a64603b02b4130fe52958cc12488900
commit-date: 2015-09-15
host: x86_64-pc-windows-gnu
release: 1.3.0

we@we-pc MINGW64 ~
$ RUST_BACKTRACE=1 rustc test.rs
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
thread 'rustc' panicked at 'No def'n found for DefId { krate: 0, node: 11 }Z in tcx.item_variance_map', ../src/librustc\middle\ty.rs:5152

stack backtrace:
   1:         0x6c6bbc58 - sys::backtrace::write::h9192fb96d06a04279Hs
   2:         0x6c6c582d - rt::unwind::register::hf2debafc128a8bbcxWw
   3:         0x6c685a6f - rt::unwind::begin_unwind_inner::hcb96b94c1db5c440GTw
   4:         0x6c6863f6 - rt::unwind::begin_unwind_fmt::h28f0332c3e8f2752MSw
   5:         0x70f40b75 - middle::ty::ctxt<'tcx>::item_variances::ha5d9a4cd6cc6326f2tb
   6:           0xca4f1c - check::FnCtxt<'a, 'tcx>::register_builtin_bound::h943db64912a2171cN2o
   7:           0xca27b2 - check::wf::CheckTypeWellFormedVisitor<'ccx, 'tcx>::new::hd82ea0334ef8c589c5j
   8:           0xca976e - check::wf::CheckTypeWellFormedVisitor<'ccx, 'tcx>.Visitor<'v>::visit_item::hc1d90458249ea5fdiCk
   9:           0xca9588 - check::wf::CheckTypeWellFormedVisitor<'ccx, 'tcx>.Visitor<'v>::visit_item::hc1d90458249ea5fdiCk
  10:           0xca8abb - check::wf::CheckTypeWellFormedVisitor<'ccx, 'tcx>.Visitor<'v>::visit_item::hc1d90458249ea5fdiCk
  11:           0xcc6fd9 - check::check_item_types::h3739f0f18758ac32ewn
  12:           0xd83344 - check_crate::h8ef65f4638074ba9laD
  13:         0x64b2651b - driver::assign_node_ids_and_map::hae564fdeb2ac85907Da
  14:         0x64b24c34 - driver::assign_node_ids_and_map::hae564fdeb2ac85907Da
  15:         0x64b1f3b7 - driver::assign_node_ids_and_map::hae564fdeb2ac85907Da
  16:         0x64b02766 - driver::compile_input::h23ee8d2f30f41c3bTba
  17:         0x64beb53c - run_compiler::h13fbc9ea0e4d6074A7b
  18:         0x64be92b7 - run::h6cfcc1625a0298d9g7b
  19:         0x64be8c59 - run::h6cfcc1625a0298d9g7b
  20:         0x6c6af014 - rt::unwind::try::inner_try::hac50b220aa363696zPw
  21:         0x64be8e27 - run::h6cfcc1625a0298d9g7b
  22:         0x6c6c344e - sys::process::Command::cwd::h809ce520f020ef4eyzv
  23:     0x7ffa1fa313d2 - BaseThreadInitThunk

1.4 beta:

we@we-pc MINGW64 ~
$ rustc --version --verbose
rustc 1.4.0-beta.3 (20eba406f 2015-10-16)
binary: rustc
commit-hash: 20eba406fa504192cbca531a18117290559d0e34
commit-date: 2015-10-16
host: x86_64-pc-windows-gnu
release: 1.4.0-beta.3

we@we-pc MINGW64 ~
$ RUST_BACKTRACE=1 rustc test.rs
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/m                                                                                                    aster/CONTRIBUTING.md#bug-reports
thread 'rustc' panicked at 'No def'n found for DefId { krate: 0, node: 11 => Z }                                                                                                     in tcx.item_variance_map', ../src/librustc\middle\ty/mod.rs:1901

stack backtrace:
   0:         0x6c6414fe - <unknown>
   1:         0x6c63deb9 - <unknown>
   2:         0x6c60571e - <unknown>
   3:         0x6c60608b - <unknown>
   4:         0x6e003906 - <unknown>
   5:         0x6868191b - <unknown>
   6:         0x6867b772 - <unknown>
   7:         0x6868601f - <unknown>
   8:         0x6868565a - <unknown>
   9:         0x686bbc18 - <unknown>
  10:         0x6877045a - <unknown>
  11:         0x670087c2 - <unknown>
  12:         0x66feab77 - <unknown>
  13:         0x66fe64d8 - <unknown>
  14:         0x66fc2cf8 - <unknown>
  15:         0x67131c07 - <unknown>
  16:         0x6712f3f1 - <unknown>
  17:         0x6712ede8 - <unknown>
  18:         0x6c631643 - <unknown>
  19:         0x6712ef83 - <unknown>
  20:         0x6c64721d - <unknown>
  21:     0x7ffa1fa313d1 - <unknown>

@petrochenkov
Copy link
Contributor Author

So, bisecting wasn't even needed, the offending commit is d4fc3ec (cc @Diggsey ), it turns

   1:         0x678fd44e - sys::backtrace::write::h3024f58ca3f5eb766zs
   2:         0x67907245 - rt::unwind::register::hadf032378d991618hLw
   3:         0x678c56ef - rt::unwind::begin_unwind_inner::hc42fb035aa2af464qIw
   4:         0x678c607c - rt::unwind::begin_unwind_fmt::h59c34472f3dfb703wHw
   5:         0x66709346 - middle::ty::ctxt<'tcx>::item_variances::hfeb92355602d7732D9b
   6:         0x6e6c517c - check::FnCtxt<'a, 'tcx>::register_old_wf_obligation::hbd2744ade1fcd5024fr
   7:         0x6e6bd536 - check::upvar::AdjustBorrowKind<'a, 'tcx>.euv..Delegate<'tcx>::mutate::h0a132986484fdfdabik
   8:         0x6e6c9880 - check::wf::CheckTypeWellFormedVisitor<'ccx, 'tcx>.Visitor<'v>::visit_item::hce8d60b81e9327f72Pk
   9:         0x6e6c8ebb - check::wf::CheckTypeWellFormedVisitor<'ccx, 'tcx>.Visitor<'v>::visit_item::hce8d60b81e9327f72Pk
  10:         0x6e700dd9 - check::check_wf_old::h8b89178a347e371fPsp
  11:         0x6e7b849b - check_crate::h36559b16f5d9854cpDE
  12:         0x69dca9e7 - driver::make_map::h23f7851fbeae8a9a3Ha
  13:         0x69dab073 - driver::make_map::h23f7851fbeae8a9a3Ha
  14:         0x69da6890 - driver::make_map::h23f7851fbeae8a9a3Ha
  15:         0x69d82f8f - driver::compile_input::h09923605db044031Yba
  16:         0x69ef81b8 - run_compiler::h10e9fc42d3924e41eqc
  17:         0x69ef5b10 - run::hcb4846b1cc934675Upc
  18:         0x69ef5589 - run::hcb4846b1cc934675Upc
  19:         0x678f07d4 - rt::unwind::try::inner_try::hbc6dcc50ff381facjEw
  20:         0x69ef5724 - run::hcb4846b1cc934675Upc
  21:         0x67904f1e - rt::util::report_overflow::h9b383e344a59c1e8Zow
  22:     0x7ffa1fa313d2 - BaseThreadInitThunk

into

   0:         0x678fe3ce - <unknown>
   1:         0x679070d4 - <unknown>
   2:         0x678c56ee - <unknown>
   3:         0x678c607b - <unknown>
   4:         0x66709265 - <unknown>
   5:         0x6e6c517b - <unknown>
   6:         0x6e6bd535 - <unknown>
   7:         0x6e6c987f - <unknown>
   8:         0x6e6c8eba - <unknown>
   9:         0x6e700dd8 - <unknown>
  10:         0x6e7b849a - <unknown>
  11:         0x69dca9e6 - <unknown>
  12:         0x69dab072 - <unknown>
  13:         0x69da688f - <unknown>
  14:         0x69d82f8e - <unknown>
  15:         0x69ef84c7 - <unknown>
  16:         0x69ef5e1f - <unknown>
  17:         0x69ef5898 - <unknown>
  18:         0x678f07d3 - <unknown>
  19:         0x69ef5a33 - <unknown>
  20:         0x67904dad - <unknown>
  21:     0x7ffa1fa313d1 - <unknown>

I'll try to find out what happens, but I don't know much about backtraces, probably @Diggsey will be able to fix it faster.

@Diggsey
Copy link
Contributor

Diggsey commented Oct 27, 2015

I still can't reproduce this: can you try attaching gdb and breaking at the same place in the program, it would be useful to see the backtrace gdb generates, since both gdb and rust use libbacktrace.

For reference, this commit changes backtraces for windows-gnu entirely: previously it used the windows API to get symbol information. This method looks at the symbol table to turn addresses into symbol names. However, it is incapable of giving file and line number information.

This commit changes it to use libbacktrace: libbacktrace was already used on non-windows targets, and relies on the embedded dwarf debugging information to turn addresses into symbol names. If that debug information is missing, or for some reason libbacktrace can't read it, then there will be cases where backtraces are worse off with this change.

@petrochenkov
Copy link
Contributor Author

@Diggsey
gdb works:
(this is stage1, so no unwinding)

we@we-pc MINGW64 ~
$ gdb --args rust/x86_64-pc-windows-gnu/stage1/bin/rustc test.rs
GNU gdb (GDB) 7.10
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from rust/x86_64-pc-windows-gnu/stage1/bin/rustc...done.
(gdb) run
Starting program: C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc.exe test.rs
[New Thread 16184.0x2e60]
[New Thread 16184.0x340c]
gdb: unknown target exception 0xe1525354 at 0x7ffa1f5d871c

Program received signal ?, Unknown signal.
[Switching to Thread 16184.0x340c]
0x00007ffa1f5d871c in RaiseException ()
   from C:\Windows\system32\KernelBase.dll
(gdb) bt
#0  0x00007ffa1f5d871c in RaiseException ()
   from C:\Windows\system32\KernelBase.dll
#1  0x000000006cd7e2dd in rt::unwind::imp::panic::h138fc8f543c5f59afHw ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\std-a5fc0d6c.dll
#2  0x000000006cd7e729 in rust_panic ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\std-a5fc0d6c.dll
#3  0x000000006cd44f56 in rt::unwind::begin_unwind_inner::h5fc0152d0aafc9b7qWw
    ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\std-a5fc0d6c.dll
#4  0x000000006cd45711 in rt::unwind::begin_unwind_fmt::hfba13529a9c1d09ewVw
    ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\std-a5fc0d6c.dll
#5  0x0000000069f97469 in middle::ty::ctxt$LT$$u27$tcx$GT$::item_variances::hd71df34ac43909d1D9b ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc-a5fc0d6c.dll
#6  0x0000000063caef9c in check::wf::CheckTypeWellFormedVisitor$LT$$u27$ccx$C$$u20$$u27$tcx$GT$::check_variances_for_type_defn::h70494ae83f1695746Dk ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_typeck-a5fc0d6c.dll
#7  0x0000000063ca99ab in check::wf::CheckTypeWellFormedVisitor$LT$$u27$ccx$C$$u20$$u27$tcx$GT$::check_item_well_formed::h0cfd3f328e35e059hlk ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_typeck-a5fc0d6c.dll
#8  0x0000000063cb2d20 in visit::walk_expr::h1538854274586118031 ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_typeck-a5fc0d6c.dll
#9  0x0000000063cb235b in visit::walk_item::h3466652534645482253 ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_typeck-a5fc0d6c.dll
#10 0x0000000063cdc3dd in check::check_wf_old::h5c158d441ae75523Psp ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_typeck-a5fc0d6c.dll
#11 0x0000000063d752fa in check_crate::hcc545635754bdf91pDE ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_typeck-a5fc0d6c.dll
#12 0x000000006c660def in driver::phase_3_run_analysis_passes::closure.21717
    ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_driver-a5fc0d6c.dll
#13 0x000000006c65f8a0 in driver::phase_3_run_analysis_passes::h16750792111201762936 ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_driver-a5fc0d6c.dll
#14 0x000000006c6435a5 in driver::compile_input::h7319900f2a3481bdYba ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_driver-a5fc0d6c.dll
#15 0x000000006c70ab66 in run_compiler::hcd12017dca535bb4eqc ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_driver-a5fc0d6c.dll
#16 0x000000006c7087d0 in boxed::F.FnBox$LT$A$GT$::call_box::h10755116803638224322 ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_driver-a5fc0d6c.dll
#17 0x000000006c708249 in rt::unwind::try::try_fn::h152431577491858085 ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_driver-a5fc0d6c.dll
#18 0x000000006cd6b14b in rt::unwind::try::inner_try::h8e2b93ec3e97781fjSw ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\std-a5fc0d6c.dll
#19 0x000000006c7083d1 in boxed::F.FnBox$LT$A$GT$::call_box::h8720629305512333058 ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\rustc_driver-a5fc0d6c.dll
#20 0x000000006cd7cd25 in sys::thread::Thread::new::thread_start::h521642f36151b154A4v ()
   from C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage1\bin\std-a5fc0d6c.dll
#21 0x00007ffa1fa313d2 in KERNEL32!BaseThreadInitThunk ()
   from C:\Windows\system32\kernel32.dll
#22 0x00007ffa223c5454 in ntdll!RtlUserThreadStart ()
   from C:\Windows\SYSTEM32\ntdll.dll
#23 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

petrochenkov added a commit to petrochenkov/rust that referenced this issue Jan 15, 2016
New:
Fix for rust-lang#28447

Merged:
openbsd support: rust-lang@fcb30a0
bitrig integration: rust-lang@cd8f317

Not merged:
rust-lang@d4fc3ec
@Diggsey says this change was unintended (rust-lang#30666 (comment))
bors added a commit that referenced this issue Jan 16, 2016
@dhuseby @semarie Please, confirm that all Rust-specific changes in the second commit are still required.
(It would be ideal to have an unpatched copy of libbacktrace straight from the gcc repo.)

Fixes #28447

r? @alexcrichton
alexcrichton added a commit to alexcrichton/libbacktrace that referenced this issue Jul 25, 2017
This updates the local declaration of `str_size` to always be 4 bytes instead of
platform-dependent as its initialization later on only fills in 4 bytes instead
of all the bytes of `size_t`.

Originally reported as rust-lang/rust#28447 this was fixed in
rust-lang/rust#30908
alexcrichton added a commit to alexcrichton/rust that referenced this issue Jul 17, 2020
This commit is a proof-of-concept for switching the standard library's
backtrace symbolication mechanism on most platforms from libbacktrace to
gimli. The standard library's support for `RUST_BACKTRACE=1` requires
in-process parsing of object files and DWARF debug information to
interpret it and print the filename/line number of stack frames as part
of a backtrace.

Historically this support in the standard library has come from a
library called "libbacktrace". The libbacktrace library seems to have
been extracted from gcc at some point and is written in C. We've had a
lot of issues with libbacktrace over time, unfortunately, though. The
library does not appear to be actively maintained since we've had
patches sit for months-to-years without comments. We have discovered a
good number of soundness issues with the library itself, both when
parsing valid DWARF as well as invalid DWARF. This is enough of an issue
that the libs team has previously decided that we cannot feed untrusted
inputs to libbacktrace. This also doesn't take into account the
portability of libbacktrace which has been difficult to manage and
maintain over time. While possible there are lots of exceptions and it's
the main C dependency of the standard library right now.

For years it's been the desire to switch over to a Rust-based solution
for symbolicating backtraces. It's been assumed that we'll be using the
Gimli family of crates for this purpose, which are targeted at safely
and efficiently parsing DWARF debug information. I've been working
recently to shore up the Gimli support in the `backtrace` crate. As of a
few weeks ago the `backtrace` crate, by default, uses Gimli when loaded
from crates.io. This transition has gone well enough that I figured it
was time to start talking seriously about this change to the standard
library.

This commit is a preview of what's probably the best way to integrate
the `backtrace` crate into the standard library with the Gimli feature
turned on. While today it's used as a crates.io dependency, this commit
switches the `backtrace` crate to a submodule of this repository which
will need to be updated manually. This is not done lightly, but is
thought to be the best solution. The primary reason for this is that the
`backtrace` crate needs to do some pretty nontrivial filesystem
interactions to locate debug information. Working without `std::fs` is
not an option, and while it might be possible to do some sort of
trait-based solution when prototyped it was found to be too unergonomic.
Using a submodule allows the `backtrace` crate to build as a submodule
of the `std` crate itself, enabling it to use `std::fs` and such.

Otherwise this adds new dependencies to the standard library. This step
requires extra attention because this means that these crates are now
going to be included with all Rust programs by default. It's important
to note, however, that we're already shipping libbacktrace with all Rust
programs by default and it has a bunch of C code implementing all of
this internally anyway, so we're basically already switching
already-shipping functionality to Rust from C.

* `object` - this crate is used to parse object file headers and
  contents. Very low-level support is used from this crate and almost
  all of it is disabled. Largely we're just using struct definitions as
  well as convenience methods internally to read bytes and such.

* `addr2line` - this is the main meat of the implementation for
  symbolication. This crate depends on `gimli` for DWARF parsing and
  then provides interfaces needed by the `backtrace` crate to turn an
  address into a filename / line number. This crate is actually pretty
  small (fits in a single file almost!) and mirrors most of what
  `dwarf.c` does for libbacktrace.

* `miniz_oxide` - the libbacktrace crate transparently handles
  compressed debug information which is compressed with zlib. This crate
  is used to decompress compressed debug sections.

* `gimli` - not actually used directly, but a dependency of `addr2line`.

* `adler32`- not used directly either, but a dependency of
  `miniz_oxide`.

The goal of this change is to improve the safety of backtrace
symbolication in the standard library, especially in the face of
possibly malformed DWARF debug information. Even to this day we're still
seeing segfaults in libbacktrace which could possibly become security
vulnerabilities. This change should almost entirely eliminate this
possibility whilc also paving the way forward to adding more features
like split debug information.

Some references for those interested are:

* Original addition of libbacktrace - rust-lang#12602
* OOM with libbacktrace - rust-lang#24231
* Backtrace failure due to use of uninitialized value - rust-lang#28447
* Possibility to feed untrusted data to libbacktrace - rust-lang#21889
* Soundness fix for libbacktrace - rust-lang#33729
* Crash in libbacktrace - rust-lang#39468
* Support for macOS, never merged - ianlancetaylor/libbacktrace#2
* Performance issues with libbacktrace - rust-lang#29293, rust-lang#37477
* Update procedure is quite complicated due to how many patches we
  need to carry - rust-lang#50955
* Libbacktrace doesn't work on MinGW with dynamic libs - rust-lang#71060
* Segfault in libbacktrace on macOS - rust-lang#71397

Switching to Rust will not make us immune to all of these issues. The
crashes are expected to go away, but correctness and performance may
still have bugs arise. The gimli and `backtrace` crates, however, are
actively maintained unlike libbacktrace, so this should enable us to at
least efficiently apply fixes as situations come up.
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 18, 2020
…Simulacrum

std: Switch from libbacktrace to gimli

This commit is a proof-of-concept for switching the standard library's
backtrace symbolication mechanism on most platforms from libbacktrace to
gimli. The standard library's support for `RUST_BACKTRACE=1` requires
in-process parsing of object files and DWARF debug information to
interpret it and print the filename/line number of stack frames as part
of a backtrace.

Historically this support in the standard library has come from a
library called "libbacktrace". The libbacktrace library seems to have
been extracted from gcc at some point and is written in C. We've had a
lot of issues with libbacktrace over time, unfortunately, though. The
library does not appear to be actively maintained since we've had
patches sit for months-to-years without comments. We have discovered a
good number of soundness issues with the library itself, both when
parsing valid DWARF as well as invalid DWARF. This is enough of an issue
that the libs team has previously decided that we cannot feed untrusted
inputs to libbacktrace. This also doesn't take into account the
portability of libbacktrace which has been difficult to manage and
maintain over time. While possible there are lots of exceptions and it's
the main C dependency of the standard library right now.

For years it's been the desire to switch over to a Rust-based solution
for symbolicating backtraces. It's been assumed that we'll be using the
Gimli family of crates for this purpose, which are targeted at safely
and efficiently parsing DWARF debug information. I've been working
recently to shore up the Gimli support in the `backtrace` crate. As of a
few weeks ago the `backtrace` crate, by default, uses Gimli when loaded
from crates.io. This transition has gone well enough that I figured it
was time to start talking seriously about this change to the standard
library.

This commit is a preview of what's probably the best way to integrate
the `backtrace` crate into the standard library with the Gimli feature
turned on. While today it's used as a crates.io dependency, this commit
switches the `backtrace` crate to a submodule of this repository which
will need to be updated manually. This is not done lightly, but is
thought to be the best solution. The primary reason for this is that the
`backtrace` crate needs to do some pretty nontrivial filesystem
interactions to locate debug information. Working without `std::fs` is
not an option, and while it might be possible to do some sort of
trait-based solution when prototyped it was found to be too unergonomic.
Using a submodule allows the `backtrace` crate to build as a submodule
of the `std` crate itself, enabling it to use `std::fs` and such.

Otherwise this adds new dependencies to the standard library. This step
requires extra attention because this means that these crates are now
going to be included with all Rust programs by default. It's important
to note, however, that we're already shipping libbacktrace with all Rust
programs by default and it has a bunch of C code implementing all of
this internally anyway, so we're basically already switching
already-shipping functionality to Rust from C.

* `object` - this crate is used to parse object file headers and
  contents. Very low-level support is used from this crate and almost
  all of it is disabled. Largely we're just using struct definitions as
  well as convenience methods internally to read bytes and such.

* `addr2line` - this is the main meat of the implementation for
  symbolication. This crate depends on `gimli` for DWARF parsing and
  then provides interfaces needed by the `backtrace` crate to turn an
  address into a filename / line number. This crate is actually pretty
  small (fits in a single file almost!) and mirrors most of what
  `dwarf.c` does for libbacktrace.

* `miniz_oxide` - the libbacktrace crate transparently handles
  compressed debug information which is compressed with zlib. This crate
  is used to decompress compressed debug sections.

* `gimli` - not actually used directly, but a dependency of `addr2line`.

* `adler32`- not used directly either, but a dependency of
  `miniz_oxide`.

The goal of this change is to improve the safety of backtrace
symbolication in the standard library, especially in the face of
possibly malformed DWARF debug information. Even to this day we're still
seeing segfaults in libbacktrace which could possibly become security
vulnerabilities. This change should almost entirely eliminate this
possibility whilc also paving the way forward to adding more features
like split debug information.

Some references for those interested are:

* Original addition of libbacktrace - rust-lang#12602
* OOM with libbacktrace - rust-lang#24231
* Backtrace failure due to use of uninitialized value - rust-lang#28447
* Possibility to feed untrusted data to libbacktrace - rust-lang#21889
* Soundness fix for libbacktrace - rust-lang#33729
* Crash in libbacktrace - rust-lang#39468
* Support for macOS, never merged - ianlancetaylor/libbacktrace#2
* Performance issues with libbacktrace - rust-lang#29293, rust-lang#37477
* Update procedure is quite complicated due to how many patches we
  need to carry - rust-lang#50955
* Libbacktrace doesn't work on MinGW with dynamic libs - rust-lang#71060
* Segfault in libbacktrace on macOS - rust-lang#71397

Switching to Rust will not make us immune to all of these issues. The
crashes are expected to go away, but correctness and performance may
still have bugs arise. The gimli and `backtrace` crates, however, are
actively maintained unlike libbacktrace, so this should enable us to at
least efficiently apply fixes as situations come up.

---

I want to note that my purpose for creating a PR here is to start a conversation about this. I think that all the various pieces are in place that this is compelling enough that I think this transition should be talked about seriously. There are a number of items which still need to be addressed before actually merging this PR, however:

* [ ] `gimli` needs to be published to crates.io
* [ ] `addr2line` needs a publish
* [ ] `miniz_oxide` needs a publish
* [ ] Tests probably shouldn't recommend the `gimli` crate's traits for implementing
* [ ] The `backtrace` crate's branch changes need to be merged to the master branch (rust-lang/backtrace-rs#349)
* [ ] The support for `libbacktrace` on some platforms needs to be audited to see if we should support more strategies in the gimli implementation - rust-lang/backtrace-rs#325, rust-lang/backtrace-rs#326, rust-lang/backtrace-rs#350, rust-lang/backtrace-rs#351

Most of the merging/publishing I'm not actively pushing on right now. It's a bit wonky for crates to support libstd so I'm holding off on pulling the trigger everywhere until there's a bit more discussion about how to go through with this. Namely rust-lang/backtrace-rs#349 I'm going to hold off merging until we decide to go through with the submodule strategy.

In any case this is a pretty major change, so I suspect that the compiler team is likely going to be interested in this. I don't mean to force changes by dumping a bunch of code by any means. Integration of external crates into the standard library is so difficult I wanted to have a proof-of-concept to review while talking about whether to do this at all (hence the PR), but I'm more than happy to follow any processes needed to merge this. I must admit though that I'm not entirely sure myself at this time what the process would be to decide to merge this, so I'm hoping others can help me figure that out!
alexcrichton added a commit to alexcrichton/rust that referenced this issue Jul 28, 2020
This commit is a proof-of-concept for switching the standard library's
backtrace symbolication mechanism on most platforms from libbacktrace to
gimli. The standard library's support for `RUST_BACKTRACE=1` requires
in-process parsing of object files and DWARF debug information to
interpret it and print the filename/line number of stack frames as part
of a backtrace.

Historically this support in the standard library has come from a
library called "libbacktrace". The libbacktrace library seems to have
been extracted from gcc at some point and is written in C. We've had a
lot of issues with libbacktrace over time, unfortunately, though. The
library does not appear to be actively maintained since we've had
patches sit for months-to-years without comments. We have discovered a
good number of soundness issues with the library itself, both when
parsing valid DWARF as well as invalid DWARF. This is enough of an issue
that the libs team has previously decided that we cannot feed untrusted
inputs to libbacktrace. This also doesn't take into account the
portability of libbacktrace which has been difficult to manage and
maintain over time. While possible there are lots of exceptions and it's
the main C dependency of the standard library right now.

For years it's been the desire to switch over to a Rust-based solution
for symbolicating backtraces. It's been assumed that we'll be using the
Gimli family of crates for this purpose, which are targeted at safely
and efficiently parsing DWARF debug information. I've been working
recently to shore up the Gimli support in the `backtrace` crate. As of a
few weeks ago the `backtrace` crate, by default, uses Gimli when loaded
from crates.io. This transition has gone well enough that I figured it
was time to start talking seriously about this change to the standard
library.

This commit is a preview of what's probably the best way to integrate
the `backtrace` crate into the standard library with the Gimli feature
turned on. While today it's used as a crates.io dependency, this commit
switches the `backtrace` crate to a submodule of this repository which
will need to be updated manually. This is not done lightly, but is
thought to be the best solution. The primary reason for this is that the
`backtrace` crate needs to do some pretty nontrivial filesystem
interactions to locate debug information. Working without `std::fs` is
not an option, and while it might be possible to do some sort of
trait-based solution when prototyped it was found to be too unergonomic.
Using a submodule allows the `backtrace` crate to build as a submodule
of the `std` crate itself, enabling it to use `std::fs` and such.

Otherwise this adds new dependencies to the standard library. This step
requires extra attention because this means that these crates are now
going to be included with all Rust programs by default. It's important
to note, however, that we're already shipping libbacktrace with all Rust
programs by default and it has a bunch of C code implementing all of
this internally anyway, so we're basically already switching
already-shipping functionality to Rust from C.

* `object` - this crate is used to parse object file headers and
  contents. Very low-level support is used from this crate and almost
  all of it is disabled. Largely we're just using struct definitions as
  well as convenience methods internally to read bytes and such.

* `addr2line` - this is the main meat of the implementation for
  symbolication. This crate depends on `gimli` for DWARF parsing and
  then provides interfaces needed by the `backtrace` crate to turn an
  address into a filename / line number. This crate is actually pretty
  small (fits in a single file almost!) and mirrors most of what
  `dwarf.c` does for libbacktrace.

* `miniz_oxide` - the libbacktrace crate transparently handles
  compressed debug information which is compressed with zlib. This crate
  is used to decompress compressed debug sections.

* `gimli` - not actually used directly, but a dependency of `addr2line`.

* `adler32`- not used directly either, but a dependency of
  `miniz_oxide`.

The goal of this change is to improve the safety of backtrace
symbolication in the standard library, especially in the face of
possibly malformed DWARF debug information. Even to this day we're still
seeing segfaults in libbacktrace which could possibly become security
vulnerabilities. This change should almost entirely eliminate this
possibility whilc also paving the way forward to adding more features
like split debug information.

Some references for those interested are:

* Original addition of libbacktrace - rust-lang#12602
* OOM with libbacktrace - rust-lang#24231
* Backtrace failure due to use of uninitialized value - rust-lang#28447
* Possibility to feed untrusted data to libbacktrace - rust-lang#21889
* Soundness fix for libbacktrace - rust-lang#33729
* Crash in libbacktrace - rust-lang#39468
* Support for macOS, never merged - ianlancetaylor/libbacktrace#2
* Performance issues with libbacktrace - rust-lang#29293, rust-lang#37477
* Update procedure is quite complicated due to how many patches we
  need to carry - rust-lang#50955
* Libbacktrace doesn't work on MinGW with dynamic libs - rust-lang#71060
* Segfault in libbacktrace on macOS - rust-lang#71397

Switching to Rust will not make us immune to all of these issues. The
crashes are expected to go away, but correctness and performance may
still have bugs arise. The gimli and `backtrace` crates, however, are
actively maintained unlike libbacktrace, so this should enable us to at
least efficiently apply fixes as situations come up.
dscho pushed a commit to dscho/libbacktrace that referenced this issue Jul 18, 2022
This updates the local declaration of `str_size` to always be 4 bytes instead of
platform-dependent as its initialization later on only fills in 4 bytes instead
of all the bytes of `size_t`.

Originally reported as rust-lang/rust#28447 this was fixed in
rust-lang/rust#30908
johnsonjh added a commit to johnsonjh/libbacktrace that referenced this issue Oct 18, 2022
… of uninitialized bytes in pecoff.c; 3) Fix a use of undefined memory in pecoff.c

1) Note: as we target MINGW here, we still want to look up the symbols via
the DWARF method (the native Windows way would be to call the
SymFromAddr() function, but that would require .pdb files which MINGW
does not produce).
Signed-off-by: Johannes Schindelin <[email protected]>

2) This updates the local declaration of `str_size` to always be 4 bytes instead of
platform-dependent as its initialization later on only fills in 4 bytes instead
of all the bytes of `size_t`.
Originally reported as rust-lang/rust#28447 this was fixed in
rust-lang/rust#30908

3) In rust-lang/rust#39468 it was discovered that this could cause a crash in
libbacktrace due to freeing uninitialized memory, and this specific instance was
fixed in rust-lang/rust#39509
dscho pushed a commit to dscho/libbacktrace that referenced this issue Apr 8, 2024
This updates the local declaration of `str_size` to always be 4 bytes instead of
platform-dependent as its initialization later on only fills in 4 bytes instead
of all the bytes of `size_t`.

Originally reported as rust-lang/rust#28447 this was fixed in
rust-lang/rust#30908
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows O-windows-gnu Toolchain: GNU, Operating system: Windows
Projects
None yet
Development

No branches or pull requests

3 participants