vyre-runtime 0.6.3

Persistent megakernel + io_uring zero-copy streaming runtime for vyre - GPU as VIR0 bytecode interpreter
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
//! Adversarial break-it tests for the runtime pipeline cache (disk + remote).
//!
//! Every assertion here is the strongest reasonable invariant.  If a test
//! passes trivially the invariant is too weak.  Several tests are
//! intentionally designed to **fail** against the current implementation  -
//! they document bugs that must be fixed before the cache is safe at
//! internet scale.
//!
//! Invariants enforced:
//!   - No torn reads under concurrent `put()` pressure.
//!   - Corrupted / truncated on-disk artifacts are rejected (`None`).
//!   - Disk-full conditions are handled gracefully (no panic).
//!   - Symlinks in the cache root are never followed (anti-traversal).
//!   - Concurrent `put` + `get` interleaving is race-free.
//!   - `LayeredPipelineCache` routes writes to layer-0 and reads fall through.
//!   - `RemoteCache` is feature-gated; this file compiles without `remote`.

#![forbid(unsafe_code)]

use std::path::Path;
use std::process::Command;
use std::sync::{Arc, Barrier};
use std::thread;

use proptest::prelude::*;
use tempfile::TempDir;
use vyre_runtime::pipeline_cache::DiskCache;
use vyre_runtime::{
    InMemoryPipelineCache, LayeredPipelineCache, PipelineCacheStore, PipelineFingerprint,
};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Deterministic fingerprint from a single seed byte.
fn fp_from_seed(seed: u8) -> PipelineFingerprint {
    let mut bytes = [seed; 32];
    bytes[0] = seed;
    bytes[31] = seed.wrapping_mul(7);
    PipelineFingerprint(bytes)
}

/// Fixed fingerprint for tests that need a stable path name.
fn dead_fp() -> PipelineFingerprint {
    PipelineFingerprint([0u8; 32])
}

// ---------------------------------------------------------------------------
// 1. Atomic rename race  -  100 writers, same fp, different 1 MiB payloads
// ---------------------------------------------------------------------------

#[test]
fn disk_cache_atomic_rename_race_100_threads() {
    let dir = TempDir::new().unwrap();
    let cache = Arc::new(DiskCache::new(dir.path()).unwrap());
    let fp = fp_from_seed(42);

    // 64 KiB payloads  -  large enough that any torn read would have a
    // mismatched length, small enough that the test finishes quickly.
    let payloads: Vec<Vec<u8>> = (0..100).map(|i| vec![i as u8; 64 * 1024]).collect();

    let barrier = Arc::new(Barrier::new(101)); // 100 writers + 1 reader
    let mut handles = vec![];

    // 100 writers race to put different payloads for the same fingerprint.
    for payload in payloads.clone() {
        let cache: Arc<DiskCache> = Arc::clone(&cache);
        let barrier = Arc::clone(&barrier);
        handles.push(thread::spawn(move || {
            barrier.wait();
            cache.put(fp, payload);
        }));
    }

    // Reader continuously samples the cache while writers are active.
    let cache_reader: Arc<DiskCache> = Arc::clone(&cache);
    let reader = thread::spawn(move || {
        barrier.wait();
        let mut observations = vec![];
        for _ in 0..10_000 {
            if let Some(bytes) = cache_reader.get(&fp) {
                observations.push(bytes);
            }
            // Yield occasionally to increase scheduler interleaving.
            if observations.len() % 1000 == 0 {
                thread::yield_now();
            }
        }
        observations
    });

    for h in handles {
        h.join().expect("writer thread must not panic");
    }
    let observations = reader.join().expect("reader thread must not panic");

    // Strongest invariant: every single observed value is a *complete*
    // payload.  A torn state would be a prefix of one payload concatenated
    // with a suffix of another, giving a length or content mismatch.
    for (idx, obs) in observations.iter().enumerate() {
        assert!(
            payloads.contains(obs),
            "torn read at observation #{idx}: {} bytes do not match any complete payload",
            obs.len()
        );
    }

    // Eventual consistency: after all writers finish, the final value must
    // also be one of the complete payloads (or None if every writer lost
    // its race  -  impossible here, but we allow it).
    if let Some(bytes) = cache.get(&fp) {
        assert!(
            payloads.contains(&bytes),
            "final state is {} bytes, not a complete payload",
            bytes.len()
        );
    }
}

// ---------------------------------------------------------------------------
// 2. Corrupted .bin file rejection (property-based)
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig {
        cases: 256,
        failure_persistence: Some(Box::new(
            proptest::test_runner::FileFailurePersistence::Off
        )),
        ..ProptestConfig::default()
    })]

    #[test]
    fn corrupted_bin_file_rejected(
        seed in 0u8..=255,
        partial in prop::collection::vec(0u8..=255, 1..1024)
    ) {
        let dir = TempDir::new().unwrap();
        let cache = DiskCache::new(dir.path()).unwrap();
        let fp = fp_from_seed(seed);
        let path = cache.root().join(format!("{}.bin", fp.hex()));

        // Manually write partial bytes to simulate a crash mid-write or a
        // truncated download.
        std::fs::write(&path, &partial).unwrap();

        // The cache must detect corruption / truncation and return None.
        // Returning the raw bytes would pass a truncated target binary
        // artifact to the driver → undefined behaviour at internet scale.
        let result = cache.get(&fp);
        prop_assert!(
            result.is_none(),
            "corrupted file returned Some({} bytes) instead of None",
            result.map(|v| v.len()).unwrap_or(0)
        );
    }
}

// ---------------------------------------------------------------------------
// 3. Disk-full simulation via 4 KB tmpfs
// ---------------------------------------------------------------------------

/// Attempt to mount a tmpfs of a given size onto a temporary directory.
/// On drop it lazily unmounts so `TempDir` can clean up the empty folder.
struct TmpfsGuard {
    dir: TempDir,
}

impl TmpfsGuard {
    fn with_size_kb(size_kb: usize) -> Result<Self, String> {
        let dir = TempDir::new().map_err(|e| e.to_string())?;

        // Try `sudo -n` first (typical dev / CI environments).
        let sudo_out = Command::new("sudo")
            .args([
                "-n",
                "mount",
                "-t",
                "tmpfs",
                "-o",
                &format!("size={}k", size_kb),
                "tmpfs",
            ])
            .arg(dir.path())
            .output();

        match sudo_out {
            Ok(out) if out.status.success() => return Ok(Self { dir }),
            _ => {}
        }

        // Fallback: direct mount (works when we are already root or in a
        // mount namespace with privileges).
        let direct_out = Command::new("mount")
            .args(["-t", "tmpfs", "-o", &format!("size={}k", size_kb), "tmpfs"])
            .arg(dir.path())
            .output()
            .map_err(|e| format!("failed to spawn mount: {}", e))?;

        if direct_out.status.success() {
            Ok(Self { dir })
        } else {
            let stderr = String::from_utf8_lossy(&direct_out.stderr);
            Err(format!(
                "tmpfs mount failed (tried sudo and direct mount): {}",
                stderr
            ))
        }
    }

    fn path(&self) -> &Path {
        self.dir.path()
    }
}

impl Drop for TmpfsGuard {
    fn drop(&mut self) {
        // Lazy unmount detaches immediately so `TempDir` can `rmdir` the
        // (now empty) mount point.
        let _ = Command::new("sudo")
            .args(["-n", "umount", "-l"])
            .arg(self.dir.path())
            .stderr(std::process::Stdio::null())
            .status();
        let _ = Command::new("umount")
            .args(["-l"])
            .arg(self.dir.path())
            .stderr(std::process::Stdio::null())
            .status();
    }
}

#[test]
fn disk_full_tmpfs_put_no_panic() {
    let guard = TmpfsGuard::with_size_kb(4).expect(
        "this test requires mount privileges to create a 4 KB tmpfs; \
         configure passwordless sudo or run in a mount-capable environment",
    );

    let cache = DiskCache::new(guard.path()).unwrap();
    let fp = fp_from_seed(1);
    let big_artifact = vec![0xABu8; 100 * 1024]; // 100 KB into 4 KB fs

    // Best-effort `put` must not panic.  The write to the temp file will
    // fail with ENOSPC; the implementation silently drops the error.
    cache.put(fp, big_artifact);

    // Because the temp file could not be written, the final rename never
    // happens.  `get()` must therefore return `None`.
    assert!(
        cache.get(&fp).is_none(),
        "get() returned Some after a disk-full put  -  partial artifact must not be served"
    );
}

// ---------------------------------------------------------------------------
// 4. Symlink attack  -  must refuse to follow, must not read /etc/passwd
// ---------------------------------------------------------------------------

#[cfg(unix)]
#[test]
fn symlink_attack_must_not_read_etc_passwd() {
    let dir = TempDir::new().unwrap();
    let cache = DiskCache::new(dir.path()).unwrap();
    let fp = dead_fp();
    let bin_path = cache.root().join(format!("{}.bin", fp.hex()));

    // Use `/etc/passwd` when available (standard on Unix) so the test
    // matches the real attack surface.  Fallback to a synthetic target
    // on stripped-down environments so the code path is still exercised.
    let target: std::path::PathBuf = if Path::new("/etc/passwd").exists() {
        Path::new("/etc/passwd").into()
    } else {
        let fallback = dir.path().join("synthetic_secret");
        std::fs::write(&fallback, b"SYNTHETIC-SECRET-DATA").unwrap();
        fallback
    };

    std::os::unix::fs::symlink(&target, &bin_path).unwrap();

    // `get()` must refuse to follow the symlink and return `None`.
    // If it follows the symlink, it would return the contents of the
    // target file  -  a directory-traversal / information-disclosure
    // vulnerability.
    let result = cache.get(&fp);
    assert!(
        result.is_none(),
        "symlink attack succeeded: get() read {} bytes from {:?} instead of refusing",
        result.as_ref().map(|v| v.len()).unwrap_or(0),
        target
    );
}

