Skip to content

Add a zero_initializer function - #1230

Closed
danielsn wants to merge 10 commits into
model-checking:mainfrom
danielsn:zero_init
Closed

danielsn wants to merge 10 commits into
model-checking:mainfrom
danielsn:zero_init

Conversation

@danielsn

@danielsn danielsn commented May 26, 2022 •

Copy link
Copy Markdown
Contributor

Description of changes:

Right now, we can only zero_init scalar types. This adds a recursive form to allow us to zero_init complex types as well.

Resolved issues:

Resolves #ISSUE-NUMBER

Call-outs:

Using this may improve performance, at a potential soundness cost. Since the tradeoff is not clear, putting it behind a flag to allow experimentation, but keeping the current default for now.

Testing:

  • How is this change tested?

    1. Existing regression tests. Turned on the flag, all regressions still pass, except for ones testing maybe_uninit, which we would expect to fail.
    2. Existing regression tests. Flag off, all regressions still pass.
    3. New test which maybe_units a variable and checks that it is all 0.
  • Is this a refactor change? No

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.

@danielsn
danielsn requested a review from a team as a code owner May 26, 2022 22:00
@danielsn
danielsn marked this pull request as draft May 26, 2022 22:00
@danielsn
danielsn force-pushed the zero_init branch 2 times, most recently from 2dd9035 to 8b6b50c Compare June 1, 2022 01:39
@tautschnig

Copy link
Copy Markdown
Member

I re-enabled the RustRealloc hook that was removed in #1088 and then tried the examples from #702 with this PR. I got:

$ kani 702.rs --enable-unstable --zero-init-vars
thread 'rustc' panicked at 'Can't zero init Code { parameters: [Parameter { typ: Pointer { typ: CInteger(SizeT) }, identifier: None, base_name: None }, Parameter { typ: Pointer { typ: CInteger(SizeT) }, identifier: None, base_name: None }], return_type: StructTag("tag-_11659839863328250985") }', cprover_bindings/src/goto_program/typ.rs:1262:38
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Kani unexpectedly panicked during compilation.
If you are seeing this message, please file an issue here: https://github.com/model-checking/kani/issues/new?labels=bug&template=bug_report.md

[Kani] current codegen item: codegen_function: std::cmp::max_by
_RINvNtCs8yXqNEgggfL_4core3cmp6max_byjNvYjNtB2_3Ord3cmpECs1cE2uPJmwEu_3_702
[Kani] current codegen location: Loc { file: "/home/ubuntu/.rustup/toolchains/nightly-2022-05-17-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs", function: None, line: 1276, col: Some(1) }
Error: [...]/kani-compiler exited with status exit status: 101

@danielsn

Copy link
Copy Markdown
Contributor Author

blocked on #1243

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.

Nit: This could be written in a nicer way with if let Some(largest) = ... { ... } else { /* empty case */ }, thus avoiding unwrap().

Comment thread kani-driver/src/args.rs Resolved
@fzaiser

fzaiser commented Aug 1, 2022

Copy link
Copy Markdown
Contributor

Looks good to me. Two things:

  • Is there a way this could be tested? Specifically test that it does zero initialization? (Not sure it's worth the hassle.)
  • Did you observe a performance improvement on any examples?

@fzaiser

fzaiser commented Aug 1, 2022

Copy link
Copy Markdown
Contributor

One more thing: shouldn't we zero-initialize StatementKind::DeInit as well? As far as I can tell, that part of the code still uses nondet. In particular, DeInit is used to "initialize" generators, so they wouldn't benefit from the PR as is.

@celinval celinval 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.

Can you at least add a few sanity tests? Have you tried measuring the performance gain at all? I am a bit worried about this PR since it is doing something way out of the rust spec.

@fzaiser

fzaiser commented Aug 3, 2022

Copy link
Copy Markdown
Contributor

Looks good to me, except I think this should have a test. Running Kani with the flag and checking that the value of an uninitialized variable is zero (or something to that effect). Or is there a reason not to test this?

@celinval celinval 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.

Thanks for adding a test, but it does show how this feature is unsound. I don't think we should add this code to Kani.

I would prefer if you publish a separate PR for the code that isn't directly related to this feature and keep the zero_initializer stuff in a branch for now.

If it is too painful to keep the zero_initializer logic on a branch, then I would suggest that we remove the kani option and just use a constant to enable / disable this code.

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.

How are the changes in this file related to the zero initialization?

@danielsn

danielsn commented Aug 3, 2022

Copy link
Copy Markdown
Contributor Author

What about an --enable-unsound flag

@fzaiser fzaiser changed the title Add a zero_initilizer function Add a zero_initializer function Aug 4, 2022
@danielsn danielsn self-assigned this Aug 9, 2022
@danielsn danielsn closed this Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants