iOS: Add null checks on shell dereference (flutter/engine#57099)

`FlutterEngine` at the `_shell` unique_ptr ivar it owns have different lifetimes. `_shell` is initialised transitively from `runWithEntrypoint`, and reset in `[FlutterEngine destroyContext]`, which is called transitively from `[FlutterviewController dealloc]` via `[FlutterEngine notifyViewControllerDeallocated]`.

As such, all uses of `_shell` should be checked either via an assertion, in cases we know the shell should be non-null, or via a runtime null check in cases where it's expected that it may be null.

Specifically, this guards against a crash that can occur if we get a CoreAnimation transaction commit callback for an inflight frame just as we're shutting down the app (or removing the FlutterView in an add-to-app scenario).

Example stack trace:
```
0  Flutter                        0x11b28 -[FlutterEngine platformView] + 53 (weak_ptr.h:53)
1  Flutter                        0x11994 -[FlutterEngine updateViewportMetrics:] + 186 (ref_ptr.h:186)
2  Flutter                        0x1f854 -[FlutterViewController updateViewportMetricsIfNeeded] + 427 (vector:427)
3  Flutter                        0x1f9b8 -[FlutterViewController viewDidLayoutSubviews] + 1411 (FlutterViewController.mm:1411)
4  UIKitCore                      0x8c864 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2376
5  QuartzCore                     0x1fa0c CA::Layer::layout_if_needed(CA::Transaction*) + 516
6  QuartzCore                     0x1ae84c CA::Context::commit_transaction(CA::Transaction*, double, double*) + 516
7  QuartzCore                     0x2888 CA::Transaction::commit() + 648
```

Issue: https://github.com/flutter/flutter/issues/98735
Issue: https://github.com/flutter/flutter/issues/159639

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Chris Bracken
2024-12-10 11:06:13 -08:00
committed by GitHub
parent 9d726bb38e
commit 721eec4d5b
4 changed files with 41 additions and 8 deletions

View File

@@ -320,27 +320,37 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
}
- (fml::WeakPtr<flutter::PlatformView>)platformView {
FML_DCHECK(_shell);
if (!_shell) {
return {};
}
return _shell->GetPlatformView();
}
- (flutter::PlatformViewIOS*)iosPlatformView {
FML_DCHECK(_shell);
if (!_shell) {
return nullptr;
}
return static_cast<flutter::PlatformViewIOS*>(_shell->GetPlatformView().get());
}
- (fml::RefPtr<fml::TaskRunner>)platformTaskRunner {
FML_DCHECK(_shell);
if (!_shell) {
return {};
}
return _shell->GetTaskRunners().GetPlatformTaskRunner();
}
- (fml::RefPtr<fml::TaskRunner>)uiTaskRunner {
FML_DCHECK(_shell);
if (!_shell) {
return {};
}
return _shell->GetTaskRunners().GetUITaskRunner();
}
- (fml::RefPtr<fml::TaskRunner>)rasterTaskRunner {
FML_DCHECK(_shell);
if (!_shell) {
return {};
}
return _shell->GetTaskRunners().GetRasterTaskRunner();
}
@@ -392,6 +402,9 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
}
- (void)ensureSemanticsEnabled {
if (!self.iosPlatformView) {
return;
}
self.iosPlatformView->SetSemanticsEnabled(true);
}
@@ -419,6 +432,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
}
- (void)attachView {
FML_DCHECK(self.iosPlatformView);
self.iosPlatformView->attachView();
}
@@ -1224,6 +1238,7 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
#pragma mark - FlutterTextureRegistry
- (int64_t)registerTexture:(NSObject<FlutterTexture>*)texture {
FML_DCHECK(self.iosPlatformView);
int64_t textureId = self.nextTextureId++;
self.iosPlatformView->RegisterExternalTexture(textureId, texture);
return textureId;

View File

@@ -69,6 +69,19 @@ FLUTTER_ASSERT_ARC
XCTAssertNotNil(engine);
}
- (void)testShellGetters {
FlutterDartProject* project = [[FlutterDartProject alloc] init];
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar" project:project];
XCTAssertNotNil(engine);
// Ensure getters don't deref _shell when it's null, and instead return nullptr.
XCTAssertEqual(engine.platformView.get(), nullptr);
XCTAssertEqual(engine.iosPlatformView, nullptr);
XCTAssertEqual(engine.platformTaskRunner.get(), nullptr);
XCTAssertEqual(engine.uiTaskRunner.get(), nullptr);
XCTAssertEqual(engine.rasterTaskRunner.get(), nullptr);
}
- (void)testInfoPlist {
// Check the embedded Flutter.framework Info.plist, not the linked dylib.
NSURL* flutterFrameworkURL =

View File

@@ -653,6 +653,8 @@ static void SendFakeTouchEvent(UIScreen* screen,
weakPlatformView->SetNextFrameCallback([weakSelf,
platformTaskRunner = self.engine.platformTaskRunner,
rasterTaskRunner = self.engine.rasterTaskRunner]() {
FML_DCHECK(platformTaskRunner);
FML_DCHECK(rasterTaskRunner);
FML_DCHECK(rasterTaskRunner->RunsTasksOnCurrentThread());
// Get callback on raster thread and jump back to platform thread.
platformTaskRunner->PostTask([weakSelf]() { [weakSelf onFirstFrameRendered]; });
@@ -1822,7 +1824,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
});
};
_keyboardAnimationVSyncClient = [[VSyncClient alloc] initWithTaskRunner:[self.engine uiTaskRunner]
_keyboardAnimationVSyncClient = [[VSyncClient alloc] initWithTaskRunner:self.engine.uiTaskRunner
callback:uiCallback];
_keyboardAnimationVSyncClient.allowPauseAfterVsync = NO;
[_keyboardAnimationVSyncClient await];
@@ -2105,6 +2107,9 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
return;
}
fml::WeakPtr<flutter::PlatformView> platformView = self.engine.platformView;
if (!platformView) {
return;
}
int32_t flags = self.accessibilityFlags;
#if TARGET_OS_SIMULATOR
// There doesn't appear to be any way to determine whether the accessibility

View File

@@ -70,9 +70,9 @@ double VsyncWaiterIOS::GetRefreshRate() const {
- (instancetype)initWithTaskRunner:(fml::RefPtr<fml::TaskRunner>)task_runner
callback:(flutter::VsyncWaiter::Callback)callback {
self = [super init];
FML_DCHECK(task_runner);
if (self) {
if (self = [super init]) {
_refreshRate = DisplayLinkManager.displayRefreshRate;
_allowPauseAfterVsync = YES;
_callback = std::move(callback);