Bug 19176

Summary: invalid code generation for async+linq code
Product: [Mono] Compilers Reporter: Jon Goldberger [MSFT] <jon.goldberger>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mohitk, mono-bugs+monotouch, mono-bugs+mono, roosmaa, vargaz
Priority: Normal    
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Test Project

Comment 2 Jon Goldberger [MSFT] 2014-04-18 20:44:53 UTC
From case:

I've started iOS development for our app, but the shared project AOT fails
when targeting an actual device (works in simulator) with
generic_sharing_context
assertion<https://github.com/mono/mono/blob/master/mono/mini/method-to-ir.c#L717>(see
complete error below).

This is happening with both stable and alpha channels.

Is there any way to easily attach a debugger (on mac) to the mono runtime
and catch the assertion such that I'd know what C# type is causing it and
possibly fix it? Currently it would be like looking for a needle in a
haystack, commenting out code to finally arrive at the reason why it's not
working.

Build log:

*Compiling to native code*
> /Developer/MonoTouch/usr/bin/mtouch -sdkroot
> "/Applications/Xcode.app/Contents/Developer" --cache
> "/Users/mart/Projects/Toggl/mobile_dot/Ross/obj/iPhone/Debug/mtouch-cache"
> --nomanifest --nosign -dev
> "/Users/mart/Projects/Toggl/mobile_dot/Ross/bin/iPhone/Debug/Ross.app" -r
> "/Users/mart/Projects/Toggl/mobile_dot/Phoebe/bin/iOS/Debug/Phoebe.dll" -r
> "/Users/mart/Projects/Toggl/mobile_dot/Contrib/Cirrious.FluentLayout/Cirrious.FluentLayout/bin/iPhoneSimulator/Debug/Cirrious.FluentLayouts.Touch.dll"
> -r "/Developer/MonoTouch/usr/lib/mono/2.1/System.dll" -r
> "/Developer/MonoTouch/usr/lib/mono/2.1/System.Xml.dll" -r
> "/Developer/MonoTouch/usr/lib/mono/2.1/System.Core.dll" -r
> "/Developer/MonoTouch/usr/lib/mono/2.1/monotouch.dll" -debug -linksdkonly
> -sdk "7.1" -targetver "7.0" --abi=armv7
> "/Users/mart/Projects/Toggl/mobile_dot/Ross/bin/iPhone/Debug/Ross.exe"
> Xamarin.iOS 7.2.1 Business Edition using framework:
> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk
> AOT Compilation exited with code 134, command:
> MONO_PATH=/Users/mart/Projects/Toggl/mobile_dot/Ross/obj/iPhone/Debug/mtouch-cache/Build
> /Developer/MonoTouch/usr/bin/arm-darwin-mono-boehm --debug -O=gsharedvt
> --aot=mtriple=armv7-ios,full,static,asmonly,direct-icalls,soft-debug,dwarfdebug,no-direct-calls,iphone-abi,outfile=/Users/mart/Projects/Toggl/mobile_dot/Ross/obj/iPhone/Debug/mtouch-cache/Phoebe.dll.armv7.s
> "/Users/mart/Projects/Toggl/mobile_dot/Ross/obj/iPhone/Debug/mtouch-cache/Build/Phoebe.dll"
> Mono Ahead of Time compiler - compiling assembly
> /Users/mart/Projects/Toggl/mobile_dot/Ross/obj/iPhone/Debug/mtouch-cache/Build/Phoebe.dll
> * Assertion at ../../../../../mono/mono/mini/method-to-ir.c:717, condition
> `cfg->generic_sharing_context' not met
>
> error MT3001: Could not AOT the assembly
> '/Users/mart/Projects/Toggl/mobile_dot/Ross/obj/iPhone/Debug/mtouch-cache/Build/Phoebe.dll'


I composed a test-case with the DLLs and build.sh to execute the above
command. [Attached to bug report]

The source for the project is up on Github:
https://github.com/toggl/mobile/tree/bb04c7313d788352a12ec9f345662754ef92ed05

-----------------------------------------------------------------

 I narrowed down the problem to 3 lines:
TogglRestClient.cs:148<https://github.com/toggl/mobile/blob/bb04c7313d788352a12ec9f345662754ef92ed05/Phoebe/Net/TogglRestClient.cs#L148>;
, TogglRestClient.cs:154<https://github.com/toggl/mobile/blob/bb04c7313d788352a12ec9f345662754ef92ed05/Phoebe/Net/TogglRestClient.cs#L154>;
and
TogglRestClient.cs:156<https://github.com/toggl/mobile/blob/bb04c7313d788352a12ec9f345662754ef92ed05/Phoebe/Net/TogglRestClient.cs#L156>;.
What fails there is the lambda function passed into select. Apparently the
AOT wasn't able to compile the generic function calls. Though, it is able
to compile (
object model) => DeleteClient ((ClientModel)model) successfully.
Comment 3 Zoltan Varga 2014-04-19 02:24:46 UTC
Phoebe.dll contains the following method:

    .method private hidebysig 
           instance default class [mscorlib]System.Threading.Tasks.Task '<Delete`1>m__3' (!!0 model)  cil managed 

This method is not generic, but it has a generic parameter as a parameter, which is not valid IL. What c# compiler and what version created this file ?
Comment 4 Mart Roosmaa 2014-04-19 03:02:17 UTC
The issue was present for both stable and alpha channels.

Xamarin Studio: 4.3.4 (build 0)
Xamarin.iOS: 7.2.1.34 (Business Edition)
Mono 3.4.0 ((no/d4511ef)

Built with Xamarin Studios, so the default compiler for used for iOS projects.
Comment 5 Zoltan Varga 2014-05-01 14:38:34 UTC
This is an mcs problem. Testcase:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

public class Model {
}

public class Tests {

	Task DeleteClient (Model m) {
		return null;
	}

	public async Task Delete<T> (IEnumerable<T> models)
		where T : Model
	{
		await Task.WhenAll (models.Select ((model) => DeleteClient (model)));
	}

	public static void Main (String[] args) {
	}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

The '<Delete`1>m__0' method generated by mcs has the following IL signature:

   .method private hidebysig 
          instance default class [mscorlib]System.Threading.Tasks.Task '<Delete`1>m__0' (!!0 model)  cil managed

The parameter has a mvar type, but the method is not generic.

This happens with mcs with master.
Comment 6 Marek Safar 2014-05-02 09:33:12 UTC
Fixed in master