5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
11 #include <mono/metadata/object-internals.h>
12 #include <mono/metadata/verify.h>
13 #include <mono/metadata/verify-internals.h>
14 #include <mono/metadata/opcodes.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/reflection.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/mono-endian.h>
19 #include <mono/metadata/metadata.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/class-internals.h>
22 #include <mono/metadata/security-manager.h>
23 #include <mono/metadata/security-core-clr.h>
24 #include <mono/metadata/tokentype.h>
31 * Pull the list of opcodes
33 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
37 #include "mono/cil/opcode.def"
42 #ifdef MONO_VERIFIER_DEBUG
43 #define VERIFIER_DEBUG(code) do { code } while (0)
45 #define VERIFIER_DEBUG(code)
48 //////////////////////////////////////////////////////////////////
49 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
50 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
51 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
52 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
53 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
54 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
56 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
57 vinfo->info.status = __status; \
58 vinfo->info.message = ( __msg ); \
59 vinfo->exception_type = (__exception); \
60 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
63 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
64 #define ADD_VERIFY_ERROR(__ctx, __msg) \
66 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
70 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
72 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
73 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
74 (__ctx)->verifiable = 0; \
75 if (IS_FAIL_FAST_MODE (__ctx)) \
80 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
82 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
86 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
88 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
89 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
90 (__ctx)->verifiable = 0; \
91 if (IS_FAIL_FAST_MODE (__ctx)) \
95 /*Flags to be used with ILCodeDesc::flags */
97 /*Instruction has not been processed.*/
98 IL_CODE_FLAG_NOT_PROCESSED = 0,
99 /*Instruction was decoded by mono_method_verify loop.*/
100 IL_CODE_FLAG_SEEN = 1,
101 /*Instruction was target of a branch or is at a protected block boundary.*/
102 IL_CODE_FLAG_WAS_TARGET = 2,
103 /*Used by stack_init to avoid double initialize each entry.*/
104 IL_CODE_FLAG_STACK_INITED = 4,
105 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
106 IL_CODE_STACK_MERGED = 8,
107 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
108 IL_CODE_DELEGATE_SEQUENCE = 0x10,
109 /*This is a delegate created from a ldftn to a non final virtual method*/
110 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
111 /*This is a call to a non final virtual method*/
112 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
147 /*Allocated fnptr MonoType that should be freed by us.*/
149 /*Type dup'ed exception types from catch blocks.*/
150 GSList *exception_types;
155 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
159 MonoMethodSignature *signature;
160 MonoMethodHeader *header;
162 MonoGenericContext *generic_context;
166 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
167 *on a method that creates a delegate for a non-final virtual method using ldftn*/
168 gboolean has_this_store;
170 /*This flag is used to control if the contructor of the parent class has been called.
171 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
172 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
174 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
175 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
177 gboolean super_ctor_called;
181 MonoType *constrained_type;
185 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
188 get_stack_type (MonoType *type);
191 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
194 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
197 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
198 //////////////////////////////////////////////////////////////////
203 TYPE_INV = 0, /* leave at 0. */
208 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
210 /* value types and classes */
212 /* Number of types, used to define the size of the tables*/
215 /* Used by tables to signal that a result is not verifiable*/
216 NON_VERIFIABLE_RESULT = 0x80,
218 /*Mask used to extract just the type, excluding flags */
221 /* The stack type is a managed pointer, unmask the value to res */
222 POINTER_MASK = 0x100,
224 /*Stack type with the pointer mask*/
225 RAW_TYPE_MASK = 0x10F,
227 /* Controlled Mutability Manager Pointer */
230 /* The stack type is a null literal*/
231 NULL_LITERAL_MASK = 0x400,
233 /**Used by ldarg.0 and family to let delegate verification happens.*/
234 THIS_POINTER_MASK = 0x800,
236 /**Signals that this is a boxed value type*/
239 /*This is an unitialized this ref*/
240 UNINIT_THIS_MASK = 0x2000,
243 static const char* const
244 type_names [TYPE_MAX + 1] = {
255 PREFIX_UNALIGNED = 1,
258 PREFIX_CONSTRAINED = 8,
261 //////////////////////////////////////////////////////////////////
264 /*Token validation macros and functions */
265 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
266 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
267 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
268 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
270 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
271 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
272 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
273 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
274 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
275 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
278 * Verify if @token refers to a valid row on int's table.
281 token_bounds_check (MonoImage *image, guint32 token)
284 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
285 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
289 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
291 MonoType *res = g_new0 (MonoType, 1);
292 //FIXME use mono_method_get_signature_full
293 res->data.method = mono_method_signature (method);
294 res->type = MONO_TYPE_FNPTR;
295 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
300 * mono_type_is_enum_type:
302 * Returns TRUE if @type is an enum type.
305 mono_type_is_enum_type (MonoType *type)
307 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
309 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
315 * mono_type_is_value_type:
317 * Returns TRUE if @type is named after @namespace.@name.
321 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
323 return type->type == MONO_TYPE_VALUETYPE &&
324 !strcmp (namespace, type->data.klass->name_space) &&
325 !strcmp (name, type->data.klass->name);
329 * Returns TURE if @type is VAR or MVAR
332 mono_type_is_generic_argument (MonoType *type)
334 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
338 * mono_type_get_underlying_type_any:
340 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
342 * Returns the underlying type of @type regardless if it is byref or not.
345 mono_type_get_underlying_type_any (MonoType *type)
347 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
348 return mono_class_enum_basetype (type->data.klass);
349 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
350 return mono_class_enum_basetype (type->data.generic_class->container_class);
355 mono_type_get_stack_name (MonoType *type)
357 return type_names [get_stack_type (type) & TYPE_MASK];
360 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
361 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
364 mono_method_is_constructor (MonoMethod *method)
366 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
367 !(method->flags & CTOR_INVALID_FLAGS) &&
368 !strcmp (".ctor", method->name));
372 mono_class_has_default_constructor (MonoClass *klass)
377 mono_class_setup_methods (klass);
379 for (i = 0; i < klass->method.count; ++i) {
380 method = klass->methods [i];
381 if (mono_method_is_constructor (method) &&
382 mono_method_signature (method)->param_count == 0 &&
383 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
390 mono_class_interface_implements_interface (MonoClass *candidate, MonoClass *iface)
394 if (candidate == iface)
396 mono_class_setup_interfaces (candidate);
397 for (i = 0; i < candidate->interface_count; ++i) {
398 if (candidate->interfaces [i] == iface || mono_class_interface_implements_interface (candidate->interfaces [i], iface))
401 candidate = candidate->parent;
407 * Verify if @type is valid for the given @ctx verification context.
408 * this function checks for VAR and MVAR types that are invalid under the current verifier,
411 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
414 MonoGenericInst *inst;
416 switch (type->type) {
421 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
422 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
425 case MONO_TYPE_SZARRAY:
426 return mono_type_is_valid_type_in_context (&type->data.klass->byval_arg, context);
427 case MONO_TYPE_ARRAY:
428 return mono_type_is_valid_type_in_context (&type->data.array->eklass->byval_arg, context);
430 return mono_type_is_valid_type_in_context (type->data.type, context);
431 case MONO_TYPE_GENERICINST:
432 inst = type->data.generic_class->context.class_inst;
435 for (i = 0; i < inst->type_argc; ++i)
436 if (!mono_type_is_valid_type_in_context (inst->type_argv [i], context))
443 /*This function returns NULL if the type is not instantiatable*/
445 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
447 if (!mono_type_is_valid_type_in_context (type, context))
449 return mono_class_inflate_generic_type (type, context);
452 * Test if @candidate is a subtype of @target using the minimal possible information
453 * TODO move the code for non finished TypeBuilders to here.
456 mono_class_is_constraint_compatible (MonoClass *candidate, MonoClass *target)
458 if (candidate == target)
460 if (target == mono_defaults.object_class)
463 //setup_supertypes don't mono_class_init anything
464 mono_class_setup_supertypes (candidate);
465 mono_class_setup_supertypes (target);
467 if (mono_class_has_parent (candidate, target))
470 //if target is not a supertype it must be an interface
471 if (!MONO_CLASS_IS_INTERFACE (target))
474 if (candidate->image->dynamic && !candidate->wastypebuilder) {
475 MonoReflectionTypeBuilder *tb = candidate->reflection_info;
477 if (tb->interfaces) {
478 for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
479 MonoReflectionType *iface = mono_array_get (tb->interfaces, MonoReflectionType*, j);
480 MonoClass *ifaceClass = mono_class_from_mono_type (iface->type);
481 if (mono_class_is_constraint_compatible (ifaceClass, target)) {
488 return mono_class_interface_implements_interface (candidate, target);
492 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
496 if (ginst->type_argc != gc->type_argc)
499 for (i = 0; i < gc->type_argc; ++i) {
500 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
501 MonoClass *paramClass;
502 MonoClass **constraints;
504 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
506 if (mono_type_is_generic_argument (ginst->type_argv [i]))
507 continue; //it's not our job to validate type variables
509 paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
511 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
514 /*it's not safe to call mono_class_init from here*/
515 if (paramClass->generic_class && !paramClass->inited) {
516 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
520 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
523 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
526 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
529 if (!param_info->constraints)
532 for (constraints = param_info->constraints; *constraints; ++constraints) {
533 MonoClass *ctr = *constraints;
536 inflated = mono_class_inflate_generic_type (&ctr->byval_arg, context);
537 ctr = mono_class_from_mono_type (inflated);
538 mono_metadata_free_type (inflated);
540 if (!mono_class_is_constraint_compatible (paramClass, ctr))
548 * Return true if @candidate is constraint compatible with @target.
550 * This means that @candidate constraints are a super set of @target constaints
553 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoGenericContext *context)
555 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
556 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
558 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
559 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
560 if ((tmask & cmask) != tmask)
563 if (tinfo->constraints) {
564 MonoClass **target_class, **candidate_class;
565 if (!cinfo->constraints)
567 for (target_class = tinfo->constraints; *target_class; ++target_class) {
569 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
572 tc = mono_class_from_mono_type (inflated);
573 mono_metadata_free_type (inflated);
575 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
577 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
580 cc = mono_class_from_mono_type (inflated);
581 mono_metadata_free_type (inflated);
583 if (mono_class_is_assignable_from (tc, cc))
586 if (!*candidate_class)
593 static MonoGenericParam*
594 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
596 MonoGenericContainer *gc;
597 MonoMethod *method = ctx->method;
600 num = mono_type_get_generic_param_num (type);
602 if (type->type == MONO_TYPE_VAR) {
603 MonoClass *gtd = method->klass;
604 if (gtd->generic_class)
605 gtd = gtd->generic_class->container_class;
606 gc = gtd->generic_container;
608 MonoMethod *gmd = method;
609 if (method->is_inflated)
610 gmd = ((MonoMethodInflated*)method)->declaring;
611 gc = mono_method_get_generic_container (gmd);
615 return mono_generic_container_get_param (gc, num);
621 * Verify if @type is valid for the given @ctx verification context.
622 * this function checks for VAR and MVAR types that are invalid under the current verifier,
623 * This means that it either
626 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
628 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
632 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
635 for (i = 0; i < ginst->type_argc; ++i) {
636 MonoType *type = ginst->type_argv [i];
637 if (!is_valid_type_in_context (ctx, type))
644 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
647 for (i = 0; i < ginst->type_argc; ++i) {
648 MonoType *type = ginst->type_argv [i];
649 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
650 MonoGenericParam *candidate;
652 if (!mono_type_is_generic_argument (type))
655 if (!is_valid_type_in_context (ctx, type))
658 candidate = verifier_get_generic_param_from_type (ctx, type);
660 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, context))
667 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
669 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
670 MonoGenericInst *ginst = gmethod->context.method_inst;
671 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
672 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
676 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
678 MonoGenericClass *gklass = klass->generic_class;
679 MonoGenericInst *ginst = gklass->context.class_inst;
680 MonoGenericContainer *gc = gklass->container_class->generic_container;
681 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
685 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
687 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
688 MonoGenericInst *ginst = gmethod->context.method_inst;
689 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
690 if (!gc) /*non-generic inflated method - it's part of a generic type */
692 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
694 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
699 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
701 MonoGenericClass *gklass = klass->generic_class;
702 MonoGenericInst *ginst = gklass->context.class_inst;
703 MonoGenericContainer *gc = gklass->container_class->generic_container;
704 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
706 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
710 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
714 if (!is_valid_type_in_context (ctx, type)) {
715 char *str = mono_type_full_name (type);
716 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
717 type->type == MONO_TYPE_VAR ? "!" : "!!",
719 type->type == MONO_TYPE_VAR ? "class" : "method",
721 MONO_EXCEPTION_BAD_IMAGE);
726 klass = mono_class_from_mono_type (type);
727 mono_class_init (klass);
728 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
729 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
730 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic instantiation of type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
732 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
736 if (klass->exception_type != MONO_EXCEPTION_NONE || (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE)) {
737 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
741 if (!klass->generic_class)
744 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
745 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
749 if (!mono_class_repect_method_constraints (ctx, klass)) {
750 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s (generic args don't respect target's constraints) at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
757 static verify_result_t
758 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
760 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
761 return RESULT_INVALID;
763 if (!method->is_inflated)
766 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
767 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset), MONO_EXCEPTION_UNVERIFIABLE_IL);
768 return RESULT_INVALID;
771 if (!mono_method_repect_method_constraints (ctx, method)) {
772 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s (generic args don't respect target's constraints) at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset));
773 return RESULT_UNVERIFIABLE;
779 static MonoClassField*
780 verifier_load_field (VerifyContext *ctx, int token, MonoClass **klass, const char *opcode) {
781 MonoClassField *field;
783 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
784 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid field token 0x%x08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
788 field = mono_field_from_token (ctx->image, token, klass, ctx->generic_context);
789 if (!field || !field->parent) {
790 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load field from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
794 if (!mono_type_is_valid_in_context (ctx, &field->parent->byval_arg))
801 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
804 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
805 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid method token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
809 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
812 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load method from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
816 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
823 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
826 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
827 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
831 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
834 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load type from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
838 if (!mono_type_is_valid_in_context (ctx, type))
845 /* stack_slot_get_type:
847 * Returns the stack type of @value. This value includes POINTER_MASK.
849 * Use this function to checks that account for a managed pointer.
852 stack_slot_get_type (ILStackDesc *value)
854 return value->stype & RAW_TYPE_MASK;
857 /* stack_slot_get_underlying_type:
859 * Returns the stack type of @value. This value does not include POINTER_MASK.
861 * Use this function is cases where the fact that the value could be a managed pointer is
862 * irrelevant. For example, field load doesn't care about this fact of type on stack.
865 stack_slot_get_underlying_type (ILStackDesc *value)
867 return value->stype & TYPE_MASK;
870 /* stack_slot_is_managed_pointer:
872 * Returns TRUE is @value is a managed pointer.
875 stack_slot_is_managed_pointer (ILStackDesc *value)
877 return (value->stype & POINTER_MASK) == POINTER_MASK;
880 /* stack_slot_is_managed_mutability_pointer:
882 * Returns TRUE is @value is a managed mutability pointer.
884 static G_GNUC_UNUSED gboolean
885 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
887 return (value->stype & CMMP_MASK) == CMMP_MASK;
890 /* stack_slot_is_null_literal:
892 * Returns TRUE is @value is the null literal.
895 stack_slot_is_null_literal (ILStackDesc *value)
897 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
901 /* stack_slot_is_this_pointer:
903 * Returns TRUE is @value is the this literal
906 stack_slot_is_this_pointer (ILStackDesc *value)
908 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
911 /* stack_slot_is_boxed_value:
913 * Returns TRUE is @value is a boxed value
916 stack_slot_is_boxed_value (ILStackDesc *value)
918 return (value->stype & BOXED_MASK) == BOXED_MASK;
922 stack_slot_get_name (ILStackDesc *value)
924 return type_names [value->stype & TYPE_MASK];
927 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
928 if (PRED (value)) { \
930 g_string_append (str, ", "); \
931 g_string_append (str, NAME); \
936 stack_slot_stack_type_full_name (ILStackDesc *value)
938 GString *str = g_string_new ("");
941 if ((value->stype & TYPE_MASK) != value->stype) {
942 gboolean first = TRUE;
943 g_string_append(str, "[");
944 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
945 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
946 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
947 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
948 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
949 g_string_append(str, "] ");
952 g_string_append (str, stack_slot_get_name (value));
954 g_string_free (str, FALSE);
959 stack_slot_full_name (ILStackDesc *value)
961 char *type_name = mono_type_full_name (value->type);
962 char *stack_name = stack_slot_stack_type_full_name (value);
963 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
969 //////////////////////////////////////////////////////////////////
971 mono_free_verify_list (GSList *list)
973 MonoVerifyInfoExtended *info;
976 for (tmp = list; tmp; tmp = tmp->next) {
978 g_free (info->info.message);
984 #define ADD_ERROR(list,msg) \
986 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
987 vinfo->info.status = MONO_VERIFY_ERROR; \
988 vinfo->info.message = (msg); \
989 (list) = g_slist_prepend ((list), vinfo); \
992 #define ADD_WARN(list,code,msg) \
994 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
995 vinfo->info.status = (code); \
996 vinfo->info.message = (msg); \
997 (list) = g_slist_prepend ((list), vinfo); \
1001 valid_cultures[][9] = {
1002 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
1003 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
1004 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
1005 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
1006 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
1007 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
1008 "da-DK", "de-DE", "de-CH", "de-AT",
1009 "de-LU", "de-LI", "el-GR", "en-US",
1010 "en-GB", "en-AU", "en-CA", "en-NZ",
1011 "en-IE", "en-ZA", "en-JM", "en-CB",
1012 "en-BZ", "en-TT", "en-ZW", "en-PH",
1013 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
1014 "es-CR", "es-PA", "es-DO", "es-VE",
1015 "es-CO", "es-PE", "es-AR", "es-EC",
1016 "es-CL", "es-UY", "es-PY", "es-BO",
1017 "es-SV", "es-HN", "es-NI", "es-PR",
1018 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
1019 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
1020 "hu-HU", "is-IS", "it-IT", "it-CH",
1021 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
1022 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
1023 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
1024 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
1025 "sv-SE", "sv-FI", "th-TH", "tr-TR",
1026 "ur-PK", "id-ID", "uk-UA", "be-BY",
1027 "sl-SI", "et-EE", "lv-LV", "lt-LT",
1028 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
1030 "eu-ES", "mk-MK", "af-ZA",
1031 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
1032 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
1033 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
1034 "gu-IN", "ta-IN", "te-IN", "kn-IN",
1035 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
1036 "kok-IN", "syr-SY", "div-MV"
1040 is_valid_culture (const char *cname)
1045 found = *cname == 0;
1046 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
1047 if (g_strcasecmp (valid_cultures [i], cname)) {
1056 is_valid_assembly_flags (guint32 flags) {
1057 /* Metadata: 22.1.2 */
1058 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
1059 return ((flags == 1) || (flags == 0));
1063 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
1066 const char *p, *blob_end;
1068 if (blob_index >= image->heap_blob.size)
1070 p = mono_metadata_blob_heap (image, blob_index);
1071 size = mono_metadata_decode_blob_size (p, &blob_end);
1072 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
1074 if (notnull && !size)
1080 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
1082 const char *p, *blob_end, *res;
1084 if (str_index >= image->heap_strings.size)
1086 res = p = mono_metadata_string_heap (image, str_index);
1087 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
1091 * FIXME: should check it's a valid utf8 string, too.
1093 while (p <= blob_end) {
1098 return *p? NULL: res;
1102 is_valid_cls_ident (const char *p)
1105 * FIXME: we need the full unicode glib support for this.
1106 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
1107 * We do the lame thing for now.
1113 if (!isalnum (*p) && *p != '_')
1121 is_valid_filename (const char *p)
1125 return strpbrk (p, "\\//:")? 0: 1;
1129 verify_assembly_table (MonoImage *image, GSList *list, int level)
1131 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
1132 guint32 cols [MONO_ASSEMBLY_SIZE];
1135 if (level & MONO_VERIFY_ERROR) {
1137 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
1138 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
1140 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
1141 case ASSEMBLY_HASH_NONE:
1142 case ASSEMBLY_HASH_MD5:
1143 case ASSEMBLY_HASH_SHA1:
1146 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
1149 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
1150 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
1152 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
1153 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
1155 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
1156 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
1158 if (strpbrk (p, ":\\/."))
1159 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
1162 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
1163 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
1165 if (!is_valid_culture (p))
1166 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
1173 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
1175 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1176 guint32 cols [MONO_ASSEMBLYREF_SIZE];
1180 if (level & MONO_VERIFY_ERROR) {
1181 for (i = 0; i < t->rows; ++i) {
1182 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
1183 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
1184 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
1186 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
1187 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
1189 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
1190 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
1192 if (!is_valid_culture (p))
1193 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
1196 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
1197 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
1200 if (level & MONO_VERIFY_WARNING) {
1201 /* check for duplicated rows */
1202 for (i = 0; i < t->rows; ++i) {
1209 verify_class_layout_table (MonoImage *image, GSList *list, int level)
1211 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
1212 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
1213 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
1216 if (level & MONO_VERIFY_ERROR) {
1217 for (i = 0; i < t->rows; ++i) {
1218 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
1220 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
1221 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
1223 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
1224 if (value & TYPE_ATTRIBUTE_INTERFACE)
1225 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
1226 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
1227 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
1228 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
1229 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
1230 case 0: case 1: case 2: case 4: case 8: case 16:
1231 case 32: case 64: case 128: break;
1233 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
1235 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1237 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
1238 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
1239 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid with explicit layout", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
1243 * FIXME: we need to check that if class size != 0,
1244 * it needs to be greater than the class calculated size.
1245 * If parent is a valuetype it also needs to be smaller than
1246 * 1 MByte (0x100000 bytes).
1247 * To do both these checks we need to load the referenced
1248 * assemblies, though (the spec claims we didn't have to, bah).
1251 * We need to check that the parent types have the same layout
1262 verify_constant_table (MonoImage *image, GSList *list, int level)
1264 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
1265 guint32 cols [MONO_CONSTANT_SIZE];
1267 GHashTable *dups = g_hash_table_new (NULL, NULL);
1269 for (i = 0; i < t->rows; ++i) {
1270 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
1272 if (level & MONO_VERIFY_ERROR)
1273 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
1274 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
1275 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
1276 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
1278 switch (cols [MONO_CONSTANT_TYPE]) {
1279 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
1283 if (level & MONO_VERIFY_CLS)
1284 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Type 0x%x not CLS compliant in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
1285 case MONO_TYPE_BOOLEAN:
1286 case MONO_TYPE_CHAR:
1293 case MONO_TYPE_STRING:
1294 case MONO_TYPE_CLASS:
1297 if (level & MONO_VERIFY_ERROR)
1298 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
1300 if (level & MONO_VERIFY_ERROR) {
1301 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
1302 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
1303 case MONO_HASCONSTANT_FIEDDEF:
1304 if (value > image->tables [MONO_TABLE_FIELD].rows)
1305 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
1307 case MONO_HASCONSTANT_PARAM:
1308 if (value > image->tables [MONO_TABLE_PARAM].rows)
1309 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
1311 case MONO_HASCONSTANT_PROPERTY:
1312 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
1313 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
1316 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
1320 if (level & MONO_VERIFY_CLS) {
1322 * FIXME: verify types is consistent with the enum type
1323 * is parent is an enum.
1327 g_hash_table_destroy (dups);
1332 verify_event_map_table (MonoImage *image, GSList *list, int level)
1334 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
1335 guint32 cols [MONO_EVENT_MAP_SIZE];
1336 guint32 i, last_event;
1337 GHashTable *dups = g_hash_table_new (NULL, NULL);
1341 for (i = 0; i < t->rows; ++i) {
1342 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
1343 if (level & MONO_VERIFY_ERROR)
1344 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
1345 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1346 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
1347 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
1348 if (level & MONO_VERIFY_ERROR) {
1349 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
1350 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1351 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
1352 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
1354 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
1355 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
1356 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
1360 g_hash_table_destroy (dups);
1365 verify_event_table (MonoImage *image, GSList *list, int level)
1367 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
1368 guint32 cols [MONO_EVENT_SIZE];
1372 for (i = 0; i < t->rows; ++i) {
1373 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
1375 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
1376 if (level & MONO_VERIFY_ERROR)
1377 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
1379 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
1380 if (level & MONO_VERIFY_ERROR)
1381 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
1383 if (level & MONO_VERIFY_CLS) {
1384 if (!is_valid_cls_ident (p))
1385 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
1389 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
1390 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
1391 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
1392 case MONO_TYPEDEFORREF_TYPEDEF:
1393 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
1394 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1396 case MONO_TYPEDEFORREF_TYPEREF:
1397 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
1398 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1400 case MONO_TYPEDEFORREF_TYPESPEC:
1401 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
1402 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1405 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1409 * FIXME: check that there is 1 add and remove row in methodsemantics
1410 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
1411 * these while checking methodsemantics).
1412 * check for duplicated names for the same type [ERROR]
1413 * check for CLS duplicate names for the same type [CLS]
1420 verify_field_table (MonoImage *image, GSList *list, int level)
1422 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
1423 guint32 cols [MONO_FIELD_SIZE];
1427 for (i = 0; i < t->rows; ++i) {
1428 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
1430 * Check this field has only one owner and that the owner is not
1431 * an interface (done in verify_typedef_table() )
1433 flags = cols [MONO_FIELD_FLAGS];
1434 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
1435 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
1436 case FIELD_ATTRIBUTE_PRIVATE:
1437 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
1438 case FIELD_ATTRIBUTE_ASSEMBLY:
1439 case FIELD_ATTRIBUTE_FAMILY:
1440 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
1441 case FIELD_ATTRIBUTE_PUBLIC:
1444 if (level & MONO_VERIFY_ERROR)
1445 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
1448 if (level & MONO_VERIFY_ERROR) {
1449 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
1450 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
1451 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
1452 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
1453 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
1454 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
1456 * FIXME: check there is only one owner in the respective table.
1457 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
1458 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
1459 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
1462 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
1463 if (level & MONO_VERIFY_ERROR)
1464 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
1466 if (level & MONO_VERIFY_CLS) {
1467 if (!is_valid_cls_ident (p))
1468 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
1473 * if owner is module needs to be static, access mask needs to be compilercontrolled,
1474 * public or private (not allowed in cls mode).
1475 * if owner is an enum ...
1484 verify_file_table (MonoImage *image, GSList *list, int level)
1486 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1487 guint32 cols [MONO_FILE_SIZE];
1490 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1492 for (i = 0; i < t->rows; ++i) {
1493 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
1494 if (level & MONO_VERIFY_ERROR) {
1495 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
1496 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
1497 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
1498 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
1500 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
1501 if (level & MONO_VERIFY_ERROR)
1502 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
1504 if (level & MONO_VERIFY_ERROR) {
1505 if (!is_valid_filename (p))
1506 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
1507 else if (g_hash_table_lookup (dups, p)) {
1508 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
1510 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1514 * FIXME: I don't understand what this means:
1515 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
1516 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
1520 if (level & MONO_VERIFY_WARNING) {
1521 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
1522 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
1524 g_hash_table_destroy (dups);
1529 verify_moduleref_table (MonoImage *image, GSList *list, int level)
1531 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
1532 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
1533 guint32 cols [MONO_MODULEREF_SIZE];
1535 guint32 found, i, j, value;
1536 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1538 for (i = 0; i < t->rows; ++i) {
1539 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
1540 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
1541 if (level & MONO_VERIFY_ERROR)
1542 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
1544 if (level & MONO_VERIFY_ERROR) {
1545 if (!is_valid_filename (p))
1546 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
1547 else if (g_hash_table_lookup (dups, p)) {
1548 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
1549 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1551 for (j = 0; j < tfile->rows; ++j) {
1552 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
1553 if ((pf = is_valid_string (image, value, TRUE)))
1554 if (strcmp (p, pf) == 0) {
1560 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
1565 g_hash_table_destroy (dups);
1570 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
1572 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
1573 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1577 for (i = 0; i < t->rows; ++i) {
1578 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1579 if (level & MONO_VERIFY_ERROR) {
1580 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
1581 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
1583 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
1584 /* FIXME: check it's a valid locals or method sig.*/
1592 mono_image_verify_tables (MonoImage *image, int level)
1594 GSList *error_list = NULL;
1596 error_list = verify_assembly_table (image, error_list, level);
1598 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
1599 * AssemblyRefProcessor should be ignored,
1600 * though we may want to emit a warning, since it should not
1601 * be present in a PE file.
1603 error_list = verify_assemblyref_table (image, error_list, level);
1604 error_list = verify_class_layout_table (image, error_list, level);
1605 error_list = verify_constant_table (image, error_list, level);
1607 * cutom attribute, declsecurity
1609 error_list = verify_event_map_table (image, error_list, level);
1610 error_list = verify_event_table (image, error_list, level);
1611 error_list = verify_field_table (image, error_list, level);
1612 error_list = verify_file_table (image, error_list, level);
1613 error_list = verify_moduleref_table (image, error_list, level);
1614 error_list = verify_standalonesig_table (image, error_list, level);
1616 return g_slist_reverse (error_list);
1619 #define ADD_INVALID(list,msg) \
1621 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1622 vinfo->status = MONO_VERIFY_ERROR; \
1623 vinfo->message = (msg); \
1624 (list) = g_slist_prepend ((list), vinfo); \
1625 /*G_BREAKPOINT ();*/ \
1629 #define CHECK_STACK_UNDERFLOW(num) \
1631 if (cur_stack < (num)) \
1632 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1635 #define CHECK_STACK_OVERFLOW() \
1637 if (cur_stack >= max_stack) \
1638 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1643 in_any_block (MonoMethodHeader *header, guint offset)
1646 MonoExceptionClause *clause;
1648 for (i = 0; i < header->num_clauses; ++i) {
1649 clause = &header->clauses [i];
1650 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1652 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1654 if (MONO_OFFSET_IN_FILTER (clause, offset))
1661 * in_any_exception_block:
1663 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1666 in_any_exception_block (MonoMethodHeader *header, guint offset)
1669 MonoExceptionClause *clause;
1671 for (i = 0; i < header->num_clauses; ++i) {
1672 clause = &header->clauses [i];
1673 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1675 if (MONO_OFFSET_IN_FILTER (clause, offset))
1682 * is_valid_branch_instruction:
1684 * Verify if it's valid to perform a branch from @offset to @target.
1685 * This should be used with br and brtrue/false.
1686 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1687 * The major diferent from other similiar functions is that branching into a
1688 * finally/fault block is invalid instead of just unverifiable.
1691 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1694 MonoExceptionClause *clause;
1696 for (i = 0; i < header->num_clauses; ++i) {
1697 clause = &header->clauses [i];
1698 /*branching into a finally block is invalid*/
1699 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1700 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1701 MONO_OFFSET_IN_HANDLER (clause, target))
1704 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1706 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1708 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1715 * is_valid_cmp_branch_instruction:
1717 * Verify if it's valid to perform a branch from @offset to @target.
1718 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1719 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1721 * The major diferences from other similar functions are that most errors lead to invalid
1722 * code and only branching out of finally, filter or fault clauses is unverifiable.
1725 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1728 MonoExceptionClause *clause;
1730 for (i = 0; i < header->num_clauses; ++i) {
1731 clause = &header->clauses [i];
1732 /*branching out of a handler or finally*/
1733 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1734 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1735 !MONO_OFFSET_IN_HANDLER (clause, target))
1738 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1740 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1742 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1749 * A leave can't escape a finally block
1752 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1755 MonoExceptionClause *clause;
1757 for (i = 0; i < header->num_clauses; ++i) {
1758 clause = &header->clauses [i];
1759 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1761 if (MONO_OFFSET_IN_FILTER (clause, offset))
1768 * A rethrow can't happen outside of a catch handler.
1771 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1774 MonoExceptionClause *clause;
1776 for (i = 0; i < header->num_clauses; ++i) {
1777 clause = &header->clauses [i];
1778 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1780 if (MONO_OFFSET_IN_FILTER (clause, offset))
1787 * An endfinally can't happen outside of a finally/fault handler.
1790 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1793 MonoExceptionClause *clause;
1795 for (i = 0; i < header->num_clauses; ++i) {
1796 clause = &header->clauses [i];
1797 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1805 * An endfilter can only happens inside a filter clause.
1806 * In non-strict mode filter is allowed inside the handler clause too
1808 static MonoExceptionClause *
1809 is_correct_endfilter (VerifyContext *ctx, guint offset)
1812 MonoExceptionClause *clause;
1814 for (i = 0; i < ctx->header->num_clauses; ++i) {
1815 clause = &ctx->header->clauses [i];
1816 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1818 if (MONO_OFFSET_IN_FILTER (clause, offset))
1820 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1828 * Non-strict endfilter can happens inside a try block or any handler block
1831 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1834 MonoExceptionClause *clause;
1836 for (i = 0; i < ctx->header->num_clauses; ++i) {
1837 clause = &ctx->header->clauses [i];
1838 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1845 is_valid_bool_arg (ILStackDesc *arg)
1847 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1851 switch (stack_slot_get_underlying_type (arg)) {
1854 case TYPE_NATIVE_INT:
1858 g_assert (arg->type);
1859 switch (arg->type->type) {
1860 case MONO_TYPE_CLASS:
1861 case MONO_TYPE_STRING:
1862 case MONO_TYPE_OBJECT:
1863 case MONO_TYPE_SZARRAY:
1864 case MONO_TYPE_ARRAY:
1865 case MONO_TYPE_FNPTR:
1868 case MONO_TYPE_GENERICINST:
1869 /*We need to check if the container class
1870 * of the generic type is a valuetype, iow:
1871 * is it a "class Foo<T>" or a "struct Foo<T>"?
1873 return !arg->type->data.generic_class->container_class->valuetype;
1881 /*Type manipulation helper*/
1883 /*Returns the byref version of the supplied MonoType*/
1885 mono_type_get_type_byref (MonoType *type)
1889 return &mono_class_from_mono_type (type)->this_arg;
1893 /*Returns the byval version of the supplied MonoType*/
1895 mono_type_get_type_byval (MonoType *type)
1899 return &mono_class_from_mono_type (type)->byval_arg;
1903 mono_type_from_stack_slot (ILStackDesc *slot)
1905 if (stack_slot_is_managed_pointer (slot))
1906 return mono_type_get_type_byref (slot->type);
1910 /*Stack manipulation code*/
1913 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1915 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1918 state->flags |= IL_CODE_FLAG_STACK_INITED;
1920 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1924 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1926 to->size = from->size;
1927 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1931 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1933 to->stype = from->stype;
1934 to->type = from->type;
1935 to->method = from->method;
1939 check_underflow (VerifyContext *ctx, int size)
1941 if (ctx->eval.size < size) {
1942 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1949 check_overflow (VerifyContext *ctx)
1951 if (ctx->eval.size >= ctx->max_stack) {
1952 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1958 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1960 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1962 if (stack_slot_get_type (value) == TYPE_PTR) {
1963 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1969 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1971 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1973 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1974 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1981 static ILStackDesc *
1982 stack_push (VerifyContext *ctx)
1984 return & ctx->eval.stack [ctx->eval.size++];
1987 static ILStackDesc *
1988 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1990 ILStackDesc *top = stack_push (ctx);
1996 static ILStackDesc *
1997 stack_pop (VerifyContext *ctx)
1999 ILStackDesc *ret = ctx->eval.stack + --ctx->eval.size;
2000 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
2001 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
2005 /* This function allows to safely pop an unititialized this ptr from
2006 * the eval stack without marking the method as unverifiable.
2008 static ILStackDesc *
2009 stack_pop_safe (VerifyContext *ctx)
2011 return ctx->eval.stack + --ctx->eval.size;
2014 static ILStackDesc *
2015 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
2017 ILStackDesc *top = stack_push (ctx);
2018 copy_stack_value (top, value);
2022 /* Returns the MonoType associated with the token, or NULL if it is invalid.
2024 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
2027 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
2032 if (!(type = verifier_load_type (ctx, token, opcode)))
2035 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
2036 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
2040 if (type->type == MONO_TYPE_VOID) {
2041 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
2045 if (type->type == MONO_TYPE_TYPEDBYREF)
2046 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
2048 check_unverifiable_type (ctx, type);
2053 /*operation result tables */
2055 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
2056 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2057 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2058 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2059 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2060 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2061 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2064 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
2065 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2066 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2067 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2068 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2069 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
2070 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2073 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
2074 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2075 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2076 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2077 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2078 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
2079 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2082 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
2083 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2084 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2085 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2086 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2087 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2088 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2091 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
2092 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2093 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
2094 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2095 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2096 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2097 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2100 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
2101 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2102 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2103 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2104 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
2105 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
2106 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2109 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
2110 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2111 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2112 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
2113 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
2114 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
2115 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
2118 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
2119 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2120 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2121 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2122 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2123 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
2124 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2127 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
2128 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2129 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2130 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2131 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2132 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
2133 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2136 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
2137 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2138 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2139 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2140 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2141 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2142 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2145 #ifdef MONO_VERIFIER_DEBUG
2149 dump_stack_value (ILStackDesc *value)
2151 printf ("[(%x)(%x)", value->type->type, value->stype);
2153 if (stack_slot_is_this_pointer (value))
2156 if (stack_slot_is_boxed_value (value))
2157 printf ("[boxed] ");
2159 if (stack_slot_is_null_literal (value))
2162 if (stack_slot_is_managed_mutability_pointer (value))
2163 printf ("Controled Mutability MP: ");
2165 if (stack_slot_is_managed_pointer (value))
2166 printf ("Managed Pointer to: ");
2168 switch (stack_slot_get_underlying_type (value)) {
2170 printf ("invalid type]");
2178 case TYPE_NATIVE_INT:
2179 printf ("native int]");
2182 printf ("float64]");
2185 printf ("unmanaged pointer]");
2188 switch (value->type->type) {
2189 case MONO_TYPE_CLASS:
2190 case MONO_TYPE_VALUETYPE:
2191 printf ("complex] (%s)", value->type->data.klass->name);
2193 case MONO_TYPE_STRING:
2194 printf ("complex] (string)");
2196 case MONO_TYPE_OBJECT:
2197 printf ("complex] (object)");
2199 case MONO_TYPE_SZARRAY:
2200 printf ("complex] (%s [])", value->type->data.klass->name);
2202 case MONO_TYPE_ARRAY:
2203 printf ("complex] (%s [%d %d %d])",
2204 value->type->data.array->eklass->name,
2205 value->type->data.array->rank,
2206 value->type->data.array->numsizes,
2207 value->type->data.array->numlobounds);
2209 case MONO_TYPE_GENERICINST:
2210 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
2213 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
2215 case MONO_TYPE_MVAR:
2216 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
2219 //should be a boxed value
2220 char * name = mono_type_full_name (value->type);
2221 printf ("complex] %s", name);
2227 printf ("unknown stack %x type]\n", value->stype);
2228 g_assert_not_reached ();
2233 dump_stack_state (ILCodeDesc *state)
2237 printf ("(%d) ", state->size);
2238 for (i = 0; i < state->size; ++i)
2239 dump_stack_value (state->stack + i);
2244 /*Returns TRUE if candidate array type can be assigned to target.
2245 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
2248 is_array_type_compatible (MonoType *target, MonoType *candidate)
2250 MonoArrayType *left = target->data.array;
2251 MonoArrayType *right = candidate->data.array;
2253 g_assert (target->type == MONO_TYPE_ARRAY);
2254 g_assert (candidate->type == MONO_TYPE_ARRAY);
2256 if (left->rank != right->rank)
2259 return mono_class_is_assignable_from (left->eklass, right->eklass);
2263 get_stack_type (MonoType *type)
2266 int type_kind = type->type;
2268 mask = POINTER_MASK;
2269 /*TODO handle CMMP_MASK */
2272 switch (type_kind) {
2275 case MONO_TYPE_BOOLEAN:
2278 case MONO_TYPE_CHAR:
2281 return TYPE_I4 | mask;
2285 return TYPE_NATIVE_INT | mask;
2287 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
2288 case MONO_TYPE_FNPTR:
2290 case MONO_TYPE_TYPEDBYREF:
2291 return TYPE_PTR | mask;
2294 case MONO_TYPE_MVAR:
2296 case MONO_TYPE_CLASS:
2297 case MONO_TYPE_STRING:
2298 case MONO_TYPE_OBJECT:
2299 case MONO_TYPE_SZARRAY:
2300 case MONO_TYPE_ARRAY:
2301 return TYPE_COMPLEX | mask;
2303 case MONO_TYPE_GENERICINST:
2304 if (mono_type_is_enum_type (type)) {
2305 type = mono_type_get_underlying_type_any (type);
2306 type_kind = type->type;
2309 return TYPE_COMPLEX | mask;
2314 return TYPE_I8 | mask;
2318 return TYPE_R8 | mask;
2320 case MONO_TYPE_VALUETYPE:
2321 if (mono_type_is_enum_type (type)) {
2322 type = mono_type_get_underlying_type_any (type);
2323 type_kind = type->type;
2326 return TYPE_COMPLEX | mask;
2330 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
2331 g_assert_not_reached ();
2336 /* convert MonoType to ILStackDesc format (stype) */
2338 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
2341 int type_kind = type->type;
2343 if (type->byref || take_addr)
2344 mask = POINTER_MASK;
2345 /* TODO handle CMMP_MASK */
2350 switch (type_kind) {
2353 case MONO_TYPE_BOOLEAN:
2356 case MONO_TYPE_CHAR:
2359 stack->stype = TYPE_I4 | mask;
2363 stack->stype = TYPE_NATIVE_INT | mask;
2366 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2367 case MONO_TYPE_FNPTR:
2369 case MONO_TYPE_TYPEDBYREF:
2370 stack->stype = TYPE_PTR | mask;
2373 case MONO_TYPE_CLASS:
2374 case MONO_TYPE_STRING:
2375 case MONO_TYPE_OBJECT:
2376 case MONO_TYPE_SZARRAY:
2377 case MONO_TYPE_ARRAY:
2380 case MONO_TYPE_MVAR:
2381 stack->stype = TYPE_COMPLEX | mask;
2384 case MONO_TYPE_GENERICINST:
2385 if (mono_type_is_enum_type (type)) {
2386 type = mono_type_get_underlying_type_any (type);
2387 type_kind = type->type;
2390 stack->stype = TYPE_COMPLEX | mask;
2396 stack->stype = TYPE_I8 | mask;
2400 stack->stype = TYPE_R8 | mask;
2402 case MONO_TYPE_VALUETYPE:
2403 if (mono_type_is_enum_type (type)) {
2404 type = mono_type_get_underlying_type_any (type);
2405 type_kind = type->type;
2408 stack->stype = TYPE_COMPLEX | mask;
2412 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
2413 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
2420 * init_stack_with_value_at_exception_boundary:
2422 * Initialize the stack and push a given type.
2423 * The instruction is marked as been on the exception boundary.
2426 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
2428 MonoType *type = mono_class_inflate_generic_type (&klass->byval_arg, ctx->generic_context);
2429 stack_init (ctx, code);
2430 set_stack_value (ctx, code->stack, type, FALSE);
2431 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
2433 code->flags |= IL_CODE_FLAG_WAS_TARGET;
2434 if (mono_type_is_generic_argument (type))
2435 code->stack->stype |= BOXED_MASK;
2438 /*Verify if type 'candidate' can be stored in type 'target'.
2440 * If strict, check for the underlying type and not the verification stack types
2443 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
2445 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2446 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2448 MonoType *original_candidate = candidate;
2449 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
2451 /*only one is byref */
2452 if (candidate->byref ^ target->byref) {
2453 /* converting from native int to byref*/
2454 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2455 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2460 strict |= target->byref;
2461 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2462 candidate = mono_type_get_underlying_type_any (candidate);
2465 switch (target->type) {
2466 case MONO_TYPE_VOID:
2467 return candidate->type == MONO_TYPE_VOID;
2470 case MONO_TYPE_BOOLEAN:
2472 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2475 case MONO_TYPE_CHAR:
2477 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2479 case MONO_TYPE_U4: {
2480 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2481 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2483 return is_native_int || is_int4;
2484 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2489 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2494 return candidate->type == target->type;
2495 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2499 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2500 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2502 return is_native_int || is_int4;
2503 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2507 if (candidate->type != MONO_TYPE_PTR)
2509 /* check the underlying type */
2510 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2512 case MONO_TYPE_FNPTR: {
2513 MonoMethodSignature *left, *right;
2514 if (candidate->type != MONO_TYPE_FNPTR)
2517 left = mono_type_get_signature (target);
2518 right = mono_type_get_signature (candidate);
2519 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2522 case MONO_TYPE_GENERICINST: {
2523 MonoClass *target_klass;
2524 MonoClass *candidate_klass;
2525 if (mono_type_is_enum_type (target)) {
2526 target = mono_type_get_underlying_type_any (target);
2529 target_klass = mono_class_from_mono_type (target);
2530 candidate_klass = mono_class_from_mono_type (candidate);
2531 if (mono_class_is_nullable (target_klass)) {
2532 if (!mono_class_is_nullable (candidate_klass))
2534 return target_klass == candidate_klass;
2537 return mono_class_is_assignable_from (target_klass, candidate_klass);
2540 case MONO_TYPE_STRING:
2541 return candidate->type == MONO_TYPE_STRING;
2543 case MONO_TYPE_CLASS:
2545 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2546 * to take boxing status into account.
2548 if (mono_type_is_generic_argument (original_candidate))
2550 /* If candidate is an enum it should return true for System.Enum and supertypes.
2551 * That's why here we use the original type and not the underlying type.
2553 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2555 case MONO_TYPE_OBJECT:
2556 return MONO_TYPE_IS_REFERENCE (candidate);
2558 case MONO_TYPE_SZARRAY: {
2561 if (candidate->type != MONO_TYPE_SZARRAY)
2564 left = mono_class_from_mono_type (target)->element_class;
2565 right = mono_class_from_mono_type (candidate)->element_class;
2566 return mono_class_is_assignable_from (left, right);
2569 case MONO_TYPE_ARRAY:
2570 if (candidate->type != MONO_TYPE_ARRAY)
2572 return is_array_type_compatible (target, candidate);
2574 case MONO_TYPE_TYPEDBYREF:
2575 return candidate->type == MONO_TYPE_TYPEDBYREF;
2577 case MONO_TYPE_VALUETYPE:
2578 if (candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass)
2580 if (mono_type_is_enum_type (target)) {
2581 target = mono_type_get_underlying_type_any (target);
2587 if (candidate->type != MONO_TYPE_VAR)
2589 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2591 case MONO_TYPE_MVAR:
2592 if (candidate->type != MONO_TYPE_MVAR)
2594 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2597 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2598 g_assert_not_reached ();
2607 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2609 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2613 * Returns the generic param bound to the context been verified.
2616 static MonoGenericParam*
2617 get_generic_param (VerifyContext *ctx, MonoType *param)
2619 guint16 param_num = mono_type_get_generic_param_num (param);
2620 if (param->type == MONO_TYPE_VAR) {
2621 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2622 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2625 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2628 /*param must be a MVAR */
2629 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2630 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2633 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2637 * is_compatible_boxed_valuetype:
2639 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2641 * @type The source type. It it tested to be of the proper type.
2642 * @candidate type of the boxed valuetype.
2643 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2644 * @strict if TRUE candidate must be boxed compatible to the target type
2648 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2650 if (!stack_slot_is_boxed_value (stack))
2652 if (type->byref || candidate->byref)
2655 if (mono_type_is_generic_argument (candidate)) {
2656 MonoGenericParam *param = get_generic_param (ctx, candidate);
2658 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2659 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (& (*class)->byval_arg), FALSE))
2664 if (mono_type_is_generic_argument (type))
2670 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2674 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2676 MonoType *candidate = mono_type_from_stack_slot (stack);
2677 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2680 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2683 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2686 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2690 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2692 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2696 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2698 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2702 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2704 if (candidate->byref ^ target->byref)
2707 switch (target->type) {
2708 case MONO_TYPE_VOID:
2711 case MONO_TYPE_BOOLEAN:
2714 case MONO_TYPE_CHAR:
2723 case MONO_TYPE_STRING:
2724 case MONO_TYPE_TYPEDBYREF:
2725 return candidate->type == target->type;
2728 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2730 case MONO_TYPE_FNPTR:
2731 if (candidate->type != MONO_TYPE_FNPTR)
2733 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2735 case MONO_TYPE_GENERICINST: {
2736 MonoClass *target_klass;
2737 MonoClass *candidate_klass;
2738 target_klass = mono_class_from_mono_type (target);
2739 candidate_klass = mono_class_from_mono_type (candidate);
2740 /*FIXME handle nullables and enum*/
2741 return mono_class_is_assignable_from (target_klass, candidate_klass);
2743 case MONO_TYPE_OBJECT:
2744 return MONO_TYPE_IS_REFERENCE (candidate);
2746 case MONO_TYPE_CLASS:
2747 return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2749 case MONO_TYPE_SZARRAY:
2750 if (candidate->type != MONO_TYPE_SZARRAY)
2752 return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2754 case MONO_TYPE_ARRAY:
2755 if (candidate->type != MONO_TYPE_ARRAY)
2757 return is_array_type_compatible (target, candidate);
2759 case MONO_TYPE_VALUETYPE:
2760 /*FIXME handle nullables and enum*/
2761 return candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass;
2764 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2767 case MONO_TYPE_MVAR:
2768 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2772 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2773 g_assert_not_reached ();
2779 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2781 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2784 return mono_delegate_type_equal (method, delegate);
2788 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2790 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2793 return mono_delegate_type_equal (delegate, method);
2797 * mono_delegate_signature_equal:
2799 * Compare two signatures in the way expected by delegates.
2801 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2803 * FIXME can this function be eliminated and proper metadata functionality be used?
2806 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2809 int method_offset = is_static_ldftn ? 1 : 0;
2811 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2814 if (delegate_sig->call_convention != method_sig->call_convention)
2817 for (i = 0; i < delegate_sig->param_count; i++) {
2818 MonoType *p1 = delegate_sig->params [i];
2819 MonoType *p2 = method_sig->params [i + method_offset];
2821 if (!mono_delegate_param_equal (p1, p2))
2825 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2832 * verify_ldftn_delegate:
2834 * Verify properties of ldftn based delegates.
2837 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2839 MonoMethod *method = funptr->method;
2841 /*ldftn non-final virtuals only allowed if method is not static,
2842 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2843 * This rules doesn't apply if the object on stack is a boxed valuetype.
2845 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2846 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2847 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2848 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2850 /*current method must not be static*/
2851 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2852 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2854 /*value is the this pointer, loaded using ldarg.0 */
2855 if (!stack_slot_is_this_pointer (value))
2856 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object argument, it is not the this pointer, to ldftn with virtual method at 0x%04x", ctx->ip_offset));
2858 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2863 * verify_delegate_compatibility:
2865 * Verify delegate creation sequence.
2869 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2871 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2872 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2874 MonoMethod *invoke, *method;
2875 const guint8 *ip = ctx->header->code;
2876 guint32 ip_offset = ctx->ip_offset;
2877 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2879 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2880 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2884 invoke = mono_get_delegate_invoke (delegate);
2885 method = funptr->method;
2887 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2889 if (is_static_ldftn)
2890 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2892 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2893 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2894 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2895 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer signature '%s' doesn't match delegate's signatuere '%s' at 0x%04x", fun_sig, invoke_sig, ctx->ip_offset));
2897 g_free (invoke_sig);
2901 * Delegate code sequences:
2907 * [-6] ldvirtftn token
2911 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2912 verify_ldftn_delegate (ctx, delegate, value, funptr);
2913 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2914 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2916 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2918 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2921 if (is_first_arg_bound) {
2922 if (!verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2923 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2925 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2926 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2929 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2930 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2932 #undef IS_VALID_OPCODE
2933 #undef IS_LOAD_FUN_PTR
2936 /* implement the opcode checks*/
2938 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2942 if (arg >= ctx->max_args) {
2944 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2946 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2947 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2948 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2950 } else if (check_overflow (ctx)) {
2951 /*We must let the value be pushed, otherwise we would get an underflow error*/
2952 check_unverifiable_type (ctx, ctx->params [arg]);
2953 if (ctx->params [arg]->byref && take_addr)
2954 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2955 top = stack_push (ctx);
2956 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2959 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2961 ctx->has_this_store = TRUE;
2963 top->stype |= THIS_POINTER_MASK;
2964 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2965 top->stype |= UNINIT_THIS_MASK;
2971 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2973 if (arg >= ctx->num_locals) {
2974 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2975 } else if (check_overflow (ctx)) {
2976 /*We must let the value be pushed, otherwise we would get an underflow error*/
2977 check_unverifiable_type (ctx, ctx->locals [arg]);
2978 if (ctx->locals [arg]->byref && take_addr)
2979 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2981 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2986 store_arg (VerifyContext *ctx, guint32 arg)
2990 if (arg >= ctx->max_args) {
2991 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2992 if (check_underflow (ctx, 1))
2997 if (check_underflow (ctx, 1)) {
2998 value = stack_pop (ctx);
2999 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
3000 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3003 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
3004 ctx->has_this_store = 1;
3008 store_local (VerifyContext *ctx, guint32 arg)
3011 if (arg >= ctx->num_locals) {
3012 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
3016 /*TODO verify definite assigment */
3017 if (check_underflow (ctx, 1)) {
3018 value = stack_pop(ctx);
3019 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
3020 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
3021 stack_slot_get_name (value),
3022 mono_type_get_stack_name (ctx->locals [arg]),
3028 /*FIXME add and sub needs special care here*/
3030 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
3032 ILStackDesc *a, *b, *top;
3033 int idxa, idxb, complexMerge = 0;
3036 if (!check_underflow (ctx, 2))
3038 b = stack_pop (ctx);
3039 a = stack_pop (ctx);
3041 idxa = stack_slot_get_underlying_type (a);
3042 if (stack_slot_is_managed_pointer (a)) {
3047 idxb = stack_slot_get_underlying_type (b);
3048 if (stack_slot_is_managed_pointer (b)) {
3055 res = table [idxa][idxb];
3057 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
3058 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3060 top = stack_push (ctx);
3061 if (res == TYPE_INV) {
3062 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction applyed to ill formed stack (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
3063 copy_stack_value (top, a);
3067 if (res & NON_VERIFIABLE_RESULT) {
3068 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
3070 res = res & ~NON_VERIFIABLE_RESULT;
3073 if (complexMerge && res == TYPE_PTR) {
3074 if (complexMerge == 1)
3075 copy_stack_value (top, a);
3076 else if (complexMerge == 2)
3077 copy_stack_value (top, b);
3079 * There is no need to merge the type of two pointers.
3080 * The only valid operation is subtraction, that returns a native
3081 * int as result and can be used with any 2 pointer kinds.
3082 * This is valid acording to Patition III 1.1.4
3091 do_boolean_branch_op (VerifyContext *ctx, int delta)
3093 int target = ctx->ip_offset + delta;
3096 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3098 if (target < 0 || target >= ctx->code_size) {
3099 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
3103 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3105 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3108 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3112 ctx->target = target;
3114 if (!check_underflow (ctx, 1))
3117 top = stack_pop (ctx);
3118 if (!is_valid_bool_arg (top))
3119 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
3121 check_unmanaged_pointer (ctx, top);
3125 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
3127 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
3131 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
3136 int target = ctx->ip_offset + delta;
3138 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3140 if (target < 0 || target >= ctx->code_size) {
3141 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3145 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3146 case 1: /*FIXME use constants and not magic numbers.*/
3147 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3150 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3154 ctx->target = target;
3156 if (!check_underflow (ctx, 2))
3159 b = stack_pop (ctx);
3160 a = stack_pop (ctx);
3162 idxa = stack_slot_get_underlying_type (a);
3163 if (stack_slot_is_managed_pointer (a))
3166 idxb = stack_slot_get_underlying_type (b);
3167 if (stack_slot_is_managed_pointer (b))
3170 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3175 res = table [idxa][idxb];
3178 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
3179 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3181 if (res == TYPE_INV) {
3182 CODE_NOT_VERIFIABLE (ctx,
3183 g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
3184 } else if (res & NON_VERIFIABLE_RESULT) {
3185 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
3186 res = res & ~NON_VERIFIABLE_RESULT;
3191 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
3197 if (!check_underflow (ctx, 2))
3199 b = stack_pop (ctx);
3200 a = stack_pop (ctx);
3202 if (opcode == CEE_CGT_UN) {
3203 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
3204 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3209 idxa = stack_slot_get_underlying_type (a);
3210 if (stack_slot_is_managed_pointer (a))
3213 idxb = stack_slot_get_underlying_type (b);
3214 if (stack_slot_is_managed_pointer (b))
3217 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3222 res = table [idxa][idxb];
3225 if(res == TYPE_INV) {
3226 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
3227 } else if (res & NON_VERIFIABLE_RESULT) {
3228 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
3229 res = res & ~NON_VERIFIABLE_RESULT;
3231 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3235 do_ret (VerifyContext *ctx)
3237 MonoType *ret = ctx->signature->ret;
3238 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3239 if (ret->type != MONO_TYPE_VOID) {
3241 if (!check_underflow (ctx, 1))
3244 top = stack_pop(ctx);
3246 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3247 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
3251 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3252 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3255 if (ctx->eval.size > 0) {
3256 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3258 if (in_any_block (ctx->header, ctx->ip_offset))
3259 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3263 * FIXME we need to fix the case of a non-virtual instance method defined in the parent but call using a token pointing to a subclass.
3264 * This is illegal but mono_get_method_full decoded it.
3265 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3268 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
3271 MonoMethodSignature *sig;
3274 gboolean virt_check_this = FALSE;
3275 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3277 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
3281 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3283 if (method->klass->valuetype) // && !constrained ???
3284 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3286 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3287 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3290 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3291 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3293 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL)) {
3294 virt_check_this = TRUE;
3295 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3299 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
3300 sig = mono_method_get_signature (method, ctx->image, method_token);
3302 param_count = sig->param_count + sig->hasthis;
3303 if (!check_underflow (ctx, param_count))
3306 for (i = sig->param_count - 1; i >= 0; --i) {
3307 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3308 value = stack_pop (ctx);
3309 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
3310 char *stack_name = stack_slot_full_name (value);
3311 char *sig_name = mono_type_full_name (sig->params [i]);
3312 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature: %s X %s at 0x%04x", sig_name, stack_name, ctx->ip_offset));
3313 g_free (stack_name);
3317 if (stack_slot_is_managed_mutability_pointer (value))
3318 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of %s at 0x%04x", virtual ? "callvirt" : "call", ctx->ip_offset));
3320 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3321 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot pass a byref argument to a tail %s at 0x%04x", virtual ? "callvirt" : "call", ctx->ip_offset));
3327 MonoType *type = &method->klass->byval_arg;
3330 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3331 if (!mono_method_is_constructor (ctx->method))
3332 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3333 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3334 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor to a type diferent that this or super at 0x%04x", ctx->ip_offset));
3336 ctx->super_ctor_called = TRUE;
3337 value = stack_pop_safe (ctx);
3338 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3339 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3341 value = stack_pop (ctx);
3344 copy_stack_value (©, value);
3345 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3346 //Other parts of the code suffer from the same issue of
3347 copy.type = mono_type_get_type_byval (copy.type);
3348 copy.stype &= ~POINTER_MASK;
3350 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3351 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a non-final virtual method from an objet diferent thant the this pointer at 0x%04x", ctx->ip_offset));
3353 if (constrained && virtual) {
3354 if (!stack_slot_is_managed_pointer (value))
3355 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3356 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
3357 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3358 copy.stype |= BOXED_MASK;
3360 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3361 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a reference type using a managed pointer to the this arg at 0x%04x", ctx->ip_offset));
3363 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3364 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3366 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3367 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3369 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3370 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a boxed or literal valuetype to call a valuetype method at 0x%04x", ctx->ip_offset));
3372 if (!verify_stack_type_compatibility (ctx, type, ©))
3373 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
3375 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, value->type->data.klass)) {
3376 char *name = mono_method_full_name (method, TRUE);
3377 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3381 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3382 char *name = mono_method_full_name (method, TRUE);
3383 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3387 if (sig->ret->type != MONO_TYPE_VOID) {
3388 if (check_overflow (ctx)) {
3389 value = stack_push (ctx);
3390 set_stack_value (ctx, value, sig->ret, FALSE);
3391 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3392 ctx->prefix_set &= ~PREFIX_READONLY;
3393 value->stype |= CMMP_MASK;
3398 if ((ctx->prefix_set & PREFIX_TAIL)) {
3399 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3400 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3401 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3402 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3408 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3410 MonoClassField *field;
3413 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3415 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3418 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3419 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3422 /*taking the address of initonly field only works from the static constructor */
3423 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3424 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3425 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3427 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3428 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3430 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3434 do_store_static_field (VerifyContext *ctx, int token) {
3435 MonoClassField *field;
3438 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3440 if (!check_underflow (ctx, 1))
3443 value = stack_pop (ctx);
3445 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3448 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3449 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3453 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3454 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3458 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3459 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3461 if (!verify_stack_type_compatibility (ctx, field->type, value))
3462 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in static field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3466 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3468 MonoClassField *field;
3470 gboolean is_pointer;
3472 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3473 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3477 //the value on stack is going to be used as a pointer
3478 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3480 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3481 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3484 g_assert (obj->type);
3486 /*The value on the stack must be a subclass of the defining type of the field*/
3487 /* we need to check if we can load the field from the stack value*/
3489 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3490 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3492 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3493 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3495 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3496 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a managed pointer to a reference type and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3498 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3499 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3500 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a boxed valuetype and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3502 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE))
3503 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3505 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3506 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3509 check_unmanaged_pointer (ctx, obj);
3514 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3517 MonoClassField *field;
3520 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3522 if (!check_underflow (ctx, 1))
3524 obj = stack_pop_safe (ctx);
3526 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3529 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3530 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3532 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3533 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3534 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3536 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3540 do_store_field (VerifyContext *ctx, int token)
3542 ILStackDesc *value, *obj;
3543 MonoClassField *field;
3544 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3546 if (!check_underflow (ctx, 2))
3549 value = stack_pop (ctx);
3550 obj = stack_pop_safe (ctx);
3552 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3555 if (!verify_stack_type_compatibility (ctx, field->type, value))
3556 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3559 /*TODO proper handle for Nullable<T>*/
3561 do_box_value (VerifyContext *ctx, int klass_token)
3564 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3570 if (!check_underflow (ctx, 1))
3573 value = stack_pop (ctx);
3574 /*box is a nop for reference types*/
3576 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3577 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3582 if (!verify_stack_type_compatibility (ctx, type, value))
3583 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3585 klass = mono_class_from_mono_type (type);
3586 if (mono_class_is_nullable (klass))
3587 type = &mono_class_get_nullable_param (klass)->byval_arg;
3588 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3592 do_unbox_value (VerifyContext *ctx, int klass_token)
3595 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3600 if (!check_underflow (ctx, 1))
3603 if (!mono_class_from_mono_type (type)->valuetype)
3604 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3606 value = stack_pop (ctx);
3608 /*Value should be: a boxed valuetype or a reference type*/
3609 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3610 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3611 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3613 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3614 value->stype |= CMMP_MASK;
3618 do_unbox_any (VerifyContext *ctx, int klass_token)
3621 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3626 if (!check_underflow (ctx, 1))
3629 value = stack_pop (ctx);
3631 /*Value should be: a boxed valuetype or a reference type*/
3632 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3633 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3634 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox.any operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3636 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3640 do_unary_math_op (VerifyContext *ctx, int op)
3643 if (!check_underflow (ctx, 1))
3645 value = stack_pop (ctx);
3646 switch (stack_slot_get_type (value)) {
3649 case TYPE_NATIVE_INT:
3654 case TYPE_COMPLEX: /*only enums are ok*/
3655 if (mono_type_is_enum_type (value->type))
3658 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3660 stack_push_stack_val (ctx, value);
3664 do_conversion (VerifyContext *ctx, int kind)
3667 if (!check_underflow (ctx, 1))
3669 value = stack_pop (ctx);
3671 switch (stack_slot_get_type (value)) {
3674 case TYPE_NATIVE_INT:
3678 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type (%s) at stack for conversion operation. Numeric type expected at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3683 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3686 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3689 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3691 case TYPE_NATIVE_INT:
3692 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3695 g_error ("unknown type %02x in conversion", kind);
3701 do_load_token (VerifyContext *ctx, int token)
3704 MonoClass *handle_class;
3705 if (!check_overflow (ctx))
3707 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3709 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3712 if (handle_class == mono_defaults.typehandle_class) {
3713 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3714 } else if (handle_class == mono_defaults.methodhandle_class) {
3715 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3716 } else if (handle_class == mono_defaults.fieldhandle_class) {
3717 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3719 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3721 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3725 do_ldobj_value (VerifyContext *ctx, int token)
3728 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3729 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3734 if (!check_underflow (ctx, 1))
3737 value = stack_pop (ctx);
3738 if (!stack_slot_is_managed_pointer (value)
3739 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3740 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3741 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3745 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3746 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3748 /*We have a byval on the stack, but the comparison must be strict. */
3749 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3750 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3752 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3756 do_stobj (VerifyContext *ctx, int token)
3758 ILStackDesc *dest, *src;
3759 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3760 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3765 if (!check_underflow (ctx, 2))
3768 src = stack_pop (ctx);
3769 dest = stack_pop (ctx);
3771 if (stack_slot_is_managed_mutability_pointer (dest))
3772 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3774 if (!stack_slot_is_managed_pointer (dest))
3775 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3777 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3778 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
3780 if (!verify_stack_type_compatibility (ctx, type, src))
3781 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
3783 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3784 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3788 do_cpobj (VerifyContext *ctx, int token)
3790 ILStackDesc *dest, *src;
3791 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3795 if (!check_underflow (ctx, 2))
3798 src = stack_pop (ctx);
3799 dest = stack_pop (ctx);
3801 if (!stack_slot_is_managed_pointer (src))
3802 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3804 if (!stack_slot_is_managed_pointer (dest))
3805 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3807 if (stack_slot_is_managed_mutability_pointer (dest))
3808 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3810 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3811 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3813 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3814 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3818 do_initobj (VerifyContext *ctx, int token)
3821 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3825 if (!check_underflow (ctx, 1))
3828 obj = stack_pop (ctx);
3830 if (!stack_slot_is_managed_pointer (obj))
3831 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3833 if (stack_slot_is_managed_mutability_pointer (obj))
3834 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3836 stack = mono_type_get_type_byval (obj->type);
3837 if (MONO_TYPE_IS_REFERENCE (stack)) {
3838 if (!verify_type_compatibility (ctx, stack, type))
3839 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3840 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3841 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3842 } else if (!verify_type_compatibility (ctx, stack, type)) {
3843 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3848 do_newobj (VerifyContext *ctx, int token)
3852 MonoMethodSignature *sig;
3854 gboolean is_delegate = FALSE;
3856 if (!(method = verifier_load_method (ctx, token, "newobj")))
3859 if (!mono_method_is_constructor (method)) {
3860 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3864 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3865 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3867 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
3868 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor not visible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3870 //FIXME use mono_method_get_signature_full
3871 sig = mono_method_signature (method);
3872 if (!check_underflow (ctx, sig->param_count))
3875 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3878 ILStackDesc *funptr;
3879 //first arg is object, second arg is fun ptr
3880 if (sig->param_count != 2) {
3881 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3884 funptr = stack_pop (ctx);
3885 value = stack_pop (ctx);
3886 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3888 for (i = sig->param_count - 1; i >= 0; --i) {
3889 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3890 value = stack_pop (ctx);
3891 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3892 char *stack_name = stack_slot_full_name (value);
3893 char *sig_name = mono_type_full_name (sig->params [i]);
3894 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with constructor signature: %s X %s at 0x%04x", sig_name, stack_name, ctx->ip_offset));
3895 g_free (stack_name);
3899 if (stack_slot_is_managed_mutability_pointer (value))
3900 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3904 if (check_overflow (ctx))
3905 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3909 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3915 if (!check_underflow (ctx, 1))
3918 if (!(type = verifier_load_type (ctx, token, opcode)))
3922 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3926 value = stack_pop (ctx);
3927 is_boxed = stack_slot_is_boxed_value (value);
3929 if (stack_slot_is_managed_pointer (value))
3930 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3931 else if (mono_class_from_mono_type (value->type)->valuetype && !is_boxed)
3932 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Value cannot be a valuetype for %s at 0x%04x", opcode, ctx->ip_offset));
3934 switch (value->type->type) {
3935 case MONO_TYPE_FNPTR:
3937 case MONO_TYPE_TYPEDBYREF:
3938 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3941 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3942 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3946 mono_type_from_opcode (int opcode) {
3954 return &mono_defaults.sbyte_class->byval_arg;
3962 return &mono_defaults.int16_class->byval_arg;
3970 return &mono_defaults.int32_class->byval_arg;
3976 return &mono_defaults.int64_class->byval_arg;
3982 return &mono_defaults.single_class->byval_arg;
3988 return &mono_defaults.double_class->byval_arg;
3994 return &mono_defaults.int_class->byval_arg;
3998 case CEE_LDELEM_REF:
3999 case CEE_STELEM_REF:
4000 return &mono_defaults.object_class->byval_arg;
4003 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
4009 do_load_indirect (VerifyContext *ctx, int opcode)
4012 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
4014 if (!check_underflow (ctx, 1))
4017 value = stack_pop (ctx);
4018 if (!stack_slot_is_managed_pointer (value)) {
4019 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
4020 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4024 if (opcode == CEE_LDIND_REF) {
4025 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
4026 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
4027 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
4029 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
4030 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4031 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4036 do_store_indirect (VerifyContext *ctx, int opcode)
4038 ILStackDesc *addr, *val;
4039 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
4041 if (!check_underflow (ctx, 2))
4044 val = stack_pop (ctx);
4045 addr = stack_pop (ctx);
4047 check_unmanaged_pointer (ctx, addr);
4049 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
4050 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
4054 if (stack_slot_is_managed_mutability_pointer (addr)) {
4055 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
4059 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
4060 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4062 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
4063 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4067 do_newarr (VerifyContext *ctx, int token)
4070 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
4075 if (!check_underflow (ctx, 1))
4078 value = stack_pop (ctx);
4079 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4080 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Array size type on stack (%s) is not a verifiable type at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
4082 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
4085 /*FIXME handle arrays that are not 0-indexed*/
4087 do_ldlen (VerifyContext *ctx)
4091 if (!check_underflow (ctx, 1))
4094 value = stack_pop (ctx);
4096 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
4097 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
4099 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
4102 /*FIXME handle arrays that are not 0-indexed*/
4103 /*FIXME handle readonly prefix and CMMP*/
4105 do_ldelema (VerifyContext *ctx, int klass_token)
4107 ILStackDesc *index, *array, *res;
4108 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
4114 if (!check_underflow (ctx, 2))
4117 index = stack_pop (ctx);
4118 array = stack_pop (ctx);
4120 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4121 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelema is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
4123 if (!stack_slot_is_null_literal (array)) {
4124 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4125 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4127 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
4128 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
4130 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
4133 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
4137 res = stack_push (ctx);
4138 set_stack_value (ctx, res, type, TRUE);
4139 if (ctx->prefix_set & PREFIX_READONLY) {
4140 ctx->prefix_set &= ~PREFIX_READONLY;
4141 res->stype |= CMMP_MASK;
4146 * FIXME handle arrays that are not 0-indexed
4147 * FIXME handle readonly prefix and CMMP
4150 do_ldelem (VerifyContext *ctx, int opcode, int token)
4152 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4153 ILStackDesc *index, *array;
4155 if (!check_underflow (ctx, 2))
4158 if (opcode == CEE_LDELEM_ANY) {
4159 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
4160 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4164 type = mono_type_from_opcode (opcode);
4167 index = stack_pop (ctx);
4168 array = stack_pop (ctx);
4170 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4171 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
4173 if (!stack_slot_is_null_literal (array)) {
4174 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4175 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4177 if (opcode == CEE_LDELEM_REF) {
4178 if (array->type->data.klass->valuetype)
4179 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
4180 type = &array->type->data.klass->byval_arg;
4182 MonoType *candidate = &array->type->data.klass->byval_arg;
4183 if (IS_STRICT_MODE (ctx)) {
4184 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
4185 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
4186 if ((IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I, MONO_TYPE_U)) ||
4187 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
4188 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4190 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
4191 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4196 set_stack_value (ctx, stack_push (ctx), type, FALSE);
4201 * FIXME handle arrays that are not 0-indexed
4204 do_stelem (VerifyContext *ctx, int opcode, int token)
4206 ILStackDesc *index, *array, *value;
4208 if (!check_underflow (ctx, 3))
4211 if (opcode == CEE_STELEM_ANY) {
4212 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
4213 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4217 type = mono_type_from_opcode (opcode);
4220 value = stack_pop (ctx);
4221 index = stack_pop (ctx);
4222 array = stack_pop (ctx);
4224 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4225 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for stdelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
4227 if (!stack_slot_is_null_literal (array)) {
4228 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
4229 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for stelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4231 if (opcode == CEE_STELEM_REF) {
4232 if (array->type->data.klass->valuetype)
4233 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4234 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
4235 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4239 if (opcode == CEE_STELEM_REF) {
4240 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
4241 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4242 } else if (opcode != CEE_STELEM_REF) {
4243 if (!verify_stack_type_compatibility (ctx, type, value))
4244 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4246 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
4247 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
4253 do_throw (VerifyContext *ctx)
4255 ILStackDesc *exception;
4256 if (!check_underflow (ctx, 1))
4258 exception = stack_pop (ctx);
4260 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4261 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4263 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
4264 char *name = mono_type_full_name (exception->type);
4265 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type but found unboxed %s at 0x%04x ", name, ctx->ip_offset));
4268 /*The stack is left empty after a throw*/
4274 do_endfilter (VerifyContext *ctx)
4276 MonoExceptionClause *clause;
4278 if (IS_STRICT_MODE (ctx)) {
4279 if (ctx->eval.size != 1)
4280 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4282 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4283 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4286 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4287 if (IS_STRICT_MODE (ctx)) {
4288 if (ctx->ip_offset != clause->handler_offset - 2)
4289 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4291 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4292 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4295 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4296 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4298 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4305 do_leave (VerifyContext *ctx, int delta)
4307 int target = ((gint32)ctx->ip_offset) + delta;
4308 if (target >= ctx->code_size || target < 0)
4309 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4311 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4312 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4319 * Verify br and br.s opcodes.
4322 do_static_branch (VerifyContext *ctx, int delta)
4324 int target = ctx->ip_offset + delta;
4325 if (target < 0 || target >= ctx->code_size) {
4326 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4330 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4332 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4335 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4339 ctx->target = target;
4343 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4345 int i, base = ctx->ip_offset + 5 + count * 4;
4348 if (!check_underflow (ctx, 1))
4351 value = stack_pop (ctx);
4353 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4354 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4356 for (i = 0; i < count; ++i) {
4357 int target = base + read32 (data + i * 4);
4359 if (target < 0 || target >= ctx->code_size) {
4360 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4364 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4366 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4369 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4372 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4377 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4382 if (virtual && !check_underflow (ctx, 1))
4385 if (!virtual && !check_overflow (ctx))
4388 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4389 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4393 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4396 if (mono_method_is_constructor (method))
4397 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4400 ILStackDesc *top = stack_pop (ctx);
4402 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4403 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4405 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4406 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4408 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4409 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4412 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4413 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Loaded method is not visible for ldftn/ldvirtftn at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
4415 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4416 top->method = method;
4420 do_sizeof (VerifyContext *ctx, int token)
4424 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4425 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4429 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4432 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4433 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4437 if (type->type == MONO_TYPE_VOID) {
4438 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4442 if (check_overflow (ctx))
4443 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4446 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4448 do_localloc (VerifyContext *ctx)
4452 if (ctx->eval.size != 1) {
4453 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4457 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4458 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4462 /*TODO verify top type*/
4463 top = stack_pop (ctx);
4465 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4466 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4470 do_ldstr (VerifyContext *ctx, guint32 token)
4472 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4473 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4477 if (!ctx->image->dynamic && mono_metadata_token_index (token) >= ctx->image->heap_us.size) {
4478 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4482 if (check_overflow (ctx))
4483 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4487 do_refanyval (VerifyContext *ctx, int token)
4491 if (!check_underflow (ctx, 1))
4494 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4497 top = stack_pop (ctx);
4499 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4500 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanyval, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4502 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4506 do_refanytype (VerifyContext *ctx)
4510 if (!check_underflow (ctx, 1))
4513 top = stack_pop (ctx);
4515 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4516 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanytype, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4518 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4523 do_mkrefany (VerifyContext *ctx, int token)
4527 if (!check_underflow (ctx, 1))
4530 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4533 top = stack_pop (ctx);
4535 if (stack_slot_is_managed_mutability_pointer (top))
4536 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4538 if (!stack_slot_is_managed_pointer (top)) {
4539 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a managed pointer for mkrefany, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4541 MonoType *stack_type = mono_type_get_type_byval (top->type);
4542 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4543 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4545 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4546 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4549 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4553 do_ckfinite (VerifyContext *ctx)
4556 if (!check_underflow (ctx, 1))
4559 top = stack_pop (ctx);
4561 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4562 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected float32 or float64 on stack for ckfinit but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4563 stack_push_stack_val (ctx, top);
4567 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4569 * @from holds new values for a given control path
4570 * @to holds the current values of a given control path
4572 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4575 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4578 stack_init (ctx, to);
4581 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4584 stack_copy (&ctx->eval, to);
4586 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4587 stack_copy (to, &ctx->eval);
4590 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4592 if (from->size != to->size) {
4593 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4594 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d) at 0x%04x", from->size, to->size, ctx->ip_offset));
4598 //FIXME we need to preserve CMMP attributes
4599 //FIXME we must take null literals into consideration.
4600 for (i = 0; i < from->size; ++i) {
4601 ILStackDesc *new_slot = from->stack + i;
4602 ILStackDesc *old_slot = to->stack + i;
4603 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4604 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4605 MonoClass *old_class = mono_class_from_mono_type (old_type);
4606 MonoClass *new_class = mono_class_from_mono_type (new_type);
4607 MonoClass *match_class = NULL;
4609 // S := T then U = S (new value is compatible with current value, keep current)
4610 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4611 copy_stack_value (new_slot, old_slot);
4615 // T := S then U = T (old value is compatible with current value, use new)
4616 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4617 copy_stack_value (old_slot, new_slot);
4621 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4622 char *old_name = stack_slot_full_name (old_slot);
4623 char *new_name = stack_slot_full_name (new_slot);
4624 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
4630 //both are reference types, use closest common super type
4631 if (!mono_class_from_mono_type (old_type)->valuetype
4632 && !mono_class_from_mono_type (new_type)->valuetype
4633 && !stack_slot_is_managed_pointer (old_slot)
4634 && !stack_slot_is_managed_pointer (new_slot)) {
4636 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4637 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4638 match_class = old_class->supertypes [j];
4643 mono_class_setup_interfaces (old_class);
4644 for (j = 0; j < old_class->interface_count; ++j) {
4645 for (k = 0; k < new_class->interface_count; ++k) {
4646 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4647 match_class = old_class->interfaces [j];
4653 //No decent super type found, use object
4654 match_class = mono_defaults.object_class;
4656 } else if (is_compatible_boxed_valuetype (ctx,old_type, new_type, new_slot, FALSE) || is_compatible_boxed_valuetype (ctx, new_type, old_type, old_slot, FALSE)) {
4657 match_class = mono_defaults.object_class;
4662 char *old_name = stack_slot_full_name (old_slot);
4663 char *new_name = stack_slot_full_name (new_slot);
4664 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
4668 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4672 g_assert (match_class);
4673 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4674 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4680 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4681 to->flags |= IL_CODE_STACK_MERGED;
4684 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4685 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4688 * is_clause_in_range :
4690 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4693 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4695 if (clause->try_offset >= start && clause->try_offset < end)
4697 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4703 * is_clause_inside_range :
4705 * Returns TRUE if @clause lies completely inside the @start - @end range.
4708 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4710 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4712 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4718 * is_clause_nested :
4720 * Returns TRUE if @nested is nested in @clause.
4723 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4725 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4727 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4728 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4731 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4732 * the each pair of exception must have the following properties:
4733 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4734 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4735 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4738 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4740 /*clause is nested*/
4741 if (is_clause_nested (to_test, clause)) {
4742 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4743 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4748 /*wrong nesting order.*/
4749 if (is_clause_nested (clause, to_test)) {
4750 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4754 /*mutual protection*/
4755 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4756 /*handlers are not disjoint*/
4757 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4758 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4761 /* handlers are not catch or filter */
4762 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4763 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4770 /*not completelly disjoint*/
4771 if (is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4772 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len))
4773 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4776 #define code_bounds_check(size) \
4777 if (ip + size > end) {\
4778 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4783 * FIXME: need to distinguish between valid and verifiable.
4784 * Need to keep track of types on the stack.
4785 * Verify types for opcodes.
4788 mono_method_verify (MonoMethod *method, int level)
4790 const unsigned char *ip;
4791 const unsigned char *end;
4792 int i, n, need_merge = 0, start = 0;
4793 guint token, ip_offset = 0, prefix = 0;
4794 MonoGenericContext *generic_context = NULL;
4798 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4800 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4801 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4805 memset (&ctx, 0, sizeof (VerifyContext));
4807 //FIXME use mono_method_get_signature_full
4808 ctx.signature = mono_method_signature (method);
4809 if (!ctx.signature) {
4810 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4813 ctx.header = mono_method_get_header (method);
4815 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4818 ctx.method = method;
4819 ip = ctx.header->code;
4820 end = ip + ctx.header->code_size;
4821 ctx.image = image = method->klass->image;
4824 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4825 ctx.max_stack = ctx.header->max_stack;
4826 ctx.verifiable = ctx.valid = 1;
4829 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4830 ctx.code_size = ctx.header->code_size;
4832 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4835 ctx.num_locals = ctx.header->num_locals;
4836 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4838 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4839 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4841 ctx.params = g_new (MonoType*, ctx.max_args);
4842 if (ctx.signature->hasthis)
4843 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4844 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4846 if (ctx.signature->is_inflated)
4847 ctx.generic_context = generic_context = mono_method_get_context (method);
4849 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4850 if (method->is_generic)
4851 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4853 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4856 for (i = 0; i < ctx.num_locals; ++i)
4857 ctx.locals [i] = mono_class_inflate_generic_type (ctx.locals [i], ctx.generic_context);
4858 for (i = 0; i < ctx.max_args; ++i)
4859 ctx.params [i] = mono_class_inflate_generic_type (ctx.params [i], ctx.generic_context);
4860 stack_init (&ctx, &ctx.eval);
4862 for (i = 0; i < ctx.num_locals; ++i) {
4863 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i])) {
4864 /*TODO use the last error message to provide better feedback. */
4865 ADD_VERIFY_ERROR2 (&ctx, g_strdup_printf ("Invalid local variable %d", i), MONO_EXCEPTION_BAD_IMAGE);
4870 for (i = 0; i < ctx.max_args; ++i) {
4871 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i])) {
4872 /*TODO use the last error message to provide better feedback. */
4873 ADD_VERIFY_ERROR2 (&ctx, g_strdup_printf ("Invalid parameter %d", i), MONO_EXCEPTION_BAD_IMAGE);
4881 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4882 MonoExceptionClause *clause = ctx.header->clauses + i;
4883 VERIFIER_DEBUG (printf ("clause try %x len %x filter at %x handler at %x len %x\n", clause->try_offset, clause->try_len, clause->data.filter_offset, clause->handler_offset, clause->handler_len); );
4885 if (clause->try_offset > ctx.code_size || clause->try_offset + clause->try_len > ctx.code_size)
4886 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4888 if (clause->try_len <= 0)
4889 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4891 if (clause->handler_offset > ctx.code_size || clause->handler_offset + clause->handler_len > ctx.code_size)
4892 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4894 if (clause->handler_len <= 0)
4895 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4897 if (clause->try_offset < clause->handler_offset && clause->try_offset + clause->try_len > HANDLER_START (clause))
4898 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4900 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4901 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4906 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4907 if (clause->try_offset + clause->try_len < ctx.code_size)
4908 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4909 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4910 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4912 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4913 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4915 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4916 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4917 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4921 while (ip < end && ctx.valid) {
4922 ctx.ip_offset = ip_offset = ip - ctx.header->code;
4924 /*We need to check against fallthrou in and out of protected blocks.
4925 * For fallout we check the once a protected block ends, if the start flag is not set.
4926 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4927 * TODO convert these checks to be done using flags and not this loop
4929 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4930 MonoExceptionClause *clause = ctx.header->clauses + i;
4932 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4933 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4937 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4938 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4939 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4941 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4945 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4946 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4950 if (clause->handler_offset == ip_offset && start == 0) {
4951 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4955 if (clause->try_offset == ip_offset && ctx.eval.size > 0) {
4956 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4965 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4966 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4969 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4972 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4973 #ifdef MONO_VERIFIER_DEBUG
4976 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4977 discode [strlen (discode) - 1] = 0; /* no \n */
4978 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4981 dump_stack_state (&ctx.code [ip_offset]);
4982 dump_stack_state (&ctx.eval);
4995 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
5001 code_bounds_check (2);
5002 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
5006 case CEE_ADD_OVF_UN:
5007 do_binop (&ctx, *ip, add_ovf_un_table);
5011 case CEE_SUB_OVF_UN:
5012 do_binop (&ctx, *ip, sub_ovf_un_table);
5019 case CEE_MUL_OVF_UN:
5020 do_binop (&ctx, *ip, bin_ovf_table);
5025 do_binop (&ctx, *ip, add_table);
5030 do_binop (&ctx, *ip, sub_table);
5037 do_binop (&ctx, *ip, bin_op_table);
5046 do_binop (&ctx, *ip, int_bin_op_table);
5053 do_binop (&ctx, *ip, shift_op_table);
5058 if (!check_underflow (&ctx, 1))
5060 stack_pop_safe (&ctx);
5074 /*TODO support definite assignment verification? */
5075 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
5083 store_local (&ctx, *ip - CEE_STLOC_0);
5088 code_bounds_check (2);
5089 store_local (&ctx, ip [1]);
5094 code_bounds_check (2);
5095 store_arg (&ctx, ip [1]);
5109 if (check_overflow (&ctx))
5110 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5115 code_bounds_check (2);
5116 if (check_overflow (&ctx))
5117 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5122 code_bounds_check (5);
5123 if (check_overflow (&ctx))
5124 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
5129 code_bounds_check (9);
5130 if (check_overflow (&ctx))
5131 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
5136 code_bounds_check (5);
5137 if (check_overflow (&ctx))
5138 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5143 code_bounds_check (9);
5144 if (check_overflow (&ctx))
5145 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5150 if (check_overflow (&ctx))
5151 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
5157 code_bounds_check (2);
5158 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
5171 code_bounds_check (2);
5172 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5179 code_bounds_check (5);
5180 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5193 code_bounds_check (5);
5194 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5201 code_bounds_check (2);
5202 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5206 /* FIXME: warn/error instead? */
5213 if (!check_underflow (&ctx, 1))
5215 if (!check_overflow (&ctx))
5217 top = stack_pop_safe (&ctx);
5218 copy_stack_value (stack_push (&ctx), top);
5219 copy_stack_value (stack_push (&ctx), top);
5225 code_bounds_check (5);
5227 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5228 token = read32 (ip + 1);
5229 if (in_any_block (ctx.header, ip_offset))
5230 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5232 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5234 * FIXME: check signature, retval, arguments etc.
5240 code_bounds_check (5);
5241 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5246 code_bounds_check (5);
5247 token = read32 (ip + 1);
5249 * FIXME: check signature, retval, arguments etc.
5250 * FIXME: check requirements for tail call
5252 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5256 code_bounds_check (2);
5257 do_static_branch (&ctx, (signed char)ip [1] + 2);
5265 code_bounds_check (2);
5266 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5272 code_bounds_check (5);
5273 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5281 code_bounds_check (5);
5282 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5288 code_bounds_check (5);
5289 n = read32 (ip + 1);
5290 code_bounds_check (5 + sizeof (guint32) * n);
5292 do_switch (&ctx, n, (ip + 5));
5294 ip += 5 + sizeof (guint32) * n;
5308 do_load_indirect (&ctx, *ip);
5320 do_store_indirect (&ctx, *ip);
5326 do_unary_math_op (&ctx, *ip);
5336 do_conversion (&ctx, TYPE_I4);
5342 do_conversion (&ctx, TYPE_I8);
5349 do_conversion (&ctx, TYPE_R8);
5355 do_conversion (&ctx, TYPE_NATIVE_INT);
5360 code_bounds_check (5);
5361 do_cpobj (&ctx, read32 (ip + 1));
5366 code_bounds_check (5);
5367 do_ldobj_value (&ctx, read32 (ip + 1));
5372 code_bounds_check (5);
5373 do_ldstr (&ctx, read32 (ip + 1));
5378 code_bounds_check (5);
5379 do_newobj (&ctx, read32 (ip + 1));
5385 code_bounds_check (5);
5386 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5392 ++ip; /* warn, error ? */
5396 code_bounds_check (5);
5397 do_unbox_value (&ctx, read32 (ip + 1));
5409 code_bounds_check (5);
5410 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5416 code_bounds_check (5);
5417 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5422 code_bounds_check (5);
5423 do_store_field (&ctx, read32 (ip + 1));
5428 code_bounds_check (5);
5429 do_store_static_field (&ctx, read32 (ip + 1));
5434 code_bounds_check (5);
5435 do_stobj (&ctx, read32 (ip + 1));
5439 case CEE_CONV_OVF_I1_UN:
5440 case CEE_CONV_OVF_I2_UN:
5441 case CEE_CONV_OVF_I4_UN:
5442 case CEE_CONV_OVF_U1_UN:
5443 case CEE_CONV_OVF_U2_UN:
5444 case CEE_CONV_OVF_U4_UN:
5445 do_conversion (&ctx, TYPE_I4);
5449 case CEE_CONV_OVF_I8_UN:
5450 case CEE_CONV_OVF_U8_UN:
5451 do_conversion (&ctx, TYPE_I8);
5455 case CEE_CONV_OVF_I_UN:
5456 case CEE_CONV_OVF_U_UN:
5457 do_conversion (&ctx, TYPE_NATIVE_INT);
5462 code_bounds_check (5);
5463 do_box_value (&ctx, read32 (ip + 1));
5468 code_bounds_check (5);
5469 do_newarr (&ctx, read32 (ip + 1));
5479 code_bounds_check (5);
5480 do_ldelema (&ctx, read32 (ip + 1));
5494 case CEE_LDELEM_REF:
5495 do_ldelem (&ctx, *ip, 0);
5506 case CEE_STELEM_REF:
5507 do_stelem (&ctx, *ip, 0);
5511 case CEE_LDELEM_ANY:
5512 code_bounds_check (5);
5513 do_ldelem (&ctx, *ip, read32 (ip + 1));
5517 case CEE_STELEM_ANY:
5518 code_bounds_check (5);
5519 do_stelem (&ctx, *ip, read32 (ip + 1));
5524 code_bounds_check (5);
5525 do_unbox_any (&ctx, read32 (ip + 1));
5529 case CEE_CONV_OVF_I1:
5530 case CEE_CONV_OVF_U1:
5531 case CEE_CONV_OVF_I2:
5532 case CEE_CONV_OVF_U2:
5533 case CEE_CONV_OVF_I4:
5534 case CEE_CONV_OVF_U4:
5535 do_conversion (&ctx, TYPE_I4);
5539 case CEE_CONV_OVF_I8:
5540 case CEE_CONV_OVF_U8:
5541 do_conversion (&ctx, TYPE_I8);
5545 case CEE_CONV_OVF_I:
5546 case CEE_CONV_OVF_U:
5547 do_conversion (&ctx, TYPE_NATIVE_INT);
5552 code_bounds_check (5);
5553 do_refanyval (&ctx, read32 (ip + 1));
5563 code_bounds_check (5);
5564 do_mkrefany (&ctx, read32 (ip + 1));
5569 code_bounds_check (5);
5570 do_load_token (&ctx, read32 (ip + 1));
5574 case CEE_ENDFINALLY:
5575 if (!is_correct_endfinally (ctx.header, ip_offset))
5576 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5583 code_bounds_check (5);
5584 do_leave (&ctx, read32 (ip + 1) + 5);
5590 code_bounds_check (2);
5591 do_leave (&ctx, (signed char)ip [1] + 2);
5597 code_bounds_check (2);
5601 code_bounds_check (3);
5602 store_local (&ctx, read16 (ip + 1));
5607 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5615 do_cmp_op (&ctx, cmp_br_op, *ip);
5620 code_bounds_check (3);
5621 store_arg (&ctx, read16 (ip + 1) );
5627 check_overflow (&ctx);
5628 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5629 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5630 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5635 code_bounds_check (5);
5636 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5641 code_bounds_check (5);
5642 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5652 code_bounds_check (3);
5653 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5659 code_bounds_check (3);
5660 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5673 do_endfilter (&ctx);
5677 case CEE_UNALIGNED_:
5678 code_bounds_check (2);
5679 prefix |= PREFIX_UNALIGNED;
5683 prefix |= PREFIX_VOLATILE;
5687 prefix |= PREFIX_TAIL;
5689 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5690 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5694 code_bounds_check (5);
5695 do_initobj (&ctx, read32 (ip + 1));
5699 case CEE_CONSTRAINED_:
5700 code_bounds_check (5);
5701 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5702 prefix |= PREFIX_CONSTRAINED;
5707 prefix |= PREFIX_READONLY;
5712 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5713 if (!check_underflow (&ctx, 3))
5715 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5720 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5721 if (!check_underflow (&ctx, 3))
5723 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5731 if (!is_correct_rethrow (ctx.header, ip_offset))
5732 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5742 code_bounds_check (5);
5743 do_sizeof (&ctx, read32 (ip + 1));
5747 case CEE_REFANYTYPE:
5748 do_refanytype (&ctx);
5753 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5759 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5763 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5765 if (!ctx.prefix_set) //first prefix
5766 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5767 ctx.prefix_set |= prefix;
5768 ctx.has_flags = TRUE;
5772 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5774 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5775 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5776 if (ctx.prefix_set & PREFIX_READONLY)
5777 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5778 if (ctx.prefix_set & PREFIX_VOLATILE)
5779 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5780 if (ctx.prefix_set & PREFIX_UNALIGNED)
5781 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5782 ctx.prefix_set = prefix = 0;
5783 ctx.has_flags = FALSE;
5787 * if ip != end we overflowed: mark as error.
5789 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5790 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5793 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5794 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5795 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5796 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5797 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5799 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5800 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5802 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5803 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5805 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5806 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid call to a non-final virtual function in method with stdarg.0 or ldarga.0 at 0x%04x", i));
5809 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class)
5810 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor not calling super\n"));
5814 for (i = 0; i < ctx.header->code_size; ++i) {
5815 if (ctx.code [i].stack)
5816 g_free (ctx.code [i].stack);
5820 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5822 g_slist_free (ctx.funptrs);
5824 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5825 mono_metadata_free_type (tmp->data);
5826 g_slist_free (ctx.exception_types);
5828 for (i = 0; i < ctx.num_locals; ++i)
5829 mono_metadata_free_type (ctx.locals [i]);
5830 for (i = 0; i < ctx.max_args; ++i)
5831 mono_metadata_free_type (ctx.params [i]);
5834 g_free (ctx.eval.stack);
5837 g_free (ctx.locals);
5838 g_free (ctx.params);
5844 mono_verify_corlib ()
5846 /* This is a public API function so cannot be removed */
5850 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
5851 static gboolean verify_all = FALSE;
5854 * Set the desired level of checks for the verfier.
5858 mono_verifier_set_mode (MiniVerifierMode mode)
5860 verifier_mode = mode;
5864 mono_verifier_enable_verify_all ()
5870 * Returns true if @method needs to be verified.
5874 mono_verifier_is_enabled_for_method (MonoMethod *method)
5876 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5880 * Returns true if @klass need to be verified.
5884 mono_verifier_is_enabled_for_class (MonoClass *klass)
5886 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !klass->image->assembly->in_gac && klass->image != mono_defaults.corlib);
5890 mono_verifier_is_method_full_trust (MonoMethod *method)
5892 return mono_verifier_is_class_full_trust (method->klass);
5896 * Returns if @klass is under full trust or not.
5898 * TODO This code doesn't take CAS into account.
5900 * Under verify_all all user code must be verifiable if no security option was set
5904 mono_verifier_is_class_full_trust (MonoClass *klass)
5906 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5907 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
5908 klass->image->assembly->in_gac : mono_security_core_clr_is_platform_image (klass->image);
5910 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5911 return trusted_location || klass->image == mono_defaults.corlib;
5912 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
5916 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5918 return mono_method_verify (method,
5919 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5920 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5921 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5925 get_field_end (MonoClassField *field)
5928 int size = mono_type_size (field->type, &align);
5930 size = 4; /*FIXME Is this a safe bet?*/
5931 return size + field->offset;
5935 verify_class_for_overlapping_reference_fields (MonoClass *class)
5938 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5939 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) || !class->has_references)
5942 //we must check for stuff overlapping reference fields
5943 for (i = 0; i < class->field.count; ++i) {
5944 MonoClassField *field = &class->fields [i];
5945 int fieldEnd = get_field_end (field);
5946 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5947 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5950 for (j = i + 1; j < class->field.count; ++j) {
5951 MonoClassField *other = &class->fields [j];
5952 int otherEnd = get_field_end (other);
5953 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5956 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5959 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5967 * Check if the class is verifiable.
5969 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5970 * field and invalid generic instantiation are fatal errors.
5972 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5976 mono_verifier_verify_class (MonoClass *class)
5978 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
5980 if (!verify_class_for_overlapping_reference_fields (class))
5983 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))