Skip to content

Commit

Permalink
wrap up
Browse files Browse the repository at this point in the history
  • Loading branch information
ovstinga authored and camilbancioiu committed Oct 4, 2022
1 parent 54cd3cc commit eeb8096
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/middleware-common/src/opcode_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl FunctionMiddleware for OpcodeControl {
}
}

/// Sets the number of points used by an Instance.
/// Set internal field `FIELD_MEMORY_GROW_COUNT` to 0.
pub fn reset_memory_grow_count(instance: &mut Instance) {
instance.set_internal(&FIELD_MEMORY_GROW_COUNT, 0);
}
13 changes: 2 additions & 11 deletions lib/runtime-c-api/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ use wasmer_middleware_common::opcode_control::{self, reset_memory_grow_count};
use wasmer_middleware_common::opcode_trace;
use wasmer_middleware_common::runtime_breakpoints;

use wasmer_runtime_core::backing::{RESET_BACKING_GLOBALS, RESET_BACKING_MEMORIES};

/// Opaque pointer to a `wasmer_runtime::Instance` value in Rust.
///
/// A `wasmer_runtime::Instance` represents a WebAssembly instance. It
Expand Down Expand Up @@ -252,10 +250,10 @@ pub unsafe extern "C" fn wasmer_instantiate_with_options(
wasmer_result_t::WASMER_OK
}

/// Resets an WebAssembly instance, cleaning memory and globals
/// Reset an WebAssembly instance, cleaning memories and globals
#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_instantiate_reset(
pub unsafe extern "C" fn wasmer_instance_reset(
instance: *mut wasmer_instance_t,
) -> wasmer_result_t {
if instance.is_null() {
Expand Down Expand Up @@ -724,10 +722,3 @@ pub extern "C" fn wasmer_instance_destroy(instance: *mut wasmer_instance_t) {
unsafe { Box::from_raw(instance as *mut Instance) };
}
}

#[allow(clippy::cast_ptr_alignment)]
#[no_mangle]
pub unsafe extern "C" fn wasmer_instance_reset_options(reset_memories: bool, reset_globals: bool) {
RESET_BACKING_MEMORIES = reset_memories;
RESET_BACKING_GLOBALS = reset_globals;
}
10 changes: 4 additions & 6 deletions lib/runtime-c-api/wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,10 @@ uint64_t wasmer_instance_get_runtime_breakpoint_value(wasmer_instance_t *instanc
*/
bool wasmer_instance_is_function_imported(wasmer_instance_t *instance, const char *name);

void wasmer_instance_reset_options(bool reset_memories, bool reset_globals);
/**
* Reset an WebAssembly instance, cleaning memories and globals
*/
wasmer_result_t wasmer_instance_reset(wasmer_instance_t *instance);

void wasmer_instance_set_points_limit(wasmer_instance_t *instance, uint64_t limit);

Expand Down Expand Up @@ -1150,11 +1153,6 @@ wasmer_result_t wasmer_instantiate(wasmer_instance_t **instance,
wasmer_import_t *imports,
int imports_len);

/**
* Resets an WebAssembly instance, cleaning memory and globals
*/
wasmer_result_t wasmer_instantiate_reset(wasmer_instance_t *instance);

wasmer_result_t wasmer_instantiate_with_options(wasmer_instance_t **instance,
uint8_t *wasm_bytes,
uint32_t wasm_bytes_len,
Expand Down
6 changes: 2 additions & 4 deletions lib/runtime-c-api/wasmer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ uint64_t wasmer_instance_get_runtime_breakpoint_value(wasmer_instance_t *instanc
/// Verifies whether the specified function name is imported by the given instance.
bool wasmer_instance_is_function_imported(wasmer_instance_t *instance, const char *name);

void wasmer_instance_reset_options(bool reset_memories, bool reset_globals);
/// Reset an WebAssembly instance, cleaning memories and globals
wasmer_result_t wasmer_instance_reset(wasmer_instance_t *instance);

void wasmer_instance_set_points_limit(wasmer_instance_t *instance, uint64_t limit);

Expand Down Expand Up @@ -947,9 +948,6 @@ wasmer_result_t wasmer_instantiate(wasmer_instance_t **instance,
wasmer_import_t *imports,
int imports_len);

/// Resets an WebAssembly instance, cleaning memory and globals
wasmer_result_t wasmer_instantiate_reset(wasmer_instance_t *instance);

wasmer_result_t wasmer_instantiate_with_options(wasmer_instance_t **instance,
uint8_t *wasm_bytes,
uint32_t wasm_bytes_len,
Expand Down
35 changes: 12 additions & 23 deletions lib/runtime-core/src/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ pub const INTERNALS_SIZE: usize = 256;

pub(crate) struct Internals(pub(crate) [u64; INTERNALS_SIZE]);

pub static mut RESET_BACKING_MEMORIES: bool = true;
pub static mut RESET_BACKING_GLOBALS: bool = true;

impl Debug for Internals {
fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(formatter, "Internals({:?})", &self.0[..])
Expand Down Expand Up @@ -107,37 +104,29 @@ impl LocalBacking {
}

/// Resets the `LocalBacking` (`Memories` and `Globals`) for an `Instance` using the provided `ModuleInfo`
pub(crate) fn reset(&mut self, module_info: &ModuleInfo) -> RuntimeResult<()> {
unsafe {
if RESET_BACKING_MEMORIES {
Self::reset_memories(&module_info, &mut self.memories)?;
}

if RESET_BACKING_GLOBALS {
Self::reset_globals(&module_info, &mut self.globals)?;
}
}
Ok(())
pub(crate) fn reset(&self, module_info: &ModuleInfo) -> RuntimeResult<()> {
Self::reset_memories(&module_info, &self.memories)?;
Self::reset_globals(&module_info, &self.globals)
}

fn reset_memories(
module_info: &ModuleInfo,
memories: &mut SliceMap<LocalMemoryIndex, Memory>,
memories: &SliceMap<LocalMemoryIndex, Memory>,
) -> RuntimeResult<()> {
Self::shrink_memories(memories)?;
Self::zero_memories(memories);
Self::reinitialize_memories(module_info, memories)
}

fn shrink_memories(memories: &mut SliceMap<LocalMemoryIndex, Memory>) -> RuntimeResult<()> {
for (_index, memory) in memories.iter_mut() {
fn shrink_memories(memories: &SliceMap<LocalMemoryIndex, Memory>) -> RuntimeResult<()> {
for (_index, memory) in memories.iter() {
memory.shrink_to_minimum()?;
}
Ok(())
}

fn zero_memories(memories: &mut SliceMap<LocalMemoryIndex, Memory>) {
for (_index, memory) in memories.iter_mut() {
fn zero_memories(memories: &SliceMap<LocalMemoryIndex, Memory>) {
for (_index, memory) in memories.iter() {
let view = &memory.view::<u64>();
for cell in view.iter() {
cell.set(0);
Expand All @@ -147,7 +136,7 @@ impl LocalBacking {

fn reinitialize_memories(
module_info: &ModuleInfo,
memories: &mut SliceMap<LocalMemoryIndex, Memory>,
memories: &SliceMap<LocalMemoryIndex, Memory>,
) -> RuntimeResult<()> {
for data_initializer in module_info.data_initializers.iter() {
let DataInitializer {
Expand All @@ -167,7 +156,7 @@ impl LocalBacking {
};

match memory_index.local_or_import(&module_info) {
LocalOrImport::Local(index) => match memories.get_mut(index) {
LocalOrImport::Local(index) => match memories.get(index) {
Some(memory) => {
let cells = &memory.view()[offset..offset + data.len()];
let zipped_cells = cells.iter().zip(data.iter());
Expand All @@ -189,7 +178,7 @@ impl LocalBacking {

fn reset_globals(
module_info: &ModuleInfo,
globals: &mut SliceMap<LocalGlobalIndex, Global>,
globals: &SliceMap<LocalGlobalIndex, Global>,
) -> RuntimeResult<()> {
for (index, global_init) in module_info.globals.iter() {
let GlobalInit { desc, init } = global_init;
Expand All @@ -202,7 +191,7 @@ impl LocalBacking {
};

if desc.mutable {
match globals.get_mut(index) {
match globals.get(index) {
Some(global) => global.set(value),
None => return Err(RuntimeError(Box::new("Undefined global"))),
}
Expand Down
5 changes: 2 additions & 3 deletions lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ impl Instance {
Ok(instance)
}

/// Resets an `Instance`
/// Reset an `Instance`
pub fn reset(&mut self) -> RuntimeResult<()> {
self.inner.backing.reset(&self.module.info)?;
Ok(())
self.inner.backing.reset(&self.module.info)
}

/// Load an `Instance` using the given loader.
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime-core/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Memory {
fn validate_memory_pages_count(pages: Pages) -> CreationResult<()> {
if pages > MAX_MEMORY_PAGES_COUNT {
return Err(CreationError::InvalidDescriptor(format!(
"Number of memory pages used: {:?} is more than the alllowed number of pages: {:?}",
"Number of memory pages used: {:?} is more than the allowed number of pages: {:?}",
pages, MAX_MEMORY_PAGES_COUNT
)));
}
Expand Down Expand Up @@ -330,7 +330,7 @@ impl UnsharedMemory {

match &mut *storage {
UnsharedMemoryStorage::Dynamic(dynamic_memory) => {
dynamic_memory.shrink_to_minimum(&mut local)
dynamic_memory.shrink_to_minimum(&mut local);
}
UnsharedMemoryStorage::Static(_static_memory) => {
return Err(RuntimeError(Box::new("Cannot shrink static memory")));
Expand Down

0 comments on commit eeb8096

Please sign in to comment.