Skip to content

Commit

Permalink
feat(go-sdk): add wasi hostcalls used by the Go SDK
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 committed Dec 13, 2024
1 parent 3212034 commit 405c928
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
15 changes: 13 additions & 2 deletions include/proxy-wasm/exports.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ Word grpc_stream(Word service_ptr, Word service_size, Word service_name_ptr, Wor
Word grpc_cancel(Word token);
Word grpc_close(Word token);
Word grpc_send(Word token, Word message_ptr, Word message_size, Word end_stream);
Word secure_getenv(Word name);
Word getpid();

Word set_tick_period_milliseconds(Word tick_period_milliseconds);
Word get_current_time_nanoseconds(Word result_uint64_ptr);
Expand Down Expand Up @@ -146,6 +148,12 @@ Word wasi_unstable_args_sizes_get(Word argc_ptr, Word argv_buf_size_ptr);
void wasi_unstable_proc_exit(Word);
Word wasi_unstable_clock_time_get(Word, uint64_t, Word);
Word wasi_unstable_random_get(Word, Word);
Word wasi_unstable_fd_filestat_get(Word fd, Word statOut);
Word wasi_unstable_fd_readdir(Word fd, Word buf, Word buf_len, int64_t cookie, Word bufused);
Word wasi_unstable_path_filestat_get(Word fd, Word flags, Word path, Word path_len, Word statOut);
Word wasi_unstable_fd_fdstat_set_flags(Word fd, Word flags);
Word wasi_unstable_sched_yield();
Word wasi_unstable_poll_oneoff(Word in, Word out, Word nsubscriptions, Word nevents);
Word pthread_equal(Word left, Word right);
void emscripten_notify_memory_growth(Word);

Expand All @@ -163,7 +171,8 @@ void emscripten_notify_memory_growth(Word);
_f(get_current_time_nanoseconds) _f(define_metric) \
_f(increment_metric) _f(record_metric) _f(get_metric) \
_f(set_effective_context) _f(done) \
_f(call_foreign_function)
_f(call_foreign_function) _f(getpid) \
_f(secure_getenv)

#define FOR_ALL_HOST_FUNCTIONS_ABI_SPECIFIC(_f) \
_f(get_configuration) _f(continue_request) _f(continue_response) _f(clear_route_cache) \
Expand All @@ -172,7 +181,9 @@ void emscripten_notify_memory_growth(Word);
#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(proc_exit) _f(path_open) _f(fd_prestat_get) _f(fd_prestat_dir_name) \
_f(fd_filestat_get) _f(fd_readdir) _f(path_filestat_get) _f(fd_fdstat_set_flags) \
_f(sched_yield) _f(poll_oneoff)

// 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
6 changes: 4 additions & 2 deletions include/proxy-wasm/wasm_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ using WasmCallback_WWl = Word (*)(Word, int64_t);
using WasmCallback_WWlWW = Word (*)(Word, int64_t, Word, Word);
using WasmCallback_WWm = Word (*)(Word, uint64_t);
using WasmCallback_WWmW = Word (*)(Word, uint64_t, Word);
using WasmCallback_WWWWlW = Word (*)(Word, Word, Word, int64_t, Word);
using WasmCallback_WWWWWWllWW = Word (*)(Word, Word, Word, Word, Word, int64_t, int64_t, Word,
Word);
using WasmCallback_dd = double (*)(double);
Expand All @@ -130,8 +131,9 @@ using WasmCallback_dd = double (*)(double);
_f(proxy_wasm::WasmCallback_WWlWW) \
_f(proxy_wasm::WasmCallback_WWm) \
_f(proxy_wasm::WasmCallback_WWmW) \
_f(proxy_wasm::WasmCallback_WWWWWWllWW) \
_f(proxy_wasm::WasmCallback_dd)
_f(proxy_wasm::WasmCallback_WWWWlW) \
_f(proxy_wasm::WasmCallback_WWWWWWllWW)\
_f(proxy_wasm::WasmCallback_dd)

enum class Cloneable {
NotCloneable, // VMs can not be cloned and should be created from scratch.
Expand Down
28 changes: 28 additions & 0 deletions src/exports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ void wasi_unstable_proc_exit(Word /*exit_code*/) {
context->error("wasi_unstable proc_exit");
}

Word wasi_unstable_sched_yield() { return 0; }

Word wasi_unstable_poll_oneoff(Word in, Word out, Word nsubscriptions, Word nevents) { return 0; }

Word pthread_equal(Word left, Word right) { return static_cast<uint64_t>(left == right); }

void emscripten_notify_memory_growth(Word /*memory_index*/) {}
Expand Down Expand Up @@ -925,5 +929,29 @@ Word get_log_level(Word result_level_uint32_ptr) {
return WasmResult::Ok;
}

Word wasi_unstable_fd_fdstat_set_flags(Word fd, Word flags) {
// Don't support reading of any files.
return 52; // __WASI_ERRNO_ENOSYS
}

Word wasi_unstable_fd_filestat_get(Word fd, Word statOut) {
// Don't support reading of any files.
return 52; // __WASI_ERRNO_ENOSYS
}

Word wasi_unstable_fd_readdir(Word fd, Word buf, Word buf_len, int64_t cookie, Word bufused) {
// Don't support reading of any files.
return 52; // __WASI_ERRNO_ENOSYS
}

Word wasi_unstable_path_filestat_get(Word fd, Word flags, Word path, Word path_len, Word statOut) {
// Don't support reading of any files.
return 52; // __WASI_ERRNO_ENOSYS
}

Word secure_getenv(Word name) { return 0; }

Word getpid() { return 4; }

} // namespace exports
} // namespace proxy_wasm
2 changes: 2 additions & 0 deletions src/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ void WasmBase::registerCallbacks() {
"env", #_fn, &exports::_fn, \
&ConvertFunctionWordToUint32<decltype(exports::_fn), \
exports::_fn>::convertFunctionWordToUint32)
_REGISTER(secure_getenv);
_REGISTER(getpid);
_REGISTER(pthread_equal);
_REGISTER(emscripten_notify_memory_growth);
#undef _REGISTER
Expand Down

0 comments on commit 405c928

Please sign in to comment.