pub enum MemoryInitialization {
Segmented(Vec<MemoryInitializer>),
Paged {
map: PrimaryMap<MemoryIndex, Vec<(u64, Range<u32>)>>,
},
}
Expand description
The type of WebAssembly linear memory initialization to use for a module.
Variants
Segmented(Vec<MemoryInitializer>)
Memory initialization is segmented.
Segmented initialization can be used for any module, but it is required if:
- A data segment referenced an imported memory.
- A data segment uses a global base.
Segmented initialization is performed by processing the complete set of data segments when the module is instantiated.
This is the default memory initialization type.
Paged
Fields
map: PrimaryMap<MemoryIndex, Vec<(u64, Range<u32>)>>
The map of defined memory index to a list of initialization pages.
The list of page data is sparse, with each element starting with
the offset in memory where it will be placed (specified here, as
a page index, with a u64
). Each page of initialization data is
WebAssembly page-sized (64 KiB). Pages whose offset are not
specified in this array start with 0s in memory. The Range
indices, like those in MemoryInitializer
, point within a data
segment that will come as an auxiliary descriptor with other data
such as the compiled code for the wasm module.
Memory initialization is paged.
To be paged, the following requirements must be met:
- All data segments must reference defined memories.
- All data segments must not use a global base.
Paged initialization is performed by copying (or mapping) entire WebAssembly pages to each linear memory.
The uffd
feature makes use of this type of memory initialization
because it can instruct the kernel to back an entire WebAssembly page
from an existing set of in-memory pages.
By processing the data segments at module compilation time, the uffd fault handler doesn’t have to do any work to point the kernel at the right linear memory page to use.
Implementations
sourceimpl MemoryInitialization
impl MemoryInitialization
sourcepub fn is_segmented(&self) -> bool
pub fn is_segmented(&self) -> bool
Returns whether this initialization is of the form
MemoryInitialization::Segmented
.
sourcepub fn init_memory(
&self,
state: InitMemory<'_>,
write: &mut dyn FnMut(MemoryIndex, u64, &Range<u32>) -> bool
) -> bool
pub fn init_memory(
&self,
state: InitMemory<'_>,
write: &mut dyn FnMut(MemoryIndex, u64, &Range<u32>) -> bool
) -> bool
Performs the memory initialization steps for this set of initializers.
This will perform wasm initialization in compliance with the wasm spec and how data segments are processed. This doesn’t need to necessarily only be called as part of initialization, however, as it’s structured to allow learning about memory ahead-of-time at compile time possibly.
The various callbacks provided here are used to drive the smaller bits of initialization, such as:
-
get_cur_size_in_pages
- gets the current size, in wasm pages, of the memory specified. For compile-time purposes this would be the memory type’s minimum size. -
get_global
- gets the value of the global specified. This is statically, via validation, a pointer to the global of the correct type (either u32 or u64 depending on the memory), but the value returned here isu64
. ANone
value can be returned to indicate that the global’s value isn’t known yet. -
write
- a callback used to actually write data. This indicates that the specified memory must receive the specified range of data at the specified offset. This can internally return an false error if it wants to fail.
This function will return true if all memory initializers are processed
successfully. If any initializer hits an error or, for example, a
global value is needed but None
is returned, then false will be
returned. At compile-time this typically means that the “error” in
question needs to be deferred to runtime, and at runtime this means
that an invalid initializer has been found and a trap should be
generated.
Trait Implementations
sourceimpl Clone for MemoryInitialization
impl Clone for MemoryInitialization
sourcefn clone(&self) -> MemoryInitialization
fn clone(&self) -> MemoryInitialization
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for MemoryInitialization
impl Debug for MemoryInitialization
sourceimpl Default for MemoryInitialization
impl Default for MemoryInitialization
sourceimpl<'de> Deserialize<'de> for MemoryInitialization
impl<'de> Deserialize<'de> for MemoryInitialization
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Serialize for MemoryInitialization
impl Serialize for MemoryInitialization
Auto Trait Implementations
impl RefUnwindSafe for MemoryInitialization
impl Send for MemoryInitialization
impl Sync for MemoryInitialization
impl Unpin for MemoryInitialization
impl UnwindSafe for MemoryInitialization
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more