Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 2 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
39 changes: 39 additions & 0 deletions client/executor/wasmtime/src/instance_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,33 @@ impl InstanceWrapper {
return;
}
}
} else if #[cfg(target_os = "macos")] {
use std::sync::Once;

unsafe {
let ptr = self.memory.data_ptr(&self.store);
let len = self.memory.data_size(&self.store);

// On MacOS we can simply overwrite memory mapping.
if libc::mmap(
ptr as _,
len,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_FIXED | libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
-1,
0,
) == libc::MAP_FAILED {
static LOGGED: Once = Once::new();
LOGGED.call_once(|| {
log::warn!(
"WASM instance memory mmap failed: {}",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to decommit WASM instance memory through mmap: {} or something along those likes would be better I think, to make it explicit that the main purpose of this is not allocating new memory, but just clearing it.

std::io::Error::last_os_error(),
);
});
} else {
return;
}
}
}
}

Expand All @@ -377,3 +404,15 @@ impl InstanceWrapper {
&mut self.store
}
}

#[test]
fn decommit_works() {
let engine = wasmtime::Engine::default();
let code = wat::parse_str("(module (memory (export \"memory\") 1 4))").unwrap();
let module = Module::new(&engine, code).unwrap();
let mut wrapper = InstanceWrapper::new::<()>(&module, 2, true, None).unwrap();
unsafe { *wrapper.memory.data_ptr(&wrapper.store) = 42 };
Comment thread
arkpar marked this conversation as resolved.
assert_eq!(unsafe { *wrapper.memory.data_ptr(&wrapper.store) }, 42);
wrapper.decommit();
assert_eq!(unsafe { *wrapper.memory.data_ptr(&wrapper.store) }, 0);
}