Bug 28182 - Runtime crash when using a castle proxy (via Moq) to mock a Generic method
Summary: Runtime crash when using a castle proxy (via Moq) to mock a Generic method
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Alexander Kyte
Depends on:
Reported: 2015-03-19 04:47 UTC by Darren
Modified: 2015-09-11 10:23 UTC (History)
7 users (show)

Is this bug a regression?: ---
Last known good build:

Test project that reproduces the issue. Uses NUnit and Moq (5.34 KB, application/x-zip-compressed)
2015-03-25 01:33 UTC, Darren

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:

Description Darren 2015-03-19 04:47:51 UTC
When running on a latest development version of Mono (JIT compiler version 4.1.0 (tarball Wed Mar 18 13:23:29 UTC 2015)) on trusty64 Ubuntu 14.04.2 LTS.  The test below fails reliably with:  .* Assertion at sgen-alloc.c:460, condition `*p == NULL' not met

Note: the Test works under 13.12.1 but I cannot use this version as I require some TPL functionality that is not available in that release.

Test that fails:

namespace MonoTest
    using Moq;
    using NUnit.Framework;

    public class MoqShould
        public void CallAMoqMethodThatHasAGenericsSignature()
            var moqInterface = new Mock<ITestInterface>();
            moqInterface.Setup(i => i.MethodWithGeneric<string>()).Returns("hello");
            var result = moqInterface.Object.MethodWithGeneric<string>();

            Assert.AreEqual("hello", result);

    public interface ITestInterface
        T MethodWithGeneric<T>();


[mono-20150318124607]vagrant@vagrant-ubuntu-trusty-64:/vagrant/MonoTest/MonoTest$ mono /usr/lib/nunit/nunit-console.exe bin/Debug/MonoTest.dll -run  MonoTest.MoqShould.CallAMoqMethodThatHasAGenericsSignature
NUnit-Console version
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.

Runtime Environment -
   OS Version: Unix
  CLR Version: 4.0.30319.17020 ( Mono 4.0 ( 4.1.0 (tarball Wed Mar 18 13:23:29 UTC 2015) ) )

ProcessModel: Default    DomainUsage: Single
Execution Runtime: mono-4.0
Selected test(s): MonoTest.MoqShould.CallAMoqMethodThatHasAGenericsSignature
.* Assertion at sgen-alloc.c:460, condition `*p == NULL' not met


  at <unknown> <0xffffffff>
  at Castle.Proxies.ITestInterfaceProxy.MethodWithGeneric<T> () <0x0015b>
  at MonoTest.MoqShould.CallAMoqMethodThatHasAGenericsSignature () <0x00197>
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <0xffffffff>
  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <0xffffffff>
  at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <0x000dc>
  at System.Reflection.MethodBase.Invoke (object,object[]) <0x0002a>
  at NUnit.Core.Reflect.InvokeMethod (System.Reflection.MethodInfo,object,object[]) <0x00064>
  at NUnit.Core.TestMethod.RunTestMethod () <0x00053>
  at NUnit.Core.TestMethod.RunTestCase (NUnit.Core.TestResult) <0x00027>
  at NUnit.Core.TestMethod.RunTest () <0x0012f>
  at NUnit.Core.NUnitTestMethod.RunTest () <0x0000f>
  at NUnit.Core.TestMethod.RunRepeatedTest () <0x0012f>
  at NUnit.Core.TestMethod.RunTestInContext () <0x0060b>
  at NUnit.Core.TestMethod.Run (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x000af>
  at NUnit.Core.TestSuite.RunAllTests (NUnit.Core.TestResult,NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x002f1>
  at NUnit.Core.TestSuite.RunSuite (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x0046f>
  at NUnit.Core.TestSuite.RunSuiteInContext (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x000cb>
  at NUnit.Core.TestSuite.Run (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x0006f>
  at NUnit.Core.TestFixture.Run (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x00083>
  at NUnit.Core.TestSuite.RunAllTests (NUnit.Core.TestResult,NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x002f1>
  at NUnit.Core.TestSuite.RunSuite (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x0046f>
  at NUnit.Core.TestSuite.RunSuiteInContext (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x000cb>
  at NUnit.Core.TestSuite.Run (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x0006f>
  at NUnit.Core.TestSuite.RunAllTests (NUnit.Core.TestResult,NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x002f1>
  at NUnit.Core.TestSuite.RunSuite (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x0046f>
  at NUnit.Core.TestSuite.RunSuiteInContext (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x000cb>
  at NUnit.Core.TestSuite.Run (NUnit.Core.EventListener,NUnit.Core.ITestFilter) <0x0006f>
  at NUnit.Core.SimpleTestRunner.Run (NUnit.Core.EventListener,NUnit.Core.ITestFilter,bool,NUnit.Core.LoggingThreshold) <0x00128>
  at NUnit.Core.TestRunnerThread.TestRunnerThreadProc () <0x0005e>
  at System.Threading.Thread.StartInternal () <0x00071>
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <0xffffffff>

Native stacktrace:

        mono(mono_handle_native_sigsegv+0xcc) [0x4cfb1c]
        /lib/x86_64-linux-gnu/libpthread.so.0(+0x10340) [0x7f119b1bd340]
        /lib/x86_64-linux-gnu/libc.so.6(gsignal+0x39) [0x7f119ae1dcc9]
        /lib/x86_64-linux-gnu/libc.so.6(abort+0x148) [0x7f119ae210d8]
        mono() [0x65b9a9]
        mono(monoeg_g_logv+0x47) [0x65bbb7]
        mono(monoeg_assertion_message+0x96) [0x65bd06]
        mono() [0x617bef]
        mono(mono_gc_alloc_string+0x52) [0x618102]
        mono(mono_string_new_size+0x4a) [0x5dc53a]
        mono(mono_string_new_utf16+0x1f) [0x5dc6af]
        mono() [0x5dc73a]
        mono(mono_method_to_ir+0x2952a) [0x490e0a]
        mono(mini_method_compile+0x7cf) [0x52f6af]
        mono(mono_jit_compile_method_inner+0x17d) [0x530b1d]
        mono() [0x4564ec]
        mono(mono_jit_compile_method+0x2b) [0x456b0b]
        mono() [0x4d203e]

Debug info from gdb:

Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.

Aborted (core dumped)

Mono Version:
Mono JIT compiler version 4.1.0 (tarball Wed Mar 18 13:23:29 UTC 2015)
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
Comment 1 Darren 2015-03-22 05:06:09 UTC
Let me know if there is anything I can do to diagnose this issue any further.
Comment 2 Alexander Kyte 2015-03-24 17:58:03 UTC
If you could provide
Comment 3 Alexander Kyte 2015-03-24 18:07:53 UTC
> If you could provide

Oop, the enter key is a poor choice for typos. If you could provide a sample project that would be useful, but I'm duplicating now and I don't think it's necessary.
Comment 4 Alexander Kyte 2015-03-24 18:17:42 UTC
I've got it reproduced. I'll look into it.
Comment 5 Darren 2015-03-25 01:32:28 UTC
I'm please you can reproduce.  I have uploaded my test project, but I don't think you need this as I assume you've recreated from the test above.  please let me know if I can do anything to assist.
Comment 6 Darren 2015-03-25 01:33:25 UTC
Created attachment 10489 [details]
Test project that reproduces the issue.  Uses NUnit and Moq
Comment 7 Darren 2015-03-25 01:35:27 UTC
Note:  You'll need to nuget restore to get the dependencies as they're not included within the zip.
Comment 8 Alexander Kyte 2015-03-27 16:20:06 UTC
I have a fix in the PR linked below. We weren't initializing our generic classes correctly, and ended up having the constructor overwrite the next object's data.
Comment 9 Alexander Kyte 2015-03-27 16:20:14 UTC
Comment 10 Neale Ferguson 2015-03-31 12:06:30 UTC
Will this make it into the mono-4.0.0 branch?
Comment 11 Darren 2015-04-02 14:24:57 UTC
The pull request failed, do you think that this fix will be resubmitted as I would love a fix to this issue
Comment 12 flutos 2015-04-02 15:53:53 UTC
Just as some info, I applied this patch and for us I ran all 20000 of our unit tests and integration tests and it got rid of the sgen crashing and everything passed successfully .
Comment 13 Darren 2015-04-12 15:14:06 UTC
As this bug appears to have gone dead, do I need to something to re-raise the issue to get it progressed?
Comment 14 Alexander Kyte 2015-04-12 15:23:03 UTC
Your bug should have been fixed by:


Try to run with master.
Comment 15 Darren 2015-04-12 17:34:05 UTC
I will give it a go, thanks for getting back to me. I had stopped taking the latest version of Master as the build had become very unstable. I will check and close if the issue if it is resolved.
Comment 16 Peter Major 2015-06-03 09:37:50 UTC
Is there no way that a patch can be released for this on the Stable Xamarin update channel?

Not being able to run unit tests on any test project that has a generic and that uses ANY castle derivative on the Stable Xamarin channel is kinda a big thing!

Just to be clear, once a test is encountered that causes this issue, the runner bombs out completely and does not run any further tests.

Furthermore, pretty much every mocking framework that I know of uses castle for proxies.
Comment 17 jeremy 2015-09-11 10:23:35 UTC
I'm getting this exception as wellwhen running some fake scripts in latest beta channel for xamarin studio. Is it released to the wild yet?