Skip to content

ABI compatibility for repr(C) structs (and unions) #626

Open

Description

@hkalbasi

Currently, this code is accepted in miri:

mod m1 {
    unsafe extern "C" {
        pub fn foo(x: Foo);
    }

    #[repr(C)]
    pub struct Foo {
        pub x: i32,
        pub y: i32,
    }
}

mod m2 {
    #[unsafe(no_mangle)]
    extern "C" fn foo(x: Foo) {
        dbg!(x);
    }

    #[repr(C)]
    #[derive(Debug)]
    struct Foo {
        x: i32,
        y: i32,
    }
}

pub fn main() {
    unsafe {
        m1::foo(m1::Foo { x: 5, y: 3 });
    }
}

But it wasn't working in previous versions of miri. If I rename one to Foo2, it no longer works in neither version. To me, it is very surprising that the behavior depends on the name, but I can imagine where it comes from. I didn't find the PR who did this, so I thought it might be a regression somehow. If it is intended, I'm curious about the underlying rules and how it depends on the names.

Activity

  1. RalfJung commented on Oct 11, 2026

    @RalfJung
    Member

    The relevant PR is rust-lang/rust#161734.

    The tl;dr is that the C standard makes the name of the struct relevant, so it is the most plausible thing for us to do as well when checking compatibility of repr(C) types.

  2. hkalbasi commented on Oct 11, 2026

    @hkalbasi
    MemberAuthor

    Makes sense.

    If I understand the C standard correctly, it concerns itself with the struct tag, which doesn't exist as a concept in Rust I think. C structs are compatible with other versions with different names if they get defined in this way:

    typedef struct { int x; } A;

    So we can relax the name condition in the Rust side if we consider all Rust types as untagged in the C sense. Which I think is closer to C. Tags are a very C thing that live in their own namespace and are really independent from the rest of the things. This is valid C:

    typedef struct A { int x; } B;
    typedef int A;

    But in Rust struct names and typedefs live in the same namespace. If we really want to model C struct tags for cross language opsem (I don't think we need) we can use untagged structs by default and have an attribute to control the tag, so the above code becomes:

    #[c_tag("A")]
    struct B { x: c_int }
    
    type A = c_int;

    Using the struct name as the tag is insufficient, as it can't express untagged structs which are very common in C.

  3. changed the title [-]Behavior changed in calling extern function with different ABI compatible structs[/-] [+]Opsem question: Should struct name affect ABI compatiblity?[/+] on Oct 11, 2026
  4. RalfJung commented on Oct 11, 2026

    @RalfJung
    Member

    Using the name follows the convention established by CFI tools, which actually implement those name-dependent semantics in C and which use the struct name in Rust.

    Opsem question:

    Indeed this is more of an opsem question than a Miri issue / discussion, so I will move this over to https://github.com/rust-lang/unsafe-code-guidelines/ if I can.

  5. transferred this issue fromrust-lang/mirion Oct 11, 2026
  6. Jules-Bertholet commented on Oct 12, 2026

    @Jules-Bertholet

    Using the name follows the convention established by CFI tools, which actually implement those name-dependent semantics in C

    Current CFI tools are non-conformant with the C standard as relates to untagged structs.

  7. changed the title [-]Opsem question: Should struct name affect ABI compatiblity?[/-] [+]Should struct name affect ABI compatiblity?[/+] on Oct 12, 2026
  8. hkalbasi commented on Oct 12, 2026

    @hkalbasi
    MemberAuthor

    So if CFI tools wants to use the struct tags, and they become standard conformant in the future, Rust either needs a #[c_untagged] to declare untagged structs or #[c_tag(foo)] for tagged structs to be maximally compatible with C.

    Current CFI tools are non-conformant with the C standard as relates to untagged structs.

    Should I raise this issue with clang? Or is it intended behavior and CFI doesn't care about exact standard semantics?

  9. RalfJung commented on Oct 12, 2026

    @RalfJung
    Member

    Using the name follows the convention established by CFI tools, which actually implement those name-dependent semantics in C

    Current CFI tools are non-conformant with the C standard as relates to untagged structs.

    We had some past discussion about that somewhere, right? Do you remember where?

  10. hkalbasi commented on Oct 15, 2026

    @hkalbasi
    MemberAuthor

    If we end up with names being significant, how would it play with #[cfi_encoding]? Currently, this code is rejected by miri but works with CFI:

    #![feature(cfi_encoding)]
    
    mod m1 {
        unsafe extern "C" {
            pub fn foo(x: Foo);
        }
    
        #[repr(C)]
        #[cfi_encoding = "Foo"]
        pub struct Foo {
            pub x: i32,
            pub y: i32,
        }
    }
    
    mod m2 {
        #[unsafe(no_mangle)]
        extern "C" fn foo(x: Foo2) {
            dbg!(x);
        }
    
        #[repr(C)]
        #[derive(Debug)]
        #[cfi_encoding = "Foo"]
        struct Foo2 {
            x: i32,
            y: i32,
            // z: i32,
        }
    }
    
    fn do_once(f: unsafe extern "C" fn(m1::Foo), arg: m1::Foo) {
        unsafe {
            f(arg);
        }
    }
    
    pub fn main() {
        let f: unsafe extern "C" fn(m1::Foo) = m1::foo;
        do_once(f, m1::Foo { x: 5, y: 3 });
    }

    If the name effect on semantics exists solely to make CFI implementation happy, I guess it should get overriden by the #[cfi_encoding], so the rule of compatibility becomes compatibility of field types + field names + cfi_encoding or struct name if there is no cfi_encoding.

  11. Darksonn commented on Oct 15, 2026

    @Darksonn
    Member

    The effect of cfi_encoding is that it renames the type, and so if we make the name a more important ABI property it may make snese to just rename it to #[abi_name].

  12. Jules-Bertholet commented on Oct 15, 2026

    @Jules-Bertholet

    It's specifically meant to match C struct/union tags, so I think the name should reflect that, e.g #[c_tag = "…"]. Or perhaps #[repr(C, tag = "…")].

  13. RalfJung commented on Oct 15, 2026

    @RalfJung
    Member

    Is cfi_encoding just the tag or does it also overwrite other information about the type that's encoded into CFI information?

  14. Darksonn commented on Oct 15, 2026

    @Darksonn
    Member

    Just the tag

  15. RalfJung commented on Oct 23, 2026

    @RalfJung
    Member

    Apparently "just the tag" is enough to make a struct type identical to an integer though? I thought such type information would be encoded separately.

    But I guess for ABI compatibility it doesn't really matter. The struct and int will still not be ABI-compatible, CFI might just miss that bug if cfi_encoding is misused.

  16. added a commit that references this issue on Oct 23, 2026
  17. added a commit that references this issue on Oct 24, 2026
  18. changed the title [-]Should struct name affect ABI compatiblity?[/-] [+]ABI compatibility for repr(C) structs (and unions)[/+] on Oct 31, 2026
  19. added
    I-opsem-nominated
    on Oct 31, 2026
  20. RalfJung commented on Oct 31, 2026

    @RalfJung
    Member

    Currently Miri accepts this as kind of a hack because otherwise even our own ABI checks don't work reliably enough.

    However, we should get this on solid documented footing. @rust-lang/opsem I think we should have an ABI compatibility rule like the following, do you think that makes sense?

    • Two repr(C) structs are ABI-compatible if they have the same name, contain the same fields with the same names declared in the same order, and the types of their fields are ABI-compatible. Similarly, two repr(C) unions are ABI-compatible under the same conditions.

    This is required by the C standard, given that one can copy-paste a type definition from one library to another to make a cross-library call. I don't know if the field names can ever actually be relevant, I added that part just to be conservative because who knows which ideas CFI may have next. The same goes for the order of fields on a union.

    We do allow overwriting the struct name with #[cfg_encoding]. but that's an unstable attribute so not (yet) relevant for what we put into stable docs.

  21. Darksonn commented on Oct 31, 2026

    @Darksonn
    Member

    I recently learned that CFI includes the module path in the struct's tag for calls using the Rust abi, so the proposed rule only works for calls using the C abi under today's implementation.

    Of course this is easily changed.

  22. RalfJung commented on Oct 31, 2026

    @RalfJung
    Member

    We could restrict the entire rule to the C ABI. However, I'd prefer to treat all ABIs consistently, unless there's a strong reason not to.

  23. Jules-Bertholet commented on Oct 31, 2026

    @Jules-Bertholet

    I think we should further guarantee:

    • Fields with a trivial ABI type are ignored.
    • Behind a pointer, all fields are ignored. (Corresponding to an incomplete struct/union in C)
  24. RalfJung commented on Oct 31, 2026

    @RalfJung
    Member

    Fields with a trivial ABI type are ignored.

    That seems reasonable.

    Behind a pointer, all fields are ignored. (Corresponding to an incomplete struct/union in C)

    Our docs currently say all pointers are ABI-compatible no matter the pointee type. So this part only makes sense once we restrict that, which is blocked on rust-lang/rust#140803.

  25. CAD97 commented on Oct 31, 2026

    @CAD97
    Contributor

    I think what makes the most sense is that the rules for #[repr(C)] types' "tag name" should behave in a comparable way to extern "C" fn. Unfortunately, whether an extern function's link name is mangled by default depends on whether it's a definition or just a declaration, a separation which does not exist for types.

    With that being the case, the option which keeps the most code functional without latent UB is to set the "tag name" to the plain type name, and to require that to match for ABI compatibility. (Along with field names and types being compatible.)

  26. RalfJung commented on Oct 31, 2026

    @RalfJung
    Member

    With that being the case, the option which keeps the most code functional without latent UB is to set the "tag name" to the plain type name, and to require that to match for ABI compatibility. (Along with field names and types being compatible.)

    Yes that is what I had in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-opsem-nominated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions