I encountered an issue while working on my first Rust-produced WASM, and I am unsure how to debug it.
wasm-000650c2-23:340 Uncaught RuntimeError: memory access out of bounds
at dlmalloc::dlmalloc::Dlmalloc::free::h36961b6fbcc40c05 (wasm-function[23]:670)
at __rdl_dealloc (wasm-function[367]:8)
at __rust_dealloc (wasm-function[360]:7)
at alloc::alloc::dealloc::h90df92e1f727e726 (wasm-function[146]:100)
at <alloc::alloc::Global as core::alloc::Alloc>::dealloc::h7f22ab187c7f5835 (wasm-function[194]:84)
at <alloc::raw_vec::RawVec<T, A>>::dealloc_buffer::hdce29184552be976 (wasm-function[82]:231)
at <alloc::raw_vec::RawVec<T, A> as core::ops::drop::Drop>::drop::h3910dccc175e44e6 (wasm-function[269]:38)
at core::ptr::real_drop_in_place::hd26be2408c00ce9d (wasm-function[267]:38)
at core::ptr::real_drop_in_place::h6acb013dbd13c114 (wasm-function[241]:50)
at core::ptr::real_drop_in_place::hb270ba635548ab74 (wasm-function[69]:192)
The scenario is as follows: I am using the latest Chrome browser, with Rust wasm-bindgen code that is being called from a TypeScript custom element. This operation involves a canvas within the shadow DOM, where the data rendered to the canvas comes from an HTML5 AudioBuffer. All Rust variables are locally scoped.
When there is only one instance of the web component present in the document, everything works perfectly fine. However, when multiple instances are added, a stack trace similar to the one above is generated. The code executes without any other issues.
I am aware of the existing memory bugs in Chrome - could this be a manifestation of those bugs, or does it appear unusual to experienced Rust/WASM developers?
js-sys = "0.3.19"
wasm-bindgen = "0.2.42"
wee_alloc = { version = "0.4.2", optional = true }
[dependencies.web-sys]
version = "0.3.4"
The Rust code itself is relatively small and simply renders two channels of an AudioBuffer to a supplied HTMLCanvasElement:
#[wasm_bindgen]
pub fn render(
canvas: web_sys::HtmlCanvasElement,
audio_buffer: &web_sys::AudioBuffer,
stroke_style: &JsValue,
line_width: f64,
step_size: usize,
) {
// ...
let mut channel_data: [Vec<<f32>; 2] = unsafe { std::mem::uninitialized() }; // !
for channel_number in 0..1 {
channel_data[channel_number] = audio_buffer
.get_channel_data(channel_number as u32)
.unwrap();
}
// ...
I have attempted debugging by commenting out parts of the functionality. If the code does not manipulate the canvas but includes the aforementioned section, the error occurs. Making a slight change results in a straightforward 'out of wam memory' error. The size of the audio file is 1,200k.
let channel_data: [Vec<<f32>; 2] = [
audio_buffer.get_channel_data(0).unwrap(),
audio_buffer.get_channel_data(1).unwrap()
];
EDIT: I was initially puzzled by the out of memory
error message that arose after making the correction above. However, it turns out to be a known Chrome bug.