From 0a5737e441b0866300a2c5e87b9365dd4a4304d0 Mon Sep 17 00:00:00 2001 From: Callum Moffat Date: Fri, 16 Sep 2022 13:11:08 -0400 Subject: [PATCH] Properly throw Dart error from PluginUtilities.getCallbackHandle FFI (flutter/engine#36032) --- .../src/flutter/lib/ui/dart_runtime_hooks.cc | 4 +- .../shell/common/fixtures/shell_test.dart | 71 +++++++++++++++++++ .../flutter/shell/common/shell_unittests.cc | 30 ++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/lib/ui/dart_runtime_hooks.cc b/engine/src/flutter/lib/ui/dart_runtime_hooks.cc index 3153a8b349..5b7021a031 100644 --- a/engine/src/flutter/lib/ui/dart_runtime_hooks.cc +++ b/engine/src/flutter/lib/ui/dart_runtime_hooks.cc @@ -259,7 +259,9 @@ Dart_Handle DartRuntimeHooks::GetCallbackHandle(Dart_Handle func) { } Dart_Handle DartRuntimeHooks::GetCallbackFromHandle(int64_t handle) { - return DartCallbackCache::GetCallback(handle); + Dart_Handle result = DartCallbackCache::GetCallback(handle); + PropagateIfError(result); + return result; } void DartPluginRegistrant_EnsureInitialized() { diff --git a/engine/src/flutter/shell/common/fixtures/shell_test.dart b/engine/src/flutter/shell/common/fixtures/shell_test.dart index a41715add9..cd56b95362 100644 --- a/engine/src/flutter/shell/common/fixtures/shell_test.dart +++ b/engine/src/flutter/shell/common/fixtures/shell_test.dart @@ -364,3 +364,74 @@ Future toImageSync() async { picture2.dispose(); notifyNative(); } + +@pragma('vm:entry-point') +Future included() async { + +} + +Future excluded() async { + +} + +class IsolateParam { + const IsolateParam(this.sendPort, this.rawHandle); + final SendPort sendPort; + final int rawHandle; +} + +@pragma('vm:entry-point') +Future runCallback(IsolateParam param) async { + try { + final Future Function() func = PluginUtilities.getCallbackFromHandle( + CallbackHandle.fromRawHandle(param.rawHandle) + )! as Future Function(); + await func.call(); + param.sendPort.send(true); + } + on NoSuchMethodError { + param.sendPort.send(false); + } +} + +@pragma('vm:entry-point') +void notifyNativeBool(bool value) native 'NotifyNativeBool'; + +@pragma('vm:entry-point') +Future testPluginUtilitiesCallbackHandle() async { + ReceivePort port = ReceivePort(); + await Isolate.spawn( + runCallback, + IsolateParam( + port.sendPort, + PluginUtilities.getCallbackHandle(included)!.toRawHandle() + ), + onError: port.sendPort + ); + final dynamic result1 = await port.first; + if (result1 != true) { + print('Expected $result1 to == true'); + notifyNativeBool(false); + return; + } + port.close(); + if (const bool.fromEnvironment('dart.vm.product')) { + port = ReceivePort(); + await Isolate.spawn( + runCallback, + IsolateParam( + port.sendPort, + PluginUtilities.getCallbackHandle(excluded)!.toRawHandle() + ), + onError: port.sendPort + ); + final dynamic result2 = await port.first; + if (result2 != false) { + print('Expected $result2 to == false'); + notifyNativeBool(false); + return; + } + port.close(); + } + notifyNativeBool(true); +} diff --git a/engine/src/flutter/shell/common/shell_unittests.cc b/engine/src/flutter/shell/common/shell_unittests.cc index dbf6b0d725..2e01623aa9 100644 --- a/engine/src/flutter/shell/common/shell_unittests.cc +++ b/engine/src/flutter/shell/common/shell_unittests.cc @@ -3850,6 +3850,36 @@ TEST_F(ShellTest, PictureToImageSync) { DestroyShell(std::move(shell)); } +TEST_F(ShellTest, PluginUtilitiesCallbackHandleErrorHandling) { + auto settings = CreateSettingsForFixture(); + std::unique_ptr shell = + CreateShell(settings, GetTaskRunnersForFixture()); + + fml::AutoResetWaitableEvent latch; + bool test_passed; + AddNativeCallback("NotifyNativeBool", CREATE_NATIVE_ENTRY([&](auto args) { + Dart_Handle exception = nullptr; + test_passed = tonic::DartConverter::FromArguments( + args, 0, exception); + latch.Signal(); + })); + + ASSERT_NE(shell, nullptr); + ASSERT_TRUE(shell->IsSetup()); + auto configuration = RunConfiguration::InferFromSettings(settings); + PlatformViewNotifyCreated(shell.get()); + configuration.SetEntrypoint("testPluginUtilitiesCallbackHandle"); + RunEngine(shell.get(), std::move(configuration)); + PumpOneFrame(shell.get()); + + latch.Wait(); + + ASSERT_TRUE(test_passed); + + PlatformViewNotifyDestroyed(shell.get()); + DestroyShell(std::move(shell)); +} + } // namespace testing } // namespace flutter