Skip to content

[release/8.0-staging] [mono][interp] Fix incorrect stack type information #94966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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