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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Resource limits implementation for the sandbox

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use crate::error::{Error, Result};
use crate::security::{
    MemoryLimits, CpuLimits, IoLimits, TimeLimits, ResourceLimits
};

/// Memory resource tracker
#[derive(Debug, Clone)]
pub struct MemoryResourceTracker {
    /// Maximum memory pages
    pub max_memory_pages: u32,
    
    /// Current memory pages
    current_pages: Arc<AtomicU64>,
    
    /// Peak memory pages
    peak_pages: Arc<AtomicU64>,
    
    /// Memory growth rate tracking
    growth_tracker: Arc<Mutex<MemoryGrowthTracker>>,
}

/// Tracking for memory growth rate
#[derive(Debug)]
struct MemoryGrowthTracker {
    /// Maximum growth rate
    max_rate: Option<u32>,
    
    /// Last memory size
    last_size: u64,
    
    /// Last check time
    last_check: Instant,
    
    /// Growth events in current window
    growth_events: Vec<(Instant, u64)>,
    
    /// Window duration
    window: Duration,
}

impl MemoryResourceTracker {
    /// Create a new memory resource tracker
    pub fn new(limits: &MemoryLimits) -> Self {
        let growth_tracker = MemoryGrowthTracker {
            max_rate: limits.max_growth_rate,
            last_size: 0,
            last_check: Instant::now(),
            growth_events: Vec::new(),
            window: Duration::from_secs(1), // 1 second window for growth rate
        };
        
        Self {
            max_memory_pages: limits.max_memory_pages,
            current_pages: Arc::new(AtomicU64::new(limits.reserved_memory_pages as u64)),
            peak_pages: Arc::new(AtomicU64::new(limits.reserved_memory_pages as u64)),
            growth_tracker: Arc::new(Mutex::new(growth_tracker)),
        }
    }
    
    /// Check if memory allocation is allowed
    pub fn check_allocation(&self, pages: u32) -> Result<()> {
        let current = self.current_pages.load(Ordering::Acquire);
        let requested = current + pages as u64;
        
        if requested > self.max_memory_pages as u64 {
            return Err(Error::ResourceLimit {
                message: format!("Memory allocation of {} pages would exceed limit of {} pages", 
                    pages, self.max_memory_pages)
            });
        }
        
        // Check growth rate
        if let Some(max_rate) = self.growth_tracker.lock().unwrap().max_rate {
            let now = Instant::now();
            let mut tracker = self.growth_tracker.lock().unwrap();
            
            // Clean up old events
            let cutoff = now - tracker.window;
            tracker.growth_events.retain(|(time, _)| *time >= cutoff);
            
            // Add current growth
            tracker.growth_events.push((now, pages as u64));
            
            // Calculate total growth in window
            let total_growth: u64 = tracker.growth_events.iter().map(|(_, size)| *size).sum();
            
            if total_growth > max_rate as u64 {
                return Err(Error::ResourceLimit {
                    message: format!("Memory growth rate of {} pages/s exceeds limit of {} pages/s",
                        total_growth, max_rate)
                });
            }
            
            tracker.last_size = requested;
            tracker.last_check = now;
        }
        
        Ok(())
    }
    
    /// Update memory usage
    pub fn update(&self, pages: u32) {
        let current = self.current_pages.fetch_add(pages as u64, Ordering::AcqRel) + pages as u64;
        let mut peak = self.peak_pages.load(Ordering::Acquire);
        
        while current > peak {
            match self.peak_pages.compare_exchange_weak(
                peak,
                current,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => break,
                Err(actual) => peak = actual,
            }
        }
    }
    
    /// Get current memory usage in pages
    pub fn current_pages(&self) -> u64 {
        self.current_pages.load(Ordering::Acquire)
    }
    
    /// Get peak memory usage in pages
    pub fn peak_pages(&self) -> u64 {
        self.peak_pages.load(Ordering::Acquire)
    }
    
    /// Reset peak memory usage
    pub fn reset_peak(&self) {
        self.peak_pages.store(
            self.current_pages.load(Ordering::Acquire),
            Ordering::Release
        );
    }
}

/// CPU resource tracker
#[derive(Debug, Clone)]
pub struct CpuResourceTracker {
    /// Maximum execution time
    pub max_execution_time: Duration,
    
    /// Target CPU usage percentage
    pub cpu_usage_percentage: Option<u8>,
    
    /// Maximum number of threads
    pub max_threads: Option<u32>,
    
    /// Execution start time
    start_time: Arc<Mutex<Option<Instant>>>,
    
    /// Total execution time
    total_time: Arc<AtomicU64>,
    
    /// Number of active threads
    active_threads: Arc<AtomicU64>,
}

impl CpuResourceTracker {
    /// Create a new CPU resource tracker
    pub fn new(limits: &CpuLimits) -> Self {
        Self {
            max_execution_time: Duration::from_millis(limits.max_execution_time_ms),
            cpu_usage_percentage: limits.cpu_usage_percentage,
            max_threads: limits.max_threads,
            start_time: Arc::new(Mutex::new(None)),
            total_time: Arc::new(AtomicU64::new(0)),
            active_threads: Arc::new(AtomicU64::new(0)),
        }
    }
    
    /// Start execution tracking
    pub fn start_execution(&self) {
        let mut start = self.start_time.lock().unwrap();
        if start.is_none() {
            *start = Some(Instant::now());
        }
    }
    
    /// Stop execution tracking
    pub fn stop_execution(&self) {
        let mut start_lock = self.start_time.lock().unwrap();
        if let Some(start) = *start_lock {
            let elapsed = start.elapsed();
            self.total_time.fetch_add(elapsed.as_millis() as u64, Ordering::AcqRel);
            *start_lock = None;
        }
    }
    
    /// Check if execution time limit has been exceeded
    pub fn check_time_limit(&self) -> Result<()> {
        let total = self.total_time.load(Ordering::Acquire);
        
        // Add current execution time if running
        let mut current_total = total;
        let start_lock = self.start_time.lock().unwrap();
        if let Some(start) = *start_lock {
            current_total += start.elapsed().as_millis() as u64;
        }
        
        if current_total > self.max_execution_time.as_millis() as u64 {
            return Err(Error::Timeout {
                operation: "execution".to_string(),
                duration: Duration::from_millis(current_total),
                instance_id: None,
            });
        }
        
        Ok(())
    }
    
    /// Register a new thread
    pub fn register_thread(&self) -> Result<()> {
        if let Some(max) = self.max_threads {
            let current = self.active_threads.fetch_add(1, Ordering::AcqRel) + 1;
            if current > max as u64 {
                // Rollback the increment
                self.active_threads.fetch_sub(1, Ordering::AcqRel);
                return Err(Error::ResourceLimit {
                    message: format!("Thread limit of {} exceeded", max)
                });
            }
        } else {
            self.active_threads.fetch_add(1, Ordering::AcqRel);
        }
        
        Ok(())
    }
    
    /// Unregister a thread
    pub fn unregister_thread(&self) {
        self.active_threads.fetch_sub(1, Ordering::AcqRel);
    }
    
    /// Get total execution time in milliseconds
    pub fn total_time_ms(&self) -> u64 {
        let total = self.total_time.load(Ordering::Acquire);
        
        // Add current execution time if running
        let mut current_total = total;
        let start_lock = self.start_time.lock().unwrap();
        if let Some(start) = *start_lock {
            current_total += start.elapsed().as_millis() as u64;
        }
        
        current_total
    }
    
    /// Get number of active threads
    pub fn active_threads(&self) -> u32 {
        self.active_threads.load(Ordering::Acquire) as u32
    }
    
    /// Apply CPU usage throttling
    pub fn apply_throttling(&self) {
        if let Some(percentage) = self.cpu_usage_percentage {
            if percentage >= 100 {
                return; // No throttling needed
            }
            
            // Simple throttling: sleep proportionally to target usage
            if percentage > 0 {
                // Example: For 50% CPU usage, sleep for 1ms after every 1ms of execution
                let sleep_time_ns = (100 - percentage) as u64 * 10_000; // convert to nanoseconds
                std::thread::sleep(Duration::from_nanos(sleep_time_ns));
            }
        }
    }
}

/// I/O resource tracker
#[derive(Debug, Clone)]
pub struct IoResourceTracker {
    /// Maximum number of open files
    pub max_open_files: u32,
    
    /// Maximum read bytes per second
    pub max_read_bytes_per_second: Option<u64>,
    
    /// Maximum write bytes per second
    pub max_write_bytes_per_second: Option<u64>,
    
    /// Maximum total read bytes
    pub max_total_read_bytes: Option<u64>,
    
    /// Maximum total write bytes
    pub max_total_write_bytes: Option<u64>,
    
    /// Current number of open files
    open_files: Arc<AtomicU64>,
    
    /// Total bytes read
    total_read: Arc<AtomicU64>,
    
    /// Total bytes written
    total_write: Arc<AtomicU64>,
    
    /// Rate tracking
    rate_tracker: Arc<Mutex<IoRateTracker>>,
}

/// I/O rate tracking
#[derive(Debug)]
struct IoRateTracker {
    /// Read events in the current window
    read_events: Vec<(Instant, u64)>,
    
    /// Write events in the current window
    write_events: Vec<(Instant, u64)>,
    
    /// Window duration
    window: Duration,
}

impl IoResourceTracker {
    /// Create a new I/O resource tracker
    pub fn new(limits: &IoLimits) -> Self {
        let rate_tracker = IoRateTracker {
            read_events: Vec::new(),
            write_events: Vec::new(),
            window: Duration::from_secs(1), // 1 second window
        };
        
        Self {
            max_open_files: limits.max_open_files,
            max_read_bytes_per_second: limits.max_read_bytes_per_second,
            max_write_bytes_per_second: limits.max_write_bytes_per_second,
            max_total_read_bytes: limits.max_total_read_bytes,
            max_total_write_bytes: limits.max_total_write_bytes,
            open_files: Arc::new(AtomicU64::new(0)),
            total_read: Arc::new(AtomicU64::new(0)),
            total_write: Arc::new(AtomicU64::new(0)),
            rate_tracker: Arc::new(Mutex::new(rate_tracker)),
        }
    }
    
    /// Register a file open
    pub fn register_open(&self) -> Result<()> {
        let current = self.open_files.fetch_add(1, Ordering::AcqRel) + 1;
        if current > self.max_open_files as u64 {
            // Rollback the increment
            self.open_files.fetch_sub(1, Ordering::AcqRel);
            return Err(Error::ResourceLimit {
                message: format!("Open file limit of {} exceeded", self.max_open_files)
            });
        }
        
        Ok(())
    }
    
    /// Register a file close
    pub fn register_close(&self) {
        self.open_files.fetch_sub(1, Ordering::AcqRel);
    }
    
    /// Register a read operation
    pub fn register_read(&self, bytes: u64) -> Result<()> {
        // Update total
        let total = self.total_read.fetch_add(bytes, Ordering::AcqRel) + bytes;
        
        // Check total limit
        if let Some(limit) = self.max_total_read_bytes {
            if total > limit {
                return Err(Error::ResourceLimit {
                    message: format!("Total read limit of {} bytes exceeded", limit)
                });
            }
        }
        
        // Check rate limit
        if let Some(rate_limit) = self.max_read_bytes_per_second {
            let now = Instant::now();
            let mut tracker = self.rate_tracker.lock().unwrap();
            
            // Clean up old events
            let cutoff = now - tracker.window;
            tracker.read_events.retain(|(time, _)| *time >= cutoff);
            
            // Add current read
            tracker.read_events.push((now, bytes));
            
            // Calculate total in window
            let window_total: u64 = tracker.read_events.iter().map(|(_, size)| *size).sum();
            
            if window_total > rate_limit {
                return Err(Error::ResourceLimit {
                    message: format!("Read rate limit of {} bytes/s exceeded", rate_limit)
                });
            }
        }
        
        Ok(())
    }
    
