mielin-wasm 0.1.0-rc.1

WebAssembly sandboxing and execution runtime for agent cells using Wasmtime
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
595
596
597
598
599
600
//! Runtime abstraction layer for multiple WASM engines
//!
//! Provides a unified interface for different WASM runtime engines,
//! allowing seamless switching between Wasmtime, Wasmer, and others.

use anyhow::{anyhow, Result};
use std::sync::Arc;

/// WASM runtime engine type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuntimeEngine {
    /// Wasmtime engine (default)
    Wasmtime,
    /// Wasmer engine (future support)
    Wasmer,
    /// WASM3 interpreter (future support)
    Wasm3,
}

impl RuntimeEngine {
    /// Get engine name
    pub fn name(&self) -> &'static str {
        match self {
            RuntimeEngine::Wasmtime => "wasmtime",
            RuntimeEngine::Wasmer => "wasmer",
            RuntimeEngine::Wasm3 => "wasm3",
        }
    }

    /// Check if engine is available
    pub fn is_available(&self) -> bool {
        match self {
            RuntimeEngine::Wasmtime => true, // Always available
            RuntimeEngine::Wasmer => false,  // Not yet implemented
            RuntimeEngine::Wasm3 => false,   // Not yet implemented
        }
    }

    /// Get all available engines
    pub fn available_engines() -> Vec<RuntimeEngine> {
        vec![
            RuntimeEngine::Wasmtime,
            RuntimeEngine::Wasmer,
            RuntimeEngine::Wasm3,
        ]
        .into_iter()
        .filter(|e| e.is_available())
        .collect()
    }
}

/// Runtime configuration
#[derive(Debug, Clone)]
pub struct RuntimeConfig {
    /// Selected engine
    pub engine: RuntimeEngine,
    /// Maximum memory size (bytes)
    pub max_memory_bytes: usize,
    /// Enable JIT compilation
    pub enable_jit: bool,
    /// Enable AOT compilation
    pub enable_aot: bool,
    /// Enable SIMD support
    pub enable_simd: bool,
    /// Enable multi-threading
    pub enable_threads: bool,
    /// Enable bulk memory operations
    pub enable_bulk_memory: bool,
    /// Enable reference types
    pub enable_reference_types: bool,
}

impl Default for RuntimeConfig {
    fn default() -> Self {
        Self {
            engine: RuntimeEngine::Wasmtime,
            max_memory_bytes: 64 * 1024 * 1024, // 64 MB
            enable_jit: true,
            enable_aot: false,
            enable_simd: true,
            enable_threads: false,
            enable_bulk_memory: true,
            enable_reference_types: true,
        }
    }
}

impl RuntimeConfig {
    /// Create configuration for embedded systems
    pub fn embedded() -> Self {
        Self {
            engine: RuntimeEngine::Wasmtime,
            max_memory_bytes: 4 * 1024 * 1024, // 4 MB
            enable_jit: false,
            enable_aot: false,
            enable_simd: true, // Keep enabled for compatibility
            enable_threads: false,
            enable_bulk_memory: true,
            enable_reference_types: true, // Required by Wasmtime
        }
    }

    /// Create configuration for high performance
    pub fn performance() -> Self {
        Self {
            engine: RuntimeEngine::Wasmtime,
            max_memory_bytes: 512 * 1024 * 1024, // 512 MB
            enable_jit: true,
            enable_aot: true,
            enable_simd: true,
            enable_threads: true,
            enable_bulk_memory: true,
            enable_reference_types: true,
        }
    }

    /// Create configuration for security-focused environments
    pub fn secure() -> Self {
        Self {
            engine: RuntimeEngine::Wasmtime,
            max_memory_bytes: 16 * 1024 * 1024, // 16 MB
            enable_jit: false,
            enable_aot: false,
            enable_simd: true, // Keep enabled for compatibility
            enable_threads: false,
            enable_bulk_memory: true,
            enable_reference_types: true, // Required by Wasmtime
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        if !self.engine.is_available() {
            return Err(anyhow!(
                "Runtime engine '{}' is not available",
                self.engine.name()
            ));
        }

        if self.max_memory_bytes == 0 {
            return Err(anyhow!("max_memory_bytes must be greater than 0"));
        }

        if self.max_memory_bytes > 4 * 1024 * 1024 * 1024 {
            // 4 GB
            return Err(anyhow!("max_memory_bytes exceeds 4 GB limit"));
        }

        Ok(())
    }
}

/// Runtime statistics
#[derive(Debug, Clone, Default)]
pub struct RuntimeStats {
    /// Number of modules compiled
    pub modules_compiled: u64,
    /// Number of modules executed
    pub modules_executed: u64,
    /// Total compilation time (microseconds)
    pub total_compilation_time_us: u64,
    /// Total execution time (microseconds)
    pub total_execution_time_us: u64,
    /// Peak memory usage (bytes)
    pub peak_memory_bytes: usize,
    /// Current active instances
    pub active_instances: usize,
}

impl RuntimeStats {
    /// Calculate average compilation time
    pub fn avg_compilation_time_us(&self) -> f64 {
        if self.modules_compiled == 0 {
            0.0
        } else {
            self.total_compilation_time_us as f64 / self.modules_compiled as f64
        }
    }

    /// Calculate average execution time
    pub fn avg_execution_time_us(&self) -> f64 {
        if self.modules_executed == 0 {
            0.0
        } else {
            self.total_execution_time_us as f64 / self.modules_executed as f64
        }
    }

    /// Get peak memory in MB
    pub fn peak_memory_mb(&self) -> f64 {
        self.peak_memory_bytes as f64 / (1024.0 * 1024.0)
    }
}

/// Runtime capabilities
#[derive(Debug, Clone, Default)]
pub struct RuntimeCapabilities {
    /// Supports JIT compilation
    pub supports_jit: bool,
    /// Supports AOT compilation
    pub supports_aot: bool,
    /// Supports SIMD instructions
    pub supports_simd: bool,
    /// Supports multi-threading
    pub supports_threads: bool,
    /// Supports bulk memory operations
    pub supports_bulk_memory: bool,
    /// Supports reference types
    pub supports_reference_types: bool,
    /// Supports component model
    pub supports_component_model: bool,
    /// Maximum memory size (bytes)
    pub max_memory_bytes: usize,
}

impl RuntimeCapabilities {
    /// Get capabilities for Wasmtime
    pub fn wasmtime() -> Self {
        Self {
            supports_jit: true,
            supports_aot: true,
            supports_simd: true,
            supports_threads: true,
            supports_bulk_memory: true,
            supports_reference_types: true,
            supports_component_model: true,
            max_memory_bytes: 4 * 1024 * 1024 * 1024, // 4 GB
        }
    }

    /// Get capabilities for Wasmer (future)
    pub fn wasmer() -> Self {
        Self {
            supports_jit: true,
            supports_aot: true,
            supports_simd: true,
            supports_threads: true,
            supports_bulk_memory: true,
            supports_reference_types: true,
            supports_component_model: false,
            max_memory_bytes: 4 * 1024 * 1024 * 1024, // 4 GB
        }
    }

    /// Get capabilities for WASM3 (future)
    pub fn wasm3() -> Self {
        Self {
            supports_jit: false,
            supports_aot: false,
            supports_simd: false,
            supports_threads: false,
            supports_bulk_memory: true,
            supports_reference_types: false,
            supports_component_model: false,
            max_memory_bytes: 16 * 1024 * 1024, // 16 MB
        }
    }

    /// Get capabilities for the given engine
    pub fn for_engine(engine: RuntimeEngine) -> Self {
        match engine {
            RuntimeEngine::Wasmtime => Self::wasmtime(),
            RuntimeEngine::Wasmer => Self::wasmer(),
            RuntimeEngine::Wasm3 => Self::wasm3(),
        }
    }

    /// Check if configuration is compatible
    pub fn is_compatible(&self, config: &RuntimeConfig) -> bool {
        if config.enable_jit && !self.supports_jit {
            return false;
        }
        if config.enable_aot && !self.supports_aot {
            return false;
        }
        if config.enable_simd && !self.supports_simd {
            return false;
        }
        if config.enable_threads && !self.supports_threads {
            return false;
        }
        if config.enable_bulk_memory && !self.supports_bulk_memory {
            return false;
        }
        if config.enable_reference_types && !self.supports_reference_types {
            return false;
        }
        if config.max_memory_bytes > self.max_memory_bytes {
            return false;
        }
        true
    }
}

/// Runtime abstraction trait
pub trait Runtime: Send + Sync {
    /// Get runtime engine type
    fn engine(&self) -> RuntimeEngine;

    /// Get runtime capabilities
    fn capabilities(&self) -> RuntimeCapabilities;

    /// Get runtime statistics
    fn stats(&self) -> RuntimeStats;

    /// Compile a WASM module
    fn compile(&self, wasm_bytes: &[u8]) -> Result<Arc<dyn Module>>;

    /// Validate a WASM module
    fn validate(&self, wasm_bytes: &[u8]) -> Result<()>;
}

/// Module abstraction trait
pub trait Module: Send + Sync {
    /// Get module size in bytes
    fn size_bytes(&self) -> usize;

    /// Get module hash
    fn hash(&self) -> u64;

    /// Serialize module for caching
    fn serialize(&self) -> Result<Vec<u8>>;
}

/// Runtime factory
pub struct RuntimeFactory;

impl RuntimeFactory {
    /// Create a runtime with the given configuration
    pub fn create(config: RuntimeConfig) -> Result<Arc<dyn Runtime>> {
        config.validate()?;

        let caps = RuntimeCapabilities::for_engine(config.engine);
        if !caps.is_compatible(&config) {
            return Err(anyhow!(
                "Configuration is incompatible with {} engine capabilities",
                config.engine.name()
            ));
        }

        match config.engine {
            RuntimeEngine::Wasmtime => Ok(Arc::new(WasmtimeRuntime::new(config)?)),
            RuntimeEngine::Wasmer => Err(anyhow!("Wasmer support not yet implemented")),
            RuntimeEngine::Wasm3 => Err(anyhow!("WASM3 support not yet implemented")),
        }
    }

    /// Create a runtime with default configuration
    pub fn default_runtime() -> Result<Arc<dyn Runtime>> {
        Self::create(RuntimeConfig::default())
    }

    /// Get list of available engines
    pub fn available_engines() -> Vec<RuntimeEngine> {
        RuntimeEngine::available_engines()
    }
}

/// Wasmtime-specific runtime implementation
struct WasmtimeRuntime {
    _config: RuntimeConfig,
    engine: wasmtime::Engine,
}

impl WasmtimeRuntime {
    fn new(config: RuntimeConfig) -> Result<Self> {
        let mut wasmtime_config = wasmtime::Config::new();

        // Configure based on RuntimeConfig
        if config.enable_jit {
            wasmtime_config.strategy(wasmtime::Strategy::Cranelift);
        }

        wasmtime_config.wasm_simd(config.enable_simd);
        wasmtime_config.wasm_threads(config.enable_threads);
        wasmtime_config.wasm_bulk_memory(config.enable_bulk_memory);
        wasmtime_config.wasm_reference_types(config.enable_reference_types);

        let engine = wasmtime::Engine::new(&wasmtime_config)?;

        Ok(Self {
            _config: config,
            engine,
        })
    }
}

impl Runtime for WasmtimeRuntime {
    fn engine(&self) -> RuntimeEngine {
        RuntimeEngine::Wasmtime
    }

    fn capabilities(&self) -> RuntimeCapabilities {
        RuntimeCapabilities::wasmtime()
    }

    fn stats(&self) -> RuntimeStats {
        // For now, return default stats
        // In a full implementation, we would track these
        RuntimeStats::default()
    }

    fn compile(&self, wasm_bytes: &[u8]) -> Result<Arc<dyn Module>> {
        let module = wasmtime::Module::new(&self.engine, wasm_bytes)?;
        Ok(Arc::new(WasmtimeModule { module }))
    }

    fn validate(&self, wasm_bytes: &[u8]) -> Result<()> {
        wasmtime::Module::validate(&self.engine, wasm_bytes)?;
        Ok(())
    }
}

/// Wasmtime-specific module implementation
struct WasmtimeModule {
    module: wasmtime::Module,
}

impl Module for WasmtimeModule {
    fn size_bytes(&self) -> usize {
        // Approximate size - in a full implementation we would track this
        0
    }

    fn hash(&self) -> u64 {
        // Compute a hash - for now return 0
        0
    }

