Bug 7829 - DynamicILInfo.GetTokenFor() throws InvalidCastException for Constructor
Summary: DynamicILInfo.GetTokenFor() throws InvalidCastException for Constructor
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-14 06:12 UTC by Atsushi Eno
Modified: 2013-08-18 23:15 UTC (History)
2 users (show)

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


Attachments
patch (1.91 KB, patch)
2012-10-14 06:12 UTC, Atsushi Eno
Details
revised patch now with test (which is only to expose bug, not really practical) (4.39 KB, patch)
2013-08-15 06:27 UTC, Atsushi Eno
Details


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 Atsushi Eno 2012-10-14 06:12:20 UTC
Created attachment 2741 [details]
patch

I was trying to run Jurassic Javascript compiler under mono and got exception. It seems our Dynamic IL does not support constructor in core level.

Repro:

get Jurassic (sources can be found here): http://jurassic.codeplex.com/ and build it (xbuild should work).

compile the example below with mcs -r:Jurassic.dll :
--------
using System;
using System.IO;
using Jurassic;

public class Test
{
	public static void Main (string [] args)
	{
		var engine = new ScriptEngine ();
		foreach (var file in args)
			engine.Evaluate ("alert('OK')");
	}
}
--------

Actual result:

atsushi@atsushi-Aspire-3820:/svn/jurassic/Jurassic/bin/Debug$ mono -O=-all --debug=casts test.exe

Unhandled Exception:
System.InvalidCastException: Unable to cast object of type 'System.Reflection.MonoCMethod' to type 'System.Reflection.MethodInfo'.
  at System.Reflection.Emit.DynamicILInfo.GetTokenFor (RuntimeMethodHandle method) [0x00000] in /svn/mono/mcs/class/corlib/System.Reflection.Emit/DynamicILInfo.cs:69 
  at Jurassic.Compiler.DynamicILGenerator.GetToken (System.Reflection.MethodBase method) [0x0006b] in /svn/jurassic/Jurassic/Compiler/Emit/ILGenerator/DynamicILGenerator.cs:456 
  at Jurassic.Compiler.DynamicILGenerator.NewObject (System.Reflection.ConstructorInfo constructor) [0x00019] in /svn/jurassic/Jurassic/Compiler/Emit/ILGenerator/DynamicILGenerator.cs:1744 
  at Jurassic.Compiler.Scope.GenerateDeclarations (Jurassic.Compiler.ILGenerator generator, Jurassic.Compiler.OptimizationInfo optimizationInfo) [0x000bc] in /svn/jurassic/Jurassic/Compiler/Scope/Scope.cs:289 
  at Jurassic.Compiler.EvalMethodGenerator.GenerateCode (Jurassic.Compiler.ILGenerator generator, Jurassic.Compiler.OptimizationInfo optimizationInfo) [0x00041] in /svn/jurassic/Jurassic/Compiler/MethodGenerator/EvalMethodGenerator.cs:86 
  at Jurassic.Compiler.MethodGenerator.GenerateCode () [0x000fe] in /svn/jurassic/Jurassic/Compiler/MethodGenerator/MethodGenerator.cs:213 
  at Jurassic.ScriptEngine.Evaluate (Jurassic.ScriptSource source) [0x0007b] in /svn/jurassic/Jurassic/Core/ScriptEngine.cs:570 
  at Jurassic.ScriptEngine.Evaluate (System.String code) [0x00001] in /svn/jurassic/Jurassic/Core/ScriptEngine.cs:527 
  at Test.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Unable to cast object of type 'System.Reflection.MonoCMethod' to type 'System.Reflection.MethodInfo'.
  at System.Reflection.Emit.DynamicILInfo.GetTokenFor (RuntimeMethodHandle method) [0x00000] in /svn/mono/mcs/class/corlib/System.Reflection.Emit/DynamicILInfo.cs:69 
  at Jurassic.Compiler.DynamicILGenerator.GetToken (System.Reflection.MethodBase method) [0x0006b] in /svn/jurassic/Jurassic/Compiler/Emit/ILGenerator/DynamicILGenerator.cs:456 
  at Jurassic.Compiler.DynamicILGenerator.NewObject (System.Reflection.ConstructorInfo constructor) [0x00019] in /svn/jurassic/Jurassic/Compiler/Emit/ILGenerator/DynamicILGenerator.cs:1744 
  at Jurassic.Compiler.Scope.GenerateDeclarations (Jurassic.Compiler.ILGenerator generator, Jurassic.Compiler.OptimizationInfo optimizationInfo) [0x000bc] in /svn/jurassic/Jurassic/Compiler/Scope/Scope.cs:289 
  at Jurassic.Compiler.EvalMethodGenerator.GenerateCode (Jurassic.Compiler.ILGenerator generator, Jurassic.Compiler.OptimizationInfo optimizationInfo) [0x00041] in /svn/jurassic/Jurassic/Compiler/MethodGenerator/EvalMethodGenerator.cs:86 
  at Jurassic.Compiler.MethodGenerator.GenerateCode () [0x000fe] in /svn/jurassic/Jurassic/Compiler/MethodGenerator/MethodGenerator.cs:213 
  at Jurassic.ScriptEngine.Evaluate (Jurassic.ScriptSource source) [0x0007b] in /svn/jurassic/Jurassic/Core/ScriptEngine.cs:570 
  at Jurassic.ScriptEngine.Evaluate (System.String code) [0x00001] in /svn/jurassic/Jurassic/Core/ScriptEngine.cs:527 
  at Test.Main (System.String[] args) [0x00000] in <filename unknown>:0 

Expected result:

not sure but something that is not InvalidCastException. (Jurassic still won't work because our DynamicILInfo.SetLocalSignature() throws NotImplementedException.)

Patch:

The attached patch removes that exception away, but I'm not sure if it is valid (especially on marshaling the unmanaged structure layout).
Comment 1 Atsushi Eno 2013-08-15 06:27:18 UTC
Created attachment 4640 [details]
revised patch now with test (which is only to expose bug, not really practical)

I will apply the patch later unless there is objection.
Comment 2 Zoltan Varga 2013-08-15 13:51:56 UTC
The patch changes the signature of public methods, which doesn't look ok.
Comment 3 Atsushi Eno 2013-08-18 02:38:12 UTC
In which particular class did I change a signature of public method?
Comment 4 Zoltan Varga 2013-08-18 09:21:17 UTC
Like here:

-		public int GetToken (MethodInfo method, Type[] opt_param_types) {
+		public int GetToken (MethodBase method, Type[] opt_param_types) {
Comment 5 Zoltan Varga 2013-08-18 09:22:29 UTC
I see, those are internal classes.
Comment 6 Atsushi Eno 2013-08-18 23:15:16 UTC
Yes they are internals.

I applied the fix in master. [88dadaa]