Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure vmoffset are aligned to pointer size (for #4059) #4167

Merged
merged 7 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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