-
Notifications
You must be signed in to change notification settings - Fork 4
Transmute integer to fieldless enum #840
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
Merged
automergerpr-permission-manager
merged 9 commits into
master
from
dc/cast-u8-to-enum-fieldless
Nov 15, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9e81b90
Added test for transmuting integer to fieldless enum
dkcumming 0bca0e9
Added some validation that the cast is same size and signedness
dkcumming dbce69a
Added symbolic test to show unsound branch
dkcumming 4e010d4
Added tests and corrected rules:
dkcumming 830e45c
Removed `#tagCompatible` and special rule for discriminants with thunk
dkcumming a31513c
Changed `#validDiscriminantAux` to process disjunction to avoid branch
dkcumming c1aff19
Changed rules to work with unsigned numbers and added some more tests
dkcumming 9d1dfa6
Updated documentation
dkcumming 010cda4
Merge branch 'master' into dc/cast-u8-to-enum-fieldless
dkcumming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...on/data/prove-rs/show/transmute-u8-to-enum-changed-discriminant-signed-fail.main.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| ┌─ 1 (root, init) | ||
| │ #execTerminator ( terminator ( ... kind: terminatorKindCall ( ... func: operandC | ||
| │ span: 0 | ||
| │ | ||
| │ (100 steps) | ||
| └─ 3 (stuck, leaf) | ||
| #traverseProjection ( toLocal ( 2 ) , thunk ( #decodeConstant ( constantKindAllo | ||
| span: 153 | ||
|
|
||
|
|
||
| ┌─ 2 (root, leaf, target, terminal) | ||
| │ #EndProgram ~> .K | ||
|
|
||
|
|
||
15 changes: 15 additions & 0 deletions
15
kmir/src/tests/integration/data/prove-rs/show/transmute-u8-to-enum-fail.main.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| ┌─ 1 (root, init) | ||
| │ #execTerminator ( terminator ( ... kind: terminatorKindCall ( ... func: operandC | ||
| │ span: 0 | ||
| │ | ||
| │ (13 steps) | ||
| └─ 3 (stuck, leaf) | ||
| #UBErrorInvalidDiscriminantsInEnumCast ~> .K | ||
| function: main | ||
|
|
||
|
|
||
| ┌─ 2 (root, leaf, target, terminal) | ||
| │ #EndProgram ~> .K | ||
|
|
||
|
|
40 changes: 40 additions & 0 deletions
40
kmir/src/tests/integration/data/prove-rs/transmute-try-from-symbolic.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| use std::convert::TryFrom; | ||
|
|
||
| pub enum ProgramError { | ||
| InvalidAccountData | ||
| } | ||
|
|
||
| #[repr(u8)] | ||
| #[derive(Clone, Copy, Debug, PartialEq)] | ||
| pub enum AccountState { | ||
| Uninitialized, | ||
| Initialized, | ||
| Frozen, | ||
| } | ||
|
|
||
| impl TryFrom<u8> for AccountState { | ||
| type Error = ProgramError; | ||
|
|
||
| #[inline(always)] | ||
| fn try_from(value: u8) -> Result<Self, Self::Error> { | ||
| match value { | ||
| // SAFETY: `value` is guaranteed to be in the range of the enum variants. | ||
| 0..=2 => Ok(unsafe { core::mem::transmute::<u8, AccountState>(value) }), | ||
| _ => Err(ProgramError::InvalidAccountData), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| num_into_state(0); | ||
| } | ||
|
|
||
| fn num_into_state(arg: u8) { | ||
| let state: Result<AccountState, ProgramError> = AccountState::try_from(arg); | ||
|
|
||
| if arg <= 2 { | ||
| assert!(state.is_ok()); | ||
| } else { | ||
| assert!(state.is_err()); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
.../tests/integration/data/prove-rs/transmute-u8-to-enum-changed-discriminant-signed-fail.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #[repr(i8)] | ||
| #[derive(Clone, Copy, Debug, PartialEq)] | ||
| pub enum AccountState { | ||
| Uninitialized = 0, | ||
| Initialized = 1, | ||
| Frozen = -1, | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| assert_eq!(core::mem::transmute::<u8, AccountState>(0), AccountState::Uninitialized); | ||
| assert_eq!(core::mem::transmute::<i8, AccountState>(0), AccountState::Uninitialized); | ||
|
|
||
| assert_eq!(core::mem::transmute::<u8, AccountState>(1), AccountState::Initialized); | ||
| assert_eq!(core::mem::transmute::<i8, AccountState>(1), AccountState::Initialized); | ||
|
|
||
| assert_eq!(core::mem::transmute::<u8, AccountState>(255), AccountState::Frozen); | ||
| assert_eq!(core::mem::transmute::<i8, AccountState>(-1), AccountState::Frozen); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
kmir/src/tests/integration/data/prove-rs/transmute-u8-to-enum-changed-discriminant.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #[repr(u8)] | ||
| #[derive(Clone, Copy, Debug, PartialEq)] | ||
| pub enum AccountState { | ||
| Uninitialized = 11, | ||
| Initialized = 22, | ||
| Frozen = 255, | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| assert_eq!(core::mem::transmute::<u8, AccountState>(11), AccountState::Uninitialized); | ||
| assert_eq!(core::mem::transmute::<u8, AccountState>(22), AccountState::Initialized); | ||
| assert_eq!(core::mem::transmute::<u8, AccountState>(255), AccountState::Frozen); | ||
|
|
||
| // Compiler rejects transmuting between different width types | ||
| // assert_eq!(core::mem::transmute::<u64, AccountState>(-1), AccountState::Frozen); | ||
|
|
||
| // The within the same width the type can be different, here an `-1_i8` is used instead | ||
| // of a `255_u8`, but the bit pattern matches it creates the correct type | ||
| assert_eq!(core::mem::transmute::<i8, AccountState>(-1), AccountState::Frozen); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
kmir/src/tests/integration/data/prove-rs/transmute-u8-to-enum-fail.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #[repr(u8)] | ||
| #[derive(Clone, Copy, Debug, PartialEq)] | ||
| pub enum AccountState { | ||
| Uninitialized, | ||
| Initialized, | ||
| Frozen, | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| std::hint::black_box( | ||
| core::mem::transmute::<u8, AccountState>(4) | ||
| ); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
kmir/src/tests/integration/data/prove-rs/transmute-u8-to-enum.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #[repr(u8)] | ||
| #[derive(Clone, Copy, Debug, PartialEq)] | ||
| pub enum AccountState { | ||
| Uninitialized, | ||
| Initialized, | ||
| Frozen, | ||
| } | ||
|
|
||
| fn main() { | ||
| unsafe { | ||
| assert_eq!(core::mem::transmute::<u8, AccountState>(0), AccountState::Uninitialized); | ||
| assert_eq!(core::mem::transmute::<u8, AccountState>(1), AccountState::Initialized); | ||
| assert_eq!(core::mem::transmute::<u8, AccountState>(2), AccountState::Frozen); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should look into why this is failing. My guess is that the decoding code is not ready for signed discriminants.
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 did a bit of minimised testing and wrote issue #848