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

Long delay before exiting after reporting compilation errors in rustc 1.14.0 nightly when RUST_BACKTRACE=1 #37477

Closed
jchlapinski opened this issue Oct 29, 2016 · 36 comments · Fixed by #38984
Labels
regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jchlapinski
Copy link

jchlapinski commented Oct 29, 2016

When I have any compilation error(s) there is a rather long delay (around 14s on my machine) before rustc finally exits. Successful compilation is very fast.
I am on Ubuntu 16.04 in VMware Workstation 12.5 for Windows virtual machine.

Steps to reproduce:

$ touch test.rs
$ time rustc test.rs
error: main function not found

error: aborting due to previous error


real    0m13.423s
user    0m13.276s
sys 0m0.112s

Meta

rustc --version --verbose:

rustc 1.14.0-nightly (3f4408347 2016-10-27)
binary: rustc
commit-hash: 3f4408347d2109803edbf53c89c8bce575de4b67
commit-date: 2016-10-27
host: x86_64-unknown-linux-gnu
release: 1.14.0-nightly
LLVM version: 3.9
@jchlapinski
Copy link
Author

If it helps, that same version of rustc on Windows 10 works OK for me, there is no noticeable delay after compilation error reporting.

@jchlapinski
Copy link
Author

jchlapinski commented Oct 30, 2016

Actually I failed to report that I was using Ubuntu 16.04 virtual machine on VMware Workstation 12 (most recent version) on native Windows 10. I just have tested for this issue on Ubuntu 16.04 on bare metal, and rustc worked fine. The issue seems to be related to virtualization somehow.

$ touch test.rs
$ time rustc test.rs
error: main function not found

error: aborting due to previous error


real    0m0.042s
user    0m0.040s
sys 0m0.000s

@jchlapinski jchlapinski changed the title Long delay before exiting after reporting compilation errors in rustc 1.14.0 nightly Long delay before exiting after reporting compilation errors in rustc 1.14.0 nightly when runned on virtual machine Oct 30, 2016
@alexcrichton
Copy link
Member

Note that the compiler continues after it hits the first error, so the compiler could possibly have been doing real compilation work (looking for more errors), so perhaps that's what's happening here?

@retep998
Copy link
Member

Have you considered profiling rustc to see where it is spending most of those 13 seconds?

@jchlapinski
Copy link
Author

jchlapinski commented Oct 30, 2016

@retep998 here is what I've gathered from 'perf'

$ touch test.rs
$ perf record rustc test.rs
$ perf report --stdio --header

the results from my Ubuntu 16.04 virtual machine running on VMware Workstation (only few lines from the top for brevity):

# ========
# captured on: Sun Oct 30 21:00:17 2016
# hostname : ubuntu
# os release : 4.4.0-45-generic
# perf version : 4.4.21
# arch : x86_64
# nrcpus online : 8
# nrcpus avail : 8
# cpudesc : Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
# cpuid : GenuineIntel,6,94,3
# total memory : 16415168 kB
# cmdline : /usr/lib/linux-tools-4.4.0-45/perf record rustc test.rs 
# event : name = cycles, , size = 112, { sample_period, sample_freq } = 4000, sample_type = IP|TID|TIME|PERIOD, disabled = 1, inherit = 1, mmap = 1, comm = 1, freq = 1, enable_on_exec = 1, task = 1, sample_id_all = 1, exclude_guest = 1, mmap2 = 1, comm_exec = 1
# HEADER_CPU_TOPOLOGY info available, use -I to display
# HEADER_NUMA_TOPOLOGY info available, use -I to display
# pmu mappings: cpu = 4, msr = 6, software = 1, tracepoint = 2, breakpoint = 5
# ========
#
#
# Total Lost Samples: 0
#
# Samples: 53K of event 'cycles'
# Event count (approx.): 53214853068
#
# Overhead  Command  Shared Object                  Symbol                                                                                         
# ........  .......  .............................  ...............................................................................................................................................................................................................................
#
    96.65%  rustc    libstd-6eb85298.so             [.] backtrace_alloc                                                                                                                                                                                                            
     1.06%  rustc    libstd-6eb85298.so             [.] swap                                                                                                                                                                                                                       
     0.23%  rustc    libstd-6eb85298.so             [.] backtrace_qsort                                                                                                                                                                                                            
     0.11%  rustc    libstd-6eb85298.so             [.] read_function_entry                                                                                                                                                                                                        
     0.11%  rustc    libstd-6eb85298.so             [.] line_compare                                                                                                                                                                                                               
     0.10%  rustc    libstd-6eb85298.so             [.] read_uleb128                                                                                                                                                                                                               
     0.10%  rustc    libstd-6eb85298.so             [.] elf_symbol_compare                                                                                                                                                                                                         
     0.09%  rustc    rustc                          [.] swap                                                                                                                                                                                                                       
     0.08%  rustc    [unknown]                      [k] 0xffffffff813feb49                                                                                                                                                                                                         
     0.08%  rustc    libstd-6eb85298.so             [.] read_line_program                                                                                                                                                                                                          
     0.07%  rustc    libstd-6eb85298.so             [.] advance             

Clearly the problem is backtrace_alloc function.

I will also put here the results from bare metal for comparison.

@jchlapinski
Copy link
Author

results for Ubuntu 16.04 running bare metal:

# ========
# captured on: Sun Oct 30 21:16:37 2016
# hostname : kron
# os release : 4.4.0-45-generic
# perf version : 4.4.21
# arch : x86_64
# nrcpus online : 8
# nrcpus avail : 8
# cpudesc : Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
# cpuid : GenuineIntel,6,94,3
# total memory : 32863380 kB
# cmdline : /usr/lib/linux-tools-4.4.0-45/perf record rustc test.rs 
# event : name = cycles:pp, , size = 112, { sample_period, sample_freq } = 4000, sample_type = IP|TID|TIME|PERIOD, disabled = 1, inherit = 1, mmap = 1, comm = 1, freq = 1, enable_on_exec = 1, task = 1, precise_ip = 2, sample_id_all = 1, exclude_guest = 1, mmap2 = 1, comm_exe
# HEADER_CPU_TOPOLOGY info available, use -I to display
# HEADER_NUMA_TOPOLOGY info available, use -I to display
# pmu mappings: intel_pt = 7, intel_bts = 6, cpu = 4, msr = 8, software = 1, tracepoint = 2, cstate_core = 9, cstate_pkg = 10, breakpoint = 5
# ========
#
#
# Total Lost Samples: 0
#
# Samples: 149  of event 'cycles:pp'
# Event count (approx.): 159792095
#
# Overhead  Command  Shared Object                  Symbol                                                                                         
# ........  .......  .............................  ...............................................................................................................................................................................................................................
#
    10.85%  rustc    ld-2.23.so                     [.] do_lookup_x                                                                                                                                                                                                                
     7.68%  rustc    librustc_metadata-6eb85298.so  [.] rustc_metadata::decoder::_$LT$impl$u20$rustc_metadata..cstore..MetadataBlob$GT$::load_key_map::hf181e213f9856db2                                                                                                           
     6.81%  rustc    ld-2.23.so                     [.] strcmp                                                                                                                                                                                                                     
     6.54%  rustc    ld-2.23.so                     [.] check_match                                                                                                                                                                                                                
     3.45%  rustc    librustc_metadata-6eb85298.so  [.] _$LT$rustc_metadata..schema..Entry$LT$$u27$tcx$GT$$u20$as$u20$serialize..serialize..Decodable$GT$::decode::h6190a6fb1f040282                                                                                               
     3.35%  rustc    [unknown]                      [k] 0xffffffff813feaf7                                                                                                                                                                                                         
     3.04%  rustc    libstd-6eb85298.so             [.] mallocx                                                                                                                                                                                                                    
     3.03%  rustc    ld-2.23.so                     [.] _dl_check_map_versions                                                                                                                                                                                                     
     2.84%  rustc    libsyntax-6eb85298.so          [.] _$LT$std..collections..hash..map..HashMap$LT$K$C$$u20$V$C$$u20$S$GT$$GT$::get::h92e875ddcaeef5eb                                                                                                                           
     2.74%  rustc    ld-2.23.so                     [.] _dl_relocate_object                                                                                                                                                                                                        
     2.73%  rustc    [unknown]                      [k] 0xffffffff811cbca7    

@jchlapinski
Copy link
Author

jchlapinski commented Oct 30, 2016

I have just realized that on my virtual machine I had environment variable RUST_BACKTRACE=1, whereas on my bare-metal I had not. Indeed when setting RUST_BACKTRACE=1 on both machines the results are more or less the same (there is a very long delay before rustc exits). So the problem is not with virtualization after all. Definitely the problem lays in backtrace code somewhere.

I started to notice this issue somewhere around a week ago, as I am updating rust nightly very often (daily).

@jchlapinski jchlapinski changed the title Long delay before exiting after reporting compilation errors in rustc 1.14.0 nightly when runned on virtual machine Long delay before exiting after reporting compilation errors in rustc 1.14.0 nightly when RUST_BACKTRACE=1 Oct 30, 2016
@jchlapinski
Copy link
Author

Possibly duplicate of #29293

@m-n
Copy link

m-n commented Dec 16, 2016

1.14 beta exhibits this issue. If it goes stable it will be a significant headache/puzzle for everyone who leaves RUST_BACKTRACE set. cc @nikomatsakis

#37571 seems to be a duplicate issue.

In the same 10/25 nightly that introduced this, the release hello world executable size on x86_64-unknown-linux-gnu grew from ~650k to ~2MB, not sure if that may be related.

@shepmaster
Copy link
Member

This is negatively impacting the alternate playground, which always sets RUST_BACKTRACE, and is now present in released 1.14.

shepmaster added a commit to rust-lang/rust-playground that referenced this issue Dec 25, 2016
@arielb1
Copy link
Contributor

arielb1 commented Dec 25, 2016

#0  0x00007f47d8f9acbb in elf_add ()
   from /opt/rust/build-debug-assertions/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-a81d7bd1df01b16f.so
#1  0x00007f47d8f9b5a7 in phdr_callback ()
   from /opt/rust/build-debug-assertions/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-a81d7bd1df01b16f.so
#2  0x00007f47d8c86b14 in __GI___dl_iterate_phdr (
    callback=0x7f47d8f9b510 <phdr_callback>, data=0x7f47cfc2f690) at dl-iteratephdr.c:76
#3  0x00007f47d8f9b684 in backtrace_initialize ()
   from /opt/rust/build-debug-assertions/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-a81d7bd1df01b16f.so
#4  0x00007f47d8f9a19e in fileline_initialize ()
   from /opt/rust/build-debug-assertions/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-a81d7bd1df01b16f.so
#5  0x00007f47d8f9a2a2 in backtrace_syminfo ()
   from /opt/rust/build-debug-assertions/build/x86_64-unknown-linux-gnu/stage1/bin/../lib/libstd-a81d7bd1df01b16f.so
#6  0x00007f47d8f83668 in std::sys_common::gnu::libbacktrace::print (w=&mut Write, 
    idx=<optimized out>, addr=<optimized out>, symaddr=<optimized out>)
    at /opt/rust/src/libstd/sys_common/gnu/libbacktrace.rs:145
#7  std::sys::imp::backtrace::tracing::imp::write::trace_fn (ctx=<optimized out>, 
    arg=0x7f47cfc2fdf0)
    at /opt/rust/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:101
#8  0x00007f47d0e66609 in _Unwind_Backtrace (
    trace=0x7f47d8f83490 <std::sys::imp::backtrace::tracing::imp::write::trace_fn>, 
    trace_argument=0x7f47cfc2fdf0) at ../../../src/libgcc/unwind.inc:295
#9  0x00007f47d8f83213 in std::sys::imp::backtrace::tracing::imp::write (w=&mut Write)
    at /opt/rust/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:42
#10 0x00007f47d8f961e0 in std::panicking::default_hook::{{closure}} (err=&mut Write)
    at /opt/rust/src/libstd/panicking.rs:349
#11 0x00007f47d8f92599 in std::panicking::default_hook (info=<optimized out>)
    at /opt/rust/src/libstd/panicking.rs:359
#12 0x00007f47d8f92d68 in std::panicking::rust_panic_with_hook (msg=Box<Any>, 
    file_line=<optimized out>)
    at /opt/rust/src/libstd/panicking.rs:553
#13 0x00007f47d55f9efb in std::panicking::begin_panic<rustc_errors::FatalError> (
    msg=..., file_line=0x1)
    at /opt/rust/src/libstd/panicking.rs:515
#14 0x00007f47d576096d in syntax::parse::file_to_filemap (sess=0x7f47cfc34f10, 
    path=0x7f47c8002a40, 
    spanopt=<error reading variable: access outside bounds of object referenced via synthetic pointer>) at /opt/rust/src/libsyntax/parse/mod.rs:213
#15 0x00007f47d575fd47 in syntax::parse::new_parser_from_file (sess=0x7f47cfc34f10, 
    path=0x3) at /opt/rust/src/libsyntax/parse/mod.rs:160
#16 syntax::parse::parse_crate_from_file (input=0x3, sess=0x7f47cfc34f10)
    at /opt/rust/src/libsyntax/parse/mod.rs:98
#17 0x00007f47d92cce52 in rustc_driver::driver::phase_1_parse_input::{{closure}} ()
    at /opt/rust/src/librustc_driver/driver.rs:494
#18 rustc::util::common::time<core::result::Result<syntax::ast::Crate, rustc_errors::diagnostic_builder::DiagnosticBuilder>,closure> (
    what=<error reading variable: access outside bounds of object referenced via synthetic pointer>, do_it=<optimized out>, f=...)
    at /opt/rust/src/librustc/util/common.rs:34
#19 rustc_driver::driver::phase_1_parse_input (sess=0x7f47cfc344d0, 
    input=0x7f47cfc35a90)
    at /opt/rust/src/librustc_driver/driver.rs:491
#20 0x00007f47d92c90e8 in rustc_driver::driver::compile_input (sess=0x3, 
    cstore=0x7f47c8001060, input=0x7f47cfc35a90, outdir=0x7f47cfc35a78, 
    output=0x7f47cfc35a58, addl_plugins=..., control=0x7f47cfc353b8)
    at /opt/rust/src/librustc_driver/driver.rs:95
#21 0x00007f47d92f9283 in rustc_driver::run_compiler (args=..., 
    callbacks=&mut CompilerCalls, file_loader=..., emitter_dest=...)
    at /opt/rust/src/librustc_driver/lib.rs:221
#22 0x00007f47d923a262 in rustc_driver::main::{{closure}} ()

@matklad
Copy link
Member

matklad commented Jan 1, 2017

Hey, this has made it's way to stable and it is a rather serious problem I think. @alexcrichton, perhaps it should have some labels/assignee applied?

We face it in IntelliJ Rust: intellij-rust/intellij-rust#867

We run cargo commands with RUST_BACTRACE=1 by default, so this leaks to the compiler even though we are interested only in the bactraces for the user's code.

If it is useful, here's a flamegraph of the hard working rustc: http://svgshare.com/i/TU.svg

@alexcrichton
Copy link
Member

@matklad sounds reasonable to me. I'll nominate for discussion in libs triage as I believe this is basically entirely about libbacktrace

@alexcrichton alexcrichton added I-nominated T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jan 1, 2017
@nikomatsakis
Copy link
Contributor

What makes libbacktrace so slow, anyway? I've definitely noticed the very long lags in the past.

@shepmaster
Copy link
Member

What makes libbacktrace so slow, anyway?

Judging by the pretty flamegraph and the perf report, I'm going to guess that something is being eagerly allocated when it doesn't need to be.

@Mark-Simulacrum
Copy link
Member

I've tried to track this down, but been unable to reproduce with nightly on macOS 10.12.2 or stage1 and stage2 compilers on Ubuntu 16.04 (on 95b14a3). While I do notice a slight increase (~100-400 ms) when either the compiler ICEs or a crate panics, I sort of expect that since more work is being done.

However, I can reproduce with nightly (rustc 1.16.0-nightly (4682271 2017-01-03)) on Ubuntu 16.04, which sort of confuses me. I'm not sure what the difference between those and stage1/stage2 builds on the same machine is.

To summarize:

Compiler Delay Noticeable?
macOS Nightly No
Ubuntu 16.04 Stage 1 Compiler No
Ubuntu 16.04 Stage 2 Compiler No
Ubuntu 16.04 Nightly Yes

This makes benchmarking and testing changes difficult/impossible, since I can't reproduce with a locally built compiler. I'm not really sure what can be said here.

I also confirmed that this is not due to the wrapper script that rustbuild introduces.

Exact measurements below. test.rs is empty in all cases.

macOS 10.12.2, rustc 1.16.0-nightly (4682271 2017-01-03).

$ time rustc test.rs
error: main function not found

error: aborting due to previous error

real	0m0.061s
user	0m0.034s
sys	0m0.024s

$ time RUST_BACKTRACE=1 rustc test.rs
error: main function not found

error: aborting due to previous error

real	0m0.123s
user	0m0.061s
sys	0m0.031s

Ubuntu 16.04, rustc 1.16.0-nightly (4682271 2017-01-03) -- same as on macOS

$ time rustc test.rs
error: main function not found

error: aborting due to previous error

real	0m0.045s
user	0m0.036s
sys	0m0.000s

$ time RUST_BACKTRACE=1 rustc test.rs
error: main function not found

error: aborting due to previous error

real	0m10.737s
user	0m10.592s
sys	0m0.032s

Ubuntu 16.04, Stage 1 Compiler (./build/x86_64-unknown-linux-gnu/stage1/bin/rustc) at 95b14a3.

$ time ./rustc-stage1 test.rs
error: main function not found

error: aborting due to previous error


real	0m0.041s
user	0m0.028s
sys	0m0.008s

$ time RUST_BACKTRACE=1 ./rustc-stage1 test.rs
error: main function not found

error: aborting due to previous error


real	0m0.153s
user	0m0.136s
sys	0m0.012s

Ubuntu 16.04, Stage 2 Compiler (./build/x86_64-unknown-linux-gnu/stage2/bin/rustc) at 95b14a3.

$ time ./rustc-stage2 test.rs
error: main function not found

error: aborting due to previous error

real	0m0.041s
user	0m0.032s
sys	0m0.012s

$ time RUST_BACKTRACE=1 ./rustc-stage2 test.rs
error: main function not found

error: aborting due to previous error

real	0m0.157s
user	0m0.136s
sys	0m0.016s

It may also be of interested that utilizing strace, it seems that the stage1/stage2 compilers benchmarked above issue hundreds of mmap calls, approximately page-by-page ([pid 8042] mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1a793ad000) in case it matters, while the nightly rustc issues thousands, from what I can tell. It almost feels like it's loading the libraries in ~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/, possibly. It seems to iterate over those (with open calls), but that might not be during the panic.

Hopefully all of this can help narrow this issue down. I'd be happy to collect any other statistics that could be helpful.

@alexcrichton
Copy link
Member

@Mark-Simulacrum interesting! I wonder if this means it's all related to how we're building the nightlies. The main differences I can think of there are:

  • We're compiling with a much older gcc most likely (4.7)
  • We're compiling against a much older glibc (2.5 IIRC)
  • LLVM assertions are enabled (but just for Rust code)
  • Debuginfo is enabled in the compiler

Now that I list these out, did you enable debuginfo in the compiler you produced locally? IIRC that was a historical problem and I don't think we ever fixed that.

@matklad
Copy link
Member

matklad commented Jan 5, 2017

test.rs is empty in all cases.

I see delay even if rustc is called with non existent file: RUST_BACKTRACE=1 rustc gibberish.

@shepmaster
Copy link
Member

I wonder if this means it's all related to how we're building the nightlies

This occurs with Rust 1.14 stable on Linux for me.

@alexcrichton
Copy link
Member

@shepmaster oh I should say releases*, not just nightlies. They're all built the same way modulo llvm-assertions (which we can now rule out if it's a problem on stable)

@shepmaster
Copy link
Member

FWIW, here's the reproduction steps I have:

# Outside Docker container
docker run -it --rm ubuntu:16.04

# Inside Docker container
apt-get update && apt-get install -y curl
curl https://sh.rustup.rs -sSf | sh
source $HOME/.cargo/env
time RUST_BACKTRACE=1 rustc foo.rs
real	0m12.233s
user	0m12.060s
sys	0m0.160s

@matklad
Copy link
Member

matklad commented Jan 5, 2017

For some reason, this reminds me of rust-lang/rustup#783. Most likely unrelated, but symptoms are somewhat similar.

bors added a commit that referenced this issue Jan 11, 2017
rustbuild: Don't enable debuginfo in rustc

In #37280 we enabled line number debugging information in release artifacts,
primarily to close out #36452 where debugging information was critical for MSVC
builds of Rust to be useful in production. This commit, however, apparently had
some unfortunate side effects.

Namely it was noticed in #37477 that if `RUST_BACKTRACE=1` was set then any
compiler error would take a very long time for the compiler to exit. The cause
of the problem here was somewhat deep:

* For all compiler errors, the compiler will `panic!` with a known value. This
  tears down the main compiler thread and allows cleaning up all the various
  resources. By default, however, this panic output is suppressed for "normal"
  compiler errors.
* When `RUST_BACKTRACE=1` was set this caused every compiler error to generate a
  backtrace.
* The libbacktrace library hits a pathological case where it spends a very long
  time in its custom allocation function, `backtrace_alloc`, because the
  compiler has so much debugging information. More information about this can be
  found in #29293 with a summary at the end of #37477.

To solve this problem this commit simply removes debuginfo from the compiler but
not from the standard library. This should allow us to keep #36452 closed while
also closing #37477. I've measured the difference to be orders of magnitude
faster than it was before, so we should see a much quicker time-to-exit after a
compile error when `RUST_BACKTRACE=1` is set.

Closes #37477
Closes #37571
@alexcrichton
Copy link
Member

@Mark-Simulacrum oh sorry #38984 landed more quickly than I thought it would! To clarify --enable-debug and such still enable debuginfo in the compiler. I only changed releases. That means that local development should still work out just fine.

I personally prefer to not change libbacktrace much to make future vendoring efforts easier, but that's mostly just me.

@nikomatsakis
Copy link
Contributor

What I really want is for us to adopt some kind of library that works across platforms, so that mac can enjoy line numbers too! (I'm not sure of the status on windows here...)

brson pushed a commit to brson/rust that referenced this issue Jan 17, 2017
In rust-lang#37280 we enabled line number debugging information in release artifacts,
primarily to close out rust-lang#36452 where debugging information was critical for MSVC
builds of Rust to be useful in production. This commit, however, apparently had
some unfortunate side effects.

Namely it was noticed in rust-lang#37477 that if `RUST_BACKTRACE=1` was set then any
compiler error would take a very long time for the compiler to exit. The cause
of the problem here was somewhat deep:

* For all compiler errors, the compiler will `panic!` with a known value. This
  tears down the main compiler thread and allows cleaning up all the various
  resources. By default, however, this panic output is suppressed for "normal"
  compiler errors.
* When `RUST_BACKTRACE=1` was set this caused every compiler error to generate a
  backtrace.
* The libbacktrace library hits a pathological case where it spends a very long
  time in its custom allocation function, `backtrace_alloc`, because the
  compiler has so much debugging information. More information about this can be
  found in rust-lang#29293 with a summary at the end of rust-lang#37477.

To solve this problem this commit simply removes debuginfo from the compiler but
not from the standard library. This should allow us to keep rust-lang#36452 closed while
also closing rust-lang#37477. I've measured the difference to be orders of magnitude
faster than it was before, so we should see a much quicker time-to-exit after a
compile error when `RUST_BACKTRACE=1` is set.

Closes rust-lang#37477
Closes rust-lang#37571
alexcrichton added a commit to alexcrichton/rust that referenced this issue Oct 25, 2017
This is sort of a long overdue change from the investigation in rust-lang#29293
and rust-lang#37477. The released binaries of rustc don't have debug information and so
don't actively suffer this problem but this can hit local development of rustc
and also larger programs compiled against libstd generating backtraces.

The main purpose of the mmap allocator in libacktrace is to be usable from a
signal handler, but we don't do that, so the normal allocator using malloc/free
should work well for us.
bors added a commit that referenced this issue Oct 26, 2017
std: Disable usage of mmap allocator in libbacktrace

This is sort of a long overdue change from the investigation in #29293
and #37477. The released binaries of rustc don't have debug information and so
don't actively suffer this problem but this can hit local development of rustc
and also larger programs compiled against libstd generating backtraces.

The main purpose of the mmap allocator in libacktrace is to be usable from a
signal handler, but we don't do that, so the normal allocator using malloc/free
should work well for us.
ianlancetaylor added a commit to ianlancetaylor/libbacktrace that referenced this issue Jan 25, 2018
@kennytm kennytm mentioned this issue Apr 27, 2018
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants