Skip to content

Commit

Permalink
Extend test coverage for SafeHandle (dotnet/coreclr#27285)
Browse files Browse the repository at this point in the history
* Extend test coverage for SafeHandle

Trigger unmarshaling failure using an array marshaller. This is something crossgen2 will be able to pregenerate. The existing test case with CustomMarshaller will always be jitted and never pregenerated.

* a


Commit migrated from dotnet/coreclr@605124c
  • Loading branch information
MichalStrehovsky authored and jkoritzinsky committed Oct 19, 2019
1 parent 1b56ed1 commit e0f07d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ public static void RunTest()
IntPtr value = (IntPtr)123;
TestSafeHandle h = new TestSafeHandle();

Assert.Throws<InvalidOperationException>(() => SafeHandleNative.GetHandleAndCookie(value, out h, out var cookie));
Assert.Throws<InvalidOperationException>(() => SafeHandleNative.GetHandleAndCookie(out _, value, out h));

Assert.AreEqual(value, h.DangerousGetHandle());

// Try again, this time triggering unmarshal failure with an array.
value = (IntPtr)456;
h = new TestSafeHandle();

Assert.Throws<OverflowException>(() => SafeHandleNative.GetHandleAndArray(out _, out _, value, out h));

Assert.AreEqual(value, h.DangerousGetHandle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

#include <platformdefines.h>
#include <xplatform.h>

struct StructWithHandle
{
Expand Down Expand Up @@ -60,9 +61,18 @@ extern "C" DLL_EXPORT void STDMETHODVCALLTYPE SafeHandle_Invalid(...)
{
}

extern "C" void DLL_EXPORT GetHandleAndCookie(intptr_t value, HANDLE* handle, void** pCookie)
extern "C" void DLL_EXPORT GetHandleAndCookie(void** pCookie, intptr_t value, HANDLE* handle)
{
*handle = (HANDLE)value;
*pCookie = (void*)4567; // the value here does not matter. It just needs to not be nullptr.
}

extern "C" void DLL_EXPORT GetHandleAndArray(/*out*/SHORT* arrSize, SHORT** ppActual, intptr_t value, HANDLE* handle)
{
*arrSize = -1; // minus one is going to make this throw on unmarshal

// Still need to provide something allocated using an expected allocator so that the marshaller can unalloc
*ppActual = (SHORT*)CoreClrAlloc(sizeof(SHORT));

*handle = (HANDLE)value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ public object MarshalNativeToManaged(IntPtr pNativeData)
public static extern void StructWithSafeHandleOut(out StructWithHandle str, IntPtr expectedValue);

[DllImport(nameof(SafeHandleNative))]
public static extern void GetHandleAndCookie(IntPtr value, out TestSafeHandle handle, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ThrowingCustomMarshaler), MarshalCookie = "")] out object cookie);
public static extern void GetHandleAndCookie([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(ThrowingCustomMarshaler), MarshalCookie = "")] out object cookie, IntPtr value, out TestSafeHandle handle);

[DllImport(nameof(SafeHandleNative))]
public static extern void GetHandleAndArray(out short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out short[] arrShort, IntPtr value, out TestSafeHandle handle);

[DllImport(nameof(SafeHandleNative), CallingConvention = CallingConvention.Cdecl)]
public static extern void SafeHandle_Invalid([MarshalAs(UnmanagedType.Interface)] TestSafeHandle handle);
Expand Down

0 comments on commit e0f07d0

Please sign in to comment.