    /// Register a write operation
    pub fn register_write(&self, bytes: u64) -> Result<()> {
        // Update total
        let total = self.total_write.fetch_add(bytes, Ordering::AcqRel) + bytes;
        
        // Check total limit
        if let Some(limit) = self.max_total_write_bytes {
            if total > limit {
                return Err(Error::ResourceLimit {
                    message: format!("Total write limit of {} bytes exceeded", limit)
                });
            }
        }
        
        // Check rate limit
        if let Some(rate_limit) = self.max_write_bytes_per_second {
            let now = Instant::now();
            let mut tracker = self.rate_tracker.lock().unwrap();
            
            // Clean up old events
            let cutoff = now - tracker.window;
            tracker.write_events.retain(|(time, _)| *time >= cutoff);
            
            // Add current write
            tracker.write_events.push((now, bytes));
            
            // Calculate total in window
            let window_total: u64 = tracker.write_events.iter().map(|(_, size)| *size).sum();
            
            if window_total > rate_limit {
                return Err(Error::ResourceLimit {
                    message: format!("Write rate limit of {} bytes/s exceeded", rate_limit)
                });
            }
        }
        
        Ok(())
    }
    
    /// Get number of open files
    pub fn open_files(&self) -> u32 {
        self.open_files.load(Ordering::Acquire) as u32
    }
    
    /// Get total bytes read
    pub fn total_read(&self) -> u64 {
        self.total_read.load(Ordering::Acquire)
    }
    
    /// Get total bytes written
    pub fn total_write(&self) -> u64 {
        self.total_write.load(Ordering::Acquire)
    }
    
    /// Get current read rate in bytes per second
    pub fn read_rate(&self) -> u64 {
        let tracker = self.rate_tracker.lock().unwrap();
        let now = Instant::now();
        let cutoff = now - tracker.window;
        
        // Sum bytes in current window
        tracker.read_events
            .iter()
            .filter(|(time, _)| *time >= cutoff)
            .map(|(_, size)| *size)
            .sum()
    }
    
    /// Get current write rate in bytes per second
    pub fn write_rate(&self) -> u64 {
        let tracker = self.rate_tracker.lock().unwrap();
        let now = Instant::now();
        let cutoff = now - tracker.window;
        
        // Sum bytes in current window
        tracker.write_events
            .iter()
            .filter(|(time, _)| *time >= cutoff)
            .map(|(_, size)| *size)
            .sum()
    }
}

/// Time resource tracker
#[derive(Debug, Clone)]
pub struct TimeResourceTracker {
    /// Maximum total time
    pub max_total_time: Duration,
    
    /// Maximum idle time
    pub max_idle_time: Option<Duration>,
    
    /// Start time
    start_time: Arc<Mutex<Instant>>,
    
    /// Last activity time
    last_activity: Arc<Mutex<Instant>>,
}

impl TimeResourceTracker {
    /// Create a new time resource tracker
    pub fn new(limits: &TimeLimits) -> Self {
        let now = Instant::now();
        
        Self {
            max_total_time: Duration::from_millis(limits.max_total_time_ms),
            max_idle_time: limits.max_idle_time_ms.map(Duration::from_millis),
            start_time: Arc::new(Mutex::new(now)),
            last_activity: Arc::new(Mutex::new(now)),
        }
    }
    
    /// Register activity to reset idle timer
    pub fn register_activity(&self) {
        *self.last_activity.lock().unwrap() = Instant::now();
    }
    
    /// Check if time limits have been exceeded
    pub fn check_limits(&self) -> Result<()> {
        let now = Instant::now();
        
        // Check total time
        let elapsed = now.duration_since(*self.start_time.lock().unwrap());
        if elapsed > self.max_total_time {
            return Err(Error::Timeout {
                operation: "total time".to_string(),
                duration: elapsed,
                instance_id: None,
            });
        }
        
        // Check idle time
        if let Some(idle_limit) = self.max_idle_time {
            let idle_time = now.duration_since(*self.last_activity.lock().unwrap());
            if idle_time > idle_limit {
                return Err(Error::ResourceLimit {
                    message: format!("Idle time limit of {}ms exceeded", idle_limit.as_millis())
                });
            }
        }
        
        Ok(())
    }
    
    /// Get elapsed time in milliseconds
    pub fn elapsed_ms(&self) -> u64 {
        let now = Instant::now();
        now.duration_since(*self.start_time.lock().unwrap()).as_millis() as u64
    }
    
    /// Get idle time in milliseconds
    pub fn idle_ms(&self) -> u64 {
        let now = Instant::now();
        now.duration_since(*self.last_activity.lock().unwrap()).as_millis() as u64
    }
}

/// Main resource limit manager
#[derive(Debug, Clone)]
pub struct ResourceLimitManager {
    /// Memory resource tracker
    pub memory: MemoryResourceTracker,
    
    /// CPU resource tracker
    pub cpu: CpuResourceTracker,
    
    /// I/O resource tracker
    pub io: IoResourceTracker,
    
    /// Time resource tracker
    pub time: TimeResourceTracker,
    
    /// Fuel limit and usage
    pub fuel: Option<Arc<AtomicU64>>,
}

impl ResourceLimitManager {
    /// Create a new resource limit manager
    pub fn new(limits: &ResourceLimits) -> Self {
        let fuel = limits.fuel.map(|f| Arc::new(AtomicU64::new(f)));
        
        Self {
            memory: MemoryResourceTracker::new(&limits.memory),
            cpu: CpuResourceTracker::new(&limits.cpu),
            io: IoResourceTracker::new(&limits.io),
            time: TimeResourceTracker::new(&limits.time),
            fuel,
        }
    }
    
    /// Check all resource limits
    pub fn check_all_limits(&self) -> Result<()> {
        // Check time limits
        self.time.check_limits()?;
        
        // Check CPU limits
        self.cpu.check_time_limit()?;
        
        // Check fuel limits
        if let Some(fuel) = &self.fuel {
            if fuel.load(Ordering::Acquire) == 0 {
                return Err(Error::ResourceLimit {
                    message: "Fuel limit exceeded".to_string()
                });
            }
        }
        
        Ok(())
    }
    
    /// Consume fuel
    pub fn consume_fuel(&self, amount: u64) -> Result<()> {
        if let Some(fuel) = &self.fuel {
            let current = fuel.load(Ordering::Acquire);
            if current < amount {
                return Err(Error::ResourceLimit {
                    message: format!("Not enough fuel: requested {}, available {}", amount, current)
                });
            }
            
            fuel.fetch_sub(amount, Ordering::AcqRel);
        }
        
        Ok(())
    }
    
    /// Add fuel
    pub fn add_fuel(&self, amount: u64) -> Result<()> {
        if let Some(fuel) = &self.fuel {
            fuel.fetch_add(amount, Ordering::AcqRel);
            Ok(())
        } else {
            Err(Error::UnsupportedOperation {
                message: "Fuel metering is not enabled".to_string()
            })
        }
    }
    
    /// Reset fuel
    pub fn reset_fuel(&self, amount: u64) -> Result<()> {
        if let Some(fuel) = &self.fuel {
            fuel.store(amount, Ordering::Release);
            Ok(())
        } else {
            Err(Error::UnsupportedOperation {
                message: "Fuel metering is not enabled".to_string()
            })
        }
    }
    
    /// Get remaining fuel
    pub fn get_remaining_fuel(&self) -> Option<u64> {
        self.fuel.as_ref().map(|f| f.load(Ordering::Acquire))
    }
    
    /// Start a background monitor thread for resource limits
    pub fn start_monitor(&self) -> std::thread::JoinHandle<()> {
        // Clone the trackers
        let cpu_tracker = self.cpu.clone();
        let time_tracker = self.time.clone();
        
        std::thread::spawn(move || {
            let check_interval = Duration::from_millis(100); // Check every 100ms
            
            loop {
                // Sleep
                std::thread::sleep(check_interval);
                
                // Register activity for time tracker (monitor itself counts as activity)
                time_tracker.register_activity();
                
                // Apply CPU throttling
                cpu_tracker.apply_throttling();
                
                // Check limits
                // NOTE: We don't handle errors here because the monitor thread
                // doesn't have a way to signal the main thread directly.
                // This is just a background check - the main thread should also
                // check limits at critical points.
                let _ = time_tracker.check_limits();
                let _ = cpu_tracker.check_time_limit();
            }
        })
    }
}