C中如何使用wasi-sdk直接调用WASI函数(导入模块问题)

How to call WASI functions directly in C using wasi-sdk (import module problem)

我正在尝试使用 wasi-sdk 在 C 程序中直接调用 WASI 函数 (fd_write)。这是图书馆 (lib.c):

#include <stdint.h>

struct Ciovec
{
    uint8_t *buf;
    uint32_t buf_len;
};

uint16_t fd_write(uint32_t fd, struct Ciovec *iovs_ptr, uint32_t iovs_len, uint32_t *nwritten);

static char *str = "just testing\n";

void c_main()
{
    struct Ciovec vec = { .buf = (uint8_t*)str, .buf_len = 13 };
    uint32_t nwritten = 0;
    fd_write(1, &vec, 1, &nwritten);
}

现在,如果我使用 wasi-sdk 将其构建为可静态链接的库

~/wasi-sdk-11.0/bin/clang --sysroot ~/wasi-sdk-11.0/share/wasi-sysroot/ lib.c -c -o lib.o -fpic

我得到这个 wat:(wasm2wat --enable-all 输出)

(module
  (type (;0;) (func))
  (type (;1;) (func (param i32 i32 i32 i32) (result i32)))
  (import "env" "__linear_memory" (memory (;0;) 1))
  (import "env" "__indirect_function_table" (table (;0;) 0 funcref))
  (import "env" "__stack_pointer" (global (;0;) (mut i32)))
  (import "env" "fd_write" (func (;0;) (type 1)))
  ...)

现在 fd_write 的导入行不正确。据我所知,WASI 函数需要从 wasi_snapshot_preview1wasi_unstable 导入(我没有第一个的源代码,我只是在开源代码中看到它,第二个请参阅示例 here,但我不确定在 C 中该怎么做。有什么想法吗?

wasi-libc source code中找到答案:

uint16_t fd_write(uint32_t fd, struct Ciovec *iovs_ptr, uint32_t iovs_len, uint32_t *nwritten)
    __attribute__((
                __import_module__("wasi_snapshot_preview1"),
                __import_name__("fd_write"),
                ));

生成的 Wasm:

  (import "wasi_snapshot_preview1" "fd_write" (func $fd_write (type 1)))

虽然这仍然不起作用,我收到 undefined symbol: fd_write 错误,但至少我现在知道如何指定导入路径了。

所以我让它起作用了:

#include <stdint.h>
#include <stdio.h>

extern uint16_t my_func()
__attribute__((
    __import_module__("my_module"),
    __import_name__("my_func"),
));

int main(){
    my_func();
    return 0;
}

-Wl,--allow-undefined

编译

生成

(module
  (type (;0;) (func (param i32 i32 i32) (result i32)))
  (type (;1;) (func (param i32 i64 i32) (result i64)))
  (type (;2;) (func (param i32 i32) (result i32)))
  (type (;3;) (func (result i32)))
  (type (;4;) (func (param i32)))
  (type (;5;) (func (param i32) (result i32)))
  (type (;6;) (func (param i32 i32 i32 i32) (result i32)))
  (type (;7;) (func (param i32 i64 i32 i32) (result i32)))
  (type (;8;) (func))
  (import "wasi_unstable" "fd_prestat_get" (func $__wasi_fd_prestat_get (type 2)))
  (import "wasi_unstable" "fd_prestat_dir_name" (func $__wasi_fd_prestat_dir_name (type 0)))
  (import "my_module" "my_func" (func $my_func__ (type 3)))
  (import "wasi_unstable" "proc_exit" (func $__wasi_proc_exit (type 4)))
  (import "wasi_unstable" "fd_fdstat_get" (func $__wasi_fd_fdstat_get (type 2)))
  (import "wasi_unstable" "fd_close" (func $__wasi_fd_close (type 5)))
  (import "wasi_unstable" "fd_write" (func $__wasi_fd_write (type 6)))
  (import "wasi_unstable" "fd_seek" (func $__wasi_fd_seek (type 7)))
  (func $__wasm_call_ctors (type 8))
  (func $undefined:__wasilibc_populate_environ (type 3) (result i32)