WebAssembly/wasi-sdk

Can I provide my own system calls?

KernelDeimos opened this issue · 6 comments

I was able to provide my own system calls when compiling web assembly with llvm (no emscripten). Trying to compile a libc to wasm but I'm starting to realize many of them use inline assembly which is... a problem. Anyhow yeah, curious if I can combine this with syscall implementations in javascript

sbc100 commented

Is there some reason you wouldn't want to use the libc that comes with wasi-sdk? The system call layer for WASI is the WASI API, and you can implement it javascript if you like yes.

If you are asking about added new system calls on top of the existing set of WASI APIs, that is possible, but of course non-portable.

Oh yes, I was unclear; I'm trying to compile a libc to wasm - any lib c - and this (wasi-sdk) is one of those libc's that I'm considering. (I was also thinking of adding an architecture to musl-libc, which looks maybe simpler than doing the same for glibc)

What I want to do is the second thing you described - implement the system calls in javascript. Is there documentation on this?

Also just after writing this I realized that musl is being used for wasi-sdk as well so I have new questions.

wasm32/syscall_arch.h is empty, so where do those functions get defined?

What I'd like to do is something like this:

extern long int syscall (long int __sysno, ...)
    __attribute__((import_module("env"), import_name("syscall")));

but I suspect I won't simply be able to link it to this form of musl and expect it to work, right?

sbc100 commented

The two main libc's used today (emscripten and wasi-libc) are both based on musl using musl would be a logical choice. Note the upstream musl does not historically accept patches to port to non-linux platforms, so both musl ports exist in forks today.

Why don't you want to just use wasi-libc or emscripten's libc? Why start from scratch? Note that both these existing musl ports avoid using the generic syscall function and instead implement each syscall individually as a separate call out the host (JS). For example here is syscall that emscripten implements in JS: https://github.com/emscripten-core/emscripten/blob/c5c2714a438f3e51d2907c85c25e0c01f9385bde/src/library_syscall.js#L171-L174.

For wasi-libc the syscalls are the WASI APIs themselves, so you would need to imlement the preview1 syscalls.

I'll use anything that makes what I'm trying to do easier, but I do intend to intercept every system call. The example in emscripten tells me how to contribute a syscall implementation to emscripten, but I'm not sure how to use something like emscripten without the system call implementations that are already attached to it.

Another clarification, I'm not opposed to having some system calls implemented for me if I can still remove them one-by-one as I continue development on my kernel. That would really be a huge win, I'm just struggling a bit to figure out how to use wasi-sdk this way.

edit: also sorry to open this as an issue, a discussion board or chatroom would be more appropriate - is there one?

I found this repo which, while outdated, was enough of a starting point to get me where I wanted. Thanks!