Bug 664249

Summary: The runtime crashes when an NRE is raised from native code during variable evaluation.
Product: [Mono] Mono: Runtime Reporter: Levi Bard <taktaktaktaktaktaktaktaktaktak>
Component: debugAssignee: Mono Bugs <mono-bugs>
Status: NEW --- QA Contact: Mono Bugs <mono-bugs>
Severity: Critical    
Priority: P5 - None CC: forgotten_vxPDddArjq, joncham
Version: 2.6.x   
Target Milestone: ---   
Hardware: x86-64   
OS: Windows 7   
Whiteboard:
Found By: --- Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---

Description Levi Bard 2011-01-13 14:01:46 UTC
User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13

The runtime crashes when a null reference exception is raised from native code during variable evaluation. Appears to happen only on windows (I'm using a 32bit embedded mono runtime on windows 7 x86-64).

Spent quite a bit of time trying to debug this - it appears that the exception gets handled normally in the runtime itself, but the crash occurs when transitioning back to native code: i.e., the handler code seems to execute up to restore_context(), but the debugger agent code never executes anything after mono_runtime_invoke.

// C Code
void throw_native_exception () {
	MonoException* exception = mono_exception_from_name_msg (mono_get_corlib (), "System", "NullReferenceException", "");
	mono_raise_exception (exception);
}

// C# Code
[DllImport(blah)]
public static extern void throw_native_exception();

public int throw_managed_exception() {
    string s = null;
    return s.Length;
}

Reproducible: Always

Steps to Reproduce:
Create an embedded project that invokes some managed code.
Run with sdb, break inside the managed code.
In the immediate window, ?throw_managed_exception() will fail gracefully.
In the immediate window, ?throw_native_exception() will crash the runtime.
(This also affects hover-inspection, watch pad, etc., in MD.)
Comment 1 Forgotten User vxPDddArjq 2011-01-13 17:36:36 UTC
Converting SIGSEGVs in native code to NREs was never supported in mono. If it worked, it only worked by accident.
Comment 2 Levi Bard 2011-01-14 10:33:43 UTC
This isn't converting a sigsegv to an NRE - this is explicitly raising a NullReferenceException using mono_raise_exception()
Comment 3 Forgotten User vxPDddArjq 2011-01-14 13:41:45 UTC
Sorry, misread your report. I'm having trouble debugging stuff on windows, so somebody with windows experience needs to track this down.
Comment 4 Forgotten User vxPDddArjq 2011-02-22 11:59:30 UTC
I tried reproducing it with:
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

__declspec(dllexport)
void Foo ()
{
	printf ("HIT!\n");
	void* exception = mono_exception_from_name_msg (mono_get_corlib
(), "System", "NullReferenceException", "");
    mono_raise_exception (exception);
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

and:

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
using System;
using System.Runtime.InteropServices;

public class Tests
{
	// C# Code
	[DllImport("blah")]
	public static extern void Foo ();

	public static void Main () {
		//Foo ();
	}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

and invoking 'Foo' using the sdb api seems to work fine in HEAD.
Comment 5 Forgotten User vxPDddArjq 2011-02-24 18:32:14 UTC
I can reproduce this now. Dunno what changed.
Comment 6 Forgotten User vxPDddArjq 2011-02-25 04:49:58 UTC
I take that back. Recompiling the native library made it work again.
Comment 7 Forgotten User vxPDddArjq 2011-02-28 22:27:14 UTC
I can't repro this using 2.6 either. Could this be a win7/wow64 etc. problem ?
Comment 8 Levi Bard 2011-03-01 09:34:43 UTC
This could definitely be a win7/wow64 problem - as far as I recall, all of our repro cases have been in that environment.