Skip to content
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

[mono][aot] Type load checks do not fail at compile time but produce a runtime exception #91261

Merged
merged 21 commits into from
Sep 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
62 changes: 48 additions & 14 deletions src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -6193,6 +6193,49 @@ emit_llvmonly_interp_entry (MonoCompile *cfg, MonoMethodHeader *header)
link_bblock (cfg, cfg->cbb, cfg->bb_exit);
}

static void
method_make_alwaysthrow_typeloadfailure (MonoCompile* cfg, MonoClass* klass)
{
// Get rid of all out-BBs from the entry BB. (all but init BB)
for (gint16 i = cfg->bb_entry->out_count - 1; i >= 0; i--) {
if (cfg->bb_entry->out_bb [i] != cfg->bb_init) {
mono_unlink_bblock (cfg, cfg->bb_entry, cfg->bb_entry->out_bb [i]);
mono_remove_bblock (cfg, cfg->bb_entry->out_bb [i]);
}
}

// Discard all out-BBs from the init BB.
for (gint16 i = cfg->bb_init->out_count - 1; i >= 0; i--) {
if (cfg->bb_init->out_bb [i] != cfg->bb_exit) {
mono_unlink_bblock (cfg, cfg->bb_init, cfg->bb_init->out_bb [i]);
mono_remove_bblock (cfg, cfg->bb_init->out_bb [i]);
}
}

// Maintain linked list consistency. This BB should have been added as the last,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to remove dead blocks between the bb_init and bb_exit which may not have a direct link to them?

Copy link
Member Author

@jandupej jandupej Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly not; it seems that unreachable blocks are detected (and removed) without my explicitly doing it. This is the first iteration that seems to work locally, waiting for CI. I may be able to simplify it if CI turns out favorable.

EDIT: Are you aware of any unlinked blocks that should be kept?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks good to me. I was just curious whether the unreachable blocks are detected and removed.

Is it possible to skip the bb_init and link the bb_entry with the throw block?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had control flow issues when generating LLVM IR without the init block (which was empty no less). I'll see if it can be done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is to restart the compilation, there is already a mechanism to do that if llvm fails, etc. and emit the exception without compiling the rest of the IL.

// ignoring the ones that held actual method code.
cfg->cbb = cfg->bb_init;

// Create a new BB that only throws, link it after the entry.
MonoBasicBlock* bb;
NEW_BBLOCK (cfg, bb);
bb->cil_code = NULL;
bb->cil_length = 0;
cfg->cbb->next_bb = bb;
cfg->cbb = bb;

emit_type_load_failure (cfg, klass);
MonoInst* ins;
MONO_INST_NEW (cfg, ins, OP_NOT_REACHED);
MONO_ADD_INS (cfg->cbb, ins);

ADD_BBLOCK (cfg, bb);
mono_link_bblock (cfg, cfg->bb_init, bb);
mono_link_bblock (cfg, bb, cfg->bb_exit);

cfg->disable_inline = TRUE;
}

typedef union _MonoOpcodeParameter {
gint32 i32;
gint64 i64;
Expand Down Expand Up @@ -9939,19 +9982,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
EMIT_NEW_PCONST (cfg, *sp, NULL);
jandupej marked this conversation as resolved.
Show resolved Hide resolved
sp++;
} else if (il_op == MONO_CEE_LDFLD || il_op == MONO_CEE_LDSFLD) {
// An object is expected. Make best effort to find its type and push that. If that
// attempt fails, push a pointer to balance the stack.
mono_class_init_internal (klass);
field = mono_class_get_field (klass, token);

if (field == NULL) {
// FIXME: this may be incorrect. Either find the correct type to push or skip the rest of BB.
EMIT_NEW_PCONST (cfg, *sp, NULL);
sp++;
} else {
ftype = mono_field_get_type_internal (field);
goto fld_emit_stage;
}
// An object is expected here. It may be impossible to correctly infer its type,
// we turn this entire method into a throw.
method_make_alwaysthrow_typeloadfailure (cfg, klass);
goto all_bbs_done;
}

break;
Expand Down Expand Up @@ -10349,7 +10383,6 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
if (!addr)
addr = mono_static_field_get_addr (vtable, field);

fld_emit_stage:
ro_type = ftype->type;
if (ro_type == MONO_TYPE_VALUETYPE && m_class_is_enumtype (ftype->data.klass)) {
ro_type = mono_class_enum_basetype_internal (ftype->data.klass)->type;
Expand Down Expand Up @@ -12079,6 +12112,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
}
if (start_new_bblock != 1)
UNVERIFIED;
all_bbs_done:

cfg->cbb->cil_length = GPTRDIFF_TO_INT32 (ip - cfg->cbb->cil_code);
if (cfg->cbb->next_bb) {
Expand Down