-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new wasm32-unknown-wasi target
This commit adds a new wasm32-based target distributed through rustup, supported in the standard library, and implemented in the compiler. The `wasm32-unknown-wasi` target is intended to be a WebAssembly target which matches the [WASI proposal recently announced.][LINK]. In summary the WASI target is an effort to define a standard set of syscalls for WebAssembly modules, allowing WebAssembly modules to not only be portable across architectures but also be portable across environments implementing this standard set of system calls. The wasi target in libstd is still somewhat bare bones. This PR does not fill out the filesystem, networking, threads, etc. Instead it only provides the most basic of integration with the wasi syscalls, enabling features like: * `Instant::now` and `SystemTime::now` work * `env::args` is hooked up * `env::vars` will look up environment variables * `println!` will print to standard out * `process::{exit, abort}` should be hooked up appropriately None of these APIs can work natively on the `wasm32-unknown-unknown` target, but with the assumption of the WASI set of syscalls we're able to provide implementations of these syscalls that engines can implement. Currently the primary engine implementing wasi is [wasmtime], but more will surely emerge! In terms of future development of libstd, I think this is something we'll probably want to discuss. The purpose of the WASI target is to provide a standardized set of syscalls, but it's *also* to provide a standard C sysroot for compiling C/C++ programs. This means it's intended that functions like `read` and `write` are implemented for this target with a relatively standard definition and implementation. It's unclear, therefore, how we want to expose file descriptors and how we'll want to implement system primitives. For example should `std::fs::File` have a libc-based file descriptor underneath it? The raw wasi file descriptor? We'll see! Currently these details are all intentionally hidden and things we can change over time. A `WasiFd` sample struct was added to the standard library as part of this commit, but it's not currently used. It shows how all the wasi syscalls could be ergonomically bound in Rust, and they offer a possible implementation of primitives like `std::fs::File` if we bind wasi file descriptors exactly. Apart from the standard library, there's also the matter of how this target is integrated with respect to its C standard library. The reference sysroot, for example, provides managment of standard unix file descriptors and also standard APIs like `open` (as opposed to the relative `openat` inspiration for the wasi ssycalls). Currently the standard library relies on the C sysroot symbols for operations such as environment management, process exit, and `read`/`write` of stdio fds. We want these operations in Rust to be interoperable with C if they're used in the same process. Put another way, if Rust and C are linked into the same WebAssembly binary they should work together, but that requires that the same C standard library is used. We also, however, want the `wasm32-unknown-wasi` target to be usable-by-default with the Rust compiler without requiring a separate toolchain to get downloaded and configured. With that in mind, there's two modes of operation for the `wasm32-unknown-wasi` target: 1. By default the C standard library is statically provided inside of `liblibc.rlib` distributed as part of the sysroot. This means that you can `rustc foo.wasm --target wasm32-unknown-unknown` and you're good to go, a fully workable wasi binary pops out. This is incompatible with linking in C code, however, which may be compiled against a different sysroot than the Rust code was previously compiled against. In this mode the default of `rust-lld` is used to link binaries. 2. For linking with C code, the `-C target-feature=-crt-static` flag needs to be passed. This takes inspiration from the musl target for this flag, but the idea is that you're no longer using the provided static C runtime, but rather one will be provided externally. This flag is intended to also get coupled with an external `clang` compiler configured with its own sysroot. Therefore you'll typically use this flag with `-C linker=/path/to/clang-script-wrapper`. Using this mode the Rust code will continue to reference standard C symbols, but the definition will be pulled in by the linker configured. Alright so that's all the current state of this PR. I suspect we'll definitely want to discuss this before landing of course! This PR is coupled with libc changes as well which I'll be posting shortly. [LINK]: [wasmtime]:
- Loading branch information
1 parent
e782d79
commit ace7124
Showing
33 changed files
with
2,226 additions
and
82 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/sh | ||
# | ||
# ignore-tidy-linelength | ||
|
||
set -ex | ||
|
||
# Originally from https://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz | ||
curl https://s3-us-west-1.amazonaws.com/rust-lang-ci2/rust-ci-mirror/clang%2Bllvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz | \ | ||
tar xJf - | ||
export PATH=`pwd`/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-14.04/bin:$PATH | ||
|
||
git clone https://github.com/CraneStation/wasi-sysroot | ||
|
||
cd wasi-sysroot | ||
git reset --hard 320054e84f8f2440def3b1c8700cedb8fd697bf8 | ||
make -j$(nproc) INSTALL_DIR=/wasm32-unknown-wasi install | ||
|
||
cd .. | ||
rm -rf reference-sysroot-wasi | ||
rm -rf clang+llvm* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//! The `wasm32-unknown-wasi` target is a new and still (as of March 2019) | ||
//! experimental target. The definition in this file is likely to be tweaked | ||
//! over time and shouldn't be relied on too much. | ||
//! | ||
//! The `wasi` target is a proposal to define a standardized set of syscalls | ||
//! that WebAssembly files can interoperate with. This set of syscalls is | ||
//! intended to empower WebAssembly binaries with native capabilities such as | ||
//! filesystem access, network access, etc. | ||
//! | ||
//! You can see more about the proposal at https://wasi.dev | ||
//! | ||
//! The Rust target definition here is interesting in a few ways. We want to | ||
//! serve two use cases here with this target: | ||
//! | ||
//! * First, we want Rust usage of the target to be as hassle-free as possible, | ||
//! ideally avoiding the need to configure and install a local | ||
//! wasm32-unknown-wasi toolchain. | ||
//! | ||
//! * Second, one of the primary use cases of LLVM's new wasm backend and the | ||
//! wasm support in LLD is that any compiled language can interoperate with | ||
//! any other. To that the `wasm32-unknown-wasi` target is the first with a | ||
//! viable C standard library and sysroot common definition, so we want Rust | ||
//! and C/C++ code to interoperate when compiled to `wasm32-unknown-unknown`. | ||
//! | ||
//! You'll note, however, that the two goals above are somewhat at odds with one | ||
//! another. To attempt to solve both use cases in one go we define a target | ||
//! that (ab)uses the `crt-static` target feature to indicate which one you're | ||
//! in. | ||
//! | ||
//! ## No interop with C required | ||
//! | ||
//! By default the `crt-static` target feature is enabled, and when enabled | ||
//! this means that the the bundled version of `libc.a` found in `liblibc.rlib` | ||
//! is used. This isn't intended really for interoperation with a C because it | ||
//! may be the case that Rust's bundled C library is incompatible with a | ||
//! foreign-compiled C library. In this use case, though, we use `rust-lld` and | ||
//! some copied crt startup object files to ensure that you can download the | ||
//! wasi target for Rust and you're off to the races, no further configuration | ||
//! necessary. | ||
//! | ||
//! All in all, by default, no external dependencies are required. You can | ||
//! compile `wasm32-unknown-wasi` binaries straight out of the box. You can't, | ||
//! however, reliably interoperate with C code in this mode (yet). | ||
//! | ||
//! ## Interop with C required | ||
//! | ||
//! For the second goal we repurpose the `target-feature` flag, meaning that | ||
//! you'll need to do a few things to have C/Rust code interoperate. | ||
//! | ||
//! 1. All Rust code needs to be compiled with `-C target-feature=-crt-static`, | ||
//! indicating that the bundled C standard library in the Rust sysroot will | ||
//! not be used. | ||
//! | ||
//! 2. If you're using rustc to build a linked artifact then you'll need to | ||
//! specify `-C linker` to a `clang` binary that supports | ||
//! `wasm32-unknown-wasi` and is configured with the `wasm32-unknown-wasi` | ||
//! sysroot. This will cause Rust code to be linked against the libc.a that | ||
//! the specified `clang` provides. | ||
//! | ||
//! 3. If you're building a staticlib and integrating Rust code elsewhere, then | ||
//! compiling with `-C target-feature=-crt-static` is all you need to do. | ||
//! | ||
//! You can configure the linker via Cargo using the | ||
//! `CARGO_TARGET_WASM32_UNKNOWN_WASI_LINKER` env var. Be sure to also set | ||
//! `CC_wasm32-unknown-wasi` if any crates in the dependency graph are using | ||
//! the `cc` crate. | ||
//! | ||
//! ## Remember, this is all in flux | ||
//! | ||
//! The wasi target is **very** new in its specification. It's likely going to | ||
//! be a long effort to get it standardized and stable. We'll be following it as | ||
//! best we can with this target. Don't start relying on too much here unless | ||
//! you know what you're getting in to! | ||
|
||
use super::wasm32_base; | ||
use super::{LinkerFlavor, LldFlavor, Target}; | ||
|
||
pub fn target() -> Result<Target, String> { | ||
let mut options = wasm32_base::options(); | ||
|
||
options | ||
.pre_link_args | ||
.entry(LinkerFlavor::Gcc) | ||
.or_insert(Vec::new()) | ||
.push("--target=wasm32-unknown-wasi".to_string()); | ||
|
||
// When generating an executable be sure to put the startup object at the | ||
// front so the main function is correctly hooked up. | ||
options.pre_link_objects_exe_crt.push("crt1.o".to_string()); | ||
|
||
// Right now this is a bit of a workaround but we're currently saying that | ||
// the target by default has a static crt which we're taking as a signal | ||
// for "use the bundled crt". If that's turned off then the system's crt | ||
// will be used, but this means that default usage of this target doesn't | ||
// need an external compiler but it's still interoperable with an external | ||
// compiler if configured correctly. | ||
options.crt_static_default = true; | ||
options.crt_static_respected = true; | ||
|
||
Ok(Target { | ||
llvm_target: "wasm32-unknown-wasi".to_string(), | ||
target_endian: "little".to_string(), | ||
target_pointer_width: "32".to_string(), | ||
target_c_int_width: "32".to_string(), | ||
target_os: "unknown".to_string(), | ||
target_env: "wasi".to_string(), | ||
target_vendor: "unknown".to_string(), | ||
data_layout: "e-m:e-p:32:32-i64:64-n32:64-S128".to_string(), | ||
arch: "wasm32".to_string(), | ||
linker_flavor: LinkerFlavor::Lld(LldFlavor::Wasm), | ||
options, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! WASI-specific definitions | ||
|
||
#![stable(feature = "raw_ext", since = "1.1.0")] | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub use crate::sys::ext::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::alloc::{GlobalAlloc, Layout, System}; | ||
use crate::ptr; | ||
use crate::sys_common::alloc::{MIN_ALIGN, realloc_fallback}; | ||
use libc; | ||
|
||
#[stable(feature = "alloc_system_type", since = "1.28.0")] | ||
unsafe impl GlobalAlloc for System { | ||
#[inline] | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { | ||
libc::malloc(layout.size()) as *mut u8 | ||
} else { | ||
libc::aligned_alloc(layout.size(), layout.align()) as *mut u8 | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { | ||
libc::calloc(layout.size(), 1) as *mut u8 | ||
} else { | ||
let ptr = self.alloc(layout.clone()); | ||
if !ptr.is_null() { | ||
ptr::write_bytes(ptr, 0, layout.size()); | ||
} | ||
ptr | ||
} | ||
} | ||
|
||
#[inline] | ||
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { | ||
libc::free(ptr as *mut libc::c_void) | ||
} | ||
|
||
#[inline] | ||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | ||
if layout.align() <= MIN_ALIGN && layout.align() <= new_size { | ||
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 | ||
} else { | ||
realloc_fallback(self, ptr, layout, new_size) | ||
} | ||
} | ||
} |
Oops, something went wrong.