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
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions src/mono/mono/mini/jit-icalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -1689,11 +1689,14 @@ mono_throw_invalid_program (const char *msg)
}

void
mono_throw_type_load (const char* msg)
mono_throw_type_load (MonoClass* klass)
{
ERROR_DECL (error);
mono_error_set_generic_error (error, "System", "TypeLoadException", "Attempting to load invalid type '%s'.", msg);
char* klass_name = mono_type_get_full_name (klass);
mono_error_set_type_load_class (error, klass, "Attempting to load invalid type '%s'.", klass_name);
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
mono_error_set_pending_exception (error);

g_free (klass_name);
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/mini/jit-icalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ ICALL_EXPORT void mono_throw_platform_not_supported (void);

ICALL_EXPORT void mono_throw_invalid_program (const char *msg);

ICALL_EXPORT void mono_throw_type_load (const char* msg);
ICALL_EXPORT void mono_throw_type_load (MonoClass* klass);

ICALL_EXPORT void mono_dummy_jit_icall (void);

Expand Down
72 changes: 21 additions & 51 deletions src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -2306,14 +2306,12 @@ emit_not_supported_failure (MonoCompile *cfg)
static void
emit_type_load_failure (MonoCompile* cfg, MonoClass* klass)
{
char* class_name;
if (G_UNLIKELY (!klass))
class_name = mono_mem_manager_strdup (cfg->mem_manager, "Unknown class" );
else
class_name = mono_mem_manager_strdup (cfg->mem_manager, mono_class_full_name (klass));
MonoClass* failed_class = klass;
if (G_UNLIKELY (!failed_class))
failed_class = mono_defaults.object_class;

MonoInst* iargs[1];
EMIT_NEW_LDSTRLITCONST (cfg, iargs [0], class_name);
EMIT_NEW_CLASSCONST (cfg, iargs [0], failed_class);
mono_emit_jit_icall (cfg, mono_throw_type_load, iargs);
}

Expand Down Expand Up @@ -4979,8 +4977,12 @@ inline_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig,

#define CLEAR_TYPELOAD_EXCEPTION(cfg) if (cfg->exception_type == MONO_EXCEPTION_TYPE_LOAD) { clear_cfg_error (cfg); cfg->exception_type = MONO_EXCEPTION_NONE; }
#define IF_TYPELOAD_ERROR(klass) if (!(klass) || mono_class_has_failure (klass))
jandupej marked this conversation as resolved.
Show resolved Hide resolved
#define HANDLE_TYPELOAD_ERROR(cfg,klass) if (!cfg->compile_aot) TYPE_LOAD_ERROR ((klass)); emit_type_load_failure (cfg, klass); CLEAR_TYPELOAD_EXCEPTION (cfg);

#define HANDLE_TYPELOAD_ERROR(cfg,klass) do { \
if (!cfg->compile_aot) \
TYPE_LOAD_ERROR ((klass)); \
emit_type_load_failure (cfg, klass); \
CLEAR_TYPELOAD_EXCEPTION (cfg); \
} while (0)

/* offset from br.s -> br like opcodes */
#define BIG_BRANCH_OFFSET 13
Expand Down Expand Up @@ -9405,10 +9407,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
case MONO_CEE_ISINST: {
--sp;
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached only in AOT
}
CHECK_TYPELOAD (klass);
if (sp [0]->type != STACK_OBJ)
UNVERIFIED;

Expand All @@ -9430,10 +9429,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b

--sp;
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached in AOT only
}
CHECK_TYPELOAD (klass);

mono_save_token_info (cfg, image, token, klass);

Expand Down Expand Up @@ -9865,10 +9861,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
case MONO_CEE_UNBOX: {
--sp;
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached in AOT only
}
CHECK_TYPELOAD (klass);

mono_save_token_info (cfg, image, token, klass);

Expand Down Expand Up @@ -10420,10 +10413,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
case MONO_CEE_STOBJ:
sp -= 2;
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break;
}
CHECK_TYPELOAD (klass);

/* FIXME: should check item at sp [1] is compatible with the type of the store. */
mini_emit_memory_store (cfg, m_class_get_byval_arg (klass), sp [0], sp [1], ins_flag);
Expand All @@ -10443,10 +10433,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
--sp;

klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break;
}
CHECK_TYPELOAD (klass);
if (m_class_get_byval_arg (klass)->type == MONO_TYPE_VOID)
UNVERIFIED;

Expand Down Expand Up @@ -10572,10 +10559,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
cfg->flags |= MONO_CFG_HAS_LDELEMA;

klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached only in AOT
}
CHECK_TYPELOAD (klass);
/* we need to make sure that this array is exactly the type it needs
* to be for correctness. the wrappers are lax with their usage
* so we need to ignore them here
Expand Down Expand Up @@ -10608,10 +10592,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b

if (il_op == MONO_CEE_LDELEM) {
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached only in AOT
}
CHECK_TYPELOAD (klass);
mono_class_init_internal (klass);
}
else
Expand Down Expand Up @@ -10659,10 +10640,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b

if (il_op == MONO_CEE_STELEM) {
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached only in AOT
}
CHECK_TYPELOAD (klass);
mono_class_init_internal (klass);
}
else
Expand Down Expand Up @@ -10709,10 +10687,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
MONO_INST_NEW (cfg, ins, il_op);
--sp;
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached only in AOT
}
CHECK_TYPELOAD (klass);

context_used = mini_class_check_context_used (cfg, klass);

Expand Down Expand Up @@ -10749,10 +10724,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
MONO_INST_NEW (cfg, ins, il_op);
--sp;
klass = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (klass) {
HANDLE_TYPELOAD_ERROR (cfg, klass);
break; // reached only in AOT
}
CHECK_TYPELOAD (klass);

context_used = mini_class_check_context_used (cfg, klass);

Expand Down Expand Up @@ -11927,9 +11899,7 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
break;
case MONO_CEE_CONSTRAINED_:
constrained_class = mini_get_class (method, token, generic_context);
IF_TYPELOAD_ERROR (constrained_class) {
HANDLE_TYPELOAD_ERROR (cfg, constrained_class);
}
CHECK_TYPELOAD (klass);
ins_has_side_effect = FALSE;
break;
case MONO_CEE_CPBLK:
Expand Down
Loading