flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.72k stars 27.37k forks source link

App crash when I create FFI template in objective-c. #69559

Open gzjaychen opened 3 years ago

gzjaychen commented 3 years ago

https://flutter.dev/docs/development/platform-integration/c-interop 1: Create a plugin by flutter create 2: Add C/C++ sources 3: Load the code using the FFI library 4: Run on iPhone

works well: flutter create --platforms=android,ios --template=plugin native_add

crash happen: flutter create -a java -i objc --platforms=android,ios --template=plugin native_add

TahaTesser commented 3 years ago

Hi @gzjaychen Can you please provide your flutter doctor -v your flutter run --verbose and a complete reproducible minimal code sample in a GitHub repository Thank you

gzjaychen commented 3 years ago

Thank you for your reply. Here‘s the info you need.

flutter doctor -v

[✓] Flutter (Channel stable, 1.22.3, on Mac OS X 10.15.6 19G2021, locale zh-Hans-CN)
    • Flutter version 1.22.3 at /Users/chenjiangfeng/dev_tool/flutter_sdk/flutter_macos_1.22.3-stable
    • Framework revision 8874f21e79 (5 days ago), 2020-10-29 14:14:35 -0700
    • Engine revision a1440ca392
    • Dart version 2.10.3

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/chenjiangfeng/Library/Android/sdk
    • Platform android-29, build-tools 29.0.3
    • ANDROID_HOME = /Users/chenjiangfeng/Library/Android/sdk
    • Java binary at: /Applications/Android Studio 4.1.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 12.1, Build version 12A7403
    • CocoaPods version 1.9.3

[!] Android Studio (version 4.1)
    • Android Studio at /Applications/Android Studio 4.1.app/Contents
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[!] IntelliJ IDEA Community Edition (version 2020.2.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.dev/intellij-setup/#installing-the-plugins

[✓] VS Code (version 1.50.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.16.0

[✓] Connected device (1 available)
    • iPhone 12 Pro Max (mobile) • 00B9F33D-BD5A-48CA-85EB-D6702369FC0B • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-1 (simulator)

! Doctor found issues in 2 categories.

✗ Flutter plugin not installed
✗ Dart plugin not installed

I have already installed these plugins, and they work well. I don't know why these errors happen. And I think we can ignore these.

GitHub repository:

https://github.com/gzjaychen/native_add

flutter run --verbose:

comment is too long to commit. https://drive.google.com/file/d/1rl5g8ZGq3nTK-_DiTOc8O-s9nuWh0wyX/view?usp=sharing Invalid argument(s): Failed to lookup symbol (dlsym(RTLD_DEFAULT, native_add): symbol not found)

TahaTesser commented 3 years ago
logs ```bash Launching lib/main.dart on iPhone 12 in debug mode... lib/main.dart Warning: Missing build name (CFBundleShortVersionString). Warning: Missing build number (CFBundleVersion). Action Required: You must set a build name and number in the pubspec.yaml file version field before submitting to the App Store. Xcode build done. 38.1s Connecting to VM Service at ws://127.0.0.1:63643/ghjuY-niuqo=/ws ════════ Exception caught by widgets library ═══════════════════════════════════ The following ArgumentError was thrown building MyApp(dirty, state: _MyAppState#12411): Invalid argument(s): Failed to lookup symbol (dlsym(RTLD_DEFAULT, native_add): symbol not found) The relevant error-causing widget was MyApp lib/main.dart When the exception was thrown, this was the stack #0 DynamicLibrary.lookup (dart:ffi-patch/ffi_dynamic_library_patch.dart:31:29) #1 nativeAdd package:native_add/native_add.dart #2 nativeAdd (package:native_add/native_add.dart) package:native_add/native_add.dart #3 _MyAppState.build lib/main.dart #4 StatefulElement.build package:flutter/…/widgets/framework.dart ... ════════════════════════════════════════════════════════════════════════════════ ════════ Exception caught by widgets library ═══════════════════════════════════ The method 'call' was called on null. Receiver: null Tried calling: call() The relevant error-causing widget was MyApp lib/main.dart ════════════════════════════════════════════════════════════════════════════════ ```
multiple flutter doctor -v ```bash [✓] Flutter (Channel stable, 1.22.3, on Mac OS X 10.15.7 19H2, locale en-GB) • Flutter version 1.22.3 at /Users/tahatesser/Code/flutter_stable • Framework revision 8874f21e79 (6 days ago), 2020-10-29 14:14:35 -0700 • Engine revision a1440ca392 • Dart version 2.10.3 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/tahatesser/Code/sdk • Platform android-30, build-tools 30.0.2 • ANDROID_HOME = /Users/tahatesser/Code/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 12.1) • Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer • Xcode 12.1, Build version 12A7403 • CocoaPods version 1.10.0 [!] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) [✓] VS Code (version 1.50.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.16.0 [✓] Connected device (2 available) • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29) • Taha’s iPad (mobile) • 00008020-000255113EE8402E • ios • iOS 14.1 ! Doctor found issues in 1 category. ``` ```bash [✓] Flutter (Channel stable, 1.22.3, on Mac OS X 10.15.7 19H2, locale en-GB) • Flutter version 1.22.3 at /Users/tahatesser/Code/flutter_stable • Framework revision 8874f21e79 (6 days ago), 2020-10-29 14:14:35 -0700 • Engine revision a1440ca392 • Dart version 2.10.3 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at /Users/tahatesser/Code/sdk • Platform android-30, build-tools 30.0.2 • ANDROID_HOME = /Users/tahatesser/Code/sdk • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 12.1) • Xcode at /Volumes/Extreme/Xcode.app/Contents/Developer • Xcode 12.1, Build version 12A7403 • CocoaPods version 1.10.0 [!] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593) [✓] VS Code (version 1.50.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.16.0 [✓] Connected device (2 available) • RMX2001 (mobile) • EUYTFEUSQSRGDA6D • android-arm64 • Android 10 (API 29) • Taha’s iPad (mobile) • 00008020-000255113EE8402E • ios • iOS 14.1 ! Doctor found issues in 1 category. ```
chinmaygarde commented 3 years ago

The dlsym call is failing which means the templates (or user code) tried to lookup native symbols in a library that does not exist.

chinmaygarde commented 3 years ago

cc @jmagman

jmagman commented 3 years ago

nm Runner.app/Runner shows the Objective-C symbols but not the fii function:

00000001000070f0 t -[NativeAddPlugin handleMethodCall:result:]

I can can muck up the linker flags to not include -l"native_add" and it doesn't include the Objective-CNativeAddPlugin symbols. Which means it's actually linking the static lib. Library search paths are correct.

I changed the function name to native_adder to pick out just the C function, and I do see it in the static lib:

nm libnative_add.a
...
0000000000000000 T _native_adder

When I directly link the app target on libnative_add.a to bypass the CocoaPods stuff, it's the same behavior. Though maybe that's a red herring? Anyway it still crashes.

It works when I use dylibs (use_frameworks! in the Podfile).

And it also works on the dart side when I first declare and call it in the app itself:

int32_t native_adder(int32_t x, int32_t y);

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];

  native_adder(1,2);

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end

I can try bisecting, but I think someone who knows more about what DynamicLibrary.process() is doing should take a look.

jmagman commented 3 years ago

@sjindel-google Do you know the last time this worked? Is there an integration test running somewhere?

gzjaychen commented 3 years ago

@sjindel-google Can you take a moment to look at this problem? Please, thank you.

huycozy commented 1 year ago

This issue is reproducible on the latest stable and master channels with sample code at https://github.com/flutter/flutter/issues/69559#issuecomment-721080779.

Similar issue on dart-lang/ffi repository: https://github.com/dart-lang/native/issues/897

Exception logs ```bash Debug service listening on ws://127.0.0.1:58329/8cboXfpHr1c=/ws Syncing files to device iPhone 14 Pro... ======== Exception caught by widgets library ======================================================= The following ArgumentError was thrown building MyApp(dirty, state: _MyAppState#252c1): Invalid argument(s): Failed to lookup symbol 'native_add': dlsym(RTLD_DEFAULT, native_add): symbol not found The relevant error-causing widget was: MyApp MyApp:file:///Users/huynq/Documents/NEVERCODE.IO/RepoduceProjects/OPClone/native_add/example/lib/main.dart:8:10 When the exception was thrown, this was the stack: #0 DynamicLibrary.lookup (dart:ffi-patch/ffi_dynamic_library_patch.dart:33:70) dart-lang/ffi#1 nativeAdd (package:native_add/native_add.dart:12:6) dart-lang/ffi#2 nativeAdd (package:native_add/native_add.dart) dart-lang/ffi#3 _MyAppState.build (package:native_add_example/main.dart:53:35) dart-lang/ffi#4 StatefulElement.build (package:flutter/src/widgets/framework.dart:5080:27) dart-lang/ffi#5 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4968:15) dart-lang/ffi#6 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11) dart-lang/ffi#7 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) dart-lang/ffi#8 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5) dart-lang/ffi#9 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5124:11) dart-lang/ffi#10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5) dart-lang/ffi#11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16) dart-lang/ffi#12 Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18) dart-lang/ffi#13 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1176:16) dart-lang/ffi#14 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1145:5) dart-lang/ffi#15 RenderObjectToWidgetAdapter.attachToRenderTree. (package:flutter/src/widgets/binding.dart:1092:18) dart-lang/ffi#16 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2682:19) dart-lang/ffi#17 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1091:13) dart-lang/ffi#18 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:926:7) dart-lang/ffi#19 WidgetsBinding.scheduleAttachRootWidget. (package:flutter/src/widgets/binding.dart:906:7) dart-lang/ffi#23 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26) (elided 3 frames from class _Timer and dart:async-patch) ==================================================================================================== ======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building MyApp(dirty, state: _MyAppState#252c1): The method 'call' was called on null. Receiver: null Tried calling: call() The relevant error-causing widget was: MyApp MyApp:file:///Users/huynq/Documents/NEVERCODE.IO/RepoduceProjects/OPClone/native_add/example/lib/main.dart:8:10 When the exception was thrown, this was the stack: #0 _MyAppState.build (package:native_add_example/main.dart:53:44) dart-lang/ffi#1 StatefulElement.build (package:flutter/src/widgets/framework.dart:5080:27) dart-lang/ffi#2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4968:15) dart-lang/ffi#3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11) dart-lang/ffi#4 Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5) dart-lang/ffi#5 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2743:19) dart-lang/ffi#6 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:863:21) dart-lang/ffi#7 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:381:5) dart-lang/ffi#8 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1289:15) dart-lang/ffi#9 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1218:9) dart-lang/ffi#10 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1076:5) dart-lang/ffi#11 _invoke (dart:ui/hooks.dart:145:13) dart-lang/ffi#12 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5) dart-lang/ffi#13 _drawFrame (dart:ui/hooks.dart:112:31) ==================================================================================================== ```
flutter doctor -v (stable and master) ```bash [✓] Flutter (Channel stable, 3.7.5, on macOS 13.0.1 22A400 darwin-x64, locale en-EE) • Flutter version 3.7.5 on channel stable at /Users/huynq/Documents/GitHub/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision c07f788888 (5 days ago), 2023-02-22 17:52:33 -0600 • Engine revision 0f359063c4 • Dart version 2.19.2 • DevTools version 2.20.1 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-33, build-tools 31.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Users/huynq/Library/Java/JavaVirtualMachines/corretto-1.8.0_302/Contents/Home/bin/java • Java version OpenJDK Runtime Environment Corretto-8.302.08.1 (build 1.8.0_302-b08) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14B47b • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio (version 2022.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [!] Android Studio • Android Studio at /Users/huynq/Downloads/Android Studio Preview.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] IntelliJ IDEA Community Edition (version 2022.1.1) • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.75.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.58.0 [✓] Connected device (3 available) • SM T225 (mobile) • R9JT3004VRJ • android-arm64 • Android 13 (API 33) • macOS (desktop) • macos • darwin-x64 • macOS 13.0.1 22A400 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 110.0.5481.100 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 2 categories. ``` ```bash [!] Flutter (Channel master, 3.8.0-17.0.pre.39, on macOS 13.0.1 22A400 darwin-x64, locale en-EE) • Flutter version 3.8.0-17.0.pre.39 on channel master at /Users/huynq/Documents/GitHub/flutter_master ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 35f191576b (24 minutes ago), 2023-02-28 01:45:11 -0500 • Engine revision 365f348645 • Dart version 3.0.0 (build 3.0.0-277.0.dev) • DevTools version 2.22.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/huynq/Library/Android/sdk • Platform android-33, build-tools 31.0.0 • ANDROID_HOME = /Users/huynq/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14B47b • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio • Android Studio at /Applications/Android Studio Preview.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] Android Studio (version 2022.1) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301) [✓] IntelliJ IDEA Community Edition (version 2022.1.1) • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart [✓] VS Code (version 1.75.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.58.0 [✓] Connected device (3 available) • Pixel 3a (mobile) • 964AY0WL20 • android-arm64 • Android 12 (API 32) • macOS (desktop) • macos • darwin-x64 • macOS 13.0.1 22A400 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 110.0.5481.100 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 2 categories. ```
jmagman commented 8 months ago

cc @dcharkes maybe someone working on ffi tooling wants to take a look.

dcharkes commented 8 months ago

I'm a bit confused what this issue is about:

flutter create -a java -i objc --platforms=android,ios --template=plugin native_add

This does not use the plugin_ffi or package_ffi template.

The documentation on https://docs.flutter.dev/platform-integration/macos/c-interop is outdated. Devs should be using one of the new templates for FFI development. (This issue and the documentation are from before those templates existed.) I can take a look at updating this documenation.