Commit Graph

131 Commits

Author SHA1 Message Date
Mark Rousskov cc4f547cf4 Revert "std: Switch from libbacktrace to gimli"
This reverts commit 13db3cc1e8.
2020-07-22 07:16:45 -04:00
Alex Crichton 13db3cc1e8 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 - #12602
* OOM with libbacktrace - #24231
* Backtrace failure due to use of uninitialized value - #28447
* Possibility to feed untrusted data to libbacktrace - #21889
* Soundness fix for libbacktrace - #33729
* Crash in libbacktrace - #39468
* Support for macOS, never merged - ianlancetaylor/libbacktrace#2
* Performance issues with libbacktrace - #29293, #37477
* Update procedure is quite complicated due to how many patches we
  need to carry - #50955
* Libbacktrace doesn't work on MinGW with dynamic libs - #71060
* Segfault in libbacktrace on macOS - #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.
2020-07-17 14:32:18 -07:00
Eduardo Sánchez Muñoz 0f1adc8ec8 Further improve comments in libstd/panicking.rs. 2020-07-02 13:47:19 +02:00
Eduardo Sánchez Muñoz aac2f734de Improve comments from https://github.com/rust-lang/rust/pull/72617, as suggested by RalfJung. 2020-07-01 16:59:50 +02:00
Manish Goregaokar 5158b3c998
Rollup merge of #72617 - eduardosm:panicking, r=Amanieu
Add a fast path for `std:🧵:panicking`.

This is done by adding a global atomic variable (non-TLS) that counts how many threads are panicking. In order to check if the current thread is panicking, this variable is read and, if it is zero, no thread (including the one where `panicking` is being called) is panicking and `panicking` can return `false` immediately without needing to access TLS. If the global counter is not zero, the local counter is accessed from TLS to check if the current thread is panicking.
2020-06-25 18:00:02 -07:00
Eduardo Sánchez Muñoz 771a1d8e0a Make `std::panicking::panic_count::is_zero` inline and move the slow path into a separate cold function. 2020-06-24 18:17:27 +02:00
Lzu Tao 64a6de25ea Join mutiple lines if it is more readable 2020-06-15 13:15:47 +00:00
Mark Rousskov 7139342249 Bump to 1.46 2020-06-03 15:27:51 -04:00
Eduardo Sánchez Muñoz f03cf9916a Add a fast path for `std:🧵:panicking`.
This is done by adding a global atomic variable (non-TLS) that counts how many threads are panicking. In order to check if the current thread is panicking, this variable is read and, if it is zero, no thread (including the one where `panicking` is being called) is panicking and `panicking` can return `false` immediately without needing to access TLS. If the global counter is not zero, the local counter is accessed from TLS to check if the current thread is panicking.
2020-05-26 17:46:10 +02:00
Ralf Jung 5980d972d1 make abort intrinsic safe, and correct its documentation 2020-05-17 11:23:42 +02:00
Mark Rousskov 93eed402ad Bump bootstrap compiler 2020-04-25 09:25:33 -04:00
bors 8e6de3244c Auto merge of #70010 - Amanieu:fix-opt-catch, r=Mark-Simulacrum
Add a workaround for catch_unwind in stage1 mingw target

Fixes #70001

cc @petrochenkov

r? @Mark-Simulacrum
2020-03-16 08:08:51 +00:00
Amanieu d'Antras 864d05bc8a Add a workaround for catch_unwind in stage1 mingw target
Fixes #70001
2020-03-14 20:55:50 +01:00
Ralf Jung 4452843720 update panicking comments in libstd 2020-03-14 11:36:40 +01:00
Amanieu d'Antras 1c950e5c6f Simplify the try intrinsic by using a callback in the catch block 2020-03-05 17:36:50 +00:00
Amanieu d'Antras 01d04944ce Apply review feedback 2020-03-02 11:43:07 +00:00
Amanieu d'Antras 5b682354f2 Fix some minor issues 2020-03-02 11:43:07 +00:00
Mark Rousskov bdcc02360f Mark cleanup cold 2020-03-02 11:43:07 +00:00
Mark Rousskov d45ce5aed6 Inline catching panics into std::catch_unwind
This allows LLVM to inline the happy path, such that catching unwinding is
zero-cost when no panic occurs. This also allows us to match the code generated
by C++ try/catch.
2020-03-02 11:43:06 +00:00
bors 126ad2b813 Auto merge of #68708 - Mark-Simulacrum:stage0-step, r=pietroalbini
Step stage0 to bootstrap from 1.42

This also includes a commit which fixes the rustfmt downloading logic to redownload when the rustfmt channel changes, and bumps rustfmt to a more recent version.
2020-02-04 14:16:18 +00:00
Mark Rousskov 31dcdc9e13 Drop cfg(bootstrap) code 2020-01-31 12:31:09 -05:00
Tomasz Miąsko 80c3bec9fe Remove incorrect debug assertions from catch_unwind
Previously the debug assertions in the implementation of catch_unwind
used to verify consistency of the panic count by checking that the count
is zero just before leaving the function. This incorrectly assumed that
no panic was in progress when entering `catch_unwind`.
2020-01-31 00:00:00 +00:00
Dylan DPC 1389caf860
Rollup merge of #68096 - varkor:diagnostic-cleanup, r=Centril
Clean up some diagnostics by making them more consistent

In general:

- Diagnostic should start with a lowercase letter.
- Diagnostics should not end with a full stop.
- Ellipses contain three dots.
- Backticks should encode Rust code.

I also reworded a couple of messages to make them read more clearly.

It might be sensible to create a style guide for diagnostics, so these informal conventions are written down somewhere, after which we could audit the existing diagnostics.

r? @Centril
2020-01-16 11:58:02 +05:30
Amanieu d'Antras 8f60db8da8 Don't include __rust_drop_panic when testing libstd 2020-01-12 16:59:44 +00:00
varkor 8461fa5119 Diagnostics should not end with a full stop 2020-01-12 15:37:50 +00:00
Amanieu d'Antras 3a025760be Abort if C++ tries to swallow a Rust panic 2020-01-11 10:18:44 +00:00
Adam Perry b76a5be18f Clean up comments in panicking infra. 2020-01-04 10:02:17 -08:00
Adam Perry eaccda009f core and std macros and panic internals use panic::Location::caller. 2020-01-04 10:02:17 -08:00
Mark Rousskov a06baa56b9 Format the world 2019-12-22 17:42:47 -05:00
Ross MacArthur f7256d28d1
Require issue = "none" over issue = "0" in unstable attributes 2019-12-21 13:16:18 +02:00
Ralf Jung babe9fcbc1 rename update_count_then_panic -> rust_panic_without_hook 2019-11-26 10:23:15 +01:00
Ralf Jung 4a19ef938c explain why __rust_start_panic does not take a Box 2019-11-26 09:29:39 +01:00
Ralf Jung 61486f4de3 expand comment 2019-11-26 09:27:11 +01:00
Ralf Jung 3e96ca2bf7 abort on BoxMeUp misuse 2019-11-26 09:24:39 +01:00
Ralf Jung 3a8e1b63cf panic_handler -> begin_panic_handler (and more comments) 2019-11-26 08:21:51 +01:00
Ralf Jung 3c48579551 more panicking comments 2019-11-25 23:55:54 +01:00
Ralf Jung 08f779cb4b better comment and rename BoxMeUp::box_me_up to take_box 2019-11-25 12:16:08 +01:00
Ralf Jung cd5d0c7b10 Rename continue_panic_fmt to panic_handler, and make it the #[panic_handler] directly
The "continue" in the name was really confusing; it sounds way too much like "resume" which is a totally different concept around panics.
2019-11-25 12:14:23 +01:00
Ralf Jung 80f5213cee expand type info on __rust_start_panic 2019-11-24 13:13:33 +01:00
Ralf Jung 35ee6bd59b panicking comments 2019-11-24 11:26:07 +01:00
Adam Perry aec97e050e Panicking infra uses &core::panic::Location.
This allows us to remove `static_panic_msg` from the SSA<->LLVM
boundary, along with its fat pointer representation for &str.

Also changes the signature of PanicInfo::internal_contructor to
avoid copying.

Closes #65856.
2019-10-27 12:50:58 -07:00
Mateusz Mikuła 95c06a2970 Apply clippy::needless_return suggestions 2019-10-22 19:23:10 +02:00
Oliver Scherer 2fc257ca81 Prefer `ManuallyDrop::{take,new}` over `ptr::{read,write}` 2019-10-11 10:43:54 +02:00
Simon Sapin 0a08841bb0 Remove uses of `allow(unions_with_drop_fields)` in the standard library 2019-10-11 10:43:54 +02:00
Alex Crichton 1d06058a77 std: Reduce checks for `feature = "backtrace"`
This is a stylistic change to libstd to reduce the number of checks of
`feature = "backtrace"` now that we unconditionally depend on the
`backtrace` crate and rely on it having an empty implementation.
otherwise.
2019-09-25 06:43:49 -07:00
Ralf Jung 49854c4f71 avoid #[cfg] in favor of cfg! 2019-09-16 16:37:44 +02:00
Ralf Jung dac0a158eb rename the crate, not the feature 2019-09-14 12:12:32 +02:00
Ralf Jung b60954757e std: always depend on backtrace, but only enable its features on demand 2019-09-14 10:23:51 +02:00
Taylor Cramer 290f5b2275 Use backtrace formatting from the backtrace crate 2019-09-04 16:18:12 -07:00
Chris Gregory 636f5e6d11 Convert more usages over 2019-07-01 20:21:12 -07:00