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

dart2wasm fails on Random.secure().nextInt() #55031

Closed
konsultaner opened this issue Feb 27, 2024 · 7 comments
Closed

dart2wasm fails on Random.secure().nextInt() #55031

konsultaner opened this issue Feb 27, 2024 · 7 comments
Assignees
Labels
area-dart2wasm Issues for the dart2wasm compiler.

Comments

@konsultaner
Copy link

konsultaner commented Feb 27, 2024

At the moment it is not possible to use the Random.secure().nextInt() method in conjunction with dart2wasm.

import 'dart:math';

import 'package:test/test.dart';

void main() {
  group('Dart2WASM', () {
    test('Random secure issue', () async {
      expect(Random.secure().nextInt(256), 2);
    });
  });
}

this test running with -p chrome -c dart2wasm

will output:

unparsed  unparsed      at _SecureRandom._getBytes (http://localhost:38411/iGXEHWjpHrlKzYPYwWHrFGDKxWMbPDMS/test/issue_test.dart.browser_test.dart.wasm:wasm-function[1593]:0x56673)
unparsed  unparsed      at new _SecureRandom (constructor body) (http://localhost:38411/iGXEHWjpHrlKzYPYwWHrFGDKxWMbPDMS/test/issue_test.dart.browser_test.dart.wasm:wasm-function[1615]:0x570bd)
unparsed  unparsed      at _SecureRandom (http://localhost:38411/iGXEHWjpHrlKzYPYwWHrFGDKxWMbPDMS/test/issue_test.dart.browser_test.dart.wasm:wasm-function[1613]:0x570b0)
unparsed  unparsed      at Random._secureRandom (http://localhost:38411/iGXEHWjpHrlKzYPYwWHrFGDKxWMbPDMS/test/issue_test.dart.browser_test.dart.wasm:wasm-function[1612]:0x57091)
unparsed  unparsed      at new Random.secure (http://localhost:38411/iGXEHWjpHrlKzYPYwWHrFGDKxWMbPDMS/test/issue_test.dart.browser_test.dart.wasm:wasm-function[822]:0x433b1)
unparsed  unparsed      at main closure at file://##############.dart:7:61 inner (http://localhost:38411/iGXEHWjpHrlKzYPYwWHrFGDKxWMbPDMS/test/issue_test.dart.browser_test.dart.wasm:wasm-function[819]:0x43335)
unparsed  unparsed      at main closure at file://##############.dart:7:61 (http://localhost:38411/iGXEHWjpHrlKzYPYwWHrFGDKxWMbPDMS/test/issue_test.dart.browser_test.dart.wasm:wasm-function[812]:0x4321c)

NoSuchMethodError: method not found: '_getBytes'
Receiver: null
Arguments: []

@parlough parlough added the area-dart2wasm Issues for the dart2wasm compiler. label Feb 27, 2024
@konsultaner
Copy link
Author

konsultaner commented Feb 28, 2024

I'm not a pro for writing wasm interop code but it seems like

external static int _getBytes(int count);

is missing a

@pragma("wasm:import", "get_random_bytes")

I found this docu about the wasm random:

https://github.com/WebAssembly/wasi-random?tab=readme-ov-file#api-walk-through

Also there are a lot of todos in the math class. Is it fully supported yet?

@mkustermann You seem to be working on wasm. I would contribute, but it's quite hard to get into the code. Is there a guide how the wasm binding works with dart?

@mkustermann
Copy link
Member

Thank you for the bug report, @konsultaner !

This is a bug: It hasn't been implemented yet (the only external method in that patch that has no implementation) - it was probably missed.

We have a test for this: https://github.com/dart-lang/sdk/blob/main/tests/lib/math/random_secure_test.dart which is failing on dart2wasm atm.

Also there are a lot of todos in the math class. Is it fully supported yet?

Those TODOs aren't critical to address (in fact they come from the VM backend - which the wasm version was based on - I believe)

Generally most things should be working in dart2wasm. There's a few things that aren't quite there yet (e.g. edge cases in async, #55025), which we'll work on in the coming weeks/months.

Though please notice that dart2wasm is still experimental

@pragma("wasm:import", "get_random_bytes")

Our main target for dart2wasm is the browser atm, which doesn't have WASI support. I think maybe something like this will do the trick.

/cc @osa1 Could you implement Random.secure()?

@osa1
Copy link
Member

osa1 commented Mar 6, 2024

Implemented in https://dart-review.googlesource.com/c/sdk/+/356040.

@konsultaner
Copy link
Author

@osa1 Should I close this issue as you implemented it?

@osa1
Copy link
Member

osa1 commented Mar 7, 2024

@konsultaner it's not ready yet. The issue will be automatically closed when it's merged.

@konsultaner
Copy link
Author

Implemented in https://dart-review.googlesource.com/c/sdk/+/356040

@osa1 which version of dart will have it included?

@osa1
Copy link
Member

osa1 commented Apr 9, 2024

@konsultaner the commit 1285976 was tagged as 3.4.0-dev so it will be included in 3.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart2wasm Issues for the dart2wasm compiler.
Projects
None yet
Development

No branches or pull requests

4 participants