Skip to content

Commit ae0ce29

Browse files
committed
[mono] Fix more memory leaks in aot+interp mode.
1 parent 254263f commit ae0ce29

File tree

6 files changed

+68
-35
lines changed

6 files changed

+68
-35
lines changed

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

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,8 @@ mono_interp_get_imethod (MonoMethod *method)
488488

489489
sig = mono_method_signature_internal (method);
490490

491-
MonoMemPool *dyn_mp = NULL;
492491
if (method->dynamic)
493-
// FIXME: Use this in more places
494-
dyn_mp = mono_mempool_new_size (sizeof (InterpMethod));
495-
496-
if (dyn_mp)
497-
imethod = (InterpMethod*)mono_mempool_alloc0 (dyn_mp, sizeof (InterpMethod));
492+
imethod = (InterpMethod*)mono_dyn_method_alloc0 (method, sizeof (InterpMethod));
498493
else
499494
imethod = (InterpMethod*)m_method_alloc0 (method, sizeof (InterpMethod));
500495
imethod->method = method;
@@ -512,8 +507,8 @@ mono_interp_get_imethod (MonoMethod *method)
512507
imethod->rtype = m_class_get_byval_arg (mono_defaults.string_class);
513508
else
514509
imethod->rtype = mini_get_underlying_type (sig->ret);
515-
if (dyn_mp)
516-
imethod->param_types = (MonoType**)mono_mempool_alloc0 (dyn_mp, sizeof (MonoType*) * sig->param_count);
510+
if (method->dynamic)
511+
imethod->param_types = (MonoType**)mono_dyn_method_alloc0 (method, sizeof (MonoType*) * sig->param_count);
517512
else
518513
imethod->param_types = (MonoType**)m_method_alloc0 (method, sizeof (MonoType*) * sig->param_count);
519514
for (i = 0; i < sig->param_count; ++i)
@@ -523,11 +518,7 @@ mono_interp_get_imethod (MonoMethod *method)
523518
InterpMethod *old_imethod;
524519
if (!((old_imethod = mono_internal_hash_table_lookup (&jit_mm->interp_code_hash, method)))) {
525520
mono_internal_hash_table_insert (&jit_mm->interp_code_hash, method, imethod);
526-
if (method->dynamic)
527-
((MonoDynamicMethod*)method)->mp = dyn_mp;
528521
} else {
529-
if (dyn_mp)
530-
mono_mempool_destroy (dyn_mp);
531522
imethod = old_imethod; /* leak the newly allocated InterpMethod to the mempool */
532523
}
533524
jit_mm_unlock (jit_mm);
@@ -1284,17 +1275,12 @@ compute_arg_offset (MonoMethodSignature *sig, int index)
12841275
}
12851276

