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

Fix returning negative i64 values on web target #3796

Merged
merged 6 commits into from
May 3, 2023

Conversation

kajacx
Copy link
Contributor

@kajacx kajacx commented Apr 20, 2023

Description

Fixes #3773, which is a bug where returning a negative i64 from a module caused a crash when wasmer itself was running inside the browser using the js feature.

Looking at js-sys's implementation of TryFrom, it looks like BigInt is converted to JsValue anyway before converting it further, so converting BigInt to JsValue just so it is converted back seems like extra work.

To be honest, this needs an automated test, but I don't really know to make one. Can node load wasmer with the js feature? That is worth looking into.

Copy link
Member

@syrusakbary syrusakbary left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add tests verifying that the issue is fixed?

@kajacx
Copy link
Contributor Author

kajacx commented Apr 21, 2023

Can we add tests verifying that the issue is fixed?

Yes, I wanted to do that, but I do know where to begin. I am looking into the tests folder in the repo, but I don't see any tests for the js web target that I could easily add too.

The closest file I could find is types_functions.rs, but I do not know if the automated test suit already runs this with the js feature or not.

@kajacx kajacx force-pushed the fix-negative-i64-return-on-web branch from 3adb439 to fe3d307 Compare April 21, 2023 18:23
@kajacx
Copy link
Contributor Author

kajacx commented Apr 21, 2023

Ok, I have tried adding this test. It is loading the WASM plugin from a wat text just like the other tests, which makes it simpler to work with.

The plugin exports a function that adds one, then calls the imported "add one" function and then adds one again to add three in total. This tests back and forth exchanging of values between the plugin and the host. Same idea as I did with fp-bindgen.

I am testing it with interesting values that might overflow (near 0, i64::MIN and i64::MAX).

I have verified that this test fails on the current master branch but succeeds with this fix.

But what I am missing is a way to run this automatically. It seems that node can run WASM, but I will have to try that.

@syrusakbary
Copy link
Member

syrusakbary commented Apr 23, 2023

Cool, feel free to add the test inside lib/api/tests/import_function.rs (you will need to replace #[wasm_bindgen] with #[universal_test]). Also, try to make the test as generic as possible (no wasm bindgen or js specific stuff such as console_error_panic_hook, as that's done automatically by the universal test)

You can also that the test is working by running make test-js-api @kajacx

@kajacx kajacx force-pushed the fix-negative-i64-return-on-web branch from 308ef91 to 6f0ea57 Compare April 23, 2023 09:05
@kajacx
Copy link
Contributor Author

kajacx commented Apr 23, 2023

Cool, feel free to add the test inside lib/api/tests/import_function.rs (you will need to replace #[wasm_bindgen] with #[universal_test]). Also, try to make the test as generic as possible (no wasm bindgen or js specific stuff such as console_error_panic_hook, as that's done automatically by the universal test)

You can also that the test is working by running make test-js-api @kajacx

Ok, I have tried that, but I cannot get it to work. There seems to be some problem with i64 values on node. I checked out the current master, and ran the make-test-api command, but it gave the following error: (full error message)

---- module::calling_host_functions_with_negative_values_works_js output ----
    error output:
        panicked at 'called `Result::unwrap()` on an `Err` value: "RuntimeError { source: Js(JsValue(TypeError: wasm function signature contains illegal type
TypeError: wasm function signature contains illegal type

After doing some searching, I have found this article about bigint support in node, but I have not figures out how to pass the --experimental-wasm-bigint flag to node. Any help @syrusakbary? Thanks.

@syrusakbary
Copy link
Member

Tests passed in CI so I suspect that you may have an old version of Node installed in your system @kajacx

@kajacx
Copy link
Contributor Author

kajacx commented Apr 27, 2023

I am ill right now, so I will not be doing anything on the computer for a few weeks. You can merge the PR if it looks good, I will re-check it to my satisfaction later either way.

@ptitSeb ptitSeb added this to the v3.3 milestone May 2, 2023
@ptitSeb
Copy link
Contributor

ptitSeb commented May 2, 2023

@syrusakbary it seems ready to be merged?

@syrusakbary
Copy link
Member

Yup, good to merge!

@ptitSeb ptitSeb enabled auto-merge (squash) May 3, 2023 06:57
@syrusakbary syrusakbary disabled auto-merge May 3, 2023 06:58
@syrusakbary syrusakbary merged commit 2f61755 into wasmerio:master May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Passing i64 around always fails with the js feature
3 participants