-
Notifications
You must be signed in to change notification settings - Fork 119
Fix the bug: Static union values can panic Kani #4112
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
Fix the bug: Static union values can panic Kani #4112
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we can do something similar to #3401 instead of adding a padding field?
In particular, we already have logic for codegen_alignment_padding
, so I'd prefer not to introduce a second way of reasoning about alignment and padding if we can avoid it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not following why we need a separate DatatypeComponent
field for unions. Can't we represent the Type
as a vector of DatatypeComponent::Field
and DatatypeComponent::Padding
like we do for structs and enums? So in the example of this test, we'd end up with a Type
constructed from a vector of DataTypeComponent
s where the first item is the [u8; 3]
field, the second element is 1 byte of padding, the third element is the u16
field, and the fourth element is 2 bytes of padding.
Happy to take this discussion offline if that's easier. I would prefer to use the same padding infrastructure that we have for structs and enums if possible. Perhaps it isn't possible, but if it's not I'm not seeing why.
It is difficult to come up with a short answer for it, but after trying several ways of implementation, I feel that this is the most convenient way, for the following observations:
|
2ad7573
This PR fixes the bug: Static union values can panic Kani.
The reason behind the bug is Kani's translation of Union type is not consistent with its layout defined by Rust. Specifically, the size of the Union is not just the max of its fields but that maximum should be round up to a multiple of its alignment.
See:
https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/reference/type-layout.html#:~:text=The%20layout%20of%20a%20type,also%20part%20of%20type%20layout
In this PR, I add another variant for DataComponent: DataComponent::UnionField, with a padded_type of a Union (a struct which contains a padding and the real field type) to fix the translated layout and support reasoning about transmute function for union type.
Resolves #4097
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.