| 1 |
use core::alloc::Layout; |
| 2 |
use core::ptr::{self, NonNull}; |
| 3 |
|
| 4 |
// Re-export `bitflags` so that we can reference it from macros. |
| 5 |
#[cfg(feature = "bitflags")] |
| 6 |
pub use bitflags; |
| 7 |
|
| 8 |
#[cfg(not(feature = "bitflags"))] |
| 9 |
pub mod bitflags { |
| 10 |
#[macro_export] |
| 11 |
macro_rules! bitflags { |
| 12 |
( |
| 13 |
$(#[$attr:meta])* |
| 14 |
$vis:vis struct $name:ident : $repr:ty { |
| 15 |
$( |
| 16 |
$(#[$flag_attr:meta])* |
| 17 |
const $flag:ident = $val:expr; |
| 18 |
)* |
| 19 |
} |
| 20 |
) => { |
| 21 |
$(#[$attr])* |
| 22 |
$vis struct $name { |
| 23 |
bits: $repr, |
| 24 |
} |
| 25 |
|
| 26 |
impl $name { |
| 27 |
$( |
| 28 |
$(#[$flag_attr])* |
| 29 |
$vis const $flag: Self = Self { bits: $val }; |
| 30 |
)* |
| 31 |
$vis fn empty() -> Self { |
| 32 |
Self { bits: 0 } |
| 33 |
} |
| 34 |
|
| 35 |
$vis fn from_bits_retain(bits: $repr) -> Self { |
| 36 |
Self { bits } |
| 37 |
} |
| 38 |
|
| 39 |
$vis fn bits(&self) -> $repr { |
| 40 |
self.bits |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl core::ops::BitOr<$name> for $name { |
| 45 |
type Output = Self; |
| 46 |
fn bitor(self, rhs: $name) -> $name { |
| 47 |
Self { bits: self.bits | rhs.bits } |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl core::ops::BitAnd<$name> for $name { |
| 52 |
type Output = Self; |
| 53 |
fn bitand(self, rhs: $name) -> $name { |
| 54 |
Self { bits: self.bits & rhs.bits } |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl core::ops::BitXor<$name> for $name { |
| 59 |
type Output = Self; |
| 60 |
fn bitxor(self, rhs: $name) -> $name { |
| 61 |
Self { bits: self.bits ^ rhs.bits } |
| 62 |
} |
| 63 |
} |
| 64 |
}; |
| 65 |
} |
| 66 |
|
| 67 |
pub use crate::bitflags; |
| 68 |
} |
| 69 |
|
| 70 |
/// For more information about this see `./ci/rebuild-libwit-bindgen-cabi.sh`. |
| 71 |
#[cfg(not(target_env = "p2"))] |
| 72 |
mod wit_bindgen_cabi_realloc; |
| 73 |
|
| 74 |
/// This function is called from generated bindings and will be deleted by |
| 75 |
/// the linker. The purpose of this function is to force a reference to the |
| 76 |
/// symbol `cabi_realloc` to make its way through to the final linker |
| 77 |
/// command line. That way `wasm-ld` will pick it up, see it needs to be |
| 78 |
/// exported, and then export it. |
| 79 |
/// |
| 80 |
/// For more information about this see `./ci/rebuild-libwit-bindgen-cabi.sh`. |
| 81 |
pub fn maybe_link_cabi_realloc() { |
| 82 |
#[cfg(all(target_family = "wasm", not(target_env = "p2")))] |
| 83 |
{ |
| 84 |
extern "C" { |
| 85 |
fn cabi_realloc( |
| 86 |
old_ptr: *mut u8, |
| 87 |
old_len: usize, |
| 88 |
align: usize, |
| 89 |
new_len: usize, |
| 90 |
) -> *mut u8; |
| 91 |
} |
| 92 |
// Force the `cabi_realloc` symbol to be referenced from here. This |
| 93 |
// is done with a `#[used]` Rust `static` to ensure that this |
| 94 |
// reference makes it all the way to the linker before it's |
| 95 |
// considered for garbage collection. When the linker sees it it'll |
| 96 |
// remove this `static` here (due to it not actually being needed) |
| 97 |
// but the linker will have at that point seen the `cabi_realloc` |
| 98 |
// symbol and it should get exported. |
| 99 |
#[used] |
| 100 |
static _NAME_DOES_NOT_MATTER: unsafe extern "C" fn( |
| 101 |
*mut u8, |
| 102 |
usize, |
| 103 |
usize, |
| 104 |
usize, |
| 105 |
) -> *mut u8 = cabi_realloc; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/// NB: this function is called by a generated function in the |
| 110 |
/// `cabi_realloc` module above. It's otherwise never explicitly called. |
| 111 |
/// |
| 112 |
/// For more information about this see `./ci/rebuild-libwit-bindgen-cabi.sh`. |
| 113 |
#[cfg(not(target_env = "p2"))] |
| 114 |
pub unsafe fn cabi_realloc( |
| 115 |
old_ptr: *mut u8, |
| 116 |
old_len: usize, |
| 117 |
align: usize, |
| 118 |
new_len: usize, |
| 119 |
) -> *mut u8 { |
| 120 |
use alloc::alloc::{alloc as allocate, handle_alloc_error, realloc, Layout}; |
| 121 |
|
| 122 |
let layout; |
| 123 |
let ptr = if old_len == 0 { |
| 124 |
if new_len == 0 { |
| 125 |
return align as *mut u8; |
| 126 |
} |
| 127 |
layout = Layout::from_size_align_unchecked(new_len, align); |
| 128 |
allocate(layout) |
| 129 |
} else { |
| 130 |
debug_assert_ne!(new_len, 0, "non-zero old_len requires non-zero new_len!"); |
| 131 |
layout = Layout::from_size_align_unchecked(old_len, align); |
| 132 |
realloc(old_ptr, layout, new_len) |
| 133 |
}; |
| 134 |
if ptr.is_null() { |
| 135 |
// Print a nice message in debug mode, but in release mode don't |
| 136 |
// pull in so many dependencies related to printing so just emit an |
| 137 |
// `unreachable` instruction. |
| 138 |
if cfg!(debug_assertions) { |
| 139 |
handle_alloc_error(layout); |
| 140 |
} else { |
| 141 |
#[cfg(target_arch = "wasm32")] |
| 142 |
core::arch::wasm32::unreachable(); |
| 143 |
#[cfg(not(target_arch = "wasm32"))] |
| 144 |
unreachable!(); |
| 145 |
} |
| 146 |
} |
| 147 |
return ptr; |
| 148 |
} |
| 149 |
|
| 150 |
/// Provide a hook for generated export functions to run static constructors at |
| 151 |
/// most once. |
| 152 |
/// |
| 153 |
/// wit-bindgen-rust generates a call to this function at the start of all |
| 154 |
/// component export functions. Importantly, it is not called as part of |
| 155 |
/// `cabi_realloc`, which is a *core* export func, but should not execute ctors. |
| 156 |
#[cfg(target_arch = "wasm32")] |
| 157 |
pub fn run_ctors_once() { |
| 158 |
static mut RUN: bool = false; |
| 159 |
unsafe { |
| 160 |
if !RUN { |
| 161 |
// This function is synthesized by `wasm-ld` to run all static |
| 162 |
// constructors. wasm-ld will either provide an implementation |
| 163 |
// of this symbol, or synthesize a wrapper around each |
| 164 |
// exported function to (unconditionally) run ctors. By using |
| 165 |
// this function, the linked module is opting into "manually" |
| 166 |
// running ctors. |
| 167 |
extern "C" { |
| 168 |
fn __wasm_call_ctors(); |
| 169 |
} |
| 170 |
__wasm_call_ctors(); |
| 171 |
RUN = true; |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/// Support for using the Component Model Async ABI |
| 177 |
#[cfg(feature = "async")] |
| 178 |
pub mod async_support; |
| 179 |
|
| 180 |
/// Cleanup helper used to deallocate blocks of canonical ABI data from |
| 181 |
/// lowerings. |
| 182 |
pub struct Cleanup { |
| 183 |
ptr: NonNull<u8>, |
| 184 |
layout: Layout, |
| 185 |
} |
| 186 |
|
| 187 |
// Usage of the returned pointer is always unsafe and must abide by these |
| 188 |
// conventions, but this structure itself has no inherent reason to not be |
| 189 |
// send/sync. |
| 190 |
unsafe impl Send for Cleanup {} |
| 191 |
unsafe impl Sync for Cleanup {} |
| 192 |
|
| 193 |
impl Cleanup { |
| 194 |
/// Allocates a chunk of memory with `layout` and returns an object to clean |
| 195 |
/// it up. |
| 196 |
/// |
| 197 |
/// Always returns a pointer which is null if `layout` has size zero. The |
| 198 |
/// optional cleanup returned will be present if `layout` has a non-zero |
| 199 |
/// size. When dropped `Cleanup` will deallocate the pointer returned. |
| 200 |
pub fn new(layout: Layout) -> (*mut u8, Option<Cleanup>) { |
| 201 |
use alloc::alloc; |
| 202 |
|
| 203 |
if layout.size() == 0 { |
| 204 |
return (ptr::null_mut(), None); |
| 205 |
} |
| 206 |
let ptr = unsafe { alloc::alloc(layout) }; |
| 207 |
let ptr = match NonNull::new(ptr) { |
| 208 |
Some(ptr) => ptr, |
| 209 |
None => alloc::handle_alloc_error(layout), |
| 210 |
}; |
| 211 |
(ptr.as_ptr(), Some(Cleanup { ptr, layout })) |
| 212 |
} |
| 213 |
|
| 214 |
/// Discards this cleanup to leak its memory or intentionally transfer |
| 215 |
/// ownership to some other location. |
| 216 |
pub fn forget(self) { |
| 217 |
core::mem::forget(self); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
impl Drop for Cleanup { |
| 222 |
fn drop(&mut self) { |
| 223 |
unsafe { |
| 224 |
for i in 0..self.layout.size() { |
| 225 |
*self.ptr.add(i).as_ptr() = 0xff; |
| 226 |
} |
| 227 |
alloc::alloc::dealloc(self.ptr.as_ptr(), self.layout); |
| 228 |
} |
| 229 |
} |
| 230 |
} |