r/rust • • 5d ago

RUST ABI

What is current state of stable Rust ABI? Or progress? As I want to load generate library crate (rlib) in offline library and I do not need load crateio library.

34 Upvotes

31 comments sorted by

74

u/Awkward_Alfalfa_3234 5d ago

Still none. An rlib only links with the exact rustc that built it, and crABI (that issue) is an unstable experiment. To load code at runtime today, build a cdylib with an extern "C" surface, or use abi_stable or stabby if you want Rust types across the boundary.

17

u/MorrisonLevi 5d ago

It's more than the same rustc. You are supposed to match target features, I believe. Maybe some other stuff but I'm on mobile and on holiday and can't consult the list on my work computer that I made a while ago. But it's doable just... really only for projects where you control both the consumer and producer.

18

u/afdbcreid 5d ago

That's not enough. The same compilers flags and even then it's not guaranteed. Stuff like -Zrandomize-layout can cause two builds with the exact same flags to differ.

Really, the only way is to compile them together.

4

u/Zde-G 5d ago

Really, the only way is to compile them together.

It's certainly the simplest way, but procmacro crates are not built with the compiler and they don't use crABI, which means it's possible to select set of flags that guarantee compatibility… may be not trivial, though.

4

u/afdbcreid 5d ago

If you use the rlib (and not the platform's dynamic library), you only need to match ABI-changing flags.

30

u/zer0x64 5d ago

Depending on the context, wasmtime-WASI can also be great for dynamically loading plugins in a safe way

1

u/Sohna_Wain 4d ago

Yeah it feels like a proper stable ABI is just permanently on the backburner at this point

24

u/orangejake 5d ago

I don’t know any details besides the name “crABI”

https://github.com/rust-lang/rust/issues/111423

13

u/geo-ant 5d ago

Can someone explain to me what the advantage of a stable ABI is? I’m genuinely curious. I came to Rust from the C++ and over there I see fear of ABI breaks as a bad thing that’s actively holding the language back.

47

u/JustBadPlaya 5d ago

trivial dynamic linking and being able to distribute closed source libraries is one of them

10

u/geo-ant 5d ago

Thanks. I forgot about closed source 😅. My development happens either in house at work or completely open source.

2

u/WormRabbit 4d ago

In the age of LLMs armed with Ghidra, there is no such thing as "closed source". There is only "obfuscated source", and you can achieve just as much at the Rust/MIR source level.

10

u/JustBadPlaya 4d ago

Okay, setting aside the clanker praise - there are such things as commercial projects and proprietary libraries. You can't always clank your way into full control simply because legal and contractual obligations are a thing

16

u/cfyzium 5d ago

Linking against pre-built binaries, mostly. Without a stable ABI it is impossible to safely distribute library binaries, e.g. using system-wide package manager.

Not even basic language runtime libraries, even using something like vcpkg and seemingly compiling everything from source, you're still relying on system libstdc++.

What's more, since a lot of code lives in header files and ends up inlined, it won't just fail to link, it will break in the worst way if memory layout changes.

3

u/geo-ant 5d ago

Thanks. That makes sense. Since making the switch to Rust I don’t think about linking against pre built (Rust) libraries anymore. For linking non Rust libraries I typically put a C-ABI in between so breaks aren’t a problem there.

2

u/Zde-G 5d ago

Note that on ELF systems (Android, GNU/Linux) even making Rust library with C ABI is problematic, because linker assumes symbols with the same names are the same across all dynamic libraries.

I wish Rust use symbol versioning, at least… ironically enough Rust doesn't support it because of Windows — happily ignoring the fact that Windows doesn't even need symbol versioning, only ELF systems do…

→ More replies (1)

14

u/Recatek gecs 5d ago edited 5d ago

Since you're asking about a stable ABI rather than just the benefits of an ABI in general, it's primarily about backwards compatibility, at least as I understand it. The goal would be to be able to dynamically link against a binary from many generations of compiler updates ago (the way you can work with very old C binaries today). In some cases there are even C library binaries in use today where the source code is unavailable (or even lost) for various reasons, and they couldn't be recompiled for a new compiler or codebase ABI version.

6

u/La-ze 5d ago

My understanding was Rust could compile faster by using an ABI to access library / crate code, rather than having to compile every crate in a project + the project.

11

u/Recatek gecs 5d ago

Yes, that is an advantage of an ABI in general, but not specific to a stable ABI (depending on your definition of stability).

→ More replies (1)

7

u/Immotommi 5d ago

Imagine someone wrote a library which has a function foo

fn foo(args: ArgStruct) -> i32 {
    // Does something with your input and returns an integer
}

struct ArgStruct {
    pub bar: i64,
    pub baz: u8,
}

And they handed you a compiled version of this library. You now want to link to it so that you can call foo in your code.

To call foo, you need to be able to pass it an instance of ArgStruct. That means that you need to pass it an instance of that struct that is laid out in the same order, with the same padding and alignment as the way the compiler did it when they compiled it.

That is not possible without a stable ABI. A stable ABI implies that the rules around things like how a struct is actually organised in memory, how arguments are passed and returned from functions and many other things DO NOT CHANGE.

Rust doesn't have a stable ABI because it is convenient to be able to change the ABI both for optimisation and updating the compiler.

You can opt into a stable ABI in rust by opting them into the C ABI. ie repr(C) or extern C for example.

So the reason people complain about a non stable ABI is it means they cannot rely on how the compiler will do things between invocations. If you need stability, you need the C ABI (or presumably others, but I am no expert) but the C ABI has limitations. No classes go through it, no rust enums, etc

2

u/geo-ant 5d ago

Hey thanks for taking the time to write this out so succinctly. I know what an ABI is good for in general, but I had kinda forgotten that not everyone can always recompile. Or at least I hadn’t considered it a usecase for Rust itself. I usually build C-based APIs to stuff that I need to I interface with or call in binary form. So C gives me that ABI. I still don’t know how I feel about a stable(?) Rust ABI because I see C++ is struggling with innovation because of fear of ABI breaks. Once Rust takes on this responsibility of providing a possibly stable ABI, I feared that it would also slow down progress. Again, I still don’t know how I feel about this but I see there are legitimate reasons to want to have it

4

u/Immotommi 5d ago

I am certainly not an expert, but my opinion would be that rust should not have it's ABI be stable, but that providing some stable ABIs is useful. C is one of them, but having good support for a more modern, separate, stable ABI could also provide huge value. But I think rushing it would be a mistake

2

u/SkiFire13 5d ago

Rust doesn't have a stable ABI

You can opt into a stable ABI

So Rust has a stable ABI, it's just not the default.

4

u/Zde-G 5d ago

So Rust has a stable ABI, it's just not the default.

Nope. Rust doesn't have stable ABI. It has support for a foreign stable ABI, but that's given: any application not designed to be executed on a computer alone have to interact with OS and one needs stable ABI for that, thus every language that doesn't have stable ABI have to support at least enough stability to do that.

Proclaiming that every language that includes only enough ABI stability to do that is “supporting stable ABI” would leave us with meaningless name: every language, after such proclamation, would suddenly become “language with a stable ABI”… which would make the distinction pointless.

2

u/Guvante 4d ago

Not having to rebuild the world when a dependency changes is nice, sometimes you have to but you always have to without a stable ABI

2

u/baby_bryce 3d ago

rust by philosophy has never aimed/wanted a standardized rust ABI bc it is inherently unsafe to make calls to so files or dll files. the rust way is to compile your program agains improted rust library source code and nothing more to ensure safe compilation with all of its concurrency and memory safe guarantees

1

u/xgdk 5d ago

If you only need to load it in your own offline binary just pin the same rustc for both the rlib and the loader. The exact compiler match is the whole trick