如何调用 `exec` 来从一个 WASM 程序链加载另一个程序?

huangapple go评论76阅读模式
英文:

How can I invoke `exec` to chainload one WASM program from another?

问题

在WASM中如何调用exec?当我尝试下面的示例时,它会报错45(操作不支持)。是否有一些标志可以启用execcallee.wasm是否是要执行的正确文件?

终端:

> emcc callee.c -o callee.wasm
> emcc caller.c --embed-file callee.wasm -o index.html
> # 将通过index.html使用,但是对于开发来说,node更快
> # 注意:MEMFS工作目录是“/”,并且存在“/callee.wasm”
> # https://emscripten.org/docs/api_reference/Filesystem-API.html
> node index.js
Caller
Caller: 45

caller.c

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int main() {
  printf("Caller\n");

  char *args[] = {"./callee.wasm", NULL};
  execvp(args[0], args);

  printf("Caller: %d\n", errno);
}

callee.c

#include <stdio.h>

int main() {
  printf("Success!\n");
}
英文:

How can I invoke exec in WASM? When I try the example below, it gives error 45 (Operation not supported). Is there some flag to enable exec? Is callee.wasm not the right file to exec?

Terminal:

> emcc callee.c -o callee.wasm
> emcc caller.c --embed-file callee.wasm -o index.html
> # will be used through index.html, but node is faster for development
> # note: MEMFS working directory is "/" and "/callee.wasm" exists
> # https://emscripten.org/docs/api_reference/Filesystem-API.html
> node index.js
Caller
Caller: 45

caller.c

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

int main() {
  printf("Caller\n");

  char *args[] = {"./callee.wasm", NULL};
  execvp(args[0], args);

  printf("Caller: %d\n", errno);
}

callee.c

#include <stdio.h>

int main() {
  printf("Success!\n");
}

It should be made clear that this is simplified version, so do not suggest putting printf("Success!\n"); in caller.c and avoiding exec entirely.

答案1

得分: 3

以下是翻译好的部分:

"Point of order first: errno 45 is not EOPNOTSUPP under Emscripten as the question would suggest; it is, in fact, ENOEXEC (see arch/emscripten/bits/errno.h and wasi/api.h). errno numbers are generally not consistent between platforms, and there is no reason why errno numbers in a BSD libc would agree with Emscripten libc.

And the Emscripten libc hardcodes execve to always fail with ENOEXEC, and the entire exec* family of libc calls ultimately ends up there, including execvp. The symbol is declared as weak, however, so you can in principle replace the default implementation with your own. How you are going to accomplish that, I leave up to you."

英文:

It seems you can’t, short of reimplementing exec yourself.

Point of order first: errno 45 is not EOPNOTSUPP under Emscripten as the question would suggest; it is, in fact, ENOEXEC (see arch/emscripten/bits/errno.h and wasi/api.h). errno numbers are generally not consistent between platforms, and there is no reason why errno numbers in a BSD libc would agree with Emscripten libc.

And the Emscripten libc hardcodes execve to always fail with ENOEXEC, and the entire exec* family of libc calls ultimately ends up there, including execvp. The symbol is declared as weak, however, so you can in principle replace the default implementation with your own. How you are going to accomplish that, I leave up to you.

huangapple
  • 本文由 发表于 2023年4月11日 13:03:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定