Skip to content

Commit 79f86f9

Browse files
authored
Support ICE reporting using json diagnostics (#2167)
This will allow us to retrieve crash information from Kani driver once we merge #2166.
1 parent 3d02a42 commit 79f86f9

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

kani-compiler/src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ use kani_queries::{QueryDb, ReachabilityType, UserInput};
4444
use rustc_data_structures::fx::FxHashMap;
4545
use rustc_driver::{Callbacks, RunCompiler};
4646
use rustc_hir::definitions::DefPathHash;
47+
use rustc_interface::Config;
48+
use rustc_session::config::ErrorOutputType;
49+
use session::json_panic_hook;
4750
use std::ffi::OsStr;
4851
use std::path::PathBuf;
4952
use std::rc::Rc;
@@ -141,7 +144,13 @@ fn main() -> Result<(), &'static str> {
141144
struct KaniCallbacks {}
142145

143146
/// Use default function implementations.
144-
impl Callbacks for KaniCallbacks {}
147+
impl Callbacks for KaniCallbacks {
148+
fn config(&mut self, config: &mut Config) {
149+
if matches!(config.opts.error_format, ErrorOutputType::Json { .. }) {
150+
json_panic_hook();
151+
}
152+
}
153+
}
145154

146155
/// The Kani root folder has all binaries inside bin/ and libraries inside lib/.
147156
/// This folder can also be used as a rustc sysroot.

kani-compiler/src/session.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
66
use crate::parser;
77
use clap::ArgMatches;
8+
use rustc_errors::{
9+
emitter::Emitter, emitter::HumanReadableErrorType, fallback_fluent_bundle, json::JsonEmitter,
10+
ColorConfig, Diagnostic,
11+
};
812
use std::panic;
913
use std::str::FromStr;
1014
use std::sync::LazyLock;
@@ -18,7 +22,7 @@ const LOG_ENV_VAR: &str = "KANI_LOG";
1822
const BUG_REPORT_URL: &str =
1923
"https://github.com/model-checking/kani/issues/new?labels=bug&template=bug_report.md";
2024

21-
// Custom panic hook.
25+
// Custom panic hook when running under user friendly message format.
2226
#[allow(clippy::type_complexity)]
2327
static PANIC_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
2428
LazyLock::new(|| {
@@ -30,9 +34,33 @@ static PANIC_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 's
3034

3135
// Print the Kani message
3236
eprintln!("Kani unexpectedly panicked during compilation.");
33-
eprintln!(
34-
"If you are seeing this message, please file an issue here: {BUG_REPORT_URL}"
37+
eprintln!("Please file an issue here: {BUG_REPORT_URL}");
38+
}));
39+
hook
40+
});
41+
42+
// Custom panic hook when executing under json error format `--error-format=json`.
43+
#[allow(clippy::type_complexity)]
44+
static JSON_PANIC_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
45+
LazyLock::new(|| {
46+
let hook = panic::take_hook();
47+
panic::set_hook(Box::new(|info| {
48+
// Print stack trace.
49+
let msg = format!("Kani unexpectedly panicked at {info}.",);
50+
let fallback_bundle =
51+
fallback_fluent_bundle(rustc_errors::DEFAULT_LOCALE_RESOURCES, false);
52+
let mut emitter = JsonEmitter::basic(
53+
false,
54+
HumanReadableErrorType::Default(ColorConfig::Never),
55+
None,
56+
fallback_bundle,
57+
None,
58+
false,
59+
false,
3560
);
61+
let diagnostic = Diagnostic::new(rustc_errors::Level::Bug, msg);
62+
emitter.emit_diagnostic(&diagnostic);
63+
(*JSON_PANIC_HOOK)(info);
3664
}));
3765
hook
3866
});
@@ -94,3 +122,8 @@ fn init_panic_hook() {
94122
// Install panic hook
95123
LazyLock::force(&PANIC_HOOK); // Install ice hook
96124
}
125+
126+
pub fn json_panic_hook() {
127+
// Install panic hook
128+
LazyLock::force(&JSON_PANIC_HOOK); // Install ice hook
129+
}

0 commit comments

Comments
 (0)