Skip to content

Commit c4d7bb0

Browse files
authored
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 <[email protected]>
1 parent 63cb9c1 commit c4d7bb0

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

include/proxy-wasm/exports.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ Word wasi_unstable_fd_read(Word, Word, Word, Word);
139139
Word wasi_unstable_fd_seek(Word, int64_t, Word, Word);
140140
Word wasi_unstable_fd_close(Word);
141141
Word wasi_unstable_fd_fdstat_get(Word fd, Word statOut);
142+
Word wasi_unstable_fd_fdstat_set_flags(Word fd, Word flags);
142143
Word wasi_unstable_environ_get(Word, Word);
143144
Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr);
144145
Word wasi_unstable_args_get(Word argc_ptr, Word argv_buf_size_ptr);
145146
Word wasi_unstable_args_sizes_get(Word argc_ptr, Word argv_buf_size_ptr);
147+
Word wasi_unstable_sched_yield();
148+
Word wasi_unstable_poll_oneoff(Word in, Word out, Word nsubscriptions, Word nevents);
146149
void wasi_unstable_proc_exit(Word);
147150
Word wasi_unstable_clock_time_get(Word, uint64_t, Word);
148151
Word wasi_unstable_random_get(Word, Word);
@@ -170,9 +173,10 @@ void emscripten_notify_memory_growth(Word);
170173
_f(continue_stream) _f(close_stream) _f(get_log_level)
171174

172175
#define FOR_ALL_WASI_FUNCTIONS(_f) \
173-
_f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(environ_get) \
174-
_f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) _f(random_get) \
175-
_f(proc_exit) _f(path_open) _f(fd_prestat_get) _f(fd_prestat_dir_name)
176+
_f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(fd_fdstat_set_flags) \
177+
_f(environ_get) _f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) \
178+
_f(random_get) _f(sched_yield) _f(poll_oneoff) _f(proc_exit) _f(path_open) \
179+
_f(fd_prestat_get) _f(fd_prestat_dir_name)
176180

177181
// Helpers to generate a stub to pass to VM, in place of a restricted proxy-wasm capability.
178182
#define _CREATE_PROXY_WASM_STUB(_fn) \

src/exports.cc

+25
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,9 @@ Word grpc_send(Word token, Word message_ptr, Word message_size, Word end_stream)
646646
return context->grpcSend(token, message.value(), end_stream != 0U);
647647
}
648648

649+
// WASIp1 typings in comments sourced from
650+
// https://github.com/WebAssembly/wasi-libc/blob/446cb3f1aa21f9b1a1eab372f82d65d19003e924/libc-bottom-half/headers/public/wasi/api.h
651+
649652
// __wasi_errno_t path_open(__wasi_fd_t fd, __wasi_lookupflags_t dirflags, const char *path,
650653
// size_t path_len, __wasi_oflags_t oflags, __wasi_rights_t fs_rights_base, __wasi_rights_t
651654
// 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) {
776779
return 0; // __WASI_ESUCCESS
777780
}
778781

782+
// __wasi_errno_t __wasi_fd_fdstat_set_flags(__wasi_fd_t fd, __wasi_fdflags_t flags)
783+
Word wasi_unstable_fd_fdstat_set_flags(Word /*fd*/, Word /*flags*/) {
784+
// Flags that can be specified: append, dsync, nonblock, rsync, and sync. Proxy-wasm only supports
785+
// STDOUT and STDERR, but none of these flags have any effect in Proxy-Wasm.
786+
return 52; // __WASI_ERRNO_ENOSYS
787+
}
788+
779789
// __wasi_errno_t __wasi_environ_get(char **environ, char *environ_buf);
780790
Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
781791
auto *context = contextOrEffectiveContext();
@@ -879,6 +889,21 @@ Word wasi_unstable_random_get(Word result_buf_ptr, Word buf_len) {
879889
return 0; // __WASI_ESUCCESS
880890
}
881891

892+
// __wasi_errno_t __wasi_sched_yield()
893+
Word wasi_unstable_sched_yield() {
894+
// Per POSIX man pages, it is valid to return success if the calling thread is the only thread in
895+
// the highest priority list. This is vacuously true for wasm without threads. There are no valid
896+
// error cases defined.
897+
return 0; // __WASI_ESUCCESS
898+
}
899+
900+
// __wasi_errno_t __wasi_poll_oneoff(const __wasi_subscription_t *in, __wasi_event_t *out,
901+
// __wasi_size_t nsubscriptions, __wasi_size_t *nevents)
902+
Word wasi_unstable_poll_oneoff(Word /*in*/, Word /*out*/, Word /*nsubscriptions*/,
903+
Word /*nevents_ptr*/) {
904+
return 52; // __WASI_ERRNO_ENOSYS
905+
}
906+
882907
// void __wasi_proc_exit(__wasi_exitcode_t rval);
883908
void wasi_unstable_proc_exit(Word /*exit_code*/) {
884909
auto *context = contextOrEffectiveContext();

src/wasm.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,10 @@ void WasmBase::startVm(ContextBase *root_context) {
394394
// time
395395
"wasi_unstable.clock_time_get", "wasi_snapshot_preview1.clock_time_get",
396396
// random
397-
"wasi_unstable.random_get", "wasi_snapshot_preview1.random_get"});
397+
"wasi_unstable.random_get", "wasi_snapshot_preview1.random_get",
398+
// Go runtime initialization
399+
"wasi_unstable.fd_fdstat_get", "wasi_snapshot_preview1.fd_fdstat_get",
400+
"wasi_unstable.fd_fdstat_set_flags", "wasi_snapshot_preview1.fd_fdstat_set_flags"});
398401
if (_initialize_) {
399402
// WASI reactor.
400403
_initialize_(root_context);

0 commit comments

Comments
 (0)