2019-07-26 21:54:25 +00:00
|
|
|
// run-pass
|
|
|
|
|
2018-09-14 10:20:28 +00:00
|
|
|
#![allow(unused_must_use)]
|
|
|
|
#![allow(unconditional_recursion)]
|
2014-12-19 02:56:28 +00:00
|
|
|
// ignore-android: FIXME (#20004)
|
2017-10-18 01:45:42 +00:00
|
|
|
// ignore-emscripten no processes
|
2019-04-24 16:26:33 +00:00
|
|
|
// ignore-sgx no processes
|
2014-12-19 02:56:28 +00:00
|
|
|
|
2022-01-12 00:00:00 +00:00
|
|
|
#![feature(core_intrinsics)]
|
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the
standard library has always been a little special, it's never been able
to depend on crates.io crates for runtime dependencies. This has been a
result of various limitations, namely that Cargo doesn't understand that
crates from crates.io depend on libcore, so Cargo tries to build crates
before libcore is finished.
I had an idea this afternoon, however, which lifts the strategy
from #52919 to directly depend on crates.io crates from the standard
library. After all is said and done this removes a whopping three
submodules that we need to manage!
The basic idea here is that for any crate `std` depends on it adds an
*optional* dependency on an empty crate on crates.io, in this case named
`rustc-std-workspace-core`. This crate is overridden via `[patch]` in
this repository to point to a local crate we write, and *that* has a
`path` dependency on libcore.
Note that all `no_std` crates also depend on `compiler_builtins`, but if
we're not using submodules we can publish `compiler_builtins` to
crates.io and all crates can depend on it anyway! The basic strategy
then looks like:
* The standard library (or some transitive dep) decides to depend on a
crate `foo`.
* The standard library adds
```toml
[dependencies]
foo = { version = "0.1", features = ['rustc-dep-of-std'] }
```
* The crate `foo` has an optional dependency on `rustc-std-workspace-core`
* The crate `foo` has an optional dependency on `compiler_builtins`
* The crate `foo` has a feature `rustc-dep-of-std` which activates these
crates and any other necessary infrastructure in the crate.
A sample commit for `dlmalloc` [turns out to be quite simple][commit].
After that all `no_std` crates should largely build "as is" and still be
publishable on crates.io! Notably they should be able to continue to use
stable Rust if necessary, since the `rename-dependency` feature of Cargo
is soon stabilizing.
As a proof of concept, this commit removes the `dlmalloc`,
`libcompiler_builtins`, and `libc` submodules from this repository. Long
thorns in our side these are now gone for good and we can directly
depend on crates.io! It's hoped that in the long term we can bring in
other crates as necessary, but for now this is largely intended to
simply make it easier to manage these crates and remove submodules.
This should be a transparent non-breaking change for all users, but one
possible stickler is that this almost for sure breaks out-of-tree
`std`-building tools like `xargo` and `cargo-xbuild`. I think it should
be relatively easy to get them working, however, as all that's needed is
an entry in the `[patch]` section used to build the standard library.
Hopefully we can work with these tools to solve this problem!
[commit]: https://github.com/alexcrichton/dlmalloc-rs/commit/28ee12db813a3b650a7c25d1c36d2c17dcb88ae3
2018-11-20 05:52:50 +00:00
|
|
|
#![feature(rustc_private)]
|
2016-01-31 23:30:32 +00:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
extern crate libc;
|
2014-02-20 05:01:50 +00:00
|
|
|
|
2015-02-16 14:04:02 +00:00
|
|
|
use std::env;
|
2015-07-27 20:41:35 +00:00
|
|
|
use std::process::Command;
|
|
|
|
use std::thread;
|
2014-02-20 05:01:50 +00:00
|
|
|
|
2014-04-02 05:07:37 +00:00
|
|
|
// Inlining to avoid llvm turning the recursive functions into tail calls,
|
|
|
|
// which doesn't consume stack.
|
|
|
|
#[inline(always)]
|
2022-01-12 00:00:00 +00:00
|
|
|
pub fn black_box<T>(dummy: T) { std::intrinsics::black_box(dummy); }
|
2014-02-20 05:01:50 +00:00
|
|
|
|
|
|
|
fn silent_recurse() {
|
2015-07-27 20:41:35 +00:00
|
|
|
let buf = [0u8; 1000];
|
2014-02-20 05:01:50 +00:00
|
|
|
black_box(buf);
|
|
|
|
silent_recurse();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn loud_recurse() {
|
|
|
|
println!("hello!");
|
|
|
|
loud_recurse();
|
2014-03-27 11:54:41 +00:00
|
|
|
black_box(()); // don't optimize this into a tail call. please.
|
2014-02-20 05:01:50 +00:00
|
|
|
}
|
|
|
|
|
2016-01-31 23:30:32 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
fn check_status(status: std::process::ExitStatus)
|
|
|
|
{
|
|
|
|
use std::os::unix::process::ExitStatusExt;
|
|
|
|
|
|
|
|
assert!(!status.success());
|
Use libc::abort, not intrinsics::abort, in rtabort!
intrinsics::abort compiles down to an illegal instruction, which on
Unix-like platforms causes the process to be killed with SIGILL. A more
appropriate way to kill the process would be SIGABRT; this indicates
better that the runtime has explicitly aborted, rather than some kind of
compiler bug or architecture mismatch that SIGILL might indicate.
For rtassert!, replace this with libc::abort. libc::abort raises
SIGABRT, but is defined to do so in such a way that it will terminate
the process even if SIGABRT is currently masked or caught by a signal
handler that returns.
On non-Unix platforms, retain the existing behavior. On Windows we
prefer to avoid depending on the C runtime, and we need a fallback for
any other platforms that may be defined. An alternative on Windows
would be to call TerminateProcess, but this seems less essential than
switching to using SIGABRT on Unix-like platforms, where it is common
for the process-killing signal to be printed out or logged.
This is a [breaking-change] for any code that depends on the exact
signal raised to abort a process via rtabort!
cc #31273
cc #31333
2016-02-06 22:16:56 +00:00
|
|
|
assert_eq!(status.signal(), Some(libc::SIGABRT));
|
2016-01-31 23:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn check_status(status: std::process::ExitStatus)
|
|
|
|
{
|
|
|
|
assert!(!status.success());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-20 05:01:50 +00:00
|
|
|
fn main() {
|
2015-02-16 14:04:02 +00:00
|
|
|
let args: Vec<String> = env::args().collect();
|
2015-02-02 02:53:25 +00:00
|
|
|
if args.len() > 1 && args[1] == "silent" {
|
2014-02-20 05:01:50 +00:00
|
|
|
silent_recurse();
|
2015-02-02 02:53:25 +00:00
|
|
|
} else if args.len() > 1 && args[1] == "loud" {
|
2014-02-20 05:01:50 +00:00
|
|
|
loud_recurse();
|
2015-07-27 20:41:35 +00:00
|
|
|
} else if args.len() > 1 && args[1] == "silent-thread" {
|
|
|
|
thread::spawn(silent_recurse).join();
|
|
|
|
} else if args.len() > 1 && args[1] == "loud-thread" {
|
|
|
|
thread::spawn(loud_recurse).join();
|
2014-02-20 05:01:50 +00:00
|
|
|
} else {
|
2015-07-27 20:41:35 +00:00
|
|
|
let mut modes = vec![
|
|
|
|
"silent-thread",
|
|
|
|
"loud-thread",
|
|
|
|
];
|
|
|
|
|
|
|
|
// On linux it looks like the main thread can sometimes grow its stack
|
|
|
|
// basically without bounds, so we only test the child thread cases
|
|
|
|
// there.
|
|
|
|
if !cfg!(target_os = "linux") {
|
|
|
|
modes.push("silent");
|
|
|
|
modes.push("loud");
|
|
|
|
}
|
|
|
|
for mode in modes {
|
|
|
|
println!("testing: {}", mode);
|
|
|
|
|
|
|
|
let silent = Command::new(&args[0]).arg(mode).output().unwrap();
|
2016-01-31 23:30:32 +00:00
|
|
|
|
|
|
|
check_status(silent.status);
|
|
|
|
|
2015-07-27 20:41:35 +00:00
|
|
|
let error = String::from_utf8_lossy(&silent.stderr);
|
|
|
|
assert!(error.contains("has overflowed its stack"),
|
|
|
|
"missing overflow message: {}", error);
|
|
|
|
}
|
2014-02-20 05:01:50 +00:00
|
|
|
}
|
|
|
|
}
|