Bug 12762 - Accessing instance variables from async anonymous method, JIT compile exception
Summary: Accessing instance variables from async anonymous method, JIT compile exception
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-06-19 08:51 UTC by kissling
Modified: 2013-07-01 11:08 UTC (History)
3 users (show)

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


Attachments
Test cases (2.69 KB, application/zip)
2013-06-19 08:51 UTC, kissling
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 kissling 2013-06-19 08:51:11 UTC
Created attachment 4156 [details]
Test cases

When accessing an instance variable within an anonymous method marked with the async keyword, a JIT compile exception occurs.

    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        X _x = new X();

        public override bool FinishedLaunching(UIApplication app,
                                               NSDictionary options)
        {
            Test();

            return true;
        }

        public async Task Test()
        {
            await Task.Run(new Func<Task>(async () => _x.ToString()));  // Crashes
        }
    }

Example source with more test cases is attached.

A functional workaround consists of extracting the anonymous method into a named method.

The problem has been observed on an iPhone 5 running iOS 7 Beta 1, on an iPod touch running iOS 6.0.1.

Test 1
Test 2
Test 3
 System.ExecutionEngineException: Attempting to JIT compile method 'AsyncTests3.AppDelegate:<Test>m__0 ()' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.

  at System.Threading.Tasks.TaskActionInvoker+FuncInvoke`1[System.Threading.Tasks.Task].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.Task.InnerInvoke () [0x00028] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:520 
  at System.Threading.Tasks.Task.ThreadStart () [0x00097] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:419 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/TaskAwaiter.cs:57 
  at AsyncTests3.AppDelegate+<Test>c__async0.MoveNext () [0x00167] in /***/AppDelegate.cs:47 
Test 4
Test 5
System.ExecutionEngineException: Attempting to JIT compile method 'AsyncTests3.AppDelegate:<Test>m__2 ()' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.

  at System.Threading.Tasks.TaskActionInvoker+FuncInvoke`1[System.Threading.Tasks.Task].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in <filename unknown>:0 
  at System.Threading.Tasks.Task.InnerInvoke () [0x00028] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:520 
  at System.Threading.Tasks.Task.ThreadStart () [0x00097] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:419 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/TaskAwaiter.cs:57 
  at AsyncTests3.AppDelegate+<Test>c__async0.MoveNext () [0x002b1] in /***/AppDelegate.cs:68 





Interestingly, when run on the iOS Simulator 7.0 which seems to be capable of JIT-compilation in similar cases (see https://bugzilla.xamarin.com/show_bug.cgi?id=12746), different exceptions are thrown:

Test 1
Test 2
Test 3
System.InvalidProgramException: Invalid IL code in AsyncTests3.AppDelegate:<Test>m__0 (): IL_0003: stfld     0x04000010


  at System.Threading.Tasks.TaskActionInvoker+FuncInvoke`1[System.Threading.Tasks.Task].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/TaskActionInvoker.cs:241 
  at System.Threading.Tasks.Task.InnerInvoke () [0x00028] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:520 
  at System.Threading.Tasks.Task.ThreadStart () [0x00097] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:419 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00021] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/TaskAwaiter.cs:57 
  at AsyncTests3.AppDelegate+<Test>c__async0.MoveNext () [0x00167] in /***/AppDelegate.cs:47 
Test 4
Test 5
System.InvalidProgramException: Invalid IL code in AsyncTests3.AppDelegate:<Test>m__2 (): IL_0003: stfld     0x04000013


  at System.Threading.Tasks.TaskActionInvoker+FuncInvoke`1[System.Threading.Tasks.Task].Invoke (System.Threading.Tasks.Task owner, System.Object state, System.Threading.Tasks.Task context) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/TaskActionInvoker.cs:241 
  at System.Threading.Tasks.Task.InnerInvoke () [0x00028] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:520 
  at System.Threading.Tasks.Task.ThreadStart () [0x00097] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading.Tasks/Task.cs:419 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:62 
  at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00021] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/TaskAwaiter.cs:57 
  at AsyncTests3.AppDelegate+<Test>c__async0.MoveNext () [0x002b1] in /***/AppDelegate.cs:68 




The version numbers where the issue has been observed are as follows:

Xamarin Studio
Version 4.1.4 (build 117)
Installation UUID: 938751df-f7a5-457d-b87b-379763e0ada1
Runtime:
	Mono 3.0.12 ((no/d6c5db8)
	GTK 2.24.18
	GTK# (2.12.0.0)
	Package version: 300120000

Apple Developer Tools
Xcode 5.0 (3322.38)
Build 5A11314m

Xamarin.iOS
Version: 6.3.6.77 (Business Edition)
Hash: fa63be7
Branch: 
Build date: 2013-17-06 20:54:24-0400

Xamarin.Android
Version: 4.7.8 (Starter Edition)
Android SDK: /Users/***/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0   (API level 14)
		4.0.3 (API level 15)
Java SDK: /usr
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06-451-11M4406)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01-451, mixed mode)

Xamarin.Mac
Xamarin.Mac: Not Installed

Build Information
Release ID: 401040117
Git revision: e0f38912731e43ed9d3f1a410dd4fd907e7f1ba0
Build date: 2013-06-11 19:29:58+0000
Xamarin addins: 8fd84026361c1969794b85bee48efab97a5f33b3

Operating System
Mac OS X 10.8.4
Darwin ***s-MacBook-Pro.local 12.4.0 Darwin Kernel Version 12.4.0
    Wed May  1 17:57:12 PDT 2013
    root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
Comment 2 Marek Safar 2013-07-01 11:08:52 UTC
Fixed in master