Bug 29108 - GetCallingAssembly is broken for dynamic assemblies
Summary: GetCallingAssembly is broken for dynamic assemblies
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Aleksey Kliger
URL:
Depends on:
Blocks:
 
Reported: 2015-04-15 02:58 UTC by Marek Safar
Modified: 2017-07-13 16:51 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 GitHub or Developer Community 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 Marek Safar 2015-04-15 02:58:51 UTC
using System;
using System.Reflection;
using System.Reflection.Emit;

public class Loader
{
	public static void Main (String[] args)
	{
		const string ASSEMBLY_NAME = "IL_Test";

		AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (
			new AssemblyName (ASSEMBLY_NAME), AssemblyBuilderAccess.RunAndSave);
		ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule (ASSEMBLY_NAME, ASSEMBLY_NAME + ".dll");
		TypeBuilder typeBuilder = moduleBuilder.DefineType ("Program", TypeAttributes.Public);

		MethodBuilder mb1 = typeBuilder.DefineMethod ("TestCalling", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
		var gen1 = mb1.GetILGenerator ();

		gen1.Emit (OpCodes.Call, typeof(Assembly).GetMethod ("GetCallingAssembly"));
		gen1.Emit (OpCodes.Callvirt, typeof (object).GetMethod ("GetType"));
		gen1.Emit (OpCodes.Call, typeof(Console).GetMethod ("WriteLine", new Type[] { typeof(object) }));
		gen1.Emit (OpCodes.Ret);

		MethodBuilder methodBuilder = typeBuilder.DefineMethod ("TestCall", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
		ILGenerator gen = methodBuilder.GetILGenerator ();

		gen.Emit (OpCodes.Call, typeof(Assembly).GetMethod ("GetCallingAssembly"));
		gen.Emit (OpCodes.Call, typeof(Console).GetMethod ("WriteLine", new Type[] { typeof(object) }));

		gen.Emit (OpCodes.Call, mb1);
		gen.Emit (OpCodes.Ret);

		var t = typeBuilder.CreateType ();
		assemblyBuilder.Save (ASSEMBLY_NAME + ".dll");

		var mm = t.GetMethod ("TestCall");
		mm.Invoke (null, null);
	}
}

Output:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Reflection.Emit.AssemblyBuilder

Expected output:
test-app, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
System.Reflection.MonoAssembly
Comment 1 Zoltan Varga 2015-04-17 13:31:58 UTC
"TestCall" is invoked by MethodInfo.Invoke () which is in mscorlib, so this output looks ok.
Comment 2 Marek Safar 2015-04-17 14:02:35 UTC
There are two bugs (causing bugs in code running fine on .net)

First is incorrect caller, it should use the closest caller not the earliest one. 

Second one is incorrect assembly type.
Comment 3 Zoltan Varga 2015-04-17 15:12:47 UTC
According to msdn, GetCallingAssembly () needs to return the caller of the method which called it, and that method is MethodInfo.Invoke (). MS.NET does seem to return the user assembly.
Comment 4 Zoltan Varga 2015-04-17 15:32:48 UTC
A simpler testcase is:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
using System;
using System.Reflection;
using System.Reflection.Emit;

public class Loader
{
	public static void foo () {
		Console.WriteLine ("A: " + Assembly.GetCallingAssembly ());
	}

    public static void Main (String[] args)
    {
		typeof (Loader).GetMethod ("foo").Invoke (null, null);
     }
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Comment 5 Aleksey Kliger 2015-10-01 16:38:45 UTC
Okay, so we skip System.Reflection while walking the stack.
Comment 6 Aleksey Kliger 2015-10-02 13:14:28 UTC
Right, so there are two parts:

1.  MethodInfo.Invoke should not be reported as the caller.  Pretty straightforward  (comment 5).

2. The dynamic assembly's reflected type is System.Reflection.Emit.AssemblyBuilder rather than System.Reflection.MonoAssembly.  
I'm not clear on the expected behavior here: "Program.TestCall" is the caller, and it is in the "IL_Test" assembly, which is an AssemblyBuilder.

My plan: make a new bug for part 1 and fix; leave part 2 as needinfo.
Comment 7 Aleksey Kliger 2015-10-06 10:45:09 UTC
Hm. Update on Part 2: The behavior on .NET is similar to Mono - the assembly type is an internal class of the reflection emit API.
Comment 8 Aleksey Kliger 2015-10-14 11:37:40 UTC
Part 1 fixed in commit 97ad88dadc48474cd7e172192d283181cc29ee8a

Part 2 leaving open. I don't understand the desired behavior.
Comment 9 Ludovic Henry 2017-07-12 23:11:33 UTC
Is that fixed in latest version of mono?
Comment 10 Aleksey Kliger 2017-07-13 14:33:23 UTC
I've done no additional work on this bug since Comment 8 - I've fixed Part 1 (See comment 6 for details) and left Part 2 as-is because I don't think what we do is any different from .NETFramework.
Comment 11 Ludovic Henry 2017-07-13 16:51:38 UTC
Marking as resolved, please reopen if we have more information on how the behaviour exactly differ from .NET.