dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
MIT License
1.42k stars 199 forks source link

NativeAOT-LLVM: Init callback declaration #2364

Closed yowl closed 1 year ago

yowl commented 1 year ago

This PR accepts the recommendation from NativeAOT_StaticInitialization and teherby removes NativeAOT_StaticInitialization and its initialisation. This enables the runtime to be initialised without the use of static c++ ctors which are not invoked presently for WASI reactor components.

https://github.com/WebAssembly/wasi-libc/pull/74 https://github.com/WebAssembly/WASI/issues/13

Instead the initialisation from a WASI reactor component is done via the wasi-libc _initialize function. The call to this function is done in /Bootstrap/main.cpp which is#if defed in for WASI and NATIVEAOT_DLL

WASI reactor components can now be compiled and executed (although the WIT codegen is not ready yet).

This change does introduce the problem of intialisation of CustomNativeMain from a WASI program as it is not distnguishable from a wasi reactor compoent, and the crt1.o for the program case does not contain _initialize, _intialize is only present for the reactor version of crt1 https://github.com/WebAssembly/wasi-libc/blob/9f51a7102085ec6a6ced5778f0864c9af9f50000/libc-bottom-half/crt/crt1-reactor.c#L7-L27

To solve the problem of no _initialize for WASI + NATIVEAOT_DLL from a CustomNativeMain I added a __weak dummy function.

yowl commented 1 year ago

Worth mentioning here is what node.js does.

At the bottom of this document https://nodejs.org/api/wasi.html it describes how the node.js runtime initializes WASI modules/compoents. Ive not tested this because I have been focusing on wasmtime because it looks to be leading the way in terms of WIT support and wasm component linking and running. However on the surface it looks like with this change, if node.js was used as the host, then _initialize would be called twice resulting in failure.

yowl commented 1 year ago

Yes, a good question, I will try to get a more definitive answer and update here, but my current understanding is that wasmtime will call _initialize when it is invoking a WASI reactor module, but that is not the same thing as a WASM component (components are a lower level and dont require WASI). To invoke the c# wasm component it is passed to the instantiation of another module, as in the example https://docs.rs/wasmtime/latest/wasmtime/component/macro.bindgen.html .