Understanding GCC 5 _GLIBCXX_USE_CXX11_ABI or the new ABI

https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

I ran into problems with / valgrind crash using std :: string in GCC 5. The link above indicates that GCC 5.x is running in ABI. The new default ABI for libstd ++ is C ++ 11/14 ... which is incompatible with the old ABI. There is a way to select a senior ABI using the definition.

I am trying to understand what is the difference between ABI and did not find details. I would like to help understand:

  • What problems with std :: string need to be fixed in order to be compatible with the new ABI? Are they associated with the copy to record?
  • Will these changes change it for an older ABI?
  • Any tips for working _GLIBCXX_USE_CXX11_ABI?

More on the issue I encountered ( https://github.com/YasserAsmi/jvar/issues/21 ) The project worked great in GCC 4.8 and Clang. With GCC, the same code refuses to run:

x_misc(33112,0x7fff728c2000) malloc: *** error for object 0x7fd639c034cc: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap: 6 

And here is the partial output of Valgrind:

 ==33027== Invalid read of size 1 ==33027== at 0x1006F78BA: _platform_memmove$VARIANT$Nehalem (in /usr/lib/system/libsystem_platform.dylib) ==33027== by 0x100009388: jvar::Variant::toString[abi:cxx11]() const (in bin/ex_misc) ==33027== by 0x1000023A7: bugreport() (in bin/ex_misc) ==33027== by 0x1000133B8: main (in bin/ex_misc) 

The project uses std :: string and has some custom memory management. It performs some non-standard but valid operations using new placement constructors, etc. I try to better understand what code the API runs and how to fix it - a place to run.

+14
source share
2 answers
  • The old std::string not compatible with C ++ 11, since this standard prohibits the implementation of copy to write. It was not possible to create a compatible std::string without breaking the ABI, so they did this to return to the incompatible version for compatibility with ABI.

  • Yes.

  • Make sure all translation units in your program use the same _GLIBCXX_USE_CXX11_ABI value and everything should be in order. If you mix them with translation units, you are likely to have problems. You may be fine if you have different definition values ​​in different translation units that do not bind string each other.

+8
source

There is an interesting difference between the COW string and non-COW, for example:

 std::string some_string; std::string foo() { return some_string; } char const *s = foo().c_str(); printf("%s\n",s); 

This will work with COW strings like c_str () that are returned by some_string and foo point to the same memory, but if it is not COW, then s will be invalid after the std :: string returned by foo destroyed.

From the example there you should look in that direction.

+4
source

Source: https://habr.com/ru/post/1247790/


All Articles