| 1 |
#![no_std] |
| 2 |
|
| 3 |
extern crate alloc; |
| 4 |
|
| 5 |
// Re-export `bitflags` so that we can reference it from macros. |
| 6 |
#[cfg(feature = "bitflags")] |
| 7 |
#[doc(hidden)] |
| 8 |
pub use bitflags; |
| 9 |
|
| 10 |
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`. |
| 11 |
#[cfg(not(target_env = "p2"))] |
| 12 |
mod cabi_realloc; |
| 13 |
|
| 14 |
/// This function is called from generated bindings and will be deleted by |
| 15 |
/// the linker. The purpose of this function is to force a reference to the |
| 16 |
/// symbol `cabi_realloc` to make its way through to the final linker |
| 17 |
/// command line. That way `wasm-ld` will pick it up, see it needs to be |
| 18 |
/// exported, and then export it. |
| 19 |
/// |
| 20 |
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`. |
| 21 |
pub fn maybe_link_cabi_realloc() { |
| 22 |
#[cfg(all(target_family = "wasm", not(target_env = "p2")))] |
| 23 |
{ |
| 24 |
extern "C" { |
| 25 |
fn cabi_realloc( |
| 26 |
old_ptr: *mut u8, |
| 27 |
old_len: usize, |
| 28 |
align: usize, |
| 29 |
new_len: usize, |
| 30 |
) -> *mut u8; |
| 31 |
} |
| 32 |
// Force the `cabi_realloc` symbol to be referenced from here. This |
| 33 |
// is done with a `#[used]` Rust `static` to ensure that this |
| 34 |
// reference makes it all the way to the linker before it's |
| 35 |
// considered for garbage collection. When the linker sees it it'll |
| 36 |
// remove this `static` here (due to it not actually being needed) |
| 37 |
// but the linker will have at that point seen the `cabi_realloc` |
| 38 |
// symbol and it should get exported. |
| 39 |
#[used] |
| 40 |
static _NAME_DOES_NOT_MATTER: unsafe extern "C" fn( |
| 41 |
*mut u8, |
| 42 |
usize, |
| 43 |
usize, |
| 44 |
usize, |
| 45 |
) -> *mut u8 = cabi_realloc; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// NB: this function is called by a generated function in the |
| 50 |
/// `cabi_realloc` module above. It's otherwise never explicitly called. |
| 51 |
/// |
| 52 |
/// For more information about this see `./ci/rebuild-libcabi-realloc.sh`. |
| 53 |
#[cfg(not(target_env = "p2"))] |
| 54 |
pub unsafe fn cabi_realloc( |
| 55 |
old_ptr: *mut u8, |
| 56 |
old_len: usize, |
| 57 |
align: usize, |
| 58 |
new_len: usize, |
| 59 |
) -> *mut u8 { |
| 60 |
use self::alloc::alloc::{self, Layout}; |
| 61 |
|
| 62 |
let layout; |
| 63 |
let ptr = if old_len == 0 { |
| 64 |
if new_len == 0 { |
| 65 |
return align as *mut u8; |
| 66 |
} |
| 67 |
layout = Layout::from_size_align_unchecked(new_len, align); |
| 68 |
alloc::alloc(layout) |
| 69 |
} else { |
| 70 |
debug_assert_ne!(new_len, 0, "non-zero old_len requires non-zero new_len!"); |
| 71 |
layout = Layout::from_size_align_unchecked(old_len, align); |
| 72 |
alloc::realloc(old_ptr, layout, new_len) |
| 73 |
}; |
| 74 |
if ptr.is_null() { |
| 75 |
// Print a nice message in debug mode, but in release mode don't |
| 76 |
// pull in so many dependencies related to printing so just emit an |
| 77 |
// `unreachable` instruction. |
| 78 |
if cfg!(debug_assertions) { |
| 79 |
alloc::handle_alloc_error(layout); |
| 80 |
} else { |
| 81 |
#[cfg(target_arch = "wasm32")] |
| 82 |
core::arch::wasm32::unreachable(); |
| 83 |
#[cfg(not(target_arch = "wasm32"))] |
| 84 |
unreachable!(); |
| 85 |
} |
| 86 |
} |
| 87 |
return ptr; |
| 88 |
} |
| 89 |
|
| 90 |
/// Provide a hook for generated export functions to run static constructors at |
| 91 |
/// most once. |
| 92 |
/// |
| 93 |
/// wit-bindgen-rust generates a call to this function at the start of all |
| 94 |
/// component export functions. Importantly, it is not called as part of |
| 95 |
/// `cabi_realloc`, which is a *core* export func, but should not execute ctors. |
| 96 |
#[cfg(target_arch = "wasm32")] |
| 97 |
pub fn run_ctors_once() { |
| 98 |
static mut RUN: bool = false; |
| 99 |
unsafe { |
| 100 |
if !RUN { |
| 101 |
// This function is synthesized by `wasm-ld` to run all static |
| 102 |
// constructors. wasm-ld will either provide an implementation |
| 103 |
// of this symbol, or synthesize a wrapper around each |
| 104 |
// exported function to (unconditionally) run ctors. By using |
| 105 |
// this function, the linked module is opting into "manually" |
| 106 |
// running ctors. |
| 107 |
extern "C" { |
| 108 |
fn __wasm_call_ctors(); |
| 109 |
} |
| 110 |
__wasm_call_ctors(); |
| 111 |
RUN = true; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/// Support for using the Component Model Async ABI |
| 117 |
#[cfg(feature = "async")] |
| 118 |
pub mod async_support; |