Bug 1616 - Runtime crash: cant marshal array as native type 04, when calling IPGlobalProperties.GetActiveTcpConnections() on Win32
Summary: Runtime crash: cant marshal array as native type 04, when calling IPGlobalPr...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-10-20 14:06 UTC by Pablo Ruiz García
Modified: 2011-11-05 12:18 UTC (History)
3 users (show)

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

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:
Status:
RESOLVED FIXED

Description Pablo Ruiz García 2011-10-20 14:06:49 UTC
Calling Sys.Net.NetworkInformation.IPGlobalProperties.GetActiveTcpConnections() on Win32 causes mono to creash with "cant marshal array as native type 04".

A simple test demostrating the issue would be like:

               public static void Main() {
IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
                }


However, after inspecting a little around mono code it seems to be related to the way marshaling layout for WIN32_IN6_ADDR is defined:

               [StructLayout (LayoutKind.Explicit)]
                struct Win32_IN6_ADDR
                {
                        [FieldOffset (0)]
                        [MarshalAs ((short) UnmanagedType.U1, SizeConst = 16)]
                        public byte [] Bytes;
                }

As it looks like the marshaller dont know howto marshal arrays of U1 native type.

I'm made a simple program demostrating the specific issue too by extracting some code from: mcs/class/System/System.Net.NetworkInformation/IPGlobalProperties.cs. 

using System;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;

namespace Test
{
                [StructLayout (LayoutKind.Explicit)]
                struct Win32_IN6_ADDR
                {
                        [FieldOffset (0)]
                        [MarshalAs ((short) UnmanagedType.U1, SizeConst = 16)]
                        public byte [] Bytes;
                }

                [StructLayout (LayoutKind.Sequential)]
                class Win32_MIB_TCP6ROW
                {
                        public TcpState State;
                        public Win32_IN6_ADDR LocalAddr;
                        public uint LocalScopeId;
                        public int LocalPort;
                        public uint RemoteScopeId;
                        public int RemotePort;

                }

        public static class Program {

               [DllImport ("Iphlpapi.dll")]
                static extern int GetTcpTable (byte [] pTcpTable, ref int pdwSize, bool bOrder);

                [DllImport ("Iphlpapi.dll")]
                static extern int GetTcp6Table (byte [] TcpTable, ref int SizePointer, bool Order);


                public static void Main() {

                        var data = Marshal.SizeOf (typeof (Win32_MIB_TCP6ROW));

                }
        }
}
Comment 1 Zoltan Varga 2011-10-20 16:18:03 UTC
The definition seems to be wrong, it should be something like:
   [MarshalAs ((short) UnmanagedType.ByValArray, SizeConst = 16)]

you can't convert a byte[] to an U1.
Comment 2 Rodrigo Kumpera 2011-11-03 18:35:10 UTC
A hard crash is hardly the way to go here.
Comment 3 Rodrigo Kumpera 2011-11-05 12:18:49 UTC
The win32 marshaling bug has been fixed in trunk/2.10/mobile-master. The marshaling hard crash still on the works and will eventually land master.