[loader] Init MonoClass:sizes.element_size lazily (Fixes #43563) (#5559)
[mono.git] / mono / tests / assemblyresolve_event6.cs
1 using System;
2 using System.IO;
3 using System.Threading;
4 using System.Reflection;
5
6 public class App
7 {
8         public static int Main ()
9         {
10                 /* Regression test for #58950: When the
11                  * ReflectionOnlyAssemblyResolve event handler throws an
12                  * exception, mono would unwind native code in the loader,
13                  * which left stale coop handles on the coop handle stack.
14                  * Then, the domain unload, asserted in
15                  * mono_handle_stack_free_domain (). */
16                 var d = AppDomain.CreateDomain ("TestDomain");
17                 var o = d.CreateInstanceAndUnwrap (typeof (App).Assembly.FullName, "App/Work") as Work;
18                 var r = o.DoSomething ();
19                 if (r != 0)
20                         return r;
21                 AppDomain.Unload (d);
22                 return 0;
23         }
24
25         public class MyExn : Exception {
26                 public MyExn () : base ("MyReflectionResolveEventHandler threw") {}
27         }
28
29         public class Work : MarshalByRefObject {
30                 public Work () { }
31
32                 public int DoSomething ()
33                 {
34                         AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler (MyReflectionResolveEventHandler);
35                         bool caught = false;
36                         try {
37                                 Assembly a = Assembly.ReflectionOnlyLoadFrom ("assemblyresolve_asm.dll");
38                                 var t = a.GetType ("Asm2");
39                                 var m = t.GetMethod ("M"); // This triggers a load of TestBase
40                                 Console.Error.WriteLine ("got '{0}'", m);
41                         } catch (FileNotFoundException e) {
42                                 Console.WriteLine ("caught FNFE {0}", e);
43                                 caught = true;
44                         } catch (MyExn ) {
45                                 Console.Error.WriteLine ("caught MyExn, should've been a FNFE");
46                                 return 2;
47                         }
48                         if (!caught) {
49                                 Console.Error.WriteLine ("expected to catch a FNFE");
50                                 return 3;
51                         }
52                         return 0;
53                 }
54
55                 static Assembly MyReflectionResolveEventHandler (object sender, ResolveEventArgs args) {
56                         Console.Error.WriteLine ($"Load event for: {args.Name}");
57                         if (args.Name == "Test, Version=0.0.0.0, Culture=neutral" || args.Name == "Test, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
58                                 return Assembly.ReflectionOnlyLoadFrom (Path.Combine (Directory.GetCurrentDirectory (), "assemblyresolve_deps", "Test.dll"));
59                         // a request to load TestBase will throw here, which
60                         // should be caught in the runtime
61                         throw new MyExn ();
62                 }
63         }
64 }