diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp index aabca72daabe..f00e3b2536b7 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; @@ -113,47 +113,63 @@ void AnimatedPropsRegistry::initializeSurface(SurfaceId surfaceId) { surfaceContexts_.try_emplace(surfaceId); } -std::pair< - std::unordered_set>&, - SnapshotMap&> +std::pair, std::shared_ptr> AnimatedPropsRegistry::getMap(SurfaceId surfaceId) { auto lock = std::lock_guard(mutex_); auto& [pendingMap, map, pendingFamilies, families] = surfaceContexts_[surfaceId]; - for (auto& family : pendingFamilies) { - families.insert(family); + if (!pendingFamilies.empty()) { + auto nextFamilies = families ? std::make_shared(*families) + : std::make_shared(); + nextFamilies->merge(pendingFamilies); + pendingFamilies.clear(); + families = std::move(nextFamilies); } - for (auto& [tag, propsSnapshot] : pendingMap) { - auto currentIt = map.find(tag); - if (currentIt == map.end()) { - map.insert_or_assign(tag, std::move(propsSnapshot)); - } else { - auto& currentSnapshot = currentIt->second; - if (propsSnapshot->rawProps) { + if (!pendingMap.empty()) { + auto nextMap = map ? std::make_shared(*map) + : std::make_shared(); + for (auto& [tag, propsSnapshot] : pendingMap) { + auto currentIt = nextMap->find(tag); + if (currentIt == nextMap->end()) { + nextMap->insert_or_assign(tag, std::move(propsSnapshot)); + } else { + const auto& currentSnapshot = currentIt->second; + auto merged = std::make_shared(); if (currentSnapshot->rawProps) { - mergeAnimatedRawProps( - *currentSnapshot->rawProps, *propsSnapshot->rawProps); + merged->rawProps = + std::make_unique(*currentSnapshot->rawProps); + } + if (propsSnapshot->rawProps) { + if (merged->rawProps) { + mergeAnimatedRawProps(*merged->rawProps, *propsSnapshot->rawProps); + } else { + merged->rawProps = std::move(propsSnapshot->rawProps); + } + } + if (propsSnapshot->text) { + merged->text = std::move(propsSnapshot->text); } else { - currentSnapshot->rawProps = std::move(propsSnapshot->rawProps); + merged->text = currentSnapshot->text; } - } - if (propsSnapshot->text) { - currentSnapshot->text = std::move(propsSnapshot->text); - } - if (!currentSnapshot->props) { - currentSnapshot->props = std::move(propsSnapshot->props); - currentSnapshot->propNames = std::move(propsSnapshot->propNames); - } else { - for (auto& propName : propsSnapshot->propNames) { - currentSnapshot->propNames.insert(propName); - updateProp(propName, *currentSnapshot->props, *propsSnapshot); + if (!currentSnapshot->props) { + 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) { + merged->propNames.insert(propName); + updateProp(propName, *merged->props, *propsSnapshot); + } } + currentIt->second = std::move(merged); } } + pendingMap.clear(); + map = std::move(nextMap); } - pendingMap.clear(); - pendingFamilies.clear(); return {families, map}; } @@ -163,8 +179,8 @@ void AnimatedPropsRegistry::clear(SurfaceId surfaceId) { if (auto it = surfaceContexts_.find(surfaceId); it != surfaceContexts_.end()) { auto& surfaceContext = it->second; - surfaceContext.families.clear(); - surfaceContext.map.clear(); + surfaceContext.families.reset(); + surfaceContext.map.reset(); } } diff --git a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h index e2ea6c082dcb..d2e124046cd8 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h @@ -27,13 +27,17 @@ struct PropsSnapshot { std::optional text; }; +using FamilySet = std::unordered_set>; +using PendingSnapshotMap = std::unordered_map>; +using SnapshotMap = std::unordered_map>; + struct SurfaceContext { - std::unordered_map> pendingMap, map; - std::unordered_set> pendingFamilies, families; + PendingSnapshotMap pendingMap; + std::shared_ptr map; + FamilySet pendingFamilies; + std::shared_ptr 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 +57,7 @@ class AnimatedPropsRegistry { void initializeSurface(SurfaceId surfaceId); void clear(SurfaceId surfaceId); void clearOnSurfaceStop(SurfaceId surfaceId); - std::pair> &, SnapshotMap &> getMap(SurfaceId surfaceId); + std::pair, std::shared_ptr> 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..f60c9a997d89 100644 --- a/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp +++ b/packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp @@ -29,13 +29,12 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit( return newRootShadowNode; } - const auto& res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId()); - auto& surfaceFamilies = res.first; - auto& updates = res.second; - - if (surfaceFamilies.empty()) { + const auto res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId()); + if (!res.first || res.first->empty() || !res.second) { return newRootShadowNode; } + const auto& surfaceFamilies = *res.first; + const auto& updates = *res.second; auto clonedRootShadowNode = std::static_pointer_cast(newRootShadowNode->cloneMultiple( surfaceFamilies, @@ -48,7 +47,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..eb19bd37c008 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,13 @@ 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() { + auto map = registry.getMap(1).second; + EXPECT_NE(map, nullptr); + lastSnapshot = map->at(10); + return *lastSnapshot; } }; @@ -216,7 +223,8 @@ TEST_F(AnimatedPropsRegistryTest, stoppedSurfacesIgnoreUpdates) { registry.clearOnSurfaceStop(1); updateRaw(folly::dynamic::object("opacity", 0.5)); registry.initializeSurface(1); - EXPECT_TRUE(registry.getMap(1).second.empty()); + auto map = registry.getMap(1).second; + EXPECT_TRUE(!map || map->empty()); } TEST_F(AnimatedPropsRegistryTest, keepsLatestText) { @@ -269,4 +277,87 @@ 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); + if (!families || !map) { + continue; + } + 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