Introduce runtime check that it is root isolate that makes UI native calls. (flutter/engine#18050)

* Revert "Do not register UI-related native functions in secondary isolates (#6401)"

This reverts commit c23deb818e as it doesn't work when root and secondary isolates run in the same isolate group.

* Confirm it is root isolate that makes UI native calls.

* Fix format, UIDartState reference from Fuchsia source

* No UI isolate check for fuchsia calls

* Fix typo. Remove redundant runtime calls
This commit is contained in:
Alexander Aprelev
2020-05-01 09:05:41 -07:00
committed by GitHub
parent fbdf852d1d
commit 4b5878c6ab
25 changed files with 61 additions and 34 deletions

View File

@@ -37,6 +37,7 @@
namespace flutter {
static void SceneBuilder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&SceneBuilder::create, args);
}

View File

@@ -47,6 +47,7 @@ using SceneHostBindings = std::unordered_map<SceneHostBindingKey,
static SceneHostBindings scene_host_bindings;
void SceneHost_constructor(Dart_NativeArguments args) {
flutter::UIDartState::ThrowIfUIOperationsProhibited();
tonic::DartCallConstructor(&flutter::SceneHost::Create, args);
}

View File

@@ -42,7 +42,6 @@ namespace flutter {
namespace {
static tonic::DartLibraryNatives* g_natives;
static tonic::DartLibraryNatives* g_natives_secondary;
Dart_NativeFunction GetNativeFunction(Dart_Handle name,
int argument_count,
@@ -50,21 +49,10 @@ Dart_NativeFunction GetNativeFunction(Dart_Handle name,
return g_natives->GetNativeFunction(name, argument_count, auto_setup_scope);
}
Dart_NativeFunction GetNativeFunctionSecondary(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
return g_natives_secondary->GetNativeFunction(name, argument_count,
auto_setup_scope);
}
const uint8_t* GetSymbol(Dart_NativeFunction native_function) {
return g_natives->GetSymbol(native_function);
}
const uint8_t* GetSymbolSecondary(Dart_NativeFunction native_function) {
return g_natives_secondary->GetSymbol(native_function);
}
} // namespace
void DartUI::InitForGlobal() {
@@ -97,21 +85,13 @@ void DartUI::InitForGlobal() {
#if defined(OS_FUCHSIA)
SceneHost::RegisterNatives(g_natives);
#endif
// Secondary isolates do not provide UI-related APIs.
g_natives_secondary = new tonic::DartLibraryNatives();
DartRuntimeHooks::RegisterNatives(g_natives_secondary);
IsolateNameServerNatives::RegisterNatives(g_natives_secondary);
}
}
void DartUI::InitForIsolate(bool is_root_isolate) {
void DartUI::InitForIsolate() {
FML_DCHECK(g_natives);
auto get_native_function =
is_root_isolate ? GetNativeFunction : GetNativeFunctionSecondary;
auto get_symbol = is_root_isolate ? GetSymbol : GetSymbolSecondary;
Dart_Handle result = Dart_SetNativeResolver(
Dart_LookupLibrary(ToDart("dart:ui")), get_native_function, get_symbol);
Dart_LookupLibrary(ToDart("dart:ui")), GetNativeFunction, GetSymbol);
if (Dart_IsError(result)) {
Dart_PropagateError(result);
}

View File

@@ -12,7 +12,7 @@ namespace flutter {
class DartUI {
public:
static void InitForGlobal();
static void InitForIsolate(bool is_root_isolate);
static void InitForIsolate();
private:
FML_DISALLOW_IMPLICIT_CONSTRUCTORS(DartUI);

View File

@@ -25,6 +25,7 @@ using tonic::ToDart;
namespace flutter {
static void Canvas_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&Canvas::Create, args);
}

View File

@@ -145,6 +145,7 @@ static std::variant<ImageDecoder::ImageInfo, std::string> ConvertImageInfo(
}
static void InstantiateImageCodec(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
Dart_Handle callback_handle = Dart_GetNativeArgument(args, 1);
if (!Dart_IsClosure(callback_handle)) {
Dart_SetReturnValue(args, tonic::ToDart("Callback must be a function"));

View File

@@ -4,6 +4,7 @@
#include "flutter/lib/ui/painting/color_filter.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/dart_args.h"
#include "third_party/tonic/dart_binding_macros.h"
@@ -12,6 +13,7 @@
namespace flutter {
static void ColorFilter_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&ColorFilter::Create, args);
}

View File

@@ -17,6 +17,7 @@ typedef CanvasGradient
Gradient; // Because the C++ name doesn't match the Dart name.
static void Gradient_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&CanvasGradient::Create, args);
}

View File

@@ -16,6 +16,7 @@
namespace flutter {
static void ImageFilter_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&ImageFilter::Create, args);
}

View File

@@ -8,6 +8,7 @@
#include <math.h>
#include "flutter/lib/ui/painting/matrix.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/dart_args.h"
#include "third_party/tonic/dart_binding_macros.h"
@@ -20,6 +21,7 @@ namespace flutter {
typedef CanvasPath Path;
static void Path_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&CanvasPath::CreateNew, args);
}

View File

@@ -8,6 +8,7 @@
#include <math.h>
#include "flutter/lib/ui/painting/matrix.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/dart_args.h"
#include "third_party/tonic/dart_binding_macros.h"
@@ -20,6 +21,7 @@ namespace flutter {
typedef CanvasPathMeasure PathMeasure;
static void PathMeasure_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&CanvasPathMeasure::Create, args);
}

View File

@@ -14,6 +14,7 @@
namespace flutter {
static void PictureRecorder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&PictureRecorder::Create, args);
}

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "flutter/lib/ui/painting/vertices.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include <algorithm>
@@ -27,6 +28,7 @@ void DecodeInts(const tonic::Int32List& ints, T* out) {
} // namespace
static void Vertices_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&Vertices::Create, args);
}

View File

@@ -7,6 +7,7 @@
#include <memory>
#include "flutter/lib/ui/painting/matrix.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/tonic/converter/dart_converter.h"
#include "third_party/tonic/dart_args.h"
#include "third_party/tonic/dart_binding_macros.h"

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "flutter/lib/ui/semantics/semantics_update_builder.h"
#include "flutter/lib/ui/ui_dart_state.h"
#include "third_party/skia/include/core/SkScalar.h"
#include "third_party/tonic/converter/dart_converter.h"
@@ -13,6 +14,7 @@
namespace flutter {
static void SemanticsUpdateBuilder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&SemanticsUpdateBuilder::create, args);
}

View File

@@ -39,6 +39,7 @@ void LoadFontFromList(tonic::Uint8List& font_data,
}
void _LoadFontFromList(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
tonic::DartCallStatic(LoadFontFromList, args);
}

View File

@@ -130,6 +130,7 @@ const int sForceStrutHeightMask = 1 << sForceStrutHeightIndex;
} // namespace
static void ParagraphBuilder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&ParagraphBuilder::create, args);
}

View File

@@ -25,7 +25,8 @@ UIDartState::UIDartState(
std::string advisory_script_entrypoint,
std::string logger_prefix,
UnhandledExceptionCallback unhandled_exception_callback,
std::shared_ptr<IsolateNameServer> isolate_name_server)
std::shared_ptr<IsolateNameServer> isolate_name_server,
bool is_root_isolate)
: task_runners_(std::move(task_runners)),
add_callback_(std::move(add_callback)),
remove_callback_(std::move(remove_callback)),
@@ -36,6 +37,7 @@ UIDartState::UIDartState(
advisory_script_uri_(std::move(advisory_script_uri)),
advisory_script_entrypoint_(std::move(advisory_script_entrypoint)),
logger_prefix_(std::move(logger_prefix)),
is_root_isolate_(is_root_isolate),
unhandled_exception_callback_(unhandled_exception_callback),
isolate_name_server_(std::move(isolate_name_server)) {
AddOrRemoveTaskObserver(true /* add */);
@@ -62,6 +64,13 @@ void UIDartState::DidSetIsolate() {
SetDebugName(debug_name.str());
}
void UIDartState::ThrowIfUIOperationsProhibited() {
if (!UIDartState::Current()->IsRootIsolate()) {
Dart_ThrowException(
tonic::ToDart("UI actions are only available on root isolate."));
}
}
void UIDartState::SetDebugName(const std::string debug_name) {
debug_name_ = debug_name;
if (window_)

View File

@@ -34,6 +34,9 @@ class UIDartState : public tonic::DartState {
static UIDartState* Current();
Dart_Port main_port() const { return main_port_; }
// Root isolate of the VM application
bool IsRootIsolate() const { return is_root_isolate_; }
static void ThrowIfUIOperationsProhibited();
void SetDebugName(const std::string name);
@@ -89,7 +92,8 @@ class UIDartState : public tonic::DartState {
std::string advisory_script_entrypoint,
std::string logger_prefix,
UnhandledExceptionCallback unhandled_exception_callback,
std::shared_ptr<IsolateNameServer> isolate_name_server);
std::shared_ptr<IsolateNameServer> isolate_name_server,
bool is_root_isolate_);
~UIDartState() override;
@@ -113,6 +117,7 @@ class UIDartState : public tonic::DartState {
const std::string advisory_script_entrypoint_;
const std::string logger_prefix_;
Dart_Port main_port_ = ILLEGAL_PORT;
const bool is_root_isolate_;
std::string debug_name_;
std::unique_ptr<Window> window_;
tonic::DartMicrotaskQueue microtask_queue_;

View File

@@ -18,16 +18,19 @@ namespace flutter {
namespace {
void DefaultRouteName(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
std::string routeName =
UIDartState::Current()->window()->client()->DefaultRouteName();
Dart_SetReturnValue(args, tonic::StdStringToDart(routeName));
}
void ScheduleFrame(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
UIDartState::Current()->window()->client()->ScheduleFrame();
}
void Render(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
Dart_Handle exception = nullptr;
Scene* scene =
tonic::DartConverter<Scene*>::FromArguments(args, 1, exception);
@@ -39,6 +42,7 @@ void Render(Dart_NativeArguments args) {
}
void UpdateSemantics(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
Dart_Handle exception = nullptr;
SemanticsUpdate* update =
tonic::DartConverter<SemanticsUpdate*>::FromArguments(args, 1, exception);
@@ -50,6 +54,7 @@ void UpdateSemantics(Dart_NativeArguments args) {
}
void SetIsolateDebugName(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
Dart_Handle exception = nullptr;
const std::string name =
tonic::DartConverter<std::string>::FromArguments(args, 1, exception);
@@ -61,12 +66,15 @@ void SetIsolateDebugName(Dart_NativeArguments args) {
}
void SetNeedsReportTimings(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
Dart_Handle exception = nullptr;
bool value = tonic::DartConverter<bool>::FromArguments(args, 1, exception);
UIDartState::Current()->window()->client()->SetNeedsReportTimings(value);
}
void ReportUnhandledException(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
Dart_Handle exception = nullptr;
auto error_name =
@@ -143,6 +151,8 @@ void _RespondToPlatformMessage(Dart_NativeArguments args) {
}
void GetPersistentIsolateData(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
auto persistent_isolate_data =
UIDartState::Current()->window()->client()->GetPersistentIsolateData();
@@ -270,6 +280,7 @@ void Window::UpdateSemanticsEnabled(bool enabled) {
if (!dart_state)
return;
tonic::DartState::Scope scope(dart_state);
UIDartState::ThrowIfUIOperationsProhibited();
tonic::LogIfError(tonic::DartInvokeField(
library_.value(), "_updateSemanticsEnabled", {tonic::ToDart(enabled)}));

View File

@@ -136,8 +136,8 @@ DartIsolate::DartIsolate(const Settings& settings,
advisory_script_entrypoint,
settings.log_tag,
settings.unhandled_exception_callback,
DartVMRef::GetIsolateNameServer()),
is_root_isolate_(is_root_isolate),
DartVMRef::GetIsolateNameServer(),
is_root_isolate),
disable_http_(settings.disable_http) {
phase_ = Phase::Uninitialized;
}
@@ -264,7 +264,7 @@ bool DartIsolate::LoadLibraries() {
DartIO::InitForIsolate(disable_http_);
DartUI::InitForIsolate(IsRootIsolate());
DartUI::InitForIsolate();
const bool is_service_isolate = Dart_IsServiceIsolate(isolate());

View File

@@ -381,9 +381,6 @@ class DartIsolate : public UIDartState {
///
fml::RefPtr<fml::TaskRunner> GetMessageHandlingTaskRunner() const;
// Root isolate of the VM application
bool IsRootIsolate() const { return is_root_isolate_; }
private:
class AutoFireClosure {
public:
@@ -401,7 +398,6 @@ class DartIsolate : public UIDartState {
std::vector<std::shared_ptr<const fml::Mapping>> kernel_buffers_;
std::vector<std::unique_ptr<AutoFireClosure>> shutdown_callbacks_;
fml::RefPtr<fml::TaskRunner> message_handling_task_runner_;
const bool is_root_isolate_;
const bool disable_http_;
DartIsolate(const Settings& settings,

View File

@@ -163,7 +163,7 @@ void Handle::ScheduleCallback(tonic::DartPersistentValue callback,
DART_REGISTER_NATIVE_STATIC(CLASS, METHOD),
FOR_EACH_STATIC_BINDING(DART_NATIVE_CALLBACK_STATIC)
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
FOR_EACH_BINDING(DART_NATIVE_NO_UI_CHECK_CALLBACK)
void Handle::RegisterNatives(tonic::DartLibraryNatives* natives) {
natives->Register({FOR_EACH_STATIC_BINDING(DART_REGISTER_NATIVE_STATIC_)

View File

@@ -26,7 +26,7 @@ IMPLEMENT_WRAPPERTYPEINFO(zircon, HandleWaiter);
#define FOR_EACH_BINDING(V) V(HandleWaiter, Cancel)
FOR_EACH_BINDING(DART_NATIVE_CALLBACK)
FOR_EACH_BINDING(DART_NATIVE_NO_UI_CHECK_CALLBACK)
void HandleWaiter::RegisterNatives(tonic::DartLibraryNatives* natives) {
natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE)});

View File

@@ -7,8 +7,14 @@
#include "tonic/dart_args.h"
#define DART_NATIVE_NO_UI_CHECK_CALLBACK(CLASS, METHOD) \
static void CLASS##_##METHOD(Dart_NativeArguments args) { \
tonic::DartCall(&CLASS::METHOD, args); \
}
#define DART_NATIVE_CALLBACK(CLASS, METHOD) \
static void CLASS##_##METHOD(Dart_NativeArguments args) { \
UIDartState::ThrowIfUIOperationsProhibited(); \
tonic::DartCall(&CLASS::METHOD, args); \
}