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

Shrink JS-allocated strings down to the correct size before passing them to Rust #3808

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
* Deprecate `wasm_bindgen_test_configure!`s `run_in_worker` in favor of `run_in_dedicated_worker`.
[#3804](https://github.com/rustwasm/wasm-bindgen/pull/3804)

### Fixed

* Fixed UB when freeing strings received from JS if not using the default allocator.
[#3808](https://github.com/rustwasm/wasm-bindgen/pull/3808)

## [0.2.90](https://github.com/rustwasm/wasm-bindgen/compare/0.2.89...0.2.90)

Released 2024-01-06
Expand Down
6 changes: 1 addition & 5 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,11 +1296,6 @@ impl<'a> Context<'a> {
mem = mem,
);

// TODO:
// When converting a JS string to UTF-8, the maximum size is `arg.length * 3`,
// so we just allocate that. This wastes memory, so we should investigate
// looping over the string to calculate the precise size, or perhaps using
// `shrink_to_fit` on the Rust side.
self.global(&format!(
"function {name}(arg, malloc, realloc) {{
{debug}
Expand All @@ -1314,6 +1309,7 @@ impl<'a> Context<'a> {
const ret = encodeString(arg, view);
{debug_end}
offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
Copy link

Choose a reason for hiding this comment

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

Could add a check for whether len != offset before reallocing again, to avoid spurious allocs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I considered that, but I didn't think it'd be worth it since in the vast majority of cases the string won't take up the maximum amount of space anyway.

I'd be fine to add it if you want but I don't think the difference would be significant either way.

}}

WASM_VECTOR_LEN = offset;
Expand Down
1 change: 1 addition & 0 deletions crates/cli/tests/reference/string-arg.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function passStringToWasm0(arg, malloc, realloc) {
const ret = encodeString(arg, view);

offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}

WASM_VECTOR_LEN = offset;
Expand Down