ktstr/
verifier.rs

1//! BPF verifier log parsing, cycle detection, and output formatting.
2//!
3//! Provides:
4//! - [`VerifierStats`] / [`ProgStats`] / [`DiffRow`] — data types
5//! - [`collect_verifier_output`] — boot VM, collect stats via host introspection
6//! - [`format_verifier_output`] / [`format_verifier_diff`] — text formatting
7//! - [`extract_verifier_log`] — extract verifier trace from libbpf log blob
8//! - [`parse_verifier_stats`] — extract insn/state counts from verifier log
9//! - [`normalize_verifier_line`] — strip variable register state annotations
10//! - [`detect_cycle`] / [`collapse_cycles`] — loop iteration compression
11//! - [`build_b_map`] / [`build_diff_rows`] — A/B comparison helpers
12//! - `SCHED_OUTPUT_START` / `SCHED_OUTPUT_END` — delimiters the
13//!   guest's rust_init emits over the bulk port (as `MSG_TYPE_SCHED_LOG`
14//!   frames) around the scheduler log region;
15//!   `parse_sched_output` extracts the enclosed block
16
17use std::collections::HashMap;
18use std::ffi::OsStr;
19use std::path::{Path, PathBuf};
20
21/// Current schema version for [`VerifierCellOwnershipManifest`].
22pub const VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION: u32 = 1;
23
24/// One parent-elected verifier-cell owner per selected scheduler identity.
25///
26/// Test binaries are the unit nextest lists and executes. When two warmed
27/// binaries link the same exact `declare_scheduler!`, both would otherwise
28/// advertise the same `verifier/<scheduler>/<kernel>/<topology>` name and
29/// race to write its deterministic result record. The parent writes this
30/// manifest after discovery; children use it at both listing and exact
31/// dispatch boundaries.
32#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
33#[serde(deny_unknown_fields)]
34pub struct VerifierCellOwnershipManifest {
35    /// Wire schema version.
36    pub version: u32,
37    /// Full scheduler identity to canonical test-executable mapping.
38    pub entries: Vec<VerifierCellOwnershipEntry>,
39}
40
41/// One selected scheduler declaration and the sole test executable allowed to
42/// list, launch, and record its verifier cells.
43#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
44#[serde(deny_unknown_fields)]
45pub struct VerifierCellOwnershipEntry {
46    /// The complete declaration identity, not merely its cell-name component.
47    pub scheduler: crate::test_support::SchedulerJson,
48    /// Canonical absolute path of the elected warmed test executable.
49    pub executable: PathBuf,
50}
51
52/// Validated ownership view for the current test executable.
53///
54/// Construction validates the entire manifest before any scheduler lookup, so
55/// an ambiguous or partially corrupt manifest cannot selectively fall back to
56/// duplicate cell emission.
57pub(crate) struct VerifierCellOwnership {
58    current_executable: PathBuf,
59    entries: HashMap<String, VerifierCellOwnershipEntry>,
60}
61
62impl VerifierCellOwnership {
63    fn entry_for_scheduler(
64        &self,
65        scheduler_name: &str,
66    ) -> anyhow::Result<&VerifierCellOwnershipEntry> {
67        self.entries.get(scheduler_name).ok_or_else(|| {
68            anyhow::anyhow!(
69                "verifier cell ownership manifest has no entry for selected scheduler {:?}",
70                scheduler_name,
71            )
72        })
73    }
74
75    /// Whether this process is the sole owner of `scheduler`.
76    ///
77    /// A present manifest must contain every scheduler which reaches the
78    /// child's normal emission gates, and its full declaration must match the
79    /// parent-observed identity. Missing or stale entries are therefore hard
80    /// errors rather than implicit ownership.
81    pub(crate) fn owns_scheduler(
82        &self,
83        scheduler: &crate::test_support::SchedulerJson,
84    ) -> anyhow::Result<bool> {
85        let entry = self.entry_for_scheduler(&scheduler.name)?;
86        if !entry.scheduler.eq(scheduler) {
87            anyhow::bail!(
88                "verifier cell ownership declaration mismatch for scheduler {:?}: \
89                 parent recorded {:?}, child linked {:?}",
90                scheduler.name,
91                entry.scheduler,
92                scheduler,
93            );
94        }
95        Ok(entry.executable.eq(&self.current_executable))
96    }
97
98    /// Return the parent-selected full declaration when this process owns it.
99    ///
100    /// Exact dispatch uses this identity to select the matching local
101    /// registration, rather than looking up only the scheduler name and
102    /// accidentally executing an earlier same-name declaration which the
103    /// parent correctly filtered as non-emitting.
104    pub(crate) fn owned_scheduler(
105        &self,
106        scheduler_name: &str,
107    ) -> anyhow::Result<Option<&crate::test_support::SchedulerJson>> {
108        let entry = self.entry_for_scheduler(scheduler_name)?;
109        Ok(entry
110            .executable
111            .eq(&self.current_executable)
112            .then_some(&entry.scheduler))
113    }
114}
115
116fn cell_ownership_from_manifest_path(
117    manifest_path: Option<&OsStr>,
118    current_executable: &Path,
119) -> anyhow::Result<Option<VerifierCellOwnership>> {
120    let Some(manifest_path) = manifest_path else {
121        return Ok(None);
122    };
123    if manifest_path.is_empty() {
124        anyhow::bail!(
125            "{} is set but empty",
126            crate::KTSTR_VERIFIER_CELL_OWNERSHIP_MANIFEST_ENV
127        );
128    }
129    let manifest_path = PathBuf::from(manifest_path);
130    let bytes = std::fs::read(&manifest_path).map_err(|error| {
131        anyhow::anyhow!(
132            "read verifier cell ownership manifest {}: {error}",
133            manifest_path.display()
134        )
135    })?;
136    let manifest: VerifierCellOwnershipManifest =
137        serde_json::from_slice(&bytes).map_err(|error| {
138            anyhow::anyhow!(
139                "parse verifier cell ownership manifest {}: {error}",
140                manifest_path.display()
141            )
142        })?;
143    if manifest.version != VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION {
144        anyhow::bail!(
145            "unsupported verifier cell ownership manifest version {} in {} (expected {})",
146            manifest.version,
147            manifest_path.display(),
148            VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION,
149        );
150    }
151
152    let current_executable = std::fs::canonicalize(current_executable).map_err(|error| {
153        anyhow::anyhow!(
154            "canonicalize current verifier test executable {}: {error}",
155            current_executable.display()
156        )
157    })?;
158    let mut entries = HashMap::with_capacity(manifest.entries.len());
159    for entry in manifest.entries {
160        if !entry.executable.is_absolute() {
161            anyhow::bail!(
162                "verifier cell owner for scheduler {:?} is not absolute: {}",
163                entry.scheduler.name,
164                entry.executable.display(),
165            );
166        }
167        let canonical_owner = std::fs::canonicalize(&entry.executable).map_err(|error| {
168            anyhow::anyhow!(
169                "canonicalize verifier cell owner {} for scheduler {:?}: {error}",
170                entry.executable.display(),
171                entry.scheduler.name,
172            )
173        })?;
174        if canonical_owner.as_path() != entry.executable.as_path() {
175            anyhow::bail!(
176                "verifier cell owner for scheduler {:?} is not canonical: {} resolves to {}",
177                entry.scheduler.name,
178                entry.executable.display(),
179                canonical_owner.display(),
180            );
181        }
182        let name = entry.scheduler.name.clone();
183        if entries.insert(name.clone(), entry).is_some() {
184            anyhow::bail!(
185                "duplicate verifier cell ownership manifest entry for scheduler {name:?}"
186            );
187        }
188    }
189    Ok(Some(VerifierCellOwnership {
190        current_executable,
191        entries,
192    }))
193}
194
195/// Read and validate the parent-owned cell mapping for this test process.
196///
197/// `Ok(None)` means no dispatcher manifest was exported and preserves direct
198/// test-binary behavior. Any present-manifest failure is returned to the
199/// listing/dispatch boundary and must not fall back to shared ownership.
200pub(crate) fn verifier_cell_ownership_from_env() -> anyhow::Result<Option<VerifierCellOwnership>> {
201    let Some(manifest_path) = std::env::var_os(crate::KTSTR_VERIFIER_CELL_OWNERSHIP_MANIFEST_ENV)
202    else {
203        return Ok(None);
204    };
205    let current_executable = std::env::current_exe()
206        .map_err(|error| anyhow::anyhow!("locate current verifier test executable: {error}"))?;
207    cell_ownership_from_manifest_path(Some(manifest_path.as_os_str()), &current_executable)
208}
209
210/// A verifier topology which this guest kernel cannot boot without
211/// changing the topology being verified.
212///
213/// This is intentionally verifier-specific rather than a generic host
214/// insufficiency: ktstr and KVM can represent the requested topology, but
215/// a distro kernel may explicitly disable x2APIC and fall back to the
216/// 8-bit xAPIC limit. The cell is unsupported for that kernel, not a
217/// scheduler failure, and the verifier dispatcher records it as SKIP.
218#[derive(Debug)]
219pub(crate) struct VerifierTopologyUnsupported {
220    pub reason: String,
221}
222
223impl std::fmt::Display for VerifierTopologyUnsupported {
224    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
225        write!(f, "{}", self.reason)
226    }
227}
228
229impl std::error::Error for VerifierTopologyUnsupported {}
230
231/// Delimiter the guest's rust_init emits over the bulk port (as a
232/// `MSG_TYPE_SCHED_LOG` frame) immediately before the scheduler log
233/// block. Paired with [`SCHED_OUTPUT_END`].
234pub(crate) const SCHED_OUTPUT_START: &str = "===SCHED_OUTPUT_START===";
235/// Delimiter the guest's rust_init emits over the bulk port (as a
236/// `MSG_TYPE_SCHED_LOG` frame) immediately after the scheduler log
237/// block. Paired with [`SCHED_OUTPUT_START`].
238pub(crate) const SCHED_OUTPUT_END: &str = "===SCHED_OUTPUT_END===";
239
240/// Extract the scheduler log from guest output between
241/// [`SCHED_OUTPUT_START`] and [`SCHED_OUTPUT_END`]. Returns `None` if
242/// the delimiters are absent or the enclosed content is empty after
243/// trimming.
244///
245/// Uses `find` on the start marker and `rfind` on the end marker: if
246/// the scheduler log itself contains the end sentinel string (e.g. a
247/// stack trace that quotes the marker), `rfind` anchors on the last
248/// occurrence, which is the real terminator emitted by the guest's
249/// post-scenario shutdown path.
250pub(crate) fn parse_sched_output(output: &str) -> Option<&str> {
251    let start = output.find(SCHED_OUTPUT_START)?;
252    let end = output.rfind(SCHED_OUTPUT_END)?;
253    let after_marker = start + SCHED_OUTPUT_START.len();
254    if after_marker >= end {
255        return None;
256    }
257    let content = output[after_marker..end].trim();
258    if content.is_empty() {
259        return None;
260    }
261    Some(content)
262}
263
264/// Concatenate every CRC-valid `MSG_TYPE_SCHED_LOG` chunk in the
265/// bulk-port drain into one `String`, in arrival order.
266///
267/// The guest's `dump_sched_output` emits the `SCHED_OUTPUT_START`
268/// and `SCHED_OUTPUT_END` markers as their own
269/// [`crate::vmm::wire::MsgType::SchedLog`] frames, with the file
270/// content split across one or more intermediate frames. Replaying
271/// the chunks back-to-back reproduces the byte-for-byte stream the
272/// prior COM2 path appended to `output`, so [`parse_sched_output`]
273/// runs unchanged on the result.
274///
275/// Empty / `None` drain yields an empty string.
276pub(crate) fn concat_sched_log_chunks(
277    drain: Option<&crate::vmm::host_comms::BulkDrainResult>,
278) -> String {
279    concat_chunks_of(drain, crate::vmm::wire::MSG_TYPE_SCHED_LOG)
280}
281
282/// Concatenate every CRC-valid `MSG_TYPE_SCHED_STDOUT` chunk in the
283/// bulk-port drain into one `String`, in arrival order.
284///
285/// These are the scheduler child's LIVE stdout chunks (shipped as each
286/// pipe read arrives, not the teardown-only merged file), so they
287/// survive a watchdog timeout that never reaches `dump_sched_output`.
288/// Unlike [`concat_sched_log_chunks`] the payload carries no
289/// `SCHED_OUTPUT_START/END` framing — it is the raw child stream, so
290/// callers use it directly rather than through [`parse_sched_output`].
291/// Empty / `None` drain yields an empty string.
292pub(crate) fn concat_sched_stdout_chunks(
293    drain: Option<&crate::vmm::host_comms::BulkDrainResult>,
294) -> String {
295    concat_chunks_of(drain, crate::vmm::wire::MSG_TYPE_SCHED_STDOUT)
296}
297
298/// Concatenate every CRC-valid `MSG_TYPE_SCHED_STDERR` chunk in the
299/// bulk-port drain into one `String`, in arrival order. The scheduler
300/// child's live stderr stream (where libbpf / log-crate output,
301/// including the BPF verifier log region, typically lands). Same
302/// semantics as [`concat_sched_stdout_chunks`].
303pub(crate) fn concat_sched_stderr_chunks(
304    drain: Option<&crate::vmm::host_comms::BulkDrainResult>,
305) -> String {
306    concat_chunks_of(drain, crate::vmm::wire::MSG_TYPE_SCHED_STDERR)
307}
308
309/// Reassemble every complete authoritative scheduler-stream transaction.
310///
311/// A transaction begins at offset zero and is accepted only when CRC-valid,
312/// well-formed chunks cover one contiguous `0..total_len` range. Interrupted
313/// replay is deliberately invisible to callers: they retain the best-effort
314/// live stream instead. Multiple complete transactions are concatenated in
315/// arrival order for scheduler-lifecycle runs that replace a scheduler inside
316/// one VM.
317fn concat_complete_sched_stream_replays(
318    drain: Option<&crate::vmm::host_comms::BulkDrainResult>,
319    msg_type: u32,
320) -> Option<String> {
321    let drain = drain?;
322    let mut completed = Vec::<u8>::new();
323    let mut transaction = None::<(u64, Vec<u8>)>;
324    let mut saw_complete = false;
325
326    for entry in &drain.entries {
327        if entry.msg_type != msg_type {
328            continue;
329        }
330        if !entry.crc_ok {
331            transaction = None;
332            continue;
333        }
334        let Some((total_len, offset, bytes)) =
335            crate::vmm::wire::decode_sched_stream_final_chunk(&entry.payload)
336        else {
337            transaction = None;
338            continue;
339        };
340        if offset == 0 {
341            transaction = Some((total_len, Vec::new()));
342        }
343        let Some((expected_total, bytes_so_far)) = transaction.as_mut() else {
344            continue;
345        };
346        if *expected_total != total_len
347            || offset != bytes_so_far.len() as u64
348            || (bytes.is_empty() && offset != total_len)
349        {
350            transaction = None;
351            continue;
352        }
353        bytes_so_far.extend_from_slice(bytes);
354        if bytes_so_far.len() as u64 == total_len {
355            completed.extend_from_slice(bytes_so_far);
356            saw_complete = true;
357            transaction = None;
358        }
359    }
360
361    saw_complete.then(|| String::from_utf8_lossy(&completed).into_owned())
362}
363
364/// Select the authoritative terminal replay when one arrived completely;
365/// otherwise preserve the live-stream behavior used by old guests and timeout
366/// paths that never reach terminal publication.
367fn scheduler_stream_with_terminal_fallback(
368    drain: Option<&crate::vmm::host_comms::BulkDrainResult>,
369    live_msg_type: u32,
370    replay_msg_type: u32,
371) -> String {
372    concat_complete_sched_stream_replays(drain, replay_msg_type).unwrap_or_else(|| {
373        match live_msg_type {
374            crate::vmm::wire::MSG_TYPE_SCHED_STDOUT => concat_sched_stdout_chunks(drain),
375            crate::vmm::wire::MSG_TYPE_SCHED_STDERR => concat_sched_stderr_chunks(drain),
376            _ => concat_chunks_of(drain, live_msg_type),
377        }
378    })
379}
380
381/// Concatenate every CRC-valid chunk of one `msg_type` in the drain,
382/// in arrival order. Shared inner for the per-stream concat helpers.
383fn concat_chunks_of(
384    drain: Option<&crate::vmm::host_comms::BulkDrainResult>,
385    msg_type: u32,
386) -> String {
387    let Some(drain) = drain else {
388        return String::new();
389    };
390    let mut acc = String::new();
391    for e in &drain.entries {
392        if e.msg_type != msg_type || !e.crc_ok {
393            continue;
394        }
395        acc.push_str(&String::from_utf8_lossy(&e.payload));
396    }
397    acc
398}
399
400/// Extract scheduler log content even when the closing delimiter is
401/// absent. Tries [`parse_sched_output`] first (well-formed
402/// open+close); on failure, returns the slice from
403/// [`SCHED_OUTPUT_START`] to the end of `output` when only the start
404/// marker is present. Returns `None` only when neither marker is
405/// found or every candidate slice is empty after trimming.
406///
407/// Used by the auto-repro path: a scheduler that crashes mid-run
408/// emits SCHED_OUTPUT_START but never reaches the post-scenario
409/// shutdown that writes SCHED_OUTPUT_END. The partial content still
410/// holds the stack frames the probe pipeline needs to seed kprobe
411/// targets, so discarding it would lose the only crash signal.
412pub(crate) fn parse_sched_output_partial(output: &str) -> Option<&str> {
413    if let Some(content) = parse_sched_output(output) {
414        return Some(content);
415    }
416    let start = output.find(SCHED_OUTPUT_START)?;
417    let after_marker = start + SCHED_OUTPUT_START.len();
418    let content = output[after_marker..].trim();
419    if content.is_empty() {
420        return None;
421    }
422    Some(content)
423}
424
425/// Parsed verifier stats from the kernel log line:
426/// `processed N insns (limit M) max_states_per_insn X total_states Y peak_states Z mark_read W`
427pub struct VerifierStats {
428    /// Instructions processed during verification.
429    pub processed_insns: u64,
430    /// Total explored verifier states.
431    pub total_states: u64,
432    /// Peak concurrent explored states.
433    pub peak_states: u64,
434    /// Stack depth in the format `"<prog>+<subprog>+<main>"` (e.g.
435    /// `"32+16+8"`) when BPF_LOG_STATS emitted a "stack depth" line.
436    pub stack_depth: Option<String>,
437}
438
439/// Per-program verifier statistics collected from a VM run.
440#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
441pub struct ProgStats {
442    /// Program name as registered with the kernel.
443    pub name: String,
444    /// Instructions processed by the verifier (from host-side
445    /// `bpf_prog_aux->verified_insns`).
446    pub verified_insns: u32,
447}
448
449/// A single row in the A/B diff output.
450pub struct DiffRow {
451    /// Program name present in both A and B runs.
452    pub name: String,
453    /// `verified_insns` from the A run.
454    pub a: u64,
455    /// `verified_insns` from the B run.
456    pub b: u64,
457    /// Signed delta (`b - a`); positive means B's verifier cost grew
458    /// relative to A.
459    pub delta: i64,
460}
461
462/// Parse `raw` as `u64`, warning with `field` context on failure.
463/// Returns 0 on parse error. Centralizes the
464/// warn-and-default-to-zero path for the three count words
465/// (`processed_insns`, `total_states`, `peak_states`) so their
466/// error handling stays in lock-step.
467fn parse_or_warn(raw: &str, field: &str) -> u64 {
468    match raw.parse() {
469        Ok(n) => n,
470        Err(e) => {
471            tracing::warn!(
472                field,
473                word = raw,
474                err = %e,
475                "malformed BPF verifier count; leaving 0",
476            );
477            0
478        }
479    }
480}
481
482/// Parse verifier stats from the log output.
483///
484/// The kernel always emits a "processed N insns ..." line. When
485/// BPF_LOG_STATS is set, it may also emit a "stack depth" line.
486/// Verification wall time is deliberately ignored: it is a noisy
487/// environment-dependent measurement and does not belong in verifier
488/// result output.
489pub fn parse_verifier_stats(log: &str) -> VerifierStats {
490    let mut stats = VerifierStats {
491        processed_insns: 0,
492        total_states: 0,
493        peak_states: 0,
494        stack_depth: None,
495    };
496
497    let mut found_insns = false;
498    let mut found_stack = false;
499
500    for line in log.lines().rev() {
501        if !found_insns && line.starts_with("processed ") {
502            found_insns = true;
503            let words: Vec<&str> = line.split_whitespace().collect();
504            if words.len() >= 2 {
505                stats.processed_insns = parse_or_warn(words[1], "processed_insns");
506            }
507            for (i, &w) in words.iter().enumerate() {
508                if w == "total_states"
509                    && let Some(v) = words.get(i + 1)
510                {
511                    stats.total_states = parse_or_warn(v, "total_states");
512                }
513                if w == "peak_states"
514                    && let Some(v) = words.get(i + 1)
515                {
516                    stats.peak_states = parse_or_warn(v, "peak_states");
517                }
518            }
519        }
520        if !found_stack && line.contains("stack depth") {
521            found_stack = true;
522            if let Some(pos) = line.find("stack depth") {
523                let after = &line[pos + "stack depth".len()..];
524                let depth_str = after.trim();
525                if !depth_str.is_empty() {
526                    stats.stack_depth = Some(depth_str.to_string());
527                }
528            }
529        }
530        if found_insns && found_stack {
531            break;
532        }
533    }
534
535    stats
536}
537
538/// Normalize a BPF verifier log line by stripping variable register-state
539/// annotations so that lines from different loop iterations compare equal.
540///
541/// Handles:
542/// - Instruction with `; frame` annotation: `3006: (07) r9 += 1  ; frame1: R9_w=2`
543/// - Instruction with `; R` + digit annotation: `9: (15) if r7 == 0x0 goto pc+1  ; R7=scalar(...)`
544/// - Branch with inline target state: `3026: (b5) if r6 <= 0x11dc0 goto pc+2 3029: frame1: R0=1`
545/// - Standalone register dump with frame: `3041: frame1: R0_w=scalar()`
546/// - Standalone register dump without frame: `3029: R0=1 R6=scalar()`
547///
548/// Preserves source comments (`; for (int j = 0; ...)`) and non-annotation
549/// semicolons (`; Return value`) -- these serve as cycle anchors.
550pub fn normalize_verifier_line(line: &str) -> &str {
551    let trimmed = line.trim();
552    if trimmed.is_empty() || !trimmed.as_bytes()[0].is_ascii_digit() {
553        return trimmed;
554    }
555    // "3041: frame1: ..." or "3041: R0_w=scalar()" — standalone register dump.
556    // State-only lines; keep just the instruction index.
557    if let Some(colon) = trimmed.find(": ") {
558        let after = &trimmed[colon + 2..];
559        if after.starts_with("frame")
560            || (after.starts_with('R')
561                && after.as_bytes().get(1).is_some_and(|b| b.is_ascii_digit()))
562        {
563            return &trimmed[..colon + 1];
564        }
565    }
566    // "; frame" annotation on instruction line
567    if let Some(pos) = trimmed.find("; frame") {
568        return trimmed[..pos].trim_end();
569    }
570    // "; R" followed by digit — register annotation without frame prefix
571    if let Some(pos) = trimmed.find("; R")
572        && trimmed
573            .as_bytes()
574            .get(pos + 3)
575            .is_some_and(|b| b.is_ascii_digit())
576    {
577        return trimmed[..pos].trim_end();
578    }
579    // Inline branch-target state: "goto pc+2 3029: frame1: ..."
580    if let Some(goto_pos) = trimmed.find("goto pc") {
581        let after_goto = &trimmed[goto_pos + 7..];
582        let end = after_goto
583            .find(|c: char| c != '+' && c != '-' && !c.is_ascii_digit())
584            .unwrap_or(after_goto.len());
585        let insn_end = goto_pos + 7 + end;
586        if insn_end < trimmed.len() {
587            return trimmed[..insn_end].trim_end();
588        }
589    }
590    trimmed
591}
592
593/// Normalize for cycle detection: strip register annotations (via
594/// `normalize_verifier_line`) then strip the leading instruction address
595/// (`NNN: `). Unrolled loops place each copy at different addresses, so
596/// the address must be removed for block comparison to find repeats.
597fn normalize_for_cycle_detection(line: &str) -> &str {
598    let n = normalize_verifier_line(line);
599    // Strip leading digits + ": " prefix (e.g. "42: (07) r1 += 8" -> "(07) r1 += 8").
600    if let Some(colon) = n.find(": ") {
601        let before = &n[..colon];
602        if !before.is_empty() && before.bytes().all(|b| b.is_ascii_digit()) {
603            return &n[colon + 2..];
604        }
605    }
606    n
607}
608
609/// Detect a single repeating cycle in a slice of lines.
610///
611/// Returns `Some((start, period, count))` where the cycle begins at
612/// `start`, each iteration is `period` lines, and it repeats `count` times.
613pub fn detect_cycle(lines: &[&str]) -> Option<(usize, usize, usize)> {
614    const MIN_PERIOD: usize = 5;
615    const MIN_REPS: usize = 3;
616
617    if lines.len() < MIN_PERIOD * MIN_REPS {
618        return None;
619    }
620
621    // Two normalization levels:
622    // - anchor_norms: keeps addresses, strips register annotations. Used for
623    //   anchor frequency counting — prevents within-period duplicates at
624    //   different addresses from inflating frequency.
625    // - block_norms: also strips addresses. Used for block equality comparison
626    //   so unrolled loops (same instructions at different addresses) can match.
627    let anchor_norms: Vec<&str> = lines.iter().map(|l| normalize_verifier_line(l)).collect();
628    let block_norms: Vec<&str> = lines
629        .iter()
630        .map(|l| normalize_for_cycle_detection(l))
631        .collect();
632
633    // Find most frequent non-trivial anchor-normalized line.
634    let mut sorted_norms: Vec<&str> = anchor_norms
635        .iter()
636        .filter(|l| l.len() >= 10)
637        .copied()
638        .collect();
639    sorted_norms.sort_unstable();
640
641    let mut best_anchor: Option<(&str, usize)> = None;
642    let mut i = 0;
643    while i < sorted_norms.len() {
644        let mut j = i + 1;
645        while j < sorted_norms.len() && sorted_norms[j] == sorted_norms[i] {
646            j += 1;
647        }
648        let count = j - i;
649        if count >= MIN_REPS && best_anchor.is_none_or(|(_, best)| count > best) {
650            best_anchor = Some((sorted_norms[i], count));
651        }
652        i = j;
653    }
654
655    // If address-preserving anchor search found nothing (unrolled loops
656    // where every address is unique), fall back to address-stripped norms.
657    let (anchor, use_block_norms_for_positions) = match best_anchor {
658        Some((a, _)) => (a, false),
659        None => {
660            let mut sorted_block: Vec<&str> = block_norms
661                .iter()
662                .filter(|l| l.len() >= 10)
663                .copied()
664                .collect();
665            sorted_block.sort_unstable();
666            let mut ba: Option<(&str, usize)> = None;
667            let mut bi = 0;
668            while bi < sorted_block.len() {
669                let mut bj = bi + 1;
670                while bj < sorted_block.len() && sorted_block[bj] == sorted_block[bi] {
671                    bj += 1;
672                }
673                let c = bj - bi;
674                if c >= MIN_REPS && ba.is_none_or(|(_, best)| c > best) {
675                    ba = Some((sorted_block[bi], c));
676                }
677                bi = bj;
678            }
679            match ba {
680                Some((a, _)) => (a, true),
681                None => return None,
682            }
683        }
684    };
685
686    let norms_for_pos = if use_block_norms_for_positions {
687        &block_norms
688    } else {
689        &anchor_norms
690    };
691    let positions: Vec<usize> = norms_for_pos
692        .iter()
693        .enumerate()
694        .filter(|(_, l)| **l == anchor)
695        .map(|(i, _)| i)
696        .collect();
697
698    // Try strides 1..=3 to handle anchors appearing K times per cycle.
699    for stride in 1..=3usize {
700        if positions.len() <= stride {
701            continue;
702        }
703
704        let mut gaps: Vec<usize> = positions
705            .windows(stride + 1)
706            .map(|w| w[stride] - w[0])
707            .filter(|g| *g >= MIN_PERIOD)
708            .collect();
709        gaps.sort_unstable();
710
711        let mut best_period = 0;
712        let mut best_gap_count = 0;
713        let mut gi = 0;
714        while gi < gaps.len() {
715            let mut gj = gi + 1;
716            while gj < gaps.len() && gaps[gj] == gaps[gi] {
717                gj += 1;
718            }
719            let count = gj - gi;
720            if count > best_gap_count {
721                best_gap_count = count;
722                best_period = gaps[gi];
723            }
724            gi = gj;
725        }
726        if best_period == 0 || best_gap_count < MIN_REPS - 1 {
727            continue;
728        }
729        let period = best_period;
730
731        for &pos in &positions {
732            if pos + 2 * period > lines.len() {
733                break;
734            }
735            if block_norms[pos..pos + period] == block_norms[pos + period..pos + 2 * period] {
736                let first_block = &block_norms[pos..pos + period];
737                let mut count = 1;
738                while pos + (count + 1) * period <= lines.len() {
739                    if block_norms[pos + count * period..pos + (count + 1) * period] != *first_block
740                    {
741                        break;
742                    }
743                    count += 1;
744                }
745                // Try earlier starts to find best alignment.
746                let mut best_start = pos;
747                let mut best_count = count;
748                for offset in 1..period {
749                    let Some(cand) = pos.checked_sub(offset) else {
750                        break;
751                    };
752                    if cand + 2 * period > lines.len() {
753                        continue;
754                    }
755                    if block_norms[cand..cand + period]
756                        != block_norms[cand + period..cand + 2 * period]
757                    {
758                        continue;
759                    }
760                    let mut c = 2;
761                    while cand + (c + 1) * period <= lines.len()
762                        && block_norms[cand + c * period..cand + (c + 1) * period]
763                            == block_norms[cand..cand + period]
764                    {
765                        c += 1;
766                    }
767                    if c > best_count {
768                        best_start = cand;
769                        best_count = c;
770                    }
771                }
772                if best_count >= MIN_REPS {
773                    return Some((best_start, period, best_count));
774                }
775            }
776        }
777    }
778
779    None
780}
781
782/// Collapse repeating cycles in a verifier log.
783///
784/// Runs cycle detection iteratively (up to 5 passes for nested loops).
785/// Each cycle is replaced with:
786/// - `--- Nx of the following M lines ---` (count header, no closing marker)
787/// - first iteration (with original register annotations)
788/// - `--- K identical iterations omitted ---` (omission marker)
789/// - last iteration (with original register annotations)
790/// - `--- end repeat ---` (closes the omission)
791pub fn collapse_cycles(log: &str) -> String {
792    const MAX_PASSES: usize = 5;
793    let mut text = log.to_string();
794
795    for _ in 0..MAX_PASSES {
796        let lines: Vec<&str> = text.lines().collect();
797        let (start, period, count) = match detect_cycle(&lines) {
798            Some(c) => c,
799            None => break,
800        };
801
802        let mut out = String::new();
803        for line in &lines[..start] {
804            out.push_str(line);
805            out.push('\n');
806        }
807        out.push_str(&format!(
808            "--- {}x of the following {} lines ---\n",
809            count, period
810        ));
811        for line in &lines[start..start + period] {
812            out.push_str(line);
813            out.push('\n');
814        }
815        out.push_str(&format!(
816            "--- {} identical iterations omitted ---\n",
817            count - 2
818        ));
819        let last_start = start + (count - 1) * period;
820        for line in &lines[last_start..last_start + period] {
821            out.push_str(line);
822            out.push('\n');
823        }
824        out.push_str("--- end repeat ---\n");
825        let suffix_start = start + count * period;
826        for line in &lines[suffix_start..] {
827            out.push_str(line);
828            out.push('\n');
829        }
830        text = out;
831    }
832
833    text
834}
835
836/// Build diff rows from A stats and B lookup map.
837pub fn build_diff_rows(stats_a: &[ProgStats], b_map: &HashMap<String, u64>) -> Vec<DiffRow> {
838    let mut rows = Vec::new();
839    for ps in stats_a {
840        let a = ps.verified_insns as u64;
841        let b = b_map.get(&ps.name).copied().unwrap_or(0);
842        rows.push(DiffRow {
843            name: ps.name.clone(),
844            a,
845            b,
846            delta: a as i64 - b as i64,
847        });
848    }
849    rows
850}
851
852/// Build the B-side lookup map from collected stats.
853pub fn build_b_map(stats_b: &[ProgStats]) -> HashMap<String, u64> {
854    stats_b
855        .iter()
856        .map(|ps| (ps.name.clone(), ps.verified_insns as u64))
857        .collect()
858}
859
860// ---------------------------------------------------------------------------
861// VM-based verifier collection
862// ---------------------------------------------------------------------------
863
864/// Whether the scheduler positively confirmed it turned on during a
865/// verifier VM run.
866///
867/// The guest init's attach gate (`poll_startup` + `poll_scx_attached`
868/// in `crate::vmm::rust_init::scheduler`) already runs on every verifier
869/// VM boot: it confirms the scheduler process survived BPF load AND that
870/// `/sys/kernel/sched_ext/state` reached `enabled`. The kernel sets
871/// `enabled` only after `ops.init`, per-task init, and switching
872/// eligible tasks to the sched_ext class (`kernel/sched/ext.c`
873/// `scx_root_enable_workfn`), so `enabled` proves the scheduler turned
874/// on and is scheduling — not merely that its BPF loaded.
875///
876/// The verdict is POSITIVE-confirmation, not absence-of-failure: a
877/// verifier cell PASSes only when the guest emitted the definitive
878/// `SchedulerAttached` lifecycle frame. The guest emits that frame at the
879/// one site where the scheduler child is alive and sched_ext reached
880/// `enabled`. In particular, `PayloadStarting` is not attach evidence:
881/// Phase 5 emits it for schedulerless runs too. Treating it as evidence
882/// lets a schedulerless verifier workload running under the kernel's
883/// fallback policy false-PASS.
884///
885/// On attach failure the guest emits `SchedulerDied` /
886/// `SchedulerNotAttached`; a guest that vanishes without any conclusive
887/// frame — e.g. a kernel panic which reboots via `panic=-1` — is
888/// [`AttachOutcome::Unconfirmed`], also a FAIL. [`collect_verifier_output`]
889/// consumes this verdict instead of discarding it. Scheduler-agnostic
890/// (kernel sysfs state), so it holds for every declared scheduler.
891#[derive(Debug, Clone, PartialEq, Eq)]
892pub enum AttachOutcome {
893    /// The guest emitted `SchedulerAttached` with no failure frame: the
894    /// scheduler loaded, stayed alive, and reached sched_ext `enabled`.
895    Attached,
896    /// Scheduler process exited during BPF load / startup
897    /// (`LifecyclePhase::SchedulerDied`).
898    Died,
899    /// Scheduler stayed alive but never reached `enabled`
900    /// (`LifecyclePhase::SchedulerNotAttached`); carries the guest's
901    /// reason suffix when present.
902    NotAttached(String),
903    /// No failure frame AND no `SchedulerAttached` frame: attach was never
904    /// positively confirmed. This includes an early guest kernel panic
905    /// and a schedulerless run which proceeds to `PayloadStarting`.
906    Unconfirmed,
907}
908
909impl AttachOutcome {
910    /// Human-readable failure reason, or `None` when attached.
911    pub fn failure_reason(&self) -> Option<String> {
912        match self {
913            AttachOutcome::Attached => None,
914            AttachOutcome::Died => {
915                Some("scheduler process exited during BPF load/startup".to_string())
916            }
917            AttachOutcome::NotAttached(reason) if reason.is_empty() => {
918                Some("scheduler never reached sched_ext 'enabled'".to_string())
919            }
920            AttachOutcome::NotAttached(reason) => Some(format!(
921                "scheduler never reached sched_ext 'enabled': {reason}"
922            )),
923            AttachOutcome::Unconfirmed => Some(
924                "scheduler attach unconfirmed \
925                 (no SchedulerAttached frame; scheduler may be absent or guest may have crashed)"
926                    .to_string(),
927            ),
928        }
929    }
930}
931
932/// Derive the [`AttachOutcome`] from a VM run's bulk-port lifecycle
933/// frames using a POSITIVE-confirmation rule:
934/// - a `SchedulerDied` frame ⇒ [`AttachOutcome::Died`] (wins outright —
935///   a process that exited cannot have attached);
936/// - else a `SchedulerNotAttached` frame ⇒ [`AttachOutcome::NotAttached`]
937///   (with its reason suffix);
938/// - else a `SchedulerAttached` frame ⇒ [`AttachOutcome::Attached`];
939/// - else [`AttachOutcome::Unconfirmed`] — no failure AND no positive
940///   frame, so attach was never confirmed (including a schedulerless run
941///   which nevertheless reaches `PayloadStarting`).
942///
943/// Corrupt frames (`crc_ok == false`) and empty payloads are skipped. A
944/// `None` `guest_messages` (no frames at all) is
945/// [`AttachOutcome::Unconfirmed`].
946pub(crate) fn attach_outcome_from_messages(
947    guest_messages: Option<&crate::vmm::host_comms::BulkDrainResult>,
948) -> AttachOutcome {
949    let Some(drain) = guest_messages else {
950        return AttachOutcome::Unconfirmed;
951    };
952    let mut not_attached: Option<String> = None;
953    let mut scheduler_attached = false;
954    for e in &drain.entries {
955        if e.msg_type != crate::vmm::wire::MSG_TYPE_LIFECYCLE || !e.crc_ok || e.payload.is_empty() {
956            continue;
957        }
958        match crate::vmm::wire::LifecyclePhase::from_wire(e.payload[0]) {
959            Some(crate::vmm::wire::LifecyclePhase::SchedulerDied) => return AttachOutcome::Died,
960            Some(crate::vmm::wire::LifecyclePhase::SchedulerNotAttached) => {
961                not_attached = Some(String::from_utf8_lossy(&e.payload[1..]).into_owned());
962            }
963            Some(crate::vmm::wire::LifecyclePhase::SchedulerAttached) => {
964                scheduler_attached = true;
965            }
966            _ => {}
967        }
968    }
969    if let Some(reason) = not_attached {
970        AttachOutcome::NotAttached(reason)
971    } else if scheduler_attached {
972        AttachOutcome::Attached
973    } else {
974        AttachOutcome::Unconfirmed
975    }
976}
977
978/// Whether the guest confirmed workload dispatch: at least one
979/// `WorkloadDispatched` lifecycle frame (crc-ok, non-empty payload) in
980/// the run's bulk-port frames. Emitted by `ktstr_guest_init` Phase 5 only
981/// when the injected SpinWait workload recorded a worker with non-zero
982/// `iterations` under a confirmed SCHED_EXT policy (`sched_policy_error`
983/// is None). Combined with a definitive `SchedulerAttached` frame, this
984/// is a positive, scheduler-agnostic proof the scheduler dispatched a
985/// task onto a CPU. Alone it is not attach proof: schedulerless SCHED_EXT
986/// tasks may make progress through the kernel fallback.
987/// Corrupt frames (`crc_ok == false`) and empty payloads are skipped. A
988/// `None` `guest_messages` (no frames at all) is `false`.
989fn dispatch_confirmed_from_messages(
990    guest_messages: Option<&crate::vmm::host_comms::BulkDrainResult>,
991) -> bool {
992    let Some(drain) = guest_messages else {
993        return false;
994    };
995    drain.entries.iter().any(|e| {
996        e.msg_type == crate::vmm::wire::MSG_TYPE_LIFECYCLE
997            && e.crc_ok
998            && !e.payload.is_empty()
999            && crate::vmm::wire::LifecyclePhase::from_wire(e.payload[0])
1000                == Some(crate::vmm::wire::LifecyclePhase::WorkloadDispatched)
1001    })
1002}
1003
1004/// Whether the guest's scheduler-exit monitor observed the scheduler child
1005/// exiting unexpectedly. Unlike `SchedulerDied`, which is a startup
1006/// lifecycle outcome, `MSG_TYPE_SCHED_EXIT` covers a scheduler that attached
1007/// and then died while the verifier workload was running. A crc-invalid frame
1008/// is not evidence.
1009fn scheduler_exited_from_messages(
1010    guest_messages: Option<&crate::vmm::host_comms::BulkDrainResult>,
1011) -> bool {
1012    let Some(drain) = guest_messages else {
1013        return false;
1014    };
1015    drain
1016        .entries
1017        .iter()
1018        .any(|e| e.msg_type == crate::vmm::wire::MSG_TYPE_SCHED_EXIT && e.crc_ok)
1019}
1020
1021/// Decode the verifier guest's explicit terminal exit frame.
1022///
1023/// This deliberately does not use `VmResult::exit_code`: the BSP run loop
1024/// assigns zero to a generic guest shutdown even when no `MSG_TYPE_EXIT`
1025/// arrived. Verifier success needs positive terminal evidence, so only a
1026/// crc-valid four-byte frame written by `guest_comms::send_exit` counts.
1027fn terminal_guest_exit_from_messages(
1028    guest_messages: Option<&crate::vmm::host_comms::BulkDrainResult>,
1029) -> Option<i32> {
1030    let drain = guest_messages?;
1031    drain
1032        .entries
1033        .iter()
1034        .rev()
1035        .find(|e| e.msg_type == crate::vmm::wire::MSG_TYPE_EXIT && e.crc_ok && e.payload.len() == 4)
1036        .map(|e| i32::from_le_bytes(e.payload[..4].try_into().unwrap()))
1037}
1038
1039/// Result of collecting verifier output from a VM run.
1040pub struct VerifierVmResult {
1041    /// Per-program verifier statistics from host-side memory
1042    /// introspection (`bpf_prog_aux->verified_insns`).
1043    pub stats: Vec<ProgStats>,
1044    /// Scheduler log (merged stdout+stderr) from the VM's teardown
1045    /// `dump_sched_output` (the `/tmp/sched.log` file bracketed by the
1046    /// `SCHED_OUTPUT_START/END` markers). Contains libbpf's verifier
1047    /// instruction traces when BPF load fails. Present only when the run
1048    /// reached a dump path (teardown / scheduler-exit / boot-failure);
1049    /// a watchdog timeout kills the VM before any dump, leaving this
1050    /// empty — [`Self::scheduler_stdout`] / [`Self::scheduler_stderr`]
1051    /// are the live-streamed fallbacks that survive that case.
1052    pub scheduler_log: String,
1053    /// The scheduler child's stdout. Normally concatenated directly from the
1054    /// live `MSG_TYPE_SCHED_STDOUT` frames shipped per pipe read. When the
1055    /// guest detects that a live frame was suppressed or failed, a complete
1056    /// terminal replay replaces that partial live stream only after its
1057    /// offsets prove every byte arrived. Watchdog-killed and older guests keep
1058    /// the best-effort live behavior. Raw child stream — no
1059    /// `SCHED_OUTPUT_START/END` framing.
1060    pub scheduler_stdout: String,
1061    /// The scheduler child's stderr, using the same live-first,
1062    /// completion-proven terminal fallback semantics as
1063    /// [`Self::scheduler_stdout`]. Libbpf / log-crate output (including the
1064    /// BPF verifier log region between the `-- BEGIN/END PROG LOAD LOG --`
1065    /// markers) typically lands here.
1066    pub scheduler_stderr: String,
1067    /// Whether the scheduler positively confirmed attach. Derived from
1068    /// the guest's lifecycle frames ([`AttachOutcome`]). Attach is
1069    /// necessary but NOT sufficient for a cell PASS: this must be
1070    /// [`AttachOutcome::Attached`] (the guest emitted the definitive
1071    /// `SchedulerAttached` frame) AND [`Self::dispatched`] must be true.
1072    /// Verification alone (non-empty `stats`) is not enough — a scheduler
1073    /// whose BPF loads but never reaches sched_ext `enabled`, or a guest
1074    /// that vanishes before attach, is a real failure.
1075    pub attach: AttachOutcome,
1076    /// Whether the guest confirmed the injected verifier workload
1077    /// dispatched — a `WorkloadDispatched` lifecycle frame, emitted by
1078    /// `ktstr_guest_init` Phase 5 when the SpinWait probe recorded a
1079    /// worker with non-zero `iterations` after requesting SCHED_EXT
1080    /// policy. This progress signal is not independent attach proof:
1081    /// schedulerless SCHED_EXT tasks may run through the kernel fallback.
1082    /// A cell PASSes only when this is true AND [`Self::attach`] is
1083    /// [`AttachOutcome::Attached`]: a scheduler that turns on (sched_ext
1084    /// `enabled`) but never dispatches a runnable task is a real, distinct
1085    /// failure — worse than never attaching — that the attach verdict
1086    /// alone cannot catch. Derived from the run's lifecycle frames;
1087    /// scheduler-agnostic — the probe runs as SCHED_EXT, so the BPF
1088    /// scheduler dispatches it under any switch mode (full or
1089    /// `SCX_OPS_SWITCH_PARTIAL`) and non-zero worker progress proves
1090    /// dispatch, unlike an scx-specific `nr_dispatched` counter.
1091    pub dispatched: bool,
1092    /// The guest scheduler-exit monitor observed the scheduler process
1093    /// disappear after startup. This is independent of the historical
1094    /// `SchedulerAttached` frame: attach-at-t1 followed by exit-at-t2 is a
1095    /// failure even if SCHED_EXT fallback later lets a probe worker run.
1096    pub scheduler_exited: bool,
1097    /// Explicit terminal verifier guest exit code from a crc-valid
1098    /// `MSG_TYPE_EXIT` frame. The verifier guest emits 0 only after worker
1099    /// progress, a live scheduler child, and sched_ext `enabled` are observed
1100    /// at the same completion edge. `None` is a failure: a generic VM
1101    /// shutdown is not terminal verifier evidence.
1102    pub guest_exit_code: Option<i32>,
1103    /// The host watchdog fired (hard-deadline hang) before the guest
1104    /// exited. NOT orthogonal to [`Self::attach`] in the way the message
1105    /// once implied: a hang can occur EITHER after attach (a teardown
1106    /// wedge, leaving `attach == Attached`) OR before it (a scheduler
1107    /// that died / never reached `enabled` DURING BPF load and then
1108    /// wedged without rebooting, leaving `attach` at
1109    /// [`AttachOutcome::Died`] / [`AttachOutcome::NotAttached`] /
1110    /// [`AttachOutcome::Unconfirmed`]). An early kernel panic is the one
1111    /// case this flag does NOT cover: `panic=-1` reboots via an i8042
1112    /// reset (`ExitAction::Shutdown`, `timed_out == false`), caught by
1113    /// the attach gate as [`AttachOutcome::Unconfirmed`] instead. Because
1114    /// the hang is not attach-implying, [`Self::cell_verdict`] words the
1115    /// timeout message per [`Self::attach`] rather than always claiming
1116    /// "after attach". A verifier cell FAILs on a hang.
1117    pub timed_out: bool,
1118    /// The guest's structured crash message (a `PANIC:`-prefixed line
1119    /// routed through the host's `extract_panic_message` into
1120    /// [`crate::vmm::VmResult::crash_message`]), if any. A self-describing
1121    /// guest infra fault — most notably the AP-bring-up gap the boot
1122    /// retry exhausts — that [`Self::cell_verdict`] surfaces ABOVE the
1123    /// attach/dispatch gates, mirroring `test_support::eval`'s
1124    /// crash_message priority. `None` on the common no-crash path.
1125    pub crash_message: Option<String>,
1126    /// Host-side vCPU scheduling dilation for this cell's VM run —
1127    /// `D = 1 + Σrun_delay/Σon_cpu` over the vCPU host threads (see
1128    /// `vmm::result::HostVcpuSchedstat`). `None` on hosts
1129    /// without `CONFIG_SCHEDSTATS` or when no vCPU thread was sampled.
1130    /// Purely observational: surfaced in the per-cell output but NEVER
1131    /// folded into [`Self::cell_verdict`] or the exit code.
1132    pub dilation: Option<f64>,
1133}
1134
1135impl VerifierVmResult {
1136    /// The verifier cell PASS/FAIL verdict: `Ok(())` when the scheduler
1137    /// verified its BPF, attached (sched_ext `enabled`), AND dispatched
1138    /// the injected workload; `Err(reason)` naming the first failing gate
1139    /// otherwise. Gate order is timeout, guest crash, observed scheduler
1140    /// exit, attach, dispatch, then explicit terminal guest exit. Root cause
1141    /// is reported first: an attach failure is named before the dispatch
1142    /// gate it necessarily also trips, and a terminal guest exit of 0 is
1143    /// required after all positive gates.
1144    pub fn cell_verdict(&self) -> Result<(), String> {
1145        // A hard-deadline hang, reported FIRST (outranks crash_message
1146        // and the attach/dispatch gates). The hang is NOT proof of a
1147        // post-attach wedge: the watchdog also fires when the scheduler
1148        // died / never reached `enabled` during BPF load and the guest
1149        // then wedged without rebooting. So keep the exact historical
1150        // "after attach" wording ONLY when attach was positively
1151        // confirmed; otherwise say the timeout carried no confirmed
1152        // attach and fold in the attach failure reason. Both arms keep
1153        // the "timed out" substring the gate-order test asserts.
1154        if self.timed_out {
1155            return Err(match self.attach {
1156                AttachOutcome::Attached => {
1157                    "VM timed out (hung after attach, before exit)".to_string()
1158                }
1159                _ => match self.attach.failure_reason() {
1160                    Some(reason) => {
1161                        format!("VM timed out (hung with no confirmed scheduler attach — {reason})")
1162                    }
1163                    None => "VM timed out (hung with no confirmed scheduler attach)".to_string(),
1164                },
1165            });
1166        }
1167        // A self-describing guest infra fault outranks the attach/dispatch
1168        // gates: the guest PID-1 panicked before the scheduler ran (most
1169        // notably the AP-bring-up gap the boot retry could not clear), so
1170        // "scheduler did not turn on" would misattribute an infra failure
1171        // to the scheduler. Surface the crash verbatim, mirroring
1172        // `test_support::eval`'s crash_message priority.
1173        if let Some(crash) = &self.crash_message {
1174            return Err(crash.clone());
1175        }
1176        if self.scheduler_exited {
1177            return Err("scheduler exited during the verifier workload".to_string());
1178        }
1179        // PASS requires the scheduler to have turned ON, not just to have
1180        // loaded + verified its BPF.
1181        if let Some(reason) = self.attach.failure_reason() {
1182            return Err(format!("scheduler did not turn on — {reason}"));
1183        }
1184        // PASS also requires DISPATCH: the guest injects a SpinWait
1185        // workload after attach and only emits WorkloadDispatched when a
1186        // SCHED_EXT worker makes forward progress. A scheduler that turns
1187        // on but never dispatches a runnable task is a distinct, worse
1188        // failure the attach gate can't catch.
1189        if !self.dispatched {
1190            return Err(
1191                "scheduler attached but did not dispatch the injected workload (0 iterations)"
1192                    .to_string(),
1193            );
1194        }
1195        match self.guest_exit_code {
1196            Some(0) => {}
1197            Some(code) => {
1198                return Err(format!(
1199                    "verifier guest did not complete successfully (exit code {code})"
1200                ));
1201            }
1202            None => {
1203                return Err("verifier guest did not publish a terminal exit status".to_string());
1204            }
1205        }
1206        Ok(())
1207    }
1208}
1209
1210/// Complete scheduler-owned launch environment for one verifier VM.
1211///
1212/// Generated cells build this from the linked [`crate::test_support::Scheduler`]
1213/// declaration so verifier and ordinary test boots agree on every
1214/// scheduler-wide input which can affect startup or BPF verification. Direct
1215/// API callers retain their historical explicit-CLI-only behavior through
1216/// [`Self::direct`].
1217#[derive(Debug, Clone, PartialEq, Eq)]
1218pub(crate) struct VerifierSchedulerLaunchPlan {
1219    sched_args: Vec<String>,
1220    cmdline_extra: String,
1221    include_files: Vec<(String, PathBuf)>,
1222    scheduler_cgroup_parent: Option<String>,
1223}
1224
1225impl VerifierSchedulerLaunchPlan {
1226    fn direct(extra_sched_args: &[String]) -> Self {
1227        Self {
1228            sched_args: extra_sched_args.to_vec(),
1229            cmdline_extra: String::new(),
1230            include_files: Vec::new(),
1231            scheduler_cgroup_parent: None,
1232        }
1233    }
1234
1235    pub(crate) fn from_scheduler(scheduler: &crate::test_support::Scheduler) -> Self {
1236        let mut sched_args = Vec::new();
1237        let mut include_files = Vec::new();
1238        if let Some((archive_path, host_path, guest_path)) =
1239            crate::test_support::runtime::scheduler_config_file_parts(scheduler)
1240        {
1241            include_files.push((archive_path, host_path));
1242            sched_args.push("--config".to_string());
1243            sched_args.push(guest_path);
1244        }
1245        sched_args.extend(scheduler.sched_args.iter().map(|arg| arg.to_string()));
1246
1247        Self {
1248            sched_args,
1249            cmdline_extra: crate::test_support::runtime::scheduler_cmdline_tokens(scheduler)
1250                .join(" "),
1251            include_files,
1252            scheduler_cgroup_parent: scheduler
1253                .cgroup_parent
1254                .map(|parent| parent.as_str().to_string()),
1255        }
1256    }
1257}
1258
1259/// Boot a VM and collect verifier statistics via host-side memory
1260/// introspection. Per-program `verified_insns` comes from
1261/// `bpf_prog_aux->verified_insns` read through the guest's physical
1262/// memory. On load failure, libbpf prints the verifier log to stderr;
1263/// the returned `scheduler_log` field contains the scheduler's captured
1264/// output from the VM.
1265///
1266/// `topology` selects the verifier VM's emulated topology via
1267/// [`crate::test_support::TopologyJson`] — the same named-field
1268/// shape carried in the per-scheduler `--ktstr-list-schedulers`
1269/// JSON. The named fields force callers to spell every dimension
1270/// at the call site, preventing position-swap misorders. The sole
1271/// caller is the sweep cell handler (`crate::test_support::dispatch`'s
1272/// `run_verifier_cell`), which passes the topology of the gauntlet
1273/// preset named in the cell — the verifier sweeps each scheduler across
1274/// every preset its constraints accept under no_perf_mode.
1275pub fn collect_verifier_output(
1276    sched_bin: &std::path::Path,
1277    ktstr_bin: &std::path::Path,
1278    kernel: &std::path::Path,
1279    extra_sched_args: &[String],
1280    topology: crate::vmm::topology::Topology,
1281    forced_cpu_budget: Option<u32>,
1282) -> anyhow::Result<VerifierVmResult> {
1283    let memory_min_mib = crate::test_support::runtime::cpu_scaled_memory_mib(topology.total_cpus());
1284    let launch = VerifierSchedulerLaunchPlan::direct(extra_sched_args);
1285    collect_verifier_output_with_memory_min(
1286        sched_bin,
1287        ktstr_bin,
1288        kernel,
1289        &launch,
1290        topology,
1291        memory_min_mib,
1292        forced_cpu_budget,
1293    )
1294}
1295
1296/// Preset-sweep implementation with an explicit deferred-memory floor.
1297///
1298/// The public direct API above retains its historical CPU-scaled behavior.
1299/// The preset dispatcher passes the topology preset's capped floor so large
1300/// synthetic topologies do not inflate guest RAM independently of the
1301/// preset's declared budget.
1302pub(crate) fn collect_verifier_output_with_memory_min(
1303    sched_bin: &std::path::Path,
1304    ktstr_bin: &std::path::Path,
1305    kernel: &std::path::Path,
1306    launch: &VerifierSchedulerLaunchPlan,
1307    topology: crate::vmm::topology::Topology,
1308    memory_min_mib: u32,
1309    forced_cpu_budget: Option<u32>,
1310) -> anyhow::Result<VerifierVmResult> {
1311    use anyhow::Context;
1312
1313    // Take the Topology directly (not the lossy TopologyJson wire shape,
1314    // which drops `nodes` / `distances` / `llc_cores`): a preset like
1315    // 192cpu-11llc-smt carries per-LLC core counts that MUST reach the guest
1316    // CPUID synthesis, so round-tripping through TopologyJson here would
1317    // silently boot a uniform shape instead. Validate directly so a bad
1318    // topology surfaces a clean error instead of the builder's panic.
1319    let validated = topology;
1320    validated
1321        .validate()
1322        .map_err(|e| anyhow::anyhow!("invalid topology {validated:?}: {e}"))?;
1323
1324    // The verifier only loads the scheduler's BPF and reads the kernel
1325    // verifier's load-time `verified_insns` counts via host-side
1326    // introspection — a value fixed at BPF load, wholly INDEPENDENT of
1327    // perf-mode tuning (CPU pinning, RT priority, hugepages, NUMA mbind,
1328    // KVM exit suppression). So the verifier VM ALWAYS runs with
1329    // performance mode disabled: it needs none of that tuning. Disabling
1330    // perf mode also routes the run through the no-perf plan, which reserves
1331    // a shared (`LOCK_SH`) subset of LLCs and admission permits atomically.
1332    // `LOCK_SH` holders are
1333    // mutually compatible, so parallel verifier / no-perf cells no longer
1334    // starve each other on the LLC lock; a `performance_mode` peer holding
1335    // `LOCK_EX` on those LLCs can still defer a cell (nextest retries it),
1336    // which is correct — the verifier must not perturb an isolated peer's
1337    // pinned CPUs. Note `no_perf_mode(true)` does NOT skip the reservation
1338    // ENTIRELY — only `KTSTR_BYPASS_LLC_LOCKS=1` does; see
1339    // [`crate::vmm::KtstrVmBuilder::no_perf_mode`].
1340    // Pass the validated Topology directly so misorder cannot occur
1341    // at the builder boundary (the TryFrom above already enforces the
1342    // type-level invariants).
1343    //
1344    // Feed the caller-selected memory floor through the budget model.
1345    // Direct API callers retain the historical vCPU-scaled floor, while
1346    // canned verifier cells cap it at the preset's declared budget. The
1347    // verifier /init is a large instrumented test
1348    // binary, so the budget model raises the actual
1349    // allocation to fit the real initramfs (floor enforced at
1350    // `initramfs_min_memory_mib(&budget).max(self.memory_min_mib)` in
1351    // `vmm::setup::KtstrVm::prepared_memory_mib`). Verifier cells have
1352    // no wprof, so no wprof floor applies here.
1353    // Bounded whole-boot retry on the guest AP-bring-up-gap infra fault
1354    // (an AP that missed its INIT-SIPI window → the guest PID-1 panics
1355    // pre-test). Rebuild the VM each attempt: `build` consumes the
1356    // builder and `run` consumes the boot. Every other failure returns
1357    // on the first attempt — the retry keys strictly on the marker.
1358    let result = crate::test_support::run_vm_with_ap_gap_retry(|| {
1359        let mut builder = crate::vmm::KtstrVm::builder()
1360            .kernel(kernel)
1361            // Prebuilt distro kernels ship virtio as modules; embed the
1362            // ordered boot-module set from the cache entry (no-op for built
1363            // kernels, which have no sibling modules/ dir).
1364            .kernel_modules(crate::cache::boot_modules_for_image(kernel))
1365            .initrd_compression(crate::cache::initrd_compression_for_image(kernel))
1366            .init_binary(ktstr_bin)
1367            .scheduler_binary(sched_bin)
1368            .sched_args(&launch.sched_args)
1369            .cmdline(&launch.cmdline_extra)
1370            // Boot the guest into the verifier dispatch probe: the sweep VM
1371            // has no `#[ktstr_test]` body, so Phase 5 spawns a SpinWait
1372            // workload and emits `WorkloadDispatched` on confirmed progress.
1373            // The PASS verdict requires that frame in addition to attach.
1374            .run_args(&[crate::test_support::VERIFIER_WORKLOAD_FLAG.to_string()])
1375            .topology(validated)
1376            .memory_deferred_min(memory_min_mib)
1377            // Timeout mirrors the `#[ktstr_test]` path's shape: a flat
1378            // lifecycle base plus vCPU-scaled dead-man boot headroom
1379            // (`vm_timeout_from_entry`'s split). This is only Tier-3 —
1380            // the progress watchdog governs wedges, and a wide cell
1381            // booting slowly under CI concurrency is progress-protected
1382            // rather than deadline-raced.
1383            // `workload_duration` arms the watchdog's attach reset: a
1384            // slow boot cannot eat the probe/teardown budget (the reset
1385            // is extend-only).
1386            .timeout(crate::test_support::runtime::verifier_vm_timeout(
1387                validated.total_cpus(),
1388            ))
1389            .workload_duration(crate::test_support::runtime::VERIFIER_WORKLOAD_BUDGET)
1390            .no_perf_mode(true);
1391        if !launch.include_files.is_empty() {
1392            builder = builder.include_files(launch.include_files.clone());
1393        }
1394        if let Some(parent) = launch.scheduler_cgroup_parent.as_ref() {
1395            builder = builder.scheduler_cgroup_parent(parent.clone());
1396        }
1397        // A preset with a forced CPU budget (192cpu-11llc-smt) pins the
1398        // no-perf mask to that many host CPUs so its vCPUs ALWAYS
1399        // overcommit — the deliberate, continuous time-slicing path.
1400        // Absent (every stock preset) leaves budget on the unchanged
1401        // auto-sized path: vCPU count plus service-thread headroom,
1402        // capped by the allowed host cpuset. no_perf_mode is already set
1403        // above, so cpu_budget is on its valid path.
1404        //
1405        // Clamp to the host's allowed CPUs: the forced budget exists to
1406        // FORCE overcommit, so on a host smaller than the budget it must
1407        // COLLAPSE to what is available (deeper overcommit — e.g. 192
1408        // vCPUs over 64 CPUs = 3x on this dev box) rather than hard-error
1409        // the way an explicit per-test `cpu_budget` would. That collapse
1410        // is the intended, storm-validated behavior. `.max(1)` guards the
1411        // barely-runnable host whose allowed set reads empty.
1412        if let Some(budget) = forced_cpu_budget {
1413            let allowed = crate::vmm::host_topology::host_allowed_cpus().len().max(1) as u32;
1414            builder = builder.cpu_budget(budget.min(allowed));
1415        }
1416        let vm = builder.build().context("build verifier VM")?;
1417
1418        vm.run().context("run verifier VM")
1419    })?;
1420
1421    // A cold retry is useful when an AP merely misses its INIT-SIPI
1422    // window under host contention. It is useless when the guest kernel
1423    // explicitly disables x2APIC and then rejects a topology APIC ID at
1424    // the 8-bit ceiling: another boot cannot change that kernel build.
1425    // Nor may the verifier compact IDs — Linux derives cache domains from
1426    // those topology bit fields, so compacting this shape would verify a
1427    // different topology under the original preset name. Surface a typed
1428    // unsupported-cell result; the dispatcher records SKIP, not scheduler
1429    // FAIL.
1430    #[cfg(target_arch = "x86_64")]
1431    if crate::test_support::guest_kernel_rejected_wide_apic(&result) {
1432        let max_apic_id = crate::vmm::x86_64::topology::max_apic_id(&validated);
1433        return Err(anyhow::Error::new(VerifierTopologyUnsupported {
1434            reason: format!(
1435                "guest kernel disabled x2APIC, but topology requires APIC IDs \
1436                 through {max_apic_id} (xAPIC supports 0..=254); compacting IDs \
1437                 would change the requested LLC topology"
1438            ),
1439        }));
1440    }
1441
1442    // Concatenate bulk-port `MSG_TYPE_SCHED_LOG` chunks, then run
1443    // the marker-pair extractor on the merged stream — the
1444    // SCHED_OUTPUT_START/END markers travel verbatim inside chunk
1445    // bytes so the existing parser slices the same content the
1446    // prior COM2 dump produced. Falls back to `result.output` when
1447    // the bulk-port drain has no SchedLog frames (verifier VM
1448    // running on a kernel without the bulk port, for instance).
1449    let merged = concat_sched_log_chunks(result.guest_messages.as_ref());
1450    let scheduler_log = if !merged.is_empty() {
1451        parse_sched_output(&merged).unwrap_or("").to_string()
1452    } else {
1453        parse_sched_output(&result.output).unwrap_or("").to_string()
1454    };
1455
1456    // Prefer a completion-proven terminal replay only when the guest observed
1457    // a dropped live chunk for that stream. Otherwise retain the zero-replay
1458    // live path. Watchdog kills and old guests never emit final frames, so
1459    // their existing best-effort live semantics remain unchanged.
1460    let scheduler_stdout = scheduler_stream_with_terminal_fallback(
1461        result.guest_messages.as_ref(),
1462        crate::vmm::wire::MSG_TYPE_SCHED_STDOUT,
1463        crate::vmm::wire::MSG_TYPE_SCHED_STDOUT_FINAL,
1464    );
1465    let scheduler_stderr = scheduler_stream_with_terminal_fallback(
1466        result.guest_messages.as_ref(),
1467        crate::vmm::wire::MSG_TYPE_SCHED_STDERR,
1468        crate::vmm::wire::MSG_TYPE_SCHED_STDERR_FINAL,
1469    );
1470
1471    // Build ProgStats from host-side ProgVerifierStats. Each program
1472    // that loaded successfully is visible in prog_idr with its
1473    // verified_insns count.
1474    let stats: Vec<ProgStats> = result
1475        .verifier_stats
1476        .iter()
1477        .map(|pvs| ProgStats {
1478            name: pvs.name.clone(),
1479            verified_insns: pvs.verified_insns,
1480        })
1481        .collect();
1482
1483    let attach = attach_outcome_from_messages(result.guest_messages.as_ref());
1484    let dispatched = dispatch_confirmed_from_messages(result.guest_messages.as_ref());
1485    let scheduler_exited = scheduler_exited_from_messages(result.guest_messages.as_ref());
1486    let guest_exit_code = terminal_guest_exit_from_messages(result.guest_messages.as_ref());
1487
1488    Ok(VerifierVmResult {
1489        stats,
1490        scheduler_log,
1491        scheduler_stdout,
1492        scheduler_stderr,
1493        attach,
1494        dispatched,
1495        scheduler_exited,
1496        guest_exit_code,
1497        timed_out: result.timed_out,
1498        crash_message: result.crash_message.clone(),
1499        // Host-side vCPU dilation, derived from the run's raw schedstat
1500        // totals. `None` when schedstats were unavailable or no vCPU was
1501        // sampled — carried through untouched, never gating the verdict.
1502        dilation: result
1503            .host_vcpu_schedstat
1504            .as_ref()
1505            .and_then(|s| s.dilation()),
1506    })
1507}
1508
1509/// Extract the verifier instruction trace from a scheduler log blob.
1510///
1511/// libbpf wraps the kernel verifier log between marker lines:
1512///   `-- BEGIN PROG LOAD LOG --`
1513///   `-- END PROG LOAD LOG --`
1514///
1515/// Returns the content between the first pair of markers, or `None` if
1516/// no markers are found (backward compat with logs that contain only
1517/// raw verifier output).
1518pub fn extract_verifier_log(scheduler_log: &str) -> Option<&str> {
1519    let (start, end) = verifier_log_region(scheduler_log)?;
1520    // `end` includes the trailing newline before the END-marker line;
1521    // trim it (and any earlier trailing blanks) to match the historical
1522    // extracted-content shape parse_verifier_stats consumes.
1523    Some(scheduler_log[start..end].trim_end_matches('\n'))
1524}
1525
1526/// Locate the libbpf verifier-log region inside a scheduler log blob:
1527/// the byte range `[start, end)` of the content between libbpf's
1528///   `-- BEGIN PROG LOAD LOG --`
1529///   `-- END PROG LOAD LOG --`
1530/// markers. `start` is just past the BEGIN marker (skipping the marker's
1531/// own trailing newline); `end` is the byte AFTER the last content
1532/// newline preceding the END marker — so the region includes that
1533/// trailing newline but excludes the END-marker line itself (and any
1534/// partial `libbpf: ` prefix when the END marker sits mid-line). Returns
1535/// `None` when either marker is absent.
1536///
1537/// Shared marker-location logic for [`extract_verifier_log`] (which
1538/// trims the region into the collapsible trace) and
1539/// [`collapse_verifier_region`] (which replaces the region in place),
1540/// so the two stay consistent — notably on the mid-line-END case where
1541/// the partial prefix stays OUTSIDE the region and thus byte-identical
1542/// in the in-place transform.
1543fn verifier_log_region(scheduler_log: &str) -> Option<(usize, usize)> {
1544    const BEGIN: &str = "-- BEGIN PROG LOAD LOG --";
1545    const END: &str = "-- END PROG LOAD LOG --";
1546
1547    let begin_pos = scheduler_log.find(BEGIN)?;
1548    let content_start = begin_pos + BEGIN.len();
1549    // Skip the newline after the BEGIN marker if present.
1550    let content_start = if scheduler_log.as_bytes().get(content_start) == Some(&b'\n') {
1551        content_start + 1
1552    } else {
1553        content_start
1554    };
1555    let rel_end = scheduler_log[content_start..].find(END)?;
1556    let raw_end = content_start + rel_end;
1557    // The END marker may appear mid-line (e.g. "libbpf: -- END ...").
1558    // Anchor the region end at the last content newline before the
1559    // marker (inclusive), so the partial "libbpf: " prefix stays in the
1560    // suffix — keeping it byte-identical under the in-place collapse.
1561    let region_end = match scheduler_log[content_start..raw_end].rfind('\n') {
1562        Some(p) => content_start + p + 1,
1563        None => raw_end,
1564    };
1565    Some((content_start, region_end))
1566}
1567
1568/// Return `s` with the libbpf verifier-log region (between the
1569/// `-- BEGIN/END PROG LOAD LOG --` markers) replaced by
1570/// [`collapse_cycles`] of that region — collapsed IN PLACE, with every
1571/// byte OUTSIDE the region (the surrounding scheduler output, the BEGIN
1572/// and END marker lines, any mid-line-END partial prefix) preserved
1573/// exactly. Input WITHOUT the marker pair is returned unchanged.
1574///
1575/// This replaces the prior extract-and-drop rendering: the scheduler's
1576/// full output is preserved and only the (potentially huge, cyclic)
1577/// verifier trace is compressed, rather than discarding everything
1578/// outside the trace.
1579pub fn collapse_verifier_region(s: &str) -> String {
1580    let Some((start, end)) = verifier_log_region(s) else {
1581        return s.to_string();
1582    };
1583    let mut out = String::with_capacity(s.len());
1584    out.push_str(&s[..start]);
1585    out.push_str(&collapse_cycles(&s[start..end]));
1586    out.push_str(&s[end..]);
1587    out
1588}
1589
1590/// Format verifier results as text: brief lines per program and collapsed
1591/// logs.
1592pub fn format_verifier_output(label: &str, result: &VerifierVmResult, raw: bool) -> String {
1593    let mut out = String::new();
1594    out.push_str(&format!("\n{label}\n"));
1595    if result.timed_out {
1596        out.push_str("  scheduler: UNKNOWN — VM timed out before exit\n");
1597    } else if result.scheduler_exited {
1598        out.push_str("  scheduler: EXITED during verifier workload\n");
1599    } else {
1600        match result.attach.failure_reason() {
1601            None => {
1602                out.push_str("  scheduler: attached (sched_ext enabled)\n");
1603                if result.dispatched {
1604                    out.push_str("  dispatch: confirmed (injected workload ran)\n");
1605                } else {
1606                    out.push_str(
1607                        "  dispatch: NOT CONFIRMED — attached but injected workload made no progress\n",
1608                    );
1609                }
1610            }
1611            Some(reason) => out.push_str(&format!("  scheduler: NOT ATTACHED — {reason}\n")),
1612        }
1613    }
1614    // Host-side vCPU scheduling dilation. `D = 1 + run_delay/on_cpu`, so
1615    // the fraction of the CPU the vCPUs actually received when runnable is
1616    // `on_cpu/(on_cpu+run_delay) == 1/D` exactly — rendered as a whole
1617    // percent. `None` (schedstats off / nothing sampled) is stated as
1618    // such, distinct from a genuine 1.00x.
1619    match result.dilation {
1620        Some(d) => {
1621            let pct = (100.0 / d).round() as u32;
1622            out.push_str(&format!(
1623                "  host dilation: {d:.2}x (vCPUs received {pct}% of the CPU they were runnable for)\n"
1624            ));
1625        }
1626        None => out.push_str("  host dilation: n/a (host schedstat unavailable)\n"),
1627    }
1628    for ps in &result.stats {
1629        out.push_str(&format!(
1630            "  {:<40} verified_insns={}\n",
1631            ps.name, ps.verified_insns
1632        ));
1633    }
1634
1635    // verifier stats: parse from whichever stream actually carries the
1636    // libbpf `-- BEGIN/END PROG LOAD LOG --` markers. With the split
1637    // live streams the verifier log usually lands on stderr; fall back
1638    // to stdout, then the teardown merged-file dump. When no stream has
1639    // the markers, parse the first non-empty stream whole — backward
1640    // compat with logs that contain only raw verifier output (the same
1641    // fallback `extract_verifier_log` documents). Renders on the
1642    // timed_out branch too (the streams survive a watchdog kill).
1643    let streams = [
1644        result.scheduler_stderr.as_str(),
1645        result.scheduler_stdout.as_str(),
1646        result.scheduler_log.as_str(),
1647    ];
1648    let stats_src = streams
1649        .into_iter()
1650        .find(|s| extract_verifier_log(s).is_some())
1651        .or_else(|| streams.into_iter().find(|s| !s.is_empty()));
1652    if let Some(src) = stats_src {
1653        let verifier_log = extract_verifier_log(src).unwrap_or(src);
1654        let vs = parse_verifier_stats(verifier_log);
1655        if vs.processed_insns > 0 {
1656            out.push_str(&format!("\n{label} --- verifier stats ---\n"));
1657            out.push_str(&format!(
1658                "  processed={}  states={}/{}",
1659                vs.processed_insns, vs.peak_states, vs.total_states
1660            ));
1661            if let Some(ref s) = vs.stack_depth {
1662                out.push_str(&format!("  stack={s}"));
1663            }
1664            out.push('\n');
1665        }
1666    }
1667
1668    // The scheduler-log section renders the FULL scheduler stdout — the
1669    // live-streamed `scheduler_stdout` (which survives a watchdog kill
1670    // that never reaches the teardown dump). NOT extract-and-drop: the
1671    // verifier trace is collapsed IN PLACE via `collapse_verifier_region`,
1672    // keeping everything around it. Emitted on the timed_out branch as
1673    // well — that is the core bug this fixes. The merged teardown dump
1674    // `scheduler_log` is the fallback ONLY when NEITHER live stream
1675    // arrived (streaming failed or the guest predates the split): when
1676    // live stderr did arrive, an empty live stdout means the scheduler
1677    // wrote nothing to stdout, and rendering the merged dump here would
1678    // duplicate the stderr section (the common libbpf/log-crate case —
1679    // schedulers that log exclusively to stderr).
1680    let stdout_src = if !result.scheduler_stdout.is_empty() {
1681        result.scheduler_stdout.as_str()
1682    } else if result.scheduler_stderr.is_empty() {
1683        result.scheduler_log.as_str()
1684    } else {
1685        ""
1686    };
1687    if !stdout_src.is_empty() {
1688        out.push_str(&format!("\n{label} --- scheduler log ---\n"));
1689        if raw {
1690            out.push_str(stdout_src);
1691        } else {
1692            out.push_str(&collapse_verifier_region(stdout_src));
1693        }
1694    }
1695
1696    out
1697}
1698
1699/// Format the scheduler's captured STDERR for emission to the test's
1700/// stderr, or the empty string when there is no stderr to show.
1701///
1702/// Best-effort: renders whatever `scheduler_stderr` streamed before the
1703/// VM exited or was watchdog-killed. Non-raw runs collapse the libbpf
1704/// verifier-log region IN PLACE via [`collapse_verifier_region`] (the
1705/// verifier trace usually lands on stderr with the split streams); raw
1706/// runs pass the bytes through unmodified. A leading `--- scheduler
1707/// stderr ---` header labels the section to match the stdout section's
1708/// style so a reader can tell the two streams apart. The caller emits
1709/// the result via `eprint!` so it lands on the test's real stderr.
1710pub fn format_verifier_stderr(label: &str, result: &VerifierVmResult, raw: bool) -> String {
1711    if result.scheduler_stderr.is_empty() {
1712        return String::new();
1713    }
1714    let body = if raw {
1715        result.scheduler_stderr.clone()
1716    } else {
1717        collapse_verifier_region(&result.scheduler_stderr)
1718    };
1719    format!("\n{label} --- scheduler stderr ---\n{body}")
1720}
1721
1722/// Format an A/B diff table comparing two sets of verifier stats.
1723pub fn format_verifier_diff(
1724    label_a: &str,
1725    stats_a: &[ProgStats],
1726    label_b: &str,
1727    stats_b: &[ProgStats],
1728) -> String {
1729    let b_map = build_b_map(stats_b);
1730    let diff_rows = build_diff_rows(stats_a, &b_map);
1731
1732    let mut out = String::new();
1733    out.push_str(&format!("\ndelta A/B diff: {label_a} vs {label_b}\n"));
1734    let mut table = crate::cli::new_table();
1735    table.set_header(vec!["program", "A", "B", "delta"]);
1736    for row in &diff_rows {
1737        table.add_row(vec![
1738            row.name.clone(),
1739            row.a.to_string(),
1740            row.b.to_string(),
1741            format!("{:+}", row.delta),
1742        ]);
1743    }
1744    out.push_str(&table.to_string());
1745    out.push('\n');
1746    out
1747}
1748
1749// ---------------------------------------------------------------------------
1750// Per-cell PASS/FAIL result capture (for the run-summary table)
1751// ---------------------------------------------------------------------------
1752
1753/// One `cargo ktstr verifier` cell's outcome. The cell process writes it
1754/// (via `write_cell_record`) into the directory named by
1755/// [`crate::KTSTR_VERIFIER_RESULT_DIR_ENV`]; after nextest returns the
1756/// dispatcher reads them back (via [`read_cell_records`]) and renders one
1757/// PASS/FAIL/SKIP grid per scheduler (rows = topology, cols = kernel). A cell
1758/// is one
1759/// (scheduler, kernel, topology): the verifier sweeps each declared
1760/// scheduler across topologies, so topology IS a result axis — a
1761/// scheduler can pass on one topology and fail on another.
1762#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
1763pub struct VerifierCellRecord {
1764    /// Declared scheduler name (the `<sched>` cell-name segment).
1765    pub scheduler: String,
1766    /// Sanitized kernel label (the `<kernel>` cell-name segment).
1767    pub kernel: String,
1768    /// Gauntlet topology preset (the `<preset>` cell-name segment).
1769    pub topology: String,
1770    /// Whether the cell passed. False for both FAIL and SKIP; consult
1771    /// [`Self::skipped`] to distinguish them.
1772    pub passed: bool,
1773    /// Whether the cell was unsupported for this guest kernel. Mutually
1774    /// exclusive with `passed`; rendered as `-` and excluded from both
1775    /// pass and fail tallies. `serde(default)` accepts records from ktstr
1776    /// versions which predate the explicit verifier-skip state.
1777    #[serde(default)]
1778    pub skipped: bool,
1779    /// Per-program stats (program name + its `verified_insns` count)
1780    /// captured for this cell, copied from the VM run's
1781    /// [`VerifierVmResult::stats`]. Empty when the cell failed before
1782    /// producing stats. Drives the per-scheduler `verified_insns` tables
1783    /// ([`render_instruction_count_tables`], rows = BPF program, cols =
1784    /// kernel, cell = the count summarized across topologies) that the
1785    /// dispatcher prints before the PASS/FAIL grids.
1786    pub stats: Vec<ProgStats>,
1787}
1788
1789/// Map a cell's full name to a compact content-addressed record filename.
1790///
1791/// The fixed-seed ahash matches ktstr's other fast content-addressed
1792/// identities. Hashing the raw, length-delimited name avoids aliases introduced
1793/// by filesystem sanitization (`foo-bar` versus `foo_bar`) and keeps the
1794/// filename below `NAME_MAX` even when a valid scheduler name is long. A
1795/// nextest RETRY of the same cell resolves to the same path and overwrites its
1796/// prior record, so the FINAL attempt's outcome wins.
1797fn cell_record_filename(full_name: &str) -> String {
1798    use std::hash::{BuildHasher, Hasher};
1799
1800    let bytes = full_name.as_bytes();
1801    let mut hasher = ahash::RandomState::with_seeds(0, 0, 0, 0).build_hasher();
1802    hasher.write(&(bytes.len() as u64).to_le_bytes());
1803    hasher.write(bytes);
1804    format!("cell-{:016x}.json", hasher.finish())
1805}
1806
1807/// Write a cell's PASS/FAIL/SKIP record into `dir`. Parses `full_name`
1808/// (`verifier/<sched>/<kernel>/<preset>`); a name that does not fit that
1809/// shape is skipped (the cell already errored on the malformed name).
1810/// The record is serialized to a same-directory temporary and atomically
1811/// renamed over the deterministic final path. Readers therefore see either
1812/// the preceding retry's complete record or the current retry's complete
1813/// record, never a truncated JSON prefix. Best-effort: a write failure is
1814/// logged and swallowed — the summary table is a convenience over the
1815/// per-cell nextest output, so a lost record must never turn an
1816/// otherwise-passing cell into a failure.
1817///
1818/// `pub(crate)`: the only writer is the cell handler in
1819/// `test_support::dispatch` (same crate); the reader side
1820/// ([`read_cell_records`] / [`render_result_table`]) is `pub` for the
1821/// `cargo-ktstr` binary crate.
1822pub(crate) fn write_cell_record(
1823    dir: &std::path::Path,
1824    full_name: &str,
1825    passed: bool,
1826    skipped: bool,
1827    stats: &[ProgStats],
1828) {
1829    debug_assert!(
1830        !(passed && skipped),
1831        "a verifier cell cannot be both passed and skipped"
1832    );
1833    let Some(rest) = full_name.strip_prefix("verifier/") else {
1834        return;
1835    };
1836    let parts: Vec<&str> = rest.splitn(3, '/').collect();
1837    if parts.len() != 3 {
1838        return;
1839    }
1840    let record = VerifierCellRecord {
1841        scheduler: parts[0].to_string(),
1842        kernel: parts[1].to_string(),
1843        topology: parts[2].to_string(),
1844        passed,
1845        skipped,
1846        stats: stats.to_vec(),
1847    };
1848    let path = dir.join(cell_record_filename(full_name));
1849    let result = (|| -> anyhow::Result<()> {
1850        let mut temporary = tempfile::NamedTempFile::new_in(dir)?;
1851        serde_json::to_writer(temporary.as_file_mut(), &record)?;
1852        std::io::Write::flush(temporary.as_file_mut())?;
1853        temporary.persist(&path).map_err(|error| error.error)?;
1854        Ok(())
1855    })();
1856    if let Err(error) = result {
1857        eprintln!(
1858            "ktstr verifier: warning: could not atomically write result record {}: {error:#}",
1859            path.display(),
1860        );
1861    }
1862}
1863
1864/// Read every `*.json` cell record under `dir` (non-recursive). A missing
1865/// dir or an unparseable record is skipped (best-effort). Returns records
1866/// in filesystem-iteration order; [`render_result_table`] sorts for a
1867/// deterministic render.
1868pub fn read_cell_records(dir: &std::path::Path) -> Vec<VerifierCellRecord> {
1869    let Ok(entries) = std::fs::read_dir(dir) else {
1870        return Vec::new();
1871    };
1872    entries
1873        .flatten()
1874        .filter(|e| {
1875            e.path()
1876                .extension()
1877                .is_some_and(|x| x.eq_ignore_ascii_case("json"))
1878        })
1879        .filter_map(|e| std::fs::read(e.path()).ok())
1880        .filter_map(|bytes| serde_json::from_slice::<VerifierCellRecord>(&bytes).ok())
1881        .collect()
1882}
1883
1884/// The terminal disposition of a `cargo ktstr verifier` run, decided by
1885/// [`classify_run_outcome`] after nextest returns and the report grids
1886/// have been printed.
1887#[derive(Debug, Clone, PartialEq, Eq)]
1888pub enum RunOutcome {
1889    /// Clean success — the dispatcher returns `Ok(())`.
1890    Success,
1891    /// A diagnosable failure whose message the dispatcher routes through
1892    /// main's stderr error handler (exit 1). Covers a nextest nonzero
1893    /// exit with NO failed cell (a build / internal failure — nothing in
1894    /// the grid to point at), a signal exit (no numeric code), and the
1895    /// empty-record diagnostics.
1896    Failed(String),
1897    /// nextest exited nonzero AND the printed grid already shows at least
1898    /// one failing (✗) cell, so the failure is already visible on stdout.
1899    /// The dispatcher exits with this (nextest's own) code but does NOT emit a
1900    /// redundant `error: cargo nextest run exited with N` line: that line
1901    /// goes to stderr while the report goes to stdout, and with two
1902    /// unordered pipes it otherwise interleaves mid-report in CI logs.
1903    SilentExit(i32),
1904}
1905
1906/// Decide the `cargo ktstr verifier` run outcome from nextest's exit
1907/// success, whether any per-cell records were produced, whether any of
1908/// those records is a FAILED cell (already shown as a ✗ in the printed
1909/// grid), and the optional `--scheduler` filter.
1910///
1911/// The dispatcher runs nextest with `--no-tests=pass`, so a run that
1912/// selects zero verifier cells exits 0 (success) with empty records
1913/// rather than nextest's generic "no tests to run" (exit 4). That lets
1914/// the dispatcher diagnose the empty case itself instead of surfacing a
1915/// cryptic nextest exit:
1916/// - `--scheduler <NAME>` set + no records: the name is not a declared
1917///   scheduler, or its declared topology constraints / verifier-only
1918///   exclusions reject every selected preset.
1919/// - no `--scheduler` + no records: no `declare_scheduler!` is linked into
1920///   a test binary the sweep sees, or every declared scheduler rejects all
1921///   selected presets through those declaration-level gates.
1922///
1923/// A genuine build/exec failure still fails nextest (exit non-zero, which
1924/// `--no-tests=pass` does not mask). When that nonzero exit is a REAL
1925/// cell failure — `has_failed_cell` true and nextest handed back a
1926/// numeric `exit_code` — the printed grid already shows the failing
1927/// cell(s), so the outcome is [`RunOutcome::SilentExit`] (carry nextest's
1928/// code, emit no stderr line). Every other nonzero case — a build /
1929/// internal failure that produced no failed cell, or a signal exit with
1930/// no numeric code — stays [`RunOutcome::Failed`] with the descriptive
1931/// message, as does each empty-record diagnostic.
1932pub fn classify_run_outcome(
1933    success: bool,
1934    records_empty: bool,
1935    has_failed_cell: bool,
1936    scheduler: Option<&str>,
1937    exit_code: Option<i32>,
1938) -> RunOutcome {
1939    if !success {
1940        // The printed grid already shows the failing cell(s): carry
1941        // nextest's code out silently instead of a stderr error line that
1942        // would interleave mid-report. Only with a numeric code — a
1943        // signal exit (None) keeps the descriptive message.
1944        if has_failed_cell && let Some(code) = exit_code {
1945            return RunOutcome::SilentExit(code);
1946        }
1947        let code = exit_code.map_or_else(|| "signal".to_string(), |c| c.to_string());
1948        return RunOutcome::Failed(format!("cargo nextest run exited with {code}"));
1949    }
1950    if records_empty {
1951        return RunOutcome::Failed(match scheduler {
1952            Some(name) => format!(
1953                "--scheduler {name:?}: matched no verifier cell — no declared BPF \
1954                 scheduler by that name, or its declared topology constraints or \
1955                 verifier-only exclusions rejected every selected preset. Run \
1956                 `cargo ktstr verifier` with no --scheduler to see the swept set."
1957            ),
1958            None => "no verifier cells ran — no scheduler is declared via \
1959                 declare_scheduler! in a linked test binary, or every declared \
1960                 scheduler's topology constraints or verifier-only exclusions \
1961                 rejected every selected preset."
1962                .to_string(),
1963        });
1964    }
1965    RunOutcome::Success
1966}
1967
1968/// Build the `cargo nextest run` argument vector for the verifier sweep.
1969///
1970/// Load-bearing tokens:
1971/// - `--run-ignored all`: verifier cells are emitted ignore-gated, so
1972///   nextest skips them unless opted in.
1973/// - `--no-tests pass`: a zero-cell selection exits 0 (not nextest's
1974///   default exit-4 "no tests to run"), so [`classify_run_outcome`] can
1975///   emit a targeted diagnostic instead of a cryptic nextest exit.
1976/// - `-E 'test(/^verifier/) & !test(/^verifier::/)'`: the `verifier/...`
1977///   cells, excluding the verifier module's own `verifier::tests::*`.
1978///
1979/// `nextest_profile`, if set, becomes nextest's `--profile <NAME>`,
1980/// emitted before `forward` so a forwarded token cannot shadow it.
1981/// `forward` is the user's trailing cargo/nextest args, appended verbatim.
1982pub fn build_nextest_args(nextest_profile: Option<&str>, forward: &[String]) -> Vec<String> {
1983    let mut args = vec![
1984        "nextest".to_string(),
1985        "run".to_string(),
1986        "--run-ignored".to_string(),
1987        "all".to_string(),
1988        // The `=` spelling is the one accepted across every supported
1989        // nextest: the flag landed in 0.9.75 with `require_equals`, and
1990        // the space-separated form only parses from 0.9.89.
1991        "--no-tests=pass".to_string(),
1992        "-E".to_string(),
1993        "test(/^verifier/) & !test(/^verifier::/)".to_string(),
1994    ];
1995    if let Some(np) = nextest_profile {
1996        args.push("--profile".to_string());
1997        args.push(np.to_string());
1998    }
1999    args.extend(forward.iter().cloned());
2000    args
2001}
2002
2003/// Render the per-cell records into one PASS/FAIL/SKIP grid PER declared
2004/// scheduler (BTreeSet-sorted). Each scheduler's section is a tally line
2005/// followed by a bordered grid whose rows are the topology presets that
2006/// ran the scheduler (BTreeSet) and whose columns are the kernels that
2007/// ran it (BTreeSet); the header row's first column is `topology`. Each
2008/// grid cell is the (topology, kernel) result FOR THAT SCHEDULER,
2009/// rendered nextest-style:
2010/// - `✓` (bold green) — every record for the triple passed,
2011/// - `✗` (bold red) — any record for it failed; this also absorbs the
2012///   defensive duplicate-record case where one triple somehow carries
2013///   both a pass and a fail — any failure means failure,
2014/// - `-` (plain) — every record for it skipped, or no record (the
2015///   scheduler's constraints rejected the preset on that kernel).
2016///
2017/// Cells are single-width glyphs (U+2713 / U+2717), not emoji: emoji
2018/// are not exactly two monospace cells in GitHub's log viewer / code
2019/// blocks, so they drift the box-drawing columns. The glyph styling
2020/// comes from [`comfy_table::Cell::fg`] + `Attribute::Bold`, with
2021/// emission governed by [`crate::cli::new_bordered_table`]'s color
2022/// policy (styled on a TTY and in color-forcing CI, plain otherwise).
2023/// The tally line keeps the emoji — `{sched}: {n_pass} ✅  {n_fail} ❌`
2024/// — because it is prose with nothing to column-align. The counts tally
2025/// the grid CELLS (a `-` cell counts toward neither; a cell with any
2026/// failing record counts toward `n_fail`). The kernel is a grid axis
2027/// rather than folded into the cell, so a ✗ cell already names the
2028/// exact kernel that failed — the old flat `failing combinations
2029/// (scheduler / kernel / topology)` list it used to need is gone.
2030///
2031/// Returns `None` for an empty record set (the caller prints nothing).
2032/// Schedulers, rows, and columns are BTreeSet-sorted so the same run
2033/// renders the same output (shell-pipeline stable). Uses
2034/// [`crate::cli::new_bordered_table`] (box-drawing borders) so each
2035/// scheduler's grid is visually delimited from its tally line and from
2036/// the next scheduler's section.
2037pub fn render_result_table(records: &[VerifierCellRecord]) -> Option<String> {
2038    if records.is_empty() {
2039        return None;
2040    }
2041    use std::collections::{BTreeMap, BTreeSet};
2042    let mut schedulers: BTreeSet<String> = BTreeSet::new();
2043    // scheduler -> topology rows that ran it.
2044    let mut sched_rows: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
2045    // scheduler -> kernel columns that ran it.
2046    let mut sched_cols: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
2047    #[derive(Clone, Copy, Default)]
2048    struct CellDisposition {
2049        any_pass: bool,
2050        any_fail: bool,
2051        any_skip: bool,
2052    }
2053    // scheduler -> (topology, kernel) -> folded disposition. Failure
2054    // dominates pass, which dominates skip, for defensive duplicates.
2055    let mut agg: BTreeMap<String, BTreeMap<(String, String), CellDisposition>> = BTreeMap::new();
2056    for r in records {
2057        schedulers.insert(r.scheduler.clone());
2058        sched_rows
2059            .entry(r.scheduler.clone())
2060            .or_default()
2061            .insert(r.topology.clone());
2062        sched_cols
2063            .entry(r.scheduler.clone())
2064            .or_default()
2065            .insert(r.kernel.clone());
2066        let disposition = agg
2067            .entry(r.scheduler.clone())
2068            .or_default()
2069            .entry((r.topology.clone(), r.kernel.clone()))
2070            .or_default();
2071        if r.skipped {
2072            disposition.any_skip = true;
2073        } else if r.passed {
2074            disposition.any_pass = true;
2075        } else {
2076            disposition.any_fail = true;
2077        }
2078    }
2079
2080    let mut out =
2081        String::from("\nverifier results (per scheduler; rows: topology, cols: kernel):\n");
2082    for sched in &schedulers {
2083        let rows = &sched_rows[sched];
2084        let cols = &sched_cols[sched];
2085        let cells = &agg[sched];
2086        let (mut n_pass, mut n_fail) = (0usize, 0usize);
2087        let mut table = crate::cli::new_bordered_table();
2088        let mut header: Vec<String> = vec!["topology".to_string()];
2089        for kernel in cols {
2090            header.push(kernel.clone());
2091        }
2092        table.set_header(header);
2093        for topo in rows {
2094            let mut line: Vec<comfy_table::Cell> = vec![comfy_table::Cell::new(topo)];
2095            for kernel in cols {
2096                let cell = match cells.get(&(topo.clone(), kernel.clone())).copied() {
2097                    None => comfy_table::Cell::new("-"),
2098                    Some(d) if d.any_fail => {
2099                        n_fail += 1;
2100                        comfy_table::Cell::new("✗")
2101                            .fg(comfy_table::Color::Red)
2102                            .add_attribute(comfy_table::Attribute::Bold)
2103                    }
2104                    Some(d) if d.any_pass => {
2105                        n_pass += 1;
2106                        comfy_table::Cell::new("✓")
2107                            .fg(comfy_table::Color::Green)
2108                            .add_attribute(comfy_table::Attribute::Bold)
2109                    }
2110                    Some(d) => {
2111                        debug_assert!(d.any_skip);
2112                        comfy_table::Cell::new("-")
2113                    }
2114                };
2115                line.push(cell);
2116            }
2117            table.add_row(line);
2118        }
2119        out.push_str(&format!("\n{sched}: {n_pass} ✅  {n_fail} ❌\n{table}\n"));
2120    }
2121    Some(out)
2122}
2123
2124/// Render one `verified_insns` table per declared scheduler. Within each
2125/// scheduler's section: rows = BPF program, columns = kernel version, and
2126/// each cell is that program's `verified_insns` for the (scheduler,
2127/// kernel) summarized ACROSS the topologies that ran it — a single number
2128/// when topology-invariant, `lo..hi` when it varies (`-` when that program
2129/// reported no stats on that kernel).
2130///
2131/// `verified_insns` is the verifier's PROCESSED-instruction count
2132/// (`env->insn_processed`) — fixed per load, but NOT topology-invariant
2133/// (a scheduler whose verification path depends on topology-derived
2134/// `.rodata`, e.g. `nr_cpus`, processes a different count per topology).
2135/// So topology is folded into the cell as a range rather than shown as its
2136/// own (usually all-identical) axis; the axes it genuinely varies on — BPF
2137/// program (y) and kernel version (x) — are the table axes, sectioned per
2138/// declared scheduler. Program is the ROW axis because a scheduler declares
2139/// many programs (scx_lavd has ~26) and the count is unbounded, so rows
2140/// scale down the page without wrapping; kernel is the COLUMN axis because
2141/// a sweep runs only a handful of kernels, so the columns stay narrow
2142/// enough to read in a CI log. Identical-binary declarations are sectioned
2143/// separately on purpose (they are run separately). Returns `None` when no
2144/// record carries any per-program stats (the caller prints nothing).
2145///
2146/// A kernel that ran a scheduler but produced NO stats at all — e.g.
2147/// every cell on it died during BPF load, so no program existed to
2148/// introspect — still gets a column (all cells `-`) as long as the
2149/// scheduler has at least one program row from some OTHER kernel.
2150/// Without it the stats-less kernel would silently vanish from the
2151/// table, hiding that it ran and failed to load; the explicit all-`-`
2152/// column makes that absence visible. Kernel columns therefore come from
2153/// every kernel that ran the scheduler, not only those that produced stats.
2154///
2155/// Schedulers, kernels, and programs are BTree-sorted so the same run
2156/// renders the same output (shell-pipeline stable). The range drops which
2157/// topology produced which count; a per-topology breakdown is a separate
2158/// detailed view, not this summary.
2159pub fn render_instruction_count_tables(records: &[VerifierCellRecord]) -> Option<String> {
2160    use std::collections::{BTreeMap, BTreeSet};
2161    // scheduler -> kernel -> program -> (min, max) verified_insns across
2162    // the topologies that ran it. Topology is folded into the (min, max)
2163    // range: a flat scheduler has min == max (one number), a
2164    // topology-sensitive one has min < max (`lo..hi`).
2165    type VerifiedInsnSpans = BTreeMap<String, BTreeMap<String, BTreeMap<String, (u32, u32)>>>;
2166    let mut by_sched: VerifiedInsnSpans = BTreeMap::new();
2167    // scheduler -> the union of program names it reported (the columns).
2168    let mut sched_progs: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
2169    // scheduler -> every kernel that has a record for it, whether or not
2170    // it contributed stats. A kernel present here but absent from
2171    // `by_sched[sched]` ran the scheduler yet produced no program stats,
2172    // so it becomes an all-`-` column rather than vanishing.
2173    let mut sched_all_kernels: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
2174    for r in records {
2175        sched_all_kernels
2176            .entry(r.scheduler.clone())
2177            .or_default()
2178            .insert(r.kernel.clone());
2179        for s in &r.stats {
2180            let span = by_sched
2181                .entry(r.scheduler.clone())
2182                .or_default()
2183                .entry(r.kernel.clone())
2184                .or_default()
2185                .entry(s.name.clone())
2186                .or_insert((s.verified_insns, s.verified_insns));
2187            span.0 = span.0.min(s.verified_insns);
2188            span.1 = span.1.max(s.verified_insns);
2189            sched_progs
2190                .entry(r.scheduler.clone())
2191                .or_default()
2192                .insert(s.name.clone());
2193        }
2194    }
2195    if by_sched.is_empty() {
2196        return None;
2197    }
2198
2199    let mut out = String::from(
2200        "\nverifier verified_insns (per scheduler; rows: BPF program, cols: kernel, \
2201         cell: range across topologies):\n",
2202    );
2203    for (sched, kernels) in &by_sched {
2204        let progs = &sched_progs[sched];
2205        // EVERY kernel that ran this scheduler, not only the ones that
2206        // produced stats, so a load-failure kernel surfaces as an all-`-`
2207        // column instead of vanishing.
2208        let all_kernels = &sched_all_kernels[sched];
2209        let mut table = crate::cli::new_bordered_table();
2210        let mut header: Vec<String> = vec!["program".to_string()];
2211        for kernel in all_kernels {
2212            header.push(kernel.clone());
2213        }
2214        table.set_header(header);
2215        for p in progs {
2216            let mut line: Vec<String> = vec![p.clone()];
2217            for kernel in all_kernels {
2218                let text = match kernels.get(kernel).and_then(|m| m.get(p)) {
2219                    Some((lo, hi)) if lo == hi => lo.to_string(),
2220                    Some((lo, hi)) => format!("{lo}..{hi}"),
2221                    None => "-".to_string(),
2222                };
2223                line.push(text);
2224            }
2225            table.add_row(line);
2226        }
2227        out.push_str(&format!("\n{sched}:\n{table}\n"));
2228    }
2229    Some(out)
2230}
2231
2232#[cfg(test)]
2233mod tests {
2234    use super::*;
2235
2236    fn ownership_scheduler(name: &str) -> crate::test_support::SchedulerJson {
2237        let mut scheduler = crate::test_support::SchedulerJson::from_scheduler(
2238            &crate::test_support::Scheduler::EEVDF,
2239        );
2240        scheduler.name = name.to_string();
2241        scheduler
2242    }
2243
2244    fn write_ownership_manifest(dir: &Path, manifest: &VerifierCellOwnershipManifest) -> PathBuf {
2245        let path = dir.join("cell-ownership.json");
2246        std::fs::write(
2247            &path,
2248            serde_json::to_vec(manifest).expect("serialize ownership manifest"),
2249        )
2250        .expect("write ownership manifest");
2251        path
2252    }
2253
2254    #[test]
2255    fn cell_ownership_allows_exactly_the_canonical_elected_executable() {
2256        let dir = tempfile::tempdir().expect("ownership tempdir");
2257        let owner = dir.path().join("owner");
2258        let non_owner = dir.path().join("non-owner");
2259        std::fs::write(&owner, b"owner").expect("write owner");
2260        std::fs::write(&non_owner, b"non-owner").expect("write non-owner");
2261        let owner = std::fs::canonicalize(owner).expect("canonical owner");
2262        let non_owner = std::fs::canonicalize(non_owner).expect("canonical non-owner");
2263        let owner_alias = dir.path().join("owner-alias");
2264        std::os::unix::fs::symlink(&owner, &owner_alias).expect("symlink owner alias");
2265        let scheduler = ownership_scheduler("shared");
2266        let manifest_path = write_ownership_manifest(
2267            dir.path(),
2268            &VerifierCellOwnershipManifest {
2269                version: VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION,
2270                entries: vec![VerifierCellOwnershipEntry {
2271                    scheduler: scheduler.clone(),
2272                    executable: owner.clone(),
2273                }],
2274            },
2275        );
2276
2277        let elected = cell_ownership_from_manifest_path(Some(manifest_path.as_os_str()), &owner)
2278            .expect("load owner view")
2279            .expect("present manifest");
2280        let rejected =
2281            cell_ownership_from_manifest_path(Some(manifest_path.as_os_str()), &non_owner)
2282                .expect("load non-owner view")
2283                .expect("present manifest");
2284        let elected_through_alias =
2285            cell_ownership_from_manifest_path(Some(manifest_path.as_os_str()), &owner_alias)
2286                .expect("load owner alias view")
2287                .expect("present manifest");
2288        assert!(elected.owns_scheduler(&scheduler).expect("owner lookup"));
2289        assert!(
2290            elected_through_alias
2291                .owns_scheduler(&scheduler)
2292                .expect("owner alias lookup"),
2293            "current_exe aliases must compare by their canonical executable path",
2294        );
2295        assert!(
2296            !rejected
2297                .owns_scheduler(&scheduler)
2298                .expect("non-owner lookup")
2299        );
2300        let mut stale_child = scheduler.clone();
2301        stale_child.kargs.push("identity=stale-child".into());
2302        assert!(
2303            elected.owns_scheduler(&stale_child).is_err(),
2304            "matching only the scheduler name cannot establish ownership",
2305        );
2306    }
2307
2308    #[test]
2309    fn present_cell_ownership_manifest_fails_closed() {
2310        let dir = tempfile::tempdir().expect("ownership tempdir");
2311        let owner = dir.path().join("owner");
2312        std::fs::write(&owner, b"owner").expect("write owner");
2313        let owner = std::fs::canonicalize(owner).expect("canonical owner");
2314        let scheduler = ownership_scheduler("shared");
2315        assert!(
2316            cell_ownership_from_manifest_path(None, &owner)
2317                .expect("absent manifest")
2318                .is_none(),
2319            "only an absent environment value retains direct invocation",
2320        );
2321        assert!(
2322            cell_ownership_from_manifest_path(Some(OsStr::new("")), &owner).is_err(),
2323            "an explicitly empty manifest path is still present and must fail",
2324        );
2325
2326        let wrong_version_path = write_ownership_manifest(
2327            dir.path(),
2328            &VerifierCellOwnershipManifest {
2329                version: VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION + 1,
2330                entries: Vec::new(),
2331            },
2332        );
2333        assert!(
2334            cell_ownership_from_manifest_path(Some(wrong_version_path.as_os_str()), &owner)
2335                .is_err(),
2336            "unknown ownership schemas cannot fall back to shared ownership",
2337        );
2338
2339        let empty_path = write_ownership_manifest(
2340            dir.path(),
2341            &VerifierCellOwnershipManifest {
2342                version: VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION,
2343                entries: Vec::new(),
2344            },
2345        );
2346        let empty = cell_ownership_from_manifest_path(Some(empty_path.as_os_str()), &owner)
2347            .expect("load empty manifest")
2348            .expect("present manifest");
2349        assert!(
2350            empty.owns_scheduler(&scheduler).is_err(),
2351            "a selected scheduler missing from a present manifest cannot become shared",
2352        );
2353
2354        let duplicate_path = write_ownership_manifest(
2355            dir.path(),
2356            &VerifierCellOwnershipManifest {
2357                version: VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION,
2358                entries: vec![
2359                    VerifierCellOwnershipEntry {
2360                        scheduler: scheduler.clone(),
2361                        executable: owner.clone(),
2362                    },
2363                    VerifierCellOwnershipEntry {
2364                        scheduler: scheduler.clone(),
2365                        executable: owner.clone(),
2366                    },
2367                ],
2368            },
2369        );
2370        assert!(
2371            cell_ownership_from_manifest_path(Some(duplicate_path.as_os_str()), &owner).is_err(),
2372            "ambiguous scheduler owners cannot be accepted",
2373        );
2374
2375        let noncanonical_owner = dir.path().join("owner-symlink");
2376        std::os::unix::fs::symlink(&owner, &noncanonical_owner).expect("symlink owner");
2377        let noncanonical_path = write_ownership_manifest(
2378            dir.path(),
2379            &VerifierCellOwnershipManifest {
2380                version: VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION,
2381                entries: vec![VerifierCellOwnershipEntry {
2382                    scheduler: scheduler.clone(),
2383                    executable: noncanonical_owner,
2384                }],
2385            },
2386        );
2387        assert!(
2388            cell_ownership_from_manifest_path(Some(noncanonical_path.as_os_str()), &owner).is_err(),
2389            "owner paths must be canonical rather than merely equivalent",
2390        );
2391
2392        let malformed_path = dir.path().join("malformed-ownership.json");
2393        std::fs::write(&malformed_path, b"{").expect("write malformed manifest");
2394        assert!(
2395            cell_ownership_from_manifest_path(Some(malformed_path.as_os_str()), &owner).is_err(),
2396            "malformed JSON cannot fall back to shared ownership",
2397        );
2398        assert!(
2399            cell_ownership_from_manifest_path(
2400                Some(dir.path().join("missing.json").as_os_str()),
2401                &owner,
2402            )
2403            .is_err(),
2404            "a missing present-manifest path cannot fall back to shared ownership",
2405        );
2406    }
2407
2408    #[test]
2409    fn declared_scheduler_launch_plan_preserves_boot_environment_and_order() {
2410        use crate::test_support::{Scheduler, Sysctl};
2411
2412        const SYSCTLS: &[Sysctl] = &[
2413            Sysctl::new("kernel.ktstr_first", "1"),
2414            Sysctl::new("kernel.ktstr_second", "2"),
2415        ];
2416        const KARGS: &[&str] = &["ktstr_first_karg=one", "ktstr_second_karg=two"];
2417        const SCHED_ARGS: &[&str] = &["--mode", "verifier"];
2418        let scheduler = Scheduler::named("launch-plan")
2419            .sysctls(SYSCTLS)
2420            .kargs(KARGS)
2421            .cgroup_parent("/ktstr-verifier")
2422            .config_file("/host/configs/verifier.toml")
2423            .sched_args(SCHED_ARGS);
2424
2425        let plan = VerifierSchedulerLaunchPlan::from_scheduler(&scheduler);
2426        assert_eq!(
2427            plan.cmdline_extra,
2428            "sysctl.kernel.ktstr_first=1 sysctl.kernel.ktstr_second=2 \
2429             ktstr_first_karg=one ktstr_second_karg=two"
2430        );
2431        assert_eq!(
2432            plan.include_files,
2433            vec![(
2434                "include-files/verifier.toml".to_string(),
2435                PathBuf::from("/host/configs/verifier.toml"),
2436            )]
2437        );
2438        assert_eq!(
2439            plan.sched_args,
2440            [
2441                "--config",
2442                "/include-files/verifier.toml",
2443                "--mode",
2444                "verifier"
2445            ]
2446        );
2447        assert_eq!(
2448            plan.scheduler_cgroup_parent.as_deref(),
2449            Some("/ktstr-verifier")
2450        );
2451    }
2452
2453    #[test]
2454    fn inline_config_definition_without_test_content_is_not_fabricated() {
2455        use crate::test_support::Scheduler;
2456
2457        let scheduler = Scheduler::named("inline-template")
2458            .config_file_def("--config={file}", "/include-files/inline.json")
2459            .sched_args(&["--mode", "baseline"]);
2460        let plan = VerifierSchedulerLaunchPlan::from_scheduler(&scheduler);
2461        assert!(plan.include_files.is_empty());
2462        assert_eq!(plan.sched_args, ["--mode", "baseline"]);
2463    }
2464
2465    #[test]
2466    fn direct_verifier_api_retains_explicit_cli_only_launch_shape() {
2467        let plan = VerifierSchedulerLaunchPlan::direct(&["--direct".into(), "value".into()]);
2468        assert_eq!(plan.sched_args, ["--direct", "value"]);
2469        assert!(plan.cmdline_extra.is_empty());
2470        assert!(plan.include_files.is_empty());
2471        assert!(plan.scheduler_cgroup_parent.is_none());
2472    }
2473
2474    #[test]
2475    fn terminal_scheduler_stream_requires_completion_and_overrides_live_atomically() {
2476        use crate::vmm::host_comms::BulkDrainResult;
2477        use crate::vmm::wire::{
2478            MSG_TYPE_SCHED_STDERR, MSG_TYPE_SCHED_STDERR_FINAL, ShmEntry,
2479            encode_sched_stream_final_chunk,
2480        };
2481
2482        let entry = |msg_type, payload: Vec<u8>| ShmEntry {
2483            msg_type,
2484            payload,
2485            crc_ok: true,
2486        };
2487        let live = || {
2488            entry(
2489                MSG_TYPE_SCHED_STDERR,
2490                b"live prefix missing one chunk\n".to_vec(),
2491            )
2492        };
2493
2494        let old_guest = BulkDrainResult {
2495            entries: vec![live()],
2496        };
2497        assert_eq!(
2498            scheduler_stream_with_terminal_fallback(
2499                Some(&old_guest),
2500                MSG_TYPE_SCHED_STDERR,
2501                MSG_TYPE_SCHED_STDERR_FINAL,
2502            ),
2503            "live prefix missing one chunk\n",
2504            "absence of new frames must preserve old-guest live semantics"
2505        );
2506
2507        let complete = b"authoritative complete scheduler stderr\n";
2508        let split = 14usize;
2509        let recovered = BulkDrainResult {
2510            entries: vec![
2511                live(),
2512                entry(
2513                    MSG_TYPE_SCHED_STDERR_FINAL,
2514                    encode_sched_stream_final_chunk(complete.len() as u64, 0, &complete[..split]),
2515                ),
2516                entry(
2517                    MSG_TYPE_SCHED_STDERR_FINAL,
2518                    encode_sched_stream_final_chunk(
2519                        complete.len() as u64,
2520                        split as u64,
2521                        &complete[split..],
2522                    ),
2523                ),
2524            ],
2525        };
2526        assert_eq!(
2527            scheduler_stream_with_terminal_fallback(
2528                Some(&recovered),
2529                MSG_TYPE_SCHED_STDERR,
2530                MSG_TYPE_SCHED_STDERR_FINAL,
2531            ),
2532            String::from_utf8_lossy(complete),
2533            "one completion-proven replay must replace the partial live stream"
2534        );
2535
2536        let interrupted = BulkDrainResult {
2537            entries: vec![
2538                live(),
2539                entry(
2540                    MSG_TYPE_SCHED_STDERR_FINAL,
2541                    encode_sched_stream_final_chunk(complete.len() as u64, 0, &complete[..split]),
2542                ),
2543            ],
2544        };
2545        assert_eq!(
2546            scheduler_stream_with_terminal_fallback(
2547                Some(&interrupted),
2548                MSG_TYPE_SCHED_STDERR,
2549                MSG_TYPE_SCHED_STDERR_FINAL,
2550            ),
2551            "live prefix missing one chunk\n",
2552            "an interrupted terminal transaction must never replace live output"
2553        );
2554
2555        let explicit_empty = BulkDrainResult {
2556            entries: vec![
2557                live(),
2558                entry(
2559                    MSG_TYPE_SCHED_STDERR_FINAL,
2560                    encode_sched_stream_final_chunk(0, 0, &[]),
2561                ),
2562            ],
2563        };
2564        assert_eq!(
2565            scheduler_stream_with_terminal_fallback(
2566                Some(&explicit_empty),
2567                MSG_TYPE_SCHED_STDERR,
2568                MSG_TYPE_SCHED_STDERR_FINAL,
2569            ),
2570            "",
2571            "a complete empty transaction is distinct from no final transaction"
2572        );
2573    }
2574
2575    // -----------------------------------------------------------------------
2576    // per-cell result capture + summary table
2577    // -----------------------------------------------------------------------
2578
2579    /// A malformed cell name is skipped (no record); a well-formed
2580    /// 3-segment cell records (scheduler/kernel/topology); and a nextest
2581    /// RETRY of the same cell overwrites its own prior record so the
2582    /// FINAL outcome wins (fail-then-pass -> PASS).
2583    #[test]
2584    fn cell_record_write_read_roundtrip_and_retry_overwrites() {
2585        let dir = tempfile::tempdir().expect("result tempdir");
2586        // Malformed: no verifier/ prefix, and a 2-segment name (no
2587        // <preset> after the kernel) — both skipped.
2588        write_cell_record(dir.path(), "not_a_cell", true, false, &[]);
2589        write_cell_record(dir.path(), "verifier/only/two", true, false, &[]);
2590        // Well-formed cell: fail, then a retry passes -> overwrites.
2591        let name = "verifier/scx_a/kernel_6_14/4cpu-1llc-nosmt";
2592        write_cell_record(dir.path(), name, false, false, &[]);
2593        // The retry passes and carries per-program verified_insns, so the
2594        // final record has both the PASS outcome and the stats.
2595        let stats = [
2596            ProgStats {
2597                name: "ktstr_dispatch".into(),
2598                verified_insns: 321,
2599            },
2600            ProgStats {
2601                name: "ktstr_enqueue".into(),
2602                verified_insns: 123,
2603            },
2604        ];
2605        write_cell_record(dir.path(), name, true, false, &stats);
2606        let recs = read_cell_records(dir.path());
2607        assert_eq!(
2608            recs.len(),
2609            1,
2610            "malformed names skipped; the retry overwrote its own record (one file): {recs:?}",
2611        );
2612        assert_eq!(
2613            std::fs::read_dir(dir.path())
2614                .expect("read result directory")
2615                .count(),
2616            1,
2617            "atomic retry replacement must leave no temporary record behind",
2618        );
2619        assert_eq!(recs[0].scheduler, "scx_a");
2620        assert_eq!(recs[0].kernel, "kernel_6_14");
2621        assert_eq!(recs[0].topology, "4cpu-1llc-nosmt");
2622        assert!(
2623            recs[0].passed,
2624            "final retry outcome (PASS) wins over the earlier FAIL"
2625        );
2626        // Per-program verified_insns survive the JSON roundtrip and reflect
2627        // the final (retry) write, not the earlier stat-less fail.
2628        assert_eq!(recs[0].stats, stats, "stats roundtrip via serde");
2629    }
2630
2631    /// Distinct valid scheduler names may differ only at punctuation which a
2632    /// filesystem sanitizer would collapse. Their cells can finish
2633    /// concurrently, so the raw-name content address must keep both records
2634    /// independent while preserving same-cell retry overwrite semantics.
2635    #[test]
2636    fn concurrent_cell_records_do_not_alias_sanitized_scheduler_names() {
2637        use std::sync::{Arc, Barrier};
2638
2639        let dir = tempfile::tempdir().expect("result tempdir");
2640        let hyphenated = "verifier/foo-bar/kernel_6_14/4cpu-1llc-nosmt";
2641        let underscored = "verifier/foo_bar/kernel_6_14/4cpu-1llc-nosmt";
2642
2643        assert_ne!(
2644            cell_record_filename(hyphenated),
2645            cell_record_filename(underscored),
2646            "the raw cell identity, not a lossy sanitized spelling, keys the record",
2647        );
2648
2649        let barrier = Arc::new(Barrier::new(3));
2650        std::thread::scope(|scope| {
2651            for (name, verified_insns) in [(hyphenated, 111), (underscored, 222)] {
2652                let barrier = Arc::clone(&barrier);
2653                let dir = dir.path();
2654                scope.spawn(move || {
2655                    let stats = [ProgStats {
2656                        name: "ktstr_dispatch".into(),
2657                        verified_insns,
2658                    }];
2659                    barrier.wait();
2660                    write_cell_record(dir, name, true, false, &stats);
2661                });
2662            }
2663            barrier.wait();
2664        });
2665
2666        let mut records = read_cell_records(dir.path());
2667        records.sort_by(|a, b| a.scheduler.cmp(&b.scheduler));
2668        assert_eq!(
2669            records.len(),
2670            2,
2671            "concurrent punctuation-distinct cells must retain two JSON records: {records:?}",
2672        );
2673        assert_eq!(records[0].scheduler, "foo-bar");
2674        assert_eq!(records[0].stats[0].verified_insns, 111);
2675        assert_eq!(records[1].scheduler, "foo_bar");
2676        assert_eq!(records[1].stats[0].verified_insns, 222);
2677    }
2678
2679    /// A verifier SKIP is distinct from both PASS and FAIL in the persisted
2680    /// record, including across JSON round-trip.
2681    #[test]
2682    fn cell_record_skip_roundtrips() {
2683        let dir = std::env::temp_dir().join(format!("ktstr-verif-skip-rec-{}", std::process::id()));
2684        std::fs::create_dir_all(&dir).expect("mk temp dir");
2685        write_cell_record(
2686            &dir,
2687            "verifier/scx_a/kernel_steamos/192cpu-11llc-smt",
2688            false,
2689            true,
2690            &[],
2691        );
2692        let recs = read_cell_records(&dir);
2693        assert_eq!(recs.len(), 1);
2694        assert!(!recs[0].passed);
2695        assert!(recs[0].skipped);
2696        std::fs::remove_dir_all(&dir).ok();
2697    }
2698
2699    /// Per-scheduler PASS/FAIL grids: one section per declared scheduler
2700    /// (rows = topology, cols = kernel), each a tally line plus a bordered
2701    /// grid whose cells are single-width ✓/✗ glyphs (emoji only on the
2702    /// tally line). Covers a multi-scheduler / multi-kernel run, a `-`
2703    /// cell (no record for the triple), the defensive duplicate-record
2704    /// case (a pass AND a fail for one triple -> ✗, any failure means
2705    /// failure), and the box-drawing borders of the bordered table. An
2706    /// empty record set renders nothing.
2707    ///
2708    /// Cell assertions use `contains` on the glyph/word so they hold
2709    /// whether or not ANSI escapes are present — the color policy styles
2710    /// cells on a TTY and under `GITHUB_ACTIONS`/`FORCE_COLOR`, so this
2711    /// test must not assert the ABSENCE of escapes (it runs under GHA in
2712    /// CI). Tally lines are built with plain `format!` (never styled), so
2713    /// exact-match assertions on them are safe everywhere.
2714    #[test]
2715    fn render_result_table_per_scheduler_grids_tally_and_borders() {
2716        let recs = vec![
2717            // scx_a across two kernels and two topologies; the
2718            // (128cpu-4llc-smt, kernel_6_15) cell has no record -> `-`.
2719            VerifierCellRecord {
2720                scheduler: "scx_a".into(),
2721                kernel: "kernel_6_14".into(),
2722                topology: "4cpu-1llc-nosmt".into(),
2723                passed: true,
2724                skipped: false,
2725                stats: vec![],
2726            },
2727            VerifierCellRecord {
2728                scheduler: "scx_a".into(),
2729                kernel: "kernel_6_15".into(),
2730                topology: "4cpu-1llc-nosmt".into(),
2731                passed: false,
2732                skipped: false,
2733                stats: vec![],
2734            },
2735            VerifierCellRecord {
2736                scheduler: "scx_a".into(),
2737                kernel: "kernel_6_14".into(),
2738                topology: "128cpu-4llc-smt".into(),
2739                passed: true,
2740                skipped: false,
2741                stats: vec![],
2742            },
2743            // Explicit kernel/topology SKIP. It renders the same neutral
2744            // `-` as an inapplicable matrix intersection and contributes
2745            // to neither the pass nor fail tally.
2746            VerifierCellRecord {
2747                scheduler: "scx_a".into(),
2748                kernel: "kernel_6_15".into(),
2749                topology: "128cpu-4llc-smt".into(),
2750                passed: false,
2751                skipped: true,
2752                stats: vec![],
2753            },
2754            // scx_b: a duplicate (scheduler, kernel, topology) triple with
2755            // one pass AND one fail -> ✗ (any failure means failure).
2756            VerifierCellRecord {
2757                scheduler: "scx_b".into(),
2758                kernel: "kernel_6_14".into(),
2759                topology: "4cpu-1llc-nosmt".into(),
2760                passed: true,
2761                skipped: false,
2762                stats: vec![],
2763            },
2764            VerifierCellRecord {
2765                scheduler: "scx_b".into(),
2766                kernel: "kernel_6_14".into(),
2767                topology: "4cpu-1llc-nosmt".into(),
2768                passed: false,
2769                skipped: false,
2770                stats: vec![],
2771            },
2772        ];
2773        let out = render_result_table(&recs).expect("non-empty records -> Some");
2774        assert!(
2775            out.contains("verifier results (per scheduler; rows: topology, cols: kernel):"),
2776            "per-scheduler header: {out}"
2777        );
2778        // The grid uses the bordered (UTF8_FULL) table, so box-drawing
2779        // characters must appear — distinct from every other verifier
2780        // table, which stays borderless.
2781        assert!(
2782            ['│', '─', '┼', '├', '┤', '┬', '┴']
2783                .iter()
2784                .any(|c| out.contains(*c)),
2785            "bordered grid must carry box-drawing chars: {out}"
2786        );
2787        // Split into the two scheduler sections at scx_b's tally.
2788        let (a_sec, b_sec) = out.split_once("scx_b:").expect("scx_b section present");
2789        // scx_a tally: 2 ✅ + 1 ❌ (emoji live ONLY on the tally line).
2790        let a_tally = a_sec
2791            .lines()
2792            .find(|l| l.trim_start().starts_with("scx_a:"))
2793            .expect("scx_a tally line");
2794        assert_eq!(
2795            a_tally.trim(),
2796            "scx_a: 2 ✅  1 ❌",
2797            "scx_a tally counts grid cells: {a_tally}"
2798        );
2799        // Kernel is a grid axis (a column per kernel), not folded away.
2800        assert!(
2801            a_sec.contains("kernel_6_14") && a_sec.contains("kernel_6_15"),
2802            "scx_a kernel columns present: {a_sec}"
2803        );
2804        // The (128cpu-4llc-smt, kernel_6_15) triple explicitly SKIPped -> `-`,
2805        // alongside the (128cpu-4llc-smt, kernel_6_14) ✓. The space-padded
2806        // " - " match avoids the hyphens inside topology names.
2807        let a_large = a_sec
2808            .lines()
2809            .find(|l| l.contains("128cpu-4llc-smt"))
2810            .expect("scx_a 128cpu-4llc-smt row");
2811        assert!(
2812            a_large.contains('✓') && a_large.contains(" - "),
2813            "skipped cell renders `-`: {a_large}"
2814        );
2815        // 4cpu-1llc-nosmt row: kernel_6_14 ✓, kernel_6_15 ✗ — the failing
2816        // kernel is named by its own column, no separate failing list.
2817        let a_tiny = a_sec
2818            .lines()
2819            .find(|l| l.contains("4cpu-1llc-nosmt"))
2820            .expect("scx_a 4cpu-1llc-nosmt row");
2821        assert!(
2822            a_tiny.contains('✓') && a_tiny.contains('✗'),
2823            "4cpu-1llc-nosmt row shows the pass and the fail per kernel: {a_tiny}"
2824        );
2825        // scx_b: the duplicate pass+fail triple is ONE cell, counted as a
2826        // fail — any failure means failure, no partial state.
2827        assert!(
2828            b_sec.starts_with(" 0 ✅  1 ❌"),
2829            "duplicate pass+fail triple tallies as one fail: {b_sec}"
2830        );
2831        let b_tiny = b_sec
2832            .lines()
2833            .find(|l| l.contains("4cpu-1llc-nosmt"))
2834            .expect("scx_b 4cpu-1llc-nosmt row");
2835        assert!(
2836            b_tiny.contains('✗') && !b_tiny.contains('✓'),
2837            "a pass+fail duplicate-record cell renders ✗: {b_tiny}"
2838        );
2839        // Summary cells contain only their verdict glyph. Runtime
2840        // measurements belong in the detailed per-cell output and must
2841        // not widen or misalign this matrix.
2842        assert!(
2843            !out.contains('.'),
2844            "summary grid must not render runtime measurements: {out}"
2845        );
2846        // Emoji stay OFF the grid rows (they drift box-drawing columns in
2847        // GitHub's log viewer); the retired glyphs and flat list are gone.
2848        for row in out.lines().filter(|l| l.contains('│')) {
2849            assert!(
2850                !row.contains('✅') && !row.contains('❌'),
2851                "grid rows carry ✓/✗ glyphs, not emoji: {row}"
2852            );
2853        }
2854        assert!(
2855            !out.contains('❎') && !out.contains('🟡') && !out.contains("failing combinations"),
2856            "retired ❎ / 🟡 / failing-combinations list must not appear: {out}"
2857        );
2858        assert!(render_result_table(&[]).is_none(), "empty -> None");
2859    }
2860
2861    /// Result records from v0.40 carried a `dilation` field. Serde ignores
2862    /// that now-unknown field during a rolling upgrade, and the summary
2863    /// still renders a glyph-only verdict cell.
2864    #[test]
2865    fn legacy_record_dilation_is_ignored_by_summary() {
2866        let record: VerifierCellRecord = serde_json::from_str(
2867            r#"{
2868                "scheduler": "scx_a",
2869                "kernel": "kernel_6_14",
2870                "topology": "4cpu-1llc-nosmt",
2871                "passed": true,
2872                "skipped": false,
2873                "stats": [],
2874                "dilation": 11.62
2875            }"#,
2876        )
2877        .expect("v0.40 record with dilation still deserializes");
2878
2879        let out = render_result_table(&[record]).expect("record renders a summary");
2880        assert!(out.contains('✓'), "pass glyph is present: {out}");
2881        assert!(
2882            !out.contains("11.62"),
2883            "legacy dilation must not enter the summary cell: {out}"
2884        );
2885    }
2886
2887    /// Per-scheduler verified_insns tables: one section per declared
2888    /// scheduler; within it rows = BPF program, columns = kernel version,
2889    /// each cell that program's verified_insns across the topologies that
2890    /// ran it — a single number when topology-invariant, `lo..hi` when it
2891    /// varies. A (kernel, program) that reported no stats shows `-`; a
2892    /// kernel that ran a scheduler but produced NO stats at all still gets
2893    /// an all-`-` column; an empty record set renders nothing.
2894    #[test]
2895    fn instruction_count_tables_per_scheduler_kernel_program_range() {
2896        let recs = vec![
2897            // scx_a / kernel_6_14 on two topologies with IDENTICAL counts
2898            // -> the cell collapses to a single number (topology-flat).
2899            VerifierCellRecord {
2900                scheduler: "scx_a".into(),
2901                kernel: "kernel_6_14".into(),
2902                topology: "4cpu-1llc-nosmt".into(),
2903                passed: true,
2904                skipped: false,
2905                stats: vec![
2906                    ProgStats {
2907                        name: "ktstr_dispatch".into(),
2908                        verified_insns: 128,
2909                    },
2910                    ProgStats {
2911                        name: "ktstr_enqueue".into(),
2912                        verified_insns: 64,
2913                    },
2914                ],
2915            },
2916            VerifierCellRecord {
2917                scheduler: "scx_a".into(),
2918                kernel: "kernel_6_14".into(),
2919                topology: "128cpu-4llc-smt".into(),
2920                passed: true,
2921                skipped: false,
2922                stats: vec![
2923                    ProgStats {
2924                        name: "ktstr_dispatch".into(),
2925                        verified_insns: 128,
2926                    },
2927                    ProgStats {
2928                        name: "ktstr_enqueue".into(),
2929                        verified_insns: 64,
2930                    },
2931                ],
2932            },
2933            // scx_a / kernel_6_15: ktstr_dispatch DIFFERS across topologies
2934            // -> `lo..hi` range; ktstr_enqueue is absent on this kernel
2935            // -> `-` in the ktstr_enqueue row's kernel_6_15 column.
2936            VerifierCellRecord {
2937                scheduler: "scx_a".into(),
2938                kernel: "kernel_6_15".into(),
2939                topology: "4cpu-1llc-nosmt".into(),
2940                passed: true,
2941                skipped: false,
2942                stats: vec![ProgStats {
2943                    name: "ktstr_dispatch".into(),
2944                    verified_insns: 130,
2945                }],
2946            },
2947            VerifierCellRecord {
2948                scheduler: "scx_a".into(),
2949                kernel: "kernel_6_15".into(),
2950                topology: "128cpu-4llc-smt".into(),
2951                passed: true,
2952                skipped: false,
2953                stats: vec![ProgStats {
2954                    name: "ktstr_dispatch".into(),
2955                    verified_insns: 150,
2956                }],
2957            },
2958            // scx_b: a separate section (its own declaration).
2959            VerifierCellRecord {
2960                scheduler: "scx_b".into(),
2961                kernel: "kernel_6_14".into(),
2962                topology: "4cpu-1llc-nosmt".into(),
2963                passed: true,
2964                skipped: false,
2965                stats: vec![ProgStats {
2966                    name: "ktstr_dispatch".into(),
2967                    verified_insns: 200,
2968                }],
2969            },
2970            // scx_a / kernel_6_16: a record but NO stats — every cell died
2971            // during BPF load, so no program existed to introspect. It must
2972            // still surface as an all-`-` column, not vanish from the table.
2973            VerifierCellRecord {
2974                scheduler: "scx_a".into(),
2975                kernel: "kernel_6_16".into(),
2976                topology: "4cpu-1llc-nosmt".into(),
2977                passed: false,
2978                skipped: false,
2979                stats: vec![],
2980            },
2981        ];
2982        let out = render_instruction_count_tables(&recs).expect("stats present -> Some");
2983        // One section per declared scheduler.
2984        assert!(
2985            out.contains("scx_a:") && out.contains("scx_b:"),
2986            "one section per declared scheduler: {out}"
2987        );
2988        // Rows = BPF programs; columns = kernel version.
2989        assert!(
2990            out.contains("ktstr_dispatch") && out.contains("ktstr_enqueue"),
2991            "BPF-program rows: {out}"
2992        );
2993        assert!(
2994            out.contains("kernel_6_14") && out.contains("kernel_6_15"),
2995            "kernel-version columns: {out}"
2996        );
2997        // Topology folded into the cell as a range: flat -> "128",
2998        // varies across topologies -> "130..150".
2999        assert!(
3000            out.contains("128"),
3001            "topology-flat cell is a single number: {out}"
3002        );
3003        assert!(
3004            out.contains("130..150"),
3005            "topology-varying cell is a lo..hi range: {out}"
3006        );
3007        assert!(
3008            out.contains("64") && out.contains("200"),
3009            "other counts render: {out}"
3010        );
3011        // ktstr_enqueue reported no stats on kernel_6_15 -> `-`.
3012        assert!(
3013            out.contains('-'),
3014            "a (kernel, program) with no stats renders '-': {out}"
3015        );
3016        // kernel_6_16 ran scx_a but produced NO stats -> it stays as a
3017        // column (named in the header) rather than vanishing. Its all-`-`
3018        // column shows up as a `-` in every program row: the ktstr_enqueue
3019        // row is absent on both kernel_6_15 and kernel_6_16, so it carries
3020        // exactly two `-` cells (the box-drawing borders use `─`/`│`, not
3021        // ASCII `-`, so they do not count).
3022        let header = out
3023            .lines()
3024            .find(|l| l.contains("program") && l.contains("kernel_6_16"))
3025            .expect("stats-less kernel kept as a column in the header");
3026        assert!(
3027            header.contains("kernel_6_14") && header.contains("kernel_6_15"),
3028            "every kernel that ran is a column: {header}"
3029        );
3030        let enqueue_row = out
3031            .lines()
3032            .find(|l| l.contains("ktstr_enqueue"))
3033            .expect("ktstr_enqueue row present");
3034        assert_eq!(
3035            enqueue_row.matches('-').count(),
3036            2,
3037            "ktstr_enqueue is absent on kernel_6_15 and kernel_6_16: {enqueue_row}"
3038        );
3039        // Topology is NOT a table axis (folded into the range), so no
3040        // topology label appears in the output.
3041        assert!(
3042            !out.contains("4cpu-1llc-nosmt") && !out.contains("128cpu-4llc-smt"),
3043            "topology is not a table axis: {out}"
3044        );
3045
3046        // No record carries stats -> nothing to render.
3047        let bare = vec![VerifierCellRecord {
3048            scheduler: "scx_a".into(),
3049            kernel: "kernel_6_14".into(),
3050            topology: "4cpu-1llc-nosmt".into(),
3051            passed: false,
3052            skipped: false,
3053            stats: vec![],
3054        }];
3055        assert!(
3056            render_instruction_count_tables(&bare).is_none(),
3057            "no stats -> None"
3058        );
3059    }
3060
3061    /// classify_run_outcome maps nextest's exit + the records into a
3062    /// [`RunOutcome`]: a successful run with records is `Success`; a
3063    /// successful-but-empty run is a diagnosed `Failed` (friendly
3064    /// no-such-scheduler when --scheduler was set, no-cells-ran otherwise);
3065    /// a nonzero exit whose failed cell the grid ALREADY shows is a
3066    /// message-less `SilentExit` carrying nextest's code, while a nonzero
3067    /// exit with NO failed cell (build/internal failure) or a signal exit
3068    /// stays a descriptive `Failed`.
3069    #[test]
3070    fn classify_run_outcome_cases() {
3071        use RunOutcome::{Failed, SilentExit, Success};
3072
3073        // Records present + success -> Success regardless of --scheduler.
3074        assert_eq!(
3075            classify_run_outcome(true, false, false, None, Some(0)),
3076            Success
3077        );
3078        assert_eq!(
3079            classify_run_outcome(true, false, false, Some("ktstr_sched"), Some(0)),
3080            Success
3081        );
3082
3083        // Success + empty + --scheduler -> friendly "no such scheduler".
3084        // Reachable ONLY because --no-tests=pass turns a 0-cell match into
3085        // exit 0; under the old `auto` default a 0-match exited 4 -> the
3086        // failure arm, leaving this message dead.
3087        let Failed(e) = classify_run_outcome(true, true, false, Some("nope"), Some(0)) else {
3088            panic!("expected Failed");
3089        };
3090        assert!(
3091            e.contains("--scheduler \"nope\"") && e.contains("matched no verifier cell"),
3092            "scheduler-empty diagnostic: {e}"
3093        );
3094        assert!(
3095            e.contains("declared topology constraints or verifier-only exclusions")
3096                && !e.contains("host"),
3097            "scheduler-empty diagnostic must describe declaration gates, not host capacity: {e}"
3098        );
3099
3100        // Success + empty + no --scheduler -> "no cells ran" diagnosis
3101        // (must NOT silently succeed under --no-tests=pass).
3102        let Failed(e) = classify_run_outcome(true, true, false, None, Some(0)) else {
3103            panic!("expected Failed");
3104        };
3105        assert!(
3106            e.contains("no verifier cells ran") && e.contains("declare_scheduler!"),
3107            "no-cells diagnostic: {e}"
3108        );
3109        assert!(
3110            e.contains("topology constraints or verifier-only exclusions") && !e.contains("host"),
3111            "no-cells diagnostic must describe declaration gates, not host capacity: {e}"
3112        );
3113
3114        // Nonzero exit WITH a failed cell + numeric code -> SilentExit: the
3115        // printed grid already shows the ✗, so carry nextest's code out
3116        // without a redundant stderr line that would interleave mid-report.
3117        assert_eq!(
3118            classify_run_outcome(false, false, true, None, Some(100)),
3119            SilentExit(100)
3120        );
3121
3122        // Nonzero exit with NO failed cell (build/internal failure) keeps
3123        // the descriptive message — nothing in the grid to point at.
3124        let Failed(e) = classify_run_outcome(false, true, false, None, Some(101)) else {
3125            panic!("expected Failed");
3126        };
3127        assert_eq!(e, "cargo nextest run exited with 101");
3128
3129        // A signal exit (no numeric code) keeps the message EVEN with a
3130        // failed cell present — there is no code to carry out silently.
3131        let Failed(e) = classify_run_outcome(false, false, true, None, None) else {
3132            panic!("expected Failed");
3133        };
3134        assert_eq!(e, "cargo nextest run exited with signal");
3135    }
3136
3137    /// build_nextest_args carries the flags that make the friendly
3138    /// diagnostic reachable: `--run-ignored all` (cells are ignore-gated)
3139    /// and `--no-tests pass` (a 0-cell selection exits 0 so
3140    /// classify_run_outcome runs instead of nextest's exit-4). Guards
3141    /// against a future edit silently dropping either, plus the
3142    /// profile-before-forwarded-args ordering.
3143    #[test]
3144    fn build_nextest_args_carries_load_bearing_flags() {
3145        let args = build_nextest_args(None, &[]);
3146        let ri = args
3147            .iter()
3148            .position(|a| a == "--run-ignored")
3149            .expect("--run-ignored present");
3150        assert_eq!(args[ri + 1], "all", "--run-ignored all");
3151        assert!(
3152            args.iter().any(|a| a == "--no-tests=pass"),
3153            "--no-tests=pass present as a single `=` token (the only \
3154             spelling every supported nextest parses): {args:?}",
3155        );
3156        assert!(
3157            args.iter()
3158                .any(|a| a == "test(/^verifier/) & !test(/^verifier::/)"),
3159            "verifier-cell filter present: {args:?}"
3160        );
3161
3162        // --profile <NAME> is emitted before forwarded args so a forwarded
3163        // token cannot shadow it.
3164        let args = build_nextest_args(Some("ci"), &["--features".to_string(), "wprof".to_string()]);
3165        let p = args
3166            .iter()
3167            .position(|a| a == "--profile")
3168            .expect("--profile present");
3169        assert_eq!(args[p + 1], "ci");
3170        let f = args
3171            .iter()
3172            .position(|a| a == "--features")
3173            .expect("forwarded --features present");
3174        assert!(p < f, "profile emitted before forwarded args: {args:?}");
3175    }
3176
3177    // -----------------------------------------------------------------------
3178    // scheduler attach verdict
3179    // -----------------------------------------------------------------------
3180
3181    /// attach_outcome_from_messages positive-confirmation rule:
3182    /// Died > NotAttached > SchedulerAttached > Unconfirmed. A
3183    /// PayloadStarting frame alone is Unconfirmed (FAIL), because the
3184    /// guest emits it for schedulerless runs. Corrupt / empty /
3185    /// non-LIFECYCLE / unknown frames are skipped.
3186    #[test]
3187    fn attach_outcome_from_lifecycle_frames() {
3188        use crate::vmm::host_comms::BulkDrainResult;
3189        use crate::vmm::wire::{LifecyclePhase, MSG_TYPE_LIFECYCLE, ShmEntry};
3190
3191        let frame = |phase: LifecyclePhase, reason: &str| -> ShmEntry {
3192            let mut payload = vec![phase.wire_value()];
3193            payload.extend_from_slice(reason.as_bytes());
3194            ShmEntry {
3195                msg_type: MSG_TYPE_LIFECYCLE,
3196                payload,
3197                crc_ok: true,
3198            }
3199        };
3200        let drain = |entries: Vec<ShmEntry>| BulkDrainResult { entries };
3201
3202        // No frames at all -> Unconfirmed (guest vanished before any
3203        // phase; e.g. an early kernel panic that reboots via panic=-1).
3204        assert_eq!(
3205            attach_outcome_from_messages(None),
3206            AttachOutcome::Unconfirmed,
3207        );
3208
3209        // Reached init but no definitive attach -> Unconfirmed.
3210        let init_only = drain(vec![frame(LifecyclePhase::InitStarted, "")]);
3211        assert_eq!(
3212            attach_outcome_from_messages(Some(&init_only)),
3213            AttachOutcome::Unconfirmed,
3214        );
3215
3216        // Schedulerless runs also reach PayloadStarting. That is not
3217        // positive attach evidence.
3218        let schedulerless = drain(vec![
3219            frame(LifecyclePhase::InitStarted, ""),
3220            frame(LifecyclePhase::PayloadStarting, ""),
3221        ]);
3222        assert_eq!(
3223            attach_outcome_from_messages(Some(&schedulerless)),
3224            AttachOutcome::Unconfirmed,
3225        );
3226
3227        // Only the explicit SchedulerAttached frame confirms attach.
3228        let attached = drain(vec![
3229            frame(LifecyclePhase::InitStarted, ""),
3230            frame(LifecyclePhase::SchedulerAttached, ""),
3231            frame(LifecyclePhase::PayloadStarting, ""),
3232        ]);
3233        assert_eq!(
3234            attach_outcome_from_messages(Some(&attached)),
3235            AttachOutcome::Attached,
3236        );
3237
3238        // SchedulerNotAttached carries its reason suffix verbatim.
3239        let not_attached = drain(vec![frame(LifecyclePhase::SchedulerNotAttached, "timeout")]);
3240        assert_eq!(
3241            attach_outcome_from_messages(Some(&not_attached)),
3242            AttachOutcome::NotAttached("timeout".to_string()),
3243        );
3244
3245        // A failure frame wins over a (defensively) co-present
3246        // SchedulerAttached.
3247        let fail_beats_positive = drain(vec![
3248            frame(LifecyclePhase::SchedulerAttached, ""),
3249            frame(LifecyclePhase::SchedulerNotAttached, "sysfs absent"),
3250        ]);
3251        assert_eq!(
3252            attach_outcome_from_messages(Some(&fail_beats_positive)),
3253            AttachOutcome::NotAttached("sysfs absent".to_string()),
3254        );
3255
3256        // SchedulerDied wins over NotAttached, in BOTH orders.
3257        for entries in [
3258            vec![
3259                frame(LifecyclePhase::SchedulerNotAttached, "timeout"),
3260                frame(LifecyclePhase::SchedulerDied, ""),
3261            ],
3262            vec![
3263                frame(LifecyclePhase::SchedulerDied, ""),
3264                frame(LifecyclePhase::SchedulerNotAttached, "timeout"),
3265            ],
3266        ] {
3267            let d = drain(entries);
3268            assert_eq!(attach_outcome_from_messages(Some(&d)), AttachOutcome::Died);
3269        }
3270
3271        // Died wins even over SchedulerAttached.
3272        let died_beats_positive = drain(vec![
3273            frame(LifecyclePhase::SchedulerAttached, ""),
3274            frame(LifecyclePhase::SchedulerDied, ""),
3275        ]);
3276        assert_eq!(
3277            attach_outcome_from_messages(Some(&died_beats_positive)),
3278            AttachOutcome::Died,
3279        );
3280
3281        // Skipped frames (corrupt crc / empty payload / non-LIFECYCLE /
3282        // unknown discriminant) must NOT suppress a real
3283        // SchedulerAttached: pairing each with a valid SchedulerAttached
3284        // must still resolve Attached — proving the frame was skipped,
3285        // not acted on (a corrupt/non-LIFECYCLE Died byte would otherwise
3286        // force Died).
3287        let corrupt_died = ShmEntry {
3288            msg_type: MSG_TYPE_LIFECYCLE,
3289            payload: vec![LifecyclePhase::SchedulerDied.wire_value()],
3290            crc_ok: false,
3291        };
3292        let empty = ShmEntry {
3293            msg_type: MSG_TYPE_LIFECYCLE,
3294            payload: Vec::new(),
3295            crc_ok: true,
3296        };
3297        let non_lifecycle_died = ShmEntry {
3298            msg_type: MSG_TYPE_LIFECYCLE + 1,
3299            payload: vec![LifecyclePhase::SchedulerDied.wire_value()],
3300            crc_ok: true,
3301        };
3302        let unknown_phase = ShmEntry {
3303            msg_type: MSG_TYPE_LIFECYCLE,
3304            payload: vec![250],
3305            crc_ok: true,
3306        };
3307        for skipped in [corrupt_died, empty, non_lifecycle_died, unknown_phase] {
3308            let d = drain(vec![skipped, frame(LifecyclePhase::SchedulerAttached, "")]);
3309            assert_eq!(
3310                attach_outcome_from_messages(Some(&d)),
3311                AttachOutcome::Attached,
3312                "a skipped frame must not suppress a valid SchedulerAttached",
3313            );
3314        }
3315    }
3316
3317    /// A schedulerless verifier run can reach Phase 5 and make progress
3318    /// under the kernel fallback policy. PayloadStarting plus
3319    /// WorkloadDispatched therefore must not be enough to PASS without
3320    /// the definitive SchedulerAttached frame.
3321    #[test]
3322    fn schedulerless_payload_progress_cannot_pass_verification() {
3323        use crate::vmm::host_comms::BulkDrainResult;
3324        use crate::vmm::wire::{LifecyclePhase, MSG_TYPE_LIFECYCLE, ShmEntry};
3325
3326        let messages = BulkDrainResult {
3327            entries: [
3328                LifecyclePhase::PayloadStarting,
3329                LifecyclePhase::WorkloadDispatched,
3330            ]
3331            .into_iter()
3332            .map(|phase| ShmEntry {
3333                msg_type: MSG_TYPE_LIFECYCLE,
3334                payload: vec![phase.wire_value()],
3335                crc_ok: true,
3336            })
3337            .collect(),
3338        };
3339        let result = VerifierVmResult {
3340            stats: Vec::new(),
3341            scheduler_log: String::new(),
3342            scheduler_stdout: String::new(),
3343            scheduler_stderr: String::new(),
3344            attach: attach_outcome_from_messages(Some(&messages)),
3345            dispatched: dispatch_confirmed_from_messages(Some(&messages)),
3346            scheduler_exited: scheduler_exited_from_messages(Some(&messages)),
3347            guest_exit_code: Some(0),
3348            timed_out: false,
3349            crash_message: None,
3350            dilation: None,
3351        };
3352
3353        assert_eq!(result.attach, AttachOutcome::Unconfirmed);
3354        assert!(
3355            result.dispatched,
3356            "the fallback-running probe made progress"
3357        );
3358        assert!(
3359            result
3360                .cell_verdict()
3361                .expect_err("no scheduler attached, so the cell must fail")
3362                .contains("no SchedulerAttached frame"),
3363        );
3364    }
3365
3366    /// A scheduler that attached and then died cannot PASS by combining
3367    /// its historical attach frame with probe progress made later through
3368    /// the kernel's SCHED_EXT fallback.
3369    #[test]
3370    fn attached_then_scheduler_exit_cannot_pass_verification() {
3371        use crate::vmm::host_comms::BulkDrainResult;
3372        use crate::vmm::wire::{LifecyclePhase, MSG_TYPE_LIFECYCLE, MSG_TYPE_SCHED_EXIT, ShmEntry};
3373
3374        let lifecycle = |phase: LifecyclePhase| ShmEntry {
3375            msg_type: MSG_TYPE_LIFECYCLE,
3376            payload: vec![phase.wire_value()],
3377            crc_ok: true,
3378        };
3379        let messages = BulkDrainResult {
3380            entries: vec![
3381                lifecycle(LifecyclePhase::SchedulerAttached),
3382                lifecycle(LifecyclePhase::WorkloadDispatched),
3383                ShmEntry {
3384                    msg_type: MSG_TYPE_SCHED_EXIT,
3385                    payload: 1i32.to_le_bytes().to_vec(),
3386                    crc_ok: true,
3387                },
3388            ],
3389        };
3390        let result = VerifierVmResult {
3391            stats: Vec::new(),
3392            scheduler_log: String::new(),
3393            scheduler_stdout: String::new(),
3394            scheduler_stderr: String::new(),
3395            attach: attach_outcome_from_messages(Some(&messages)),
3396            dispatched: dispatch_confirmed_from_messages(Some(&messages)),
3397            scheduler_exited: scheduler_exited_from_messages(Some(&messages)),
3398            guest_exit_code: Some(0),
3399            timed_out: false,
3400            crash_message: None,
3401            dilation: None,
3402        };
3403
3404        assert_eq!(result.attach, AttachOutcome::Attached);
3405        assert!(result.dispatched);
3406        assert!(result.scheduler_exited);
3407        assert!(
3408            result
3409                .cell_verdict()
3410                .expect_err("post-attach scheduler exit must fail")
3411                .contains("scheduler exited"),
3412        );
3413
3414        let mut torn = messages;
3415        torn.entries.last_mut().unwrap().crc_ok = false;
3416        assert!(
3417            !scheduler_exited_from_messages(Some(&torn)),
3418            "crc-invalid scheduler-exit frame is not evidence",
3419        );
3420    }
3421
3422    #[test]
3423    fn terminal_guest_exit_requires_explicit_valid_exit_frame() {
3424        use crate::vmm::host_comms::BulkDrainResult;
3425        use crate::vmm::wire::{LifecyclePhase, MSG_TYPE_EXIT, MSG_TYPE_LIFECYCLE, ShmEntry};
3426
3427        assert_eq!(terminal_guest_exit_from_messages(None), None);
3428
3429        let lifecycle_only = BulkDrainResult {
3430            entries: vec![ShmEntry {
3431                msg_type: MSG_TYPE_LIFECYCLE,
3432                payload: vec![LifecyclePhase::WorkloadDispatched.wire_value()],
3433                crc_ok: true,
3434            }],
3435        };
3436        assert_eq!(
3437            terminal_guest_exit_from_messages(Some(&lifecycle_only)),
3438            None,
3439            "a generic shutdown plus lifecycle transcript is not terminal evidence",
3440        );
3441
3442        for invalid in [
3443            ShmEntry {
3444                msg_type: MSG_TYPE_EXIT,
3445                payload: 0i32.to_le_bytes().to_vec(),
3446                crc_ok: false,
3447            },
3448            ShmEntry {
3449                msg_type: MSG_TYPE_EXIT,
3450                payload: vec![0, 0, 0],
3451                crc_ok: true,
3452            },
3453        ] {
3454            let drain = BulkDrainResult {
3455                entries: vec![invalid],
3456            };
3457            assert_eq!(terminal_guest_exit_from_messages(Some(&drain)), None);
3458        }
3459
3460        let explicit = BulkDrainResult {
3461            entries: vec![ShmEntry {
3462                msg_type: MSG_TYPE_EXIT,
3463                payload: 7i32.to_le_bytes().to_vec(),
3464                crc_ok: true,
3465            }],
3466        };
3467        assert_eq!(terminal_guest_exit_from_messages(Some(&explicit)), Some(7));
3468    }
3469
3470    /// dispatch_confirmed_from_messages: true only when a crc-ok,
3471    /// non-empty WorkloadDispatched frame is present; false for None / no
3472    /// such frame / corrupt / empty / non-LIFECYCLE.
3473    #[test]
3474    fn dispatch_confirmed_from_lifecycle_frames() {
3475        use crate::vmm::host_comms::BulkDrainResult;
3476        use crate::vmm::wire::{LifecyclePhase, MSG_TYPE_LIFECYCLE, ShmEntry};
3477
3478        let frame = |phase: LifecyclePhase| -> ShmEntry {
3479            ShmEntry {
3480                msg_type: MSG_TYPE_LIFECYCLE,
3481                payload: vec![phase.wire_value()],
3482                crc_ok: true,
3483            }
3484        };
3485        let drain = |entries: Vec<ShmEntry>| BulkDrainResult { entries };
3486
3487        // No frames at all -> false.
3488        assert!(!dispatch_confirmed_from_messages(None));
3489
3490        // SchedulerAttached but no WorkloadDispatched -> false.
3491        let attached_only = drain(vec![frame(LifecyclePhase::SchedulerAttached)]);
3492        assert!(!dispatch_confirmed_from_messages(Some(&attached_only)));
3493
3494        // WorkloadDispatched present -> true.
3495        let dispatched = drain(vec![
3496            frame(LifecyclePhase::SchedulerAttached),
3497            frame(LifecyclePhase::WorkloadDispatched),
3498        ]);
3499        assert!(dispatch_confirmed_from_messages(Some(&dispatched)));
3500
3501        // Corrupt crc / empty payload / non-LIFECYCLE WorkloadDispatched
3502        // frames are skipped and must not confirm dispatch.
3503        let corrupt = ShmEntry {
3504            msg_type: MSG_TYPE_LIFECYCLE,
3505            payload: vec![LifecyclePhase::WorkloadDispatched.wire_value()],
3506            crc_ok: false,
3507        };
3508        let empty = ShmEntry {
3509            msg_type: MSG_TYPE_LIFECYCLE,
3510            payload: Vec::new(),
3511            crc_ok: true,
3512        };
3513        let non_lifecycle = ShmEntry {
3514            msg_type: MSG_TYPE_LIFECYCLE + 1,
3515            payload: vec![LifecyclePhase::WorkloadDispatched.wire_value()],
3516            crc_ok: true,
3517        };
3518        for skipped in [corrupt, empty, non_lifecycle] {
3519            let d = drain(vec![skipped]);
3520            assert!(
3521                !dispatch_confirmed_from_messages(Some(&d)),
3522                "a corrupt/empty/non-LIFECYCLE frame must not confirm dispatch",
3523            );
3524        }
3525    }
3526
3527    /// VerifierVmResult::cell_verdict gate order + messages: timed_out >
3528    /// scheduler exit > attach failure > dispatch failure > terminal exit
3529    /// > PASS.
3530    #[test]
3531    fn cell_verdict_gate_order_and_messages() {
3532        let base = |attach: AttachOutcome, dispatched: bool, timed_out: bool| VerifierVmResult {
3533            stats: Vec::new(),
3534            scheduler_log: String::new(),
3535            scheduler_stdout: String::new(),
3536            scheduler_stderr: String::new(),
3537            attach,
3538            dispatched,
3539            scheduler_exited: false,
3540            guest_exit_code: Some(0),
3541            timed_out,
3542            crash_message: None,
3543            dilation: None,
3544        };
3545
3546        // Verified + attached + dispatched, no scheduler-exit evidence,
3547        // and explicit terminal exit 0 -> PASS.
3548        assert_eq!(
3549            base(AttachOutcome::Attached, true, false).cell_verdict(),
3550            Ok(()),
3551        );
3552
3553        // Attached but 0-dispatch -> FAIL naming the dispatch gate.
3554        let no_dispatch = base(AttachOutcome::Attached, false, false).cell_verdict();
3555        assert!(
3556            no_dispatch
3557                .as_ref()
3558                .unwrap_err()
3559                .contains("did not dispatch"),
3560            "dispatch gate must name the failure: {no_dispatch:?}",
3561        );
3562
3563        let mut exited = base(AttachOutcome::Attached, true, false);
3564        exited.scheduler_exited = true;
3565        assert!(
3566            exited
3567                .cell_verdict()
3568                .expect_err("scheduler exit must fail")
3569                .contains("scheduler exited"),
3570        );
3571
3572        let mut guest_failed = base(AttachOutcome::Attached, true, false);
3573        guest_failed.guest_exit_code = Some(1);
3574        assert!(
3575            guest_failed
3576                .cell_verdict()
3577                .expect_err("non-zero terminal exit must fail")
3578                .contains("exit code 1"),
3579        );
3580
3581        let mut no_terminal_exit = base(AttachOutcome::Attached, true, false);
3582        no_terminal_exit.guest_exit_code = None;
3583        assert!(
3584            no_terminal_exit
3585                .cell_verdict()
3586                .expect_err("missing terminal frame must fail")
3587                .contains("did not publish"),
3588        );
3589
3590        // Attach failure -> FAIL naming attach, even if dispatched is
3591        // (defensively) true.
3592        let attach_fail = base(AttachOutcome::Died, true, false).cell_verdict();
3593        assert!(
3594            attach_fail
3595                .as_ref()
3596                .unwrap_err()
3597                .contains("did not turn on"),
3598            "attach gate must win over dispatch: {attach_fail:?}",
3599        );
3600
3601        // timed_out wins over everything, even a clean attach + dispatch.
3602        // With a CONFIRMED attach the historical post-attach wording is
3603        // preserved verbatim.
3604        let hung = base(AttachOutcome::Attached, true, true).cell_verdict();
3605        assert_eq!(
3606            hung,
3607            Err("VM timed out (hung after attach, before exit)".to_string()),
3608            "timed_out + Attached keeps the exact post-attach message: {hung:?}",
3609        );
3610
3611        // A hang WITHOUT a confirmed attach (scheduler died / never
3612        // reached `enabled` / unconfirmed during BPF load, then wedged)
3613        // must NOT claim "after attach": it says the timeout carried no
3614        // confirmed attach and folds in the attach failure reason, while
3615        // still containing the "timed out" substring the gate-order
3616        // machinery keys on.
3617        for attach in [
3618            AttachOutcome::Died,
3619            AttachOutcome::NotAttached(String::new()),
3620            AttachOutcome::NotAttached("sysfs absent".to_string()),
3621            AttachOutcome::Unconfirmed,
3622        ] {
3623            let verdict = base(attach.clone(), false, true).cell_verdict();
3624            let msg = verdict.as_ref().unwrap_err();
3625            assert!(
3626                msg.contains("timed out"),
3627                "timeout must keep the 'timed out' substring for {attach:?}: {verdict:?}",
3628            );
3629            assert!(
3630                msg.contains("no confirmed scheduler attach"),
3631                "un-attached timeout must say so for {attach:?}: {verdict:?}",
3632            );
3633            assert!(
3634                !msg.contains("hung after attach"),
3635                "un-attached timeout must not claim post-attach for {attach:?}: {verdict:?}",
3636            );
3637        }
3638
3639        // timed_out still outranks the crash_message gate.
3640        let mut hung_crash = base(AttachOutcome::Died, false, true);
3641        hung_crash.crash_message = Some("PANIC: guest infra fault".to_string());
3642        let verdict = hung_crash.cell_verdict();
3643        assert!(
3644            verdict.as_ref().unwrap_err().contains("timed out"),
3645            "timed_out must outrank crash_message: {verdict:?}",
3646        );
3647
3648        // Attach failure outranks a co-present dispatch failure (root
3649        // cause reported first).
3650        let both = base(AttachOutcome::Died, false, false).cell_verdict();
3651        assert!(
3652            both.as_ref().unwrap_err().contains("did not turn on"),
3653            "attach failure reported before dispatch failure: {both:?}",
3654        );
3655
3656        // A guest crash message (a self-describing infra fault — e.g. the
3657        // AP-bring-up gap the boot retry exhausted) is surfaced VERBATIM
3658        // and outranks the attach gate: the scheduler never ran, so
3659        // "did not turn on" would misattribute the infra failure. Sits
3660        // below timed_out (a hang has no crash message) but above attach.
3661        let mut crashed = base(AttachOutcome::NotAttached(String::new()), false, false);
3662        crashed.crash_message =
3663            Some("CPUs [4] failed to come online (AP bring-up failed; 127/128 online)".to_string());
3664        let verdict = crashed.cell_verdict();
3665        assert_eq!(
3666            verdict,
3667            Err("CPUs [4] failed to come online (AP bring-up failed; 127/128 online)".to_string()),
3668            "a crash message must be surfaced verbatim, above the attach gate",
3669        );
3670        assert!(
3671            !verdict.as_ref().unwrap_err().contains("did not turn on"),
3672            "crash message must not be masked by the attach gate: {verdict:?}",
3673        );
3674    }
3675
3676    /// AttachOutcome::failure_reason surfaces (None when attached, the
3677    /// distinct Died / NotAttached reasons otherwise).
3678    #[test]
3679    fn attach_outcome_failure_reason() {
3680        assert_eq!(AttachOutcome::Attached.failure_reason(), None);
3681        assert!(
3682            AttachOutcome::Died
3683                .failure_reason()
3684                .unwrap()
3685                .contains("exited during BPF load"),
3686        );
3687        assert!(
3688            AttachOutcome::NotAttached(String::new())
3689                .failure_reason()
3690                .unwrap()
3691                .contains("never reached sched_ext 'enabled'"),
3692        );
3693        assert_eq!(
3694            AttachOutcome::NotAttached("sysfs absent".to_string()).failure_reason(),
3695            Some("scheduler never reached sched_ext 'enabled': sysfs absent".to_string()),
3696        );
3697        assert!(
3698            AttachOutcome::Unconfirmed
3699                .failure_reason()
3700                .unwrap()
3701                .contains("attach unconfirmed"),
3702        );
3703    }
3704
3705    /// A timed-out run shows UNKNOWN (not attached), overriding the
3706    /// attach line — catches a post-attach teardown hang (the frame scan
3707    /// + Unconfirmed already handle a guest that vanishes before attach).
3708    #[test]
3709    fn format_verifier_output_timed_out_shows_unknown() {
3710        let result = VerifierVmResult {
3711            stats: Vec::new(),
3712            scheduler_log: String::new(),
3713            scheduler_stdout: String::new(),
3714            scheduler_stderr: String::new(),
3715            attach: AttachOutcome::Attached,
3716            dispatched: false,
3717            scheduler_exited: false,
3718            guest_exit_code: None,
3719            timed_out: true,
3720            crash_message: None,
3721            dilation: None,
3722        };
3723        let out = format_verifier_output("verifier", &result, false);
3724        assert!(
3725            out.contains("scheduler: UNKNOWN — VM timed out"),
3726            "timed-out run must show UNKNOWN: {out}",
3727        );
3728        assert!(
3729            !out.contains("scheduler: attached"),
3730            "timed-out run must not claim attached: {out}",
3731        );
3732    }
3733
3734    /// An attached-but-not-dispatched run shows the attach line AND a
3735    /// "dispatch: NOT CONFIRMED" line — the signal that the scheduler
3736    /// turned on but never dispatched the injected workload.
3737    /// Guards the `format_verifier_output` dispatched==false render branch,
3738    /// which the snapshot tests (dispatched==true / Died) do not reach.
3739    #[test]
3740    fn format_verifier_output_attached_not_dispatched_shows_not_confirmed() {
3741        let result = VerifierVmResult {
3742            stats: Vec::new(),
3743            scheduler_log: String::new(),
3744            scheduler_stdout: String::new(),
3745            scheduler_stderr: String::new(),
3746            attach: AttachOutcome::Attached,
3747            dispatched: false,
3748            scheduler_exited: false,
3749            guest_exit_code: Some(1),
3750            timed_out: false,
3751            crash_message: None,
3752            dilation: None,
3753        };
3754        let out = format_verifier_output("verifier", &result, false);
3755        assert!(
3756            out.contains("scheduler: attached"),
3757            "attached run must show the attach line: {out}",
3758        );
3759        assert!(
3760            out.contains("dispatch: NOT CONFIRMED"),
3761            "attached-but-not-dispatched must render the NOT CONFIRMED signal: {out}",
3762        );
3763    }
3764
3765    #[test]
3766    fn format_verifier_output_omits_verification_time() {
3767        let result = VerifierVmResult {
3768            stats: Vec::new(),
3769            scheduler_log: String::new(),
3770            scheduler_stdout: String::new(),
3771            scheduler_stderr: "processed 1234 insns (limit 1000000) \
3772                max_states_per_insn 5 total_states 200 peak_states 50 mark_read 10\n\
3773                verification time 424242 usec\n\
3774                stack depth 32+0\n"
3775                .to_string(),
3776            attach: AttachOutcome::Attached,
3777            dispatched: true,
3778            scheduler_exited: false,
3779            guest_exit_code: Some(0),
3780            timed_out: false,
3781            crash_message: None,
3782            dilation: None,
3783        };
3784        let out = format_verifier_output("verifier", &result, false);
3785        assert!(out.contains("processed=1234  states=50/200  stack=32+0"));
3786        assert!(
3787            !out.contains("time=") && !out.contains("424242"),
3788            "derived verifier output must not contain environment-dependent time: {out}",
3789        );
3790    }
3791
3792    // -----------------------------------------------------------------------
3793    // parse_verifier_stats
3794    // -----------------------------------------------------------------------
3795
3796    #[test]
3797    fn parse_verifier_stats_full_line() {
3798        let log = "processed 1234 insns (limit 1000000) max_states_per_insn 5 total_states 200 peak_states 50 mark_read 10\nverification time 42 usec\nstack depth 32+0\n";
3799        let vs = parse_verifier_stats(log);
3800        assert_eq!(vs.processed_insns, 1234);
3801        assert_eq!(vs.total_states, 200);
3802        assert_eq!(vs.peak_states, 50);
3803        assert_eq!(vs.stack_depth.as_deref(), Some("32+0"));
3804    }
3805
3806    #[test]
3807    fn parse_verifier_stats_insns_only() {
3808        let log = "processed 500 insns (limit 1000000) max_states_per_insn 1 total_states 10 peak_states 3 mark_read 0\n";
3809        let vs = parse_verifier_stats(log);
3810        assert_eq!(vs.processed_insns, 500);
3811        assert_eq!(vs.total_states, 10);
3812        assert_eq!(vs.peak_states, 3);
3813        assert!(vs.stack_depth.is_none());
3814    }
3815
3816    #[test]
3817    fn parse_verifier_stats_empty() {
3818        let vs = parse_verifier_stats("");
3819        assert_eq!(vs.processed_insns, 0);
3820        assert_eq!(vs.total_states, 0);
3821        assert_eq!(vs.peak_states, 0);
3822        assert!(vs.stack_depth.is_none());
3823    }
3824
3825    #[test]
3826    fn parse_verifier_stats_garbage_lines() {
3827        let log = "some random output\nnot a stats line\n";
3828        let vs = parse_verifier_stats(log);
3829        assert_eq!(vs.processed_insns, 0);
3830        assert_eq!(vs.total_states, 0);
3831    }
3832
3833    #[test]
3834    fn parse_verifier_stats_ignores_time_without_insns() {
3835        let log = "verification time 100 usec\nstack depth 64\n";
3836        let vs = parse_verifier_stats(log);
3837        assert_eq!(vs.processed_insns, 0);
3838        assert_eq!(vs.stack_depth.as_deref(), Some("64"));
3839    }
3840
3841    #[test]
3842    fn parse_verifier_stats_multi_subprogram_stack() {
3843        let log = "processed 42 insns (limit 1000000) max_states_per_insn 1 total_states 5 peak_states 2 mark_read 0\nstack depth 32+16+8\n";
3844        let vs = parse_verifier_stats(log);
3845        assert_eq!(vs.processed_insns, 42);
3846        assert_eq!(vs.stack_depth.as_deref(), Some("32+16+8"));
3847    }
3848
3849    #[test]
3850    fn parse_verifier_stats_noise_between_lines() {
3851        let log = "\
3852libbpf: loading something
3853processed 999 insns (limit 1000000) max_states_per_insn 3 total_states 77 peak_states 20 mark_read 5
3854libbpf: prog 'dispatch': attached
3855verification time 7 usec
3856stack depth 48+0
3857";
3858        let vs = parse_verifier_stats(log);
3859        assert_eq!(vs.processed_insns, 999);
3860        assert_eq!(vs.total_states, 77);
3861        assert_eq!(vs.peak_states, 20);
3862        assert_eq!(vs.stack_depth.as_deref(), Some("48+0"));
3863    }
3864
3865    #[test]
3866    fn parse_verifier_stats_partial_insns_line() {
3867        let log = "processed 123\n";
3868        let vs = parse_verifier_stats(log);
3869        assert_eq!(vs.processed_insns, 123);
3870        assert_eq!(vs.total_states, 0);
3871        assert_eq!(vs.peak_states, 0);
3872    }
3873
3874    #[test]
3875    fn parse_verifier_stats_only_stack_depth() {
3876        let log = "stack depth 128\n";
3877        let vs = parse_verifier_stats(log);
3878        assert_eq!(vs.stack_depth.as_deref(), Some("128"));
3879        assert_eq!(vs.processed_insns, 0);
3880    }
3881
3882    #[test]
3883    fn parse_verifier_stats_zero_insns() {
3884        let log = "processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0\n";
3885        let vs = parse_verifier_stats(log);
3886        assert_eq!(vs.processed_insns, 0);
3887        assert_eq!(vs.total_states, 0);
3888        assert_eq!(vs.peak_states, 0);
3889    }
3890
3891    #[test]
3892    fn parse_verifier_stats_large_values() {
3893        let log = "processed 999999 insns (limit 1000000) max_states_per_insn 100 total_states 50000 peak_states 12345 mark_read 9999\nverification time 123456 usec\n";
3894        let vs = parse_verifier_stats(log);
3895        assert_eq!(vs.processed_insns, 999999);
3896        assert_eq!(vs.total_states, 50000);
3897        assert_eq!(vs.peak_states, 12345);
3898    }
3899
3900    #[test]
3901    fn parse_verifier_stats_stack_depth_single() {
3902        let log = "stack depth 64\n";
3903        let vs = parse_verifier_stats(log);
3904        assert_eq!(vs.stack_depth.as_deref(), Some("64"));
3905    }
3906
3907    #[test]
3908    fn parse_verifier_stats_stack_depth_many_subprograms() {
3909        let log = "stack depth 32+16+8+0+0\n";
3910        let vs = parse_verifier_stats(log);
3911        assert_eq!(vs.stack_depth.as_deref(), Some("32+16+8+0+0"));
3912    }
3913
3914    #[test]
3915    fn parse_verifier_stats_multiple_processed_lines_takes_last() {
3916        let log = "processed 100 insns (limit 1000000) max_states_per_insn 1 total_states 5 peak_states 2 mark_read 0\nprocessed 200 insns (limit 1000000) max_states_per_insn 2 total_states 10 peak_states 4 mark_read 0\n";
3917        let vs = parse_verifier_stats(log);
3918        assert_eq!(vs.processed_insns, 200);
3919        assert_eq!(vs.total_states, 10);
3920    }
3921
3922    #[test]
3923    fn parse_verifier_stats_complexity_error_with_stats() {
3924        let log = "\
3925func#0 @0
39260: R1=ctx() R10=fp0
39271: (bf) r6 = r1                       ; R1=ctx() R6_w=ctx()
3928back-edge from insn 42 to 10
3929BPF program is too complex
3930processed 131071 insns (limit 131072) max_states_per_insn 12 total_states 9999 peak_states 5000 mark_read 800
3931verification time 250000 usec
3932stack depth 96+32
3933";
3934        let vs = parse_verifier_stats(log);
3935        assert_eq!(vs.processed_insns, 131071);
3936        assert_eq!(vs.total_states, 9999);
3937        assert_eq!(vs.peak_states, 5000);
3938        assert_eq!(vs.stack_depth.as_deref(), Some("96+32"));
3939    }
3940
3941    #[test]
3942    fn parse_verifier_stats_complexity_error_no_stats() {
3943        let log = "\
3944func#0 @0
39450: R1=ctx() R10=fp0
3946R1 type=ctx expected=fp
3947";
3948        let vs = parse_verifier_stats(log);
3949        assert_eq!(vs.processed_insns, 0);
3950        assert_eq!(vs.total_states, 0);
3951        assert!(vs.stack_depth.is_none());
3952    }
3953
3954    #[test]
3955    fn parse_verifier_stats_loop_warning_with_stats() {
3956        let log = "\
3957infinite loop detected at insn 15
3958back-edge from insn 30 to 15
3959processed 500 insns (limit 1000000) max_states_per_insn 3 total_states 40 peak_states 15 mark_read 5
3960verification time 100 usec
3961";
3962        let vs = parse_verifier_stats(log);
3963        assert_eq!(vs.processed_insns, 500);
3964        assert_eq!(vs.total_states, 40);
3965        assert_eq!(vs.peak_states, 15);
3966    }
3967
3968    #[test]
3969    fn parse_verifier_stats_processed_no_number() {
3970        let log = "processed\n";
3971        let vs = parse_verifier_stats(log);
3972        assert_eq!(vs.processed_insns, 0);
3973    }
3974
3975    #[test]
3976    fn parse_verifier_stats_keyword_at_end_no_value() {
3977        let log = "processed 100 insns (limit 1000000) max_states_per_insn 1 total_states\n";
3978        let vs = parse_verifier_stats(log);
3979        assert_eq!(vs.processed_insns, 100);
3980        assert_eq!(vs.total_states, 0);
3981    }
3982
3983    #[test]
3984    fn parse_verifier_stats_non_numeric_values() {
3985        let log = "processed 100 insns (limit 1000000) max_states_per_insn 1 total_states abc peak_states xyz mark_read 0\n";
3986        let vs = parse_verifier_stats(log);
3987        assert_eq!(vs.processed_insns, 100);
3988        assert_eq!(vs.total_states, 0);
3989        assert_eq!(vs.peak_states, 0);
3990    }
3991
3992    #[test]
3993    fn parse_verifier_stats_verification_time_is_ignored() {
3994        let log = "verification time unknown usec\n";
3995        let vs = parse_verifier_stats(log);
3996        assert_eq!(vs.processed_insns, 0);
3997        assert!(vs.stack_depth.is_none());
3998    }
3999
4000    #[test]
4001    fn parse_verifier_stats_stack_depth_empty() {
4002        let log = "stack depth   \n";
4003        let vs = parse_verifier_stats(log);
4004        assert!(vs.stack_depth.is_none());
4005    }
4006
4007    #[test]
4008    fn parse_verifier_stats_peak_states_at_end() {
4009        let log = "processed 50 insns (limit 1000000) max_states_per_insn 1 total_states 10 peak_states\n";
4010        let vs = parse_verifier_stats(log);
4011        assert_eq!(vs.processed_insns, 50);
4012        assert_eq!(vs.total_states, 10);
4013        assert_eq!(vs.peak_states, 0);
4014    }
4015
4016    #[test]
4017    fn parse_verifier_stats_windows_line_endings() {
4018        let log = "processed 42 insns (limit 1000000) max_states_per_insn 1 total_states 5 peak_states 2 mark_read 0\r\nverification time 10 usec\r\nstack depth 16\r\n";
4019        let vs = parse_verifier_stats(log);
4020        assert_eq!(vs.processed_insns, 42);
4021        assert!(vs.stack_depth.is_some());
4022    }
4023
4024    // -----------------------------------------------------------------------
4025    // normalize_verifier_line
4026    // -----------------------------------------------------------------------
4027
4028    #[test]
4029    fn normalize_plain_instruction() {
4030        assert_eq!(
4031            normalize_verifier_line("100: (07) r1 += 8"),
4032            "100: (07) r1 += 8"
4033        );
4034    }
4035
4036    #[test]
4037    fn normalize_strips_frame_annotation() {
4038        assert_eq!(
4039            normalize_verifier_line("3006: (07) r9 += 1  ; frame1: R9_w=2"),
4040            "3006: (07) r9 += 1"
4041        );
4042    }
4043
4044    #[test]
4045    fn normalize_strips_register_annotation() {
4046        assert_eq!(
4047            normalize_verifier_line("42: (bf) r6 = r1 ; R1=ctx() R6_w=ctx()"),
4048            "42: (bf) r6 = r1"
4049        );
4050    }
4051
4052    #[test]
4053    fn normalize_standalone_register_dump() {
4054        assert_eq!(
4055            normalize_verifier_line("3041: frame1: R0_w=scalar()"),
4056            "3041:"
4057        );
4058    }
4059
4060    #[test]
4061    fn normalize_goto_inline_state() {
4062        assert_eq!(
4063            normalize_verifier_line(
4064                "3026: (b5) if r6 <= 0x11dc0 goto pc+2 3029: frame1: R0=1 R6=scalar()"
4065            ),
4066            "3026: (b5) if r6 <= 0x11dc0 goto pc+2"
4067        );
4068    }
4069
4070    #[test]
4071    fn normalize_goto_no_inline_state() {
4072        assert_eq!(
4073            normalize_verifier_line("50: (05) goto pc+10"),
4074            "50: (05) goto pc+10"
4075        );
4076    }
4077
4078    #[test]
4079    fn normalize_non_instruction_line() {
4080        assert_eq!(normalize_verifier_line("func#0 @0"), "func#0 @0");
4081    }
4082
4083    #[test]
4084    fn normalize_empty() {
4085        assert_eq!(normalize_verifier_line(""), "");
4086    }
4087
4088    #[test]
4089    fn normalize_goto_negative_offset() {
4090        assert_eq!(
4091            normalize_verifier_line("50: (05) goto pc-10 60: frame1: R0=1"),
4092            "50: (05) goto pc-10"
4093        );
4094    }
4095
4096    #[test]
4097    fn normalize_semicolon_source_comment() {
4098        let line = "100: (07) r1 += 8 ; for (int j = 0; j < n; j++)";
4099        assert_eq!(normalize_verifier_line(line), line);
4100    }
4101
4102    #[test]
4103    fn normalize_semicolon_return_value_comment() {
4104        let line = "200: (b7) r0 = 0 ; Return value";
4105        assert_eq!(normalize_verifier_line(line), line);
4106    }
4107
4108    #[test]
4109    fn normalize_standalone_bare_register_dump() {
4110        assert_eq!(
4111            normalize_verifier_line("3029: R0=1 R6=scalar(id=1)"),
4112            "3029:"
4113        );
4114    }
4115
4116    #[test]
4117    fn normalize_standalone_r10_dump() {
4118        assert_eq!(normalize_verifier_line("42: R10=fp0"), "42:");
4119    }
4120
4121    // -----------------------------------------------------------------------
4122    // detect_cycle / collapse_cycles
4123    // -----------------------------------------------------------------------
4124
4125    fn repeating_log(prefix: usize, period: usize, reps: usize, suffix: usize) -> String {
4126        let mut lines = Vec::new();
4127        for i in 0..prefix {
4128            lines.push(format!("{}: (07) r1 += {i}", 1000 + i));
4129        }
4130        for rep in 0..reps {
4131            for j in 0..period {
4132                let insn = 100 + j;
4133                lines.push(format!(
4134                    "{insn}: (bf) r{} = r{} ; frame1: R{}_w={}",
4135                    j % 10,
4136                    (j + 1) % 10,
4137                    j % 10,
4138                    rep * 100 + j
4139                ));
4140            }
4141        }
4142        for i in 0..suffix {
4143            lines.push(format!("{}: (95) exit_{i}", 2000 + i));
4144        }
4145        lines.join("\n")
4146    }
4147
4148    #[test]
4149    fn detect_cycle_basic() {
4150        let log = repeating_log(0, 10, 8, 0);
4151        let lines: Vec<&str> = log.lines().collect();
4152        let result = detect_cycle(&lines);
4153        assert!(result.is_some(), "should detect cycle");
4154        let (start, period, count) = result.unwrap();
4155        assert_eq!(period, 10);
4156        assert!(count >= 6, "count={count}");
4157        assert_eq!(start, 0);
4158    }
4159
4160    #[test]
4161    fn detect_cycle_with_prefix_suffix() {
4162        let log = repeating_log(5, 10, 8, 5);
4163        let lines: Vec<&str> = log.lines().collect();
4164        let result = detect_cycle(&lines);
4165        assert!(result.is_some(), "should detect cycle with prefix/suffix");
4166        let (_start, period, count) = result.unwrap();
4167        assert_eq!(period, 10);
4168        assert!(count >= 6);
4169    }
4170
4171    #[test]
4172    fn detect_cycle_too_few_reps() {
4173        let log = repeating_log(0, 10, 2, 0);
4174        let lines: Vec<&str> = log.lines().collect();
4175        assert!(detect_cycle(&lines).is_none());
4176    }
4177
4178    #[test]
4179    fn detect_cycle_too_few_lines() {
4180        let lines: Vec<String> = (0..20)
4181            .map(|i| format!("{}: (07) r1 += {i}", 100 + i % 3))
4182            .collect();
4183        let refs: Vec<&str> = lines.iter().map(|s| s.as_str()).collect();
4184        assert!(detect_cycle(&refs).is_none());
4185    }
4186
4187    #[test]
4188    fn detect_cycle_no_cycle() {
4189        let lines: Vec<String> = (0..100).map(|i| format!("{i}: unique_insn_{i}")).collect();
4190        let refs: Vec<&str> = lines.iter().map(|s| s.as_str()).collect();
4191        assert!(detect_cycle(&refs).is_none());
4192    }
4193
4194    #[test]
4195    fn detect_cycle_empty() {
4196        let empty: Vec<&str> = vec![];
4197        assert!(detect_cycle(&empty).is_none());
4198    }
4199
4200    #[test]
4201    fn detect_cycle_exact_boundary() {
4202        let log = repeating_log(0, 5, 6, 0);
4203        let lines: Vec<&str> = log.lines().collect();
4204        assert_eq!(lines.len(), 30);
4205        let result = detect_cycle(&lines);
4206        assert!(result.is_some(), "boundary case should detect cycle");
4207        let (_start, period, count) = result.unwrap();
4208        assert_eq!(period, 5);
4209        assert_eq!(count, 6);
4210    }
4211
4212    #[test]
4213    fn collapse_cycles_empty_string() {
4214        assert_eq!(collapse_cycles(""), "");
4215    }
4216
4217    #[test]
4218    fn collapse_cycles_basic() {
4219        let log = repeating_log(2, 10, 8, 2);
4220        let collapsed = collapse_cycles(&log);
4221        assert!(collapsed.contains("identical iterations omitted"));
4222        assert!(collapsed.contains("8x of the following 10 lines"));
4223        assert!(collapsed.contains("end repeat"));
4224        assert!(collapsed.lines().count() < log.lines().count());
4225    }
4226
4227    #[test]
4228    fn collapse_cycles_no_cycle() {
4229        let log = "line 1\nline 2\nline 3\n";
4230        let collapsed = collapse_cycles(log);
4231        assert_eq!(collapsed, log);
4232    }
4233
4234    #[test]
4235    fn collapse_cycles_preserves_stats() {
4236        let mut log = repeating_log(0, 10, 8, 0);
4237        log.push_str("\nprocessed 1000 insns (limit 1000000) max_states_per_insn 5 total_states 100 peak_states 30 mark_read 10\n");
4238        let collapsed = collapse_cycles(&log);
4239        assert!(collapsed.contains("processed 1000 insns"));
4240    }
4241
4242    #[test]
4243    fn collapse_cycles_with_register_annotations() {
4244        let mut lines = Vec::new();
4245        lines.push("0: (07) r1 += 1".to_string());
4246        for rep in 0..8 {
4247            for j in 0..6 {
4248                let insn = 100 + j;
4249                lines.push(format!(
4250                    "{insn}: (bf) r{} = r{} ; frame1: R{}_w={}",
4251                    j % 10,
4252                    (j + 1) % 10,
4253                    j % 10,
4254                    rep * 100 + j
4255                ));
4256            }
4257        }
4258        lines.push("200: (95) exit".to_string());
4259        let log = lines.join("\n");
4260        let collapsed = collapse_cycles(&log);
4261        assert!(collapsed.contains("identical iterations omitted"));
4262    }
4263
4264    // -----------------------------------------------------------------------
4265    // build_b_map / build_diff_rows
4266    // -----------------------------------------------------------------------
4267
4268    fn prog(name: &str, verified_insns: u32) -> ProgStats {
4269        ProgStats {
4270            name: name.to_string(),
4271            verified_insns,
4272        }
4273    }
4274
4275    #[test]
4276    fn build_b_map_basic() {
4277        let stats_b = vec![prog("dispatch", 500)];
4278        let map = build_b_map(&stats_b);
4279        assert_eq!(map.get("dispatch"), Some(&500));
4280    }
4281
4282    #[test]
4283    fn build_b_map_empty() {
4284        let map = build_b_map(&[]);
4285        assert!(map.is_empty());
4286    }
4287
4288    #[test]
4289    fn build_diff_rows_matching_programs() {
4290        let stats_a = vec![prog("dispatch", 500)];
4291        let mut b_map = HashMap::new();
4292        b_map.insert("dispatch".to_string(), 300u64);
4293        let rows = build_diff_rows(&stats_a, &b_map);
4294        assert_eq!(rows.len(), 1);
4295        assert_eq!(rows[0].name, "dispatch");
4296        assert_eq!(rows[0].a, 500);
4297        assert_eq!(rows[0].b, 300);
4298        assert_eq!(rows[0].delta, 200);
4299    }
4300
4301    #[test]
4302    fn build_diff_rows_program_missing_from_b() {
4303        let stats_a = vec![prog("new_prog", 100)];
4304        let b_map = HashMap::new();
4305        let rows = build_diff_rows(&stats_a, &b_map);
4306        assert_eq!(rows.len(), 1);
4307        assert_eq!(rows[0].a, 100);
4308        assert_eq!(rows[0].b, 0);
4309        assert_eq!(rows[0].delta, 100);
4310    }
4311
4312    #[test]
4313    fn build_diff_rows_negative_delta() {
4314        let stats_a = vec![prog("dispatch", 200)];
4315        let mut b_map = HashMap::new();
4316        b_map.insert("dispatch".to_string(), 500u64);
4317        let rows = build_diff_rows(&stats_a, &b_map);
4318        assert_eq!(rows[0].delta, -300);
4319    }
4320
4321    #[test]
4322    fn build_diff_rows_empty_a() {
4323        let b_map = HashMap::new();
4324        let rows = build_diff_rows(&[], &b_map);
4325        assert!(rows.is_empty());
4326    }
4327
4328    /// Simulates the verifier trace produced by #pragma unroll loops.
4329    /// Each copy is at a different base address but has the same
4330    /// instruction sequence. After normalize_for_cycle_detection strips
4331    /// addresses and register annotations, all copies look identical.
4332    fn unrolled_verifier_log(copies: usize, body_len: usize) -> String {
4333        let ops = [
4334            "(85) call bpf_ktime_get_ns#5",
4335            "(bf) r2 = r0",
4336            "(77) r0 >>= 16",
4337            "(af) r1 ^= r0",
4338            "(77) r2 >>= 32",
4339            "(0f) r1 += r2",
4340            "(24) w1 *= 7",
4341            "(04) w1 += 1",
4342        ];
4343        let mut lines = Vec::new();
4344        lines.push("func#0 @0".to_string());
4345        lines.push("0: R1=ctx() R10=fp0".to_string());
4346        let mut addr = 10;
4347        for copy in 0..copies {
4348            for (j, op) in ops.iter().enumerate().take(body_len) {
4349                lines.push(format!(
4350                    "{}: {op} ; R0_w=scalar(id={})",
4351                    addr,
4352                    copy * 100 + j
4353                ));
4354                addr += 1;
4355            }
4356        }
4357        lines.push(format!("{addr}: (05) goto pc-1"));
4358        lines.push(
4359            "processed 1000 insns (limit 1000000) max_states_per_insn 3 \
4360             total_states 50 peak_states 20 mark_read 5"
4361                .to_string(),
4362        );
4363        lines.join("\n")
4364    }
4365
4366    #[test]
4367    fn detect_cycle_unrolled_loop() {
4368        let log = unrolled_verifier_log(8, 6);
4369        let lines: Vec<&str> = log.lines().collect();
4370        let result = detect_cycle(&lines);
4371        assert!(result.is_some(), "should detect cycle in unrolled loop");
4372        let (_start, period, count) = result.unwrap();
4373        assert_eq!(period, 6);
4374        assert!(count >= 6, "count={count}");
4375    }
4376
4377    #[test]
4378    fn collapse_cycles_unrolled_loop() {
4379        let log = unrolled_verifier_log(8, 6);
4380        let collapsed = collapse_cycles(&log);
4381        assert!(
4382            collapsed.contains("identical iterations omitted"),
4383            "should collapse unrolled loop"
4384        );
4385        assert!(collapsed.lines().count() < log.lines().count());
4386    }
4387
4388    // -----------------------------------------------------------------------
4389    // extract_verifier_log
4390    // -----------------------------------------------------------------------
4391
4392    #[test]
4393    fn extract_verifier_log_basic() {
4394        let log = "\
4395libbpf: prog 'dispatch': BPF program load failed: -22
4396-- BEGIN PROG LOAD LOG --
4397func#0 @0
43980: R1=ctx() R10=fp0
4399processed 100 insns (limit 1000000) max_states_per_insn 1 total_states 5 peak_states 2 mark_read 0
4400-- END PROG LOAD LOG --
4401libbpf: failed to load object 'ktstr_ops'
4402";
4403        let extracted = extract_verifier_log(log);
4404        assert!(extracted.is_some());
4405        let v = extracted.unwrap();
4406        assert!(v.starts_with("func#0 @0"));
4407        assert!(v.contains("processed 100 insns"));
4408        assert!(!v.contains("BEGIN PROG LOAD LOG"));
4409        assert!(!v.contains("END PROG LOAD LOG"));
4410        assert!(!v.contains("libbpf:"));
4411    }
4412
4413    #[test]
4414    fn extract_verifier_log_none_without_markers() {
4415        let log = "func#0 @0\n0: R1=ctx()\nprocessed 50 insns\n";
4416        assert!(extract_verifier_log(log).is_none());
4417    }
4418
4419    #[test]
4420    fn extract_verifier_log_empty() {
4421        assert!(extract_verifier_log("").is_none());
4422    }
4423
4424    /// Attack 1: libbpf wraps verifier output with "libbpf: " prefix lines.
4425    /// `parse_verifier_stats` looks for `starts_with("processed ")` which
4426    /// won't match `libbpf: processed ...`. Without extraction, stats
4427    /// parsing fails on blobs where the `processed` line is only inside
4428    /// the markers.
4429    #[test]
4430    fn extract_verifier_log_attack1_stats_parse() {
4431        let blob = "\
4432libbpf: prog 'ktstr_ops_dispatch': BPF program load failed: -22
4433libbpf: -- BEGIN PROG LOAD LOG --
4434func#0 @0
44350: R1=ctx() R10=fp0
44361: (bf) r6 = r1 ; R1=ctx() R6_w=ctx()
4437back-edge from insn 42 to 10
4438BPF program is too complex
4439processed 131071 insns (limit 131072) max_states_per_insn 12 total_states 9999 peak_states 5000 mark_read 800
4440verification time 250000 usec
4441stack depth 96+32
4442libbpf: -- END PROG LOAD LOG --
4443libbpf: failed to load BPF skeleton 'ktstr_ops': -22
4444";
4445        let extracted = extract_verifier_log(blob);
4446        assert!(extracted.is_some(), "should find markers");
4447        let v = extracted.unwrap();
4448        let vs = parse_verifier_stats(v);
4449        assert_eq!(vs.processed_insns, 131071);
4450        assert_eq!(vs.total_states, 9999);
4451        assert_eq!(vs.peak_states, 5000);
4452        assert_eq!(vs.stack_depth.as_deref(), Some("96+32"));
4453
4454        // Without extraction, parsing the full blob must also work
4455        // because the "processed" line doesn't have a "libbpf: " prefix
4456        // inside the markers. But verify extraction gives cleaner input.
4457        let vs_raw = parse_verifier_stats(blob);
4458        assert_eq!(vs_raw.processed_insns, 131071);
4459    }
4460
4461    /// Attack 3: three distinct program load logs in a single blob.
4462    /// Each has different instructions. `collapse_cycles` must NOT treat
4463    /// them as a repeating cycle.
4464    #[test]
4465    fn extract_verifier_log_attack3_no_false_collapse() {
4466        let blob = "\
4467libbpf: prog 'init': BPF program load failed: -22
4468libbpf: -- BEGIN PROG LOAD LOG --
4469func#0 @0
44700: R1=ctx() R10=fp0
44711: (bf) r6 = r1
44722: (07) r6 += 8
44733: (61) r0 = *(u32 *)(r6 + 0)
44744: (95) exit
4475processed 5 insns (limit 1000000) max_states_per_insn 1 total_states 3 peak_states 1 mark_read 0
4476libbpf: -- END PROG LOAD LOG --
4477libbpf: prog 'dispatch': BPF program load failed: -22
4478libbpf: -- BEGIN PROG LOAD LOG --
4479func#1 @10
448010: R1=ctx() R10=fp0
448111: (bf) r7 = r1
448212: (85) call bpf_ktime_get_ns#5
448313: (77) r0 >>= 32
448414: (95) exit
4485processed 5 insns (limit 1000000) max_states_per_insn 1 total_states 3 peak_states 1 mark_read 0
4486libbpf: -- END PROG LOAD LOG --
4487libbpf: prog 'enqueue': BPF program load failed: -22
4488libbpf: -- BEGIN PROG LOAD LOG --
4489func#2 @20
449020: R1=ctx() R10=fp0
449121: (b7) r0 = 0
449222: (63) *(u32 *)(r10 - 4) = r0
449323: (61) r1 = *(u32 *)(r10 - 4)
449424: (95) exit
4495processed 5 insns (limit 1000000) max_states_per_insn 1 total_states 3 peak_states 1 mark_read 0
4496libbpf: -- END PROG LOAD LOG --
4497libbpf: failed to load BPF skeleton 'ktstr_ops': -22
4498";
4499        // extract_verifier_log returns the FIRST log section.
4500        let extracted = extract_verifier_log(blob);
4501        assert!(extracted.is_some());
4502        let v = extracted.unwrap();
4503        assert!(v.contains("func#0 @0"), "should get first program's log");
4504        assert!(!v.contains("func#1"), "should not include second program");
4505
4506        // collapse_cycles on the extracted first section must not
4507        // collapse — it's only 7 lines total.
4508        let collapsed = collapse_cycles(v);
4509        assert!(
4510            !collapsed.contains("identical iterations omitted"),
4511            "must not false-collapse distinct program logs"
4512        );
4513    }
4514
4515    // -----------------------------------------------------------------------
4516    // collapse_verifier_region — in-place collapse of the verifier trace,
4517    // preserving everything outside the BEGIN/END markers.
4518    // -----------------------------------------------------------------------
4519
4520    /// Markers present: the region between them is collapsed while the
4521    /// prefix (surrounding scheduler chatter + BEGIN marker) and suffix
4522    /// (END marker + trailing chatter) survive byte-for-byte. Built with
4523    /// a genuine cycle (a >=5-line block repeated >=3x) so the collapse
4524    /// fires and we can assert the omission marker lands INSIDE the
4525    /// region without disturbing the surrounding text.
4526    #[test]
4527    fn collapse_verifier_region_collapses_in_place() {
4528        // 6-line repeating block × 4 → detect_cycle fires (>=5 period,
4529        // >=3 reps). Distinct addresses per copy so normalization maps
4530        // them together.
4531        let mut trace = String::new();
4532        for i in 0..24 {
4533            trace.push_str(&format!("{}: (07) r1 += 8 ; op{}\n", i, i % 6));
4534        }
4535        let prefix = "scheduler: starting up\nlibbpf: loading\n";
4536        let suffix = "libbpf: load failed: -22\nscheduler exiting\n";
4537        let input =
4538            format!("{prefix}-- BEGIN PROG LOAD LOG --\n{trace}-- END PROG LOAD LOG --\n{suffix}",);
4539
4540        let out = collapse_verifier_region(&input);
4541
4542        // Everything before the BEGIN marker's content and everything
4543        // from the END marker onward is byte-identical.
4544        assert!(
4545            out.starts_with(&format!("{prefix}-- BEGIN PROG LOAD LOG --\n")),
4546            "prefix + BEGIN marker must be preserved verbatim: {out}",
4547        );
4548        assert!(
4549            out.contains(&format!("-- END PROG LOAD LOG --\n{suffix}")),
4550            "END marker + suffix must be preserved verbatim: {out}",
4551        );
4552        // The collapse fired inside the region.
4553        assert!(
4554            out.contains("identical iterations omitted"),
4555            "the cyclic region must be collapsed in place: {out}",
4556        );
4557    }
4558
4559    /// No markers: identity (returned unchanged), even when the input
4560    /// contains a collapsible cycle — without the markers there is no
4561    /// region to collapse.
4562    #[test]
4563    fn collapse_verifier_region_identity_without_markers() {
4564        let mut input = String::from("scheduler output with no verifier markers\n");
4565        for i in 0..24 {
4566            input.push_str(&format!("{}: (07) r1 += 8 ; op{}\n", i, i % 6));
4567        }
4568        assert_eq!(
4569            collapse_verifier_region(&input),
4570            input,
4571            "input without BEGIN/END markers must be returned unchanged",
4572        );
4573    }
4574
4575    /// Mid-line END marker (`libbpf: -- END ...`): the partial `libbpf: `
4576    /// prefix line stays OUTSIDE the collapsed region (in the suffix),
4577    /// consistent with `extract_verifier_log`'s trim-back-to-newline
4578    /// behavior — so the region boundary the two functions compute agree.
4579    #[test]
4580    fn collapse_verifier_region_midline_end_matches_extract() {
4581        let input = "\
4582head\n\
4583-- BEGIN PROG LOAD LOG --\n\
45840: R1=ctx()\n\
4585processed 5 insns (limit 1) total_states 3 peak_states 1\n\
4586libbpf: -- END PROG LOAD LOG --\n\
4587tail\n";
4588        // Short region → no collapse; collapse_verifier_region is an
4589        // identity transform on the bytes here, so the whole input is
4590        // preserved AND the `libbpf: -- END` line is intact.
4591        let out = collapse_verifier_region(input);
4592        assert_eq!(
4593            out, input,
4594            "short region must round-trip byte-for-byte: {out}"
4595        );
4596        // The extracted region must NOT include the partial libbpf END
4597        // line — cross-check that the shared region logic agrees.
4598        let extracted = extract_verifier_log(input).unwrap();
4599        assert!(
4600            extracted.contains("processed 5 insns"),
4601            "extract must include the last real content line: {extracted}",
4602        );
4603        assert!(
4604            !extracted.contains("-- END"),
4605            "extract must not include the END-marker line: {extracted}",
4606        );
4607    }
4608
4609    /// A timed-out run STILL renders the live-streamed scheduler stdout:
4610    /// the core bug fix — a watchdog kill never reaches the teardown
4611    /// merged-file dump (`scheduler_log` empty), but whatever streamed
4612    /// over `scheduler_stdout` before the kill must appear.
4613    #[test]
4614    fn format_verifier_output_timed_out_prints_live_stdout() {
4615        let result = VerifierVmResult {
4616            stats: Vec::new(),
4617            scheduler_log: String::new(),
4618            scheduler_stdout: "scheduler: entered main loop\ndispatching tasks...\n".to_string(),
4619            scheduler_stderr: String::new(),
4620            attach: AttachOutcome::Attached,
4621            dispatched: false,
4622            scheduler_exited: false,
4623            guest_exit_code: None,
4624            timed_out: true,
4625            crash_message: None,
4626            dilation: None,
4627        };
4628        let out = format_verifier_output("verifier", &result, false);
4629        assert!(
4630            out.contains("scheduler: UNKNOWN — VM timed out"),
4631            "timed-out run must show UNKNOWN: {out}",
4632        );
4633        assert!(
4634            out.contains("--- scheduler log ---"),
4635            "timed-out run must still render the scheduler-log section: {out}",
4636        );
4637        assert!(
4638            out.contains("scheduler: entered main loop") && out.contains("dispatching tasks..."),
4639            "the live-streamed stdout captured before the watchdog kill must be printed: {out}",
4640        );
4641    }
4642
4643    /// Merged-dump fallback selection: with live stderr present and live
4644    /// stdout empty, the stdout section renders NOTHING — the merged
4645    /// teardown dump would duplicate the stderr section (schedulers that
4646    /// log exclusively to stderr, the common libbpf/log-crate case). The
4647    /// dump only renders when NEITHER live stream arrived.
4648    #[test]
4649    fn format_verifier_output_no_dump_duplication_with_live_stderr() {
4650        let base = |stdout: &str, stderr: &str, log: &str| VerifierVmResult {
4651            stats: Vec::new(),
4652            scheduler_log: log.to_string(),
4653            scheduler_stdout: stdout.to_string(),
4654            scheduler_stderr: stderr.to_string(),
4655            attach: AttachOutcome::Died,
4656            dispatched: false,
4657            scheduler_exited: false,
4658            guest_exit_code: Some(1),
4659            timed_out: false,
4660            crash_message: None,
4661            dilation: None,
4662        };
4663        // Live stderr arrived, live stdout empty: the merged dump (same
4664        // bytes as stderr here) must NOT render on stdout.
4665        let with_stderr = base("", "load failed: EINVAL\n", "load failed: EINVAL\n");
4666        let out = format_verifier_output("verifier", &with_stderr, false);
4667        assert!(
4668            !out.contains("--- scheduler log ---") && !out.contains("load failed"),
4669            "live stderr present -> no merged-dump duplication on stdout: {out}",
4670        );
4671        // No live stream at all: the merged dump is the fallback.
4672        let dump_only = base("", "", "load failed: EINVAL\n");
4673        let out = format_verifier_output("verifier", &dump_only, false);
4674        assert!(
4675            out.contains("--- scheduler log ---") && out.contains("load failed"),
4676            "no live streams -> merged dump renders: {out}",
4677        );
4678    }
4679
4680    /// The stderr formatter labels the section and collapses the verifier
4681    /// region in place (non-raw); empty stderr yields the empty string so
4682    /// the caller emits nothing.
4683    #[test]
4684    fn format_verifier_stderr_labels_and_collapses() {
4685        let empty = VerifierVmResult {
4686            stats: Vec::new(),
4687            scheduler_log: String::new(),
4688            scheduler_stdout: String::new(),
4689            scheduler_stderr: String::new(),
4690            attach: AttachOutcome::Attached,
4691            dispatched: true,
4692            scheduler_exited: false,
4693            guest_exit_code: Some(0),
4694            timed_out: false,
4695            crash_message: None,
4696            dilation: None,
4697        };
4698        assert_eq!(
4699            format_verifier_stderr("verifier", &empty, false),
4700            "",
4701            "empty stderr must format to the empty string",
4702        );
4703
4704        let mut trace = String::new();
4705        for i in 0..24 {
4706            trace.push_str(&format!("{}: (07) r1 += 8 ; op{}\n", i, i % 6));
4707        }
4708        let with_err = VerifierVmResult {
4709            stats: Vec::new(),
4710            scheduler_log: String::new(),
4711            scheduler_stdout: String::new(),
4712            scheduler_stderr: format!(
4713                "libbpf: -- BEGIN PROG LOAD LOG --\n{trace}-- END PROG LOAD LOG --\nload failed\n",
4714            ),
4715            attach: AttachOutcome::Died,
4716            dispatched: false,
4717            scheduler_exited: false,
4718            guest_exit_code: Some(1),
4719            timed_out: false,
4720            crash_message: None,
4721            dilation: None,
4722        };
4723        let s = format_verifier_stderr("verifier", &with_err, false);
4724        assert!(
4725            s.contains("--- scheduler stderr ---"),
4726            "stderr section must carry a labeled header: {s}",
4727        );
4728        assert!(
4729            s.contains("identical iterations omitted"),
4730            "non-raw stderr must collapse the verifier region: {s}",
4731        );
4732        assert!(
4733            s.contains("load failed"),
4734            "text outside the verifier region must be preserved: {s}",
4735        );
4736        // Raw mode: no collapsing.
4737        let raw = format_verifier_stderr("verifier", &with_err, true);
4738        assert!(
4739            !raw.contains("identical iterations omitted"),
4740            "raw mode must not collapse: {raw}",
4741        );
4742    }
4743
4744    // -- insta snapshot tests --
4745
4746    #[test]
4747    fn snapshot_format_verifier_output_no_log() {
4748        let result = VerifierVmResult {
4749            stats: vec![
4750                ProgStats {
4751                    name: "enqueue".into(),
4752                    verified_insns: 500,
4753                },
4754                ProgStats {
4755                    name: "dispatch".into(),
4756                    verified_insns: 1200,
4757                },
4758                ProgStats {
4759                    name: "init".into(),
4760                    verified_insns: 300,
4761                },
4762            ],
4763            scheduler_log: String::new(),
4764            scheduler_stdout: String::new(),
4765            scheduler_stderr: String::new(),
4766            attach: AttachOutcome::Attached,
4767            dispatched: true,
4768            scheduler_exited: false,
4769            guest_exit_code: Some(0),
4770            timed_out: false,
4771            crash_message: None,
4772            dilation: None,
4773        };
4774        insta::assert_snapshot!(format_verifier_output("default", &result, false));
4775    }
4776
4777    #[test]
4778    fn snapshot_format_verifier_output_with_log() {
4779        let log = "\
4780-- BEGIN PROG LOAD LOG --\n\
4781func#0 @0\n\
47820: R1=ctx() R10=fp0\n\
4783processed 42 insns (limit 1000000) max_states_per_insn 1 total_states 10 peak_states 8 mark_read 5\n\
4784-- END PROG LOAD LOG --";
4785        let result = VerifierVmResult {
4786            stats: vec![ProgStats {
4787                name: "enqueue".into(),
4788                verified_insns: 42,
4789            }],
4790            scheduler_log: log.into(),
4791            scheduler_stdout: String::new(),
4792            scheduler_stderr: String::new(),
4793            // A load log present means the scheduler printed a verifier
4794            // trace then exited — the SchedulerDied failure path.
4795            attach: AttachOutcome::Died,
4796            dispatched: false,
4797            scheduler_exited: false,
4798            guest_exit_code: Some(1),
4799            timed_out: false,
4800            crash_message: None,
4801            dilation: None,
4802        };
4803        insta::assert_snapshot!(format_verifier_output("llc+steal", &result, false));
4804    }
4805
4806    #[test]
4807    fn snapshot_format_verifier_diff() {
4808        let stats_a = vec![
4809            ProgStats {
4810                name: "enqueue".into(),
4811                verified_insns: 500,
4812            },
4813            ProgStats {
4814                name: "dispatch".into(),
4815                verified_insns: 1200,
4816            },
4817            ProgStats {
4818                name: "init".into(),
4819                verified_insns: 300,
4820            },
4821        ];
4822        let stats_b = vec![
4823            ProgStats {
4824                name: "enqueue".into(),
4825                verified_insns: 480,
4826            },
4827            ProgStats {
4828                name: "dispatch".into(),
4829                verified_insns: 1350,
4830            },
4831            ProgStats {
4832                name: "init".into(),
4833                verified_insns: 300,
4834            },
4835        ];
4836        insta::assert_snapshot!(format_verifier_diff("default", &stats_a, "llc", &stats_b));
4837    }
4838
4839    #[test]
4840    fn snapshot_format_verifier_diff_missing_program() {
4841        let stats_a = vec![
4842            ProgStats {
4843                name: "enqueue".into(),
4844                verified_insns: 500,
4845            },
4846            ProgStats {
4847                name: "new_prog".into(),
4848                verified_insns: 100,
4849            },
4850        ];
4851        let stats_b = vec![ProgStats {
4852            name: "enqueue".into(),
4853            verified_insns: 500,
4854        }];
4855        insta::assert_snapshot!(format_verifier_diff("A", &stats_a, "B", &stats_b));
4856    }
4857
4858    // -----------------------------------------------------------------------
4859    // extract_verifier_log — log extraction + cross-check against
4860    // parse_sched_output so the two slicers stay consistent on shared input.
4861    // -----------------------------------------------------------------------
4862
4863    #[test]
4864    fn extract_verifier_log_between_begin_end_markers() {
4865        // libbpf wraps the verifier log between explicit marker lines;
4866        // the extractor returns the content between them, trimmed of
4867        // the BEGIN newline and the trailing libbpf END prefix.
4868        let blob = "\
4869            unrelated preamble\n\
4870            libbpf: -- BEGIN PROG LOAD LOG --\n\
4871            processed 1234 insns (limit 1000000) max_states_per_insn 5 total_states 200 peak_states 50 mark_read 10\n\
4872            libbpf: -- END PROG LOAD LOG --\n\
4873            trailing diagnostics\n";
4874        let log = extract_verifier_log(blob).expect("markers present");
4875        assert!(log.contains("processed 1234 insns"));
4876        assert!(!log.contains("BEGIN PROG LOAD LOG"));
4877        assert!(!log.contains("END PROG LOAD LOG"));
4878    }
4879
4880    #[test]
4881    fn extract_verifier_log_returns_none_when_markers_absent() {
4882        // Backward compat: logs without the libbpf markers are treated
4883        // as "no markers" — the caller falls back to using the raw blob.
4884        assert!(extract_verifier_log("no markers in here").is_none());
4885        assert!(extract_verifier_log("only BEGIN marker -- BEGIN PROG LOAD LOG --").is_none());
4886    }
4887
4888    #[test]
4889    fn extract_verifier_log_consistent_with_parse_sched_output() {
4890        // `collect_verifier_output` chains parse_sched_output →
4891        // extract_verifier_log on the VM stdout blob. Both slicers
4892        // operate on the same input without duplicating work, so a
4893        // single SCHED_OUTPUT block that wraps a libbpf-marked verifier
4894        // log must produce the same verifier text when extracted in
4895        // that order.
4896        let sched_inner = "\
4897            libbpf: -- BEGIN PROG LOAD LOG --\n\
4898            processed 7 insns (limit 1000000) max_states_per_insn 1 total_states 1 peak_states 1 mark_read 0\n\
4899            libbpf: -- END PROG LOAD LOG --\n";
4900        let vm_output = format!(
4901            "kernel boot junk\n{SCHED_OUTPUT_START}\n{sched_inner}{SCHED_OUTPUT_END}\nafterward\n",
4902        );
4903        let sched = parse_sched_output(&vm_output).expect("SCHED_OUTPUT block");
4904        let verifier_log = extract_verifier_log(sched).expect("verifier markers");
4905        assert!(verifier_log.contains("processed 7 insns"));
4906        assert!(!verifier_log.contains("SCHED_OUTPUT"));
4907        assert!(!verifier_log.contains("BEGIN PROG LOAD LOG"));
4908    }
4909
4910    #[test]
4911    fn parse_sched_output_valid() {
4912        let output = format!(
4913            "noise\n{SCHED_OUTPUT_START}\nscheduler log line 1\nline 2\n{SCHED_OUTPUT_END}\nmore"
4914        );
4915        let parsed = parse_sched_output(&output);
4916        assert!(parsed.is_some());
4917        let content = parsed.unwrap();
4918        assert!(content.contains("scheduler log line 1"));
4919        assert!(content.contains("line 2"));
4920    }
4921
4922    #[test]
4923    fn parse_sched_output_missing_start() {
4924        let output = format!("no start\n{SCHED_OUTPUT_END}\n");
4925        assert!(parse_sched_output(&output).is_none());
4926    }
4927
4928    #[test]
4929    fn parse_sched_output_missing_end() {
4930        let output = format!("{SCHED_OUTPUT_START}\nsome content");
4931        assert!(parse_sched_output(&output).is_none());
4932    }
4933
4934    #[test]
4935    fn parse_sched_output_empty_content() {
4936        let output = format!("{SCHED_OUTPUT_START}\n\n{SCHED_OUTPUT_END}");
4937        assert!(parse_sched_output(&output).is_none());
4938    }
4939
4940    #[test]
4941    fn parse_sched_output_with_stack_traces() {
4942        let stack = "do_enqueue_task+0x1a0/0x380\nbalance_one+0x50/0x100\n";
4943        let output = format!("{SCHED_OUTPUT_START}\n{stack}\n{SCHED_OUTPUT_END}");
4944        let parsed = parse_sched_output(&output).unwrap();
4945        assert!(parsed.contains("do_enqueue_task"));
4946        assert!(parsed.contains("balance_one"));
4947    }
4948
4949    #[test]
4950    fn parse_sched_output_rfind_survives_end_marker_in_content() {
4951        // Regression: if the scheduler log echoes the END marker
4952        // inside its own content (e.g. a shell heredoc, a diagnostic
4953        // that quotes the sentinel), `find` truncated the section at
4954        // the first occurrence — which was inside the content, not
4955        // at the terminator. `rfind` anchors on the last occurrence,
4956        // which is the real terminator.
4957        let content = format!("line1\nfake {SCHED_OUTPUT_END} inside\nline3");
4958        let output = format!("{SCHED_OUTPUT_START}\n{content}\n{SCHED_OUTPUT_END}\n");
4959        let parsed = parse_sched_output(&output).unwrap();
4960        assert!(
4961            parsed.contains("line3"),
4962            "rfind must keep content after an embedded END marker: {parsed:?}"
4963        );
4964        assert!(
4965            parsed.contains("fake"),
4966            "content before the embedded marker must also survive: {parsed:?}"
4967        );
4968    }
4969
4970    // -- parse_sched_output_partial --
4971
4972    #[test]
4973    fn parse_sched_output_partial_well_formed_matches_strict() {
4974        // When both delimiters are present, the partial parser
4975        // returns the same content as the strict parser.
4976        let output = format!(
4977            "noise\n{SCHED_OUTPUT_START}\nscheduler log line 1\nline 2\n{SCHED_OUTPUT_END}\nmore"
4978        );
4979        assert_eq!(
4980            parse_sched_output_partial(&output),
4981            parse_sched_output(&output),
4982        );
4983    }
4984
4985    #[test]
4986    fn parse_sched_output_partial_missing_end_returns_partial() {
4987        // When SCHED_OUTPUT_END is absent (scheduler crashed mid-run
4988        // before writing the closing delimiter), the partial parser
4989        // returns content from after SCHED_OUTPUT_START to end of
4990        // buffer. The strict parser returns None for the same input.
4991        let output = format!("{SCHED_OUTPUT_START}\nstack frame 1\nstack frame 2");
4992        assert!(parse_sched_output(&output).is_none());
4993        let partial = parse_sched_output_partial(&output).unwrap();
4994        assert!(partial.contains("stack frame 1"));
4995        assert!(partial.contains("stack frame 2"));
4996    }
4997
4998    #[test]
4999    fn parse_sched_output_partial_missing_start_returns_none() {
5000        // No start marker → no content recoverable. The end-marker-
5001        // only case is unrecoverable: we cannot infer where the log
5002        // begins.
5003        let output = format!("garbage\n{SCHED_OUTPUT_END}\n");
5004        assert!(parse_sched_output_partial(&output).is_none());
5005    }
5006
5007    #[test]
5008    fn parse_sched_output_partial_empty_content_returns_none() {
5009        // Start marker present but no payload after it.
5010        let output = format!("{SCHED_OUTPUT_START}\n");
5011        assert!(parse_sched_output_partial(&output).is_none());
5012    }
5013}