Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle crash when [out] parameter of type int is passed to delegate #81276

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/mono/mono/metadata/marshal-lightweight.c
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,7 @@ emit_managed_wrapper_ilgen (MonoMethodBuilder *mb, MonoMethodSignature *invoke_s
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_PTR:
case MONO_TYPE_I:
mono_emit_marshal (m, i, invoke_sig->params [i], mspecs [i + 1], tmp_locals [i], NULL, MARSHAL_ACTION_MANAGED_CONV_OUT);
break;
default:
Expand Down
25 changes: 25 additions & 0 deletions src/tests/Interop/MarshalAPI/FunctionPointer/FunctionPointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ static class FunctionPointerNative

[DllImport(nameof(FunctionPointerNative))]
static unsafe extern void FillOutPtr(IntPtr* p);

[DllImport(nameof(FunctionPointerNative))]
static unsafe extern void FillOutIntParameter(out IntPtr p);
}

delegate void VoidDelegate();
Expand Down Expand Up @@ -81,13 +84,35 @@ public static void RunGetDelForOutPtrTest()
Assert.Equal(expectedValue, outVar);
}

[DllImport(nameof(FunctionPointerNative))]
static unsafe extern void FillOutIntParameter(out IntPtr p);

private unsafe delegate void DelegateToFillOutIntParameter(out IntPtr p);

public static void RunGetDelForOutIntTest()
{
Console.WriteLine($"Running {nameof(RunGetDelForOutIntTest)}...");
IntPtr outVar = 0;
int expectedValue = 50;
unsafe
{
DelegateToFillOutIntParameter d = new DelegateToFillOutIntParameter(FillOutIntParameter);
IntPtr ptr = Marshal.GetFunctionPointerForDelegate(d);
DelegateToFillOutIntParameter OutPtrDelegate = Marshal.GetDelegateForFunctionPointer<DelegateToFillOutIntParameter>(ptr);
OutPtrDelegate(out outVar);
GC.KeepAlive(d);
}
Assert.Equal(expectedValue, outVar);
}

public static int Main()
{
try
{
RunGetDelForFcnPtrTest();
RunGetFcnPtrSingleMulticastTest();
RunGetDelForOutPtrTest();
RunGetDelForOutIntTest();
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ extern "C" DLL_EXPORT void FillOutPtr(intptr_t *p)
{
*p = 60;
}

extern "C" DLL_EXPORT void FillOutIntParameter(intptr_t *p)
{
*p = 50;
}