r/programming • • Mar 13 '22

To Save C, We Must Save ABI

https://thephd.dev/to-save-c-we-must-save-abi-fixing-c-function-abi
156 Upvotes

43 comments sorted by

53

u/AVTOCRAT Mar 13 '22

I always appreciate how this type of post (low-level, often C/C++, focused on kernel/compiler/toolchain internals) tends to get lots of upvotes but few if any comments

Not gonna lie I'm pretty terrified too

25

u/tomkeus Mar 14 '22 edited Mar 14 '22

I guess, most of the audience is not knowledgeable enough to have any succinct comments on this, but are knowledgeable enough to learn a few new things from these articles and appreciate it. At least its my case.

28

u/o11c Mar 14 '22

This doesn't seem to discuss the real problem, which function aliases don't solve.

Changing the definition of intmax_t also changes all structs that contain it. This is why the std::string transition took a decade. Whole new compiler features (ABI tags, viral warnings) had to be invented to get past that, and then the real world had to start working past it.

11

u/josefx Mar 14 '22 edited Mar 14 '22

And at least on Linux the standard library still ran into fun issues. The exception hierarchy for iostreams changed. Of course they default to off so hardly anyone noticed that the library would throw a different exception type that your code wouldn't catch unless it was compiled against a specific c++ version.

Edit: seems like I replied to the wrong comment. A different one mentioned the issues with std::string adoption.

4

u/bik1230 Mar 14 '22

But we already have an equivalent tool for that, as mentioned in the article: typedef.

3

u/o11c Mar 14 '22

typedef struct foo struct bar is a syntax error. There are a lot of APIs out there that rely on struct tags, and there's a lot of UB if you don't use a tag consistently (unlike typedefs, which disappear).

While it's true that identifiers that don't refer to an object/function don't have linkage (particularly: struct tags don't have linkage), any access to an object of that type must use a "compatible type" and that requires the tags/body to match.

1

u/johnny219407 Mar 14 '22

Doesn't help if you're passing the string to a library compiled with the other version.

2

u/bik1230 Mar 14 '22

It helps exactly as much as this Alias proposal would help for the function itself. Which is to say, it's not a magic bullet but it gives you (or your OS vendor) the tools to create a path forward while letting older programs still work.

9

u/darkslide3000 Mar 14 '22

Wow... that was a lot of words just to say "I want to standardize __attribute__((alias)) in C23".

And I mean it's a useful feature and seems fine to standardize and all, but I'm not really sure I agree with the author that it's the greatest thing since sliced bread. Can't you just do the same thing with a #define already anyway? I mean it's nice to have non-preprocessor ways to do common things, but it's not existential (and if it was such a perfect solution to solve all library versioning problems, people would already be doing it that way today).

In general, I think renaming symbols under the hood should be done very sparingly and only in exceptional cases with very good justification. The lack of name mangling is one of C's big strength, and a major reason why it's still the preferred language for most low-level systems programmers. I certainly don't want to get to a C standard where I have to remember that printf is now actually called __printf_C27Q3_version_with_MSVC_extension every time I want to call it from assembly.

Most of the time, when you have written yourself into an ABI dead end, it's probably better to just deprecate that API and replace it with a new one (and try to learn your lesson for the design choices in that one).

9

u/Snarwin Mar 14 '22

Can't you just do the same thing with a #define already anyway?

In general, yes, but in the specific case of standard-library functions, no, because the standard requires them to be callable even if macro expansion is bypassed:

Any macro definition of a function can be suppressed locally by enclosing the name of the function in parentheses, because the name is then not followed by the left parenthesis that indicates expansion of a macro function name. For the same syntactic reason, it is permitted to take the address of a library function even if it is also defined as a macro.185) The use of #undef to remove any macro definition will also ensure that an actual function is referred to.

185) This means that an implementation shall provide an actual function for each library function, even if it also provides a macro for that function.

Source: http://port70.net/~nsz/c/c11/n1570.html#7.1.4

1

u/darkslide3000 Mar 14 '22

You don't need a macro with arguments for name replacement. You can do #define printf ..., not #define printf(...) .... That will also work for all of the cases other than explicit #umdef (which code really shouldn't do without reason).

3

u/bloody-albatross Mar 15 '22

Yes, but that breaks any local variables, struct members etc. that happen to also have that name.

1

u/darkslide3000 Mar 15 '22

Does it? All occurrences of the local variable or struct member would be equally remapped from one name to the other (both declarations and uses), so I think that would actually still work.

2

u/bloody-albatross Mar 15 '22

Only if you happen to include the header with the #define everywhere where the struct member is used or defined. Error messages about that wrongly named member will be confusing. Also, less likely, it could introduce a name conflict.

1

u/darkslide3000 Mar 15 '22

All of that applies equally to the _Alias thing?

3

u/bloody-albatross Mar 15 '22

No? _Alias wouldn't replace words in the source like macros do, it would only change the actual symbol at basically link time. Struct members, locals and anywhere else where the sequence of characters making up the function name in question occurs would be unaffected.

1

u/darkslide3000 Mar 15 '22

Well, okay, I see what you're saying. _Alias is still a declaration that needs to be put in a header and all code using the aliased function needs to make sure it includes that header, so I thought it was the same thing. But you're saying that it may spill over in totally unrelated structs from other headers and then some compilation units may happen to include that other header together with the alias header, and others without. Fair enough. It seems pretty unlikely for that to happen in addition to the unlikeliness of shadowing a public library symbol with a local identifier to begin with, but I guess it's possible.

Anyway, I wasn't even trying to argue against adding this to the standard, I think it's fine. I'm just thinking you could basically get 99% there with a #define already, and if it was as useful as the author made it sound people would have regularly been doing that already.

