Skip to content

Commit

Permalink
[mono] Add monovm_initialize_preparsed for Android (dotnet#52304)
Browse files Browse the repository at this point in the history
* [mono] Add monovm_initialize_preparsed for Android

Naming on the new structs TBD

* Fixes
  • Loading branch information
CoffeeFlux authored May 10, 2021
1 parent be38ff2 commit 9ff1a10
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/mono/mono/metadata/mono-private-unstable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef __MONO_METADATA_MONO_PRIVATE_UNSTABLE_H__
#define __MONO_METADATA_MONO_PRIVATE_UNSTABLE_H__

#include <mono/utils/mono-forward.h>
#include <mono/utils/mono-publib.h>

typedef MonoGCHandle MonoAssemblyLoadContextGCHandle;
Expand All @@ -38,4 +39,6 @@ mono_register_bundled_satellite_assemblies (const MonoBundledSatelliteAssembly *
MONO_API MonoBundledSatelliteAssembly *
mono_create_new_bundled_satellite_assembly (const char *name, const char *culture, const unsigned char *data, unsigned int size);

typedef void * (*PInvokeOverrideFn) (const char *libraryName, const char *entrypointName);

#endif /*__MONO_METADATA_MONO_PRIVATE_UNSTABLE_H__*/
3 changes: 1 addition & 2 deletions src/mono/mono/metadata/native-library.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <glib.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/image.h>
#include <mono/metadata/mono-private-unstable.h>
#include <mono/metadata/object-forward.h>
#include <mono/utils/mono-forward.h>
#include <mono/utils/mono-error.h>
Expand Down Expand Up @@ -44,8 +45,6 @@ typedef struct MonoQCallFunc {
const char *method_name;
} MonoQCallFunc;

typedef void * (*PInvokeOverrideFn) (const char *libraryName, const char *entrypointName);

void
mono_loader_install_pinvoke_override (PInvokeOverrideFn override_fn);

Expand Down
24 changes: 24 additions & 0 deletions src/mono/mono/mini/mono-private-unstable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <mono/utils/mono-publib.h>
#include <mono/metadata/image.h>
#include <mono/metadata/mono-private-unstable.h>

typedef struct {
uint32_t kind; // 0 = Path of runtimeconfig.blob, 1 = pointer to image data, >= 2 undefined
Expand All @@ -30,6 +31,25 @@ typedef struct {

typedef void (*MonovmRuntimeConfigArgumentsCleanup) (MonovmRuntimeConfigArguments *args, void *user_data);

typedef struct {
uint32_t assembly_count;
char **basenames; /* Foo.dll */
uint32_t *basename_lens;
char **assembly_filepaths; /* /blah/blah/blah/Foo.dll */
} MonoCoreTrustedPlatformAssemblies;

typedef struct {
uint32_t dir_count;
char **dirs;
} MonoCoreLookupPaths;

typedef struct {
MonoCoreTrustedPlatformAssemblies *trusted_platform_assemblies;
MonoCoreLookupPaths *app_paths;
MonoCoreLookupPaths *native_dll_search_directories;
PInvokeOverrideFn pinvoke_override;
} MonoCoreRuntimeProperties;

/* 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
Expand All @@ -47,6 +67,10 @@ monovm_initialize (int propertyCount, const char **propertyKeys, const char **pr
MONO_API int
monovm_runtimeconfig_initialize (MonovmRuntimeConfigArguments *arg, MonovmRuntimeConfigArgumentsCleanup cleanup_fn, void *user_data);

// The wrapper MonoCoreRuntimeProperties struct can be stack-allocated or freed, but the structs inside it _must_ be heap-allocated and never freed, as they are not copied to avoid extra allocations
MONO_API int
monovm_initialize_preparsed (MonoCoreRuntimeProperties *parsed_properties, int propertyCount, const char **propertyKeys, const char **propertyValues);

//#ifdef HOST_WASM
typedef void* (*MonoWasmGetNativeToInterpTramp) (MonoMethod *method, void *extra_arg);

Expand Down
50 changes: 30 additions & 20 deletions src/mono/mono/mini/monovm.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@
#include <mono/mini/mini.h>
#include <mono/utils/mono-logger-internals.h>

typedef struct {
int assembly_count;
char **basenames; /* Foo.dll */
int *basename_lens;
char **assembly_filepaths; /* /blah/blah/blah/Foo.dll */
} MonoCoreTrustedPlatformAssemblies;

typedef struct {
int dir_count;
char **dirs;
} MonoCoreLookupPaths;

static MonoCoreTrustedPlatformAssemblies *trusted_platform_assemblies;
static MonoCoreLookupPaths *native_lib_paths;
static MonoCoreLookupPaths *app_paths;
Expand Down Expand Up @@ -68,7 +56,7 @@ parse_trusted_platform_assemblies (const char *assemblies_paths)
a->assembly_count = asm_count;
a->assembly_filepaths = parts;
a->basenames = g_new0 (char*, asm_count + 1);
a->basename_lens = g_new0 (int, asm_count + 1);
a->basename_lens = g_new0 (uint32_t, asm_count + 1);
for (int i = 0; i < asm_count; ++i) {
a->basenames [i] = g_path_get_basename (a->assembly_filepaths [i]);
a->basename_lens [i] = strlen (a->basenames [i]);
Expand Down Expand Up @@ -192,15 +180,11 @@ parse_properties (int propertyCount, const char **propertyKeys, const char **pro
return TRUE;
}

int
monovm_initialize (int propertyCount, const char **propertyKeys, const char **propertyValues)
static void
finish_initialization (void)
{
mono_runtime_register_appctx_properties (propertyCount, propertyKeys, propertyValues);

if (!parse_properties (propertyCount, propertyKeys, propertyValues))
return 0x80004005; /* E_FAIL */

install_assembly_loader_hooks ();

if (native_lib_paths != NULL)
mono_set_pinvoke_search_directories (native_lib_paths->dir_count, g_strdupv (native_lib_paths->dirs));
// Our load hooks don't distinguish between normal, AOT'd, and satellite lookups the way CoreCLR's does.
Expand All @@ -213,6 +197,32 @@ monovm_initialize (int propertyCount, const char **propertyKeys, const char **pr
* the requested version and culture.
*/
mono_loader_set_strict_assembly_name_check (TRUE);
}

int
monovm_initialize (int propertyCount, const char **propertyKeys, const char **propertyValues)
{
mono_runtime_register_appctx_properties (propertyCount, propertyKeys, propertyValues);

if (!parse_properties (propertyCount, propertyKeys, propertyValues))
return 0x80004005; /* E_FAIL */

finish_initialization ();

return 0;
}

int
monovm_initialize_preparsed (MonoCoreRuntimeProperties *parsed_properties, int propertyCount, const char **propertyKeys, const char **propertyValues)
{
mono_runtime_register_appctx_properties (propertyCount, propertyKeys, propertyValues);

trusted_platform_assemblies = parsed_properties->trusted_platform_assemblies;
app_paths = parsed_properties->app_paths;
native_lib_paths = parsed_properties->native_dll_search_directories;
mono_loader_install_pinvoke_override (parsed_properties->pinvoke_override);

finish_initialization ();

return 0;
}
Expand Down

0 comments on commit 9ff1a10

Please sign in to comment.