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

Correctly handle assert_* intrinsics #742

Merged
merged 1 commit into from
Jan 19, 2022

Conversation

adpaco-aws
Copy link
Contributor

Description of changes:

Adds support for the assert_uninit and assert_zero intrinsics. Inserts the code for them and assert_inhabited into a single function.

Resolved issues:

Resolves #6
Resolves #7

Call-outs:

  • All backends check the condition for assert_inhabited first, then proceed to do the other ones. This proposal follows the same pattern.
  • The actual error for assert_inhabited (i.e., in src/test/rmc/Intrinsics/Assert/inhabited_panic.rs) is
[main.assertion.1] line 13 a panicking function std::mem::MaybeUninit::<T>::assume_init is invoked: FAILURE

This is due to the Nevers hook being applied instead of the Intrinsics one. I have tried doing some changes to the application condition (for example, return false if the instance is an intrinsic) to no success (ended up in some code that expected the Nevers hook to have applied).

  • Some reorganization in tests. The top level folder Assert now contains the multiple assertions test only, and another Assert folder has been created within Intrinsics to keep the tests related to the intrinsics in this PR.

Testing:

  • How is this change tested? Existing regression + 3 new tests + 1 removed test.

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

@adpaco-aws adpaco-aws requested a review from a team as a code owner January 11, 2022 22:44
@adpaco-aws
Copy link
Contributor Author

@zhassan-aws brought #648 to my attention, thanks!!

Removing/Changing output_of_instance_is_never seems the way to go, but it appears that our backend relies on it in a few places so I do not think it will be trivial. I could create a tracking issue for the inhabited.rs test and come back to check once the change has taken place.

Copy link
Contributor

@danielsn danielsn left a comment

Choose a reason for hiding this comment

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

The tests I see are for primitive types. Would it make sense to also ensure that we do the right thing for enums and structs?

@@ -567,6 +548,48 @@ impl<'tcx> GotocCtx<'tcx> {
)
}

/// Generates either a panic or no-op for `assert_*` intrinsics.
/// These are intrinsics that statically compile to panics if the type
/// layout is invalid so we get a message that mentions the offending type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Reference to rust documentation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added links to rust docs.

);
}

if intrinsic == "assert_uninit_valid" && !layout.might_permit_raw_init(self, false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a spec we can point to here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was not able to find one as documentation regarding these intrinsics is quite hard to find.

The closest thing I could find is the actual implementation in rustc_codegen_ssa/src/mir/block.rs (codegen_panic_intrinsic) in addition to PR discussions. But if you know of one I will be happy to add it here.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, I think links directly to code serve as a reasonable reference, too. Maybe add that as a comment? (e.g. link directly to it on github?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My main concern with that is that links to rustc code may become outdated pretty quickly. But even that is better that no links, so just added a note here linking to that function.


// Then we check if the type allows "raw" initialization for the cases
// where memory is zero-initialized or entirely uninitialized
if intrinsic == "assert_zero_valid" && !layout.might_permit_raw_init(self, true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a spec we can point to here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above.

Comment on lines 8 to 10
// The code below attempts to instantiate uninhabited type `!`.
// This should cause the intrinsic `assert_inhabited` to generate a panic during
// compilation, but at present it triggers the `Nevers` hook instead.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a tracking issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Created #751 which I linked here.


// The code below attempts to zero-initialize type `&i32`, causing the intrinsic
// `assert_zero_valid` to generate a panic during compilation.
fn main() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we also have a test with an expected pass (e.g. zero initializing a u8)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test in src/test/rmc/Intrinsics/Assert/zero_valid.rs (path updated in this PR) already does this for several types, including a user-defined struct.

);
}

if intrinsic == "assert_uninit_valid" && !layout.might_permit_raw_init(self, false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, I think links directly to code serve as a reasonable reference, too. Maybe add that as a comment? (e.g. link directly to it on github?)

@adpaco-aws adpaco-aws merged commit b1396f2 into model-checking:main Jan 19, 2022
@adpaco-aws adpaco-aws mentioned this pull request Jan 19, 2022
tedinski pushed a commit to tedinski/rmc that referenced this pull request Apr 22, 2022
tedinski pushed a commit to tedinski/rmc that referenced this pull request Apr 25, 2022
tedinski pushed a commit to tedinski/rmc that referenced this pull request Apr 26, 2022
tedinski pushed a commit that referenced this pull request Apr 27, 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
Development

Successfully merging this pull request may close these issues.

Implement intrinsic assert_zero_valid Implement intrinsic assert_uninit_valid
3 participants