Notes on Type Layouts and ABIs in Rust
gankro.github.io/blah/rust-layouts-and-abis/Posted Oct 10, 2018, 2:06 PM UTC
Posted Oct 10, 2018, 2:06 PM UTC
During the 32-bit era, assuming
intwas exactly 32 bits became so rampant that when 64-bit hardware started to show up, compiler developers were forced to defineintto still be 32 bits, as too much software was completely busted whenintwas anything else.
Fun historical fact: The 32-bit to 64-bit transition was the second bit-width transition for the C ecosystem; the first was the 16-bit to 32-bit transition. During the 16-bit era (when short hadn't been invented, int was 16 bits and long was 32 bits), assuming long was exactly 32 bits became so rampant that when 32-bit hardware started to show up, compiler developers were forced to define long to still be 32 bits, as too much software was completely busted when long was anything else. So on most 32-bit platforms, an "integer" is exactly the same size as a "long integer".
By the time the 64-bit transition came around, there was much more code that assumed 32-bit ints than code that assumed 32-bit longs, so most 64-bit platforms have 32-bit ints and 64-bit longs ... except for Windows, where Microsoft wanted to absolutely maximize backwards compatibility, so 64-bit Windows has int and longas 32-bit integers, and you needlong long` to get a 64-bit integer.
bit-width transition for the C ecosystem
That's the x86 world.
I wasn't around at the time, but as I understand it, the predominant 16-bit platform was the PDP-11, the predominant 32-bit platform was the VAX, and 64-bit issues raised their heads with platforms like the Alpha. In all those cases x86 was a latecomer.
Also known as the data model split. Windows works under LLP64 ("long long" and pointers are 64b) while most unices follow LP64 ("long" and pointers are 64b), but there are also older systems on ILP64 (integers and pointers are 64b) and SILP64 (shorts and pointers are 64b).
And then there's Linux's x32 ABI, where you're running LL64 on a 64b systems (pointers and longs are 32b).
Great writeup. Can anyone speak to how a properly reflected serialization system might work given that structs have nondeterministic ordering or even possible fuzzing. Writing structs to disk only to have them all be corrupt reading back with a program compiled with a different version of rust is a bit scary.
You're supposed to either use repr(C), or serialize/deserialize the fields in declaration order (e.g. individually and not by just memcopying the type into a buffer). The latter is how serde bincode works.
Is it possible to get repr of the type in procmacro to know if layout is predefined or not?
This is a major point of terror for Abomonation. The idea of e.g. randomizing field order to "shake out bugs" is .. pretty much going to cause me to quit Rust if there isn't a complementary "ok, ok, we'll finally get serious about layout and serialization".
I'm a bit worried that there is a group of folks fascinated by struct layout bit-twiddling, without a compensatory group observing that serialization is the main cost in most data intensive systems, and round-tripping through e.g. bincode is way expensive.
I would definitely like to see those features be opt-in with a compiler flag. Making structs mem copyable across abi boundaries is really important to me for performance and I’m actually still only in the evaluation phase for Rust. This one might be a deal breaker depending on how it shakes out
Edit: a long time ago i also evaluated rust and at the time concluded that its approach to struct packing and layouts was not actually a zero cost abstraction and also made it unsuitable as a systems programming language. It was a heavily unpopular comment and the discussion sort of turned me off. I do like many ideas of the language though
Making structs mem copyable across abi boundaries is really important to me
Seeing as how rust currently has no defined ABI anyway, and that repr(C) exists and gives you the C ABI, which is also the one most everything else expects, whats the issue here?
For me at least, that none of the Rust types are repr(C). If you want to use a String, or a Vec or an Option, .. bad luck. Re-wrap your data in your own custom type system, and weep as your "users" head to a different platform rather than do this.
Edit: also, that repr(C) isn't transitive. As the example in the post, you can mark a struct as repr(C), but if it contains a Vec then too bad. If there were instead a trait, or anything you could use to enforce or constrain layouts, I'd be delighted.
Seeing as how rust currently has no defined ABI anyway
This is what u/frankmcsherry and u/m_ninepoints are taking issue with; Rust doesn't currently have a defined ABI. But there are a couple of choices for how to proceed with the evolution of the language.
One option would be to decide that Rust will never have a defined ABI, which gives the compiler writers the freedom to apply new layout optimizations, and they can do tricks like fuzzing struct order to make sure you never try to depend on layout stability as a way of future-proofing code. That option would preclude certain other possibilities, however, like being able to rely on these kinds of no-serialization data transfers, being able to write plugins with a native Rust interface rather than going through C, and so on. It could be made better by having some kind of opt-in stable ABI that goes beyond what repr(C) can do, but that has the same problem as repr(C) of not working for types which aren't explicitly annotated that way.
Another would be to decide Rust doesn't currently have a defined ABI, but that's just a temporary state, and have working towards a defined ABI is a goal. This may mean weighing the benefits of potential new layout optimizations against the costs of delaying ABI stabilization. Many high-value layout optimizations have already landed.
Even if you stabilize the ABI, there are options for not constraining yourself too much; you could still have newer optimizations that are opt-in with new repr attributes, you could have versioned ABI with multiple versions supported at once (the latest by default, but opt-in to older ABIs, for instance), I'm sure there are other possibilities.
A stable ABI for a language like Rust is no small undertaking, and it does constrain you somewhat, but there is real value to having it.
repr(C)exists and gives you the C ABI
As other point out, this only applies if everything recursively is repr(C), which means you can't use it for arbitrary Rust types.
It also doesn't apply to some things you might want to have a stable ABI for; for example, if you want to have a plugin system in which you can load shared libraries as plugins for a Rust program, going through the C ABI means losing a lot of Rust's functionality. Ideally, your plugin should be able to pass objects and callbacks to and from the host using Box<dyn Trait>, &dyn Trait, &dyn Fn, etc. But going through the C ABI, you need to have tables of function pointers with void * user data that can be passed to callbacks and cast to the correct type, and so on, adding a whole lot of unsafe code even for a Rust host working with a Rust plugin.
When I think of “systems language” certain things I believe should be the default. I come primarily for games and consoles in particular. Needing to DMA memory in and out of specialized buffers for network, audio, and graphics is important. If i were to adopt rust and get my team onboard, it’s easiest if it feels like the language was made for our use case. For the most part I like the abstractions as they are defined, but in this area, the proposed default behavior is inverted and I’d rather not need to enforce a policy by adding a struct decorator everywhere.
I'm with you on this, personally. Part of a "systems language" for me is interoperation, and I'd love more attention to consistency here vs high-concept optimizations. IME, the folks in charge are sane (but occasionally err), and staying engaged is important for keeping concerns visible.
I think that the default layout should be implementation defined, and the fuzzer would work then. OTOH there should be a way to explicitly state that a type has a well-defined layout (by using repr).
And to that point, serialization should be done separate of those concerns. Serialization should have its own concerns.
There was a really great compiler dev (gcc?) email about this history but I can't seem to find it anymore. So for the time-being I'll settle for Fabien Giesen's discussion on the matter.
I hope someone knows what the author is talking about here and can link that email chain.
[removed]
I heard about the "type-kinds" for the first time. I'd love to see some concrete examples how an ABI might be different for different type kinds? Any links?
https://www.uclibc.org/docs/psABI-x86_64.pdf#subsection.3.2.3
It determines which registers things get passed in in the x64 linux ABI (SSE vs general purpose), and determines whether things get passed in registers in the x86 linux ABI (structs are always passed on the stack, even if they only contain a single primitive int that would otherwise be passed in a register).
What exactly does an ABI mean for Rust? Like, I can google about it, read wikipedia on it, etc.. I get how it's like an API but compiled. Isn't fixing things like how parameters are passed, etc. managed by the target architecture (handled by LLVM), and not really Rust? I've also heard this in relation to things like debug symbols; how do those fit in? Thanks
API is the source code, ABI is the binary output. A single API scan have different ABIs. This is important for cross-language interoperability, as languages don’t undersatand source code.
The ABI you’re referring to is the C ABI, but that’s not the only option.
It's the layout of the type and how it gets passed to extern "C" fns
Longterm, stable vector-kind types should be available in the stdlib. Short-term they are unstably available in the simd crate.
Small note here: we do have stable vector-kind types, but they are currently defined in platform specific modules. e.g., https://doc.rust-lang.org/core/arch/x86_64/struct.__m128i.html --- Their actual definition is however not platform specific AIUI. Eventually, I think the plan is to add platform independent vector types, e.g., u8x16.
Among vector types, are there distinctions between integer vector types and floating point vector types in the ABI?
Also, this is a super helpful document. Thanks so much for writing it!
x64 linux seems to treat intx and floatx the same, but I don't think anything inherently prevents an ABI from treating them differently.
As of this writing, there are 4 type-kinds that Rust can care about:
There's also one notable 'kind' that Rust currently doesn't support, which is unfortunate: C++ classes with non-trivial copy constructors or destructors. Unlike normal structs/unions, these are forced to be passed by reference, because C++ semantics expect an object to have a stable address. But the details can be platform-specific, so you can't just use a different declaration on the Rust side.
Hm, why does *const T has “all” in the defined values column? I thought that pointers are required to be aligned?
Only if you want to dereference it without using read/write_unaligned; it's perfectly fine for it to be unaligned otherwise.
I'm less clear on &T, though
we have no type-based alias analysis (TBAA, AKA "strict aliasing")
Hm, why don't we have it? Is it not useful for Rust code?
References are guaranteed to not be mutably aliased, theoretically giving the same basic benefits (stronger even?).
Rust references provide a better system of aliasing guarantees than TBAA
I’d like to note here that I’m currently actively working on a type layout randomizer, pertinent to the article’s “fuzzing” motivation for non-deterministic layouts. More details coming over the next few months!
Out of curiousity, can your randomizer insert "useless" padding and/or change alignment? I am currently unclear if that's supposed to be allowed.
e.g. can
struct Foo(u32, u32, u32);have size greater than 12 or align greater than 4 (on x64 linux)?The answer to both questions is: not yet, but it’s easy enough to add.
I plan to make the repo public by mid-December. There are some unfortunate non-code-related constraints on me doing it any sooner.
So it is possible according to language rules? Damn. We need to fix some nasty bugs.
I have no idea. My project works as a source-to-source local-only transformation. What the language itself allows without changing the source code is something I’m even less informed about than u/Gankro.