Skip to content

NonZero (unchecked_mul & unchecked_add) Proof for Contracts #338

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

Open
wants to merge 39 commits into
base: main
Choose a base branch
from

Conversation

tautschnig
Copy link
Member

Towards #71

These are the changes contributed by SahithiMV in #184 plus the proposed changes in that PR.

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

aa-luna and others added 25 commits September 20, 2024 14:38
Added contract and placeholder harness
Corrected issues from Pull Request
Restored previous cargo files

macro_rules! nonzero_check_add {
($t:ty, $nonzero_type:ty, $nonzero_check_unchecked_add_for:ident) => {
#[kani::proof_for_contract(<$t>::unchecked_add)]

Choose a reason for hiding this comment

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

Suggested change
#[kani::proof_for_contract(<$t>::unchecked_add)]
#[kani::proof_for_contract(<$nonzero_type>::unchecked_add)]

Copy link
Member Author

Choose a reason for hiding this comment

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

No, that would not produce the right type. We want something like <i8>::unchecked_add, not <NonZeroI8>::unchecked_add.

Choose a reason for hiding this comment

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


macro_rules! check_mul_unchecked_intervals {
($t:ty, $nonzero_type:ty, $nonzero_check_mul_for:ident, $min:expr, $max:expr) => {
#[kani::proof_for_contract(<$t>::unchecked_mul)]

Choose a reason for hiding this comment

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

Suggested change
#[kani::proof_for_contract(<$t>::unchecked_mul)]
#[kani::proof_for_contract(<$nonzero_type>::unchecked_mul)]


macro_rules! check_mul_unchecked_small {
($t:ty, $nonzero_type:ty, $nonzero_check_unchecked_mul_for:ident) => {
#[kani::proof_for_contract(<$t>::unchecked_mul)]

Choose a reason for hiding this comment

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

Suggested change
#[kani::proof_for_contract(<$t>::unchecked_mul)]
#[kani::proof_for_contract(<$nonzero_type>::unchecked_mul)]

Comment on lines +455 to +461
// TODO: not sure how to refer to a parameter that is a mutable reference
// #[requires({
// let size = core::mem::size_of::<T>();
// let ptr = n as *const T as *const u8;
// let slice = unsafe { core::slice::from_raw_parts(ptr, size) };
// !slice.iter().all(|&byte| byte == 0)
// })]

Choose a reason for hiding this comment

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

I would vote removing this (and its harnesses) from this PR entirely. I think these contracts will need simplification before getting merged upstream. My hunch is that the Rust community will need to land on some canonical way of talking about the memory behind references in contracts (at least an experimental one), rather than twist ourselves into knots writing unsafe contracts.
In the meantime, I don't think there's much value-add in merging this in; there's already a similar contract in new_unchecked.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ack, will put in a PR of its own where we can discuss this properly. Also, is unrelated to the PR description.

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.

6 participants