Skip to content

Commit

Permalink
Fix alignment of WASMER_METADATA in the dylib engine
Browse files Browse the repository at this point in the history
rykv requires this to be at least 16-byte aligned.
  • Loading branch information
Amanieu committed Jan 4, 2022
1 parent cb558c4 commit 78eb5bc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
1 change: 1 addition & 0 deletions lib/compiler-llvm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ impl LLVMCompiler {
metadata_gv.set_initializer(&metadata_init);
metadata_gv.set_linkage(Linkage::DLLExport);
metadata_gv.set_dll_storage_class(DLLStorageClass::Export);
metadata_gv.set_alignment(16);

if self.config().enable_verifier {
merged_module.verify().unwrap();
Expand Down
28 changes: 10 additions & 18 deletions lib/engine-dylib/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! to be done as separate steps.
use crate::engine::{DylibEngine, DylibEngineInner};
use crate::serialize::{ArchivedModuleMetadata, ModuleMetadata};
use crate::serialize::ModuleMetadata;
use enumset::EnumSet;
use libloading::{Library, Symbol as LibrarySymbol};
use loupe::MemoryUsage;
Expand Down Expand Up @@ -217,7 +217,7 @@ impl DylibArtifact {

let serialized_data = metadata.serialize()?;

let mut metadata_binary = vec![0; 12];
let mut metadata_binary = vec![0; 16];
let mut writable = &mut metadata_binary[..];
leb128::write::unsigned(&mut writable, serialized_data.len() as u64)
.expect("Should write number");
Expand Down Expand Up @@ -256,13 +256,8 @@ impl DylibArtifact {
function_body_inputs,
)?;
let mut obj = get_object_for_target(&target_triple).map_err(to_compile_error)?;
emit_data(
&mut obj,
WASMER_METADATA_SYMBOL,
&metadata_binary,
std::mem::align_of::<ArchivedModuleMetadata>() as u64,
)
.map_err(to_compile_error)?;
emit_data(&mut obj, WASMER_METADATA_SYMBOL, &metadata_binary, 16)
.map_err(to_compile_error)?;
emit_compilation(&mut obj, compilation, &symbol_registry, &target_triple)
.map_err(to_compile_error)?;
let file = tempfile::Builder::new()
Expand Down Expand Up @@ -623,27 +618,24 @@ impl DylibArtifact {
DeserializeError::CorruptedBinary(format!("Library loading failed: {}", e))
})?;
let shared_path: PathBuf = PathBuf::from(path);
// We use 12 + 1, as the length of the module will take 12 bytes
// (we construct it like that in `metadata_length`) and we also want
// to take the first element of the data to construct the slice from
// it.
let symbol: LibrarySymbol<*mut [u8; 12 + 1]> =
// We reserve 16 bytes for the length because the rest of the metadata
// needs to be aligned to 16 bytes.
let symbol: LibrarySymbol<*mut [u8; 16]> =
lib.get(WASMER_METADATA_SYMBOL).map_err(|e| {
DeserializeError::CorruptedBinary(format!(
"The provided object file doesn't seem to be generated by Wasmer: {}",
e
))
})?;
use std::ops::Deref;
use std::slice;

let size = &mut **symbol.deref();
let mut readable = &size[..];
let metadata = &**symbol;
let mut readable = &metadata[..];
let metadata_len = leb128::read::unsigned(&mut readable).map_err(|_e| {
DeserializeError::CorruptedBinary("Can't read metadata size".to_string())
})?;
let metadata_slice: &'static [u8] =
slice::from_raw_parts(&size[12] as *const u8, metadata_len as usize);
slice::from_raw_parts(metadata.as_ptr().add(16), metadata_len as usize);

let metadata = ModuleMetadata::deserialize(metadata_slice)?;

Expand Down

0 comments on commit 78eb5bc

Please sign in to comment.