Skip to content

Use 2's complement when computing the relative discriminant to avoid signed to unsigned overflow failures#995

Merged
zhassan-aws merged 3 commits intomodel-checking:mainfrom
zhassan-aws:iss356
Apr 1, 2022
Merged

Use 2's complement when computing the relative discriminant to avoid signed to unsigned overflow failures#995
zhassan-aws merged 3 commits intomodel-checking:mainfrom
zhassan-aws:iss356

Conversation

@zhassan-aws
Copy link
Contributor

Description of changes:

An overflow may occur when casting a signed value to an unsigned one in the computation of an enum discriminant. This overflow is intentional, but gets flagged by CBMC as a failure. This PR works around the issue by using the 2's complement to avoid overflow.

Resolved issues:

Resolves #356

Call-outs:

An alternative approach would be to use CBMC pragmas to turn off signed to unsigned overflow detection during the computation of the relative discriminant. We haven't had much success with the usage of such pragmas in the past though.

Testing:

  • How is this change tested? Two new tests are added.

  • 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 March 29, 2022 23:13
Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

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

Thanks for doing this. Mostly small comments.

.clone()
.cast_to(Type::unsigned_int(64))
.le(Expr::int_constant(relative_max, Type::unsigned_int(64)))
.cast_to(Type::unsigned_int(128))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need 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.

Good point. It is not needed. I removed it and used relative_discr's type for relative_max.

} else {
// This should be a wrapping sub.
niche_val.sub(Expr::int_constant(*niche_start, discr_ty.clone()))
// Compute "niche_val - niche_start" where "-" is
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can you move this to a different function to make this more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

let d = Some(Foo::D);
let e = Some(Foo::E);
let f = Some(Foo::F);
let _ = matches!(a, Some(Foo::A));
Copy link
Contributor

Choose a reason for hiding this comment

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

can we assert?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

#[kani::proof]
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.

nit: I think we should stop using main() and add a more descriptive name to the harness now that we run them by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Updated.

// This test checks that lexicographic comparison is handled correctly

#[kani::proof]
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.

verify_lexicographic_comparison

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done (used check_lexicographic_cmp).

// }
let s64 = Type::signed_int(64);
let niche_val = niche_val.cast_to(s64.clone());
let twos_complement: i64 =
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm adding a general 2's complement in #981

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for adding a function for a general 2's complement! This one is for a specific width though, so was simple enough.

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks

@zhassan-aws zhassan-aws merged commit a2b7532 into model-checking:main Apr 1, 2022
@zhassan-aws zhassan-aws deleted the iss356 branch April 1, 2022 05:59
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.

Verification fails when comparing arrays and string slices.

3 participants