wazero icon indicating copy to clipboard operation
wazero copied to clipboard

wazero running on embedded systems

Open deadprogram opened this issue 2 years ago • 3 comments

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.

deadprogram avatar Nov 30 '23 09:11 deadprogram

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

evacchi avatar Nov 30 '23 17:11 evacchi

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:

deadprogram avatar Dec 09 '23 09:12 deadprogram

hey this is awesome!

evacchi avatar Jan 14 '24 19:01 evacchi

using this issue to track the related PRs

  • https://github.com/tetratelabs/wazero/pull/2159
  • https://github.com/tetratelabs/wazero/pull/2160
  • https://github.com/tetratelabs/wazero/pull/2161
  • https://github.com/tetratelabs/wazero/pull/2162
  • https://github.com/tetratelabs/wazero/pull/2163
  • https://github.com/tetratelabs/wazero/pull/2166

evacchi avatar Mar 26 '24 11:03 evacchi

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!

deadprogram avatar Mar 31 '24 09:03 deadprogram

This is awesome! Thanks for the incredible work!

paralin avatar Mar 31 '24 10:03 paralin

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

ncruces avatar May 14 '24 13:05 ncruces

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

deadprogram avatar May 14 '24 14:05 deadprogram

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

ncruces avatar May 20 '24 17:05 ncruces