Skip to content

Commit

Permalink
mpk: add an example testing the memory limits (#7609)
Browse files Browse the repository at this point in the history
* mpk: allow checking for MPK without a config instance

It is inconvenient to have to construct a `PoolingAllocationConfig` in
order to check if memory protection keys are available. This removes
the unused `&self` restriction.

* mpk: improve logging of calculated slab layout

When double-checking the slab layout calculations it is quite convenient
to see the total slab size. This helps in correlating with mapped
regions.

* mpk: add an example testing the memory limits

This adds an example that can be run with `cargo run --example mpk`. Not
only does the example demonstrate how to build a pool-allocated engine
that uses MPK, it performs an exponential search to find the maximum
number of slots the system can support, with and without MPK.

* review: document Linux requirement

* review: `env_logger::init`

* review: replace `proc-maps` with manual parsing

* vet: audit `bytesize`

* fix: provide `main` for non-Linux systems

* fix: move `cfg` to avoid unused code
  • Loading branch information
abrown authored Dec 2, 2023
1 parent 03a14ac commit 57efd60
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 20 deletions.
43 changes: 25 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
walkdir = { workspace = true }
test-programs-artifacts = { workspace = true }
bytesize = "1.3.0"

[target.'cfg(windows)'.dev-dependencies]
windows-sys = { workspace = true, features = ["Win32_System_Memory"] }
Expand Down
5 changes: 4 additions & 1 deletion crates/runtime/src/instance/allocator/pooling/memory_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ impl MemoryPool {
// region to start--`PROT_NONE`.
let constraints = SlabConstraints::new(&config.limits, tunables, pkeys.len())?;
let layout = calculate(&constraints)?;
log::debug!("creating memory pool: {constraints:?} -> {layout:?}");
log::debug!(
"creating memory pool: {constraints:?} -> {layout:?} (total: {})",
layout.total_slab_bytes()?
);
let mut mapping = Mmap::accessible_reserved(0, layout.total_slab_bytes()?)
.context("failed to create memory pool mapping")?;

Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2409,7 +2409,7 @@ impl PoolingAllocationConfig {
/// same method that [`MpkEnabled::Auto`] does. See
/// [`PoolingAllocationConfig::memory_protection_keys`] for more
/// information.
pub fn are_memory_protection_keys_available(&self) -> bool {
pub fn are_memory_protection_keys_available() -> bool {
mpk::is_supported()
}
}
Expand Down
Loading

0 comments on commit 57efd60

Please sign in to comment.