rust-lang / rust-lang/rust

AIX repr(C) struct layout does not always match C compiler

Open
#151,910 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-repr O-aix T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The layout rules for the f64 type on AIX are very special, and we do not always correctly implement them. https://github.com/rust-lang/rust/pull/149880 improved the situation, but to my knowledge some cases are still wrong. @workingjubilee please correct be if I am wrong.

Our current best understanding of the rather confusingly-written documentation for this is that

  • f64 generally only has an alignment requirement of 4
  • However, sometimes f64 fields will be 8-aligned (and possibly this also affects fields of struct types where the struct contains f64)

In particular, we currently compute the wrong layout for this type:

#[repr(C)]
struct Struct {  
  a1: f64,
  a2: u8,
}

The size should be 16, but it is 12.

Another one (with the comments indicating what the layout should be):

// Size: 24
#[repr(C)]
struct Floats {  
  a: f64, // at offset 0
  b: u8, // at offset 8
  c: f64, // at offset 12
}

Currently we compute the same offsets but we give the type a size of 20.

This can't be fully fixed on the Rust side without something like https://github.com/rust-lang/rfcs/pull/3845 to resolve the conflict between the platform C layout and using the standard layout algorithm that repr(C) uses everywhere else.

Contributor guide

Open the contributing guide

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 by reviewing the AIX repr(C) layout handling and Rust PR #149880, then compare the reported Struct and Floats layouts against the platform C compiler. Confirm which f64 alignment cases remain incorrect and whether the RFC conflict blocks a Rust-side fix; done means the remaining cases are corrected or the limitation is clearly established.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.