Using IntoBytes only for Module::deserialise (both sys and js)

This commit is contained in:
ptitSeb
2022-09-01 13:52:53 +02:00
parent 553d6bb8cd
commit 436bec735b
4 changed files with 31 additions and 49 deletions

View File

@@ -169,19 +169,15 @@ impl Module {
/// # } /// # }
/// ``` /// ```
#[allow(unreachable_code)] #[allow(unreachable_code)]
pub fn new(_store: &impl AsStoreRef, bytes: impl IntoBytes) -> Result<Self, CompileError> { pub fn new(_store: &impl AsStoreRef, bytes: impl AsRef<[u8]>) -> Result<Self, CompileError> {
let mut bytes = bytes.into_bytes();
#[cfg(feature = "wat")] #[cfg(feature = "wat")]
if bytes.starts_with(b"\0asm") == false { let bytes = wat::parse_bytes(bytes.as_ref()).map_err(|e| {
let parsed_bytes = wat::parse_bytes(bytes.as_ref()).map_err(|e| {
CompileError::Wasm(WasmError::Generic(format!( CompileError::Wasm(WasmError::Generic(format!(
"Error when converting wat: {}", "Error when converting wat: {}",
e e
))) )))
})?; })?;
bytes = Bytes::from(parsed_bytes.to_vec()); Self::from_binary(_store, bytes.as_ref())
}
Self::from_binary(_store, bytes)
} }
/// Creates a new WebAssembly module from a file path. /// Creates a new WebAssembly module from a file path.
@@ -197,11 +193,7 @@ impl Module {
/// Opposed to [`Module::new`], this function is not compatible with /// Opposed to [`Module::new`], this function is not compatible with
/// the WebAssembly text format (if the "wat" feature is enabled for /// the WebAssembly text format (if the "wat" feature is enabled for
/// this crate). /// this crate).
pub fn from_binary( pub fn from_binary(_store: &impl AsStoreRef, binary: &[u8]) -> Result<Self, CompileError> {
_store: &impl AsStoreRef,
binary: impl IntoBytes,
) -> Result<Self, CompileError> {
let binary = binary.into_bytes();
// //
// Self::validate(store, binary)?; // Self::validate(store, binary)?;
unsafe { Self::from_binary_unchecked(_store, binary) } unsafe { Self::from_binary_unchecked(_store, binary) }
@@ -215,9 +207,8 @@ impl Module {
/// We maintain the `unsafe` to preserve the same API as Wasmer /// We maintain the `unsafe` to preserve the same API as Wasmer
pub unsafe fn from_binary_unchecked( pub unsafe fn from_binary_unchecked(
_store: &impl AsStoreRef, _store: &impl AsStoreRef,
binary: impl IntoBytes, binary: &[u8],
) -> Result<Self, CompileError> { ) -> Result<Self, CompileError> {
let binary = binary.into_bytes();
let js_bytes = Uint8Array::view(&binary[..]); let js_bytes = Uint8Array::view(&binary[..]);
let module = WebAssembly::Module::new(&js_bytes.into()).unwrap(); let module = WebAssembly::Module::new(&js_bytes.into()).unwrap();
@@ -260,7 +251,7 @@ impl Module {
/// This validation is normally pretty fast and checks the enabled /// This validation is normally pretty fast and checks the enabled
/// WebAssembly features in the Store Engine to assure deterministic /// WebAssembly features in the Store Engine to assure deterministic
/// validation of the Module. /// validation of the Module.
pub fn validate(_store: &impl AsStoreRef, binary: impl IntoBytes) -> Result<(), CompileError> { pub fn validate(_store: &impl AsStoreRef, binary: &[u8]) -> Result<(), CompileError> {
let binary = binary.into_bytes(); let binary = binary.into_bytes();
let js_bytes = unsafe { Uint8Array::view(&binary[..]) }; let js_bytes = unsafe { Uint8Array::view(&binary[..]) };
match WebAssembly::validate(&js_bytes.into()) { match WebAssembly::validate(&js_bytes.into()) {

View File

@@ -158,19 +158,15 @@ impl Module {
/// # } /// # }
/// ``` /// ```
#[allow(unreachable_code)] #[allow(unreachable_code)]
pub fn new(store: &impl AsStoreRef, bytes: impl IntoBytes) -> Result<Self, CompileError> { pub fn new(store: &impl AsStoreRef, bytes: impl AsRef<[u8]>) -> Result<Self, CompileError> {
let mut bytes = bytes.into_bytes();
#[cfg(feature = "wat")] #[cfg(feature = "wat")]
if !bytes.starts_with(b"\0asm") { let bytes = wat::parse_bytes(bytes.as_ref()).map_err(|e| {
let parsed_bytes = wat::parse_bytes(&bytes[..]).map_err(|e| {
CompileError::Wasm(WasmError::Generic(format!( CompileError::Wasm(WasmError::Generic(format!(
"Error when converting wat: {}", "Error when converting wat: {}",
e e
))) )))
})?; })?;
bytes = Bytes::from(parsed_bytes.to_vec()); Self::from_binary(store, bytes.as_ref())
}
Self::from_binary(store, bytes)
} }
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
@@ -182,7 +178,7 @@ impl Module {
let file_ref = file.as_ref(); let file_ref = file.as_ref();
let canonical = file_ref.canonicalize()?; let canonical = file_ref.canonicalize()?;
let wasm_bytes = std::fs::read(file_ref)?; let wasm_bytes = std::fs::read(file_ref)?;
let mut module = Self::new(store, wasm_bytes)?; let mut module = Self::new(store, &wasm_bytes)?;
// Set the module name to the absolute path of the filename. // Set the module name to the absolute path of the filename.
// This is useful for debugging the stack traces. // This is useful for debugging the stack traces.
let filename = canonical.as_path().to_str().unwrap(); let filename = canonical.as_path().to_str().unwrap();
@@ -196,12 +192,8 @@ impl Module {
/// Opposed to [`Module::new`], this function is not compatible with /// Opposed to [`Module::new`], this function is not compatible with
/// the WebAssembly text format (if the "wat" feature is enabled for /// the WebAssembly text format (if the "wat" feature is enabled for
/// this crate). /// this crate).
pub fn from_binary( pub fn from_binary(store: &impl AsStoreRef, binary: &[u8]) -> Result<Self, CompileError> {
store: &impl AsStoreRef, Self::validate(store, binary)?;
binary: impl IntoBytes,
) -> Result<Self, CompileError> {
let binary = binary.into_bytes();
Self::validate(store, binary.clone())?;
unsafe { Self::from_binary_unchecked(store, binary) } unsafe { Self::from_binary_unchecked(store, binary) }
} }
@@ -215,9 +207,8 @@ impl Module {
/// beforehand. /// beforehand.
pub unsafe fn from_binary_unchecked( pub unsafe fn from_binary_unchecked(
store: &impl AsStoreRef, store: &impl AsStoreRef,
binary: impl IntoBytes, binary: &[u8],
) -> Result<Self, CompileError> { ) -> Result<Self, CompileError> {
let binary = binary.into_bytes();
let module = Self::compile(store, binary)?; let module = Self::compile(store, binary)?;
Ok(module) Ok(module)
} }
@@ -229,13 +220,12 @@ impl Module {
/// This validation is normally pretty fast and checks the enabled /// This validation is normally pretty fast and checks the enabled
/// WebAssembly features in the Store Engine to assure deterministic /// WebAssembly features in the Store Engine to assure deterministic
/// validation of the Module. /// validation of the Module.
pub fn validate(store: &impl AsStoreRef, binary: impl IntoBytes) -> Result<(), CompileError> { pub fn validate(store: &impl AsStoreRef, binary: &[u8]) -> Result<(), CompileError> {
let binary = binary.into_bytes(); store.as_store_ref().engine().validate(binary)
store.as_store_ref().engine().validate(&binary[..])
} }
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
fn compile(store: &impl AsStoreRef, binary: impl IntoBytes) -> Result<Self, CompileError> { fn compile(store: &impl AsStoreRef, binary: &[u8]) -> Result<Self, CompileError> {
let binary = binary.into_bytes(); let binary = binary.into_bytes();
let artifact = store let artifact = store
.as_store_ref() .as_store_ref()
@@ -306,9 +296,10 @@ impl Module {
/// ``` /// ```
pub unsafe fn deserialize( pub unsafe fn deserialize(
store: &impl AsStoreRef, store: &impl AsStoreRef,
bytes: &[u8], bytes: impl IntoBytes,
) -> Result<Self, DeserializeError> { ) -> Result<Self, DeserializeError> {
let artifact = store.as_store_ref().engine().deserialize(bytes)?; let bytes = bytes.into_bytes();
let artifact = store.as_store_ref().engine().deserialize(&bytes)?;
Ok(Self::from_artifact(artifact)) Ok(Self::from_artifact(artifact))
} }

View File

@@ -27,7 +27,7 @@ impl Validate {
if !is_wasm(&module_contents) { if !is_wasm(&module_contents) {
bail!("`wasmer validate` only validates WebAssembly files"); bail!("`wasmer validate` only validates WebAssembly files");
} }
Module::validate(&store, module_contents)?; Module::validate(&store, &module_contents)?;
eprintln!("Validation passed for `{}`.", self.path.display()); eprintln!("Validation passed for `{}`.", self.path.display());
Ok(()) Ok(())
} }

View File

@@ -45,7 +45,7 @@ fn test_deserialize(config: crate::Config) -> Result<()> {
let serialized_bytes = module.serialize()?; let serialized_bytes = module.serialize()?;
let headless_store = config.headless_store(); let headless_store = config.headless_store();
let deserialized_module = unsafe { Module::deserialize(&headless_store, &serialized_bytes)? }; let deserialized_module = unsafe { Module::deserialize(&headless_store, serialized_bytes)? };
assert_eq!(deserialized_module.name(), Some("name")); assert_eq!(deserialized_module.name(), Some("name"));
assert_eq!( assert_eq!(
deserialized_module.exports().collect::<Vec<_>>(), deserialized_module.exports().collect::<Vec<_>>(),