简体   繁体   English

WASI 函数的 ABI 格式

[英]ABI format of WASI functions

I am building a Webassembly runtime and am currently implementing the WASI APIs.我正在构建一个 Webassembly 运行时,目前正在实现 WASI API。 I'm wondering how the ABI looks like, according to this document: https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md根据本文档,我想知道 ABI 的样子: https : //github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md

To test, I have compiled this C application with emscripten to a standalone WASM module.为了进行测试,我使用 emscripten 将这个 C 应用程序编译为一个独立的 WASM 模块。

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

After checking with wasm-objdump, I can see the following function imports:使用 wasm-objdump 检查后,我可以看到以下函数导入:

Import[2]:
 - func[0] sig=2 <__wasi_proc_exit> <- wasi_snapshot_preview1.proc_exit
 - func[1] sig=11 <__wasi_fd_write> <- wasi_snapshot_preview1.fd_write

with the type signatures:带有类型签名:

 - type[2] (i32) -> nil
 - type[11] (i32, i32, i32, i32) -> i32

According to the spec, the fd_write function has the signature fd_write(fd: fd, iovs: ciovec_array) -> Result<size, errno> , which maps to the POSIX system call ssize_t writev(int fd, const struct iovec *iov, int iovcnt);根据规范, fd_write函数具有签名fd_write(fd: fd, iovs: ciovec_array) -> Result<size, errno> ,它映射到 POSIX 系统调用ssize_t writev(int fd, const struct iovec *iov, int iovcnt); . .

But what is the fourth argument in the WASM file?但是 WASM 文件中的第四个参数是什么? It receives some pointer to a memory address.它接收一些指向内存地址的指针。 So I thought I would have to write Result<size, errno> to that address, but if I do that and return 0 (for success), the fd_write gets called just over and over again (presumably because the printf function assumes that nothing was written).所以我想我必须将Result<size, errno>写入该地址,但是如果我这样做并返回 0(成功),则fd_write会一遍fd_write被调用(大概是因为 printf 函数假定没有书面)。 If I return the written bytes, the program terminates correctly, but what is then the fourth argument?如果我返回写入的字节,程序会正确终止,但是第四个参数是什么? Also, how can I return more complex Result s, which do not fit into an i32?另外,如何返回不适合 i32 的更复杂的Result

In wasi-libc , the function signature of `__wasi_fd_read iswasi-libc 中,`__wasi_fd_read 的函数签名是

__wasi_errno_t __wasi_fd_read( __wasi_fd_t fd, const __wasi_iovec_t *iovs, size_t iovs_len, __wasi_size_t *retptr0 )

According to some implementation of fd_write, the last argument is a pointer to return the number of bytes written, and the return value is always 0 (like what you did).根据fd_write的一些实现,最后一个参数是一个返回写入字节数的指针,返回值总是0(就像你所做的那样)。

So I guess you shold also set the number of bytes that have been read to where retptr0 points to.所以我猜你还应该将已读取的字节数设置为retptr0指向的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM