webassembly icon indicating copy to clipboard operation
webassembly copied to clipboard

multiple uses of resize: memory access out of bounds

Open pbreah opened this issue 4 years ago • 1 comments

After doing a few tests I found this problem that only happens from the 2nd use of resize() of the wasm-image-loader.

First of all to give some context of the use case:

I am loading the module so I can resize an array of images. I init the worker once then attempt to process all images. The first iteration everything works (decoding & resize), then on the 2nd image (decoding works, but not resizing) it crashes. It always happens on the second image. I followed the same pattern as the example:

    const decoded = imageLoader.decode(arrBuff, arrBuff.length, 3);
    const decodedAndWrapped = new Uint8Array(decoded);
    const { width, height, channels } = imageLoader.dimensions();
    const resized = new Uint8Array(imageLoader.resize(decodedAndWrapped, width, height, channels, newWidth, newHeight));

When the code runs the 2nd time, it gives this error:

Uncaught RuntimeError: memory access out of bounds
    at z (<anonymous>:wasm-function[25]:0x6a5)
    at <anonymous>:wasm-function[127]:0x8dd8
    at <anonymous>:wasm-function[162]:0xd3d2
    at Object.resize (eval at new_ (https://unpkg.com/@saschazar/[email protected]/wasm_image_loader.js:9:23577), <anonymous>:13:10)
    at onmessage (http://localhost:3000/c9731d57f6cbc3fb24f3.worker.js:146:42)

All of this is on the browser: Chrome 90.

I have tried calling free(), but that completely destroys the instance. Here is the weird part: when I use the original image buffer again - not the decoded one with decode() - on the resize() function first parameter, it works - but it produces an image with a larger size than the original and pixels distorted (image looks pretty bad).

Hope you are able to reproduce the issue, to see what can be done or what else is causing this. I noticed on your test suite you have a test case loading a single image from unsplash then resizing, but I guess resizing multiple images with the same instance is a different case. I look forward to your response. Thanks.

pbreah avatar Jun 05 '21 01:06 pbreah

Hi @pbreah - yes I also came across this issue. However, I haven't found a solution yet. I might have missed freeing memory somewhere in the C++ source code.

I mostly overcome this issue using completely re-instantiating the WebAssembly module within a Worker.

saschazar21 avatar Jun 22 '21 11:06 saschazar21