[Mono-bugs] [Bug 74338][Nor] New - Exception when casting GCHandle of null object

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Sat, 2 Apr 2005 16:17:03 -0500 (EST)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by jlarimer@gmail.com.

http://bugzilla.ximian.com/show_bug.cgi?id=74338

--- shadow/74338	2005-04-02 16:17:03.000000000 -0500
+++ shadow/74338.tmp.27126	2005-04-02 16:17:03.000000000 -0500
@@ -0,0 +1,96 @@
+Bug#: 74338
+Product: Mono: Runtime
+Version: 1.1
+OS: Red Hat 9.0
+OS Details: Fedora Core 2
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: GC
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: jlarimer@gmail.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Exception when casting GCHandle of null object
+
+Description of Problem:
+
+If you call GCHandle.Alloc on a null object, cast it to an IntPtr, then
+cast back to GCHandle, it will exception. The bug is in
+mono_gchandle_is_in_domain(), and is caused by mono trying to find the
+domain of a null object.
+
+I don't believe Mono 1.1.4 had this problem. In the application I was
+working on when I found this, an exception wasn't getting thrown and it
+appeared that it was happening while marshalling a struct with a couple
+GCHandle objects (one of which happened to be null) to an unmanaged library. 
+
+gdb caught the SIGSEGV in mono_gchandle_is_in_domain(), but mono itself
+didn't handle it correctly, the handles never got unlocked, causing any
+subsequent calls that try to lock the handles to hang.
+
+The code below reproduces the problem with a visible exception.
+
+Steps to reproduce the problem:
+
+using System;
+using System.Runtime.InteropServices;
+
+class GCTest {
+
+	public static void Main() {
+
+		IntPtr ptr = (IntPtr)GCHandle.Alloc(null);
+		GCHandle gch = (GCHandle)ptr;
+
+		Console.WriteLine("done {0} {1}", ptr, gch);
+	}
+}
+
+Actual Results:
+
+Output in Linux with Mono 1.1.6:
+
+Unhandled Exception: System.NullReferenceException: Object reference not
+set to an instance of an object
+in <0x00000> <unknown method>
+in (wrapper managed-to-native)
+System.Runtime.InteropServices.GCHandle:CheckCurrentDomain (int)
+in <0x00024> System.Runtime.InteropServices.GCHandle:op_Explicit (IntPtr value)
+in <0x00031> GCTest:Main ()
+
+Expected Results:
+
+Output in Windows with Microsoft.NET 1.1:
+
+done 11080064 System.Runtime.InteropServices.GCHandle
+
+How often does this happen? 
+
+Every time.
+
+Additional Information:
+
+This can be fixed by changing mono_gchandle_is_in_domain() so that if obj
+is 0, true is returned (a null object is always in the current domain?) 
+
+Index: mono/mono/metadata/gc.c
+===================================================================
+--- mono/mono/metadata/gc.c     (revision 42489)
++++ mono/mono/metadata/gc.c     (working copy)
+@@ -565,7 +565,11 @@
+                } else {
+                        MonoObject *obj;
+                        obj = handles->entries [slot];
+-                       result = domain == mono_object_domain (obj);
++                       if (obj) {
++                               result = domain == mono_object_domain (obj);
++                       } else {
++                               result = true;
++                       }
+                }
+        } else {
+                /* print a warning? */