Skip to content

Commit

Permalink
Make sure vmoffset are aligned to pointer size (for #4059) (#4167)
Browse files Browse the repository at this point in the history
* Make sure vmoffset are aligned to pointer size (for #4059)

* Fix linter

* Be more conservative on alignment

* Bumped MAGIC Header version as vmoffset changed

* Update linux wasmu files for CI deserialize test

* Updated windows wasmu files for deserialize CI test
  • Loading branch information
ptitSeb committed Aug 29, 2023
1 parent a4d23a3 commit bab924a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/types/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ pub struct MetadataHeader {
impl MetadataHeader {
/// Current ABI version. Increment this any time breaking changes are made
/// to the format of the serialized data.
pub const CURRENT_VERSION: u32 = 4;
pub const CURRENT_VERSION: u32 = 5;

/// Magic number to identify wasmer metadata.
const MAGIC: [u8; 8] = *b"WASMER\0\0";
Expand Down
23 changes: 17 additions & 6 deletions lib/types/src/vmoffsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
};
use more_asserts::assert_lt;
use std::convert::TryFrom;
use std::mem::size_of;

/// An index type for builtin functions.
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -284,34 +285,44 @@ impl VMOffsets {
base.checked_add(num_items.checked_mul(item_size).unwrap())
.unwrap()
}
/// Offset base by num_items items of size item_size, panicking on overflow
/// Also, will align the value on pointer size boundary,
/// to avoid misalignement issue
fn offset_by_aligned(base: u32, num_items: u32, item_size: u32) -> u32 {
align(
base.checked_add(num_items.checked_mul(item_size).unwrap())
.unwrap(),
size_of::<&u32>() as u32,
)
}

self.vmctx_signature_ids_begin = 0;
self.vmctx_imported_functions_begin = offset_by(
self.vmctx_imported_functions_begin = offset_by_aligned(
self.vmctx_signature_ids_begin,
self.num_signature_ids,
u32::from(self.size_of_vmshared_signature_index()),
);
self.vmctx_imported_tables_begin = offset_by(
self.vmctx_imported_tables_begin = offset_by_aligned(
self.vmctx_imported_functions_begin,
self.num_imported_functions,
u32::from(self.size_of_vmfunction_import()),
);
self.vmctx_imported_memories_begin = offset_by(
self.vmctx_imported_memories_begin = offset_by_aligned(
self.vmctx_imported_tables_begin,
self.num_imported_tables,
u32::from(self.size_of_vmtable_import()),
);
self.vmctx_imported_globals_begin = offset_by(
self.vmctx_imported_globals_begin = offset_by_aligned(
self.vmctx_imported_memories_begin,
self.num_imported_memories,
u32::from(self.size_of_vmmemory_import()),
);
self.vmctx_tables_begin = offset_by(
self.vmctx_tables_begin = offset_by_aligned(
self.vmctx_imported_globals_begin,
self.num_imported_globals,
u32::from(self.size_of_vmglobal_import()),
);
self.vmctx_memories_begin = offset_by(
self.vmctx_memories_begin = offset_by_aligned(
self.vmctx_tables_begin,
self.num_local_tables,
u32::from(self.size_of_vmtable_definition()),
Expand Down
Loading

0 comments on commit bab924a

Please sign in to comment.