Fix undefined behavior in LayoutAnimation sort comparator (#56127)#56127
Closed
Abbondanzo wants to merge 1 commit intofacebook:mainfrom
Closed
Fix undefined behavior in LayoutAnimation sort comparator (#56127)#56127Abbondanzo wants to merge 1 commit intofacebook:mainfrom
Abbondanzo wants to merge 1 commit intofacebook:mainfrom
Conversation
|
@Abbondanzo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D95909371. |
c4aaf74 to
b2446db
Compare
Abbondanzo
added a commit
to Abbondanzo/react-native
that referenced
this pull request
Mar 19, 2026
…6127) Summary: `shouldFirstComeBeforeSecondMutation` in `utils.h` uses `return &lhs < &rhs` as a fallback when mutation types don't determine ordering. That's undefined behavior per C++ standard [expr.rel]/4, comparing pointers from different allocations (which happens during `std::stable_sort`'s merge phase). This wasn't a problem until the LLVM 19.x NDK upgrade changed libc++'s sort implementation. The new insertion sort phase is more aggressive about exploiting comparator correctness, and the UB now manifests as SIGSEGV in the insertion sort phase on Android. The fix replaces the pointer comparison with a deterministic fallback using `parentTag`, `newChildShadowView.tag`, and `oldChildShadowView.tag`. When all properties are equal, returns `false` to maintain stable ordering. Also adds `MutationComparatorTest.cpp` with tests covering: - Strict weak ordering properties (irreflexivity, asymmetry, transitivity) - Type-based ordering rules (deletes last, removes before inserts, etc.) - Deterministic fallback by parentTag, newChildTag, oldChildTag - Equal mutations return false (stability) - Stress test: `std::stable_sort` on large mutation lists with duplicates Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D95909371
Abbondanzo
added a commit
to Abbondanzo/react-native
that referenced
this pull request
Mar 19, 2026
…6127) Summary: Pull Request resolved: facebook#56127 `shouldFirstComeBeforeSecondMutation` in `utils.h` uses `return &lhs < &rhs` as a fallback when mutation types don't determine ordering. That's undefined behavior per C++ standard [expr.rel]/4, comparing pointers from different allocations (which happens during `std::stable_sort`'s merge phase). This wasn't a problem until the LLVM 19.x NDK upgrade changed libc++'s sort implementation. The new insertion sort phase is more aggressive about exploiting comparator correctness, and the UB now manifests as SIGSEGV in the insertion sort phase on Android. The fix replaces the pointer comparison with a deterministic fallback using `parentTag`, `newChildShadowView.tag`, and `oldChildShadowView.tag`. When all properties are equal, returns `false` to maintain stable ordering. Also adds `MutationComparatorTest.cpp` with tests covering: - Strict weak ordering properties (irreflexivity, asymmetry, transitivity) - Type-based ordering rules (deletes last, removes before inserts, etc.) - Deterministic fallback by parentTag, newChildTag, oldChildTag - Equal mutations return false (stability) - Stress test: `std::stable_sort` on large mutation lists with duplicates Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D95909371
b2446db to
230b85a
Compare
Abbondanzo
added a commit
to Abbondanzo/react-native
that referenced
this pull request
Mar 19, 2026
…6127) Summary: `shouldFirstComeBeforeSecondMutation` in `utils.h` uses `return &lhs < &rhs` as a fallback when mutation types don't determine ordering. That's undefined behavior per C++ standard [expr.rel]/4, comparing pointers from different allocations (which happens during `std::stable_sort`'s merge phase). This wasn't a problem until the LLVM 19.x NDK upgrade changed libc++'s sort implementation. The new insertion sort phase is more aggressive about exploiting comparator correctness, and the UB now manifests as SIGSEGV in the insertion sort phase on Android. The fix replaces the pointer comparison with a deterministic fallback using `parentTag`, `newChildShadowView.tag`, and `oldChildShadowView.tag`. When all properties are equal, returns `false` to maintain stable ordering. Also adds `MutationComparatorTest.cpp` with tests covering: - Strict weak ordering properties (irreflexivity, asymmetry, transitivity) - Type-based ordering rules (deletes last, removes before inserts, etc.) - Deterministic fallback by parentTag, newChildTag, oldChildTag - Equal mutations return false (stability) - Stress test: `std::stable_sort` on large mutation lists with duplicates Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D95909371
230b85a to
ed7050b
Compare
…6127) Summary: Pull Request resolved: facebook#56127 `shouldFirstComeBeforeSecondMutation` in `utils.h` uses `return &lhs < &rhs` as a fallback when mutation types don't determine ordering. That's undefined behavior per C++ standard [expr.rel]/4, comparing pointers from different allocations (which happens during `std::stable_sort`'s merge phase). This wasn't a problem until the LLVM 19.x NDK upgrade changed libc++'s sort implementation. The new insertion sort phase is more aggressive about exploiting comparator correctness, and the UB now manifests as SIGSEGV in the insertion sort phase on Android. The fix replaces the pointer comparison with a deterministic fallback using `parentTag`, `newChildShadowView.tag`, and `oldChildShadowView.tag`. When all properties are equal, returns `false` to maintain stable ordering. Also adds `MutationComparatorTest.cpp` with tests covering: - Strict weak ordering properties (irreflexivity, asymmetry, transitivity) - Type-based ordering rules (deletes last, removes before inserts, etc.) - Deterministic fallback by parentTag, newChildTag, oldChildTag - Equal mutations return false (stability) - Stress test: `std::stable_sort` on large mutation lists with duplicates Changelog: [Internal] Reviewed By: christophpurrer Differential Revision: D95909371
ed7050b to
c316de1
Compare
|
This pull request has been merged in 1fdd167. |
Collaborator
|
This pull request was successfully merged by @Abbondanzo in 1fdd167 When will my fix make it into a release? | How to file a pick request? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
shouldFirstComeBeforeSecondMutationinutils.husesreturn &lhs < &rhsas a fallback when mutation types don't determine ordering. That's undefined behavior per C++ standard [expr.rel]/4, comparing pointers from different allocations (which happens duringstd::stable_sort's merge phase).This wasn't a problem until the LLVM 19.x NDK upgrade changed libc++'s sort implementation. The new insertion sort phase is more aggressive about exploiting comparator correctness, and the UB now manifests as SIGSEGV in the insertion sort phase on Android.
The fix replaces the pointer comparison with a deterministic fallback using
parentTag,newChildShadowView.tag, andoldChildShadowView.tag. When all properties are equal, returnsfalseto maintain stable ordering.Also adds
MutationComparatorTest.cppwith tests covering:std::stable_sorton large mutation lists with duplicatesChangelog: [Internal]
Reviewed By: christophpurrer
Differential Revision: D95909371