210 points · by mvelbaum · 3y ago · 86 comments
This ABI will be limited, but despite those limits it has a lot of utility.
“C isnt a programming language, it’s a protocol”
> Um sorry what? This is Bappyscript, not C. Where’s the Bappyscript interface for Linux?
Right here!
https://man7.org/linux/man-pages/man2/syscall.2.html#NOTES
On Linux, you don't need C at all since you can interface with the kernel directly. The ABI is stable and simple enough that a new programming language could have a system_call keyword that makes the compiler emit the system call code.
With this one piece of functionality, it's possible to do literally anything on Linux. No need for C libraries and their legacy at all. The hardest part will be describing the Linux user space API data structures in the new language so that they can be passed to and from the kernel.
- you still have to take C ideas in account because you don’t know how much stack a syscall wants
- vdso are C-ABI objects
The Linux kernel headers with all relevant structure definitions are here:
This is the most important "feature" of Rust for it's long term success.
Edit: It looks like this is still using C ABI as it's base, which increases the burden for other languages to implement it. What a let down...
How so? It seems to me that actually decreases the burden. For example C# people may not implement the Rust ABI if they don't want to, but since they (and basically every language) already support C ABI, I can implement a library over the existing C ABI so that I can access Rust types.
I assume that the grandparent poster was thinking about making it possible for hypothetical future languages to someday be able to forego supporting the C ABI entirely, which is certainly a laudable goal (there's plenty of historical baggage there), but what the Rust developers care more about here is simply being able to do FFI that's safer than what currently exists.
Basing it on the C ABI makes it only easier for "simple" languages like C to communicate with Rust.
Every language speaks C, and whether we like it or not every new language will continue to speak C for the foreseeable (for-C-able?) future. This cost is already baked in, so you might as well leverage it to reduce the cost of a second ABI by building it on top of the ABI you're already guaranteed to support.
The big problem to me is sharing pointers across FFI which is extremely dangerous in managed languages where the pointer could be invalidated by a GC sweep, but I don't think that's terribly relevant to Rust.
E.g., on x86-64 Linux, the System V x86-64 ABI [0]?
[0] https://www.owalle.com/2021/12/30/function-calling-sequence/...
EDIT: Added a link to the ABI doc.
Notably it doesn't solve anything about versioning. I honestly quite like the Swift approach. It isn't zero overhead but it defaults to really good comparability. Basically it adds a vtable for everything including member offsets and object sizes.
But the really nice thing is that you can tell the compiler when you don't need compatibility (code and types in the same library, or a library that you statically link) and the overhead just disappears.
Of course you lose a few language features (you can't know the size of a type) but then they have tags that you can apply to trade compatibility/flexibility to get those features back.
It does promise backwards compatibility. How is that not sufficient?
Swift allows libraries to naturally evolve while maintaining comparability. Most things that you would expect to work (add a new field) just work. Good luck adding a new field to `jmp_buf`. It's effectively impossible to do in an ABI compatible way. The fact that C's ABI is stable/backwards compatible doesn't help.
Basically a backwards compatible ABI is the absolute minimum requirement for making backwards compatible libraries. Having more options removes heaps of complexity for library authors.
For those unaware, the abi_stable crate makes stable ABIs (even with complex features like trait objects) pretty easy and, importantly, verifiable. It is primarily useful for rust-to-rust abi stability (for instance when creating a plugin system).
[0] https://github.com/WebAssembly/component-model/blob/main/des...
C++ has standardized ABIs but you just don't see people dynamically loading a C++ shared object, obtaining a pointer to a C++ object and calling its member functions. They always do it through the C ABI if they do it at all. Some would rather rewrite stuff in C than put up with that. C++ ABIs are so obnoxiously complex the only things that dare to touch it are C++ compilers and even they have been known to break things. I just get this incredibly hopeless feeling whenever I try to contemplate how I'd handle a C++ exception from Python code. The only reasonable answer I can think of is I wouldn't, I'd just disable C++ exceptions instead and hope for the best.
People use C because it's just simple symbols and unchanging calling conventions. Look up symbols to get function pointers, put parameters in specific registers, call the function and off you go. Nobody will interface with Rust if the ABI is too complex. Given the language's many high level abstractions, I think it's likely we'll see a repeat of the C++ situation above.
The goals in the OP seem to suggest that the objective is not to have a bespoke ABI that encapsulates the entire Rust language, but rather to build a relatively minor shim on top of the existing C ABI that supports some useful Rust patterns (e.g. Option, Result) which should be possible to express straightforwardly in many languages. If this effort succeeds then I can absolutely see it supplanting the C ABI for at least the dynamically-loaded-Rust-from-Rust use case.
No, it has not, at least not as part of the C++ standard.
> but you just don't see people dynamically loading a C++ shared object, obtaining a pointer to a C++ object and calling its member functions
We certainly do, but we have to make sure the ABIs match each other - unless we use COM-style interfaces.
The implementations seem to have converged on a de facto standard ABI.
https://itanium-cxx-abi.github.io/cxx-abi/abi.html
> we have to make sure the ABIs match each other
Which essentially boils down to ensuring everybody is using the exact same compiler, sometimes the same compiler version even.
> unless we use COM-style interfaces
These aren't C++ ABIs though.
That does not match my experience at all. From the top of my head, Qt (QtPlugin) does C++ ABI plugins, and they do that almost transparently in a myriad of platforms.
> I just get this incredibly hopeless feeling whenever I try to contemplate how I'd handle a C++ exception from Python code.
At one point I could even catch C++ exceptions from Java, and viceversa (see gcj).
This?
> provides a bunch of macros that helps us create the C-function that generates the plugin object
Looks like it uses the C ABI.
> At one point I could even catch C++ exceptions from Java, and viceversa (see gcj).
I'm interested in the details of that. Perhaps the GCJ compiler generated some sort of bridge between the languages?
...that's exactly how Autodesk Maya plugin DLLs work, and it's a PITA, because DLLs need to be recompiled with a specific MSVC version for each new Maya version. Qt DLLs on Windows are also separate for each MSVC version.
... Sorry what? This is exactly how every c++ plugin system I've worked with works. There are even some industry standards that work like this, e.g. VST3: https://steinbergmedia.github.io/vst3_dev_portal/pages/Techn... ; there are dozens of companies that provide hosts for such plugins and thousands that provide the actual plugins, all built with different toolchains, compilers etc, so obviously it works
You obtain an IUnknown and query for the supported interfaces
And then there is COM, and with WinRT it is even better as it uses .NET CLS types for the metadata.
So not much (or any) more than it currently is: it's not a stable Rust ABI, it's instead a more expressive ABI compared to C.
But I wonder why DLL linkage wouldn't already be possible without this proposal.
C++ doesn't have a standardized ABI either, but it's possible to load shared libraries with C++ interfaces just fine - as long as they've been created with the same compiler and compiler version. Since there's only a single Rust compiler vendor I'd imagine that dynamic linkage should already be much less trouble than in C++ land?
This is about going beyond that so people can interop with other languages, link with DLLs without rebuilding them every 6 weeks, etc
The general idea is for someone to eventually teach Swift about the slice type etc. When Swift defines the same mappings from its high level types (Optional etc) to C ABI representation as Rust does. And when that work is done you don’t need to drop down to banging out C headers and writing the repetitive conversion code on either end. All that would remain is keeping the “extern” declarations/imports in sync with the exports, which will be much easier once higher level types can be used easily in FFIs. Making an IDL to describe them is out of scope for now but clearly a possibility.