1

u/bloody-albatross Mar 15 '22

Yes, I don't know how important it is in practice, but at least in the standard library there shouldn't be such macros. And glibc already does use versioned symbols and provides multiple implementations that way in a single .so using the GCC syntax. So there is some usage for it.

1

u/Snarwin Mar 14 '22

I don't think the standard permits standard-library functions to be defined as macros without arguments.

Earlier in the same paragraph I quoted, it says:

Any function declared in a header may be additionally implemented as a function-like macro defined in the header

i.e., it gives explicit permission for an implementation to define a standard-library function as a macro with arguments. No such permission is given for macros without arguments ("object-like macros").

1

u/darkslide3000 Mar 15 '22

I mean, no such permission is given for an _Alias feature that doesn't exist yet either. The point is whether it would work in practice without causing issues for existing use cases.

4

u/dreugeworst Mar 14 '22

I can imagine using this would get very confusing once you get into the debugger, with the specific function you're looking for simply not being called, because it was renamed instead

10

u/goranlepuz Mar 14 '22

When listing C ways of breaking the ABI, the article really should have said "calling convention".

I am generally in favor of breaking the ABI, and that goes for C.

Any public interfaces are just better off by being specific and not just a language-level declarations. And the more used they are, the more important that is. Interface Definition Language all the way, baby.

As things stand, C is in a weird place where compiler-specific implementation details have creeped up, poorly, or at least by being poorly publicised, into all sorts of other software, other C implementations etc. The so-called "platform" ABI specifications are obscure. They should not be and they should transcend the language, any language.

ABI is too important to be dealt with in such a manner.

9

u/skulgnome Mar 14 '22

IDL

That way lies COM, which everyone hates.

6

u/next4 Mar 14 '22

Actually, on a technical level, as a language-independent, object-oriented ABI, COM is pretty great. Of course the tooling around COM had been atrocious for the first 20 years of its existence; humans should not have to deal with ref-counts manually. But ever since Microsoft created Windows Metadata format and added compiler support for it, COM, or rather its WinRT incarnation is a way better OS interface than what exists on other platforms.

1

u/goranlepuz Mar 14 '22

I think other language integration tech invented this particular acronym, but yeah... 😉

1

u/skulgnome Mar 14 '22

COM is just the best known one today. They all have the problems of either being heinous to C practice in some way or another, or for not embedding Ada's record discriminants and type system. What every other language does instead is provide FFI to C, and sometimes to C++, and some kind of compatible data structure set-up where the wheels are promised not to fall off.

1

u/goranlepuz Mar 14 '22

What every other language does instead is provide FFI to C, and sometimes to C++, and some kind of compatible data structure set-up where the wheels are promised not to fall off.

Yes, yes, but in the light of the article, it is absolutely amazing that they don't 😉

2

u/jcelerier Mar 14 '22

Oh, but they do all the time. They just get closed as ECANNOTREPRODUCE on random projects on GitHub

2

u/bik1230 Mar 14 '22

Any public interfaces are just better off by being specific and not just a language-level declarations.

And you can't switch to that without breaking the C world, so public ABI and calling convention will remain utterly entwined in C forever (probably).

3

u/ubercaesium Mar 14 '22 edited Mar 14 '22

Oh, this is the ztd.text person. I enjoy their ramblings

3

u/__phantomderp Mar 15 '22

Awww, thank you!

4

u/muti555 Mar 14 '22

the audience is not knowledgeable enough to have any succinct comments on this,

-21

u/shevy-ruby Mar 14 '22

It would be nice if we could come up with a language that would dethrone C. Unfortunately those that tried largely failed ...

13

u/transgc Mar 14 '22

We'd be back at the ABI issue in another 10 years anyway.

I'd definitely enjoy something with stronger typing though. Hopefully rust gets there eventually.

0

u/throwaway99kajillion Mar 14 '22

That's because C is as small as it needs to be and no smaller. Any language that tries to "dethrone" C is going to be exactly like C.

17

u/goranlepuz Mar 14 '22

Ehhh... Array decay, implicit numeric conversions and implicit conversions to "bool" spring to mind...

4

u/ggtsu_00 Mar 14 '22

Most efforts end up just being a meta language that transpiles to C. Much like how most "javascript killers" are just meta languages that transpiles to javascript.

C is a platform, not just a language. We aren't moving away from the C platform any time soon.

2

u/bik1230 Mar 14 '22

Most efforts end up just being a meta language that transpiles to C.

Name one, please? Because as far as I can tell that is not a common strategy among serious projects.

2

u/ggtsu_00 Mar 15 '22

More serious projects end up targeting an LLVM backend which is basically the same as transpiling to C, but just skipping the step of front-end parsing/preprocessing of text C code.

1

u/[deleted] Mar 14 '22

JS is unique pile of turd that's not really comparable to rest of software world

1

u/ggtsu_00 Mar 15 '22

The point was that just like the entire web platform is built up on top of the JavaScript runtime as the core platform, with other programming languages and abstractions eventually just compiling down to something that can run within the JavaScript VM with all of its constraints. That's why no matter how many abstractions and metal languages you put on top of JavaScript, you still can't get away from JavaScript as it sits at the bottom of the abstraction layers.

Similarly every major native OS platform, along with its abstractions are built up on top of C. You can't get away from C because C rests at the bottom of the abstraction layers needed to interop with the OS and hardware.

1

u/[deleted] Mar 15 '22

Nope, the core is CSS/HTML, DOM manipulation and events that are generated from that (onclick etc.). And none of that is actually implemented in JS in the first place.

Wouldn't be a stretch to just give WASM API layer to those, just like JS has.

1

u/[deleted] Mar 14 '22

You can defuse plenty of C landmines and still be small language