From 8bd9e6026762ac058be7e20a03f45d000f4f87ef Mon Sep 17 00:00:00 2001 From: Alexander Aprelev Date: Thu, 2 May 2024 09:06:38 -0700 Subject: [PATCH] Use new Dart_PortEx api to ensure SendPort can be rebuild properly. (flutter/engine#52498) Currently used Dart API can not be reliably used to rebuild dart SendPort object, [new API was introduced in dart sdk ](https://dart.googlesource.com/sdk/+/de4029ee1ba3bc89d839ae7d2fd0706d29abae58) and this PR moves flutter engine to use that new API. BUG: https://github.com/flutter/flutter/issues/147469 --- .../lib/ui/isolate_name_server/isolate_name_server.cc | 11 ++++++----- .../lib/ui/isolate_name_server/isolate_name_server.h | 8 ++++---- .../isolate_name_server_natives.cc | 10 +++++----- engine/src/flutter/runtime/dart_vm_unittests.cc | 11 +++++++---- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.cc b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.cc index 906e166321..0e11d2bfd9 100644 --- a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.cc +++ b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.cc @@ -10,24 +10,25 @@ IsolateNameServer::IsolateNameServer() {} IsolateNameServer::~IsolateNameServer() = default; -Dart_Port IsolateNameServer::LookupIsolatePortByName(const std::string& name) { +Dart_PortEx IsolateNameServer::LookupIsolatePortByName( + const std::string& name) { std::scoped_lock lock(mutex_); return LookupIsolatePortByNameUnprotected(name); } -Dart_Port IsolateNameServer::LookupIsolatePortByNameUnprotected( +Dart_PortEx IsolateNameServer::LookupIsolatePortByNameUnprotected( const std::string& name) { auto port_iterator = port_mapping_.find(name); if (port_iterator != port_mapping_.end()) { return port_iterator->second; } - return ILLEGAL_PORT; + return {ILLEGAL_PORT, ILLEGAL_PORT}; } -bool IsolateNameServer::RegisterIsolatePortWithName(Dart_Port port, +bool IsolateNameServer::RegisterIsolatePortWithName(Dart_PortEx port, const std::string& name) { std::scoped_lock lock(mutex_); - if (LookupIsolatePortByNameUnprotected(name) != ILLEGAL_PORT) { + if (LookupIsolatePortByNameUnprotected(name).port_id != ILLEGAL_PORT) { // Name is already registered. return false; } diff --git a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.h b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.h index 4d0b98a526..d938264331 100644 --- a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.h +++ b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server.h @@ -22,21 +22,21 @@ class IsolateNameServer { // Looks up the Dart_Port associated with a given name. Returns ILLEGAL_PORT // if the name does not exist. - Dart_Port LookupIsolatePortByName(const std::string& name); + Dart_PortEx LookupIsolatePortByName(const std::string& name); // Registers a Dart_Port with a given name. Returns true if registration is // successful, false if the name entry already exists. - bool RegisterIsolatePortWithName(Dart_Port port, const std::string& name); + bool RegisterIsolatePortWithName(Dart_PortEx port, const std::string& name); // Removes a name to Dart_Port mapping given a name. Returns true if the // mapping was successfully removed, false if the mapping does not exist. bool RemoveIsolateNameMapping(const std::string& name); private: - Dart_Port LookupIsolatePortByNameUnprotected(const std::string& name); + Dart_PortEx LookupIsolatePortByNameUnprotected(const std::string& name); mutable std::mutex mutex_; - std::map port_mapping_; + std::map port_mapping_; FML_DISALLOW_COPY_AND_ASSIGN(IsolateNameServer); }; diff --git a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc index ab68a417bb..0d44cdb91a 100644 --- a/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc +++ b/engine/src/flutter/lib/ui/isolate_name_server/isolate_name_server_natives.cc @@ -19,11 +19,11 @@ Dart_Handle IsolateNameServerNatives::LookupPortByName( if (!name_server) { return Dart_Null(); } - Dart_Port port = name_server->LookupIsolatePortByName(name); - if (port == ILLEGAL_PORT) { + Dart_PortEx port = name_server->LookupIsolatePortByName(name); + if (port.port_id == ILLEGAL_PORT) { return Dart_Null(); } - return Dart_NewSendPort(port); + return Dart_NewSendPortEx(port); } bool IsolateNameServerNatives::RegisterPortWithName(Dart_Handle port_handle, @@ -32,8 +32,8 @@ bool IsolateNameServerNatives::RegisterPortWithName(Dart_Handle port_handle, if (!name_server) { return false; } - Dart_Port port = ILLEGAL_PORT; - Dart_SendPortGetId(port_handle, &port); + Dart_PortEx port; + Dart_SendPortGetIdEx(port_handle, &port); if (!name_server->RegisterIsolatePortWithName(port, name)) { return false; } diff --git a/engine/src/flutter/runtime/dart_vm_unittests.cc b/engine/src/flutter/runtime/dart_vm_unittests.cc index e1ec550e42..18b4647dc1 100644 --- a/engine/src/flutter/runtime/dart_vm_unittests.cc +++ b/engine/src/flutter/runtime/dart_vm_unittests.cc @@ -25,11 +25,14 @@ TEST_F(DartVMTest, SimpleIsolateNameServer) { ASSERT_TRUE(vm); ASSERT_TRUE(vm.GetVMData()); auto ns = vm->GetIsolateNameServer(); - ASSERT_EQ(ns->LookupIsolatePortByName("foobar"), ILLEGAL_PORT); + ASSERT_EQ(ns->LookupIsolatePortByName("foobar").port_id, ILLEGAL_PORT); ASSERT_FALSE(ns->RemoveIsolateNameMapping("foobar")); - ASSERT_TRUE(ns->RegisterIsolatePortWithName(123, "foobar")); - ASSERT_FALSE(ns->RegisterIsolatePortWithName(123, "foobar")); - ASSERT_EQ(ns->LookupIsolatePortByName("foobar"), 123); + Dart_PortEx correct_portex = {123, 456}; + ASSERT_TRUE(ns->RegisterIsolatePortWithName(correct_portex, "foobar")); + ASSERT_FALSE(ns->RegisterIsolatePortWithName(correct_portex, "foobar")); + Dart_PortEx response = ns->LookupIsolatePortByName("foobar"); + ASSERT_EQ(response.port_id, correct_portex.port_id); + ASSERT_EQ(response.origin_id, correct_portex.origin_id); ASSERT_TRUE(ns->RemoveIsolateNameMapping("foobar")); }