Live data from Hacker News

ABI – Now or Never [pdf]

open-std.org

21–30 of 37 posts

Re: ABI – Now or Never [pdf]

#21
post #19

C++ ABI issues seem to be in this paradoxical situation where the language standard powerbrokers simultaneously feel that a. Having a real binary compatibility story is beneath C++, but b. The accidental ABI compatibility that exists today is too widely adopted to break.

Basically every modern platform (eg free of 90s mistakes) uses the itanium ABI, which defines vtable layout, RTTI layout.

But platforms define the final memory and calling conventions so that can’t be part of any language spec - this is not unique to C++.

Windows has its own ABI, which it has had for a long time, so they can’t change it, so on x86 windows it will always be that.

Re: ABI – Now or Never [pdf]

#22
post #16

Earlier quoted context omitted.

Short version: it passes a pointer to the pointer forcing a double indirection rather than a single. Simple attempts to fix don't really work. Not even sure an ABI break will be enough, but it would at least be a minimum requirement.

Makes me wonder why anybody takes them by value, and not by rvalue-reference. But unique_ptr in public interfaces feels like code smell. I have done it, but not proudly.

Seriously: if you have to pass its address regardless, why pay to construct and destroy another one that you only wanted to std::move out of anyway?

Re: ABI – Now or Never [pdf]

#23
post #3
post #2

Discussion of whether the next (or any future) c++ release should break abi compatibility.

Does C++ have an ABI?

Every implementation implicitly defines an ABI due to the size and layout of classes/structs defined in the STL.

Here's a simple example. Suppose we define std::string with the following layout (for simplicity I'm removing the template stuff, SSO, etc.):

  class string {
   public:
    // various methods here...

    size_t size() const { return len_; }
 
   private:
    char *data_;
    size_t len_;
    size_t capacity_;
  };
When a user calls .size() on a string, the compiler will emit some inlined instructions that access the len_ field at offset +8 bytes into the class (assuming 64-bit system).

Now suppose we modify our implementation of std::string, and we want to change the order of the len_ and capacity_ fields, so the new order is: data_, capacity_, len_. If an executable or library links against the STL and isn't recompiled, it will have inlined instructions that are now reading the wrong field (capacity_).

This is what we mean by the C++ ABI. This is a simple example, but there are a lot of other changes that can break ABI this way.

Re: ABI – Now or Never [pdf]

#24
post #21
post #19

C++ ABI issues seem to be in this paradoxical situation where the language standard powerbrokers simultaneously feel that a. Having a real binary compatibility story is beneath C++, but b. The accidental ABI compatibility that exists today is too widely adopted to break.

Basically every modern platform (eg free of 90s mistakes) uses the itanium ABI, which defines vtable layout, RTTI layout. But platforms define the final memory and calling conventions so that can’t be part of any language spec - this is not unique to C++. Windows has its own ABI, which it has had for a long time, so they can’t change it, so on x86 windows it will always be that.

The ABI discussed here is a bit higher level than that. Even with a fixed ABI for vtables, calling conventions, etc. you still have to care about what happens when you change a "vocabulary type" in the standard library- some changes are source-compatible but binary-breaking.

For example, C++11 broke ABI at this level by changing the representation of std::string.

Re: ABI – Now or Never [pdf]

#25
Am I wrong in thinking that this mild form of schizophrenia ( no official ABI but dont break the ABI) is part of the reason why we now live in a world where all applications talk to each-other through sockets, incurring huge overheads?

Re: ABI – Now or Never [pdf]

#26
post #24
post #21

Earlier quoted context omitted.

Basically every modern platform (eg free of 90s mistakes) uses the itanium ABI, which defines vtable layout, RTTI layout. But platforms define the final memory and calling conventions so that can’t be part of any language spec - this is not unique to C++. Windows has its own ABI, which it has had for a long time, so they can’t change it, so on x86 windows it will always be that.

The ABI discussed here is a bit higher level than that. Even with a fixed ABI for vtables, calling conventions, etc. you still have to care about what happens when you change a "vocabulary type" in the standard library- some changes are source-compatible but binary-breaking. For example, C++11 broke ABI at this level by changing the representation of std::string.

Oof I was unaware of the string ABI break (just had to look it up) - that’s kind of gross :-/

That said in general ABI compatibility is expected of all changes, without very good reasons - security seems like a good one, performance alas not - I assume std::string got the small string optimization because they were already going to break ABI for the threading issues.

Of course it doesn’t help that c++ is still C like so implementations/use of data layout gets compiled directly into the client application :-/

Re: ABI – Now or Never [pdf]

#27

Am I wrong in thinking that this mild form of schizophrenia ( no official ABI but dont break the ABI) is part of the reason why we now live in a world where all applications talk to each-other through sockets, incurring huge overheads?

Yes.

Applications talk via sockets to reduce coupling, thus fragility.

Re: ABI – Now or Never [pdf]

#28

Am I wrong in thinking that this mild form of schizophrenia ( no official ABI but dont break the ABI) is part of the reason why we now live in a world where all applications talk to each-other through sockets, incurring huge overheads?

You can use shmem instead of sockets, which is pretty fast. Not as fast as a direct call, but pretty good; good enough for most purposes.

Re: ABI – Now or Never [pdf]

#29
post #3

Earlier quoted context omitted.

Does C++ have an ABI?

Every implementation implicitly defines an ABI due to the size and layout of classes/structs defined in the STL. Here's a simple example. Suppose we define std::string with the following layout (for simplicity I'm removing the template stuff, SSO, etc.): class string { public: // various methods here... size_t size() const { return len_; } private: char *data_; size_t len_; size_t capacity_; }; When a user calls .siz…

That's not exactly the same. What you're referring to is the library ABI for the c++ standard library. Every library which can be linked to dynamically has its own ABI. The language ABI, on the other hand, describes how every library's ABI is defined, describing things like layout and name mangling. So if, for instance, the language ABI were amended to say that class members are arranged in alphabetical order in memory, then capacity_ will always be at offset 0, data_ at offset 8, and len_ at offset 16; if you change the order of declarations then, the library's ABI won't change. But if the library were compiled with an old compiler that targeted the old ABI, then it would put data_ first, followed by len_, then capacity_. So if you then compiled a new piece of code with a new compiler targeting the new ABI, but linked against it the library, there would be a mismatch.

Re: ABI – Now or Never [pdf]

#30
post #16

Earlier quoted context omitted.

Short version: it passes a pointer to the pointer forcing a double indirection rather than a single. Simple attempts to fix don't really work. Not even sure an ABI break will be enough, but it would at least be a minimum requirement.

Makes me wonder why anybody takes them by value, and not by rvalue-reference. But unique_ptr in public interfaces feels like code smell. I have done it, but not proudly.

Quite a few times, I've made C++ library APIs like this:

    struct iface
    {
        virtual ~iface(){ }
        virtual void doSomething() = 0;
        static std::unique_ptr create();
    }
I don't know good ways to replace unique_ptr here.

Requiring library users to #include the concrete implementation is not good: inflates compilation time, pollutes namespaces, pollutes IDE's autocompletion DB.

Can go C-style i.e. pass double pointer argument to the factory, or return raw pointer. But then user needs to remember to destroy the object.

Post reply on HN