Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeAOT-LLVM: Init callback declaration #2364

Merged

Conversation

yowl
Copy link
Contributor

@yowl yowl commented Jul 30, 2023

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.

WebAssembly/wasi-libc#74
WebAssembly/WASI#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.

@jkotas jkotas added the area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly) label Jul 30, 2023
@yowl yowl force-pushed the init-callback-declaration branch from 0f9e48b to 8c78dc0 Compare July 30, 2023 16:35
@yowl yowl force-pushed the init-callback-declaration branch from 8c78dc0 to e9d1f02 Compare July 30, 2023 16:38
@yowl
Copy link
Contributor Author

yowl commented Jul 30, 2023

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 yowl marked this pull request as ready for review August 2, 2023 01:18
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. @SingleAccretion ?

Copy link

@SingleAccretion SingleAccretion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code changes LGTM, but I do have a question about wasmtime's behavior w.r.t. _initialize.

On the surface of things, bytecodealliance/wasmtime#1565 claims to execute for it reactors. Why does it not work in this case?

@yowl
Copy link
Contributor Author

yowl commented Aug 3, 2023

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 .

@jkotas jkotas merged commit b355cea into dotnet:feature/NativeAOT-LLVM Aug 3, 2023
11 checks passed
@yowl yowl deleted the init-callback-declaration branch November 27, 2023 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-LLVM LLVM generation for Native AOT compilation (including Web Assembly)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants