Skip to content

Commit

Permalink
[release/8.0-staging] [mono][interp] Fix incorrect stack type informa…
Browse files Browse the repository at this point in the history
…tion (#94966)

* [mono][interp] Don't link bblock after rethrow

* [mono][interp] Small code refactoring

* [mono][interp] Fix incorrect stack type information

Assume we have a basic block that it is a forward branch destination, then its stack type information will be initialized at the moment of branching (let's say there is a value of type Obj1). If later in the code we reach this bblock by falling through (let's say the current stack contains a value of Obj2), the current stack state will be copied from the original state, with the type Obj1. If later on we do a virtual call, we will try to devirtualize it as if this is Obj1 which is incorrect, since the fallthrough path would produce an Obj2.

This commit adds missing checks for removing type information if we have different types on the execution types on incoming paths.

---------

Co-authored-by: Vlad Brezae <brezaevlad@gmail.com>
  • Loading branch information
github-actions[bot] and BrzVlad committed Nov 21, 2023
1 parent 9b0f22f commit 3e1f672
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/mono/mono/mini/interp/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -788,28 +788,32 @@ fixup_newbb_stack_locals (TransformData *td, InterpBasicBlock *newbb)
}
}

static void
merge_stack_type_information (StackInfo *state1, StackInfo *state2, int len)
{
// Discard type information if we have type conflicts for stack contents
for (int i = 0; i < len; i++) {
if (state1 [i].klass != state2 [i].klass) {
state1 [i].klass = NULL;
state2 [i].klass = NULL;
}
}
}

// Initializes stack state at entry to bb, based on the current stack state
static void
init_bb_stack_state (TransformData *td, InterpBasicBlock *bb)
{
// FIXME If already initialized, then we need to generate mov to the registers in the state.
// Check if already initialized
if (bb->stack_height >= 0) {
// Discard type information if we have type conflicts for stack contents
for (int i = 0; i < bb->stack_height; i++) {
if (bb->stack_state [i].klass != td->stack [i].klass) {
bb->stack_state [i].klass = NULL;
td->stack [i].klass = NULL;
}
merge_stack_type_information (td->stack, bb->stack_state, bb->stack_height);
} else {
bb->stack_height = GPTRDIFF_TO_INT (td->sp - td->stack);
if (bb->stack_height > 0) {
int size = bb->stack_height * sizeof (td->stack [0]);
bb->stack_state = (StackInfo*)mono_mempool_alloc (td->mempool, size);
memcpy (bb->stack_state, td->stack, size);
}
return;
}

bb->stack_height = GPTRDIFF_TO_INT (td->sp - td->stack);
if (bb->stack_height > 0) {
int size = bb->stack_height * sizeof (td->stack [0]);
bb->stack_state = (StackInfo*)mono_mempool_alloc (td->mempool, size);
memcpy (bb->stack_state, td->stack, size);
}
}

Expand Down Expand Up @@ -5010,8 +5014,12 @@ generate_code (TransformData *td, MonoMethod *method, MonoMethodHeader *header,
td->cbb = new_bb;

if (new_bb->stack_height >= 0) {
if (new_bb->stack_height > 0)
if (new_bb->stack_height > 0) {
if (link_bblocks)
merge_stack_type_information (td->stack, new_bb->stack_state, new_bb->stack_height);
// This is relevant only for copying the vars associated with the values on the stack
memcpy (td->stack, new_bb->stack_state, new_bb->stack_height * sizeof(td->stack [0]));
}
td->sp = td->stack + new_bb->stack_height;
} else if (link_bblocks) {
/* This bblock is not branched to. Initialize its stack state */
Expand Down Expand Up @@ -7550,6 +7558,7 @@ generate_code (TransformData *td, MonoMethod *method, MonoMethodHeader *header,
interp_ins_set_sreg (td->last_ins, td->sp [0].local);
td->sp = td->stack;
++td->ip;
link_bblocks = FALSE;
break;

case CEE_MONO_LD_DELEGATE_METHOD_PTR:
Expand Down

0 comments on commit 3e1f672

Please sign in to comment.