From 74f0f537442deb4dcc73f94bfa0b4a6661be54e8 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 4 Jul 2023 00:55:43 -0400 Subject: [PATCH 1/3] [mono] Precompute the CallInfo structure used by the mono_arch_..native_call_context_.. functions. --- src/mono/mono/mini/interp/interp.c | 19 ++++++++-- src/mono/mono/mini/mini-amd64.c | 60 ++++++++++++++++++++---------- src/mono/mono/mini/mini-arm.c | 59 ++++++++++++++++++++--------- src/mono/mono/mini/mini-arm64.c | 60 ++++++++++++++++++++---------- src/mono/mono/mini/mini-riscv.c | 52 +++++++++++++++++++------- src/mono/mono/mini/mini-x86.c | 48 ++++++++++++++++++------ src/mono/mono/mini/mini.h | 12 ++++-- 7 files changed, 222 insertions(+), 88 deletions(-) diff --git a/src/mono/mono/mini/interp/interp.c b/src/mono/mono/mini/interp/interp.c index d24727f6da745..eede227c65c82 100644 --- a/src/mono/mono/mini/interp/interp.c +++ b/src/mono/mono/mini/interp/interp.c @@ -1680,8 +1680,15 @@ ves_pinvoke_method ( } #ifdef MONO_ARCH_HAVE_INTERP_PINVOKE_TRAMP + gpointer call_info = *cache; + + if (!call_info) { + call_info = mono_arch_get_interp_native_call_info (get_default_mem_manager (), sig); + mono_memory_barrier (); + *cache = call_info; + } CallContext ccontext; - mono_arch_set_native_call_context_args (&ccontext, &frame, sig); + mono_arch_set_native_call_context_args (&ccontext, &frame, sig, call_info); args = &ccontext; #else InterpMethodArguments *margs = build_args_from_sig (sig, &frame); @@ -1705,7 +1712,7 @@ ves_pinvoke_method ( #ifdef MONO_ARCH_HAVE_INTERP_PINVOKE_TRAMP if (!context->has_resume_state) { - mono_arch_get_native_call_context_ret (&ccontext, &frame, sig); + mono_arch_get_native_call_context_ret (&ccontext, &frame, sig, call_info); } g_free (ccontext.stack); @@ -3045,8 +3052,10 @@ interp_entry_from_trampoline (gpointer ccontext_untyped, gpointer rmethod_untype frame.stack = sp; frame.retval = sp; + gpointer call_info = mono_arch_get_interp_native_call_info (NULL, sig); + /* Copy the args saved in the trampoline to the frame stack */ - gpointer retp = mono_arch_get_native_call_context_args (ccontext, &frame, sig); + gpointer retp = mono_arch_get_native_call_context_args (ccontext, &frame, sig, call_info); /* Allocate storage for value types */ stackval *newsp = sp; @@ -3087,7 +3096,9 @@ interp_entry_from_trampoline (gpointer ccontext_untyped, gpointer rmethod_untype /* Write back the return value */ /* 'frame' is still valid */ - mono_arch_set_native_call_context_ret (ccontext, &frame, sig, retp); + mono_arch_set_native_call_context_ret (ccontext, &frame, sig, call_info, retp); + + mono_arch_free_interp_native_call_info (call_info); } #else diff --git a/src/mono/mono/mini/mini-amd64.c b/src/mono/mono/mini/mini-amd64.c index dbb7448029937..c17ce02064b3e 100644 --- a/src/mono/mono/mini/mini-amd64.c +++ b/src/mono/mono/mini/mini-amd64.c @@ -840,6 +840,14 @@ add_valuetype (MonoMethodSignature *sig, ArgInfo *ainfo, MonoType *type, #endif /* !TARGET_WIN32 */ } +static int +call_info_size (MonoMethodSignature *sig) +{ + int n = sig->hasthis + sig->param_count; + + return sizeof (CallInfo) + (sizeof (ArgInfo) * n); +} + /* * get_call_info: * @@ -859,10 +867,11 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) CallInfo *cinfo; gboolean is_pinvoke = sig->pinvoke; + int size = call_info_size (sig); if (mp) - cinfo = (CallInfo *)mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = (CallInfo *)mono_mempool_alloc0 (mp, size); else - cinfo = (CallInfo *)g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = (CallInfo *)g_malloc0 (size); cinfo->nargs = n; cinfo->gsharedvt = mini_is_gsharedvt_variable_signature (sig); @@ -1178,11 +1187,33 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) } } +gpointer +mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) +{ + CallInfo *cinfo = get_call_info (NULL, sig); + if (mem_manager) { + int size = call_info_size (sig); + gpointer res = mono_mem_manager_alloc0 (mem_manager, size); + memcpy (res, cinfo, size); + g_free (cinfo); + return res; + } else { + return cinfo; + } +} + +void +mono_arch_free_interp_native_call_info (gpointer call_info) +{ + /* Allocated by get_call_info () */ + g_free (call_info); +} + void -mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { - CallInfo *cinfo = get_call_info (NULL, sig); const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1222,15 +1253,13 @@ mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, M if (temp_size) arg_set_val (ccontext, ainfo, storage); } - - g_free (cinfo); } void -mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer retp) +mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info, gpointer retp) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1238,7 +1267,6 @@ mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo return; interp_cb = mini_get_interp_callbacks (); - cinfo = get_call_info (NULL, sig); ainfo = &cinfo->ret; if (retp) { @@ -1263,15 +1291,13 @@ mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo if (temp_size) arg_set_val (ccontext, ainfo, storage); } - - g_free (cinfo); } gpointer -mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1302,15 +1328,14 @@ mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, M if (ainfo->storage == ArgValuetypeAddrInIReg) storage = (gpointer) ccontext->gregs [cinfo->ret.reg]; } - g_free (cinfo); return storage; } void -mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; ArgInfo *ainfo; gpointer storage; @@ -1319,7 +1344,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo return; interp_cb = mini_get_interp_callbacks (); - cinfo = get_call_info (NULL, sig); ainfo = &cinfo->ret; /* The return values were stored directly at address passed in reg */ @@ -1334,8 +1358,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo } interp_cb->data_to_frame_arg ((MonoInterpFrameHandle)frame, sig, -1, storage); } - - g_free (cinfo); } /* diff --git a/src/mono/mono/mini/mini-arm.c b/src/mono/mono/mini/mini-arm.c index 9aeb992bde410..e0a583beaf1cf 100644 --- a/src/mono/mono/mini/mini-arm.c +++ b/src/mono/mono/mini/mini-arm.c @@ -1253,6 +1253,14 @@ is_hfa (MonoType *t, int *out_nfields, int *out_esize) return TRUE; } +static int +call_info_size (MonoMethodSignature *sig) +{ + int n = sig->hasthis + sig->param_count; + + return sizeof (CallInfo) + (sizeof (ArgInfo) * n); +} + static CallInfo* get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) { @@ -1267,10 +1275,11 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) gboolean is_pinvoke = sig->pinvoke; gboolean vtype_retaddr = FALSE; + int size = call_info_size (sig); if (mp) - cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = mono_mempool_alloc0 (mp, size); else - cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = g_malloc0 (size); cinfo->nargs = n; gr = ARMREG_R0; @@ -1666,12 +1675,34 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) memcpy (ccontext->stack + ainfo->offset, (host_mgreg_t*)src + ainfo->size, ainfo->struct_size - reg_size); } +gpointer +mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) +{ + CallInfo *cinfo = get_call_info (NULL, sig); + if (mem_manager) { + int size = call_info_size (sig); + gpointer res = mono_mem_manager_alloc0 (mem_manager, size); + memcpy (res, cinfo, size); + g_free (cinfo); + return res; + } else { + return cinfo; + } +} + +void +mono_arch_free_interp_native_call_info (gpointer call_info) +{ + /* Allocated by get_call_info () */ + g_free (call_info); +} + /* Set arguments in the ccontext (for i2n entry) */ void -mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1704,16 +1735,14 @@ mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, M if (temp_size) arg_set_val (ccontext, ainfo, storage); } - - g_free (cinfo); } /* Set return value in the ccontext (for n2i return) */ void -mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer retp) +mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info, gpointer retp) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1721,7 +1750,6 @@ mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo return; interp_cb = mini_get_interp_callbacks (); - cinfo = get_call_info (NULL, sig); ainfo = &cinfo->ret; if (retp) { @@ -1734,16 +1762,14 @@ mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo memset (ccontext, 0, sizeof (CallContext)); // FIXME interp_cb->frame_arg_to_data ((MonoInterpFrameHandle)frame, sig, -1, storage); } - - g_free (cinfo); } /* Gets the arguments from ccontext (for n2i entry) */ gpointer -mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1767,16 +1793,15 @@ mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, M storage = (gpointer)(gsize)ccontext->gregs [cinfo->ret.reg]; } - g_free (cinfo); return storage; } /* Gets the return value from ccontext (for i2n exit) */ void -mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; ArgInfo *ainfo; gpointer storage; @@ -1792,8 +1817,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo storage = arg_get_storage (ccontext, ainfo); interp_cb->data_to_frame_arg ((MonoInterpFrameHandle)frame, sig, -1, storage); } - - g_free (cinfo); } #ifndef DISABLE_JIT diff --git a/src/mono/mono/mini/mini-arm64.c b/src/mono/mono/mini/mini-arm64.c index 2fbe4c070e505..bb5691a74fceb 100644 --- a/src/mono/mono/mini/mini-arm64.c +++ b/src/mono/mono/mini/mini-arm64.c @@ -1689,6 +1689,14 @@ add_param (CallInfo *cinfo, ArgInfo *ainfo, MonoType *t, gboolean is_return) } } +static int +call_info_size (MonoMethodSignature *sig) +{ + int n = sig->hasthis + sig->param_count; + + return sizeof (CallInfo) + (sizeof (ArgInfo) * n); +} + /* * get_call_info: * @@ -1703,10 +1711,11 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) n = sig->hasthis + sig->param_count; + int size = call_info_size (sig); if (mp) - cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = mono_mempool_alloc0 (mp, size); else - cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = g_malloc0 (size); cinfo->nargs = n; cinfo->pinvoke = sig->pinvoke; @@ -1825,12 +1834,34 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) } } +gpointer +mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) +{ + CallInfo *cinfo = get_call_info (NULL, sig); + if (mem_manager) { + int size = call_info_size (sig); + gpointer res = mono_mem_manager_alloc0 (mem_manager, size); + memcpy (res, cinfo, size); + g_free (cinfo); + return res; + } else { + return cinfo; + } +} + +void +mono_arch_free_interp_native_call_info (gpointer call_info) +{ + /* Allocated by get_call_info () */ + g_free (call_info); +} + /* Set arguments in the ccontext (for i2n entry) */ void -mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1869,16 +1900,14 @@ mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, M if (temp_size) arg_set_val (ccontext, ainfo, storage); } - - g_free (cinfo); } /* Set return value in the ccontext (for n2i return) */ void -mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer retp) +mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info, gpointer retp) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1886,7 +1915,6 @@ mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo return; interp_cb = mini_get_interp_callbacks (); - cinfo = get_call_info (NULL, sig); ainfo = &cinfo->ret; if (retp) { @@ -1905,16 +1933,14 @@ mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo if (temp_size) arg_set_val (ccontext, ainfo, storage); } - - g_free (cinfo); } /* Gets the arguments from ccontext (for n2i entry) */ gpointer -mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1937,16 +1963,15 @@ mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, M if (ainfo->storage == ArgVtypeByRef) storage = (gpointer) ccontext->gregs [cinfo->ret.reg]; } - g_free (cinfo); return storage; } /* Gets the return value from ccontext (for i2n exit) */ void -mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; ArgInfo *ainfo; gpointer storage; @@ -1954,7 +1979,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo return; interp_cb = mini_get_interp_callbacks (); - cinfo = get_call_info (NULL, sig); ainfo = &cinfo->ret; if (ainfo->storage != ArgVtypeByRef) { @@ -1968,8 +1992,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo } interp_cb->data_to_frame_arg ((MonoInterpFrameHandle)frame, sig, -1, storage); } - - g_free (cinfo); } typedef struct { diff --git a/src/mono/mono/mini/mini-riscv.c b/src/mono/mono/mini/mini-riscv.c index 2b4dcaa1c54f4..671c8b242386e 100644 --- a/src/mono/mono/mini/mini-riscv.c +++ b/src/mono/mono/mini/mini-riscv.c @@ -898,6 +898,14 @@ add_param (CallInfo *cinfo, ArgInfo *ainfo, MonoType *t) } } +static int +call_info_size (MonoMethodSignature *sig) +{ + int n = sig->hasthis + sig->param_count; + + return sizeof (CallInfo) + (sizeof (ArgInfo) * n); +} + /** * get_call_info: * create call info here. @@ -912,10 +920,11 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) CallInfo *cinfo; int paramNum = sig->hasthis + sig->param_count; int pindex; + int size = call_info_size (sig); if (mp) - cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * paramNum)); + cinfo = mono_mempool_alloc0 (mp, size); else - cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * paramNum)); + cinfo = g_malloc0 (size); cinfo->nargs = paramNum; @@ -1022,12 +1031,34 @@ arg_get_val (CallContext *ccontext, ArgInfo *ainfo, gpointer dest) NOT_IMPLEMENTED; } +gpointer +mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) +{ + CallInfo *cinfo = get_call_info (NULL, sig); + if (mem_manager) { + int size = call_info_size (sig); + gpointer res = mono_mem_manager_alloc0 (mem_manager, size); + memcpy (res, cinfo, size); + g_free (cinfo); + return res; + } else { + return cinfo; + } +} + +void +mono_arch_free_interp_native_call_info (gpointer call_info) +{ + /* Allocated by get_call_info () */ + g_free (call_info); +} + /* Set arguments in the ccontext (for i2n entry) */ void -mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -1068,30 +1099,28 @@ mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, M if (temp_size) arg_set_val (ccontext, ainfo, storage); } - - g_free (cinfo); } /* Set return value in the ccontext (for n2i return) */ void -mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer retp) +mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info, gpointer retp) { NOT_IMPLEMENTED; } /* Gets the arguments from ccontext (for n2i entry) */ gpointer -mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { NOT_IMPLEMENTED; } /* Gets the return value from ccontext (for i2n exit) */ void -mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; ArgInfo *ainfo; gpointer storage; @@ -1099,7 +1128,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo return; interp_cb = mini_get_interp_callbacks (); - cinfo = get_call_info (NULL, sig); ainfo = &cinfo->ret; if (ainfo->storage != ArgVtypeByRef) { @@ -1112,8 +1140,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo storage = arg_get_storage (ccontext, ainfo); interp_cb->data_to_frame_arg ((MonoInterpFrameHandle)frame, sig, -1, storage); } - - g_free (cinfo); } #ifndef DISABLE_JIT diff --git a/src/mono/mono/mini/mini-x86.c b/src/mono/mono/mini/mini-x86.c index 1006eabf989e5..a9cd17e54d252 100644 --- a/src/mono/mono/mini/mini-x86.c +++ b/src/mono/mono/mini/mini-x86.c @@ -540,16 +540,24 @@ get_call_info_internal (CallInfo *cinfo, MonoMethodSignature *sig) return cinfo; } +static int +call_info_size (MonoMethodSignature *sig) +{ + int n = sig->hasthis + sig->param_count; + + return sizeof (CallInfo) + (sizeof (ArgInfo) * n); +} + static CallInfo* get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) { - int n = sig->hasthis + sig->param_count; + int size = call_info_size (sig); CallInfo *cinfo; if (mp) - cinfo = mono_mempool_alloc0 (mp, sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = mono_mempool_alloc0 (mp, size); else - cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n)); + cinfo = g_malloc0 (size); return get_call_info_internal (cinfo, sig); } @@ -595,11 +603,33 @@ arg_get_val (CallContext *ccontext, ArgInfo *ainfo, gpointer dest) *(float*) dest = (float)ccontext->fret; } +gpointer +mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) +{ + CallInfo *cinfo = get_call_info (NULL, sig); + if (mem_manager) { + int size = call_info_size (sig); + gpointer res = mono_mem_manager_alloc0 (mem_manager, size); + memcpy (res, cinfo, size); + g_free (cinfo); + return res; + } else { + return cinfo; + } +} + +void +mono_arch_free_interp_native_call_info (gpointer call_info) +{ + /* Allocated by get_call_info () */ + g_free (call_info); +} + void -mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { - CallInfo *cinfo = get_call_info (NULL, sig); const MonoEECallbacks *interp_cb = mini_get_interp_callbacks (); + CallInfo *cinfo = (CallInfo*)call_info; gpointer storage; ArgInfo *ainfo; @@ -629,15 +659,13 @@ mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, M interp_cb->frame_arg_to_data ((MonoInterpFrameHandle)frame, sig, i, storage); } - - g_free (cinfo); } void -mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig) +mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info) { const MonoEECallbacks *interp_cb; - CallInfo *cinfo; + CallInfo *cinfo = (CallInfo*)call_info; ArgInfo *ainfo; gpointer storage; @@ -661,8 +689,6 @@ mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, Mo } interp_cb->data_to_frame_arg ((MonoInterpFrameHandle)frame, sig, -1, storage); } - - g_free (cinfo); } /* diff --git a/src/mono/mono/mini/mini.h b/src/mono/mono/mini/mini.h index 9c5f7e9e84ce6..7483e028159d4 100644 --- a/src/mono/mono/mini/mini.h +++ b/src/mono/mono/mini/mini.h @@ -2544,15 +2544,19 @@ gpointer mono_arch_get_interp_to_native_trampoline (MonoTrampInfo **info); gpointer mono_arch_get_native_to_interp_trampoline (MonoTrampInfo **info); #ifdef MONO_ARCH_HAVE_INTERP_PINVOKE_TRAMP +/* Return an arch specific structure with precomputed information for pinvoke calls with signature SIG */ +gpointer mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig); // Moves data (arguments and return vt address) from the InterpFrame to the CallContext so a pinvoke call can be made. -void mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig); +void mono_arch_set_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info); // Moves the return value from the InterpFrame to the ccontext, or to the retp (if native code passed the retvt address) -void mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer retp); +void mono_arch_set_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info, gpointer retp); // When entering interp from native, this moves the arguments from the ccontext to the InterpFrame. If we have a return // vt address, we return it. This ret vt address needs to be passed to mono_arch_set_native_call_context_ret. -gpointer mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig); +gpointer mono_arch_get_native_call_context_args (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info); // After the pinvoke call is done, this moves return value from the ccontext to the InterpFrame. -void mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig); +void mono_arch_get_native_call_context_ret (CallContext *ccontext, gpointer frame, MonoMethodSignature *sig, gpointer call_info); +/* Free the structure returned by mono_arch_get_interp_native_call_info (NULL, sig) */ +void mono_arch_free_interp_native_call_info (gpointer call_info); #endif /*New interruption machinery */ From 71c23538bedc2a24c7566d97845340973b4f104a Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 4 Jul 2023 09:46:20 -0400 Subject: [PATCH 2/3] Fix indentation. --- src/mono/mono/mini/mini-amd64.c | 2 +- src/mono/mono/mini/mini-arm.c | 2 +- src/mono/mono/mini/mini-arm64.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mono/mono/mini/mini-amd64.c b/src/mono/mono/mini/mini-amd64.c index c17ce02064b3e..40a42383d2ea6 100644 --- a/src/mono/mono/mini/mini-amd64.c +++ b/src/mono/mono/mini/mini-amd64.c @@ -1190,7 +1190,7 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) gpointer mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) { - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = get_call_info (NULL, sig); if (mem_manager) { int size = call_info_size (sig); gpointer res = mono_mem_manager_alloc0 (mem_manager, size); diff --git a/src/mono/mono/mini/mini-arm.c b/src/mono/mono/mini/mini-arm.c index e0a583beaf1cf..49d696a60d76b 100644 --- a/src/mono/mono/mini/mini-arm.c +++ b/src/mono/mono/mini/mini-arm.c @@ -1678,7 +1678,7 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) gpointer mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) { - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = get_call_info (NULL, sig); if (mem_manager) { int size = call_info_size (sig); gpointer res = mono_mem_manager_alloc0 (mem_manager, size); diff --git a/src/mono/mono/mini/mini-arm64.c b/src/mono/mono/mini/mini-arm64.c index bb5691a74fceb..2ec8f530f8ce3 100644 --- a/src/mono/mono/mini/mini-arm64.c +++ b/src/mono/mono/mini/mini-arm64.c @@ -1837,7 +1837,7 @@ arg_set_val (CallContext *ccontext, ArgInfo *ainfo, gpointer src) gpointer mono_arch_get_interp_native_call_info (MonoMemoryManager *mem_manager, MonoMethodSignature *sig) { - CallInfo *cinfo = get_call_info (NULL, sig); + CallInfo *cinfo = get_call_info (NULL, sig); if (mem_manager) { int size = call_info_size (sig); gpointer res = mono_mem_manager_alloc0 (mem_manager, size); From 03ec16b42decce8e4ae34ced9db0f338285dbb01 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 4 Jul 2023 10:21:47 -0400 Subject: [PATCH 3/3] Fix the arm build. --- src/mono/mono/mini/mini-arm.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/mono/mono/mini/mini-arm.c b/src/mono/mono/mini/mini-arm.c index 49d696a60d76b..3d5d5223280e4 100644 --- a/src/mono/mono/mini/mini-arm.c +++ b/src/mono/mono/mini/mini-arm.c @@ -1275,11 +1275,10 @@ get_call_info (MonoMemPool *mp, MonoMethodSignature *sig) gboolean is_pinvoke = sig->pinvoke; gboolean vtype_retaddr = FALSE; - int size = call_info_size (sig); if (mp) - cinfo = mono_mempool_alloc0 (mp, size); + cinfo = mono_mempool_alloc0 (mp, call_info_size (sig)); else - cinfo = g_malloc0 (size); + cinfo = g_malloc0 (call_info_size (sig)); cinfo->nargs = n; gr = ARMREG_R0;