Skip to content

Commit 98c45a2

Browse files
leonm1johnlanni
authored andcommitted
feat(go-sdk): add wasi hostcalls used by the Go SDK (proxy-wasm#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 9e5a89a commit 98c45a2

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
@@ -143,10 +143,13 @@ Word wasi_unstable_fd_read(Word, Word, Word, Word);
143143
Word wasi_unstable_fd_seek(Word, int64_t, Word, Word);
144144
Word wasi_unstable_fd_close(Word);
145145
Word wasi_unstable_fd_fdstat_get(Word fd, Word statOut);
146+
Word wasi_unstable_fd_fdstat_set_flags(Word fd, Word flags);
146147
Word wasi_unstable_environ_get(Word, Word);
147148
Word wasi_unstable_environ_sizes_get(Word count_ptr, Word buf_size_ptr);
148149
Word wasi_unstable_args_get(Word argc_ptr, Word argv_buf_size_ptr);
149150
Word wasi_unstable_args_sizes_get(Word argc_ptr, Word argv_buf_size_ptr);
151+
Word wasi_unstable_sched_yield();
152+
Word wasi_unstable_poll_oneoff(Word in, Word out, Word nsubscriptions, Word nevents);
150153
void wasi_unstable_proc_exit(Word);
151154
Word wasi_unstable_clock_time_get(Word, uint64_t, Word);
152155
Word wasi_unstable_random_get(Word, Word);
@@ -175,9 +178,10 @@ void emscripten_notify_memory_growth(Word);
175178
_f(continue_stream) _f(close_stream) _f(get_log_level)
176179

177180
#define FOR_ALL_WASI_FUNCTIONS(_f) \
178-
_f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(environ_get) \
179-
_f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) _f(random_get) \
180-
_f(proc_exit) _f(path_open) _f(fd_prestat_get) _f(fd_prestat_dir_name)
181+
_f(fd_write) _f(fd_read) _f(fd_seek) _f(fd_close) _f(fd_fdstat_get) _f(fd_fdstat_set_flags) \
182+
_f(environ_get) _f(environ_sizes_get) _f(args_get) _f(args_sizes_get) _f(clock_time_get) \
183+
_f(random_get) _f(sched_yield) _f(poll_oneoff) _f(proc_exit) _f(path_open) \
184+
_f(fd_prestat_get) _f(fd_prestat_dir_name)
181185

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

src/exports.cc

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

674+
// WASIp1 typings in comments sourced from
675+
// https://github.com/WebAssembly/wasi-libc/blob/446cb3f1aa21f9b1a1eab372f82d65d19003e924/libc-bottom-half/headers/public/wasi/api.h
676+
674677
// __wasi_errno_t path_open(__wasi_fd_t fd, __wasi_lookupflags_t dirflags, const char *path,
675678
// size_t path_len, __wasi_oflags_t oflags, __wasi_rights_t fs_rights_base, __wasi_rights_t
676679
// fs_rights_inheriting, __wasi_fdflags_t fdflags, __wasi_fd_t *retptr0)
@@ -801,6 +804,13 @@ Word wasi_unstable_fd_fdstat_get(Word fd, Word statOut) {
801804
return 0; // __WASI_ESUCCESS
802805
}
803806

807+
// __wasi_errno_t __wasi_fd_fdstat_set_flags(__wasi_fd_t fd, __wasi_fdflags_t flags)
808+
Word wasi_unstable_fd_fdstat_set_flags(Word /*fd*/, Word /*flags*/) {
809+
// Flags that can be specified: append, dsync, nonblock, rsync, and sync. Proxy-wasm only supports
810+
// STDOUT and STDERR, but none of these flags have any effect in Proxy-Wasm.
811+
return 52; // __WASI_ERRNO_ENOSYS
812+
}
813+
804814
// __wasi_errno_t __wasi_environ_get(char **environ, char *environ_buf);
805815
Word wasi_unstable_environ_get(Word environ_array_ptr, Word environ_buf) {
806816
auto *context = contextOrEffectiveContext();
@@ -904,6 +914,21 @@ Word wasi_unstable_random_get(Word result_buf_ptr, Word buf_len) {
904914
return 0; // __WASI_ESUCCESS
905915
}
906916

917+
// __wasi_errno_t __wasi_sched_yield()
918+
Word wasi_unstable_sched_yield() {
919+
// Per POSIX man pages, it is valid to return success if the calling thread is the only thread in
920+
// the highest priority list. This is vacuously true for wasm without threads. There are no valid
921+
// error cases defined.
922+
return 0; // __WASI_ESUCCESS
923+
}
924+
925+
// __wasi_errno_t __wasi_poll_oneoff(const __wasi_subscription_t *in, __wasi_event_t *out,
926+
// __wasi_size_t nsubscriptions, __wasi_size_t *nevents)
927+
Word wasi_unstable_poll_oneoff(Word /*in*/, Word /*out*/, Word /*nsubscriptions*/,
928+
Word /*nevents_ptr*/) {
929+
return 52; // __WASI_ERRNO_ENOSYS
930+
}
931+
907932
// void __wasi_proc_exit(__wasi_exitcode_t rval);
908933
void wasi_unstable_proc_exit(Word /*exit_code*/) {
909934
auto *context = contextOrEffectiveContext();

src/wasm.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,10 @@ void WasmBase::startVm(ContextBase *root_context) {
392392
// time
393393
"wasi_unstable.clock_time_get", "wasi_snapshot_preview1.clock_time_get",
394394
// random
395-
"wasi_unstable.random_get", "wasi_snapshot_preview1.random_get"});
395+
"wasi_unstable.random_get", "wasi_snapshot_preview1.random_get",
396+
// Go runtime initialization
397+
"wasi_unstable.fd_fdstat_get", "wasi_snapshot_preview1.fd_fdstat_get",
398+
"wasi_unstable.fd_fdstat_set_flags", "wasi_snapshot_preview1.fd_fdstat_set_flags"});
396399
if (_initialize_) {
397400
// WASI reactor.
398401
_initialize_(root_context);

0 commit comments

Comments
 (0)