From 39a2e380114d1bcac7bbf221e79e512daddbd041 Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Tue, 16 Mar 2021 22:41:25 -0400 Subject: [PATCH 01/19] Add new Mono embedding API to consume binary runtimeconfig format --- src/mono/mono/mini/mono-private-unstable.h | 18 +++++ src/mono/mono/mini/monovm.c | 86 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/src/mono/mono/mini/mono-private-unstable.h b/src/mono/mono/mini/mono-private-unstable.h index 4d746dd00e2581..236816f28632ca 100644 --- a/src/mono/mono/mini/mono-private-unstable.h +++ b/src/mono/mono/mini/mono-private-unstable.h @@ -15,6 +15,21 @@ #include #include +typedef struct { + uint32_t kind; // 0 = Path of runtimeconfig.blob, 1 = pointer to image data, >= 2 undefined + union { + struct { + const char *path; + } name; + struct { + const char *data; + uint32_t data_len; + } data; + } runtimeconfig; +} MonovmRuntimeConfigArguments; + +typedef void (*MonovmRuntimeConfigArgumentsCleanup) (MonovmRuntimeConfigArguments *args, void *user_data); + /* These are used to load the AOT data for aot images compiled with MONO_AOT_FILE_FLAG_SEPARATE_DATA */ /* * Return the AOT data for ASSEMBLY. SIZE is the size of the data. OUT_HANDLE should be set to a handle which is later @@ -29,6 +44,9 @@ mono_install_load_aot_data_hook (MonoLoadAotDataFunc load_func, MonoFreeAotDataF MONO_API int monovm_initialize (int propertyCount, const char **propertyKeys, const char **propertyValues); +MONO_API int +monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void* user_data); + //#ifdef HOST_WASM typedef void* (*MonoWasmGetNativeToInterpTramp) (MonoMethod *method, void *extra_arg); diff --git a/src/mono/mono/mini/monovm.c b/src/mono/mono/mini/monovm.c index ad6be1c8a86bcc..0afc8b2a53dfef 100644 --- a/src/mono/mono/mini/monovm.c +++ b/src/mono/mono/mini/monovm.c @@ -11,6 +11,8 @@ #include #include +#define MAX_PROPERTY_COUNT 100 + typedef struct { int assembly_count; char **basenames; /* Foo.dll */ @@ -226,6 +228,90 @@ monovm_initialize (int propertyCount, const char **propertyKeys, const char **pr return 0; } +void mono_extract_and_register_properties (const char *buffer, long fileSize); + +// Initialize monovm with properties set by runtimeconfig.json. Primarily used by mobile targets. +int +monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void *user_data) +{ + switch (arg->kind) { + case 0: { + FILE *filePtr = NULL; + long fileSize = 0; + char *buffer = NULL; + size_t result; + + filePtr = fopen(arg->runtimeconfig.name.path, "rb"); + g_assert (filePtr != NULL); + + fseek (filePtr , 0 , SEEK_END); + fileSize = ftell (filePtr); + rewind (filePtr); + + if (fileSize <= 0) + return 0; + + buffer = (char *) malloc (sizeof (char) * fileSize); + g_assert (buffer != NULL); + + result = fread (buffer,1,fileSize,filePtr); + g_assert (result == fileSize); + + mono_extract_and_register_properties (buffer, fileSize); + + fclose (filePtr); + g_free (buffer); + (*cleanup_fn) (arg, user_data); + return 0; + } + case 1: { + mono_extract_and_register_properties (arg->runtimeconfig.data.data, (long)arg->runtimeconfig.data.data_len); + (*cleanup_fn) (arg, user_data); + return 0; + } + default: + g_assert_not_reached (); + } +} + +void +mono_extract_and_register_properties (const char *buffer, long fileSize) +{ + int propertyCount; + int strLen; + int currentIdx = 0; + char *propertyKeys [MAX_PROPERTY_COUNT]; + char *propertyValues [MAX_PROPERTY_COUNT]; + + propertyCount = buffer [0] - '\0'; + currentIdx = 1; + for (int i = 0; i < propertyCount; ++i) + { + g_assert (fileSize > currentIdx); + strLen = buffer [currentIdx] - '\0'; + currentIdx++; + + g_assert (fileSize > (currentIdx + strLen)); + propertyKeys [i] = (char *) malloc (sizeof (char) * (strLen + 1)); + strncpy (propertyKeys [i], buffer + currentIdx, strLen); + propertyKeys [i][strLen] = '\0'; + currentIdx += strLen; + + g_assert (fileSize > currentIdx); + strLen = buffer [currentIdx] - '\0'; + currentIdx++; + + g_assert (fileSize >= (currentIdx + strLen)); + propertyValues [i] = (char *) malloc (sizeof (char) * (strLen + 1)); + strncpy (propertyValues [i], buffer + currentIdx, strLen); + propertyValues [i][strLen] = '\0'; + currentIdx += strLen; + } + + if (propertyCount > 0) + mono_runtime_register_appctx_properties (propertyCount, (const char **)propertyKeys, (const char **)propertyValues); +} + int monovm_execute_assembly (int argc, const char **argv, const char *managedAssemblyPath, unsigned int *exitCode) { From f29ee6f0812db67c1bb217ebd076529e15863242 Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Thu, 18 Mar 2021 14:41:18 -0400 Subject: [PATCH 02/19] Merge properties --- src/mono/mono/metadata/appdomain.c | 74 +++++++++++--- src/mono/mono/metadata/domain-internals.h | 3 + src/mono/mono/mini/monovm.c | 113 +++++++++++----------- 3 files changed, 117 insertions(+), 73 deletions(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 170560c081f7b0..9e7a964bd5d9d9 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -92,8 +92,12 @@ typedef struct static gboolean no_exec = FALSE; static int n_appctx_props; -static gunichar2 **appctx_keys; -static gunichar2 **appctx_values; +static char **appctx_keys; +static char **appctx_values; + +static int n_runtimeconfig_json_props; +static char **runtimeconfig_json_keys; +static char **runtimeconfig_json_values; static const char * mono_check_corlib_version_internal (void); @@ -1239,13 +1243,16 @@ void mono_runtime_register_appctx_properties (int nprops, const char **keys, const char **values) { n_appctx_props = nprops; - appctx_keys = g_new0 (gunichar2*, nprops); - appctx_values = g_new0 (gunichar2*, nprops); + appctx_keys = (char**)keys; + appctx_values = (char**)values; +} - for (int i = 0; i < nprops; ++i) { - appctx_keys [i] = g_utf8_to_utf16 (keys [i], strlen (keys [i]), NULL, NULL, NULL); - appctx_values [i] = g_utf8_to_utf16 (values [i], strlen (values [i]), NULL, NULL, NULL); - } +void +mono_runtime_register_runtimeconfig_json_properties (int nprops, const char **keys, const char **values) +{ + n_runtimeconfig_json_props = nprops; + runtimeconfig_json_keys = (char**)keys; + runtimeconfig_json_values = (char**)values; } static GENERATE_GET_CLASS_WITH_CACHE (appctx, "System", "AppContext") @@ -1256,27 +1263,62 @@ mono_runtime_install_appctx_properties (void) { ERROR_DECL (error); gpointer args [3]; + int n_total_props; + char **total_keys_raw; + char **total_values_raw; + gunichar2 **total_keys; + gunichar2 **total_values; MonoMethod *setup = mono_class_get_method_from_name_checked (mono_class_get_appctx_class (), "Setup", 3, 0, error); g_assert (setup); // FIXME: TRUSTED_PLATFORM_ASSEMBLIES is very large + // Combine and convert properties + n_total_props = n_appctx_props + n_runtimeconfig_json_props; + total_keys_raw = g_new0 (char*, n_total_props); + total_values_raw = g_new0 (char*, n_total_props);; + total_keys = g_new0 (gunichar2*, n_total_props); + total_values = g_new0 (gunichar2*, n_total_props); + for (int i = 0; i < n_appctx_props; ++i) { + total_keys_raw [i] = g_new0 (char, strlen (appctx_keys [i])); + total_values_raw [i] = (char *) malloc (sizeof (char) * (strlen (appctx_values [i]))); // g_new0 doesn't work well with TRUSTED_PLATFORM_ASSEMBLIES's value + strcpy(total_keys_raw [i], appctx_keys [i]); + strcpy(total_values_raw [i], appctx_values [i]); + } + for (int i = 0; i < n_runtimeconfig_json_props; ++i) { + total_keys_raw [i + n_appctx_props] = g_new0 (char, strlen (runtimeconfig_json_keys [i])); + total_values_raw [i + n_appctx_props] = g_new0 (char, strlen (runtimeconfig_json_values [i])); + strcpy(total_keys_raw [i + n_appctx_props], runtimeconfig_json_keys [i]); + strcpy(total_values_raw [i + n_appctx_props], runtimeconfig_json_values [i]); + } + for (int i = 0; i < n_total_props; ++i) { + total_keys [i] = g_utf8_to_utf16 (total_keys_raw [i], strlen (total_keys_raw [i]), NULL, NULL, NULL); + total_values [i] = g_utf8_to_utf16 (total_values_raw [i], strlen (total_values_raw [i]), NULL, NULL, NULL); + } + /* internal static unsafe void Setup(char** pNames, char** pValues, int count) */ - args [0] = appctx_keys; - args [1] = appctx_values; - args [2] = &n_appctx_props; + args [0] = total_keys; + args [1] = total_values; + args [2] = &n_total_props; mono_runtime_invoke_checked (setup, NULL, args, error); mono_error_assert_ok (error); /* No longer needed */ - for (int i = 0; i < n_appctx_props; ++i) { - g_free (appctx_keys [i]); - g_free (appctx_values [i]); + for (int i = 0; i < n_total_props; ++i) { + g_free (total_keys_raw [i]); + g_free (total_values_raw [i]); + g_free (total_keys [i]); + g_free (total_values [i]); } - g_free (appctx_keys); - g_free (appctx_values); + g_free (total_keys_raw); + g_free (total_values_raw); + g_free (total_keys); + g_free (total_values); + appctx_keys = NULL; appctx_values = NULL; + runtimeconfig_json_keys = NULL; + runtimeconfig_json_values = NULL; } diff --git a/src/mono/mono/metadata/domain-internals.h b/src/mono/mono/metadata/domain-internals.h index 9521b9cbfec053..ee7c6b4174ddf0 100644 --- a/src/mono/mono/metadata/domain-internals.h +++ b/src/mono/mono/metadata/domain-internals.h @@ -449,6 +449,9 @@ mono_domain_get_assemblies (MonoDomain *domain); void mono_runtime_register_appctx_properties (int nprops, const char **keys, const char **values); +void +mono_runtime_register_runtimeconfig_json_properties (int nprops, const char **keys, const char **values); + void mono_runtime_install_appctx_properties (void); diff --git a/src/mono/mono/mini/monovm.c b/src/mono/mono/mini/monovm.c index 0afc8b2a53dfef..3fa6a1951222e3 100644 --- a/src/mono/mono/mini/monovm.c +++ b/src/mono/mono/mini/monovm.c @@ -228,7 +228,7 @@ monovm_initialize (int propertyCount, const char **propertyKeys, const char **pr return 0; } -void mono_extract_and_register_properties (const char *buffer, long fileSize); +void mono_extract_and_register_properties (const char *buffer, guint64 file_size); // Initialize monovm with properties set by runtimeconfig.json. Primarily used by mobile targets. int @@ -236,37 +236,32 @@ monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntim { switch (arg->kind) { case 0: { - FILE *filePtr = NULL; - long fileSize = 0; - char *buffer = NULL; - size_t result; - - filePtr = fopen(arg->runtimeconfig.name.path, "rb"); - g_assert (filePtr != NULL); - - fseek (filePtr , 0 , SEEK_END); - fileSize = ftell (filePtr); - rewind (filePtr); - - if (fileSize <= 0) + guint64 file_size = 0; + MonoFileMap *map; + void *ret_handle; + char *buffer; + + map = mono_file_map_open (arg->runtimeconfig.name.path); + g_assert (map); + file_size = mono_file_map_size (map); + buffer = (char*)mono_file_map (file_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (map), 0, &ret_handle); + g_assert (buffer); + + if (file_size <= 0) return 0; - buffer = (char *) malloc (sizeof (char) * fileSize); - g_assert (buffer != NULL); - - result = fread (buffer,1,fileSize,filePtr); - g_assert (result == fileSize); - - mono_extract_and_register_properties (buffer, fileSize); + mono_extract_and_register_properties (buffer, file_size); - fclose (filePtr); - g_free (buffer); - (*cleanup_fn) (arg, user_data); + mono_file_unmap (map, ret_handle); + mono_file_map_close (map); + if (cleanup_fn) + (*cleanup_fn) (arg, user_data); return 0; } case 1: { - mono_extract_and_register_properties (arg->runtimeconfig.data.data, (long)arg->runtimeconfig.data.data_len); - (*cleanup_fn) (arg, user_data); + mono_extract_and_register_properties (arg->runtimeconfig.data.data, (guint64)arg->runtimeconfig.data.data_len); + if (cleanup_fn) + (*cleanup_fn) (arg, user_data); return 0; } default: @@ -275,41 +270,45 @@ monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntim } void -mono_extract_and_register_properties (const char *buffer, long fileSize) +mono_extract_and_register_properties (const char *buffer, guint64 file_size) { - int propertyCount; - int strLen; - int currentIdx = 0; - char *propertyKeys [MAX_PROPERTY_COUNT]; - char *propertyValues [MAX_PROPERTY_COUNT]; - - propertyCount = buffer [0] - '\0'; - currentIdx = 1; - for (int i = 0; i < propertyCount; ++i) + int property_count; + int str_len; + int current_idx = 0; + char **property_keys; + char **property_values; + + property_count = mono_metadata_decode_value(buffer, &buffer); + property_keys = g_new0 (char*, property_count); + property_values = g_new0 (char*, property_count); + current_idx++; + for (int i = 0; i < property_count; ++i) { - g_assert (fileSize > currentIdx); - strLen = buffer [currentIdx] - '\0'; - currentIdx++; - - g_assert (fileSize > (currentIdx + strLen)); - propertyKeys [i] = (char *) malloc (sizeof (char) * (strLen + 1)); - strncpy (propertyKeys [i], buffer + currentIdx, strLen); - propertyKeys [i][strLen] = '\0'; - currentIdx += strLen; - - g_assert (fileSize > currentIdx); - strLen = buffer [currentIdx] - '\0'; - currentIdx++; - - g_assert (fileSize >= (currentIdx + strLen)); - propertyValues [i] = (char *) malloc (sizeof (char) * (strLen + 1)); - strncpy (propertyValues [i], buffer + currentIdx, strLen); - propertyValues [i][strLen] = '\0'; - currentIdx += strLen; + g_assert (file_size > current_idx); + str_len = mono_metadata_decode_value(buffer, &buffer); + current_idx++; + + g_assert (file_size > (current_idx + str_len)); + property_keys [i] = g_new0 (char, str_len + 1); + strncpy (property_keys [i], buffer, str_len); + property_keys [i][str_len] = '\0'; + current_idx += str_len; + buffer += str_len; + + g_assert (file_size > current_idx); + str_len = mono_metadata_decode_value(buffer, &buffer); + current_idx++; + + g_assert (file_size >= (current_idx + str_len)); + property_values [i] = g_new0 (char, str_len + 1); + strncpy (property_values [i], buffer, str_len); + property_values [i][str_len] = '\0'; + current_idx += str_len; + buffer += str_len; } - if (propertyCount > 0) - mono_runtime_register_appctx_properties (propertyCount, (const char **)propertyKeys, (const char **)propertyValues); + if (property_count > 0) + mono_runtime_register_runtimeconfig_json_properties (property_count, (const char**)property_keys, (const char**)property_values); } int From 02d4d942d4c4affdaa8363855e5062cbeaea2a78 Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Thu, 18 Mar 2021 14:48:11 -0400 Subject: [PATCH 03/19] Remove redundant macro --- src/mono/mono/mini/monovm.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mono/mono/mini/monovm.c b/src/mono/mono/mini/monovm.c index 3fa6a1951222e3..8c5d2be7594a92 100644 --- a/src/mono/mono/mini/monovm.c +++ b/src/mono/mono/mini/monovm.c @@ -11,8 +11,6 @@ #include #include -#define MAX_PROPERTY_COUNT 100 - typedef struct { int assembly_count; char **basenames; /* Foo.dll */ From 7b0e20ead9be3a721d6f965d6ab87e7d98038b8d Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Thu, 18 Mar 2021 21:44:14 -0400 Subject: [PATCH 04/19] Move string process to the install function --- src/mono/mono/metadata/appdomain.c | 91 ++++++++++++++-------- src/mono/mono/metadata/domain-internals.h | 3 +- src/mono/mono/mini/mono-private-unstable.h | 2 +- src/mono/mono/mini/monovm.c | 69 +++------------- 4 files changed, 73 insertions(+), 92 deletions(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 9e7a964bd5d9d9..bdb399bc52986d 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -95,9 +95,9 @@ static int n_appctx_props; static char **appctx_keys; static char **appctx_values; -static int n_runtimeconfig_json_props; -static char **runtimeconfig_json_keys; -static char **runtimeconfig_json_values; +static MonovmRuntimeConfigArguments *runtime_config_arg; +static MonovmRuntimeConfigArgumentsCleanup runtime_config_cleanup_fn; +static void *runtime_config_user_data; static const char * mono_check_corlib_version_internal (void); @@ -1243,16 +1243,23 @@ void mono_runtime_register_appctx_properties (int nprops, const char **keys, const char **values) { n_appctx_props = nprops; - appctx_keys = (char**)keys; - appctx_values = (char**)values; + appctx_keys = g_new0 (char*, n_appctx_props); + appctx_values = g_new0 (char*, n_appctx_props); + + for (int i = 0; i < n_appctx_props; ++i) { + appctx_keys [i] = g_new0 (char, strlen (keys [i])); + appctx_values [i] = g_new0 (char, strlen (values [i])); + strcpy(appctx_keys [i], keys [i]); + strcpy(appctx_values [i], values [i]); + } } void -mono_runtime_register_runtimeconfig_json_properties (int nprops, const char **keys, const char **values) +mono_runtime_register_runtimeconfig_json_properties (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void *user_data) { - n_runtimeconfig_json_props = nprops; - runtimeconfig_json_keys = (char**)keys; - runtimeconfig_json_values = (char**)values; + runtime_config_arg = arg; + runtime_config_cleanup_fn = cleanup_fn; + runtime_config_user_data = user_data; } static GENERATE_GET_CLASS_WITH_CACHE (appctx, "System", "AppContext") @@ -1263,11 +1270,11 @@ mono_runtime_install_appctx_properties (void) { ERROR_DECL (error); gpointer args [3]; + int n_runtimeconfig_json_props; int n_total_props; - char **total_keys_raw; - char **total_values_raw; gunichar2 **total_keys; gunichar2 **total_values; + char *buffer; MonoMethod *setup = mono_class_get_method_from_name_checked (mono_class_get_appctx_class (), "Setup", 3, 0, error); g_assert (setup); @@ -1275,26 +1282,41 @@ mono_runtime_install_appctx_properties (void) // FIXME: TRUSTED_PLATFORM_ASSEMBLIES is very large // Combine and convert properties + buffer = runtime_config_arg->runtimeconfig.data.data; + n_runtimeconfig_json_props = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); + n_total_props = n_appctx_props + n_runtimeconfig_json_props; - total_keys_raw = g_new0 (char*, n_total_props); - total_values_raw = g_new0 (char*, n_total_props);; total_keys = g_new0 (gunichar2*, n_total_props); total_values = g_new0 (gunichar2*, n_total_props); + for (int i = 0; i < n_appctx_props; ++i) { - total_keys_raw [i] = g_new0 (char, strlen (appctx_keys [i])); - total_values_raw [i] = (char *) malloc (sizeof (char) * (strlen (appctx_values [i]))); // g_new0 doesn't work well with TRUSTED_PLATFORM_ASSEMBLIES's value - strcpy(total_keys_raw [i], appctx_keys [i]); - strcpy(total_values_raw [i], appctx_values [i]); + total_keys [i] = g_utf8_to_utf16 (appctx_keys [i], strlen (appctx_keys [i]), NULL, NULL, NULL); + total_values [i] = g_utf8_to_utf16 (appctx_values [i], strlen (appctx_values [i]), NULL, NULL, NULL); } + for (int i = 0; i < n_runtimeconfig_json_props; ++i) { - total_keys_raw [i + n_appctx_props] = g_new0 (char, strlen (runtimeconfig_json_keys [i])); - total_values_raw [i + n_appctx_props] = g_new0 (char, strlen (runtimeconfig_json_values [i])); - strcpy(total_keys_raw [i + n_appctx_props], runtimeconfig_json_keys [i]); - strcpy(total_values_raw [i + n_appctx_props], runtimeconfig_json_values [i]); - } - for (int i = 0; i < n_total_props; ++i) { - total_keys [i] = g_utf8_to_utf16 (total_keys_raw [i], strlen (total_keys_raw [i]), NULL, NULL, NULL); - total_values [i] = g_utf8_to_utf16 (total_values_raw [i], strlen (total_values_raw [i]), NULL, NULL, NULL); + int str_len; + char *property_key; + char *property_value; + + str_len = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); + property_key = g_new0 (char, str_len + 1); + strncpy (property_key, buffer, str_len); + property_key [str_len] = '\0'; + buffer += str_len; + + total_keys [i + n_appctx_props] = g_utf8_to_utf16 (property_key, strlen (property_key), NULL, NULL, NULL); + + str_len = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); + property_value = g_new0 (char, str_len + 1); + strncpy (property_value, buffer, str_len); + property_value [str_len] = '\0'; + buffer += str_len; + + total_values [i + n_appctx_props] = g_utf8_to_utf16 (property_value, strlen (property_value), NULL, NULL, NULL); + + g_free (property_key); + g_free (property_value); } /* internal static unsafe void Setup(char** pNames, char** pValues, int count) */ @@ -1305,20 +1327,27 @@ mono_runtime_install_appctx_properties (void) mono_runtime_invoke_checked (setup, NULL, args, error); mono_error_assert_ok (error); + // Call user defined cleanup function + if (runtime_config_cleanup_fn) + (*runtime_config_cleanup_fn) (runtime_config_arg, runtime_config_user_data); + /* No longer needed */ for (int i = 0; i < n_total_props; ++i) { - g_free (total_keys_raw [i]); - g_free (total_values_raw [i]); g_free (total_keys [i]); g_free (total_values [i]); } - g_free (total_keys_raw); - g_free (total_values_raw); g_free (total_keys); g_free (total_values); + for (int i = 0; i < n_appctx_props; ++i) { + g_free (appctx_keys [i]); + g_free (appctx_values [i]); + } + g_free (appctx_keys); + g_free (appctx_values); appctx_keys = NULL; appctx_values = NULL; - runtimeconfig_json_keys = NULL; - runtimeconfig_json_values = NULL; + runtime_config_arg = NULL; + runtime_config_cleanup_fn = NULL; + runtime_config_user_data = NULL; } diff --git a/src/mono/mono/metadata/domain-internals.h b/src/mono/mono/metadata/domain-internals.h index ee7c6b4174ddf0..fb17d9faf27e30 100644 --- a/src/mono/mono/metadata/domain-internals.h +++ b/src/mono/mono/metadata/domain-internals.h @@ -20,6 +20,7 @@ #include #include #include +#include G_BEGIN_DECLS @@ -450,7 +451,7 @@ void mono_runtime_register_appctx_properties (int nprops, const char **keys, const char **values); void -mono_runtime_register_runtimeconfig_json_properties (int nprops, const char **keys, const char **values); +mono_runtime_register_runtimeconfig_json_properties (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void *user_data); void mono_runtime_install_appctx_properties (void); diff --git a/src/mono/mono/mini/mono-private-unstable.h b/src/mono/mono/mini/mono-private-unstable.h index 236816f28632ca..54b3529bedf558 100644 --- a/src/mono/mono/mini/mono-private-unstable.h +++ b/src/mono/mono/mini/mono-private-unstable.h @@ -22,7 +22,7 @@ typedef struct { const char *path; } name; struct { - const char *data; + char *data; uint32_t data_len; } data; } runtimeconfig; diff --git a/src/mono/mono/mini/monovm.c b/src/mono/mono/mini/monovm.c index 8c5d2be7594a92..d1b1bbe88369a2 100644 --- a/src/mono/mono/mini/monovm.c +++ b/src/mono/mono/mini/monovm.c @@ -226,87 +226,38 @@ monovm_initialize (int propertyCount, const char **propertyKeys, const char **pr return 0; } -void mono_extract_and_register_properties (const char *buffer, guint64 file_size); - // Initialize monovm with properties set by runtimeconfig.json. Primarily used by mobile targets. int monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void *user_data) { switch (arg->kind) { case 0: { - guint64 file_size = 0; MonoFileMap *map; void *ret_handle; char *buffer; map = mono_file_map_open (arg->runtimeconfig.name.path); g_assert (map); - file_size = mono_file_map_size (map); - buffer = (char*)mono_file_map (file_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (map), 0, &ret_handle); + arg->runtimeconfig.data.data_len = (uint32_t)mono_file_map_size (map); + g_assert (arg->runtimeconfig.data.data_len > 0); + buffer = (char*)mono_file_map (arg->runtimeconfig.data.data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (map), 0, &ret_handle); g_assert (buffer); - if (file_size <= 0) - return 0; - - mono_extract_and_register_properties (buffer, file_size); + arg->runtimeconfig.data.data = g_new0 (char, arg->runtimeconfig.data.data_len); + memcpy (arg->runtimeconfig.data.data, buffer, sizeof(char) * arg->runtimeconfig.data.data_len); mono_file_unmap (map, ret_handle); mono_file_map_close (map); - if (cleanup_fn) - (*cleanup_fn) (arg, user_data); - return 0; - } - case 1: { - mono_extract_and_register_properties (arg->runtimeconfig.data.data, (guint64)arg->runtimeconfig.data.data_len); - if (cleanup_fn) - (*cleanup_fn) (arg, user_data); - return 0; + break; } + case 1: + break; default: g_assert_not_reached (); } -} -void -mono_extract_and_register_properties (const char *buffer, guint64 file_size) -{ - int property_count; - int str_len; - int current_idx = 0; - char **property_keys; - char **property_values; - - property_count = mono_metadata_decode_value(buffer, &buffer); - property_keys = g_new0 (char*, property_count); - property_values = g_new0 (char*, property_count); - current_idx++; - for (int i = 0; i < property_count; ++i) - { - g_assert (file_size > current_idx); - str_len = mono_metadata_decode_value(buffer, &buffer); - current_idx++; - - g_assert (file_size > (current_idx + str_len)); - property_keys [i] = g_new0 (char, str_len + 1); - strncpy (property_keys [i], buffer, str_len); - property_keys [i][str_len] = '\0'; - current_idx += str_len; - buffer += str_len; - - g_assert (file_size > current_idx); - str_len = mono_metadata_decode_value(buffer, &buffer); - current_idx++; - - g_assert (file_size >= (current_idx + str_len)); - property_values [i] = g_new0 (char, str_len + 1); - strncpy (property_values [i], buffer, str_len); - property_values [i][str_len] = '\0'; - current_idx += str_len; - buffer += str_len; - } - - if (property_count > 0) - mono_runtime_register_runtimeconfig_json_properties (property_count, (const char**)property_keys, (const char**)property_values); + mono_runtime_register_runtimeconfig_json_properties (arg, cleanup_fn, user_data); + return 0; } int From 2f1c7c206f1d8ffb2e07e907336ede0f8237071c Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Fri, 19 Mar 2021 08:26:26 -0400 Subject: [PATCH 05/19] Check if runtime_config_arg exist --- src/mono/mono/metadata/appdomain.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index bdb399bc52986d..d1542bf18b03b8 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1247,8 +1247,8 @@ mono_runtime_register_appctx_properties (int nprops, const char **keys, const c appctx_values = g_new0 (char*, n_appctx_props); for (int i = 0; i < n_appctx_props; ++i) { - appctx_keys [i] = g_new0 (char, strlen (keys [i])); - appctx_values [i] = g_new0 (char, strlen (values [i])); + appctx_keys [i] = g_new0 (char, strlen (keys [i]) + 1); + appctx_values [i] = g_new0 (char, strlen (values [i]) + 1); strcpy(appctx_keys [i], keys [i]); strcpy(appctx_values [i], values [i]); } @@ -1270,7 +1270,7 @@ mono_runtime_install_appctx_properties (void) { ERROR_DECL (error); gpointer args [3]; - int n_runtimeconfig_json_props; + int n_runtimeconfig_json_props = 0; int n_total_props; gunichar2 **total_keys; gunichar2 **total_values; @@ -1282,8 +1282,10 @@ mono_runtime_install_appctx_properties (void) // FIXME: TRUSTED_PLATFORM_ASSEMBLIES is very large // Combine and convert properties - buffer = runtime_config_arg->runtimeconfig.data.data; - n_runtimeconfig_json_props = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); + if (runtime_config_arg){ + buffer = runtime_config_arg->runtimeconfig.data.data; + n_runtimeconfig_json_props = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); + } n_total_props = n_appctx_props + n_runtimeconfig_json_props; total_keys = g_new0 (gunichar2*, n_total_props); @@ -1293,7 +1295,7 @@ mono_runtime_install_appctx_properties (void) total_keys [i] = g_utf8_to_utf16 (appctx_keys [i], strlen (appctx_keys [i]), NULL, NULL, NULL); total_values [i] = g_utf8_to_utf16 (appctx_values [i], strlen (appctx_values [i]), NULL, NULL, NULL); } - + for (int i = 0; i < n_runtimeconfig_json_props; ++i) { int str_len; char *property_key; @@ -1347,7 +1349,9 @@ mono_runtime_install_appctx_properties (void) appctx_keys = NULL; appctx_values = NULL; - runtime_config_arg = NULL; - runtime_config_cleanup_fn = NULL; - runtime_config_user_data = NULL; + if (runtime_config_arg){ + runtime_config_arg = NULL; + runtime_config_cleanup_fn = NULL; + runtime_config_user_data = NULL; + } } From 5325d64b3aac2e2b600ddbacea79bc74fb8854fa Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Sat, 20 Mar 2021 22:34:43 -0400 Subject: [PATCH 06/19] Move file processing to install function --- src/mono/mono/metadata/appdomain.c | 115 +++++++++++++++------ src/mono/mono/mini/mono-private-unstable.h | 2 +- src/mono/mono/mini/monovm.c | 26 ----- 3 files changed, 85 insertions(+), 58 deletions(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index d1542bf18b03b8..1dc09140ae3d4d 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -97,7 +97,7 @@ static char **appctx_values; static MonovmRuntimeConfigArguments *runtime_config_arg; static MonovmRuntimeConfigArgumentsCleanup runtime_config_cleanup_fn; -static void *runtime_config_user_data; +static gpointer runtime_config_user_data; static const char * mono_check_corlib_version_internal (void); @@ -126,6 +126,12 @@ add_assemblies_to_domain (MonoDomain *domain, MonoAssembly *ass, GHashTable *ht) static void add_assembly_to_alc (MonoAssemblyLoadContext *alc, MonoAssembly *ass); +static const char* +runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap **file_map, gpointer *buf_handle); + +static void +runtimeconfig_json_read_props (const char *ptr, const char **endp, int nprops, gunichar2 **dest_keys, gunichar2 **dest_values); + static MonoLoadFunc load_function = NULL; /* Lazy class loading functions */ @@ -1246,7 +1252,7 @@ mono_runtime_register_appctx_properties (int nprops, const char **keys, const c appctx_keys = g_new0 (char*, n_appctx_props); appctx_values = g_new0 (char*, n_appctx_props); - for (int i = 0; i < n_appctx_props; ++i) { + for (int i = 0; i < nprops; ++i) { appctx_keys [i] = g_new0 (char, strlen (keys [i]) + 1); appctx_values [i] = g_new0 (char, strlen (values [i]) + 1); strcpy(appctx_keys [i], keys [i]); @@ -1274,7 +1280,10 @@ mono_runtime_install_appctx_properties (void) int n_total_props; gunichar2 **total_keys; gunichar2 **total_values; - char *buffer; + MonoFileMap *runtimeconfig_json_map = NULL; + gpointer runtimeconfig_json_map_handle = NULL; + const char *buffer_start = runtimeconfig_json_get_buffer (runtime_config_arg, &runtimeconfig_json_map, &runtimeconfig_json_map_handle); + const char *buffer = buffer_start; MonoMethod *setup = mono_class_get_method_from_name_checked (mono_class_get_appctx_class (), "Setup", 3, 0, error); g_assert (setup); @@ -1282,10 +1291,8 @@ mono_runtime_install_appctx_properties (void) // FIXME: TRUSTED_PLATFORM_ASSEMBLIES is very large // Combine and convert properties - if (runtime_config_arg){ - buffer = runtime_config_arg->runtimeconfig.data.data; - n_runtimeconfig_json_props = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); - } + if (buffer) + n_runtimeconfig_json_props = mono_metadata_decode_value(buffer, &buffer); n_total_props = n_appctx_props + n_runtimeconfig_json_props; total_keys = g_new0 (gunichar2*, n_total_props); @@ -1296,30 +1303,7 @@ mono_runtime_install_appctx_properties (void) total_values [i] = g_utf8_to_utf16 (appctx_values [i], strlen (appctx_values [i]), NULL, NULL, NULL); } - for (int i = 0; i < n_runtimeconfig_json_props; ++i) { - int str_len; - char *property_key; - char *property_value; - - str_len = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); - property_key = g_new0 (char, str_len + 1); - strncpy (property_key, buffer, str_len); - property_key [str_len] = '\0'; - buffer += str_len; - - total_keys [i + n_appctx_props] = g_utf8_to_utf16 (property_key, strlen (property_key), NULL, NULL, NULL); - - str_len = mono_metadata_decode_value((const char*)buffer, (const char **)&buffer); - property_value = g_new0 (char, str_len + 1); - strncpy (property_value, buffer, str_len); - property_value [str_len] = '\0'; - buffer += str_len; - - total_values [i + n_appctx_props] = g_utf8_to_utf16 (property_value, strlen (property_value), NULL, NULL, NULL); - - g_free (property_key); - g_free (property_value); - } + runtimeconfig_json_read_props (buffer, &buffer, n_runtimeconfig_json_props, total_keys+n_appctx_props, total_values+n_appctx_props); /* internal static unsafe void Setup(char** pNames, char** pValues, int count) */ args [0] = total_keys; @@ -1329,6 +1313,11 @@ mono_runtime_install_appctx_properties (void) mono_runtime_invoke_checked (setup, NULL, args, error); mono_error_assert_ok (error); + if (runtimeconfig_json_map != NULL) { + mono_file_unmap ((gpointer)buffer_start, runtimeconfig_json_map_handle); + mono_file_map_close (runtimeconfig_json_map); + } + // Call user defined cleanup function if (runtime_config_cleanup_fn) (*runtime_config_cleanup_fn) (runtime_config_arg, runtime_config_user_data); @@ -1355,3 +1344,67 @@ mono_runtime_install_appctx_properties (void) runtime_config_user_data = NULL; } } + +static const char* +runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap **file_map, gpointer *buf_handle) +{ + if (arg != NULL) + { + switch (arg->kind) { + case 0: { + char *buffer = NULL; + guint64 file_len = 0; + + *file_map = mono_file_map_open (arg->runtimeconfig.name.path); + g_assert (*file_map); + file_len = mono_file_map_size (*file_map); + g_assert (file_len > 0); + buffer = (char*)mono_file_map (file_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (*file_map), 0, buf_handle); + g_assert (buffer); + return buffer; + } + case 1: { + *file_map = NULL; + *buf_handle = NULL; + return arg->runtimeconfig.data.data; + } + default: + g_assert_not_reached (); + } + } + + *file_map = NULL; + *buf_handle = NULL; + return NULL; +} + +static void +runtimeconfig_json_read_props (const char *ptr, const char **endp, int nprops, gunichar2 **dest_keys, gunichar2 **dest_values) +{ + for (int i = 0; i < nprops; ++i) { + int str_len; + char *property_key; + char *property_value; + + str_len = mono_metadata_decode_value(ptr, &ptr); + property_key = g_new0 (char, str_len + 1); + strncpy (property_key, ptr, str_len); + property_key [str_len] = '\0'; + ptr += str_len; + + dest_keys [i] = g_utf8_to_utf16 (property_key, strlen (property_key), NULL, NULL, NULL); + + str_len = mono_metadata_decode_value(ptr, &ptr); + property_value = g_new0 (char, str_len + 1); + strncpy (property_value, ptr, str_len); + property_value [str_len] = '\0'; + ptr += str_len; + + dest_values [i] = g_utf8_to_utf16 (property_value, strlen (property_value), NULL, NULL, NULL); + + g_free (property_key); + g_free (property_value); + } + + *endp = ptr; +} diff --git a/src/mono/mono/mini/mono-private-unstable.h b/src/mono/mono/mini/mono-private-unstable.h index 54b3529bedf558..236816f28632ca 100644 --- a/src/mono/mono/mini/mono-private-unstable.h +++ b/src/mono/mono/mini/mono-private-unstable.h @@ -22,7 +22,7 @@ typedef struct { const char *path; } name; struct { - char *data; + const char *data; uint32_t data_len; } data; } runtimeconfig; diff --git a/src/mono/mono/mini/monovm.c b/src/mono/mono/mini/monovm.c index d1b1bbe88369a2..a993480e7202c0 100644 --- a/src/mono/mono/mini/monovm.c +++ b/src/mono/mono/mini/monovm.c @@ -230,32 +230,6 @@ monovm_initialize (int propertyCount, const char **propertyKeys, const char **pr int monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void *user_data) { - switch (arg->kind) { - case 0: { - MonoFileMap *map; - void *ret_handle; - char *buffer; - - map = mono_file_map_open (arg->runtimeconfig.name.path); - g_assert (map); - arg->runtimeconfig.data.data_len = (uint32_t)mono_file_map_size (map); - g_assert (arg->runtimeconfig.data.data_len > 0); - buffer = (char*)mono_file_map (arg->runtimeconfig.data.data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (map), 0, &ret_handle); - g_assert (buffer); - - arg->runtimeconfig.data.data = g_new0 (char, arg->runtimeconfig.data.data_len); - memcpy (arg->runtimeconfig.data.data, buffer, sizeof(char) * arg->runtimeconfig.data.data_len); - - mono_file_unmap (map, ret_handle); - mono_file_map_close (map); - break; - } - case 1: - break; - default: - g_assert_not_reached (); - } - mono_runtime_register_runtimeconfig_json_properties (arg, cleanup_fn, user_data); return 0; } From 6a3695f903fe26fb9a004b140d909e627dff75c9 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:05:44 -0400 Subject: [PATCH 07/19] Update src/mono/mono/metadata/appdomain.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aleksey Kliger (λgeek) --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 1dc09140ae3d4d..aa799ba81f2f75 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1386,7 +1386,7 @@ runtimeconfig_json_read_props (const char *ptr, const char **endp, int nprops, g char *property_key; char *property_value; - str_len = mono_metadata_decode_value(ptr, &ptr); + str_len = mono_metadata_decode_value (ptr, &ptr); property_key = g_new0 (char, str_len + 1); strncpy (property_key, ptr, str_len); property_key [str_len] = '\0'; From aa22f862ade2d40140c2663eba99a498a81bc0dc Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:05:56 -0400 Subject: [PATCH 08/19] Update src/mono/mono/metadata/appdomain.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aleksey Kliger (λgeek) --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index aa799ba81f2f75..697e7aa49a5c57 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1394,7 +1394,7 @@ runtimeconfig_json_read_props (const char *ptr, const char **endp, int nprops, g dest_keys [i] = g_utf8_to_utf16 (property_key, strlen (property_key), NULL, NULL, NULL); - str_len = mono_metadata_decode_value(ptr, &ptr); + str_len = mono_metadata_decode_value (ptr, &ptr); property_value = g_new0 (char, str_len + 1); strncpy (property_value, ptr, str_len); property_value [str_len] = '\0'; From 830daa77b8c947ac10d72aa96c259f07324c4556 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:06:11 -0400 Subject: [PATCH 09/19] Update src/mono/mono/metadata/appdomain.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Aleksey Kliger (λgeek) --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 697e7aa49a5c57..d33719eb283cf5 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1292,7 +1292,7 @@ mono_runtime_install_appctx_properties (void) // Combine and convert properties if (buffer) - n_runtimeconfig_json_props = mono_metadata_decode_value(buffer, &buffer); + n_runtimeconfig_json_props = mono_metadata_decode_value (buffer, &buffer); n_total_props = n_appctx_props + n_runtimeconfig_json_props; total_keys = g_new0 (gunichar2*, n_total_props); From bc30109ac3772b6391e3e1e1ca405e0464715b16 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:09:10 -0400 Subject: [PATCH 10/19] Update src/mono/mono/metadata/appdomain.c Co-authored-by: Ryan Lucia --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index d33719eb283cf5..2a288ec8f8a28c 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1249,7 +1249,7 @@ void mono_runtime_register_appctx_properties (int nprops, const char **keys, const char **values) { n_appctx_props = nprops; - appctx_keys = g_new0 (char*, n_appctx_props); + appctx_keys = g_new0 (char *, n_appctx_props); appctx_values = g_new0 (char*, n_appctx_props); for (int i = 0; i < nprops; ++i) { From 938bb5fd25466fbb3fd30cd561ed8d301c83b072 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:09:21 -0400 Subject: [PATCH 11/19] Update src/mono/mono/metadata/appdomain.c Co-authored-by: Ryan Lucia --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 2a288ec8f8a28c..597eaf4f901435 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1250,7 +1250,7 @@ mono_runtime_register_appctx_properties (int nprops, const char **keys, const c { n_appctx_props = nprops; appctx_keys = g_new0 (char *, n_appctx_props); - appctx_values = g_new0 (char*, n_appctx_props); + appctx_values = g_new0 (char *, n_appctx_props); for (int i = 0; i < nprops; ++i) { appctx_keys [i] = g_new0 (char, strlen (keys [i]) + 1); From 6395fb9b1f907bd8d939a4fb1dcf978065b2772f Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:10:53 -0400 Subject: [PATCH 12/19] Update src/mono/mono/metadata/appdomain.c Co-authored-by: Ryan Lucia --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 597eaf4f901435..4b65d7f886f9b5 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -126,7 +126,7 @@ add_assemblies_to_domain (MonoDomain *domain, MonoAssembly *ass, GHashTable *ht) static void add_assembly_to_alc (MonoAssemblyLoadContext *alc, MonoAssembly *ass); -static const char* +static const char * runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap **file_map, gpointer *buf_handle); static void From f03e478a199d35d8eea819d000191852d6fc1932 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:11:08 -0400 Subject: [PATCH 13/19] Update src/mono/mono/metadata/appdomain.c Co-authored-by: Ryan Lucia --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 4b65d7f886f9b5..60442a40d7b3db 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1338,7 +1338,7 @@ mono_runtime_install_appctx_properties (void) appctx_keys = NULL; appctx_values = NULL; - if (runtime_config_arg){ + if (runtime_config_arg) { runtime_config_arg = NULL; runtime_config_cleanup_fn = NULL; runtime_config_user_data = NULL; From ee15a50099a19a5cd58a97cb4cbd2b4cfea993bc Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:11:25 -0400 Subject: [PATCH 14/19] Update src/mono/mono/metadata/appdomain.c Co-authored-by: Ryan Lucia --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 60442a40d7b3db..531a495cfcdb6b 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1345,7 +1345,7 @@ mono_runtime_install_appctx_properties (void) } } -static const char* +static const char * runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap **file_map, gpointer *buf_handle) { if (arg != NULL) From 45831817ed9dd3e2d5dffdc12f4936929012456a Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:11:40 -0400 Subject: [PATCH 15/19] Update src/mono/mono/metadata/appdomain.c Co-authored-by: Ryan Lucia --- src/mono/mono/metadata/appdomain.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 531a495cfcdb6b..120b14b3f47330 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1348,8 +1348,7 @@ mono_runtime_install_appctx_properties (void) static const char * runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap **file_map, gpointer *buf_handle) { - if (arg != NULL) - { + if (arg != NULL) { switch (arg->kind) { case 0: { char *buffer = NULL; From 83ca707204254db92d2cb20698a68237029be342 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:11:56 -0400 Subject: [PATCH 16/19] Update src/mono/mono/metadata/appdomain.c Co-authored-by: Ryan Lucia --- src/mono/mono/metadata/appdomain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 120b14b3f47330..4adcb98f7c0578 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1358,7 +1358,7 @@ runtimeconfig_json_get_buffer (MonovmRuntimeConfigArguments *arg, MonoFileMap ** g_assert (*file_map); file_len = mono_file_map_size (*file_map); g_assert (file_len > 0); - buffer = (char*)mono_file_map (file_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (*file_map), 0, buf_handle); + buffer = (char *)mono_file_map (file_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (*file_map), 0, buf_handle); g_assert (buffer); return buffer; } From c67a0bd095fb95e7d2a8d4d46eabba630f8060a3 Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Mon, 22 Mar 2021 08:37:14 -0400 Subject: [PATCH 17/19] Remove redundant intermediate copy --- src/mono/mono/metadata/appdomain.c | 55 ++++++++++++------------------ 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 4adcb98f7c0578..2b6a9f09d60481 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1255,8 +1255,8 @@ mono_runtime_register_appctx_properties (int nprops, const char **keys, const c for (int i = 0; i < nprops; ++i) { appctx_keys [i] = g_new0 (char, strlen (keys [i]) + 1); appctx_values [i] = g_new0 (char, strlen (values [i]) + 1); - strcpy(appctx_keys [i], keys [i]); - strcpy(appctx_values [i], values [i]); + appctx_keys [i] = g_strdup (keys [i]); + appctx_values [i] = g_strdup (values [i]); } } @@ -1277,9 +1277,9 @@ mono_runtime_install_appctx_properties (void) ERROR_DECL (error); gpointer args [3]; int n_runtimeconfig_json_props = 0; - int n_total_props; - gunichar2 **total_keys; - gunichar2 **total_values; + int n_combined_props; + gunichar2 **combined_keys; + gunichar2 **combined_values; MonoFileMap *runtimeconfig_json_map = NULL; gpointer runtimeconfig_json_map_handle = NULL; const char *buffer_start = runtimeconfig_json_get_buffer (runtime_config_arg, &runtimeconfig_json_map, &runtimeconfig_json_map_handle); @@ -1294,21 +1294,21 @@ mono_runtime_install_appctx_properties (void) if (buffer) n_runtimeconfig_json_props = mono_metadata_decode_value (buffer, &buffer); - n_total_props = n_appctx_props + n_runtimeconfig_json_props; - total_keys = g_new0 (gunichar2*, n_total_props); - total_values = g_new0 (gunichar2*, n_total_props); + n_combined_props = n_appctx_props + n_runtimeconfig_json_props; + combined_keys = g_new0 (gunichar2 *, n_combined_props); + combined_values = g_new0 (gunichar2 *, n_combined_props); for (int i = 0; i < n_appctx_props; ++i) { - total_keys [i] = g_utf8_to_utf16 (appctx_keys [i], strlen (appctx_keys [i]), NULL, NULL, NULL); - total_values [i] = g_utf8_to_utf16 (appctx_values [i], strlen (appctx_values [i]), NULL, NULL, NULL); + combined_keys [i] = g_utf8_to_utf16 (appctx_keys [i], -1, NULL, NULL, NULL); + combined_values [i] = g_utf8_to_utf16 (appctx_values [i], -1, NULL, NULL, NULL); } - runtimeconfig_json_read_props (buffer, &buffer, n_runtimeconfig_json_props, total_keys+n_appctx_props, total_values+n_appctx_props); + runtimeconfig_json_read_props (buffer, &buffer, n_runtimeconfig_json_props, combined_keys + n_appctx_props, combined_values + n_appctx_props); /* internal static unsafe void Setup(char** pNames, char** pValues, int count) */ - args [0] = total_keys; - args [1] = total_values; - args [2] = &n_total_props; + args [0] = combined_keys; + args [1] = combined_values; + args [2] = &n_combined_props; mono_runtime_invoke_checked (setup, NULL, args, error); mono_error_assert_ok (error); @@ -1323,12 +1323,12 @@ mono_runtime_install_appctx_properties (void) (*runtime_config_cleanup_fn) (runtime_config_arg, runtime_config_user_data); /* No longer needed */ - for (int i = 0; i < n_total_props; ++i) { - g_free (total_keys [i]); - g_free (total_values [i]); + for (int i = 0; i < n_combined_props; ++i) { + g_free (combined_keys [i]); + g_free (combined_values [i]); } - g_free (total_keys); - g_free (total_values); + g_free (combined_keys); + g_free (combined_values); for (int i = 0; i < n_appctx_props; ++i) { g_free (appctx_keys [i]); g_free (appctx_values [i]); @@ -1382,27 +1382,14 @@ runtimeconfig_json_read_props (const char *ptr, const char **endp, int nprops, g { for (int i = 0; i < nprops; ++i) { int str_len; - char *property_key; - char *property_value; str_len = mono_metadata_decode_value (ptr, &ptr); - property_key = g_new0 (char, str_len + 1); - strncpy (property_key, ptr, str_len); - property_key [str_len] = '\0'; + dest_keys [i] = g_utf8_to_utf16 (ptr, str_len, NULL, NULL, NULL); ptr += str_len; - dest_keys [i] = g_utf8_to_utf16 (property_key, strlen (property_key), NULL, NULL, NULL); - str_len = mono_metadata_decode_value (ptr, &ptr); - property_value = g_new0 (char, str_len + 1); - strncpy (property_value, ptr, str_len); - property_value [str_len] = '\0'; + dest_values [i] = g_utf8_to_utf16 (ptr, str_len, NULL, NULL, NULL); ptr += str_len; - - dest_values [i] = g_utf8_to_utf16 (property_value, strlen (property_value), NULL, NULL, NULL); - - g_free (property_key); - g_free (property_value); } *endp = ptr; From fc7a8c602a8344019a9ad559699a34b49a73aca5 Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Mon, 22 Mar 2021 08:43:46 -0400 Subject: [PATCH 18/19] Remove extra memory allocation --- src/mono/mono/metadata/appdomain.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mono/mono/metadata/appdomain.c b/src/mono/mono/metadata/appdomain.c index 2b6a9f09d60481..a8a7c595d72960 100644 --- a/src/mono/mono/metadata/appdomain.c +++ b/src/mono/mono/metadata/appdomain.c @@ -1253,8 +1253,6 @@ mono_runtime_register_appctx_properties (int nprops, const char **keys, const c appctx_values = g_new0 (char *, n_appctx_props); for (int i = 0; i < nprops; ++i) { - appctx_keys [i] = g_new0 (char, strlen (keys [i]) + 1); - appctx_values [i] = g_new0 (char, strlen (values [i]) + 1); appctx_keys [i] = g_strdup (keys [i]); appctx_values [i] = g_strdup (values [i]); } From 8190dbf3b6b99b4369e24df84cc30acb80ffe91c Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 22 Mar 2021 08:55:18 -0400 Subject: [PATCH 19/19] Update src/mono/mono/mini/mono-private-unstable.h Co-authored-by: Ryan Lucia --- src/mono/mono/mini/mono-private-unstable.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/mini/mono-private-unstable.h b/src/mono/mono/mini/mono-private-unstable.h index 236816f28632ca..b8e67248bb3a62 100644 --- a/src/mono/mono/mini/mono-private-unstable.h +++ b/src/mono/mono/mini/mono-private-unstable.h @@ -45,7 +45,7 @@ MONO_API int monovm_initialize (int propertyCount, const char **propertyKeys, const char **propertyValues); MONO_API int -monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void* user_data); +monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void *user_data); //#ifdef HOST_WASM typedef void* (*MonoWasmGetNativeToInterpTramp) (MonoMethod *method, void *extra_arg);