Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
4.3 kB · 89 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990diff --git a/ReactCommon/react/renderer/scheduler/Scheduler.cpp b/ReactCommon/react/renderer/scheduler/Scheduler.cppindex e74e802..1b61233 100644--- a/ReactCommon/react/renderer/scheduler/Scheduler.cpp+++ b/ReactCommon/react/renderer/scheduler/Scheduler.cpp@@ -31,7 +31,8 @@ Scheduler::Scheduler( const SchedulerToolbox& schedulerToolbox, UIManagerAnimationDelegate* animationDelegate, SchedulerDelegate* delegate)- : runtimeExecutor_(schedulerToolbox.runtimeExecutor),+ : delegateInvalidated_(std::make_shared<std::atomic<bool>>(false)),+ runtimeExecutor_(schedulerToolbox.runtimeExecutor), contextContainer_(schedulerToolbox.contextContainer) { // Creating a container for future `EventDispatcher` instance. eventDispatcher_ = std::make_shared<std::optional<const EventDispatcher>>();@@ -181,6 +182,12 @@ Scheduler::~Scheduler() { LOG(WARNING) << "Scheduler::~Scheduler() was called (address: " << this << ")."; + // Invalidate any lambdas already queued via scheduleRenderingUpdate that+ // captured a raw delegate_ pointer; without this they'd dereference a+ // dangling SchedulerDelegate after Scheduler teardown. (No replacement+ // token is allocated here - Scheduler is going away.)+ *delegateInvalidated_ = true;+ auto weakRuntimeScheduler = contextContainer_->find<std::weak_ptr<RuntimeScheduler>>( RuntimeSchedulerKey);@@ -270,6 +277,19 @@ Scheduler::findComponentDescriptorByHandle_DO_NOT_USE_THIS_IS_BROKEN( #pragma mark - Delegate void Scheduler::setDelegate(SchedulerDelegate* delegate) {+ if (delegate_ != delegate) {+ // Mark the *current* token invalid: any rendering-update lambda already+ // queued holds a shared_ptr to this atomic and will observe `true` on+ // its next read, so it no-ops instead of calling into the previous+ // delegate (which the caller is about to drop).+ *delegateInvalidated_ = true;+ // Then install a *fresh* token (a new atomic) so lambdas captured+ // against the new delegate use their own non-invalidated flag.+ // Reusing the previous atomic and flipping it back to `false` would+ // re-arm the in-flight lambdas - exactly the use-after-free we're+ // trying to prevent - because they share the same shared_ptr.+ delegateInvalidated_ = std::make_shared<std::atomic<bool>>(false);+ } delegate_ = delegate; } @@ -304,7 +324,11 @@ void Scheduler::uiManagerDidFinishTransaction( runtimeScheduler_->scheduleRenderingUpdate( surfaceId, [delegate = delegate_,+ invalidated = delegateInvalidated_, mountingCoordinator = std::move(mountingCoordinator)]() {+ if (*invalidated) {+ return;+ } delegate->schedulerShouldRenderTransactions(mountingCoordinator); }); } else {@@ -330,9 +354,13 @@ void Scheduler::uiManagerDidDispatchCommand( runtimeScheduler_->scheduleRenderingUpdate( shadowNode->getSurfaceId(), [delegate = delegate_,+ invalidated = delegateInvalidated_, shadowView = std::move(shadowView), commandName, args]() {+ if (*invalidated) {+ return;+ } delegate->schedulerDidDispatchCommand(shadowView, commandName, args); }); }diff --git a/ReactCommon/react/renderer/scheduler/Scheduler.h b/ReactCommon/react/renderer/scheduler/Scheduler.hindex 1f5a59d..bdac7c9 100644--- a/ReactCommon/react/renderer/scheduler/Scheduler.h+++ b/ReactCommon/react/renderer/scheduler/Scheduler.h@@ -126,6 +126,11 @@ class Scheduler final : public UIManagerDelegate { friend class SurfaceHandler; SchedulerDelegate *delegate_;+ // Invalidation token captured by-value into lambdas deferred via+ // runtimeScheduler_->scheduleRenderingUpdate. Set to true on delegate+ // change or Scheduler destruction so a lambda that outlives its captured+ // raw delegate pointer can no-op instead of dereferencing dangling memory.+ std::shared_ptr<std::atomic<bool>> delegateInvalidated_; SharedComponentDescriptorRegistry componentDescriptorRegistry_; RuntimeExecutor runtimeExecutor_; std::shared_ptr<UIManager> uiManager_;