Path: blob/main/tests/all/custom_code_memory.rs
1690 views
#[cfg(all(not(target_os = "windows"), not(miri)))]1mod not_for_windows {2use rustix::mm::{MprotectFlags, mprotect};3use rustix::param::page_size;4use std::sync::Arc;5use wasmtime::*;67struct CustomCodePublish;8impl CustomCodeMemory for CustomCodePublish {9fn required_alignment(&self) -> usize {10page_size()11}1213fn publish_executable(&self, ptr: *const u8, len: usize) -> anyhow::Result<()> {14unsafe {15mprotect(16ptr as *mut _,17len,18MprotectFlags::READ | MprotectFlags::EXEC,19)?;20}21Ok(())22}2324fn unpublish_executable(&self, ptr: *const u8, len: usize) -> anyhow::Result<()> {25unsafe {26mprotect(27ptr as *mut _,28len,29MprotectFlags::READ | MprotectFlags::WRITE,30)?;31}32Ok(())33}34}3536#[test]37fn custom_code_publish() {38let mut config = Config::default();39config.with_custom_code_memory(Some(Arc::new(CustomCodePublish)));40let engine = Engine::new(&config).unwrap();41let module = Module::new(42&engine,43"(module (func (export \"main\") (result i32) i32.const 42))",44)45.unwrap();46let mut store = Store::new(&engine, ());47let instance = Instance::new(&mut store, &module, &[]).unwrap();48let func: TypedFunc<(), i32> = instance.get_typed_func(&mut store, "main").unwrap();49let result = func.call(&mut store, ()).unwrap();50assert_eq!(result, 42);51}52}535455