Devin Jeanpierre | 2111ede | 2022-04-25 15:52:46 -0700 | [diff] [blame] | 1 | # Struct Layout |
| 2 | |
| 3 | C++ (in the Itanium ABI) extends the C layout rules, and so `repr(C)` isn't |
| 4 | enough. This pages documents the tweaks to Rust structs to give them the same |
| 5 | layout as C++ structs. |
| 6 | |
| 7 | In particular: |
| 8 | |
| 9 | * C++ classes and Rust structs must have the same alignment, so that |
| 10 | references can be exchanged without violating the alignment rules. This is |
| 11 | usually ensured by the regular `#[repr(C)]` layout algorithm, but sometimes |
| 12 | the interop tool needs to generate explicit `#[repr(align(n))]` annotations. |
| 13 | * C++ classes and Rust structs must have the same size, so that arrays of |
| 14 | objects can be exchanged. |
| 15 | * Public subobjects must have the same offsets in C++ and Rust versions of the |
| 16 | structs. |
| 17 | |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 18 | ## Non-field data |
Devin Jeanpierre | 2111ede | 2022-04-25 15:52:46 -0700 | [diff] [blame] | 19 | |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 20 | Rust bindings introduce a `__non_field_data: [MaybeUninit<u8>; N]` field to |
| 21 | cover data within the object that is not part of individual fields. This |
| 22 | includes: |
| 23 | |
| 24 | * Base classes. |
| 25 | * VTable pointers. |
| 26 | * Empty struct padding. |
| 27 | |
| 28 | ### Empty Structs |
| 29 | |
| 30 | One notable special case of this is the empty struct padding. An empty struct or |
| 31 | class (e.g. `struct Empty{};`) has size `1`, while in Rust, it has size `0`. To |
| 32 | make the layout match up, bindings for empty structs will always enforce that |
| 33 | the struct has size of at least 1, via `__non_field_data`. |
Devin Jeanpierre | 2111ede | 2022-04-25 15:52:46 -0700 | [diff] [blame] | 34 | |
| 35 | (In C++, different array elements are guaranteed to have different addresses, |
| 36 | and also, arrays are guaranteed to be contiguous. Therefore, no object in C++ |
| 37 | can have size `0`. Rust, like C++, has only contiguous arrays, but unlike C++ |
| 38 | Rust does not guarantee that distinct elements have distinct addresses.) |
| 39 | |
| 40 | ## Potentially-overlapping objects |
| 41 | |
| 42 | In C++, in some circumstances, the requirement that objects do not overlap is |
| 43 | relaxed: base classes and `[[no_unique_address]]` member variables can have |
| 44 | subsequent objects live inside of their tail padding. The most famous instance |
| 45 | of this is the |
| 46 | [empty base class optimization (EBCO)](https://en.cppreference.com/w/cpp/language/ebo): |
| 47 | a base class with no data members is permitted to take up zero space inside of |
| 48 | derived classes. |
| 49 | |
| 50 | NOTE: This has other, non-layout consequences for Rust: for example, it is not |
| 51 | safe to obtain two `&mut` references to overlapping objects, unless they are of |
| 52 | size `0`. (To prevent this, classes that might be base classes are always |
Googler | 5a7b052 | 2022-08-19 09:51:57 -0700 | [diff] [blame] | 53 | [`!Unpin`](unpin.md).) |
Devin Jeanpierre | 2111ede | 2022-04-25 15:52:46 -0700 | [diff] [blame] | 54 | |
| 55 | This is impossible to represent in a C-like struct. (Indeed, it's impossible to |
| 56 | represent even in a C++-like struct, before the introduction of |
| 57 | `[[no_unique_address]]`). Therefore, in Rust, we don't even try: |
| 58 | potentially-overlapping subobjects are replaced in the Rust layout by a |
| 59 | `[MaybeUninit<u8>; N]` field, where `N` is large enough to ensure that the next |
| 60 | subobject starts at the correct offset. The alignment of the struct is still |
| 61 | changed so that it matches the C++ alignment, but via `#[repr(align(n))]` |
| 62 | instead of by aligning the field. |
| 63 | |
| 64 | ### Example |
| 65 | |
| 66 | For example, consider these two C++ classes: |
| 67 | |
| 68 | ```c++ |
| 69 | // This is a class, instead of a struct, to ensure that it is not POD for the |
| 70 | // purpose of layout. (The Itanium ABI disables the overlapping subobject |
| 71 | // optimization for POD types.) |
| 72 | class A { |
| 73 | int16_t x_; |
| 74 | int8_t y_; |
| 75 | }; |
| 76 | |
| 77 | struct B final : A { |
| 78 | int8_t z; |
| 79 | } |
| 80 | ``` |
| 81 | |
| 82 | In memory, this may be laid out as so: |
| 83 | |
| 84 | ``` |
| 85 | | x_ | x_ | y_ | z | |
| 86 | <------------> <-> |
| 87 | A subobject | B |
| 88 | <------------------> |
| 89 | sizeof(A) |
| 90 | (also sizeof(B)) |
| 91 | |
| 92 | ``` |
| 93 | |
| 94 | The correct representation for `B`, in Rust, is something like this: |
| 95 | |
| 96 | ```rs |
| 97 | #[repr(C)] |
| 98 | #[repr(align(2))] // match the alignment of the int16_t variable. |
| 99 | struct B { |
| 100 | // The We don't use a field of type `A`, because it would have a size of 4, |
| 101 | // and Rust wouldn't permit `z` to live inside of it. |
| 102 | // Nor do we align the array, for the same reason -- correct alignment must be |
| 103 | // achieved via the repr(align(2)) at the top. |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 104 | __non_field_data : [MaybeUninit<u8>; 3]; |
Devin Jeanpierre | 2111ede | 2022-04-25 15:52:46 -0700 | [diff] [blame] | 105 | pub z: i8, |
| 106 | } |
| 107 | ``` |