Bug 2996 - AOT compiler crash, relating to IKVM and MulticastDelegate
Summary: AOT compiler crash, relating to IKVM and MulticastDelegate
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools ()
Version: 5.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-01-20 17:19 UTC by Michael Bayne
Modified: 2012-01-31 11:17 UTC (History)
3 users (show)

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

DLLs &c for reproducing crash. (5.35 MB, application/zip)
2012-01-20 17:19 UTC, Michael Bayne

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 Developer Community or GitHub 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:

Description Michael Bayne 2012-01-20 17:19:04 UTC
Created attachment 1246 [details]
DLLs &c for reproducing crash.

I'm trying to get IKVM working with MonoTouch, and have gotten things working in the simulator, but when I try to do the AOT compilation, the compiler crashes with the following error message:

AOT Compilation exited with code 134, command:
/Developer/MonoTouch/usr/bin/arm-darwin-mono --debug
Mono Ahead of Time compiler - compiling assembly .../IKVM.OpenJDK.Core.dll
* Assertion at ../../../../../mono/mono/metadata/marshal.c:2586,
condition `method && method->klass->parent ==
mono_defaults.multicastdelegate_class && !strcmp (method->name,
"BeginInvoke")' not met

I've done some research into the failure, which I've documented on this mailing list thread:


Based on my analysis so far, I suspect that perhaps IKVM is generating a delegate class with Begin/EndInvoke which does not extend from MulticastDelegate. However, I'm having trouble investigating that possibility because monodis is crashing when I try to list all of the types defined in IKVM.OpenJDK.Core.dll:

########## ikvm.internal.Winsock

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000008
0x0000f459 in dis_stringify_method_signature_full (m=0x902200, method=0x0, methoddef_row=869, container=0x0, fully_qualified=0, with_marshal_info=1) at get.c:913
913	get.c: No such file or directory.
	in get.c

So I'm currently trying to fix that and see if I can make any headway in debugging the problem.

I've attached all of the dlls and a compile.sh script which invokes the MonoTouch compiler and triggers the crash. If you guys have any insights, that would be super helpful.

Comment 1 Michael Bayne 2012-01-20 17:49:01 UTC
Further digging has uncovered that IKVM does do some funny stuff with delegates. It defines an intermediating interface:

.namespace ikvm.internal
  .class interface public auto ansi abstract DelegateInterface`1<(class [mscorlib]System.MulticastDelegate) T>

  } // end of class ikvm.internal.DelegateInterface`1

and then has delegate implementations implement this interface. For example:

.namespace java.lang
  .class private auto ansi sealed beforefieldinit Shutdown$1
        extends java.lang.Object
        implements class ikvm.internal.DelegateInterface`1<class [mscorlib]System.EventHandler>  {

However, I don't know how it is possible for an interface to extend System.MulticastDelegate, which AFAIK is a class, not an interface. I did not think the CLR supported multiple inheritance.

Regardless of what magic is afoot here, it would seem these delegates do not meet the requirement of the AOT compiler, which is that the delegate implementation class in question *directly* extend MulticastDelegate. Is it possible to relax that requirement?

I'm also looking into whether it is possible to get IKVM to not generate this strange delegate code, but I suspect that the machinations it needs to do to mediate between java.lang.Object and System.Object both being at the "root" of the object hierarchy necessitate some funny business.
Comment 2 Michael Bayne 2012-01-20 17:50:11 UTC
Oh, maybe I'm misreading the monodis output (I'm new to all of this, apologies). Perhaps DelegateInterface is *parameterized* on a class that extends MulticastDelegate? If so, this may be a red herring.
Comment 3 Michael Bayne 2012-01-20 18:07:30 UTC
Yeah, ignore my previous comments, I was totally off base. IKVM supports delegates in Java by creating an instance of a Java class and then instantiating a delegate with a pointer to the Java class's method:

        // Method begins at RVA 0x1d7b8
        // Code size 30 (0x1e)
        .maxstack 8
        IL_0000:  call class [mscorlib]System.AppDomain class [mscorlib]System.AppDomain::get_CurrentDomain()
        IL_0005:  newobj instance void class java.lang.Shutdown$1::'.ctor'()
        IL_000a:  dup 
        IL_000b:  ldvirtftn instance void class java.lang.Shutdown$1::Invoke(object, class [mscorlib]System.EventArgs)
        IL_0011:  newobj instance void class [mscorlib]System.EventHandler::'.ctor'(object, native int)
        IL_0016:  callvirt instance void class [mscorlib]System.AppDomain::add_ProcessExit(class [mscorlib]System.EventHandler)
        IL_001b:  leave.s IL_001d

It looks like there shouldn't be any classes in IKVM.OpenJDK.Core.dll that extend MulticastDelegate at all (and indeed, I don't see any). So I have no idea what's causing the mono AOT compiler to freak out. Back to the drawing board.
Comment 4 Zoltan Varga 2012-01-20 19:12:02 UTC
This particular crash has now been fixed, however, I highly doubt that you can make ikvm work with monotouch. It probably does a lot of runtime code generation, among other things.
Comment 5 Michael Bayne 2012-01-20 19:17:01 UTC
I've got the runtime code generation part covered. It doesn't do that for anything essential, and I've extracted all of the nonessential uses so that it doesn't freak out the MonoTouch AOT compiler. I've also excised the AWT stuff from the OpenJDK used by IKVM. I'm pretty confident that it will either be working once this compiler problem is fixed, or very close to working.

Can I get my hands on this fix?
Comment 6 Zoltan Varga 2012-01-20 22:43:19 UTC
Reopening until the fix is in a released monotouch version.
Comment 7 Michael Bayne 2012-01-20 22:53:11 UTC
Is that a subtle way of saying "No, you can't get your hands on this fix."? :)
Comment 8 Zoltan Varga 2012-01-21 00:31:56 UTC
You might be able to get an updated version from xamarin support.
Comment 9 Michael Bayne 2012-01-21 16:06:13 UTC
Awesome! I'll ask them.
Comment 10 Sebastien Pouliot 2012-01-31 11:17:53 UTC
5.2.x beta releases contains this fix.