You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ABI compatibility for repr(C) structs (and unions) #626
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.
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.
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:
typedefstruct { intx; } 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:
typedefstructA { intx; } B;
typedefintA;
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")]structB{x:c_int}typeA = c_int;
Using the struct name as the tag is insufficient, as it can't express untagged structs which are very common in C.
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
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.
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?
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.
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].
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.
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.
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.
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.
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.)
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.)
Activity
RalfJung commentedon Oct 11, 2026
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.
hkalbasi commentedon Oct 11, 2026
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:
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:
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:
Using the struct name as the tag is insufficient, as it can't express untagged structs which are very common in C.
[-]Behavior changed in calling extern function with different ABI compatible structs[/-][+]Opsem question: Should struct name affect ABI compatiblity?[/+]RalfJung commentedon Oct 11, 2026
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.
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.
Jules-Bertholet commentedon Oct 12, 2026
Current CFI tools are non-conformant with the C standard as relates to untagged structs.
[-]Opsem question: Should struct name affect ABI compatiblity?[/-][+]Should struct name affect ABI compatiblity?[/+]hkalbasi commentedon Oct 12, 2026
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.Should I raise this issue with clang? Or is it intended behavior and CFI doesn't care about exact standard semantics?
RalfJung commentedon Oct 12, 2026
We had some past discussion about that somewhere, right? Do you remember where?
Jules-Bertholet commentedon Oct 13, 2026
Discussed at https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/Re-evaluate.20ABI.20compatibility.20rules.20in.20light.20of.20CFI/near/585685370 and https://rust-lang.zulipchat.com/#narrow/channel/136281-t-opsem/topic/repr.28Int.29.20enum.20ABI.20compatibility/near/618227999
I advocated for changing CFI to match the C standard, @Darksonn disagreed.
hkalbasi commentedon Oct 15, 2026
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: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.Darksonn commentedon Oct 15, 2026
The effect of
cfi_encodingis 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].Jules-Bertholet commentedon Oct 15, 2026
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 = "…")].RalfJung commentedon Oct 15, 2026
Is
cfi_encodingjust the tag or does it also overwrite other information about the type that's encoded into CFI information?Darksonn commentedon Oct 15, 2026
Just the tag
RalfJung commentedon Oct 23, 2026
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_encodingis misused.Rollup merge of rust-lang#164739 - hkalbasi:cfi_encoding_miri, r=Ralf…
Unrolled build for #164739 in rollup 164819
Rollup merge of #164739 - hkalbasi:cfi_encoding_miri, r=RalfJung
[-]Should struct name affect ABI compatiblity?[/-][+]ABI compatibility for repr(C) structs (and unions)[/+]RalfJung commentedon Oct 31, 2026
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?
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, tworepr(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.Darksonn commentedon Oct 31, 2026
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.
RalfJung commentedon Oct 31, 2026
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.
Jules-Bertholet commentedon Oct 31, 2026
I think we should further guarantee:
RalfJung commentedon Oct 31, 2026
That seems reasonable.
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.
CAD97 commentedon Oct 31, 2026
I think what makes the most sense is that the rules for
#[repr(C)]types' "tag name" should behave in a comparable way toextern "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.)
RalfJung commentedon Oct 31, 2026
Yes that is what I had in mind.