runtimeverification / runtimeverification/mir-semantics

Decoding failing for enum with negative discriminant type

Open
#848 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
52
Forks
5
PR merge metrics
No merged PRs in 30d

Description

Decoding an enum alloc fails when the discriminant is negative typed value. Consider two programs (A and B) which are semantically equivalent:

Program A - passing
#[repr(i8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AccountState {
    Uninitialized = 0,
    Initialized = 1,
    Frozen = -1,
}

fn main() {
    let lhs = unsafe { core::mem::transmute::<i8, AccountState>(0) };
    let rhs = AccountState::Uninitialized;
    assert_eq!(lhs, rhs);
}
Program B - failing
#[repr(i8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AccountState {
    Uninitialized = 0,
    Initialized = 1,
    Frozen = -1,
}

fn main() {
    unsafe {
        assert_eq!(core::mem::transmute::<i8, AccountState>(0), AccountState::Uninitialized);
    }
}

A is passing and B is failing. The difference is that B has the attempts to decode the AccountState::Uninitialized from allocated bytes. It is not the transmute / lhs that is failing, that cast (#cast ( Integer ( 0 , 8 , true ) , castKindTransmute , ty ( 2 ) , ty ( 37 ) )) correctly evaluates into the enum value (Aggregate ( variantIdx ( 0 ) , .List )). It is the rhs / decoding the enum that fails... (ty(37) is AccountState)

thunk ( #decodeConstant ( constantKindAllocated ( allocation ( ... bytes: b"\x00\x00\x00\x00\x00\x00\x00\x00" , provenance: provenanceMap ( ... ptrs: provenanceMapEntry ( ... offset: 0 , allocId: allocId ( 0 ) )  .ProvenanceMapEntries ) , align: align ( 8 ) , mutability: mutabilityMut ) ) , ty ( 25 ) , typeInfoRefType ( ty ( 37 ) ) ) )

We need to look at the decoding rules for enums

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the enum decoding rules and compare the allocated-byte decoding path with the transmute path shown in the issue. Reproduce Programs A and B, then trace why the negative discriminant type causes the enum decode to fail; done means both semantically equivalent programs decode successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.