From 97b2c35b693e246dd01560a66dbfef1851b607cf Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 21 Feb 2023 14:29:43 -0500 Subject: [PATCH 01/30] [tasks] Add auto runtime initialization template to LibraryBuilder --- .../msbuild/android/build/AndroidApp.targets | 1 + .../msbuild/common/LibraryBuilder.targets | 2 +- src/tasks/LibraryBuilder/LibraryBuilder.cs | 3 + .../Templates/CMakeLists.txt.template | 1 + src/tasks/LibraryBuilder/Templates/autoinit.c | 166 ++++++++++++++++++ 5 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 src/tasks/LibraryBuilder/Templates/autoinit.c diff --git a/src/mono/msbuild/android/build/AndroidApp.targets b/src/mono/msbuild/android/build/AndroidApp.targets index d40e5a2784004b..7fb35a9c4d3bfe 100644 --- a/src/mono/msbuild/android/build/AndroidApp.targets +++ b/src/mono/msbuild/android/build/AndroidApp.targets @@ -85,6 +85,7 @@ + diff --git a/src/mono/msbuild/common/LibraryBuilder.targets b/src/mono/msbuild/common/LibraryBuilder.targets index ae920b8dec07aa..eacc9e2cae7544 100644 --- a/src/mono/msbuild/common/LibraryBuilder.targets +++ b/src/mono/msbuild/common/LibraryBuilder.targets @@ -20,8 +20,8 @@ ExtraLinkerArguments="@(_ExtraLinkerArgs)" ExtraSources="@(_ExtraLibrarySources)" IsSharedLibrary="$(_IsSharedLibrary)" - Name="$(AssemblyName)" MonoRuntimeHeaders="$(_MonoHeaderPath)" + Name="$(AssemblyName)" OutputDirectory="$(BundleDir)" RuntimeIdentifier="$(RuntimeIdentifier)" RuntimeLibraries="@(_RuntimeLibraries)" diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 0b3489db177ff6..7b5fed50e618bc 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -239,6 +239,9 @@ private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, st .Replace("%AotObjects%", aotObjects) .Replace("%ExtraSources%", extraSources) .Replace("%LIBRARY_LINKER_ARGS%", linkerArgs)); + + File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), + Utils.GetEmbeddedResource("autoinit.c")); } private void WriteAssembliesToLoadList(List assemblies) diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index 1fa43e84a9edeb..b27caa82f44d01 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -13,6 +13,7 @@ set(DOTNET_AOT_OBJECTS set(DOTNET_EXTRA_SOURCES %ExtraSources% assembly_list.c + autoinit.c ) include_directories("%MonoInclude%") diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c new file mode 100644 index 00000000000000..14a1dbe905f202 --- /dev/null +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +// TODO grab the directory where the assemblies are placed on device and remove path hardcode +static char *bundle_path = "/data/user/0/net.dot.Android.Device_Emulator.Aot_Llvm.Test/files"; + +#define RUNTIMECONFIG_BIN_FILE "runtimeconfig.bin" + +void register_aot_modules (void); + +void +cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) +{ + free (args); + free (user_data); +} + +void register_bundled_modules () +{ + char *file_name = RUNTIMECONFIG_BIN_FILE; + int str_len = strlen (bundle_path) + strlen (file_name) + 1; // +1 is for the "/" + char *file_path = (char *)malloc (sizeof (char) * (str_len +1)); // +1 is for the terminating null character + int num_char = snprintf (file_path, (str_len + 1), "%s/%s", bundle_path, file_name); + struct stat buffer; + + assert (num_char > 0 && num_char == str_len); + + if (stat (file_path, &buffer) == 0) { + MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments)); + arg->kind = 0; + arg->runtimeconfig.name.path = file_path; + monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, file_path); + } else { + free (file_path); + } +} + +static MonoAssembly* +mono_droid_load_assembly (const char *name, const char *culture) +{ + char filename [1024]; + char path [1024]; + int res; + + int len = strlen (name); + int has_extension = len > 3 && name [len - 4] == '.' && (!strcmp ("exe", name + (len - 3)) || !strcmp ("dll", name + (len - 3))); + + // add extensions if required. + strlcpy (filename, name, sizeof (filename)); + if (!has_extension) { + strlcat (filename, ".dll", sizeof (filename)); + } + + if (culture && strcmp (culture, "")) + res = snprintf (path, sizeof (path) - 1, "%s/%s/%s", bundle_path, culture, filename); + else + res = snprintf (path, sizeof (path) - 1, "%s/%s", bundle_path, filename); + assert (res > 0); + + struct stat buffer; + if (stat (path, &buffer) == 0) { + MonoAssembly *assembly = mono_assembly_open (path, NULL); + assert (assembly); + return assembly; + } + return NULL; +} + +static MonoAssembly* +mono_droid_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) +{ + const char *name = mono_assembly_name_get_name (aname); + const char *culture = mono_assembly_name_get_culture (aname); + return mono_droid_load_assembly (name, culture); +} + +static unsigned char * +load_aot_data (MonoAssembly *assembly, int size, void *user_data, void **out_handle) +{ + *out_handle = NULL; + + char path [1024]; + int res; + + MonoAssemblyName *assembly_name = mono_assembly_get_name (assembly); + const char *aname = mono_assembly_name_get_name (assembly_name); + + res = snprintf (path, sizeof (path) - 1, "%s/%s.aotdata", bundle_path, aname); + assert (res > 0); + + int fd = open (path, O_RDONLY); + if (fd < 0) { + return NULL; + } + + void *ptr = mmap (NULL, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); + if (ptr == MAP_FAILED) { + close (fd); + return NULL; + } + + close (fd); + *out_handle = ptr; + return (unsigned char *) ptr; +} + +static void +free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) +{ + munmap (handle, size); +} + +void runtime_init_callback () +{ + register_aot_modules (); + + register_bundled_modules (); + + char *assemblyPath = bundle_path; + mono_set_assemblies_path ((assemblyPath && assemblyPath[0] != '\0') ? assemblyPath : "./"); + + mono_jit_set_aot_only (true); + + mono_install_assembly_preload_hook (mono_droid_assembly_preload_hook, NULL); + + // TODO test debug scenario +#if DEBUG_ENABLED + bool wait_for_debugger = false; + mono_debug_init (MONO_DEBUG_FORMAT_MONO); + if (wait_for_debugger) { + char* options[] = { "--debugger-agent=transport=dt_socket,server=y,address=0.0.0.0:55555" }; + mono_jit_parse_options (1, options); + } +#endif + + mono_install_load_aot_data_hook (load_aot_data, free_aot_data, NULL); + + mono_jit_init ("dotnet.android"); // Pass in via LibraryBuilder? + + // Load assemblies with UnmanagedCallersOnly exported methods + // TODO leverage get_loadable_assemblies and call mono_assembly_open on each + mono_assembly_open("Android.Device_Emulator.Aot_Llvm.Test.dll", NULL); +} + +void init_mono_runtime () +{ + mono_set_runtime_init_callback (&runtime_init_callback); +} + +void __attribute__((constructor)) +autoinit () +{ + init_mono_runtime (); +} From bc86f2b91c76f0e40ad60516dcb1031d02e7054b Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 7 Mar 2023 12:03:30 -0500 Subject: [PATCH 02/30] Grab shared library directory --- src/tasks/LibraryBuilder/Templates/autoinit.c | 23 ++++++++++++++++++- ...droid.Device_Emulator.Aot_Llvm.Test.csproj | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 14a1dbe905f202..6746a15a3811da 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -19,6 +19,11 @@ static char *bundle_path = "/data/user/0/net.dot.Android.Device_Emulator.Aot_Llv #define RUNTIMECONFIG_BIN_FILE "runtimeconfig.bin" void register_aot_modules (void); +bool monoeg_g_module_address (void *addr, char *file_name, size_t file_name_len, + void **file_base, char *sym_name, + size_t sym_name_len, void **sym_addr); +char *mono_path_resolve_symlinks(const char *path); +char *monoeg_g_path_get_dirname (const char *filename); void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) @@ -78,6 +83,22 @@ mono_droid_load_assembly (const char *name, const char *culture) return NULL; } +// Assumes that the dl containing this function is in the same directory as the assemblies to load. +static char * +assemblies_dir (void) +{ + static char *dl_dir_name = NULL; + char dl_filename[4096]; + + if (monoeg_g_module_address ((void *)assemblies_dir, dl_filename, sizeof (dl_filename), NULL, NULL, 0, NULL)) { + char *resolved_dl_filename = mono_path_resolve_symlinks (dl_filename); + dl_dir_name = monoeg_g_path_get_dirname (resolved_dl_filename); + free (resolved_dl_filename); + } + + return dl_dir_name; +} + static MonoAssembly* mono_droid_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) { @@ -128,7 +149,7 @@ void runtime_init_callback () register_bundled_modules (); - char *assemblyPath = bundle_path; + char *assemblyPath = assemblies_dir (); mono_set_assemblies_path ((assemblyPath && assemblyPath[0] != '\0') ? assemblyPath : "./"); mono_jit_set_aot_only (true); diff --git a/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj index 49ddce32380595..655b1b05edefdc 100644 --- a/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj +++ b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj @@ -3,6 +3,7 @@ Exe false true + true true $(NetCoreAppCurrent) Android.Device_Emulator.Aot_Llvm.Test.dll From bef3de9ecd31a4a79d9d556d112f85ce0e735575 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Wed, 8 Mar 2023 17:38:13 -0500 Subject: [PATCH 03/30] [tasks] Extend auto initialization template to account for customization --- .../msbuild/android/build/AndroidApp.targets | 3 +- .../msbuild/common/LibraryBuilder.targets | 5 +- src/tasks/LibraryBuilder/LibraryBuilder.cs | 105 +++++++++++++++++- .../LibraryBuilder/LibraryBuilder.csproj | 1 + src/tasks/LibraryBuilder/Templates/autoinit.c | 54 +++++---- 5 files changed, 133 insertions(+), 35 deletions(-) diff --git a/src/mono/msbuild/android/build/AndroidApp.targets b/src/mono/msbuild/android/build/AndroidApp.targets index 7fb35a9c4d3bfe..e1a5363d22fbe4 100644 --- a/src/mono/msbuild/android/build/AndroidApp.targets +++ b/src/mono/msbuild/android/build/AndroidApp.targets @@ -85,7 +85,8 @@ - + + diff --git a/src/mono/msbuild/common/LibraryBuilder.targets b/src/mono/msbuild/common/LibraryBuilder.targets index eacc9e2cae7544..2d54b6a07dd161 100644 --- a/src/mono/msbuild/common/LibraryBuilder.targets +++ b/src/mono/msbuild/common/LibraryBuilder.targets @@ -8,6 +8,8 @@ <_IsSharedLibrary>false <_IsSharedLibrary Condition="'$(NativeLib)' == 'shared'">true + <_UsesCustomRuntimeInitCallback>false + <_UsesCustomRuntimeInitCallback Condition="$(CustomRuntimeInitCallback) != ''">true @@ -25,7 +27,8 @@ OutputDirectory="$(BundleDir)" RuntimeIdentifier="$(RuntimeIdentifier)" RuntimeLibraries="@(_RuntimeLibraries)" - TargetOS="$(TargetOS)"> + TargetOS="$(TargetOS)" + UsesCustomRuntimeInitCallback="$(_UsesCustomRuntimeInitCallback)"> diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 7b5fed50e618bc..bec8d46b3b1d17 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -66,6 +66,26 @@ public bool IsSharedLibrary } } + /// + /// Determines whether or not the mono runtime auto initialization + /// tremplate, autoinit.c, is used. + /// + public bool UsesCustomRuntimeInitCallback { get; set; } + + public string? AssetsPath { get; set; } + + /// + /// + public ITaskItem[] AppContextKeys { get; set; } = Array.Empty(); + + /// + /// + public ITaskItem[] AppContextValues { get; set; } = Array.Empty(); + + /// + /// + public string? RuntimeConfigBinFile { get; set; } + public bool StripDebugSymbols { get; set; } /// @@ -113,16 +133,23 @@ public override bool Execute() GatherAotSourcesObjects(aotSources, aotObjects, extraSources, linkerArgs); GatherLinkerArgs(linkerArgs); + if (!UsesCustomRuntimeInitCallback) + { + WriteAutoInitializationFromTemplate(); + } + WriteCMakeFileFromTemplate(aotSources.ToString(), aotObjects.ToString(), extraSources.ToString(), linkerArgs.ToString()); OutputPath = BuildLibrary(); return true; } + + private List exportedAssemblies = new List(); + private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aotObjects, StringBuilder extraSources, StringBuilder linkerArgs) { List exportedSymbols = new List(); - List exportedAssemblies = new List(); bool hasExports = false; foreach (CompiledAssembly compiledAssembly in CompiledAssemblies) @@ -226,6 +253,79 @@ private static void WriteLinkerScriptFile(string exportsFile, List expor .Replace("%GLOBAL_SYMBOLS%", globalExports)); } + private void WriteAutoInitializationFromTemplate() + { + string appContextEnvVariables = GenerateAppContextEnvVariables(); + string assembliesLoader = GenerateAssembliesLoader(); + string assetsPath = GenerateAssetsPath(); + string runtimeConfig = GenerateRuntimeConfig(); + File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), + Utils.GetEmbeddedResource("autoinit.c") + .Replace("%APPCTX_ENV_VARIABLES%", appContextEnvVariables) + .Replace("%ASSEMBLIES_LOADER%", assembliesLoader) + .Replace("%ASSETS_PATH%", assetsPath) + .Replace("%RUNTIME_CONFIG%", runtimeConfig)); + } + + private string GenerateAssembliesLoader() + { + var assembliesLoader = new StringBuilder(); + foreach (string exportedAssembly in exportedAssemblies) + { + assembliesLoader.AppendLine($" mono_assembly_open(\"{exportedAssembly}\", NULL);"); + } + return assembliesLoader.ToString(); + } + + private string GenerateAssetsPath() + { + return AssetsPath ?? "DOTNET_ASSETS_PATH"; + } + + private string GenerateRuntimeConfig() + { + if (string.IsNullOrEmpty(RuntimeConfigBinFile)) + return " return;"; + + var runtimeConfig = new StringBuilder(); + runtimeConfig.Append($" char *file_name = {RuntimeConfigBinFile};"); + runtimeConfig.Append(" int str_len = strlen (bundle_path) + strlen (file_name) + 1; // +1 is for the \"/\""); + runtimeConfig.Append(" char *file_path = (char *)malloc (sizeof (char) * (str_len +1)); // +1 is for the terminating null character"); + runtimeConfig.Append(" int num_char = snprintf (file_path, (str_len + 1), \"%s/%s\", bundle_path, file_name);"); + runtimeConfig.Append(" struct stat buffer;\n"); + runtimeConfig.Append(" assert (num_char > 0 && num_char == str_len);\n"); + runtimeConfig.Append(" if (stat (file_path, &buffer) == 0) {"); + runtimeConfig.Append(" MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments));"); + runtimeConfig.Append(" arg->kind = 0;"); + runtimeConfig.Append(" arg->runtimeconfig.name.path = file_path;"); + runtimeConfig.Append(" monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, file_path);"); + runtimeConfig.Append(" } else {"); + runtimeConfig.Append(" free (file_path);"); + runtimeConfig.Append(" }"); + return runtimeConfig.ToString(); + } + + private string GenerateAppContextEnvVariables() + { + if (AppContextKeys.Length != AppContextValues.Length) + { + throw new LogAsErrorException($"'{nameof(AppContextKeys)}' length does not match '{nameof(AppContextValues)}' length. {AppContextKeys.Length} != {AppContextValues.Length}"); + } + + int numArgs = AppContextKeys.Length; + var appContextEnvVariables = new StringBuilder(); + + appContextEnvVariables.AppendLine($" const char **appctx_keys, **appctx_values = (char**)malloc({numArgs} * sizeof(char*));"); + for (int i = 0; i < numArgs; i++) + { + appContextEnvVariables.AppendLine($" appctx_keys[{i}] = \"{AppContextKeys[i]}\";"); + appContextEnvVariables.AppendLine($" appctx_values[{i}] = \"{AppContextValues[i]}\";"); + } + appContextEnvVariables.AppendLine($" monovm_initialize({numArgs}, appctx_keys, appctx_values);"); + + return appContextEnvVariables.ToString(); + } + private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, string extraSources, string linkerArgs) { // BundleDir @@ -239,9 +339,6 @@ private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, st .Replace("%AotObjects%", aotObjects) .Replace("%ExtraSources%", extraSources) .Replace("%LIBRARY_LINKER_ARGS%", linkerArgs)); - - File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), - Utils.GetEmbeddedResource("autoinit.c")); } private void WriteAssembliesToLoadList(List assemblies) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.csproj b/src/tasks/LibraryBuilder/LibraryBuilder.csproj index c2a8ae77a3c51e..bdd1543fd13a53 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.csproj +++ b/src/tasks/LibraryBuilder/LibraryBuilder.csproj @@ -19,6 +19,7 @@ + diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 6746a15a3811da..f18140758cef11 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -13,10 +13,7 @@ #include #include -// TODO grab the directory where the assemblies are placed on device and remove path hardcode -static char *bundle_path = "/data/user/0/net.dot.Android.Device_Emulator.Aot_Llvm.Test/files"; - -#define RUNTIMECONFIG_BIN_FILE "runtimeconfig.bin" +static char *bundle_path; void register_aot_modules (void); bool monoeg_g_module_address (void *addr, char *file_name, size_t file_name_len, @@ -24,32 +21,25 @@ bool monoeg_g_module_address (void *addr, char *file_name, size_t file_name_len, size_t sym_name_len, void **sym_addr); char *mono_path_resolve_symlinks(const char *path); char *monoeg_g_path_get_dirname (const char *filename); +char *monoeg_g_getenv (const char *variable); -void +static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) { free (args); free (user_data); } -void register_bundled_modules () +static void +initialize_runtimeconfig () { - char *file_name = RUNTIMECONFIG_BIN_FILE; - int str_len = strlen (bundle_path) + strlen (file_name) + 1; // +1 is for the "/" - char *file_path = (char *)malloc (sizeof (char) * (str_len +1)); // +1 is for the terminating null character - int num_char = snprintf (file_path, (str_len + 1), "%s/%s", bundle_path, file_name); - struct stat buffer; - - assert (num_char > 0 && num_char == str_len); +%RUNTIME_CONFIG% +} - if (stat (file_path, &buffer) == 0) { - MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments)); - arg->kind = 0; - arg->runtimeconfig.name.path = file_path; - monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, file_path); - } else { - free (file_path); - } +static void +initialize_appctx_env_variables () +{ +%APPCTX_ENV_VARIABLES% } static MonoAssembly* @@ -143,14 +133,18 @@ free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) munmap (handle, size); } -void runtime_init_callback () +void +runtime_init_callback () { + initialize_runtimeconfig (); + + initialize_appctx_env_variables (); + register_aot_modules (); - register_bundled_modules (); + // register all bundled modules - char *assemblyPath = assemblies_dir (); - mono_set_assemblies_path ((assemblyPath && assemblyPath[0] != '\0') ? assemblyPath : "./"); + mono_set_assemblies_path ((bundle_path && bundle_path[0] != '\0') ? bundle_path : "./"); mono_jit_set_aot_only (true); @@ -168,15 +162,17 @@ void runtime_init_callback () mono_install_load_aot_data_hook (load_aot_data, free_aot_data, NULL); + mono_set_signal_chaining (true); + mono_jit_init ("dotnet.android"); // Pass in via LibraryBuilder? - // Load assemblies with UnmanagedCallersOnly exported methods - // TODO leverage get_loadable_assemblies and call mono_assembly_open on each - mono_assembly_open("Android.Device_Emulator.Aot_Llvm.Test.dll", NULL); +%ASSEMBLIES_LOADER% } -void init_mono_runtime () +void +init_mono_runtime () { + bundle_path = monoeg_g_getenv("%ASSETS_PATH%"); mono_set_runtime_init_callback (&runtime_init_callback); } From 24585415cddca264c3d6a03ff3af315d5d4954d7 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Wed, 8 Mar 2023 17:49:10 -0500 Subject: [PATCH 04/30] Revert "Grab shared library directory" This reverts commit bc86f2b91c76f0e40ad60516dcb1031d02e7054b. --- src/tasks/LibraryBuilder/Templates/autoinit.c | 21 ------------------- ...droid.Device_Emulator.Aot_Llvm.Test.csproj | 1 - 2 files changed, 22 deletions(-) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index f18140758cef11..9a579c669dcb23 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -16,11 +16,6 @@ static char *bundle_path; void register_aot_modules (void); -bool monoeg_g_module_address (void *addr, char *file_name, size_t file_name_len, - void **file_base, char *sym_name, - size_t sym_name_len, void **sym_addr); -char *mono_path_resolve_symlinks(const char *path); -char *monoeg_g_path_get_dirname (const char *filename); char *monoeg_g_getenv (const char *variable); static void @@ -73,22 +68,6 @@ mono_droid_load_assembly (const char *name, const char *culture) return NULL; } -// Assumes that the dl containing this function is in the same directory as the assemblies to load. -static char * -assemblies_dir (void) -{ - static char *dl_dir_name = NULL; - char dl_filename[4096]; - - if (monoeg_g_module_address ((void *)assemblies_dir, dl_filename, sizeof (dl_filename), NULL, NULL, 0, NULL)) { - char *resolved_dl_filename = mono_path_resolve_symlinks (dl_filename); - dl_dir_name = monoeg_g_path_get_dirname (resolved_dl_filename); - free (resolved_dl_filename); - } - - return dl_dir_name; -} - static MonoAssembly* mono_droid_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) { diff --git a/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj index 655b1b05edefdc..49ddce32380595 100644 --- a/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj +++ b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj @@ -3,7 +3,6 @@ Exe false true - true true $(NetCoreAppCurrent) Android.Device_Emulator.Aot_Llvm.Test.dll From 6dc389582e8cb41a1e18f56872ec126d9af5e5d5 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Wed, 8 Mar 2023 18:37:53 -0500 Subject: [PATCH 05/30] Remove platform specific naming --- src/tasks/LibraryBuilder/Templates/autoinit.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 9a579c669dcb23..6fb7694a7f355e 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -38,7 +38,7 @@ initialize_appctx_env_variables () } static MonoAssembly* -mono_droid_load_assembly (const char *name, const char *culture) +mono_load_assembly (const char *name, const char *culture) { char filename [1024]; char path [1024]; @@ -69,11 +69,11 @@ mono_droid_load_assembly (const char *name, const char *culture) } static MonoAssembly* -mono_droid_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) +mono_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) { const char *name = mono_assembly_name_get_name (aname); const char *culture = mono_assembly_name_get_culture (aname); - return mono_droid_load_assembly (name, culture); + return mono_load_assembly (name, culture); } static unsigned char * @@ -127,7 +127,7 @@ runtime_init_callback () mono_jit_set_aot_only (true); - mono_install_assembly_preload_hook (mono_droid_assembly_preload_hook, NULL); + mono_install_assembly_preload_hook (mono_assembly_preload_hook, NULL); // TODO test debug scenario #if DEBUG_ENABLED @@ -143,7 +143,7 @@ runtime_init_callback () mono_set_signal_chaining (true); - mono_jit_init ("dotnet.android"); // Pass in via LibraryBuilder? + mono_jit_init ("mono.self.contained.library"); // Pass in via LibraryBuilder? %ASSEMBLIES_LOADER% } From d772bf8083220fac63099b8b12be98e594ef0afd Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 12:11:33 -0500 Subject: [PATCH 06/30] [tasks] Move App Context setup back to autoinit.c --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 49 +------------------ src/tasks/LibraryBuilder/Templates/autoinit.c | 27 +++++++++- 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index bec8d46b3b1d17..afc9ec0f9a7b57 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -255,16 +255,13 @@ private static void WriteLinkerScriptFile(string exportsFile, List expor private void WriteAutoInitializationFromTemplate() { - string appContextEnvVariables = GenerateAppContextEnvVariables(); string assembliesLoader = GenerateAssembliesLoader(); string assetsPath = GenerateAssetsPath(); - string runtimeConfig = GenerateRuntimeConfig(); File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), Utils.GetEmbeddedResource("autoinit.c") - .Replace("%APPCTX_ENV_VARIABLES%", appContextEnvVariables) .Replace("%ASSEMBLIES_LOADER%", assembliesLoader) .Replace("%ASSETS_PATH%", assetsPath) - .Replace("%RUNTIME_CONFIG%", runtimeConfig)); + .Replace("%RUNTIME_IDENTIFIER%", RuntimeIdentifier)); } private string GenerateAssembliesLoader() @@ -282,50 +279,6 @@ private string GenerateAssetsPath() return AssetsPath ?? "DOTNET_ASSETS_PATH"; } - private string GenerateRuntimeConfig() - { - if (string.IsNullOrEmpty(RuntimeConfigBinFile)) - return " return;"; - - var runtimeConfig = new StringBuilder(); - runtimeConfig.Append($" char *file_name = {RuntimeConfigBinFile};"); - runtimeConfig.Append(" int str_len = strlen (bundle_path) + strlen (file_name) + 1; // +1 is for the \"/\""); - runtimeConfig.Append(" char *file_path = (char *)malloc (sizeof (char) * (str_len +1)); // +1 is for the terminating null character"); - runtimeConfig.Append(" int num_char = snprintf (file_path, (str_len + 1), \"%s/%s\", bundle_path, file_name);"); - runtimeConfig.Append(" struct stat buffer;\n"); - runtimeConfig.Append(" assert (num_char > 0 && num_char == str_len);\n"); - runtimeConfig.Append(" if (stat (file_path, &buffer) == 0) {"); - runtimeConfig.Append(" MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments));"); - runtimeConfig.Append(" arg->kind = 0;"); - runtimeConfig.Append(" arg->runtimeconfig.name.path = file_path;"); - runtimeConfig.Append(" monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, file_path);"); - runtimeConfig.Append(" } else {"); - runtimeConfig.Append(" free (file_path);"); - runtimeConfig.Append(" }"); - return runtimeConfig.ToString(); - } - - private string GenerateAppContextEnvVariables() - { - if (AppContextKeys.Length != AppContextValues.Length) - { - throw new LogAsErrorException($"'{nameof(AppContextKeys)}' length does not match '{nameof(AppContextValues)}' length. {AppContextKeys.Length} != {AppContextValues.Length}"); - } - - int numArgs = AppContextKeys.Length; - var appContextEnvVariables = new StringBuilder(); - - appContextEnvVariables.AppendLine($" const char **appctx_keys, **appctx_values = (char**)malloc({numArgs} * sizeof(char*));"); - for (int i = 0; i < numArgs; i++) - { - appContextEnvVariables.AppendLine($" appctx_keys[{i}] = \"{AppContextKeys[i]}\";"); - appContextEnvVariables.AppendLine($" appctx_values[{i}] = \"{AppContextValues[i]}\";"); - } - appContextEnvVariables.AppendLine($" monovm_initialize({numArgs}, appctx_keys, appctx_values);"); - - return appContextEnvVariables.ToString(); - } - private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, string extraSources, string linkerArgs) { // BundleDir diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 6fb7694a7f355e..e87c3a2b4423e7 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -28,13 +28,36 @@ cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) static void initialize_runtimeconfig () { -%RUNTIME_CONFIG% + char *file_name = "runtimeconfig.bin"; + int str_len = strlen (bundle_path) + strlen (file_name) + 1; // +1 is for the \"/\" + char *file_path = (char *)malloc (sizeof (char) * (str_len +1)); // +1 is for the terminating null character + int num_char = snprintf (file_path, (str_len + 1), "%s/%s", bundle_path, file_name); + struct stat buffer; + + assert (num_char > 0 && num_char == str_len); + + if (stat (file_path, &buffer) == 0) { + MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments)); + arg->kind = 0; + arg->runtimeconfig.name.path = file_path; + monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, file_path); + } else { + free (file_path); + } } static void initialize_appctx_env_variables () { -%APPCTX_ENV_VARIABLES% + const char *appctx_keys[2], *appctx_values[2]; + + appctx_keys[0] = "RUNTIME_IDENTIFIER"; + appctx_values[0] = "%RUNTIME_IDENTIFIER%"; + + appctx_keys[1] = "APP_CONTEXT_BASE_DIRECTORY"; + appctx_values[1] = bundle_path; + + monovm_initialize(2, appctx_keys, appctx_values); } static MonoAssembly* From bb228c4ab85c40d5e291a8dcba0e28a328fccddb Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 12:32:17 -0500 Subject: [PATCH 07/30] Remove registering bundled modules and debugging until they can be validated --- src/tasks/LibraryBuilder/Templates/autoinit.c | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index e87c3a2b4423e7..7365774b2020c0 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -144,24 +144,12 @@ runtime_init_callback () register_aot_modules (); - // register all bundled modules - mono_set_assemblies_path ((bundle_path && bundle_path[0] != '\0') ? bundle_path : "./"); mono_jit_set_aot_only (true); mono_install_assembly_preload_hook (mono_assembly_preload_hook, NULL); - // TODO test debug scenario -#if DEBUG_ENABLED - bool wait_for_debugger = false; - mono_debug_init (MONO_DEBUG_FORMAT_MONO); - if (wait_for_debugger) { - char* options[] = { "--debugger-agent=transport=dt_socket,server=y,address=0.0.0.0:55555" }; - mono_jit_parse_options (1, options); - } -#endif - mono_install_load_aot_data_hook (load_aot_data, free_aot_data, NULL); mono_set_signal_chaining (true); From c47e5d53d10dc4d3ee40ec17e640fb74e9af3618 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 12:35:12 -0500 Subject: [PATCH 08/30] Remove unnecessary wrapper --- src/tasks/LibraryBuilder/Templates/autoinit.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 7365774b2020c0..2a1693370c24ec 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -159,15 +159,9 @@ runtime_init_callback () %ASSEMBLIES_LOADER% } -void -init_mono_runtime () -{ - bundle_path = monoeg_g_getenv("%ASSETS_PATH%"); - mono_set_runtime_init_callback (&runtime_init_callback); -} - void __attribute__((constructor)) autoinit () { - init_mono_runtime (); + bundle_path = monoeg_g_getenv("%ASSETS_PATH%"); + mono_set_runtime_init_callback (&runtime_init_callback); } From 806c55a2f145318f1d204cbadb3aad181fbd5c88 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 12:52:40 -0500 Subject: [PATCH 09/30] Grab bundle_path later without eglib Setting bundle_path in constructor may be too early for environment variables to be set so grab it first thing when initializing the runtime. Use getenv directly instead of calling into eglib with just one api --- src/tasks/LibraryBuilder/Templates/autoinit.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 2a1693370c24ec..4a6b2087a0754f 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -16,7 +16,6 @@ static char *bundle_path; void register_aot_modules (void); -char *monoeg_g_getenv (const char *variable); static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) @@ -138,6 +137,8 @@ free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) void runtime_init_callback () { + bundle_path = strdup(getenv("%ASSETS_PATH%")); + initialize_runtimeconfig (); initialize_appctx_env_variables (); @@ -157,11 +158,11 @@ runtime_init_callback () mono_jit_init ("mono.self.contained.library"); // Pass in via LibraryBuilder? %ASSEMBLIES_LOADER% + free (bundle_path); } void __attribute__((constructor)) autoinit () { - bundle_path = monoeg_g_getenv("%ASSETS_PATH%"); mono_set_runtime_init_callback (&runtime_init_callback); } From 108489ffc753429d525b3ad7055d02c5e88e217e Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 12:59:55 -0500 Subject: [PATCH 10/30] Rename AssetsPath to AssembliesLocation --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 10 +++++----- src/tasks/LibraryBuilder/Templates/autoinit.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index afc9ec0f9a7b57..16c9debf2387fd 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -72,7 +72,7 @@ public bool IsSharedLibrary /// public bool UsesCustomRuntimeInitCallback { get; set; } - public string? AssetsPath { get; set; } + public string? AssembliesLocation { get; set; } /// /// @@ -256,11 +256,11 @@ private static void WriteLinkerScriptFile(string exportsFile, List expor private void WriteAutoInitializationFromTemplate() { string assembliesLoader = GenerateAssembliesLoader(); - string assetsPath = GenerateAssetsPath(); + string assembliesLocation = GenerateAssembliesLocation(); File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), Utils.GetEmbeddedResource("autoinit.c") .Replace("%ASSEMBLIES_LOADER%", assembliesLoader) - .Replace("%ASSETS_PATH%", assetsPath) + .Replace("%ASSEMBLIES_LOCATION%", assembliesLocation) .Replace("%RUNTIME_IDENTIFIER%", RuntimeIdentifier)); } @@ -274,9 +274,9 @@ private string GenerateAssembliesLoader() return assembliesLoader.ToString(); } - private string GenerateAssetsPath() + private string GenerateAssembliesLocation() { - return AssetsPath ?? "DOTNET_ASSETS_PATH"; + return AssembliesLocation ?? "DOTNET_ASSETS_PATH"; } private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, string extraSources, string linkerArgs) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 4a6b2087a0754f..ab9b804a0a5697 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -137,7 +137,7 @@ free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) void runtime_init_callback () { - bundle_path = strdup(getenv("%ASSETS_PATH%")); + bundle_path = strdup(getenv(%ASSEMBLIES_LOCATION%)); initialize_runtimeconfig (); From 3b71e955501c32489a01dfe503e40898da367152 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 13:50:36 -0500 Subject: [PATCH 11/30] Move assembly loading into separate function --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 50 +++++-------------- .../Templates/CMakeLists.txt.template | 2 +- .../LibraryBuilder/Templates/assembly_list.c | 9 ---- src/tasks/LibraryBuilder/Templates/autoinit.c | 6 ++- .../Templates/load_assemblies.c | 9 ++++ 5 files changed, 26 insertions(+), 50 deletions(-) delete mode 100644 src/tasks/LibraryBuilder/Templates/assembly_list.c create mode 100644 src/tasks/LibraryBuilder/Templates/load_assemblies.c diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 16c9debf2387fd..6c7184f25e8362 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -138,6 +138,8 @@ public override bool Execute() WriteAutoInitializationFromTemplate(); } + GenerateAssembliesLoader(); + WriteCMakeFileFromTemplate(aotSources.ToString(), aotObjects.ToString(), extraSources.ToString(), linkerArgs.ToString()); OutputPath = BuildLibrary(); @@ -194,8 +196,6 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot WriteExportedSymbolsArg(MobileSymbolFileName, linkerArgs); } - WriteAssembliesToLoadList(exportedAssemblies); - foreach (ITaskItem item in ExtraSources) { extraSources.AppendLine(item.ItemSpec); @@ -255,28 +255,29 @@ private static void WriteLinkerScriptFile(string exportsFile, List expor private void WriteAutoInitializationFromTemplate() { - string assembliesLoader = GenerateAssembliesLoader(); string assembliesLocation = GenerateAssembliesLocation(); File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), Utils.GetEmbeddedResource("autoinit.c") - .Replace("%ASSEMBLIES_LOADER%", assembliesLoader) .Replace("%ASSEMBLIES_LOCATION%", assembliesLocation) .Replace("%RUNTIME_IDENTIFIER%", RuntimeIdentifier)); } - private string GenerateAssembliesLoader() + private string GenerateAssembliesLocation() + { + return AssembliesLocation ?? "DOTNET_ASSETS_PATH"; + } + + private void GenerateAssembliesLoader() { var assembliesLoader = new StringBuilder(); foreach (string exportedAssembly in exportedAssemblies) { - assembliesLoader.AppendLine($" mono_assembly_open(\"{exportedAssembly}\", NULL);"); + assembliesLoader.Append($" mono_assembly_open(\"{exportedAssembly}\", NULL);\n"); } - return assembliesLoader.ToString(); - } - private string GenerateAssembliesLocation() - { - return AssembliesLocation ?? "DOTNET_ASSETS_PATH"; + File.WriteAllText(Path.Combine(OutputDirectory, "load_assemblies.c"), + Utils.GetEmbeddedResource("load_assemblies.c") + .Replace("%ASSEMBLIES_LOADER%", assembliesLoader.ToString())); } private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, string extraSources, string linkerArgs) @@ -294,33 +295,6 @@ private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, st .Replace("%LIBRARY_LINKER_ARGS%", linkerArgs)); } - private void WriteAssembliesToLoadList(List assemblies) - { - string content; - - if (assemblies.Count == 0) - { - content = " return NULL;"; - } - else - { - StringBuilder sb = new StringBuilder(); - sb.AppendLine($" char** assembly_list = (char**)malloc({assemblies.Count.ToString()} * sizeof(char*));"); - - for (int i = 0; i < assemblies.Count; i++) - { - sb.AppendLine($" assembly_list[{i.ToString()}] = \"{assemblies[i]}\";"); - } - - sb.AppendLine(" return assembly_list;"); - content = sb.ToString(); - } - - File.WriteAllText(Path.Combine(OutputDirectory, "assembly_list.c"), - Utils.GetEmbeddedResource("assembly_list.c") - .Replace("%LOADABLE_ASSEMBLIES%", content)); - } - private string BuildLibrary() { string libraryOutputPath; diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index b27caa82f44d01..364c728c68509e 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -12,7 +12,7 @@ set(DOTNET_AOT_OBJECTS ) set(DOTNET_EXTRA_SOURCES %ExtraSources% - assembly_list.c + load_assemblies.c autoinit.c ) diff --git a/src/tasks/LibraryBuilder/Templates/assembly_list.c b/src/tasks/LibraryBuilder/Templates/assembly_list.c deleted file mode 100644 index ef2168d3e50bf7..00000000000000 --- a/src/tasks/LibraryBuilder/Templates/assembly_list.c +++ /dev/null @@ -1,9 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -#include -#include - -char** get_loadable_assemblies () -{ -%LOADABLE_ASSEMBLIES% -} \ No newline at end of file diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index ab9b804a0a5697..198a4608ce2800 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -16,6 +16,7 @@ static char *bundle_path; void register_aot_modules (void); +void load_assemblies_with_exported_symbols (void); static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) @@ -137,7 +138,7 @@ free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) void runtime_init_callback () { - bundle_path = strdup(getenv(%ASSEMBLIES_LOCATION%)); + bundle_path = strdup(getenv("%ASSEMBLIES_LOCATION%")); initialize_runtimeconfig (); @@ -157,7 +158,8 @@ runtime_init_callback () mono_jit_init ("mono.self.contained.library"); // Pass in via LibraryBuilder? -%ASSEMBLIES_LOADER% + load_assemblies_with_exported_symbols (); + free (bundle_path); } diff --git a/src/tasks/LibraryBuilder/Templates/load_assemblies.c b/src/tasks/LibraryBuilder/Templates/load_assemblies.c new file mode 100644 index 00000000000000..91a1c97f1d3625 --- /dev/null +++ b/src/tasks/LibraryBuilder/Templates/load_assemblies.c @@ -0,0 +1,9 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +#include + +MonoAssembly* mono_assembly_open (const char *filename, MonoImageOpenStatus *status); + +void load_assemblies_with_exported_symbols () +{ +%ASSEMBLIES_LOADER%} \ No newline at end of file From 89c2c8eea570cd07a7c4aeef2c226e0f251efea7 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 13:54:05 -0500 Subject: [PATCH 12/30] Remove unnecessary function --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 6c7184f25e8362..7703f53db4e501 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -255,18 +255,12 @@ private static void WriteLinkerScriptFile(string exportsFile, List expor private void WriteAutoInitializationFromTemplate() { - string assembliesLocation = GenerateAssembliesLocation(); File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), Utils.GetEmbeddedResource("autoinit.c") - .Replace("%ASSEMBLIES_LOCATION%", assembliesLocation) + .Replace("%ASSEMBLIES_LOCATION%", AssembliesLocation ?? "DOTNET_ASSETS_PATH") .Replace("%RUNTIME_IDENTIFIER%", RuntimeIdentifier)); } - private string GenerateAssembliesLocation() - { - return AssembliesLocation ?? "DOTNET_ASSETS_PATH"; - } - private void GenerateAssembliesLoader() { var assembliesLoader = new StringBuilder(); From f5598a18d4376bd092bdb5b38a449f8697bc231e Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 14:12:47 -0500 Subject: [PATCH 13/30] Cleanup CMakeLists template indentation --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 5 +++-- .../LibraryBuilder/Templates/CMakeLists.txt.template | 11 ++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 7703f53db4e501..d961ee8bb1ad8c 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -136,6 +136,7 @@ public override bool Execute() if (!UsesCustomRuntimeInitCallback) { WriteAutoInitializationFromTemplate(); + extraSources.AppendLine(" autoinit.c"); } GenerateAssembliesLoader(); @@ -158,7 +159,7 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot { if (!string.IsNullOrEmpty(compiledAssembly.AssemblerFile)) { - aotSources.AppendLine(compiledAssembly.AssemblerFile); + aotSources.AppendLine($" {compiledAssembly.AssemblerFile}"); } if (!string.IsNullOrEmpty(compiledAssembly.LlvmObjectFile)) @@ -198,7 +199,7 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot foreach (ITaskItem item in ExtraSources) { - extraSources.AppendLine(item.ItemSpec); + extraSources.AppendLine($" {item.ItemSpec}"); } } diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index 364c728c68509e..b77e1f7c717cfd 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -5,15 +5,13 @@ project(%LIBRARY_NAME%) enable_language(%CMAKE_LANGS%) set(DOTNET_AOT_SOURCES - %AotSources% +%AotSources% ) set(DOTNET_AOT_OBJECTS - %AotObjects% +%AotObjects% ) set(DOTNET_EXTRA_SOURCES - %ExtraSources% - load_assemblies.c - autoinit.c +%ExtraSources% load_assemblies.c ) include_directories("%MonoInclude%") @@ -54,5 +52,4 @@ target_link_libraries( PUBLIC aot_library ${MOBILE_SYSTEM_LIBS} - %LIBRARY_LINKER_ARGS% -) +%LIBRARY_LINKER_ARGS%) From b9a8c7ce85f565b4cd9d75d5d655772943897d64 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 14:15:50 -0500 Subject: [PATCH 14/30] Add dotnet header to autoinit.c --- src/tasks/LibraryBuilder/Templates/autoinit.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 198a4608ce2800..080e8a01b1fc12 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -1,3 +1,5 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. #include #include #include From 50f24b0b33841249ed816ede82d95a90e3d5784f Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 16:08:45 -0500 Subject: [PATCH 15/30] Default bundle_path if assemblies path environment variable not set --- src/mono/msbuild/android/build/AndroidApp.targets | 3 ++- src/tasks/LibraryBuilder/Templates/autoinit.c | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/mono/msbuild/android/build/AndroidApp.targets b/src/mono/msbuild/android/build/AndroidApp.targets index e1a5363d22fbe4..750da90ce26d07 100644 --- a/src/mono/msbuild/android/build/AndroidApp.targets +++ b/src/mono/msbuild/android/build/AndroidApp.targets @@ -219,7 +219,8 @@ + Condition="Exists('$(_AndroidRuntimeConfigFilePath)')" + BeforeTargets="_BuildNativeLibrary"> <_RuntimeConfigReservedProperties Include="RUNTIME_IDENTIFIER"/> <_RuntimeConfigReservedProperties Include="APP_CONTEXT_BASE_DIRECTORY"/> diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 080e8a01b1fc12..9af9aac24d05d4 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -140,7 +140,8 @@ free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) void runtime_init_callback () { - bundle_path = strdup(getenv("%ASSEMBLIES_LOCATION%")); + const char *assemblies_path = strdup(getenv("%ASSEMBLIES_LOCATION%")); + bundle_path = (assemblies_path && assemblies_path[0] != '\0') ? assemblies_path : "./"; initialize_runtimeconfig (); @@ -148,7 +149,7 @@ runtime_init_callback () register_aot_modules (); - mono_set_assemblies_path ((bundle_path && bundle_path[0] != '\0') ? bundle_path : "./"); + mono_set_assemblies_path (bundle_path); mono_jit_set_aot_only (true); @@ -158,11 +159,11 @@ runtime_init_callback () mono_set_signal_chaining (true); - mono_jit_init ("mono.self.contained.library"); // Pass in via LibraryBuilder? + mono_jit_init ("mono.self.contained.library"); load_assemblies_with_exported_symbols (); - free (bundle_path); + free (assemblies_path); } void __attribute__((constructor)) From 5bd4aa39c6ea9fbe135b85905d8bdab934163f1f Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Fri, 10 Mar 2023 21:52:51 -0500 Subject: [PATCH 16/30] Pass bundle_path to load assemblies --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 2 +- src/tasks/LibraryBuilder/Templates/autoinit.c | 6 +++--- .../Templates/load_assemblies.c | 20 ++++++++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index d961ee8bb1ad8c..b30de728464449 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -267,7 +267,7 @@ private void GenerateAssembliesLoader() var assembliesLoader = new StringBuilder(); foreach (string exportedAssembly in exportedAssemblies) { - assembliesLoader.Append($" mono_assembly_open(\"{exportedAssembly}\", NULL);\n"); + assembliesLoader.Append($" mono_assembly_open_from_dir(dir, \"{exportedAssembly}\");\n"); } File.WriteAllText(Path.Combine(OutputDirectory, "load_assemblies.c"), diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 9af9aac24d05d4..d03361a7fd1c05 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -18,7 +18,7 @@ static char *bundle_path; void register_aot_modules (void); -void load_assemblies_with_exported_symbols (void); +void load_assemblies_with_exported_symbols (const char *dir); static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) @@ -161,9 +161,9 @@ runtime_init_callback () mono_jit_init ("mono.self.contained.library"); - load_assemblies_with_exported_symbols (); + load_assemblies_with_exported_symbols (bundle_path); - free (assemblies_path); + free ((void *)assemblies_path); } void __attribute__((constructor)) diff --git a/src/tasks/LibraryBuilder/Templates/load_assemblies.c b/src/tasks/LibraryBuilder/Templates/load_assemblies.c index 91a1c97f1d3625..4af61efc7bf0b9 100644 --- a/src/tasks/LibraryBuilder/Templates/load_assemblies.c +++ b/src/tasks/LibraryBuilder/Templates/load_assemblies.c @@ -1,9 +1,27 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#include +#include + #include MonoAssembly* mono_assembly_open (const char *filename, MonoImageOpenStatus *status); -void load_assemblies_with_exported_symbols () +void +mono_assembly_open_from_dir (const char *dir, const char* filename) +{ + int str_len = strlen (dir) + strlen (filename) + 2; + const char *assembly_path = (char*)malloc (sizeof (char) * str_len); + int num_char = snprintf (assembly_path, str_len, "%s/%s", dir, filename); + + assert (num_char > 0 && num_char < str_len); + + mono_assembly_open (assembly_path, NULL); + + free ((void *)assembly_path); +} + +void +load_assemblies_with_exported_symbols (const char *dir) { %ASSEMBLIES_LOADER%} \ No newline at end of file From 7fe528fbe6636d19268b5ef88b87d9f225ad975d Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Mon, 13 Mar 2023 19:08:29 -0400 Subject: [PATCH 17/30] Add logging and abort on failures --- .../Templates/CMakeLists.txt.template | 5 +- src/tasks/LibraryBuilder/Templates/autoinit.c | 72 +++++++++++++++---- .../Templates/load_assemblies.c | 29 ++++++-- 3 files changed, 84 insertions(+), 22 deletions(-) diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index b77e1f7c717cfd..54fc49512626b3 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -18,7 +18,7 @@ include_directories("%MonoInclude%") add_library( aot_library STATIC - ${DOTNET_AOT_SOURCES} + ${DOTNET_AOT_SOURCES} ) target_link_libraries( aot_library @@ -34,8 +34,9 @@ add_library( if(TARGETS_ANDROID) set(MOBILE_SYSTEM_LIBS libz.so - log + log ) + add_definitions(-DHOST_ANDROID=1) else() set(MOBILE_SYSTEM_LIBS "-framework Foundation" diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index d03361a7fd1c05..88736b263a6c0a 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -1,24 +1,36 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#include #include +#include #include #include #include #include #include -#include #include #include #include #include -#include -static char *bundle_path; +#ifdef HOST_ANDROID +#include + +#define LOG_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO, "MONO_SELF_CONTAINED_LIBRARY", fmt, ##__VA_ARGS__) +#define LOG_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, "MONO_SELF_CONTAINED_LIBRARY", fmt, ##__VA_ARGS__) +#else +#include + +#define LOG_INFO(fmt, ...) os_log_info (OS_LOG_DEFAULT, fmt, ##__VA_ARGS__) +#define LOG_ERROR(fmt, ...) os_log_error (OS_LOG_DEFAULT, fmt, ##__VA_ARGS__) +#endif + +static const char *bundle_path; void register_aot_modules (void); void load_assemblies_with_exported_symbols (const char *dir); +typedef void (*MonoRuntimeInitCallback) (void); +void mono_set_runtime_init_callback (MonoRuntimeInitCallback callback); static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) @@ -31,12 +43,20 @@ static void initialize_runtimeconfig () { char *file_name = "runtimeconfig.bin"; - int str_len = strlen (bundle_path) + strlen (file_name) + 1; // +1 is for the \"/\" - char *file_path = (char *)malloc (sizeof (char) * (str_len +1)); // +1 is for the terminating null character - int num_char = snprintf (file_path, (str_len + 1), "%s/%s", bundle_path, file_name); + size_t str_len = sizeof (char) * (strlen (bundle_path) + strlen (file_name) + 2); // +1 "/", +1 null-terminating char + char *file_path = (char *)malloc (str_len); + if (!file_path) { + LOG_ERROR ("Could not allocate %zu bytes to format '%s' and '%s' together to initialize the runtime configuration.\n", str_len, bundle_path, file_name); + abort (); + } + + int num_char = snprintf (file_path, str_len, "%s/%s", bundle_path, file_name); struct stat buffer; - assert (num_char > 0 && num_char == str_len); + if (num_char <= 0 || num_char != (str_len - 1)) { + LOG_ERROR ("Could not format '%s' and '%s' together into \"%%s/%%s\" to initialize the runtime configuration.\n", bundle_path, file_name); + abort (); + } if (stat (file_path, &buffer) == 0) { MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments)); @@ -44,6 +64,7 @@ initialize_runtimeconfig () arg->runtimeconfig.name.path = file_path; monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, file_path); } else { + LOG_INFO ("Could not stat file '%s'. Runtime configuration properties not initialized.\n", file_path); free (file_path); } } @@ -82,14 +103,26 @@ mono_load_assembly (const char *name, const char *culture) res = snprintf (path, sizeof (path) - 1, "%s/%s/%s", bundle_path, culture, filename); else res = snprintf (path, sizeof (path) - 1, "%s/%s", bundle_path, filename); - assert (res > 0); + + if (res <= 0 && culture) { + LOG_ERROR ("Could not format '%s', '%s', and '%s' together into \"%%s/%%s/%%s\" for assembly preloading.\n", bundle_path, culture, filename); + abort (); + } + if (res <= 0) { + LOG_ERROR ("Could not format '%s' and '%s' together into \"%%s/%%s\" for assembly preloading.\n", bundle_path, filename); + abort (); + } struct stat buffer; if (stat (path, &buffer) == 0) { MonoAssembly *assembly = mono_assembly_open (path, NULL); - assert (assembly); + if (!assembly) { + LOG_ERROR ("Could not open assembly '%s'.\n", path); + abort (); + } return assembly; } + LOG_INFO ("Could not stat file '%s'. Did not successfully preload assembly.\n", path); return NULL; } @@ -113,15 +146,20 @@ load_aot_data (MonoAssembly *assembly, int size, void *user_data, void **out_han const char *aname = mono_assembly_name_get_name (assembly_name); res = snprintf (path, sizeof (path) - 1, "%s/%s.aotdata", bundle_path, aname); - assert (res > 0); + if (res <= 0) { + LOG_ERROR ("Could not format '%s' and '%s' together into \"%%s/%%s.aotdata\" for assembly preloading.\n", bundle_path, aname); + abort (); + } int fd = open (path, O_RDONLY); if (fd < 0) { + LOG_INFO ("Could not open file '%s' while trying to load aot data.\n", path); return NULL; } void *ptr = mmap (NULL, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); if (ptr == MAP_FAILED) { + LOG_INFO ("Could not mmap file '%s'.\n", path); close (fd); return NULL; } @@ -141,7 +179,11 @@ void runtime_init_callback () { const char *assemblies_path = strdup(getenv("%ASSEMBLIES_LOCATION%")); - bundle_path = (assemblies_path && assemblies_path[0] != '\0') ? assemblies_path : "./"; + if (!assemblies_path) { + LOG_ERROR ("Could not duplicate value of environment variable %ASSEMBLIES_LOCATION% due to insufficient memory.\n"); + abort (); + } + bundle_path = (assemblies_path[0] != '\0') ? assemblies_path : "./"; initialize_runtimeconfig (); @@ -159,7 +201,11 @@ runtime_init_callback () mono_set_signal_chaining (true); - mono_jit_init ("mono.self.contained.library"); + MonoDomain *domain = mono_jit_init ("mono.self.contained.library"); + if (!domain) { + LOG_ERROR ("Could not auto initialize runtime.\n"); + abort (); + } load_assemblies_with_exported_symbols (bundle_path); diff --git a/src/tasks/LibraryBuilder/Templates/load_assemblies.c b/src/tasks/LibraryBuilder/Templates/load_assemblies.c index 4af61efc7bf0b9..1661b9d25be148 100644 --- a/src/tasks/LibraryBuilder/Templates/load_assemblies.c +++ b/src/tasks/LibraryBuilder/Templates/load_assemblies.c @@ -1,22 +1,37 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#include +#include +#include #include #include -MonoAssembly* mono_assembly_open (const char *filename, MonoImageOpenStatus *status); +#ifdef HOST_ANDROID +#include + +#define LOG_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, "MONO_SELF_CONTAINED_LIBRARY", fmt, ##__VA_ARGS__) +#else +#include + +#define LOG_ERROR(fmt, ...) os_log_error (OS_LOG_DEFAULT, fmt, ##__VA_ARGS__) +#endif void mono_assembly_open_from_dir (const char *dir, const char* filename) { - int str_len = strlen (dir) + strlen (filename) + 2; - const char *assembly_path = (char*)malloc (sizeof (char) * str_len); + size_t str_len = strlen (dir) + strlen (filename) + 2; // +1 "/", +1 null-terminating char + char *assembly_path = (char*)malloc (str_len); + if (!assembly_path) { + LOG_ERROR ("Could not allocate %zu bytes to format '%s' and '%s' together to open assembly to initialize GOT slots.\n", str_len, dir, filename); + abort (); + } int num_char = snprintf (assembly_path, str_len, "%s/%s", dir, filename); - assert (num_char > 0 && num_char < str_len); - - mono_assembly_open (assembly_path, NULL); + MonoAssembly *assembly = mono_assembly_open (assembly_path, NULL); + if (!assembly) { + LOG_ERROR ("Could not open assembly '%s'. Unable to properly initialize GOT slots.\n", assembly_path); + abort (); + } free ((void *)assembly_path); } From cbdaa3e1d90e9fa1d088f0063dd61af9c6bfdfec Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 14 Mar 2023 13:23:06 -0400 Subject: [PATCH 18/30] Load assemblies with partial name --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 4 ++-- src/tasks/LibraryBuilder/Templates/autoinit.c | 4 ++-- .../Templates/load_assemblies.c | 20 ++++--------------- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index b30de728464449..28183ba8150fd9 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -175,7 +175,7 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot if (symbolsAdded > 0) { - exportedAssemblies.Add(Path.GetFileName(compiledAssembly.Path)); + exportedAssemblies.Add(Path.GetFileNameWithoutExtension(compiledAssembly.Path)); } } } @@ -267,7 +267,7 @@ private void GenerateAssembliesLoader() var assembliesLoader = new StringBuilder(); foreach (string exportedAssembly in exportedAssemblies) { - assembliesLoader.Append($" mono_assembly_open_from_dir(dir, \"{exportedAssembly}\");\n"); + assembliesLoader.Append($" mono_assembly_load_with_partial_name_check(\"{exportedAssembly}\");\n"); } File.WriteAllText(Path.Combine(OutputDirectory, "load_assemblies.c"), diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 88736b263a6c0a..8b7340007a1a4b 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -28,7 +28,7 @@ static const char *bundle_path; void register_aot_modules (void); -void load_assemblies_with_exported_symbols (const char *dir); +void load_assemblies_with_exported_symbols (); typedef void (*MonoRuntimeInitCallback) (void); void mono_set_runtime_init_callback (MonoRuntimeInitCallback callback); @@ -207,7 +207,7 @@ runtime_init_callback () abort (); } - load_assemblies_with_exported_symbols (bundle_path); + load_assemblies_with_exported_symbols (); free ((void *)assemblies_path); } diff --git a/src/tasks/LibraryBuilder/Templates/load_assemblies.c b/src/tasks/LibraryBuilder/Templates/load_assemblies.c index 1661b9d25be148..d92404fb81aad7 100644 --- a/src/tasks/LibraryBuilder/Templates/load_assemblies.c +++ b/src/tasks/LibraryBuilder/Templates/load_assemblies.c @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#include #include -#include #include @@ -17,26 +15,16 @@ #endif void -mono_assembly_open_from_dir (const char *dir, const char* filename) +mono_assembly_load_with_partial_name_check (const char* filename) { - size_t str_len = strlen (dir) + strlen (filename) + 2; // +1 "/", +1 null-terminating char - char *assembly_path = (char*)malloc (str_len); - if (!assembly_path) { - LOG_ERROR ("Could not allocate %zu bytes to format '%s' and '%s' together to open assembly to initialize GOT slots.\n", str_len, dir, filename); - abort (); - } - int num_char = snprintf (assembly_path, str_len, "%s/%s", dir, filename); - - MonoAssembly *assembly = mono_assembly_open (assembly_path, NULL); + MonoAssembly *assembly = mono_assembly_load_with_partial_name (filename, NULL); if (!assembly) { - LOG_ERROR ("Could not open assembly '%s'. Unable to properly initialize GOT slots.\n", assembly_path); + LOG_ERROR ("Could not open assembly '%s'. Unable to properly initialize GOT slots.\n", filename); abort (); } - - free ((void *)assembly_path); } void -load_assemblies_with_exported_symbols (const char *dir) +load_assemblies_with_exported_symbols () { %ASSEMBLIES_LOADER%} \ No newline at end of file From c29d0cf87113a79c8cd3e44419ca7ad25d7bf914 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 14 Mar 2023 14:40:55 -0400 Subject: [PATCH 19/30] Clean up targets and unneeded properties --- .../msbuild/android/build/AndroidApp.targets | 3 +-- .../msbuild/common/LibraryBuilder.targets | 1 + src/tasks/LibraryBuilder/LibraryBuilder.cs | 20 +++++-------------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/mono/msbuild/android/build/AndroidApp.targets b/src/mono/msbuild/android/build/AndroidApp.targets index 750da90ce26d07..e1a5363d22fbe4 100644 --- a/src/mono/msbuild/android/build/AndroidApp.targets +++ b/src/mono/msbuild/android/build/AndroidApp.targets @@ -219,8 +219,7 @@ + Condition="Exists('$(_AndroidRuntimeConfigFilePath)')"> <_RuntimeConfigReservedProperties Include="RUNTIME_IDENTIFIER"/> <_RuntimeConfigReservedProperties Include="APP_CONTEXT_BASE_DIRECTORY"/> diff --git a/src/mono/msbuild/common/LibraryBuilder.targets b/src/mono/msbuild/common/LibraryBuilder.targets index 2d54b6a07dd161..c80aa354dac3ff 100644 --- a/src/mono/msbuild/common/LibraryBuilder.targets +++ b/src/mono/msbuild/common/LibraryBuilder.targets @@ -19,6 +19,7 @@ exportedAssemblies = new List(); /// /// The name of the library being generated @@ -72,19 +73,11 @@ public bool IsSharedLibrary /// public bool UsesCustomRuntimeInitCallback { get; set; } - public string? AssembliesLocation { get; set; } - /// + /// The environment variable name that will point to where assemblies + /// are located on the app host device. /// - public ITaskItem[] AppContextKeys { get; set; } = Array.Empty(); - - /// - /// - public ITaskItem[] AppContextValues { get; set; } = Array.Empty(); - - /// - /// - public string? RuntimeConfigBinFile { get; set; } + public string? AssembliesLocation { get; set; } public bool StripDebugSymbols { get; set; } @@ -147,9 +140,6 @@ public override bool Execute() return true; } - - private List exportedAssemblies = new List(); - private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aotObjects, StringBuilder extraSources, StringBuilder linkerArgs) { List exportedSymbols = new List(); @@ -258,7 +248,7 @@ private void WriteAutoInitializationFromTemplate() { File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), Utils.GetEmbeddedResource("autoinit.c") - .Replace("%ASSEMBLIES_LOCATION%", AssembliesLocation ?? "DOTNET_ASSETS_PATH") + .Replace("%ASSEMBLIES_LOCATION%", !string.IsNullOrEmpty(AssembliesLocation) ? AssembliesLocation : "DOTNET_ASSETS_PATH") .Replace("%RUNTIME_IDENTIFIER%", RuntimeIdentifier)); } From 46218d58fb0fe619d9e26a8a78aa8eb47d6d49f9 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 14 Mar 2023 15:16:57 -0400 Subject: [PATCH 20/30] Rename source file --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 4 ++-- src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template | 2 +- .../Templates/{load_assemblies.c => preloaded_assemblies.c} | 0 3 files changed, 3 insertions(+), 3 deletions(-) rename src/tasks/LibraryBuilder/Templates/{load_assemblies.c => preloaded_assemblies.c} (100%) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index e3d10a0e01687d..dc901825b1941c 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -260,8 +260,8 @@ private void GenerateAssembliesLoader() assembliesLoader.Append($" mono_assembly_load_with_partial_name_check(\"{exportedAssembly}\");\n"); } - File.WriteAllText(Path.Combine(OutputDirectory, "load_assemblies.c"), - Utils.GetEmbeddedResource("load_assemblies.c") + File.WriteAllText(Path.Combine(OutputDirectory, "preloaded_assemblies.c"), + Utils.GetEmbeddedResource("preloaded_assemblies.c") .Replace("%ASSEMBLIES_LOADER%", assembliesLoader.ToString())); } diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index 54fc49512626b3..f7278df47476d8 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -11,7 +11,7 @@ set(DOTNET_AOT_OBJECTS %AotObjects% ) set(DOTNET_EXTRA_SOURCES -%ExtraSources% load_assemblies.c +%ExtraSources% preloaded_assemblies.c ) include_directories("%MonoInclude%") diff --git a/src/tasks/LibraryBuilder/Templates/load_assemblies.c b/src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c similarity index 100% rename from src/tasks/LibraryBuilder/Templates/load_assemblies.c rename to src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c From bc5662f8dca8f3e9fbdde7e0803706bb6fb6d990 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 14 Mar 2023 15:58:33 -0400 Subject: [PATCH 21/30] Remove preload hook and make out of memory error more generic --- src/tasks/LibraryBuilder/Templates/autoinit.c | 80 +++---------------- 1 file changed, 11 insertions(+), 69 deletions(-) diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 8b7340007a1a4b..50f2a15a13118b 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -46,18 +46,13 @@ initialize_runtimeconfig () size_t str_len = sizeof (char) * (strlen (bundle_path) + strlen (file_name) + 2); // +1 "/", +1 null-terminating char char *file_path = (char *)malloc (str_len); if (!file_path) { - LOG_ERROR ("Could not allocate %zu bytes to format '%s' and '%s' together to initialize the runtime configuration.\n", str_len, bundle_path, file_name); + LOG_ERROR ("Out of memory.\n"); abort (); } int num_char = snprintf (file_path, str_len, "%s/%s", bundle_path, file_name); struct stat buffer; - if (num_char <= 0 || num_char != (str_len - 1)) { - LOG_ERROR ("Could not format '%s' and '%s' together into \"%%s/%%s\" to initialize the runtime configuration.\n", bundle_path, file_name); - abort (); - } - if (stat (file_path, &buffer) == 0) { MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments)); arg->kind = 0; @@ -83,83 +78,32 @@ initialize_appctx_env_variables () monovm_initialize(2, appctx_keys, appctx_values); } -static MonoAssembly* -mono_load_assembly (const char *name, const char *culture) -{ - char filename [1024]; - char path [1024]; - int res; - - int len = strlen (name); - int has_extension = len > 3 && name [len - 4] == '.' && (!strcmp ("exe", name + (len - 3)) || !strcmp ("dll", name + (len - 3))); - - // add extensions if required. - strlcpy (filename, name, sizeof (filename)); - if (!has_extension) { - strlcat (filename, ".dll", sizeof (filename)); - } - - if (culture && strcmp (culture, "")) - res = snprintf (path, sizeof (path) - 1, "%s/%s/%s", bundle_path, culture, filename); - else - res = snprintf (path, sizeof (path) - 1, "%s/%s", bundle_path, filename); - - if (res <= 0 && culture) { - LOG_ERROR ("Could not format '%s', '%s', and '%s' together into \"%%s/%%s/%%s\" for assembly preloading.\n", bundle_path, culture, filename); - abort (); - } - if (res <= 0) { - LOG_ERROR ("Could not format '%s' and '%s' together into \"%%s/%%s\" for assembly preloading.\n", bundle_path, filename); - abort (); - } - - struct stat buffer; - if (stat (path, &buffer) == 0) { - MonoAssembly *assembly = mono_assembly_open (path, NULL); - if (!assembly) { - LOG_ERROR ("Could not open assembly '%s'.\n", path); - abort (); - } - return assembly; - } - LOG_INFO ("Could not stat file '%s'. Did not successfully preload assembly.\n", path); - return NULL; -} - -static MonoAssembly* -mono_assembly_preload_hook (MonoAssemblyName *aname, char **assemblies_path, void* user_data) -{ - const char *name = mono_assembly_name_get_name (aname); - const char *culture = mono_assembly_name_get_culture (aname); - return mono_load_assembly (name, culture); -} - static unsigned char * load_aot_data (MonoAssembly *assembly, int size, void *user_data, void **out_handle) { *out_handle = NULL; - char path [1024]; - int res; - MonoAssemblyName *assembly_name = mono_assembly_get_name (assembly); const char *aname = mono_assembly_name_get_name (assembly_name); - res = snprintf (path, sizeof (path) - 1, "%s/%s.aotdata", bundle_path, aname); - if (res <= 0) { - LOG_ERROR ("Could not format '%s' and '%s' together into \"%%s/%%s.aotdata\" for assembly preloading.\n", bundle_path, aname); + size_t str_len = sizeof (char) * (strlen (bundle_path) + strlen (aname) + 10); // +1 "/", +8 ".aotdata", +1 null-terminating char + char *file_path = (char *)malloc (str_len); + if (!file_path) { + LOG_ERROR ("Out of memory.\n"); abort (); } - int fd = open (path, O_RDONLY); + int res = snprintf (file_path, str_len, "%s/%s.aotdata", bundle_path, aname); + + int fd = open (file_path, O_RDONLY); if (fd < 0) { - LOG_INFO ("Could not open file '%s' while trying to load aot data.\n", path); + LOG_INFO ("Could not open file '%s' while trying to load aot data.\n", file_path); return NULL; } void *ptr = mmap (NULL, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); if (ptr == MAP_FAILED) { - LOG_INFO ("Could not mmap file '%s'.\n", path); + LOG_INFO ("Could not mmap file '%s'.\n", file_path); close (fd); return NULL; } @@ -180,7 +124,7 @@ runtime_init_callback () { const char *assemblies_path = strdup(getenv("%ASSEMBLIES_LOCATION%")); if (!assemblies_path) { - LOG_ERROR ("Could not duplicate value of environment variable %ASSEMBLIES_LOCATION% due to insufficient memory.\n"); + LOG_ERROR ("Out of memory.\n"); abort (); } bundle_path = (assemblies_path[0] != '\0') ? assemblies_path : "./"; @@ -195,8 +139,6 @@ runtime_init_callback () mono_jit_set_aot_only (true); - mono_install_assembly_preload_hook (mono_assembly_preload_hook, NULL); - mono_install_load_aot_data_hook (load_aot_data, free_aot_data, NULL); mono_set_signal_chaining (true); From 5c2135f08e8af4f0552bcf0d3ffb397a05791a3a Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 14 Mar 2023 16:04:23 -0400 Subject: [PATCH 22/30] Throw when no exported symbols are found --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index dc901825b1941c..2391b0f3aba4b8 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -169,6 +169,10 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot } } } + if (exportedAssemblies.Count == 0) + { + throw new LogAsErrorException($"None of the compiled assemblies contain exported symbols. Resulting shared library would be unusable."); + } // for android, all symbols to keep go in one linker script // From 5b6cb510af2c25bad0f2bdaa7373da0c5aa07465 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Tue, 14 Mar 2023 18:46:18 -0400 Subject: [PATCH 23/30] Remove aot data load prehook and aotdata generation --- .../msbuild/android/build/AndroidApp.targets | 2 + src/mono/msbuild/apple/build/AppleApp.targets | 2 + src/tasks/LibraryBuilder/Templates/autoinit.c | 47 +------------------ 3 files changed, 5 insertions(+), 46 deletions(-) diff --git a/src/mono/msbuild/android/build/AndroidApp.targets b/src/mono/msbuild/android/build/AndroidApp.targets index e1a5363d22fbe4..b96a31c8aea669 100644 --- a/src/mono/msbuild/android/build/AndroidApp.targets +++ b/src/mono/msbuild/android/build/AndroidApp.targets @@ -151,6 +151,7 @@ <_EnableUnmanagedCallersOnlyMethodsExport Condition="'$(_IsLibraryMode)' == 'true'">true + <_UseAotDataFile Condition="'$(_IsLibraryMode)' == 'true' and '$(_UseAotDataFile)' == ''">false diff --git a/src/mono/msbuild/apple/build/AppleApp.targets b/src/mono/msbuild/apple/build/AppleApp.targets index 6e97c7b016c847..e0f87db0203dab 100644 --- a/src/mono/msbuild/apple/build/AppleApp.targets +++ b/src/mono/msbuild/apple/build/AppleApp.targets @@ -93,6 +93,7 @@ <_EnableUnmanagedCallersOnlyMethodsExport Condition="'$(_IsLibraryMode)' == 'true'">true + <_UseAotDataFile Condition="'$(_IsLibraryMode)' == 'true' and '_UseAotDataFile' == ''">false @@ -166,6 +167,7 @@ Mode="$(_AOTMode)" OutputDir="$(_MobileIntermediateOutputPath)" OutputType="AsmOnly" + UseAotDataFile="$(_UseAotDataFile)" UseLLVM="$(MonoEnableLLVM)"> diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 50f2a15a13118b..3592f1d39eaff5 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -1,13 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#include #include +#include #include #include #include -#include #include -#include #include #include @@ -78,47 +76,6 @@ initialize_appctx_env_variables () monovm_initialize(2, appctx_keys, appctx_values); } -static unsigned char * -load_aot_data (MonoAssembly *assembly, int size, void *user_data, void **out_handle) -{ - *out_handle = NULL; - - MonoAssemblyName *assembly_name = mono_assembly_get_name (assembly); - const char *aname = mono_assembly_name_get_name (assembly_name); - - size_t str_len = sizeof (char) * (strlen (bundle_path) + strlen (aname) + 10); // +1 "/", +8 ".aotdata", +1 null-terminating char - char *file_path = (char *)malloc (str_len); - if (!file_path) { - LOG_ERROR ("Out of memory.\n"); - abort (); - } - - int res = snprintf (file_path, str_len, "%s/%s.aotdata", bundle_path, aname); - - int fd = open (file_path, O_RDONLY); - if (fd < 0) { - LOG_INFO ("Could not open file '%s' while trying to load aot data.\n", file_path); - return NULL; - } - - void *ptr = mmap (NULL, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); - if (ptr == MAP_FAILED) { - LOG_INFO ("Could not mmap file '%s'.\n", file_path); - close (fd); - return NULL; - } - - close (fd); - *out_handle = ptr; - return (unsigned char *) ptr; -} - -static void -free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) -{ - munmap (handle, size); -} - void runtime_init_callback () { @@ -139,8 +96,6 @@ runtime_init_callback () mono_jit_set_aot_only (true); - mono_install_load_aot_data_hook (load_aot_data, free_aot_data, NULL); - mono_set_signal_chaining (true); MonoDomain *domain = mono_jit_init ("mono.self.contained.library"); From b46fdb34f57ee24bcb159ab0747dc329275c6d40 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Wed, 15 Mar 2023 11:45:30 -0400 Subject: [PATCH 24/30] Address feedback --- src/mono/msbuild/apple/build/AppleApp.targets | 2 + src/tasks/LibraryBuilder/LibraryBuilder.cs | 15 +++--- .../Templates/CMakeLists.txt.template | 10 ++-- src/tasks/LibraryBuilder/Templates/autoinit.c | 48 +++++++------------ .../Templates/preloaded_assemblies.c | 23 +++------ 5 files changed, 43 insertions(+), 55 deletions(-) diff --git a/src/mono/msbuild/apple/build/AppleApp.targets b/src/mono/msbuild/apple/build/AppleApp.targets index e0f87db0203dab..bb32c14e629474 100644 --- a/src/mono/msbuild/apple/build/AppleApp.targets +++ b/src/mono/msbuild/apple/build/AppleApp.targets @@ -105,6 +105,8 @@ + + diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 2391b0f3aba4b8..13dc024d81c2ef 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -126,14 +126,17 @@ public override bool Execute() GatherAotSourcesObjects(aotSources, aotObjects, extraSources, linkerArgs); GatherLinkerArgs(linkerArgs); + File.WriteAllText(Path.Combine(OutputDirectory, "shared_library_log.h"), + Utils.GetEmbeddedResource("shared_library_log.h")); + + GenerateAssembliesLoader(); + if (!UsesCustomRuntimeInitCallback) { WriteAutoInitializationFromTemplate(); extraSources.AppendLine(" autoinit.c"); } - GenerateAssembliesLoader(); - WriteCMakeFileFromTemplate(aotSources.ToString(), aotObjects.ToString(), extraSources.ToString(), linkerArgs.ToString()); OutputPath = BuildLibrary(); @@ -252,21 +255,21 @@ private void WriteAutoInitializationFromTemplate() { File.WriteAllText(Path.Combine(OutputDirectory, "autoinit.c"), Utils.GetEmbeddedResource("autoinit.c") - .Replace("%ASSEMBLIES_LOCATION%", !string.IsNullOrEmpty(AssembliesLocation) ? AssembliesLocation : "DOTNET_ASSETS_PATH") + .Replace("%ASSEMBLIES_LOCATION%", !string.IsNullOrEmpty(AssembliesLocation) ? AssembliesLocation : "DOTNET_LIBRARY_ASSEMBLY_PATH") .Replace("%RUNTIME_IDENTIFIER%", RuntimeIdentifier)); } private void GenerateAssembliesLoader() { - var assembliesLoader = new StringBuilder(); + var assemblyPreloaders = new List(); foreach (string exportedAssembly in exportedAssemblies) { - assembliesLoader.Append($" mono_assembly_load_with_partial_name_check(\"{exportedAssembly}\");\n"); + assemblyPreloaders.Add($"preload_assembly(\"{exportedAssembly}\");"); } File.WriteAllText(Path.Combine(OutputDirectory, "preloaded_assemblies.c"), Utils.GetEmbeddedResource("preloaded_assemblies.c") - .Replace("%ASSEMBLIES_LOADER%", assembliesLoader.ToString())); + .Replace("%ASSEMBLIES_PRELOADER%", string.Join("\n ", assemblyPreloaders))); } private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, string extraSources, string linkerArgs) diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index f7278df47476d8..b4f53f380c48fc 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -11,8 +11,9 @@ set(DOTNET_AOT_OBJECTS %AotObjects% ) set(DOTNET_EXTRA_SOURCES -%ExtraSources% preloaded_assemblies.c -) + shared_library_log.h + preloaded_assemblies.c +%ExtraSources%) include_directories("%MonoInclude%") @@ -37,7 +38,7 @@ if(TARGETS_ANDROID) log ) add_definitions(-DHOST_ANDROID=1) -else() +elseif(TARGETS_APPLE_MOBILE) set(MOBILE_SYSTEM_LIBS "-framework Foundation" "-framework Security" @@ -46,6 +47,9 @@ else() "-lc++" "-liconv" ) + add_definitiions(-DHOST_APPLE_MOBILE=1) +else() + message(FATAL_ERROR "Unsupported Platform. Ensure the TargetOS is supported by the LibraryBuilder and the platform specific libs are added here.") endif() target_link_libraries( diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 3592f1d39eaff5..bf4b8f193d753d 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -11,28 +11,19 @@ #include #include -#ifdef HOST_ANDROID -#include - -#define LOG_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO, "MONO_SELF_CONTAINED_LIBRARY", fmt, ##__VA_ARGS__) -#define LOG_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, "MONO_SELF_CONTAINED_LIBRARY", fmt, ##__VA_ARGS__) -#else -#include - -#define LOG_INFO(fmt, ...) os_log_info (OS_LOG_DEFAULT, fmt, ##__VA_ARGS__) -#define LOG_ERROR(fmt, ...) os_log_error (OS_LOG_DEFAULT, fmt, ##__VA_ARGS__) -#endif +#include "shared_library_log.h" static const char *bundle_path; void register_aot_modules (void); -void load_assemblies_with_exported_symbols (); +void preload_assemblies_with_exported_symbols (); typedef void (*MonoRuntimeInitCallback) (void); void mono_set_runtime_init_callback (MonoRuntimeInitCallback callback); static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) { + free ((void *)args->runtimeconfig.name.path); free (args); free (user_data); } @@ -43,21 +34,25 @@ initialize_runtimeconfig () char *file_name = "runtimeconfig.bin"; size_t str_len = sizeof (char) * (strlen (bundle_path) + strlen (file_name) + 2); // +1 "/", +1 null-terminating char char *file_path = (char *)malloc (str_len); - if (!file_path) { + if (!file_path) LOG_ERROR ("Out of memory.\n"); - abort (); - } int num_char = snprintf (file_path, str_len, "%s/%s", bundle_path, file_name); + if (num_char < 0) + LOG_ERROR ("Encoding error while formatting '%s' and '%s' into \"%%s/%%s\".\n", bundle_path, file_name); + struct stat buffer; if (stat (file_path, &buffer) == 0) { MonovmRuntimeConfigArguments *arg = (MonovmRuntimeConfigArguments *)malloc (sizeof (MonovmRuntimeConfigArguments)); + if (!arg) + LOG_ERROR ("Out of memory.\n"); + arg->kind = 0; arg->runtimeconfig.name.path = file_path; - monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, file_path); + monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, NULL); } else { - LOG_INFO ("Could not stat file '%s'. Runtime configuration properties not initialized.\n", file_path); + LOG_INFO ("Could not find file '%s'. Runtime configuration properties not initialized.\n", file_path); free (file_path); } } @@ -76,15 +71,12 @@ initialize_appctx_env_variables () monovm_initialize(2, appctx_keys, appctx_values); } -void +static void runtime_init_callback () { - const char *assemblies_path = strdup(getenv("%ASSEMBLIES_LOCATION%")); - if (!assemblies_path) { - LOG_ERROR ("Out of memory.\n"); - abort (); - } - bundle_path = (assemblies_path[0] != '\0') ? assemblies_path : "./"; + bundle_path = getenv("%ASSEMBLIES_LOCATION%"); + if (!bundle_path || bundle_path[0] == '\0') + bundle_path = "./"; initialize_runtimeconfig (); @@ -99,14 +91,10 @@ runtime_init_callback () mono_set_signal_chaining (true); MonoDomain *domain = mono_jit_init ("mono.self.contained.library"); - if (!domain) { + if (!domain) LOG_ERROR ("Could not auto initialize runtime.\n"); - abort (); - } - - load_assemblies_with_exported_symbols (); - free ((void *)assemblies_path); + preload_assemblies_with_exported_symbols (); } void __attribute__((constructor)) diff --git a/src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c b/src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c index d92404fb81aad7..62d024dcdfbaa5 100644 --- a/src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c +++ b/src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c @@ -4,27 +4,18 @@ #include -#ifdef HOST_ANDROID -#include +#include "shared_library_log.h" -#define LOG_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR, "MONO_SELF_CONTAINED_LIBRARY", fmt, ##__VA_ARGS__) -#else -#include - -#define LOG_ERROR(fmt, ...) os_log_error (OS_LOG_DEFAULT, fmt, ##__VA_ARGS__) -#endif - -void -mono_assembly_load_with_partial_name_check (const char* filename) +static void +preload_assembly (const char* filename) { MonoAssembly *assembly = mono_assembly_load_with_partial_name (filename, NULL); - if (!assembly) { + if (!assembly) LOG_ERROR ("Could not open assembly '%s'. Unable to properly initialize GOT slots.\n", filename); - abort (); - } } void -load_assemblies_with_exported_symbols () +preload_assemblies_with_exported_symbols () { -%ASSEMBLIES_LOADER%} \ No newline at end of file + %ASSEMBLIES_PRELOADER% +} \ No newline at end of file From 5761f4c41602685bf0d2a0d4069553c94481e885 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Thu, 16 Mar 2023 11:08:39 -0400 Subject: [PATCH 25/30] Address more feedback --- .../msbuild/android/build/AndroidApp.targets | 20 +++++++---- src/mono/msbuild/apple/build/AppleApp.targets | 13 ++++--- .../msbuild/common/LibraryBuilder.targets | 3 +- src/tasks/LibraryBuilder/LibraryBuilder.cs | 19 +++++++---- .../Templates/CMakeLists.txt.template | 4 +-- src/tasks/LibraryBuilder/Templates/autoinit.c | 7 +--- .../Templates/library-builder.h | 34 +++++++++++++++++++ ...ed_assemblies.c => preloaded-assemblies.c} | 4 +-- 8 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 src/tasks/LibraryBuilder/Templates/library-builder.h rename src/tasks/LibraryBuilder/Templates/{preloaded_assemblies.c => preloaded-assemblies.c} (75%) diff --git a/src/mono/msbuild/android/build/AndroidApp.targets b/src/mono/msbuild/android/build/AndroidApp.targets index b96a31c8aea669..c3091a42ae5630 100644 --- a/src/mono/msbuild/android/build/AndroidApp.targets +++ b/src/mono/msbuild/android/build/AndroidApp.targets @@ -76,6 +76,11 @@ <_AOTMode Condition="'$(ForceFullAOT)' == 'true'">Full + + <_UsesRuntimeInitCallback>$(UsesRuntimeInitCallback) + <_UsesRuntimeInitCallback Condition="'$(_UsesRuntimeInitCallback)' == ''">true + + @@ -85,8 +90,8 @@ - - + + @@ -135,6 +140,12 @@ Condition="'$(RunAOTCompilation)' == 'true'" DependsOnTargets="_AndroidBeforeAotCompileApp"> + + <_EnableUnmanagedCallersOnlyMethodsExport>true + <_UseAotDataFile>$(UseAotDataFile)<_UseAotDataFile> + <_UseAotDataFile Condition="'_UseAotDataFile' == ''">false + + @@ -149,11 +160,6 @@ - - <_EnableUnmanagedCallersOnlyMethodsExport Condition="'$(_IsLibraryMode)' == 'true'">true - <_UseAotDataFile Condition="'$(_IsLibraryMode)' == 'true' and '$(_UseAotDataFile)' == ''">false - - JustInterp - - <_EnableUnmanagedCallersOnlyMethodsExport Condition="'$(_IsLibraryMode)' == 'true'">true - <_UseAotDataFile Condition="'$(_IsLibraryMode)' == 'true' and '_UseAotDataFile' == ''">false + + <_EnableUnmanagedCallersOnlyMethodsExport>true + <_UseAotDataFile>$(UseAotDataFile)<_UseAotDataFile> + <_UseAotDataFile Condition="'_UseAotDataFile' == ''">false + <_UsesRuntimeInitCallback>$(UsesRuntimeInitCallback) + <_UsesRuntimeInitCallback Condition="'$(_UsesRuntimeInitCallback)' == ''">true @@ -105,8 +108,8 @@ - - + + diff --git a/src/mono/msbuild/common/LibraryBuilder.targets b/src/mono/msbuild/common/LibraryBuilder.targets index c80aa354dac3ff..5b4da1edea3f99 100644 --- a/src/mono/msbuild/common/LibraryBuilder.targets +++ b/src/mono/msbuild/common/LibraryBuilder.targets @@ -29,7 +29,8 @@ RuntimeIdentifier="$(RuntimeIdentifier)" RuntimeLibraries="@(_RuntimeLibraries)" TargetOS="$(TargetOS)" - UsesCustomRuntimeInitCallback="$(_UsesCustomRuntimeInitCallback)"> + UsesCustomRuntimeInitCallback="$(_UsesCustomRuntimeInitCallback)" + UsesRuntimeInitCallback="$(_UsesRuntimeInitCallback)"> diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 13dc024d81c2ef..2dcac13c139821 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -69,10 +69,15 @@ public bool IsSharedLibrary /// /// Determines whether or not the mono runtime auto initialization - /// tremplate, autoinit.c, is used. + /// template, autoinit.c, is used. /// public bool UsesCustomRuntimeInitCallback { get; set; } + /// + /// Determines if there is a mono runtime init callback + /// + public bool UsesRuntimeInitCallback { get; set; } + /// /// The environment variable name that will point to where assemblies /// are located on the app host device. @@ -126,12 +131,12 @@ public override bool Execute() GatherAotSourcesObjects(aotSources, aotObjects, extraSources, linkerArgs); GatherLinkerArgs(linkerArgs); - File.WriteAllText(Path.Combine(OutputDirectory, "shared_library_log.h"), - Utils.GetEmbeddedResource("shared_library_log.h")); + File.WriteAllText(Path.Combine(OutputDirectory, "library-builder.h"), + Utils.GetEmbeddedResource("library-builder.h")); GenerateAssembliesLoader(); - if (!UsesCustomRuntimeInitCallback) + if (UsesRuntimeInitCallback && !UsesCustomRuntimeInitCallback) { WriteAutoInitializationFromTemplate(); extraSources.AppendLine(" autoinit.c"); @@ -172,7 +177,7 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot } } } - if (exportedAssemblies.Count == 0) + if (IsSharedLibrary && exportedAssemblies.Count == 0) { throw new LogAsErrorException($"None of the compiled assemblies contain exported symbols. Resulting shared library would be unusable."); } @@ -267,8 +272,8 @@ private void GenerateAssembliesLoader() assemblyPreloaders.Add($"preload_assembly(\"{exportedAssembly}\");"); } - File.WriteAllText(Path.Combine(OutputDirectory, "preloaded_assemblies.c"), - Utils.GetEmbeddedResource("preloaded_assemblies.c") + File.WriteAllText(Path.Combine(OutputDirectory, "preloaded-assemblies.c"), + Utils.GetEmbeddedResource("preloaded-assemblies.c") .Replace("%ASSEMBLIES_PRELOADER%", string.Join("\n ", assemblyPreloaders))); } diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index b4f53f380c48fc..b02627ed936d63 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -11,8 +11,8 @@ set(DOTNET_AOT_OBJECTS %AotObjects% ) set(DOTNET_EXTRA_SOURCES - shared_library_log.h - preloaded_assemblies.c + library-builder.h + preloaded-assemblies.c %ExtraSources%) include_directories("%MonoInclude%") diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index bf4b8f193d753d..d3fd8b34f5d5b8 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -11,15 +11,10 @@ #include #include -#include "shared_library_log.h" +#include "library-builder.h" static const char *bundle_path; -void register_aot_modules (void); -void preload_assemblies_with_exported_symbols (); -typedef void (*MonoRuntimeInitCallback) (void); -void mono_set_runtime_init_callback (MonoRuntimeInitCallback callback); - static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) { diff --git a/src/tasks/LibraryBuilder/Templates/library-builder.h b/src/tasks/LibraryBuilder/Templates/library-builder.h new file mode 100644 index 00000000000000..691518b30a491b --- /dev/null +++ b/src/tasks/LibraryBuilder/Templates/library-builder.h @@ -0,0 +1,34 @@ +#include + +#if defined(HOST_ANDROID) + +#include + +#define LOG_ERROR(fmt, ...) \ + do \ + { \ + __android_log_print(ANDROID_LOG_ERROR, "MONO_SELF_CONTAINED_LIBRARY", fmt, ##__VA_ARGS__); \ + abort (); \ + } while (0) + +#elif defined(HOST_APPLE_MOBILE) + +#include + +#define LOG_ERROR(fmt, ...) \ + do \ + { \ + os_log_error (OS_LOG_DEFAULT, fmt, ##__VA_ARGS__); \ + abort (); \ + } while (0) + +#else + +#error Unsupported Host Platform. Ensure the hosting platform is supported by the LibraryBuilder and the appropriate logging functions are added. + +#endif + +void register_aot_modules (void); +void preload_assemblies_with_exported_symbols (); +typedef void (*MonoRuntimeInitCallback) (void); +void mono_set_runtime_init_callback (MonoRuntimeInitCallback callback); \ No newline at end of file diff --git a/src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c b/src/tasks/LibraryBuilder/Templates/preloaded-assemblies.c similarity index 75% rename from src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c rename to src/tasks/LibraryBuilder/Templates/preloaded-assemblies.c index 62d024dcdfbaa5..71be26d870dc0b 100644 --- a/src/tasks/LibraryBuilder/Templates/preloaded_assemblies.c +++ b/src/tasks/LibraryBuilder/Templates/preloaded-assemblies.c @@ -4,14 +4,14 @@ #include -#include "shared_library_log.h" +#include "library-builder.h" static void preload_assembly (const char* filename) { MonoAssembly *assembly = mono_assembly_load_with_partial_name (filename, NULL); if (!assembly) - LOG_ERROR ("Could not open assembly '%s'. Unable to properly initialize GOT slots.\n", filename); + LOG_ERROR ("Could not open assembly '%s'.\n", filename); } void From a2d03603d21ef200ed591164d12f75b382336d6c Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Thu, 16 Mar 2023 11:53:47 -0400 Subject: [PATCH 26/30] Add various fixes and make bundle_path a local variable --- src/mono/msbuild/android/build/AndroidApp.targets | 6 +++--- src/mono/msbuild/apple/build/AppleApp.targets | 2 +- src/tasks/LibraryBuilder/Templates/autoinit.c | 15 ++++++--------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/mono/msbuild/android/build/AndroidApp.targets b/src/mono/msbuild/android/build/AndroidApp.targets index c3091a42ae5630..4bc558943daebe 100644 --- a/src/mono/msbuild/android/build/AndroidApp.targets +++ b/src/mono/msbuild/android/build/AndroidApp.targets @@ -76,7 +76,7 @@ <_AOTMode Condition="'$(ForceFullAOT)' == 'true'">Full - + <_UsesRuntimeInitCallback>$(UsesRuntimeInitCallback) <_UsesRuntimeInitCallback Condition="'$(_UsesRuntimeInitCallback)' == ''">true @@ -142,8 +142,8 @@ <_EnableUnmanagedCallersOnlyMethodsExport>true - <_UseAotDataFile>$(UseAotDataFile)<_UseAotDataFile> - <_UseAotDataFile Condition="'_UseAotDataFile' == ''">false + <_UseAotDataFile>$(UseAotDataFile) + <_UseAotDataFile Condition="'$(_UseAotDataFile)' == ''">false diff --git a/src/mono/msbuild/apple/build/AppleApp.targets b/src/mono/msbuild/apple/build/AppleApp.targets index 6a21ca8ae32893..a648da357d7304 100644 --- a/src/mono/msbuild/apple/build/AppleApp.targets +++ b/src/mono/msbuild/apple/build/AppleApp.targets @@ -94,7 +94,7 @@ <_EnableUnmanagedCallersOnlyMethodsExport>true <_UseAotDataFile>$(UseAotDataFile)<_UseAotDataFile> - <_UseAotDataFile Condition="'_UseAotDataFile' == ''">false + <_UseAotDataFile Condition="'$(_UseAotDataFile)' == ''">false <_UsesRuntimeInitCallback>$(UsesRuntimeInitCallback) <_UsesRuntimeInitCallback Condition="'$(_UsesRuntimeInitCallback)' == ''">true diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index d3fd8b34f5d5b8..6496f46b9501b2 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -13,8 +13,6 @@ #include "library-builder.h" -static const char *bundle_path; - static void cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) { @@ -24,7 +22,7 @@ cleanup_runtime_config (MonovmRuntimeConfigArguments *args, void *user_data) } static void -initialize_runtimeconfig () +initialize_runtimeconfig (const char *bundle_path) { char *file_name = "runtimeconfig.bin"; size_t str_len = sizeof (char) * (strlen (bundle_path) + strlen (file_name) + 2); // +1 "/", +1 null-terminating char @@ -33,7 +31,7 @@ initialize_runtimeconfig () LOG_ERROR ("Out of memory.\n"); int num_char = snprintf (file_path, str_len, "%s/%s", bundle_path, file_name); - if (num_char < 0) + if (num_char <= 0 || num_char >= str_len) LOG_ERROR ("Encoding error while formatting '%s' and '%s' into \"%%s/%%s\".\n", bundle_path, file_name); struct stat buffer; @@ -47,13 +45,12 @@ initialize_runtimeconfig () arg->runtimeconfig.name.path = file_path; monovm_runtimeconfig_initialize (arg, cleanup_runtime_config, NULL); } else { - LOG_INFO ("Could not find file '%s'. Runtime configuration properties not initialized.\n", file_path); free (file_path); } } static void -initialize_appctx_env_variables () +initialize_appctx_env_variables (const char *bundle_path) { const char *appctx_keys[2], *appctx_values[2]; @@ -69,13 +66,13 @@ initialize_appctx_env_variables () static void runtime_init_callback () { - bundle_path = getenv("%ASSEMBLIES_LOCATION%"); + const char *bundle_path = getenv("%ASSEMBLIES_LOCATION%"); if (!bundle_path || bundle_path[0] == '\0') bundle_path = "./"; - initialize_runtimeconfig (); + initialize_runtimeconfig (bundle_path); - initialize_appctx_env_variables (); + initialize_appctx_env_variables (bundle_path); register_aot_modules (); From 5c7bec85c7c040d6ce47f3c14eadb648c07de871 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Thu, 16 Mar 2023 13:29:08 -0400 Subject: [PATCH 27/30] Reintroduce load aot data hook in autoinit with guard --- src/tasks/LibraryBuilder/LibraryBuilder.cs | 20 ++++++ .../Templates/CMakeLists.txt.template | 2 + src/tasks/LibraryBuilder/Templates/autoinit.c | 61 +++++++++++++++++-- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/tasks/LibraryBuilder/LibraryBuilder.cs b/src/tasks/LibraryBuilder/LibraryBuilder.cs index 2dcac13c139821..dd3a51324f5a8e 100644 --- a/src/tasks/LibraryBuilder/LibraryBuilder.cs +++ b/src/tasks/LibraryBuilder/LibraryBuilder.cs @@ -19,6 +19,7 @@ public class LibraryBuilderTask : AppBuilderTask private string cmakeProjectLanguages = ""; private string targetOS = ""; + private bool usesAOTDataFile; private List exportedAssemblies = new List(); /// @@ -160,6 +161,11 @@ private void GatherAotSourcesObjects(StringBuilder aotSources, StringBuilder aot aotSources.AppendLine($" {compiledAssembly.AssemblerFile}"); } + if (!usesAOTDataFile && !string.IsNullOrEmpty(compiledAssembly.DataFile)) + { + usesAOTDataFile = true; + } + if (!string.IsNullOrEmpty(compiledAssembly.LlvmObjectFile)) { aotObjects.AppendLine($" {compiledAssembly.LlvmObjectFile}"); @@ -279,6 +285,7 @@ private void GenerateAssembliesLoader() private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, string extraSources, string linkerArgs) { + string extraDefinitions = GenerateExtraDefinitions(); // BundleDir File.WriteAllText(Path.Combine(OutputDirectory, "CMakeLists.txt"), Utils.GetEmbeddedResource("CMakeLists.txt.template") @@ -288,10 +295,23 @@ private void WriteCMakeFileFromTemplate(string aotSources, string aotObjects, st .Replace("%MonoInclude%", MonoRuntimeHeaders) .Replace("%AotSources%", aotSources) .Replace("%AotObjects%", aotObjects) + .Replace("%ExtraDefinitions%", extraDefinitions) .Replace("%ExtraSources%", extraSources) .Replace("%LIBRARY_LINKER_ARGS%", linkerArgs)); } + private string GenerateExtraDefinitions() + { + var extraDefinitions = new StringBuilder(); + + if (usesAOTDataFile) + { + extraDefinitions.AppendLine("add_definitions(-DUSES_AOT_DATA=1)"); + } + + return extraDefinitions.ToString(); + } + private string BuildLibrary() { string libraryOutputPath; diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index b02627ed936d63..cf5c9822dd3131 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -52,6 +52,8 @@ else() message(FATAL_ERROR "Unsupported Platform. Ensure the TargetOS is supported by the LibraryBuilder and the platform specific libs are added here.") endif() +%ExtraDefinitions% + target_link_libraries( %LIBRARY_NAME% PUBLIC diff --git a/src/tasks/LibraryBuilder/Templates/autoinit.c b/src/tasks/LibraryBuilder/Templates/autoinit.c index 6496f46b9501b2..6fd37510d00a1c 100644 --- a/src/tasks/LibraryBuilder/Templates/autoinit.c +++ b/src/tasks/LibraryBuilder/Templates/autoinit.c @@ -6,6 +6,11 @@ #include #include #include +#if defined(USES_AOT_DATA) +#include +#include +#include +#endif #include #include @@ -60,15 +65,59 @@ initialize_appctx_env_variables (const char *bundle_path) appctx_keys[1] = "APP_CONTEXT_BASE_DIRECTORY"; appctx_values[1] = bundle_path; - monovm_initialize(2, appctx_keys, appctx_values); + monovm_initialize (2, appctx_keys, appctx_values); +} + +#if defined(USES_AOT_DATA) +static unsigned char * +load_aot_data (MonoAssembly *assembly, int size, void *user_data, void **out_handle) +{ + *out_handle = NULL; + const char *bundle_path = (const char*)user_data; + + MonoAssemblyName *assembly_name = mono_assembly_get_name (assembly); + const char *aname = mono_assembly_name_get_name (assembly_name); + + size_t str_len = sizeof (char) * (strlen (bundle_path) + strlen (aname) + 10); // +1 "/", +8 ".aotdata", +1 null-terminating char + char *file_path = (char *)malloc (str_len); + if (!file_path) + LOG_ERROR ("Out of memory.\n"); + + int res = snprintf (file_path, str_len, "%s/%s.aotdata", bundle_path, aname); + if (res <= 0 || res >= str_len) + LOG_ERROR ("Encoding error while formatting '%s' and '%s' into \"%%s/%%s\".\n", bundle_path, aname); + + int fd = open (file_path, O_RDONLY); + if (fd < 0) + LOG_ERROR ("Could not open file '%s'.\n", file_path); + + void *ptr = mmap (NULL, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0); + close (fd); + if (ptr == MAP_FAILED) + LOG_ERROR ("Could not map file '%s' to memory.\n", file_path); + + *out_handle = ptr; + return (unsigned char *) ptr; } +static void +free_aot_data (MonoAssembly *assembly, int size, void *user_data, void *handle) +{ + munmap (handle, size); +} +#endif + static void runtime_init_callback () { - const char *bundle_path = getenv("%ASSEMBLIES_LOCATION%"); - if (!bundle_path || bundle_path[0] == '\0') - bundle_path = "./"; + const char *assemblies_location = getenv ("%ASSEMBLIES_LOCATION%"); + if (!assemblies_location || assemblies_location[0] == '\0') + assemblies_location = "./"; + + // Don't free as load_aot_data may be called later on, if used. + const char *bundle_path = strdup (assemblies_location); + if (!bundle_path) + LOG_ERROR ("Out of memory.\n"); initialize_runtimeconfig (bundle_path); @@ -80,6 +129,10 @@ runtime_init_callback () mono_jit_set_aot_only (true); +#if defined(USES_AOT_DATA) + mono_install_load_aot_data_hook (load_aot_data, free_aot_data, bundle_path); +#endif + mono_set_signal_chaining (true); MonoDomain *domain = mono_jit_init ("mono.self.contained.library"); From 1af3bd92e15051f4302ec903ae04c95d5a8f87fe Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Thu, 16 Mar 2023 15:39:58 -0400 Subject: [PATCH 28/30] Fix closing tag --- src/mono/msbuild/apple/build/AppleApp.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/msbuild/apple/build/AppleApp.targets b/src/mono/msbuild/apple/build/AppleApp.targets index a648da357d7304..7e77d8a5388aa0 100644 --- a/src/mono/msbuild/apple/build/AppleApp.targets +++ b/src/mono/msbuild/apple/build/AppleApp.targets @@ -93,7 +93,7 @@ <_EnableUnmanagedCallersOnlyMethodsExport>true - <_UseAotDataFile>$(UseAotDataFile)<_UseAotDataFile> + <_UseAotDataFile>$(UseAotDataFile) <_UseAotDataFile Condition="'$(_UseAotDataFile)' == ''">false <_UsesRuntimeInitCallback>$(UsesRuntimeInitCallback) <_UsesRuntimeInitCallback Condition="'$(_UsesRuntimeInitCallback)' == ''">true From e41c3d3981d58feb82764dc748159479b204a12d Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Thu, 16 Mar 2023 17:29:16 -0400 Subject: [PATCH 29/30] Include guard library-builder.h --- src/tasks/LibraryBuilder/Templates/library-builder.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/tasks/LibraryBuilder/Templates/library-builder.h b/src/tasks/LibraryBuilder/Templates/library-builder.h index 691518b30a491b..c0dee67be95adc 100644 --- a/src/tasks/LibraryBuilder/Templates/library-builder.h +++ b/src/tasks/LibraryBuilder/Templates/library-builder.h @@ -1,3 +1,9 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#ifndef __MONO_LIBRARY_BUILDER_H__ +#define __MONO_LIBRARY_BUILDER_H__ + #include #if defined(HOST_ANDROID) @@ -31,4 +37,6 @@ void register_aot_modules (void); void preload_assemblies_with_exported_symbols (); typedef void (*MonoRuntimeInitCallback) (void); -void mono_set_runtime_init_callback (MonoRuntimeInitCallback callback); \ No newline at end of file +void mono_set_runtime_init_callback (MonoRuntimeInitCallback callback); + +#endif /*__MONO_LIBRARY_BUILDER_H__*/ From f87980b60d99c8988de90250d9f39a053aabf9e9 Mon Sep 17 00:00:00 2001 From: mdh1418 Date: Thu, 16 Mar 2023 19:35:31 -0400 Subject: [PATCH 30/30] Fix typo --- src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template index cf5c9822dd3131..8f138c5fd25445 100644 --- a/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template +++ b/src/tasks/LibraryBuilder/Templates/CMakeLists.txt.template @@ -47,7 +47,7 @@ elseif(TARGETS_APPLE_MOBILE) "-lc++" "-liconv" ) - add_definitiions(-DHOST_APPLE_MOBILE=1) + add_definitions(-DHOST_APPLE_MOBILE=1) else() message(FATAL_ERROR "Unsupported Platform. Ensure the TargetOS is supported by the LibraryBuilder and the platform specific libs are added here.") endif()