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 JS<->WASM string marshaling crash #42486

Merged
merged 2 commits into from
Sep 19, 2020
Merged

Conversation

kg
Copy link
Member

@kg kg commented Sep 19, 2020

Under some circumstances passing strings across the JS<->WASM boundary will crash or corrupt memory. We also currently truncate strings at the first embedded null, which is wrong. This PR fixes both.

kg added 2 commits September 18, 2020 18:36
Fix passing strings across the boundary
Fix JS strings being truncated at the first null when passed to mono
@kg kg requested a review from marek-safar as a code owner September 19, 2020 01:53
@kg kg added the Servicing-consider Issue for next servicing release review label Sep 19, 2020
@kg
Copy link
Member Author

kg commented Sep 19, 2020

This likely fixes #41604 along with another non-github-tracked issue involving large strings.

@kg kg added arch-wasm WebAssembly architecture and removed Servicing-consider Issue for next servicing release review labels Sep 19, 2020
Copy link
Contributor

@CoffeeFlux CoffeeFlux left a comment

Choose a reason for hiding this comment

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

Mostly LGTM. Thanks a lot!

src/mono/wasm/runtime/binding_support.js Show resolved Hide resolved
var buffer = Module._malloc ((string.length + 1) * 2);
var buffer16 = (buffer / 2) | 0;
for (var i = 0; i < string.length; i++)
Module.HEAP16[buffer16 + i] = string.charCodeAt (i);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Is this file intended to follow Mono conventions? If so, space before the [ here and the next line.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is, though I don't know if we follow that convention for the JS. I can make it match.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll correct this in the larger bindings optimization PR, didn't want to let a formatting change delay the merge on this one

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't string[i] access generally faster than charCodeAt ?

Copy link
Member Author

Choose a reason for hiding this comment

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

string[i] in JS returns a single-character string

src/mono/wasm/runtime/driver.c Show resolved Hide resolved
@CoffeeFlux CoffeeFlux added this to the 5.0.0 milestone Sep 19, 2020
@kg kg merged commit ddc83ad into dotnet:master Sep 19, 2020
@kg
Copy link
Member Author

kg commented Sep 19, 2020

/backport to release/5.0-rc2

@github-actions
Copy link
Contributor

Started backporting to release/5.0-rc2: https://github.com/dotnet/runtime/actions/runs/262527073

@github-actions
Copy link
Contributor

@kg backporting to release/5.0-rc2 failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix passing mono object ptrs to bound functions Fix passing strings across the boundary Fix JS strings being truncated at the first null when passed to mono
Applying: Add new string conv wrapper
error: sha1 information is lacking or useless (src/mono/wasm/runtime/binding_support.js).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0002 Add new string conv wrapper
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@lewing
Copy link
Member

lewing commented Sep 21, 2020

This adds another allocation+copy for that marshal case, what was the failure case before?

@kg
Copy link
Member Author

kg commented Sep 21, 2020

Random out-of-bounds memory accesses / memory corruptions, and truncation at the first null

lewing added a commit to lewing/runtime that referenced this pull request Sep 23, 2020
kg added a commit to kg/runtime that referenced this pull request Sep 24, 2020
Fix JS strings being truncated at the first null when passed to mono
Fix crashes when moving large strings across the JS<->WASM boundary
lewing added a commit that referenced this pull request Sep 24, 2020
* Fix an api regression introduced in #42486

* Update src/mono/wasm/runtime/binding_support.js

a number it is

Co-authored-by: Ankit Jain <[email protected]>

Co-authored-by: Ankit Jain <[email protected]>
github-actions bot pushed a commit that referenced this pull request Sep 30, 2020
Fix JS strings being truncated at the first null when passed to mono
Fix crashes when moving large strings across the JS<->WASM boundary
marek-safar pushed a commit that referenced this pull request Oct 9, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-Interop-mono
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants