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

Failure to run simple app compiled to WASM with error about identityHashCode #148617

Closed
Rexios80 opened this issue May 18, 2024 · 25 comments
Closed
Assignees
Labels
e: wasm Issues related to the wasm build of Flutter Web. found in release: 3.22 Found to occur in 3.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on platform-web Web applications specifically r: fixed Issue is closed as already fixed in a newer version team-web Owned by Web platform team

Comments

@Rexios80
Copy link
Contributor

Rexios80 commented May 18, 2024

Steps to reproduce

  1. Clone https://github.com/Rexios80/rexios.dev
  2. Checkout the branch feature/wasm-support
  3. Run flutter run --wasm (must be on flutter master, or do it the hard way on stable)
  4. See the error in the web debugger

Expected results

No exception

Actual results

The following error:

Uncaught (in promise) CompileError: WebAssembly.compileStreaming(): Compiling function #1263:"identityHashCode" failed: type error in return[0] (expected i64, got (ref null 103)) @+1479968

Code sample

https://github.com/Rexios80/rexios.dev

There isn't much there

Screenshots or Video

No response

Logs

Web debugger:

Uncaught (in promise) CompileError: WebAssembly.compileStreaming(): Compiling function #1263:"identityHashCode" failed: type error in return[0] (expected i64, got (ref null 103)) @+1479968

Nothing in the Flutter logs

Flutter Doctor output

Doctor output
[✓] Flutter (Channel main, 3.22.0-36.0.pre.49, on macOS 14.5 23F79 darwin-arm64,
    locale en-US)
    • Flutter version 3.22.0-36.0.pre.49 on channel main at
      /Users/rexios/repos/flutter
    • Upstream repository git@github.com:flutter/flutter.git
    • Framework revision f7012db828 (63 minutes ago), 2024-05-18 10:41:56 -0700
    • Engine revision 552a965b70
    • Dart version 3.5.0 (build 3.5.0-167.0.dev)
    • DevTools version 2.36.0-dev.10

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/rexios/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio
      Preview.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version unknown)
    • 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 determine Android Studio version.
    • Java version OpenJDK Runtime Environment (build
      17.0.10+0-17.0.10b1087.21-11609105)

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

[✓] Connected device (4 available)
    • Aaron’s iPhone (mobile)         • 00008130-001C35CE3A20001C • ios
      • iOS 17.5 21F79
    • macOS (desktop)                 • macos                     • darwin-arm64
      • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad     • darwin
      • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                    •
      web-javascript • Google Chrome 124.0.6367.208
    ! Error: Browsing on the local area network for Raj's Apple Vision Pro.
      Ensure the device is unlocked and attached with a cable or associated with
      the same local area network as this Mac. (code -27)
    ! Error: Browsing on the local area network for Joe’s iPhone. Ensure the
      device is unlocked and attached with a cable or associated with the same
      local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code
      -27)

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
@huycozy huycozy added the in triage Presently being triaged by the triage team label May 20, 2024
@huycozy
Copy link
Member

huycozy commented May 20, 2024

Hi @Rexios80
I checked this on a new Flutter project without any additional plugins/packages and I didn't see the issue. Steps I did (following docs on https://docs.flutter.dev/platform-integration/web/wasm):

  1. Build with wasm: flutter build web --wasm
  2. activate dhttpd: flutter pub global activate dhttpd
  3. Hosting the web: dhttpd '--headers=Cross-Origin-Embedder-Policy=credentialless;Cross-Origin-Opener-Policy=same-origin'

I tried the same on your project but it built with a long error. Since your project is using many dependencies, please narrow it down to see if one of them may cause this. Thanks!

@huycozy huycozy added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2024
@Rexios80
Copy link
Contributor Author

I've narrowed it down to pub_api_client, but I'm not sure it's that package's fault either. There are no references to identityHashCode in that code base. There are other packages with weird issues as well:

That package has perfectly valid code that is failing to compile to WASM. There isn't anything to work off of from either the Flutter debugger or the web dev tools besides that cryptic error without any other information.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2024
@huycozy
Copy link
Member

huycozy commented May 21, 2024

I've narrowed it down to pub_api_client, but I'm not sure it's that package's fault either.

Does that mean the issue disappeared after you removed it from project? You can also check Requires JS-interop to access browser and JS APIs to filter out which packages haven't been migrated to support wasm compilation yet.

@huycozy huycozy added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 21, 2024
@Rexios80
Copy link
Contributor Author

If it was a web package migration issue wouldn't the app not even compile? This is a runtime error.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 21, 2024
@huycozy
Copy link
Member

huycozy commented May 21, 2024

I will mark this as an issue with flutter run --wasm to get insights from others. Reproduced the error above WebAssembly.compileStreaming() on the latest master channel with given sample code.

