yomaytk / elfconv

An experimental AOT compiler that translates Linux ELF binary to WebAssembly
Apache License 2.0
148 stars 6 forks source link

Compiling to wasm32-wasi and wasm32-freestanding? #1

Open jedisct1 opened 6 months ago

jedisct1 commented 6 months ago

Hi,

And congrats for this amazing and very useful project.

Instead of emscripten, is there a way to use zig cc in order to produce WebAssembly modules that run on server environments with and without WASI, and with specific WASM runtime optimizations (--target=wasm32-wasi -mcpu=baseline+simd128+bulk_memory)?

Thank you!

yomaytk commented 6 months ago

I have never used zig cc so I don't know if we could compile instead of emscripten or not. However, the generated file by elfconv is a normal LLVM bitcode, so I think it can be compiled by the LLVM-based compiler. In bin/elfconv.sh, $EMCC and $EMAR are the paths of the compiler and the archive command, so you might be able to compile by replacing them with those commands of zig.

Please tell me if you have a use case or the demands for using zig instead of emscripten.

jedisct1 commented 6 months ago

Hi, and thanks for your response.

I'd like to run linux binaries (originally written in OCaml) on serverless environments such as FAASM and Fastly Compute, that don't support the Emscripten interface.

I changed EMCC/EMAR/EMCCFLAGS to:

EMCC="zig c++"
EMAR="zig ar"
EMCCFLAGS="-Os -I${ROOT_DIR}/backend/remill/include -fno-exceptions --target=wasm32-wasi -D_WASI_EMULATED_GETPID -D_WASI_EMULATED_SIGNAL -DWASI_EMULATED_PROCESS_CLOCKS -lwasi-emulated-getpid -lwasi-emulated-signal -lwasi-emulated-process-clocks"

And had to replace tcgetattr(), kill() and sigaction() with stubs returning an error (these are not support by WASI)

Wasm files got created with SERVER=1 ./elfconv.sh ../examples/hello/a.out, but then wasmedge is unable to run them (tried with the "hello" example)

$ wasmedge exe.wasm
[2024-01-12 18:09:13.808] [error] execution failed: unreachable, Code: 0x89
[2024-01-12 18:09:13.809] [error]     In instruction: unreachable (0x00) , Bytecode offset: 0x01193113
[2024-01-12 18:09:13.809] [error]     When executing function name: "_start"

Maybe due to exceptions, that are not supported yet, but that Emscripten somehow emulates?

yomaytk commented 6 months ago

I'd like to run linux binaries (originally written in OCaml) on serverless environments such as FAASM and Fastly Compute, that don't support the Emscripten interface.

I got it. We use only emscripten for the backend, but we can use wasi-sdk to generate WASM only in the case of the host environment, so if possible, please consider that as well, but I may add the support of zig.

And had to replace tcgetattr(), kill() and sigaction() with stubs returning an error (these are not support by WASI) Wasm files got created with SERVER=1 ./elfconv.sh ../examples/hello/a.out, but then wasmedge is unable to run them (tried with the "hello" example)

Maybe due to exceptions, that are not supported yet, but that Emscripten somehow emulates?

There are many Linux system calls that elfconv doesn't support now, and I will implement those. Actually, examples/hello cannot be executed in the case of the host environment because of the no support of the part of Linux system calls (e.g. readlinkat), but I will deal with that as soon as possible, sorry.🙏 As you say, it seems that emscripten targeting the browser implements many libc functions of the system calls not supported by WASI, so we can easily implement many system calls, unlike the case host environment. That's why examples/hello can be executed on the browser but cannot on the host environment now.