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

memory access out of bounds error when allocating strings in WebAssembly #47194

Closed
jsonnull opened this issue Jan 4, 2018 · 1 comment
Closed

Comments

@jsonnull
Copy link

jsonnull commented Jan 4, 2018

I'm working with passing strings between Rust and JavaScript on the wasm32-unknown-unknown target, but I'm consistently getting memory access out of bounds panics in Rust. I'm not able to determine if Rust or my code is at fault.

Here's a simple demonstration of the problem.

A short overview of what's going on in my failing example:

  • The module is built by webpack and the Rust build is invoked by rust-native-wasm-loader.
  • In the browser console there are console logs showing the first Rust function is invoked from JavaScript, which requires an allocation for the input string pointer, and an allocation for the string output pointer—there are JavaScript calls for the input alloc and both the input and output dealloc, if that matters.
  • There's a console log for the second Rust function invoked by the JavaScript side, and it's somewhere in here that the memory access out of bounds error occurs. Here's the full error message:
Uncaught (in promise) RuntimeError: memory access out of bounds
    at wasm-function[78]:192
    at wasm-function[1]:79
    at toUppercase (webpack:///./src/index.js?:30:42)
    at eval (webpack:///./src/index.js?:34:41)
    at <anonymous>
  • This is in release mode. Change release to false in webpack.config.js for a debug build. This time there's no console logs and an error appears immediately:
Uncaught (in promise) RuntimeError: function signature mismatch
    at core::fmt::write::hea786a1481adb9e0 (wasm-function[392]:3395)
    at core::fmt::Write::write_fmt::h3f7ec2411e6ba1c8 (wasm-function[164]:262)
    at std::panicking::begin_panic_fmt::h3fe5ef599a9ea351 (wasm-function[245]:318)
    at rust_begin_unwind (wasm-function[244]:86)
    at core::panicking::panic_fmt::h1bceab3183b318fb (wasm-function[442]:588)
    at core::panicking::panic::he74d65cc02d05038 (wasm-function[440]:207)
    at <core::option::Option<T>>::unwrap::h286d6daff2767775 (wasm-function[52]:87)
    at <alloc::raw_vec::RawVec<T, A>>::allocate_in::h7e6e3ac111344e5e (wasm-function[19]:372)
    at <alloc::raw_vec::RawVec<T>>::with_capacity::h81415bd27391e5af (wasm-function[18]:60)
    at <alloc::vec::Vec<T>>::with_capacity::hebebed3340b32776 (wasm-function[28]:54)
    at alloc (wasm-function[73]:69)
    at Prelude.createString (webpack:////wasm-rust-utils/packages/wasm-rust-utils/dist/wasm-rust-utils.js?:93:24)
    at opposite (webpack:///./src/index.js?:19:25)
    at eval (webpack:///./src/index.js?:25:42)
    at <anonymous>

I'm totally lost as to how to proceed, on the surface it seems that an arbitrary amount of allocations in Rust cause the program to fail in release mode, and there's some other issue in debug mode.

Could this be at all related to #46367 ?

@jsonnull
Copy link
Author

jsonnull commented Jan 6, 2018

I've figured out all the issues I was running into, which can pretty much all be summed up as calling CString::from_raw without fully understanding the underlying semantics.

The issue with building in debug mode has existed in other reports and seems to be LLVM related.

@jsonnull jsonnull closed this as completed Jan 6, 2018
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

No branches or pull requests

1 participant