Skip to content

Commit

Permalink
[2019-12] [jit] Compute the instance size/alignment correctly for gsh…
Browse files Browse the repository at this point in the history
…ared types whose constraint is a generic valuetype. (mono#18529)

* [jit] Compute the instance size/alignment correctly for gshared types whose constraint is a generic valuetype.

Fixes mono#18455.

* [jit] Simplify code

Fix some line damage

Co-authored-by: Zoltan Varga <[email protected]>
Co-authored-by: Vlad Brezae <[email protected]>
  • Loading branch information
3 people committed Jan 21, 2020
1 parent 2b0e9bf commit 2edccc5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
9 changes: 4 additions & 5 deletions mono/metadata/class-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ static MonoClass*
make_generic_param_class (MonoGenericParam *param)
{
MonoClass *klass, **ptr;
int count, pos, i;
int count, pos, i, min_align;
MonoGenericParamInfo *pinfo = mono_generic_param_info (param);
MonoGenericContainer *container = mono_generic_param_owner (param);
g_assert_checked (container);
Expand Down Expand Up @@ -1260,13 +1260,12 @@ make_generic_param_class (MonoGenericParam *param)
/* We don't use type_token for VAR since only classes can use it (not arrays, pointer, VARs, etc) */
klass->sizes.generic_param_token = !is_anonymous ? pinfo->token : 0;

/*Init these fields to sane values*/
klass->min_align = 1;
/*
* This makes sure the the value size of this class is equal to the size of the types the gparam is
* constrained to, the JIT depends on this.
*/
klass->instance_size = MONO_ABI_SIZEOF (MonoObject) + mono_type_stack_size_internal (m_class_get_byval_arg (klass), NULL, TRUE);
klass->instance_size = MONO_ABI_SIZEOF (MonoObject) + mono_type_size (m_class_get_byval_arg (klass), &min_align);
klass->min_align = min_align;
mono_memory_barrier ();
klass->size_inited = 1;

Expand Down Expand Up @@ -3904,7 +3903,7 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_

MonoType *klass_byval_arg = m_class_get_byval_arg (klass);
if (klass_byval_arg->type == MONO_TYPE_VAR || klass_byval_arg->type == MONO_TYPE_MVAR)
instance_size = MONO_ABI_SIZEOF (MonoObject) + mono_type_stack_size_internal (klass_byval_arg, NULL, TRUE);
instance_size = MONO_ABI_SIZEOF (MonoObject) + mono_type_size (klass_byval_arg, &min_align);
else if (klass_byval_arg->type == MONO_TYPE_PTR)
instance_size = MONO_ABI_SIZEOF (MonoObject) + MONO_ABI_SIZEOF (gpointer);

Expand Down
21 changes: 21 additions & 0 deletions mono/mini/gshared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,27 @@ public static int test_0_gsharedvt_out_dim () {
return (c.Foo () == "abcd") ? 0 : 1;
}
#endif

class KvpList<T> {
public T[] array = new T[4];
int size = 0;

[MethodImpl(MethodImplOptions.NoInlining)]
public void MyAdd(T item) {
if (size < (uint)array.Length) {
array [size] = item;
size++;
}
}
}

public static int test_0_regress_18455 () {
var p = new KvpList<KeyValuePair<int, KeyValuePair<int,int>>> ();
KeyValuePair<int, KeyValuePair<int,int>> kvp = new KeyValuePair<int, KeyValuePair<int,int>> (3, new KeyValuePair<int,int> (1, 2));

p.MyAdd (kvp);
return p.array [1].Key == 0 ? 0 : 1;
}
}

// #13191
Expand Down

0 comments on commit 2edccc5

Please sign in to comment.