Skip to content

Commit abb0393

Browse files
Rollup merge of #122411 - alexcrichton:wasm32-wasip2-cabi-realloc, r=m-ou-se
Provide cabi_realloc on wasm32-wasip2 by default This commit provides a component model intrinsic in the standard library by default on the `wasm32-wasip2` target. This intrinsic is not required by the component model itself but is quite common to use, for example it's needed if a wasm module receives a string or a list. The intention of this commit is to provide an overridable definition in the standard library through a weak definition of this function. That means that downstream crates can provide their own customized and more specific versions if they'd like, but the standard library's version should suffice for general-purpose use.
2 parents 8938f88 + 5af8187 commit abb0393

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

library/std/src/sys/pal/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ cfg_if::cfg_if! {
3737
} else if #[cfg(target_os = "hermit")] {
3838
mod hermit;
3939
pub use self::hermit::*;
40-
} else if #[cfg(target_os = "wasi")] {
41-
mod wasi;
42-
pub use self::wasi::*;
4340
} else if #[cfg(all(target_os = "wasi", target_env = "p2"))] {
4441
mod wasip2;
4542
pub use self::wasip2::*;
43+
} else if #[cfg(target_os = "wasi")] {
44+
mod wasi;
45+
pub use self::wasi::*;
4646
} else if #[cfg(target_family = "wasm")] {
4747
mod wasm;
4848
pub use self::wasm::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//! This module contains a canonical definition of the `cabi_realloc` function
2+
//! for the component model.
3+
//!
4+
//! The component model's canonical ABI for representing datatypes in memory
5+
//! makes use of this function when transferring lists and strings, for example.
6+
//! This function behaves like C's `realloc` but also takes alignment into
7+
//! account.
8+
//!
9+
//! Components are notably not required to export this function, but nearly
10+
//! all components end up doing so currently. This definition in the standard
11+
//! library removes the need for all compilations to define this themselves.
12+
//!
13+
//! More information about the canonical ABI can be found at
14+
//! <https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md>
15+
//!
16+
//! Note that the name of this function is not standardized in the canonical ABI
17+
//! at this time. Instead it's a convention of the "componentization process"
18+
//! where a core wasm module is converted to a component to use this name.
19+
//! Additionally this is not the only possible definition of this function, so
20+
//! this is defined as a "weak" symbol. This means that other definitions are
21+
//! allowed to overwrite it if they are present in a compilation.
22+
23+
use crate::alloc::{self, Layout};
24+
use crate::ptr;
25+
26+
#[used]
27+
static FORCE_CODEGEN_OF_CABI_REALLOC: unsafe extern "C" fn(
28+
*mut u8,
29+
usize,
30+
usize,
31+
usize,
32+
) -> *mut u8 = cabi_realloc;
33+
34+
#[linkage = "weak"]
35+
#[no_mangle]
36+
pub unsafe extern "C" fn cabi_realloc(
37+
old_ptr: *mut u8,
38+
old_len: usize,
39+
align: usize,
40+
new_len: usize,
41+
) -> *mut u8 {
42+
let layout;
43+
let ptr = if old_len == 0 {
44+
if new_len == 0 {
45+
return ptr::without_provenance_mut(align);
46+
}
47+
layout = Layout::from_size_align_unchecked(new_len, align);
48+
alloc::alloc(layout)
49+
} else {
50+
debug_assert_ne!(new_len, 0, "non-zero old_len requires non-zero new_len!");
51+
layout = Layout::from_size_align_unchecked(old_len, align);
52+
alloc::realloc(old_ptr, layout, new_len)
53+
};
54+
if ptr.is_null() {
55+
// Print a nice message in debug mode, but in release mode don't
56+
// pull in so many dependencies related to printing so just emit an
57+
// `unreachable` instruction.
58+
if cfg!(debug_assertions) {
59+
alloc::handle_alloc_error(layout);
60+
} else {
61+
super::abort_internal();
62+
}
63+
}
64+
return ptr;
65+
}

library/std/src/sys/pal/wasip2/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
pub mod alloc;
1111
#[path = "../wasi/args.rs"]
1212
pub mod args;
13-
#[path = "../unix/cmath.rs"]
14-
pub mod cmath;
1513
#[path = "../wasi/env.rs"]
1614
pub mod env;
1715
#[path = "../wasi/fd.rs"]
@@ -28,10 +26,6 @@ pub mod io;
2826
pub mod net;
2927
#[path = "../wasi/os.rs"]
3028
pub mod os;
31-
#[path = "../unix/os_str.rs"]
32-
pub mod os_str;
33-
#[path = "../unix/path.rs"]
34-
pub mod path;
3529
#[path = "../unsupported/pipe.rs"]
3630
pub mod pipe;
3731
#[path = "../unsupported/process.rs"]
@@ -72,3 +66,5 @@ pub use helpers::decode_error_kind;
7266
use helpers::err2io;
7367
pub use helpers::hashmap_random_keys;
7468
pub use helpers::is_interrupted;
69+
70+
mod cabi_realloc;

0 commit comments

Comments
 (0)