flutter doctor -v
[!] Flutter (Channel master, 3.22.0-39.0.pre.1, on macOS 14.1 23B74 darwin-x64, locale en-VN)
    • Flutter version 3.22.0-39.0.pre.1 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 165e535474 (3 hours ago), 2024-05-20 16:52:15 -0700
    • Engine revision c2ef01f6f1
    • Dart version 3.5.0 (build 3.5.0-172.0.dev)
    • DevTools version 2.36.0-dev.10
    • 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 34.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-34, build-tools 34.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 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode15.3.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2)
    • 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
    • android-studio-dir = /Applications/Android Studio.app/
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

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

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 14.1 23B74 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 125.0.6422.61

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

@huycozy huycozy added platform-web Web applications specifically has reproducible steps The issue has been confirmed reproducible and is ready to work on e: wasm Issues related to the wasm build of Flutter Web. team-web Owned by Web platform team found in release: 3.22 Found to occur in 3.22 and removed in triage Presently being triaged by the triage team labels May 21, 2024
@mraleph
Copy link
Member

mraleph commented May 22, 2024

cc @mkustermann @osa1 do you recognize the issue with identityHashCode being miscompiled?

@osa1
Copy link

osa1 commented May 22, 2024

cc @mkustermann @osa1 do you recognize the issue with identityHashCode being miscompiled?

I think this is probably new, I can debug this.

If it was a web package migration issue wouldn't the app not even compile? This is a runtime error.

This is a type error in the generated Wasm file, and type errors in the generated code are always a compiler bug, dart2wasm should never generate ill-typed Wasm. It happens in runtime because compilation of the generated Wasm happens in runtime.

We could actually catch these kinds of errors in compile time by validating the generated code right after generating it, in compile time. @mkustermann any thoughts on this?

@osa1 osa1 self-assigned this May 22, 2024
@Rexios80
Copy link
Contributor Author

Should I make a separate issue for this one? flyerhq/flutter_chat_ui#596

@Rexios80
Copy link
Contributor Author

I'm also having a bunch of issues with type casting with JSON deserialization. This code works without running on WASM. Are those issues I should report as well or are they expected?

@leoafarias
Copy link
Contributor

leoafarias commented May 22, 2024

The issue seems to occur on version 3.22.0-42.0.pre.6, but I was not able to replicate it on the latest stable 3.22.0.

I created a reproducible example using the dart_mappable package.
https://github.com/leoafarias/reproducible_dart_mappable_wasm

cc @osa1 @mraleph

@osa1
Copy link

osa1 commented May 22, 2024

Should I make a separate issue for this one? flyerhq/flutter_chat_ui#596

@Rexios80 Yes, that's also a dart2wasm bug and a separate bug.

I think these should be reported in the SDK repo as they're not Flutter bugs.

I'm also having a bunch of issues with type casting with JSON deserialization. This code works without running on WASM. Are those issues I should report as well or are they expected?

I can't tell without seeing the program and the output. When in doubt please just report.

The issue seems to occur on version 3.22.0-42.0.pre.6, but I was not able to replicate it on the latest stable 3.22.0.

@leoafarias I wonder if this was fixed with dart-lang/sdk@0299c05481c. Is this commit included in the fixed version?

@leoafarias
Copy link
Contributor

@osa1 Is there a commit with this change on flutter, and I can test it

@osa1
Copy link

osa1 commented May 22, 2024

@leoafarias I don't know how the SDK dependency is updated in Flutter repo so I'm not sure.

Basically dart2wasm between dart-lang/sdk@9f075b1 and dart-lang/sdk@0299c05481c is buggy, so we should check if the buggy version uses dart2wasm between those two commits.

I can't see the tag 3.22.0-36.0.pre.49 in https://github.com/flutter/flutter/tags.. Where do I get that tag?

@leoafarias
Copy link
Contributor

That tag just comes from the latest on master.

This is the version of the Dart SDK, let me know if that helps?
Dart 3.5.0 (build 3.5.0-177.0.dev)

@osa1
Copy link

osa1 commented May 22, 2024

Dart 3.5.0 (build 3.5.0-177.0.dev)

Is this the version that works, or the version with the bug?

If it's the version that works, then this was probably an issue caused by the bug between the linked comments in my previous comment and it's probably fixed now.

If it's the version with the bug then this is probably a new issue and I will take a look.

@leoafarias
Copy link
Contributor

leoafarias commented May 22, 2024

@osa1

This version does not work: flutter/engine#52970

This version works: flutter/engine#52625

At least based on my tests.

@Rexios80
Copy link
Contributor Author

I do actually get the same error at compile time on Flutter master:
log.txt

@osa1
Copy link

osa1 commented May 23, 2024

I found the bug, but I don't understand why it happens.

Basically in this program Object.hashCode somehow returns a boxed int, instead of unboxed:

  (func $Object.hashCode (;1268;) (param $var0 (ref $#Top)) (result (ref null $_BoxedInt))
    (local $var1 i64)
    local.get $var0
    call $Object._objectHashCode
    local.set $var1
    i32.const 5
    local.get $var1
    struct.new $_BoxedInt               ;; <----- Why box here??
    return
    ref.null none)

And Object.identityHashCode code generator assumes that it will return unboxed.

This can be easily fixed but I'd like to understand why Object.hashCode returns boxed first. Since int can't be subtyped, and all overrides of hashCode will return int, I would expect Object.identityHashCode to return unboxed.

@osa1
Copy link

osa1 commented May 23, 2024

This seems to be a TFA bug. In this method:

    method hashValue(covariant-by-class final map3::MapperBase::T value, [final map2::MapperContainer? container = #C2]) → core::int {
      try {
        final map8::MappingContext context = new map8::MappingContext::•(container: container, args: () → core::List<core::Type> => typ12::TypePlus|get#args(value.{core::Object::runtimeType}{core::Type}).{core::Iterable::map}<core::Type>((final core::Type t) → core::Type => t =={core::Type::==}{(core::Object) → core::bool} #C99088 ?{core::Type} #C99083 : t){((core::Type) → core::Type) → core::Iterable<core::Type>}.{core::Iterable::toList}(){({growable: core::bool}) → core::List<core::Type>});
        return this.{map3::MapperBase::hash}(value, context){(map3::MapperBase::T, map8::MappingContext) → core::int};
      }
      on core::Object catch(final core::Object e, final core::StackTrace stacktrace) {
        core::Error::throwWithStackTrace(map4::MapperException::chain(#C99098, "[${value}]", e), stacktrace);
        e;
        stacktrace;
      }
    }

which I think is coming from:

https://github.com/schultek/dart_mappable/blob/4777e79cdc99e15730d18990e1d774edc0064b0a/packages/dart_mappable/lib/src/mappers/mapper_mixins.dart#L18

TFA infers the return type as int?, even though the Dart type is int:

    [@vm.inferred-return-type.metadata=dart.core::_BoxedInt?]
    [@vm.procedure-attributes.metadata=methodOrSetterCalledDynamically:false,getterCalledDynamically:false,hasThisUses:false,hasTearOffUses:false,methodOrSetterSelectorId:12475,getterSelectorId:12476]
    method hashValue([@vm.inferred-type.metadata=!] [@vm.inferred-arg-type.metadata=!] covariant-by-class final map2::MapperBase::T value, [[@vm.inferred-arg-type.metadata=library package:dart_mappable/src/mapper_container.dart::_MapperContainerBase?] final map::MapperContainer? container = #C43]) → core::int { ... }

@alexmarkov do you already know what the issue may be here or do you need a smaller repro? We tried to minimize this for a while, I can try more if you need a smaller repro.

copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue May 23, 2024
…pe is non-nullable

With sound null safety, implicit "return null" at the end of function
should be taken into account only if return type is nullable.

TEST=pkg/vm/testcases/transformations/type_flow/transformer/regress_flutter148617.dart

Issue: flutter/flutter#148617
Change-Id: I209fc9a3b10639fd4024d4b1807591498349e1f0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367980
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
@osa1
Copy link

osa1 commented May 24, 2024

This should be fixed by dart-lang/sdk@345b437.

@Rexios80
Copy link
Contributor Author

Can confirm a successful build/run on version:

Flutter 3.23.0-4.0.pre.6 • channel main • git@github.com:flutter/flutter.git
Framework • revision 8dd08314d2 (57 minutes ago) • 2024-05-24 07:26:25 -0400
Engine • revision 4ab4424752
Tools • Dart 3.5.0 (build 3.5.0-188.0.dev) • DevTools 2.36.0-dev.10

@alexmarkov
Copy link
Contributor

The fix rolled into Flutter in b3d8053.

@Rexios80
Copy link
Contributor Author

@alexmarkov Will this be included in the next hotfix?

@alexmarkov
Copy link
Contributor

Cherry-pick request for beta branch: dart-lang/sdk#55837. Please follow that issue for updates.

@Rexios80 Do you also need the fix in stable?

@huycozy huycozy added the r: fixed Issue is closed as already fixed in a newer version label May 27, 2024
@Rexios80
Copy link
Contributor Author

@alexmarkov I would think any WASM compilation bug fixes should be in stable

copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue May 29, 2024
…atic type is non-nullable

With sound null safety, implicit "return null" at the end of function
should be taken into account only if return type is nullable.

TEST=pkg/vm/testcases/transformations/type_flow/transformer/regress_flutter148617.dart

Issue: flutter/flutter#148617
Change-Id: Iebf68d4b279055fc2f902c36b5fb916bafe7bb8e
Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/367980
Cherry-pick-request: TBA
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368180
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e: wasm Issues related to the wasm build of Flutter Web. found in release: 3.22 Found to occur in 3.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on platform-web Web applications specifically r: fixed Issue is closed as already fixed in a newer version team-web Owned by Web platform team
Projects
None yet
Development

No branches or pull requests

6 participants