Skip to content

Commit c33dad3

Browse files
dcharkescommit-bot@chromium.org
authored andcommitted
Revert "Reland "[vm/ffi] Disallow Pointers and structs in finalizers and expandos""
This reverts commit 3f0ad61. Reason for revert: Conflicts with https://dart-review.googlesource.com/c/sdk/+/194765 Original change's description: > Reland "[vm/ffi] Disallow `Pointer`s and structs in finalizers and expandos" > > `Dart_NewWeakPersistentHandle` and `Dart_NewFinalizableHandle` in > `dart_api.h` do no longer accept `Pointer`s and subtypes of `Struct`s > as values passed in to the `object` parameter. > > Expandos no longer accept `Pointer`s and subtypes of `Struct`s > as values passed in to the `object` parameter. > > Cleans up unused object_store->ffi_struct_class. > > Closes: #45071 > Breaking change: #45072 > > TEST=vm/cc/DartAPI_FinalizableHandleErrors > TEST=vm/cc/DartAPI_WeakPersistentHandleErrors > TEST=tests/ffi(_2)/expando_test.dart > > Change-Id: I9af6d0173db60614091068c218391f73756c135f > Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-linux-debug-x64-try > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195061 > Reviewed-by: Martin Kustermann <[email protected]> > Commit-Queue: Daco Harkes <[email protected]> [email protected],[email protected] Change-Id: I90064b6b73155a43f37388f987c6b29f72ce9770 No-Presubmit: true No-Tree-Checks: true No-Try: true Cq-Include-Trybots: luci.dart.try:vm-kernel-precomp-linux-debug-x64-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195076 Reviewed-by: Daco Harkes <[email protected]> Commit-Queue: Daco Harkes <[email protected]>
1 parent f953637 commit c33dad3

File tree

14 files changed

+31
-221
lines changed

14 files changed

+31
-221
lines changed

CHANGELOG.md

-9
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@
88
daylight saving changes that are not precisely one hour.
99
(No change on the Web which uses the JavaScript `Date` object.)
1010

