From 362948d5bccd8dfe4af1d3b21077c4edcd93459b Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Wed, 27 Jul 2022 20:19:25 +0300 Subject: [PATCH] compiler: move MetadataHeader to wasmer-types --- .../src/artifact_builders/artifact_builder.rs | 3 +- lib/compiler/src/engine/artifact.rs | 3 +- lib/compiler/src/traits.rs | 73 +------------------ 3 files changed, 5 insertions(+), 74 deletions(-) diff --git a/lib/compiler/src/artifact_builders/artifact_builder.rs b/lib/compiler/src/artifact_builders/artifact_builder.rs index 7a3c9132a17..321434b031a 100644 --- a/lib/compiler/src/artifact_builders/artifact_builder.rs +++ b/lib/compiler/src/artifact_builders/artifact_builder.rs @@ -7,14 +7,13 @@ use crate::ArtifactCreate; #[cfg(feature = "compiler")] use crate::EngineInner; use crate::Features; -use crate::MetadataHeader; -#[cfg(feature = "compiler")] use crate::{ModuleEnvironment, ModuleMiddlewareChain}; use enumset::EnumSet; use std::mem; use wasmer_types::entity::PrimaryMap; #[cfg(feature = "compiler")] use wasmer_types::CompileModuleInfo; +use wasmer_types::MetadataHeader; use wasmer_types::SerializeError; use wasmer_types::{ CompileError, CpuFeature, CustomSection, Dwarf, FunctionIndex, LocalFunctionIndex, MemoryIndex, diff --git a/lib/compiler/src/engine/artifact.rs b/lib/compiler/src/engine/artifact.rs index 778c6224a78..876b8957792 100644 --- a/lib/compiler/src/engine/artifact.rs +++ b/lib/compiler/src/engine/artifact.rs @@ -9,13 +9,14 @@ use crate::Features; use crate::ModuleEnvironment; use crate::{ register_frame_info, resolve_imports, FunctionExtent, GlobalFrameInfoRegistration, - InstantiationError, MetadataHeader, RuntimeError, Tunables, + InstantiationError, RuntimeError, Tunables, }; use crate::{Engine, EngineInner}; use enumset::EnumSet; use std::sync::Arc; use std::sync::Mutex; use wasmer_types::entity::{BoxedSlice, PrimaryMap}; +use wasmer_types::MetadataHeader; use wasmer_types::{ CompileError, CpuFeature, DataInitializer, DeserializeError, FunctionIndex, LocalFunctionIndex, MemoryIndex, ModuleInfo, OwnedDataInitializer, SerializableModule, SerializeError, diff --git a/lib/compiler/src/traits.rs b/lib/compiler/src/traits.rs index e8da762451e..8b0cb748794 100644 --- a/lib/compiler/src/traits.rs +++ b/lib/compiler/src/traits.rs @@ -3,14 +3,13 @@ use crate::Features; use enumset::EnumSet; use std::any::Any; -use std::convert::TryInto; +use std::fs; use std::path::Path; -use std::{fs, mem}; use wasmer_types::entity::PrimaryMap; +use wasmer_types::SerializeError; use wasmer_types::{ CpuFeature, MemoryIndex, MemoryStyle, ModuleInfo, OwnedDataInitializer, TableIndex, TableStyle, }; -use wasmer_types::{DeserializeError, SerializeError}; /// An `Artifact` is the product that the `Engine` /// implementation produce and use. @@ -87,71 +86,3 @@ impl dyn ArtifactCreate + 'static { self.upcast_any_mut().downcast_mut::() } } - -/// Metadata header which holds an ABI version and the length of the remaining -/// metadata. -#[repr(C)] -#[derive(Clone, Copy)] -pub struct MetadataHeader { - magic: [u8; 8], - version: u32, - len: u32, -} - -impl MetadataHeader { - /// Current ABI version. Increment this any time breaking changes are made - /// to the format of the serialized data. - const CURRENT_VERSION: u32 = 1; - - /// Magic number to identify wasmer metadata. - const MAGIC: [u8; 8] = *b"WASMER\0\0"; - - /// Length of the metadata header. - pub const LEN: usize = 16; - - /// Alignment of the metadata. - pub const ALIGN: usize = 16; - - /// Creates a new header for metadata of the given length. - pub fn new(len: usize) -> Self { - Self { - magic: Self::MAGIC, - version: Self::CURRENT_VERSION, - len: len.try_into().expect("metadata exceeds maximum length"), - } - } - - /// Convert the header into its bytes representation. - pub fn into_bytes(self) -> [u8; 16] { - unsafe { mem::transmute(self) } - } - - /// Parses the header and returns the length of the metadata following it. - pub fn parse(bytes: &[u8]) -> Result { - if bytes.as_ptr() as usize % 16 != 0 { - return Err(DeserializeError::CorruptedBinary( - "misaligned metadata".to_string(), - )); - } - let bytes: [u8; 16] = bytes - .get(..16) - .ok_or_else(|| { - DeserializeError::CorruptedBinary("invalid metadata header".to_string()) - })? - .try_into() - .unwrap(); - let header: Self = unsafe { mem::transmute(bytes) }; - if header.magic != Self::MAGIC { - return Err(DeserializeError::Incompatible( - "The provided bytes were not serialized by Wasmer".to_string(), - )); - } - if header.version != Self::CURRENT_VERSION { - return Err(DeserializeError::Incompatible( - "The provided bytes were serialized by an incompatible version of Wasmer" - .to_string(), - )); - } - Ok(header.len as usize) - } -}