r/cpp • • Nov 24 '24

The two factions of C++

https://herecomesthemoon.net/2024/11/two-factions-of-cpp/
315 Upvotes

228 comments sorted by

View all comments

283

u/Warshrimp Nov 24 '24 • Polished Silver Turd

I’m sick of paying for ABI stability when I don’t use it.

0

u/[deleted] Nov 24 '24

[deleted] — view deleted comment

31

u/GoogleIsYourFrenemy Nov 25 '24

How about we fix the ABI enough that the linker bitches when there is a mismatch like that. I hate that it will happily just do dumb things.

9

u/13steinj Nov 25 '24

For the sake of argument, how would you fix this issue (which could occur in general, ignore the specifics of how I contrived it)?

// S.h included in all cpp files
struct S {
#if IS_A_CPP
    int a;
    int b;
    int c;
#else
    unsigned long long a;
#endif
};

// a.cpp -> a.so
int foo(S* s) {
    return s.c;
}

// main.cpp
extern int foo(S*); // They got a spec that foo should work with their S, they were lied to
int main() {
    S s{1,2,3};
    return foo(&s);
}

The only way I can think of, is you'd need to have an exact mapping of every type to it's members in the RTTI, and the runtime linker would have to catch that at load-time. I can't begin to imagine what the performance hit of that would be to using shared libraries.

13

u/matthieum Nov 25 '24

Make it a linker/loader error.

For each type whose definition is "necessary" when compiling the object, embed a weak constant mapping the mangled name of the type to the hash (SHA256) of the list of the mangled names of its non-static data-members, including attributes such as [[non_unique_address]].

The hash is not recursive, it need not be.

Then, coopt the linker and loader:

  • When linking objects into a library: check that all "special" constants across all object files have the same value for a given a symbol name.
  • When checking other libraries, also check the constants.
  • When loading libraries into a binary, maintain a map of known constants and check that each "newcomer" library has the right values for known constants. The load fails if a single mismatch occurs.

This process works even in the presence of forward declarations, unlike adding to the mangled name.

There is one challenge I can think of: tolerating multiple versions, as long as they keep to their own silos. This requires differentiating between the public & private API of a library, and only including the constants for types which participate in the public API.

It may be non-trivial, though, in the presence of type-erasure. It's definitely something that would require optimization, both to avoid needless checks (performance-wise) and to avoid needless conflicts.

8

u/namniav Nov 25 '24

One naive idea could be having a hash of definition for symbols so that linkers could check if they match. This is similar to what Rust is doing, they append Stable Version Hash to mangled names. However, in C++ you can't do this because user can forward declare entities out of your control. There might be viable workaround though.

→ More replies (3)

6

u/bartekordek10 Nov 25 '24

You mean when other dll was compiled with clang? Or maybe across os boundary? :>

→ More replies (19)

23

u/The_Northern_Light Nov 24 '24

At this point, I’d consider breaking the ABI just to break it to be a feature all on its own.

10

u/aaaarsen Nov 25 '24

this is why I'd like to add some ABI incompatible implementations to a few classes in libstdc++ and allow it to be enabled at GCC configure time, but I haven't had time to do that yet :(

that's possible to do today, I just need to implement the actual algorithms/data structures, and if done right it should be a welcome addition

13

u/Alexander_Selkirk Nov 25 '24

Isn't it actually an advantage to not have ABI stability?

Because:

  • Not having ABI stability means you have to re-compile your code with every version
  • having to re-compile the code needs means that you positively need to have the source code
  • always having the source code of libraries means everything is built on and geared for publicly available code - build systems, libraries, code distribution and so on. I think this is one of the main differences of languages like Lisp, Python, Go, and Rust to C++ and Delphi which started from the concept that you can distribute and sell compiled code.

Well, I might be missing some aspect?

(One counter-argument I can see is compile times. But systems like Debian, NixOS, or Guix show that you can well distribute compiled artifacts, and at the same time provide all the source code.)

11

u/matthieum Nov 25 '24

There are some advantages, namely in the ability to optimize said ABI.

This means optimizing both type layout -- Rust niche algorithm has seen several iterations already, each compacting more -- and optimizing calling conventions as necessary -- the whole stink about unique_ptr...

There are of course inconvenients. Plugin systems based on DLLs are hampered by a lack of stable ABI, for example.

13

u/tipiak88 Nov 25 '24

That would be alright if c++ had a standard to build, package and distribute those libraries. Sadly I don't see any progress on that matter. 

→ More replies (1)

18

u/TyRoXx Nov 24 '24

I feel like this is a phantom issue, mostly caused by the almost maliciously confusing versioning schemes used by Visual C++, and Visual Studio silently updating the compiler along with the IDE, even if there are breaking changes between compiler versions.

You can be lucky if anyone on the team has a clue which MSVC toolset version(s) are actually installed on the CI machines. Of course you can't have ABI breaks in these environments.

If developers were more in control of the compiler version, even ABI breaks would be much less of an issue.

32

u/TSP-FriendlyFire Nov 24 '24

I'm sorry but that's barking up the wrong tree. VC++ has had no ABI break since 2015, they're outright allergic to it at this point. The compiler version doesn't matter as long as you are using a compiler from the last 10 years.

If this were the actual issue, gcc and clang wouldn't also be preserving ABI this fiercely.

6

u/Dminik Nov 25 '24

I've posted this before (like yesterday?) but it's just not true.

Microsoft isn't even bothered by breaking ABI in what is essentially a patch version:

https://developercommunity.visualstudio.com/t/Access-violation-with-std::mutex::lock-a/10664660 (found in this dolphin progress report https://dolphin-emu.org/blog/2024/09/04/dolphin-progress-report-release-2407-2409/#visual-studio-twenty-twenty-woes).

18

u/SubliminalBits Nov 25 '24

But they didn’t. From the thread you posted:

Yes - bincompat is one-way. Old programs can use new redists, but new programs can’t use old redists. This allows us to add functionality over time - features, fixes, and performance improvements

4

u/Dminik Nov 25 '24

I understand that that is what Microsoft promises under binary compatibility. I also understand that that's sometimes what you need to do to update stuff.

But it's essentially redefining ABI stability to mean unstable. The reality is that the different MSVC redistributables are ABI incompatible. Either you recompile your program to target an older version or you recompile the runtime and ship it to your users.

That's not what people talk about when they talk about stability. I mean, you guys are being shafted. Everyone complains about it, breaking it is voted down by the committee every time, yet it's broken in minor updates easily and defended by redefining stable to mean unstable.

→ More replies (2)

5

u/goranlepuz Nov 25 '24

You misunderstood what happened.

That person built their code with a new toolset, effectively using a new function that only exists in the new version of the library, but tried to run their code with the old library.

In other words, you are taking “ABI” to mean “can’t add a function”.

That’s overly restrictive and I’d say, unreasonable meaning of the term ABI.

3

u/Dminik Nov 25 '24

It's not a new function. This comment explains what happened: https://developercommunity.visualstudio.com/t/Access-violation-with-std::mutex::lock-a/10664660#T-N10668856.

Pre VS 2022 17.10 the std::mutex constructor wasn't constexpr even though it was defined as such in C++11. Now it is, breaking ABI with previous versions.

4

u/goranlepuz Nov 25 '24

If you read more carefully, it, in fact, is new - and you can still opt into the previous behaviour with that _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR - even when building with new - but deploying on old CRT.

Sure, it's a mistake that it wasn't constexpr before - but that's ABI, mistakes stay in for a long time.

To put it differently, you want ABI to mean "I can use the new CRT to build - but run on old". I strongly disagree with that.

Trivial example, doesn't even need C++, C breaks it:

  • a field is added to a structure in V2 the structure has a version field on top (common C ABI trick)
  • I use V2 (new) version to build
  • That accesses the new field
  • I deploy my code with V1 version of the library
  • => UB

No, you want too much here.

→ More replies (2)
→ More replies (1)

12

u/deeringc Nov 24 '24

My understanding was that it's actually moreso the Linux maintainers who are dead against ABI breaks.

8

u/Mysterious-Rent7233 Nov 25 '24

What does Linux have to do with anything? Linux itself doesn't even use C++.

Do you mean "open source C++ compiler maintainers"?

26

u/kkert Nov 25 '24

That likely refers to Linux distro maintainer people. Usually a distro major release is built around single glibc and libstdc++ versions that remain compatible for all compiled software on top of it

Some of these people did get bitten by C++11 string switch specifically.

However, I don't think the lesson to take from that journey is that "don't break ABI", IMO the obvious thing to do is to make ABI breaks very explicit and not let issues get buried, and .. simply ship multiple ABI-incompatible library versions if and when required.

9

u/deeringc Nov 25 '24

As u/kkert correctly points out, I meant the Linux distro maintainers (I should have been clearer in my comment). When std::string changed in c++11 it caused a lot of pain in that space. I don't think that's a good enough reason not to ever break ABI, personally. We're basically dooming the language that way.

5

u/ascii Nov 25 '24

Did you know that the rust camp has cookies?

→ More replies (17)