Bug 59153 - Crash when jitting delegate of un-inflated generic type
Summary: Crash when jitting delegate of un-inflated generic type
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT (show other bugs)
Version: 5.0 (2017-02)
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-11-06 23:01 UTC (History)
6 users (show)

Tags: bugpool-archive
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


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 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