Fix WASM entropy source when HEAPU8 is not exported - #265
Merged
Merged
Conversation
The Emscripten variant of entropy() accessed the WebAssembly memory through Module.HEAPU8. Current Emscripten versions no longer export HEAPU8 on the Module object by default, so the access failed, entropy() returned no data and chacha20_rng() aborted on its first use, for example when writing a database encrypted with the default cipher scheme. Use the HEAPU8 view directly. It is always in scope for EM_JS code and is kept up to date when the memory grows.
Owner
|
The change was an attempt to replace the call to |
utelle
added a commit
that referenced
this pull request
Sep 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Ulrich,
while testing the WebAssembly build, I noticed that encryption aborts on
mainas soon as random numbers are needed.wasm_crypto_getrandom()insrc/chacha20poly1305.c(added in 91b45ec) accesses the memory asModule.HEAPU8. Current Emscripten versions no longer exportHEAPU8on theModuleobject by default. The official v2.5.1 WASM package doesn't expose it there either. So the call fails,entropy()returns no data, andchacha20_rng()aborts. The release build only reportsAborted(); with-sASSERTIONSthe message is:This affects every cipher scheme that needs random numbers:
chacha20(the default),sqlcipher,ascon128andaegis. Onlyaes128cbc,aes256cbcandrc4, which don't use random numbers, keep working. The released v2.5.1 isn't affected, because it still usedgetentropy(). The same line is also on thehwaccelbranch.The change: use
HEAPU8directly instead ofModule.HEAPU8. InsideEM_JScode the view is always in scope, whether or not it's exported, and it is kept up to date when the memory grows.How I tested it
Emscripten 6.0.9 with Node. I rebuilt the WASM package the way the release workflow does (SQLite 3.53.4 sources, the patches from
packaging/wasm,make dist dist.build=oz), once frommainand once with this change. Then I wrote and read a database with each cipher scheme, each in its own process:aes128cbc,aes256cbc,rc4chacha20,sqlcipher,ascon128,aegisA small test program built directly with
emccgives the same results, for the default build (with and without-sASSERTIONS) and for-sMODULARIZE -sEXPORT_ES6. With-sEXPORTED_RUNTIME_METHODS=HEAPU8, both versions work. These builds use-sALLOW_MEMORY_GROWTHwith a small initial memory (16 MiB).Best regards,
Markus