Fix double-counting imports in VMOffsets calculations (#4209)

* Fix double-counting imports in `VMOffsets` calculations

This fixes an oversight in the initial creation of `VMOffsets` for a
module to avoid double-counting imported globals, tables, and memories
for calculating the size of the `VMContext`. Prior to this PR imported
items are accidentally also counted as defined items for sizing
calculations meaning that when a memory is imported but not defined, for
example, the `VMContext` will have a space for an inline
`VMMemoryDefinition` when it doesn't need to.

Auditing where all this relates to it appears that the only issue from
this mistake is that `VMContext` is a bit larger than it would otherwise
need to be. Extra slots are uninitialized memory but nothing in Wasmtime
ever actually accesses the memory either, so it should be harmless to
have extra space here. Nevertheless it seems better to shrink the size
as much as possible to avoid wasting space where we can.

* Fix tests
This commit is contained in:
Alex Crichton
2022-06-02 13:39:38 -05:00
committed by GitHub
parent 0cf0230432
commit 9f5f978baa
3 changed files with 11 additions and 18 deletions

View File

@@ -62,8 +62,6 @@ pub struct VMOffsets<P> {
pub num_imported_memories: u32,
/// The number of imported globals in the module.
pub num_imported_globals: u32,
/// The number of defined functions in the module.
pub num_defined_functions: u32,
/// The number of defined tables in the module.
pub num_defined_tables: u32,
/// The number of defined memories in the module.
@@ -129,8 +127,6 @@ pub struct VMOffsetsFields<P> {
pub num_imported_memories: u32,
/// The number of imported globals in the module.
pub num_imported_globals: u32,
/// The number of defined functions in the module.
pub num_defined_functions: u32,
/// The number of defined tables in the module.
pub num_defined_tables: u32,
/// The number of defined memories in the module.
@@ -151,10 +147,11 @@ impl<P: PtrSize> VMOffsets<P> {
num_imported_tables: cast_to_u32(module.num_imported_tables),
num_imported_memories: cast_to_u32(module.num_imported_memories),
num_imported_globals: cast_to_u32(module.num_imported_globals),
num_defined_functions: cast_to_u32(module.functions.len()),
num_defined_tables: cast_to_u32(module.table_plans.len()),
num_defined_memories: cast_to_u32(module.memory_plans.len()),
num_defined_globals: cast_to_u32(module.globals.len()),
num_defined_tables: cast_to_u32(module.table_plans.len() - module.num_imported_tables),
num_defined_memories: cast_to_u32(
module.memory_plans.len() - module.num_imported_memories,
),
num_defined_globals: cast_to_u32(module.globals.len() - module.num_imported_globals),
num_escaped_funcs: cast_to_u32(module.num_escaped_funcs),
})
}
@@ -183,7 +180,6 @@ impl<P: PtrSize> VMOffsets<P> {
num_defined_tables: _,
num_defined_globals: _,
num_defined_memories: _,
num_defined_functions: _,
num_escaped_funcs: _,
// used as the initial size below
@@ -237,7 +233,6 @@ impl<P: PtrSize> From<VMOffsetsFields<P>> for VMOffsets<P> {
num_imported_tables: fields.num_imported_tables,
num_imported_memories: fields.num_imported_memories,
num_imported_globals: fields.num_imported_globals,
num_defined_functions: fields.num_defined_functions,
num_defined_tables: fields.num_defined_tables,
num_defined_memories: fields.num_defined_memories,
num_defined_globals: fields.num_defined_globals,

View File

@@ -1041,7 +1041,6 @@ mod tests {
num_imported_tables: 0,
num_imported_memories: 0,
num_imported_globals: 0,
num_defined_functions: 0,
num_defined_tables: 0,
num_defined_memories: 0,
num_defined_globals: 0,
@@ -1068,7 +1067,6 @@ mod tests {
num_imported_tables: 0,
num_imported_memories: 0,
num_imported_globals: 0,
num_defined_functions: 0,
num_defined_tables: 0,
num_defined_memories: 0,
num_defined_globals: 0,
@@ -1095,7 +1093,6 @@ mod tests {
num_imported_tables: 0,
num_imported_memories: 0,
num_imported_globals: 0,
num_defined_functions: 0,
num_defined_tables: 0,
num_defined_memories: 0,
num_defined_globals: 0,