From 038101e93da2b9a748ed035aa00f21f2a657281c 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 | 2 +- lib/compiler/src/engine/artifact.rs | 3 +- lib/compiler/src/traits.rs | 73 +------------------ 3 files changed, 5 insertions(+), 73 deletions(-) diff --git a/lib/compiler/src/artifact_builders/artifact_builder.rs b/lib/compiler/src/artifact_builders/artifact_builder.rs index 9558c0b2dc2..82df23855ac 100644 --- a/lib/compiler/src/artifact_builders/artifact_builder.rs +++ b/lib/compiler/src/artifact_builders/artifact_builder.rs @@ -4,7 +4,6 @@ #[cfg(feature = "engine_compilation")] use super::trampoline::{libcall_trampoline_len, make_libcall_trampolines}; use crate::Features; -use crate::MetadataHeader; use crate::{ArtifactCreate, EngineBuilder}; #[cfg(feature = "engine_compilation")] use crate::{ModuleEnvironment, ModuleMiddlewareChain}; @@ -13,6 +12,7 @@ use std::mem; use wasmer_types::entity::PrimaryMap; #[cfg(feature = "engine_compilation")] 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 1409ecde6f5..977674a5486 100644 --- a/lib/compiler/src/engine/artifact.rs +++ b/lib/compiler/src/engine/artifact.rs @@ -10,12 +10,13 @@ use crate::Features; use crate::ModuleEnvironment; use crate::{ register_frame_info, resolve_imports, FunctionExtent, GlobalFrameInfoRegistration, - InstantiationError, MetadataHeader, RuntimeError, Tunables, + InstantiationError, RuntimeError, Tunables, }; 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) - } -}