12861277
static gpointer
1287-
imethod_alloc0 (InterpMethod *imethod, size_t size)
1288-
{
1289-
if (imethod->method->dynamic) {
1290-
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();
1291-
jit_mm_lock (jit_mm);
1292-
gpointer ret = mono_mempool_alloc0 (((MonoDynamicMethod*)imethod->method)->mp, (guint)size);
1293-
jit_mm_unlock (jit_mm);
1294-
return ret;
1295-
} else {
1296-
return m_method_alloc0 (imethod->method, (guint)size);
1297-
}
1278+
imethod_alloc0 (InterpMethod *imethod, guint size)
1279+
{
1280+
if (imethod->method->dynamic)
1281+
return mono_dyn_method_alloc0 (imethod->method, size);
1282+
else
1283+
return m_method_alloc0 (imethod->method, size);
12981284
}
12991285

13001286
static guint32*

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,15 +1290,10 @@ interp_get_icall_sig (MonoMethodSignature *sig);
12901290
static gpointer
12911291
imethod_alloc0 (TransformData *td, size_t size)
12921292
{
1293-
if (td->rtm->method->dynamic) {
1294-
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();
1295-
jit_mm_lock (jit_mm);
1296-
gpointer ret = mono_mempool_alloc0 (((MonoDynamicMethod*)td->rtm->method)->mp, (guint)size);
1297-
jit_mm_unlock (jit_mm);
1298-
return ret;
1299-
} else {
1293+
if (td->rtm->method->dynamic)
1294+
return mono_dyn_method_alloc0 (td->rtm->method, (guint)size);
1295+
else
13001296
return mono_mem_manager_alloc0 (td->mem_manager, (guint)size);
1301-
}
13021297
}
13031298

13041299
static void

src/mono/mono/mini/llvmonly-runtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ mini_llvmonly_create_ftndesc (MonoMethod *m, gpointer addr, gpointer arg)
118118
{
119119
MonoFtnDesc *ftndesc;
120120

121-
if (m->dynamic && ((MonoDynamicMethod*)m)->mp)
122-
ftndesc = mono_mempool_alloc0 (((MonoDynamicMethod*)m)->mp, sizeof (MonoFtnDesc));
121+
if (m->dynamic)
122+
ftndesc = mono_dyn_method_alloc0 (m, sizeof (MonoFtnDesc));
123123
else
124124
ftndesc = (MonoFtnDesc*)m_method_alloc0 (m, sizeof (MonoFtnDesc));
125125
ftndesc->addr = addr;

src/mono/mono/mini/mini-runtime.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,6 +2950,21 @@ invalidated_delegate_trampoline (char *desc)
29502950
}
29512951
#endif
29522952

2953+
void *
2954+
mono_dyn_method_alloc0 (MonoMethod *method, guint size)
2955+
{
2956+
MonoDynamicMethod *dmethod = (MonoDynamicMethod*)method;
2957+
2958+
g_assert (method->dynamic);
2959+
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();
2960+
jit_mm_lock (jit_mm);
2961+
if (!dmethod->mp)
2962+
dmethod->mp = mono_mempool_new_size (128);
2963+
gpointer ret = mono_mempool_alloc0 (dmethod->mp, size);
2964+
jit_mm_unlock (jit_mm);
2965+
return ret;
2966+
}
2967+
29532968
/*
29542969
* mono_jit_free_method:
29552970
*
@@ -2969,6 +2984,21 @@ mono_jit_free_method (MonoMethod *method)
29692984
if (mono_use_interpreter)
29702985
mini_get_interp_callbacks ()->free_method (method);
29712986

2987+
jit_mm = jit_mm_for_method (method);
2988+
2989+
jit_mm_lock (jit_mm);
2990+
if (jit_mm->dyn_delegate_info_hash) {
2991+
GSList *pairs = g_hash_table_lookup (jit_mm->dyn_delegate_info_hash, method);
2992+
for (GSList *l = pairs; l; l = l->next) {
2993+
MonoDelegateClassMethodPair *dpair = (MonoDelegateClassMethodPair *)l->data;
2994+
g_assert (dpair->method == method);
2995+
g_hash_table_remove (jit_mm->delegate_info_hash, dpair);
2996+
}
2997+
g_slist_free (pairs);
2998+
g_hash_table_remove (jit_mm->dyn_delegate_info_hash, method);
2999+
}
3000+
jit_mm_unlock (jit_mm);
3001+
29723002
ji = mono_dynamic_code_hash_lookup (method);
29733003
if (!ji)
29743004
return;
@@ -4399,6 +4429,8 @@ free_jit_mem_manager (MonoMemoryManager *mem_manager)
43994429
g_hash_table_destroy (info->jump_trampoline_hash);
44004430
g_hash_table_destroy (info->jit_trampoline_hash);
44014431
g_hash_table_destroy (info->delegate_info_hash);
4432+
if (info->dyn_delegate_info_hash)
4433+
g_hash_table_destroy (info->dyn_delegate_info_hash);
44024434
g_hash_table_destroy (info->static_rgctx_trampoline_hash);
44034435
g_hash_table_destroy (info->mrgctx_hash);
44044436
g_hash_table_destroy (info->interp_method_pointer_hash);

src/mono/mono/mini/mini-runtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct {
2828
GHashTable *jump_trampoline_hash;
2929
GHashTable *jit_trampoline_hash;
3030
GHashTable *delegate_info_hash;
31+
GHashTable *dyn_delegate_info_hash;
3132
/* Maps ClassMethodPair -> MonoDelegateTrampInfo */
3233
GHashTable *static_rgctx_trampoline_hash;
3334
/* maps MonoMethod -> MonoJitDynamicMethodInfo */
@@ -620,6 +621,7 @@ void mono_jit_dump_cleanup (void);
620621
gpointer mini_alloc_generic_virtual_trampoline (MonoVTable *vtable, int size);
621622
MonoException* mini_get_stack_overflow_ex (void);
622623
MonoJitInfo* mini_alloc_jinfo (MonoJitMemoryManager *jit_mm, int size);
624+
void* mono_dyn_method_alloc0 (MonoMethod *method, guint size);
623625

624626
/*
625627
* Per-OS implementation functions.

src/mono/mono/mini/mini-trampolines.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,11 @@ mono_create_delegate_trampoline_info (MonoClass *klass, MonoMethod *method, gboo
14251425
invoke = mono_get_delegate_invoke_internal (klass);
14261426
g_assert (invoke);
14271427

1428-
tramp_info = (MonoDelegateTrampInfo *)mono_mem_manager_alloc0 (jit_mm->mem_manager, sizeof (MonoDelegateTrampInfo));
1428+
if (method && method->dynamic)
1429+
// FIXME: Can the dynamic method outlive the delegate class if its unloadable ?
1430+
tramp_info = (MonoDelegateTrampInfo *)mono_dyn_method_alloc0 (method, sizeof (MonoDelegateTrampInfo));
1431+
else
1432+
tramp_info = (MonoDelegateTrampInfo *)mono_mem_manager_alloc0 (jit_mm->mem_manager, sizeof (MonoDelegateTrampInfo));
14291433
tramp_info->klass = klass;
14301434
tramp_info->invoke = invoke;
14311435
tramp_info->invoke_sig = mono_method_signature_internal (invoke);
@@ -1456,14 +1460,28 @@ mono_create_delegate_trampoline_info (MonoClass *klass, MonoMethod *method, gboo
14561460
}
14571461
#endif
14581462

1459-
dpair = (MonoDelegateClassMethodPair *)mono_mem_manager_alloc0 (jit_mm->mem_manager, sizeof (MonoDelegateClassMethodPair));
1463+
if (method && method->dynamic)
1464+
dpair = (MonoDelegateClassMethodPair *)mono_dyn_method_alloc0 (method, sizeof (MonoDelegateClassMethodPair));
1465+
else
1466+
dpair = (MonoDelegateClassMethodPair *)mono_mem_manager_alloc0 (jit_mm->mem_manager, sizeof (MonoDelegateClassMethodPair));
14601467
memcpy (dpair, &pair, sizeof (MonoDelegateClassMethodPair));
14611468

14621469
/* store trampoline address */
14631470
jit_mm_lock (jit_mm);
14641471
g_hash_table_insert (jit_mm->delegate_info_hash, dpair, tramp_info);
14651472
jit_mm_unlock (jit_mm);
14661473

1474+
if (method && method->dynamic) {
1475+
jit_mm = jit_mm_for_method (method);
1476+
jit_mm_lock (jit_mm);
1477+
if (!jit_mm->dyn_delegate_info_hash)
1478+
jit_mm->dyn_delegate_info_hash = g_hash_table_new (NULL, NULL);
1479+
GSList *l = g_hash_table_lookup (jit_mm->dyn_delegate_info_hash, method);
1480+
l = g_slist_prepend (l, dpair);
1481+
g_hash_table_insert (jit_mm->dyn_delegate_info_hash, method, l);
1482+
jit_mm_unlock (jit_mm);
1483+
}
1484+
14671485
return tramp_info;
14681486
}
14691487

0 commit comments

Comments
 (0)