[Mono-bugs] [Bug 59405][Nor] New - Inter-assembly custom marshaler lookup fails

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Tue, 1 Jun 2004 13:16:14 -0400 (EDT)


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 patrick@vrac.iastate.edu.

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

--- shadow/59405	2004-06-01 13:16:14.000000000 -0400
+++ shadow/59405.tmp.29112	2004-06-01 13:16:14.000000000 -0400
@@ -0,0 +1,241 @@
+Bug#: 59405
+Product: Mono: Runtime
+Version: unspecified
+OS: 
+OS Details: Fedora Core 1
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: patrick@vrac.iastate.edu               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Inter-assembly custom marshaler lookup fails
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+Description of Problem:
+
+With Mono 0.91 and beyond, I have found that the lookup of custom
+marshalers fails with an assertion error.  Using the 0.94-20040531
+snapshot, I get the following error:
+
+** ERROR **: file marshal.c: line 3940 (mono_marshal_get_native_wrapper):
+assertion failed: (mtype != NULL)
+aborting...
+
+I reported a problem very similar to this with Mono 0.29 back in late
+January 2004 that was fixed with Mono 0.30:
+
+http://lists.ximian.com/archives/public/mono-list/2004-January/017851.html
+
+The symptoms are very much the same in this case; only the line numbers in
+mono/metadata/marshal.c and mono/metadata/reflection.c have changed.
+
+Steps to reproduce the problem:
+1. Compile the included code.
+2. mono B.exe
+
+Actual Results:
+ 
+** ERROR **: file marshal.c: line 3940 (mono_marshal_get_native_wrapper):
+assertion failed: (mtype != NULL)
+aborting...
+
+Expected Results:
+
+[C#] BaseMarshaler.MarshalManagedToNative() obj: B.Derived
+     obj.mRawObject: 0x9a42d48
+[C++] handleBase(): received 0x9a42d48
+
+How often does this happen? 
+
+Every time.
+
+Additional Information:
+
+I have included the code I used to generate the above errors.  This code
+works fine with Mono 0.30.2, but it fails with Mono Beta 1 and newer.
+
+*** Makefile ***
+# Linux
+CSC=            mcs /debug
+MAKE_DLL=       c++ -g -shared -o $@ $<
+LDFLAGS=        -L. -la_native
+NATIVE_DLL_A=   liba_native.so
+NATIVE_DLL_B=   libb_native.so
+ 
+# Windows
+#CSC=           csc /nologo /debug
+#MAKE_DLL=      cl /DWIN32 /nologo /EHsc $< /link /dll /out:$@
+#LDFLAGS=       /libpath:. a_native.lib
+#NATIVE_DLL_A=  a_native.dll
+#NATIVE_DLL_B=  b_native.dll
+ 
+all: A.dll B.exe $(NATIVE_DLL_A) $(NATIVE_DLL_B)
+ 
+A.dll: A.cs
+        $(CSC) /target:library A.cs
+ 
+B.exe: B.cs
+        $(CSC) B.cs /r:A.dll
+ 
+$(NATIVE_DLL_A): a_native.cpp
+        $(MAKE_DLL)
+ 
+$(NATIVE_DLL_B): b_native.cpp
+        $(MAKE_DLL) $(LDFLAGS)
+ 
+clean:
+        rm -f *.exe *.lib *.exp *.dll *.so *.pdb *.obj
+
+*** A.cs ***
+using System;
+using System.Runtime.InteropServices;
+ 
+ 
+namespace A
+{
+ 
+public class Base
+{
+   protected internal IntPtr mRawObject = IntPtr.Zero;
+ 
+   internal Base(IntPtr obj)
+   {
+      mRawObject = obj;
+   }
+ 
+   [DllImport("a_native", CharSet = CharSet.Ansi)]
+   private extern static IntPtr new_Base();
+ 
+   public Base()
+   {
+      mRawObject = new_Base();
+   }
+ 
+   public virtual void f()
+   {
+      Console.WriteLine("[C#] Base.f()");
+   }
+}
+ 
+public class BaseMarshaler : ICustomMarshaler
+{
+   public void CleanUpManagedData(Object obj) { }
+ 
+   public void CleanUpNativeData(IntPtr nativeData) { }
+ 
+   public int GetNativeDataSize()
+   {
+      return -1;
+   }
+ 
+   // Marshaling for managed data being passed to C++.
+   public IntPtr MarshalManagedToNative(Object obj)
+   {
+      Console.WriteLine("[C#] BaseMarshaler.MarshalManagedToNative() obj: " +
+                        obj);
+      Console.WriteLine("     obj.mRawObject: 0x{0:x}",
+                        ((Base) obj).mRawObject.ToInt32());
+      return ((Base) obj).mRawObject;
+   }
+ 
+   // Marshaling for native memory coming from C++.
+   public Object MarshalNativeToManaged(IntPtr nativeObj)
+   {
+      return new Base(nativeObj);
+   }
+ 
+   public static ICustomMarshaler GetInstance(string cookie)
+   {
+      return mInstance;
+   }
+ 
+   private static BaseMarshaler mInstance = new BaseMarshaler();
+}
+ 
+}
+
+*** B.cs ***
+using System;
+using System.Runtime.InteropServices;
+ 
+ 
+namespace B
+{
+ 
+public class Run
+{
+   [DllImport("b_native", CharSet = CharSet.Ansi)]
+   private extern static void handleBase(
+      [MarshalAs(UnmanagedType.CustomMarshaler,
+                 MarshalTypeRef = typeof(A.BaseMarshaler))] A.Base obj);
+ 
+   public static void Main(String[] args)
+   {
+      A.Base obj = new A.Base();
+      handleBase(obj);
+   }
+}
+ 
+}
+
+*** a_native.cpp ***
+#include "a_native.h"
+ 
+#ifdef WIN32
+#define DLL_API __declspec(dllexport)
+#else
+#define DLL_API
+#endif
+ 
+extern "C"
+{
+ 
+DLL_API Base* new_Base()
+{
+   Base* obj = new Base;
+   return obj;
+}
+ 
+}
+
+*** a_native.h ***
+#ifndef _A_NATIVE_H_
+#define _A_NATIVE_H_
+ 
+class Base
+{
+public:
+   virtual void f()
+   {
+   }
+};
+ 
+#endif
+
+*** b_native.cpp ***
+#include <iostream>
+#include "a_native.h"
+ 
+#ifdef WIN32
+#define DLL_API __declspec(dllexport)
+#else
+#define DLL_API
+#endif
+ 
+extern "C"
+{
+ 
+DLL_API void handleBase(Base* obj)
+{
+   std::cout << "[C++] handleBase(): received " << std::hex << obj <<
+std::endl;
+}
+ 
+}