shape-runtime 0.3.2

Bytecode compiler, builtins, and runtime infrastructure for Shape
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
//! In-memory virtual filesystem for sandbox mode.
//!
//! All reads and writes operate against an in-memory store. The host can
//! pre-seed read-only files before execution and extract written files after.
//! No real disk I/O is performed.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::RwLock;

use super::runtime_policy::{FileMetadata, FileSystemProvider, PathEntry};

// ============================================================================
// VFS Types
// ============================================================================

/// A single entry in the virtual filesystem.
#[derive(Debug, Clone)]
struct VfsEntry {
    content: Vec<u8>,
    is_dir: bool,
}

/// In-memory virtual filesystem implementing [`FileSystemProvider`].
///
/// Thread-safe via internal `RwLock`. Designed for sandbox mode where no real
/// disk access is permitted.
pub struct VirtualFilesystem {
    files: RwLock<HashMap<PathBuf, VfsEntry>>,
    read_only: RwLock<HashSet<PathBuf>>,
    total_written: RwLock<usize>,
    max_size: usize,
}

impl VirtualFilesystem {
    /// Create a new empty VFS with the given maximum total size in bytes.
    ///
    /// `max_size` of `0` means unlimited.
    pub fn new(max_size: usize) -> Self {
        Self {
            files: RwLock::new(HashMap::new()),
            read_only: RwLock::new(HashSet::new()),
            total_written: RwLock::new(0),
            max_size,
        }
    }

    /// Pre-seed a read-only file. Typically called by the host before
    /// executing a sandboxed program (e.g., from `[sandbox.seed_files]`).
    pub fn seed_file(&self, path: impl Into<PathBuf>, content: Vec<u8>) {
        let path = path.into();
        // Ensure parent directories exist.
        self.ensure_parents(&path);
        let mut files = self.files.write().unwrap();
        files.insert(
            path.clone(),
            VfsEntry {
                content,
                is_dir: false,
            },
        );
        self.read_only.write().unwrap().insert(path);
    }

    /// Seed a directory (empty). Automatically seeds all parent directories.
    pub fn seed_dir(&self, path: impl Into<PathBuf>) {
        let path = path.into();
        self.ensure_parents(&path);
        let mut files = self.files.write().unwrap();
        files.insert(
            path.clone(),
            VfsEntry {
                content: Vec::new(),
                is_dir: true,
            },
        );
        self.read_only.write().unwrap().insert(path);
    }

    /// Extract all files written by the sandboxed program (excludes seed files).
    ///
    /// Returns a map from path to file content.
    pub fn extract_written_files(&self) -> HashMap<PathBuf, Vec<u8>> {
        let files = self.files.read().unwrap();
        let ro = self.read_only.read().unwrap();
        files
            .iter()
            .filter(|(p, e)| !e.is_dir && !ro.contains(*p))
            .map(|(p, e)| (p.clone(), e.content.clone()))
            .collect()
    }

    /// Total bytes written by the sandboxed program (excludes seed files).
    pub fn total_bytes_written(&self) -> usize {
        *self.total_written.read().unwrap()
    }

    /// Ensure all ancestor directories of `path` exist as directory entries.
    fn ensure_parents(&self, path: &Path) {
        let mut files = self.files.write().unwrap();
        for ancestor in path.ancestors().skip(1) {
            if ancestor == Path::new("") || ancestor == Path::new("/") {
                // Always implicitly exists.
                continue;
            }
            files
                .entry(ancestor.to_path_buf())
                .or_insert_with(|| VfsEntry {
                    content: Vec::new(),
                    is_dir: true,
                });
        }
    }

    /// Check size limit, returning PermissionDenied if exceeded.
    fn check_size_limit(&self, additional: usize) -> std::io::Result<()> {
        if self.max_size == 0 {
            return Ok(());
        }
        let current = *self.total_written.read().unwrap();
        if current + additional > self.max_size {
            return Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!(
                    "VFS size limit exceeded: {} + {} > {}",
                    current, additional, self.max_size
                ),
            ));
        }
        Ok(())
    }
}

