Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for host startup hooks to Mono #47462

Closed
lambdageek opened this issue Jan 26, 2021 · 10 comments · Fixed by #80391
Closed

Add support for host startup hooks to Mono #47462

lambdageek opened this issue Jan 26, 2021 · 10 comments · Fixed by #80391

Comments

@lambdageek
Copy link
Member

lambdageek commented Jan 26, 2021

The dotnet core host supports host startup hooks to inject assemblies to run before the main method.

We should implement support for this in Mono.


However for some use cases, such as mobile workloads, it is not enough to add runtime support - we will also need workload integration to bundle the startup hook assemblies into the mobile apps. At that point it may make sense to use a more static approach - instead of inspecting an environment variable and doing method lookup using reflection, just execute the hooks directly from the mobile embedders before the runtime starts (or pass a less dynamic structure to the runtime to execute the methods on the embedders' behalf).

Also need to investigate if in general we need to give the embedders a way to identify the right time to run the startup hooks (for example after some managed code executes to set up the Android or ios environment).

Additionally if the startup hooks need any special permissions beyond what the app needs, the workloads need to be involved.


In scenarios where MonoVM may be substituted for CoreCLR (ie console apps) we should implement the startup hook support as spec'd.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-Host untriaged New issue has not been triaged by the area owner labels Jan 26, 2021
@ghost
Copy link

ghost commented Jan 26, 2021

Tagging subscribers to this area: @vitek-karas, @agocke
See info in area-owners.md if you want to be subscribed.

Issue Details

The dotnet core host supports host startup hooks to inject assemblies to run before the runtime.

We should implement support for this in Mono.

In addition to the ALC use-cases described in the design document, above, it may also be used for #44806 to configure and set up hot reload

Author: lambdageek
Assignees: -
Labels:

area-Host, untriaged

Milestone: -

@lambdageek
Copy link
Member Author

For reference, CoreCLR calls the startup hooks from

RunStartupHooks();

And the class StartupHookProvider looks like it can be shared between both runtimes.

@lambdageek lambdageek added area-VM-meta-mono and removed area-Host untriaged New issue has not been triaged by the area owner labels Jan 26, 2021
@ghost
Copy link

ghost commented Jan 26, 2021

Tagging subscribers to this area: @CoffeeFlux
See info in area-owners.md if you want to be subscribed.

Issue Details

The dotnet core host supports host startup hooks to inject assemblies to run before the runtime.

We should implement support for this in Mono.

In addition to the ALC use-cases described in the design document, above, it may also be used for #44806 to configure and set up hot reload

Author: lambdageek
Assignees: -
Labels:

area-VM-meta-mono

Milestone: -

@lambdageek lambdageek added this to the 6.0.0 milestone Jan 26, 2021
@lambdageek lambdageek removed this from the 6.0.0 milestone Mar 16, 2021
@lambdageek
Copy link
Member Author

Because of embedders, (who must call mono_jit_init_version, but not necessarily mono_runtime_exec_main) we should actually run the startup hooks near the end of mono_init_internal (after the domain and the base types have been installed), rather than at the beginning of mono_runtime_exec_main.

Also I'm assuming that the startup hooks are primarily about setting up the managed environment. If they need to do Android or iOS API calls, running them near the end of mono_jit_init_version might be too early - the mobile embedders may need to do some initialization from their managed entrypoints before the startup hooks can do their work. In that case we may need to provide some managed callback or event that the embedders can invoke when they're prepared to run the startup hooks.

@vitek-karas
Copy link
Member

In CoreCLR startup hooks only run when coreclr_execute_assembly is used. The other ways to run code from native hosting (coreclr_create_delegate) do not trigger the startup hooks.

@CoffeeFlux CoffeeFlux added this to the 7.0.0 milestone Jun 14, 2021
@SamMonoRT
Copy link
Member

cc @lambdageek - is this something we can get to for 7.0 ?

@lambdageek
Copy link
Member Author

@SamMonoRT We can probably add the runtime support, but:

  1. I'm not aware of an end to end scenario where we have a use case for startup hooks on mobile.
  2. We would also need to do updates to the SDKs to bundle the startup hooks on mobile

It might make sense to add the support for desktop only so that, for example, s390x can use startup hooks to more closedly match coreclr behavior on their platform.

@SamMonoRT
Copy link
Member

Moving to 8.0.0

@SamMonoRT SamMonoRT modified the milestones: 7.0.0, 8.0.0 Aug 9, 2022
@lambdageek
Copy link
Member Author

We may want this early in net8 to support Ctrl-F5 hot reload on mobile

@lambdageek
Copy link
Member Author

lambdageek commented Jan 5, 2023

Looks like the "how to get code on the device" problem is understood by @mcumming and @jonathanpeppers, so the runtime should just do our bits (look for the var, call the StartupHookProvider class)

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 9, 2023
lambdageek added a commit that referenced this issue Jan 18, 2023
…#80391)

Fixes #47462 

**CoreCLR** This also makes some changes to CoreCLR to decouple EventPipe and startup hooks. Presently if startup hooks are disabled, `RuntimeEventSource.Initialize` is never called.  The PR makes the two features independent by moving runtime event source initialization out of the startup hook feature check.

* Implement startup hooks support in Mono

* Keep StartupHookProvider.ProcessStartupHooks under feature flag

* Don't catch/cleanup the exceptions from startup hooks.

* Add an ios simulator startup hook functional test

* Implement Android functional test

* Add WASM functional test

* Make a single managed startup method for CoreCLR

A common configuration for coreclr is event source enabled, startup
hooks disabled, so at least one managed call is inevitable.  Since we
have to call into managed no matter what, let the trimmer determine
what happens once we get there.

This is different from mono where published trimmed apps may have both
startup hooks and event source disabled.  In that case we would rather
avoid a managed call to an empty method early in startup.

* fix build and line damage
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 18, 2023
mdh1418 pushed a commit to mdh1418/runtime that referenced this issue Jan 24, 2023
…dotnet#80391)

Fixes dotnet#47462 

**CoreCLR** This also makes some changes to CoreCLR to decouple EventPipe and startup hooks. Presently if startup hooks are disabled, `RuntimeEventSource.Initialize` is never called.  The PR makes the two features independent by moving runtime event source initialization out of the startup hook feature check.

* Implement startup hooks support in Mono

* Keep StartupHookProvider.ProcessStartupHooks under feature flag

* Don't catch/cleanup the exceptions from startup hooks.

* Add an ios simulator startup hook functional test

* Implement Android functional test

* Add WASM functional test

* Make a single managed startup method for CoreCLR

A common configuration for coreclr is event source enabled, startup
hooks disabled, so at least one managed call is inevitable.  Since we
have to call into managed no matter what, let the trimmer determine
what happens once we get there.

This is different from mono where published trimmed apps may have both
startup hooks and event source disabled.  In that case we would rather
avoid a managed call to an empty method early in startup.

* fix build and line damage
@ghost ghost locked as resolved and limited conversation to collaborators Feb 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants