Bug 41509

Summary: DLR crashes when trying to convert a object to null.
Product: [Mono] Class Libraries Reporter: matthi.d
Component: System.CoreAssignee: Marek Safar <masafa>
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: 4.2.0 (C6)   
Target Milestone: Untriaged   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

Description matthi.d 2016-06-04 14:42:05 UTC

1. Open Repl
2. Enter:

    public class TestConvert : System.Dynamic.DynamicObject
        public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result)
            result = null;
            return true;

    public class Test : System.Dynamic.DynamicObject
        public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
            result = new TestConvert();
            return true;

        public static void TestMethod()
            dynamic t = new Test();
            string result = t.SomeMethod();

3. Enter:

Test.TestMethod(); // Works on .NET, crashes on mono

Expected: No Exception
System.InvalidCastException: The result type 'null' of the dynamic binding produced by the object with type 'TestConvert' for the binder 'Microsoft.CSharp.RuntimeBinder.CSharpConvertBinder' is not compatible with the result type 'System.String' expected by the call site.
  at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object)
  at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet] (System.Runtime.CompilerServices.CallSite site, System.Dynamic.T0 arg0) <0x406df090 + 0x00520> in <filename unknown>:0
  at Test.TestMethod () <0x406d98e0 + 0x00189> in <filename unknown>:0
  at <InteractiveExpressionClass>.Host (System.Object& $retval) <0x406d98c0 + 0x0000b> in <filename unknown>:0
  at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) <0x405e5ed0 + 0x000e0> in <filename unknown>:0
  at Mono.CSharpShell.Evaluate (System.String input) <0x405e5dc0 + 0x00053> in <filename unknown>:0

Tested with:

matthid@localhost ~ $ mono --version
Mono JIT compiler version 4.2.3 (Stable Sun Apr  3 14:36:23 CEST 2016)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen

I might send a PR for this (if one is welcome). I think the problem is here: https://github.com/mono/mono/blob/master/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/DynamicObject.cs#L505
Comment 1 matthi.d 2016-06-04 14:43:19 UTC
Problem happened here: https://github.com/pythonnet/pythonnet/pull/219
Comment 2 matthi.d 2016-06-04 14:50:58 UTC
By the way. The Reference-Source uses another condition: https://github.com/mono/mono/blob/master/mcs/class/referencesource/System.Core/Microsoft/Scripting/Actions/DynamicObject.cs#L507
Comment 3 Marek Safar 2016-09-02 09:04:34 UTC
Fixed in master and Mono 4.6