Skip to content

Commit

Permalink
Fix a panic with custom-page-sizes and pooling allocation (#9547)
Browse files Browse the repository at this point in the history
This commit fixes a similar panic to one found in #9533 where the
pooling allocator was combined with modules using custom page sizes. The
fix is similar where a variable needs page-aligning where previously it
wasn't necessary due to wasm sizes always being page-aligned.
  • Loading branch information
alexcrichton authored Nov 4, 2024
1 parent 0cabb39 commit 6ec4854
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,11 @@ fn calculate(constraints: &SlabConstraints) -> Result<SlabLayout> {
guard_before_slots,
} = *constraints;

// Page-align the maximum size of memory since that's the granularity that
// permissions are going to be controlled at.
let max_memory_bytes = round_usize_up_to_host_pages(max_memory_bytes)
.context("maximum size of memory is too large")?;

// If the user specifies a guard region, we always need to allocate a
// `PROT_NONE` region for it before any memory slots. Recall that we can
// avoid bounds checks for loads and stores with immediates up to
Expand Down Expand Up @@ -728,11 +733,7 @@ fn calculate(constraints: &SlabConstraints) -> Result<SlabLayout> {
};

// The page-aligned slot size; equivalent to `memory_and_guard_size`.
let page_alignment = crate::runtime::vm::host_page_size() - 1;
let slot_bytes = slot_bytes
.checked_add(page_alignment)
.and_then(|slot_bytes| Some(slot_bytes & !page_alignment))
.ok_or_else(|| anyhow!("slot size is too large"))?;
let slot_bytes = round_usize_up_to_host_pages(slot_bytes).context("slot size is too large")?;

// We may need another guard region (like `pre_slab_guard_bytes`) at the end
// of our slab to maintain our `faulting_region_bytes` guarantee. We could
Expand Down
24 changes: 24 additions & 0 deletions tests/all/pooling_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,3 +1327,27 @@ fn custom_page_sizes_reusing_same_slot() -> Result<()> {
}
Ok(())
}

#[test]
#[cfg_attr(miri, ignore)]
fn instantiate_non_page_aligned_sizes() -> Result<()> {
let mut config = Config::new();
config.wasm_custom_page_sizes(true);
let mut cfg = PoolingAllocationConfig::default();
cfg.total_memories(1);
cfg.max_memory_size(761927);
config.allocation_strategy(InstanceAllocationStrategy::Pooling(cfg));
let engine = Engine::new(&config)?;

let module = Module::new(
&engine,
r#"
(module
(memory 761927 761927 (pagesize 0x1))
)
"#,
)?;
let mut store = Store::new(&engine, ());
Instance::new(&mut store, &module, &[])?;
Ok(())
}

0 comments on commit 6ec4854

Please sign in to comment.