Skip to content

[Experimental] wasi/wasm32 platform support - #3603

Closed
sipsma wants to merge 10 commits into
dagger:mainfrom
sipsma:wasm
Closed

sipsma wants to merge 10 commits into
dagger:mainfrom
sipsma:wasm

Conversation

@sipsma

@sipsma sipsma commented Oct 31, 2022 •

Copy link
Copy Markdown
Contributor

Just a quick pass to see if we could get support for the wasi/wasm32 platform on Dagger. Seems to be working and initial impression is that this could be cleaned up to the point of being mergeable; probably not a throwaway experiment.

Based on top of the multiplatform PR.

Usage

Example usage from included test case (pulling and running a test image I built that has a wasm binary and uses platform wasi/wasm32):
https://github.com/dagger/dagger/blob/ff64e543a317d70f3d1fe889cd2a8ac46f050ec8/core/integration/platform_test.go#L346-L351

In that example the Exec defaults to the Entrypoint of the container, but in general args just need to be set to point to a .wasm binary.

Example of building a wasm binary from rust code and then executing it:
https://github.com/dagger/dagger/blob/ff64e543a317d70f3d1fe889cd2a8ac46f050ec8/core/integration/platform_test.go#L285-L336

Implementation

Quick hackish solution implemented in this PR:

  • Build wasmtime as a static binary
  • Include it alongside our shim binary
  • When wasi/wasm32 is being used, our shim invokes the wasmtime binary, which itself invokes the actual user's wasm binary
  • wasmtime is configured to setup mounts + env vars as configured in the container

Surprisingly that's it.

I started w/ a much more complicated path of using wasmedge and configuring buildkitd to use the containerd worker which is instrumented to use crun w/ wasmedge support (as described here), but I eventually realized all that did was result in this very light wrapper being invoked from crun's codebase.

However, we already have our own shim in dagger, so that's the much easier thing to use right now. It may be the right approach in the long run too? Not sure yet, haven't thought in depth.

Current Caveats

  1. There seems to be module incompatibilities between wasm binaries built by different tools and at different times, so some of the existing wasi/wasm32 images I tried fail to load due to incompatible modules being required
    • AFAICT this is just an issue in the ecosystem, but I have not done anything w/ Wasm until today so I could easily be missing something. Need to research more.
    • For example, the michaelirwin244/wasm-example image fails to load w/ our wasmtime shim due to the errors listed here.
  2. wasmtime lets you "mount" directories into the wasm sandbox but not individual files, so right now the silly workaround is that you can only include mounts which don't have a source path. Will need a better solution than this before merging though.
  3. There's probably a lot of other features that don't translate perfectly to the wasm sandbox (i.e. I'm not sure you can set the current workding dir). We can decide if that's a dealbreaker or if it's okay to just have clean errors in those cases.
  4. The shim build code statically compiles wasmtime from source, which takes a while (several minutes on my laptop). It's cached afterwards but still would be nice to avoid this as it slows down uncached CI builds a lot.

sipsma added 10 commits October 29, 2022 14:09
Includes emulation support, support for cross-compilation (converting
host dirs into other platforms) and multiplatform image push.

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
@aluzzardi

Copy link
Copy Markdown
Contributor

This is awesome :)

@sipsma Given the speed of this repo -- should we merge this even if unfinished to avoid rotting? Or are there side effects to the non-wasm codepath?

@sipsma

sipsma commented Nov 4, 2022

Copy link
Copy Markdown
Contributor Author

Given the speed of this repo -- should we merge this even if unfinished to avoid rotting? Or are there side effects to the non-wasm codepath?

I think I got rid of the impacts to non-wasm codepaths. The wasm code path needs a little love though; it currently statically compiles wasmtime from source, which takes several minutes (though obviously it's cached later). wasmtime is also incompatible with some of the wasm images I tried. So my overall idea was to try to pull a different pre-built image for wasmedge and get it working even when not statically linked (have an idea on how to do this pretty easily).

I don't want this to languish though either I agree. I'll rebase it once there's breathing room around the new SDK release and then quickly try to improve it. If the improvements don't work out, we can just merge it as is and then put a warning label on it that it's experimental or something maybe.

@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 7 days with no activity.

@github-actions github-actions Bot closed this Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants