From 0a398f006019bfa045ff0f679e31e23e280751e4 Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Thu, 3 Dec 2026 15:11:10 -0800 Subject: [PATCH 1/2] Fix AnimatedPropsRegistry use-after-free during concurrent commits (#60519) Summary: `AnimatedPropsRegistry::getMap` previously acquired `mutex_`, merged pending updates into the surface's `families` and `map`, released `mutex_`, and returned raw references to those internal containers. `AnimationBackendCommitHook::shadowTreeWillCommit` then iterated `families` and read `PropsSnapshot` entries outside the lock while concurrent `ShadowTree` commits, `clear()`, or `clearOnSurfaceStop()` mutated, rehashed, or freed them, causing heap-use-after-free and heap corruption. Store committed `PropsSnapshot` entries as immutable `std::shared_ptr` (creating a new merged snapshot when updating an already-committed entry) and return `{families, map}` by value from `getMap`. Callers iterate their own snapshot without holding a lock or racing with concurrent `getMap()`, `clear()`, or `clearOnSurfaceStop()`. Changelog: [General][Fixed] - Fix heap-use-after-free race in `AnimatedPropsRegistry` during concurrent commits Signed-off-by: Peter Abbondanzo Differential Revision: D136029530 --- .../AnimatedPropsRegistry.cpp | 37 +++--- .../animationbackend/AnimatedPropsRegistry.h | 9 +- .../AnimationBackendCommitHook.cpp | 8 +- .../tests/AnimatedPropsRegistryTest.cpp | 89 ++++++++++++- .../tests/AnimationBackendTest.cpp | 121 ++++++++++++++++++ 5 files changed, 239 insertions(+), 25 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp index aabca72daabe..244d81419c5e 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp @@ -75,7 +75,7 @@ void AnimatedPropsRegistry::update( const auto& animatedProps = mutation.props; auto it = pendingMap.find(tag); if (it == pendingMap.end()) { - it = pendingMap.insert_or_assign(tag, std::make_unique()) + it = pendingMap.insert_or_assign(tag, std::make_shared()) .first; } auto& snapshot = it->second; @@ -114,8 +114,8 @@ void AnimatedPropsRegistry::initializeSurface(SurfaceId surfaceId) { } std::pair< - std::unordered_set>&, - SnapshotMap&> + std::unordered_set>, + SnapshotMap> AnimatedPropsRegistry::getMap(SurfaceId surfaceId) { auto lock = std::lock_guard(mutex_); auto& [pendingMap, map, pendingFamilies, families] = @@ -129,27 +129,34 @@ AnimatedPropsRegistry::getMap(SurfaceId surfaceId) { if (currentIt == map.end()) { map.insert_or_assign(tag, std::move(propsSnapshot)); } else { - auto& currentSnapshot = currentIt->second; + const auto& currentSnapshot = currentIt->second; + auto merged = std::make_shared(); + if (currentSnapshot->rawProps) { + merged->rawProps = + std::make_unique(*currentSnapshot->rawProps); + } if (propsSnapshot->rawProps) { - if (currentSnapshot->rawProps) { - mergeAnimatedRawProps( - *currentSnapshot->rawProps, *propsSnapshot->rawProps); + if (merged->rawProps) { + mergeAnimatedRawProps(*merged->rawProps, *propsSnapshot->rawProps); } else { - currentSnapshot->rawProps = std::move(propsSnapshot->rawProps); + merged->rawProps = std::move(propsSnapshot->rawProps); } } - if (propsSnapshot->text) { - currentSnapshot->text = std::move(propsSnapshot->text); - } + merged->text = propsSnapshot->text ? std::move(propsSnapshot->text) + : currentSnapshot->text; if (!currentSnapshot->props) { - currentSnapshot->props = std::move(propsSnapshot->props); - currentSnapshot->propNames = std::move(propsSnapshot->propNames); + merged->props = std::move(propsSnapshot->props); + merged->propNames = std::move(propsSnapshot->propNames); } else { + merged->props = + std::make_unique(*currentSnapshot->props); + merged->propNames = currentSnapshot->propNames; for (auto& propName : propsSnapshot->propNames) { - currentSnapshot->propNames.insert(propName); - updateProp(propName, *currentSnapshot->props, *propsSnapshot); + merged->propNames.insert(propName); + updateProp(propName, *merged->props, *propsSnapshot); } } + currentIt->second = std::move(merged); } } pendingMap.clear(); diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h index e2ea6c082dcb..88f81fdfcf51 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h @@ -27,13 +27,14 @@ struct PropsSnapshot { std::optional text; }; +using SnapshotMap = std::unordered_map>; + struct SurfaceContext { - std::unordered_map> pendingMap, map; + std::unordered_map> pendingMap; + SnapshotMap map; std::unordered_set> pendingFamilies, families; }; -using SnapshotMap = std::unordered_map>; - /* * Merges raw animated props per key, like the props merged by the animation * drivers. A null value is kept, so it still resets the prop on commit. @@ -53,7 +54,7 @@ class AnimatedPropsRegistry { void initializeSurface(SurfaceId surfaceId); void clear(SurfaceId surfaceId); void clearOnSurfaceStop(SurfaceId surfaceId); - std::pair> &, SnapshotMap &> getMap(SurfaceId surfaceId); + std::pair>, SnapshotMap> getMap(SurfaceId surfaceId); private: std::unordered_map surfaceContexts_; diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp index 5142e60e6d30..1d88b4f5403e 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp @@ -29,9 +29,9 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit( return newRootShadowNode; } - const auto& res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId()); - auto& surfaceFamilies = res.first; - auto& updates = res.second; + const auto res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId()); + const auto& surfaceFamilies = res.first; + const auto& updates = res.second; if (surfaceFamilies.empty()) { return newRootShadowNode; @@ -48,7 +48,7 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit( if (auto updatesIter = updates.find(shadowNode.getTag()); updatesIter != updates.end() && surfaceFamilies.contains(shadowNode.getFamilyShared())) { - auto& snapshot = updatesIter->second; + const auto& snapshot = updatesIter->second; if (!snapshot->propNames.empty() || snapshot->rawProps) { PropsParserContext propsParserContext{ shadowNode.getSurfaceId(), diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimatedPropsRegistryTest.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimatedPropsRegistryTest.cpp index 85b8f4ceaddb..9060d7b9b548 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimatedPropsRegistryTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimatedPropsRegistryTest.cpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include namespace facebook::react { @@ -52,8 +54,11 @@ class AnimatedPropsRegistryTest : public ::testing::Test { EXPECT_EQ(*snapshot().rawProps, expected); } - PropsSnapshot& snapshot() { - return *registry.getMap(1).second.at(10); + std::shared_ptr lastSnapshot; + + const PropsSnapshot& snapshot() { + lastSnapshot = registry.getMap(1).second.at(10); + return *lastSnapshot; } }; @@ -269,4 +274,84 @@ TEST_F(AnimatedPropsRegistryTest, childrenWithTextKeepsNodeWithoutRawText) { EXPECT_EQ(childrenWithText(*textNode, {}, "new"), nullptr); } +TEST_F(AnimatedPropsRegistryTest, concurrentGetMapClearAndSurfaceStop) { + constexpr int kNumViews = 16; + constexpr int kIterations = 200; + + std::vector> views; + views.reserve(kNumViews); + for (int i = 0; i < kNumViews; ++i) { + views.push_back( + builder.build(Element().surfaceId(1).tag(100 + i))); + } + + std::atomic start{false}; + std::atomic done{false}; + + std::thread writer([&]() { + while (!start.load(std::memory_order_acquire)) { + } + for (int iter = 0; iter < kIterations; ++iter) { + std::vector batches(1); + for (int i = 0; i < kNumViews; ++i) { + AnimatedPropsBuilder propsBuilder; + propsBuilder.setOpacity(static_cast((iter + i) % 100) / 100.0f); + propsBuilder.setText(std::to_string(iter + i)); + folly::dynamic raw = folly::dynamic::object("zIndex", iter + i); + propsBuilder.storeDynamic(raw); + batches[0].batch.push_back( + AnimationMutation{ + .tag = views[i]->getTag(), + .family = views[i]->getFamilyShared(), + .props = propsBuilder.get()}); + } + registry.update(batches); + } + done.store(true, std::memory_order_release); + }); + + std::thread reader([&]() { + while (!start.load(std::memory_order_acquire)) { + } + while (!done.load(std::memory_order_acquire)) { + const auto [families, map] = registry.getMap(1); + for (const auto& family : families) { + if (auto it = map.find(family->getTag()); it != map.end()) { + const auto& snapshot = it->second; + BaseViewProps viewProps; + for (const auto& propName : snapshot->propNames) { + updateProp(propName, viewProps, *snapshot); + } + if (snapshot->rawProps) { + EXPECT_TRUE(snapshot->rawProps->isObject()); + } + if (snapshot->text) { + EXPECT_FALSE(snapshot->text->empty()); + } + } + } + } + }); + + std::thread clearer([&]() { + while (!start.load(std::memory_order_acquire)) { + } + int iter = 0; + while (!done.load(std::memory_order_acquire)) { + if (iter % 10 == 0) { + registry.clearOnSurfaceStop(1); + registry.initializeSurface(1); + } else { + registry.clear(1); + } + ++iter; + } + }); + + start.store(true, std::memory_order_release); + writer.join(); + reader.join(); + clearer.join(); +} + } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp index 91eb21f3302d..91cab6bfb06e 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp @@ -8,13 +8,18 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include #include +#include +#include namespace facebook::react { @@ -138,4 +143,120 @@ TEST_F(AnimationBackendTest, textOnlyMutationIsNotWrittenDirectly) { EXPECT_TRUE(delegate.synchronousUpdates.empty()); } +TEST_F( + AnimationBackendTest, + concurrentCommitsAndRegistryClearDoNotCorruptHeap) { + constexpr SurfaceId kSurfaceId = 1; + constexpr int kNumViews = 32; + constexpr int kIterations = 200; + + std::vector> views(kNumViews); + std::vector childElements; + childElements.reserve(kNumViews); + for (int i = 0; i < kNumViews; ++i) { + childElements.push_back( + Element() + .surfaceId(kSurfaceId) + .tag(100 + i) + .reference(views[i])); + } + + std::shared_ptr initialRoot; + builder.build( + Element() + .surfaceId(kSurfaceId) + .tag(1) + .reference(initialRoot) + .children(std::move(childElements))); + + auto localUiManager = std::make_shared( + [](std::function&& /*callback*/) {}, + contextContainer); + RecordingUIManagerDelegate localDelegate; + localUiManager->setDelegate(&localDelegate); + + auto shadowTree = std::make_unique( + kSurfaceId, + LayoutConstraints{}, + LayoutContext{}, + *localUiManager, + *contextContainer); + auto* shadowTreePtr = shadowTree.get(); + + shadowTree->commit( + [&initialRoot](const RootShadowNode& /*oldRootShadowNode*/) { + return initialRoot; + }, + {.mountSynchronously = true}); + + localUiManager->startEmptySurface(std::move(shadowTree)); + auto localBackend = std::make_shared( + std::make_shared(), localUiManager); + localUiManager->unstable_setAnimationBackend(localBackend); + + std::atomic start{false}; + + std::thread mainThread([&]() { + while (!start.load(std::memory_order_acquire)) { + } + for (int iter = 0; iter < kIterations; ++iter) { + localBackend->pushAnimationMutations( + [&, iter](AnimationTimestamp /*ts*/) { + AnimationMutations mutations; + for (int i = 0; i < kNumViews; ++i) { + AnimatedPropsBuilder propsBuilder; + propsBuilder.setOpacity( + static_cast((iter + i) % 100) / 100.0f); + folly::dynamic raw = folly::dynamic::object( + "opacity", (iter + i) % 100 / 100.0)("zIndex", iter + i); + propsBuilder.storeDynamic(raw); + mutations.batch.push_back( + AnimationMutation{ + .tag = views[i]->getTag(), + .family = views[i]->getFamilyShared(), + .props = propsBuilder.get(), + .hasLayoutUpdates = false}); + } + return mutations; + }); + + shadowTreePtr->commit( + [](const RootShadowNode& oldRootShadowNode) { + return std::make_shared( + oldRootShadowNode, ShadowNodeFragment{}); + }, + { + .mountSynchronously = true, + .source = ShadowTreeCommitSource::ReactRevisionMerge, + }); + } + }); + + std::thread jsThread([&]() { + while (!start.load(std::memory_order_acquire)) { + } + for (int iter = 0; iter < kIterations * 2; ++iter) { + shadowTreePtr->commit( + [](const RootShadowNode& oldRootShadowNode) { + return std::make_shared( + oldRootShadowNode, ShadowNodeFragment{}); + }, + { + .mountSynchronously = false, + .source = (iter % 2 == 0) + ? ShadowTreeCommitSource::React + : ShadowTreeCommitSource::AnimationEndSync, + }); + localBackend->clearRegistry(kSurfaceId); + } + }); + + start.store(true, std::memory_order_release); + mainThread.join(); + jsThread.join(); + + localUiManager->stopSurface(kSurfaceId); + localUiManager->setDelegate(nullptr); +} + } // namespace facebook::react From 4b6ec1a105d6f48438eb9875f7b39f6395c1560a Mon Sep 17 00:00:00 2001 From: Peter Abbondanzo Date: Thu, 3 Dec 2026 15:11:10 -0800 Subject: [PATCH 2/2] Unregister AnimationBackendCommitHook in ~AnimationBackend Summary: `AnimationBackend` registers `commitHook_` with `UIManager` in its constructor, but `~AnimationBackend()` previously did not unregister it. If `AnimationBackend` is destroyed while `UIManager` is still alive, `UIManager::commitHooks_` retains a dangling pointer that crashes on the next `ShadowTree` commit. Unregister `commitHook_` from `UIManager` in `~AnimationBackend()` when `uiManager_` is still alive. Changelog: [General][Fixed] - Unregister `AnimationBackendCommitHook` when `AnimationBackend` is destroyed Signed-off-by: Peter Abbondanzo Differential Revision: D136090145 --- .../animationbackend/AnimationBackend.cpp | 6 ++- .../tests/AnimationBackendTest.cpp | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp index 7b17e13c25ce..4e461d226637 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp @@ -107,7 +107,11 @@ AnimationBackend::AnimationBackend( } } -AnimationBackend::~AnimationBackend() = default; +AnimationBackend::~AnimationBackend() { + if (auto uiManager = uiManager_.lock()) { + uiManager->unregisterCommitHook(*commitHook_); + } +} void AnimationBackend::unpackMutations( AnimationMutations& mutations, diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp index 91cab6bfb06e..38677ebb873e 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/tests/AnimationBackendTest.cpp @@ -259,4 +259,44 @@ TEST_F( localUiManager->setDelegate(nullptr); } +TEST_F(AnimationBackendTest, destroyingBackendUnregistersCommitHook) { + constexpr SurfaceId kSurfaceId = 2; + std::shared_ptr initialRoot; + builder.build( + Element() + .surfaceId(kSurfaceId) + .tag(1) + .reference(initialRoot)); + + auto localUiManager = std::make_shared( + [](std::function&& /*callback*/) {}, + contextContainer); + RecordingUIManagerDelegate localDelegate; + localUiManager->setDelegate(&localDelegate); + + auto shadowTree = std::make_unique( + kSurfaceId, + LayoutConstraints{}, + LayoutContext{}, + *localUiManager, + *contextContainer); + auto* shadowTreePtr = shadowTree.get(); + localUiManager->startEmptySurface(std::move(shadowTree)); + + { + auto temporaryBackend = std::make_shared( + std::make_shared(), localUiManager); + } + + auto status = shadowTreePtr->commit( + [&initialRoot](const RootShadowNode& /*oldRootShadowNode*/) { + return initialRoot; + }, + {.source = ShadowTreeCommitSource::React}); + EXPECT_EQ(status, ShadowTree::CommitStatus::Succeeded); + + localUiManager->stopSurface(kSurfaceId); + localUiManager->setDelegate(nullptr); +} + } // namespace facebook::react