简体   繁体   English

如何在使用 emcc 从 c++ 编译的 wee8 wasm 代码中运行? (wee8 的 WASI?)

[英]How to run in wee8 wasm code that was compiled from c++ with emcc? (WASI in wee8?)

I am trying to compile C++ code to wasm and then embed it in other C++ code with wee8 (v8's wasm-api).我正在尝试将 C++ 代码编译为 wasm,然后使用 wee8(v8 的 wasm-api)将其嵌入到其他 C++ 代码中。 Currently I'm getting a Segfault on instantiating the module:目前我在实例化模块时遇到段错误:

    auto instance = wasm::Instance::make(store, module.get(), imports);

Note that I have no problem embedding code that I write as .wat and convert to .wasm , so the problem is specifically with embedding code compiled with emcc .请注意,嵌入我编写为.wat并转换为.wasm的代码没有问题,因此问题特别在于嵌入使用emcc编译的代码。 I am guessing that what I'm missing is WASI support in wee8?我猜我缺少的是 wee8 中的 WASI 支持? Does it exist?它存在吗? How can I enable it?我怎样才能启用它? Alternatively: can I ask emcc not to generate any WASI calls?或者:我可以要求 emcc 不生成任何 WASI 调用吗?

Here is a minimal example which results in:这是一个最小的例子,结果是:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

In cpp42.cpp:在 cpp42.cpp 中:

int main() {
    return 42;
}

Compiling this to wasm with:将其编译为 wasm:

emcc -O3 cpp42.cpp -o cpp42.wasm

Inspecting the compiled wasm module with wabt 's wasm2wat shows that it contains the following import使用wabtwasm2wat检查已编译的 wasm 模块表明它包含以下导入

  (import "wasi_snapshot_preview1" "proc_exit" (func (;0;) (type 0)))

Which I suspect to be the cause of the problem.我怀疑这是问题的原因。

Then embedding with wee8 like in the examples in the repo and like I do with other wasm files causes the segfault mentioned above.然后像 repo 中的示例一样嵌入 wee8,就像我对其他 wasm 文件所做的那样会导致上述段错误。

Just as another check: running就像另一个检查:运行

wasmer cpp42.wasm 
echo $?
> 42

Works without a problem.工作没有问题。

I can answer part of your question:我可以回答你的部分问题:

I'm missing is WASI support in wee8?我缺少 wee8 中的 WASI 支持吗? Does it exist?它存在吗?

No, wee8 does not implement WASI.不,wee8 没有实现 WASI。 Adding such support is theoretically possible, but not currently scheduled to get done.添加此类支持在理论上是可能的,但目前尚未计划完成。

You can implement it yourself in your wee8 embedder, and make it available to loaded modules via imports.你可以在你的 wee8 嵌入器中自己实现它,并通过导入使其可用于加载的模块。 Most (or all?) of it could probably be a reusable (among many engine implementations) library, potentially offered and maintained by the WASI project itself.它的大部分(或全部?)可能是一个可重用的(在许多引擎实现中)库,可能由 WASI 项目本身提供和维护。 (I don't know whether such a library exists already.) (我不知道这样的图书馆是否已经存在。)

You didn't say what imports object you're currently passing;您没有说您当前传递的是什么imports object; it needs to be an array of wasm::Extern* pointers that's at least as long as the imports of the module , and ordered equivalently (ie imports[i] will be the module 's i th import).它需要是一个wasm::Extern*指针数组,至少与module的导入一样长,并且顺序相同(即imports[i]将是module的第i个导入)。

(I agree that the Wasm C/C++ API is very barebones currently. Unless/until that is changed, you'll have to build any convenience mechanisms yourself. It's all possible with the information that's available, it's just clearly less convenient than instantiating Wasm modules from JavaScript.) (我同意 Wasm C/C++ API 目前非常准系统。除非/直到它被改变,你必须自己构建任何便利机制。可用的信息都是可能的,它显然不如实例化 Wasm 方便来自 JavaScript 的模块。)

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

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