Skip to content

Commit 7e75e84

Browse files
aamCommit Queue
authored andcommitted
[vm/shared] Guard deferred-libraries and environment access from being used in isolate group bound dart code.
TEST=ffi/run_isolate_group_run_test BUG=#61323 BUG=#61324 Change-Id: Ib66ab85424adde58b0d4771f8785f8584c021c75 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/446000 Commit-Queue: Alexander Aprelev <[email protected]> Reviewed-by: Ryan Macnak <[email protected]>
1 parent d93cd60 commit 7e75e84

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

runtime/lib/isolate.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ DEFINE_NATIVE_ENTRY(Capability_get_hashcode, 0, 1) {
5757
return Smi::New(hash);
5858
}
5959

60-
static void ThrowCantRunWithoutIsolateError() {
61-
const auto& error =
62-
String::Handle(String::New("Only available when running in context of "
63-
"an isolate, rather than isolate group."));
64-
Exceptions::ThrowArgumentError(error);
65-
}
66-
6760
DEFINE_NATIVE_ENTRY(RawReceivePort_factory, 0, 2) {
6861
ASSERT(
6962
TypeArguments::CheckedHandle(zone, arguments->NativeArgAt(0)).IsNull());

runtime/lib/object.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "lib/invocation_mirror.h"
88
#include "vm/bytecode_reader.h"
99
#include "vm/code_patcher.h"
10+
#include "vm/dart_api_impl.h"
1011
#include "vm/dart_entry.h"
1112
#include "vm/exceptions.h"
1213
#include "vm/heap/heap.h"
@@ -247,12 +248,20 @@ DEFINE_NATIVE_ENTRY(Type_equality, 0, 2) {
247248
}
248249

249250
DEFINE_NATIVE_ENTRY(LibraryPrefix_isLoaded, 0, 1) {
251+
if (isolate == nullptr) {
252+
ThrowCantRunWithoutIsolateError();
253+
UNREACHABLE();
254+
}
250255
const LibraryPrefix& prefix =
251256
LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0));
252257
return Bool::Get(isolate->IsPrefixLoaded(prefix)).ptr();
253258
}
254259

255260
DEFINE_NATIVE_ENTRY(LibraryPrefix_setLoaded, 0, 1) {
261+
if (isolate == nullptr) {
262+
ThrowCantRunWithoutIsolateError();
263+
UNREACHABLE();
264+
}
256265
const LibraryPrefix& prefix =
257266
LibraryPrefix::CheckedHandle(zone, arguments->NativeArgAt(0));
258267
isolate->SetPrefixIsLoaded(prefix);

runtime/vm/dart_api_impl.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5344,6 +5344,10 @@ StringPtr Api::GetEnvironmentValue(Thread* thread, const String& name) {
53445344

53455345
StringPtr Api::CallEnvironmentCallback(Thread* thread, const String& name) {
53465346
Isolate* isolate = thread->isolate();
5347+
if (isolate == nullptr) {
5348+
ThrowCantRunWithoutIsolateError();
5349+
UNREACHABLE();
5350+
}
53475351
Dart_EnvironmentCallback callback = isolate->environment_callback();
53485352
if (callback != nullptr) {
53495353
Scope api_scope(thread);
@@ -5372,7 +5376,10 @@ StringPtr Api::CallEnvironmentCallback(Thread* thread, const String& name) {
53725376
DART_EXPORT Dart_Handle
53735377
Dart_SetEnvironmentCallback(Dart_EnvironmentCallback callback) {
53745378
Isolate* isolate = Isolate::Current();
5375-
CHECK_ISOLATE(isolate);
5379+
if (isolate == nullptr) {
5380+
ThrowCantRunWithoutIsolateError();
5381+
UNREACHABLE();
5382+
}
53765383
isolate->set_environment_callback(callback);
53775384
return Api::Success();
53785385
}
@@ -5804,7 +5811,7 @@ static Dart_Handle LoadLibrary(Thread* T, const ExternalTypedData& td) {
58045811
kernel::KernelLoader::LoadEntireProgram(program.get(), false);
58055812
program.reset();
58065813

5807-
IsolateGroupSource* source = Isolate::Current()->source();
5814+
IsolateGroupSource* source = IsolateGroup::Current()->source();
58085815
source->add_loaded_blob(Z, td);
58095816

58105817
return Api::NewHandle(T, result.ptr());
@@ -7080,4 +7087,11 @@ DART_EXPORT char* Dart_WriteHeapSnapshot(
70807087
#endif
70817088
}
70827089

7090+
void ThrowCantRunWithoutIsolateError() {
7091+
const auto& error =
7092+
String::Handle(String::New("Only available when running in context of "
7093+
"an isolate, rather than isolate group."));
7094+
Exceptions::ThrowArgumentError(error);
7095+
}
7096+
70837097
} // namespace dart

runtime/vm/dart_api_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ Isolate* CreateWithinExistingIsolateGroup(IsolateGroup* group,
354354
const char* name,
355355
char** error);
356356

357+
void ThrowCantRunWithoutIsolateError();
358+
357359
} // namespace dart.
358360

359361
#endif // RUNTIME_VM_DART_API_IMPL_H_

tests/ffi/run_isolate_group_run_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import 'dart:isolate';
1717

1818
import "package:expect/expect.dart";
1919

20+
import "run_isolate_group_run_test.dart" deferred as lib1;
21+
2022
var foo = 42;
2123
var foo_no_initializer;
2224

@@ -74,6 +76,8 @@ ListMethodTearoffTest(List<String> args) {
7476
);
7577
}
7678

79+
thefun() {}
80+
7781
main(List<String> args) {
7882
IsolateGroup.runSync(() {
7983
final l = <int>[];
@@ -220,5 +224,19 @@ main(List<String> args) {
220224
);
221225
rp.close();
222226

227+
// deferred libraries can't be used from isolate group callbacks.
228+
Expect.throws(() {
229+
IsolateGroup.runSync(() {
230+
lib1.thefun();
231+
});
232+
}, (e) => e is ArgumentError && e.toString().contains("Only available when"));
233+
234+
// environment can't be accessed from isolate group callbacks.
235+
Expect.throws(() {
236+
IsolateGroup.runSync(() {
237+
new bool.hasEnvironment("Anything");
238+
});
239+
}, (e) => e is ArgumentError && e.toString().contains("Only available when"));
240+
223241
print("All tests completed :)");
224242
}

0 commit comments

Comments
 (0)