Bug 46660 - GCHandle.Alloc does not throw an ArgumentException exception when called with a non-blittable type (for a pinned handle)
Summary: GCHandle.Alloc does not throw an ArgumentException exception when called with...
Status: CONFIRMED
Alias: None
Product: Runtime
Classification: Mono
Component: GC (show other bugs)
Version: master
Hardware: All All
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-11-09 19:46 UTC by Josh Peterson
Modified: 2017-10-09 21:58 UTC (History)
5 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 for Bug 46660 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Josh Peterson 2016-11-09 19:46:41 UTC
I believe that it should not be possible to allocate a pinned GCHandle to a non-blittable type. Mono allows this though. At least, .NET does not allow this, and throws an ArgumentException exception in this case.

This code demonstrates the cases we test this for IL2CPP:

using System;
using System.Runtime.InteropServices;

class Program
{
	public static void ExceptException<T>(Action methodThatShouldThrow) where T : Exception
	{
		try
		{
			methodThatShouldThrow();
		}
		catch (T)
		{
			Console.WriteLine("An exception of type {0} was thrown, as expected.", typeof(T).Name);
			return;
		}

		Console.WriteLine("An exception of type {0} was not thrown, which is not expected.", typeof(T).Name);
	}

	class AnyClass
	{
	}

	struct StructWithReferenceTypeInside
	{
		public string myStr;
	}

	struct GenericStruct<T>
	{
		public T myItem;
	}

	[StructLayout(LayoutKind.Auto)]
	struct StructWithIntInsideAutoLayout
	{
		public int myInt;
	}

	public static void Main()
	{
		var arrayOfStrings = new string[] { "a", "B" };
		ExceptException<ArgumentException>(() => GCHandle.Alloc(arrayOfStrings, GCHandleType.Pinned));

		var arrayOfIntArrays = new int[][]
		{
				new int[] {1, 2},
				new int[] {3, 4, 5},
		};
		ExceptException<ArgumentException>(() => GCHandle.Alloc(arrayOfIntArrays, GCHandleType.Pinned));
		ExceptException<ArgumentException>(() => GCHandle.Alloc(new AnyClass(), GCHandleType.Pinned));

		var nonBlittableStruct = default(StructWithReferenceTypeInside);
		ExceptException<ArgumentException>(() => GCHandle.Alloc(nonBlittableStruct, GCHandleType.Pinned));

		var nonBlittableGenericStruct = default(GenericStruct<string>);
		ExceptException<ArgumentException>(() => GCHandle.Alloc(nonBlittableGenericStruct, GCHandleType.Pinned));

		ExceptException<ArgumentException>(() => GCHandle.Alloc(default(DateTime), GCHandleType.Pinned));

		ExceptException<ArgumentException>(() => GCHandle.Alloc(default(StructWithIntInsideAutoLayout), GCHandleType.Pinned));
	}
}

On .NET, the output is:

An exception of type ArgumentException was thrown, as expected.
An exception of type ArgumentException was thrown, as expected.
An exception of type ArgumentException was thrown, as expected.
An exception of type ArgumentException was thrown, as expected.
An exception of type ArgumentException was thrown, as expected.
An exception of type ArgumentException was thrown, as expected.
An exception of type ArgumentException was thrown, as expected.

On Mono, the output is:

An exception of type ArgumentException was not thrown, which is not expected.
An exception of type ArgumentException was not thrown, which is not expected.
An exception of type ArgumentException was not thrown, which is not expected.
An exception of type ArgumentException was not thrown, which is not expected.
An exception of type ArgumentException was not thrown, which is not expected.
An exception of type ArgumentException was not thrown, which is not expected.
An exception of type ArgumentException was not thrown, which is not expected.

I would expect the output to match .NET.
Comment 1 Josh Peterson 2016-11-09 20:20:01 UTC
I'm working on a fix for this bug now - I'll submit a pull request when the fix is ready.
Comment 2 Miguel de Icaza [MSFT] 2016-11-28 02:31:07 UTC
The pull request with the fix is being discussed here:

https://github.com/mono/mono/pull/3929
Comment 3 Josh Peterson 2017-03-15 16:29:19 UTC
I've started a new PR for the fix for this bug here: https://github.com/mono/mono/pull/4533
Comment 4 Ludovic Henry 2017-10-06 23:50:25 UTC
I can reproduce with Mono 5.8.0.2 (2017-10/a3943e28cf8) on macOS