Skip to content

Commit

Permalink
feat(go-sdk): add wasi hostcalls used by the Go SDK (#427)
Browse files Browse the repository at this point in the history
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 <mattleon@google.com>
  • Loading branch information
leonm1 authored Dec 19, 2024
1 parent 63cb9c1 commit c4d7bb0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
10 changes: 7 additions & 3 deletions include/proxy-wasm/exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) \
Expand Down
25 changes: 25 additions & 0 deletions src/exports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c4d7bb0

Please sign in to comment.