Skip to content

Commit

Permalink
[mono] Set the 'dynamic' flag on method builders on creation so a Mon…
Browse files Browse the repository at this point in the history
…oDynamicMethod is allocated instead of a MonoMethodWrapper.
  • Loading branch information
vargaz committed Jul 17, 2023
1 parent 90b85ea commit b7a4ea2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
7 changes: 0 additions & 7 deletions src/mono/mono/metadata/marshal-lightweight.c
Original file line number Diff line number Diff line change
Expand Up @@ -2185,12 +2185,6 @@ mb_skip_visibility_ilgen (MonoMethodBuilder *mb)
mb->skip_visibility = 1;
}

static void
mb_set_dynamic_ilgen (MonoMethodBuilder *mb)
{
mb->dynamic = 1;
}

static void
emit_synchronized_wrapper_ilgen (MonoMethodBuilder *mb, MonoMethod *method, MonoGenericContext *ctx, MonoGenericContainer *container, MonoMethod *enter_method, MonoMethod *exit_method, MonoMethod *gettypefromhandle_method)
{
Expand Down Expand Up @@ -3392,7 +3386,6 @@ mono_marshal_lightweight_init (void)
cb.emit_return = emit_return_ilgen;
cb.emit_vtfixup_ftnptr = emit_vtfixup_ftnptr_ilgen;
cb.mb_skip_visibility = mb_skip_visibility_ilgen;
cb.mb_set_dynamic = mb_set_dynamic_ilgen;
cb.mb_emit_exception = mb_emit_exception_ilgen;
cb.mb_emit_exception_for_error = mb_emit_exception_for_error_ilgen;
cb.mb_emit_byte = mb_emit_byte_ilgen;
Expand Down
12 changes: 6 additions & 6 deletions src/mono/mono/metadata/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4073,7 +4073,10 @@ marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, Mono

sig = mono_method_signature_internal (method);

mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
if (target_handle)
mb = mono_mb_new_dynamic (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
else
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);

/*the target gchandle must be the first entry after size and the wrapper itself.*/
mono_mb_add_data (mb, target_handle);
Expand Down Expand Up @@ -4181,7 +4184,6 @@ marshal_get_managed_wrapper (MonoMethod *method, MonoClass *delegate_klass, Mono
mb, csig, sig->param_count + 16,
info, NULL);
} else {
get_marshal_cb ()->mb_set_dynamic (mb);
res = mono_mb_create (mb, csig, sig->param_count + 16, NULL);
}
}
Expand Down Expand Up @@ -4254,7 +4256,7 @@ mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type)
mspecs = g_new0 (MonoMarshalSpec*, sig->param_count + 1);
mono_method_get_marshal_info (method, mspecs);

mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
mb = mono_mb_new_dynamic (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
csig = mono_metadata_signature_dup_full (image, sig);
csig->hasthis = 0;
csig->pinvoke = 1;
Expand All @@ -4277,7 +4279,6 @@ mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type)
mono_marshal_emit_managed_wrapper (mb, sig, mspecs, &m, method, 0, error);
mono_error_assert_ok (error);

get_marshal_cb ()->mb_set_dynamic (mb);
method = mono_mb_create (mb, csig, sig->param_count + 16, NULL);
mono_mb_free (mb);

Expand All @@ -4292,11 +4293,10 @@ mono_marshal_get_vtfixup_ftnptr (MonoImage *image, guint32 token, guint16 type)
}

sig = mono_method_signature_internal (method);
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_MANAGED);
mb = mono_mb_new_dynamic (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_MANAGED);

param_count = sig->param_count + sig->hasthis;
get_marshal_cb ()->emit_vtfixup_ftnptr (mb, method, param_count, type);
get_marshal_cb ()->mb_set_dynamic (mb);

