Skip to content

Commit 2e628ef

Browse files
Abbondanzofacebook-github-bot
authored andcommitted
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 any lock while concurrent `ShadowTree` commits, `clear()`, or `clearOnSurfaceStop()` mutated, rehashed, or freed them, causing heap-use-after-free and heap corruption. Replace `getMap` with a callback-based `withMap` API that holds a per-surface `mapMutex` across the merge and commit-hook traversal while keeping `SurfaceContext` alive via `std::shared_ptr`. `update()` only acquires `mutex_` to write pending updates and `withMap()` swaps them out in O(1) under `mapMutex` -> `mutex_` (creating the `SurfaceContext` on miss as `getMap()` did for surfaces started via `startEmptySurface`), avoiding blocking animation frame updates on `cloneMultiple`. Also unregister `commitHook_` in `~AnimationBackend()` when `UIManager` is still alive and guard `BaseViewProps` downcasts with `std::dynamic_pointer_cast`. Changelog: [General][Fixed] - Fix heap-use-after-free race in `AnimatedPropsRegistry` during concurrent commits Signed-off-by: Peter Abbondanzo <abbo@meta.com> Differential Revision: D136029530
1 parent 9d5467a commit 2e628ef

6 files changed

Lines changed: 457 additions & 121 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.cpp‎

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void AnimatedPropsRegistry::update(
6868
if (contextIt == surfaceContexts_.end()) {
6969
continue;
7070
}
71-
auto& surfaceContext = contextIt->second;
71+
auto& surfaceContext = *contextIt->second;
7272
auto& pendingMap = surfaceContext.pendingMap;
7373
surfaceContext.pendingFamilies.insert(family);
7474
const auto tag = mutation.tag;
@@ -110,16 +110,38 @@ void AnimatedPropsRegistry::update(
110110

111111
void AnimatedPropsRegistry::initializeSurface(SurfaceId surfaceId) {
112112
auto lock = std::lock_guard(mutex_);
113-
surfaceContexts_.try_emplace(surfaceId);
113+
if (surfaceContexts_.find(surfaceId) == surfaceContexts_.end()) {
114+
surfaceContexts_.emplace(surfaceId, std::make_shared<SurfaceContext>());
115+
}
114116
}
115117

116-
std::pair<
117-
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>>&,
118-
SnapshotMap&>
119-
AnimatedPropsRegistry::getMap(SurfaceId surfaceId) {
120-
auto lock = std::lock_guard(mutex_);
121-
auto& [pendingMap, map, pendingFamilies, families] =
122-
surfaceContexts_[surfaceId];
118+
void AnimatedPropsRegistry::withMap(
119+
SurfaceId surfaceId,
120+
const SurfaceMapCallback& callback) {
121+
std::shared_ptr<SurfaceContext> surfaceContext;
122+
{
123+
auto lock = std::lock_guard(mutex_);
124+
auto it = surfaceContexts_.find(surfaceId);
125+
if (it == surfaceContexts_.end()) {
126+
it = surfaceContexts_
127+
.emplace(surfaceId, std::make_shared<SurfaceContext>())
128+
.first;
129+
}
130+
surfaceContext = it->second;
131+
}
132+
133+
auto mapLock = std::lock_guard(surfaceContext->mapMutex);
134+
135+
SnapshotMap pendingMap;
136+
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> pendingFamilies;
137+
{
138+
auto lock = std::lock_guard(mutex_);
139+
pendingMap.swap(surfaceContext->pendingMap);
140+
pendingFamilies.swap(surfaceContext->pendingFamilies);
141+
}
142+
143+
auto& families = surfaceContext->families;
144+
auto& map = surfaceContext->map;
123145

124146
for (auto& family : pendingFamilies) {
125147
families.insert(family);
@@ -152,25 +174,40 @@ AnimatedPropsRegistry::getMap(SurfaceId surfaceId) {
152174
}
153175
}
154176
}
155-
pendingMap.clear();
156-
pendingFamilies.clear();
157177

158-
return {families, map};
178+
callback(families, map);
159179
}
160180

161181
void AnimatedPropsRegistry::clear(SurfaceId surfaceId) {
162-
auto lock = std::lock_guard(mutex_);
163-
if (auto it = surfaceContexts_.find(surfaceId);
164-
it != surfaceContexts_.end()) {
165-
auto& surfaceContext = it->second;
166-
surfaceContext.families.clear();
167-
surfaceContext.map.clear();
182+
std::shared_ptr<SurfaceContext> surfaceContext;
183+
{
184+
auto lock = std::lock_guard(mutex_);
185+
auto it = surfaceContexts_.find(surfaceId);
186+
if (it == surfaceContexts_.end()) {
187+
return;
188+
}
189+
surfaceContext = it->second;
190+
}
191+
192+
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> familiesToFree;
193+
SnapshotMap mapToFree;
194+
{
195+
auto mapLock = std::lock_guard(surfaceContext->mapMutex);
196+
familiesToFree.swap(surfaceContext->families);
197+
mapToFree.swap(surfaceContext->map);
168198
}
169199
}
170200

171201
void AnimatedPropsRegistry::clearOnSurfaceStop(SurfaceId surfaceId) {
172-
auto lock = std::lock_guard(mutex_);
173-
surfaceContexts_.erase(surfaceId);
202+
std::shared_ptr<SurfaceContext> surfaceContext;
203+
{
204+
auto lock = std::lock_guard(mutex_);
205+
if (auto it = surfaceContexts_.find(surfaceId);
206+
it != surfaceContexts_.end()) {
207+
surfaceContext = std::move(it->second);
208+
surfaceContexts_.erase(it);
209+
}
210+
}
174211
}
175212

176213
} // namespace facebook::react

‎packages/react-native/ReactCommon/react/renderer/animationbackend/AnimatedPropsRegistry.h‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <folly/dynamic.h>
1616
#include <react/renderer/uimanager/UIManager.h>
1717
#include <react/renderer/uimanager/UIManagerCommitHook.h>
18+
#include <functional>
19+
#include <memory>
20+
#include <mutex>
1821
#include "AnimatedProps.h"
1922
#include "AnimationMutation.h"
2023

@@ -27,12 +30,16 @@ struct PropsSnapshot {
2730
std::optional<std::string> text;
2831
};
2932

33+
using SnapshotMap = std::unordered_map<Tag, std::unique_ptr<PropsSnapshot>>;
34+
3035
struct SurfaceContext {
31-
std::unordered_map<Tag, std::unique_ptr<PropsSnapshot>> pendingMap, map;
32-
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> pendingFamilies, families;
33-
};
36+
std::mutex mapMutex;
37+
SnapshotMap map;
38+
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> families;
3439

35-
using SnapshotMap = std::unordered_map<Tag, std::unique_ptr<PropsSnapshot>>;
40+
SnapshotMap pendingMap;
41+
std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> pendingFamilies;
42+
};
3643

3744
/*
3845
* Merges raw animated props per key, like the props merged by the animation
@@ -49,14 +56,17 @@ childrenWithText(const ShadowNode &shadowNode, const ShadowNodeFragment &fragmen
4956

5057
class AnimatedPropsRegistry {
5158
public:
59+
using SurfaceMapCallback =
60+
std::function<void(const std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> &, const SnapshotMap &)>;
61+
5262
void update(const std::vector<AnimationMutations> &batches);
5363
void initializeSurface(SurfaceId surfaceId);
5464
void clear(SurfaceId surfaceId);
5565
void clearOnSurfaceStop(SurfaceId surfaceId);
56-
std::pair<std::unordered_set<std::shared_ptr<const ShadowNodeFamily>> &, SnapshotMap &> getMap(SurfaceId surfaceId);
66+
void withMap(SurfaceId surfaceId, const SurfaceMapCallback &callback);
5767

5868
private:
59-
std::unordered_map<SurfaceId, SurfaceContext> surfaceContexts_;
69+
std::unordered_map<SurfaceId, std::shared_ptr<SurfaceContext>> surfaceContexts_;
6070
std::mutex mutex_;
6171
};
6272

‎packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackend.cpp‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ static inline Props::Shared cloneProps(
4646
propsParserContext, shadowNode.getProps(), {});
4747
}
4848

49-
auto viewProps = std::const_pointer_cast<BaseViewProps>(
50-
std::static_pointer_cast<const BaseViewProps>(newProps));
51-
for (auto& animatedProp : animatedProps.props) {
52-
cloneProp(*viewProps, *animatedProp);
49+
if (!animatedProps.props.empty()) {
50+
if (auto viewProps = std::const_pointer_cast<BaseViewProps>(
51+
std::dynamic_pointer_cast<const BaseViewProps>(newProps))) {
52+
for (auto& animatedProp : animatedProps.props) {
53+
cloneProp(*viewProps, *animatedProp);
54+
}
55+
}
5356
}
5457
return newProps;
5558
}
@@ -107,7 +110,11 @@ AnimationBackend::AnimationBackend(
107110
}
108111
}
109112

110-
AnimationBackend::~AnimationBackend() = default;
113+
AnimationBackend::~AnimationBackend() {
114+
if (auto uiManager = uiManager_.lock()) {
115+
uiManager->unregisterCommitHook(*commitHook_);
116+
}
117+
}
111118

112119
void AnimationBackend::unpackMutations(
113120
AnimationMutations& mutations,

‎packages/react-native/ReactCommon/react/renderer/animationbackend/AnimationBackendCommitHook.cpp‎

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,58 +29,66 @@ RootShadowNode::Unshared AnimationBackendCommitHook::shadowTreeWillCommit(
2929
return newRootShadowNode;
3030
}
3131

32-
const auto& res = animatedPropsRegistry_->getMap(shadowTree.getSurfaceId());
33-
auto& surfaceFamilies = res.first;
34-
auto& updates = res.second;
32+
RootShadowNode::Unshared clonedRootShadowNode;
33+
animatedPropsRegistry_->withMap(
34+
shadowTree.getSurfaceId(),
35+
[&](const auto& surfaceFamilies, const auto& updates) {
36+
if (surfaceFamilies.empty()) {
37+
return;
38+
}
39+
clonedRootShadowNode = std::static_pointer_cast<RootShadowNode>(
40+
newRootShadowNode->cloneMultiple(
41+
surfaceFamilies,
42+
[&surfaceFamilies, &updates](
43+
const ShadowNode& shadowNode,
44+
const ShadowNodeFragment& fragment) {
45+
auto newProps = ShadowNodeFragment::propsPlaceholder();
46+
auto children = fragment.children;
47+
std::shared_ptr<BaseViewProps> viewProps = nullptr;
48+
if (auto updatesIter = updates.find(shadowNode.getTag());
49+
updatesIter != updates.end() &&
50+
surfaceFamilies.contains(shadowNode.getFamilyShared())) {
51+
const auto& snapshot = updatesIter->second;
52+
if (!snapshot->propNames.empty() || snapshot->rawProps) {
53+
PropsParserContext propsParserContext{
54+
shadowNode.getSurfaceId(),
55+
*shadowNode.getContextContainer()};
56+
if (snapshot->rawProps) {
57+
newProps =
58+
shadowNode.getComponentDescriptor().cloneProps(
59+
propsParserContext,
60+
shadowNode.getProps(),
61+
RawProps(*snapshot->rawProps));
62+
} else {
63+
newProps =
64+
shadowNode.getComponentDescriptor().cloneProps(
65+
propsParserContext, shadowNode.getProps(), {});
66+
}
67+
if (!snapshot->propNames.empty()) {
68+
viewProps = std::const_pointer_cast<BaseViewProps>(
69+
std::dynamic_pointer_cast<const BaseViewProps>(
70+
newProps));
71+
}
72+
}
3573

36-
if (surfaceFamilies.empty()) {
37-
return newRootShadowNode;
38-
}
39-
auto clonedRootShadowNode =
40-
std::static_pointer_cast<RootShadowNode>(newRootShadowNode->cloneMultiple(
41-
surfaceFamilies,
42-
[&surfaceFamilies, &updates](
43-
const ShadowNode& shadowNode,
44-
const ShadowNodeFragment& fragment) {
45-
auto newProps = ShadowNodeFragment::propsPlaceholder();
46-
auto children = fragment.children;
47-
std::shared_ptr<BaseViewProps> viewProps = nullptr;
48-
if (auto updatesIter = updates.find(shadowNode.getTag());
49-
updatesIter != updates.end() &&
50-
surfaceFamilies.contains(shadowNode.getFamilyShared())) {
51-
auto& snapshot = updatesIter->second;
52-
if (!snapshot->propNames.empty() || snapshot->rawProps) {
53-
PropsParserContext propsParserContext{
54-
shadowNode.getSurfaceId(),
55-
*shadowNode.getContextContainer()};
56-
if (snapshot->rawProps) {
57-
newProps = shadowNode.getComponentDescriptor().cloneProps(
58-
propsParserContext,
59-
shadowNode.getProps(),
60-
RawProps(*snapshot->rawProps));
61-
} else {
62-
newProps = shadowNode.getComponentDescriptor().cloneProps(
63-
propsParserContext, shadowNode.getProps(), {});
64-
}
65-
viewProps = std::const_pointer_cast<BaseViewProps>(
66-
std::static_pointer_cast<const BaseViewProps>(newProps));
67-
}
68-
69-
for (const auto& propName : snapshot->propNames) {
70-
updateProp(propName, *viewProps, *snapshot);
71-
}
74+
if (viewProps) {
75+
for (const auto& propName : snapshot->propNames) {
76+
updateProp(propName, *viewProps, *snapshot);
77+
}
78+
}
7279

73-
if (snapshot->text) {
74-
children =
75-
childrenWithText(shadowNode, fragment, *snapshot->text);
76-
}
77-
}
78-
return shadowNode.clone(
79-
{.props = newProps,
80-
.children = children,
81-
.state = shadowNode.getState(),
82-
.runtimeShadowNodeReference = true});
83-
}));
80+
if (snapshot->text) {
81+
children = childrenWithText(
82+
shadowNode, fragment, *snapshot->text);
83+
}
84+
}
85+
return shadowNode.clone(
86+
{.props = newProps,
87+
.children = children,
88+
.state = shadowNode.getState(),
89+
.runtimeShadowNodeReference = true});
90+
}));
91+
});
8492

8593
if (clonedRootShadowNode == nullptr) {
8694
return newRootShadowNode;

0 commit comments

Comments
 (0)