boxlite 0.9.7

Embeddable virtual machine runtime for secure, isolated code execution
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
//! Integration tests for detach mode behavior.
//!
//! Verifies detached boxes survive runtime drop, non-detached boxes exit
//! via watchdog POLLHUP, and detached boxes can be recovered after restart.

mod common;

use boxlite::BoxliteRuntime;
use boxlite::litebox::BoxCommand;
use boxlite::runtime::options::BoxliteOptions;
use boxlite::runtime::types::BoxStatus;
use boxlite::util::{PidFileReader, is_process_alive};
use std::path::{Path, PathBuf};

// ============================================================================
// LOCAL HELPERS
// ============================================================================

/// Get the PID file path for a box under the given home directory.
fn pid_file_path(home_dir: &Path, box_id: &str) -> PathBuf {
    home_dir.join("boxes").join(box_id).join("shim.pid")
}

/// Count live processes whose argv references `box_id` (Linux `/proc` scan).
///
/// A detached box's whole tree — the outer bwrap launcher, the inner
/// pid-namespace bwrap, the shim, and the VM — carries the unique box id in its
/// bwrap bind paths, so this counts every process in the tree. Used to assert
/// that the production reap path tears the *whole* tree down, not just the
/// recorded outer-bwrap pid.
#[cfg(target_os = "linux")]
fn box_proc_count(box_id: &str) -> usize {
    let mut count = 0;
    let Ok(entries) = std::fs::read_dir("/proc") else {
        return 0;
    };
    for entry in entries.flatten() {
        let name = entry.file_name();
        let Some(name) = name.to_str() else { continue };
        if name.parse::<u32>().is_err() {
            continue; // not a pid dir
        }
        if let Ok(cmdline) = std::fs::read(entry.path().join("cmdline"))
            && cmdline
                .windows(box_id.len())
                .any(|w| w == box_id.as_bytes())
        {
            count += 1;
        }
    }
    count
}

#[cfg(target_os = "linux")]
async fn wait_for_box_proc_count_zero(box_id: &str) -> usize {
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
    let mut count = box_proc_count(box_id);

    while count != 0 && std::time::Instant::now() < deadline {
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        count = box_proc_count(box_id);
    }

    count
}

// ============================================================================
// DETACH MODE TESTS
// ============================================================================

#[tokio::test]
async fn detached_box_creates_pid_file() {
    let home = boxlite_test_utils::home::PerTestBoxHome::new();
    let runtime = BoxliteRuntime::new(BoxliteOptions {
        home_dir: home.path.clone(),
        image_registries: common::test_registries(),
    })
    .expect("create runtime");

    let handle = runtime
        .create(
            boxlite::runtime::options::BoxOptions {
                detach: true,
                ..common::alpine_opts()
            },
            None,
        )
        .await
        .unwrap();

    let _ = handle.exec(BoxCommand::new("sleep").args(["300"])).await;

    let pf = pid_file_path(&home.path, handle.id().as_str());
    assert!(pf.exists(), "Detached box should have PID file");

    // Cleanup
    runtime.remove(handle.id().as_str(), true).await.unwrap();
}

#[tokio::test]
async fn detached_box_survives_runtime_drop() {
    let home = boxlite_test_utils::home::PerTestBoxHome::new();
    let box_id: String;
    let original_pid: u32;

    // Create detached box
    {
        let runtime = BoxliteRuntime::new(BoxliteOptions {
            home_dir: home.path.clone(),
            image_registries: common::test_registries(),
        })
        .unwrap();

        let handle = runtime
            .create(
                boxlite::runtime::options::BoxOptions {
                    detach: true,
                    ..common::alpine_opts()
                },
                None,
            )
            .await
            .unwrap();

        let _ = handle.exec(BoxCommand::new("sleep").args(["300"])).await;
        box_id = handle.id().to_string();

        let pf = pid_file_path(&home.path, &box_id);
        original_pid = PidFileReader::at(&pf).read().map(|r| r.pid).unwrap();

        // Runtime drops here - box should survive
    }

    // Wait a moment
    tokio::time::sleep(std::time::Duration::from_millis(200)).await;

    // Verify process still alive
    assert!(
        is_process_alive(original_pid),
        "Detached box process {} should survive runtime drop",
        original_pid
    );

    // Cleanup
    let runtime = BoxliteRuntime::new(BoxliteOptions {
        home_dir: home.path.clone(),
        image_registries: common::test_registries(),
    })
    .unwrap();
    runtime.remove(&box_id, true).await.unwrap();
}

/// Non-detached box should exit when runtime drops (watchdog POLLHUP).
///
/// Symmetric counterpart to `detached_box_survives_runtime_drop`.
/// Verifies the full watchdog chain:
///   Keepalive drop -> pipe close -> shim POLLHUP -> SIGTERM -> process exit
#[tokio::test]
async fn non_detached_box_exits_on_runtime_drop() {
    // Use /tmp for shorter paths -- macOS default TempDir paths exceed SUN_LEN for Unix sockets.
    let home = boxlite_test_utils::home::PerTestBoxHome::new_in("/tmp");
    let home_dir = home.path.clone();
    let original_pid: u32;

    // Create non-detached box
    {
        let runtime = BoxliteRuntime::new(BoxliteOptions {
            home_dir: home_dir.clone(),
            image_registries: common::test_registries(),
        })
        .unwrap();

        let handle = runtime.create(common::alpine_opts(), None).await.unwrap();

        handle
            .exec(BoxCommand::new("sleep").args(["300"]))
            .await
            .unwrap();

        let pf = pid_file_path(&home_dir, handle.id().as_str());
        original_pid = PidFileReader::at(&pf).read().map(|r| r.pid).unwrap();

        // Verify process is running before drop
        assert!(
            is_process_alive(original_pid),
            "Process {} should be alive before runtime drop",
            original_pid
        );

        // Runtime + handler + Keepalive drop here -> POLLHUP -> shim exit
    }

    // Wait for shim to detect POLLHUP and exit gracefully.
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
    while std::time::Instant::now() < deadline {
        if !is_process_alive(original_pid) {
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    }

    // Verify process exited
    assert!(
        !is_process_alive(original_pid),
        "Non-detached box process {} should exit after runtime drop (watchdog POLLHUP)",
        original_pid
    );
}

#[tokio::test]
async fn multiple_detached_boxes_each_have_pid_file() {
    let home = boxlite_test_utils::home::PerTestBoxHome::new();
    let runtime = BoxliteRuntime::new(BoxliteOptions {
        home_dir: home.path.clone(),
        image_registries: common::test_registries(),
    })
    .expect("create runtime");

    let mut box_ids = Vec::new();

    // Create 3 detached boxes
    for _ in 0..3 {
        let handle = runtime
            .create(
                boxlite::runtime::options::BoxOptions {
                    detach: true,
                    ..common::alpine_opts()
                },
                None,
            )
            .await
            .unwrap();

        let _ = handle.exec(BoxCommand::new("sleep").args(["300"])).await;
        box_ids.push(handle.id().to_string());
    }

    // Verify each has unique PID file with different PID
    let mut pids = std::collections::HashSet::new();
    for box_id in &box_ids {
        let pf = pid_file_path(&home.path, box_id);
        assert!(pf.exists(), "Box {} should have PID file", box_id);
        let pid = PidFileReader::at(&pf).read().map(|r| r.pid).unwrap();
        assert!(
            pids.insert(pid),
            "Each box should have unique PID, but {} is duplicate",
            pid
        );
    }

    // Cleanup
    for box_id in box_ids {
        runtime.remove(&box_id, true).await.unwrap();
    }
}

// ============================================================================
// DETACH + RECOVERY TEST
// ============================================================================

#[tokio::test]
async fn detached_box_recoverable_after_restart() {
    let home = boxlite_test_utils::home::PerTestBoxHome::new();
    let box_id: String;

    // Create and run detached box
    {
        let runtime = BoxliteRuntime::new(BoxliteOptions {
            home_dir: home.path.clone(),
            image_registries: common::test_registries(),
        })
        .unwrap();

        let handle = runtime
            .create(
                boxlite::runtime::options::BoxOptions {
                    detach: true,
                    ..common::alpine_opts()
                },
                None,
            )
            .await
            .unwrap();

        let _ = handle.exec(BoxCommand::new("sleep").args(["300"])).await;
        box_id = handle.id().to_string();
    }

    // Create NEW runtime - should recover the box
    {
        let runtime = BoxliteRuntime::new(BoxliteOptions {
            home_dir: home.path.clone(),
            image_registries: common::test_registries(),
        })
        .unwrap();

        // Should recover the box
        let info = runtime
            .get_info(&box_id)
            .await
            .unwrap()
            .expect("Box should be recovered");

        assert_eq!(
            info.status,
            BoxStatus::Running,
            "Box should be recovered as Running"
        );
        assert!(info.pid.is_some(), "Recovered box should have PID");

        // Should be able to stop it
        let handle = runtime.get(&box_id).await.unwrap().unwrap();
        handle.stop().await.unwrap();

        let info = runtime
            .get_info(&box_id)
            .await
            .unwrap()
            .expect("Box should exist");
        assert_eq!(info.status, BoxStatus::Stopped);

        // Cleanup
        runtime.remove(&box_id, false).await.unwrap();
    }
}

/// `runtime.remove(force)` on a detached box must reap its *entire* process
/// tree. `kill_process` only signals the recorded outer bwrap; since #851
/// dropped `--die-with-parent`, the inner pid-ns tree (inner bwrap + shim + VM)
/// survives that. The production fix reaps the box's cgroup. This is the
/// regression guard for the silent orphan VM that `boxlite rm -f` used to leave
/// behind — note `PerTestBoxHome`'s `shim.pid`-based leak check can't see it
/// (force-remove deletes the file), so the explicit `box_proc_count` is the
/// real assertion here.
#[cfg(target_os = "linux")]
#[tokio::test]
async fn detached_box_force_remove_reaps_whole_tree() {
    let home = boxlite_test_utils::home::PerTestBoxHome::new();
    let runtime = BoxliteRuntime::new(BoxliteOptions {
        home_dir: home.path.clone(),
        image_registries: common::test_registries(),
    })
    .expect("create runtime");

    let handle = runtime
        .create(
            boxlite::runtime::options::BoxOptions {
                detach: true,
                ..common::alpine_opts()
            },
            None,
        )
        .await
        .unwrap();
    handle
        .exec(BoxCommand::new("sleep").args(["300"]))
        .await
        .expect("start detached sleep workload");
    let box_id = handle.id().to_string();

    assert!(
        box_proc_count(&box_id) > 0,
        "detached box should have a live process tree after start"
    );

    // Production reap path.
    runtime.remove(&box_id, true).await.unwrap();
    let proc_count = wait_for_box_proc_count_zero(&box_id).await;

    assert_eq!(
        proc_count, 0,
        "force remove must reap the whole detached box tree \
         (outer + inner bwrap + shim + VM), not just the recorded pid"
    );
}

/// `box.stop()` on a detached box must reap the full process tree while
/// preserving the box record and disk state for a later start.
#[cfg(target_os = "linux")]
#[tokio::test]
async fn detached_box_stop_reaps_whole_tree_and_keeps_box() {
    let home = boxlite_test_utils::home::PerTestBoxHome::new();
    let runtime = BoxliteRuntime::new(BoxliteOptions {
        home_dir: home.path.clone(),
        image_registries: common::test_registries(),
    })
    .expect("create runtime");

    let handle = runtime
        .create(
            boxlite::runtime::options::BoxOptions {
                detach: true,
                ..common::alpine_opts()
            },
            None,
        )
        .await
        .unwrap();
    handle
        .exec(BoxCommand::new("sleep").args(["300"]))
        .await
        .expect("start detached sleep workload");
    let box_id = handle.id().to_string();

    assert!(
        box_proc_count(&box_id) > 0,
        "detached box should have a live process tree after start"
    );

    handle.stop().await.unwrap();
    let proc_count = wait_for_box_proc_count_zero(&box_id).await;

    assert_eq!(
        proc_count, 0,
        "stop must reap the whole detached box tree \
         (outer + inner bwrap + shim + VM), not just the recorded pid"
    );

    let info = runtime
        .get_info(&box_id)
        .await
        .unwrap()
        .expect("stopped box should still exist");
    assert_eq!(info.status, BoxStatus::Stopped);

    runtime.remove(&box_id, false).await.unwrap();
}