1use std::collections::HashMap;
18use std::ffi::OsStr;
19use std::path::{Path, PathBuf};
20
21pub const VERIFIER_CELL_OWNERSHIP_MANIFEST_VERSION: u32 = 1;
23
24#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
33#[serde(deny_unknown_fields)]
34pub struct VerifierCellOwnershipManifest {
35 pub version: u32,
37 pub entries: Vec<VerifierCellOwnershipEntry>,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
44#[serde(deny_unknown_fields)]
45pub struct VerifierCellOwnershipEntry {
46 pub scheduler: crate::test_support::SchedulerJson,
48 pub executable: PathBuf,
50}
51
52pub(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 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 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
195pub(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()), ¤t_executable)
208}
209
210#[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
231pub(crate) const SCHED_OUTPUT_START: &str = "===SCHED_OUTPUT_START===";
235pub(crate) const SCHED_OUTPUT_END: &str = "===SCHED_OUTPUT_END===";
239
240pub(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
264pub(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
282pub(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
298pub(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
309fn 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
364fn 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
381fn 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
400pub(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
425pub struct VerifierStats {
428 pub processed_insns: u64,
430 pub total_states: u64,
432 pub peak_states: u64,
434 pub stack_depth: Option<String>,
437}
438
439#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
441pub struct ProgStats {
442 pub name: String,
444 pub verified_insns: u32,
447}
448
449pub struct DiffRow {
451 pub name: String,
453 pub a: u64,
455 pub b: u64,
457 pub delta: i64,
460}
461
462fn 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
482pub 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
538pub 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 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 if let Some(pos) = trimmed.find("; frame") {
568 return trimmed[..pos].trim_end();
569 }
570 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 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
593fn normalize_for_cycle_detection(line: &str) -> &str {
598 let n = normalize_verifier_line(line);
599 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
609pub 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 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 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 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 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 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
782pub 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
836pub 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
852pub 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#[derive(Debug, Clone, PartialEq, Eq)]
892pub enum AttachOutcome {
893 Attached,
896 Died,
899 NotAttached(String),
903 Unconfirmed,
907}
908
909impl AttachOutcome {
910 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
932pub(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
978fn 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
1004fn 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
1021fn 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
1039pub struct VerifierVmResult {
1041 pub stats: Vec<ProgStats>,
1044 pub scheduler_log: String,
1053 pub scheduler_stdout: String,
1061 pub scheduler_stderr: String,
1067 pub attach: AttachOutcome,
1076 pub dispatched: bool,
1092 pub scheduler_exited: bool,
1097 pub guest_exit_code: Option<i32>,
1103 pub timed_out: bool,
1118 pub crash_message: Option<String>,
1126 pub dilation: Option<f64>,
1133}
1134
1135impl VerifierVmResult {
1136 pub fn cell_verdict(&self) -> Result<(), String> {
1145 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 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 if let Some(reason) = self.attach.failure_reason() {
1182 return Err(format!("scheduler did not turn on — {reason}"));
1183 }
1184 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#[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
1259pub 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
1296pub(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 let validated = topology;
1320 validated
1321 .validate()
1322 .map_err(|e| anyhow::anyhow!("invalid topology {validated:?}: {e}"))?;
1323
1324 let result = crate::test_support::run_vm_with_ap_gap_retry(|| {
1359 let mut builder = crate::vmm::KtstrVm::builder()
1360 .kernel(kernel)
1361 .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 .run_args(&[crate::test_support::VERIFIER_WORKLOAD_FLAG.to_string()])
1375 .topology(validated)
1376 .memory_deferred_min(memory_min_mib)
1377 .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 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 #[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 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 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 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 dilation: result
1503 .host_vcpu_schedstat
1504 .as_ref()
1505 .and_then(|s| s.dilation()),
1506 })
1507}
1508
1509pub fn extract_verifier_log(scheduler_log: &str) -> Option<&str> {
1519 let (start, end) = verifier_log_region(scheduler_log)?;
1520 Some(scheduler_log[start..end].trim_end_matches('\n'))
1524}
1525
1526fn 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 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 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
1568pub 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
1590pub 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 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 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 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
1699pub 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
1722pub 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#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
1763pub struct VerifierCellRecord {
1764 pub scheduler: String,
1766 pub kernel: String,
1768 pub topology: String,
1770 pub passed: bool,
1773 #[serde(default)]
1778 pub skipped: bool,
1779 pub stats: Vec<ProgStats>,
1787}
1788
1789fn 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
1807pub(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
1864pub 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#[derive(Debug, Clone, PartialEq, Eq)]
1888pub enum RunOutcome {
1889 Success,
1891 Failed(String),
1897 SilentExit(i32),
1904}
1905
1906pub 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 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
1968pub 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 "--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
2003pub 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 let mut sched_rows: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
2045 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 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
2124pub fn render_instruction_count_tables(records: &[VerifierCellRecord]) -> Option<String> {
2160 use std::collections::{BTreeMap, BTreeSet};
2161 type VerifiedInsnSpans = BTreeMap<String, BTreeMap<String, BTreeMap<String, (u32, u32)>>>;
2166 let mut by_sched: VerifiedInsnSpans = BTreeMap::new();
2167 let mut sched_progs: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
2169 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 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 #[test]
2584 fn cell_record_write_read_roundtrip_and_retry_overwrites() {
2585 let dir = tempfile::tempdir().expect("result tempdir");
2586 write_cell_record(dir.path(), "not_a_cell", true, false, &[]);
2589 write_cell_record(dir.path(), "verifier/only/two", true, false, &[]);
2590 let name = "verifier/scx_a/kernel_6_14/4cpu-1llc-nosmt";
2592 write_cell_record(dir.path(), name, false, false, &[]);
2593 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 assert_eq!(recs[0].stats, stats, "stats roundtrip via serde");
2629 }
2630
2631 #[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 #[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 #[test]
2715 fn render_result_table_per_scheduler_grids_tally_and_borders() {
2716 let recs = vec![
2717 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 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 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 assert!(
2782 ['│', '─', '┼', '├', '┤', '┬', '┴']
2783 .iter()
2784 .any(|c| out.contains(*c)),
2785 "bordered grid must carry box-drawing chars: {out}"
2786 );
2787 let (a_sec, b_sec) = out.split_once("scx_b:").expect("scx_b section present");
2789 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 assert!(
2801 a_sec.contains("kernel_6_14") && a_sec.contains("kernel_6_15"),
2802 "scx_a kernel columns present: {a_sec}"
2803 );
2804 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 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 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 assert!(
2843 !out.contains('.'),
2844 "summary grid must not render runtime measurements: {out}"
2845 );
2846 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 #[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 #[test]
2895 fn instruction_count_tables_per_scheduler_kernel_program_range() {
2896 let recs = vec![
2897 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 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 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 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 assert!(
2985 out.contains("scx_a:") && out.contains("scx_b:"),
2986 "one section per declared scheduler: {out}"
2987 );
2988 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 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 assert!(
3013 out.contains('-'),
3014 "a (kernel, program) with no stats renders '-': {out}"
3015 );
3016 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 assert!(
3042 !out.contains("4cpu-1llc-nosmt") && !out.contains("128cpu-4llc-smt"),
3043 "topology is not a table axis: {out}"
3044 );
3045
3046 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 #[test]
3070 fn classify_run_outcome_cases() {
3071 use RunOutcome::{Failed, SilentExit, Success};
3072
3073 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 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 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 assert_eq!(
3118 classify_run_outcome(false, false, true, None, Some(100)),
3119 SilentExit(100)
3120 );
3121
3122 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 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 #[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 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 #[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 assert_eq!(
3205 attach_outcome_from_messages(None),
3206 AttachOutcome::Unconfirmed,
3207 );
3208
3209 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 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 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 let not_attached = drain(vec![frame(LifecyclePhase::SchedulerNotAttached, "timeout")]);
3240 assert_eq!(
3241 attach_outcome_from_messages(Some(¬_attached)),
3242 AttachOutcome::NotAttached("timeout".to_string()),
3243 );
3244
3245 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 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 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 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 #[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 #[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 #[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 assert!(!dispatch_confirmed_from_messages(None));
3489
3490 let attached_only = drain(vec![frame(LifecyclePhase::SchedulerAttached)]);
3492 assert!(!dispatch_confirmed_from_messages(Some(&attached_only)));
3493
3494 let dispatched = drain(vec![
3496 frame(LifecyclePhase::SchedulerAttached),
3497 frame(LifecyclePhase::WorkloadDispatched),
3498 ]);
3499 assert!(dispatch_confirmed_from_messages(Some(&dispatched)));
3500
3501 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 #[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 assert_eq!(
3549 base(AttachOutcome::Attached, true, false).cell_verdict(),
3550 Ok(()),
3551 );
3552
3553 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 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 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 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 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 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 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 #[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 #[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 #[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 #[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 #[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 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 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 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 #[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 #[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 let vs_raw = parse_verifier_stats(blob);
4458 assert_eq!(vs_raw.processed_insns, 131071);
4459 }
4460
4461 #[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 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 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 #[test]
4527 fn collapse_verifier_region_collapses_in_place() {
4528 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 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 assert!(
4554 out.contains("identical iterations omitted"),
4555 "the cyclic region must be collapsed in place: {out}",
4556 );
4557 }
4558
4559 #[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 #[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 let out = collapse_verifier_region(input);
4592 assert_eq!(
4593 out, input,
4594 "short region must round-trip byte-for-byte: {out}"
4595 );
4596 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 #[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 #[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 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 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 #[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 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 #[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 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 #[test]
4864 fn extract_verifier_log_between_begin_end_markers() {
4865 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 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 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 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 #[test]
4973 fn parse_sched_output_partial_well_formed_matches_strict() {
4974 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 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 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 let output = format!("{SCHED_OUTPUT_START}\n");
5011 assert!(parse_sched_output_partial(&output).is_none());
5012 }
5013}