Skip to content

add all wasi hostcalls used by Go SDK #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

johnlanni
Copy link

In #427, a portion of the wasi hostcalls was added, but not all. Now, all the wasi hostcalls have been included, and their stability has been verified in our multiple go 1.24 compiled wasm plugins

@johnlanni
Copy link
Author

cc @leonm1 @PiotrSikora

@leonm1
Copy link
Contributor

leonm1 commented May 9, 2025

Thank you for sending this PR! I will take a look at the code a bit later today, but I was wondering if any of the wasm plugins importing these host calls are public so I can take a look.

I'd be interested to see what logic causes the inclusion of them.

Copy link
Contributor

@leonm1 leonm1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. @PiotrSikora do you have any reservations against implementing the rest of the wasip1 surface?

@johnlanni
Copy link
Author

Thank you for sending this PR! I will take a look at the code a bit later today, but I was wondering if any of the wasm plugins importing these host calls are public so I can take a look.

I'd be interested to see what logic causes the inclusion of them.

Sorry for the late reply. Here are some examples of plugin code that has additional WASI dependencies. I've been encountering new WASI dependencies with different plugins, so I included all of them:
https://github.com/alibaba/higress/tree/main/plugins/wasm-go/mcp-servers/all-in-one
https://github.com/alibaba/higress/tree/main/plugins/wasm-go/extensions/ext-auth

Convert to wat and see the dependencies on some previously unsupported wasi:
image

@johnlanni
Copy link
Author

@leonm1 There are two more important changes:

  1. Allow calling poll_oneoff during malloc, because Go calls sleep during GC
  2. The implementation of poll_oneoff has been changed to no-op instead of error, as the previous implementation would cause a panic during GC

Comment on lines +173 to +176
Word wasi_unstable_sock_accept(Word, Word, Word);
Word wasi_unstable_sock_recv(Word, Word, Word, Word, Word, Word);
Word wasi_unstable_sock_send(Word, Word, Word, Word, Word);
Word wasi_unstable_sock_shutdown(Word, Word);
Copy link
Member

@PiotrSikora PiotrSikora May 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably, those socket functions are not "randomly" imported by the Go runtime, and if we add stubs for them, then we're replacing load time failure with a runtime failure.

There is a difference between existing functions (bare minimum required by runtimes to load targets compiled to a WASI architecture) vs adding everything without implementing it.

Why are those needed?

Word wasi_unstable_random_get(Word, Word);
Word pthread_equal(Word left, Word right);
void emscripten_notify_memory_growth(Word);
Word wasi_unstable_path_filestat_get(Word fd, Word flags, Word path, Word path_len, Word buf);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this should be grouped with other wasi_unstable_path_* functions.

Comment on lines +1135 to +1138
// Set the number of bytes read to 0
if (!context->wasmVm()->setWord(retptr0, Word(0))) {
return 21; // __WASI_EFAULT
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general convention is that return values aren't expected to be legitimate if the function returns error, so this shouldn't be needed.

Comment on lines +939 to +949
// fd_datasync is used to synchronize the data of a file to disk.
// Since we don't have a real file system in proxy-wasm, we can just return success for stdout/stderr
// and an error for other file descriptors.

// We only support stdout and stderr in proxy-wasm
if (fd != 1 /* stdout */ && fd != 2 /* stderr */) {
return 8; // __WASI_ERRNO_BADF - Bad file descriptor
}

// For stdout and stderr, there's no need to sync as they're handled by the host system
return 0; // __WASI_ESUCCESS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is repeated a lot, but are those function actually called for stdout and stderr or is it simply copy & paste? Some of those would be rather surprising.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants