| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Parsing of Miri stdout, JSON stderr diagnostics, and return | |
| //! lines into observed outcomes. | |
| use self::json_stderr::parse_miri_json_stderr; | |
| pub use self::ret_line::parse_miri_ret_line; | |
| use super::*; | |
| /// Parse Miri stdout, stderr, and exit status into an [`ObservedOutcome`]. | |
| pub fn parse_miri_output(exit_code: Option<i32>, stdout: &str, stderr: &str) -> ObservedOutcome { | |
| parse_miri_output_with_token(exit_code, stdout, stderr, None) | |
| } | |
| pub(super) fn parse_miri_output_with_token( | |
| exit_code: Option<i32>, | |
| stdout: &str, | |
| stderr: &str, | |
| expected_token: Option<&str>, | |
| ) -> ObservedOutcome { | |
| if exit_code == Some(0) { | |
| let prefix = miri_ret_prefix_for_token(expected_token); | |
| let mut matched: Option<&str> = None; | |
| for raw_line in stdout.lines() { | |
| if let Some(payload) = raw_line.strip_prefix(&prefix) { | |
| if matched.is_some() { | |
| return ObservedOutcome::ToolError { | |
| detail: format!( | |
| "Miri stdout contained duplicate `{prefix}` markers; stdout={stdout:?}" | |
| ), | |
| }; | |
| } | |
| matched = Some(payload); | |
| } else if expected_token.is_some() | |
| && (raw_line.starts_with(MIRI_RET_PREFIX) | |
| || raw_line.starts_with(&format!("{MIRI_RET_STEM}:"))) | |
| { | |
| return ObservedOutcome::ToolError { | |
| detail: format!( | |
| "Miri stdout contained unauthenticated `{MIRI_RET_STEM}` marker; stdout={stdout:?}" | |
| ), | |
| }; | |
| } | |
| } | |
| let Some(line) = matched else { | |
| return ObservedOutcome::ToolError { | |
| detail: format!("Miri exited 0 without `{prefix}` line; stdout={stdout:?}"), | |
| }; | |
| }; | |
| if line == "PANICKED" { | |
| return ObservedOutcome::Panic { | |
| detail: "panicked during Miri execution (caught by harness)".to_owned(), | |
| }; | |
| } | |
| if line == "UNEXPECTED_RETURN" { | |
| // Represent a clean return cleanly so evaluate_case distinguishes | |
| // normal return from expected UB or panic. | |
| return ObservedOutcome::Returned { value: MirValue::Unit }; | |
| } | |
| return match parse_miri_ret_line(line) { | |
| Ok(value) => ObservedOutcome::Returned { value }, | |
| Err(e) => { | |
| ObservedOutcome::ToolError { detail: format!("parse Miri return {line:?}: {e}") } | |
| } | |
| }; | |
| } | |
| if expected_token.is_some() { | |
| return parse_miri_json_stderr(exit_code, stderr); | |
| } | |
| if stderr.contains("error: internal compiler error:") | |
| || stderr.contains("thread 'rustc' panicked at ") | |
| { | |
| return ObservedOutcome::ToolError { | |
| detail: format!( | |
| "Miri compiler ICE ({:?}): {}", | |
| exit_code, | |
| stderr.lines().take(6).collect::<Vec<_>>().join(" | ") | |
| ), | |
| }; | |
| } | |
| if stderr.contains("error: Undefined Behavior:") { | |
| let headline = stderr | |
| .lines() | |
| .find(|l| l.contains("error: Undefined Behavior:")) | |
| .unwrap_or("Undefined Behavior") | |
| .trim() | |
| .to_owned(); | |
| return ObservedOutcome::UndefinedBehavior { detail: headline }; | |
| } | |
| if stderr.contains("error: abnormal termination:") { | |
| let headline = stderr | |
| .lines() | |
| .find(|l| l.contains("error: abnormal termination:")) | |
| .unwrap_or("abnormal termination") | |
| .trim() | |
| .to_owned(); | |
| return ObservedOutcome::AbnormalAbort { detail: headline }; | |
| } | |
| if stderr.contains("error: unsupported operation:") { | |
| let headline = stderr | |
| .lines() | |
| .find(|l| l.contains("error: unsupported operation:")) | |
| .unwrap_or("unsupported operation") | |
| .trim() | |
| .to_owned(); | |
| return ObservedOutcome::Unsupported { detail: headline }; | |
| } | |
| if stderr.contains("panicked at ") { | |
| let headline = stderr | |
| .lines() | |
| .find(|l| l.contains("panicked at ")) | |
| .unwrap_or("panicked") | |
| .trim() | |
| .to_owned(); | |
| return ObservedOutcome::Panic { detail: headline }; | |
| } | |
| ObservedOutcome::ToolError { | |
| detail: format!( | |
| "Miri exited {:?}: {}", | |
| exit_code, | |
| stderr.lines().take(6).collect::<Vec<_>>().join(" | ") | |
| ), | |
| } | |
| } | |
| #[path = "parse/diag_origin.rs"] | |
| mod diag_origin; | |
| #[path = "parse/json_stderr.rs"] | |
| mod json_stderr; | |
| #[path = "parse/ret_line.rs"] | |
| mod ret_line; | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| #[test] | |
| fn json_stderr_requires_aborting_summary_alongside_fatal_diagnostic() { | |
| let ub_diag = "{\"$message_type\":\"diagnostic\",\ | |
| \"message\":\"Undefined Behavior: entering unreachable code\",\ | |
| \"level\":\"error\",\ | |
| \"spans\":[{\"file_name\":\"/tmp/h/harness.rs\"}],\ | |
| \"children\":[],\ | |
| \"rendered\":\"...\"}\n"; | |
| let missing_summary = parse_miri_output_with_token(Some(1), "", ub_diag, Some("deadbeef")); | |
| assert!( | |
| matches!( | |
| missing_summary, | |
| ObservedOutcome::ToolError { ref detail } | |
| if detail.contains("lacked clean aborting summary") | |
| ), | |
| "{missing_summary:?}" | |
| ); | |
| let with_summary = format!( | |
| "{ub_diag}{{\"$message_type\":\"diagnostic\",\ | |
| \"message\":\"aborting due to 1 previous error\",\ | |
| \"level\":\"error\",\"spans\":[],\"children\":[],\"rendered\":\"...\"}}\n" | |
| ); | |
| let classified = parse_miri_output_with_token(Some(1), "", &with_summary, Some("deadbeef")); | |
| assert!( | |
| matches!( | |
| classified, | |
| ObservedOutcome::UndefinedBehavior { ref detail } | |
| if detail == "error: Undefined Behavior: entering unreachable code" | |
| ), | |
| "{classified:?}" | |
| ); | |
| } | |
| #[test] | |
| fn ret_line_rejects_non_unicode_scalar_char_payloads() { | |
| for bad in ["CHAR:55296", "CHAR:57343", "CHAR:1114112"] { | |
| let err = parse_miri_ret_line(bad).unwrap_err(); | |
| assert!(err.contains("not a Unicode scalar"), "{bad}: {err}"); | |
| } | |
| let outcome = parse_miri_output_with_token( | |
| Some(0), | |
| "ANNEAL_MIRI_RET:deadbeef=CHAR:55296\n", | |
| "", | |
| Some("deadbeef"), | |
| ); | |
| assert!( | |
| matches!( | |
| outcome, | |
| ObservedOutcome::ToolError { ref detail } | |
| if detail.contains("not a Unicode scalar") | |
| ), | |
| "{outcome:?}" | |
| ); | |
| assert_eq!(parse_miri_ret_line("CHAR:97"), Ok(MirValue::Char { scalar: 97 })); | |
| assert_eq!(parse_miri_ret_line("CHAR:1114111"), Ok(MirValue::Char { scalar: 1114111 })); | |
| } | |
| } |