Skip to content

Commit aa8462b

Browse files
committed
Auto merge of #112922 - g0djan:godjan/wasi-threads, r=wesleywiser
WASI threads, implementation of wasm32-wasi-preview1-threads target This PR adds a target proposed in rust-lang/compiler-team#574 by `@abrown` and implementation of `std::thread::spawn` for the target `wasm32-wasi-preview1-threads` ### Tier 3 Target Policy As tier 3 targets, the new targets are required to adhere to [the tier 3 target policy](https://doc.rust-lang.org/nightly/rustc/target-tier-policy.html#tier-3-target-policy) requirements. This section quotes each requirement in entirety and describes how they are met. > - A tier 3 target must have a designated developer or developers (the "target maintainers") on record to be CCed when issues arise regarding the target. (The mechanism to track and CC such developers may evolve over time.) See [src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md](https://github.com/rust-lang/rust/pull/112922/files#diff-a48ee9d94f13e12be24eadd08eb47b479c153c340eeea4ef22276d876dfd4f3e). > - Targets must use naming consistent with any existing targets; for instance, a target for the same CPU or OS as an existing Rust target should use the same name for that CPU or OS. Targets should normally use the same names and naming conventions as used elsewhere in the broader ecosystem beyond Rust (such as in other toolchains), unless they have a very good reason to diverge. Changing the name of a target can be highly disruptive, especially once the target reaches a higher tier, so getting the name right is important even for a tier 3 target. > - Target names should not introduce undue confusion or ambiguity unless absolutely necessary to maintain ecosystem compatibility. For example, if the name of the target makes people extremely likely to form incorrect beliefs about what it targets, the name should be changed or augmented to disambiguate it. If possible, use only letters, numbers, dashes and underscores for the name. Periods (.) are known to cause issues in Cargo. The target is using the same name for $ARCH=wasm32 and $OS=wasi as existing Rust targets. The suffix `preview1` introduced to accurately set expectations because eventually this target will be deprecated and follows [MCP 607](rust-lang/compiler-team#607). The suffix `threads` indicates that it’s an extension that enables threads to the existing target and it follows [MCP 574](rust-lang/compiler-team#574) which describes the rationale behind introducing a separate target. > - Tier 3 targets may have unusual requirements to build or use, but must not create legal issues or impose onerous legal terms for the Rust project or for Rust developers or users. > - The target must not introduce license incompatibilities. > - Anything added to the Rust repository must be under the standard Rust license (MIT OR Apache-2.0). > - The target must not cause the Rust tools or libraries built for any other host (even when supporting cross-compilation to the target) to depend on any new dependency less permissive than the Rust licensing policy. This applies whether the dependency is a Rust crate that would require adding new license exceptions (as specified by the tidy tool in the rust-lang/rust repository), or whether the dependency is a native library or binary. In other words, the introduction of the target must not cause a user installing or running a version of Rust or the Rust tools to be subject to any new license requirements. > - Compiling, linking, and emitting functional binaries, libraries, or other code for the target (whether hosted on the target itself or cross-compiling from another target) must not depend on proprietary (non-FOSS) libraries. Host tools built for the target itself may depend on the ordinary runtime libraries supplied by the platform and commonly used by other applications built for the target, but those libraries must not be required for code generation for the target; cross-compilation to the target must not require such libraries at all. For instance, rustc built for the target may depend on a common proprietary C runtime library or console output library, but must not depend on a proprietary code generation library or code optimization library. Rust's license permits such combinations, but the Rust project has no interest in maintaining such combinations within the scope of Rust itself, even at tier 3. > - "onerous" here is an intentionally subjective term. At a minimum, "onerous" legal/licensing terms include but are not limited to: non-disclosure requirements, non-compete requirements, contributor license agreements (CLAs) or equivalent, "non-commercial"/"research-only"/etc terms, requirements conditional on the employer or employment of any particular Rust developers, revocable terms, any requirements that create liability for the Rust project or its developers or users, or any requirements that adversely affect the livelihood or prospects of the Rust project or its developers or users. This PR does not introduce any new dependency. The new target doesn’t support building host tools. > Tier 3 targets should attempt to implement as much of the standard libraries as possible and appropriate (core for most targets, alloc for targets that can support dynamic memory allocation, std for targets with an operating system or equivalent layer of system-provided functionality), but may leave some code unimplemented (either unavailable or stubbed out as appropriate), whether because the target makes it impossible to implement or challenging to implement. The authors of pull requests are not obligated to avoid calling any portions of the standard library on the basis of a tier 3 target not implementing those portions. The full standard library is available for this target as it’s an extension to an existing target that has already supported it. > The target must provide documentation for the Rust community explaining how to build for the target, using cross-compilation if possible. If the target supports running binaries, or running tests (even if they do not pass), the documentation must explain how to run such binaries or tests for the target, using emulation if possible or dedicated hardware if necessary. Only manual test running is supported at the moment with some tweaks in the test runner codebase. For build and running tests see [src/doc/rustc/src/platform-support/wasm32-wasi-preview1-threads.md](https://github.com/rust-lang/rust/pull/112922/files#diff-a48ee9d94f13e12be24eadd08eb47b479c153c340eeea4ef22276d876dfd4f3e). > - Neither this policy nor any decisions made regarding targets shall create any binding agreement or estoppel by any party. If any member of an approving Rust team serves as one of the maintainers of a target, or has any legal or employment requirement (explicit or implicit) that might affect their decisions regarding a target, they must recuse themselves from any approval decisions regarding the target's tier status, though they may otherwise participate in discussions. > - This requirement does not prevent part or all of this policy from being cited in an explicit contract or work agreement (e.g. to implement or maintain support for a target). This requirement exists to ensure that a developer or team responsible for reviewing and approving a target does not face any legal threats or obligations that would prevent them from freely exercising their judgment in such approval, even if such judgment involves subjective matters or goes beyond the letter of these requirements. > - Tier 3 targets must not impose burden on the authors of pull requests, or other developers in the community, to maintain the target. In particular, do not post comments (automated or manual) on a PR that derail or suggest a block on the PR based on a tier 3 target. Do not send automated messages or notifications (via any medium, including via `@)` to a PR author or others involved with a PR regarding a tier 3 target, unless they have opted into such messages. > - Backlinks such as those generated by the issue/PR tracker when linking to an issue or PR are not considered a violation of this policy, within reason. However, such messages (even on a separate repository) must not generate notifications to anyone involved with a PR who has not requested such notifications. > - Patches adding or updating tier 3 targets must not break any existing tier 2 or tier 1 target, and must not knowingly break another tier 3 target without approval of either the compiler team or the maintainers of the other tier 3 target. > - In particular, this may come up when working on closely related targets, such as variations of the same architecture with different features. Avoid introducing unconditional uses of features that another variation of the target may not have; use conditional compilation or runtime detection, as appropriate, to let each target run code supported by that target. I acknowledge these requirements and intend to ensure they are met.
2 parents f77c624 + 5697f16 commit aa8462b

File tree

15 files changed

+473
-31
lines changed

15 files changed

+473
-31
lines changed

Diff for: compiler/rustc_target/src/spec/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,7 @@ supported_targets! {
14041404
("wasm32-unknown-emscripten", wasm32_unknown_emscripten),
14051405
("wasm32-unknown-unknown", wasm32_unknown_unknown),
14061406
("wasm32-wasi", wasm32_wasi),
1407+
("wasm32-wasi-preview1-threads", wasm32_wasi_preview1_threads),
14071408
("wasm64-unknown-unknown", wasm64_unknown_unknown),
14081409

14091410
("thumbv6m-none-eabi", thumbv6m_none_eabi),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
//! The `wasm32-wasi-preview1-threads` target is a new and still (as of July 2023) an
2+
//! experimental target. The definition in this file is likely to be tweaked
3+
//! over time and shouldn't be relied on too much.
4+
//!
5+
//! The `wasi-threads` target is a proposal to define a standardized set of syscalls
6+
//! that WebAssembly files can interoperate with. This set of syscalls is
7+
//! intended to empower WebAssembly binaries with native capabilities such as
8+
//! threads, filesystem access, network access, etc.
9+
//!
10+
//! You can see more about the proposal at <https://github.com/WebAssembly/wasi-threads>.
11+
//!
12+
//! The Rust target definition here is interesting in a few ways. We want to
13+
//! serve two use cases here with this target:
14+
//!
15+
//! * First, we want Rust usage of the target to be as hassle-free as possible,
16+
//! ideally avoiding the need to configure and install a local wasm32-wasi-preview1-threads
17+
//! toolchain.
18+
//!
19+
//! * Second, one of the primary use cases of LLVM's new wasm backend and the
20+
//! wasm support in LLD is that any compiled language can interoperate with
21+
//! any other. To that the `wasm32-wasi-preview1-threads` target is the first with a viable C
22+
//! standard library and sysroot common definition, so we want Rust and C/C++
23+
//! code to interoperate when compiled to `wasm32-unknown-unknown`.
24+
//!
25+
//! You'll note, however, that the two goals above are somewhat at odds with one
26+
//! another. To attempt to solve both use cases in one go we define a target
27+
//! that (ab)uses the `crt-static` target feature to indicate which one you're
28+
//! in.
29+
//!
30+
//! ## No interop with C required
31+
//!
32+
//! By default the `crt-static` target feature is enabled, and when enabled
33+
//! this means that the bundled version of `libc.a` found in `liblibc.rlib`
34+
//! is used. This isn't intended really for interoperation with a C because it
35+
//! may be the case that Rust's bundled C library is incompatible with a
36+
//! foreign-compiled C library. In this use case, though, we use `rust-lld` and
37+
//! some copied crt startup object files to ensure that you can download the
38+
//! wasi target for Rust and you're off to the races, no further configuration
39+
//! necessary.
40+
//!
41+
//! All in all, by default, no external dependencies are required. You can
42+
//! compile `wasm32-wasi-preview1-threads` binaries straight out of the box. You can't, however,
43+
//! reliably interoperate with C code in this mode (yet).
44+
//!
45+
//! ## Interop with C required
46+
//!
47+
//! For the second goal we repurpose the `target-feature` flag, meaning that
48+
//! you'll need to do a few things to have C/Rust code interoperate.
49+
//!
50+
//! 1. All Rust code needs to be compiled with `-C target-feature=-crt-static`,
51+
//! indicating that the bundled C standard library in the Rust sysroot will
52+
//! not be used.
53+
//!
54+
//! 2. If you're using rustc to build a linked artifact then you'll need to
55+
//! specify `-C linker` to a `clang` binary that supports
56+
//! `wasm32-wasi-preview1-threads` and is configured with the `wasm32-wasi-preview1-threads` sysroot. This
57+
//! will cause Rust code to be linked against the libc.a that the specified
58+
//! `clang` provides.
59+
//!
60+
//! 3. If you're building a staticlib and integrating Rust code elsewhere, then
61+
//! compiling with `-C target-feature=-crt-static` is all you need to do.
62+
//!
63+
//! You can configure the linker via Cargo using the
64+
//! `CARGO_TARGET_WASM32_WASI_LINKER` env var. Be sure to also set
65+
//! `CC_wasm32-wasi-preview1-threads` if any crates in the dependency graph are using the `cc`
66+
//! crate.
67+
//!
68+
//! ## Remember, this is all in flux
69+
//!
70+
//! The wasi target is **very** new in its specification. It's likely going to
71+
//! be a long effort to get it standardized and stable. We'll be following it as
72+
//! best we can with this target. Don't start relying on too much here unless
73+
//! you know what you're getting in to!
74+
75+
use super::crt_objects::{self, LinkSelfContainedDefault};
76+
use super::{wasm_base, Cc, LinkerFlavor, Target};
77+
78+
pub fn target() -> Target {
79+
let mut options = wasm_base::options();
80+
81+
options.os = "wasi".into();
82+
83+
options.add_pre_link_args(
84+
LinkerFlavor::WasmLld(Cc::No),
85+
&["--import-memory", "--export-memory", "--shared-memory"],
86+
);
87+
options.add_pre_link_args(
88+
LinkerFlavor::WasmLld(Cc::Yes),
89+
&[
90+
"--target=wasm32-wasi-threads",
91+
"-Wl,--import-memory",
92+
"-Wl,--export-memory,",
93+
"-Wl,--shared-memory",
94+
],
95+
);
96+
97+
options.pre_link_objects_self_contained = crt_objects::pre_wasi_self_contained();
98+
options.post_link_objects_self_contained = crt_objects::post_wasi_self_contained();
99+
100+
// FIXME: Figure out cases in which WASM needs to link with a native toolchain.
101+
options.link_self_contained = LinkSelfContainedDefault::True;
102+
103+
// Right now this is a bit of a workaround but we're currently saying that
104+
// the target by default has a static crt which we're taking as a signal
105+
// for "use the bundled crt". If that's turned off then the system's crt
106+
// will be used, but this means that default usage of this target doesn't
107+
// need an external compiler but it's still interoperable with an external
108+
// compiler if configured correctly.
109+
options.crt_static_default = true;
110+
options.crt_static_respected = true;
111+
112+
// Allow `+crt-static` to create a "cdylib" output which is just a wasm file
113+
// without a main function.
114+
options.crt_static_allows_dylibs = true;
115+
116+
// WASI's `sys::args::init` function ignores its arguments; instead,
117+
// `args::args()` makes the WASI API calls itself.
118+
options.main_needs_argc_argv = false;
119+
120+
// And, WASI mangles the name of "main" to distinguish between different
121+
// signatures.
122+
options.entry_name = "__main_void".into();
123+
124+
options.singlethread = false;
125+
options.features = "+atomics,+bulk-memory,+mutable-globals".into();
126+
127+
Target {
128+
llvm_target: "wasm32-wasi".into(),
129+
pointer_width: 32,
130+
data_layout: "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20".into(),
131+
arch: "wasm32".into(),
132+
options,
133+
}
134+
}

Diff for: library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fortanix-sgx-abi = { version = "0.5.0", features = ['rustc-dep-of-std'], public
4747
[target.'cfg(target_os = "hermit")'.dependencies]
4848
hermit-abi = { version = "0.3.2", features = ['rustc-dep-of-std'], public = true }
4949

50-
[target.wasm32-wasi.dependencies]
50+
[target.'cfg(target_os = "wasi")'.dependencies]
5151
wasi = { version = "0.11.0", features = ['rustc-dep-of-std'], default-features = false }
5252

5353
[features]

Diff for: library/std/src/sys/wasi/mod.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ pub mod fs;
2929
#[path = "../wasm/atomics/futex.rs"]
3030
pub mod futex;
3131
pub mod io;
32-
#[path = "../unsupported/locks/mod.rs"]
33-
pub mod locks;
32+
3433
pub mod net;
3534
pub mod os;
3635
#[path = "../unix/os_str.rs"]
@@ -47,14 +46,27 @@ pub mod thread;
4746
pub mod thread_local_dtor;
4847
#[path = "../unsupported/thread_local_key.rs"]
4948
pub mod thread_local_key;
50-
#[path = "../unsupported/thread_parking.rs"]
51-
pub mod thread_parking;
5249
pub mod time;
5350

5451
cfg_if::cfg_if! {
55-
if #[cfg(not(target_feature = "atomics"))] {
52+
if #[cfg(target_feature = "atomics")] {
53+
#[path = "../unix/locks"]
54+
pub mod locks {
55+
#![allow(unsafe_op_in_unsafe_fn)]
56+
mod futex_condvar;
57+
mod futex_mutex;
58+
mod futex_rwlock;
59+
pub(crate) use futex_condvar::Condvar;
60+
pub(crate) use futex_mutex::Mutex;
61+
pub(crate) use futex_rwlock::RwLock;
62+
}
63+
} else {
64+
#[path = "../unsupported/locks/mod.rs"]
65+
pub mod locks;
5666
#[path = "../unsupported/once.rs"]
5767
pub mod once;
68+
#[path = "../unsupported/thread_parking.rs"]
69+
pub mod thread_parking;
5870
}
5971
}
6072

Diff for: library/std/src/sys/wasi/os.rs

+5
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> {
224224
})
225225
}
226226

