Skip to content

Commit

Permalink
Add test coverage for unsupported marshal scenarios (dotnet/coreclr#2…
Browse files Browse the repository at this point in the history
…7393)

* Pointers to non-blittable structs are not supported
* SizeParamIndex could be bogus

Commit migrated from dotnet/coreclr@f1a900d
  • Loading branch information
MichalStrehovsky authored Oct 24, 2019
1 parent 0629300 commit 3ffd57e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using TestLibrary;

unsafe class Program
{
[StructLayout(LayoutKind.Sequential)]
struct NonBlittable
{
bool _nonBlittable;
}

// the "string unused" parameter is just so that we don't hit https://github.com/dotnet/coreclr/issues/27408 that
// makes this p/invoke actually work.
[DllImport("Unused")]
private static extern void PointerToNonBlittableType(NonBlittable* pNonBlittable, string unused);

static int Main()
{
Assert.Throws<MarshalDirectiveException>(() => PointerToNonBlittableType(null, null));

return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<DefineConstants>$(DefineConstants);STATIC</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="PInvokePointerTest.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Interop.settings.targets))\Interop.settings.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using TestLibrary;

class Program
{
[DllImport("Unused")]
static extern void SizeParamIndexTooBig(
out byte arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 20)] out byte[] arrByte);

[DllImport("Unused")]
public static extern void SizeParamIndexWrongType(
out string arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out byte[] arrByte);

static int Main()
{
Assert.Throws<MarshalDirectiveException>(() => SizeParamIndexTooBig(out var _, out var _));
Assert.Throws<MarshalDirectiveException>(() => SizeParamIndexWrongType(out var _, out var _));

return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<DefineConstants>$(DefineConstants);STATIC</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="InvalidParamIndex.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Interop.settings.targets))\Interop.settings.targets" />
</Project>

0 comments on commit 3ffd57e

Please sign in to comment.