【问题标题】:How to call WASI functions directly in C using wasi-sdk (import module problem)如何使用wasi-sdk在C中直接调用WASI函数(导入模块问题)
【发布时间】:2020-07-24 11:13:18
【问题描述】:

我正在尝试使用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 导入(我没有第一个的源代码,我只是在开源代码中看到它,第二个见示例@ 987654322@,但我不确定在 C 中该怎么做。有什么想法吗?

【问题讨论】:

    标签: c webassembly wasi


    【解决方案1】:

    wasi-libcsource 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 错误,但至少我现在知道如何指定导入路径。

    【讨论】:

    • 从那以后你有进步吗?我正在尝试做同样的事情,但使用自定义函数。
    【解决方案2】:

    所以我做了这个工作:

    #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)
    

    【讨论】:

      猜你喜欢
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多