227+
#[allow(dead_code)]
228+
pub fn page_size() -> usize {
229+
unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
230+
}
231+
227232
pub fn temp_dir() -> PathBuf {
228233
panic!("no filesystem on wasm")
229234
}

Diff for: library/std/src/sys/wasi/thread.rs

+118-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,120 @@
1-
#![deny(unsafe_op_in_unsafe_fn)]
2-
31
use crate::ffi::CStr;
42
use crate::io;
53
use crate::mem;
64
use crate::num::NonZeroUsize;
75
use crate::sys::unsupported;
86
use crate::time::Duration;
97

10-
pub struct Thread(!);
8+
cfg_if::cfg_if! {
9+
if #[cfg(target_feature = "atomics")] {
10+
use crate::cmp;
11+
use crate::ptr;
12+
use crate::sys::os;
13+
// Add a few symbols not in upstream `libc` just yet.
14+
mod libc {
15+
pub use crate::ffi;
16+
pub use crate::mem;
17+
pub use libc::*;
18+
19+
// defined in wasi-libc
20+
// https://github.com/WebAssembly/wasi-libc/blob/a6f871343313220b76009827ed0153586361c0d5/libc-top-half/musl/include/alltypes.h.in#L108
21+
#[repr(C)]
22+
union pthread_attr_union {
23+
__i: [ffi::c_int; if mem::size_of::<ffi::c_int>() == 8 { 14 } else { 9 }],
24+
__vi: [ffi::c_int; if mem::size_of::<ffi::c_int>() == 8 { 14 } else { 9 }],
25+
__s: [ffi::c_ulong; if mem::size_of::<ffi::c_int>() == 8 { 7 } else { 9 }],
26+
}
27+
28+
#[repr(C)]
29+
pub struct pthread_attr_t {
30+
__u: pthread_attr_union,
31+
}
32+
33+
#[allow(non_camel_case_types)]
34+
pub type pthread_t = *mut ffi::c_void;
35+
36+
extern "C" {
37+
pub fn pthread_create(
38+
native: *mut pthread_t,
39+
attr: *const pthread_attr_t,
40+
f: extern "C" fn(*mut ffi::c_void) -> *mut ffi::c_void,
41+
value: *mut ffi::c_void,
42+
) -> ffi::c_int;
43+
pub fn pthread_join(native: pthread_t, value: *mut *mut ffi::c_void) -> ffi::c_int;
44+
pub fn pthread_attr_init(attrp: *mut pthread_attr_t) -> ffi::c_int;
45+
pub fn pthread_attr_setstacksize(
46+
attr: *mut pthread_attr_t,
47+
stack_size: libc::size_t,
48+
) -> ffi::c_int;
49+
pub fn pthread_attr_destroy(attr: *mut pthread_attr_t) -> ffi::c_int;
50+
}
51+
}
52+
53+
pub struct Thread {
54+
id: libc::pthread_t,
55+
}
56+
} else {
57+
pub struct Thread(!);
58+
}
59+
}
1160