    fn serialize(&self) -> Result<Vec<u8>> {
        self.module.serialize().map_err(|e| anyhow!(e))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_runtime_engine_name() {
        assert_eq!(RuntimeEngine::Wasmtime.name(), "wasmtime");
        assert_eq!(RuntimeEngine::Wasmer.name(), "wasmer");
        assert_eq!(RuntimeEngine::Wasm3.name(), "wasm3");
    }

    #[test]
    fn test_runtime_engine_available() {
        assert!(RuntimeEngine::Wasmtime.is_available());
        assert!(!RuntimeEngine::Wasmer.is_available());
        assert!(!RuntimeEngine::Wasm3.is_available());
    }

    #[test]
    fn test_available_engines() {
        let engines = RuntimeEngine::available_engines();
        assert_eq!(engines.len(), 1);
        assert_eq!(engines[0], RuntimeEngine::Wasmtime);
    }

    #[test]
    fn test_runtime_config_default() {
        let config = RuntimeConfig::default();
        assert_eq!(config.engine, RuntimeEngine::Wasmtime);
        assert!(config.enable_jit);
        assert!(!config.enable_aot);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_runtime_config_embedded() {
        let config = RuntimeConfig::embedded();
        assert_eq!(config.max_memory_bytes, 4 * 1024 * 1024);
        assert!(!config.enable_jit);
        assert!(config.enable_simd); // SIMD must be enabled for Wasmtime compatibility
        assert!(config.enable_reference_types); // Required by Wasmtime
    }

    #[test]
    fn test_runtime_config_performance() {
        let config = RuntimeConfig::performance();
        assert!(config.enable_jit);
        assert!(config.enable_aot);
        assert!(config.enable_simd);
        assert!(config.enable_threads);
    }

    #[test]
    fn test_runtime_config_secure() {
        let config = RuntimeConfig::secure();
        assert!(!config.enable_jit);
        assert!(!config.enable_threads);
        assert_eq!(config.max_memory_bytes, 16 * 1024 * 1024);
    }

    #[test]
    fn test_runtime_config_validation() {
        let mut config = RuntimeConfig::default();
        assert!(config.validate().is_ok());

        config.max_memory_bytes = 0;
        assert!(config.validate().is_err());

        config.max_memory_bytes = 5 * 1024 * 1024 * 1024; // > 4 GB
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_runtime_capabilities_wasmtime() {
        let caps = RuntimeCapabilities::wasmtime();
        assert!(caps.supports_jit);
        assert!(caps.supports_aot);
        assert!(caps.supports_simd);
        assert!(caps.supports_component_model);
    }

    #[test]
    fn test_runtime_capabilities_compatibility() {
        let caps = RuntimeCapabilities::wasmtime();
        let config = RuntimeConfig::default();
        assert!(caps.is_compatible(&config));

        let incompatible_config = RuntimeConfig {
            max_memory_bytes: 5 * 1024 * 1024 * 1024, // > 4 GB
            ..Default::default()
        };
        assert!(!caps.is_compatible(&incompatible_config));
    }

    #[test]
    fn test_runtime_stats() {
        let mut stats = RuntimeStats::default();
        assert_eq!(stats.avg_compilation_time_us(), 0.0);

        stats.modules_compiled = 10;
        stats.total_compilation_time_us = 1000;
        assert_eq!(stats.avg_compilation_time_us(), 100.0);

        stats.peak_memory_bytes = 10 * 1024 * 1024;
        assert_eq!(stats.peak_memory_mb(), 10.0);
    }

    #[test]
    fn test_runtime_factory_create() {
        let config = RuntimeConfig::default();
        let runtime = RuntimeFactory::create(config);
        assert!(runtime.is_ok());

        let runtime = runtime.expect("Failed to create runtime");
        assert_eq!(runtime.engine(), RuntimeEngine::Wasmtime);
    }

    #[test]
    fn test_runtime_factory_default() {
        let runtime = RuntimeFactory::default_runtime();
        assert!(runtime.is_ok());
    }

    #[test]
    fn test_runtime_factory_unavailable_engine() {
        let config = RuntimeConfig {
            engine: RuntimeEngine::Wasmer,
            ..Default::default()
        };
        let runtime = RuntimeFactory::create(config);
        assert!(runtime.is_err());
    }

    #[test]
    fn test_wasmtime_runtime_validate() {
        let runtime = RuntimeFactory::default_runtime().expect("Failed to create runtime");

        // Valid WASM module
        let wasm = wat::parse_str("(module)").expect("Failed to parse WAT");
        assert!(runtime.validate(&wasm).is_ok());

        // Invalid WASM
        let invalid = b"not wasm";
        assert!(runtime.validate(invalid).is_err());
    }

    #[test]
    fn test_wasmtime_runtime_compile() {
        let runtime = RuntimeFactory::default_runtime().expect("Failed to create runtime");

        let wasm = wat::parse_str("(module)").expect("Failed to parse WAT");
        let module = runtime.compile(&wasm);
        assert!(module.is_ok());
    }

    #[test]
    fn test_wasmtime_module_serialize() {
        let runtime = RuntimeFactory::default_runtime().expect("Failed to create runtime");
        let wasm = wat::parse_str("(module)").expect("Failed to parse WAT");
        let module = runtime.compile(&wasm).expect("Failed to compile");

        let serialized = module.serialize();
        assert!(serialized.is_ok());
        assert!(!serialized.expect("Serialization failed").is_empty());
    }
}