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

Workaround for a TextDecoder bug in Safari causing a RangeError to be thrown #4472

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bes
Copy link

@bes bes commented Apr 6, 2025

TextDecoder in Safari has a limitation that causes it to throw RangeError after decoding more than 2GiB of data. This causes long running wasm programs that need to use TextDecoder to crash and start throwing RuntimeError with the message "Out of bounds memory access".

We work around the issue by tracking how much data has been decoded by any given TextDecoder, and replace it when it comes close to 2GiB, deducting a small margin of 1MiB which has been empirically shown to reduce the likelihood of miscounting (for unknown reasons) causing a RangeError to be thrown.

This commit also adds stricter handling of the kind of declaration used for TextDecoder and TextEncoder - TextDecoder always uses let because it needs to be mutable, and TextEncoder always uses const because it doesn't need to be mutable.

Fixes #4471

@@ -11,16 +11,16 @@
(type (;9;) (func (param i32 f64)))
(import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0)))
(func $__wbindgen_realloc (;1;) (type 8) (param i32 i32 i32 i32) (result i32))
(func $__wbindgen_malloc (;2;) (type 6) (param i32 i32) (result i32))
Copy link
Author

Choose a reason for hiding this comment

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

I am not sure why I needed these changes. I am using rustc 1.86.0, maybe I should be using another version?

@bes bes force-pushed the fix-safari-textdecoder-bug branch from 06346fa to 1a75ba6 Compare April 6, 2025 17:06
@bes
Copy link
Author

bes commented Apr 6, 2025

Some unrelated clippy lints are failing, has CI rustc been updated without updating the code?

@bes
Copy link
Author

bes commented Apr 7, 2025

There don't seem to be any tests (generated .js-files) that use the non-browser path of getText 🤔

… thrown

`TextDecoder` in Safari has a limitation that causes it to throw `RangeError`
after decoding more than 2GiB of data. This causes long running wasm programs
that need to use `TextDecoder` to crash and start throwing `RuntimeError` with
the message "Out of bounds memory access".

We work around the issue by tracking how much data has been decoded by any
given `TextDecoder`, and replace it when it comes close to 2GiB, deducting
a small margin of 1MiB which has been empirically shown to reduce the
likelihood of miscounting (for unknown reasons) causing a `RangeError` to
be thrown.

This commit also adds stricter handling of the kind of declaration used for
TextDecoder and TextEncoder - TextDecoder always uses let because it needs
to be mutable, and TextEncoder always uses const because it doesn't need to
be mutable.

Fixes rustwasm#4471
@bes bes force-pushed the fix-safari-textdecoder-bug branch from 1a75ba6 to 20a27e6 Compare April 7, 2025 14:16
@stephanemagnenat
Copy link

Some unrelated clippy lints are failing, has CI rustc been updated without updating the code?

@daxpedda do you have any input on these? The PR works around a critical bug on Safari and would be very useful for many people!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants