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)

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


Attachments

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

Note You need to log in before you can comment on or make changes to this bug.