Skip to content

i128 / u128 are not compatible with C's definition. #54341

Closed

Description

@emilio

While fixing various bindgen bugs related to long double, int128, (rust-lang/rust-bindgen#1370, etc).

I realized that the following Rust program, in my x86_64 Linux machine:

#[repr(C)]
struct Foo {
    f: u128,
}

fn main() {
    println!("Align: {}", ::std::mem::align_of::<Foo>());
}

Prints 8.

While the following C program:

#include <stdio.h>

struct foo {
  unsigned __int128 t;
};

int main() {
  printf("Align: %ld\n", _Alignof(struct foo));
}

Prints 16 on the same system. This is pretty unexpected, and means that i128 / u128 are not really usable for FFI / alignment purposes.

Activity

  1. vext01 commented on Sep 19, 2018

    @vext01
    Contributor

    I think the correct fix would be to add long_double type to libc with 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.

  2. nagisa commented on Sep 19, 2018

    @nagisa
    Member

    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.

  3. nagisa commented on Sep 19, 2018

    @nagisa
    Member

    Rather, this is blocked on LLVM data-layouts getting fixed, because rustc expects them to be matched last time I checked.

  4. emilio commented on Sep 19, 2018

    @emilio
    ContributorAuthor

    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.

    #include <stdio.h>
    
    struct foo {
      long double bar;
    };
    
    int main() {
      printf("%ld\n", _Alignof(struct foo)); // 16
    }
  5. strega-nil commented on Oct 10, 2018

    @strega-nil
    Contributor

    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

  6. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 11, 2018
  7. gnzlbg commented on Jun 24, 2019

    @gnzlbg
    Contributor

    The improper_ctypes warning diagnoses usage of these types in C FFI, so at least users are alerted that this does not work. Playground

  8. gnzlbg commented on Jun 28, 2019

    @gnzlbg
    Contributor

    @Centril @eddyb this should be I-unsound.

  9. RalfJung commented on Jun 28, 2019

    @RalfJung
    Member

    Why that? Rust itself is sound. The types are not FFI-safe, but FFI needs unsafe code.

  10. gnzlbg commented on Jun 28, 2019

    @gnzlbg
    Contributor

    Using i128 and u128 on FFI is sound. It currently isn't due to a bug on our end, therefore, this is IMO a soundness bug.

  11. nagisa commented on Jun 28, 2019

    @nagisa
    Member

    Using i128 and u128 on FFI is sound.

    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.

  12. gnzlbg commented on Jun 28, 2019

    @gnzlbg
    Contributor

    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?

  13. gnzlbg commented on Jun 28, 2019

    @gnzlbg
    Contributor

    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.

  14. RalfJung commented on Jun 28, 2019

    @RalfJung
    Member

    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.

  15. 101 remaining items

  16. added a commit that references this issue on Jun 4, 2025
  17. added a commit that references this issue on Jun 4, 2025
  18. added 2 commits that reference this issue on Jun 17, 2025
  19. added a commit that references this issue on Sep 8, 2026
    35c20c4
  20. added a commit that references this issue on Sep 8, 2026
  21. added a commit that references this issue on Sep 8, 2026
    761576f
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

    A-ABIArea: Concerning the application binary interface (ABI)A-FFIArea: Foreign function interface (FFI)A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions