[lib] Private API for disabling Isolate.exit().

This is needed to prevent users from closing the platform isolate.

Bug: https://github.com/flutter/flutter/issues/136314
Change-Id: I98ab0a847dad245a1f1d6c16e96f2f4add39a84f
TEST=runtime/vm/isolate_test.cc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352760
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Liam Appelbe <liama@google.com>
This commit is contained in:
Liam Appelbe 2024-02-15 01:18:40 +00:00 committed by Commit Queue
parent 4df361c3c3
commit c3a626f729
2 changed files with 58 additions and 0 deletions

View file

@ -237,4 +237,56 @@ ISOLATE_UNIT_TEST_CASE(Isolate_Ports) {
EXPECT(!isolate->HasLivePorts());
}
ISOLATE_UNIT_TEST_CASE(Isolate_MayExit_True) {
TransitionVMToNative transition(thread);
EXPECT_EQ(false, thread->is_unwind_in_progress());
Dart_EnterScope();
Dart_Handle lib =
Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate"));
EXPECT(!Dart_IsError(lib));
Dart_Handle isolate_type = Dart_GetNonNullableType(
lib, Dart_NewStringFromCString("Isolate"), 0, nullptr);
EXPECT(!Dart_IsError(isolate_type));
Dart_Handle result =
Dart_Invoke(isolate_type, Dart_NewStringFromCString("exit"), 0, nullptr);
EXPECT(Dart_IsFatalError(result));
Dart_ExitScope();
EXPECT_EQ(true, thread->is_unwind_in_progress());
}
ISOLATE_UNIT_TEST_CASE(Isolate_MayExit_False) {
TransitionVMToNative transition(thread);
EXPECT_EQ(false, thread->is_unwind_in_progress());
Dart_EnterScope();
Dart_Handle lib =
Dart_LookupLibrary(Dart_NewStringFromCString("dart:isolate"));
EXPECT(!Dart_IsError(lib));
Dart_Handle isolate_type = Dart_GetNonNullableType(
lib, Dart_NewStringFromCString("Isolate"), 0, nullptr);
EXPECT(!Dart_IsError(isolate_type));
Dart_Handle setter_result = Dart_SetField(
isolate_type, Dart_NewStringFromCString("_mayExit"), Dart_False());
EXPECT(!Dart_IsError(setter_result));
Dart_Handle result =
Dart_Invoke(isolate_type, Dart_NewStringFromCString("exit"), 0, nullptr);
EXPECT(Dart_IsUnhandledExceptionError(result));
Dart_ExitScope();
EXPECT_EQ(false, thread->is_unwind_in_progress());
}
} // namespace dart

View file

@ -652,8 +652,14 @@ final class Isolate {
@pragma("vm:external-name", "Isolate_exit_")
external static Never _exit(SendPort? finalMessagePort, Object? message);
@pragma("vm:entry-point")
static bool _mayExit = true;
@patch
static Never exit([SendPort? finalMessagePort, Object? message]) {
if (!_mayExit) {
throw UnsupportedError("Isolate.exit");
}
_exit(finalMessagePort, message);
}