11-
### Dart VM
12-
13-
* **Breaking Change** [#45071][]: `Dart_NewWeakPersistentHandle`'s and
14-
`Dart_NewFinalizableHandle`'s `object` parameter no longer accepts
15-
`Pointer`s and subtypes of `Struct`. Expandos no longer accept
16-
`Pointer`s and subtypes of `Struct`s.
17-
18-
[#45071]: https://github.com/dart-lang/sdk/issues/45071
19-
2011
## 2.13.0
2112

2213
### Language

pkg/vm/lib/transformations/ffi_use_sites.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -834,9 +834,7 @@ class _FfiUseSiteTransformer extends FfiTransformer {
834834
: null;
835835
}
836836

837-
if (!nativeTypesClasses.contains(klass) &&
838-
klass != arrayClass &&
839-
klass != arraySizeClass) {
837+
if (!nativeTypesClasses.contains(klass) && klass != arrayClass) {
840838
for (final parent in nativeTypesClasses) {
841839
if (hierarchy.isSubtypeOf(klass, parent)) {
842840
return parent;

runtime/include/dart_api.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ DART_EXPORT void Dart_DeletePersistentHandle(Dart_PersistentHandle object);
475475
*
476476
* Requires there to be a current isolate.
477477
*
478-
* \param object An object with identity.
478+
* \param object An object.
479479
* \param peer A pointer to a native object or NULL. This value is
480480
* provided to callback when it is invoked.
481481
* \param external_allocation_size The number of externally allocated
@@ -531,7 +531,7 @@ DART_EXPORT void Dart_UpdateExternalSize(Dart_WeakPersistentHandle object,
531531
*
532532
* Requires there to be a current isolate.
533533
*
534-
* \param object An object with identity.
534+
* \param object An object.
535535
* \param peer A pointer to a native object or NULL. This value is
536536
* provided to callback when it is invoked.
537537
* \param external_allocation_size The number of externally allocated

runtime/vm/dart_api_impl.cc

+21-44
Original file line numberDiff line numberDiff line change
@@ -991,23 +991,6 @@ DART_EXPORT void Dart_SetPersistentHandle(Dart_PersistentHandle obj1,
991991
obj1_ref->set_ptr(obj2_ref);
992992
}
993993

994-
// TODO(https://dartbug.com/38491): Reject Unions here as well.
995-
static bool IsFfiStruct(Thread* T, const Object& obj) {
996-
if (obj.IsNull()) {
997-
return false;
998-
}
999-
1000-
// CFE guarantees we can only have direct subclasses of `Struct`
1001-
// (no implementations or indirect subclasses are allowed).
1002-
const auto& klass = Class::Handle(Z, obj.clazz());
1003-
const auto& super_klass = Class::Handle(Z, klass.SuperClass());
1004-
if (super_klass.Name() != Symbols::Struct().ptr()) {
1005-
return false;
1006-
}
1007-
const auto& library = Library::Handle(Z, super_klass.library());
1008-
return library.url() == Symbols::DartFfi().ptr();
1009-
}
1010-
1011994
static Dart_WeakPersistentHandle AllocateWeakPersistentHandle(
1012995
Thread* thread,
1013996
const Object& ref,
@@ -1017,13 +1000,6 @@ static Dart_WeakPersistentHandle AllocateWeakPersistentHandle(
10171000
if (!ref.ptr()->IsHeapObject()) {
10181001
return NULL;
10191002
}
1020-
if (ref.IsPointer()) {
1021-
return NULL;
1022-
}
1023-
if (IsFfiStruct(thread, ref)) {
1024-
return NULL;
1025-
}
1026-
10271003
FinalizablePersistentHandle* finalizable_ref =
10281004
FinalizablePersistentHandle::New(thread->isolate_group(), ref, peer,
10291005
callback, external_allocation_size,
@@ -1032,14 +1008,16 @@ static Dart_WeakPersistentHandle AllocateWeakPersistentHandle(
10321008
}
10331009

10341010
static Dart_WeakPersistentHandle AllocateWeakPersistentHandle(
1035-
Thread* T,
1011+
Thread* thread,
10361012
Dart_Handle object,
10371013
void* peer,
10381014
intptr_t external_allocation_size,
10391015
Dart_HandleFinalizer callback) {
1040-
const auto& ref = Object::Handle(Z, Api::UnwrapHandle(object));
1041-
return AllocateWeakPersistentHandle(T, ref, peer, external_allocation_size,
1042-
callback);
1016+
REUSABLE_OBJECT_HANDLESCOPE(thread);
1017+
Object& ref = thread->ObjectHandle();
1018+
ref = Api::UnwrapHandle(object);
1019+
return AllocateWeakPersistentHandle(thread, ref, peer,
1020+
external_allocation_size, callback);
10431021
}
10441022

10451023
static Dart_FinalizableHandle AllocateFinalizableHandle(
@@ -1051,12 +1029,6 @@ static Dart_FinalizableHandle AllocateFinalizableHandle(
10511029
if (!ref.ptr()->IsHeapObject()) {
10521030
return NULL;
10531031
}
1054-
if (ref.IsPointer()) {
1055-
return NULL;
1056-
}
1057-
if (IsFfiStruct(thread, ref)) {
1058-
return NULL;
1059-
}
10601032

10611033
FinalizablePersistentHandle* finalizable_ref =
10621034
FinalizablePersistentHandle::New(thread->isolate_group(), ref, peer,
@@ -1066,13 +1038,15 @@ static Dart_FinalizableHandle AllocateFinalizableHandle(
10661038
}
10671039

10681040
static Dart_FinalizableHandle AllocateFinalizableHandle(
1069-
Thread* T,
1041+
Thread* thread,
10701042
Dart_Handle object,
10711043
void* peer,
10721044
intptr_t external_allocation_size,
10731045
Dart_HandleFinalizer callback) {
1074-
const auto& ref = Object::Handle(Z, Api::UnwrapHandle(object));
1075-
return AllocateFinalizableHandle(T, ref, peer, external_allocation_size,
1046+
REUSABLE_OBJECT_HANDLESCOPE(thread);
1047+
Object& ref = thread->ObjectHandle();
1048+
ref = Api::UnwrapHandle(object);
1049+
return AllocateFinalizableHandle(thread, ref, peer, external_allocation_size,
10761050
callback);
10771051
}
10781052

@@ -1081,27 +1055,30 @@ Dart_NewWeakPersistentHandle(Dart_Handle object,
10811055
void* peer,
10821056
intptr_t external_allocation_size,
10831057
Dart_HandleFinalizer callback) {
1084-
DARTSCOPE(Thread::Current());
1058+
Thread* thread = Thread::Current();
1059+
CHECK_ISOLATE(thread->isolate());
10851060
if (callback == NULL) {
10861061
return NULL;
10871062
}
1063+
TransitionNativeToVM transition(thread);
10881064

1089-
return AllocateWeakPersistentHandle(T, object, peer, external_allocation_size,
1090-
callback);
1065+
return AllocateWeakPersistentHandle(thread, object, peer,
1066+
external_allocation_size, callback);
10911067
}
10921068

10931069
DART_EXPORT Dart_FinalizableHandle
10941070
Dart_NewFinalizableHandle(Dart_Handle object,
10951071
void* peer,
10961072
intptr_t external_allocation_size,
10971073
Dart_HandleFinalizer callback) {
1098-
DARTSCOPE(Thread::Current());
1074+
Thread* thread = Thread::Current();
1075+
CHECK_ISOLATE(thread->isolate());
10991076
if (callback == nullptr) {
11001077
return nullptr;
11011078
}
1102-
1103-
return AllocateFinalizableHandle(T, object, peer, external_allocation_size,
1104-
callback);
1079+
TransitionNativeToVM transition(thread);
1080+
return AllocateFinalizableHandle(thread, object, peer,
1081+
external_allocation_size, callback);
11051082
}
11061083

11071084
DART_EXPORT void Dart_UpdateExternalSize(Dart_WeakPersistentHandle object,

runtime/vm/dart_api_impl_test.cc

-56
Original file line numberDiff line numberDiff line change
@@ -3368,35 +3368,6 @@ TEST_CASE(DartAPI_WeakPersistentHandleErrors) {
33683368
Dart_NewWeakPersistentHandle(obj2, NULL, 0, NopCallback);
33693369
EXPECT_EQ(ref2, static_cast<void*>(NULL));
33703370

3371-
// Pointer object.
3372-
Dart_Handle ffi_lib = Dart_LookupLibrary(NewString("dart:ffi"));
3373-
Dart_Handle pointer_type =
3374-
Dart_GetNonNullableType(ffi_lib, NewString("Pointer"), 0, NULL);
3375-
Dart_Handle obj3 = Dart_Allocate(pointer_type);
3376-
EXPECT_VALID(obj3);
3377-
Dart_WeakPersistentHandle ref3 =
3378-
Dart_NewWeakPersistentHandle(obj3, nullptr, 0, FinalizableHandleCallback);
3379-
EXPECT_EQ(ref3, static_cast<void*>(nullptr));
3380-
3381-
// Subtype of Struct object.
3382-
const char* kScriptChars = R"(
3383-
import 'dart:ffi';
3384-
3385-
class MyStruct extends Struct {
3386-
external Pointer notEmpty;
3387-
}
3388-
)";
3389-
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3390-
Dart_Handle my_struct_type =
3391-
Dart_GetNonNullableType(lib, NewString("MyStruct"), 0, NULL);
3392-
Dart_Handle obj4 = Dart_Allocate(my_struct_type);
3393-
EXPECT_VALID(obj4);
3394-
Dart_WeakPersistentHandle ref4 =
3395-
Dart_NewWeakPersistentHandle(obj4, nullptr, 0, FinalizableHandleCallback);
3396-
EXPECT_EQ(ref4, static_cast<void*>(nullptr));
3397-
3398-
// TODO(https://dartbug.com/38491): Reject Unions here as well.
3399-
34003371
Dart_ExitScope();
34013372
}
34023373

@@ -3417,33 +3388,6 @@ TEST_CASE(DartAPI_FinalizableHandleErrors) {
34173388
Dart_NewFinalizableHandle(obj2, nullptr, 0, FinalizableHandleCallback);
34183389
EXPECT_EQ(ref2, static_cast<void*>(nullptr));
34193390

3420-
// Pointer object.
3421-
Dart_Handle ffi_lib = Dart_LookupLibrary(NewString("dart:ffi"));
3422-
Dart_Handle pointer_type =
3423-
Dart_GetNonNullableType(ffi_lib, NewString("Pointer"), 0, NULL);
3424-
Dart_Handle obj3 = Dart_Allocate(pointer_type);
3425-
EXPECT_VALID(obj3);
3426-
Dart_FinalizableHandle ref3 =
3427-
Dart_NewFinalizableHandle(obj3, nullptr, 0, FinalizableHandleCallback);
3428-
EXPECT_EQ(ref3, static_cast<void*>(nullptr));
3429-
3430-
// Subtype of Struct object.
3431-
const char* kScriptChars = R"(
3432-
import 'dart:ffi';
3433-
3434-
class MyStruct extends Struct {
3435-
external Pointer notEmpty;
3436-
}
3437-
)";
3438-
Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
3439-
Dart_Handle my_struct_type =
3440-
Dart_GetNonNullableType(lib, NewString("MyStruct"), 0, NULL);
3441-
Dart_Handle obj4 = Dart_Allocate(my_struct_type);
3442-
EXPECT_VALID(obj4);
3443-
Dart_FinalizableHandle ref4 =
3444-
Dart_NewFinalizableHandle(obj4, nullptr, 0, FinalizableHandleCallback);
3445-
EXPECT_EQ(ref4, static_cast<void*>(nullptr));
3446-
34473391
Dart_ExitScope();
34483392
}
34493393

runtime/vm/kernel_loader.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1367,8 +1367,7 @@ void KernelLoader::LoadLibraryImportsAndExports(Library* library,
13671367
"runtime");
13681368
}
13691369
if (!Api::IsFfiEnabled() &&
1370-
target_library.url() == Symbols::DartFfi().ptr() &&
1371-
library->url() != Symbols::DartCore().ptr()) {
1370+
target_library.url() == Symbols::DartFfi().ptr()) {
13721371
H.ReportError(
13731372
"import of dart:ffi is not supported in the current Dart runtime");
13741373
}

runtime/vm/object_store.h

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ class ObjectPointerVisitor;
236236
RW(GrowableObjectArray, ffi_callback_functions) \
237237
RW(Class, ffi_pointer_class) \
238238
RW(Class, ffi_native_type_class) \
239+
RW(Class, ffi_struct_class) \
239240
RW(Object, ffi_as_function_internal) \
240241
// Please remember the last entry must be referred in the 'to' function below.
241242

sdk/lib/_internal/vm/lib/core_patch.dart

-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ import "dart:collection"
4949

5050
import "dart:convert" show ascii, Encoding, json, latin1, utf8;
5151

52-
import "dart:ffi" show Pointer, Struct;
53-
5452
import "dart:isolate" show Isolate;
5553

5654
import "dart:math" show Random;

sdk/lib/_internal/vm/lib/expando_patch.dart

+2-5
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,9 @@ class Expando<T> {
140140
if ((object == null) ||
141141
(object is bool) ||
142142
(object is num) ||
143-
(object is String) ||
144-
(object is Pointer) ||
145-
(object is Struct)) {
146-
// TODO(https://dartbug.com/38491): Reject Unions here as well.
143+
(object is String)) {
147144
throw new ArgumentError.value(object,
148-
"Expandos are not allowed on strings, numbers, booleans, null, Pointers or Structs.");
145+
"Expandos are not allowed on strings, numbers, booleans or null");
149146
}
150147
}
151148

sdk/lib/core/expando.dart

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ part of dart.core;
66

77
/// An [Expando] allows adding new properties to objects.
88
///
9-
/// Does not work on numbers, strings, booleans, `null`, `dart:ffi` pointers
10-
/// or `dart:ffi` structs.
9+
/// Does not work on numbers, strings, booleans or `null`.
1110
///
1211
/// An `Expando` does not hold on to the added property value after an object
1312
/// becomes inaccessible.
@@ -39,15 +38,13 @@ class Expando<T extends Object> {
3938
/// object. If the object hasn't been expanded, the method returns
4039
/// `null`.
4140
///
42-
/// The object must not be a number, a string, a boolean, `null`, a
43-
/// `dart:ffi` pointer, or a `dart:ffi` struct.
41+
/// The object must not be a number, a string or a boolean.
4442
external T? operator [](Object object);
4543

4644
/// Sets the value of this [Expando]'s property on the given
4745
/// object. Properties can effectively be removed again by setting
4846
/// their value to `null`.
4947
///
50-
/// The object must not be a number, a string, a boolean, `null`, a
51-
/// `dart:ffi` pointer, or a `dart:ffi` struct.
48+
/// The object must not be a number, a string or a boolean.
5249
external void operator []=(Object object, T? value);
5350
}

tests/corelib/expando_test.dart

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
//
5-
// VMOptions=--enable-ffi=true
6-
// VMOptions=--enable-ffi=false
74

85
import "package:expect/expect.dart";
96

tests/corelib_2/expando_test.dart

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4-
//
5-
// VMOptions=--enable-ffi=true
6-
// VMOptions=--enable-ffi=false
74

85
import "package:expect/expect.dart";
96

tests/ffi/expando_test.dart

-43
This file was deleted.

0 commit comments

Comments
 (0)