bytecodealliance / wasmtime-dotnet

.NET embedding of Wasmtime https://bytecodealliance.github.io/wasmtime-dotnet/
Apache License 2.0
409 stars 52 forks source link

Question: Run compiled wasm from wasmtime-dotnet #302

Closed relcodedev closed 4 months ago

relcodedev commented 5 months ago

If I compile a wasm to a .cwasm and run with example code. It fails

Invalid input WebAssembly code at offset 0: magic header not detected: bad magic number - expected=[ 0x0, 0x61, 0x73, 0x6d, ] actual=[ 0x7f, 0x45, 0x4c, 0x46, ] at Wasmtime.Module.FromBytes(Engine engine, String name, ReadOnlySpan`1 bytes) at Wasmtime.Module.FromFile(Engine engine, String path)

At command line we can use --allow-compiled, how is it done through code? Thanks.

peterhuene commented 5 months ago

Hi @relcodedev.

To use a .cwasm file that was previously created with the wasmtime compile command or Module.Serialize, one must use the Module.DeserializeFile method to recreate the module instead of Module.FromFile; the former expects an already compiled module in a binary format and the latter expects a WebAssembly module (either text or binary format).

relcodedev commented 4 months ago

Excellent. Thank you.

relcodedev commented 4 months ago

it seems that this works with wasmtime compile and a Module.DeserializeFile if they are the same version 19.0.1. If wasmtime-dotnet was in 19.0.0 I get exception. Is it a requirement that the wasmtime runtime compile be the same version as the wasmtime-dotnet to run it?

peterhuene commented 4 months ago

Unfortunately, the Engine you use to deserialize the previously compiled module must be not only the same version of Wasmtime, but also the exact same compilation settings (and obviously also CPU architecture).

This is because Wasmtime doesn't make guarantees around compatibility of the serialized format, so it simply checks that the versions match.

I did publish 19.0.1 of the .NET package yesterday, so I hope that helps!

relcodedev commented 4 months ago

Thanks. It did work once I updated. Excellent.