1261
pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
1362

1463
impl Thread {
1564
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
16-
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
17-
unsupported()
65+
cfg_if::cfg_if! {
66+
if #[cfg(target_feature = "atomics")] {
67+
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
68+
let p = Box::into_raw(Box::new(p));
69+
let mut native: libc::pthread_t = mem::zeroed();
70+
let mut attr: libc::pthread_attr_t = mem::zeroed();
71+
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
72+
73+
let stack_size = cmp::max(stack, DEFAULT_MIN_STACK_SIZE);
74+
75+
match libc::pthread_attr_setstacksize(&mut attr, stack_size) {
76+
0 => {}
77+
n => {
78+
assert_eq!(n, libc::EINVAL);
79+
// EINVAL means |stack_size| is either too small or not a
80+
// multiple of the system page size. Because it's definitely
81+
// >= PTHREAD_STACK_MIN, it must be an alignment issue.
82+
// Round up to the nearest page and try again.
83+
let page_size = os::page_size();
84+
let stack_size =
85+
(stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
86+
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
87+
}
88+
};
89+
90+
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
91+
// Note: if the thread creation fails and this assert fails, then p will
92+
// be leaked. However, an alternative design could cause double-free
93+
// which is clearly worse.
94+
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
95+
96+
return if ret != 0 {
97+
// The thread failed to start and as a result p was not consumed. Therefore, it is
98+
// safe to reconstruct the box so that it gets deallocated.
99+
drop(Box::from_raw(p));
100+
Err(io::Error::from_raw_os_error(ret))
101+
} else {
102+
Ok(Thread { id: native })
103+
};
104+
105+
extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
106+
unsafe {
107+
// Finally, let's run some code.
108+
Box::from_raw(main as *mut Box<dyn FnOnce()>)();
109+
}
110+
ptr::null_mut()
111+
}
112+
}
113+
} else {
114+
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
115+
unsupported()
116+
}
117+
}
18118
}
19119