method = mono_mb_create (mb, sig, param_count, NULL);
mono_mb_free (mb);
Expand Down
1 change: 0 additions & 1 deletion src/mono/mono/metadata/marshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ typedef struct {
void (*emit_return) (MonoMethodBuilder *mb);
void (*emit_vtfixup_ftnptr) (MonoMethodBuilder *mb, MonoMethod *method, int param_count, guint16 type);
void (*mb_skip_visibility) (MonoMethodBuilder *mb);
void (*mb_set_dynamic) (MonoMethodBuilder *mb);
void (*mb_emit_exception) (MonoMethodBuilder *mb, const char *exc_nspace, const char *exc_name, const char *msg);
void (*mb_emit_exception_for_error) (MonoMethodBuilder *mb, const MonoError *emitted_error);
void (*mb_emit_byte) (MonoMethodBuilder *mb, guint8 op);
Expand Down
10 changes: 7 additions & 3 deletions src/mono/mono/metadata/method-builder-ilgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum {
#undef OPDEF

static MonoMethodBuilder *
new_base_ilgen (MonoClass *klass, MonoWrapperType type)
new_base_ilgen (MonoClass *klass, MonoWrapperType type, gboolean dynamic)
{
MonoMethodBuilder *mb;
MonoMethod *m;
Expand All @@ -38,7 +38,10 @@ new_base_ilgen (MonoClass *klass, MonoWrapperType type)

mb = g_new0 (MonoMethodBuilder, 1);

mb->method = m = (MonoMethod *)g_new0 (MonoMethodWrapper, 1);
if (dynamic)
mb->method = m = (MonoMethod *)g_new0 (MonoDynamicMethod, 1);
else
mb->method = m = (MonoMethod *)g_new0 (MonoMethodWrapper, 1);

m->klass = klass;
m->inline_info = 1;
Expand All @@ -47,6 +50,7 @@ new_base_ilgen (MonoClass *klass, MonoWrapperType type)
mb->code_size = 40;
mb->code = (unsigned char *)g_malloc (mb->code_size);
mb->init_locals = TRUE;
mb->dynamic = dynamic;

/* placeholder for the wrapper always at index 1 */
mono_mb_add_data (mb, NULL);
Expand Down Expand Up @@ -109,7 +113,7 @@ create_method_ilgen (MonoMethodBuilder *mb, MonoMethodSignature *signature, int
image = m_class_get_image (mb->method->klass);

if (mb->dynamic) {
/* Allocated in reflection_methodbuilder_to_mono_method () */
/* Allocated in reflection_methodbuilder_to_mono_method ()/mb_new () */
method = mb->method;
} else {
method = (MonoMethod *)mb_alloc0 (mb, sizeof (MonoMethodWrapper));
Expand Down
12 changes: 10 additions & 2 deletions src/mono/mono/metadata/method-builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ get_mb_cb (void)
MonoMethodBuilder *
mono_mb_new_no_dup_name (MonoClass *klass, const char *name, MonoWrapperType type)
{
MonoMethodBuilder *mb = get_mb_cb ()->new_base (klass, type);
MonoMethodBuilder *mb = get_mb_cb ()->new_base (klass, type, FALSE);
mb->name = (char*)name;
mb->no_dup_name = TRUE;
return mb;
Expand All @@ -89,7 +89,15 @@ mono_mb_new_no_dup_name (MonoClass *klass, const char *name, MonoWrapperType typ
MonoMethodBuilder *
mono_mb_new (MonoClass *klass, const char *name, MonoWrapperType type)
{
MonoMethodBuilder *mb = get_mb_cb ()->new_base (klass, type);
MonoMethodBuilder *mb = get_mb_cb ()->new_base (klass, type, FALSE);
mb->name = g_strdup (name);
return mb;
}

MonoMethodBuilder *
mono_mb_new_dynamic (MonoClass *klass, const char *name, MonoWrapperType type)
{
MonoMethodBuilder *mb = get_mb_cb ()->new_base (klass, type, TRUE);
mb->name = g_strdup (name);
return mb;
}
Expand Down
5 changes: 4 additions & 1 deletion src/mono/mono/metadata/method-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ typedef struct _MonoMethodBuilder MonoMethodBuilder;

typedef struct {
int version;
MonoMethodBuilder* (*new_base) (MonoClass *klass, MonoWrapperType type);
MonoMethodBuilder* (*new_base) (MonoClass *klass, MonoWrapperType type, gboolean dynamic);
void (*free) (MonoMethodBuilder *mb);
MonoMethod* (*create_method) (MonoMethodBuilder *mb, MonoMethodSignature *signature, int max_stack);
} MonoMethodBuilderCallbacks;

MONO_COMPONENT_API MonoMethodBuilder *
mono_mb_new (MonoClass *klass, const char *name, MonoWrapperType type);

MONO_COMPONENT_API MonoMethodBuilder *
mono_mb_new_dynamic (MonoClass *klass, const char *name, MonoWrapperType type);

MonoMethodBuilder *
mono_mb_new_no_dup_name (MonoClass *klass, const char *name, MonoWrapperType type);

Expand Down

0 comments on commit b7a4ea2

Please sign in to comment.