Skip to content

Override the print macros with a version suitable for verification - #825

Merged
zhassan-aws merged 5 commits into
model-checking:mainfrom
zhassan-aws:override-print-macros
Feb 15, 2022
Merged

zhassan-aws merged 5 commits into
model-checking:mainfrom
zhassan-aws:override-print-macros

Conversation

@zhassan-aws

Copy link
Copy Markdown
Contributor

Description of changes:

Currently any programs containing (reachable) print macros (e.g. println) result in verification failure (see #824). This PR overrides std's print macros by providing versions that are suitable for verification.

Resolved issues:

Resolves #194 and #824

Call-outs:

The overriden versions of the macros do not properly parse the arguments, so Kani may accept programs that are rejected by rustc, e.g.

fn main() {
    println!("Two arguments expected, but only one provided: {} {}", 4+5);
}

Testing:

  • How is this change tested? Added two new tests.

  • 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.

@zhassan-aws
zhassan-aws requested a review from a team as a code owner February 11, 2022 21:57

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

This is amazing! Thanks for doing it. I just have a few minor change request, but no blocker.

Comment thread library/std/src/lib.rs Outdated
#[macro_export]
macro_rules! evaluate_print_args {
() => { /* do nothing */ };
($x:expr $(, $arg:expr)* $(,)?) => {

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: I'm assuming x is the message itself. Can you please rename it to make this easier to read?

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.

Fixed and added a comment to clarify.

Comment thread library/std/src/lib.rs Outdated
}

// Override the print macros to skip all the formatting functionality (which
/// is not relevant for verification)

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: There's a mix of // and /// in these comments.

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.

Fixed.

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

Really excited about this, thank you @zhassan-aws !!

My only concern about this is the lack of user-facing documentation. What do you think about keeping a list or table of the macros/functions we override that describes the behavior or limitations for each one (e.g., the message formatting call-out in these print macros).

Comment thread tests/kani/Print/side_effects.rs Resolved
Comment on lines +16 to +20
println!("calling function with side-effect from println!: {}", subtract_two(&mut x));
assert!(x == 10);

eprintln!("calling function with side-effect from eprintln!: {}", subtract_two(&mut x));
assert!(x == 15);

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 not test the "no newline" variants here as well?

Comment thread tests/kani/Print/main.rs Outdated
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

// This test checks that println! and eprintln! do not result in verification

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.

Suggested change
// This test checks that println! and eprintln! do not result in verification
// This test checks that `print!`, `println!`, `eprint!` and `eprintln!` do not result in verification

or just use "print macros" to refer to these

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.

Fixed.

@zhassan-aws

Copy link
Copy Markdown
Contributor Author

Really excited about this, thank you @zhassan-aws !!

My only concern about this is the lack of user-facing documentation. What do you think about keeping a list or table of the macros/functions we override that describes the behavior or limitations for each one (e.g., the message formatting call-out in these print macros).

Yes, this makes sense. Should I put this under Limitations section of the docs?

@adpaco

adpaco commented Feb 14, 2022

Copy link
Copy Markdown
Contributor

Yes, I would either add it to that section or create a subsection under limitations for overrides specifically. Both options are fine with me for now, we can define a better structure once we review the documentation.

@zhassan-aws

Copy link
Copy Markdown
Contributor Author

Yes, I would either add it to that section or create a subsection under limitations for overrides specifically. Both options are fine with me for now, we can define a better structure once we review the documentation.

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect verification success on print statement

3 participants