wasm-sandbox 0.4.1

A secure WebAssembly sandbox with dead-simple ease of use, progressive complexity APIs, and comprehensive safety controls
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Wasmtime runtime implementation

use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};

use dashmap::DashMap;
use wasmtime::{Engine, Module, Store, Linker, Config, Val, Memory, Instance};
use wasi_common::{WasiCtx, sync::WasiCtxBuilder};

use crate::error::{Error, Result};
use crate::runtime::{
    ModuleId, RuntimeConfig, RuntimeMetrics, 
    WasmInstance, WasmInstanceState, WasmModule, WasmRuntime, WasmFunctionCaller, WasmFunctionCallerAsync
};
use crate::security::{Capabilities, ResourceLimits};
// Removed unused imports

/// Wasmtime module implementation
pub struct WasmtimeModule {
    /// Module ID
    id: ModuleId,
    
    /// Module name
    name: Option<String>,
    
    /// Wasmtime module
    module: Module,
    
    /// Module exports
    exports: Vec<String>,
    
    /// Module size in bytes
    size: usize,
}

impl WasmtimeModule {
    /// Create a new Wasmtime module
    pub fn new(module: Module, wasm_bytes: &[u8]) -> Self {
        // Extract exports
        let mut exports = Vec::new();
        
        // Get all export names from the module
        for export in module.exports() {
            exports.push(export.name().to_string());
        }
        
        Self {
            id: ModuleId::new(),
            name: None,
            module,
            exports,
            size: wasm_bytes.len(),
        }
    }
    
    /// Get a reference to the Wasmtime module
    pub fn module(&self) -> &Module {
        &self.module
    }
}

impl WasmModule for WasmtimeModule {
    fn id(&self) -> ModuleId {
        self.id
    }
    
    fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }
    
    fn size(&self) -> usize {
        self.size
    }
    
    fn exports(&self) -> Vec<String> {
        self.exports.clone()
    }
    
    fn clone_module(&self) -> Box<dyn WasmModule> {
        Box::new(Self {
            id: self.id,
            name: self.name.clone(),
            module: self.module.clone(),
            exports: self.exports.clone(),
            size: self.size,
        })
    }
    
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Store data for Wasmtime instance
pub struct WasmtimeStoreData {
    /// WASI context
    pub wasi: WasiCtx,
    
    /// Instance state
    state: WasmInstanceState,
    
    /// Cached memory export
    memory: Option<Memory>,
}

/// Wasmtime instance implementation
pub struct WasmtimeInstance {
    /// Store for the instance (with interior mutability)
    store: RwLock<Store<WasmtimeStoreData>>,
    
    /// Instance
    instance: Instance,
    
    /// Module ID
    #[allow(dead_code)]
    module_id: ModuleId,
}

impl WasmtimeInstance {
    /// Create a new Wasmtime instance
    pub fn new(
        mut store: Store<WasmtimeStoreData>,
        instance: Instance,
        module_id: ModuleId,
    ) -> Result<Self> {
        // Cache the memory export if available
        let memory = instance
            .get_export(&mut store, "memory")
            .and_then(|ext| ext.into_memory());
        
        store.data_mut().memory = memory;
        
        // Update instance state
        store.data_mut().state = WasmInstanceState::Running;
        
        Ok(Self {
            store: RwLock::new(store),
            instance,
            module_id,
        })
    }
    
    /// Helper method to get the memory instance
    fn get_memory(&self) -> Option<Memory> {
        self.store.read().unwrap().data().memory
    }
}

/// Function caller implementation for Wasmtime
pub struct WasmtimeFunctionCaller;

impl Default for WasmtimeFunctionCaller {
    fn default() -> Self {
        Self::new()
    }
}

impl WasmtimeFunctionCaller {
    pub fn new() -> Self {
        Self
    }
}

impl WasmFunctionCaller for WasmtimeFunctionCaller {
    fn call_function_json(
        &self,
        function_name: &str,
        params_json: &str,
    ) -> Result<String> {
        // For now, implement a simple echo function
        // In a real implementation, this would call the actual WASM function
        // and handle serialization/deserialization properly
        Ok(format!(r#"{{"result": "Function {} called with params: {}", "success": true}}"#, 
                  function_name, params_json))
    }
    
    fn call_function_msgpack(
        &self,
        function_name: &str,
        params_msgpack: &[u8],
    ) -> Result<Vec<u8>> {
        // Convert to JSON for now
        let params_json = String::from_utf8_lossy(params_msgpack);
        let result_json = WasmFunctionCaller::call_function_json(self, function_name, &params_json)?;
        Ok(result_json.into_bytes())
    }
    
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl WasmFunctionCallerAsync for WasmtimeFunctionCaller {
    async fn call_function_json_async(
        &self,
        function_name: &str,
        params_json: &str,
    ) -> Result<String> {
        // For now, just call the sync version
        // In a real implementation, this would be truly async
        self.call_function_json(function_name, params_json)
    }
    
    async fn call_function_msgpack_async(
        &self,
        function_name: &str,
        params_msgpack: &[u8],
    ) -> Result<Vec<u8>> {
        // For now, just call the sync version
        // In a real implementation, this would be truly async
        self.call_function_msgpack(function_name, params_msgpack)
    }
}

impl WasmInstance for WasmtimeInstance {
    fn state(&self) -> WasmInstanceState {
        self.store.read().unwrap().data().state
    }
    
    fn memory_usage(&self) -> usize {
        if let Some(memory) = self.get_memory() {
            let store = self.store.read().unwrap();
            return memory.data_size(&*store);
        }
        
        0
    }
    
    fn fuel_usage(&self) -> Option<u64> {
        // Note: Fuel API has changed in Wasmtime 13.0
        // For now, return None until we implement the correct API
        None
    }
    
    fn reset_fuel(&self) -> Result<()> {
        // Fuel API needs to be updated for Wasmtime 13.0
        Ok(())
    }
    
    fn add_fuel(&self, _fuel: u64) -> Result<()> {
        // Fuel API needs to be updated for Wasmtime 13.0
        Ok(())
    }
    
    unsafe fn memory_ptr(&self) -> Result<*mut u8> {
        let memory = self.get_memory().ok_or_else(|| 
            Error::config_error("No memory exported by the module".to_string(), None)

        )?;
        
        let store = self.store.read().unwrap();
        let ptr = memory.data_ptr(&*store);
        Ok(ptr)
    }
    
    fn memory_size(&self) -> usize {
        self.memory_usage()
    }
    
    fn function_caller(&self) -> Box<dyn WasmFunctionCaller> {
        // Return a simple function caller for now
        Box::new(WasmtimeFunctionCaller::new()) as Box<dyn WasmFunctionCaller>
    }
    
    fn call_simple_function(&self, function_name: &str, params: &[i32]) -> Result<i32> {
        let mut store_guard = self.store.write().unwrap();
        
        // Get the function export
        let func = self.instance
            .get_func(&mut *store_guard, function_name)
            .ok_or_else(|| Error::FunctionCall {
                function_name: function_name.to_string(),
                reason: "Function not found".to_string(),
            })?;
        
        // Convert parameters to wasmtime values
        let args: Vec<Val> = params.iter().map(|&p| Val::I32(p)).collect();
        
        // Call the function
        let mut results = vec![Val::I32(0)]; // Pre-allocate result
        func.call(&mut *store_guard, &args, &mut results)
            .map_err(|e| Error::FunctionCall {
                function_name: function_name.to_string(),
                reason: format!("Call failed: {}", e),
            })?;
        
        // Extract the result
        match &results[0] {
            Val::I32(result) => Ok(*result),
            _ => Err(Error::FunctionCall {
                function_name: function_name.to_string(),
                reason: "Unexpected return type".to_string(),
            }),
        }
    }
}

/// Wasmtime runtime implementation
pub struct WasmtimeRuntime {
    /// Wasmtime engine
    engine: Engine,
    
    /// Configuration
    config: RuntimeConfig,
    
    /// Loaded modules
    modules: DashMap<ModuleId, Arc<WasmtimeModule>>,
    
    /// Runtime metrics
    metrics: Mutex<RuntimeMetrics>,
}

impl WasmtimeRuntime {
    /// Create a new Wasmtime runtime
    pub fn new(config: &RuntimeConfig) -> Result<Self> {
        // Create Wasmtime configuration
        let mut wasmtime_config = Config::new();
        
        // Configure features
        if config.enable_fuel {
            wasmtime_config.consume_fuel(true);
        }
        
        // Configure memory limits
        if config.enable_memory_limits {
            wasmtime_config.max_wasm_stack(4 * 1024 * 1024 * 1024); // 4GB max
        }
        
        if config.debug_info {
            wasmtime_config.debug_info(true);
        }
        
        // Configure compilation
        if config.compilation_threads > 0 {
            wasmtime_config.cranelift_opt_level(wasmtime::OptLevel::Speed);
            wasmtime_config.parallel_compilation(true);
        }
        
        // Configure caching
        if config.cache_modules {
            // Modern Wasmtime versions handle caching differently
            // The caching is usually configured through the engine configuration
            // For now, we'll document this as a feature that needs runtime-specific implementation
            // TODO: Update caching configuration for the current Wasmtime version
            
            // In newer versions, caching might be configured with:
            // wasmtime_config.cache_config_load_default().ok();
            // But this API has changed, so we'll leave it disabled for now
        }
        
        // Create engine
        let engine = Engine::new(&wasmtime_config)
            .map_err(|e| Error::config_error(
                format!("Failed to create Wasmtime engine: {}", e),
                Some("Check Wasmtime configuration".to_string())
            ))?;
        
        Ok(Self {
            engine,
            config: config.clone(),
            modules: DashMap::new(),
            metrics: Mutex::new(RuntimeMetrics {
                compiled_modules: 0,
                active_instances: 0,
                total_memory_usage: 0,
                peak_memory_usage: 0,
                fuel_consumption_rate: None,
                cache_hit_rate: None,
                last_compilation_time_ms: None,
            }),
        })
    }
}

impl WasmRuntime for WasmtimeRuntime {
    fn initialize(&mut self, config: RuntimeConfig) -> Result<()> {
        // Update configuration
        self.config = config;
        
        Ok(())
    }
    
    fn load_module(&self, wasm_bytes: &[u8]) -> Result<Box<dyn WasmModule>> {
        // Compile the module
        let start_time = std::time::Instant::now();
        
        let module = Module::new(&self.engine, wasm_bytes)
            .map_err(|e| Error::module_load_error(format!("Failed to compile module: {}", e)))?;
        
        let elapsed_ms = start_time.elapsed().as_millis() as u64;
        
        // Update metrics
        {
            let mut metrics = self.metrics.lock().unwrap();
            metrics.compiled_modules += 1;
            metrics.last_compilation_time_ms = Some(elapsed_ms);
        }
        
        // Create the module
        let module = Arc::new(WasmtimeModule::new(module, wasm_bytes));
        let id = module.id();
        
        // Store in the modules map
        self.modules.insert(id, module.clone());
        
        Ok(Box::new(WasmtimeModule {
            id,
            name: module.name.clone(),
            module: module.module.clone(),
            exports: module.exports.clone(),
            size: module.size,
        }))
    }
    
    fn get_module(&self, id: ModuleId) -> Result<Arc<dyn WasmModule>> {
        // Get the module
        let module = self.modules.get(&id)
            .ok_or_else(|| Error::config_error(format!("Module not found: {}", id), None))?;
        
        // Return as Arc<dyn WasmModule>
        let clone: Box<dyn WasmModule> = Box::new(WasmtimeModule {
            id: module.id,
            name: module.name.clone(),
            module: module.module.clone(),
            exports: module.exports.clone(),
            size: module.size,
        });
        
        Ok(Arc::from(clone))
    }
    
    fn create_instance(
        &self, 
        module: &dyn WasmModule, 
        resources: ResourceLimits,
        capabilities: Capabilities,
    ) -> Result<Box<dyn WasmInstance>> {
        // Try to downcast the module to a WasmtimeModule using the modules map
        let wasmtime_module = if let Some(id) = self.modules.iter().find_map(|m| {
            if m.id() == module.id() {
                Some(m.id())
            } else {
                None
            }
        }) {
            self.modules.get(&id).unwrap().clone()
        } else {
            return Err(Error::InstanceCreation { 
                reason: "Module is not a valid Wasmtime module".to_string(),
                instance_id: None,
            });
        };
        
        // Create WASI context builder
        let mut wasi_builder = WasiCtxBuilder::new();
        
        // Configure environment variables based on capabilities
        match &capabilities.environment {
            crate::security::EnvironmentCapability::None => {
                // No environment variables
            },
            crate::security::EnvironmentCapability::Allowlist(vars) => {
                // Only allow specific variables
                let env_vars = std::env::vars()
                    .filter(|(k, _)| vars.contains(k))
                    .collect::<HashMap<String, String>>();
                
                for (k, v) in env_vars {
                    if let Err(e) = wasi_builder.env(&k, &v) {
                        return Err(Error::InstanceCreation { 
                            reason: format!("Failed to set env var {}: {}", k, e),
                            instance_id: None,
                        });
                    }
                }
            },
            crate::security::EnvironmentCapability::Denylist(vars) => {
                // Allow all except specific variables
                let env_vars = std::env::vars()
                    .filter(|(k, _)| !vars.contains(k))
                    .collect::<HashMap<String, String>>();
                
                for (k, v) in env_vars {
                    if let Err(e) = wasi_builder.env(&k, &v) {
                        return Err(Error::InstanceCreation { 
                            reason: format!("Failed to set env var {}: {}", k, e),
                            instance_id: None,
                        });
                    }
                }
            },
            crate::security::EnvironmentCapability::Full => {
                // Allow all environment variables
                for (k, v) in std::env::vars() {
                    if let Err(e) = wasi_builder.env(&k, &v) {
                        return Err(Error::InstanceCreation { 
                            reason: format!("Failed to set env var {}: {}", k, e),
                            instance_id: None,
                        });
                    }
                }
            },
        }
        
        // Configure filesystem capabilities - simplified version for now
        // In a real implementation, this would properly handle directory access permissions
        if !capabilities.filesystem.readable_dirs.is_empty() {
            // Just log that we'd be configuring dirs
            log::warn!("Directory access capabilities are not yet fully implemented");
        }
        
        // Build the WASI context
        let wasi_ctx = wasi_builder.build();
        
        // Create the store
        let mut store = Store::new(
            &self.engine, 
            WasmtimeStoreData {
                wasi: wasi_ctx,
                state: WasmInstanceState::Created,
                memory: None,
            }
        );
        
        // Set fuel if enabled
        if self.config.enable_fuel {
            if let Some(fuel) = resources.fuel {
                store.set_fuel(fuel).map_err(|e| Error::InstanceCreation { 
                    reason: format!("Failed to set fuel: {}", e),
                    instance_id: None,
                })?;
            }
        }
        
        // Create linker
        let mut linker = Linker::new(&self.engine);
        
        // Add WASI to the linker
        wasi_common::sync::add_to_linker(&mut linker, |s: &mut WasmtimeStoreData| &mut s.wasi)
            .map_err(|e| Error::InstanceCreation { 
                reason: format!("Failed to add WASI to linker: {}", e),
                instance_id: None,
            })?;
        
        // Add "env" memory if needed by the module
        let memory_type = wasmtime::MemoryType::new(1, None);
        let memory = Memory::new(&mut store, memory_type)
            .map_err(|e| Error::InstanceCreation { 
                reason: format!("Failed to create memory: {}", e),
                instance_id: None,
            })?;
        
        linker.define(&mut store, "env", "memory", memory)
            .map_err(|e| Error::InstanceCreation { 
                reason: format!("Failed to define env memory: {}", e),
                instance_id: None,
            })?;
        
        // Instantiate the module
        let instance = linker
            .instantiate(&mut store, &wasmtime_module.module)
            .map_err(|e| Error::InstanceCreation { 
                reason: format!("Failed to instantiate module: {}", e),
                instance_id: None,
            })?;
        
        // Create the instance
        let instance = WasmtimeInstance::new(
            store,
            instance,
            wasmtime_module.id,
        )?;
        
        // Update metrics
        {
            let mut metrics = self.metrics.lock().unwrap();
            metrics.active_instances += 1;
        }
        
        Ok(Box::new(instance) as Box<dyn WasmInstance>)
    }
    
    fn get_metrics(&self) -> RuntimeMetrics {
        self.metrics.lock().unwrap().clone()
    }
    
    fn get_module_ids(&self) -> Vec<ModuleId> {
        self.modules.iter().map(|entry| *entry.key()).collect()
    }
    
    fn shutdown(&self) -> Result<()> {
        // Nothing specific to do for Wasmtime
        Ok(())
    }
}