Skip to content

[vm/ffi] NativeFieldWrapperClass1 FfiNative methods should check for nullptr #49620

@dcharkes

Description

@dcharkes
class Scene extends NativeFieldWrapperClass1 {
  @FfiNative<Void Function(Pointer<Void>)>('Scene::dispose')
  external void dispose();
}

NativeFieldWrapperClasses often share their identity between Dart and C++.

The first, and in this case only native field represents the C++ identity. The Dart object itself has the Dart identity.

If the C++ part has already been released/disposed/cleared, the native field is usually set to nullptr (unboxed to 0).

If the native field was never set (it is Dart null), trying to call dispose() will throw. However, if the native field has been cleared to nullptr, a C call is made with C++ this as nullptr usually leading to a segfault in the native code.

We should check that the native field value is not nullptr in addition to checking it for null.

We should only do this check for FfiNative instance methods on NativeFieldWrapperClasses, and only for the automatically converted receiver. It is perfectly fine to pass nullptr to a Foo* argument to any argument of a top level function or to any argument (except for the receiver) of a C++ method.

Flutter issue:

Metadata

Metadata

Assignees

Labels

area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffi

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions