Skip to content

Commit 5478ce2

Browse files
authored
Fix crash when building with _GLIBCXX_USE_CXX11_ABI=0 (#686)
**Issue:** A user had been building aws-crt-cpp with `CXXFLAGS=-D_GLIBCXX_USE_CXX11_ABI=0`, and that worked fine until the recent change where we started hiding symbols by default (PR #666) With symbols hidden by default, and `_GLIBCXX_USE_CXX11_ABI=0`, tests would crash in the destructor of `Aws::Crt::String` (which is `std::string` with a custom allocator). **Description of changes:** Don't hide symbols when building for the ancient glibcxx ABI. I don't 100% understand why this fixes the issue, but the situation is esoteric enough that it doesn't seem worth spending more days on this problem. **Background** See: https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html GCC had to introduce a new ABI for C++11 which removed the copy-on-write optimization for strings, which is forbidden in C++11 and later (new ABI uses a small-string optimization instead). But GCC let users manually set `_GLIBCXX_USE_CXX11_ABI=0` so they could opt back into the old ABI and continue working with libraries compiled for C++03. I don't think GCC intended devs to continue using this 10 years later, but some people are, because the old copy-on-write optimization has less memory usage.
1 parent 9ffe4ef commit 5478ce2

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

.github/workflows/ci.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ env:
2121
concurrency:
2222
group: ${{ github.workflow }}-${{ github.ref }}
2323
cancel-in-progress: true
24-
24+
2525
jobs:
2626
linux-compat-use-openssl:
2727
runs-on: ubuntu-22.04 # latest
@@ -143,6 +143,14 @@ jobs:
143143
aws s3 cp s3://aws-crt-test-stuff/ci/${{ env.BUILDER_VERSION }}/linux-container-ci.sh ./linux-container-ci.sh && chmod a+x ./linux-container-ci.sh
144144
./linux-container-ci.sh ${{ env.BUILDER_VERSION }} aws-crt-${{ env.LINUX_BASE_IMAGE }} build -p ${{ env.PACKAGE_NAME }} --compiler=${{ matrix.compiler }} --cmake-extra=-DBUILD_SHARED_LIBS=ON
145145
146+
linux-glibcxx-ancient-abi:
147+
runs-on: ubuntu-24.04 # latest
148+
steps:
149+
- name: Build ${{ env.PACKAGE_NAME }}
150+
run: |
151+
aws s3 cp s3://aws-crt-test-stuff/ci/${{ env.BUILDER_VERSION }}/linux-container-ci.sh ./linux-container-ci.sh && chmod a+x ./linux-container-ci.sh
152+
./linux-container-ci.sh ${{ env.BUILDER_VERSION }} aws-crt-${{ env.LINUX_BASE_IMAGE }} build -p ${{ env.PACKAGE_NAME }} --compiler=gcc-11 --cmake-extra=-DBUILD_SHARED_LIBS=ON --cmake-extra=-DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI=0
153+
146154
linux-openssl-static:
147155
runs-on: ubuntu-22.04 # latest
148156
steps:

CMakeLists.txt

+13-3
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,19 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)
298298
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD ${CMAKE_CXX_STANDARD})
299299

300300
# Hide symbols by default
301-
# Except for ancient GCC, because it leads to crashes in shared-lib builds
302-
# see: https://github.com/awslabs/aws-crt-cpp/pull/675
303-
if(NOT (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0"))
301+
# Except where it causes problems, and the situation is weird enough that it's not worth investigating further.
302+
#
303+
# We've seen people set _GLIBCXX_USE_CXX11_ABI=0 which forces GCC to use it's pre-C++11 string implementation,
304+
# which leads to crashes on shared-lib builds. Search every variant of CXX_FLAGS to see if it's set.
305+
string(FIND "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CMAKE_CXX_FLAGS_MINSIZEREL}"
306+
"-D_GLIBCXX_USE_CXX11_ABI=0" found_ancient_abi_flag)
307+
if(found_ancient_abi_flag GREATER -1)
308+
message(WARNING "_GLIBCXX_USE_CXX11_ABI=0 is set. Making all symbols visible to prevent weird crashes")
309+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0")
310+
# Ancient GCC leads to crashes in shared-lib builds
311+
# see: https://github.com/awslabs/aws-crt-cpp/pull/675
312+
message(WARNING "Ancient compiler (${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}). Making all symbols visible to prevent weird crashes")
313+
else()
304314
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON)
305315
endif()
306316

0 commit comments

Comments
 (0)