Skip to content

Commit

Permalink
Mono runtime dynamic component support. (#53221)
Browse files Browse the repository at this point in the history
  • Loading branch information
lateralusX authored May 26, 2021
1 parent c5fb258 commit e8875e3
Show file tree
Hide file tree
Showing 21 changed files with 196 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,16 @@
<PlatformManifestFileEntry Include="mono-aot-cross.exe" IsNative="true" />
<PlatformManifestFileEntry Include="opt" IsNative="true" />
<!-- Mono components specific files -->
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing-static.dll" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing-static.so" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing-static.dylib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing.dll" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing.so" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing.dylib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing-static.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing-stub-static.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing-static.lib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-diagnostics_tracing-stub-static.lib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload-static.dll" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload-static.so" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload-static.dylib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload.dll" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload.so" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload.dylib" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload-static.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload-stub-static.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-component-hot_reload-static.lib" IsNative="true" />
Expand Down
8 changes: 7 additions & 1 deletion src/mono/mono.proj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<MonoComponentsStatic Condition="'$(TargetsBrowser)' == 'true' and '$(MonoComponentsStatic)' == ''">true</MonoComponentsStatic>
<MonoComponentsStatic Condition="'$(TargetsiOS)' == 'true' and '$(TargetsiOSSimulator)' != 'true' and '$(MonoComponentsStatic)' == ''">true</MonoComponentsStatic>
<MonoComponentsStatic Condition="'$(TargetstvOS)' == 'true' and '$(TargetstvOSSimulator)' != 'true' and '$(MonoComponentsStatic)' == ''">true</MonoComponentsStatic>
<MonoComponentsStatic Condition="'$(TargetsAndroid)' == 'true' and '$(MonoComponentsStatic)' == ''">false</MonoComponentsStatic>
<!-- by default, do dynamic components -->
<!-- TODO: Change to dynamic as default once package/deploy is fixed for all targets -->
<MonoComponentsStatic Condition="'$(MonoComponentsStatic)' == ''">true</MonoComponentsStatic>
Expand Down Expand Up @@ -419,12 +420,17 @@
<_MonoCMakeArgs Include="-DENABLE_MSCORDBI=1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true' or '$(TargetsAndroid)' == 'true'">
<ItemGroup Condition="'$(TargetsiOS)' == 'true' or '$(TargetstvOS)' == 'true'">
<_MonoCMakeArgs Include="-DFEATURE_PERFTRACING_PAL_TCP=1"/>
<_MonoCMakeArgs Include="-DFEATURE_PERFTRACING_DISABLE_DEFAULT_LISTEN_PORT=1"/>
<_MonoCMakeArgs Include="-DDISABLE_LINK_STATIC_COMPONENTS=1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetsAndroid)' == 'true'">
<_MonoCMakeArgs Include="-DFEATURE_PERFTRACING_PAL_TCP=1"/>
<_MonoCMakeArgs Include="-DFEATURE_PERFTRACING_DISABLE_DEFAULT_LISTEN_PORT=1"/>
</ItemGroup>

<!-- Components -->
<ItemGroup Condition="'$(MonoComponentsStatic)' == 'true'">
<_MonoCMakeArgs Include="-DSTATIC_COMPONENTS=1" />
Expand Down
31 changes: 21 additions & 10 deletions src/mono/mono/component/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,27 @@ set(${MONO_DIAGNOSTICS_TRACING_COMPONENT_NAME}-dependencies

# from here down, all the components are treated in the same way

#define a library for each component and component stub
function(define_component_libs)
if (NOT DISABLE_LIBS)
foreach(component IN LISTS components)
add_library("mono-component-${component}-static" STATIC $<TARGET_OBJECTS:${component}-objects>)
install(TARGETS "mono-component-${component}-static" LIBRARY)
endforeach()
foreach(component IN LISTS components)
add_library("mono-component-${component}-stub-static" STATIC $<TARGET_OBJECTS:${component}-stub-objects>)
install(TARGETS "mono-component-${component}-stub-static" LIBRARY)
endforeach()
endif()
endfunction()

# a generic component interface that all components implement
add_library(component_base INTERFACE)
target_sources(component_base INTERFACE
${MONO_COMPONENT_PATH}/component.h
)

if(DISABLE_COMPONENTS OR (NOT STATIC_COMPONENTS AND HOST_WIN32))
if(DISABLE_COMPONENTS OR DISABLE_LIBS)
set(DISABLE_COMPONENT_OBJECTS 1)
endif()

Expand All @@ -90,6 +104,7 @@ if(NOT DISABLE_COMPONENTS AND NOT STATIC_COMPONENTS)
target_compile_definitions("mono-component-${component}" PRIVATE -DCOMPILING_COMPONENT_DYNAMIC;-DMONO_DLL_IMPORT)
else()
add_library("mono-component-${component}" SHARED $<TARGET_OBJECTS:${component}-objects>)
target_compile_definitions("${component}-objects" PRIVATE -DCOMPILING_COMPONENT_DYNAMIC;-DMONO_DLL_IMPORT)
endif()
foreach(dependency IN LISTS "${component}-dependencies")
add_dependencies("mono-component-${component}" "${dependency}")
Expand All @@ -111,17 +126,13 @@ if(NOT DISABLE_COMPONENTS AND NOT STATIC_COMPONENTS)
install(TARGETS "mono-component-${component}" LIBRARY)
endforeach()

#define a library for each component and component stub
define_component_libs()

elseif(NOT DISABLE_COMPONENTS AND STATIC_COMPONENTS)

#define a static library for each component and component stub
foreach(component IN LISTS components)
add_library("mono-component-${component}-static" STATIC $<TARGET_OBJECTS:${component}-objects>)
install(TARGETS "mono-component-${component}-static" LIBRARY)
endforeach()
foreach(component IN LISTS components)
add_library("mono-component-${component}-stub-static" STATIC $<TARGET_OBJECTS:${component}-stub-objects>)
install(TARGETS "mono-component-${component}-stub-static" LIBRARY)
endforeach()
#define a library for each component and component stub
define_component_libs()

# define a list of mono-components objects for mini if building a shared libmono with static-linked components
set(mono-components-objects "")
Expand Down
17 changes: 9 additions & 8 deletions src/mono/mono/component/diagnostics_server-stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ diagnostics_server_stub_pause_for_diagnostics_monitor (void);
static void
diagnostics_server_stub_disable (void);

static MonoComponentDiagnosticsServer *
component_diagnostics_server_stub_init (void);

static MonoComponentDiagnosticsServer fn_table = {
{ MONO_COMPONENT_ITF_VERSION, &diagnostics_server_stub_available },
&diagnostics_server_stub_init,
Expand Down Expand Up @@ -61,17 +64,15 @@ diagnostics_server_stub_disable (void)
{
}

#ifdef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentDiagnosticsServer *
mono_component_diagnostics_server_init (void)
static MonoComponentDiagnosticsServer *
component_diagnostics_server_stub_init (void)
{
return mono_component_diagnostics_server_stub_init ();
return &fn_table;
}
#endif

MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentDiagnosticsServer *
mono_component_diagnostics_server_stub_init (void)
mono_component_diagnostics_server_init (void)
{
return &fn_table;
return component_diagnostics_server_stub_init ();
}
6 changes: 0 additions & 6 deletions src/mono/mono/component/diagnostics_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
#include <mono/utils/mono-compiler.h>
#include <eventpipe/ds-server.h>

#ifndef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentDiagnosticsServer *
mono_component_diagnostics_server_init (void);
#endif

static bool
diagnostics_server_available (void);

Expand Down
2 changes: 0 additions & 2 deletions src/mono/mono/component/diagnostics_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ typedef struct _MonoComponentDiagnosticsServer {
void (*disable) (void);
} MonoComponentDiagnosticsServer;

#ifdef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentDiagnosticsServer *
mono_component_diagnostics_server_init (void);
#endif

#endif /*_MONO_COMPONENT_DIAGNOSTICS_SERVER_H*/
14 changes: 7 additions & 7 deletions src/mono/mono/component/event_pipe-stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ event_pipe_stub_thread_ctrl_activity_id (
static bool
event_pipe_stub_write_event_ee_startup_start (void);

MonoComponentEventPipe *
component_event_pipe_stub_init (void);

static MonoComponentEventPipe fn_table = {
{ MONO_COMPONENT_ITF_VERSION, &event_pipe_stub_available },
Expand Down Expand Up @@ -247,17 +249,15 @@ event_pipe_stub_write_event_ee_startup_start (void)
return true;
}

#ifdef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentEventPipe *
mono_component_event_pipe_init (void)
component_event_pipe_stub_init (void)
{
return mono_component_event_pipe_stub_init ();
return &fn_table;
}
#endif

MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentEventPipe *
mono_component_event_pipe_stub_init (void)
mono_component_event_pipe_init (void)
{
return &fn_table;
return component_event_pipe_stub_init ();
}
6 changes: 0 additions & 6 deletions src/mono/mono/component/event_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,6 @@ event_pipe_thread_ctrl_activity_id (
return result;
}

#ifndef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentEventPipe *
mono_component_event_pipe_init (void);
#endif

MonoComponentEventPipe *
mono_component_event_pipe_init (void)
{
Expand Down
2 changes: 0 additions & 2 deletions src/mono/mono/component/event_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ typedef struct _MonoComponentEventPipe {
event_pipe_component_write_event_ee_startup_start_func write_event_ee_startup_start;
} MonoComponentEventPipe;

#ifdef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentEventPipe *
mono_component_event_pipe_init (void);
#endif

#endif /*_MONO_COMPONENT_EVENT_PIPE_H*/
32 changes: 16 additions & 16 deletions src/mono/mono/component/hot_reload-stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ hot_reload_stub_available (void);
static void
hot_reload_stub_apply_changes (MonoImage *base_image, gconstpointer dmeta, uint32_t dmeta_len, gconstpointer dil, uint32_t dil_len, MonoError *error);

static MonoComponentHotReload *
component_hot_reload_stub_init (void);

static MonoComponentHotReload fn_table = {
{ MONO_COMPONENT_ITF_VERSION, &hot_reload_stub_available },
&hot_reload_stub_apply_changes,
};

#ifdef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentHotReload *
mono_component_hot_reload_init (void)
static bool
hot_reload_stub_available (void)
{
return mono_component_hot_reload_stub_init ();
return false;
}
#endif

MonoComponentHotReload *
mono_component_hot_reload_stub_init (void)
static void
hot_reload_stub_apply_changes (MonoImage *base_image, gconstpointer dmeta, uint32_t dmeta_len, gconstpointer dil, uint32_t dil_len, MonoError *error)
{
return &fn_table;
mono_error_set_not_supported (error, "Hot reload not supported in this runtime.");
}

bool
hot_reload_stub_available (void)
static MonoComponentHotReload *
component_hot_reload_stub_init (void)
{
return false;
return &fn_table;
}

void
hot_reload_stub_apply_changes (MonoImage *base_image, gconstpointer dmeta, uint32_t dmeta_len, gconstpointer dil, uint32_t dil_len, MonoError *error)
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentHotReload *
mono_component_hot_reload_init (void)
{
mono_error_set_not_supported (error, "Hot reload not supported in this runtime.");
return component_hot_reload_stub_init ();
}

20 changes: 7 additions & 13 deletions src/mono/mono/component/hot_reload.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

#include <mono/utils/mono-compiler.h>

#ifndef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentHotReload *
mono_component_hot_reload_init (void);
#endif

static bool
hot_reload_available (void);

Expand All @@ -26,13 +20,6 @@ static MonoComponentHotReload fn_table = {
&hot_reload_apply_changes,
};

MonoComponentHotReload *
mono_component_hot_reload_init (void)
{
/* TODO: implement me */
return &fn_table;
}

static bool
hot_reload_available (void)
{
Expand All @@ -44,3 +31,10 @@ hot_reload_apply_changes (MonoImage *base_image, gconstpointer dmeta, uint32_t d
{
/* TODO: implement me */
}

MonoComponentHotReload *
mono_component_hot_reload_init (void)
{
/* TODO: implement me */
return &fn_table;
}
2 changes: 0 additions & 2 deletions src/mono/mono/component/hot_reload.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ typedef struct _MonoComponentHotReload {
void (*apply_changes) (MonoImage *base_image, gconstpointer dmeta, uint32_t dmeta_len, gconstpointer dil, uint32_t dil_len, MonoError *error);
} MonoComponentHotReload;

#ifdef STATIC_COMPONENTS
MONO_COMPONENT_EXPORT_ENTRYPOINT
MonoComponentHotReload *
mono_component_hot_reload_init (void);
#endif

#endif/*_MONO_COMPONENT_HOT_RELOAD_H*/
2 changes: 1 addition & 1 deletion src/mono/mono/eventpipe/ep-rt-mono.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ ep_rt_mono_init_finish (void)
// Managed init of diagnostics classes, like registration of RuntimeEventSource (if available).
ERROR_DECL (error);

MonoClass *runtime_event_source = mono_class_from_name_checked (mono_defaults.corlib, "System.Diagnostics.Tracing", "RuntimeEventSource", error);
MonoClass *runtime_event_source = mono_class_from_name_checked (mono_get_corlib (), "System.Diagnostics.Tracing", "RuntimeEventSource", error);
if (is_ok (error) && runtime_event_source) {
MonoMethod *init = mono_class_get_method_from_name_checked (runtime_event_source, "Initialize", -1, 0, error);
if (is_ok (error) && init) {
Expand Down
4 changes: 2 additions & 2 deletions src/mono/mono/metadata/class-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ mono_class_get_dim_conflicts (MonoClass *klass);
MONO_COMPONENT_API MonoMethod *
mono_class_get_method_from_name_checked (MonoClass *klass, const char *name, int param_count, int flags, MonoError *error);

gboolean
MONO_COMPONENT_API gboolean
mono_method_has_no_body (MonoMethod *method);

// FIXME Replace all internal callers of mono_method_get_header_checked with
Expand All @@ -1445,7 +1445,7 @@ mono_method_has_no_body (MonoMethod *method);
// And then mark mono_method_get_header_checked as MONO_RT_EXTERNAL_ONLY MONO_API.
//
// Internal callers expected to use ERROR_DECL. External callers are not.
MonoMethodHeader*
MONO_COMPONENT_API MonoMethodHeader*
mono_method_get_header_internal (MonoMethod *method, MonoError *error);

MonoType*
Expand Down
19 changes: 10 additions & 9 deletions src/mono/mono/metadata/components.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ typedef struct _MonoComponentEntry {
MonoDl *lib;
} MonoComponentEntry;

#ifdef STATIC_COMPONENTS
#define COMPONENT_INIT_FUNC(name) (MonoComponentInitFn) mono_component_ ## name ## _init
#else
#define COMPONENT_INIT_FUNC(name) (MonoComponentInitFn) mono_component_ ## name ## _stub_init
#endif

#define HOT_RELOAD_LIBRARY_NAME "hot_reload"
#define HOT_RELOAD_COMPONENT_NAME HOT_RELOAD_LIBRARY_NAME
Expand Down Expand Up @@ -140,16 +136,15 @@ static MonoDl*
try_load (const char* dir, const MonoComponentEntry *component, const char* component_base_lib)
{
MonoDl *lib = NULL;
void *iter = NULL;
char *path = NULL;
while ((path = mono_dl_build_path (dir, component_base_lib, &iter)) && !lib) {
path = g_module_build_path (dir, component_base_lib);
if (path) {
char *error_msg = NULL;
lib = mono_dl_open (path, MONO_DL_EAGER, &error_msg);
lib = mono_dl_open (path, MONO_DL_EAGER | MONO_DL_LOCAL, &error_msg);
if (!lib) {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Component %s not found: %s", component->name, error_msg);
g_free (error_msg);
continue;
}
g_free (error_msg);
}
if (lib)
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Component %s found at %s", component->name, path);
Expand All @@ -160,6 +155,12 @@ try_load (const char* dir, const MonoComponentEntry *component, const char* comp
static MonoComponentInitFn
load_component (const MonoComponentEntry *component, MonoDl **lib_out)
{
// If init method has been static linked not using stub library, use that instead of dynamic component.
if (component->init() && component->init()->available ()) {
*lib_out = NULL;
return component->init;
}

char *component_base_lib = component_library_base_name (component);
MonoComponentInitFn result = NULL;

Expand Down
Loading

0 comments on commit e8875e3

Please sign in to comment.