From 4455e224e95cbdc27b1b223f3256f67d3ab4208d Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Mon, 6 Nov 2023 11:07:55 +0100 Subject: [PATCH] Fix FFI-based tonic layer to use proper C++ static_cast<>()s (flutter/engine#47644) Flutter makes use of C++ multiple inheritence in it's C++ classes that have corresponding Dart objects attached. An example is e.g. ``` class Canvas : public RefCountedDartWrappable, DisplayListOpFlags { } template class RefCountedDartWrappable : public fml::RefCountedThreadSafe, public tonic::DartWrappable { } ``` When a C++ class has multiple base classes, the C++ compiler has the liberty to decide the order in which they get layed out in memory. (It doesn't necessarily follow declaration order, in fact it has a preference for having classes with vtables before classes without vtables.) Two casts to consider (with multiple inheritance in mind): * upcasts: Those are done by C++ automatically but can modify the actual address (i.e. pointer value) * downcasts: Those have to be done with `static_cast<>()`, which can also modify the address (i.e. pointer value) - using `reinterpret_cast<>()` is incorrect (as it doesn't modify the address) Now the Dart objects that refer to C++ objects have a native field in them. The value of that field is a `tonic::DartWrappable*`. That means whenever we convert between the C++ object pointer (e.g. `flutter::Canvas*`) and the value of the native field (`tonic::Wrappable*`) the actual address / pointer may need modification. There were bugs in the C FFI based tonic layer: Pointer values coming from Dart (via C FFI) are `dart::Wrappable*` and not pointers to subtypes. => For methods we type the first parameter in tonic trampolines as `dart::Wrappable*` and `static_cast()` that value => Similarly for arguments, the parameter has to be typed as `dart::Wrappable*` and `static_cast()` that value How did it ever work? Well it happens to be that the C++ compiler decided to layout `tonic::DartWrappable` before `fml::RefCountedThreadSafe`, making the `tonic::DartWrappable*` and the `flutter::Canvas*` (and other classes) have the same pointer value. => This worked due to implementation choice of C++ compiler. => This breaks immediately if one decided to add another base class (with virtual methods) to `RefCountedDartWrappable` --- engine/src/flutter/third_party/tonic/dart_args.h | 12 ++++++------ .../src/flutter/third_party/tonic/dart_wrappable.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/third_party/tonic/dart_args.h b/engine/src/flutter/third_party/tonic/dart_args.h index 2469e24f7b..ee8e99ceb7 100644 --- a/engine/src/flutter/third_party/tonic/dart_args.h +++ b/engine/src/flutter/third_party/tonic/dart_args.h @@ -356,11 +356,11 @@ struct FfiDispatcher { // Static C entry-point with Dart FFI signature. static FfiReturn Call( - C* receiver, + DartWrappable* receiver, typename DartConverter::type>::type>::FfiType... args) { // Call C++ method on receiver, forwarding converted native arguments. - return DartConverter::ToFfi((receiver->*method)( + return DartConverter::ToFfi((static_cast(receiver)->*method)( DartConverter::type>::type>::FromFfi(args)...)); } @@ -408,11 +408,11 @@ struct FfiDispatcher { // Static C entry-point with Dart FFI signature. static FfiReturn Call( - C* receiver, + DartWrappable* receiver, typename DartConverter::type>::type>::FfiType... args) { // Call C++ method on receiver, forwarding converted native arguments. - return DartConverter::ToFfi((receiver->*method)( + return DartConverter::ToFfi((static_cast(receiver)->*method)( DartConverter::type>::type>::FromFfi(args)...)); } @@ -501,11 +501,11 @@ struct FfiDispatcher { // Static C entry-point with Dart FFI signature. static void Call( - C* receiver, + DartWrappable* receiver, typename DartConverter::type>::type>::FfiType... args) { // Call C++ method on receiver, forwarding converted native arguments. - (receiver->*method)( + (static_cast(receiver)->*method)( DartConverter::type>::type>::FromFfi(args)...); } diff --git a/engine/src/flutter/third_party/tonic/dart_wrappable.h b/engine/src/flutter/third_party/tonic/dart_wrappable.h index 42b1dace5e..7270c3fef9 100644 --- a/engine/src/flutter/third_party/tonic/dart_wrappable.h +++ b/engine/src/flutter/third_party/tonic/dart_wrappable.h @@ -93,7 +93,7 @@ struct DartConverter< T*, typename std::enable_if< std::is_convertible::value>::type> { - using FfiType = T*; + using FfiType = DartWrappable*; static constexpr const char* kFfiRepresentation = "Pointer"; static constexpr const char* kDartRepresentation = "Pointer"; static constexpr bool kAllowedInLeafCall = true; @@ -145,7 +145,7 @@ struct DartConverter< DartConverterWrappable::FromArguments(args, index, exception)); } - static T* FromFfi(FfiType val) { return val; } + static T* FromFfi(FfiType val) { return static_cast(val); } static FfiType ToFfi(T* val) { return val; } static const char* GetFfiRepresentation() { return kFfiRepresentation; } static const char* GetDartRepresentation() { return kDartRepresentation; }