Forum: IronRuby Regression ? "Invalid IL code in (wrapper dynamic-method)"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Thibaut B. (Guest)
on 2009-02-27 00:59
(Received via mailing list)
Hi again,
I'm using Mono 2.4 to run the binaries at
http://nightlybuilds.cloudapp.net/rss.ashx?project...)
- (not sure if it supposed to work, but it did until
today).

Everything I tried worked fine with DLR.10606.release.zip. Switching to
the
latest DLR.12211.release.zip makes "require 'mscorlib'" throw the
following
error:

mscorlib:0:in `CreateDelegate': Invalid IL code in (wrapper
dynamic-method)
object:CallSite.Target
(Microsoft.Runtime.CompilerServices.Closure,Microsoft.Runtime.CompilerServices.CallSite,IronRuby.Runtime.RubyContext,IronRuby.Builtins.MutableString):
IL_0024: pop

 (System::InvalidProgramException)
from mscorlib:0:in `CreateDelegate'
from mscorlib:0:in `CreateDelegate'
from Microsoft.Scripting.Core:0:in `CreateDelegate'
from Microsoft.Scripting.Core:0:in `Compile'
from Microsoft.Scripting.Core:0:in `Compile'
from Microsoft.Scripting.Core:0:in `SetTarget'

Is it a know issue ? (I'm staying on previous builds for the moment).

-- Thibaut
Ivan P. (Guest)
on 2009-02-27 09:19
(Received via mailing list)
I can confirm this but from git.
---
Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
GSM: +32.486.787.582
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim



On Thu, Feb 26, 2009 at 11:41 PM, Thibaut Barrère
<removed_email_address@domain.invalid
Jim D. (Guest)
on 2009-02-27 09:44
(Received via mailing list)
Can anyone confirm if this is only a Mono problem? Also, what platform
x86 or x64 (doubt it makes a difference, but I want to be certain)? I
can’t repro on .NET.

JD

From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Ivan Porto
Carrero
Sent: Thursday, February 26, 2009 10:42 PM
To: removed_email_address@domain.invalid
Subject: Re: [Ironruby-core] Regression ? "Invalid IL code in (wrapper
dynamic-method)"

I can confirm this but from git.
---
Met vriendelijke groeten - Best regards - Salutations
Ivan Porto C.
GSM: +32.486.787.582
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim


On Thu, Feb 26, 2009 at 11:41 PM, Thibaut Barrère
<removed_email_address@domain.invalid<mailto:removed_email_address@domain.invalid>> 
wrote:
Hi again,

I'm using Mono 2.4 to run the binaries at
http://nightlybuilds.cloudapp.net/rss.ashx?project=dlr
(DLR.xxxxx.release.zip) - (not sure if it supposed to work, but it did
until today).

Everything I tried worked fine with DLR.10606.release.zip. Switching to
the latest DLR.12211.release.zip makes "require 'mscorlib'" throw the
following error:

mscorlib:0:in `CreateDelegate': Invalid IL code in (wrapper
dynamic-method) object:CallSite.Target
(Microsoft.Runtime.CompilerServices.Closure,Microsoft.Runtime.CompilerServices.CallSite,IronRuby.Runtime.RubyContext,IronRuby.Builtins.MutableString):
IL_0024: pop

 (System::InvalidProgramException)
          from mscorlib:0:in `CreateDelegate'
          from mscorlib:0:in `CreateDelegate'
          from Microsoft.Scripting.Core:0:in `CreateDelegate'
          from Microsoft.Scripting.Core:0:in `Compile'
          from Microsoft.Scripting.Core:0:in `Compile'
          from Microsoft.Scripting.Core:0:in `SetTarget'

Is it a know issue ? (I'm staying on previous builds for the moment).

-- Thibaut

_______________________________________________
Ironruby-core mailing list
removed_email_address@domain.invalid<mailto:removed_email_address@domain.invalid>
http://rubyforge.org/mailman/listinfo/ironruby-core
Jb Evain (Guest)
on 2009-02-27 10:20
(Received via mailing list)
Hey Jim,

On 2/27/09, Jim D. <removed_email_address@domain.invalid> wrote:
> Can anyone confirm if this is only a Mono problem? Also, what platform x86
> or x64 (doubt it makes a difference, but I want to be certain)? I can’t
> repro on .NET.

Let say it's a shared problem :)

The issue comes from our verifier, which, well, verifies the whole
method body. It's shocking on a dynamic method which looks like:

ldc.i4.1
brfalse target
ldarg.3
call foo
ret
pop

target:

ldarg.1
call bar
bla bla bla
...

The issue is the pop, in between the ret and the jump target. .net's
JIT will optimize it away, and not even verify it, as it will never be
executed.

But our verifier, well, verifies the whole method body, hence the
InvalidProgramException.

We've had this issue already with some obfuscators injecting plain non
sense, so I guess at some point we'll fix it on our side. It would be
nice if IronRuby could emit nice and verifiable code though.
Ivan P. (Guest)
on 2009-02-27 10:31
(Received via mailing list)
It seems that it's only a mono problem.

I use mono from trunk. and to repro it I built Michaels linux branch
Then I just enter a console and do require 'rubygems'
that did it for me.
Ivan P. (Guest)
on 2009-02-27 10:46
(Received via mailing list)
It isn't relevant to this problem but is there a reference or a good
book
for IL generation?
Or some links?
Jb Evain (Guest)
on 2009-02-27 11:15
(Received via mailing list)
On 2/27/09, Jb Evain <removed_email_address@domain.invalid> wrote:
>  The issue is the pop, in between the ret and the jump target. .net's
>  JIT will optimize it away, and not even verify it, as it will never be
>  executed.

It's definitely a common pattern:

push condition
brfalse target
ldarg 3
// action on the RubyContext
ret
pop

target:
bla bla bla

I guess it shouldn't be too hard for you guys to bisect where the
issues comes from between 10606:12221.
Brad W. (Guest)
on 2009-02-27 11:17
(Received via mailing list)
Serge Liden's book is probably the bible for MSIL 2.0.
http://www.amazon.com/Expert-NET-2-0-IL-Assembler/...

IL generation is the trivial part, once you actually understand IL. :)
Ivan P. (Guest)
on 2009-02-27 13:05
(Received via mailing list)
Great thanks, ordered it :)
Tomas M. (Guest)
on 2009-02-27 19:34
(Received via mailing list)
If this is true we should fix it. I'll investigate.

Tomas
Tomas M. (Guest)
on 2009-02-27 19:56
(Received via mailing list)
Jim, could you push the latest sources to GIT?
Jb, could you try again using the latest source code (after Jim pushes
it) and if it fails send me please full stack trace and complete IL of
the offending method?

Thanks,
Tomas
Jim D. (Guest)
on 2009-02-27 20:23
(Received via mailing list)
I have just pushed the latest.

JD
Jb Evain (Guest)
on 2009-02-27 22:07
(Received via mailing list)
Hey Tomas,

On 2/27/09, Tomas M. <removed_email_address@domain.invalid> wrote:
>  Jb, could you try again using the latest source code (after Jim pushes it) and if it 
fails send me please full stack trace and complete IL of the offending method?

Sure.

So, after building from source the updated git repo, we have a
different error, but similar error.

I'm simply executing mono ir.exe test.rb, where test.rb only contains
a call to `require "mscorlib"`.

The stacktrace looks like this:

(gdb) bt
#0  mono_method_to_ir (cfg=0x8ec0ac0, method=0x8ecbce8,
start_bblock=0x8ec48b4, end_bblock=0x8ec494c, return_var=0x0,
dont_inline=0x8ec9a60, inline_args=0x0, inline_offset=0,
is_virtual_call=0)at method-to-ir.c:4606
#1  0x0806124d in mini_method_compile (method=0x8ecbce8,
opts=64055807, domain=0x25ee0, run_cctors=<value optimized out>,
compile_aot=<value optimized out>, parts=0) at mini.c:3208
#2  0x08062b19 in mono_jit_compile_method (method=0x8ecbce8) at
mini.c:3847
#3  0x0818ef88 in ves_icall_System_Delegate_CreateDelegate_internal
(type=0xc6960, target=0x1f27b0, info=0x2e0a0) at icall.c:5988
#4  0xb6c9ae43 in (wrapper managed-to-native)
System.Delegate:CreateDelegate_internal (param0=0xc6960,
param1=0x1f27b0, param2=0x2e0a0) at xdb.il:7790
#5  0xb6c9ab34 in System.Delegate:CreateDelegate (type=0xc6960,
firstArgument=0x1f27b0, method=0x2e0a0, throwOnBindFailure=true)at
/home/jbevain/sources/mcs/class/corlib/System/Delegate.cs:256
#6  0xb6c9a6e6 in System.Delegate:CreateDelegate (type=0xc6960,
firstArgument=0x1f27b0, method=0x2e0a0) at
/home/jbevain/sources/mcs/class/corlib/System/Delegate.cs:263
#7  0xb6bc152b in System.Reflection.Emit.DynamicMethod:CreateDelegate
(this=0x2e0a0, delegateType=0xc6960, target=0x1f27b0) at
/home/jbevain/sources/mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs:189
#8  0xb6bc14c8 in System.Dynamic.Utils.TypeExtensions:CreateDelegate
(methodInfo=0x2e0a0, delegateType=0xc6960, target=0x1f27b0) at
xdb.il:44617
#9  0xb6bc13d7 in
System.Linq.Expressions.Compiler.LambdaCompiler:CreateDelegate
(this=0x1f17e0) at xdb.il:44564
#10 0xb6bcb61f in
System.Linq.Expressions.Compiler.LambdaCompiler:Compile
(lambda=0x1f0ca0) at xdb.il:34475
#11 0xb6bcb57c in System.Linq.Expressions.Expression`1<object>:Compile
(this=0x1f0ca0)
#12 0xb6b722ba in System.Dynamic.SmallRuleSet`1<object>:MakeTarget
(this=0x1f3c60)
#13 0xb6b72235 in System.Dynamic.SmallRuleSet`1<object>:GetTarget
(this=0x1f3c60)
#14 0xb6b721ea in
System.Runtime.CompilerServices.CallSiteOps:SetTarget<object>(site=1120512,
rule=2047096)
#15 0xb6bc293b in
System.Dynamic.UpdateDelegates:UpdateAndExecute3<object, object,
object, object> (site=0xe9940, arg0=0x5ab80, arg1=0x50cf0,
arg2=0x111528)
#16 0xb6bbe81c in (wrapper static-rgctx-invoke)
System.Dynamic.UpdateDelegates:static_rgctx_invoke_object_CallSite_RubyScope_object_MutableString
(param0=0x111900, param1=0x5ab80, param2=0x50cf0, param3=0x111528)at
xdb.il:43447
#17 0xb6ba3120 in (wrapper dynamic-method) object:IR;#;test.rb;0;
(param0=0xe9ab0, param1=0x111d50, param2=0x25aa0) at xdb.il:44669
#18 0xb6bcb2b8 in Microsoft.Scripting.ScriptCode:InvokeTarget
(this=0x111d68, code=0x4e700, scope=0x111d50) at xdb.il:34374
#19 0xb6bcb279 in Microsoft.Scripting.ScriptCode:Run (this=0x111d68,
scope=0x111d50) at xdb.il:34361
#20 0xb6bcb1f3 in Microsoft.Scripting.ScriptCode:Run (this=0x111d68)
at xdb.il:34331
#21 0xb6beac36 in IronRuby.Runtime.RubyContext:ExecuteProgram
(this=0x25aa0, program=0xe2eb0) at xdb.il:17847
#22 0xb6beab8d in
Microsoft.Scripting.Hosting.ScriptSource:ExecuteProgram (this=0x8b978)
at xdb.il:17816
#23 0xb6beab2c in (wrapper remoting-invoke-with-check)
Microsoft.Scripting.Hosting.ScriptSource:ExecuteProgram (this=0x8b978)
at xdb.il:17807
#24 0xb6beaa4e in
Microsoft.Scripting.Hosting.Shell.CommandLine:RunFile (this=0xe2f00,
source=0x8b978) at xdb.il:17749
#25 0xb6bea389 in IronRuby.Hosting.RubyCommandLine:RunFile
(this=0xe2f00, fileName=0x48fe0) at xdb.il:17406
#26 0xb6bea2d2 in Microsoft.Scripting.Hosting.Shell.CommandLine:Run
(this=0xe2f00) at xdb.il:17363
#27 0xb6bea103 in Microsoft.Scripting.Hosting.Shell.CommandLine:Run
(this=0xe2f00, engine=0x4e880, console=0xe2ed8, options=0x4b320) at
xdb.il:17265
#28 0xb6be5570 in
Microsoft.Scripting.Hosting.Shell.ConsoleHost:RunCommandLine(this=0x4bfc8)
at xdb.il:16737
#29 0xb6be5370 in
Microsoft.Scripting.Hosting.Shell.ConsoleHost:ExecuteInternal(this=0x4bfc8)
at xdb.il:16638
#30 0xb6be520e in
Microsoft.Scripting.Hosting.Shell.ConsoleHost:Execute (this=0x4bfc8)
at xdb.il:16568
#31 0xb780b81b in Microsoft.Scripting.Hosting.Shell.ConsoleHost:Run
(this=0x4bfc8, args=0x29f48) at xdb.il:276
#32 0xb780b2ca in RubyConsoleHost:Main (args=0x29f48) at xdb.il:79
#33 0xb780b203 in (wrapper runtime-invoke)
RubyConsoleHost:runtime_invoke_int_object (param0=<value optimized
out>, param1=-1073746312, param2=0, param3=-1216302424) at xdb.il:53
#34 0x0814551e in mono_runtime_exec_main (method=0x82cdde4,
args=0x29f48, exc=0x0) at object.c:3335
#35 0x08145c3b in mono_runtime_run_main (method=0x82cdde4, argc=1,
argv=0xbffff0d8, exc=0x0) at object.c:3123
#36 0x080b1b0a in mono_main (argc=3, argv=0xbffff0d4) at driver.c:969
#37 0x0805aff1 in main (argc=149723456, argv=0x0) at main.c:34
(gdb)

And most importantly, the code that is trying to be emitted by the
DynamicMethod is as follows:

(gdb) p mono_method_get_header(method)->code
$1 = (const unsigned char *) 0x8ec15e0 "\005\002{\001"
(gdb) p mono_method_get_header(method)->code_size
$2 = 198
(gdb) p mono_disasm_code (0, method, 0x8ec15e0, 0x8ec15e0 + 198)
IL_0000: ldarg.3
IL_0001: ldarg.0
IL_0002: ldfld     <System.Runtime.CompilerServices.Closure:Constants>
IL_0007: ldc.i4.0
IL_0008: ldelem.ref
IL_0009: ceq
IL_000b: brfalse   IL_00a3
IL_0010: ldarg.2
IL_0011: call      <IronRuby.Runtime.RubyOps:GetContextFromScope
(IronRuby.Runtime.RubyScope)>
IL_0016: ldarg.0
IL_0017: ldfld     <System.Runtime.CompilerServices.Closure:Constants>
IL_001c: ldc.i4.1
IL_001d: ldelem.ref
IL_001e: castclass <RubyContext>
IL_0023: ceq
IL_0025: brfalse   IL_00a3
IL_002a: ldarg.s   4
IL_002c: brfalse   IL_00a3
IL_0031: ldarg.s   4
IL_0033: castclass <Object>
IL_0038: callvirt  <object:GetType ()>
IL_003d: ldtoken   <IronRuby.Builtins.MutableString>
IL_0042: call      <System.Type:GetTypeFromHandle
(System.RuntimeTypeHandle)>
IL_0047: ceq
IL_0049: brfalse   IL_00a3
IL_004e: ldarg.0
IL_004f: ldfld     <System.Runtime.CompilerServices.Closure:Constants>
IL_0054: ldc.i4.2
IL_0055: ldelem.ref
IL_0056: castclass <StrongBox`1>
IL_005b: ldfld     <System.Runtime.CompilerServices.StrongBox`1:Value>
IL_0060: ldc.i4    189
IL_0065: ceq
IL_0067: brfalse   IL_009d
IL_006c: ldarg.2
IL_006d: ldarg.3
IL_006e: ldarg.0
IL_006f: ldfld     <System.Runtime.CompilerServices.Closure:Constants>
IL_0074: ldc.i4.3
IL_0075: ldelem.ref
IL_0076: castclass <CallSite`1>
IL_007b: dup
IL_007c: stloc.0
IL_007d: ldfld     <System.Runtime.CompilerServices.CallSite`1:Target>
IL_0082: ldloc.0
IL_0083: ldarg.2
IL_0084: call      <IronRuby.Runtime.RubyOps:GetContextFromScope
(IronRuby.Runtime.RubyScope)>
IL_0089: ldarg.s   4
IL_008b: callvirt
<System.Func`4<System.Runtime.CompilerServices.CallSite,
IronRuby.Runtime.RubyContext, IronRuby.Builtins.MutableString,
IronRuby.Builtins.MutableString>:Invoke
(System.Runtime.CompilerServices.CallSite,IronRuby.Runtime.RubyContext,IronRuby.Builtins.MutableString)>
IL_0090: call      <IronRuby.Builtins.KernelOps:Require
(IronRuby.Runtime.RubyScope,object,IronRuby.Builtins.MutableString)>
IL_0095: tail.
IL_0097: call
<Microsoft.Scripting.Runtime.ScriptingRuntimeHelpers:BooleanToObject
(bool)>
IL_009c: ret
IL_009d: br        IL_00a3
IL_00a2: ret
IL_00a3: ldarg.1
IL_00a4: call
<System.Runtime.CompilerServices.CallSiteOps:NeedsUpdate
(System.Runtime.CompilerServices.CallSite)>
IL_00a9: brfalse   IL_00b0
IL_00ae: ldnull
IL_00af: ret
IL_00b0: ldarg.1
IL_00b1: castclass <CallSite`1>
IL_00b6: callvirt
<System.Runtime.CompilerServices.CallSite`1<System.Func`5<System.Runtime.CompilerServices.CallSite,
IronRuby.Runtime.RubyScope, object, IronRuby.Builtins.MutableString,
object>>:get_Update ()>
IL_00bb: ldarg.1
IL_00bc: ldarg.2
IL_00bd: ldarg.3
IL_00be: ldarg.s   4
IL_00c0: callvirt
<System.Func`5<System.Runtime.CompilerServices.CallSite,
IronRuby.Runtime.RubyScope, object, IronRuby.Builtins.MutableString,
object>:Invoke
(System.Runtime.CompilerServices.CallSite,IronRuby.Runtime.RubyScope,object,IronRuby.Builtins.MutableString)>
IL_00c5: ret

The issue here is the instruction:

IL_00a2: ret

With is right after an unconditional branch, thus will be executed,
but the IL is still wrong.
Tomas M. (Guest)
on 2009-02-28 01:20
(Received via mailing list)
There is a bug in DLR compiler. We are working on a fix.

Thanks for great report,
Tomas
Tomas M. (Guest)
on 2009-03-03 07:51
(Received via mailing list)
This issue is now fixed in the GIT repo.

Tomas
This topic is locked and can not be replied to.