Bug 477396

Summary: Marshal.StructureToPtr handles null pointers/delegates incorrectly
Product: [Mono] Mono: Class Libraries Reporter: Christian Hoff <christian_hoff>
Component: CORLIBAssignee: Mono Bugs <mono-bugs>
Status: RESOLVED FIXED QA Contact: Mono Bugs <mono-bugs>
Severity: Major    
Priority: P5 - None CC: forgotten_vxPDddArjq
Version: SVN   
Target Milestone: ---   
Hardware: x86-64   
OS: Linux   
Whiteboard:
Found By: Development Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---
Attachments: test case

Description Christian Hoff 2009-02-18 19:58:26 UTC
When invoking StructureToPtr, delegates in the returned structure are never NULL.

Suppose you have a class struct containing one delegate:
struct TestStruct {
TestDelegate test;
}

"test" is never set to null when it doesn't point to a C method. Invoking that method results in a segfault.

See the attached test case for a minimal repro.
The unmanaged code is already compiled to a static lib; if it doesn't work for you(probably only working on 64Bit machaines), recompile it using:
gcc -c -fPIC ./main.c -o ./main.o
gcc -shared -Wl,-soname,libptr_to_struct.so.1 -o libptr_to_struct.so  ./main.o

As this is a blocker for a Gtk# patch, it would be really nice if you could have a look at that bug soon :-)
Comment 1 Christian Hoff 2009-02-18 20:00:02 UTC
That's the output of the test case(compile it with gmcs, not mcs):

Creating dummy
--- structure.test should be null ---
Stacktrace:

  at (wrapper managed-to-native) object.wrapper_native_(nil) () <0x00042>
  at (wrapper managed-to-native) object.wrapper_native_(nil) () <0xffffffff>
  at TestStructToPtr.MainClass.Main (string[]) <0x0007a>
  at (wrapper runtime-invoke) TestStructToPtr.MainClass.runtime_invoke_void_object (object,intptr,intptr,intptr) <0xffffffff>

Native stacktrace:

        mono [0x4bd937]
        mono [0x520a00]
        mono [0x421c91]
        /lib/libpthread.so.0 [0x7f2c33bb5a80]

Debug info from gdb:


=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
Comment 2 Christian Hoff 2009-02-18 20:00:56 UTC
Created attachment 273712 [details]
test case
Comment 3 Forgotten User vxPDddArjq 2009-02-18 23:15:38 UTC
Fixed in SVN HEAD/2.4 branch:
http://lists.ximian.com/pipermail/mono-patches/2009-February/142158.html