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
i128 / u128 are not compatible with C's definition. #54341
This can be generalized for any T. The complications arise with getting layout right on both sides of the FFI (more specifically it is sound to use repr(Rust) type as long as you manage to get layout of T right on the other side). Since only a few targets have a defined layout for 128-bit integer types and many don’t even have an implementation defined behaviour, it makes perfect sense to delegate them to the same level of FFI support as we do for other repr(Rust) types.
Just to be entirely clear: even if we fixed this particular issue for x86_64 SysV targets, it will only make u128 matching with other x86_64 SysV-abiding artifacts. For most other targets the fact that i/u128 ABI is not specified or is implementation-specific will remain. And so this type can never get any more guarantees than what it currently gets.
Since only a few targets have a defined layout for 128-bit integer types
AFAIK all 64-bit targets except for MSVC have a defined __int128. That's a lot of targets.
Since only a few targets have a defined layout for 128-bit integer types and many don’t even have an implementation defined behaviour, it makes perfect sense to delegate them to the same level of FFI support as we do for other repr(Rust) types.
That means that the layout of these types is unspecified, which prevents users from using i128 to interface with C, requiring us to add a libc::__int128 on those platforms that's incompatible with the Rust type, and requiring users to use that in C FFI.
What's the rationale for not providing a layout that's compatible with __int128 on those platforms?
For all other integer types and bool, we match the C layout of the platform when the platform has the integer type - the stdint.h header is optional, so not all platforms are required to have it.
EDIT: NVM, for the integer types, stdint.h spec matches our, so if it exists, ours match. For bool we do specify it as being equal to C's _Bool, iff the platform has a _Bool. We could do the same for i128.
What's the rationale for not providing a layout that's compatible with __int128 on those platforms?
Nobody argued that we should have a different layout. I agree using a different layout is a bug. I just don't agree it's a soundness bug.
u128/i128 are not FFI-safe when they should be. That's a bug. That's why this issue exists and is still open. We also don't claim that they are FFI safe, so it's not a soundness bug.
Ultimately where to draw the line for soundness bugs can be arbitrary though.
Reacted by gnzlbg, Mara van der Laan, Shuwen Sun, Renato Westphal, Mads Marquart and Raphaël Thériault
Activity
vext01 commentedon Sep 19, 2018
I think the correct fix would be to add
long_doubletype tolibcwith the correct alignment.I guess that's probably not hard? I could have a go at that if it sounds like the right thing to do.
nagisa commentedon Sep 19, 2018
This is a bug in llvm. There was an attempt to fix this in the past, but it got reverted because of reasons.
The sysV __int128 should be 16-byte aligned and rust follows that ABI.
nagisa commentedon Sep 19, 2018
Rather, this is blocked on LLVM data-layouts getting fixed, because rustc expects them to be matched last time I checked.
emilio commentedon Sep 19, 2018
Ugh, thanks @nagisa... I think this is also a problem for
long double, reading that LLVM patch they'd align it to 64 bits. Sigh.Always force repr(align) attributes for stuff with alignment >= 16
strega-nil commentedon Oct 10, 2018
Copying from the last thread, since this thread seems to mostly be about long double.
In C and C++, on x86_64 Linux, alignof(__int128) is equal to 16. However, in Rust, align_of::() is equal to 8.
C++: https://gcc.godbolt.org/z/YAq1XC
Rust: https://gcc.godbolt.org/z/QvZeqK
This will cause subtle UB if you ever try to use i128s across FFI boundaries; for example:
C++: https://gcc.godbolt.org/z/PrtHlp
Rust: https://gcc.godbolt.org/z/_SdVqD
gnzlbg commentedon Jun 24, 2019
The
improper_ctypeswarning diagnoses usage of these types in C FFI, so at least users are alerted that this does not work. Playgroundgnzlbg commentedon Jun 28, 2019
@Centril @eddyb this should be I-unsound.
RalfJung commentedon Jun 28, 2019
Why that? Rust itself is sound. The types are not FFI-safe, but FFI needs unsafe code.
gnzlbg commentedon Jun 28, 2019
Using
i128andu128on FFI is sound. It currently isn't due to a bug on our end, therefore, this is IMO a soundness bug.nagisa commentedon Jun 28, 2019
This can be generalized for any
T. The complications arise with getting layout right on both sides of the FFI (more specifically it is sound to userepr(Rust)type as long as you manage to get layout ofTright on the other side). Since only a few targets have a defined layout for 128-bit integer types and many don’t even have an implementation defined behaviour, it makes perfect sense to delegate them to the same level of FFI support as we do for otherrepr(Rust)types.Just to be entirely clear: even if we fixed this particular issue for x86_64 SysV targets, it will only make u128 matching with other x86_64 SysV-abiding artifacts. For most other targets the fact that i/u128 ABI is not specified or is implementation-specific will remain. And so this type can never get any more guarantees than what it currently gets.
gnzlbg commentedon Jun 28, 2019
AFAIK all 64-bit targets except for MSVC have a defined
__int128. That's a lot of targets.That means that the layout of these types is unspecified, which prevents users from using
i128to interface with C, requiring us to add alibc::__int128on those platforms that's incompatible with the Rust type, and requiring users to use that in C FFI.What's the rationale for not providing a layout that's compatible with
__int128on those platforms?gnzlbg commentedon Jun 28, 2019
For all other integer types and bool, we match the C layout of the platform when the platform has the integer type - thestdint.hheader is optional, so not all platforms are required to have it.EDIT: NVM, for the integer types,
stdint.hspec matches our, so if it exists, ours match. Forboolwe do specify it as being equal to C's_Bool, iff the platform has a_Bool. We could do the same fori128.RalfJung commentedon Jun 28, 2019
Nobody argued that we should have a different layout. I agree using a different layout is a bug. I just don't agree it's a soundness bug.
u128/i128are not FFI-safe when they should be. That's a bug. That's why this issue exists and is still open. We also don't claim that they are FFI safe, so it's not a soundness bug.Ultimately where to draw the line for soundness bugs can be arbitrary though.
101 remaining items
BOTSML-464: Fix rkyv `u128` aligment issue for ARM architectures.
Remove `i128` and `u128` from `improper_ctypes_definitions`
Remove `i128` and `u128` from `improper_ctypes_definitions`
Remove `i128` and `u128` from `improper_ctypes_definitions`
Rollup merge of rust-lang#137306 - tgross35:remove-i128-u128-improper…
Unrolled build for #137306
Rollup merge of #137306 - tgross35:remove-i128-u128-improper-ctypes, …
Remove `i128` and `u128` from `improper_ctypes_definitions`
Remove `i128` and `u128` from `improper_ctypes_definitions`
Rollup merge of rust-lang#137306 - tgross35:remove-i128-u128-improper…
i128 / u128 do not round-trip through the MSVC C ABI (#276)
FFI contract Phase B: every type decision goes through ffi_contract.jl (
Bind __int128, and refuse its unsigned sibling by name
@iRust refuses a 128-bit result, not only a 128-bit argument
Numeric payloads for cxx containers: fixed-width unique_ptr, shared_p…