rust/src/test/ui/no-stdio.rs

121 lines
3.1 KiB
Rust
Raw Normal View History

// run-pass
// ignore-android
// ignore-cloudabi no processes
// ignore-emscripten no processes
2019-04-24 18:26:33 +02:00
// ignore-sgx no processes
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 06:52:50 +01:00
#![feature(rustc_private)]
extern crate libc;
use std::process::{Command, Stdio};
use std::env;
use std::io::{self, Read, Write};
#[cfg(unix)]
unsafe fn without_stdio<R, F: FnOnce() -> R>(f: F) -> R {
let doit = |a| {
let r = libc::dup(a);
assert!(r >= 0);
return r
};
let a = doit(0);
let b = doit(1);
let c = doit(2);
assert!(libc::close(0) >= 0);
assert!(libc::close(1) >= 0);
assert!(libc::close(2) >= 0);
let r = f();
assert!(libc::dup2(a, 0) >= 0);
assert!(libc::dup2(b, 1) >= 0);
assert!(libc::dup2(c, 2) >= 0);
return r
}
#[cfg(windows)]
unsafe fn without_stdio<R, F: FnOnce() -> R>(f: F) -> R {
type DWORD = u32;
type HANDLE = *mut u8;
type BOOL = i32;
const STD_INPUT_HANDLE: DWORD = -10i32 as DWORD;
const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
extern "system" {
fn GetStdHandle(which: DWORD) -> HANDLE;
fn SetStdHandle(which: DWORD, handle: HANDLE) -> BOOL;
}
let doit = |id| {
let handle = GetStdHandle(id);
assert!(handle != INVALID_HANDLE_VALUE);
assert!(SetStdHandle(id, INVALID_HANDLE_VALUE) != 0);
return handle
};
let a = doit(STD_INPUT_HANDLE);
let b = doit(STD_OUTPUT_HANDLE);
let c = doit(STD_ERROR_HANDLE);
let r = f();
let doit = |id, handle| {
assert!(SetStdHandle(id, handle) != 0);
};
doit(STD_INPUT_HANDLE, a);
doit(STD_OUTPUT_HANDLE, b);
doit(STD_ERROR_HANDLE, c);
return r
}
fn main() {
if env::args().len() > 1 {
println!("test");
assert!(io::stdout().write(b"test\n").is_ok());
assert!(io::stderr().write(b"test\n").is_ok());
assert_eq!(io::stdin().read(&mut [0; 10]).unwrap(), 0);
return
}
// First, make sure reads/writes without stdio work if stdio itself is
// missing.
let (a, b, c) = unsafe {
without_stdio(|| {
let a = io::stdout().write(b"test\n");
let b = io::stderr().write(b"test\n");
let c = io::stdin().read(&mut [0; 10]);
(a, b, c)
})
};
assert_eq!(a.unwrap(), 5);
assert_eq!(b.unwrap(), 5);
assert_eq!(c.unwrap(), 0);
// Second, spawn a child and do some work with "null" descriptors to make
// sure it's ok
let me = env::current_exe().unwrap();
let status = Command::new(&me)
.arg("next")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status().unwrap();
assert!(status.success(), "{:?} isn't a success", status);
// Finally, close everything then spawn a child to make sure everything is
// *still* ok.
let status = unsafe {
without_stdio(|| Command::new(&me).arg("next").status())
}.unwrap();
assert!(status.success(), "{:?} isn't a success", status);
}