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)
9 * Copyright 2011 Rodrigo Kumpera
13 #include <mono/metadata/object-internals.h>
14 #include <mono/metadata/verify.h>
15 #include <mono/metadata/verify-internals.h>
16 #include <mono/metadata/opcodes.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/reflection.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/mono-endian.h>
21 #include <mono/metadata/metadata.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/security-manager.h>
25 #include <mono/metadata/security-core-clr.h>
26 #include <mono/metadata/tokentype.h>
27 #include <mono/metadata/mono-basic-block.h>
28 #include <mono/metadata/attrdefs.h>
29 #include <mono/utils/mono-counters.h>
30 #include <mono/utils/monobitset.h>
35 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
36 static gboolean verify_all = FALSE;
39 * Set the desired level of checks for the verfier.
43 mono_verifier_set_mode (MiniVerifierMode mode)
49 mono_verifier_enable_verify_all ()
54 #ifndef DISABLE_VERIFIER
56 * Pull the list of opcodes
58 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
62 #include "mono/cil/opcode.def"
67 #ifdef MONO_VERIFIER_DEBUG
68 #define VERIFIER_DEBUG(code) do { code } while (0)
70 #define VERIFIER_DEBUG(code)
73 //////////////////////////////////////////////////////////////////
74 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
75 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
76 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
77 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
78 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
79 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
81 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
82 vinfo->info.status = __status; \
83 vinfo->info.message = ( __msg ); \
84 vinfo->exception_type = (__exception); \
85 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
88 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
89 #define ADD_VERIFY_ERROR(__ctx, __msg) \
91 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
95 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
97 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
98 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
99 (__ctx)->verifiable = 0; \
100 if (IS_FAIL_FAST_MODE (__ctx)) \
101 (__ctx)->valid = 0; \
105 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
107 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
108 (__ctx)->valid = 0; \
111 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
113 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
114 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
115 (__ctx)->verifiable = 0; \
116 if (IS_FAIL_FAST_MODE (__ctx)) \
117 (__ctx)->valid = 0; \
121 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
122 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
124 #if SIZEOF_VOID_P == 4
125 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
127 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
130 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
131 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
133 /*Flags to be used with ILCodeDesc::flags */
135 /*Instruction has not been processed.*/
136 IL_CODE_FLAG_NOT_PROCESSED = 0,
137 /*Instruction was decoded by mono_method_verify loop.*/
138 IL_CODE_FLAG_SEEN = 1,
139 /*Instruction was target of a branch or is at a protected block boundary.*/
140 IL_CODE_FLAG_WAS_TARGET = 2,
141 /*Used by stack_init to avoid double initialize each entry.*/
142 IL_CODE_FLAG_STACK_INITED = 4,
143 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
144 IL_CODE_STACK_MERGED = 8,
145 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
146 IL_CODE_DELEGATE_SEQUENCE = 0x10,
147 /*This is a delegate created from a ldftn to a non final virtual method*/
148 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
149 /*This is a call to a non final virtual method*/
150 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
168 guint16 size, max_size;
185 /*Allocated fnptr MonoType that should be freed by us.*/
187 /*Type dup'ed exception types from catch blocks.*/
188 GSList *exception_types;
193 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
197 MonoMethodSignature *signature;
198 MonoMethodHeader *header;
200 MonoGenericContext *generic_context;
204 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
205 *on a method that creates a delegate for a non-final virtual method using ldftn*/
206 gboolean has_this_store;
208 /*This flag is used to control if the contructor of the parent class has been called.
209 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
210 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
212 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
213 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
215 gboolean super_ctor_called;
219 MonoType *constrained_type;
223 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
226 get_stack_type (MonoType *type);
229 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
232 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
235 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
237 static MonoGenericParam*
238 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type);
241 verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate);
242 //////////////////////////////////////////////////////////////////
247 TYPE_INV = 0, /* leave at 0. */
252 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
254 /* value types and classes */
256 /* Number of types, used to define the size of the tables*/
259 /* Used by tables to signal that a result is not verifiable*/
260 NON_VERIFIABLE_RESULT = 0x80,
262 /*Mask used to extract just the type, excluding flags */
265 /* The stack type is a managed pointer, unmask the value to res */
266 POINTER_MASK = 0x100,
268 /*Stack type with the pointer mask*/
269 RAW_TYPE_MASK = 0x10F,
271 /* Controlled Mutability Manager Pointer */
274 /* The stack type is a null literal*/
275 NULL_LITERAL_MASK = 0x400,
277 /**Used by ldarg.0 and family to let delegate verification happens.*/
278 THIS_POINTER_MASK = 0x800,
280 /**Signals that this is a boxed value type*/
283 /*This is an unitialized this ref*/
284 UNINIT_THIS_MASK = 0x2000,
287 static const char* const
288 type_names [TYPE_MAX + 1] = {
299 PREFIX_UNALIGNED = 1,
302 PREFIX_CONSTRAINED = 8,
305 //////////////////////////////////////////////////////////////////
307 #ifdef ENABLE_VERIFIER_STATS
309 #define MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
310 #define MEM_FREE(amt) do { working_set -= (amt); } while (0)
312 static int allocated_memory;
313 static int working_set;
314 static int max_allocated_memory;
315 static int max_working_set;
316 static int total_allocated_memory;
319 finish_collect_stats (void)
321 max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
322 max_working_set = MAX (max_working_set, working_set);
323 total_allocated_memory += allocated_memory;
324 allocated_memory = working_set = 0;
328 init_verifier_stats (void)
330 static gboolean inited;
333 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
334 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
335 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
341 #define MEM_ALLOC(amt) do {} while (0)
342 #define MEM_FREE(amt) do { } while (0)
344 #define finish_collect_stats()
345 #define init_verifier_stats()
350 //////////////////////////////////////////////////////////////////
353 /*Token validation macros and functions */
354 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
355 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
356 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
357 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
359 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
360 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
361 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
362 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
363 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
364 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
367 * Verify if @token refers to a valid row on int's table.
370 token_bounds_check (MonoImage *image, guint32 token)
373 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
374 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token) && mono_metadata_token_index (token) > 0;
378 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
380 MonoType *res = g_new0 (MonoType, 1);
381 MEM_ALLOC (sizeof (MonoType));
383 //FIXME use mono_method_get_signature_full
384 res->data.method = mono_method_signature (method);
385 res->type = MONO_TYPE_FNPTR;
386 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
391 * mono_type_is_enum_type:
393 * Returns TRUE if @type is an enum type.
396 mono_type_is_enum_type (MonoType *type)
398 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
400 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
406 * mono_type_is_value_type:
408 * Returns TRUE if @type is named after @namespace.@name.
412 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
414 return type->type == MONO_TYPE_VALUETYPE &&
415 !strcmp (namespace, type->data.klass->name_space) &&
416 !strcmp (name, type->data.klass->name);
420 * Returns TURE if @type is VAR or MVAR
423 mono_type_is_generic_argument (MonoType *type)
425 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
429 * mono_type_get_underlying_type_any:
431 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
433 * Returns the underlying type of @type regardless if it is byref or not.
436 mono_type_get_underlying_type_any (MonoType *type)
438 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
439 return mono_class_enum_basetype (type->data.klass);
440 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
441 return mono_class_enum_basetype (type->data.generic_class->container_class);
445 static G_GNUC_UNUSED const char*
446 mono_type_get_stack_name (MonoType *type)
448 return type_names [get_stack_type (type) & TYPE_MASK];
451 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
452 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
455 mono_method_is_constructor (MonoMethod *method)
457 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
458 !(method->flags & CTOR_INVALID_FLAGS) &&
459 !strcmp (".ctor", method->name));
463 mono_class_has_default_constructor (MonoClass *klass)
468 mono_class_setup_methods (klass);
469 if (klass->exception_type)
472 for (i = 0; i < klass->method.count; ++i) {
473 method = klass->methods [i];
474 if (mono_method_is_constructor (method) &&
475 mono_method_signature (method) &&
476 mono_method_signature (method)->param_count == 0 &&
477 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
484 * Verify if @type is valid for the given @ctx verification context.
485 * this function checks for VAR and MVAR types that are invalid under the current verifier,
488 mono_type_is_valid_type_in_context_full (MonoType *type, MonoGenericContext *context, gboolean check_gtd)
491 MonoGenericInst *inst;
493 switch (type->type) {
498 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
499 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
502 case MONO_TYPE_SZARRAY:
503 return mono_type_is_valid_type_in_context_full (&type->data.klass->byval_arg, context, check_gtd);
504 case MONO_TYPE_ARRAY:
505 return mono_type_is_valid_type_in_context_full (&type->data.array->eklass->byval_arg, context, check_gtd);
507 return mono_type_is_valid_type_in_context_full (type->data.type, context, check_gtd);
508 case MONO_TYPE_GENERICINST:
509 inst = type->data.generic_class->context.class_inst;
512 for (i = 0; i < inst->type_argc; ++i)
513 if (!mono_type_is_valid_type_in_context_full (inst->type_argv [i], context, check_gtd))
516 case MONO_TYPE_CLASS:
517 case MONO_TYPE_VALUETYPE: {
518 MonoClass *klass = type->data.klass;
520 * It's possible to encode generic'sh types in such a way that they disguise themselves as class or valuetype.
521 * Fixing the type decoding is really tricky since under some cases this behavior is needed, for example, to
522 * have a 'class' type pointing to a 'genericinst' class.
524 * For the runtime these non canonical (weird) encodings work fine, the worst they can cause is some
525 * reflection oddities which are harmless - to security at least.
527 if (klass->byval_arg.type != type->type)
528 return mono_type_is_valid_type_in_context_full (&klass->byval_arg, context, check_gtd);
530 if (check_gtd && klass->generic_container)
539 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
541 return mono_type_is_valid_type_in_context_full (type, context, FALSE);
544 /*This function returns NULL if the type is not instantiatable*/
546 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
551 result = mono_class_inflate_generic_type_checked (type, context, &error);
552 if (!mono_error_ok (&error)) {
553 mono_error_cleanup (&error);
559 /*A side note here. We don't need to check if arguments are broken since this
560 is only need to be done by the runtime before realizing the type.
563 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
568 if (ginst->type_argc != gc->type_argc)
571 for (i = 0; i < gc->type_argc; ++i) {
572 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
573 MonoClass *paramClass;
574 MonoClass **constraints;
575 MonoType *param_type = ginst->type_argv [i];
577 /*it's not our job to validate type variables*/
578 if (mono_type_is_generic_argument (param_type))
581 paramClass = mono_class_from_mono_type (param_type);
584 /* A GTD can't be a generic argument.
586 * Due to how types are encoded we must check for the case of a genericinst MonoType and GTD MonoClass.
587 * This happens in cases such as: class Foo<T> { void X() { new Bar<T> (); } }
589 * Open instantiations can have GTDs as this happens when one type is instantiated with others params
590 * and the former has an expansion into the later. For example:
592 * class A<T>: B<K> {}
593 * The type A <K> has a parent B<K>, that is inflated into the GTD B<>.
594 * Since A<K> is open, thus not instantiatable, this is valid.
596 if (paramClass->generic_container && param_type->type != MONO_TYPE_GENERICINST && !ginst->is_open)
599 /*it's not safe to call mono_class_init from here*/
600 if (paramClass->generic_class && !paramClass->inited) {
601 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
605 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
608 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
611 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
614 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
617 if (!param_info->constraints)
620 for (constraints = param_info->constraints; *constraints; ++constraints) {
621 MonoClass *ctr = *constraints;
624 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
625 if (!mono_error_ok (&error)) {
626 mono_error_cleanup (&error);
629 ctr = mono_class_from_mono_type (inflated);
630 mono_metadata_free_type (inflated);
632 /*FIXME maybe we need the same this as verifier_class_is_assignable_from*/
633 if (!mono_class_is_assignable_from_slow (ctr, paramClass))
641 * Return true if @candidate is constraint compatible with @target.
643 * This means that @candidate constraints are a super set of @target constaints
646 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoClass *candidate_param_class, MonoGenericContext *context)
648 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
649 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
650 MonoClass **candidate_class;
651 gboolean class_constraint_satisfied = FALSE;
652 gboolean valuetype_constraint_satisfied = FALSE;
654 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
655 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
657 if (cinfo->constraints) {
658 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
660 MonoType *inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
663 cc = mono_class_from_mono_type (inflated);
664 mono_metadata_free_type (inflated);
666 if (mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
667 class_constraint_satisfied = TRUE;
668 else if (!mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
669 valuetype_constraint_satisfied = TRUE;
672 class_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0;
673 valuetype_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) != 0;
675 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && !class_constraint_satisfied)
677 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && !valuetype_constraint_satisfied)
679 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !((cmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) ||
680 valuetype_constraint_satisfied)) {
685 if (tinfo->constraints) {
686 MonoClass **target_class;
687 for (target_class = tinfo->constraints; *target_class; ++target_class) {
689 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
692 tc = mono_class_from_mono_type (inflated);
693 mono_metadata_free_type (inflated);
696 * A constraint from @target might inflate into @candidate itself and in that case we don't need
697 * check it's constraints since it satisfy the constraint by itself.
699 if (mono_metadata_type_equal (&tc->byval_arg, &candidate_param_class->byval_arg))
702 if (!cinfo->constraints)
705 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
707 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
710 cc = mono_class_from_mono_type (inflated);
711 mono_metadata_free_type (inflated);
713 if (verifier_class_is_assignable_from (tc, cc))
717 * This happens when we have the following:
719 * Bar<K> where K : IFace
720 * Foo<T, U> where T : U where U : IFace
722 * Bar<T> <- T here satisfy K constraint transitively through to U's constraint
725 if (mono_type_is_generic_argument (&cc->byval_arg)) {
726 MonoGenericParam *other_candidate = verifier_get_generic_param_from_type (ctx, &cc->byval_arg);
728 if (mono_generic_param_is_constraint_compatible (ctx, target, other_candidate, cc, context)) {
733 if (!*candidate_class)
740 static MonoGenericParam*
741 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
743 MonoGenericContainer *gc;
744 MonoMethod *method = ctx->method;
747 num = mono_type_get_generic_param_num (type);
749 if (type->type == MONO_TYPE_VAR) {
750 MonoClass *gtd = method->klass;
751 if (gtd->generic_class)
752 gtd = gtd->generic_class->container_class;
753 gc = gtd->generic_container;
755 MonoMethod *gmd = method;
756 if (method->is_inflated)
757 gmd = ((MonoMethodInflated*)method)->declaring;
758 gc = mono_method_get_generic_container (gmd);
762 return mono_generic_container_get_param (gc, num);
768 * Verify if @type is valid for the given @ctx verification context.
769 * this function checks for VAR and MVAR types that are invalid under the current verifier,
770 * This means that it either
773 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
775 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
779 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst, gboolean check_gtd)
782 for (i = 0; i < ginst->type_argc; ++i) {
783 MonoType *type = ginst->type_argv [i];
785 if (!mono_type_is_valid_type_in_context_full (type, ctx->generic_context, TRUE))
792 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
795 for (i = 0; i < ginst->type_argc; ++i) {
796 MonoType *type = ginst->type_argv [i];
797 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
798 MonoGenericParam *candidate;
799 MonoClass *candidate_class;
801 if (!mono_type_is_generic_argument (type))
804 if (!is_valid_type_in_context (ctx, type))
807 candidate = verifier_get_generic_param_from_type (ctx, type);
808 candidate_class = mono_class_from_mono_type (type);
810 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, candidate_class, context))
817 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
819 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
820 MonoGenericInst *ginst = gmethod->context.method_inst;
821 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
822 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
826 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
828 MonoGenericClass *gklass = klass->generic_class;
829 MonoGenericInst *ginst = gklass->context.class_inst;
830 MonoGenericContainer *gc = gklass->container_class->generic_container;
831 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
835 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
837 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
838 MonoGenericInst *ginst = gmethod->context.method_inst;
839 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
840 if (!gc) /*non-generic inflated method - it's part of a generic type */
842 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
844 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
849 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
851 MonoGenericClass *gklass = klass->generic_class;
852 MonoGenericInst *ginst = gklass->context.class_inst;
853 MonoGenericContainer *gc = gklass->container_class->generic_container;
854 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
856 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
860 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
865 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
869 if (!is_valid_type_in_context (ctx, type)) {
870 char *str = mono_type_full_name (type);
871 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
872 type->type == MONO_TYPE_VAR ? "!" : "!!",
874 type->type == MONO_TYPE_VAR ? "class" : "method",
876 MONO_EXCEPTION_BAD_IMAGE);
881 klass = mono_class_from_mono_type (type);
882 mono_class_init (klass);
883 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
884 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
885 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);
887 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);
888 mono_loader_clear_error ();
892 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
893 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);
897 if (!klass->generic_class)
900 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
901 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);
905 if (!mono_class_repect_method_constraints (ctx, klass)) {
906 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);
913 static verify_result_t
914 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
916 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
917 return RESULT_INVALID;
919 if (!method->is_inflated)
922 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
923 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);
924 return RESULT_INVALID;
927 if (!mono_method_repect_method_constraints (ctx, method)) {
928 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));
929 return RESULT_UNVERIFIABLE;
935 static MonoClassField*
936 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
937 MonoClassField *field;
938 MonoClass *klass = NULL;
940 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
941 field = mono_method_get_wrapper_data (ctx->method, (guint32)token);
942 klass = field ? field->parent : NULL;
944 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
945 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid field token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
949 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
952 if (!field || !field->parent || !klass || mono_loader_get_last_error ()) {
953 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);
954 mono_loader_clear_error ();
958 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
961 if (mono_field_get_flags (field) & FIELD_ATTRIBUTE_LITERAL) {
962 char *type_name = mono_type_get_full_name (field->parent);
963 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot reference literal field %s::%s at 0x%04x", type_name, field->name, ctx->ip_offset));
973 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
976 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
977 method = mono_method_get_wrapper_data (ctx->method, (guint32)token);
979 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
980 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);
984 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
987 if (!method || mono_loader_get_last_error ()) {
988 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);
989 mono_loader_clear_error ();
993 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
1000 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
1003 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
1004 MonoClass *class = mono_method_get_wrapper_data (ctx->method, (guint32)token);
1005 type = class ? &class->byval_arg : NULL;
1007 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
1008 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
1011 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
1014 if (!type || mono_loader_get_last_error ()) {
1015 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);
1016 mono_loader_clear_error ();
1020 if (!mono_type_is_valid_in_context (ctx, type))
1027 /* stack_slot_get_type:
1029 * Returns the stack type of @value. This value includes POINTER_MASK.
1031 * Use this function to checks that account for a managed pointer.
1034 stack_slot_get_type (ILStackDesc *value)
1036 return value->stype & RAW_TYPE_MASK;
1039 /* stack_slot_get_underlying_type:
1041 * Returns the stack type of @value. This value does not include POINTER_MASK.
1043 * Use this function is cases where the fact that the value could be a managed pointer is
1044 * irrelevant. For example, field load doesn't care about this fact of type on stack.
1047 stack_slot_get_underlying_type (ILStackDesc *value)
1049 return value->stype & TYPE_MASK;
1052 /* stack_slot_is_managed_pointer:
1054 * Returns TRUE is @value is a managed pointer.
1057 stack_slot_is_managed_pointer (ILStackDesc *value)
1059 return (value->stype & POINTER_MASK) == POINTER_MASK;
1062 /* stack_slot_is_managed_mutability_pointer:
1064 * Returns TRUE is @value is a managed mutability pointer.
1066 static G_GNUC_UNUSED gboolean
1067 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
1069 return (value->stype & CMMP_MASK) == CMMP_MASK;
1072 /* stack_slot_is_null_literal:
1074 * Returns TRUE is @value is the null literal.
1077 stack_slot_is_null_literal (ILStackDesc *value)
1079 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
1083 /* stack_slot_is_this_pointer:
1085 * Returns TRUE is @value is the this literal
1088 stack_slot_is_this_pointer (ILStackDesc *value)
1090 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
1093 /* stack_slot_is_boxed_value:
1095 * Returns TRUE is @value is a boxed value
1098 stack_slot_is_boxed_value (ILStackDesc *value)
1100 return (value->stype & BOXED_MASK) == BOXED_MASK;
1104 stack_slot_get_name (ILStackDesc *value)
1106 return type_names [value->stype & TYPE_MASK];
1109 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
1110 if (PRED (value)) { \
1112 g_string_append (str, ", "); \
1113 g_string_append (str, NAME); \
1118 stack_slot_stack_type_full_name (ILStackDesc *value)
1120 GString *str = g_string_new ("");
1122 gboolean has_pred = FALSE, first = TRUE;
1124 if ((value->stype & TYPE_MASK) != value->stype) {
1125 g_string_append(str, "[");
1126 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
1127 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
1128 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
1129 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
1130 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1134 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1136 g_string_append(str, "[");
1138 g_string_append (str, ", ");
1139 g_string_append (str, "unboxed");
1144 g_string_append(str, "] ");
1146 g_string_append (str, stack_slot_get_name (value));
1148 g_string_free (str, FALSE);
1153 stack_slot_full_name (ILStackDesc *value)
1155 char *type_name = mono_type_full_name (value->type);
1156 char *stack_name = stack_slot_stack_type_full_name (value);
1157 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1159 g_free (stack_name);
1163 //////////////////////////////////////////////////////////////////
1165 mono_free_verify_list (GSList *list)
1167 MonoVerifyInfoExtended *info;
1170 for (tmp = list; tmp; tmp = tmp->next) {
1172 g_free (info->info.message);
1175 g_slist_free (list);
1178 #define ADD_ERROR(list,msg) \
1180 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1181 vinfo->info.status = MONO_VERIFY_ERROR; \
1182 vinfo->info.message = (msg); \
1183 (list) = g_slist_prepend ((list), vinfo); \
1186 #define ADD_WARN(list,code,msg) \
1188 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1189 vinfo->info.status = (code); \
1190 vinfo->info.message = (msg); \
1191 (list) = g_slist_prepend ((list), vinfo); \
1194 #define ADD_INVALID(list,msg) \
1196 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1197 vinfo->status = MONO_VERIFY_ERROR; \
1198 vinfo->message = (msg); \
1199 (list) = g_slist_prepend ((list), vinfo); \
1200 /*G_BREAKPOINT ();*/ \
1204 #define CHECK_STACK_UNDERFLOW(num) \
1206 if (cur_stack < (num)) \
1207 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1210 #define CHECK_STACK_OVERFLOW() \
1212 if (cur_stack >= max_stack) \
1213 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1218 in_any_block (MonoMethodHeader *header, guint offset)
1221 MonoExceptionClause *clause;
1223 for (i = 0; i < header->num_clauses; ++i) {
1224 clause = &header->clauses [i];
1225 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1227 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1229 if (MONO_OFFSET_IN_FILTER (clause, offset))
1236 * in_any_exception_block:
1238 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1241 in_any_exception_block (MonoMethodHeader *header, guint offset)
1244 MonoExceptionClause *clause;
1246 for (i = 0; i < header->num_clauses; ++i) {
1247 clause = &header->clauses [i];
1248 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1250 if (MONO_OFFSET_IN_FILTER (clause, offset))
1257 * is_valid_branch_instruction:
1259 * Verify if it's valid to perform a branch from @offset to @target.
1260 * This should be used with br and brtrue/false.
1261 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1262 * The major difference from other similiar functions is that branching into a
1263 * finally/fault block is invalid instead of just unverifiable.
1266 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1269 MonoExceptionClause *clause;
1271 for (i = 0; i < header->num_clauses; ++i) {
1272 clause = &header->clauses [i];
1273 /*branching into a finally block is invalid*/
1274 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1275 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1276 MONO_OFFSET_IN_HANDLER (clause, target))
1279 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1281 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1283 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1290 * is_valid_cmp_branch_instruction:
1292 * Verify if it's valid to perform a branch from @offset to @target.
1293 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1294 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1296 * The major differences from other similar functions are that most errors lead to invalid
1297 * code and only branching out of finally, filter or fault clauses is unverifiable.
1300 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1303 MonoExceptionClause *clause;
1305 for (i = 0; i < header->num_clauses; ++i) {
1306 clause = &header->clauses [i];
1307 /*branching out of a handler or finally*/
1308 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1309 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1310 !MONO_OFFSET_IN_HANDLER (clause, target))
1313 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1315 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1317 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1324 * A leave can't escape a finally block
1327 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1330 MonoExceptionClause *clause;
1332 for (i = 0; i < header->num_clauses; ++i) {
1333 clause = &header->clauses [i];
1334 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1336 if (MONO_OFFSET_IN_FILTER (clause, offset))
1343 * A rethrow can't happen outside of a catch handler.
1346 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1349 MonoExceptionClause *clause;
1351 for (i = 0; i < header->num_clauses; ++i) {
1352 clause = &header->clauses [i];
1353 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1355 if (MONO_OFFSET_IN_FILTER (clause, offset))
1362 * An endfinally can't happen outside of a finally/fault handler.
1365 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1368 MonoExceptionClause *clause;
1370 for (i = 0; i < header->num_clauses; ++i) {
1371 clause = &header->clauses [i];
1372 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1380 * An endfilter can only happens inside a filter clause.
1381 * In non-strict mode filter is allowed inside the handler clause too
1383 static MonoExceptionClause *
1384 is_correct_endfilter (VerifyContext *ctx, guint offset)
1387 MonoExceptionClause *clause;
1389 for (i = 0; i < ctx->header->num_clauses; ++i) {
1390 clause = &ctx->header->clauses [i];
1391 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1393 if (MONO_OFFSET_IN_FILTER (clause, offset))
1395 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1403 * Non-strict endfilter can happens inside a try block or any handler block
1406 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1409 MonoExceptionClause *clause;
1411 for (i = 0; i < ctx->header->num_clauses; ++i) {
1412 clause = &ctx->header->clauses [i];
1413 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1420 is_valid_bool_arg (ILStackDesc *arg)
1422 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1426 switch (stack_slot_get_underlying_type (arg)) {
1429 case TYPE_NATIVE_INT:
1433 g_assert (arg->type);
1434 switch (arg->type->type) {
1435 case MONO_TYPE_CLASS:
1436 case MONO_TYPE_STRING:
1437 case MONO_TYPE_OBJECT:
1438 case MONO_TYPE_SZARRAY:
1439 case MONO_TYPE_ARRAY:
1440 case MONO_TYPE_FNPTR:
1443 case MONO_TYPE_GENERICINST:
1444 /*We need to check if the container class
1445 * of the generic type is a valuetype, iow:
1446 * is it a "class Foo<T>" or a "struct Foo<T>"?
1448 return !arg->type->data.generic_class->container_class->valuetype;
1456 /*Type manipulation helper*/
1458 /*Returns the byref version of the supplied MonoType*/
1460 mono_type_get_type_byref (MonoType *type)
1464 return &mono_class_from_mono_type (type)->this_arg;
1468 /*Returns the byval version of the supplied MonoType*/
1470 mono_type_get_type_byval (MonoType *type)
1474 return &mono_class_from_mono_type (type)->byval_arg;
1478 mono_type_from_stack_slot (ILStackDesc *slot)
1480 if (stack_slot_is_managed_pointer (slot))
1481 return mono_type_get_type_byref (slot->type);
1485 /*Stack manipulation code*/
1488 ensure_stack_size (ILCodeDesc *stack, int required)
1493 if (required < stack->max_size)
1496 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1497 new_size = MAX (8, MAX (required, stack->max_size * 2));
1499 g_assert (new_size >= stack->size);
1500 g_assert (new_size >= required);
1502 tmp = g_new0 (ILStackDesc, new_size);
1503 MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1507 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1508 g_free (stack->stack);
1509 MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1513 stack->max_size = new_size;
1517 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1519 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1521 state->size = state->max_size = 0;
1522 state->flags |= IL_CODE_FLAG_STACK_INITED;
1526 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1528 ensure_stack_size (to, from->size);
1529 to->size = from->size;
1531 /*stack copy happens at merge points, which have small stacks*/
1533 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1537 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1539 to->stype = from->stype;
1540 to->type = from->type;
1541 to->method = from->method;
1545 check_underflow (VerifyContext *ctx, int size)
1547 if (ctx->eval.size < size) {
1548 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1555 check_overflow (VerifyContext *ctx)
1557 if (ctx->eval.size >= ctx->max_stack) {
1558 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1564 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1566 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1568 if (stack_slot_get_type (value) == TYPE_PTR) {
1569 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1575 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1577 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1579 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1580 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1586 static ILStackDesc *
1587 stack_push (VerifyContext *ctx)
1589 g_assert (ctx->eval.size < ctx->max_stack);
1590 g_assert (ctx->eval.size <= ctx->eval.max_size);
1592 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1594 return & ctx->eval.stack [ctx->eval.size++];
1597 static ILStackDesc *
1598 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1600 ILStackDesc *top = stack_push (ctx);
1606 static ILStackDesc *
1607 stack_pop (VerifyContext *ctx)
1610 g_assert (ctx->eval.size > 0);
1611 ret = ctx->eval.stack + --ctx->eval.size;
1612 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1613 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1617 /* This function allows to safely pop an unititialized this ptr from
1618 * the eval stack without marking the method as unverifiable.
1620 static ILStackDesc *
1621 stack_pop_safe (VerifyContext *ctx)
1623 g_assert (ctx->eval.size > 0);
1624 return ctx->eval.stack + --ctx->eval.size;
1627 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1629 stack_peek (VerifyContext *ctx, int distance)
1631 g_assert (ctx->eval.size - distance > 0);
1632 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1635 static ILStackDesc *
1636 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1638 ILStackDesc *top = stack_push (ctx);
1639 copy_stack_value (top, value);
1643 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1645 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1648 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1653 if (!(type = verifier_load_type (ctx, token, opcode)))
1656 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1657 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1661 if (type->type == MONO_TYPE_VOID) {
1662 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1666 if (type->type == MONO_TYPE_TYPEDBYREF)
1667 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1669 if (!(class = mono_class_from_mono_type (type)))
1670 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1672 if (class->generic_container && type->type != MONO_TYPE_GENERICINST)
1673 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the generic type definition in a boxable type position for %s at 0x%04x", opcode, ctx->ip_offset));
1675 check_unverifiable_type (ctx, type);
1680 /*operation result tables */
1682 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1683 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1684 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1685 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1686 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1687 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1688 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1691 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1692 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1693 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1694 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1695 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1696 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1697 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1700 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1701 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1702 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1703 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1704 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1705 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1706 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1709 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1710 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1711 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1712 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1713 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1714 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1715 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1718 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1719 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1720 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1721 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1722 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1723 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1724 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1727 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1728 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1729 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1730 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1731 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1732 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1733 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1736 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1737 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1738 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1739 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1740 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1741 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1742 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1745 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1746 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1747 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1748 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1749 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1750 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1751 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1754 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1755 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1756 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1757 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1758 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1759 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1760 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1763 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1764 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1765 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1766 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1767 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1768 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1769 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1772 #ifdef MONO_VERIFIER_DEBUG
1776 dump_stack_value (ILStackDesc *value)
1778 printf ("[(%x)(%x)", value->type->type, value->stype);
1780 if (stack_slot_is_this_pointer (value))
1783 if (stack_slot_is_boxed_value (value))
1784 printf ("[boxed] ");
1786 if (stack_slot_is_null_literal (value))
1789 if (stack_slot_is_managed_mutability_pointer (value))
1790 printf ("Controled Mutability MP: ");
1792 if (stack_slot_is_managed_pointer (value))
1793 printf ("Managed Pointer to: ");
1795 switch (stack_slot_get_underlying_type (value)) {
1797 printf ("invalid type]");
1805 case TYPE_NATIVE_INT:
1806 printf ("native int]");
1809 printf ("float64]");
1812 printf ("unmanaged pointer]");
1815 switch (value->type->type) {
1816 case MONO_TYPE_CLASS:
1817 case MONO_TYPE_VALUETYPE:
1818 printf ("complex] (%s)", value->type->data.klass->name);
1820 case MONO_TYPE_STRING:
1821 printf ("complex] (string)");
1823 case MONO_TYPE_OBJECT:
1824 printf ("complex] (object)");
1826 case MONO_TYPE_SZARRAY:
1827 printf ("complex] (%s [])", value->type->data.klass->name);
1829 case MONO_TYPE_ARRAY:
1830 printf ("complex] (%s [%d %d %d])",
1831 value->type->data.array->eklass->name,
1832 value->type->data.array->rank,
1833 value->type->data.array->numsizes,
1834 value->type->data.array->numlobounds);
1836 case MONO_TYPE_GENERICINST:
1837 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1840 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1842 case MONO_TYPE_MVAR:
1843 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1846 //should be a boxed value
1847 char * name = mono_type_full_name (value->type);
1848 printf ("complex] %s", name);
1854 printf ("unknown stack %x type]\n", value->stype);
1855 g_assert_not_reached ();
1860 dump_stack_state (ILCodeDesc *state)
1864 printf ("(%d) ", state->size);
1865 for (i = 0; i < state->size; ++i)
1866 dump_stack_value (state->stack + i);
1871 /*Returns TRUE if candidate array type can be assigned to target.
1872 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1875 is_array_type_compatible (MonoType *target, MonoType *candidate)
1877 MonoArrayType *left = target->data.array;
1878 MonoArrayType *right = candidate->data.array;
1880 g_assert (target->type == MONO_TYPE_ARRAY);
1881 g_assert (candidate->type == MONO_TYPE_ARRAY);
1883 if (left->rank != right->rank)
1886 return verifier_class_is_assignable_from (left->eklass, right->eklass);
1890 get_stack_type (MonoType *type)
1893 int type_kind = type->type;
1895 mask = POINTER_MASK;
1896 /*TODO handle CMMP_MASK */
1899 switch (type_kind) {
1902 case MONO_TYPE_BOOLEAN:
1905 case MONO_TYPE_CHAR:
1908 return TYPE_I4 | mask;
1912 return TYPE_NATIVE_INT | mask;
1914 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1915 case MONO_TYPE_FNPTR:
1917 case MONO_TYPE_TYPEDBYREF:
1918 return TYPE_PTR | mask;
1921 case MONO_TYPE_MVAR:
1923 case MONO_TYPE_CLASS:
1924 case MONO_TYPE_STRING:
1925 case MONO_TYPE_OBJECT:
1926 case MONO_TYPE_SZARRAY:
1927 case MONO_TYPE_ARRAY:
1928 return TYPE_COMPLEX | mask;
1932 return TYPE_I8 | mask;
1936 return TYPE_R8 | mask;
1938 case MONO_TYPE_GENERICINST:
1939 case MONO_TYPE_VALUETYPE:
1940 if (mono_type_is_enum_type (type)) {
1941 type = mono_type_get_underlying_type_any (type);
1944 type_kind = type->type;
1947 return TYPE_COMPLEX | mask;
1955 /* convert MonoType to ILStackDesc format (stype) */
1957 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1960 int type_kind = type->type;
1962 if (type->byref || take_addr)
1963 mask = POINTER_MASK;
1964 /* TODO handle CMMP_MASK */
1969 switch (type_kind) {
1972 case MONO_TYPE_BOOLEAN:
1975 case MONO_TYPE_CHAR:
1978 stack->stype = TYPE_I4 | mask;
1982 stack->stype = TYPE_NATIVE_INT | mask;
1985 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1986 case MONO_TYPE_FNPTR:
1988 case MONO_TYPE_TYPEDBYREF:
1989 stack->stype = TYPE_PTR | mask;
1992 case MONO_TYPE_CLASS:
1993 case MONO_TYPE_STRING:
1994 case MONO_TYPE_OBJECT:
1995 case MONO_TYPE_SZARRAY:
1996 case MONO_TYPE_ARRAY:
1999 case MONO_TYPE_MVAR:
2000 stack->stype = TYPE_COMPLEX | mask;
2005 stack->stype = TYPE_I8 | mask;
2009 stack->stype = TYPE_R8 | mask;
2011 case MONO_TYPE_GENERICINST:
2012 case MONO_TYPE_VALUETYPE:
2013 if (mono_type_is_enum_type (type)) {
2014 MonoType *utype = mono_type_get_underlying_type_any (type);
2016 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
2020 type_kind = type->type;
2023 stack->stype = TYPE_COMPLEX | mask;
2027 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
2028 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
2035 * init_stack_with_value_at_exception_boundary:
2037 * Initialize the stack and push a given type.
2038 * The instruction is marked as been on the exception boundary.
2041 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
2044 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
2046 if (!mono_error_ok (&error)) {
2047 char *name = mono_type_get_full_name (klass);
2048 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
2050 mono_error_cleanup (&error);
2054 if (!ctx->max_stack) {
2055 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
2059 stack_init (ctx, code);
2060 ensure_stack_size (code, 1);
2061 set_stack_value (ctx, code->stack, type, FALSE);
2062 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
2064 code->flags |= IL_CODE_FLAG_WAS_TARGET;
2065 if (mono_type_is_generic_argument (type))
2066 code->stack->stype |= BOXED_MASK;
2070 get_ienumerable_class (void)
2072 static MonoClass* generic_ienumerable_class = NULL;
2074 if (generic_ienumerable_class == NULL)
2075 generic_ienumerable_class = mono_class_from_name (mono_defaults.corlib,
2076 "System.Collections.Generic", "IEnumerable`1");
2077 return generic_ienumerable_class;
2081 get_icollection_class (void)
2083 static MonoClass* generic_icollection_class = NULL;
2085 if (generic_icollection_class == NULL)
2086 generic_icollection_class = mono_class_from_name (mono_defaults.corlib,
2087 "System.Collections.Generic", "ICollection`1");
2088 return generic_icollection_class;
2092 inflate_class_one_arg (MonoClass *gtype, MonoClass *arg0)
2095 args [0] = &arg0->byval_arg;
2097 return mono_class_bind_generic_parameters (gtype, 1, args, FALSE);
2101 verifier_inflate_and_check_compat (MonoClass *target, MonoClass *gtd, MonoClass *arg)
2104 if (!(tmp = inflate_class_one_arg (gtd, arg)))
2106 if (mono_class_is_variant_compatible (target, tmp, TRUE))
2112 verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate)
2114 MonoClass *iface_gtd;
2116 if (target == candidate)
2119 if (mono_class_has_variant_generic_params (target)) {
2120 if (MONO_CLASS_IS_INTERFACE (target)) {
2121 if (candidate->rank == 1) {
2122 if (verifier_inflate_and_check_compat (target, mono_defaults.generic_ilist_class, candidate->element_class))
2124 if (verifier_inflate_and_check_compat (target, get_icollection_class (), candidate->element_class))
2126 if (verifier_inflate_and_check_compat (target, get_ienumerable_class (), candidate->element_class))
2131 while (candidate && candidate != mono_defaults.object_class) {
2132 mono_class_setup_interfaces (candidate, &error);
2133 if (!mono_error_ok (&error)) {
2134 mono_error_cleanup (&error);
2138 /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
2139 for (i = 0; i < candidate->interface_offsets_count; ++i) {
2140 MonoClass *iface = candidate->interfaces_packed [i];
2141 if (mono_class_is_variant_compatible (target, iface, TRUE))
2145 for (i = 0; i < candidate->interface_count; ++i) {
2146 MonoClass *iface = candidate->interfaces [i];
2147 if (mono_class_is_variant_compatible (target, iface, TRUE))
2150 candidate = candidate->parent;
2153 } else if (target->delegate) {
2154 if (mono_class_is_variant_compatible (target, candidate, TRUE))
2160 if (mono_class_is_assignable_from (target, candidate))
2163 if (!MONO_CLASS_IS_INTERFACE (target) || !target->generic_class || candidate->rank != 1)
2166 iface_gtd = target->generic_class->container_class;
2167 if (iface_gtd != mono_defaults.generic_ilist_class && iface_gtd != get_icollection_class () && iface_gtd != get_ienumerable_class ())
2170 target = mono_class_from_mono_type (target->generic_class->context.class_inst->type_argv [0]);
2171 candidate = candidate->element_class;
2176 /*Verify if type 'candidate' can be stored in type 'target'.
2178 * If strict, check for the underlying type and not the verification stack types
2181 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
2183 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2184 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2186 MonoType *original_candidate = candidate;
2187 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
2189 /*only one is byref */
2190 if (candidate->byref ^ target->byref) {
2191 /* converting from native int to byref*/
2192 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2193 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2198 strict |= target->byref;
2199 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2200 candidate = mono_type_get_underlying_type_any (candidate);
2203 switch (target->type) {
2204 case MONO_TYPE_VOID:
2205 return candidate->type == MONO_TYPE_VOID;
2208 case MONO_TYPE_BOOLEAN:
2210 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2213 case MONO_TYPE_CHAR:
2215 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2217 case MONO_TYPE_U4: {
2218 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2219 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2221 return is_native_int || is_int4;
2222 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2227 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2232 return candidate->type == target->type;
2233 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2237 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2238 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2240 return is_native_int || is_int4;
2241 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2245 if (candidate->type != MONO_TYPE_PTR)
2247 /* check the underlying type */
2248 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2250 case MONO_TYPE_FNPTR: {
2251 MonoMethodSignature *left, *right;
2252 if (candidate->type != MONO_TYPE_FNPTR)
2255 left = mono_type_get_signature (target);
2256 right = mono_type_get_signature (candidate);
2257 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2260 case MONO_TYPE_GENERICINST: {
2261 MonoClass *target_klass;
2262 MonoClass *candidate_klass;
2263 if (mono_type_is_enum_type (target)) {
2264 target = mono_type_get_underlying_type_any (target);
2270 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2271 * to take boxing status into account.
2273 if (mono_type_is_generic_argument (original_candidate))
2276 target_klass = mono_class_from_mono_type (target);
2277 candidate_klass = mono_class_from_mono_type (candidate);
2278 if (mono_class_is_nullable (target_klass)) {
2279 if (!mono_class_is_nullable (candidate_klass))
2281 return target_klass == candidate_klass;
2283 return verifier_class_is_assignable_from (target_klass, candidate_klass);
2286 case MONO_TYPE_STRING:
2287 return candidate->type == MONO_TYPE_STRING;
2289 case MONO_TYPE_CLASS:
2291 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2292 * to take boxing status into account.
2294 if (mono_type_is_generic_argument (original_candidate))
2297 if (candidate->type == MONO_TYPE_VALUETYPE)
2300 /* If candidate is an enum it should return true for System.Enum and supertypes.
2301 * That's why here we use the original type and not the underlying type.
2303 return verifier_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2305 case MONO_TYPE_OBJECT:
2306 return MONO_TYPE_IS_REFERENCE (candidate);
2308 case MONO_TYPE_SZARRAY: {
2311 if (candidate->type != MONO_TYPE_SZARRAY)
2314 left = mono_class_from_mono_type (target);
2315 right = mono_class_from_mono_type (candidate);
2317 return verifier_class_is_assignable_from (left, right);
2320 case MONO_TYPE_ARRAY:
2321 if (candidate->type != MONO_TYPE_ARRAY)
2323 return is_array_type_compatible (target, candidate);
2325 case MONO_TYPE_TYPEDBYREF:
2326 return candidate->type == MONO_TYPE_TYPEDBYREF;
2328 case MONO_TYPE_VALUETYPE: {
2329 MonoClass *target_klass;
2330 MonoClass *candidate_klass;
2332 if (candidate->type == MONO_TYPE_CLASS)
2335 target_klass = mono_class_from_mono_type (target);
2336 candidate_klass = mono_class_from_mono_type (candidate);
2337 if (target_klass == candidate_klass)
2339 if (mono_type_is_enum_type (target)) {
2340 target = mono_type_get_underlying_type_any (target);
2349 if (candidate->type != MONO_TYPE_VAR)
2351 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2353 case MONO_TYPE_MVAR:
2354 if (candidate->type != MONO_TYPE_MVAR)
2356 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2359 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2360 g_assert_not_reached ();
2369 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2371 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2375 * Returns the generic param bound to the context been verified.
2378 static MonoGenericParam*
2379 get_generic_param (VerifyContext *ctx, MonoType *param)
2381 guint16 param_num = mono_type_get_generic_param_num (param);
2382 if (param->type == MONO_TYPE_VAR) {
2383 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2384 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2387 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2390 /*param must be a MVAR */
2391 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2392 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2395 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2400 recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
2402 MonoType *constraint_type = &constraint_class->byval_arg;
2403 if (recursion_level <= 0)
2406 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
2409 if (mono_type_is_generic_argument (constraint_type)) {
2410 MonoGenericParam *param = get_generic_param (ctx, constraint_type);
2414 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2415 if (recursive_boxed_constraint_type_check (ctx, type, *class, recursion_level - 1))
2423 * is_compatible_boxed_valuetype:
2425 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2427 * @type The source type. It it tested to be of the proper type.
2428 * @candidate type of the boxed valuetype.
2429 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2430 * @strict if TRUE candidate must be boxed compatible to the target type
2434 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2436 if (!stack_slot_is_boxed_value (stack))
2438 if (type->byref || candidate->byref)
2441 if (mono_type_is_generic_argument (candidate)) {
2442 MonoGenericParam *param = get_generic_param (ctx, candidate);
2447 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2448 /*256 should be enough since there can't be more than 255 generic arguments.*/
2449 if (recursive_boxed_constraint_type_check (ctx, type, *class, 256))
2454 if (mono_type_is_generic_argument (type))
2460 return MONO_TYPE_IS_REFERENCE (type) && verifier_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2464 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2466 MonoType *candidate = mono_type_from_stack_slot (stack);
2467 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2470 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2473 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2476 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2480 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2482 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2486 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2488 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2492 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2494 if (candidate->byref ^ target->byref)
2497 switch (target->type) {
2498 case MONO_TYPE_VOID:
2501 case MONO_TYPE_BOOLEAN:
2504 case MONO_TYPE_CHAR:
2513 case MONO_TYPE_STRING:
2514 case MONO_TYPE_TYPEDBYREF:
2515 return candidate->type == target->type;
2518 if (candidate->type != MONO_TYPE_PTR)
2520 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2522 case MONO_TYPE_FNPTR:
2523 if (candidate->type != MONO_TYPE_FNPTR)
2525 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2527 case MONO_TYPE_GENERICINST: {
2528 MonoClass *target_klass;
2529 MonoClass *candidate_klass;
2530 target_klass = mono_class_from_mono_type (target);
2531 candidate_klass = mono_class_from_mono_type (candidate);
2532 /*FIXME handle nullables and enum*/
2533 return verifier_class_is_assignable_from (target_klass, candidate_klass);
2535 case MONO_TYPE_OBJECT:
2536 return MONO_TYPE_IS_REFERENCE (candidate);
2538 case MONO_TYPE_CLASS:
2539 return verifier_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2541 case MONO_TYPE_SZARRAY:
2542 if (candidate->type != MONO_TYPE_SZARRAY)
2544 return verifier_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2546 case MONO_TYPE_ARRAY:
2547 if (candidate->type != MONO_TYPE_ARRAY)
2549 return is_array_type_compatible (target, candidate);
2551 case MONO_TYPE_VALUETYPE:
2552 /*FIXME handle nullables and enum*/
2553 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2556 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2559 case MONO_TYPE_MVAR:
2560 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2564 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2565 g_assert_not_reached ();
2571 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2573 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2576 return mono_delegate_type_equal (method, delegate);
2580 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2582 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2585 return mono_delegate_type_equal (delegate, method);
2589 * mono_delegate_signature_equal:
2591 * Compare two signatures in the way expected by delegates.
2593 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2595 * FIXME can this function be eliminated and proper metadata functionality be used?
2598 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2601 int method_offset = is_static_ldftn ? 1 : 0;
2603 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2606 if (delegate_sig->call_convention != method_sig->call_convention)
2609 for (i = 0; i < delegate_sig->param_count; i++) {
2610 MonoType *p1 = delegate_sig->params [i];
2611 MonoType *p2 = method_sig->params [i + method_offset];
2613 if (!mono_delegate_param_equal (p1, p2))
2617 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2624 mono_verifier_is_signature_compatible (MonoMethodSignature *target, MonoMethodSignature *candidate)
2626 return mono_delegate_signature_equal (target, candidate, FALSE);
2630 * verify_ldftn_delegate:
2632 * Verify properties of ldftn based delegates.
2635 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2637 MonoMethod *method = funptr->method;
2639 /*ldftn non-final virtuals only allowed if method is not static,
2640 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2641 * This rules doesn't apply if the object on stack is a boxed valuetype.
2643 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2644 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2645 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2646 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2648 /*current method must not be static*/
2649 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2650 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2652 /*value is the this pointer, loaded using ldarg.0 */
2653 if (!stack_slot_is_this_pointer (value))
2654 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));
2656 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2661 * verify_delegate_compatibility:
2663 * Verify delegate creation sequence.
2667 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2669 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2670 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2672 MonoMethod *invoke, *method;
2673 const guint8 *ip = ctx->header->code;
2674 guint32 ip_offset = ctx->ip_offset;
2675 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2677 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2678 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2682 invoke = mono_get_delegate_invoke (delegate);
2683 method = funptr->method;
2685 if (!method || !mono_method_signature (method)) {
2686 char *name = mono_type_get_full_name (delegate);
2687 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2692 if (!invoke || !mono_method_signature (invoke)) {
2693 char *name = mono_type_get_full_name (delegate);
2694 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2699 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2701 if (is_static_ldftn)
2702 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2704 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2705 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2706 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2707 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer signature '%s' doesn't match delegate's signature '%s' at 0x%04x", fun_sig, invoke_sig, ctx->ip_offset));
2709 g_free (invoke_sig);
2713 * Delegate code sequences:
2719 * [-6] ldvirtftn token
2723 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2724 verify_ldftn_delegate (ctx, delegate, value, funptr);
2725 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2726 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2728 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2730 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2733 if (is_first_arg_bound) {
2734 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2735 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2737 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2738 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2739 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2741 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2742 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2746 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2747 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2749 #undef IS_VALID_OPCODE
2750 #undef IS_LOAD_FUN_PTR
2753 /* implement the opcode checks*/
2755 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2759 if (arg >= ctx->max_args) {
2761 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2763 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2764 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2765 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2767 } else if (check_overflow (ctx)) {
2768 /*We must let the value be pushed, otherwise we would get an underflow error*/
2769 check_unverifiable_type (ctx, ctx->params [arg]);
2770 if (ctx->params [arg]->byref && take_addr)
2771 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2772 top = stack_push (ctx);
2773 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2776 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2778 ctx->has_this_store = TRUE;
2780 top->stype |= THIS_POINTER_MASK;
2781 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2782 top->stype |= UNINIT_THIS_MASK;
2788 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2790 if (arg >= ctx->num_locals) {
2791 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2792 } else if (check_overflow (ctx)) {
2793 /*We must let the value be pushed, otherwise we would get an underflow error*/
2794 check_unverifiable_type (ctx, ctx->locals [arg]);
2795 if (ctx->locals [arg]->byref && take_addr)
2796 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2798 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2803 store_arg (VerifyContext *ctx, guint32 arg)
2807 if (arg >= ctx->max_args) {
2808 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2809 if (check_underflow (ctx, 1))
2814 if (check_underflow (ctx, 1)) {
2815 value = stack_pop (ctx);
2816 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2817 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2820 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2821 ctx->has_this_store = 1;
2825 store_local (VerifyContext *ctx, guint32 arg)
2828 if (arg >= ctx->num_locals) {
2829 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2833 /*TODO verify definite assigment */
2834 if (!check_underflow (ctx, 1))
2837 value = stack_pop (ctx);
2838 if (ctx->locals [arg]->byref && stack_slot_is_managed_mutability_pointer (value))
2839 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly managed reference when storing on a local variable at 0x%04x", ctx->ip_offset));
2841 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2842 char *expected = mono_type_full_name (ctx->locals [arg]);
2843 char *found = stack_slot_full_name (value);
2844 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2854 /*FIXME add and sub needs special care here*/
2856 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2858 ILStackDesc *a, *b, *top;
2859 int idxa, idxb, complexMerge = 0;
2862 if (!check_underflow (ctx, 2))
2864 b = stack_pop (ctx);
2865 a = stack_pop (ctx);
2867 idxa = stack_slot_get_underlying_type (a);
2868 if (stack_slot_is_managed_pointer (a)) {
2873 idxb = stack_slot_get_underlying_type (b);
2874 if (stack_slot_is_managed_pointer (b)) {
2881 res = table [idxa][idxb];
2883 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2884 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2886 top = stack_push (ctx);
2887 if (res == TYPE_INV) {
2888 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)));
2889 copy_stack_value (top, a);
2893 if (res & NON_VERIFIABLE_RESULT) {
2894 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)));
2896 res = res & ~NON_VERIFIABLE_RESULT;
2899 if (complexMerge && res == TYPE_PTR) {
2900 if (complexMerge == 1)
2901 copy_stack_value (top, a);
2902 else if (complexMerge == 2)
2903 copy_stack_value (top, b);
2905 * There is no need to merge the type of two pointers.
2906 * The only valid operation is subtraction, that returns a native
2907 * int as result and can be used with any 2 pointer kinds.
2908 * This is valid acording to Patition III 1.1.4
2917 do_boolean_branch_op (VerifyContext *ctx, int delta)
2919 int target = ctx->ip_offset + delta;
2922 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2924 if (target < 0 || target >= ctx->code_size) {
2925 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2929 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2931 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2934 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2938 ctx->target = target;
2940 if (!check_underflow (ctx, 1))
2943 top = stack_pop (ctx);
2944 if (!is_valid_bool_arg (top))
2945 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));
2947 check_unmanaged_pointer (ctx, top);
2951 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2953 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2957 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2962 int target = ctx->ip_offset + delta;
2964 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2966 if (target < 0 || target >= ctx->code_size) {
2967 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2971 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2972 case 1: /*FIXME use constants and not magic numbers.*/
2973 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2976 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2980 ctx->target = target;
2982 if (!check_underflow (ctx, 2))
2985 b = stack_pop (ctx);
2986 a = stack_pop (ctx);
2988 idxa = stack_slot_get_underlying_type (a);
2989 if (stack_slot_is_managed_pointer (a))
2992 idxb = stack_slot_get_underlying_type (b);
2993 if (stack_slot_is_managed_pointer (b))
2996 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3001 res = table [idxa][idxb];
3004 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
3005 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3007 if (res == TYPE_INV) {
3008 CODE_NOT_VERIFIABLE (ctx,
3009 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));
3010 } else if (res & NON_VERIFIABLE_RESULT) {
3011 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));
3012 res = res & ~NON_VERIFIABLE_RESULT;
3017 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
3023 if (!check_underflow (ctx, 2))
3025 b = stack_pop (ctx);
3026 a = stack_pop (ctx);
3028 if (opcode == CEE_CGT_UN) {
3029 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
3030 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3035 idxa = stack_slot_get_underlying_type (a);
3036 if (stack_slot_is_managed_pointer (a))
3039 idxb = stack_slot_get_underlying_type (b);
3040 if (stack_slot_is_managed_pointer (b))
3043 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3048 res = table [idxa][idxb];
3051 if(res == TYPE_INV) {
3052 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));
3053 } else if (res & NON_VERIFIABLE_RESULT) {
3054 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));
3055 res = res & ~NON_VERIFIABLE_RESULT;
3057 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3061 do_ret (VerifyContext *ctx)
3063 MonoType *ret = ctx->signature->ret;
3064 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3065 if (ret->type != MONO_TYPE_VOID) {
3067 if (!check_underflow (ctx, 1))
3070 top = stack_pop(ctx);
3072 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3073 char *ret_type = mono_type_full_name (ctx->signature->ret);
3074 char *stack_type = stack_slot_full_name (top);
3075 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature, expected '%s' but got '%s' at 0x%04x", ret_type, stack_type, ctx->ip_offset));
3076 g_free (stack_type);
3081 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3082 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3085 if (ctx->eval.size > 0) {
3086 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3088 if (in_any_block (ctx->header, ctx->ip_offset))
3089 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3093 * 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.
3094 * This is illegal but mono_get_method_full decoded it.
3095 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3098 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
3101 MonoMethodSignature *sig;
3104 gboolean virt_check_this = FALSE;
3105 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3107 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
3111 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3113 if (method->klass->valuetype) // && !constrained ???
3114 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3116 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3117 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3120 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3121 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3123 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
3124 virt_check_this = TRUE;
3125 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3129 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
3130 sig = mono_method_get_signature (method, ctx->image, method_token);
3133 char *name = mono_type_get_full_name (method->klass);
3134 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name, method->name, ctx->ip_offset));
3139 param_count = sig->param_count + sig->hasthis;
3140 if (!check_underflow (ctx, param_count))
3143 for (i = sig->param_count - 1; i >= 0; --i) {
3144 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3145 value = stack_pop (ctx);
3146 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
3147 char *stack_name = stack_slot_full_name (value);
3148 char *sig_name = mono_type_full_name (sig->params [i]);
3149 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter with function signature: Calling method with signature (%s) but for argument %d there is a (%s) on stack at 0x%04x", sig_name, i, stack_name, ctx->ip_offset));
3150 g_free (stack_name);
3154 if (stack_slot_is_managed_mutability_pointer (value))
3155 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));
3157 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3158 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));
3164 MonoType *type = &method->klass->byval_arg;
3167 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3168 if (!mono_method_is_constructor (ctx->method))
3169 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3170 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3171 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor of a type different from this or super at 0x%04x", ctx->ip_offset));
3173 ctx->super_ctor_called = TRUE;
3174 value = stack_pop_safe (ctx);
3175 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3176 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3178 value = stack_pop (ctx);
3181 copy_stack_value (©, value);
3182 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3183 //Other parts of the code suffer from the same issue of
3184 copy.type = mono_type_get_type_byval (copy.type);
3185 copy.stype &= ~POINTER_MASK;
3187 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3188 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the call opcode with a non-final virtual method on an object different than the 'this' pointer at 0x%04x", ctx->ip_offset));
3190 if (constrained && virtual) {
3191 if (!stack_slot_is_managed_pointer (value))
3192 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3193 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
3194 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3195 copy.stype |= BOXED_MASK;
3197 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3198 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));
3200 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3201 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3203 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3204 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3206 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3207 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));
3209 if (!verify_stack_type_compatibility (ctx, type, ©)) {
3210 char *expected = mono_type_full_name (type);
3211 char *effective = stack_slot_full_name (©);
3212 char *method_name = mono_method_full_name (method, TRUE);
3213 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature expected '%s' but got '%s' for a call to '%s' at 0x%04x",
3214 expected, effective, method_name, ctx->ip_offset));
3215 g_free (method_name);
3220 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
3221 char *name = mono_method_full_name (method, TRUE);
3222 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3226 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3227 char *name = mono_method_full_name (method, TRUE);
3228 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3232 if (sig->ret->type != MONO_TYPE_VOID) {
3233 if (!mono_type_is_valid_in_context (ctx, sig->ret))
3236 if (check_overflow (ctx)) {
3237 value = stack_push (ctx);
3238 set_stack_value (ctx, value, sig->ret, FALSE);
3239 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3240 ctx->prefix_set &= ~PREFIX_READONLY;
3241 value->stype |= CMMP_MASK;
3246 if ((ctx->prefix_set & PREFIX_TAIL)) {
3247 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3248 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3249 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3250 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3256 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3258 MonoClassField *field;
3260 if (!check_overflow (ctx))
3263 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3265 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3268 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3269 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3272 /*taking the address of initonly field only works from the static constructor */
3273 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3274 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3275 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3277 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3278 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3280 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3284 do_store_static_field (VerifyContext *ctx, int token) {
3285 MonoClassField *field;
3288 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3290 if (!check_underflow (ctx, 1))
3293 value = stack_pop (ctx);
3295 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3298 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3299 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3303 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3304 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3308 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3309 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3311 if (!verify_stack_type_compatibility (ctx, field->type, value)) {
3312 char *stack_name = stack_slot_full_name (value);
3313 char *field_name = mono_type_full_name (field->type);
3314 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3315 field_name, stack_name, ctx->ip_offset));
3316 g_free (field_name);
3317 g_free (stack_name);
3322 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3324 MonoClassField *field;
3326 gboolean is_pointer;
3328 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3329 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3333 //the value on stack is going to be used as a pointer
3334 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3336 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3337 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3340 g_assert (obj->type);
3342 /*The value on the stack must be a subclass of the defining type of the field*/
3343 /* we need to check if we can load the field from the stack value*/
3345 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3346 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3348 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3349 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3351 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3352 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));
3354 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3355 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3356 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));
3358 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3359 char *found = stack_slot_full_name (obj);
3360 char *expected = mono_type_full_name (&field->parent->byval_arg);
3361 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected type '%s' but found '%s' referencing the 'this' argument at 0x%04x", expected, found, ctx->ip_offset));
3366 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3367 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3370 check_unmanaged_pointer (ctx, obj);
3375 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3378 MonoClassField *field;
3381 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3383 if (!check_underflow (ctx, 1))
3385 obj = stack_pop_safe (ctx);
3387 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3390 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3391 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3393 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3394 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3395 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3397 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3401 do_store_field (VerifyContext *ctx, int token)
3403 ILStackDesc *value, *obj;
3404 MonoClassField *field;
3405 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3407 if (!check_underflow (ctx, 2))
3410 value = stack_pop (ctx);
3411 obj = stack_pop_safe (ctx);
3413 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3416 if (!verify_stack_type_compatibility (ctx, field->type, value))
3417 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3420 /*TODO proper handle for Nullable<T>*/
3422 do_box_value (VerifyContext *ctx, int klass_token)
3425 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3431 if (!check_underflow (ctx, 1))
3434 value = stack_pop (ctx);
3435 /*box is a nop for reference types*/
3437 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3438 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3443 if (!verify_stack_type_compatibility (ctx, type, value))
3444 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3446 klass = mono_class_from_mono_type (type);
3447 if (mono_class_is_nullable (klass))
3448 type = &mono_class_get_nullable_param (klass)->byval_arg;
3449 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3453 do_unbox_value (VerifyContext *ctx, int klass_token)
3456 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3461 if (!check_underflow (ctx, 1))
3464 if (!mono_class_from_mono_type (type)->valuetype)
3465 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3467 value = stack_pop (ctx);
3469 /*Value should be: a boxed valuetype or a reference type*/
3470 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3471 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3472 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));
3474 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3475 value->stype |= CMMP_MASK;
3479 do_unbox_any (VerifyContext *ctx, int klass_token)
3482 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3487 if (!check_underflow (ctx, 1))
3490 value = stack_pop (ctx);
3492 /*Value should be: a boxed valuetype or a reference type*/
3493 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3494 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3495 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));
3497 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3501 do_unary_math_op (VerifyContext *ctx, int op)
3504 if (!check_underflow (ctx, 1))
3506 value = stack_pop (ctx);
3507 switch (stack_slot_get_type (value)) {
3510 case TYPE_NATIVE_INT:
3515 case TYPE_COMPLEX: /*only enums are ok*/
3516 if (mono_type_is_enum_type (value->type))
3519 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3521 stack_push_stack_val (ctx, value);
3525 do_conversion (VerifyContext *ctx, int kind)
3528 if (!check_underflow (ctx, 1))
3530 value = stack_pop (ctx);
3532 switch (stack_slot_get_type (value)) {
3535 case TYPE_NATIVE_INT:
3539 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));
3544 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3547 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3550 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3552 case TYPE_NATIVE_INT:
3553 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3556 g_error ("unknown type %02x in conversion", kind);
3562 do_load_token (VerifyContext *ctx, int token)
3565 MonoClass *handle_class;
3566 if (!check_overflow (ctx))
3569 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
3570 handle = mono_method_get_wrapper_data (ctx->method, token);
3571 handle_class = mono_method_get_wrapper_data (ctx->method, token + 1);
3572 if (handle_class == mono_defaults.typehandle_class)
3573 handle = &((MonoClass*)handle)->byval_arg;
3575 switch (token & 0xff000000) {
3576 case MONO_TOKEN_TYPE_DEF:
3577 case MONO_TOKEN_TYPE_REF:
3578 case MONO_TOKEN_TYPE_SPEC:
3579 case MONO_TOKEN_FIELD_DEF:
3580 case MONO_TOKEN_METHOD_DEF:
3581 case MONO_TOKEN_METHOD_SPEC:
3582 case MONO_TOKEN_MEMBER_REF:
3583 if (!token_bounds_check (ctx->image, token)) {
3584 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Table index out of range 0x%x for token %x for ldtoken at 0x%04x", mono_metadata_token_index (token), token, ctx->ip_offset));
3589 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid table 0x%x for token 0x%x for ldtoken at 0x%04x", mono_metadata_token_table (token), token, ctx->ip_offset));
3593 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3597 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3600 if (handle_class == mono_defaults.typehandle_class) {
3601 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3602 } else if (handle_class == mono_defaults.methodhandle_class) {
3603 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3604 } else if (handle_class == mono_defaults.fieldhandle_class) {
3605 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3607 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3609 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3613 do_ldobj_value (VerifyContext *ctx, int token)
3616 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3617 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3622 if (!check_underflow (ctx, 1))
3625 value = stack_pop (ctx);
3626 if (!stack_slot_is_managed_pointer (value)
3627 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3628 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3629 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3633 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3634 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3636 /*We have a byval on the stack, but the comparison must be strict. */
3637 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3638 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3640 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3644 do_stobj (VerifyContext *ctx, int token)
3646 ILStackDesc *dest, *src;
3647 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3648 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3653 if (!check_underflow (ctx, 2))
3656 src = stack_pop (ctx);
3657 dest = stack_pop (ctx);
3659 if (stack_slot_is_managed_mutability_pointer (dest))
3660 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3662 if (!stack_slot_is_managed_pointer (dest))
3663 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3665 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3666 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));
3668 if (!verify_stack_type_compatibility (ctx, type, src)) {
3669 char *type_name = mono_type_full_name (type);
3670 char *src_name = stack_slot_full_name (src);
3671 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token '%s' and source '%s' of stobj don't match ' at 0x%04x", type_name, src_name, ctx->ip_offset));
3676 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3677 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3681 do_cpobj (VerifyContext *ctx, int token)
3683 ILStackDesc *dest, *src;
3684 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3688 if (!check_underflow (ctx, 2))
3691 src = stack_pop (ctx);
3692 dest = stack_pop (ctx);
3694 if (!stack_slot_is_managed_pointer (src))
3695 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3697 if (!stack_slot_is_managed_pointer (dest))
3698 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3700 if (stack_slot_is_managed_mutability_pointer (dest))
3701 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3703 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3704 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3706 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3707 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3711 do_initobj (VerifyContext *ctx, int token)
3714 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3718 if (!check_underflow (ctx, 1))
3721 obj = stack_pop (ctx);
3723 if (!stack_slot_is_managed_pointer (obj))
3724 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3726 if (stack_slot_is_managed_mutability_pointer (obj))
3727 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3729 stack = mono_type_get_type_byval (obj->type);
3730 if (MONO_TYPE_IS_REFERENCE (stack)) {
3731 if (!verify_type_compatibility (ctx, stack, type))
3732 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3733 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3734 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3735 } else if (!verify_type_compatibility (ctx, stack, type)) {
3736 char *expected_name = mono_type_full_name (type);
3737 char *stack_name = mono_type_full_name (stack);
3739 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Initobj %s not compatible with value on stack %s at 0x%04x", expected_name, stack_name, ctx->ip_offset));
3740 g_free (expected_name);
3741 g_free (stack_name);
3746 do_newobj (VerifyContext *ctx, int token)
3750 MonoMethodSignature *sig;
3752 gboolean is_delegate = FALSE;
3754 if (!(method = verifier_load_method (ctx, token, "newobj")))
3757 if (!mono_method_is_constructor (method)) {
3758 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3762 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3763 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3765 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3766 char *from = mono_method_full_name (ctx->method, TRUE);
3767 char *to = mono_method_full_name (method, TRUE);
3768 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor %s not visible from %s at 0x%04x", to, from, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3773 //FIXME use mono_method_get_signature_full
3774 sig = mono_method_signature (method);
3776 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3780 if (!sig->hasthis) {
3781 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature missing hasthis at 0x%04x", ctx->ip_offset));
3785 if (!check_underflow (ctx, sig->param_count))
3788 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3791 ILStackDesc *funptr;
3792 //first arg is object, second arg is fun ptr
3793 if (sig->param_count != 2) {
3794 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3797 funptr = stack_pop (ctx);
3798 value = stack_pop (ctx);
3799 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3801 for (i = sig->param_count - 1; i >= 0; --i) {
3802 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3803 value = stack_pop (ctx);
3804 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3805 char *stack_name = stack_slot_full_name (value);
3806 char *sig_name = mono_type_full_name (sig->params [i]);
3807 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));
3808 g_free (stack_name);
3812 if (stack_slot_is_managed_mutability_pointer (value))
3813 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3817 if (check_overflow (ctx))
3818 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3822 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3828 if (!check_underflow (ctx, 1))
3831 if (!(type = get_boxable_mono_type (ctx, token, opcode)))
3835 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3839 value = stack_pop (ctx);
3840 is_boxed = stack_slot_is_boxed_value (value);
3842 if (stack_slot_is_managed_pointer (value))
3843 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3844 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3845 char *name = stack_slot_full_name (value);
3846 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a reference type on stack for %s but found %s at 0x%04x", opcode, name, ctx->ip_offset));
3850 switch (value->type->type) {
3851 case MONO_TYPE_FNPTR:
3853 case MONO_TYPE_TYPEDBYREF:
3854 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3857 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3858 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3862 mono_type_from_opcode (int opcode) {
3870 return &mono_defaults.sbyte_class->byval_arg;
3878 return &mono_defaults.int16_class->byval_arg;
3886 return &mono_defaults.int32_class->byval_arg;
3892 return &mono_defaults.int64_class->byval_arg;
3898 return &mono_defaults.single_class->byval_arg;
3904 return &mono_defaults.double_class->byval_arg;
3910 return &mono_defaults.int_class->byval_arg;
3914 case CEE_LDELEM_REF:
3915 case CEE_STELEM_REF:
3916 return &mono_defaults.object_class->byval_arg;
3919 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3925 do_load_indirect (VerifyContext *ctx, int opcode)
3928 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3930 if (!check_underflow (ctx, 1))
3933 value = stack_pop (ctx);
3934 if (!stack_slot_is_managed_pointer (value)) {
3935 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3936 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3940 if (opcode == CEE_LDIND_REF) {
3941 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3942 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3943 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3945 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3946 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3947 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3952 do_store_indirect (VerifyContext *ctx, int opcode)
3954 ILStackDesc *addr, *val;
3955 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3957 if (!check_underflow (ctx, 2))
3960 val = stack_pop (ctx);
3961 addr = stack_pop (ctx);
3963 check_unmanaged_pointer (ctx, addr);
3965 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3966 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3970 if (stack_slot_is_managed_mutability_pointer (addr)) {
3971 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3975 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3976 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3978 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3979 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3983 do_newarr (VerifyContext *ctx, int token)
3986 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3991 if (!check_underflow (ctx, 1))
3994 value = stack_pop (ctx);
3995 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3996 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));
3998 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
4001 /*FIXME handle arrays that are not 0-indexed*/
4003 do_ldlen (VerifyContext *ctx)
4007 if (!check_underflow (ctx, 1))
4010 value = stack_pop (ctx);
4012 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
4013 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
4015 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
4018 /*FIXME handle arrays that are not 0-indexed*/
4019 /*FIXME handle readonly prefix and CMMP*/
4021 do_ldelema (VerifyContext *ctx, int klass_token)
4023 ILStackDesc *index, *array, *res;
4024 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
4030 if (!check_underflow (ctx, 2))
4033 index = stack_pop (ctx);
4034 array = stack_pop (ctx);
4036 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4037 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));
4039 if (!stack_slot_is_null_literal (array)) {
4040 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4041 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4043 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
4044 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
4046 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
4049 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
4053 res = stack_push (ctx);
4054 set_stack_value (ctx, res, type, TRUE);
4055 if (ctx->prefix_set & PREFIX_READONLY) {
4056 ctx->prefix_set &= ~PREFIX_READONLY;
4057 res->stype |= CMMP_MASK;
4062 * FIXME handle arrays that are not 0-indexed
4063 * FIXME handle readonly prefix and CMMP
4066 do_ldelem (VerifyContext *ctx, int opcode, int token)
4068 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4069 ILStackDesc *index, *array;
4071 if (!check_underflow (ctx, 2))
4074 if (opcode == CEE_LDELEM) {
4075 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
4076 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4080 type = mono_type_from_opcode (opcode);
4083 index = stack_pop (ctx);
4084 array = stack_pop (ctx);
4086 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4087 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));
4089 if (!stack_slot_is_null_literal (array)) {
4090 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4091 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));
4093 if (opcode == CEE_LDELEM_REF) {
4094 if (array->type->data.klass->valuetype)
4095 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
4096 type = &array->type->data.klass->byval_arg;
4098 MonoType *candidate = &array->type->data.klass->byval_arg;
4099 if (IS_STRICT_MODE (ctx)) {
4100 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
4101 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
4102 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)) ||
4103 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
4104 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4106 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
4107 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4112 set_stack_value (ctx, stack_push (ctx), type, FALSE);
4117 * FIXME handle arrays that are not 0-indexed
4120 do_stelem (VerifyContext *ctx, int opcode, int token)
4122 ILStackDesc *index, *array, *value;
4124 if (!check_underflow (ctx, 3))
4127 if (opcode == CEE_STELEM) {
4128 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
4129 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4133 type = mono_type_from_opcode (opcode);
4136 value = stack_pop (ctx);
4137 index = stack_pop (ctx);
4138 array = stack_pop (ctx);
4140 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4141 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));
4143 if (!stack_slot_is_null_literal (array)) {
4144 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
4145 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));
4147 if (opcode == CEE_STELEM_REF) {
4148 if (array->type->data.klass->valuetype)
4149 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4150 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
4151 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4155 if (opcode == CEE_STELEM_REF) {
4156 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
4157 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4158 } else if (opcode != CEE_STELEM_REF) {
4159 if (!verify_stack_type_compatibility (ctx, type, value))
4160 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4162 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
4163 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));
4169 do_throw (VerifyContext *ctx)
4171 ILStackDesc *exception;
4172 if (!check_underflow (ctx, 1))
4174 exception = stack_pop (ctx);
4176 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4177 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4179 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
4180 char *name = mono_type_full_name (exception->type);
4181 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));
4184 /*The stack is left empty after a throw*/
4190 do_endfilter (VerifyContext *ctx)
4192 MonoExceptionClause *clause;
4194 if (IS_STRICT_MODE (ctx)) {
4195 if (ctx->eval.size != 1)
4196 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4198 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4199 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4202 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4203 if (IS_STRICT_MODE (ctx)) {
4204 if (ctx->ip_offset != clause->handler_offset - 2)
4205 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4207 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4208 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4211 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4212 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4214 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4221 do_leave (VerifyContext *ctx, int delta)
4223 int target = ((gint32)ctx->ip_offset) + delta;
4224 if (target >= ctx->code_size || target < 0)
4225 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4227 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4228 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4230 ctx->target = target;
4236 * Verify br and br.s opcodes.
4239 do_static_branch (VerifyContext *ctx, int delta)
4241 int target = ctx->ip_offset + delta;
4242 if (target < 0 || target >= ctx->code_size) {
4243 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4247 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4249 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4252 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4256 ctx->target = target;
4260 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4262 int i, base = ctx->ip_offset + 5 + count * 4;
4265 if (!check_underflow (ctx, 1))
4268 value = stack_pop (ctx);
4270 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4271 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4273 for (i = 0; i < count; ++i) {
4274 int target = base + read32 (data + i * 4);
4276 if (target < 0 || target >= ctx->code_size) {
4277 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4281 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4283 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4286 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4289 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4294 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4299 if (virtual && !check_underflow (ctx, 1))
4302 if (!virtual && !check_overflow (ctx))
4305 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
4306 method = mono_method_get_wrapper_data (ctx->method, (guint32)token);
4308 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4312 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4313 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4317 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4321 if (mono_method_is_constructor (method))
4322 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4325 ILStackDesc *top = stack_pop (ctx);
4327 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4328 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4330 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4331 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4333 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4334 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4337 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
4338 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);
4340 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4341 top->method = method;
4345 do_sizeof (VerifyContext *ctx, int token)
4349 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4352 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4353 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4357 if (type->type == MONO_TYPE_VOID) {
4358 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4362 if (check_overflow (ctx))
4363 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4366 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4368 do_localloc (VerifyContext *ctx)
4372 if (ctx->eval.size != 1) {
4373 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4377 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4378 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4382 /*TODO verify top type*/
4383 top = stack_pop (ctx);
4385 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4386 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4390 do_ldstr (VerifyContext *ctx, guint32 token)
4392 GSList *error = NULL;
4393 if (ctx->method->wrapper_type == MONO_WRAPPER_NONE && !ctx->image->dynamic) {
4394 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4395 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4399 if (!mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4401 ctx->list = g_slist_concat (ctx->list, error);
4402 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4407 if (check_overflow (ctx))
4408 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4412 do_refanyval (VerifyContext *ctx, int token)
4416 if (!check_underflow (ctx, 1))
4419 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4422 top = stack_pop (ctx);
4424 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4425 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));
4427 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4431 do_refanytype (VerifyContext *ctx)
4435 if (!check_underflow (ctx, 1))
4438 top = stack_pop (ctx);
4440 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4441 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));
4443 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4448 do_mkrefany (VerifyContext *ctx, int token)
4452 if (!check_underflow (ctx, 1))
4455 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4458 top = stack_pop (ctx);
4460 if (stack_slot_is_managed_mutability_pointer (top))
4461 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4463 if (!stack_slot_is_managed_pointer (top)) {
4464 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));
4466 MonoType *stack_type = mono_type_get_type_byval (top->type);
4467 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4468 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4470 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4471 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4474 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4478 do_ckfinite (VerifyContext *ctx)
4481 if (!check_underflow (ctx, 1))
4484 top = stack_pop (ctx);
4486 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4487 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));
4488 stack_push_stack_val (ctx, top);
4492 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4494 * @from holds new values for a given control path
4495 * @to holds the current values of a given control path
4497 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4500 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4504 stack_init (ctx, to);
4507 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4510 stack_copy (&ctx->eval, to);
4512 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4513 stack_copy (to, &ctx->eval);
4516 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4518 if (from->size != to->size) {
4519 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4520 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));
4524 //FIXME we need to preserve CMMP attributes
4525 //FIXME we must take null literals into consideration.
4526 for (i = 0; i < from->size; ++i) {
4527 ILStackDesc *new_slot = from->stack + i;
4528 ILStackDesc *old_slot = to->stack + i;
4529 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4530 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4531 MonoClass *old_class = mono_class_from_mono_type (old_type);
4532 MonoClass *new_class = mono_class_from_mono_type (new_type);
4533 MonoClass *match_class = NULL;
4535 // S := T then U = S (new value is compatible with current value, keep current)
4536 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4537 copy_stack_value (new_slot, old_slot);
4541 // T := S then U = T (old value is compatible with current value, use new)
4542 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4543 copy_stack_value (old_slot, new_slot);
4547 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4548 char *old_name = stack_slot_full_name (old_slot);
4549 char *new_name = stack_slot_full_name (new_slot);
4550 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));
4556 //both are reference types, use closest common super type
4557 if (!mono_class_from_mono_type (old_type)->valuetype
4558 && !mono_class_from_mono_type (new_type)->valuetype
4559 && !stack_slot_is_managed_pointer (old_slot)
4560 && !stack_slot_is_managed_pointer (new_slot)) {
4562 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4563 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4564 match_class = old_class->supertypes [j];
4569 mono_class_setup_interfaces (old_class, &error);
4570 if (!mono_error_ok (&error)) {
4571 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot merge stacks due to a TypeLoadException %s at 0x%04x", mono_error_get_message (&error), ctx->ip_offset));
4572 mono_error_cleanup (&error);
4575 for (j = 0; j < old_class->interface_count; ++j) {
4576 for (k = 0; k < new_class->interface_count; ++k) {
4577 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4578 match_class = old_class->interfaces [j];
4584 //No decent super type found, use object
4585 match_class = mono_defaults.object_class;
4587 } 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)) {
4588 match_class = mono_defaults.object_class;
4593 char *old_name = stack_slot_full_name (old_slot);
4594 char *new_name = stack_slot_full_name (new_slot);
4595 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));
4599 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4603 g_assert (match_class);
4604 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4605 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4611 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4612 to->flags |= IL_CODE_STACK_MERGED;
4615 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4616 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4619 * is_clause_in_range :
4621 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4624 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4626 if (clause->try_offset >= start && clause->try_offset < end)
4628 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4634 * is_clause_inside_range :
4636 * Returns TRUE if @clause lies completely inside the @start - @end range.
4639 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4641 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4643 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4649 * is_clause_nested :
4651 * Returns TRUE if @nested is nested in @clause.
4654 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4656 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4658 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4659 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4662 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4663 * the each pair of exception must have the following properties:
4664 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4665 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4666 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4669 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4671 /*clause is nested*/
4672 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4673 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4677 /*wrong nesting order.*/
4678 if (is_clause_nested (clause, to_test)) {
4679 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4683 /*mutual protection*/
4684 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4685 /*handlers are not disjoint*/
4686 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4687 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4690 /* handlers are not catch or filter */
4691 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4692 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4699 /*not completelly disjoint*/
4700 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4701 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4702 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4705 #define code_bounds_check(size) \
4706 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4707 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4712 mono_opcode_is_prefix (int op)
4715 case MONO_CEE_UNALIGNED_:
4716 case MONO_CEE_VOLATILE_:
4717 case MONO_CEE_TAIL_:
4718 case MONO_CEE_CONSTRAINED_:
4719 case MONO_CEE_READONLY_:
4726 * FIXME: need to distinguish between valid and verifiable.
4727 * Need to keep track of types on the stack.
4728 * Verify types for opcodes.
4731 mono_method_verify (MonoMethod *method, int level)
4734 const unsigned char *ip, *code_start;
4735 const unsigned char *end;
4736 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4738 int i, n, need_merge = 0, start = 0;
4739 guint token, ip_offset = 0, prefix = 0;
4740 MonoGenericContext *generic_context = NULL;
4744 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4746 init_verifier_stats ();
4748 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4749 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4753 memset (&ctx, 0, sizeof (VerifyContext));
4755 //FIXME use mono_method_get_signature_full
4756 ctx.signature = mono_method_signature (method);
4757 if (!ctx.signature) {
4758 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4760 finish_collect_stats ();
4763 if (!method->is_generic && !method->klass->is_generic && ctx.signature->has_type_parameters) {
4764 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method and signature don't match in terms of genericity"));
4765 finish_collect_stats ();
4769 ctx.header = mono_method_get_header (method);
4771 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4772 finish_collect_stats ();
4775 ctx.method = method;
4776 code_start = ip = ctx.header->code;
4777 end = ip + ctx.header->code_size;
4778 ctx.image = image = method->klass->image;
4781 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4782 ctx.max_stack = ctx.header->max_stack;
4783 ctx.verifiable = ctx.valid = 1;
4786 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4787 ctx.code_size = ctx.header->code_size;
4788 MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4790 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4792 ctx.num_locals = ctx.header->num_locals;
4793 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4794 MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4796 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4797 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4799 ctx.params = g_new (MonoType*, ctx.max_args);
4800 MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4802 if (ctx.signature->hasthis)
4803 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4804 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4806 if (ctx.signature->is_inflated)
4807 ctx.generic_context = generic_context = mono_method_get_context (method);
4809 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4810 if (method->is_generic)
4811 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4813 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4816 for (i = 0; i < ctx.num_locals; ++i) {
4817 MonoType *uninflated = ctx.locals [i];
4818 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4819 if (!mono_error_ok (&error)) {
4820 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4821 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4823 mono_error_cleanup (&error);
4824 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4830 for (i = 0; i < ctx.max_args; ++i) {
4831 MonoType *uninflated = ctx.params [i];
4832 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4833 if (!mono_error_ok (&error)) {
4834 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4835 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4837 mono_error_cleanup (&error);
4838 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4843 stack_init (&ctx, &ctx.eval);
4845 for (i = 0; i < ctx.num_locals; ++i) {
4846 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4848 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4849 char *name = mono_type_full_name (ctx.locals [i]);
4850 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4857 for (i = 0; i < ctx.max_args; ++i) {
4858 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4861 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4862 char *name = mono_type_full_name (ctx.params [i]);
4863 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4872 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4873 MonoExceptionClause *clause = ctx.header->clauses + i;
4874 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); );
4876 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4877 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4879 if (clause->try_len <= 0)
4880 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4882 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4883 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4885 if (clause->handler_len <= 0)
4886 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4888 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4889 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4891 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4892 if (clause->data.filter_offset > ctx.code_size)
4893 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4895 if (clause->data.filter_offset >= clause->handler_offset)
4896 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4899 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4900 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4905 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4906 if (clause->try_offset + clause->try_len < ctx.code_size)
4907 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4908 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4909 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4911 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4912 if (!clause->data.catch_class) {
4913 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
4917 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4919 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4920 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4921 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4925 original_bb = bb = mono_basic_block_split (method, &error);
4926 if (!mono_error_ok (&error)) {
4927 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
4928 mono_error_cleanup (&error);
4933 while (ip < end && ctx.valid) {
4935 ip_offset = ip - code_start;
4937 const unsigned char *ip_copy = ip;
4940 if (ip_offset > bb->end) {
4941 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
4945 if (ip_offset == bb->end)
4948 op_size = mono_opcode_value_and_size (&ip_copy, end, &op);
4949 if (op_size == -1) {
4950 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
4954 if (ADD_IS_GREATER_OR_OVF (ip_offset, op_size, bb->end)) {
4955 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
4959 /*Last Instruction*/
4960 if (ip_offset + op_size == bb->end && mono_opcode_is_prefix (op)) {
4961 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets between prefix '%s' and instruction at 0x%04x", mono_opcode_name (op), ip_offset));
4966 ctx.ip_offset = ip_offset = ip - code_start;
4968 /*We need to check against fallthrou in and out of protected blocks.
4969 * For fallout we check the once a protected block ends, if the start flag is not set.
4970 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4971 * TODO convert these checks to be done using flags and not this loop
4973 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4974 MonoExceptionClause *clause = ctx.header->clauses + i;
4976 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4977 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4981 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4982 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4983 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4985 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4989 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4990 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4994 if (clause->handler_offset == ip_offset && start == 0) {
4995 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4999 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
5000 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
5005 /*This must be done after fallthru detection otherwise it won't happen.*/
5007 /*FIXME remove this once we move all bad branch checking code to use BB only*/
5008 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
5017 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
5018 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
5021 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
5024 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5025 #ifdef MONO_VERIFIER_DEBUG
5028 discode = mono_disasm_code_one (NULL, method, ip, NULL);
5029 discode [strlen (discode) - 1] = 0; /* no \n */
5030 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
5033 dump_stack_state (&ctx.code [ip_offset]);
5034 dump_stack_state (&ctx.eval);
5047 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
5053 code_bounds_check (2);
5054 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
5058 case CEE_ADD_OVF_UN:
5059 do_binop (&ctx, *ip, add_ovf_un_table);
5063 case CEE_SUB_OVF_UN:
5064 do_binop (&ctx, *ip, sub_ovf_un_table);
5071 case CEE_MUL_OVF_UN:
5072 do_binop (&ctx, *ip, bin_ovf_table);
5077 do_binop (&ctx, *ip, add_table);
5082 do_binop (&ctx, *ip, sub_table);
5089 do_binop (&ctx, *ip, bin_op_table);
5098 do_binop (&ctx, *ip, int_bin_op_table);
5105 do_binop (&ctx, *ip, shift_op_table);
5110 if (!check_underflow (&ctx, 1))
5112 stack_pop_safe (&ctx);
5126 /*TODO support definite assignment verification? */
5127 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
5135 store_local (&ctx, *ip - CEE_STLOC_0);
5140 code_bounds_check (2);
5141 store_local (&ctx, ip [1]);
5146 code_bounds_check (2);
5147 store_arg (&ctx, ip [1]);
5161 if (check_overflow (&ctx))
5162 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5167 code_bounds_check (2);
5168 if (check_overflow (&ctx))
5169 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5174 code_bounds_check (5);
5175 if (check_overflow (&ctx))
5176 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
5181 code_bounds_check (9);
5182 if (check_overflow (&ctx))
5183 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
5188 code_bounds_check (5);
5189 if (check_overflow (&ctx))
5190 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5195 code_bounds_check (9);
5196 if (check_overflow (&ctx))
5197 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5202 if (check_overflow (&ctx))
5203 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
5209 code_bounds_check (2);
5210 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
5223 code_bounds_check (2);
5224 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5231 code_bounds_check (5);
5232 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5245 code_bounds_check (5);
5246 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5253 code_bounds_check (2);
5254 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5259 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5265 if (!check_underflow (&ctx, 1))
5267 if (!check_overflow (&ctx))
5269 top = stack_push (&ctx);
5270 copy_stack_value (top, stack_peek (&ctx, 1));
5276 code_bounds_check (5);
5278 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5279 token = read32 (ip + 1);
5280 if (in_any_block (ctx.header, ip_offset))
5281 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5283 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5285 * FIXME: check signature, retval, arguments etc.
5291 code_bounds_check (5);
5292 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5297 code_bounds_check (5);
5298 token = read32 (ip + 1);
5300 * FIXME: check signature, retval, arguments etc.
5301 * FIXME: check requirements for tail call
5303 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5307 code_bounds_check (2);
5308 do_static_branch (&ctx, (signed char)ip [1] + 2);
5316 code_bounds_check (2);
5317 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5323 code_bounds_check (5);
5324 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5332 code_bounds_check (5);
5333 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5340 code_bounds_check (5);
5341 entries = read32 (ip + 1);
5343 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5344 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5347 code_bounds_check (sizeof (guint32) * entries);
5349 do_switch (&ctx, entries, ip);
5350 ip += sizeof (guint32) * entries;
5364 do_load_indirect (&ctx, *ip);
5376 do_store_indirect (&ctx, *ip);
5382 do_unary_math_op (&ctx, *ip);
5392 do_conversion (&ctx, TYPE_I4);
5398 do_conversion (&ctx, TYPE_I8);
5405 do_conversion (&ctx, TYPE_R8);
5411 do_conversion (&ctx, TYPE_NATIVE_INT);
5416 code_bounds_check (5);
5417 do_cpobj (&ctx, read32 (ip + 1));
5422 code_bounds_check (5);
5423 do_ldobj_value (&ctx, read32 (ip + 1));
5428 code_bounds_check (5);
5429 do_ldstr (&ctx, read32 (ip + 1));
5434 code_bounds_check (5);
5435 do_newobj (&ctx, read32 (ip + 1));
5441 code_bounds_check (5);
5442 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5448 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5453 code_bounds_check (5);
5454 do_unbox_value (&ctx, read32 (ip + 1));
5466 code_bounds_check (5);
5467 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5473 code_bounds_check (5);
5474 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5479 code_bounds_check (5);
5480 do_store_field (&ctx, read32 (ip + 1));
5485 code_bounds_check (5);
5486 do_store_static_field (&ctx, read32 (ip + 1));
5491 code_bounds_check (5);
5492 do_stobj (&ctx, read32 (ip + 1));
5496 case CEE_CONV_OVF_I1_UN:
5497 case CEE_CONV_OVF_I2_UN:
5498 case CEE_CONV_OVF_I4_UN:
5499 case CEE_CONV_OVF_U1_UN:
5500 case CEE_CONV_OVF_U2_UN:
5501 case CEE_CONV_OVF_U4_UN:
5502 do_conversion (&ctx, TYPE_I4);
5506 case CEE_CONV_OVF_I8_UN:
5507 case CEE_CONV_OVF_U8_UN:
5508 do_conversion (&ctx, TYPE_I8);
5512 case CEE_CONV_OVF_I_UN:
5513 case CEE_CONV_OVF_U_UN:
5514 do_conversion (&ctx, TYPE_NATIVE_INT);
5519 code_bounds_check (5);
5520 do_box_value (&ctx, read32 (ip + 1));
5525 code_bounds_check (5);
5526 do_newarr (&ctx, read32 (ip + 1));
5536 code_bounds_check (5);
5537 do_ldelema (&ctx, read32 (ip + 1));
5551 case CEE_LDELEM_REF:
5552 do_ldelem (&ctx, *ip, 0);
5563 case CEE_STELEM_REF:
5564 do_stelem (&ctx, *ip, 0);
5569 code_bounds_check (5);
5570 do_ldelem (&ctx, *ip, read32 (ip + 1));
5575 code_bounds_check (5);
5576 do_stelem (&ctx, *ip, read32 (ip + 1));
5581 code_bounds_check (5);
5582 do_unbox_any (&ctx, read32 (ip + 1));
5586 case CEE_CONV_OVF_I1:
5587 case CEE_CONV_OVF_U1:
5588 case CEE_CONV_OVF_I2:
5589 case CEE_CONV_OVF_U2:
5590 case CEE_CONV_OVF_I4:
5591 case CEE_CONV_OVF_U4:
5592 do_conversion (&ctx, TYPE_I4);
5596 case CEE_CONV_OVF_I8:
5597 case CEE_CONV_OVF_U8:
5598 do_conversion (&ctx, TYPE_I8);
5602 case CEE_CONV_OVF_I:
5603 case CEE_CONV_OVF_U:
5604 do_conversion (&ctx, TYPE_NATIVE_INT);
5609 code_bounds_check (5);
5610 do_refanyval (&ctx, read32 (ip + 1));
5620 code_bounds_check (5);
5621 do_mkrefany (&ctx, read32 (ip + 1));
5626 code_bounds_check (5);
5627 do_load_token (&ctx, read32 (ip + 1));
5631 case CEE_ENDFINALLY:
5632 if (!is_correct_endfinally (ctx.header, ip_offset))
5633 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5640 code_bounds_check (5);
5641 do_leave (&ctx, read32 (ip + 1) + 5);
5648 code_bounds_check (2);
5649 do_leave (&ctx, (signed char)ip [1] + 2);
5656 code_bounds_check (2);
5660 code_bounds_check (3);
5661 store_local (&ctx, read16 (ip + 1));
5666 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5674 do_cmp_op (&ctx, cmp_br_op, *ip);
5679 code_bounds_check (3);
5680 store_arg (&ctx, read16 (ip + 1) );
5686 if (!check_overflow (&ctx))
5688 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5689 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5690 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5695 code_bounds_check (5);
5696 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5701 code_bounds_check (5);
5702 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5708 code_bounds_check (3);
5709 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5715 code_bounds_check (3);
5716 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5729 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5733 do_endfilter (&ctx);
5737 case CEE_UNALIGNED_:
5738 code_bounds_check (2);
5739 prefix |= PREFIX_UNALIGNED;
5743 prefix |= PREFIX_VOLATILE;
5747 prefix |= PREFIX_TAIL;
5749 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5750 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5754 code_bounds_check (5);
5755 do_initobj (&ctx, read32 (ip + 1));
5759 case CEE_CONSTRAINED_:
5760 code_bounds_check (5);
5761 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5762 prefix |= PREFIX_CONSTRAINED;
5767 prefix |= PREFIX_READONLY;
5772 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5773 if (!check_underflow (&ctx, 3))
5775 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5780 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5781 if (!check_underflow (&ctx, 3))
5783 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5791 if (!is_correct_rethrow (ctx.header, ip_offset))
5792 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5799 code_bounds_check (5);
5800 do_sizeof (&ctx, read32 (ip + 1));
5804 case CEE_REFANYTYPE:
5805 do_refanytype (&ctx);
5810 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5816 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5820 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5822 if (!ctx.prefix_set) //first prefix
5823 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5824 ctx.prefix_set |= prefix;
5825 ctx.has_flags = TRUE;
5829 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5831 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5832 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5833 if (ctx.prefix_set & PREFIX_READONLY)
5834 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5835 if (ctx.prefix_set & PREFIX_VOLATILE)
5836 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5837 if (ctx.prefix_set & PREFIX_UNALIGNED)
5838 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5839 ctx.prefix_set = prefix = 0;
5840 ctx.has_flags = FALSE;
5844 * if ip != end we overflowed: mark as error.
5846 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5847 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5850 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5851 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5852 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5853 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5854 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5856 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5857 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5859 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5860 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5862 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5863 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));
5866 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5867 char *method_name = mono_method_full_name (ctx.method, TRUE);
5868 char *type = mono_type_get_full_name (ctx.method->klass);
5869 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5870 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor due to a TypeLoadException on base type.", method_name, type));
5872 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5873 g_free (method_name);
5879 for (i = 0; i < ctx.header->code_size; ++i) {
5880 if (ctx.code [i].stack)
5881 g_free (ctx.code [i].stack);
5885 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5887 g_slist_free (ctx.funptrs);
5889 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5890 mono_metadata_free_type (tmp->data);
5891 g_slist_free (ctx.exception_types);
5893 for (i = 0; i < ctx.num_locals; ++i) {
5895 mono_metadata_free_type (ctx.locals [i]);
5897 for (i = 0; i < ctx.max_args; ++i) {
5899 mono_metadata_free_type (ctx.params [i]);
5903 g_free (ctx.eval.stack);
5906 g_free (ctx.locals);
5907 g_free (ctx.params);
5908 mono_basic_block_free (original_bb);
5909 mono_metadata_free_mh (ctx.header);
5911 finish_collect_stats ();
5916 mono_verify_corlib ()
5918 /* This is a public API function so cannot be removed */
5923 * Returns true if @method needs to be verified.
5927 mono_verifier_is_enabled_for_method (MonoMethod *method)
5929 return mono_verifier_is_enabled_for_class (method->klass) && (method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD);
5933 * Returns true if @klass need to be verified.
5937 mono_verifier_is_enabled_for_class (MonoClass *klass)
5939 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
5943 mono_verifier_is_enabled_for_image (MonoImage *image)
5945 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
5949 * Dynamic methods are not considered full trust since if the user is trusted and need to
5950 * generate unsafe code, make the method skip verification - this is a known good way to do it.
5953 mono_verifier_is_method_full_trust (MonoMethod *method)
5955 return mono_verifier_is_class_full_trust (method->klass) && !method->dynamic;
5959 * Returns if @klass is under full trust or not.
5961 * TODO This code doesn't take CAS into account.
5963 * Under verify_all all user code must be verifiable if no security option was set
5967 mono_verifier_is_class_full_trust (MonoClass *klass)
5969 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5970 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
5971 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
5973 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5974 return trusted_location || klass->image == mono_defaults.corlib;
5975 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
5979 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
5981 return mono_method_verify (method,
5982 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5983 | (!is_fulltrust && !mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5984 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5988 get_field_end (MonoClassField *field)
5991 int size = mono_type_size (field->type, &align);
5993 size = 4; /*FIXME Is this a safe bet?*/
5994 return size + field->offset;
5998 verify_class_for_overlapping_reference_fields (MonoClass *class)
6001 gpointer iter = NULL;
6002 MonoClassField *field;
6003 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
6004 /*We can't skip types with !has_references since this is calculated after we have run.*/
6005 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
6009 /*We must check for stuff overlapping reference fields.
6010 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
6012 while ((field = mono_class_get_fields (class, &iter))) {
6013 int fieldEnd = get_field_end (field);
6014 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
6017 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
6020 for (j = i; j < class->field.count; ++j) {
6021 MonoClassField *other = &class->fields [j];
6022 int otherEnd = get_field_end (other);
6023 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
6026 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
6029 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
6037 field_hash (gconstpointer key)
6039 const MonoClassField *field = key;
6040 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
6044 field_equals (gconstpointer _a, gconstpointer _b)
6046 const MonoClassField *a = _a;
6047 const MonoClassField *b = _b;
6048 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
6053 verify_class_fields (MonoClass *class)
6055 gpointer iter = NULL;
6056 MonoClassField *field;
6057 MonoGenericContext *context = mono_class_get_context (class);
6058 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
6059 if (class->generic_container)
6060 context = &class->generic_container->context;
6062 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
6063 if (!mono_type_is_valid_type_in_context (field->type, context)) {
6064 g_hash_table_destroy (unique_fields);
6067 if (g_hash_table_lookup (unique_fields, field)) {
6068 g_hash_table_destroy (unique_fields);
6071 g_hash_table_insert (unique_fields, field, field);
6073 g_hash_table_destroy (unique_fields);
6078 verify_interfaces (MonoClass *class)
6081 for (i = 0; i < class->interface_count; ++i) {
6082 MonoClass *iface = class->interfaces [i];
6083 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
6090 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
6093 gpointer iter = NULL;
6094 MonoClassField *field;
6095 MonoClass *field_class;
6097 if (!class->valuetype)
6100 type = class->byval_arg.type;
6101 /*primitive type fields are not properly decoded*/
6102 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
6105 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
6109 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
6112 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
6114 if (field_class == target_class || class == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
6122 verify_valuetype_layout (MonoClass *class)
6125 res = verify_valuetype_layout_with_target (class, class);
6130 recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
6133 MonoClass **constraints;
6134 MonoGenericParamInfo *param_info;
6136 g_assert (mono_type_is_generic_argument (type));
6138 idx = mono_type_get_generic_param_num (type);
6139 if (mono_bitset_test_fast (used_args, idx))
6142 mono_bitset_set_fast (used_args, idx);
6143 param_info = mono_generic_container_get_param_info (gc, idx);
6145 if (!param_info->constraints)
6148 for (constraints = param_info->constraints; *constraints; ++constraints) {
6149 MonoClass *ctr = *constraints;
6150 MonoType *constraint_type = &ctr->byval_arg;
6152 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6159 verify_generic_parameters (MonoClass *class)
6162 MonoGenericContainer *gc = class->generic_container;
6163 MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
6165 for (i = 0; i < gc->type_argc; ++i) {
6166 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
6167 MonoClass **constraints;
6169 if (!param_info->constraints)
6172 mono_bitset_clear_all (used_args);
6173 mono_bitset_set_fast (used_args, i);
6175 for (constraints = param_info->constraints; *constraints; ++constraints) {
6176 MonoClass *ctr = *constraints;
6177 MonoType *constraint_type = &ctr->byval_arg;
6179 if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
6182 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6184 if (ctr->generic_class && !mono_class_is_valid_generic_instantiation (NULL, ctr))
6188 mono_bitset_free (used_args);
6192 mono_bitset_free (used_args);
6197 * Check if the class is verifiable.
6199 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6200 * field and invalid generic instantiation are fatal errors.
6202 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6206 mono_verifier_verify_class (MonoClass *class)
6208 /*Neither <Module>, object or ifaces have parent.*/
6209 if (!class->parent &&
6210 class != mono_defaults.object_class &&
6211 !MONO_CLASS_IS_INTERFACE (class) &&
6212 (!class->image->dynamic && class->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
6214 if (class->parent) {
6215 if (MONO_CLASS_IS_INTERFACE (class->parent))
6217 if (!class->generic_class && class->parent->generic_container)
6219 if (class->parent->generic_class && !class->generic_class) {
6220 MonoGenericContext *context = mono_class_get_context (class);
6221 if (class->generic_container)
6222 context = &class->generic_container->context;
6223 if (!mono_type_is_valid_type_in_context (&class->parent->byval_arg, context))
6227 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
6229 if (class->generic_container && !verify_generic_parameters (class))
6231 if (!verify_class_for_overlapping_reference_fields (class))
6233 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
6235 if (class->generic_class == NULL && !verify_class_fields (class))
6237 if (class->valuetype && !verify_valuetype_layout (class))
6239 if (!verify_interfaces (class))
6245 mono_verifier_class_is_valid_generic_instantiation (MonoClass *class)
6247 return mono_class_is_valid_generic_instantiation (NULL, class);
6251 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)
6253 if (!method->is_inflated)
6255 return mono_method_is_valid_generic_instantiation (NULL, method);
6261 mono_verifier_verify_class (MonoClass *class)
6263 /* The verifier was disabled at compile time */
6268 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
6270 /* The verifier was disabled at compile time */
6275 mono_verifier_is_class_full_trust (MonoClass *klass)
6277 /* The verifier was disabled at compile time */
6282 mono_verifier_is_method_full_trust (MonoMethod *method)
6284 /* The verifier was disabled at compile time */
6289 mono_verifier_is_enabled_for_image (MonoImage *image)
6291 /* The verifier was disabled at compile time */
6296 mono_verifier_is_enabled_for_class (MonoClass *klass)
6298 /* The verifier was disabled at compile time */
6303 mono_verifier_is_enabled_for_method (MonoMethod *method)
6305 /* The verifier was disabled at compile time */
6310 mono_method_verify (MonoMethod *method, int level)
6312 /* The verifier was disabled at compile time */
6317 mono_free_verify_list (GSList *list)
6319 /* The verifier was disabled at compile time */
6320 /* will always be null if verifier is disabled */
6324 mono_verifier_class_is_valid_generic_instantiation (MonoClass *class)
6330 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)