[Build] Fix ABI ODR violation for C++20 consumers linking against C++… #2547
+133
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix ABI ODR violation when linking C++20 consumer against C++17 Folly
Summary
Fixes #2477 - Memory corruption / double-free crashes when a C++20 application links against a Folly library compiled with C++17.
Root Cause
The
[[no_unique_address]]C++20 attribute changes struct memory layout by allowing empty class members to occupy zero bytes. Folly uses this attribute via theFOLLY_ATTR_NO_UNIQUE_ADDRESSmacro in several internal structures.The Problem: When Folly is compiled as C++17, the attribute is not applied, and empty members occupy ≥1 byte. When the same headers are included from a C++20 application, the attribute IS applied, creating a smaller layout. This ODR (One Definition Rule) violation causes memory corruption at runtime.
Affected Structures
folly/container/span.h:126fallback_span::spanextent_folly/detail/tuple.h:126lite_tuple::entryentry_valuefolly/executors/SerialExecutor-inl.h:265SerialExecutorMPSCQueuemutex_folly/lang/Exception.cpp:316scope_guard_func_folly/coro/AutoCleanup.h:105AutoCleanupscheduled_folly/python/ProactorExecutor.h:61overlapped_Layout Difference Example
Reproduction
Compiler Flags
Minimal Reproduction
The Fix
1. Record Library ABI at Build Time
CMake/FollyConfigChecks.cmake- Detect if[[no_unique_address]]works:CMake/folly-config.h.cmake- Export to installed headers:2. Force ABI Compatibility in Headers
folly/CppAttributes.h- Conditional attribute application:Evidence of Consistent Layout
Before Fix (ODR Violation)
After Fix (ABI Compatible)
Verification Test
Behavioral Changes
New Macros
FOLLY_LIBRARY_CXX_STANDARDFOLLY_LIBRARY_HAS_NO_UNIQUE_ADDRESS[[no_unique_address]]was active at library buildFOLLY_SKIP_ABI_CHECKTesting
sizeof()checksrepro_issue_2477/reproduce_abi_issue.shFiles Changed
CMake/folly-config.h.cmake- Export library ABI infoCMake/FollyConfigChecks.cmake- Detect[[no_unique_address]]supportfolly/CppAttributes.h- ABI-aware attribute macrofolly/Portability.h- C++ version mismatch warningrepro_issue_2477/repro.cpp- Documented reproduction caserepro_issue_2477/reproduce_abi_issue.sh- Automated reproduction scriptRelated Issues
Notes for Reviewers
This fix prioritizes ABI stability over performance when there's a version mismatch. The
[[no_unique_address]]optimization is disabled to match the pre-compiled library.Users who want optimal performance should compile Folly with the same C++ standard as their application.
The warning can be suppressed with
-DFOLLY_SKIP_ABI_CHECKfor users who understand the risks.