jedisct1 / libsodium.js

libsodium compiled to Webassembly and pure JavaScript, with convenient wrappers.
Other
981 stars 140 forks source link

Uncaught RangeError: WebAssembly.Instance(): Out of memory: wasm memory #301

Open KatarinaSimnova opened 2 years ago

KatarinaSimnova commented 2 years ago

Hi, when using function sodium.crypto_pwhash in webview on a mobile phone (tested on Samsung S21 and Pixel 3) we randomly get this error: “Uncaught RangeError: WebAssembly.Instance(): Out of memory: wasm memory”

According to this article: https://blog.stackblitz.com/posts/debugging-v8-webassembly/ the only way to solve this is to optimize the code to not instantiate too many wasm modules. Do you think it would be possible to optimize the code in this library and fix the bug on mobile devices?

jedisct1 commented 2 years ago

The pwhash function takes two parameters: the number of iterations, and how much memory to use.

If a runtime doesn't allow using enough memory to match the parameters used by your application, you can reduce the memory parameter, and bump the number of iterations up as a counterbalance.

gutjuri commented 1 year ago

I have the same issue. However, I can't change the parameters to the function in question since it's part of a dependency I use. Would it be possible to address this bug in this library?

jedisct1 commented 1 year ago

This is not a bug :)

If you're calling a function that requires reserving a lot of memory, but the browser doesn't allow reserving that much memory, there are no ways to work around this. Your application needs to use less memory.

gutjuri commented 1 year ago

Well, doing require("libsodium-wrappers"); in my JS code (which I run in node.js) is enough to provoke the above-mentioned error, so I don't really think that this is an issue with my code.

It may be worth mentioning that I only get the error when I run the code in an Intel SGX enclave (that has a size >8GB), so it may be related to that. However, I can't really investigate this because the error gives no stack trace.