Bug 42784 - Reflection-only assembly loads only works if Main is called
Summary: Reflection-only assembly loads only works if Main is called
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler ()
Version: master
Hardware: PC Mac OS
: Normal normal
Target Milestone: WatchOS 2.0
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2016-07-25 14:16 UTC by Rolf Bjarne Kvinge [MSFT]
Modified: 2016-07-26 16:59 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Rolf Bjarne Kvinge [MSFT] 2016-07-25 14:16:43 UTC
Problem:

* Loading assemblies in reflection only mode does not work in watchOS extensions.

Reason:

1) domain->search_path is NULL here: https://github.com/mono/mono/blob/d8d1fe64c14111dab63a99c3670115928dc6226b/mono/metadata/appdomain.c#L1899-L1900 (this is where execution differs from a normal iOS project)
2) domain->search_path is NULL because setup->application_base is NULL here: https://github.com/mono/mono/blob/d8d1fe64c14111dab63a99c3670115928dc6226b/mono/metadata/appdomain.c#L1221
3) setup->application_base is NULL because prepare_thread_to_exec_main isn't executed: https://github.com/mono/mono/blob/d8d1fe64c14111dab63a99c3670115928dc6226b/mono/metadata/object.c#L4720
4) and prepare_thread_to_exec_main isn't executed because this is an extension (library) project, without a Main function.

I suppose Xamarin.iOS needs to call some API to setup domain->search_path/setup->application_base properly, so this is a request to get that API implemented (I couldn't find anything existing that looked like it would work).

This causes mscorlib tests using reflection only assembly loading to fail:

    [FAIL] FieldInfoTest.GetValueOnRefOnlyAssembly : An unexpected exception type was thrown
    [FAIL] FieldInfoTest.NonPublicTests : System.IO.FileNotFoundException : Could not load file or assembly 'mscorlibtests1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies
    [FAIL] FieldInfoTest.SetValueOnRefOnlyAssembly : An unexpected exception type was thrown
    [FAIL] MethodInfoTest.InvokeOnRefOnlyAssembly : System.IO.FileNotFoundException : Could not load file or assembly 'mscorlibtests1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies
Comment 1 Rodrigo Kumpera 2016-07-25 15:23:25 UTC
Hey Zoltan,

Can you work on this one?
Comment 2 Zoltan Varga 2016-07-25 17:48:33 UTC
I can add an --application-base= command line option which can be set using mono_jit_parse_options (). Would that be enough ?
Comment 3 Rolf Bjarne Kvinge [MSFT] 2016-07-25 17:53:46 UTC
I believe that would work (I can test any PRs before merging them to make sure it works as intended).
Comment 4 Zoltan Varga 2016-07-25 21:23:08 UTC
Turns out there is already an embedding function named 
mono_domain_set_config () that does this.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2016-07-26 08:07:51 UTC
Looks like my grep skills need to improve :(
Comment 6 Rolf Bjarne Kvinge [MSFT] 2016-07-26 12:20:59 UTC
PR: https://github.com/xamarin/xamarin-macios/pull/489