Skip to content

Commit

Permalink
[interp] Fix pinvokes with HandleRef (#55404)
Browse files Browse the repository at this point in the history
The m2n wrapper marshals HandleRef structs from a vtype to intptr. The MonoMethod for a pinvoke method stores the unmarshalled signature. When locating the args on the stack during the pinvoke call we need to use the marshalled signature instead (which is saved in the code stream for the calli opcode)
  • Loading branch information
BrzVlad authored Jul 10, 2021
1 parent 65b472a commit 5e65771
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/mono/mono/mini/interp/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1173,12 +1173,16 @@ compute_arg_offset (MonoMethodSignature *sig, int index, int prev_offset)
}

static guint32*
initialize_arg_offsets (InterpMethod *imethod)
initialize_arg_offsets (InterpMethod *imethod, MonoMethodSignature *csig)
{
if (imethod->arg_offsets)
return imethod->arg_offsets;

MonoMethodSignature *sig = mono_method_signature_internal (imethod->method);
// For pinvokes, csig represents the real signature with marshalled args. If an explicit
// marshalled signature was not provided, we use the managed signature of the method.
MonoMethodSignature *sig = csig;
if (!sig)
sig = mono_method_signature_internal (imethod->method);
int arg_count = sig->hasthis + sig->param_count;
g_assert (arg_count);
guint32 *arg_offsets = (guint32*) g_malloc ((sig->hasthis + sig->param_count) * sizeof (int));
Expand All @@ -1201,13 +1205,13 @@ initialize_arg_offsets (InterpMethod *imethod)
}

static guint32
get_arg_offset_fast (InterpMethod *imethod, int index)
get_arg_offset_fast (InterpMethod *imethod, MonoMethodSignature *sig, int index)
{
guint32 *arg_offsets = imethod->arg_offsets;
if (arg_offsets)
return arg_offsets [index];

arg_offsets = initialize_arg_offsets (imethod);
arg_offsets = initialize_arg_offsets (imethod, sig);
g_assert (arg_offsets);
return arg_offsets [index];
}
Expand All @@ -1216,7 +1220,7 @@ static guint32
get_arg_offset (InterpMethod *imethod, MonoMethodSignature *sig, int index)
{
if (imethod) {
return get_arg_offset_fast (imethod, index);
return get_arg_offset_fast (imethod, sig, index);
} else {
g_assert (!sig->hasthis);
return compute_arg_offset (sig, index, -1);
Expand Down Expand Up @@ -2385,7 +2389,7 @@ do_jit_call (ThreadContext *context, stackval *ret_sp, stackval *sp, InterpFrame
if (cinfo->ret_mt != -1)
args [pindex ++] = ret_sp;
for (int i = 0; i < rmethod->param_count; ++i) {
stackval *sval = STACK_ADD_BYTES (sp, get_arg_offset_fast (rmethod, stack_index + i));
stackval *sval = STACK_ADD_BYTES (sp, get_arg_offset_fast (rmethod, NULL, stack_index + i));
if (cinfo->arginfo [i] == JIT_ARG_BYVAL)
args [pindex ++] = sval->data.p;
else
Expand Down Expand Up @@ -7199,7 +7203,7 @@ interp_frame_get_arg (MonoInterpFrameHandle frame, int pos)

g_assert (iframe->imethod);

return (char*)iframe->stack + get_arg_offset_fast (iframe->imethod, pos + iframe->imethod->hasthis);
return (char*)iframe->stack + get_arg_offset_fast (iframe->imethod, NULL, pos + iframe->imethod->hasthis);
}

static gpointer
Expand Down

0 comments on commit 5e65771

Please sign in to comment.