[2020-02] Avoid following invalid pointers in mono_w32process_get_modules on Darwin...
[mono-project.git] / mono / tests / bug-349190.2.cs
blobfb1ecef1089c09b4e6b5f131af42febad893fde7
1 using System;
2 using System.Reflection;
3 using System.Reflection.Emit;
4 using System.Threading;
5 using System.IO;
6 using System.Collections.Generic;
9 public class Driver
11 public static int Main () {
12 if (!TestOneAssembly ())
13 return 1;
14 if (!TestTwoAssemblies ())
15 return 2;
16 return 0;
19 public static bool TestTwoAssemblies ()
21 AssemblyBuilder assembly2 = Thread.GetDomain ().DefineDynamicAssembly (new AssemblyName ("res2"), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
22 ModuleBuilder module2 = assembly2.DefineDynamicModule ("res2.dll");
24 TypeBuilder tb2 = module2.DefineType ("ExternalType", TypeAttributes.Public | TypeAttributes.Abstract);
26 MethodBuilder m_2 = tb2.DefineMethod ("m_2", MethodAttributes.Public | MethodAttributes.Static);
27 Type[] gparams_m_2 = m_2.DefineGenericParameters ("T");
28 m_2.SetReturnType (gparams_m_2[0]);
29 m_2.SetParameters (gparams_m_2[0]);
30 ILGenerator il = m_2.GetILGenerator ();
31 il.Emit (OpCodes.Ldarg_0);
32 il.Emit (OpCodes.Ret);
35 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (new AssemblyName ("res"), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
36 ModuleBuilder module = assembly.DefineDynamicModule ("res.exe");
38 TypeBuilder tb = module.DefineType ("Mono.Rocks.IEnumerable", TypeAttributes.Public | TypeAttributes.Abstract);
40 MethodBuilder mb = tb.DefineMethod ("NaturalSort", MethodAttributes.Public | MethodAttributes.Static);
41 Type[] gparams = mb.DefineGenericParameters ("T");
42 mb.SetReturnType (typeof (IEnumerable<>).MakeGenericType (gparams));
43 mb.SetParameters (typeof (IEnumerable<>).MakeGenericType (gparams));
45 il = mb.GetILGenerator ();
46 il.Emit (OpCodes.Ldftn, m_2);
47 il.Emit (OpCodes.Pop);
49 il.Emit (OpCodes.Ldarg_0);
50 il.Emit (OpCodes.Ret);
52 TypeBuilder driver = module.DefineType ("Driver", TypeAttributes.Public);
53 MethodBuilder main = tb.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static);
54 il = main.GetILGenerator ();
55 il.Emit (OpCodes.Ldnull);
56 il.Emit (OpCodes.Call, mb.MakeGenericMethod (typeof(int)));
57 il.Emit (OpCodes.Pop);
58 il.Emit (OpCodes.Ret);
60 assembly.SetEntryPoint (main);
62 Type t = tb.CreateType ();
63 tb2.CreateType ();
64 driver.CreateType ();
67 assembly2.Save ("res2.dll");
68 assembly.Save ("res.exe");
70 IEnumerable<int> en = new int[] { 1,2,3 };
71 bool res = en == t.GetMethod ("NaturalSort").MakeGenericMethod (typeof (int)).Invoke (null, new object[] { en });
73 Thread.GetDomain ().ExecuteAssembly(Path.GetTempPath () + Path.DirectorySeparatorChar +"res.exe");
74 return res;
79 public static bool TestOneAssembly()
81 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (new AssemblyName ("ALAL"), AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
82 ModuleBuilder module = assembly.DefineDynamicModule ("res1.exe");
84 TypeBuilder tb = module.DefineType ("Mono.Rocks.IEnumerable", TypeAttributes.Public | TypeAttributes.Abstract);
86 MethodBuilder m_2 = tb.DefineMethod ("m_2", MethodAttributes.Private | MethodAttributes.Static);
87 Type[] gparams_m_2 = m_2.DefineGenericParameters ("T");
88 m_2.SetReturnType (gparams_m_2[0]);
89 m_2.SetParameters (gparams_m_2[0]);
91 MethodBuilder mb = tb.DefineMethod ("NaturalSort", MethodAttributes.Public | MethodAttributes.Static);
92 Type[] gparams = mb.DefineGenericParameters ("T");
93 mb.SetReturnType (typeof (IEnumerable<>).MakeGenericType (gparams));
94 mb.SetParameters (typeof (IEnumerable<>).MakeGenericType (gparams));
96 ILGenerator il = mb.GetILGenerator ();
98 il.Emit (OpCodes.Ldftn, m_2);
99 il.Emit (OpCodes.Pop);
101 il.Emit (OpCodes.Ldarg_0);
102 il.Emit (OpCodes.Ret);
104 il = m_2.GetILGenerator ();
105 il.Emit (OpCodes.Ldarg_0);
106 il.Emit (OpCodes.Ret);
108 TypeBuilder driver = module.DefineType ("Driver", TypeAttributes.Public);
109 MethodBuilder main = tb.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static);
110 il = main.GetILGenerator ();
111 il.Emit (OpCodes.Ldnull);
112 il.Emit (OpCodes.Call, mb.MakeGenericMethod (typeof(int)));
113 il.Emit (OpCodes.Pop);
114 il.Emit (OpCodes.Ret);
116 assembly.SetEntryPoint (main);
119 Type t = tb.CreateType ();
120 driver.CreateType ();
122 IEnumerable<int> en = new int[] { 1,2,3 };
123 bool res = en == t.GetMethod ("NaturalSort").MakeGenericMethod (typeof (int)).Invoke (null, new object[] {en });
124 assembly.Save ("res1.exe");
125 res &= en == t.GetMethod ("NaturalSort").MakeGenericMethod (typeof (int)).Invoke (null, new object[] {en });
127 Thread.GetDomain ().ExecuteAssembly(Path.GetTempPath () + Path.DirectorySeparatorChar +"res1.exe");
128 return res;