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

Bit-vector basic operators #24

Closed
wants to merge 44 commits into from
Closed

Bit-vector basic operators #24

wants to merge 44 commits into from

Conversation

chanheec
Copy link
Contributor

This pull request implements assert_bit_vector, supporting basic operators such as shift/and/xor/add/less than/etc. Every bit-vector variable/constant/operation are treated as unsigned.asssert_bit_vector makes a separate z3 query, and the main query assumes its corresponding integer properties. When making the corresponding property, bit operators that are not existing at the integer level (namely <<, >>, &, ^, | ) are translated as uninterpreted functions.

Function level bit vector reasoning is not supported yet, although it can be annotated with
#[verifier(bit_vector)]

if let TypX::Int(IntRange::U(size)) = &**et {
return Some(*size);
}
if let TypX::Int(IntRange::I(size)) = &**et {
Copy link
Collaborator

@yizhou7 yizhou7 Jan 11, 2022

Choose a reason for hiding this comment

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

We should probably not have this case, since we don't yet have support for signed bitvector.

BinaryOp::BitAdd => "bvadd",
BinaryOp::BitSub => "bvsub",
BinaryOp::BitMul => "bvmul",
BinaryOp::BitDiv => "bvudiv", // unsigned
Copy link
Collaborator

Choose a reason for hiding this comment

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

In case we forget, we should to make sure these operations in the original context have unsigned operands.

Copy link
Contributor Author

@chanheec chanheec Jan 12, 2022

Choose a reason for hiding this comment

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

I learned that Rust blocks mixed types(signed and unsigned) for %, /, <, >. One strange thing is that Rust allows having mixed types for shifting. In case that both operands are signed integers, % and / are blocked in 'main' branch.
I made commit that forces bitvector reasoning to only have unsigned integers on a separate branch.
8fdd72f

}

// return bool type if it is comparision op
if f_name.eq("bvlt") || f_name.eq("bvgt") || f_name.eq("bvle") || f_name.eq("bvge") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's better to match on BinaryOp here than to use string comparisons.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will make that change.

assert_bit_vector( (b << 2) >> 2 == b); // FAILS
}
} => Err(err) => assert_one_fails(err)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be good to add a failing test where the assert_bit_vector succeeds but the subsequent assert about integers fails (because it doesn't match the assert_bit_vector).

It might also be good to add a failing test to check that signed integers don't accidentally use the unsigned integer reasoning.

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 agree that those tests are great to have.
About banning signed integers, I will make a few more commits to make sure of that before merging this into main.

@chanheec
Copy link
Contributor Author

Made a new pull request ( #32 ) with a few commits addressing Chris and Yi's comment.

@chanheec chanheec closed this Jan 14, 2022
@chanheec chanheec deleted the bitvector branch January 15, 2022 18:31
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.

4 participants