From e9d1f02a056608b703681d088ff35016dd07600c Mon Sep 17 00:00:00 2001 From: yowl00 Date: Sun, 30 Jul 2023 11:37:58 -0500 Subject: [PATCH 1/4] add _initialize rationale and cleanup NativeAOT_StaticInitialization --- src/coreclr/nativeaot/Bootstrap/main.cpp | 35 +++++++++---------- .../Microsoft.NETCore.Native.Unix.targets | 1 - .../Microsoft.NETCore.Native.Windows.targets | 1 - .../Microsoft.NETCore.Native.targets | 1 - src/coreclr/nativeaot/Runtime/thread.cpp | 7 +--- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/coreclr/nativeaot/Bootstrap/main.cpp b/src/coreclr/nativeaot/Bootstrap/main.cpp index 4e73f89e3fd9..39854cf603fc 100644 --- a/src/coreclr/nativeaot/Bootstrap/main.cpp +++ b/src/coreclr/nativeaot/Bootstrap/main.cpp @@ -94,7 +94,6 @@ static char& __unbox_z = __stop___unbox; #endif // _MSC_VER extern "C" bool RhInitialize(); -extern "C" void RhSetRuntimeInitializationCallback(int (*fPtr)()); extern "C" bool RhRegisterOSModule(void * pModule, void * pvManagedCodeStartRange, uint32_t cbManagedCodeRange, @@ -169,10 +168,21 @@ extern "C" int __managed__Main(int argc, char* argv[]); #else #define NATIVEAOT_ENTRYPOINT __managed__Startup extern "C" void __managed__Startup(); +// _initialize is a function generated by the WASI SDK libc that calls the LLVM synthesized __wasm_call_ctors function for reactor components: +// https://github.com/WebAssembly/wasi-libc/blob/9f51a7102085ec6a6ced5778f0864c9af9f50000/libc-bottom-half/crt/crt1-reactor.c#L7-L27 +// We define and call it for NATIVEAOT_DLL and TARGET_WASI to call all the global c++ static constructors. This ensures the runtime is intialized +// when calling into WebAssembly Component Model components +#if defined(TARGET_WASI) +extern "C" void _initialize(); +#endif // TARGET_WASI #endif // !NATIVEAOT_DLL static int InitializeRuntime() { +#if defined(NATIVEAOT_DLL) && defined(TARGET_WASI) + _initialize(); +#endif + if (!RhInitialize()) return -1; @@ -200,6 +210,12 @@ static int InitializeRuntime() return 0; } +#ifdef NATIVEAOT_DLL +int (*g_RuntimeInitializationCallback)() = &InitializeRuntime; +#else +int (*g_RuntimeInitializationCallback)() = nullptr; +#endif + #ifndef NATIVEAOT_DLL #ifdef ENSURE_PRIMARY_STACK_SIZE @@ -229,20 +245,3 @@ int main(int argc, char* argv[]) return __managed__Main(argc, argv); } #endif // !NATIVEAOT_DLL - -#ifdef NATIVEAOT_DLL -static struct InitializeRuntimePointerHelper -{ - InitializeRuntimePointerHelper() - { - RhSetRuntimeInitializationCallback(&InitializeRuntime); - } -} initializeRuntimePointerHelper; - -extern "C" void* NativeAOT_StaticInitialization(); - -void* NativeAOT_StaticInitialization() -{ - return &initializeRuntimePointerHelper; -} -#endif // NATIVEAOT_DLL diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets index f88b9a2e0ac9..29d97fa8866c 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets @@ -142,7 +142,6 @@ The .NET Foundation licenses this file to you under the MIT license. - diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets index 9512bf826c40..8c912da082d9 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets @@ -87,7 +87,6 @@ The .NET Foundation licenses this file to you under the MIT license. - diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets index 1f4bdbc95e52..8a5f8de59d25 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets @@ -508,7 +508,6 @@ The .NET Foundation licenses this file to you under the MIT license. - diff --git a/src/coreclr/nativeaot/Runtime/thread.cpp b/src/coreclr/nativeaot/Runtime/thread.cpp index 4b0ed2f9c0f5..f0e925ce5853 100644 --- a/src/coreclr/nativeaot/Runtime/thread.cpp +++ b/src/coreclr/nativeaot/Runtime/thread.cpp @@ -33,7 +33,7 @@ EXTERN_C NATIVEAOT_API void* REDHAWK_CALLCONV RhpHandleAlloc(void* pObject, int EXTERN_C NATIVEAOT_API void REDHAWK_CALLCONV RhHandleSet(void* handle, void* pObject); EXTERN_C NATIVEAOT_API void REDHAWK_CALLCONV RhHandleFree(void* handle); -static int (*g_RuntimeInitializationCallback)(); +extern int (*g_RuntimeInitializationCallback)(); static Thread* g_RuntimeInitializingThread; #endif //!DACCESS_COMPILE @@ -1173,11 +1173,6 @@ FORCEINLINE bool Thread::InlineTryFastReversePInvoke(ReversePInvokeFrame * pFram return true; } -EXTERN_C void RhSetRuntimeInitializationCallback(int (*fPtr)()) -{ - g_RuntimeInitializationCallback = fPtr; -} - void Thread::ReversePInvokeAttachOrTrapThread(ReversePInvokeFrame * pFrame) { if (!IsStateSet(TSF_Attached)) From 750a835176be1b99060c7dcf62c7114aa486ad92 Mon Sep 17 00:00:00 2001 From: yowl00 Date: Mon, 31 Jul 2023 21:51:32 -0500 Subject: [PATCH 2/4] add weak function for _initialize when it is called from a CustomNativeMain --- src/coreclr/nativeaot/Bootstrap/main.cpp | 7 ++++++- .../BuildIntegration/Microsoft.NETCore.Native.targets | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/coreclr/nativeaot/Bootstrap/main.cpp b/src/coreclr/nativeaot/Bootstrap/main.cpp index 39854cf603fc..cfe9c1a14bd0 100644 --- a/src/coreclr/nativeaot/Bootstrap/main.cpp +++ b/src/coreclr/nativeaot/Bootstrap/main.cpp @@ -172,8 +172,13 @@ extern "C" void __managed__Startup(); // https://github.com/WebAssembly/wasi-libc/blob/9f51a7102085ec6a6ced5778f0864c9af9f50000/libc-bottom-half/crt/crt1-reactor.c#L7-L27 // We define and call it for NATIVEAOT_DLL and TARGET_WASI to call all the global c++ static constructors. This ensures the runtime is intialized // when calling into WebAssembly Component Model components -#if defined(TARGET_WASI) +#if defined(NATIVEAOT_DLL) && defined(TARGET_WASI) extern "C" void _initialize(); + +__attribute__((weak)) void _initialize() +{ +} + #endif // TARGET_WASI #endif // !NATIVEAOT_DLL diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets index 8a5f8de59d25..389f7cef6408 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets @@ -522,7 +522,7 @@ The .NET Foundation licenses this file to you under the MIT license. - + From bba553ec221d91f6cd53d61f7fbb2633d81c78fa Mon Sep 17 00:00:00 2001 From: yowl00 Date: Tue, 1 Aug 2023 20:18:02 -0500 Subject: [PATCH 3/4] add comment for __weak rationale. --- src/coreclr/nativeaot/Bootstrap/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/coreclr/nativeaot/Bootstrap/main.cpp b/src/coreclr/nativeaot/Bootstrap/main.cpp index cfe9c1a14bd0..5908d92d46a0 100644 --- a/src/coreclr/nativeaot/Bootstrap/main.cpp +++ b/src/coreclr/nativeaot/Bootstrap/main.cpp @@ -175,6 +175,8 @@ extern "C" void __managed__Startup(); #if defined(NATIVEAOT_DLL) && defined(TARGET_WASI) extern "C" void _initialize(); +// CustomNativeMain programs are built using the same libbootstrapperdll as NATIVEAOT_DLL but wasi-libc will not provide an _initialize implemeentation, +// so create a dummy one here and make it weak to allow wasi-libc to provide the real implmentation for WASI reactor components. __attribute__((weak)) void _initialize() { } From c2d89ecb19b855dc11e3acb9249bca12ea24f871 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 1 Aug 2023 21:36:06 -0700 Subject: [PATCH 4/4] Apply suggestions from code review --- src/coreclr/nativeaot/Bootstrap/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coreclr/nativeaot/Bootstrap/main.cpp b/src/coreclr/nativeaot/Bootstrap/main.cpp index 5908d92d46a0..c713a1c1e08d 100644 --- a/src/coreclr/nativeaot/Bootstrap/main.cpp +++ b/src/coreclr/nativeaot/Bootstrap/main.cpp @@ -170,13 +170,13 @@ extern "C" int __managed__Main(int argc, char* argv[]); extern "C" void __managed__Startup(); // _initialize is a function generated by the WASI SDK libc that calls the LLVM synthesized __wasm_call_ctors function for reactor components: // https://github.com/WebAssembly/wasi-libc/blob/9f51a7102085ec6a6ced5778f0864c9af9f50000/libc-bottom-half/crt/crt1-reactor.c#L7-L27 -// We define and call it for NATIVEAOT_DLL and TARGET_WASI to call all the global c++ static constructors. This ensures the runtime is intialized +// We define and call it for NATIVEAOT_DLL and TARGET_WASI to call all the global c++ static constructors. This ensures the runtime is initialized // when calling into WebAssembly Component Model components #if defined(NATIVEAOT_DLL) && defined(TARGET_WASI) extern "C" void _initialize(); -// CustomNativeMain programs are built using the same libbootstrapperdll as NATIVEAOT_DLL but wasi-libc will not provide an _initialize implemeentation, -// so create a dummy one here and make it weak to allow wasi-libc to provide the real implmentation for WASI reactor components. +// CustomNativeMain programs are built using the same libbootstrapperdll as NATIVEAOT_DLL but wasi-libc will not provide an _initialize implementation, +// so create a dummy one here and make it weak to allow wasi-libc to provide the real implementation for WASI reactor components. __attribute__((weak)) void _initialize() { }