runtime: [wasm] 'Undefined symbol: arc4random' building Blazor WASM with libsodium
Description
The libsodium team maintains a wasm32-wasi.sh build script that outputs a libsodium.a archive file.
I would like to use this file as a NativeFileReference in my Blazor WASM application. I reference the file like so:
<ItemGroup>
<NativeFileReference Include="Native\libsodium.a" />
</ItemGroup>
However, I receive the following warnings when building my project:
EXEC : warning : undefined symbol: arc4random (referenced by top-level compiled C/C++ code) [C:\Users\Jack\source\repos\Crypter\crypter.web\Crypter.Web.csproj]
EXEC : warning : undefined symbol: arc4random_buf (referenced by top-level compiled C/C++ code) [C:\Users\Jack\source\repos\Crypter\crypter.web\Crypter.Web.csproj]
These warnings resurface as runtime errors when I try invoking those methods (missing function: arc4random_buf). Any ideas where the root problem may be?
The full output of running the dotnet build is attached.
Reproduction Steps
Include libsodium.a as a native file reference to a .NET 6.0 Blazor WASM project. Run dotnet build.
Expected behavior
The build warnings do not surface. The runtime errors do not surface.
Actual behavior
dotnet build surfaces several warnings.
EXEC : warning : undefined symbol: arc4random (referenced by top-level compiled C/C++ code) [C:\Users\Jack\source\repos\Crypter\crypter.web\Crypter.Web.csproj]
EXEC : warning : undefined symbol: arc4random_buf (referenced by top-level compiled C/C++ code) [C:\Users\Jack\source\repos\Crypter\crypter.web\Crypter.Web.csproj]
Regression?
I’m not aware of this ever working.
Known Workarounds
I’m not aware of any workarounds.
Configuration
Visual Studio 2022, v17.3.1 Microsoft.NET.Runtime.Emscripten.2.0.23.Sdk.win-x64\6.0.4\tools\bin\wasm-ld.exe
Other information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (11 by maintainers)
I’ll proceed with trying to load the library into wasmer-js’s wasm module and see what I can do. Thanks for the guidance.
It’s good to know I can use the libsodium.js package as a fallback in .NET 7.0. Thanks for the proof of concept of using multiple emscripten programs.
The
arc4randommust be a native function. It must be part of the translation from native code to wasm. Theemscripten.shemits a separate wasm module and javascript binding api. That doesn’t help with linking libsodium to the blazor app as native dependency.There is also the libsodium.js project. You can probably use it and do js interop from blazor app, but I’m not sure if two emscripten projects will run on the same site.
I’m sure it wouldn’t on .NET 6, because the emscripten puts itself into the global javascript namespace. In .NET 7 it should be possible, because we wrapped .NET as javascript module. Anyway we didn’t tested it running side-by-side with different emscripten-based project.