Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Introduce runtime check that it is root isolate that makes UI native calls. #18050

Merged
merged 5 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ui/compositing/scene_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
namespace flutter {

static void SceneBuilder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&SceneBuilder::create, args);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/compositing/scene_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
24 changes: 2 additions & 22 deletions lib/ui/dart_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,17 @@ namespace flutter {
namespace {

static tonic::DartLibraryNatives* g_natives;
static tonic::DartLibraryNatives* g_natives_secondary;

Dart_NativeFunction GetNativeFunction(Dart_Handle name,
int argument_count,
bool* auto_setup_scope) {
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() {
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dart_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/ui/painting/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ using tonic::ToDart;
namespace flutter {

static void Canvas_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&Canvas::Create, args);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/painting/codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/painting/color_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,6 +13,7 @@
namespace flutter {

static void ColorFilter_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&ColorFilter::Create, args);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/painting/gradient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/painting/image_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace flutter {

static void ImageFilter_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&ImageFilter::Create, args);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ui/painting/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,6 +21,7 @@ namespace flutter {
typedef CanvasPath Path;

static void Path_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&CanvasPath::CreateNew, args);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ui/painting/path_measure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,6 +21,7 @@ namespace flutter {
typedef CanvasPathMeasure PathMeasure;

static void PathMeasure_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&CanvasPathMeasure::Create, args);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/painting/picture_recorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace flutter {

static void PictureRecorder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&PictureRecorder::Create, args);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ui/painting/vertices.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/semantics/semantics_update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions lib/ui/semantics/semantics_update_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -13,6 +14,7 @@
namespace flutter {

static void SemanticsUpdateBuilder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&SemanticsUpdateBuilder::create, args);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/text/font_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void LoadFontFromList(tonic::Uint8List& font_data,
}

void _LoadFontFromList(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
tonic::DartCallStatic(LoadFontFromList, args);
}

Expand Down
1 change: 1 addition & 0 deletions lib/ui/text/paragraph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ const int sForceStrutHeightMask = 1 << sForceStrutHeightIndex;
} // namespace

static void ParagraphBuilder_constructor(Dart_NativeArguments args) {
UIDartState::ThrowIfUIOperationsProhibited();
DartCallConstructor(&ParagraphBuilder::create, args);
}

Expand Down
11 changes: 10 additions & 1 deletion lib/ui/ui_dart_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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 */);
Expand All @@ -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_)
Expand Down
7 changes: 6 additions & 1 deletion lib/ui/ui_dart_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand All @@ -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_;
Expand Down
11 changes: 11 additions & 0 deletions lib/ui/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 =
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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)}));
Expand Down
6 changes: 3 additions & 3 deletions runtime/dart_isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -264,7 +264,7 @@ bool DartIsolate::LoadLibraries() {

DartIO::InitForIsolate(disable_http_);

DartUI::InitForIsolate(IsRootIsolate());
DartUI::InitForIsolate();

const bool is_service_isolate = Dart_IsServiceIsolate(isolate());

Expand Down
4 changes: 0 additions & 4 deletions runtime/dart_isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand Down
Loading