Removed Instance::new_no_memory_init, use custom LinearMemory with custom memory_init method to get the same result

This commit is contained in:
ptitSeb
2022-10-04 14:48:51 +02:00
parent d581152641
commit c27547e41e
6 changed files with 53 additions and 76 deletions

View File

@@ -5,11 +5,13 @@
//!
//! `Memory` is to WebAssembly linear memories what `Table` is to WebAssembly tables.
use crate::trap::{Trap, TrapCode};
use crate::{mmap::Mmap, store::MaybeInstanceOwned, vmcontext::VMMemoryDefinition};
use more_asserts::assert_ge;
use std::cell::UnsafeCell;
use std::convert::TryInto;
use std::ptr::NonNull;
use std::slice;
use wasmer_types::{Bytes, MemoryError, MemoryStyle, MemoryType, Pages};
// The memory mapped area
@@ -410,4 +412,22 @@ where
/// Attempts to clone this memory (if its clonable)
fn try_clone(&self) -> Option<Box<dyn LinearMemory + 'static>>;
#[doc(hidden)]
unsafe fn initialize_with_data(&self, start: usize, data: &[u8]) -> Result<(), Trap> {
let memory = self.vmmemory().as_ref();
if start
.checked_add(data.len())
.map_or(true, |end| end > memory.current_length)
{
return Err(Trap::lib(TrapCode::HeapAccessOutOfBounds));
}
let mem_slice = slice::from_raw_parts_mut(memory.base, memory.current_length);
let end = start + data.len();
let to_init = &mut mem_slice[start..end];
to_init.copy_from_slice(data);
Ok(())
}
}