Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harness output individual files #3360

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions kani-driver/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ pub struct VerificationArgs {
)]
pub synthesize_loop_contracts: bool,

//Harness Output into individual files
#[arg(long, hide_short_help = true)]
pub output_into_files: bool,

/// Randomize the layout of structures. This option can help catching code that relies on
/// a specific layout chosen by the compiler that is not guaranteed to be stable in the future.
/// If a value is given, it will be used as the seed for randomization
Expand Down Expand Up @@ -624,6 +628,20 @@ impl ValidateArgs for VerificationArgs {
));
}

if self.output_into_files
&& !self.common_args.unstable_features.contains(UnstableFeature::UnstableOptions)
{
if self.common_args.enable_unstable {
print_deprecated(&self.common_args, "`--enable-unstable`", "-Z unstable-options");
} else {
return Err(Error::raw(
ErrorKind::MissingRequiredArgument,
"The `--output-into-files` argument is unstable and requires `-Z unstable-options` to enable \
unstable options support.",
));
}
}

Ok(())
}
}
Expand Down
66 changes: 54 additions & 12 deletions kani-driver/src/harness_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ use crate::call_cbmc::{VerificationResult, VerificationStatus};
use crate::project::Project;
use crate::session::KaniSession;

use std::fs::File;
use std::io::Write;

use std::env::current_dir;
use std::path::PathBuf;

/// A HarnessRunner is responsible for checking all proof harnesses. The data in this structure represents
/// "background information" that the controlling driver (e.g. cargo-kani or kani) computed.
///
Expand Down Expand Up @@ -101,6 +107,53 @@ impl<'sess, 'pr> HarnessRunner<'sess, 'pr> {
}

impl KaniSession {
fn process_output(&self, result: &VerificationResult, harness: &HarnessMetadata) {
if self.should_print_output() {
if self.args.output_into_files {
self.write_output_to_file(result, harness);
}

let output = result.render(
&self.args.output_format,
harness.attributes.should_panic,
self.args.coverage,
);
println!("{}", output);
}
}

fn should_print_output(&self) -> bool {
!self.args.common_args.quiet && self.args.output_format != OutputFormat::Old
}

fn write_output_to_file(&self, result: &VerificationResult, harness: &HarnessMetadata) {
let target_dir = self.result_output_dir().unwrap();
let file_name = target_dir.join(harness.pretty_name.clone());
let path = Path::new(&file_name);
let prefix = path.parent().unwrap();

std::fs::create_dir_all(prefix).unwrap();
let mut file = File::create(&file_name).unwrap();
let file_output = result.render(
&OutputFormat::Regular,
harness.attributes.should_panic,
self.args.coverage,
);

if let Err(e) = writeln!(file, "{}", file_output) {
eprintln!(
"Failed to write to file {}: {}",
file_name.into_os_string().into_string().unwrap(),
e
);
}
}

fn result_output_dir(&self) -> Result<PathBuf> {
let target_dir = self.args.target_dir.clone().map_or_else(current_dir, Ok)?;
Ok(target_dir.join("kani_results"))
}

/// Run the verification process for a single harness
pub(crate) fn check_harness(
&self,
Expand All @@ -119,18 +172,7 @@ impl KaniSession {
} else {
let mut result = self.with_timer(|| self.run_cbmc(binary, harness), "run_cbmc")?;

// When quiet, we don't want to print anything at all.
// When output is old, we also don't have real results to print.
if !self.args.common_args.quiet && self.args.output_format != OutputFormat::Old {
println!(
"{}",
result.render(
&self.args.output_format,
harness.attributes.should_panic,
self.args.coverage
)
);
}
self.process_output(&result, harness);
self.gen_and_add_concrete_playback(harness, &mut result)?;
Ok(result)
}
Expand Down
Loading