#[cfg(not(unix))]
#[test]
fn symlink_attack_not_applicable_on_non_unix() {
    // On non-Unix platforms the symlink attack vector differs; no symlink probe.
    assert!(!cfg!(unix), "symlink adversarial suite is Unix-only");
}

// ---------------------------------------------------------------------------
// 5. Concurrent put + get  -  50 put threads, 50 get threads, same fp set
// ---------------------------------------------------------------------------

#[test]
fn concurrent_put_get_50_50_same_fp_set() {
    let dir = TempDir::new().unwrap();
    let cache = Arc::new(DiskCache::new(dir.path()).unwrap());

    let fps: Vec<PipelineFingerprint> = (0..5).map(fp_from_seed).collect();
    let payloads: Vec<Vec<u8>> = (0..5).map(|i| vec![i as u8; 128 * 1024]).collect();

    let barrier = Arc::new(Barrier::new(101));
    let mut handles = vec![];

    // 50 put threads
    for i in 0..50 {
        let cache: Arc<DiskCache> = Arc::clone(&cache);
        let fps = fps.clone();
        let payloads = payloads.clone();
        let barrier = Arc::clone(&barrier);
        handles.push(thread::spawn(move || {
            barrier.wait();
            for _ in 0..20 {
                let fp = fps[i % fps.len()];
                let payload = payloads[i % payloads.len()].clone();
                cache.put(fp, payload);
            }
        }));
    }

    // 50 get threads
    for _ in 0..50 {
        let cache: Arc<DiskCache> = Arc::clone(&cache);
        let fps = fps.clone();
        let payloads = payloads.clone();
        let barrier = Arc::clone(&barrier);
        handles.push(thread::spawn(move || {
            barrier.wait();
            for _ in 0..200 {
                for fp in &fps {
                    if let Some(bytes) = cache.get(fp) {
                        assert!(
                            payloads.contains(&bytes),
                            "torn read: {} bytes not in known payload set",
                            bytes.len()
                        );
                    }
                }
            }
        }));
    }

    barrier.wait();
    for h in handles {
        h.join().expect("thread must not panic");
    }

    // Eventual consistency: every fp must resolve to one of the known
    // complete payloads (or None if no writer ever succeeded  -  unlikely).
    for fp in &fps {
        if let Some(bytes) = cache.get(fp) {
            assert!(
                payloads.contains(&bytes),
                "final state is {} bytes, not a known complete payload",
                bytes.len()
            );
        }
    }
}

// ---------------------------------------------------------------------------
// 6. LayeredPipelineCache  -  DiskCache layer 0, InMemoryPipelineCache layer 1
// ---------------------------------------------------------------------------

#[test]
fn layered_cache_disk_over_memory_fallthrough_and_put_routing() {
    let dir = TempDir::new().unwrap();
    let disk = Arc::new(DiskCache::new(dir.path()).unwrap());
    let mem = Arc::new(InMemoryPipelineCache::new());

    let fp = fp_from_seed(7);
    let artifact = b"layer-1-artifact".to_vec();

    // Pre-populate the lower layer.
    mem.put(fp, artifact.clone());

    let layered = LayeredPipelineCache::new(vec![disk.clone(), mem.clone()]);

    // Miss in disk, fall through to memory.
    assert_eq!(layered.get(&fp).unwrap(), artifact);

    // `put` routes to layer-0 (disk) only.
    let new_artifact = b"layer-0-artifact".to_vec();
    layered.put(fp, new_artifact.clone());
    assert_eq!(disk.get(&fp).unwrap(), new_artifact);

    // Lower layer is untouched by `put`.
    assert_eq!(mem.get(&fp).unwrap(), artifact);
}

// ---------------------------------------------------------------------------
// 7. RemoteCache feature-gated compilation & layered behaviour
// ---------------------------------------------------------------------------

/// Verify that the crate and this test file compile without the `remote`
/// feature.  All `RemoteCache`-dependent code is behind
/// `#[cfg(feature = "remote")]`.
#[test]
fn compiles_without_remote_feature() {
    // Compilation itself is the test  -  the test binary builds iff
    // the default feature set + this file don't reference `remote`.
    let _ = InMemoryPipelineCache::new();
}

#[cfg(feature = "remote")]
#[test]
fn remote_cache_in_layered_cache_put_and_get() {
    let dir = TempDir::new().unwrap();
    let disk = Arc::new(DiskCache::new(dir.path()).unwrap());
    let remote = Arc::new(vyre_runtime::RemoteCache::new("http://127.0.0.1:1"));

    let fp = fp_from_seed(3);

    let layered = LayeredPipelineCache::new(vec![disk.clone(), remote]);

    // `put` must route to the first layer (disk), not remote.
    layered.put(fp, b"only-on-disk".to_vec());
    assert_eq!(disk.get(&fp).unwrap(), b"only-on-disk".to_vec());

    // `get` must hit disk first; remote is never contacted because disk
    // already has the entry.
    assert_eq!(layered.get(&fp).unwrap(), b"only-on-disk".to_vec());
}