Skip to content

Commit f5db949

Browse files
authored
iOS: add null check on create impeller context (flutter/engine#56952)
In the case where `CreateImpellerContext` encounters an error while creating an `impeller::ContextMTL`, we logged an error, returned nullptr, then immediately dereferenced the null pointer. Now, rather than crash due to a segfault, we now intentionally abort with an appropriate error message. This adds checks in the `FlutterDarwinContextMetalImpeller` initialiser that aborts with an appropriate error message if impeller context creation fails, Metal device creation fails, or texture cache creation fails. Rather than bailing out and returning nil from the initialiser to pass the buck to the caller, we terminate since without a graphics context, the app won't be able to render anything to begin with. Issue: flutter#157489 Issue: [b/378790930](http://b/378790930) No test changes since this just changes an accidental crash to an intentional crash. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent 87af85b commit f5db949

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

engine/src/flutter/shell/platform/darwin/graphics/FlutterDarwinContextMetalImpeller.mm

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,8 @@
2525
std::make_shared<fml::NonOwnedMapping>(impeller_framebuffer_blend_shaders_data,
2626
impeller_framebuffer_blend_shaders_length),
2727
};
28-
auto context = impeller::ContextMTL::Create(shader_mappings, is_gpu_disabled_sync_switch,
29-
"Impeller Library");
30-
if (!context) {
31-
FML_LOG(ERROR) << "Could not create Metal Impeller Context.";
32-
return nullptr;
33-
}
34-
35-
return context;
28+
return impeller::ContextMTL::Create(shader_mappings, is_gpu_disabled_sync_switch,
29+
"Impeller Library");
3630
}
3731

3832
@implementation FlutterDarwinContextMetalImpeller
@@ -41,11 +35,9 @@ - (instancetype)init:(const std::shared_ptr<const fml::SyncSwitch>&)is_gpu_disab
4135
self = [super init];
4236
if (self != nil) {
4337
_context = CreateImpellerContext(is_gpu_disabled_sync_switch);
38+
FML_CHECK(_context) << "Could not create Metal Impeller Context.";
4439
id<MTLDevice> device = _context->GetMTLDevice();
45-
if (!device) {
46-
FML_DLOG(ERROR) << "Could not acquire Metal device.";
47-
return nil;
48-
}
40+
FML_CHECK(device) << "Could not acquire Metal device.";
4941

5042
CVMetalTextureCacheRef textureCache;
5143
CVReturn cvReturn = CVMetalTextureCacheCreate(kCFAllocatorDefault, // allocator
@@ -55,10 +47,7 @@ - (instancetype)init:(const std::shared_ptr<const fml::SyncSwitch>&)is_gpu_disab
5547
&textureCache // [out] cache
5648
);
5749

58-
if (cvReturn != kCVReturnSuccess) {
59-
FML_DLOG(ERROR) << "Could not create Metal texture cache.";
60-
return nil;
61-
}
50+
FML_CHECK(cvReturn == kCVReturnSuccess) << "Could not acquire Metal device.";
6251
_textureCache.Reset(textureCache);
6352
}
6453
return self;

0 commit comments

Comments
 (0)