Skip to content

Commit

Permalink
[vm/ffi] Fix instantiation and garbage collection of NativeType objects
Browse files Browse the repository at this point in the history
Fixes: dart-lang/sdk#37780

Change-Id: I04f5788ab2dbce13fd6bd906acb91b8acf6d7fb3
Cq-Include-Trybots: luci.dart.try:vm-ffi-android-debug-arm-try, app-kernel-linux-debug-x64-try, vm-kernel-linux-debug-simdbc64-try,vm-kernel-linux-debug-ia32-try,vm-dartkb-linux-debug-simarm64-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-dartkb-linux-debug-x64-try,vm-kernel-precomp-linux-debug-x64-try,vm-ffi-android-product-arm-try,vm-dartkb-linux-release-x64-abi-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112390
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Auto-Submit: Daco Harkes <dacoharkes@google.com>
  • Loading branch information
dcharkes authored and commit-bot@chromium.org committed Aug 9, 2019
1 parent b0d079a commit 77f545b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
5 changes: 4 additions & 1 deletion runtime/lib/ffi_native_type_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import "dart:_internal" show patch;
import 'dart:typed_data' show TypedData;

// NativeType is not private, because it is used in type arguments.
// NativeType is abstract because it not used with const constructors in
// annotations directly, so it should never be instantiated at runtime.
@patch
@pragma("vm:entry-point")
class NativeType {}
abstract class NativeType {}

@patch
@pragma("vm:entry-point")
Expand Down
4 changes: 4 additions & 0 deletions runtime/vm/compiler/runtime_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ static uword GetInstanceSizeImpl(const dart::Class& handle) {
case kByteBufferCid:
case kByteDataViewCid:
case kFfiPointerCid:
case kFfiDynamicLibraryCid:
#define HANDLE_CASE(clazz) case kFfi##clazz##Cid:
CLASS_LIST_FFI_TYPE_MARKER(HANDLE_CASE)
#undef HANDLE_CASE
#define HANDLE_CASE(clazz) \
case kTypedData##clazz##Cid: \
case kTypedData##clazz##ViewCid: \
Expand Down
7 changes: 7 additions & 0 deletions runtime/vm/raw_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ intptr_t RawObject::VisitPointersPredefined(ObjectPointerVisitor* visitor,
size = RawDynamicLibrary::VisitDynamicLibraryPointers(raw_obj, visitor);
break;
}
#define RAW_VISITPOINTERS(clazz) case kFfi##clazz##Cid:
CLASS_LIST_FFI_TYPE_MARKER(RAW_VISITPOINTERS) {
// NativeType do not have any fields or type arguments.
size = HeapSize();
break;
}
#undef RAW_VISITPOINTERS
case kFreeListElement: {
uword addr = RawObject::ToAddr(this);
FreeListElement* element = reinterpret_cast<FreeListElement*>(addr);
Expand Down
2 changes: 1 addition & 1 deletion sdk/lib/ffi/native_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ part of dart.ffi;
///
/// [NativeType]'s subtypes are not constructible in the Dart code and serve
/// purely as markers in type signatures.
class NativeType {
abstract class NativeType {
const NativeType();
}

Expand Down
20 changes: 20 additions & 0 deletions tests/ffi/regress_37780_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// SharedObjects=ffi_test_functions

import 'dart:ffi';

import 'dylib_utils.dart';

DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions");

final triggerGc = ffiTestFunctions
.lookupFunction<Void Function(), void Function()>("TriggerGC");

main(List<String> args) {
final foo = [Float(), Double(), Uint8()];
triggerGc();
print(foo);
}

0 comments on commit 77f545b

Please sign in to comment.