From c4d7bb0fda912e24c64daf2aa749ec54cec99412 Mon Sep 17 00:00:00 2001 From: Matt Leon Date: Thu, 19 Dec 2024 13:59:36 -0500 Subject: [PATCH] feat(go-sdk): add wasi hostcalls used by the Go SDK (#427) The full Go sdk imports hostcalls not currently exported to the wasm module, making the wasm module fail on instantiation. Per discussion with the Go core maintainers, these functions do not need to be implemented, but they must be present. Signed-off-by: Matt Leon --- include/proxy-wasm/exports.h | 10 +++++++--- src/exports.cc | 25 +++++++++++++++++++++++++ src/wasm.cc | 5 ++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/include/proxy-wasm/exports.h b/include/proxy-wasm/exports.h index 376a4d3b..91c51150 100644 --- a/include/proxy-wasm/exports.h +++ b/include/proxy-wasm/exports.h @@ -139,10 +139,13 @@ Word wasi_unstable_fd_read(Word, Word, Word, Word); Word wasi_unstable_fd_seek(Word, int64_t, Word, Word); Word wasi_unstable_fd_close(Word); Word wasi_unstable_fd_fdstat_get(Word fd, Word statOut); +Word wasi_unstable_fd_fdstat_set_flags(Word fd, Word flags); Word wasi_unstable_environ_get(Word, Word); Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr); Word wasi_unstable_args_get(Word argc_ptr, Word argv_buf_size_ptr); Word wasi_unstable_args_sizes_get(Word argc_ptr, Word argv_buf_size_ptr); +Word wasi_unstable_sched_yield(); +Word wasi_unstable_poll_oneoff(Word in, Word out, Word nsubscriptions, Word nevents); void wasi_unstable_proc_exit(Word); Word wasi_unstable_clock_time_get(Word, uint64_t, Word); Word wasi_unstable_random_get(Word, Word); @@ -170,9 +173,10 @@ void emscripten_notify_memory_growth(Word); _f(continue_stream) _f(close_stream) _f(get_log_level) #define FOR_ALL_WASI_FUNCTIONS(_f) \ - _f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(environ_get) \ - _f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) _f(random_get) \ - _f(proc_exit) _f(path_open) _f(fd_prestat_get) _f(fd_prestat_dir_name) + _f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(fd_fdstat_set_flags) \ + _f(environ_get) _f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) \ + _f(random_get) _f(sched_yield) _f(poll_oneoff) _f(proc_exit) _f(path_open) \ + _f(fd_prestat_get) _f(fd_prestat_dir_name) // Helpers to generate a stub to pass to VM, in place of a restricted proxy-wasm capability. #define _CREATE_PROXY_WASM_STUB(_fn) \ diff --git a/src/exports.cc b/src/exports.cc index 0290dcf0..25ca06c9 100644 --- a/src/exports.cc +++ b/src/exports.cc @@ -646,6 +646,9 @@ Word grpc_send(Word token, Word message_ptr, Word message_size, Word end_stream) return context->grpcSend(token, message.value(), end_stream != 0U); } +// WASIp1 typings in comments sourced from +// https://github.com/WebAssembly/wasi-libc/blob/446cb3f1aa21f9b1a1eab372f82d65d19003e924/libc-bottom-half/headers/public/wasi/api.h + // __wasi_errno_t path_open(__wasi_fd_t fd, __wasi_lookupflags_t dirflags, const char *path, // size_t path_len, __wasi_oflags_t oflags, __wasi_rights_t fs_rights_base, __wasi_rights_t // fs_rights_inheriting, __wasi_fdflags_t fdflags, __wasi_fd_t *retptr0) @@ -776,6 +779,13 @@ Word wasi_unstable_fd_fdstat_get(Word fd, Word statOut) { return 0; // __WASI_ESUCCESS } +// __wasi_errno_t __wasi_fd_fdstat_set_flags(__wasi_fd_t fd, __wasi_fdflags_t flags) +Word wasi_unstable_fd_fdstat_set_flags(Word /*fd*/, Word /*flags*/) { + // Flags that can be specified: append, dsync, nonblock, rsync, and sync. Proxy-wasm only supports + // STDOUT and STDERR, but none of these flags have any effect in Proxy-Wasm. + return 52; // __WASI_ERRNO_ENOSYS +} + // __wasi_errno_t __wasi_environ_get(char **environ, char *environ_buf); Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) { auto *context = contextOrEffectiveContext(); @@ -879,6 +889,21 @@ Word wasi_unstable_random_get(Word result_buf_ptr, Word buf_len) { return 0; // __WASI_ESUCCESS } +// __wasi_errno_t __wasi_sched_yield() +Word wasi_unstable_sched_yield() { + // Per POSIX man pages, it is valid to return success if the calling thread is the only thread in + // the highest priority list. This is vacuously true for wasm without threads. There are no valid + // error cases defined. + return 0; // __WASI_ESUCCESS +} + +// __wasi_errno_t __wasi_poll_oneoff(const __wasi_subscription_t *in, __wasi_event_t *out, +// __wasi_size_t nsubscriptions, __wasi_size_t *nevents) +Word wasi_unstable_poll_oneoff(Word /*in*/, Word /*out*/, Word /*nsubscriptions*/, + Word /*nevents_ptr*/) { + return 52; // __WASI_ERRNO_ENOSYS +} + // void __wasi_proc_exit(__wasi_exitcode_t rval); void wasi_unstable_proc_exit(Word /*exit_code*/) { auto *context = contextOrEffectiveContext(); diff --git a/src/wasm.cc b/src/wasm.cc index cb1dd9b3..e8a7ce43 100644 --- a/src/wasm.cc +++ b/src/wasm.cc @@ -394,7 +394,10 @@ void WasmBase::startVm(ContextBase *root_context) { // time "wasi_unstable.clock_time_get", "wasi_snapshot_preview1.clock_time_get", // random - "wasi_unstable.random_get", "wasi_snapshot_preview1.random_get"}); + "wasi_unstable.random_get", "wasi_snapshot_preview1.random_get", + // Go runtime initialization + "wasi_unstable.fd_fdstat_get", "wasi_snapshot_preview1.fd_fdstat_get", + "wasi_unstable.fd_fdstat_set_flags", "wasi_snapshot_preview1.fd_fdstat_set_flags"}); if (_initialize_) { // WASI reactor. _initialize_(root_context);