From dca72bdae59fd51d82bd1da66354e9a8ff5fc6a6 Mon Sep 17 00:00:00 2001 From: Vlad Brezae Date: Fri, 24 May 2024 18:26:31 +0300 Subject: [PATCH] [mono][interp] Fix type of args when inlining method (#102570) The vars allocated from pushing values on the execution stack might not reflect exactly the actual type of the var. Consider this pattern: condbr BB0 newobj Derived // push var0 of type Derived br BB1 BB0: newobj Base // push var1 of type Base // here we will end up inserting a `mov var1 -> var0` BB1: // top of stack will be seen as being var0 call Because we first reach BB1 with the stack contents of var0, BB1 will end up accessing top of the stack as var0. However the type of var0 at this point is not Derived, since it can also be a Base object. We currently don't update the type of var0, but just update the type information of the top of stack entry when entering BB1. When inlining, after this commit, we use the type information from the stack, rather than the type of the var present on the stack. In the future we might want to consider updating the type of var0 or creating a new var entirely with the correct type for consistency. --- src/mono/mono/mini/interp/transform.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/mini/interp/transform.c b/src/mono/mono/mini/interp/transform.c index a73656ad8f023..49bc8d1b45dd5 100644 --- a/src/mono/mono/mini/interp/transform.c +++ b/src/mono/mono/mini/interp/transform.c @@ -5172,7 +5172,7 @@ generate_code (TransformData *td, MonoMethod *method, MonoMethodHeader *header, arg_locals = (guint32*) g_malloc ((!!signature->hasthis + signature->param_count) * sizeof (guint32)); /* Allocate locals to store inlined method args from stack */ for (int i = signature->param_count - 1; i >= 0; i--) { - MonoType *type = td->vars [td->sp [-1].var].type; + MonoType *type = get_type_from_stack (td->sp [-1].type, td->sp [-1].klass); local = interp_create_var (td, type); arg_locals [i + !!signature->hasthis] = local; store_local (td, local);