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
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <mono/metadata/object-internals.h>
15 #include <mono/metadata/dynamic-image-internals.h>
16 #include <mono/metadata/verify.h>
17 #include <mono/metadata/verify-internals.h>
18 #include <mono/metadata/opcodes.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/reflection.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/mono-endian.h>
23 #include <mono/metadata/metadata.h>
24 #include <mono/metadata/metadata-internals.h>
25 #include <mono/metadata/class-internals.h>
26 #include <mono/metadata/security-manager.h>
27 #include <mono/metadata/security-core-clr.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/mono-basic-block.h>
30 #include <mono/metadata/attrdefs.h>
31 #include <mono/utils/mono-counters.h>
32 #include <mono/utils/monobitset.h>
36 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
37 static gboolean verify_all = FALSE;
40 * Set the desired level of checks for the verfier.
44 mono_verifier_set_mode (MiniVerifierMode mode)
50 mono_verifier_enable_verify_all ()
55 #ifndef DISABLE_VERIFIER
57 * Pull the list of opcodes
59 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
63 #include "mono/cil/opcode.def"
68 #ifdef MONO_VERIFIER_DEBUG
69 #define VERIFIER_DEBUG(code) do { code } while (0)
71 #define VERIFIER_DEBUG(code)
74 //////////////////////////////////////////////////////////////////
75 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
76 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
77 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
78 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
79 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
80 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
82 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
83 vinfo->info.status = __status; \
84 vinfo->info.message = ( __msg ); \
85 vinfo->exception_type = (__exception); \
86 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
89 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
90 #define ADD_VERIFY_ERROR(__ctx, __msg) \
92 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
96 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
98 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
99 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
100 (__ctx)->verifiable = 0; \
101 if (IS_FAIL_FAST_MODE (__ctx)) \
102 (__ctx)->valid = 0; \
106 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
108 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
109 (__ctx)->valid = 0; \
112 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
114 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
115 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
116 (__ctx)->verifiable = 0; \
117 if (IS_FAIL_FAST_MODE (__ctx)) \
118 (__ctx)->valid = 0; \
122 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
123 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
125 #if SIZEOF_VOID_P == 4
126 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
128 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
131 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
132 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
134 /*Flags to be used with ILCodeDesc::flags */
136 /*Instruction has not been processed.*/
137 IL_CODE_FLAG_NOT_PROCESSED = 0,
138 /*Instruction was decoded by mono_method_verify loop.*/
139 IL_CODE_FLAG_SEEN = 1,
140 /*Instruction was target of a branch or is at a protected block boundary.*/
141 IL_CODE_FLAG_WAS_TARGET = 2,
142 /*Used by stack_init to avoid double initialize each entry.*/
143 IL_CODE_FLAG_STACK_INITED = 4,
144 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
145 IL_CODE_STACK_MERGED = 8,
146 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
147 IL_CODE_DELEGATE_SEQUENCE = 0x10,
148 /*This is a delegate created from a ldftn to a non final virtual method*/
149 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
150 /*This is a call to a non final virtual method*/
151 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
169 guint16 size, max_size;
186 /*Allocated fnptr MonoType that should be freed by us.*/
188 /*Type dup'ed exception types from catch blocks.*/
189 GSList *exception_types;
194 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
198 MonoMethodSignature *signature;
199 MonoMethodHeader *header;
201 MonoGenericContext *generic_context;
205 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
206 *on a method that creates a delegate for a non-final virtual method using ldftn*/
207 gboolean has_this_store;
209 /*This flag is used to control if the contructor of the parent class has been called.
210 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
211 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
213 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
214 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
216 gboolean super_ctor_called;
220 MonoType *constrained_type;
224 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
227 get_stack_type (MonoType *type);
230 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
233 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
236 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
238 static MonoGenericParam*
239 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type);
242 verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate);
243 //////////////////////////////////////////////////////////////////
248 TYPE_INV = 0, /* leave at 0. */
253 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
255 /* value types and classes */
257 /* Number of types, used to define the size of the tables*/
260 /* Used by tables to signal that a result is not verifiable*/
261 NON_VERIFIABLE_RESULT = 0x80,
263 /*Mask used to extract just the type, excluding flags */
266 /* The stack type is a managed pointer, unmask the value to res */
267 POINTER_MASK = 0x100,
269 /*Stack type with the pointer mask*/
270 RAW_TYPE_MASK = 0x10F,
272 /* Controlled Mutability Manager Pointer */
275 /* The stack type is a null literal*/
276 NULL_LITERAL_MASK = 0x400,
278 /**Used by ldarg.0 and family to let delegate verification happens.*/
279 THIS_POINTER_MASK = 0x800,
281 /**Signals that this is a boxed value type*/
284 /*This is an unitialized this ref*/
285 UNINIT_THIS_MASK = 0x2000,
288 static const char* const
289 type_names [TYPE_MAX + 1] = {
300 PREFIX_UNALIGNED = 1,
303 PREFIX_CONSTRAINED = 8,
306 //////////////////////////////////////////////////////////////////
308 #ifdef ENABLE_VERIFIER_STATS
310 #define _MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
311 #define _MEM_FREE(amt) do { working_set -= (amt); } while (0)
313 static int allocated_memory;
314 static int working_set;
315 static int max_allocated_memory;
316 static int max_working_set;
317 static int total_allocated_memory;
320 finish_collect_stats (void)
322 max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
323 max_working_set = MAX (max_working_set, working_set);
324 total_allocated_memory += allocated_memory;
325 allocated_memory = working_set = 0;
329 init_verifier_stats (void)
331 static gboolean inited;
334 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
335 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
336 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
342 #define _MEM_ALLOC(amt) do {} while (0)
343 #define _MEM_FREE(amt) do { } while (0)
345 #define finish_collect_stats()
346 #define init_verifier_stats()
351 //////////////////////////////////////////////////////////////////
354 /*Token validation macros and functions */
355 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
356 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
357 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
358 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
360 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
361 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
362 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
363 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
364 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
365 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
368 * Verify if @token refers to a valid row on int's table.
371 token_bounds_check (MonoImage *image, guint32 token)
373 if (image_is_dynamic (image))
374 return mono_dynamic_image_is_valid_token ((MonoDynamicImage*)image, token);
375 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token) && mono_metadata_token_index (token) > 0;
379 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
381 MonoType *res = g_new0 (MonoType, 1);
382 _MEM_ALLOC (sizeof (MonoType));
384 //FIXME use mono_method_get_signature_full
385 res->data.method = mono_method_signature (method);
386 res->type = MONO_TYPE_FNPTR;
387 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
392 * mono_type_is_enum_type:
394 * Returns: TRUE if @type is an enum type.
397 mono_type_is_enum_type (MonoType *type)
399 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
401 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
407 * mono_type_is_value_type:
409 * Returns: TRUE if @type is named after @namespace.@name.
413 mono_type_is_value_type (MonoType *type, const char *namespace_, const char *name)
415 return type->type == MONO_TYPE_VALUETYPE &&
416 !strcmp (namespace_, type->data.klass->name_space) &&
417 !strcmp (name, type->data.klass->name);
421 * Returns TURE if @type is VAR or MVAR
424 mono_type_is_generic_argument (MonoType *type)
426 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
430 * mono_type_get_underlying_type_any:
432 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
434 * Returns the underlying type of @type regardless if it is byref or not.
437 mono_type_get_underlying_type_any (MonoType *type)
439 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
440 return mono_class_enum_basetype (type->data.klass);
441 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
442 return mono_class_enum_basetype (type->data.generic_class->container_class);
446 static G_GNUC_UNUSED const char*
447 mono_type_get_stack_name (MonoType *type)
449 return type_names [get_stack_type (type) & TYPE_MASK];
452 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
453 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
456 mono_method_is_constructor (MonoMethod *method)
458 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
459 !(method->flags & CTOR_INVALID_FLAGS) &&
460 !strcmp (".ctor", method->name));
464 mono_class_has_default_constructor (MonoClass *klass)
469 mono_class_setup_methods (klass);
470 if (mono_class_has_failure (klass))
473 int mcount = mono_class_get_method_count (klass);
474 for (i = 0; i < mcount; ++i) {
475 method = klass->methods [i];
476 if (mono_method_is_constructor (method) &&
477 mono_method_signature (method) &&
478 mono_method_signature (method)->param_count == 0 &&
479 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
486 * Verify if @type is valid for the given @ctx verification context.
487 * this function checks for VAR and MVAR types that are invalid under the current verifier,
490 mono_type_is_valid_type_in_context_full (MonoType *type, MonoGenericContext *context, gboolean check_gtd)
493 MonoGenericInst *inst;
495 switch (type->type) {
500 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
501 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
504 case MONO_TYPE_SZARRAY:
505 return mono_type_is_valid_type_in_context_full (&type->data.klass->byval_arg, context, check_gtd);
506 case MONO_TYPE_ARRAY:
507 return mono_type_is_valid_type_in_context_full (&type->data.array->eklass->byval_arg, context, check_gtd);
509 return mono_type_is_valid_type_in_context_full (type->data.type, context, check_gtd);
510 case MONO_TYPE_GENERICINST:
511 inst = type->data.generic_class->context.class_inst;
514 for (i = 0; i < inst->type_argc; ++i)
515 if (!mono_type_is_valid_type_in_context_full (inst->type_argv [i], context, check_gtd))
518 case MONO_TYPE_CLASS:
519 case MONO_TYPE_VALUETYPE: {
520 MonoClass *klass = type->data.klass;
522 * It's possible to encode generic'sh types in such a way that they disguise themselves as class or valuetype.
523 * Fixing the type decoding is really tricky since under some cases this behavior is needed, for example, to
524 * have a 'class' type pointing to a 'genericinst' class.
526 * For the runtime these non canonical (weird) encodings work fine, the worst they can cause is some
527 * reflection oddities which are harmless - to security at least.
529 if (klass->byval_arg.type != type->type)
530 return mono_type_is_valid_type_in_context_full (&klass->byval_arg, context, check_gtd);
532 if (check_gtd && mono_class_is_gtd (klass))
543 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
545 return mono_type_is_valid_type_in_context_full (type, context, FALSE);
548 /*This function returns NULL if the type is not instantiatable*/
550 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
555 result = mono_class_inflate_generic_type_checked (type, context, &error);
556 if (!mono_error_ok (&error)) {
557 mono_error_cleanup (&error);
563 /*A side note here. We don't need to check if arguments are broken since this
564 is only need to be done by the runtime before realizing the type.
567 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
572 if (ginst->type_argc != gc->type_argc)
575 for (i = 0; i < gc->type_argc; ++i) {
576 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
577 MonoClass *paramClass;
578 MonoClass **constraints;
579 MonoType *param_type = ginst->type_argv [i];
581 /*it's not our job to validate type variables*/
582 if (mono_type_is_generic_argument (param_type))
585 paramClass = mono_class_from_mono_type (param_type);
588 /* A GTD can't be a generic argument.
590 * Due to how types are encoded we must check for the case of a genericinst MonoType and GTD MonoClass.
591 * This happens in cases such as: class Foo<T> { void X() { new Bar<T> (); } }
593 * Open instantiations can have GTDs as this happens when one type is instantiated with others params
594 * and the former has an expansion into the later. For example:
596 * class A<T>: B<K> {}
597 * The type A <K> has a parent B<K>, that is inflated into the GTD B<>.
598 * Since A<K> is open, thus not instantiatable, this is valid.
600 if (mono_class_is_gtd (paramClass) && param_type->type != MONO_TYPE_GENERICINST && !ginst->is_open)
603 /*it's not safe to call mono_class_init from here*/
604 if (mono_class_is_ginst (paramClass) && !paramClass->inited) {
605 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
609 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
612 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
615 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
618 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
621 if (!param_info->constraints)
624 for (constraints = param_info->constraints; *constraints; ++constraints) {
625 MonoClass *ctr = *constraints;
628 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
629 if (!mono_error_ok (&error)) {
630 mono_error_cleanup (&error);
633 ctr = mono_class_from_mono_type (inflated);
634 mono_metadata_free_type (inflated);
636 /*FIXME maybe we need the same this as verifier_class_is_assignable_from*/
637 if (!mono_class_is_assignable_from_slow (ctr, paramClass))
645 * mono_generic_param_is_constraint_compatible:
647 * \returns TRUE if \p candidate is constraint compatible with \p target.
649 * This means that \p candidate constraints are a super set of \p target constaints
652 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoClass *candidate_param_class, MonoGenericContext *context)
654 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
655 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
656 MonoClass **candidate_class;
657 gboolean class_constraint_satisfied = FALSE;
658 gboolean valuetype_constraint_satisfied = FALSE;
660 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
661 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
663 if (cinfo->constraints) {
664 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
666 MonoType *inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
669 cc = mono_class_from_mono_type (inflated);
670 mono_metadata_free_type (inflated);
672 if (mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
673 class_constraint_satisfied = TRUE;
674 else if (!mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
675 valuetype_constraint_satisfied = TRUE;
678 class_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0;
679 valuetype_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) != 0;
681 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && !class_constraint_satisfied)
683 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && !valuetype_constraint_satisfied)
685 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !((cmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) ||
686 valuetype_constraint_satisfied)) {
691 if (tinfo->constraints) {
692 MonoClass **target_class;
693 for (target_class = tinfo->constraints; *target_class; ++target_class) {
695 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
698 tc = mono_class_from_mono_type (inflated);
699 mono_metadata_free_type (inflated);
702 * A constraint from @target might inflate into @candidate itself and in that case we don't need
703 * check it's constraints since it satisfy the constraint by itself.
705 if (mono_metadata_type_equal (&tc->byval_arg, &candidate_param_class->byval_arg))
708 if (!cinfo->constraints)
711 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
713 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
716 cc = mono_class_from_mono_type (inflated);
717 mono_metadata_free_type (inflated);
719 if (verifier_class_is_assignable_from (tc, cc))
723 * This happens when we have the following:
725 * Bar<K> where K : IFace
726 * Foo<T, U> where T : U where U : IFace
728 * Bar<T> <- T here satisfy K constraint transitively through to U's constraint
731 if (mono_type_is_generic_argument (&cc->byval_arg)) {
732 MonoGenericParam *other_candidate = verifier_get_generic_param_from_type (ctx, &cc->byval_arg);
734 if (mono_generic_param_is_constraint_compatible (ctx, target, other_candidate, cc, context)) {
739 if (!*candidate_class)
746 static MonoGenericParam*
747 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
749 MonoGenericContainer *gc;
750 MonoMethod *method = ctx->method;
753 num = mono_type_get_generic_param_num (type);
755 if (type->type == MONO_TYPE_VAR) {
756 MonoClass *gtd = method->klass;
757 if (mono_class_is_ginst (gtd))
758 gtd = mono_class_get_generic_class (gtd)->container_class;
759 gc = mono_class_try_get_generic_container (gtd);
761 MonoMethod *gmd = method;
762 if (method->is_inflated)
763 gmd = ((MonoMethodInflated*)method)->declaring;
764 gc = mono_method_get_generic_container (gmd);
768 return mono_generic_container_get_param (gc, num);
774 * Verify if @type is valid for the given @ctx verification context.
775 * this function checks for VAR and MVAR types that are invalid under the current verifier,
776 * This means that it either
779 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
781 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
785 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst, gboolean check_gtd)
788 for (i = 0; i < ginst->type_argc; ++i) {
789 MonoType *type = ginst->type_argv [i];
791 if (!mono_type_is_valid_type_in_context_full (type, ctx->generic_context, TRUE))
798 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
801 for (i = 0; i < ginst->type_argc; ++i) {
802 MonoType *type = ginst->type_argv [i];
803 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
804 MonoGenericParam *candidate;
805 MonoClass *candidate_class;
807 if (!mono_type_is_generic_argument (type))
810 if (!is_valid_type_in_context (ctx, type))
813 candidate = verifier_get_generic_param_from_type (ctx, type);
814 candidate_class = mono_class_from_mono_type (type);
816 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, candidate_class, context))
823 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
825 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
826 MonoGenericInst *ginst = gmethod->context.method_inst;
827 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
828 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
832 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
834 MonoGenericClass *gklass = mono_class_get_generic_class (klass);
835 MonoGenericInst *ginst = gklass->context.class_inst;
836 MonoGenericContainer *gc = mono_class_get_generic_container (gklass->container_class);
837 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
841 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
843 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
844 MonoGenericInst *ginst = gmethod->context.method_inst;
845 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
846 if (!gc) /*non-generic inflated method - it's part of a generic type */
848 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
850 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
855 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
857 MonoGenericClass *gklass = mono_class_get_generic_class (klass);
858 MonoGenericInst *ginst = gklass->context.class_inst;
859 MonoGenericContainer *gc = mono_class_get_generic_container (gklass->container_class);
860 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
862 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
866 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
871 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
875 if (!is_valid_type_in_context (ctx, type)) {
876 char *str = mono_type_full_name (type);
877 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
878 str [0] == '!' ? "" : type->type == MONO_TYPE_VAR ? "!" : "!!",
880 type->type == MONO_TYPE_VAR ? "class" : "method",
882 MONO_EXCEPTION_BAD_IMAGE);
887 klass = mono_class_from_mono_type (type);
888 mono_class_init (klass);
889 if (mono_class_has_failure (klass)) {
890 if (mono_class_is_ginst (klass) && !mono_class_is_valid_generic_instantiation (NULL, klass))
891 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);
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 (mono_class_is_ginst (klass) && mono_class_has_failure (mono_class_get_generic_class (klass)->container_class)) {
898 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);
902 if (!mono_class_is_ginst (klass))
905 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
906 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);
910 if (!mono_class_repect_method_constraints (ctx, klass)) {
911 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);
918 static verify_result_t
919 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
921 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
922 return RESULT_INVALID;
924 if (!method->is_inflated)
927 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
928 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);
929 return RESULT_INVALID;
932 if (!mono_method_repect_method_constraints (ctx, method)) {
933 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));
934 return RESULT_UNVERIFIABLE;
940 static MonoClassField*
941 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
943 MonoClassField *field;
944 MonoClass *klass = NULL;
946 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
947 field = (MonoClassField *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
948 klass = field ? field->parent : NULL;
950 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
951 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);
955 field = mono_field_from_token_checked (ctx->image, token, &klass, ctx->generic_context, &error);
956 mono_error_cleanup (&error); /*FIXME don't swallow the error */
959 if (!field || !field->parent || !klass) {
960 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);
964 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
967 if (mono_field_get_flags (field) & FIELD_ATTRIBUTE_LITERAL) {
968 char *type_name = mono_type_get_full_name (field->parent);
969 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot reference literal field %s::%s at 0x%04x", type_name, field->name, ctx->ip_offset));
979 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
983 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
984 method = (MonoMethod *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
987 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
988 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);
992 method = mono_get_method_checked (ctx->image, token, NULL, ctx->generic_context, &error);
993 mono_error_cleanup (&error); /* FIXME don't swallow this error */
997 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);
1001 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
1008 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
1011 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
1012 MonoClass *klass = (MonoClass *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
1013 type = klass ? &klass->byval_arg : NULL;
1016 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
1017 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
1020 type = mono_type_get_checked (ctx->image, token, ctx->generic_context, &error);
1021 mono_error_cleanup (&error); /*FIXME don't swallow the error */
1025 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);
1029 if (!mono_type_is_valid_in_context (ctx, type))
1036 /* stack_slot_get_type:
1038 * Returns the stack type of @value. This value includes POINTER_MASK.
1040 * Use this function to checks that account for a managed pointer.
1043 stack_slot_get_type (ILStackDesc *value)
1045 return value->stype & RAW_TYPE_MASK;
1048 /* stack_slot_get_underlying_type:
1050 * Returns the stack type of @value. This value does not include POINTER_MASK.
1052 * Use this function is cases where the fact that the value could be a managed pointer is
1053 * irrelevant. For example, field load doesn't care about this fact of type on stack.
1056 stack_slot_get_underlying_type (ILStackDesc *value)
1058 return value->stype & TYPE_MASK;
1061 /* stack_slot_is_managed_pointer:
1063 * Returns TRUE is @value is a managed pointer.
1066 stack_slot_is_managed_pointer (ILStackDesc *value)
1068 return (value->stype & POINTER_MASK) == POINTER_MASK;
1071 /* stack_slot_is_managed_mutability_pointer:
1073 * Returns TRUE is @value is a managed mutability pointer.
1075 static G_GNUC_UNUSED gboolean
1076 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
1078 return (value->stype & CMMP_MASK) == CMMP_MASK;
1081 /* stack_slot_is_null_literal:
1083 * Returns TRUE is @value is the null literal.
1086 stack_slot_is_null_literal (ILStackDesc *value)
1088 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
1092 /* stack_slot_is_this_pointer:
1094 * Returns TRUE is @value is the this literal
1097 stack_slot_is_this_pointer (ILStackDesc *value)
1099 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
1102 /* stack_slot_is_boxed_value:
1104 * Returns TRUE is @value is a boxed value
1107 stack_slot_is_boxed_value (ILStackDesc *value)
1109 return (value->stype & BOXED_MASK) == BOXED_MASK;
1113 stack_slot_get_name (ILStackDesc *value)
1115 return type_names [value->stype & TYPE_MASK];
1118 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
1119 if (PRED (value)) { \
1121 g_string_append (str, ", "); \
1122 g_string_append (str, NAME); \
1127 stack_slot_stack_type_full_name (ILStackDesc *value)
1129 GString *str = g_string_new ("");
1131 gboolean has_pred = FALSE, first = TRUE;
1133 if ((value->stype & TYPE_MASK) != value->stype) {
1134 g_string_append(str, "[");
1135 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
1136 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
1137 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
1138 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
1139 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1143 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1145 g_string_append(str, "[");
1147 g_string_append (str, ", ");
1148 g_string_append (str, "unboxed");
1153 g_string_append(str, "] ");
1155 g_string_append (str, stack_slot_get_name (value));
1157 g_string_free (str, FALSE);
1162 stack_slot_full_name (ILStackDesc *value)
1164 char *type_name = mono_type_full_name (value->type);
1165 char *stack_name = stack_slot_stack_type_full_name (value);
1166 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1168 g_free (stack_name);
1172 //////////////////////////////////////////////////////////////////
1175 * mono_free_verify_list:
1178 mono_free_verify_list (GSList *list)
1180 MonoVerifyInfoExtended *info;
1183 for (tmp = list; tmp; tmp = tmp->next) {
1184 info = (MonoVerifyInfoExtended *)tmp->data;
1185 g_free (info->info.message);
1188 g_slist_free (list);
1191 #define ADD_ERROR(list,msg) \
1193 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1194 vinfo->info.status = MONO_VERIFY_ERROR; \
1195 vinfo->info.message = (msg); \
1196 (list) = g_slist_prepend ((list), vinfo); \
1199 #define ADD_WARN(list,code,msg) \
1201 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1202 vinfo->info.status = (code); \
1203 vinfo->info.message = (msg); \
1204 (list) = g_slist_prepend ((list), vinfo); \
1207 #define ADD_INVALID(list,msg) \
1209 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1210 vinfo->status = MONO_VERIFY_ERROR; \
1211 vinfo->message = (msg); \
1212 (list) = g_slist_prepend ((list), vinfo); \
1213 /*G_BREAKPOINT ();*/ \
1217 #define CHECK_STACK_UNDERFLOW(num) \
1219 if (cur_stack < (num)) \
1220 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1223 #define CHECK_STACK_OVERFLOW() \
1225 if (cur_stack >= max_stack) \
1226 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1231 in_any_block (MonoMethodHeader *header, guint offset)
1234 MonoExceptionClause *clause;
1236 for (i = 0; i < header->num_clauses; ++i) {
1237 clause = &header->clauses [i];
1238 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1240 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1242 if (MONO_OFFSET_IN_FILTER (clause, offset))
1249 * in_any_exception_block:
1251 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1254 in_any_exception_block (MonoMethodHeader *header, guint offset)
1257 MonoExceptionClause *clause;
1259 for (i = 0; i < header->num_clauses; ++i) {
1260 clause = &header->clauses [i];
1261 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1263 if (MONO_OFFSET_IN_FILTER (clause, offset))
1270 * is_valid_branch_instruction:
1272 * Verify if it's valid to perform a branch from @offset to @target.
1273 * This should be used with br and brtrue/false.
1274 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1275 * The major difference from other similiar functions is that branching into a
1276 * finally/fault block is invalid instead of just unverifiable.
1279 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1282 MonoExceptionClause *clause;
1284 for (i = 0; i < header->num_clauses; ++i) {
1285 clause = &header->clauses [i];
1286 /*branching into a finally block is invalid*/
1287 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1288 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1289 MONO_OFFSET_IN_HANDLER (clause, target))
1292 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1294 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1296 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1303 * is_valid_cmp_branch_instruction:
1305 * Verify if it's valid to perform a branch from @offset to @target.
1306 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1307 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1309 * The major differences from other similar functions are that most errors lead to invalid
1310 * code and only branching out of finally, filter or fault clauses is unverifiable.
1313 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1316 MonoExceptionClause *clause;
1318 for (i = 0; i < header->num_clauses; ++i) {
1319 clause = &header->clauses [i];
1320 /*branching out of a handler or finally*/
1321 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1322 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1323 !MONO_OFFSET_IN_HANDLER (clause, target))
1326 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1328 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1330 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1337 * A leave can't escape a finally block
1340 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1343 MonoExceptionClause *clause;
1345 for (i = 0; i < header->num_clauses; ++i) {
1346 clause = &header->clauses [i];
1347 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1349 if (MONO_OFFSET_IN_FILTER (clause, offset))
1356 * A rethrow can't happen outside of a catch handler.
1359 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1362 MonoExceptionClause *clause;
1364 for (i = 0; i < header->num_clauses; ++i) {
1365 clause = &header->clauses [i];
1366 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1373 * An endfinally can't happen outside of a finally/fault handler.
1376 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1379 MonoExceptionClause *clause;
1381 for (i = 0; i < header->num_clauses; ++i) {
1382 clause = &header->clauses [i];
1383 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1391 * An endfilter can only happens inside a filter clause.
1392 * In non-strict mode filter is allowed inside the handler clause too
1394 static MonoExceptionClause *
1395 is_correct_endfilter (VerifyContext *ctx, guint offset)
1398 MonoExceptionClause *clause;
1400 for (i = 0; i < ctx->header->num_clauses; ++i) {
1401 clause = &ctx->header->clauses [i];
1402 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1404 if (MONO_OFFSET_IN_FILTER (clause, offset))
1406 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1414 * Non-strict endfilter can happens inside a try block or any handler block
1417 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1420 MonoExceptionClause *clause;
1422 for (i = 0; i < ctx->header->num_clauses; ++i) {
1423 clause = &ctx->header->clauses [i];
1424 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1431 is_valid_bool_arg (ILStackDesc *arg)
1433 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1437 switch (stack_slot_get_underlying_type (arg)) {
1440 case TYPE_NATIVE_INT:
1444 g_assert (arg->type);
1445 switch (arg->type->type) {
1446 case MONO_TYPE_CLASS:
1447 case MONO_TYPE_STRING:
1448 case MONO_TYPE_OBJECT:
1449 case MONO_TYPE_SZARRAY:
1450 case MONO_TYPE_ARRAY:
1451 case MONO_TYPE_FNPTR:
1454 case MONO_TYPE_GENERICINST:
1455 /*We need to check if the container class
1456 * of the generic type is a valuetype, iow:
1457 * is it a "class Foo<T>" or a "struct Foo<T>"?
1459 return !arg->type->data.generic_class->container_class->valuetype;
1469 /*Type manipulation helper*/
1471 /*Returns the byref version of the supplied MonoType*/
1473 mono_type_get_type_byref (MonoType *type)
1477 return &mono_class_from_mono_type (type)->this_arg;
1481 /*Returns the byval version of the supplied MonoType*/
1483 mono_type_get_type_byval (MonoType *type)
1487 return &mono_class_from_mono_type (type)->byval_arg;
1491 mono_type_from_stack_slot (ILStackDesc *slot)
1493 if (stack_slot_is_managed_pointer (slot))
1494 return mono_type_get_type_byref (slot->type);
1498 /*Stack manipulation code*/
1501 ensure_stack_size (ILCodeDesc *stack, int required)
1506 if (required < stack->max_size)
1509 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1510 new_size = MAX (8, MAX (required, stack->max_size * 2));
1512 g_assert (new_size >= stack->size);
1513 g_assert (new_size >= required);
1515 tmp = g_new0 (ILStackDesc, new_size);
1516 _MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1520 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1521 g_free (stack->stack);
1522 _MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1526 stack->max_size = new_size;
1530 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1532 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1534 state->size = state->max_size = 0;
1535 state->flags |= IL_CODE_FLAG_STACK_INITED;
1539 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1541 ensure_stack_size (to, from->size);
1542 to->size = from->size;
1544 /*stack copy happens at merge points, which have small stacks*/
1546 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1550 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1552 to->stype = from->stype;
1553 to->type = from->type;
1554 to->method = from->method;
1558 check_underflow (VerifyContext *ctx, int size)
1560 if (ctx->eval.size < size) {
1561 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1568 check_overflow (VerifyContext *ctx)
1570 if (ctx->eval.size >= ctx->max_stack) {
1571 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1577 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1579 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1581 if (stack_slot_get_type (value) == TYPE_PTR) {
1582 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1588 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1590 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1592 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1593 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1599 static ILStackDesc *
1600 stack_push (VerifyContext *ctx)
1602 g_assert (ctx->eval.size < ctx->max_stack);
1603 g_assert (ctx->eval.size <= ctx->eval.max_size);
1605 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1607 return & ctx->eval.stack [ctx->eval.size++];
1610 static ILStackDesc *
1611 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1613 ILStackDesc *top = stack_push (ctx);
1619 static ILStackDesc *
1620 stack_pop (VerifyContext *ctx)
1623 g_assert (ctx->eval.size > 0);
1624 ret = ctx->eval.stack + --ctx->eval.size;
1625 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1626 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1630 /* This function allows to safely pop an unititialized this ptr from
1631 * the eval stack without marking the method as unverifiable.
1633 static ILStackDesc *
1634 stack_pop_safe (VerifyContext *ctx)
1636 g_assert (ctx->eval.size > 0);
1637 return ctx->eval.stack + --ctx->eval.size;
1640 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1642 stack_peek (VerifyContext *ctx, int distance)
1644 g_assert (ctx->eval.size - distance > 0);
1645 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1648 static ILStackDesc *
1649 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1651 ILStackDesc *top = stack_push (ctx);
1652 copy_stack_value (top, value);
1656 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1658 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1661 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1666 if (!(type = verifier_load_type (ctx, token, opcode)))
1669 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1670 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1674 if (type->type == MONO_TYPE_VOID) {
1675 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1679 if (type->type == MONO_TYPE_TYPEDBYREF)
1680 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1682 if (!(klass = mono_class_from_mono_type (type)))
1683 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1685 if (mono_class_is_gtd (klass) && type->type != MONO_TYPE_GENERICINST)
1686 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));
1688 check_unverifiable_type (ctx, type);
1693 /*operation result tables */
1695 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1696 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1697 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1698 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1699 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1700 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1701 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1704 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1705 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1706 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1707 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1708 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1709 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1710 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1713 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1714 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1715 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1716 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1717 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1718 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1719 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1722 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1723 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1724 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1725 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1726 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1727 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1728 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1731 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1732 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1733 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1734 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1735 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1736 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1737 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1740 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1741 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1742 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1743 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1744 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1745 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1746 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1749 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1750 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1751 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1752 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1753 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1754 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1755 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1758 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1759 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1760 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1761 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1762 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1763 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1764 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1767 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1768 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1769 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1770 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1771 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1772 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1773 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1776 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1777 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1778 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1779 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1780 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1781 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1782 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1785 #ifdef MONO_VERIFIER_DEBUG
1789 dump_stack_value (ILStackDesc *value)
1791 printf ("[(%x)(%x)", value->type->type, value->stype);
1793 if (stack_slot_is_this_pointer (value))
1796 if (stack_slot_is_boxed_value (value))
1797 printf ("[boxed] ");
1799 if (stack_slot_is_null_literal (value))
1802 if (stack_slot_is_managed_mutability_pointer (value))
1803 printf ("Controled Mutability MP: ");
1805 if (stack_slot_is_managed_pointer (value))
1806 printf ("Managed Pointer to: ");
1808 switch (stack_slot_get_underlying_type (value)) {
1810 printf ("invalid type]");
1818 case TYPE_NATIVE_INT:
1819 printf ("native int]");
1822 printf ("float64]");
1825 printf ("unmanaged pointer]");
1828 switch (value->type->type) {
1829 case MONO_TYPE_CLASS:
1830 case MONO_TYPE_VALUETYPE:
1831 printf ("complex] (%s)", value->type->data.klass->name);
1833 case MONO_TYPE_STRING:
1834 printf ("complex] (string)");
1836 case MONO_TYPE_OBJECT:
1837 printf ("complex] (object)");
1839 case MONO_TYPE_SZARRAY:
1840 printf ("complex] (%s [])", value->type->data.klass->name);
1842 case MONO_TYPE_ARRAY:
1843 printf ("complex] (%s [%d %d %d])",
1844 value->type->data.array->eklass->name,
1845 value->type->data.array->rank,
1846 value->type->data.array->numsizes,
1847 value->type->data.array->numlobounds);
1849 case MONO_TYPE_GENERICINST:
1850 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1853 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1855 case MONO_TYPE_MVAR:
1856 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1859 //should be a boxed value
1860 char * name = mono_type_full_name (value->type);
1861 printf ("complex] %s", name);
1867 printf ("unknown stack %x type]\n", value->stype);
1868 g_assert_not_reached ();
1873 dump_stack_state (ILCodeDesc *state)
1877 printf ("(%d) ", state->size);
1878 for (i = 0; i < state->size; ++i)
1879 dump_stack_value (state->stack + i);
1885 * is_array_type_compatible:
1887 * Returns TRUE if candidate array type can be assigned to target.
1889 * Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1892 is_array_type_compatible (MonoType *target, MonoType *candidate)
1894 MonoArrayType *left = target->data.array;
1895 MonoArrayType *right = candidate->data.array;
1897 g_assert (target->type == MONO_TYPE_ARRAY);
1898 g_assert (candidate->type == MONO_TYPE_ARRAY);
1900 if (left->rank != right->rank)
1903 return verifier_class_is_assignable_from (left->eklass, right->eklass);
1907 get_stack_type (MonoType *type)
1910 int type_kind = type->type;
1912 mask = POINTER_MASK;
1913 /*TODO handle CMMP_MASK */
1916 switch (type_kind) {
1919 case MONO_TYPE_BOOLEAN:
1922 case MONO_TYPE_CHAR:
1925 return TYPE_I4 | mask;
1929 return TYPE_NATIVE_INT | mask;
1931 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1932 case MONO_TYPE_FNPTR:
1934 case MONO_TYPE_TYPEDBYREF:
1935 return TYPE_PTR | mask;
1938 case MONO_TYPE_MVAR:
1940 case MONO_TYPE_CLASS:
1941 case MONO_TYPE_STRING:
1942 case MONO_TYPE_OBJECT:
1943 case MONO_TYPE_SZARRAY:
1944 case MONO_TYPE_ARRAY:
1945 return TYPE_COMPLEX | mask;
1949 return TYPE_I8 | mask;
1953 return TYPE_R8 | mask;
1955 case MONO_TYPE_GENERICINST:
1956 case MONO_TYPE_VALUETYPE:
1957 if (mono_type_is_enum_type (type)) {
1958 type = mono_type_get_underlying_type_any (type);
1961 type_kind = type->type;
1964 return TYPE_COMPLEX | mask;
1972 /* convert MonoType to ILStackDesc format (stype) */
1974 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1977 int type_kind = type->type;
1979 if (type->byref || take_addr)
1980 mask = POINTER_MASK;
1981 /* TODO handle CMMP_MASK */
1986 switch (type_kind) {
1989 case MONO_TYPE_BOOLEAN:
1992 case MONO_TYPE_CHAR:
1995 stack->stype = TYPE_I4 | mask;
1999 stack->stype = TYPE_NATIVE_INT | mask;
2002 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2003 case MONO_TYPE_FNPTR:
2005 case MONO_TYPE_TYPEDBYREF:
2006 stack->stype = TYPE_PTR | mask;
2009 case MONO_TYPE_CLASS:
2010 case MONO_TYPE_STRING:
2011 case MONO_TYPE_OBJECT:
2012 case MONO_TYPE_SZARRAY:
2013 case MONO_TYPE_ARRAY:
2016 case MONO_TYPE_MVAR:
2017 stack->stype = TYPE_COMPLEX | mask;
2022 stack->stype = TYPE_I8 | mask;
2026 stack->stype = TYPE_R8 | mask;
2028 case MONO_TYPE_GENERICINST:
2029 case MONO_TYPE_VALUETYPE:
2030 if (mono_type_is_enum_type (type)) {
2031 MonoType *utype = mono_type_get_underlying_type_any (type);
2033 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
2037 type_kind = type->type;
2040 stack->stype = TYPE_COMPLEX | mask;
2044 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
2045 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
2052 * init_stack_with_value_at_exception_boundary:
2054 * Initialize the stack and push a given type.
2055 * The instruction is marked as been on the exception boundary.
2058 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
2061 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
2063 if (!mono_error_ok (&error)) {
2064 char *name = mono_type_get_full_name (klass);
2065 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
2067 mono_error_cleanup (&error);
2071 if (!ctx->max_stack) {
2072 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
2076 stack_init (ctx, code);
2077 ensure_stack_size (code, 1);
2078 set_stack_value (ctx, code->stack, type, FALSE);
2079 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
2081 code->flags |= IL_CODE_FLAG_WAS_TARGET;
2082 if (mono_type_is_generic_argument (type))
2083 code->stack->stype |= BOXED_MASK;
2085 /* Class lazy loading functions */
2086 static GENERATE_GET_CLASS_WITH_CACHE (ienumerable, "System.Collections.Generic", "IEnumerable`1")
2087 static GENERATE_GET_CLASS_WITH_CACHE (icollection, "System.Collections.Generic", "ICollection`1")
2088 static GENERATE_GET_CLASS_WITH_CACHE (ireadonly_list, "System.Collections.Generic", "IReadOnlyList`1")
2089 static GENERATE_GET_CLASS_WITH_CACHE (ireadonly_collection, "System.Collections.Generic", "IReadOnlyCollection`1")
2093 get_ienumerable_class (void)
2095 return mono_class_get_ienumerable_class ();
2099 get_icollection_class (void)
2101 return mono_class_get_icollection_class ();
2105 get_ireadonlylist_class (void)
2107 return mono_class_get_ireadonly_list_class ();
2111 get_ireadonlycollection_class (void)
2113 return mono_class_get_ireadonly_collection_class ();
2117 inflate_class_one_arg (MonoClass *gtype, MonoClass *arg0)
2120 args [0] = &arg0->byval_arg;
2122 return mono_class_bind_generic_parameters (gtype, 1, args, FALSE);
2126 verifier_inflate_and_check_compat (MonoClass *target, MonoClass *gtd, MonoClass *arg)
2129 if (!(tmp = inflate_class_one_arg (gtd, arg)))
2131 if (mono_class_is_variant_compatible (target, tmp, TRUE))
2137 verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate)
2139 MonoClass *iface_gtd;
2141 if (target == candidate)
2144 if (mono_class_has_variant_generic_params (target)) {
2145 if (MONO_CLASS_IS_INTERFACE (target)) {
2146 if (MONO_CLASS_IS_INTERFACE (candidate) && mono_class_is_variant_compatible (target, candidate, TRUE))
2149 if (candidate->rank == 1) {
2150 if (verifier_inflate_and_check_compat (target, mono_defaults.generic_ilist_class, candidate->element_class))
2152 if (verifier_inflate_and_check_compat (target, get_icollection_class (), candidate->element_class))
2154 if (verifier_inflate_and_check_compat (target, get_ienumerable_class (), candidate->element_class))
2156 if (verifier_inflate_and_check_compat (target, get_ireadonlylist_class (), candidate->element_class))
2158 if (verifier_inflate_and_check_compat (target, get_ireadonlycollection_class (), candidate->element_class))
2163 while (candidate && candidate != mono_defaults.object_class) {
2164 mono_class_setup_interfaces (candidate, &error);
2165 if (!mono_error_ok (&error)) {
2166 mono_error_cleanup (&error);
2170 /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
2171 for (i = 0; i < candidate->interface_offsets_count; ++i) {
2172 MonoClass *iface = candidate->interfaces_packed [i];
2173 if (mono_class_is_variant_compatible (target, iface, TRUE))
2177 for (i = 0; i < candidate->interface_count; ++i) {
2178 MonoClass *iface = candidate->interfaces [i];
2179 if (mono_class_is_variant_compatible (target, iface, TRUE))
2182 candidate = candidate->parent;
2185 } else if (target->delegate) {
2186 if (mono_class_is_variant_compatible (target, candidate, TRUE))
2192 if (mono_class_is_assignable_from (target, candidate))
2195 if (!MONO_CLASS_IS_INTERFACE (target) || !mono_class_is_ginst (target) || candidate->rank != 1)
2198 iface_gtd = mono_class_get_generic_class (target)->container_class;
2199 if (iface_gtd != mono_defaults.generic_ilist_class && iface_gtd != get_icollection_class () && iface_gtd != get_ienumerable_class ())
2202 target = mono_class_from_mono_type (mono_class_get_generic_class (target)->context.class_inst->type_argv [0]);
2203 candidate = candidate->element_class;
2208 /*Verify if type 'candidate' can be stored in type 'target'.
2210 * If strict, check for the underlying type and not the verification stack types
2213 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
2215 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2216 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2218 MonoType *original_candidate = candidate;
2219 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
2221 /*only one is byref */
2222 if (candidate->byref ^ target->byref) {
2223 /* converting from native int to byref*/
2224 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2225 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2230 strict |= target->byref;
2231 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2232 candidate = mono_type_get_underlying_type_any (candidate);
2235 switch (target->type) {
2236 case MONO_TYPE_VOID:
2237 return candidate->type == MONO_TYPE_VOID;
2240 case MONO_TYPE_BOOLEAN:
2242 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2245 case MONO_TYPE_CHAR:
2247 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2249 case MONO_TYPE_U4: {
2250 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2251 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2253 return is_native_int || is_int4;
2254 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2259 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2264 return candidate->type == target->type;
2265 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2269 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2270 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2272 return is_native_int || is_int4;
2273 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2277 if (candidate->type != MONO_TYPE_PTR)
2279 /* check the underlying type */
2280 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2282 case MONO_TYPE_FNPTR: {
2283 MonoMethodSignature *left, *right;
2284 if (candidate->type != MONO_TYPE_FNPTR)
2287 left = mono_type_get_signature (target);
2288 right = mono_type_get_signature (candidate);
2289 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2292 case MONO_TYPE_GENERICINST: {
2293 MonoClass *target_klass;
2294 MonoClass *candidate_klass;
2295 if (mono_type_is_enum_type (target)) {
2296 target = mono_type_get_underlying_type_any (target);
2302 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2303 * to take boxing status into account.
2305 if (mono_type_is_generic_argument (original_candidate))
2308 target_klass = mono_class_from_mono_type (target);
2309 candidate_klass = mono_class_from_mono_type (candidate);
2310 if (mono_class_is_nullable (target_klass)) {
2311 if (!mono_class_is_nullable (candidate_klass))
2313 return target_klass == candidate_klass;
2315 return verifier_class_is_assignable_from (target_klass, candidate_klass);
2318 case MONO_TYPE_STRING:
2319 return candidate->type == MONO_TYPE_STRING;
2321 case MONO_TYPE_CLASS:
2323 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2324 * to take boxing status into account.
2326 if (mono_type_is_generic_argument (original_candidate))
2329 if (candidate->type == MONO_TYPE_VALUETYPE)
2332 /* If candidate is an enum it should return true for System.Enum and supertypes.
2333 * That's why here we use the original type and not the underlying type.
2335 return verifier_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2337 case MONO_TYPE_OBJECT:
2338 return MONO_TYPE_IS_REFERENCE (candidate);
2340 case MONO_TYPE_SZARRAY: {
2343 if (candidate->type != MONO_TYPE_SZARRAY)
2346 left = mono_class_from_mono_type (target);
2347 right = mono_class_from_mono_type (candidate);
2349 return verifier_class_is_assignable_from (left, right);
2352 case MONO_TYPE_ARRAY:
2353 if (candidate->type != MONO_TYPE_ARRAY)
2355 return is_array_type_compatible (target, candidate);
2357 case MONO_TYPE_TYPEDBYREF:
2358 return candidate->type == MONO_TYPE_TYPEDBYREF;
2360 case MONO_TYPE_VALUETYPE: {
2361 MonoClass *target_klass;
2362 MonoClass *candidate_klass;
2364 if (candidate->type == MONO_TYPE_CLASS)
2367 target_klass = mono_class_from_mono_type (target);
2368 candidate_klass = mono_class_from_mono_type (candidate);
2369 if (target_klass == candidate_klass)
2371 if (mono_type_is_enum_type (target)) {
2372 target = mono_type_get_underlying_type_any (target);
2381 if (candidate->type != MONO_TYPE_VAR)
2383 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2385 case MONO_TYPE_MVAR:
2386 if (candidate->type != MONO_TYPE_MVAR)
2388 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2391 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2392 g_assert_not_reached ();
2401 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2403 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2407 * Returns the generic param bound to the context been verified.
2410 static MonoGenericParam*
2411 get_generic_param (VerifyContext *ctx, MonoType *param)
2413 guint16 param_num = mono_type_get_generic_param_num (param);
2414 if (param->type == MONO_TYPE_VAR) {
2415 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2416 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2419 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2422 /*param must be a MVAR */
2423 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2424 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2427 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2432 recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
2434 MonoType *constraint_type = &constraint_class->byval_arg;
2435 if (recursion_level <= 0)
2438 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
2441 if (mono_type_is_generic_argument (constraint_type)) {
2442 MonoGenericParam *param = get_generic_param (ctx, constraint_type);
2446 for (klass = mono_generic_param_info (param)->constraints; klass && *klass; ++klass) {
2447 if (recursive_boxed_constraint_type_check (ctx, type, *klass, recursion_level - 1))
2455 * is_compatible_boxed_valuetype:
2457 * Returns: TRUE if @candidate / @stack is a valid boxed valuetype.
2459 * @type The source type. It it tested to be of the proper type.
2460 * @candidate type of the boxed valuetype.
2461 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2462 * @strict if TRUE candidate must be boxed compatible to the target type
2466 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2468 if (!stack_slot_is_boxed_value (stack))
2470 if (type->byref || candidate->byref)
2473 if (mono_type_is_generic_argument (candidate)) {
2474 MonoGenericParam *param = get_generic_param (ctx, candidate);
2479 for (klass = mono_generic_param_info (param)->constraints; klass && *klass; ++klass) {
2480 /*256 should be enough since there can't be more than 255 generic arguments.*/
2481 if (recursive_boxed_constraint_type_check (ctx, type, *klass, 256))
2486 if (mono_type_is_generic_argument (type))
2492 return MONO_TYPE_IS_REFERENCE (type) && verifier_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2496 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2498 MonoType *candidate = mono_type_from_stack_slot (stack);
2499 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2502 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2505 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2508 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2512 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2514 /* Handle how Roslyn emit fixed statements by encoding it as byref */
2515 if (type->byref && candidate->byref && (type->type == MONO_TYPE_I) && !mono_type_is_reference (candidate)) {
2516 if (!IS_STRICT_MODE (ctx))
2520 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2524 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2526 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2530 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2532 if (candidate->byref ^ target->byref)
2535 switch (target->type) {
2536 case MONO_TYPE_VOID:
2539 case MONO_TYPE_BOOLEAN:
2542 case MONO_TYPE_CHAR:
2551 case MONO_TYPE_STRING:
2552 case MONO_TYPE_TYPEDBYREF:
2553 return candidate->type == target->type;
2556 if (candidate->type != MONO_TYPE_PTR)
2558 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2560 case MONO_TYPE_FNPTR:
2561 if (candidate->type != MONO_TYPE_FNPTR)
2563 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2565 case MONO_TYPE_GENERICINST: {
2566 MonoClass *target_klass;
2567 MonoClass *candidate_klass;
2568 target_klass = mono_class_from_mono_type (target);
2569 candidate_klass = mono_class_from_mono_type (candidate);
2570 /*FIXME handle nullables and enum*/
2571 return verifier_class_is_assignable_from (target_klass, candidate_klass);
2573 case MONO_TYPE_OBJECT:
2574 return MONO_TYPE_IS_REFERENCE (candidate);
2576 case MONO_TYPE_CLASS:
2577 return verifier_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2579 case MONO_TYPE_SZARRAY:
2580 if (candidate->type != MONO_TYPE_SZARRAY)
2582 return verifier_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2584 case MONO_TYPE_ARRAY:
2585 if (candidate->type != MONO_TYPE_ARRAY)
2587 return is_array_type_compatible (target, candidate);
2589 case MONO_TYPE_VALUETYPE:
2590 /*FIXME handle nullables and enum*/
2591 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2594 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2597 case MONO_TYPE_MVAR:
2598 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2602 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2603 g_assert_not_reached ();
2609 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2611 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2614 return mono_delegate_type_equal (method, delegate);
2618 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2620 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2623 return mono_delegate_type_equal (delegate, method);
2627 * mono_delegate_signature_equal:
2629 * Compare two signatures in the way expected by delegates.
2631 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2633 * FIXME can this function be eliminated and proper metadata functionality be used?
2636 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2639 int method_offset = is_static_ldftn ? 1 : 0;
2641 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2644 if (delegate_sig->call_convention != method_sig->call_convention)
2647 for (i = 0; i < delegate_sig->param_count; i++) {
2648 MonoType *p1 = delegate_sig->params [i];
2649 MonoType *p2 = method_sig->params [i + method_offset];
2651 if (!mono_delegate_param_equal (p1, p2))
2655 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2662 mono_verifier_is_signature_compatible (MonoMethodSignature *target, MonoMethodSignature *candidate)
2664 return mono_delegate_signature_equal (target, candidate, FALSE);
2668 * verify_ldftn_delegate:
2670 * Verify properties of ldftn based delegates.
2673 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2675 MonoMethod *method = funptr->method;
2677 /*ldftn non-final virtuals only allowed if method is not static,
2678 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2679 * This rules doesn't apply if the object on stack is a boxed valuetype.
2681 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !mono_class_is_sealed (method->klass) && !stack_slot_is_boxed_value (value)) {
2682 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2683 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2684 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2686 /*current method must not be static*/
2687 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2688 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2690 /*value is the this pointer, loaded using ldarg.0 */
2691 if (!stack_slot_is_this_pointer (value))
2692 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));
2694 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2699 * verify_delegate_compatibility:
2701 * Verify delegate creation sequence.
2705 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2707 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2708 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2710 MonoMethod *invoke, *method;
2711 const guint8 *ip = ctx->header->code;
2712 guint32 ip_offset = ctx->ip_offset;
2713 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2715 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2716 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2720 invoke = mono_get_delegate_invoke (delegate);
2721 method = funptr->method;
2723 if (!method || !mono_method_signature (method)) {
2724 char *name = mono_type_get_full_name (delegate);
2725 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2730 if (!invoke || !mono_method_signature (invoke)) {
2731 char *name = mono_type_get_full_name (delegate);
2732 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2737 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2739 if (is_static_ldftn)
2740 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2742 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2743 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2744 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2745 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));
2747 g_free (invoke_sig);
2751 * Delegate code sequences:
2757 * [-6] ldvirtftn token
2761 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2762 verify_ldftn_delegate (ctx, delegate, value, funptr);
2763 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2764 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2766 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2768 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2771 if (is_first_arg_bound) {
2772 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2773 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2775 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2776 if (!stack_slot_is_null_literal (value))
2777 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2779 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2780 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2784 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2785 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2787 #undef IS_VALID_OPCODE
2788 #undef IS_LOAD_FUN_PTR
2791 /* implement the opcode checks*/
2793 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2797 if (arg >= ctx->max_args) {
2799 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2801 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2802 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2803 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2805 } else if (check_overflow (ctx)) {
2806 /*We must let the value be pushed, otherwise we would get an underflow error*/
2807 check_unverifiable_type (ctx, ctx->params [arg]);
2808 if (ctx->params [arg]->byref && take_addr)
2809 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2810 top = stack_push (ctx);
2811 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2814 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2816 ctx->has_this_store = TRUE;
2818 top->stype |= THIS_POINTER_MASK;
2819 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2820 top->stype |= UNINIT_THIS_MASK;
2826 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2828 if (arg >= ctx->num_locals) {
2829 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2830 } else if (check_overflow (ctx)) {
2831 /*We must let the value be pushed, otherwise we would get an underflow error*/
2832 check_unverifiable_type (ctx, ctx->locals [arg]);
2833 if (ctx->locals [arg]->byref && take_addr)
2834 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2836 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2841 store_arg (VerifyContext *ctx, guint32 arg)
2845 if (arg >= ctx->max_args) {
2846 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2847 if (check_underflow (ctx, 1))
2852 if (check_underflow (ctx, 1)) {
2853 value = stack_pop (ctx);
2854 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2855 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2858 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2859 ctx->has_this_store = 1;
2863 store_local (VerifyContext *ctx, guint32 arg)
2866 if (arg >= ctx->num_locals) {
2867 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2871 /*TODO verify definite assigment */
2872 if (!check_underflow (ctx, 1))
2875 value = stack_pop (ctx);
2876 if (ctx->locals [arg]->byref && stack_slot_is_managed_mutability_pointer (value))
2877 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));
2879 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2880 char *expected = mono_type_full_name (ctx->locals [arg]);
2881 char *found = stack_slot_full_name (value);
2882 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2892 /*FIXME add and sub needs special care here*/
2894 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2896 ILStackDesc *a, *b, *top;
2897 int idxa, idxb, complexMerge = 0;
2900 if (!check_underflow (ctx, 2))
2902 b = stack_pop (ctx);
2903 a = stack_pop (ctx);
2905 idxa = stack_slot_get_underlying_type (a);
2906 if (stack_slot_is_managed_pointer (a)) {
2911 idxb = stack_slot_get_underlying_type (b);
2912 if (stack_slot_is_managed_pointer (b)) {
2919 res = table [idxa][idxb];
2921 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2922 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2924 top = stack_push (ctx);
2925 if (res == TYPE_INV) {
2926 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)));
2927 copy_stack_value (top, a);
2931 if (res & NON_VERIFIABLE_RESULT) {
2932 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)));
2934 res = res & ~NON_VERIFIABLE_RESULT;
2937 if (complexMerge && res == TYPE_PTR) {
2938 if (complexMerge == 1)
2939 copy_stack_value (top, a);
2940 else if (complexMerge == 2)
2941 copy_stack_value (top, b);
2943 * There is no need to merge the type of two pointers.
2944 * The only valid operation is subtraction, that returns a native
2945 * int as result and can be used with any 2 pointer kinds.
2946 * This is valid acording to Patition III 1.1.4
2955 do_boolean_branch_op (VerifyContext *ctx, int delta)
2957 int target = ctx->ip_offset + delta;
2960 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2962 if (target < 0 || target >= ctx->code_size) {
2963 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2967 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2969 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2972 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2976 ctx->target = target;
2978 if (!check_underflow (ctx, 1))
2981 top = stack_pop (ctx);
2982 if (!is_valid_bool_arg (top))
2983 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));
2985 check_unmanaged_pointer (ctx, top);
2989 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2991 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2995 stack_slot_is_reference_value (ILStackDesc *slot)
2997 return stack_slot_get_type (slot) == TYPE_COMPLEX && (MONO_TYPE_IS_REFERENCE (slot->type) || stack_slot_is_boxed_value (slot));
3001 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
3006 int target = ctx->ip_offset + delta;
3008 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3010 if (target < 0 || target >= ctx->code_size) {
3011 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3015 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3016 case 1: /*FIXME use constants and not magic numbers.*/
3017 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3020 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3024 ctx->target = target;
3026 if (!check_underflow (ctx, 2))
3029 b = stack_pop (ctx);
3030 a = stack_pop (ctx);
3032 idxa = stack_slot_get_underlying_type (a);
3033 if (stack_slot_is_managed_pointer (a))
3036 idxb = stack_slot_get_underlying_type (b);
3037 if (stack_slot_is_managed_pointer (b))
3040 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3045 res = table [idxa][idxb];
3048 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
3049 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3051 if (res == TYPE_INV) {
3052 CODE_NOT_VERIFIABLE (ctx,
3053 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));
3054 } else if (res & NON_VERIFIABLE_RESULT) {
3055 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));
3056 res = res & ~NON_VERIFIABLE_RESULT;
3061 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
3067 if (!check_underflow (ctx, 2))
3069 b = stack_pop (ctx);
3070 a = stack_pop (ctx);
3072 if (opcode == CEE_CGT_UN) {
3073 if ((stack_slot_is_reference_value (a) && stack_slot_is_null_literal (b)) ||
3074 (stack_slot_is_reference_value (b) && stack_slot_is_null_literal (a))) {
3075 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3080 idxa = stack_slot_get_underlying_type (a);
3081 if (stack_slot_is_managed_pointer (a))
3084 idxb = stack_slot_get_underlying_type (b);
3085 if (stack_slot_is_managed_pointer (b))
3088 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3093 res = table [idxa][idxb];
3096 if(res == TYPE_INV) {
3097 char *left_type = stack_slot_full_name (a);
3098 char *right_type = stack_slot_full_name (b);
3099 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", left_type, right_type, ctx->ip_offset));
3101 g_free (right_type);
3102 } else if (res & NON_VERIFIABLE_RESULT) {
3103 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));
3104 res = res & ~NON_VERIFIABLE_RESULT;
3106 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3110 do_ret (VerifyContext *ctx)
3112 MonoType *ret = ctx->signature->ret;
3113 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3114 if (ret->type != MONO_TYPE_VOID) {
3116 if (!check_underflow (ctx, 1))
3119 top = stack_pop(ctx);
3121 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3122 char *ret_type = mono_type_full_name (ctx->signature->ret);
3123 char *stack_type = stack_slot_full_name (top);
3124 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));
3125 g_free (stack_type);
3130 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3131 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3134 if (ctx->eval.size > 0) {
3135 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3137 if (in_any_block (ctx->header, ctx->ip_offset))
3138 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3142 * 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.
3143 * This is illegal but mono_get_method_full decoded it.
3144 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3147 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual_)
3151 MonoMethodSignature *sig;
3154 gboolean virt_check_this = FALSE;
3155 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3157 if (!(method = verifier_load_method (ctx, method_token, virtual_ ? "callvirt" : "call")))
3161 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3163 if (method->klass->valuetype) // && !constrained ???
3164 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3166 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3167 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3170 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3171 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3173 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !mono_class_is_sealed (method->klass)) {
3174 virt_check_this = TRUE;
3175 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3179 if (!(sig = mono_method_get_signature_checked (method, ctx->image, method_token, ctx->generic_context, &error))) {
3180 mono_error_cleanup (&error);
3181 sig = mono_method_get_signature_checked (method, ctx->image, method_token, NULL, &error);
3185 char *name = mono_type_get_full_name (method->klass);
3186 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x due to: %s", name, method->name, ctx->ip_offset, mono_error_get_message (&error)));
3187 mono_error_cleanup (&error);
3192 param_count = sig->param_count + sig->hasthis;
3193 if (!check_underflow (ctx, param_count))
3196 for (i = sig->param_count - 1; i >= 0; --i) {
3197 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3198 value = stack_pop (ctx);
3199 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
3200 char *stack_name = stack_slot_full_name (value);
3201 char *sig_name = mono_type_full_name (sig->params [i]);
3202 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));
3203 g_free (stack_name);
3207 if (stack_slot_is_managed_mutability_pointer (value))
3208 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));
3210 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3211 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));
3217 MonoType *type = &method->klass->byval_arg;
3220 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3221 if (IS_STRICT_MODE (ctx) && !mono_method_is_constructor (ctx->method))
3222 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3223 if (IS_STRICT_MODE (ctx) && method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3224 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));
3226 ctx->super_ctor_called = TRUE;
3227 value = stack_pop_safe (ctx);
3228 if (IS_STRICT_MODE (ctx) && (value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3229 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3230 if (!(value->stype & UNINIT_THIS_MASK))
3231 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Calling the base constructor on an initialized this pointer at 0x%04x", ctx->ip_offset));
3233 value = stack_pop (ctx);
3236 copy_stack_value (©, value);
3237 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3238 //Other parts of the code suffer from the same issue of
3239 copy.type = mono_type_get_type_byval (copy.type);
3240 copy.stype &= ~POINTER_MASK;
3242 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3243 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));
3245 if (constrained && virtual_) {
3246 if (!stack_slot_is_managed_pointer (value))
3247 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3248 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), mono_type_get_underlying_type (ctx->constrained_type), TRUE))
3249 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3250 copy.stype |= BOXED_MASK;
3251 copy.type = ctx->constrained_type;
3253 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3254 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));
3256 if (!virtual_ && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3257 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3259 if (virtual_ && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3260 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3262 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3263 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));
3265 if (!verify_stack_type_compatibility (ctx, type, ©)) {
3266 char *expected = mono_type_full_name (type);
3267 char *effective = stack_slot_full_name (©);
3268 char *method_name = mono_method_full_name (method, TRUE);
3269 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",
3270 expected, effective, method_name, ctx->ip_offset));
3271 g_free (method_name);
3276 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
3277 char *name = mono_method_full_name (method, TRUE);
3278 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3282 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3283 char *name = mono_method_full_name (method, TRUE);
3284 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3288 if (sig->ret->type != MONO_TYPE_VOID) {
3289 if (!mono_type_is_valid_in_context (ctx, sig->ret))
3292 if (check_overflow (ctx)) {
3293 value = stack_push (ctx);
3294 set_stack_value (ctx, value, sig->ret, FALSE);
3295 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3296 ctx->prefix_set &= ~PREFIX_READONLY;
3297 value->stype |= CMMP_MASK;
3302 if ((ctx->prefix_set & PREFIX_TAIL)) {
3303 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3304 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3305 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3306 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3312 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3314 MonoClassField *field;
3316 if (!check_overflow (ctx))
3319 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3321 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3324 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3325 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3328 /*taking the address of initonly field only works from the static constructor */
3329 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3330 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3331 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3333 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3334 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3336 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3340 do_store_static_field (VerifyContext *ctx, int token) {
3341 MonoClassField *field;
3344 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3346 if (!check_underflow (ctx, 1))
3349 value = stack_pop (ctx);
3351 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3354 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3355 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3359 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3360 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3364 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3365 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3367 if (!verify_stack_type_compatibility (ctx, field->type, value)) {
3368 char *stack_name = stack_slot_full_name (value);
3369 char *field_name = mono_type_full_name (field->type);
3370 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3371 field_name, stack_name, ctx->ip_offset));
3372 g_free (field_name);
3373 g_free (stack_name);
3378 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3380 MonoClassField *field;
3382 gboolean is_pointer;
3384 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3385 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3389 //the value on stack is going to be used as a pointer
3390 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3392 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3393 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3396 g_assert (obj->type);
3398 /*The value on the stack must be a subclass of the defining type of the field*/
3399 /* we need to check if we can load the field from the stack value*/
3401 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3402 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3404 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3405 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3407 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3408 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));
3410 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3411 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3412 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));
3414 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3415 char *found = stack_slot_full_name (obj);
3416 char *expected = mono_type_full_name (&field->parent->byval_arg);
3417 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));
3422 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3423 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3426 check_unmanaged_pointer (ctx, obj);
3431 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3434 MonoClassField *field;
3437 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3439 if (!check_underflow (ctx, 1))
3441 obj = stack_pop_safe (ctx);
3443 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3446 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3447 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3449 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3450 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3451 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3453 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3457 do_store_field (VerifyContext *ctx, int token)
3459 ILStackDesc *value, *obj;
3460 MonoClassField *field;
3461 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3463 if (!check_underflow (ctx, 2))
3466 value = stack_pop (ctx);
3467 obj = stack_pop_safe (ctx);
3469 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3472 if (!verify_stack_type_compatibility (ctx, field->type, value))
3473 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3476 /*TODO proper handle for Nullable<T>*/
3478 do_box_value (VerifyContext *ctx, int klass_token)
3481 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3487 if (!check_underflow (ctx, 1))
3490 value = stack_pop (ctx);
3491 /*box is a nop for reference types*/
3493 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3494 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3499 if (!verify_stack_type_compatibility (ctx, type, value))
3500 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3502 klass = mono_class_from_mono_type (type);
3503 if (mono_class_is_nullable (klass))
3504 type = &mono_class_get_nullable_param (klass)->byval_arg;
3505 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3509 do_unbox_value (VerifyContext *ctx, int klass_token)
3512 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3517 if (!check_underflow (ctx, 1))
3520 if (!mono_class_from_mono_type (type)->valuetype)
3521 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3523 value = stack_pop (ctx);
3525 /*Value should be: a boxed valuetype or a reference type*/
3526 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3527 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3528 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));
3530 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3531 value->stype |= CMMP_MASK;
3535 do_unbox_any (VerifyContext *ctx, int klass_token)
3538 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3543 if (!check_underflow (ctx, 1))
3546 value = stack_pop (ctx);
3548 /*Value should be: a boxed valuetype or a reference type*/
3549 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3550 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3551 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));
3553 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3557 do_unary_math_op (VerifyContext *ctx, int op)
3560 if (!check_underflow (ctx, 1))
3562 value = stack_pop (ctx);
3563 switch (stack_slot_get_type (value)) {
3566 case TYPE_NATIVE_INT:
3571 case TYPE_COMPLEX: /*only enums are ok*/
3572 if (mono_type_is_enum_type (value->type))
3575 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3577 stack_push_stack_val (ctx, value);
3581 do_conversion (VerifyContext *ctx, int kind)
3584 if (!check_underflow (ctx, 1))
3586 value = stack_pop (ctx);
3588 switch (stack_slot_get_type (value)) {
3591 case TYPE_NATIVE_INT:
3595 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));
3600 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3603 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3606 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3608 case TYPE_NATIVE_INT:
3609 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3612 g_error ("unknown type %02x in conversion", kind);
3618 do_load_token (VerifyContext *ctx, int token)
3622 MonoClass *handle_class;
3623 if (!check_overflow (ctx))
3626 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
3627 handle = mono_method_get_wrapper_data (ctx->method, token);
3628 handle_class = (MonoClass *)mono_method_get_wrapper_data (ctx->method, token + 1);
3629 if (handle_class == mono_defaults.typehandle_class)
3630 handle = &((MonoClass*)handle)->byval_arg;
3632 switch (token & 0xff000000) {
3633 case MONO_TOKEN_TYPE_DEF:
3634 case MONO_TOKEN_TYPE_REF:
3635 case MONO_TOKEN_TYPE_SPEC:
3636 case MONO_TOKEN_FIELD_DEF:
3637 case MONO_TOKEN_METHOD_DEF:
3638 case MONO_TOKEN_METHOD_SPEC:
3639 case MONO_TOKEN_MEMBER_REF:
3640 if (!token_bounds_check (ctx->image, token)) {
3641 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));
3646 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));
3650 handle = mono_ldtoken_checked (ctx->image, token, &handle_class, ctx->generic_context, &error);
3654 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x due to %s", token, ctx->ip_offset, mono_error_get_message (&error)));
3655 mono_error_cleanup (&error);
3658 if (handle_class == mono_defaults.typehandle_class) {
3659 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3660 } else if (handle_class == mono_defaults.methodhandle_class) {
3661 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3662 } else if (handle_class == mono_defaults.fieldhandle_class) {
3663 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3665 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3667 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3671 do_ldobj_value (VerifyContext *ctx, int token)
3674 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3675 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3680 if (!check_underflow (ctx, 1))
3683 value = stack_pop (ctx);
3684 if (!stack_slot_is_managed_pointer (value)
3685 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3686 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3687 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3691 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3692 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3694 /*We have a byval on the stack, but the comparison must be strict. */
3695 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3696 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3698 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3702 do_stobj (VerifyContext *ctx, int token)
3704 ILStackDesc *dest, *src;
3705 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3706 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3711 if (!check_underflow (ctx, 2))
3714 src = stack_pop (ctx);
3715 dest = stack_pop (ctx);
3717 if (stack_slot_is_managed_mutability_pointer (dest))
3718 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3720 if (!stack_slot_is_managed_pointer (dest))
3721 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3723 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3724 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));
3726 if (!verify_stack_type_compatibility (ctx, type, src)) {
3727 char *type_name = mono_type_full_name (type);
3728 char *src_name = stack_slot_full_name (src);
3729 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));
3734 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3735 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3739 do_cpobj (VerifyContext *ctx, int token)
3741 ILStackDesc *dest, *src;
3742 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3746 if (!check_underflow (ctx, 2))
3749 src = stack_pop (ctx);
3750 dest = stack_pop (ctx);
3752 if (!stack_slot_is_managed_pointer (src))
3753 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3755 if (!stack_slot_is_managed_pointer (dest))
3756 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3758 if (stack_slot_is_managed_mutability_pointer (dest))
3759 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3761 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3762 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3764 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3765 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3769 do_initobj (VerifyContext *ctx, int token)
3772 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3776 if (!check_underflow (ctx, 1))
3779 obj = stack_pop (ctx);
3781 if (!stack_slot_is_managed_pointer (obj))
3782 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3784 if (stack_slot_is_managed_mutability_pointer (obj))
3785 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3787 stack = mono_type_get_type_byval (obj->type);
3788 if (MONO_TYPE_IS_REFERENCE (stack)) {
3789 if (!verify_type_compatibility (ctx, stack, type))
3790 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3791 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3792 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3793 } else if (!verify_type_compatibility (ctx, stack, type)) {
3794 char *expected_name = mono_type_full_name (type);
3795 char *stack_name = mono_type_full_name (stack);
3797 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));
3798 g_free (expected_name);
3799 g_free (stack_name);
3804 do_newobj (VerifyContext *ctx, int token)
3808 MonoMethodSignature *sig;
3810 gboolean is_delegate = FALSE;
3812 if (!(method = verifier_load_method (ctx, token, "newobj")))
3815 if (!mono_method_is_constructor (method)) {
3816 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3820 if (mono_class_get_flags (method->klass) & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3821 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3823 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3824 char *from = mono_method_full_name (ctx->method, TRUE);
3825 char *to = mono_method_full_name (method, TRUE);
3826 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);
3831 //FIXME use mono_method_get_signature_full
3832 sig = mono_method_signature (method);
3834 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3838 if (!sig->hasthis) {
3839 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature missing hasthis at 0x%04x", ctx->ip_offset));
3843 if (!check_underflow (ctx, sig->param_count))
3846 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3849 ILStackDesc *funptr;
3850 //first arg is object, second arg is fun ptr
3851 if (sig->param_count != 2) {
3852 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3855 funptr = stack_pop (ctx);
3856 value = stack_pop (ctx);
3857 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3859 for (i = sig->param_count - 1; i >= 0; --i) {
3860 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3861 value = stack_pop (ctx);
3862 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3863 char *stack_name = stack_slot_full_name (value);
3864 char *sig_name = mono_type_full_name (sig->params [i]);
3865 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));
3866 g_free (stack_name);
3870 if (stack_slot_is_managed_mutability_pointer (value))
3871 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3875 if (check_overflow (ctx))
3876 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3880 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3886 if (!check_underflow (ctx, 1))
3889 if (!(type = get_boxable_mono_type (ctx, token, opcode)))
3893 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3897 value = stack_pop (ctx);
3898 is_boxed = stack_slot_is_boxed_value (value);
3900 if (stack_slot_is_managed_pointer (value))
3901 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3902 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3903 char *name = stack_slot_full_name (value);
3904 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));
3908 switch (value->type->type) {
3909 case MONO_TYPE_FNPTR:
3911 case MONO_TYPE_TYPEDBYREF:
3912 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3917 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3918 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3922 mono_type_from_opcode (int opcode) {
3930 return &mono_defaults.sbyte_class->byval_arg;
3938 return &mono_defaults.int16_class->byval_arg;
3946 return &mono_defaults.int32_class->byval_arg;
3952 return &mono_defaults.int64_class->byval_arg;
3958 return &mono_defaults.single_class->byval_arg;
3964 return &mono_defaults.double_class->byval_arg;
3970 return &mono_defaults.int_class->byval_arg;
3974 case CEE_LDELEM_REF:
3975 case CEE_STELEM_REF:
3976 return &mono_defaults.object_class->byval_arg;
3979 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3985 do_load_indirect (VerifyContext *ctx, int opcode)
3988 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3990 if (!check_underflow (ctx, 1))
3993 value = stack_pop (ctx);
3994 if (!stack_slot_is_managed_pointer (value)) {
3995 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3996 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4000 if (opcode == CEE_LDIND_REF) {
4001 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
4002 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
4003 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
4005 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
4006 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4007 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4012 do_store_indirect (VerifyContext *ctx, int opcode)
4014 ILStackDesc *addr, *val;
4015 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
4017 if (!check_underflow (ctx, 2))
4020 val = stack_pop (ctx);
4021 addr = stack_pop (ctx);
4023 check_unmanaged_pointer (ctx, addr);
4025 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
4026 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
4030 if (stack_slot_is_managed_mutability_pointer (addr)) {
4031 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
4035 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
4036 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4038 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
4039 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4043 do_newarr (VerifyContext *ctx, int token)
4046 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
4051 if (!check_underflow (ctx, 1))
4054 value = stack_pop (ctx);
4055 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4056 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));
4058 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
4061 /*FIXME handle arrays that are not 0-indexed*/
4063 do_ldlen (VerifyContext *ctx)
4067 if (!check_underflow (ctx, 1))
4070 value = stack_pop (ctx);
4072 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
4073 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
4075 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
4078 /*FIXME handle arrays that are not 0-indexed*/
4079 /*FIXME handle readonly prefix and CMMP*/
4081 do_ldelema (VerifyContext *ctx, int klass_token)
4083 ILStackDesc *index, *array, *res;
4084 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
4090 if (!check_underflow (ctx, 2))
4093 index = stack_pop (ctx);
4094 array = stack_pop (ctx);
4096 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4097 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));
4099 if (!stack_slot_is_null_literal (array)) {
4100 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4101 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4103 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
4104 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
4106 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
4109 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
4113 res = stack_push (ctx);
4114 set_stack_value (ctx, res, type, TRUE);
4115 if (ctx->prefix_set & PREFIX_READONLY) {
4116 ctx->prefix_set &= ~PREFIX_READONLY;
4117 res->stype |= CMMP_MASK;
4122 * FIXME handle arrays that are not 0-indexed
4123 * FIXME handle readonly prefix and CMMP
4126 do_ldelem (VerifyContext *ctx, int opcode, int token)
4128 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4129 ILStackDesc *index, *array;
4131 if (!check_underflow (ctx, 2))
4134 if (opcode == CEE_LDELEM) {
4135 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
4136 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4140 type = mono_type_from_opcode (opcode);
4143 index = stack_pop (ctx);
4144 array = stack_pop (ctx);
4146 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4147 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));
4149 if (!stack_slot_is_null_literal (array)) {
4150 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4151 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));
4153 if (opcode == CEE_LDELEM_REF) {
4154 if (array->type->data.klass->valuetype)
4155 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
4156 type = &array->type->data.klass->byval_arg;
4158 MonoType *candidate = &array->type->data.klass->byval_arg;
4159 if (IS_STRICT_MODE (ctx)) {
4160 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
4161 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
4162 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)) ||
4163 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
4164 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4166 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
4167 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4172 set_stack_value (ctx, stack_push (ctx), type, FALSE);
4177 * FIXME handle arrays that are not 0-indexed
4180 do_stelem (VerifyContext *ctx, int opcode, int token)
4182 ILStackDesc *index, *array, *value;
4184 if (!check_underflow (ctx, 3))
4187 if (opcode == CEE_STELEM) {
4188 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
4189 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4193 type = mono_type_from_opcode (opcode);
4196 value = stack_pop (ctx);
4197 index = stack_pop (ctx);
4198 array = stack_pop (ctx);
4200 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4201 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));
4203 if (!stack_slot_is_null_literal (array)) {
4204 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
4205 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));
4207 if (opcode == CEE_STELEM_REF) {
4208 if (array->type->data.klass->valuetype)
4209 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4210 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
4211 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4215 if (opcode == CEE_STELEM_REF) {
4216 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
4217 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4218 } else if (opcode != CEE_STELEM_REF) {
4219 if (!verify_stack_type_compatibility (ctx, type, value))
4220 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4222 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
4223 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));
4229 do_throw (VerifyContext *ctx)
4231 ILStackDesc *exception;
4232 if (!check_underflow (ctx, 1))
4234 exception = stack_pop (ctx);
4236 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4237 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4239 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
4240 char *name = mono_type_full_name (exception->type);
4241 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));
4244 /*The stack is left empty after a throw*/
4250 do_endfilter (VerifyContext *ctx)
4252 MonoExceptionClause *clause;
4254 if (IS_STRICT_MODE (ctx)) {
4255 if (ctx->eval.size != 1)
4256 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4258 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4259 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4262 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4263 if (IS_STRICT_MODE (ctx)) {
4264 if (ctx->ip_offset != clause->handler_offset - 2)
4265 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4267 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4268 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4271 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4272 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4274 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4281 do_leave (VerifyContext *ctx, int delta)
4283 int target = ((gint32)ctx->ip_offset) + delta;
4284 if (target >= ctx->code_size || target < 0)
4285 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4287 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4288 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4290 ctx->target = target;
4296 * Verify br and br.s opcodes.
4299 do_static_branch (VerifyContext *ctx, int delta)
4301 int target = ctx->ip_offset + delta;
4302 if (target < 0 || target >= ctx->code_size) {
4303 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4307 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4309 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4312 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4316 ctx->target = target;
4320 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4322 int i, base = ctx->ip_offset + 5 + count * 4;
4325 if (!check_underflow (ctx, 1))
4328 value = stack_pop (ctx);
4330 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4331 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4333 for (i = 0; i < count; ++i) {
4334 int target = base + read32 (data + i * 4);
4336 if (target < 0 || target >= ctx->code_size) {
4337 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4341 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4343 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4346 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4349 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4354 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual_)
4359 if (virtual_ && !check_underflow (ctx, 1))
4362 if (!virtual_ && !check_overflow (ctx))
4365 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
4366 method = (MonoMethod *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
4368 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4372 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4373 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4377 if (!(method = verifier_load_method (ctx, token, virtual_ ? "ldvirtfrn" : "ldftn")))
4381 if (mono_method_is_constructor (method))
4382 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4385 ILStackDesc *top = stack_pop (ctx);
4387 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4388 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4390 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4391 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4393 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4394 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4397 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
4398 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);
4400 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4401 top->method = method;
4405 do_sizeof (VerifyContext *ctx, int token)
4409 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4412 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4413 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4417 if (type->type == MONO_TYPE_VOID) {
4418 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4422 if (check_overflow (ctx))
4423 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4426 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4428 do_localloc (VerifyContext *ctx)
4430 if (ctx->eval.size != 1) {
4431 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4435 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4436 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4440 /*TODO verify top type*/
4441 /* top = */ stack_pop (ctx);
4443 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4444 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4448 do_ldstr (VerifyContext *ctx, guint32 token)
4450 GSList *error = NULL;
4451 if (ctx->method->wrapper_type == MONO_WRAPPER_NONE && !image_is_dynamic (ctx->image)) {
4452 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4453 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4457 if (!mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4459 ctx->list = g_slist_concat (ctx->list, error);
4460 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4465 if (check_overflow (ctx))
4466 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4470 do_refanyval (VerifyContext *ctx, int token)
4474 if (!check_underflow (ctx, 1))
4477 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4480 top = stack_pop (ctx);
4482 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4483 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));
4485 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4489 do_refanytype (VerifyContext *ctx)
4493 if (!check_underflow (ctx, 1))
4496 top = stack_pop (ctx);
4498 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4499 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));
4501 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4506 do_mkrefany (VerifyContext *ctx, int token)
4510 if (!check_underflow (ctx, 1))
4513 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4516 top = stack_pop (ctx);
4518 if (stack_slot_is_managed_mutability_pointer (top))
4519 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4521 if (!stack_slot_is_managed_pointer (top)) {
4522 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));
4524 MonoType *stack_type = mono_type_get_type_byval (top->type);
4525 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4526 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4528 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4529 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4532 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4536 do_ckfinite (VerifyContext *ctx)
4539 if (!check_underflow (ctx, 1))
4542 top = stack_pop (ctx);
4544 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4545 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));
4546 stack_push_stack_val (ctx, top);
4550 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4552 * @from holds new values for a given control path
4553 * @to holds the current values of a given control path
4555 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4558 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4562 stack_init (ctx, to);
4565 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4568 stack_copy (&ctx->eval, to);
4570 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4571 stack_copy (to, &ctx->eval);
4574 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4576 if (from->size != to->size) {
4577 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4578 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));
4582 //FIXME we need to preserve CMMP attributes
4583 //FIXME we must take null literals into consideration.
4584 for (i = 0; i < from->size; ++i) {
4585 ILStackDesc *new_slot = from->stack + i;
4586 ILStackDesc *old_slot = to->stack + i;
4587 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4588 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4589 MonoClass *old_class = mono_class_from_mono_type (old_type);
4590 MonoClass *new_class = mono_class_from_mono_type (new_type);
4591 MonoClass *match_class = NULL;
4593 // S := T then U = S (new value is compatible with current value, keep current)
4594 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4595 copy_stack_value (new_slot, old_slot);
4599 // T := S then U = T (old value is compatible with current value, use new)
4600 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4601 copy_stack_value (old_slot, new_slot);
4605 /*Both slots are the same boxed valuetype. Simply copy it.*/
4606 if (stack_slot_is_boxed_value (old_slot) &&
4607 stack_slot_is_boxed_value (new_slot) &&
4608 mono_metadata_type_equal (old_type, new_type)) {
4609 copy_stack_value (new_slot, old_slot);
4613 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4614 char *old_name = stack_slot_full_name (old_slot);
4615 char *new_name = stack_slot_full_name (new_slot);
4616 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));
4622 //both are reference types, use closest common super type
4623 if (!mono_class_from_mono_type (old_type)->valuetype
4624 && !mono_class_from_mono_type (new_type)->valuetype
4625 && !stack_slot_is_managed_pointer (old_slot)
4626 && !stack_slot_is_managed_pointer (new_slot)) {
4628 mono_class_setup_supertypes (old_class);
4629 mono_class_setup_supertypes (new_class);
4631 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4632 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4633 match_class = old_class->supertypes [j];
4638 mono_class_setup_interfaces (old_class, &error);
4639 if (!mono_error_ok (&error)) {
4640 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));
4641 mono_error_cleanup (&error);
4644 mono_class_setup_interfaces (new_class, &error);
4645 if (!mono_error_ok (&error)) {
4646 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));
4647 mono_error_cleanup (&error);
4651 /* if old class is an interface that new class implements */
4652 if (mono_class_is_interface (old_class)) {
4653 if (verifier_class_is_assignable_from (old_class, new_class)) {
4654 match_class = old_class;
4657 for (j = 0; j < old_class->interface_count; ++j) {
4658 if (verifier_class_is_assignable_from (old_class->interfaces [j], new_class)) {
4659 match_class = old_class->interfaces [j];
4665 if (mono_class_is_interface (new_class)) {
4666 if (verifier_class_is_assignable_from (new_class, old_class)) {
4667 match_class = new_class;
4670 for (j = 0; j < new_class->interface_count; ++j) {
4671 if (verifier_class_is_assignable_from (new_class->interfaces [j], old_class)) {
4672 match_class = new_class->interfaces [j];
4678 //No decent super type found, use object
4679 match_class = mono_defaults.object_class;
4681 } 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)) {
4682 match_class = mono_defaults.object_class;
4687 char *old_name = stack_slot_full_name (old_slot);
4688 char *new_name = stack_slot_full_name (new_slot);
4689 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));
4693 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4697 g_assert (match_class);
4698 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4699 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4705 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4706 to->flags |= IL_CODE_STACK_MERGED;
4709 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4710 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4713 * is_clause_in_range :
4715 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4718 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4720 if (clause->try_offset >= start && clause->try_offset < end)
4722 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4728 * is_clause_inside_range :
4730 * Returns TRUE if @clause lies completely inside the @start - @end range.
4733 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4735 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4737 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4743 * is_clause_nested :
4745 * Returns TRUE if @nested is nested in @clause.
4748 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4750 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4752 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4753 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4756 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4757 * the each pair of exception must have the following properties:
4758 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4759 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4760 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4763 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4765 /*clause is nested*/
4766 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4767 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4771 /*wrong nesting order.*/
4772 if (is_clause_nested (clause, to_test)) {
4773 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4777 /*mutual protection*/
4778 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4779 /*handlers are not disjoint*/
4780 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4781 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4784 /* handlers are not catch or filter */
4785 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4786 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4793 /*not completelly disjoint*/
4794 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4795 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4796 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4799 #define code_bounds_check(size) \
4800 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4801 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4806 mono_opcode_is_prefix (int op)
4809 case MONO_CEE_UNALIGNED_:
4810 case MONO_CEE_VOLATILE_:
4811 case MONO_CEE_TAIL_:
4812 case MONO_CEE_CONSTRAINED_:
4813 case MONO_CEE_READONLY_:
4820 * FIXME: need to distinguish between valid and verifiable.
4821 * Need to keep track of types on the stack.
4825 * mono_method_verify:
4826 * Verify types for opcodes.
4829 mono_method_verify (MonoMethod *method, int level)
4832 const unsigned char *ip, *code_start;
4833 const unsigned char *end;
4834 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4836 int i, n, need_merge = 0, start = 0;
4837 guint ip_offset = 0, prefix = 0;
4838 MonoGenericContext *generic_context = NULL;
4842 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4844 init_verifier_stats ();
4846 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4847 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4851 memset (&ctx, 0, sizeof (VerifyContext));
4853 //FIXME use mono_method_get_signature_full
4854 ctx.signature = mono_method_signature (method);
4855 if (!ctx.signature) {
4856 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4858 finish_collect_stats ();
4861 if (!method->is_generic && !mono_class_is_gtd (method->klass) && ctx.signature->has_type_parameters) {
4862 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method and signature don't match in terms of genericity"));
4863 finish_collect_stats ();
4867 ctx.header = mono_method_get_header_checked (method, &error);
4869 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header due to %s", mono_error_get_message (&error)));
4870 mono_error_cleanup (&error);
4871 finish_collect_stats ();
4874 ctx.method = method;
4875 code_start = ip = ctx.header->code;
4876 end = ip + ctx.header->code_size;
4877 ctx.image = image = method->klass->image;
4880 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4881 ctx.max_stack = ctx.header->max_stack;
4882 ctx.verifiable = ctx.valid = 1;
4885 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4886 ctx.code_size = ctx.header->code_size;
4887 _MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4889 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4891 ctx.num_locals = ctx.header->num_locals;
4892 ctx.locals = (MonoType **)g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4893 _MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4895 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4896 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4898 ctx.params = g_new (MonoType*, ctx.max_args);
4899 _MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4901 if (ctx.signature->hasthis)
4902 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4903 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4905 if (ctx.signature->is_inflated)
4906 ctx.generic_context = generic_context = mono_method_get_context (method);
4908 if (!generic_context && (mono_class_is_gtd (method->klass) || method->is_generic)) {
4909 if (method->is_generic)
4910 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4912 ctx.generic_context = generic_context = &mono_class_get_generic_container (method->klass)->context;
4915 for (i = 0; i < ctx.num_locals; ++i) {
4916 MonoType *uninflated = ctx.locals [i];
4917 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4918 if (!mono_error_ok (&error)) {
4919 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4920 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4922 mono_error_cleanup (&error);
4923 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4929 for (i = 0; i < ctx.max_args; ++i) {
4930 MonoType *uninflated = ctx.params [i];
4931 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4932 if (!mono_error_ok (&error)) {
4933 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4934 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4936 mono_error_cleanup (&error);
4937 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4942 stack_init (&ctx, &ctx.eval);
4944 for (i = 0; i < ctx.num_locals; ++i) {
4945 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4947 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4948 char *name = mono_type_full_name (ctx.locals [i]);
4949 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4956 for (i = 0; i < ctx.max_args; ++i) {
4957 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4960 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4961 char *name = mono_type_full_name (ctx.params [i]);
4962 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4971 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4972 MonoExceptionClause *clause = ctx.header->clauses + i;
4973 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); );
4975 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4976 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4978 if (clause->try_len <= 0)
4979 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4981 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4982 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4984 if (clause->handler_len <= 0)
4985 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4987 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4988 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4990 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4991 if (clause->data.filter_offset > ctx.code_size)
4992 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4994 if (clause->data.filter_offset >= clause->handler_offset)
4995 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4998 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4999 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
5004 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
5005 if (clause->try_offset + clause->try_len < ctx.code_size)
5006 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
5007 if (clause->handler_offset + clause->handler_len < ctx.code_size)
5008 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
5010 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
5011 if (!clause->data.catch_class) {
5012 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
5015 if (!mono_type_is_valid_in_context (&ctx, &clause->data.catch_class->byval_arg))
5018 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
5020 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5021 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
5022 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
5029 original_bb = bb = mono_basic_block_split (method, &error, ctx.header);
5030 if (!mono_error_ok (&error)) {
5031 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
5032 mono_error_cleanup (&error);
5037 while (ip < end && ctx.valid) {
5039 ip_offset = (guint) (ip - code_start);
5041 const unsigned char *ip_copy = ip;
5044 if (ip_offset > bb->end) {
5045 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
5049 if (ip_offset == bb->end)
5052 op_size = mono_opcode_value_and_size (&ip_copy, end, &op);
5053 if (op_size == -1) {
5054 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
5058 if (ADD_IS_GREATER_OR_OVF (ip_offset, op_size, bb->end)) {
5059 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
5063 /*Last Instruction*/
5064 if (ip_offset + op_size == bb->end && mono_opcode_is_prefix (op)) {
5065 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));
5070 ctx.ip_offset = ip_offset = (guint) (ip - code_start);
5072 /*We need to check against fallthrou in and out of protected blocks.
5073 * For fallout we check the once a protected block ends, if the start flag is not set.
5074 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5075 * TODO convert these checks to be done using flags and not this loop
5077 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
5078 MonoExceptionClause *clause = ctx.header->clauses + i;
5080 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
5081 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
5085 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
5086 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
5087 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5089 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5093 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
5094 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
5098 if (clause->handler_offset == ip_offset && start == 0) {
5099 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
5103 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
5104 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
5109 /*This must be done after fallthru detection otherwise it won't happen.*/
5111 /*FIXME remove this once we move all bad branch checking code to use BB only*/
5112 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
5121 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
5122 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
5125 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
5128 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5129 #ifdef MONO_VERIFIER_DEBUG
5132 discode = mono_disasm_code_one (NULL, method, ip, NULL);
5133 discode [strlen (discode) - 1] = 0; /* no \n */
5134 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
5137 dump_stack_state (&ctx.code [ip_offset]);
5138 dump_stack_state (&ctx.eval);
5151 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
5157 code_bounds_check (2);
5158 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
5162 case CEE_ADD_OVF_UN:
5163 do_binop (&ctx, *ip, add_ovf_un_table);
5167 case CEE_SUB_OVF_UN:
5168 do_binop (&ctx, *ip, sub_ovf_un_table);
5175 case CEE_MUL_OVF_UN:
5176 do_binop (&ctx, *ip, bin_ovf_table);
5181 do_binop (&ctx, *ip, add_table);
5186 do_binop (&ctx, *ip, sub_table);
5193 do_binop (&ctx, *ip, bin_op_table);
5202 do_binop (&ctx, *ip, int_bin_op_table);
5209 do_binop (&ctx, *ip, shift_op_table);
5214 if (!check_underflow (&ctx, 1))
5216 stack_pop_safe (&ctx);
5230 /*TODO support definite assignment verification? */
5231 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
5239 store_local (&ctx, *ip - CEE_STLOC_0);
5244 code_bounds_check (2);
5245 store_local (&ctx, ip [1]);
5250 code_bounds_check (2);
5251 store_arg (&ctx, ip [1]);
5265 if (check_overflow (&ctx))
5266 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5271 code_bounds_check (2);
5272 if (check_overflow (&ctx))
5273 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5278 code_bounds_check (5);
5279 if (check_overflow (&ctx))
5280 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
5285 code_bounds_check (9);
5286 if (check_overflow (&ctx))
5287 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
5292 code_bounds_check (5);
5293 if (check_overflow (&ctx))
5294 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5299 code_bounds_check (9);
5300 if (check_overflow (&ctx))
5301 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5306 if (check_overflow (&ctx))
5307 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
5313 code_bounds_check (2);
5314 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
5327 code_bounds_check (2);
5328 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5335 code_bounds_check (5);
5336 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5349 code_bounds_check (5);
5350 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5357 code_bounds_check (2);
5358 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5363 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5369 if (!check_underflow (&ctx, 1))
5371 if (!check_overflow (&ctx))
5373 top = stack_push (&ctx);
5374 copy_stack_value (top, stack_peek (&ctx, 1));
5380 code_bounds_check (5);
5382 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5383 /* token = read32 (ip + 1); */
5384 if (in_any_block (ctx.header, ip_offset))
5385 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5387 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5389 * FIXME: check signature, retval, arguments etc.
5395 code_bounds_check (5);
5396 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5401 code_bounds_check (5);
5402 /* token = read32 (ip + 1); */
5404 * FIXME: check signature, retval, arguments etc.
5405 * FIXME: check requirements for tail call
5407 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5411 code_bounds_check (2);
5412 do_static_branch (&ctx, (signed char)ip [1] + 2);
5420 code_bounds_check (2);
5421 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5427 code_bounds_check (5);
5428 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5436 code_bounds_check (5);
5437 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5444 code_bounds_check (5);
5445 entries = read32 (ip + 1);
5447 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5448 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5451 code_bounds_check (sizeof (guint32) * entries);
5453 do_switch (&ctx, entries, ip);
5454 ip += sizeof (guint32) * entries;
5468 do_load_indirect (&ctx, *ip);
5480 do_store_indirect (&ctx, *ip);
5486 do_unary_math_op (&ctx, *ip);
5496 do_conversion (&ctx, TYPE_I4);
5502 do_conversion (&ctx, TYPE_I8);
5509 do_conversion (&ctx, TYPE_R8);
5515 do_conversion (&ctx, TYPE_NATIVE_INT);
5520 code_bounds_check (5);
5521 do_cpobj (&ctx, read32 (ip + 1));
5526 code_bounds_check (5);
5527 do_ldobj_value (&ctx, read32 (ip + 1));
5532 code_bounds_check (5);
5533 do_ldstr (&ctx, read32 (ip + 1));
5538 code_bounds_check (5);
5539 do_newobj (&ctx, read32 (ip + 1));
5545 code_bounds_check (5);
5546 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5552 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5557 code_bounds_check (5);
5558 do_unbox_value (&ctx, read32 (ip + 1));
5570 code_bounds_check (5);
5571 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5577 code_bounds_check (5);
5578 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5583 code_bounds_check (5);
5584 do_store_field (&ctx, read32 (ip + 1));
5589 code_bounds_check (5);
5590 do_store_static_field (&ctx, read32 (ip + 1));
5595 code_bounds_check (5);
5596 do_stobj (&ctx, read32 (ip + 1));
5600 case CEE_CONV_OVF_I1_UN:
5601 case CEE_CONV_OVF_I2_UN:
5602 case CEE_CONV_OVF_I4_UN:
5603 case CEE_CONV_OVF_U1_UN:
5604 case CEE_CONV_OVF_U2_UN:
5605 case CEE_CONV_OVF_U4_UN:
5606 do_conversion (&ctx, TYPE_I4);
5610 case CEE_CONV_OVF_I8_UN:
5611 case CEE_CONV_OVF_U8_UN:
5612 do_conversion (&ctx, TYPE_I8);
5616 case CEE_CONV_OVF_I_UN:
5617 case CEE_CONV_OVF_U_UN:
5618 do_conversion (&ctx, TYPE_NATIVE_INT);
5623 code_bounds_check (5);
5624 do_box_value (&ctx, read32 (ip + 1));
5629 code_bounds_check (5);
5630 do_newarr (&ctx, read32 (ip + 1));
5640 code_bounds_check (5);
5641 do_ldelema (&ctx, read32 (ip + 1));
5655 case CEE_LDELEM_REF:
5656 do_ldelem (&ctx, *ip, 0);
5667 case CEE_STELEM_REF:
5668 do_stelem (&ctx, *ip, 0);
5673 code_bounds_check (5);
5674 do_ldelem (&ctx, *ip, read32 (ip + 1));
5679 code_bounds_check (5);
5680 do_stelem (&ctx, *ip, read32 (ip + 1));
5685 code_bounds_check (5);
5686 do_unbox_any (&ctx, read32 (ip + 1));
5690 case CEE_CONV_OVF_I1:
5691 case CEE_CONV_OVF_U1:
5692 case CEE_CONV_OVF_I2:
5693 case CEE_CONV_OVF_U2:
5694 case CEE_CONV_OVF_I4:
5695 case CEE_CONV_OVF_U4:
5696 do_conversion (&ctx, TYPE_I4);
5700 case CEE_CONV_OVF_I8:
5701 case CEE_CONV_OVF_U8:
5702 do_conversion (&ctx, TYPE_I8);
5706 case CEE_CONV_OVF_I:
5707 case CEE_CONV_OVF_U:
5708 do_conversion (&ctx, TYPE_NATIVE_INT);
5713 code_bounds_check (5);
5714 do_refanyval (&ctx, read32 (ip + 1));
5724 code_bounds_check (5);
5725 do_mkrefany (&ctx, read32 (ip + 1));
5730 code_bounds_check (5);
5731 do_load_token (&ctx, read32 (ip + 1));
5735 case CEE_ENDFINALLY:
5736 if (!is_correct_endfinally (ctx.header, ip_offset))
5737 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5744 code_bounds_check (5);
5745 do_leave (&ctx, read32 (ip + 1) + 5);
5752 code_bounds_check (2);
5753 do_leave (&ctx, (signed char)ip [1] + 2);
5760 code_bounds_check (2);
5764 code_bounds_check (3);
5765 store_local (&ctx, read16 (ip + 1));
5770 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5778 do_cmp_op (&ctx, cmp_br_op, *ip);
5783 code_bounds_check (3);
5784 store_arg (&ctx, read16 (ip + 1) );
5790 if (!check_overflow (&ctx))
5792 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5793 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5794 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5799 code_bounds_check (5);
5800 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5805 code_bounds_check (5);
5806 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5812 code_bounds_check (3);
5813 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5819 code_bounds_check (3);
5820 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5833 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5837 do_endfilter (&ctx);
5841 case CEE_UNALIGNED_:
5842 code_bounds_check (2);
5843 prefix |= PREFIX_UNALIGNED;
5847 prefix |= PREFIX_VOLATILE;
5851 prefix |= PREFIX_TAIL;
5853 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5854 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5858 code_bounds_check (5);
5859 do_initobj (&ctx, read32 (ip + 1));
5863 case CEE_CONSTRAINED_:
5864 code_bounds_check (5);
5865 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5866 prefix |= PREFIX_CONSTRAINED;
5871 prefix |= PREFIX_READONLY;
5876 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5877 if (!check_underflow (&ctx, 3))
5879 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5884 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5885 if (!check_underflow (&ctx, 3))
5887 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5895 if (!is_correct_rethrow (ctx.header, ip_offset))
5896 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5903 code_bounds_check (5);
5904 do_sizeof (&ctx, read32 (ip + 1));
5908 case CEE_REFANYTYPE:
5909 do_refanytype (&ctx);
5914 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5920 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5924 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5926 if (!ctx.prefix_set) //first prefix
5927 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5928 ctx.prefix_set |= prefix;
5929 ctx.has_flags = TRUE;
5933 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5935 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5936 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5937 if (ctx.prefix_set & PREFIX_READONLY)
5938 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5939 if (ctx.prefix_set & PREFIX_VOLATILE)
5940 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5941 if (ctx.prefix_set & PREFIX_UNALIGNED)
5942 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5943 ctx.prefix_set = prefix = 0;
5944 ctx.has_flags = FALSE;
5948 * if ip != end we overflowed: mark as error.
5950 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5951 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5954 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5955 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5956 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5957 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5958 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of instruction at 0x%04x", i));
5960 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5961 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5963 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5964 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5966 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5967 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));
5970 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5971 char *method_name = mono_method_full_name (ctx.method, TRUE);
5972 char *type = mono_type_get_full_name (ctx.method->klass);
5973 if (ctx.method->klass->parent && mono_class_has_failure (ctx.method->klass->parent))
5974 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));
5976 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5977 g_free (method_name);
5983 for (i = 0; i < ctx.header->code_size; ++i) {
5984 if (ctx.code [i].stack)
5985 g_free (ctx.code [i].stack);
5989 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5991 g_slist_free (ctx.funptrs);
5993 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5994 mono_metadata_free_type ((MonoType *)tmp->data);
5995 g_slist_free (ctx.exception_types);
5997 for (i = 0; i < ctx.num_locals; ++i) {
5999 mono_metadata_free_type (ctx.locals [i]);
6001 for (i = 0; i < ctx.max_args; ++i) {
6003 mono_metadata_free_type (ctx.params [i]);
6007 g_free (ctx.eval.stack);
6010 g_free (ctx.locals);
6011 g_free (ctx.params);
6012 mono_basic_block_free (original_bb);
6013 mono_metadata_free_mh (ctx.header);
6015 finish_collect_stats ();
6020 mono_verify_corlib ()
6022 /* This is a public API function so cannot be removed */
6027 * mono_verifier_is_enabled_for_method:
6028 * \param method the method to probe
6029 * \returns TRUE if \p method needs to be verified.
6032 mono_verifier_is_enabled_for_method (MonoMethod *method)
6034 return mono_verifier_is_enabled_for_class (method->klass) && (method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD);
6038 * mono_verifier_is_enabled_for_class:
6039 * \param klass The \c MonoClass to probe
6040 * \returns TRUE if \p klass need to be verified.
6043 mono_verifier_is_enabled_for_class (MonoClass *klass)
6045 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
6049 mono_verifier_is_enabled_for_image (MonoImage *image)
6051 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
6055 * Dynamic methods are not considered full trust since if the user is trusted and need to
6056 * generate unsafe code, make the method skip verification - this is a known good way to do it.
6059 mono_verifier_is_method_full_trust (MonoMethod *method)
6061 return mono_verifier_is_class_full_trust (method->klass) && !method_is_dynamic (method);
6065 * Returns if @klass is under full trust or not.
6067 * TODO This code doesn't take CAS into account.
6069 * Under verify_all all user code must be verifiable if no security option was set
6073 mono_verifier_is_class_full_trust (MonoClass *klass)
6075 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
6076 gboolean trusted_location = !mono_security_core_clr_enabled () ?
6077 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
6079 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
6080 return trusted_location || klass->image == mono_defaults.corlib;
6081 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
6085 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
6087 return mono_method_verify (method,
6088 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
6089 | (!is_fulltrust && !mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
6090 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
6094 get_field_end (MonoClassField *field)
6097 int size = mono_type_size (field->type, &align);
6099 size = 4; /*FIXME Is this a safe bet?*/
6100 return size + field->offset;
6104 verify_class_for_overlapping_reference_fields (MonoClass *klass)
6107 gpointer iter = NULL;
6108 MonoClassField *field;
6109 gboolean is_fulltrust = mono_verifier_is_class_full_trust (klass);
6110 /*We can't skip types with !has_references since this is calculated after we have run.*/
6111 if (!mono_class_is_explicit_layout (klass))
6115 /*We must check for stuff overlapping reference fields.
6116 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
6118 while ((field = mono_class_get_fields (klass, &iter))) {
6119 int fieldEnd = get_field_end (field);
6120 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
6123 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
6126 int fcount = mono_class_get_field_count (klass);
6127 for (j = i; j < fcount; ++j) {
6128 MonoClassField *other = &klass->fields [j];
6129 int otherEnd = get_field_end (other);
6130 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
6133 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
6136 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
6144 field_hash (gconstpointer key)
6146 const MonoClassField *field = (const MonoClassField *)key;
6147 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
6151 field_equals (gconstpointer _a, gconstpointer _b)
6153 const MonoClassField *a = (const MonoClassField *)_a;
6154 const MonoClassField *b = (const MonoClassField *)_b;
6155 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
6160 verify_class_fields (MonoClass *klass)
6162 gpointer iter = NULL;
6163 MonoClassField *field;
6164 MonoGenericContext *context = mono_class_get_context (klass);
6165 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
6166 if (mono_class_is_gtd (klass))
6167 context = &mono_class_get_generic_container (klass)->context;
6169 while ((field = mono_class_get_fields (klass, &iter)) != NULL) {
6170 if (!mono_type_is_valid_type_in_context (field->type, context)) {
6171 g_hash_table_destroy (unique_fields);
6174 if (g_hash_table_lookup (unique_fields, field)) {
6175 g_hash_table_destroy (unique_fields);
6178 g_hash_table_insert (unique_fields, field, field);
6180 g_hash_table_destroy (unique_fields);
6185 verify_interfaces (MonoClass *klass)
6188 for (i = 0; i < klass->interface_count; ++i) {
6189 MonoClass *iface = klass->interfaces [i];
6190 if (!mono_class_get_flags (iface))
6197 verify_valuetype_layout_with_target (MonoClass *klass, MonoClass *target_class)
6200 gpointer iter = NULL;
6201 MonoClassField *field;
6202 MonoClass *field_class;
6204 if (!klass->valuetype)
6207 type = klass->byval_arg.type;
6208 /*primitive type fields are not properly decoded*/
6209 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
6212 while ((field = mono_class_get_fields (klass, &iter)) != NULL) {
6216 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
6219 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
6221 if (field_class == target_class || klass == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
6229 verify_valuetype_layout (MonoClass *klass)
6232 res = verify_valuetype_layout_with_target (klass, klass);
6237 recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
6240 MonoClass **constraints;
6241 MonoGenericParamInfo *param_info;
6243 g_assert (mono_type_is_generic_argument (type));
6245 idx = mono_type_get_generic_param_num (type);
6246 if (mono_bitset_test_fast (used_args, idx))
6249 mono_bitset_set_fast (used_args, idx);
6250 param_info = mono_generic_container_get_param_info (gc, idx);
6252 if (!param_info->constraints)
6255 for (constraints = param_info->constraints; *constraints; ++constraints) {
6256 MonoClass *ctr = *constraints;
6257 MonoType *constraint_type = &ctr->byval_arg;
6259 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6266 verify_generic_parameters (MonoClass *klass)
6269 MonoGenericContainer *gc = mono_class_get_generic_container (klass);
6270 MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
6272 for (i = 0; i < gc->type_argc; ++i) {
6273 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
6274 MonoClass **constraints;
6276 if (!param_info->constraints)
6279 mono_bitset_clear_all (used_args);
6280 mono_bitset_set_fast (used_args, i);
6282 for (constraints = param_info->constraints; *constraints; ++constraints) {
6283 MonoClass *ctr = *constraints;
6284 MonoType *constraint_type = &ctr->byval_arg;
6286 if (!mono_class_can_access_class (klass, ctr))
6289 if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
6292 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6294 if (mono_class_is_ginst (ctr) && !mono_class_is_valid_generic_instantiation (NULL, ctr))
6298 mono_bitset_free (used_args);
6302 mono_bitset_free (used_args);
6307 * Check if the class is verifiable.
6309 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6310 * field and invalid generic instantiation are fatal errors.
6312 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6316 mono_verifier_verify_class (MonoClass *klass)
6318 /*Neither <Module>, object or ifaces have parent.*/
6319 if (!klass->parent &&
6320 klass != mono_defaults.object_class &&
6321 !MONO_CLASS_IS_INTERFACE (klass) &&
6322 (!image_is_dynamic (klass->image) && klass->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
6324 if (klass->parent) {
6325 if (MONO_CLASS_IS_INTERFACE (klass->parent))
6327 if (!mono_class_is_ginst (klass) && mono_class_is_gtd (klass->parent))
6329 if (mono_class_is_ginst (klass->parent) && !mono_class_is_ginst (klass)) {
6330 MonoGenericContext *context = mono_class_get_context (klass);
6331 if (mono_class_is_gtd (klass))
6332 context = &mono_class_get_generic_container (klass)->context;
6333 if (!mono_type_is_valid_type_in_context (&klass->parent->byval_arg, context))
6337 if (mono_class_is_gtd (klass) && (mono_class_is_explicit_layout (klass)))
6339 if (mono_class_is_gtd (klass) && !verify_generic_parameters (klass))
6341 if (!verify_class_for_overlapping_reference_fields (klass))
6343 if (mono_class_is_ginst (klass) && !mono_class_is_valid_generic_instantiation (NULL, klass))
6345 if (!mono_class_is_ginst (klass) && !verify_class_fields (klass))
6347 if (klass->valuetype && !verify_valuetype_layout (klass))
6349 if (!verify_interfaces (klass))
6355 mono_verifier_class_is_valid_generic_instantiation (MonoClass *klass)
6357 return mono_class_is_valid_generic_instantiation (NULL, klass);
6361 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)
6363 if (!method->is_inflated)
6365 return mono_method_is_valid_generic_instantiation (NULL, method);
6371 mono_verifier_verify_class (MonoClass *klass)
6373 /* The verifier was disabled at compile time */
6378 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
6380 /* The verifier was disabled at compile time */
6385 mono_verifier_is_class_full_trust (MonoClass *klass)
6387 /* The verifier was disabled at compile time */
6392 mono_verifier_is_method_full_trust (MonoMethod *method)
6394 /* The verifier was disabled at compile time */
6399 mono_verifier_is_enabled_for_image (MonoImage *image)
6401 /* The verifier was disabled at compile time */
6406 mono_verifier_is_enabled_for_class (MonoClass *klass)
6408 /* The verifier was disabled at compile time */
6413 mono_verifier_is_enabled_for_method (MonoMethod *method)
6415 /* The verifier was disabled at compile time */
6420 mono_method_verify (MonoMethod *method, int level)
6422 /* The verifier was disabled at compile time */
6427 mono_free_verify_list (GSList *list)
6429 /* The verifier was disabled at compile time */
6430 /* will always be null if verifier is disabled */
6434 mono_verifier_class_is_valid_generic_instantiation (MonoClass *klass)
6440 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)