boxlite 0.9.1

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
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
//! Crash report formatting for user-friendly error messages.
//!
//! Transforms raw [`ExitInfo`] into human-readable crash reports
//! with context-aware troubleshooting suggestions.

use crate::vmm::ExitInfo;
use std::path::Path;

/// Formatted crash report for user-friendly error messages.
///
/// Combines parsed exit information with formatted messages suitable
/// for displaying to users.
#[derive(Debug)]
pub struct CrashReport {
    /// User-friendly error message with troubleshooting hints.
    pub user_message: String,
    /// Raw debug info (stderr content for signals).
    pub debug_info: String,
}

impl CrashReport {
    /// Create a crash report from exit file and context.
    ///
    /// Parses the JSON exit file and formats a user-friendly message
    /// with context-specific troubleshooting suggestions.
    ///
    /// # Arguments
    /// * `exit_file` - Path to the JSON exit file written by shim
    /// * `console_log` - Path to console log (for error message)
    /// * `stderr_file` - Path to stderr file (for error message)
    /// * `box_id` - Box identifier (for error message)
    /// * `exit_code` - Exit code from waitpid (if available)
    pub fn from_exit_file(
        exit_file: &Path,
        console_log: &Path,
        stderr_file: &Path,
        box_id: &str,
        exit_code: Option<i32>,
    ) -> Self {
        let console_display = console_log.display();
        let stderr_display = stderr_file.display();

        // Always read stderr file (contains pre-main dyld errors too)
        let stderr_content = std::fs::read_to_string(stderr_file)
            .unwrap_or_default()
            .trim()
            .to_string();

        // Try to parse JSON exit file
        let Some(info) = ExitInfo::from_file(exit_file) else {
            // No exit file - use exit code and raw stderr content
            return Self::from_raw_exit(
                box_id,
                exit_code,
                &stderr_content,
                console_log,
                stderr_file,
            );
        };

        // Use stderr_content we already read from file (single source of truth)
        let debug_info = stderr_content;

        // Build user-friendly message based on crash type
        let mut user_message = match &info {
            ExitInfo::Signal { signal, .. } => match signal.as_str() {
                "SIGABRT" => format!(
                    "Box {box_id} failed to start: internal error (SIGABRT)\n\n\
                     The VM crashed during initialization.\n\n\
                     Common causes:\n\
                     • Missing or incompatible native libraries\n\
                     • Invalid VM configuration (memory, CPU)\n\
                     • Resource limits exceeded\n\n\
                     Debug files:\n\
                     • Console: {console_display}\n\
                     • Stderr:  {stderr_display}"
                ),
                "SIGSEGV" | "SIGBUS" => format!(
                    "Box {box_id} failed to start: memory error ({signal})\n\n\
                     The VM encountered a memory access error.\n\n\
                     Common causes:\n\
                     • Insufficient memory available\n\
                     • Library version mismatch\n\
                     • Corrupted binary or library\n\n\
                     Debug files:\n\
                     • Console: {console_display}\n\
                     • Stderr:  {stderr_display}"
                ),
                "SIGILL" => format!(
                    "Box {box_id} failed to start: invalid instruction (SIGILL)\n\n\
                     The VM encountered an unsupported CPU instruction.\n\n\
                     Common causes:\n\
                     • CPU compatibility issue\n\
                     • Binary compiled for different architecture\n\n\
                     Debug files:\n\
                     • Console: {console_display}\n\
                     • Stderr:  {stderr_display}"
                ),
                "SIGSYS" => format!(
                    "Box {box_id} failed to start: seccomp violation (SIGSYS)\n\n\
                     The VM was killed by a seccomp filter blocking a required syscall.\n\n\
                     Common causes:\n\
                     • Seccomp filter missing syscalls needed by gvproxy (Go runtime)\n\
                     • Custom seccomp profile too restrictive\n\n\
                     Debug files:\n\
                     • Console: {console_display}\n\
                     • Stderr:  {stderr_display}\n\n\
                     Tip: Run with RUST_LOG=debug or strace to identify the blocked syscall"
                ),
                _ => format!(
                    "Box {box_id} failed to start\n\n\
                     The VM exited unexpectedly during startup.\n\n\
                     Debug files:\n\
                     • Console: {console_display}\n\
                     • Stderr:  {stderr_display}"
                ),
            },
            ExitInfo::Panic {
                message, location, ..
            } => format!(
                "Box {box_id} failed to start: panic\n\n\
                 The shim process panicked during initialization.\n\n\
                 Panic: {message}\n\
                 Location: {location}\n\n\
                 Debug files:\n\
                 • Console: {console_display}\n\
                 • Stderr:  {stderr_display}"
            ),
            ExitInfo::Error { message, .. } => format!(
                "Box {box_id} failed to start: error\n\n\
                 The shim process exited with an error.\n\n\
                 Error: {message}\n\n\
                 Debug files:\n\
                 • Console: {console_display}\n\
                 • Stderr:  {stderr_display}"
            ),
        };

        // Include brief debug info if available (first 5 lines)
        if !debug_info.is_empty() {
            let brief_debug: Vec<&str> = debug_info.lines().take(5).collect();
            user_message.push_str("\n\nError output:\n");
            user_message.push_str(&brief_debug.join("\n"));
            if debug_info.lines().count() > 5 {
                user_message.push_str("\n... (see stderr file for full output)");
            }
        }

        Self {
            user_message,
            debug_info,
        }
    }

    /// Create crash report when no exit file exists (pre-main crash).
    ///
    /// Uses exit code and raw stderr content to provide diagnostic info.
    fn from_raw_exit(
        box_id: &str,
        exit_code: Option<i32>,
        stderr_content: &str,
        console_log: &Path,
        stderr_file: &Path,
    ) -> Self {
        let mut msg = format!("Box {box_id} failed to start\n\n");

        let console_analysis = analyze_console_log(console_log);

        // Add exit code with signal interpretation
        match exit_code {
            Some(0) => {
                msg.push_str("Exit code: 0 (clean shutdown)\n\n");
                msg.push_str(
                    "The VM started but the guest agent exited immediately.\n\
                     Common causes:\n\
                     • Guest binary (boxlite-guest) crashed before producing output\n\
                     • Guest binary not found inside the rootfs\n\
                     • Rootfs disk image corrupted or unmountable\n",
                );
            }
            Some(code) if code > 128 => {
                let signal = code - 128;
                let signal_name = match signal {
                    6 => "SIGABRT",
                    9 => "SIGKILL",
                    11 => "SIGSEGV",
                    15 => "SIGTERM",
                    _ => "unknown signal",
                };
                msg.push_str(&format!("Exit code: {code} ({signal_name})\n"));
            }
            Some(code) => {
                msg.push_str(&format!("Exit code: {code}\n"));
            }
            None => {
                msg.push_str("Exit code: unknown\n");
            }
        }
        msg.push('\n');

        // Add console.log analysis
        match console_analysis {
            ConsoleAnalysis::Empty => {
                msg.push_str("Console output: empty (no kernel or guest messages captured)\n\n");
            }
            ConsoleAnalysis::KernelOnly => {
                msg.push_str(
                    "Console output: kernel messages only (guest agent never started)\n\n",
                );
            }
            ConsoleAnalysis::HasGuestOutput => {
                // Guest started — console.log has useful info, don't add extra annotation
            }
            ConsoleAnalysis::Unreadable => {
                // Can't read the file — skip annotation
            }
        }

        // Add stderr content if available (includes dyld errors)
        if !stderr_content.is_empty() {
            msg.push_str("Shim stderr:\n");
            msg.push_str(&truncate_lines(stderr_content, 15));
            msg.push_str("\n\n");
        }

        msg.push_str(&format!(
            "Debug files:\n\
             • Console: {}\n\
             • Stderr: {}\n\n\
             Diagnostic commands:\n\
             • RUST_LOG=debug boxlite run ...   (re-run with tracing)\n\
             • dmesg | tail -50                 (kernel messages)\n\
             • file $(which boxlite-guest)      (check binary arch)",
            console_log.display(),
            stderr_file.display()
        ));

        Self {
            user_message: msg,
            debug_info: stderr_content.to_string(),
        }
    }
}

/// Result of analyzing the console.log file content.
enum ConsoleAnalysis {
    /// File is empty (0 bytes) — kernel never produced output.
    Empty,
    /// Has kernel messages but no guest agent output.
    KernelOnly,
    /// Guest agent produced output (contains `[guest]` marker).
    HasGuestOutput,
    /// File could not be read.
    Unreadable,
}

/// Analyze console.log to determine what output was captured.
fn analyze_console_log(path: &Path) -> ConsoleAnalysis {
    let metadata = match std::fs::metadata(path) {
        Ok(m) => m,
        Err(_) => return ConsoleAnalysis::Unreadable,
    };

    if metadata.len() == 0 {
        return ConsoleAnalysis::Empty;
    }

    match std::fs::read_to_string(path) {
        Ok(content) => {
            if content.contains("[guest]") {
                ConsoleAnalysis::HasGuestOutput
            } else {
                ConsoleAnalysis::KernelOnly
            }
        }
        Err(_) => ConsoleAnalysis::Unreadable,
    }
}

/// Truncate content to max_lines, showing count of remaining lines.
fn truncate_lines(content: &str, max_lines: usize) -> String {
    let lines: Vec<&str> = content.lines().take(max_lines).collect();
    let truncated = lines.join("\n");
    let total_lines = content.lines().count();
    if total_lines > max_lines {
        format!("{truncated}\n... ({} more lines)", total_lines - max_lines)
    } else {
        truncated
    }
}

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

    #[test]
    fn test_no_exit_file_with_exit_code() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("nonexistent");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        // Create stderr file with content
        std::fs::write(&stderr_file, "dyld: Library not loaded").unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(1),
        );

        assert!(report.user_message.contains("test-box failed to start"));
        assert!(report.user_message.contains("Exit code: 1"));
        assert!(report.user_message.contains("dyld: Library not loaded"));
        assert!(report.user_message.contains("Diagnostic commands"));
    }

    #[test]
    fn test_no_exit_file_with_signal_exit_code() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("nonexistent");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(134), // 128 + 6 (SIGABRT)
        );

        assert!(report.user_message.contains("Exit code: 134 (SIGABRT)"));
    }

    #[test]
    fn test_signal_crash() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("exit");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        // Exit file no longer contains stderr - it's read from stderr_file
        std::fs::write(
            &exit_file,
            r#"{"type":"signal","exit_code":134,"signal":"SIGABRT"}"#,
        )
        .unwrap();
        std::fs::write(&stderr_file, "error details").unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(134),
        );

        assert!(report.user_message.contains("SIGABRT"));
        assert!(report.user_message.contains("internal error"));
        assert_eq!(report.debug_info, "error details");
    }

    #[test]
    fn test_panic_crash() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("exit");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        std::fs::write(
            &exit_file,
            r#"{"type":"panic","exit_code":101,"message":"assertion failed","location":"main.rs:42:5"}"#,
        )
        .unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(101),
        );

        assert!(report.user_message.contains("panic"));
        assert!(report.user_message.contains("assertion failed"));
        assert!(report.user_message.contains("main.rs:42:5"));
    }

    #[test]
    fn test_error_crash() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("exit");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        std::fs::write(
            &exit_file,
            r#"{"type":"error","exit_code":1,"message":"Failed to create VM instance"}"#,
        )
        .unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(1),
        );

        assert!(report.user_message.contains("error"));
        assert!(report.user_message.contains("Failed to create VM instance"));
    }

    #[test]
    fn test_sigsys_crash() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("exit");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        std::fs::write(
            &exit_file,
            r#"{"type":"signal","exit_code":159,"signal":"SIGSYS","stderr":""}"#,
        )
        .unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(159),
        );

        assert!(report.user_message.contains("SIGSYS"));
        assert!(report.user_message.contains("seccomp violation"));
        assert!(report.user_message.contains("strace"));
    }

    #[test]
    fn test_debug_info_truncation() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("exit");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        // Create stderr file with more than 5 lines (stderr is read from file, not exit file)
        let long_stderr = (1..=10)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");

        std::fs::write(
            &exit_file,
            r#"{"type":"signal","exit_code":134,"signal":"SIGABRT"}"#,
        )
        .unwrap();
        std::fs::write(&stderr_file, &long_stderr).unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(134),
        );

        assert!(report.user_message.contains("line 1"));
        assert!(report.user_message.contains("line 5"));
        assert!(
            report
                .user_message
                .contains("... (see stderr file for full output)")
        );
        assert!(!report.user_message.contains("line 6")); // Truncated
    }

    #[test]
    fn test_truncate_lines() {
        let content = "line1\nline2\nline3\nline4\nline5";
        assert_eq!(
            truncate_lines(content, 3),
            "line1\nline2\nline3\n... (2 more lines)"
        );
        assert_eq!(truncate_lines(content, 10), content);
    }

    #[test]
    fn test_exit_code_zero_with_empty_console() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("nonexistent");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        // Empty console.log
        std::fs::write(&console_log, "").unwrap();
        std::fs::write(&stderr_file, "").unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(0),
        );

        assert!(
            report
                .user_message
                .contains("Exit code: 0 (clean shutdown)")
        );
        assert!(
            report
                .user_message
                .contains("guest agent exited immediately")
        );
        assert!(report.user_message.contains("Console output: empty"));
        assert!(report.user_message.contains("Diagnostic commands"));
    }

    #[test]
    fn test_exit_code_zero_with_kernel_only_console() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("nonexistent");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        std::fs::write(&console_log, "Linux version 6.8.0\nBooting kernel...\n").unwrap();
        std::fs::write(&stderr_file, "").unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(0),
        );

        assert!(
            report
                .user_message
                .contains("Exit code: 0 (clean shutdown)")
        );
        assert!(report.user_message.contains("kernel messages only"));
    }

    #[test]
    fn test_exit_code_zero_with_guest_output() {
        let dir = tempfile::tempdir().unwrap();
        let exit_file = dir.path().join("nonexistent");
        let console_log = dir.path().join("console.log");
        let stderr_file = dir.path().join("stderr");

        std::fs::write(&console_log, "[guest] T+0ms: agent starting\n").unwrap();
        std::fs::write(&stderr_file, "").unwrap();

        let report = CrashReport::from_exit_file(
            &exit_file,
            &console_log,
            &stderr_file,
            "test-box",
            Some(0),
        );

        assert!(
            report
                .user_message
                .contains("Exit code: 0 (clean shutdown)")
        );
        // Should NOT contain the empty/kernel annotations
        assert!(!report.user_message.contains("Console output:"));
    }

    #[test]
    fn test_analyze_console_log_empty() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("console.log");
        std::fs::write(&path, "").unwrap();
        assert!(matches!(analyze_console_log(&path), ConsoleAnalysis::Empty));
    }

    #[test]
    fn test_analyze_console_log_kernel_only() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("console.log");
        std::fs::write(&path, "Linux version 6.8.0\n").unwrap();
        assert!(matches!(
            analyze_console_log(&path),
            ConsoleAnalysis::KernelOnly
        ));
    }

    #[test]
    fn test_analyze_console_log_has_guest() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("console.log");
        std::fs::write(&path, "[guest] T+0ms: agent starting\n").unwrap();
        assert!(matches!(
            analyze_console_log(&path),
            ConsoleAnalysis::HasGuestOutput
        ));
    }

    #[test]
    fn test_analyze_console_log_missing() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nonexistent");
        assert!(matches!(
            analyze_console_log(&path),
            ConsoleAnalysis::Unreadable
        ));
    }
}