llm_test_bench_core/plugins/
runtime.rs1use anyhow::{Result, Context, bail};
10use wasmtime::*;
11use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, ResourceTable, WasiView};
12use std::sync::Arc;
13use parking_lot::Mutex;
14
15use crate::plugins::types::ResourceLimits;
16
17#[derive(Debug, Clone)]
19pub struct RuntimeConfig {
20 pub limits: ResourceLimits,
22
23 pub enable_wasi: bool,
25
26 pub enable_multi_memory: bool,
28
29 pub enable_bulk_memory: bool,
31
32 pub enable_reference_types: bool,
34}
35
36impl Default for RuntimeConfig {
37 fn default() -> Self {
38 Self {
39 limits: ResourceLimits::default(),
40 enable_wasi: true,
41 enable_multi_memory: false,
42 enable_bulk_memory: true,
43 enable_reference_types: true,
44 }
45 }
46}
47
48#[derive(Debug, Clone)]
50pub struct RuntimeLimits {
51 limits: ResourceLimits,
52}
53
54impl RuntimeLimits {
55 pub fn new(limits: ResourceLimits) -> Self {
56 Self { limits }
57 }
58
59 pub fn max_memory_bytes(&self) -> usize {
60 self.limits.max_memory_bytes
61 }
62
63 pub fn max_execution_time_ms(&self) -> u64 {
64 self.limits.max_execution_time_ms
65 }
66}
67
68pub struct WasmRuntime {
70 engine: Engine,
71 config: RuntimeConfig,
72}
73
74impl WasmRuntime {
75 pub fn new(config: RuntimeConfig) -> Result<Self> {
77 let mut engine_config = Config::new();
78
79 engine_config.wasm_bulk_memory(config.enable_bulk_memory);
81 engine_config.wasm_reference_types(config.enable_reference_types);
82 engine_config.wasm_multi_memory(config.enable_multi_memory);
83
84 engine_config.max_wasm_stack(2 * 1024 * 1024); engine_config.async_support(true);
89
90 let engine = Engine::new(&engine_config)
92 .context("Failed to create WASM engine")?;
93
94 Ok(Self { engine, config })
95 }
96
97 pub fn load_module(&self, wasm_bytes: &[u8]) -> Result<Module> {
99 Module::new(&self.engine, wasm_bytes)
100 .context("Failed to load WASM module")
101 }
102
103 pub async fn instantiate(&self, module: &Module) -> Result<WasmInstance> {
105 let mut linker = Linker::new(&self.engine);
106
107 let wasi_ctx = if self.config.enable_wasi {
109 Some(self.create_wasi_ctx()?)
110 } else {
111 None
112 };
113
114 let mut store = self.create_store(wasi_ctx)?;
116
117 if self.config.enable_wasi {
122 tracing::warn!("WASI support is temporarily disabled pending API migration to wasmtime 19.0");
123 }
126
127 let instance = linker
129 .instantiate_async(&mut store, module)
130 .await
131 .context("Failed to instantiate WASM module")?;
132
133 Ok(WasmInstance {
134 store: Arc::new(Mutex::new(store)),
135 instance,
136 config: self.config.clone(),
137 })
138 }
139
140 fn create_wasi_ctx(&self) -> Result<WasiCtx> {
142 let wasi = WasiCtxBuilder::new()
143 .inherit_stdio()
144 .build();
145 Ok(wasi)
146 }
147
148 fn create_store(&self, wasi_ctx: Option<WasiCtx>) -> Result<Store<StoreData>> {
150 let wasi = wasi_ctx.unwrap_or_else(|| {
151 WasiCtxBuilder::new().build()
152 });
153
154 let data = StoreData {
155 wasi_ctx: wasi,
156 table: ResourceTable::new(),
157 limits: StoreLimits::default(),
158 };
159
160 let mut store = Store::new(&self.engine, data);
161
162 let max_memory = self.config.limits.max_memory_bytes;
164 store.limiter(|data| &mut data.limits);
165 store.data_mut().limits.memory_size = max_memory;
166
167 Ok(store)
168 }
169
170 pub fn engine(&self) -> &Engine {
172 &self.engine
173 }
174}
175
176struct StoreData {
178 wasi_ctx: WasiCtx,
179 table: ResourceTable,
180 limits: StoreLimits,
181}
182
183impl WasiView for StoreData {
184 fn table(&mut self) -> &mut ResourceTable {
185 &mut self.table
186 }
187
188 fn ctx(&mut self) -> &mut WasiCtx {
189 &mut self.wasi_ctx
190 }
191}
192
193#[derive(Default)]
195struct StoreLimits {
196 memory_size: usize,
197}
198
199impl ResourceLimiter for StoreLimits {
200 fn memory_growing(&mut self, current: usize, desired: usize, _maximum: Option<usize>) -> Result<bool, Error> {
201 if desired > self.memory_size {
202 return Ok(false);
203 }
204 Ok(true)
205 }
206
207 fn table_growing(&mut self, _current: u32, desired: u32, _maximum: Option<u32>) -> Result<bool, Error> {
208 const MAX_TABLE_ELEMENTS: u32 = 10000;
209 Ok(desired < MAX_TABLE_ELEMENTS)
210 }
211}
212
213pub struct WasmInstance {
215 store: Arc<Mutex<Store<StoreData>>>,
216 instance: Instance,
217 config: RuntimeConfig,
218}
219
220impl WasmInstance {
221 pub async fn call_function(
223 &self,
224 name: &str,
225 args: &[Val],
226 ) -> Result<Vec<Val>> {
227 let func = self.instance
228 .get_func(&mut *self.store.lock(), name)
229 .ok_or_else(|| anyhow::anyhow!("Function '{}' not found", name))?;
230
231 let mut results = vec![Val::I32(0); func.ty(&*self.store.lock()).results().len()];
232
233 func.call_async(&mut *self.store.lock(), args, &mut results)
234 .await
235 .context(format!("Failed to call function '{}'", name))?;
236
237 Ok(results)
238 }
239
240 pub fn get_memory(&self, name: &str) -> Result<Memory> {
242 self.instance
243 .get_memory(&mut *self.store.lock(), name)
244 .ok_or_else(|| anyhow::anyhow!("Memory '{}' not found", name))
245 }
246
247 pub fn read_memory(&self, memory: &Memory, offset: usize, len: usize) -> Result<Vec<u8>> {
249 let store = self.store.lock();
250 let data = memory.data(&*store);
251
252 if offset + len > data.len() {
253 bail!("Memory read out of bounds");
254 }
255
256 Ok(data[offset..offset + len].to_vec())
257 }
258
259 pub fn write_memory(&self, memory: &Memory, offset: usize, data: &[u8]) -> Result<()> {
261 let mut store = self.store.lock();
262 let mem_data = memory.data_mut(&mut *store);
263
264 if offset + data.len() > mem_data.len() {
265 bail!("Memory write out of bounds");
266 }
267
268 mem_data[offset..offset + data.len()].copy_from_slice(data);
269 Ok(())
270 }
271
272 pub async fn allocate(&self, size: usize) -> Result<u32> {
274 let args = [Val::I32(size as i32)];
275 let results = self.call_function("plugin_alloc", &args).await?;
276
277 if let Some(Val::I32(ptr)) = results.first() {
278 Ok(*ptr as u32)
279 } else {
280 bail!("Invalid allocation result")
281 }
282 }
283
284 pub async fn free(&self, ptr: u32, size: usize) -> Result<()> {
286 let args = [Val::I32(ptr as i32), Val::I32(size as i32)];
287 self.call_function("plugin_free", &args).await?;
288 Ok(())
289 }
290
291 pub fn store(&self) -> Arc<Mutex<Store<StoreData>>> {
293 self.store.clone()
294 }
295
296 pub fn instance(&self) -> &Instance {
298 &self.instance
299 }
300}
301
302#[cfg(test)]
303mod tests {
304 use super::*;
305
306 #[test]
307 fn test_runtime_config() {
308 let config = RuntimeConfig::default();
309 assert!(config.enable_wasi);
310 assert!(config.enable_bulk_memory);
311 }
312
313 #[tokio::test]
314 async fn test_runtime_creation() {
315 let config = RuntimeConfig::default();
316 let runtime = WasmRuntime::new(config);
317 assert!(runtime.is_ok());
318 }
319
320 #[test]
321 fn test_runtime_limits() {
322 let limits = ResourceLimits {
323 max_memory_bytes: 128 * 1024 * 1024,
324 max_execution_time_ms: 60_000,
325 max_instructions: Some(2_000_000_000),
326 };
327
328 let runtime_limits = RuntimeLimits::new(limits);
329 assert_eq!(runtime_limits.max_memory_bytes(), 128 * 1024 * 1024);
330 assert_eq!(runtime_limits.max_execution_time_ms(), 60_000);
331 }
332}