20120
pub fn yield_now() {
@@ -62,7 +162,19 @@ impl Thread {
62162
}
63163

64164
pub fn join(self) {
65-
self.0
165+
cfg_if::cfg_if! {
166+
if #[cfg(target_feature = "atomics")] {
167+
unsafe {
168+
let ret = libc::pthread_join(self.id, ptr::null_mut());
169+
mem::forget(self);
170+
if ret != 0 {
171+
rtabort!("failed to join thread: {}", io::Error::from_raw_os_error(ret));
172+
}
173+
}
174+
} else {
175+
self.0
176+
}
177+
}
66178
}
67179
}
68180

Diff for: src/bootstrap/compile.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,14 @@ fn copy_self_contained_objects(
287287
let libunwind_path = copy_llvm_libunwind(builder, target, &libdir_self_contained);
288288
target_deps.push((libunwind_path, DependencyType::TargetSelfContained));
289289
}
290-
} else if target.ends_with("-wasi") {
290+
} else if target.contains("-wasi") {
291291
let srcdir = builder
292292
.wasi_root(target)
293293
.unwrap_or_else(|| {
294294
panic!("Target {:?} does not have a \"wasi-root\" key", target.triple)
295295
})
296-
.join("lib/wasm32-wasi");
296+
.join("lib")
297+
.join(target.to_string().replace("-preview1", ""));
297298
for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
298299
copy_and_stamp(
299300
builder,
@@ -393,9 +394,13 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
393394
}
394395
}
395396

396-
if target.ends_with("-wasi") {
397+
if target.contains("-wasi") {
397398
if let Some(p) = builder.wasi_root(target) {
398-
let root = format!("native={}/lib/wasm32-wasi", p.to_str().unwrap());
399+
let root = format!(
400+
"native={}/lib/{}",
401+
p.to_str().unwrap(),
402+
target.to_string().replace("-preview1", "")
403+
);
399404
cargo.rustflag("-L").rustflag(&root);
400405
}
401406
}

0 commit comments

Comments
 (0)