Skip to content

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 +177
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?

Copy link
Author

Choose a reason for hiding this comment

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

The Go dependencies might include these, but they are not actually executed in the plugin logic. However, without these stubs, the plugin cannot be loaded correctly.

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

src/exports.cc Outdated
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.

Copy link
Author

@johnlanni johnlanni Sep 23, 2025

Choose a reason for hiding this comment

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

done

src/exports.cc Outdated
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.

Copy link
Author

Choose a reason for hiding this comment

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

done

@shukitchan
Copy link

In my example use cases here - https://github.com/shukitchan/ats-wasm-ai-inference/
I find that I need this PR. Otherwise, I am getting the same error as in #308

Signed-off-by: zty98751 <[email protected]>
Signed-off-by: johnlanni <[email protected]>
Signed-off-by: johnlanni <[email protected]>
@johnlanni johnlanni force-pushed the add-all-wasi-hostcalls branch from e94334a to f3d5c5a Compare September 23, 2025 03:18
@mpwarres
Copy link
Contributor

Thanks for the updates @johnlanni ! It looks like the current presubmit check failures are due to the Github action runners exhausting their allocated disk space. I am looking into obtaining more storage for those.

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.

5 participants