use wasmtime::{
Config, Engine, InstanceAllocationStrategy, OptLevel, PoolingAllocationConfig, Strategy,
};
use crate::error::WasmtimeRuntimeError;
#[derive(Clone, Copy, Debug, Default)]
pub struct HostState;
#[derive(Clone, Debug)]
pub struct HotEngineConfig {
pub memory_max_pages: u32,
pub max_wasm_stack: usize,
pub pool_total_memories: u32,
pub pool_total_core_instances: u32,
pub plugin_failure_policy: crate::policy::PluginFailurePolicy,
pub shape_origin_policy: crate::policy::ShapeOriginPolicy,
pub wire_bounds: crate::policy::PluginWireBounds,
pub cookie_redaction: bool,
}
impl Default for HotEngineConfig {
fn default() -> Self {
Self {
memory_max_pages: 2048,
max_wasm_stack: 1024 * 1024,
pool_total_memories: 64,
pool_total_core_instances: 64,
plugin_failure_policy: crate::policy::PluginFailurePolicy::PassThrough,
shape_origin_policy: crate::policy::ShapeOriginPolicy::Unrestricted,
wire_bounds: crate::policy::PluginWireBounds::default(),
cookie_redaction: false,
}
}
}
pub fn build_hot_engine(cfg: &HotEngineConfig) -> Result<Engine, WasmtimeRuntimeError> {
let mut wcfg = Config::new();
wcfg.strategy(Strategy::Cranelift)
.cranelift_opt_level(OptLevel::Speed)
.wasm_component_model(false)
.wasm_threads(false)
.wasm_memory64(false)
.wasm_multi_memory(false)
.wasm_function_references(false)
.wasm_gc(false)
.wasm_exceptions(false)
.wasm_tail_call(false)
.wasm_relaxed_simd(false)
.signals_based_traps(true)
.memory_reservation(1u64 << 32)
.memory_guard_size(1u64 << 32)
.memory_init_cow(true)
.wasm_backtrace(false)
.coredump_on_trap(false)
.native_unwind_info(false)
.max_wasm_stack(cfg.max_wasm_stack);
let max_memory_size = (cfg.memory_max_pages as usize) << 16;
let mut pool = PoolingAllocationConfig::new();
pool.total_memories(cfg.pool_total_memories)
.total_core_instances(cfg.pool_total_core_instances)
.max_memory_size(max_memory_size);
wcfg.allocation_strategy(InstanceAllocationStrategy::Pooling(pool));
Engine::new(&wcfg).map_err(|e| WasmtimeRuntimeError::EngineInit(anyhow::Error::from(e)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_memory_max_pages_covers_files_body_cap_with_margin() {
let cfg = HotEngineConfig::default();
assert_eq!(
cfg.memory_max_pages, 2048,
"default must accommodate 100 MiB /v1/files body with rkyv envelope + scratch margin",
);
}
}