Bug 18688

Summary: Reflection DynamicInvoke broken on device
Product: iOS Reporter: James Clancey <clancey>
Component: GeneralAssignee: Marek Safar <masafa>
Status: VERIFIED FIXED    
Severity: major CC: josh.hogan, kevinknoop, miguel, mohitk, mono-bugs+monotouch, pj.beaman, rene.ruppert, sebastien, sshaw, vargaz
Priority: High    
Version: 7.2.1   
Target Milestone: 7.2.1   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description James Clancey 2014-04-01 01:23:38 UTC
I just updated to the most recent alpha, and now I get a crash using reflection. It works in the sim, but blows up on device.

Here is a repo: https://github.com/praeclarum/Bind/blob/master/samples/Bind.iOS.sln

2014-04-01 00:20:54.905 BindiOSSample[10977:60b] Unhandled managed exception: Exception has been thrown by the target of an invocation. (System.Reflection.TargetInvocationException)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0005c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:238 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MethodBase.cs:114 
  at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000d5] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Delegate.cs:424 
  at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00018] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/MulticastDelegate.cs:70 
  at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Delegate.cs:398 
  at Praeclarum.UI.Layout.Eval (System.Linq.Expressions.Expression expr) [0x00081] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/Layout.cs:255 
  at Praeclarum.UI.Layout.GetViewAndAttribute (System.Linq.Expressions.Expression expr) [0x0020a] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/Layout.cs:232 
  at Praeclarum.UI.Layout.GetRight (System.Linq.Expressions.Expression expr) [0x00194] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/Layout.cs:136 
  at Praeclarum.UI.Layout.CompileConstraint (System.Linq.Expressions.BinaryExpression expr, MonoTouch.UIKit.UIView constrainedView) [0x0007a] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/Layout.cs:76 
  at Praeclarum.UI.Layout+<ConstrainLayout>c__AnonStorey0.<>m__0 (System.Linq.Expressions.BinaryExpression e) [0x00007] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/Layout.cs:52 
  at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[System.Linq.Expressions.BinaryExpression,MonoTouch.UIKit.NSLayoutConstraint].MoveNext () [0x00000] in <filename unknown>:0 
  at System.Linq.Enumerable.ToArray[NSLayoutConstraint] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at Praeclarum.UI.Layout.ConstrainLayout (MonoTouch.UIKit.UIView view, System.Linq.Expressions.Expression`1 constraints) [0x0003a] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/Layout.cs:52 
  at Bind.iOS.PersonViewController.ViewDidLoad () [0x005e0] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/PersonViewController.cs:51 
  at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
  at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00008] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIWindow.g.cs:215 
  at Bind.iOS.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x0003a] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/AppDelegate.cs:23 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38 
  at Bind.iOS.Application.Main (System.String[] args) [0x00008] in /Users/Clancey/Projects/Bind/samples/Bind.iOS/Main.cs:16 



My current setup:

=== Xamarin Studio ===

Version 4.3.4 (build 0)
Installation UUID: 52089b5f-87fd-4a01-8acf-c63eac855683
Runtime:
Mono 3.4.0 ((no/d4511ef)
GTK+ 2.24.23 (Raleigh theme)

Package version: 304000185

=== Apple Developer Tools ===

Xcode 5.1 (5084)
Build 5B130a

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Xamarin.Android ===

Version: 4.12.3 (Business Edition)
Android SDK: /Users/Clancey/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)
4.1   (API level 16)
4.2   (API level 17)
4.3   (API level 18)
4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.iOS ===

Version: 7.2.1.25 (Business Edition)
Hash: 46b2486
Branch: 
Build date: 2014-03-24 15:04:26-0400

=== Build Information ===

Release ID: 403040000
Git revision: e2e118a56ebf05c9f15eafbf53379bdebded727c
Build date: 2014-03-12 15:39:07-04
Xamarin addins: 9f2eb9aed862e28ca23d2d2f34e22952087e6e4c

=== Operating System ===

Mac OS X 10.9.2
Darwin Jamess-MacBook-Pro-3.local 13.1.0 Darwin Kernel Version 13.1.0
    Thu Jan 16 19:40:37 PST 2014
    root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
Comment 1 Sebastien Pouliot 2014-04-01 11:07:00 UTC
Looks like this use System.Linq.Expressions.Expression* which is a new implementation in 7.2.1.

It might be that the linker definitions are not up to date with the new code base, i.e. the new code might be  reflecting different types.

Downgrading priortiy as SLE  (part of the DLR) is not supported [1] (not a blocker) but still high as it used to work.

[1] http://docs.xamarin.com/guides/ios/advanced_topics/limitations/
Comment 2 Sebastien Pouliot 2014-04-01 12:04:19 UTC
			return Expression.Lambda (expr).Compile ().DynamicInvoke ();

^ that fails even if the linker is disabled on device. It also work [1] if the linker is enabled on the simulator.

It seems that the issue is related to the different SLE implementation shipped with 7.2.1. -> Marek


[1] it does fails later when the linker is enabled on the simulator - but that would be a different issue. I'll have a look at this 2nd one while Marek looks at the first one.

Unhandled Exception:
System.ArgumentException: Set Method not found for 'Text'
  at System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) [0x00014] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoProperty.cs:354 
  at System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/PropertyInfo.cs:115 
  at Praeclarum.Bind.Binding.SetValue (System.Linq.Expressions.Expression expr, System.Object value, Int32 changeId, System.Collections.Generic.KeyValuePair`2[] args) [0x00067] in /Users/sebastienpouliot/git/Bind/src/Bind.cs:114 
  at Praeclarum.Bind.EqualityBinding..ctor (System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Collections.Generic.KeyValuePair`2[] args) [0x00052] in /Users/sebastienpouliot/git/Bind/src/Bind.cs:365 
  at Praeclarum.Bind.Binding.BindAny (System.Linq.Expressions.LambdaExpression expr, System.Collections.Generic.KeyValuePair`2[] args) [0x0002a] in /Users/sebastienpouliot/git/Bind/src/Bind.cs:91 
  at Praeclarum.Bind.Binding.Create[Boolean] (System.Linq.Expressions.Expression`1 expr, System.Collections.Generic.KeyValuePair`2[] args) [0x00003] in /Users/sebastienpouliot/git/Bind/src/Bind.cs:74 
  at Bind.iOS.PersonViewController.ViewDidLoad () [0x0065d] in /Users/sebastienpouliot/git/Bind/samples/Bind.iOS/PersonViewController.cs:67 
  at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
  at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00008] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIWindow.g.cs:215 
  at Bind.iOS.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x0003a] in /Users/sebastienpouliot/git/Bind/samples/Bind.iOS/AppDelegate.cs:23 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38 
  at Bind.iOS.Application.Main (System.String[] args) [0x00008] in /Users/sebastienpouliot/git/Bind/samples/Bind.iOS/Main.cs:16
Comment 3 Marek Safar 2014-04-01 14:59:38 UTC
I cannot reproduce it. When running Bind.iOS.Sample on a device I get

System.ArgumentNullException: Argument cannot be null.
Parameter name: uiFontTextStyle
  at MonoTouch.UIKit.UIFont.GetPreferredFontForTextStyle (MonoTouch.Foundation.NSString uiFontTextStyle) [0x00017] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIFont.g.cs:174
  at MonoTouch.UIKit.UIFont.get_PreferredHeadline () [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIFont.cs:32
  at Bind.iOS.PersonViewController.ViewDidLoad () [0x0003f] in /Users/marek/git/Bind/samples/Bind.iOS/PersonViewController.cs:42
  at at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
  at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00008] in /Developer/MonoTouch/Source/monotouch/src/build/compat/UIKit/.pmcs-compat.UIWindow.g.cs:215
  at Bind.iOS.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x0003a] in /Users/marek/git/Bind/samples/Bind.iOS/AppDelegate.cs:23
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38
  at Bind.iOS.Application.Main (System.String[] args) [0x00008] in /Users/marek/git/Bind/samples/Bind.iOS/Main.cs:16
Comment 4 Sebastien Pouliot 2014-04-01 15:05:18 UTC
What's your device iOS version ?

GetPreferredFontForTextStyle is only on 7.0 and the NSString given to it is likely null before 7.0.
Comment 5 Marek Safar 2014-04-01 15:08:51 UTC
Yeah, I have only version 6.1
Comment 7 Marek Safar 2014-04-01 15:52:09 UTC
It looks like AOT cannot handle this case too.

Zoltan, can this be fixed/implemented?

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Attempting to JIT compile method '(wrapper delegate-invoke) <Module>:invoke_callvirt_UIView_UIViewController (MonoTouch.UIKit.UIViewController)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.

  at Microsoft.Scripting.Interpreter.FuncCallInstruction`2[MonoTouch.UIKit.UIViewController,MonoTouch.UIKit.UIView].Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x00000] in <filename unknown>:0
  at Microsoft.Scripting.Interpreter.Interpreter.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x0001b] in /Developer/MonoTouch/Source/mono/mcs/class/dlr/Runtime/Microsoft.Dynamic/Interpreter/Interpreter.cs:126
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0005c] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:238
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MethodBase.cs:114
  at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000d5] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Delegate.cs:424
  at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00018] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/MulticastDelegate.cs:70
  at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/Delegate.cs:398
Comment 8 Zoltan Varga 2014-04-01 16:16:57 UTC
This is probably the same problem as in the other linq bug reports, a delegate is created without a target, and the first argument of the delegate is used as the this argument.
Comment 9 Sebastien Pouliot 2014-04-03 09:09:07 UTC
*** Bug 18776 has been marked as a duplicate of this bug. ***
Comment 10 Sebastien Pouliot 2014-04-06 12:55:35 UTC
*** Bug 18832 has been marked as a duplicate of this bug. ***
Comment 11 Marek Safar 2014-04-09 14:49:00 UTC
Fixed in master and mono 3-4 branch
Comment 12 Marek Safar 2014-04-11 03:46:59 UTC
*** Bug 18939 has been marked as a duplicate of this bug. ***