2017-01-31 22:27:51 +01:00
|
|
|
use std::env;
|
2016-01-22 00:21:13 +01:00
|
|
|
|
|
|
|
fn main() {
|
2020-07-22 19:06:37 +02:00
|
|
|
println!("cargo:rerun-if-changed=build.rs");
|
2016-09-25 17:59:12 +02:00
|
|
|
let target = env::var("TARGET").expect("TARGET was not set");
|
2016-03-08 22:15:58 +01:00
|
|
|
if target.contains("linux") {
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-09 01:18:40 +02:00
|
|
|
if target.contains("android") {
|
2016-03-08 22:15:58 +01:00
|
|
|
println!("cargo:rustc-link-lib=dl");
|
|
|
|
println!("cargo:rustc-link-lib=log");
|
|
|
|
println!("cargo:rustc-link-lib=gcc");
|
2017-08-22 23:24:29 +02:00
|
|
|
} else if !target.contains("musl") {
|
2016-01-22 00:21:13 +01:00
|
|
|
println!("cargo:rustc-link-lib=dl");
|
|
|
|
println!("cargo:rustc-link-lib=rt");
|
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
|
|
|
}
|
|
|
|
} else if target.contains("freebsd") {
|
|
|
|
println!("cargo:rustc-link-lib=execinfo");
|
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
2020-07-31 01:55:12 +02:00
|
|
|
if env::var("RUST_STD_FREEBSD_12_ABI").is_ok() {
|
|
|
|
println!("cargo:rustc-cfg=freebsd12");
|
|
|
|
}
|
2018-09-25 15:03:14 +02:00
|
|
|
} else if target.contains("netbsd") {
|
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
|
|
|
println!("cargo:rustc-link-lib=rt");
|
2019-05-13 09:13:07 +02:00
|
|
|
} else if target.contains("dragonfly") || target.contains("openbsd") {
|
2016-01-22 00:21:13 +01:00
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
2017-07-07 07:32:18 +02:00
|
|
|
} else if target.contains("solaris") {
|
|
|
|
println!("cargo:rustc-link-lib=socket");
|
|
|
|
println!("cargo:rustc-link-lib=posix4");
|
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
2017-07-25 00:57:57 +02:00
|
|
|
println!("cargo:rustc-link-lib=resolv");
|
2020-04-14 01:37:22 +02:00
|
|
|
} else if target.contains("illumos") {
|
|
|
|
println!("cargo:rustc-link-lib=socket");
|
|
|
|
println!("cargo:rustc-link-lib=posix4");
|
|
|
|
println!("cargo:rustc-link-lib=pthread");
|
|
|
|
println!("cargo:rustc-link-lib=resolv");
|
|
|
|
println!("cargo:rustc-link-lib=nsl");
|
|
|
|
// Use libumem for the (malloc-compatible) allocator
|
|
|
|
println!("cargo:rustc-link-lib=umem");
|
2016-01-22 00:21:13 +01:00
|
|
|
} else if target.contains("apple-darwin") {
|
|
|
|
println!("cargo:rustc-link-lib=System");
|
2017-04-27 18:58:52 +02:00
|
|
|
|
|
|
|
// res_init and friends require -lresolv on macOS/iOS.
|
|
|
|
// See #41582 and http://blog.achernya.com/2013/03/os-x-has-silly-libsystem.html
|
|
|
|
println!("cargo:rustc-link-lib=resolv");
|
2016-01-22 00:21:13 +01:00
|
|
|
} else if target.contains("apple-ios") {
|
|
|
|
println!("cargo:rustc-link-lib=System");
|
|
|
|
println!("cargo:rustc-link-lib=objc");
|
|
|
|
println!("cargo:rustc-link-lib=framework=Security");
|
|
|
|
println!("cargo:rustc-link-lib=framework=Foundation");
|
2017-04-27 18:58:52 +02:00
|
|
|
println!("cargo:rustc-link-lib=resolv");
|
2019-05-27 16:33:00 +02:00
|
|
|
} else if target.contains("uwp") {
|
|
|
|
println!("cargo:rustc-link-lib=ws2_32");
|
2019-05-27 16:41:52 +02:00
|
|
|
// For BCryptGenRandom
|
|
|
|
println!("cargo:rustc-link-lib=bcrypt");
|
2016-01-22 00:21:13 +01:00
|
|
|
} else if target.contains("windows") {
|
|
|
|
println!("cargo:rustc-link-lib=advapi32");
|
|
|
|
println!("cargo:rustc-link-lib=ws2_32");
|
|
|
|
println!("cargo:rustc-link-lib=userenv");
|
2016-10-25 01:42:57 +02:00
|
|
|
} else if target.contains("fuchsia") {
|
2017-09-15 21:38:08 +02:00
|
|
|
println!("cargo:rustc-link-lib=zircon");
|
|
|
|
println!("cargo:rustc-link-lib=fdio");
|
2017-12-29 09:33:26 +01:00
|
|
|
} else if target.contains("cloudabi") {
|
|
|
|
if cfg!(feature = "backtrace") {
|
|
|
|
println!("cargo:rustc-link-lib=unwind");
|
|
|
|
}
|
|
|
|
println!("cargo:rustc-link-lib=c");
|
|
|
|
println!("cargo:rustc-link-lib=compiler_rt");
|
2020-07-07 05:31:12 +02:00
|
|
|
} else if (target.contains("sgx") && target.contains("fortanix"))
|
|
|
|
|| target.contains("hermit")
|
|
|
|
|| target.contains("l4re")
|
|
|
|
|| target.contains("redox")
|
|
|
|
|| target.contains("haiku")
|
|
|
|
|| target.contains("vxworks")
|
|
|
|
|| target.contains("wasm32")
|
|
|
|
|| target.contains("asmjs")
|
|
|
|
{
|
|
|
|
// These platforms don't have any special requirements.
|
|
|
|
} else {
|
|
|
|
// This is for Cargo's build-std support, to mark std as unstable for
|
|
|
|
// typically no_std platforms.
|
|
|
|
// This covers:
|
|
|
|
// - os=none ("bare metal" targets)
|
|
|
|
// - mipsel-sony-psp
|
|
|
|
// - nvptx64-nvidia-cuda
|
2020-07-31 08:41:25 +02:00
|
|
|
// - arch=avr
|
2020-07-07 05:31:12 +02:00
|
|
|
// - tvos (aarch64-apple-tvos, x86_64-apple-tvos)
|
|
|
|
// - uefi (x86_64-unknown-uefi, i686-unknown-uefi)
|
|
|
|
// - JSON targets
|
|
|
|
// - Any new targets that have not been explicitly added above.
|
2020-06-01 03:09:25 +02:00
|
|
|
println!("cargo:rustc-cfg=feature=\"restricted-std\"");
|
|
|
|
}
|
2020-07-07 05:31:12 +02:00
|
|
|
println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap());
|
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-05-13 23:22:37 +02:00
|
|
|
println!("cargo:rustc-cfg=backtrace_in_libstd");
|
2016-01-22 00:21:13 +01:00
|
|
|
}
|