tetratelabs / wazero

wazero: the zero dependency WebAssembly runtime for Go developers
https://wazero.io
Apache License 2.0
4.94k stars 258 forks source link

wazero running on embedded systems #1854

Closed deadprogram closed 7 months ago

deadprogram commented 11 months ago

I would like to be able to do something like WAMR https://github.com/bytecodealliance/wasm-micro-runtime for embedded systems, but to use wazero as the code/runtime to do this.

Ideal scenario would be that I would be able to run:

cd /path/to/repo/wazero-micro
tinygo build -o wazero-micro.uf2 -target itsybitsy-m4 .

and get a binary I can then flash onto that MCU with the wazero runtime. The binary would contain a minimal version of the wazero runtime.

Pretty sure this could be done with WAMR, but it requires also using an RTOS like Zephyr. Wazero would be running bare metal on the hardware e.g. "The Go runtime is your RTOS" Also, it would be great to have a pure Go stack able to do this without needing any other languages/compilers.

Specific hardware functionality can be exposed to WASM modules via host functions. Deciding on how to load new WASM code onto the device can be part of the host program, for example via serial port/OTA updates/etc.

evacchi commented 11 months ago

A few days ago I ran the same thought experiment. Wazero already builds fine to wasm using the plain Go compiler (and then you can even run wazero-on-wazero in interpreter mode).

I think the bulk of the work is in the syscalls (which are mostly in the WASI lib implementation). e.g. your command fails with

  ❯ tinygo build -o wazero-micro.uf2 -target itsybitsy-m4
  # github.com/tetratelabs/wazero/internal/platform
  internal/platform/mmap_unix.go:42:22: undefined: syscall.Syscall
  internal/platform/mmap_unix.go:42:38: undefined: syscall.SYS_MPROTECT
deadprogram commented 11 months ago

Making some progress on this in #1855 when running against latest TinyGo dev branch plus branch of the TinyGo net package.

Current status:

$ tinygo flash -size short -target nano-rp2040 -stack-size 8kb -monitor ./examples/basic/
   code    data     bss |   flash     ram
 414220   72072   15040 |  486292   87112
Connected to /dev/ttyACM0. Press Ctrl-C to exit.
1970/01/01 00:00:05 Starting wazero on tinygo...
panic: runtime error at 0x10006537: out of memory
[tinygo: panic at /home/ron/Development/tinygo/tinygo/src/runtime/slice.go:30:15]

Need to start adding back the memory allocation code that I disabled to get the wazero branch to compile. :smile_cat:

evacchi commented 9 months ago

hey this is awesome!

evacchi commented 7 months ago

using this issue to track the related PRs

deadprogram commented 7 months ago

We made it! Thank you to @orsinium @evacchi @achille-roussel and everyone else who helped make this happen.

I will close this issue now. We can open smaller more focused individual issues for anything else that we want to work on related to this area.

Thanks again!

paralin commented 7 months ago

This is awesome! Thanks for the incredible work!

ncruces commented 5 months ago

In light of https://github.com/tinygo-org/tinygo/pull/4156, we will need to revert #2161.

@deadprogram, @orsinium in your opinion, when is it the right time to do that? Do you expect a quick release of the feature? Can we do this now?

Also this one is clearly wrong and escaped review: https://github.com/tetratelabs/wazero/blob/48f702e154b5727bfb3ef734f7fe1f90631b3509/internal/engine/wazevo/backend/isa/amd64/reflect_tinygo.go#L7-L11

deadprogram commented 5 months ago

We will probably have our next release in approximately 4 weeks.

ncruces commented 5 months ago

Tentative PR #2210 waiting for release to update the failing CI test.