Bug 59153 - Crash when jitting delegate of un-inflated generic type
Summary: Crash when jitting delegate of un-inflated generic type
Status: IN_PROGRESS
Alias: None
Product: Runtime
Classification: Mono
Component: JIT (show other bugs)
Version: 5.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Vlad Brezae
URL:
Depends on:
Blocks:
 
Reported: 2017-08-31 15:30 UTC by Michael DeRoy
Modified: 2017-10-16 21:44 UTC (History)
6 users (show)

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


Attachments
Test Visual Studio Project (32.58 KB, application/x-zip-compressed)
2017-08-31 15:30 UTC, Michael DeRoy
Details

Description Michael DeRoy 2017-08-31 15:30:22 UTC
Created attachment 24504 [details]
Test Visual Studio Project

Open the attached project with visual studio (contents copied below) and run with .net framework
Calling the delegate with raise an invalid operation exception

Run the built exe with mono

Crash when creating the delegate
 	mono-2.0-bdwgc.dll!issue_debug_notification(const wchar_t * const message) Line 126	C++
 	mono-2.0-bdwgc.dll!__acrt_report_runtime_error(const wchar_t * message) Line 142	C++
 	mono-2.0-bdwgc.dll!abort() Line 61	C++
 	mono-2.0-bdwgc.dll!mono_log_write_logfile(const char * log_domain, GLogLevelFlags level, int hdr, const char * message) Line 138	C
>	mono-2.0-bdwgc.dll!structured_log_adapter(const char * log_domain, GLogLevelFlags log_level, const char * message, void * user_data) Line 441	C
 	mono-2.0-bdwgc.dll!monoeg_g_logv(const char * log_domain, GLogLevelFlags log_level, const char * format, char * args) Line 116	C
 	mono-2.0-bdwgc.dll!monoeg_g_log(const char * log_domain, GLogLevelFlags log_level, const char * format, ...) Line 126	C
 	mono-2.0-bdwgc.dll!compute_class_bitmap(_MonoClass * klass, unsigned __int64 * bitmap, int size, int offset, int * max_set, int static_fields) Line 836	C
 	mono-2.0-bdwgc.dll!mono_class_compute_gc_descriptor(_MonoClass * klass) Line 1062	C
 	mono-2.0-bdwgc.dll!mono_class_create_runtime_vtable(_MonoDomain * domain, _MonoClass * klass, _MonoError * error) Line 1963	C
 	mono-2.0-bdwgc.dll!mono_class_vtable_full(_MonoDomain * domain, _MonoClass * klass, _MonoError * error) Line 1798	C
 	mono-2.0-bdwgc.dll!mono_class_vtable(_MonoDomain * domain, _MonoClass * klass) Line 1763	C
 	mono-2.0-bdwgc.dll!mono_jit_compile_method_inner(_MonoMethod * method, _MonoDomain * target_domain, int opt, _MonoError * error) Line 4344	C
 	mono-2.0-bdwgc.dll!mono_jit_compile_method_with_opt(_MonoMethod * method, unsigned int opt, _MonoError * error) Line 1971	C
 	mono-2.0-bdwgc.dll!mono_jit_compile_method(_MonoMethod * method, _MonoError * error) Line 2015	C
 	mono-2.0-bdwgc.dll!mono_compile_method_checked(_MonoMethod * method, _MonoError * error) Line 674	C
 	mono-2.0-bdwgc.dll!ves_icall_System_Delegate_CreateDelegate_internal(_MonoReflectionType * type, _MonoObject * target, _MonoReflectionMethod * info, unsigned char throwOnBindFailure) Line 6214	C
 	0000000174fc9844()	Unknown


We could remedy by adding a few lines in ves_icall_System_Delegate_CreateDelegate_internal which throws an exception creating a delegate on an uninflated generic type

	if (method->klass->class_kind == MONO_CLASS_GTD)
	{
		mono_set_pending_exception(mono_get_exception_argument("klass", " Cannot bind to the target method because the type is not inflated"));
		return NULL;
	}

However this is different behaviour than .net framework, which allows you to create the delegate but then throws when you call it.


---------------Repro File -----------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericThingBug
{
    class GenericType<T>
    {
        private T a;
        private T b;

        public static void blah()
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var method = typeof(GenericType<>).GetMethod("blah");
            var del = Delegate.CreateDelegate(typeof(Action), null, method, true);
            ((Action)del)();
            Console.WriteLine("Didn't Crash");
        }
    }
}
Comment 1 Ludovic Henry 2017-09-21 19:15:40 UTC
I can reproduce with repro case provided with Mono 5.6.0.67 (2017-08/ad9daeaf581) Mono 5.7.0 (master/2e08526b14c)
Comment 2 Ludovic Henry 2017-10-16 21:44:20 UTC
https://github.com/mono/mono/pull/5785

Note You need to log in before you can comment on or make changes to this bug.