Skip to content

Commit 4b725ec

Browse files
committed
[reflection] Return n2m wrapper from RuntimeMethodHandle.GetFunctionPointer
for functions marked with UnmanagedCallersOnlyAttribute, return a pointer to the n2m wrapper for calls to GetFunctionPointer Contributes to #59815
1 parent 9fbc728 commit 4b725ec

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/mono/mono/metadata/icall.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6293,6 +6293,11 @@ ves_icall_System_Environment_get_TickCount64 (void)
62936293
gpointer
62946294
ves_icall_RuntimeMethodHandle_GetFunctionPointer (MonoMethod *method, MonoError *error)
62956295
{
6296+
/* WISH: we should do this in managed */
6297+
if (G_UNLIKELY (mono_method_has_unmanaged_callers_only_attribute (method))) {
6298+
method = mono_marshal_get_managed_wrapper (method, NULL, (MonoGCHandle)0, error);
6299+
return_val_if_nok (error, NULL);
6300+
}
62966301
return mono_get_runtime_callbacks ()->get_ftnptr (method, error);
62976302
}
62986303

src/mono/mono/mini/interp/interp.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6537,9 +6537,16 @@ MINT_IN_CASE(MINT_BRTRUE_I8_SP) ZEROP_SP(gint64, !=); MINT_IN_BREAK;
65376537

65386538
MonoMethod *cmethod = LOCAL_VAR (ip [2], MonoMethod*);
65396539

6540-
InterpMethod *m = mono_interp_get_imethod (cmethod, error);
6541-
mono_error_assert_ok (error);
6542-
LOCAL_VAR (ip [1], gpointer) = imethod_to_ftnptr (m, FALSE);
6540+
if (G_UNLIKELY (mono_method_has_unmanaged_callers_only_attribute (cmethod))) {
6541+
cmethod = mono_marshal_get_managed_wrapper (cmethod, NULL, (MonoGCHandle)0, error);
6542+
mono_error_assert_ok (error);
6543+
gpointer addr = mini_get_interp_callbacks ()->create_method_pointer (cmethod, TRUE, error);
6544+
LOCAL_VAR (ip [1], gpointer) = addr;
6545+
} else {
6546+
InterpMethod *m = mono_interp_get_imethod (cmethod, error);
6547+
mono_error_assert_ok (error);
6548+
LOCAL_VAR (ip [1], gpointer) = imethod_to_ftnptr (m, FALSE);
6549+
}
65436550
ip += 3;
65446551
MINT_IN_BREAK;
65456552
}

0 commit comments

Comments
 (0)