Skip to content

Commit

Permalink
[interp] Fix GetType called on ptr constrained to Nullable`
Browse files Browse the repository at this point in the history
We were statically optimizing this call to return the actual constrained class type, which is incorrect for nullables, because boxing of a nullable (as part of the constrained call) actually creates an object with the type of the nullable's value (or null if there is no value).
  • Loading branch information
BrzVlad committed Nov 1, 2021
1 parent 9e795c0 commit 807b896
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/mono/mono/mini/interp/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,7 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas
if (!strcmp (tm, "InternalGetHashCode")) {
*op = MINT_INTRINS_GET_HASHCODE;
} else if (!strcmp (tm, "GetType")) {
if (constrained_class && m_class_is_valuetype (constrained_class)) {
if (constrained_class && m_class_is_valuetype (constrained_class) && !mono_class_is_nullable (constrained_class)) {
// If constrained_class is valuetype we already know its type.
// Resolve GetType to a constant so we can fold type comparisons
ERROR_DECL(error);
Expand All @@ -2457,8 +2457,16 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas
return TRUE;
} else {
if (constrained_class) {
// deref the managed pointer to get the object
interp_add_ins (td, MINT_LDIND_I);
if (mono_class_is_nullable (constrained_class)) {
// We can't determine the behavior here statically because we don't know if the
// nullable vt has a value or not. If it has a value, the result type is
// m_class_get_cast_class (constrained_class), otherwise GetType should throw NRE.
interp_add_ins (td, MINT_BOX_NULLABLE_PTR);
td->last_ins->data [0] = get_data_item_index (td, constrained_class);
} else {
// deref the managed pointer to get the object
interp_add_ins (td, MINT_LDIND_I);
}
td->sp--;
interp_ins_set_sreg (td->last_ins, td->sp [0].local);
push_simple_type (td, STACK_TYPE_O);
Expand Down

0 comments on commit 807b896

Please sign in to comment.