[Build] Fix ABI ODR violation for C++20 consumers linking against C++…#2547
[Build] Fix ABI ODR violation for C++20 consumers linking against C++…#2547divitsinghall wants to merge 1 commit intofacebook:mainfrom
Conversation
|
Hi @divitsinghall! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
I have signed the CLA an hour ago |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
cc @yfeldblum — I’m taking a pass at fixing the C++20 ABI regression/ODR violation reported in #2477. The issue stems from standard-dependent layout changes in structures using [[no_unique_address]]. I've implemented a detection layer in folly/CppAttributes.h and Portability.h that ensures header-binary consistency when consumers use a different C++ standard than the pre-compiled library. Full reproduction details and the behavioral matrix are included in the description |
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.