impl FileSystemProvider for VirtualFilesystem {
    fn read(&self, path: &Path) -> std::io::Result<Vec<u8>> {
        let files = self.files.read().unwrap();
        match files.get(path) {
            Some(entry) if !entry.is_dir => Ok(entry.content.clone()),
            Some(_) => Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("{} is a directory", path.display()),
            )),
            None => Err(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("{} not found in VFS", path.display()),
            )),
        }
    }

    fn write(&self, path: &Path, data: &[u8]) -> std::io::Result<()> {
        if self.read_only.read().unwrap().contains(path) {
            return Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                format!("{} is read-only (seed file)", path.display()),
            ));
        }
        self.check_size_limit(data.len())?;
        self.ensure_parents(path);
        let mut files = self.files.write().unwrap();

        // Subtract old size if overwriting a user-written file.
        let old_size = files
            .get(path)
            .filter(|e| !e.is_dir)
            .map(|e| e.content.len())
            .unwrap_or(0);

        files.insert(
            path.to_path_buf(),
            VfsEntry {
                content: data.to_vec(),
                is_dir: false,
            },
        );

        let mut total = self.total_written.write().unwrap();
        *total = total.saturating_sub(old_size) + data.len();
        Ok(())
    }

    fn append(&self, path: &Path, data: &[u8]) -> std::io::Result<()> {
        if self.read_only.read().unwrap().contains(path) {
            return Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                format!("{} is read-only (seed file)", path.display()),
            ));
        }
        self.check_size_limit(data.len())?;
        self.ensure_parents(path);
        let mut files = self.files.write().unwrap();
        let entry = files.entry(path.to_path_buf()).or_insert_with(|| VfsEntry {
            content: Vec::new(),
            is_dir: false,
        });
        if entry.is_dir {
            return Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("{} is a directory", path.display()),
            ));
        }
        entry.content.extend_from_slice(data);
        *self.total_written.write().unwrap() += data.len();
        Ok(())
    }

    fn exists(&self, path: &Path) -> bool {
        // Root always exists.
        if path == Path::new("/") || path == Path::new("") {
            return true;
        }
        self.files.read().unwrap().contains_key(path)
    }

    fn remove(&self, path: &Path) -> std::io::Result<()> {
        if self.read_only.read().unwrap().contains(path) {
            return Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                format!("{} is read-only (seed file)", path.display()),
            ));
        }
        let mut files = self.files.write().unwrap();
        match files.remove(path) {
            Some(entry) if !entry.is_dir => {
                // Reclaim written bytes.
                let mut total = self.total_written.write().unwrap();
                *total = total.saturating_sub(entry.content.len());
                Ok(())
            }
            Some(entry) => {
                // Put it back — can't remove non-empty dir through this API.
                files.insert(path.to_path_buf(), entry);
                Err(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    format!("{} is a directory", path.display()),
                ))
            }
            None => Err(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("{} not found in VFS", path.display()),
            )),
        }
    }

    fn list_dir(&self, path: &Path) -> std::io::Result<Vec<PathEntry>> {
        let files = self.files.read().unwrap();
        // Check the directory exists (root is implicit).
        let is_root = path == Path::new("/") || path == Path::new("");
        if !is_root {
            match files.get(path) {
                Some(e) if e.is_dir => {}
                Some(_) => {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        format!("{} is not a directory", path.display()),
                    ));
                }
                None => {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::NotFound,
                        format!("{} not found in VFS", path.display()),
                    ));
                }
            }
        }

        let mut entries = Vec::new();
        for (p, e) in files.iter() {
            if let Some(parent) = p.parent() {
                if parent == path && p != path {
                    entries.push(PathEntry {
                        path: p.clone(),
                        is_dir: e.is_dir,
                    });
                }
            }
        }
        entries.sort_by(|a, b| a.path.cmp(&b.path));
        Ok(entries)
    }

    fn metadata(&self, path: &Path) -> std::io::Result<FileMetadata> {
        let files = self.files.read().unwrap();
        match files.get(path) {
            Some(entry) => Ok(FileMetadata {
                size: entry.content.len() as u64,
                is_dir: entry.is_dir,
                is_file: !entry.is_dir,
                readonly: self.read_only.read().unwrap().contains(path),
            }),
            None => Err(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("{} not found in VFS", path.display()),
            )),
        }
    }

    fn create_dir_all(&self, path: &Path) -> std::io::Result<()> {
        let mut files = self.files.write().unwrap();
        for ancestor in path.ancestors() {
            if ancestor == Path::new("") || ancestor == Path::new("/") {
                continue;
            }
            files
                .entry(ancestor.to_path_buf())
                .or_insert_with(|| VfsEntry {
                    content: Vec::new(),
                    is_dir: true,
                });
        }
        // Ensure the target itself is also created.
        files.entry(path.to_path_buf()).or_insert_with(|| VfsEntry {
            content: Vec::new(),
            is_dir: true,
        });
        Ok(())
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    fn vfs() -> VirtualFilesystem {
        VirtualFilesystem::new(0) // unlimited
    }

    // -- Basic read/write --

    #[test]
    fn write_then_read() {
        let fs = vfs();
        fs.write(Path::new("/hello.txt"), b"world").unwrap();
        let data = fs.read(Path::new("/hello.txt")).unwrap();
        assert_eq!(data, b"world");
    }

    #[test]
    fn read_nonexistent() {
        let fs = vfs();
        let err = fs.read(Path::new("/nope")).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
    }

    #[test]
    fn overwrite_file() {
        let fs = vfs();
        fs.write(Path::new("/f"), b"old").unwrap();
        fs.write(Path::new("/f"), b"new").unwrap();
        assert_eq!(fs.read(Path::new("/f")).unwrap(), b"new");
    }

    // -- Append --

    #[test]
    fn append_creates_and_extends() {
        let fs = vfs();
        fs.append(Path::new("/log.txt"), b"line1\n").unwrap();
        fs.append(Path::new("/log.txt"), b"line2\n").unwrap();
        assert_eq!(fs.read(Path::new("/log.txt")).unwrap(), b"line1\nline2\n");
    }

    // -- Seed files (read-only) --

    #[test]
    fn seed_file_is_readable() {
        let fs = vfs();
        fs.seed_file(PathBuf::from("/config.toml"), b"key = true".to_vec());
        assert_eq!(fs.read(Path::new("/config.toml")).unwrap(), b"key = true");
    }

    #[test]
    fn seed_file_is_not_writable() {
        let fs = vfs();
        fs.seed_file(PathBuf::from("/config.toml"), b"data".to_vec());
        let err = fs.write(Path::new("/config.toml"), b"hacked").unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied);
    }

    #[test]
    fn seed_file_is_not_appendable() {
        let fs = vfs();
        fs.seed_file(PathBuf::from("/config.toml"), b"data".to_vec());
        let err = fs.append(Path::new("/config.toml"), b"extra").unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied);
    }

    #[test]
    fn seed_file_is_not_removable() {
        let fs = vfs();
        fs.seed_file(PathBuf::from("/config.toml"), b"data".to_vec());
        let err = fs.remove(Path::new("/config.toml")).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied);
    }

    #[test]
    fn seed_file_not_in_extract() {
        let fs = vfs();
        fs.seed_file(PathBuf::from("/seed.txt"), b"seed".to_vec());
        fs.write(Path::new("/output.txt"), b"output").unwrap();
        let written = fs.extract_written_files();
        assert!(written.contains_key(Path::new("/output.txt")));
        assert!(!written.contains_key(Path::new("/seed.txt")));
    }

    // -- Directories --

    #[test]
    fn create_dir_and_list() {
        let fs = vfs();
        fs.create_dir_all(Path::new("/data/subdir")).unwrap();
        fs.write(Path::new("/data/subdir/file.txt"), b"hi").unwrap();
        let entries = fs.list_dir(Path::new("/data/subdir")).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].path, PathBuf::from("/data/subdir/file.txt"));
        assert!(!entries[0].is_dir);
    }

    #[test]
    fn list_root() {
        let fs = vfs();
        fs.write(Path::new("/a.txt"), b"a").unwrap();
        fs.create_dir_all(Path::new("/dir")).unwrap();
        let entries = fs.list_dir(Path::new("/")).unwrap();
        assert!(entries.len() >= 2);
    }

    #[test]
    fn read_directory_fails() {
        let fs = vfs();
        fs.create_dir_all(Path::new("/mydir")).unwrap();
        let err = fs.read(Path::new("/mydir")).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::Other);
    }

    // -- Size limits --

    #[test]
    fn size_limit_enforced() {
        let fs = VirtualFilesystem::new(10);
        fs.write(Path::new("/a"), b"12345").unwrap();
        assert_eq!(fs.total_bytes_written(), 5);
        // This should fail — would exceed limit
        let err = fs.write(Path::new("/b"), b"123456").unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::Other);
    }

    #[test]
    fn overwrite_reclaims_space() {
        let fs = VirtualFilesystem::new(20);
        fs.write(Path::new("/f"), b"1234567890").unwrap(); // 10 bytes
        assert_eq!(fs.total_bytes_written(), 10);
        fs.write(Path::new("/f"), b"ab").unwrap(); // replace with 2 bytes
        assert_eq!(fs.total_bytes_written(), 2);
    }

    // -- Exists --

    #[test]
    fn exists_for_files_and_dirs() {
        let fs = vfs();
        assert!(!fs.exists(Path::new("/x")));
        fs.write(Path::new("/x"), b"data").unwrap();
        assert!(fs.exists(Path::new("/x")));
        fs.create_dir_all(Path::new("/d")).unwrap();
        assert!(fs.exists(Path::new("/d")));
    }

    #[test]
    fn root_always_exists() {
        let fs = vfs();
        assert!(fs.exists(Path::new("/")));
    }

    // -- Remove --

    #[test]
    fn remove_file() {
        let fs = vfs();
        fs.write(Path::new("/f"), b"data").unwrap();
        fs.remove(Path::new("/f")).unwrap();
        assert!(!fs.exists(Path::new("/f")));
    }

    #[test]
    fn remove_nonexistent_errors() {
        let fs = vfs();
        let err = fs.remove(Path::new("/nope")).unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
    }

    #[test]
    fn remove_reclaims_bytes() {
        let fs = VirtualFilesystem::new(100);
        fs.write(Path::new("/f"), b"12345").unwrap();
        assert_eq!(fs.total_bytes_written(), 5);
        fs.remove(Path::new("/f")).unwrap();
        assert_eq!(fs.total_bytes_written(), 0);
    }

    // -- Metadata --

    #[test]
    fn metadata_file() {
        let fs = vfs();
        fs.write(Path::new("/f"), b"hello").unwrap();
        let m = fs.metadata(Path::new("/f")).unwrap();
        assert!(m.is_file);
        assert!(!m.is_dir);
        assert_eq!(m.size, 5);
        assert!(!m.readonly);
    }

    #[test]
    fn metadata_seed_file_is_readonly() {
        let fs = vfs();
        fs.seed_file(PathBuf::from("/s"), b"data".to_vec());
        let m = fs.metadata(Path::new("/s")).unwrap();
        assert!(m.readonly);
    }

    #[test]
    fn metadata_dir() {
        let fs = vfs();
        fs.create_dir_all(Path::new("/d")).unwrap();
        let m = fs.metadata(Path::new("/d")).unwrap();
        assert!(m.is_dir);
        assert!(!m.is_file);
    }

    // -- Parent directory auto-creation --

    #[test]
    fn writing_deep_path_creates_parents() {
        let fs = vfs();
        fs.write(Path::new("/a/b/c/file.txt"), b"deep").unwrap();
        assert!(fs.exists(Path::new("/a")));
        assert!(fs.exists(Path::new("/a/b")));
        assert!(fs.exists(Path::new("/a/b/c")));
        let m = fs.metadata(Path::new("/a/b")).unwrap();
        assert!(m.is_dir);
    }
}