Skip to content

Introduce project and stop merging all files generated by the compiler - #1956

Merged
celinval merged 7 commits into
model-checking:mainfrom
celinval:issue-1855-rm-glob
Dec 15, 2022
Merged

celinval merged 7 commits into
model-checking:mainfrom
celinval:issue-1855-rm-glob

Conversation

@celinval

@celinval celinval commented Dec 3, 2022 •

Copy link
Copy Markdown
Contributor

Description of changes:

This PR is a pre-req for getting a per-harness reachability algorithm. First we need to stop treating all goto generated by the compiler as the same model which is what this PR does.

For that, I introduce the concept of a project, which is basically a container of artifacts produced by the build system. The goal is to abstracts the verification code from the build system we pick. It also provide methods to get artifacts relevant to a harness. Today this method will return the artifact related to the crate where it is contained, but in the future it can be easily modified to be the harness specific goto model.

As part of this refactoring, this fixes the issue with conflicting harnesses name. I also believe this fixes #1448 and it fixes #1910.

Resolved issues:

Fixes #1974
Fixes #1910

Related RFC:

Call-outs:

  • I still use glob in two scenarios: --legacy-linker and --function. Fixing these two cases will take more effort, and I'm not sure it's worth it at this point. If people feel strong about them, I think they should be fixed in a separate PR since it will require more changes.
  • I am still planning to add more comments and tests for the issues fixed, but I thought I could get some early feedback.
  • I also believe that this change will make it very easy to enable assess for standalone Kani. But it is also out of scope right now.

Testing:

  • How is this change tested? New test + old test

  • Is this a refactor change?

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

edit: Add #1974 as part of the fixes and remove #1448 since it was fixed by the MIR Linker.

- Harnesses with the same name in different crates can interfere with
each other.
@celinval
celinval requested a review from a team as a code owner December 3, 2022 02:19

@tedinski tedinski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot going on here and I'm not sure how to review it. There seems to be lots of behavior changes, some potentially unintended? (e.g. --function?, are the files generated changed? I can't tell. Wait, they must be, that one test changed. Do restrictions still work? There's changes there too and I can't figure out if they're significant. Does this change the behavior of the legacy linker in subtle ways?)

This seems to try to factor out common code from the various entry points, which sure, but the result is just big and complicated and I can't figure it out. I can't tell if that's just a bad abstraction (there's a REASON I went with a small amount of copy/paste code between the entry points) or if that's part of the changes needed to make the multiple artifacts work, or both.

As for path forward, I guess I'd at least like to see:

  1. Some thinking through of the behavior changes. This is I/O heavy code, so isn't (shouldn't won't) be something we can be confident in just by running the regression suite. I'm particularly curious about the deliberate deletion of code saying "hey we have to handle --function when it's not a harness" seemingly without concern?
  2. I think at a minimum if Project is going to be a useful abstraction is needs to be cleaner than it is right now. I don't know how right now. This PR overflowed my ability to understand what was changing

# SPDX-License-Identifier: Apache-2.0 OR MIT
# Test that Kani can handle harnesses in different crates with the same name

[workspace]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this fail before? I thought you'd want to do this with multiple same names under tests/ not multiple crates in a workspace

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this failed before in an unsound way. When we merge all the goto into one, Kani still know it has two harnesses, but their body become the same.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread kani-driver/src/project.rs Resolved
Comment thread kani-driver/src/metadata.rs Resolved
Comment thread kani-driver/src/args.rs Outdated
)]
pub concrete_playback: Option<ConcretePlaybackMode>,
/// Keep temporary files generated throughout Kani process
/// Keep temporary files generated throughout Kani process. This is a no-op for `cargo-kani`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, isn't it? Cargo kani never deletes their temporary files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the comment. Let me know if that makes sense.

/// If the harness has information about the goto_file we can use that to find the exact file.
/// For cases where there is no goto_file, we just assume that everything has been linked
/// together. I.e.: There should only be one artifact of the given type.
pub fn get_harness_artifact(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this exist at all? Shouldn't we record this stuff in HarnessMetadata?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think so. The HarnessMetadata should really just have properties of the harness. The fact that the harness metadata has information about the goto_file is also not great and it was a shortcut to get some of this work done.

I also don't think kani-driver should really be modifying that object. The metadata is a communication mechanism that is meant to go one way, the compiler writes them and the driver reads them. I believe #1758 kind of touches that point.

I really dislike that we create mock harnesses metadata, but this is a problem for later.

The one thing I don't like about this new code is that we are passing HarnessMetadata everywhere. I would like to eventually think about a better mechanism to index a harness or a better structure to pass around.

serde_json::to_writer_pretty(writer, &metadata).unwrap();
}

pub fn cargo_project(session: &KaniSession) -> Result<Project> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would this not be part of call_cargo.rs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main reason was simplicity and trying really hard to scope down this PR. I wanted to try to avoid changing call_cargo.rs because that code itself needs some refactoring (and this PR was already getting too big). So this code does a simple mapping between what call_cargo.rs generate.

FYI, the refactoring I'm talking about is to be able to get rid of cleaning the build artifacts.

Comment thread kani-driver/src/project.rs Outdated
}
}

pub struct StandaloneProjectBuilder<'a> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for? Why is there a builder? And why isn't it part of call_single_file.rs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can flatten it if you want into one function that calls two other functions. We can add the builder if the process becomes more configurable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrapped the builder into a standalone_project() function so the API is symmetric for cargo kani and kani.

@zhassan-aws zhassan-aws left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments from a cursory look. Will take a more thorough look today.

Comment thread kani-driver/src/args.rs Outdated
impl StandaloneArgs {
pub fn validate(&self) {
self.common_opts.validate::<Self>();
self.valid_input().unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this cause a crash if the input is invalid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Let me double check and also add a test case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you were correct. This was crashing. I fixed it. unfortunately I cannot easily add a e2e test because compiletest already provides a valid input. I added a unit test for the valid_input() function.

Comment thread kani-driver/src/project.rs Resolved
Comment thread kani-driver/src/project.rs Resolved
serde_json::to_writer_pretty(writer, &metadata).unwrap();
}

pub fn cargo_project(session: &KaniSession) -> Result<Project> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we call this build_project or compile_project?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, we still have a branch at the beginning of main to see if something is cargo kani or kani, so I kept their names separate. I will try to add symmetry by adding a standalone_project that will replace the builder.

Comment thread kani-driver/src/metadata.rs Outdated
return Ok(vec![harness.clone()]);
}
Ok(metadata.proof_harnesses.clone())
Ok(all_harnesses.iter().cloned().cloned().collect())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double cloned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to be a map to get rid of the double ref.

Comment thread kani-driver/src/main.rs
let results = runner.check_all_harnesses(&harnesses)?;

ctx.print_final_summary(&results)
let project = project::cargo_project(&session)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very neat!

@celinval

celinval commented Dec 7, 2022

Copy link
Copy Markdown
Contributor Author

There is a lot going on here and I'm not sure how to review it. There seems to be lots of behavior changes, some potentially unintended? (e.g. --function?, are the files generated changed? I can't tell. Wait, they must be, that one test changed. Do restrictions still work? There's changes there too and I can't figure out if they're significant. Does this change the behavior of the legacy linker in subtle ways?)

This seems to try to factor out common code from the various entry points, which sure, but the result is just big and complicated and I can't figure it out. I can't tell if that's just a bad abstraction (there's a REASON I went with a small amount of copy/paste code between the entry points) or if that's part of the changes needed to make the multiple artifacts work, or both.

As for path forward, I guess I'd at least like to see:

1. Some thinking through of the behavior changes. This is I/O heavy code, so isn't (shouldn't won't) be something we can be confident in just by running the regression suite. I'm particularly curious about the deliberate deletion of code saying "hey we have to handle `--function` when it's not a harness" seemingly without concern?

2. I think at a minimum if `Project` is going to be a useful abstraction is needs to be cleaner than it is right now. I don't know how right now. This PR overflowed my ability to understand what was changing

I'm sorry this PR is bigger than I would like it to be too. I tried really hard to scope it down, but in some cases just didn't make sense to replicate a hack or purposely inject them in the new code to keep the behavior exactly as it was. It also didn't make sense to create an intermediate fix as a separate PR since the new code was going to fix them by design.

I believe the only behavioral change in this PR is that I disabled --gen-c for --function and --legacy-linker. This was to simplify the code since all these options are for internal usage or on track to be deleted. I could pull out the changes to the args.rs file if that helps to a separate PR.

All the other behavioral changes, they are actually fixes to inconsistent / incorrect behaviors. They are the following:

  1. Kani is now sound when two or more targets contains harnesses or no mangled functions that have the same name (Kani gives wrong result if two harnesses have the same name #1974). Note that this is not the case for --legacy-linker and --function options.
  2. kani now respects the --target-dir argument provided by the customer.
  3. kani no longer crash when the crate has no harness (Standalone kani on file without proof harnesses crashes #1910).

Regarding the deliberate deletion of "hey we have to handle --function when it's not a harness". It was replaced by, "hey, I don't need to care at this point whether the information is in the metadata files". One of the goals here is to have separation of concerns, where the project is built first, which includes linking. Then we verify the code. The code that determines which harness to verify, shouldn't be modifying the project metadata.

I'm happy to talk offline about a better abstraction for Project. I also agree that it could be better, but anything more sophisticated would require even more refactoring. I tried to keep it minimal, today it has 2 public members and 2 public functions.

Comment thread kani-driver/src/args.rs Outdated
if out_dir.exists() && !out_dir.is_dir() {
return Err(Error::raw(
ErrorKind::InvalidValue,
"Invalid argument: `--target-dir` argument `{}` is not a directory",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument (out_dir) is missing from the error message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I'm missing the format call. That's why I need to add tests here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can't. :(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you still planning on adding a format call?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't believe I forgot the format. I must be going crazy. Sorry about that. Let me fix this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I did add format somewhere else. I guess I just missed this one. Thanks for catching this.

   - Fixed comments
   - Improved documentation
   - Fix argument validation + add unit test
   - Add `standalone_project()`
Comment thread kani-driver/src/args.rs Outdated
if out_dir.exists() && !out_dir.is_dir() {
return Err(Error::raw(
ErrorKind::InvalidValue,
"Invalid argument: `--target-dir` argument `{}` is not a directory",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you still planning on adding a format call?

Comment thread kani-driver/src/call_goto_instrument.rs Outdated

let c_demangled = alter_extension(output, "demangled.c");
self.demangle_c(symtabs, &c_outfile, &c_demangled)?;
let symtabs = project.get_harness_artifact(&harness, ArtifactType::SymTab).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a singular symtab?

Comment thread kani-driver/src/project.rs Outdated
///
/// For one specific harness, there should be up to one artifact of each type. I.e., artifacts of
/// the same type are linked as part of creating the project. Except for `symtab` files used for
/// demangling `C` when on legacy linker mode.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this comment still apply given that this PR makes --legacy-linker conflict with --gen-c?

@celinval
celinval merged commit bfdedb8 into model-checking:main Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants