Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dart plugin registration doesn't work when app is built with obfuscation #99477

Closed
tgucio opened this issue Mar 3, 2022 · 15 comments
Closed
Assignees
Labels
c: regression It was better in the past than it is now engine flutter/engine repository. See also e: labels. found in release: 2.11 Found to occur in 2.11 has reproducible steps The issue has been confirmed reproducible and is ready to work on P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. r: fixed Issue is closed as already fixed in a newer version

Comments

@tgucio
Copy link
Contributor

tgucio commented Mar 3, 2022

When building without obfuscation, this works.
This has started failing recently. Same issue seen on Windows, Linux and possibly other platforms.

Steps to Reproduce (Windows)

  1. Execute flutter build windows --obfuscate --split-debug-info=build\app\symbols\windows on the code sample
  2. Run the application from: build\windows\runner\Release
  3. Press the button to see if the docs path is read correctly

Expected results: Call to getApplicationDocumentsDirectory() completes and docs path is updated

Actual results: Call never completes

pubspec.yaml
name: obfuscation
description: A new Flutter project.

version: 1.0.0+1

environment:
  sdk: ">=2.17.0-165.0.dev <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  path_provider: ^2.0.9

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key, this.dir}) : super(key: key);

  final String? dir;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(title: dir ?? ''),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _path = '<?>';

  Future<void> _getPath() async {
    final Directory directory;
    try {
      directory = await getApplicationDocumentsDirectory();
    } catch (e) {
      return;
    }

    setState(() { _path = directory.path; });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Text('Documents path: $_path'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _getPath,
        tooltip: 'Get path',
        child: const Icon(Icons.folder),
      ),
    );
  }
}
Logs
flutter doctor -v
[√] Flutter (Channel master, 2.11.0-0.0.pre.752, on Microsoft Windows [Version 10.0.19044.1526], locale en-US)
    • Flutter version 2.11.0-0.0.pre.752 at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision a786f65700 (3 hours ago), 2022-03-03 03:01:22 -0500
    • Engine revision 993285f64d
    • Dart version 2.17.0 (build 2.17.0-165.0.dev)
    • DevTools version 2.11.1

[X] Android toolchain - develop for Android devices
    X Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.10)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.32126.315
    • Windows 10 SDK version 10.0.19041.0

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).

[√] VS Code, 64-bit edition (version 1.64.2)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.36.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19044.1526]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 98.0.4758.102
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 98.0.1108.43

[√] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.
@tgucio
Copy link
Contributor Author

tgucio commented Mar 3, 2022

/cc @stuartmorgan

A bit of early debug suggests that when obfuscation is enabled, PathProviderWindows isn't registered at all and instead the default MethodChannelPathProvider instance is used, which is why the calls go nowhere. After disabling obfuscation this goes away and PathProviderWindows.registerWith() is called.

@maheshmnj maheshmnj added the in triage Presently being triaged by the triage team label Mar 3, 2022
@maheshmnj
Copy link
Member

Hi @tgucio, thanks for filing the issue, I was able to reproduce the issue getApplicationDocumentsDirectory() does not return path on the master channel with obfuscation. But it does work on the stable channel. Labeling this issue as a regression.

flutter doctor -v (windows)
[√] Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.19044.1526], locale en-US)
    • Flutter version 2.10.3 at C:\flutter_sdk\stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7e9793dee1 (14 hours ago), 2022-03-02 11:23:12 -0600
    • Engine revision bd539267b4
    • Dart version 2.16.1
    • DevTools version 2.9.2

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\mahesh\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31727.386
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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.10+0-b96-7249189)

[√] VS Code (version 1.64.2)
    • VS Code at C:\Users\mahesh\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.36.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19044.1526]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 98.0.4758.102
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 98.0.1108.56

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
[√] Flutter (Channel master, 2.11.0-0.0.pre.752, on Microsoft Windows [Version 10.0.19044.1526], locale en-US)
    • Flutter version 2.11.0-0.0.pre.752 at C:\flutter_sdk\master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision a786f65700 (in the future), 2022-03-03 03:01:22 -0500
    • Engine revision 993285f64d
    • Dart version 2.17.0 (build 2.17.0-165.0.dev)
    • DevTools version 2.11.1

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\mahesh\AppData\Local\Android\sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31727.386
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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.10+0-b96-7249189)

[√] VS Code (version 1.64.2)
    • VS Code at C:\Users\mahesh\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.36.0

[√] Connected device (4 available)
    • Windows (desktop)       • windows • windows-x64     • Microsoft Windows [Version 10.0.19044.1526]
    • Windows (UWP) (desktop) • winuwp  • windows-uwp-x64 • 
    • Chrome (web)            • chrome  • web-javascript  • Google Chrome 98.0.4758.102
    • Edge (web)              • edge    • web-javascript  • Microsoft Edge 98.0.1108.56

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

@maheshmnj maheshmnj added a: desktop Running on desktop found in release: 2.10 Found to occur in 2.10 found in release: 2.11 Found to occur in 2.11 has reproducible steps The issue has been confirmed reproducible and is ready to work on p: first party platform-windows Building on or for Windows specifically c: regression It was better in the past than it is now p: path_provider Plugin that provides resolved paths to some platform directories and removed in triage Presently being triaged by the triage team found in release: 2.10 Found to occur in 2.10 labels Mar 3, 2022
@maheshmnj maheshmnj changed the title Calls to getApplicationDocumentsDirectory() never complete when app built with obfuscation [Windows] Path Provider Calls to getApplicationDocumentsDirectory() never complete when app built with obfuscation Mar 3, 2022
@stuartmorgan
Copy link
Contributor

@gaaclarke It sounds like the new lookup system not robust against obfuscation, which would be a significant regression.

@stuartmorgan stuartmorgan added the P1 High-priority issues at the top of the work list label Mar 3, 2022
@stuartmorgan stuartmorgan changed the title [Windows] Path Provider Calls to getApplicationDocumentsDirectory() never complete when app built with obfuscation Dart plugin registration doesn't work when app is built with obfuscation Mar 3, 2022
@stuartmorgan
Copy link
Contributor

It's probably this code:

     if (EndsWith(library_name,
                 "dart_tool/flutter_build/dart_plugin_registrant.dart'")) {            

combined with dart-lang/sdk#44215

@alexmarkov Is there a workaround we can use to call an entry point when obsfuscation is enabled?

@stuartmorgan
Copy link
Contributor

stuartmorgan commented Mar 3, 2022

We're already iterating through all the libraries to find the right one... what if we just tried calling InvokeDartPluginRegistrantIfAvailable on every single library until one works? The ones that are wrong would just return false it looks like:
https://github.com/flutter/engine/blob/e37b91037c4f9ee7aa2ebc47d6898913df463620/runtime/dart_plugin_registrant.cc#L57-L62

(Presumably that's slower; we could try to find it by name first, then fall back to trying everything.)

@stuartmorgan stuartmorgan added engine flutter/engine repository. See also e: labels. and removed platform-windows Building on or for Windows specifically a: desktop Running on desktop p: first party p: path_provider Plugin that provides resolved paths to some platform directories labels Mar 3, 2022
@gaaclarke
Copy link
Member

what if we just tried calling InvokeDartPluginRegistrantIfAvailable on every single library until one works?

One problem with that is we could face naming collisions with _PluginRegistrant. It would already be a bummer to have to accept the performance cost, but we'd have to try to limit the chance of collisions. I guess if we only did that logic when the executable is obfuscated it would only collide if there was a @pragma('vm:entry-point').

I'll wait to hear back about dart-lang/sdk#44215 from @alexmarkov since that's the ideal solution considering it has come up a few times already.

@alexmarkov
Copy link
Contributor

Unfortunately I'm not aware of any workarounds. We should just fix the obfuscation (dart-lang/sdk#44215).

@stuartmorgan
Copy link
Contributor

@alexmarkov How quickly could we get that done? This is a non-trivial regression, so we need a solution in the short term.

@alexmarkov
Copy link
Contributor

@mraleph might know better as he implemented the obfuscation.

@mraleph
Copy link
Member

mraleph commented Mar 4, 2022

The fix should be fairly straightforward, can be done in pure Dart. See ObfuscationProhibitionsVisitor - it should probably just be changed to add URIs of libraries containing entry points to the list of protected names.

@mraleph
Copy link
Member

mraleph commented Mar 8, 2022

I asked @jensjoha to look at this on the Dart side.

copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Mar 14, 2022
flutter/flutter#99477
#44215

TEST=Test was added.

Change-Id: I585da436f9692d7f2e08db7f74c1a71ad09a1cb1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/236220
Reviewed-by: Slava Egorov <vegorov@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
@stuartmorgan
Copy link
Contributor

It looks like a fix has landed on the Dart side, and I believe it's rolled all the way through Flutter at this point. Is this still reproducible on latest master?

@mraleph
Copy link
Member

mraleph commented Mar 18, 2022

I am gonna close given that @jensjoha has tested with Flutter and says it works.

@tgucio please verify and let us know if it does not work.

@mraleph mraleph closed this as completed Mar 18, 2022
@tgucio
Copy link
Contributor Author

tgucio commented Mar 18, 2022

Verified by running a binary built with flutter build windows --obfuscate --split-debug-info=build\app\symbols\windows using Flutter master 2.13.0-0.0.pre.142 and it starts correctly.

Thanks a lot!

@maheshmnj maheshmnj added the r: fixed Issue is closed as already fixed in a newer version label Mar 18, 2022
@github-actions
Copy link

github-actions bot commented Apr 1, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2022
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: regression It was better in the past than it is now engine flutter/engine repository. See also e: labels. found in release: 2.11 Found to occur in 2.11 has reproducible steps The issue has been confirmed reproducible and is ready to work on P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. r: fixed Issue is closed as already fixed in a newer version
Projects
None yet
Development

No branches or pull requests

6 participants