Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate standalone binary from .wasmu file #5422

Open
darccio opened this issue Feb 22, 2025 · 4 comments
Open

How to generate standalone binary from .wasmu file #5422

darccio opened this issue Feb 22, 2025 · 4 comments
Assignees
Labels
❓ question I've a question!

Comments

@darccio
Copy link

darccio commented Feb 22, 2025

Summary

By default, it seems that wasmer compile generates a .wasmu file. According to #3014 (comment), it's already contains compiled machine instructions for the target arch.

From the same thread, it seems implied that using create-exe with a .wasmu should generate a standalone binary with Wasmer runtime embedded on it and the module converted and linked as object. This would seem to align with a old blog post in wasmer.io.

When I run the following instructions, I get an error about a bad magic number.

> wasmer compile hello_world.wat -o hello_world.wasmu # Find hello_world.wat in the Additional details section.
Compiler: cranelift
Target: x86_64-unknown-linux-gnu
✔ File compiled successfully to `hello_world.wasmu`.

> wasmer create-exe hello_world.wasmu -o hello_world
Compiler: cranelift
Target: x86_64-unknown-linux-gnu
Using path `/home/dario/.wasmer/cache/wasmer-linux-amd64/lib/libwasmer-headless.a` as libwasmer path.
error: Compilation error: WebAssembly translation error: Invalid input WebAssembly code at offset 0: magic header not detected: bad magic number - expected=[
    0x0,
    0x61,
    0x73,
    0x6d,
] actual=[
    0x77,
    0x61,
    0x73,
    0x6d,
]

Does create-exe supports compile output files or should we use another tool to generate a .wasm file - like wat2wasm - to feed it to create-exe?

Thanks!

Additional details

Wasmer version: wasmer 5.0.4

hello-world.wat:

(module
  ;; Import the required WASI functions
  (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))

  ;; Define a buffer to store the message
  (memory (export "memory") 1)
  (data (i32.const 8) "Hello, World!\n")

  ;; Define the _start function
  (func $main (export "_start")
    ;; Setup the iovec array
    (i32.store (i32.const 0) (i32.const 8))    ;; pointer to the message
    (i32.store (i32.const 4) (i32.const 14))   ;; length of the message

    ;; Call fd_write
    (call $fd_write
      (i32.const 1)  ;; file_descriptor - 1 for stdout
      (i32.const 0)  ;; *iovs - pointer to the iovec array
      (i32.const 1)  ;; iovs_len - number of iovec entries
      (i32.const 20) ;; nwritten - where to store the number of bytes written
    )
    drop ;; Discard the result
  )
)
@darccio darccio added the ❓ question I've a question! label Feb 22, 2025
@darccio
Copy link
Author

darccio commented Feb 22, 2025

Update: I converted the .wat file using wat2wasm and create-exe didn't complain but running the binary failed with a reference to wasmer-universal:

> wasmer create-exe hello_world.wasm -o hello_world
Compiler: cranelift
Target: x86_64-unknown-linux-gnu
Using path `/home/dario/.wasmer/cache/wasmer-linux-amd64/lib/libwasmer-headless.a` as libwasmer path.
✔ Native executable compiled successfully to `hello_world`.
> ./hello_world 
Failed to create module from atom "hello_world"
Error len: `65`
incompatible binary: The provided bytes are not wasmer-universal

This makes sense if create-exe only works with WASM Universal binaries.

@xdoardo xdoardo self-assigned this Feb 27, 2025
@xdoardo
Copy link
Collaborator

xdoardo commented Feb 27, 2025

Hello, thanks for the report. I'll investigate this as soon as possible.

@xdoardo
Copy link
Collaborator

xdoardo commented Mar 6, 2025

Currently, create-exe only supports receiving .wasm files, but we can investigate how to make it happen.

@darccio
Copy link
Author

darccio commented Mar 22, 2025

Thanks @xdoardo. As I showed in #5422 (comment), using a .wasm returns an error mentioning bytes not being wasmer-universal.

What am I missing? I'd appreciate some orientation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓ question I've a question!
Projects
None yet
Development

No branches or pull requests

2 participants