5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Rodrigo Kumpera
13 #include <mono/metadata/object-internals.h>
14 #include <mono/metadata/verify.h>
15 #include <mono/metadata/verify-internals.h>
16 #include <mono/metadata/opcodes.h>
17 #include <mono/metadata/tabledefs.h>
18 #include <mono/metadata/reflection.h>
19 #include <mono/metadata/debug-helpers.h>
20 #include <mono/metadata/mono-endian.h>
21 #include <mono/metadata/metadata.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/security-manager.h>
25 #include <mono/metadata/security-core-clr.h>
26 #include <mono/metadata/tokentype.h>
27 #include <mono/metadata/mono-basic-block.h>
28 #include <mono/metadata/attrdefs.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/utils/mono-counters.h>
31 #include <mono/utils/monobitset.h>
35 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
36 static gboolean verify_all = FALSE;
39 * Set the desired level of checks for the verfier.
43 mono_verifier_set_mode (MiniVerifierMode mode)
49 mono_verifier_enable_verify_all ()
54 #ifndef DISABLE_VERIFIER
56 * Pull the list of opcodes
58 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
62 #include "mono/cil/opcode.def"
67 #ifdef MONO_VERIFIER_DEBUG
68 #define VERIFIER_DEBUG(code) do { code } while (0)
70 #define VERIFIER_DEBUG(code)
73 //////////////////////////////////////////////////////////////////
74 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
75 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
76 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
77 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
78 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
79 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
81 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
82 vinfo->info.status = __status; \
83 vinfo->info.message = ( __msg ); \
84 vinfo->exception_type = (__exception); \
85 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
88 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
89 #define ADD_VERIFY_ERROR(__ctx, __msg) \
91 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
95 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
97 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
98 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
99 (__ctx)->verifiable = 0; \
100 if (IS_FAIL_FAST_MODE (__ctx)) \
101 (__ctx)->valid = 0; \
105 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
107 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
108 (__ctx)->valid = 0; \
111 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
113 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
114 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
115 (__ctx)->verifiable = 0; \
116 if (IS_FAIL_FAST_MODE (__ctx)) \
117 (__ctx)->valid = 0; \
121 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
122 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
124 #if SIZEOF_VOID_P == 4
125 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
127 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
130 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
131 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
133 /*Flags to be used with ILCodeDesc::flags */
135 /*Instruction has not been processed.*/
136 IL_CODE_FLAG_NOT_PROCESSED = 0,
137 /*Instruction was decoded by mono_method_verify loop.*/
138 IL_CODE_FLAG_SEEN = 1,
139 /*Instruction was target of a branch or is at a protected block boundary.*/
140 IL_CODE_FLAG_WAS_TARGET = 2,
141 /*Used by stack_init to avoid double initialize each entry.*/
142 IL_CODE_FLAG_STACK_INITED = 4,
143 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
144 IL_CODE_STACK_MERGED = 8,
145 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
146 IL_CODE_DELEGATE_SEQUENCE = 0x10,
147 /*This is a delegate created from a ldftn to a non final virtual method*/
148 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
149 /*This is a call to a non final virtual method*/
150 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
168 guint16 size, max_size;
185 /*Allocated fnptr MonoType that should be freed by us.*/
187 /*Type dup'ed exception types from catch blocks.*/
188 GSList *exception_types;
193 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
197 MonoMethodSignature *signature;
198 MonoMethodHeader *header;
200 MonoGenericContext *generic_context;
204 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
205 *on a method that creates a delegate for a non-final virtual method using ldftn*/
206 gboolean has_this_store;
208 /*This flag is used to control if the contructor of the parent class has been called.
209 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
210 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
212 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
213 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
215 gboolean super_ctor_called;
219 MonoType *constrained_type;
223 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
226 get_stack_type (MonoType *type);
229 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
232 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
235 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
237 static MonoGenericParam*
238 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type);
241 verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate);
242 //////////////////////////////////////////////////////////////////
247 TYPE_INV = 0, /* leave at 0. */
252 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
254 /* value types and classes */
256 /* Number of types, used to define the size of the tables*/
259 /* Used by tables to signal that a result is not verifiable*/
260 NON_VERIFIABLE_RESULT = 0x80,
262 /*Mask used to extract just the type, excluding flags */
265 /* The stack type is a managed pointer, unmask the value to res */
266 POINTER_MASK = 0x100,
268 /*Stack type with the pointer mask*/
269 RAW_TYPE_MASK = 0x10F,
271 /* Controlled Mutability Manager Pointer */
274 /* The stack type is a null literal*/
275 NULL_LITERAL_MASK = 0x400,
277 /**Used by ldarg.0 and family to let delegate verification happens.*/
278 THIS_POINTER_MASK = 0x800,
280 /**Signals that this is a boxed value type*/
283 /*This is an unitialized this ref*/
284 UNINIT_THIS_MASK = 0x2000,
287 static const char* const
288 type_names [TYPE_MAX + 1] = {
299 PREFIX_UNALIGNED = 1,
302 PREFIX_CONSTRAINED = 8,
305 //////////////////////////////////////////////////////////////////
307 #ifdef ENABLE_VERIFIER_STATS
309 #define _MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
310 #define _MEM_FREE(amt) do { working_set -= (amt); } while (0)
312 static int allocated_memory;
313 static int working_set;
314 static int max_allocated_memory;
315 static int max_working_set;
316 static int total_allocated_memory;
319 finish_collect_stats (void)
321 max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
322 max_working_set = MAX (max_working_set, working_set);
323 total_allocated_memory += allocated_memory;
324 allocated_memory = working_set = 0;
328 init_verifier_stats (void)
330 static gboolean inited;
333 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
334 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
335 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
341 #define _MEM_ALLOC(amt) do {} while (0)
342 #define _MEM_FREE(amt) do { } while (0)
344 #define finish_collect_stats()
345 #define init_verifier_stats()
350 //////////////////////////////////////////////////////////////////
353 /*Token validation macros and functions */
354 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
355 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
356 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
357 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
359 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
360 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
361 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
362 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
363 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
364 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
367 * Verify if @token refers to a valid row on int's table.
370 token_bounds_check (MonoImage *image, guint32 token)
372 if (image_is_dynamic (image))
373 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
374 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token) && mono_metadata_token_index (token) > 0;
378 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
380 MonoType *res = g_new0 (MonoType, 1);
381 _MEM_ALLOC (sizeof (MonoType));
383 //FIXME use mono_method_get_signature_full
384 res->data.method = mono_method_signature (method);
385 res->type = MONO_TYPE_FNPTR;
386 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
391 * mono_type_is_enum_type:
393 * Returns TRUE if @type is an enum type.
396 mono_type_is_enum_type (MonoType *type)
398 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
400 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
406 * mono_type_is_value_type:
408 * Returns TRUE if @type is named after @namespace.@name.
412 mono_type_is_value_type (MonoType *type, const char *namespace_, const char *name)
414 return type->type == MONO_TYPE_VALUETYPE &&
415 !strcmp (namespace_, type->data.klass->name_space) &&
416 !strcmp (name, type->data.klass->name);
420 * Returns TURE if @type is VAR or MVAR
423 mono_type_is_generic_argument (MonoType *type)
425 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
429 * mono_type_get_underlying_type_any:
431 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
433 * Returns the underlying type of @type regardless if it is byref or not.
436 mono_type_get_underlying_type_any (MonoType *type)
438 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
439 return mono_class_enum_basetype (type->data.klass);
440 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
441 return mono_class_enum_basetype (type->data.generic_class->container_class);
445 static G_GNUC_UNUSED const char*
446 mono_type_get_stack_name (MonoType *type)
448 return type_names [get_stack_type (type) & TYPE_MASK];
451 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
452 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
455 mono_method_is_constructor (MonoMethod *method)
457 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
458 !(method->flags & CTOR_INVALID_FLAGS) &&
459 !strcmp (".ctor", method->name));
463 mono_class_has_default_constructor (MonoClass *klass)
468 mono_class_setup_methods (klass);
469 if (klass->exception_type)
472 for (i = 0; i < klass->method.count; ++i) {
473 method = klass->methods [i];
474 if (mono_method_is_constructor (method) &&
475 mono_method_signature (method) &&
476 mono_method_signature (method)->param_count == 0 &&
477 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
484 * Verify if @type is valid for the given @ctx verification context.
485 * this function checks for VAR and MVAR types that are invalid under the current verifier,
488 mono_type_is_valid_type_in_context_full (MonoType *type, MonoGenericContext *context, gboolean check_gtd)
491 MonoGenericInst *inst;
493 switch (type->type) {
498 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
499 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
502 case MONO_TYPE_SZARRAY:
503 return mono_type_is_valid_type_in_context_full (&type->data.klass->byval_arg, context, check_gtd);
504 case MONO_TYPE_ARRAY:
505 return mono_type_is_valid_type_in_context_full (&type->data.array->eklass->byval_arg, context, check_gtd);
507 return mono_type_is_valid_type_in_context_full (type->data.type, context, check_gtd);
508 case MONO_TYPE_GENERICINST:
509 inst = type->data.generic_class->context.class_inst;
512 for (i = 0; i < inst->type_argc; ++i)
513 if (!mono_type_is_valid_type_in_context_full (inst->type_argv [i], context, check_gtd))
516 case MONO_TYPE_CLASS:
517 case MONO_TYPE_VALUETYPE: {
518 MonoClass *klass = type->data.klass;
520 * It's possible to encode generic'sh types in such a way that they disguise themselves as class or valuetype.
521 * Fixing the type decoding is really tricky since under some cases this behavior is needed, for example, to
522 * have a 'class' type pointing to a 'genericinst' class.
524 * For the runtime these non canonical (weird) encodings work fine, the worst they can cause is some
525 * reflection oddities which are harmless - to security at least.
527 if (klass->byval_arg.type != type->type)
528 return mono_type_is_valid_type_in_context_full (&klass->byval_arg, context, check_gtd);
530 if (check_gtd && klass->generic_container)
541 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
543 return mono_type_is_valid_type_in_context_full (type, context, FALSE);
546 /*This function returns NULL if the type is not instantiatable*/
548 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
553 result = mono_class_inflate_generic_type_checked (type, context, &error);
554 if (!mono_error_ok (&error)) {
555 mono_error_cleanup (&error);
561 /*A side note here. We don't need to check if arguments are broken since this
562 is only need to be done by the runtime before realizing the type.
565 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
570 if (ginst->type_argc != gc->type_argc)
573 for (i = 0; i < gc->type_argc; ++i) {
574 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
575 MonoClass *paramClass;
576 MonoClass **constraints;
577 MonoType *param_type = ginst->type_argv [i];
579 /*it's not our job to validate type variables*/
580 if (mono_type_is_generic_argument (param_type))
583 paramClass = mono_class_from_mono_type (param_type);
586 /* A GTD can't be a generic argument.
588 * Due to how types are encoded we must check for the case of a genericinst MonoType and GTD MonoClass.
589 * This happens in cases such as: class Foo<T> { void X() { new Bar<T> (); } }
591 * Open instantiations can have GTDs as this happens when one type is instantiated with others params
592 * and the former has an expansion into the later. For example:
594 * class A<T>: B<K> {}
595 * The type A <K> has a parent B<K>, that is inflated into the GTD B<>.
596 * Since A<K> is open, thus not instantiatable, this is valid.
598 if (paramClass->generic_container && param_type->type != MONO_TYPE_GENERICINST && !ginst->is_open)
601 /*it's not safe to call mono_class_init from here*/
602 if (paramClass->generic_class && !paramClass->inited) {
603 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
607 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
610 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
613 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
616 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
619 if (!param_info->constraints)
622 for (constraints = param_info->constraints; *constraints; ++constraints) {
623 MonoClass *ctr = *constraints;
626 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
627 if (!mono_error_ok (&error)) {
628 mono_error_cleanup (&error);
631 ctr = mono_class_from_mono_type (inflated);
632 mono_metadata_free_type (inflated);
634 /*FIXME maybe we need the same this as verifier_class_is_assignable_from*/
635 if (!mono_class_is_assignable_from_slow (ctr, paramClass))
643 * Return true if @candidate is constraint compatible with @target.
645 * This means that @candidate constraints are a super set of @target constaints
648 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoClass *candidate_param_class, MonoGenericContext *context)
650 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
651 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
652 MonoClass **candidate_class;
653 gboolean class_constraint_satisfied = FALSE;
654 gboolean valuetype_constraint_satisfied = FALSE;
656 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
657 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
659 if (cinfo->constraints) {
660 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
662 MonoType *inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
665 cc = mono_class_from_mono_type (inflated);
666 mono_metadata_free_type (inflated);
668 if (mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
669 class_constraint_satisfied = TRUE;
670 else if (!mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
671 valuetype_constraint_satisfied = TRUE;
674 class_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0;
675 valuetype_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) != 0;
677 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && !class_constraint_satisfied)
679 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && !valuetype_constraint_satisfied)
681 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !((cmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) ||
682 valuetype_constraint_satisfied)) {
687 if (tinfo->constraints) {
688 MonoClass **target_class;
689 for (target_class = tinfo->constraints; *target_class; ++target_class) {
691 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
694 tc = mono_class_from_mono_type (inflated);
695 mono_metadata_free_type (inflated);
698 * A constraint from @target might inflate into @candidate itself and in that case we don't need
699 * check it's constraints since it satisfy the constraint by itself.
701 if (mono_metadata_type_equal (&tc->byval_arg, &candidate_param_class->byval_arg))
704 if (!cinfo->constraints)
707 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
709 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
712 cc = mono_class_from_mono_type (inflated);
713 mono_metadata_free_type (inflated);
715 if (verifier_class_is_assignable_from (tc, cc))
719 * This happens when we have the following:
721 * Bar<K> where K : IFace
722 * Foo<T, U> where T : U where U : IFace
724 * Bar<T> <- T here satisfy K constraint transitively through to U's constraint
727 if (mono_type_is_generic_argument (&cc->byval_arg)) {
728 MonoGenericParam *other_candidate = verifier_get_generic_param_from_type (ctx, &cc->byval_arg);
730 if (mono_generic_param_is_constraint_compatible (ctx, target, other_candidate, cc, context)) {
735 if (!*candidate_class)
742 static MonoGenericParam*
743 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
745 MonoGenericContainer *gc;
746 MonoMethod *method = ctx->method;
749 num = mono_type_get_generic_param_num (type);
751 if (type->type == MONO_TYPE_VAR) {
752 MonoClass *gtd = method->klass;
753 if (gtd->generic_class)
754 gtd = gtd->generic_class->container_class;
755 gc = gtd->generic_container;
757 MonoMethod *gmd = method;
758 if (method->is_inflated)
759 gmd = ((MonoMethodInflated*)method)->declaring;
760 gc = mono_method_get_generic_container (gmd);
764 return mono_generic_container_get_param (gc, num);
770 * Verify if @type is valid for the given @ctx verification context.
771 * this function checks for VAR and MVAR types that are invalid under the current verifier,
772 * This means that it either
775 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
777 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
781 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst, gboolean check_gtd)
784 for (i = 0; i < ginst->type_argc; ++i) {
785 MonoType *type = ginst->type_argv [i];
787 if (!mono_type_is_valid_type_in_context_full (type, ctx->generic_context, TRUE))
794 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
797 for (i = 0; i < ginst->type_argc; ++i) {
798 MonoType *type = ginst->type_argv [i];
799 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
800 MonoGenericParam *candidate;
801 MonoClass *candidate_class;
803 if (!mono_type_is_generic_argument (type))
806 if (!is_valid_type_in_context (ctx, type))
809 candidate = verifier_get_generic_param_from_type (ctx, type);
810 candidate_class = mono_class_from_mono_type (type);
812 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, candidate_class, context))
819 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
821 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
822 MonoGenericInst *ginst = gmethod->context.method_inst;
823 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
824 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
828 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
830 MonoGenericClass *gklass = klass->generic_class;
831 MonoGenericInst *ginst = gklass->context.class_inst;
832 MonoGenericContainer *gc = gklass->container_class->generic_container;
833 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
837 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
839 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
840 MonoGenericInst *ginst = gmethod->context.method_inst;
841 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
842 if (!gc) /*non-generic inflated method - it's part of a generic type */
844 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
846 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
851 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
853 MonoGenericClass *gklass = klass->generic_class;
854 MonoGenericInst *ginst = gklass->context.class_inst;
855 MonoGenericContainer *gc = gklass->container_class->generic_container;
856 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
858 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
862 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
867 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
871 if (!is_valid_type_in_context (ctx, type)) {
872 char *str = mono_type_full_name (type);
873 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
874 str [0] == '!' ? "" : type->type == MONO_TYPE_VAR ? "!" : "!!",
876 type->type == MONO_TYPE_VAR ? "class" : "method",
878 MONO_EXCEPTION_BAD_IMAGE);
883 klass = mono_class_from_mono_type (type);
884 mono_class_init (klass);
885 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
886 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
887 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);
889 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);
890 mono_loader_clear_error ();
894 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
895 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);
899 if (!klass->generic_class)
902 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
903 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);
907 if (!mono_class_repect_method_constraints (ctx, klass)) {
908 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);
915 static verify_result_t
916 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
918 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
919 return RESULT_INVALID;
921 if (!method->is_inflated)
924 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
925 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);
926 return RESULT_INVALID;
929 if (!mono_method_repect_method_constraints (ctx, method)) {
930 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));
931 return RESULT_UNVERIFIABLE;
937 static MonoClassField*
938 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
940 MonoClassField *field;
941 MonoClass *klass = NULL;
943 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
944 field = (MonoClassField *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
945 klass = field ? field->parent : NULL;
947 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
948 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);
952 field = mono_field_from_token_checked (ctx->image, token, &klass, ctx->generic_context, &error);
953 mono_error_cleanup (&error); /*FIXME don't swallow the error */
956 if (!field || !field->parent || !klass) {
957 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);
961 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
964 if (mono_field_get_flags (field) & FIELD_ATTRIBUTE_LITERAL) {
965 char *type_name = mono_type_get_full_name (field->parent);
966 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot reference literal field %s::%s at 0x%04x", type_name, field->name, ctx->ip_offset));
976 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
980 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
981 method = (MonoMethod *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
984 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
985 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);
989 method = mono_get_method_checked (ctx->image, token, NULL, ctx->generic_context, &error);
990 mono_error_cleanup (&error); /* FIXME don't swallow this error */
994 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);
998 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
1005 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
1008 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
1009 MonoClass *klass = (MonoClass *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
1010 type = klass ? &klass->byval_arg : NULL;
1013 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
1014 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
1017 type = mono_type_get_checked (ctx->image, token, ctx->generic_context, &error);
1018 mono_error_cleanup (&error); /*FIXME don't swallow the error */
1021 if (!type || mono_loader_get_last_error ()) {
1022 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);
1023 mono_loader_clear_error ();
1027 if (!mono_type_is_valid_in_context (ctx, type))
1034 /* stack_slot_get_type:
1036 * Returns the stack type of @value. This value includes POINTER_MASK.
1038 * Use this function to checks that account for a managed pointer.
1041 stack_slot_get_type (ILStackDesc *value)
1043 return value->stype & RAW_TYPE_MASK;
1046 /* stack_slot_get_underlying_type:
1048 * Returns the stack type of @value. This value does not include POINTER_MASK.
1050 * Use this function is cases where the fact that the value could be a managed pointer is
1051 * irrelevant. For example, field load doesn't care about this fact of type on stack.
1054 stack_slot_get_underlying_type (ILStackDesc *value)
1056 return value->stype & TYPE_MASK;
1059 /* stack_slot_is_managed_pointer:
1061 * Returns TRUE is @value is a managed pointer.
1064 stack_slot_is_managed_pointer (ILStackDesc *value)
1066 return (value->stype & POINTER_MASK) == POINTER_MASK;
1069 /* stack_slot_is_managed_mutability_pointer:
1071 * Returns TRUE is @value is a managed mutability pointer.
1073 static G_GNUC_UNUSED gboolean
1074 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
1076 return (value->stype & CMMP_MASK) == CMMP_MASK;
1079 /* stack_slot_is_null_literal:
1081 * Returns TRUE is @value is the null literal.
1084 stack_slot_is_null_literal (ILStackDesc *value)
1086 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
1090 /* stack_slot_is_this_pointer:
1092 * Returns TRUE is @value is the this literal
1095 stack_slot_is_this_pointer (ILStackDesc *value)
1097 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
1100 /* stack_slot_is_boxed_value:
1102 * Returns TRUE is @value is a boxed value
1105 stack_slot_is_boxed_value (ILStackDesc *value)
1107 return (value->stype & BOXED_MASK) == BOXED_MASK;
1111 stack_slot_get_name (ILStackDesc *value)
1113 return type_names [value->stype & TYPE_MASK];
1116 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
1117 if (PRED (value)) { \
1119 g_string_append (str, ", "); \
1120 g_string_append (str, NAME); \
1125 stack_slot_stack_type_full_name (ILStackDesc *value)
1127 GString *str = g_string_new ("");
1129 gboolean has_pred = FALSE, first = TRUE;
1131 if ((value->stype & TYPE_MASK) != value->stype) {
1132 g_string_append(str, "[");
1133 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
1134 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
1135 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
1136 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
1137 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1141 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1143 g_string_append(str, "[");
1145 g_string_append (str, ", ");
1146 g_string_append (str, "unboxed");
1151 g_string_append(str, "] ");
1153 g_string_append (str, stack_slot_get_name (value));
1155 g_string_free (str, FALSE);
1160 stack_slot_full_name (ILStackDesc *value)
1162 char *type_name = mono_type_full_name (value->type);
1163 char *stack_name = stack_slot_stack_type_full_name (value);
1164 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1166 g_free (stack_name);
1170 //////////////////////////////////////////////////////////////////
1172 mono_free_verify_list (GSList *list)
1174 MonoVerifyInfoExtended *info;
1177 for (tmp = list; tmp; tmp = tmp->next) {
1178 info = (MonoVerifyInfoExtended *)tmp->data;
1179 g_free (info->info.message);
1182 g_slist_free (list);
1185 #define ADD_ERROR(list,msg) \
1187 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1188 vinfo->info.status = MONO_VERIFY_ERROR; \
1189 vinfo->info.message = (msg); \
1190 (list) = g_slist_prepend ((list), vinfo); \
1193 #define ADD_WARN(list,code,msg) \
1195 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1196 vinfo->info.status = (code); \
1197 vinfo->info.message = (msg); \
1198 (list) = g_slist_prepend ((list), vinfo); \
1201 #define ADD_INVALID(list,msg) \
1203 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1204 vinfo->status = MONO_VERIFY_ERROR; \
1205 vinfo->message = (msg); \
1206 (list) = g_slist_prepend ((list), vinfo); \
1207 /*G_BREAKPOINT ();*/ \
1211 #define CHECK_STACK_UNDERFLOW(num) \
1213 if (cur_stack < (num)) \
1214 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1217 #define CHECK_STACK_OVERFLOW() \
1219 if (cur_stack >= max_stack) \
1220 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1225 in_any_block (MonoMethodHeader *header, guint offset)
1228 MonoExceptionClause *clause;
1230 for (i = 0; i < header->num_clauses; ++i) {
1231 clause = &header->clauses [i];
1232 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1234 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1236 if (MONO_OFFSET_IN_FILTER (clause, offset))
1243 * in_any_exception_block:
1245 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1248 in_any_exception_block (MonoMethodHeader *header, guint offset)
1251 MonoExceptionClause *clause;
1253 for (i = 0; i < header->num_clauses; ++i) {
1254 clause = &header->clauses [i];
1255 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1257 if (MONO_OFFSET_IN_FILTER (clause, offset))
1264 * is_valid_branch_instruction:
1266 * Verify if it's valid to perform a branch from @offset to @target.
1267 * This should be used with br and brtrue/false.
1268 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1269 * The major difference from other similiar functions is that branching into a
1270 * finally/fault block is invalid instead of just unverifiable.
1273 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1276 MonoExceptionClause *clause;
1278 for (i = 0; i < header->num_clauses; ++i) {
1279 clause = &header->clauses [i];
1280 /*branching into a finally block is invalid*/
1281 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1282 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1283 MONO_OFFSET_IN_HANDLER (clause, target))
1286 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1288 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1290 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1297 * is_valid_cmp_branch_instruction:
1299 * Verify if it's valid to perform a branch from @offset to @target.
1300 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1301 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1303 * The major differences from other similar functions are that most errors lead to invalid
1304 * code and only branching out of finally, filter or fault clauses is unverifiable.
1307 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1310 MonoExceptionClause *clause;
1312 for (i = 0; i < header->num_clauses; ++i) {
1313 clause = &header->clauses [i];
1314 /*branching out of a handler or finally*/
1315 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1316 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1317 !MONO_OFFSET_IN_HANDLER (clause, target))
1320 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1322 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1324 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1331 * A leave can't escape a finally block
1334 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1337 MonoExceptionClause *clause;
1339 for (i = 0; i < header->num_clauses; ++i) {
1340 clause = &header->clauses [i];
1341 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1343 if (MONO_OFFSET_IN_FILTER (clause, offset))
1350 * A rethrow can't happen outside of a catch handler.
1353 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1356 MonoExceptionClause *clause;
1358 for (i = 0; i < header->num_clauses; ++i) {
1359 clause = &header->clauses [i];
1360 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1367 * An endfinally can't happen outside of a finally/fault handler.
1370 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1373 MonoExceptionClause *clause;
1375 for (i = 0; i < header->num_clauses; ++i) {
1376 clause = &header->clauses [i];
1377 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1385 * An endfilter can only happens inside a filter clause.
1386 * In non-strict mode filter is allowed inside the handler clause too
1388 static MonoExceptionClause *
1389 is_correct_endfilter (VerifyContext *ctx, guint offset)
1392 MonoExceptionClause *clause;
1394 for (i = 0; i < ctx->header->num_clauses; ++i) {
1395 clause = &ctx->header->clauses [i];
1396 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1398 if (MONO_OFFSET_IN_FILTER (clause, offset))
1400 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1408 * Non-strict endfilter can happens inside a try block or any handler block
1411 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1414 MonoExceptionClause *clause;
1416 for (i = 0; i < ctx->header->num_clauses; ++i) {
1417 clause = &ctx->header->clauses [i];
1418 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1425 is_valid_bool_arg (ILStackDesc *arg)
1427 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1431 switch (stack_slot_get_underlying_type (arg)) {
1434 case TYPE_NATIVE_INT:
1438 g_assert (arg->type);
1439 switch (arg->type->type) {
1440 case MONO_TYPE_CLASS:
1441 case MONO_TYPE_STRING:
1442 case MONO_TYPE_OBJECT:
1443 case MONO_TYPE_SZARRAY:
1444 case MONO_TYPE_ARRAY:
1445 case MONO_TYPE_FNPTR:
1448 case MONO_TYPE_GENERICINST:
1449 /*We need to check if the container class
1450 * of the generic type is a valuetype, iow:
1451 * is it a "class Foo<T>" or a "struct Foo<T>"?
1453 return !arg->type->data.generic_class->container_class->valuetype;
1463 /*Type manipulation helper*/
1465 /*Returns the byref version of the supplied MonoType*/
1467 mono_type_get_type_byref (MonoType *type)
1471 return &mono_class_from_mono_type (type)->this_arg;
1475 /*Returns the byval version of the supplied MonoType*/
1477 mono_type_get_type_byval (MonoType *type)
1481 return &mono_class_from_mono_type (type)->byval_arg;
1485 mono_type_from_stack_slot (ILStackDesc *slot)
1487 if (stack_slot_is_managed_pointer (slot))
1488 return mono_type_get_type_byref (slot->type);
1492 /*Stack manipulation code*/
1495 ensure_stack_size (ILCodeDesc *stack, int required)
1500 if (required < stack->max_size)
1503 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1504 new_size = MAX (8, MAX (required, stack->max_size * 2));
1506 g_assert (new_size >= stack->size);
1507 g_assert (new_size >= required);
1509 tmp = g_new0 (ILStackDesc, new_size);
1510 _MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1514 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1515 g_free (stack->stack);
1516 _MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1520 stack->max_size = new_size;
1524 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1526 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1528 state->size = state->max_size = 0;
1529 state->flags |= IL_CODE_FLAG_STACK_INITED;
1533 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1535 ensure_stack_size (to, from->size);
1536 to->size = from->size;
1538 /*stack copy happens at merge points, which have small stacks*/
1540 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1544 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1546 to->stype = from->stype;
1547 to->type = from->type;
1548 to->method = from->method;
1552 check_underflow (VerifyContext *ctx, int size)
1554 if (ctx->eval.size < size) {
1555 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1562 check_overflow (VerifyContext *ctx)
1564 if (ctx->eval.size >= ctx->max_stack) {
1565 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1571 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1573 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1575 if (stack_slot_get_type (value) == TYPE_PTR) {
1576 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1582 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1584 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1586 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1587 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1593 static ILStackDesc *
1594 stack_push (VerifyContext *ctx)
1596 g_assert (ctx->eval.size < ctx->max_stack);
1597 g_assert (ctx->eval.size <= ctx->eval.max_size);
1599 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1601 return & ctx->eval.stack [ctx->eval.size++];
1604 static ILStackDesc *
1605 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1607 ILStackDesc *top = stack_push (ctx);
1613 static ILStackDesc *
1614 stack_pop (VerifyContext *ctx)
1617 g_assert (ctx->eval.size > 0);
1618 ret = ctx->eval.stack + --ctx->eval.size;
1619 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1620 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1624 /* This function allows to safely pop an unititialized this ptr from
1625 * the eval stack without marking the method as unverifiable.
1627 static ILStackDesc *
1628 stack_pop_safe (VerifyContext *ctx)
1630 g_assert (ctx->eval.size > 0);
1631 return ctx->eval.stack + --ctx->eval.size;
1634 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1636 stack_peek (VerifyContext *ctx, int distance)
1638 g_assert (ctx->eval.size - distance > 0);
1639 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1642 static ILStackDesc *
1643 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1645 ILStackDesc *top = stack_push (ctx);
1646 copy_stack_value (top, value);
1650 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1652 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1655 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1660 if (!(type = verifier_load_type (ctx, token, opcode)))
1663 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1664 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1668 if (type->type == MONO_TYPE_VOID) {
1669 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1673 if (type->type == MONO_TYPE_TYPEDBYREF)
1674 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1676 if (!(klass = mono_class_from_mono_type (type)))
1677 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1679 if (klass->generic_container && type->type != MONO_TYPE_GENERICINST)
1680 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));
1682 check_unverifiable_type (ctx, type);
1687 /*operation result tables */
1689 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1690 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1691 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1692 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1693 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1694 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1695 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1698 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1699 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1700 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1701 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1702 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1703 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1704 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1707 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1708 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1709 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1710 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1711 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1712 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1713 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1716 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1717 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1718 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1719 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1720 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1721 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1722 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1725 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1726 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1727 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1728 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1729 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1730 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1731 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1734 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1735 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1736 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1737 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1738 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1739 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1740 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1743 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1744 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1745 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1746 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1747 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1748 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1749 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1752 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1753 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1754 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1755 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1756 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1757 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1758 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1761 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1762 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1763 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1764 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1765 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1766 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1767 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1770 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1771 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1772 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1773 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1774 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1775 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1776 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1779 #ifdef MONO_VERIFIER_DEBUG
1783 dump_stack_value (ILStackDesc *value)
1785 printf ("[(%x)(%x)", value->type->type, value->stype);
1787 if (stack_slot_is_this_pointer (value))
1790 if (stack_slot_is_boxed_value (value))
1791 printf ("[boxed] ");
1793 if (stack_slot_is_null_literal (value))
1796 if (stack_slot_is_managed_mutability_pointer (value))
1797 printf ("Controled Mutability MP: ");
1799 if (stack_slot_is_managed_pointer (value))
1800 printf ("Managed Pointer to: ");
1802 switch (stack_slot_get_underlying_type (value)) {
1804 printf ("invalid type]");
1812 case TYPE_NATIVE_INT:
1813 printf ("native int]");
1816 printf ("float64]");
1819 printf ("unmanaged pointer]");
1822 switch (value->type->type) {
1823 case MONO_TYPE_CLASS:
1824 case MONO_TYPE_VALUETYPE:
1825 printf ("complex] (%s)", value->type->data.klass->name);
1827 case MONO_TYPE_STRING:
1828 printf ("complex] (string)");
1830 case MONO_TYPE_OBJECT:
1831 printf ("complex] (object)");
1833 case MONO_TYPE_SZARRAY:
1834 printf ("complex] (%s [])", value->type->data.klass->name);
1836 case MONO_TYPE_ARRAY:
1837 printf ("complex] (%s [%d %d %d])",
1838 value->type->data.array->eklass->name,
1839 value->type->data.array->rank,
1840 value->type->data.array->numsizes,
1841 value->type->data.array->numlobounds);
1843 case MONO_TYPE_GENERICINST:
1844 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1847 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1849 case MONO_TYPE_MVAR:
1850 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1853 //should be a boxed value
1854 char * name = mono_type_full_name (value->type);
1855 printf ("complex] %s", name);
1861 printf ("unknown stack %x type]\n", value->stype);
1862 g_assert_not_reached ();
1867 dump_stack_state (ILCodeDesc *state)
1871 printf ("(%d) ", state->size);
1872 for (i = 0; i < state->size; ++i)
1873 dump_stack_value (state->stack + i);
1878 /*Returns TRUE if candidate array type can be assigned to target.
1879 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1882 is_array_type_compatible (MonoType *target, MonoType *candidate)
1884 MonoArrayType *left = target->data.array;
1885 MonoArrayType *right = candidate->data.array;
1887 g_assert (target->type == MONO_TYPE_ARRAY);
1888 g_assert (candidate->type == MONO_TYPE_ARRAY);
1890 if (left->rank != right->rank)
1893 return verifier_class_is_assignable_from (left->eklass, right->eklass);
1897 get_stack_type (MonoType *type)
1900 int type_kind = type->type;
1902 mask = POINTER_MASK;
1903 /*TODO handle CMMP_MASK */
1906 switch (type_kind) {
1909 case MONO_TYPE_BOOLEAN:
1912 case MONO_TYPE_CHAR:
1915 return TYPE_I4 | mask;
1919 return TYPE_NATIVE_INT | mask;
1921 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1922 case MONO_TYPE_FNPTR:
1924 case MONO_TYPE_TYPEDBYREF:
1925 return TYPE_PTR | mask;
1928 case MONO_TYPE_MVAR:
1930 case MONO_TYPE_CLASS:
1931 case MONO_TYPE_STRING:
1932 case MONO_TYPE_OBJECT:
1933 case MONO_TYPE_SZARRAY:
1934 case MONO_TYPE_ARRAY:
1935 return TYPE_COMPLEX | mask;
1939 return TYPE_I8 | mask;
1943 return TYPE_R8 | mask;
1945 case MONO_TYPE_GENERICINST:
1946 case MONO_TYPE_VALUETYPE:
1947 if (mono_type_is_enum_type (type)) {
1948 type = mono_type_get_underlying_type_any (type);
1951 type_kind = type->type;
1954 return TYPE_COMPLEX | mask;
1962 /* convert MonoType to ILStackDesc format (stype) */
1964 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1967 int type_kind = type->type;
1969 if (type->byref || take_addr)
1970 mask = POINTER_MASK;
1971 /* TODO handle CMMP_MASK */
1976 switch (type_kind) {
1979 case MONO_TYPE_BOOLEAN:
1982 case MONO_TYPE_CHAR:
1985 stack->stype = TYPE_I4 | mask;
1989 stack->stype = TYPE_NATIVE_INT | mask;
1992 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1993 case MONO_TYPE_FNPTR:
1995 case MONO_TYPE_TYPEDBYREF:
1996 stack->stype = TYPE_PTR | mask;
1999 case MONO_TYPE_CLASS:
2000 case MONO_TYPE_STRING:
2001 case MONO_TYPE_OBJECT:
2002 case MONO_TYPE_SZARRAY:
2003 case MONO_TYPE_ARRAY:
2006 case MONO_TYPE_MVAR:
2007 stack->stype = TYPE_COMPLEX | mask;
2012 stack->stype = TYPE_I8 | mask;
2016 stack->stype = TYPE_R8 | mask;
2018 case MONO_TYPE_GENERICINST:
2019 case MONO_TYPE_VALUETYPE:
2020 if (mono_type_is_enum_type (type)) {
2021 MonoType *utype = mono_type_get_underlying_type_any (type);
2023 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
2027 type_kind = type->type;
2030 stack->stype = TYPE_COMPLEX | mask;
2034 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
2035 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
2042 * init_stack_with_value_at_exception_boundary:
2044 * Initialize the stack and push a given type.
2045 * The instruction is marked as been on the exception boundary.
2048 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
2051 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
2053 if (!mono_error_ok (&error)) {
2054 char *name = mono_type_get_full_name (klass);
2055 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
2057 mono_error_cleanup (&error);
2061 if (!ctx->max_stack) {
2062 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
2066 stack_init (ctx, code);
2067 ensure_stack_size (code, 1);
2068 set_stack_value (ctx, code->stack, type, FALSE);
2069 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
2071 code->flags |= IL_CODE_FLAG_WAS_TARGET;
2072 if (mono_type_is_generic_argument (type))
2073 code->stack->stype |= BOXED_MASK;
2077 get_ienumerable_class (void)
2079 static MonoClass* generic_ienumerable_class = NULL;
2081 if (generic_ienumerable_class == NULL)
2082 generic_ienumerable_class = mono_class_from_name (mono_defaults.corlib,
2083 "System.Collections.Generic", "IEnumerable`1");
2084 return generic_ienumerable_class;
2088 get_icollection_class (void)
2090 static MonoClass* generic_icollection_class = NULL;
2092 if (generic_icollection_class == NULL)
2093 generic_icollection_class = mono_class_from_name (mono_defaults.corlib,
2094 "System.Collections.Generic", "ICollection`1");
2095 return generic_icollection_class;
2099 get_ireadonlylist_class (void)
2101 static MonoClass* generic_ireadonlylist_class = NULL;
2103 if (generic_ireadonlylist_class == NULL)
2104 generic_ireadonlylist_class = mono_class_from_name (mono_defaults.corlib,
2105 "System.Collections.Generic", "IReadOnlyList`1");
2106 return generic_ireadonlylist_class;
2110 get_ireadonlycollection_class (void)
2112 static MonoClass* generic_ireadonlycollection_class = NULL;
2114 if (generic_ireadonlycollection_class == NULL)
2115 generic_ireadonlycollection_class = mono_class_from_name (mono_defaults.corlib,
2116 "System.Collections.Generic", "IReadOnlyCollection`1");
2117 return generic_ireadonlycollection_class;
2121 inflate_class_one_arg (MonoClass *gtype, MonoClass *arg0)
2124 args [0] = &arg0->byval_arg;
2126 return mono_class_bind_generic_parameters (gtype, 1, args, FALSE);
2130 verifier_inflate_and_check_compat (MonoClass *target, MonoClass *gtd, MonoClass *arg)
2133 if (!(tmp = inflate_class_one_arg (gtd, arg)))
2135 if (mono_class_is_variant_compatible (target, tmp, TRUE))
2141 verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate)
2143 MonoClass *iface_gtd;
2145 if (target == candidate)
2148 if (mono_class_has_variant_generic_params (target)) {
2149 if (MONO_CLASS_IS_INTERFACE (target)) {
2150 if (MONO_CLASS_IS_INTERFACE (candidate) && mono_class_is_variant_compatible (target, candidate, TRUE))
2153 if (candidate->rank == 1) {
2154 if (verifier_inflate_and_check_compat (target, mono_defaults.generic_ilist_class, candidate->element_class))
2156 if (verifier_inflate_and_check_compat (target, get_icollection_class (), candidate->element_class))
2158 if (verifier_inflate_and_check_compat (target, get_ienumerable_class (), candidate->element_class))
2160 if (verifier_inflate_and_check_compat (target, get_ireadonlylist_class (), candidate->element_class))
2162 if (verifier_inflate_and_check_compat (target, get_ireadonlycollection_class (), candidate->element_class))
2167 while (candidate && candidate != mono_defaults.object_class) {
2168 mono_class_setup_interfaces (candidate, &error);
2169 if (!mono_error_ok (&error)) {
2170 mono_error_cleanup (&error);
2174 /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
2175 for (i = 0; i < candidate->interface_offsets_count; ++i) {
2176 MonoClass *iface = candidate->interfaces_packed [i];
2177 if (mono_class_is_variant_compatible (target, iface, TRUE))
2181 for (i = 0; i < candidate->interface_count; ++i) {
2182 MonoClass *iface = candidate->interfaces [i];
2183 if (mono_class_is_variant_compatible (target, iface, TRUE))
2186 candidate = candidate->parent;
2189 } else if (target->delegate) {
2190 if (mono_class_is_variant_compatible (target, candidate, TRUE))
2196 if (mono_class_is_assignable_from (target, candidate))
2199 if (!MONO_CLASS_IS_INTERFACE (target) || !target->generic_class || candidate->rank != 1)
2202 iface_gtd = target->generic_class->container_class;
2203 if (iface_gtd != mono_defaults.generic_ilist_class && iface_gtd != get_icollection_class () && iface_gtd != get_ienumerable_class ())
2206 target = mono_class_from_mono_type (target->generic_class->context.class_inst->type_argv [0]);
2207 candidate = candidate->element_class;
2212 /*Verify if type 'candidate' can be stored in type 'target'.
2214 * If strict, check for the underlying type and not the verification stack types
2217 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
2219 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2220 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2222 MonoType *original_candidate = candidate;
2223 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
2225 /*only one is byref */
2226 if (candidate->byref ^ target->byref) {
2227 /* converting from native int to byref*/
2228 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2229 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2234 strict |= target->byref;
2235 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2236 candidate = mono_type_get_underlying_type_any (candidate);
2239 switch (target->type) {
2240 case MONO_TYPE_VOID:
2241 return candidate->type == MONO_TYPE_VOID;
2244 case MONO_TYPE_BOOLEAN:
2246 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2249 case MONO_TYPE_CHAR:
2251 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2253 case MONO_TYPE_U4: {
2254 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2255 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2257 return is_native_int || is_int4;
2258 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2263 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2268 return candidate->type == target->type;
2269 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2273 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2274 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2276 return is_native_int || is_int4;
2277 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2281 if (candidate->type != MONO_TYPE_PTR)
2283 /* check the underlying type */
2284 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2286 case MONO_TYPE_FNPTR: {
2287 MonoMethodSignature *left, *right;
2288 if (candidate->type != MONO_TYPE_FNPTR)
2291 left = mono_type_get_signature (target);
2292 right = mono_type_get_signature (candidate);
2293 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2296 case MONO_TYPE_GENERICINST: {
2297 MonoClass *target_klass;
2298 MonoClass *candidate_klass;
2299 if (mono_type_is_enum_type (target)) {
2300 target = mono_type_get_underlying_type_any (target);
2306 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2307 * to take boxing status into account.
2309 if (mono_type_is_generic_argument (original_candidate))
2312 target_klass = mono_class_from_mono_type (target);
2313 candidate_klass = mono_class_from_mono_type (candidate);
2314 if (mono_class_is_nullable (target_klass)) {
2315 if (!mono_class_is_nullable (candidate_klass))
2317 return target_klass == candidate_klass;
2319 return verifier_class_is_assignable_from (target_klass, candidate_klass);
2322 case MONO_TYPE_STRING:
2323 return candidate->type == MONO_TYPE_STRING;
2325 case MONO_TYPE_CLASS:
2327 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2328 * to take boxing status into account.
2330 if (mono_type_is_generic_argument (original_candidate))
2333 if (candidate->type == MONO_TYPE_VALUETYPE)
2336 /* If candidate is an enum it should return true for System.Enum and supertypes.
2337 * That's why here we use the original type and not the underlying type.
2339 return verifier_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2341 case MONO_TYPE_OBJECT:
2342 return MONO_TYPE_IS_REFERENCE (candidate);
2344 case MONO_TYPE_SZARRAY: {
2347 if (candidate->type != MONO_TYPE_SZARRAY)
2350 left = mono_class_from_mono_type (target);
2351 right = mono_class_from_mono_type (candidate);
2353 return verifier_class_is_assignable_from (left, right);
2356 case MONO_TYPE_ARRAY:
2357 if (candidate->type != MONO_TYPE_ARRAY)
2359 return is_array_type_compatible (target, candidate);
2361 case MONO_TYPE_TYPEDBYREF:
2362 return candidate->type == MONO_TYPE_TYPEDBYREF;
2364 case MONO_TYPE_VALUETYPE: {
2365 MonoClass *target_klass;
2366 MonoClass *candidate_klass;
2368 if (candidate->type == MONO_TYPE_CLASS)
2371 target_klass = mono_class_from_mono_type (target);
2372 candidate_klass = mono_class_from_mono_type (candidate);
2373 if (target_klass == candidate_klass)
2375 if (mono_type_is_enum_type (target)) {
2376 target = mono_type_get_underlying_type_any (target);
2385 if (candidate->type != MONO_TYPE_VAR)
2387 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2389 case MONO_TYPE_MVAR:
2390 if (candidate->type != MONO_TYPE_MVAR)
2392 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2395 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2396 g_assert_not_reached ();
2405 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2407 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2411 * Returns the generic param bound to the context been verified.
2414 static MonoGenericParam*
2415 get_generic_param (VerifyContext *ctx, MonoType *param)
2417 guint16 param_num = mono_type_get_generic_param_num (param);
2418 if (param->type == MONO_TYPE_VAR) {
2419 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2420 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2423 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2426 /*param must be a MVAR */
2427 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2428 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2431 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2436 recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
2438 MonoType *constraint_type = &constraint_class->byval_arg;
2439 if (recursion_level <= 0)
2442 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
2445 if (mono_type_is_generic_argument (constraint_type)) {
2446 MonoGenericParam *param = get_generic_param (ctx, constraint_type);
2450 for (klass = mono_generic_param_info (param)->constraints; klass && *klass; ++klass) {
2451 if (recursive_boxed_constraint_type_check (ctx, type, *klass, recursion_level - 1))
2459 * is_compatible_boxed_valuetype:
2461 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2463 * @type The source type. It it tested to be of the proper type.
2464 * @candidate type of the boxed valuetype.
2465 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2466 * @strict if TRUE candidate must be boxed compatible to the target type
2470 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2472 if (!stack_slot_is_boxed_value (stack))
2474 if (type->byref || candidate->byref)
2477 if (mono_type_is_generic_argument (candidate)) {
2478 MonoGenericParam *param = get_generic_param (ctx, candidate);
2483 for (klass = mono_generic_param_info (param)->constraints; klass && *klass; ++klass) {
2484 /*256 should be enough since there can't be more than 255 generic arguments.*/
2485 if (recursive_boxed_constraint_type_check (ctx, type, *klass, 256))
2490 if (mono_type_is_generic_argument (type))
2496 return MONO_TYPE_IS_REFERENCE (type) && verifier_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2500 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2502 MonoType *candidate = mono_type_from_stack_slot (stack);
2503 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2506 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2509 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2512 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2516 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2518 /* Handle how Roslyn emit fixed statements by encoding it as byref */
2519 if (type->byref && candidate->byref && (type->type == MONO_TYPE_I) && !mono_type_is_reference (candidate)) {
2520 if (!IS_STRICT_MODE (ctx))
2524 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2528 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2530 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2534 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2536 if (candidate->byref ^ target->byref)
2539 switch (target->type) {
2540 case MONO_TYPE_VOID:
2543 case MONO_TYPE_BOOLEAN:
2546 case MONO_TYPE_CHAR:
2555 case MONO_TYPE_STRING:
2556 case MONO_TYPE_TYPEDBYREF:
2557 return candidate->type == target->type;
2560 if (candidate->type != MONO_TYPE_PTR)
2562 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2564 case MONO_TYPE_FNPTR:
2565 if (candidate->type != MONO_TYPE_FNPTR)
2567 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2569 case MONO_TYPE_GENERICINST: {
2570 MonoClass *target_klass;
2571 MonoClass *candidate_klass;
2572 target_klass = mono_class_from_mono_type (target);
2573 candidate_klass = mono_class_from_mono_type (candidate);
2574 /*FIXME handle nullables and enum*/
2575 return verifier_class_is_assignable_from (target_klass, candidate_klass);
2577 case MONO_TYPE_OBJECT:
2578 return MONO_TYPE_IS_REFERENCE (candidate);
2580 case MONO_TYPE_CLASS:
2581 return verifier_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2583 case MONO_TYPE_SZARRAY:
2584 if (candidate->type != MONO_TYPE_SZARRAY)
2586 return verifier_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2588 case MONO_TYPE_ARRAY:
2589 if (candidate->type != MONO_TYPE_ARRAY)
2591 return is_array_type_compatible (target, candidate);
2593 case MONO_TYPE_VALUETYPE:
2594 /*FIXME handle nullables and enum*/
2595 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2598 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2601 case MONO_TYPE_MVAR:
2602 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2606 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2607 g_assert_not_reached ();
2613 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2615 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2618 return mono_delegate_type_equal (method, delegate);
2622 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2624 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2627 return mono_delegate_type_equal (delegate, method);
2631 * mono_delegate_signature_equal:
2633 * Compare two signatures in the way expected by delegates.
2635 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2637 * FIXME can this function be eliminated and proper metadata functionality be used?
2640 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2643 int method_offset = is_static_ldftn ? 1 : 0;
2645 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2648 if (delegate_sig->call_convention != method_sig->call_convention)
2651 for (i = 0; i < delegate_sig->param_count; i++) {
2652 MonoType *p1 = delegate_sig->params [i];
2653 MonoType *p2 = method_sig->params [i + method_offset];
2655 if (!mono_delegate_param_equal (p1, p2))
2659 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2666 mono_verifier_is_signature_compatible (MonoMethodSignature *target, MonoMethodSignature *candidate)
2668 return mono_delegate_signature_equal (target, candidate, FALSE);
2672 * verify_ldftn_delegate:
2674 * Verify properties of ldftn based delegates.
2677 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2679 MonoMethod *method = funptr->method;
2681 /*ldftn non-final virtuals only allowed if method is not static,
2682 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2683 * This rules doesn't apply if the object on stack is a boxed valuetype.
2685 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2686 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2687 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2688 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2690 /*current method must not be static*/
2691 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2692 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2694 /*value is the this pointer, loaded using ldarg.0 */
2695 if (!stack_slot_is_this_pointer (value))
2696 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));
2698 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2703 * verify_delegate_compatibility:
2705 * Verify delegate creation sequence.
2709 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2711 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2712 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2714 MonoMethod *invoke, *method;
2715 const guint8 *ip = ctx->header->code;
2716 guint32 ip_offset = ctx->ip_offset;
2717 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2719 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2720 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2724 invoke = mono_get_delegate_invoke (delegate);
2725 method = funptr->method;
2727 if (!method || !mono_method_signature (method)) {
2728 char *name = mono_type_get_full_name (delegate);
2729 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2734 if (!invoke || !mono_method_signature (invoke)) {
2735 char *name = mono_type_get_full_name (delegate);
2736 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2741 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2743 if (is_static_ldftn)
2744 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2746 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2747 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2748 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2749 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));
2751 g_free (invoke_sig);
2755 * Delegate code sequences:
2761 * [-6] ldvirtftn token
2765 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2766 verify_ldftn_delegate (ctx, delegate, value, funptr);
2767 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2768 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2770 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2772 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2775 if (is_first_arg_bound) {
2776 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2777 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2779 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2780 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2781 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2783 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2784 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2788 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2789 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2791 #undef IS_VALID_OPCODE
2792 #undef IS_LOAD_FUN_PTR
2795 /* implement the opcode checks*/
2797 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2801 if (arg >= ctx->max_args) {
2803 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2805 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2806 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2807 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2809 } else if (check_overflow (ctx)) {
2810 /*We must let the value be pushed, otherwise we would get an underflow error*/
2811 check_unverifiable_type (ctx, ctx->params [arg]);
2812 if (ctx->params [arg]->byref && take_addr)
2813 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2814 top = stack_push (ctx);
2815 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2818 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2820 ctx->has_this_store = TRUE;
2822 top->stype |= THIS_POINTER_MASK;
2823 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2824 top->stype |= UNINIT_THIS_MASK;
2830 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2832 if (arg >= ctx->num_locals) {
2833 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2834 } else if (check_overflow (ctx)) {
2835 /*We must let the value be pushed, otherwise we would get an underflow error*/
2836 check_unverifiable_type (ctx, ctx->locals [arg]);
2837 if (ctx->locals [arg]->byref && take_addr)
2838 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2840 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2845 store_arg (VerifyContext *ctx, guint32 arg)
2849 if (arg >= ctx->max_args) {
2850 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2851 if (check_underflow (ctx, 1))
2856 if (check_underflow (ctx, 1)) {
2857 value = stack_pop (ctx);
2858 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2859 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2862 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2863 ctx->has_this_store = 1;
2867 store_local (VerifyContext *ctx, guint32 arg)
2870 if (arg >= ctx->num_locals) {
2871 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2875 /*TODO verify definite assigment */
2876 if (!check_underflow (ctx, 1))
2879 value = stack_pop (ctx);
2880 if (ctx->locals [arg]->byref && stack_slot_is_managed_mutability_pointer (value))
2881 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));
2883 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2884 char *expected = mono_type_full_name (ctx->locals [arg]);
2885 char *found = stack_slot_full_name (value);
2886 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2896 /*FIXME add and sub needs special care here*/
2898 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2900 ILStackDesc *a, *b, *top;
2901 int idxa, idxb, complexMerge = 0;
2904 if (!check_underflow (ctx, 2))
2906 b = stack_pop (ctx);
2907 a = stack_pop (ctx);
2909 idxa = stack_slot_get_underlying_type (a);
2910 if (stack_slot_is_managed_pointer (a)) {
2915 idxb = stack_slot_get_underlying_type (b);
2916 if (stack_slot_is_managed_pointer (b)) {
2923 res = table [idxa][idxb];
2925 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2926 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2928 top = stack_push (ctx);
2929 if (res == TYPE_INV) {
2930 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)));
2931 copy_stack_value (top, a);
2935 if (res & NON_VERIFIABLE_RESULT) {
2936 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)));
2938 res = res & ~NON_VERIFIABLE_RESULT;
2941 if (complexMerge && res == TYPE_PTR) {
2942 if (complexMerge == 1)
2943 copy_stack_value (top, a);
2944 else if (complexMerge == 2)
2945 copy_stack_value (top, b);
2947 * There is no need to merge the type of two pointers.
2948 * The only valid operation is subtraction, that returns a native
2949 * int as result and can be used with any 2 pointer kinds.
2950 * This is valid acording to Patition III 1.1.4
2959 do_boolean_branch_op (VerifyContext *ctx, int delta)
2961 int target = ctx->ip_offset + delta;
2964 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2966 if (target < 0 || target >= ctx->code_size) {
2967 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2971 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2973 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2976 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2980 ctx->target = target;
2982 if (!check_underflow (ctx, 1))
2985 top = stack_pop (ctx);
2986 if (!is_valid_bool_arg (top))
2987 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));
2989 check_unmanaged_pointer (ctx, top);
2993 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2995 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2999 stack_slot_is_reference_value (ILStackDesc *slot)
3001 return stack_slot_get_type (slot) == TYPE_COMPLEX && (MONO_TYPE_IS_REFERENCE (slot->type) || stack_slot_is_boxed_value (slot));
3005 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
3010 int target = ctx->ip_offset + delta;
3012 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3014 if (target < 0 || target >= ctx->code_size) {
3015 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3019 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3020 case 1: /*FIXME use constants and not magic numbers.*/
3021 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3024 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3028 ctx->target = target;
3030 if (!check_underflow (ctx, 2))
3033 b = stack_pop (ctx);
3034 a = stack_pop (ctx);
3036 idxa = stack_slot_get_underlying_type (a);
3037 if (stack_slot_is_managed_pointer (a))
3040 idxb = stack_slot_get_underlying_type (b);
3041 if (stack_slot_is_managed_pointer (b))
3044 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3049 res = table [idxa][idxb];
3052 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
3053 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3055 if (res == TYPE_INV) {
3056 CODE_NOT_VERIFIABLE (ctx,
3057 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));
3058 } else if (res & NON_VERIFIABLE_RESULT) {
3059 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));
3060 res = res & ~NON_VERIFIABLE_RESULT;
3065 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
3071 if (!check_underflow (ctx, 2))
3073 b = stack_pop (ctx);
3074 a = stack_pop (ctx);
3076 if (opcode == CEE_CGT_UN) {
3077 if ((stack_slot_is_reference_value (a) && stack_slot_is_null_literal (b)) ||
3078 (stack_slot_is_reference_value (b) && stack_slot_is_null_literal (a))) {
3079 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3084 idxa = stack_slot_get_underlying_type (a);
3085 if (stack_slot_is_managed_pointer (a))
3088 idxb = stack_slot_get_underlying_type (b);
3089 if (stack_slot_is_managed_pointer (b))
3092 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3097 res = table [idxa][idxb];
3100 if(res == TYPE_INV) {
3101 char *left_type = stack_slot_full_name (a);
3102 char *right_type = stack_slot_full_name (b);
3103 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));
3105 g_free (right_type);
3106 } else if (res & NON_VERIFIABLE_RESULT) {
3107 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));
3108 res = res & ~NON_VERIFIABLE_RESULT;
3110 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3114 do_ret (VerifyContext *ctx)
3116 MonoType *ret = ctx->signature->ret;
3117 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3118 if (ret->type != MONO_TYPE_VOID) {
3120 if (!check_underflow (ctx, 1))
3123 top = stack_pop(ctx);
3125 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3126 char *ret_type = mono_type_full_name (ctx->signature->ret);
3127 char *stack_type = stack_slot_full_name (top);
3128 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));
3129 g_free (stack_type);
3134 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3135 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3138 if (ctx->eval.size > 0) {
3139 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3141 if (in_any_block (ctx->header, ctx->ip_offset))
3142 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3146 * 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.
3147 * This is illegal but mono_get_method_full decoded it.
3148 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3151 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual_)
3155 MonoMethodSignature *sig;
3158 gboolean virt_check_this = FALSE;
3159 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3161 if (!(method = verifier_load_method (ctx, method_token, virtual_ ? "callvirt" : "call")))
3165 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3167 if (method->klass->valuetype) // && !constrained ???
3168 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3170 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3171 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3174 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3175 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3177 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
3178 virt_check_this = TRUE;
3179 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3183 if (!(sig = mono_method_get_signature_checked (method, ctx->image, method_token, ctx->generic_context, &error))) {
3184 mono_error_cleanup (&error);
3185 sig = mono_method_get_signature_checked (method, ctx->image, method_token, NULL, &error);
3189 char *name = mono_type_get_full_name (method->klass);
3190 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)));
3191 mono_error_cleanup (&error);
3196 param_count = sig->param_count + sig->hasthis;
3197 if (!check_underflow (ctx, param_count))
3200 for (i = sig->param_count - 1; i >= 0; --i) {
3201 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3202 value = stack_pop (ctx);
3203 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
3204 char *stack_name = stack_slot_full_name (value);
3205 char *sig_name = mono_type_full_name (sig->params [i]);
3206 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));
3207 g_free (stack_name);
3211 if (stack_slot_is_managed_mutability_pointer (value))
3212 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));
3214 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3215 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));
3221 MonoType *type = &method->klass->byval_arg;
3224 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3225 if (IS_STRICT_MODE (ctx) && !mono_method_is_constructor (ctx->method))
3226 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3227 if (IS_STRICT_MODE (ctx) && method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3228 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));
3230 ctx->super_ctor_called = TRUE;
3231 value = stack_pop_safe (ctx);
3232 if (IS_STRICT_MODE (ctx) && (value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3233 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3235 value = stack_pop (ctx);
3238 copy_stack_value (©, value);
3239 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3240 //Other parts of the code suffer from the same issue of
3241 copy.type = mono_type_get_type_byval (copy.type);
3242 copy.stype &= ~POINTER_MASK;
3244 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3245 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));
3247 if (constrained && virtual_) {
3248 if (!stack_slot_is_managed_pointer (value))
3249 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3250 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), mono_type_get_underlying_type (ctx->constrained_type), TRUE))
3251 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3252 copy.stype |= BOXED_MASK;
3253 copy.type = ctx->constrained_type;
3255 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3256 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));
3258 if (!virtual_ && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3259 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3261 if (virtual_ && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3262 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3264 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3265 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));
3267 if (!verify_stack_type_compatibility (ctx, type, ©)) {
3268 char *expected = mono_type_full_name (type);
3269 char *effective = stack_slot_full_name (©);
3270 char *method_name = mono_method_full_name (method, TRUE);
3271 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",
3272 expected, effective, method_name, ctx->ip_offset));
3273 g_free (method_name);
3278 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
3279 char *name = mono_method_full_name (method, TRUE);
3280 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3284 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3285 char *name = mono_method_full_name (method, TRUE);
3286 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3290 if (sig->ret->type != MONO_TYPE_VOID) {
3291 if (!mono_type_is_valid_in_context (ctx, sig->ret))
3294 if (check_overflow (ctx)) {
3295 value = stack_push (ctx);
3296 set_stack_value (ctx, value, sig->ret, FALSE);
3297 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3298 ctx->prefix_set &= ~PREFIX_READONLY;
3299 value->stype |= CMMP_MASK;
3304 if ((ctx->prefix_set & PREFIX_TAIL)) {
3305 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3306 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3307 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3308 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3314 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3316 MonoClassField *field;
3318 if (!check_overflow (ctx))
3321 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3323 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3326 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3327 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3330 /*taking the address of initonly field only works from the static constructor */
3331 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3332 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3333 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3335 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3336 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3338 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3342 do_store_static_field (VerifyContext *ctx, int token) {
3343 MonoClassField *field;
3346 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3348 if (!check_underflow (ctx, 1))
3351 value = stack_pop (ctx);
3353 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3356 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3357 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3361 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3362 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3366 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3367 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3369 if (!verify_stack_type_compatibility (ctx, field->type, value)) {
3370 char *stack_name = stack_slot_full_name (value);
3371 char *field_name = mono_type_full_name (field->type);
3372 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3373 field_name, stack_name, ctx->ip_offset));
3374 g_free (field_name);
3375 g_free (stack_name);
3380 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3382 MonoClassField *field;
3384 gboolean is_pointer;
3386 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3387 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3391 //the value on stack is going to be used as a pointer
3392 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3394 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3395 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3398 g_assert (obj->type);
3400 /*The value on the stack must be a subclass of the defining type of the field*/
3401 /* we need to check if we can load the field from the stack value*/
3403 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3404 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3406 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3407 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3409 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3410 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));
3412 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3413 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3414 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));
3416 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3417 char *found = stack_slot_full_name (obj);
3418 char *expected = mono_type_full_name (&field->parent->byval_arg);
3419 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));
3424 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3425 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3428 check_unmanaged_pointer (ctx, obj);
3433 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3436 MonoClassField *field;
3439 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3441 if (!check_underflow (ctx, 1))
3443 obj = stack_pop_safe (ctx);
3445 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3448 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3449 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3451 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3452 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3453 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3455 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3459 do_store_field (VerifyContext *ctx, int token)
3461 ILStackDesc *value, *obj;
3462 MonoClassField *field;
3463 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3465 if (!check_underflow (ctx, 2))
3468 value = stack_pop (ctx);
3469 obj = stack_pop_safe (ctx);
3471 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3474 if (!verify_stack_type_compatibility (ctx, field->type, value))
3475 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3478 /*TODO proper handle for Nullable<T>*/
3480 do_box_value (VerifyContext *ctx, int klass_token)
3483 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3489 if (!check_underflow (ctx, 1))
3492 value = stack_pop (ctx);
3493 /*box is a nop for reference types*/
3495 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3496 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3501 if (!verify_stack_type_compatibility (ctx, type, value))
3502 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3504 klass = mono_class_from_mono_type (type);
3505 if (mono_class_is_nullable (klass))
3506 type = &mono_class_get_nullable_param (klass)->byval_arg;
3507 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3511 do_unbox_value (VerifyContext *ctx, int klass_token)
3514 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3519 if (!check_underflow (ctx, 1))
3522 if (!mono_class_from_mono_type (type)->valuetype)
3523 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3525 value = stack_pop (ctx);
3527 /*Value should be: a boxed valuetype or a reference type*/
3528 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3529 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3530 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));
3532 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3533 value->stype |= CMMP_MASK;
3537 do_unbox_any (VerifyContext *ctx, int klass_token)
3540 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3545 if (!check_underflow (ctx, 1))
3548 value = stack_pop (ctx);
3550 /*Value should be: a boxed valuetype or a reference type*/
3551 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3552 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3553 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));
3555 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3559 do_unary_math_op (VerifyContext *ctx, int op)
3562 if (!check_underflow (ctx, 1))
3564 value = stack_pop (ctx);
3565 switch (stack_slot_get_type (value)) {
3568 case TYPE_NATIVE_INT:
3573 case TYPE_COMPLEX: /*only enums are ok*/
3574 if (mono_type_is_enum_type (value->type))
3577 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3579 stack_push_stack_val (ctx, value);
3583 do_conversion (VerifyContext *ctx, int kind)
3586 if (!check_underflow (ctx, 1))
3588 value = stack_pop (ctx);
3590 switch (stack_slot_get_type (value)) {
3593 case TYPE_NATIVE_INT:
3597 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));
3602 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3605 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3608 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3610 case TYPE_NATIVE_INT:
3611 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3614 g_error ("unknown type %02x in conversion", kind);
3620 do_load_token (VerifyContext *ctx, int token)
3624 MonoClass *handle_class;
3625 if (!check_overflow (ctx))
3628 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
3629 handle = mono_method_get_wrapper_data (ctx->method, token);
3630 handle_class = (MonoClass *)mono_method_get_wrapper_data (ctx->method, token + 1);
3631 if (handle_class == mono_defaults.typehandle_class)
3632 handle = &((MonoClass*)handle)->byval_arg;
3634 switch (token & 0xff000000) {
3635 case MONO_TOKEN_TYPE_DEF:
3636 case MONO_TOKEN_TYPE_REF:
3637 case MONO_TOKEN_TYPE_SPEC:
3638 case MONO_TOKEN_FIELD_DEF:
3639 case MONO_TOKEN_METHOD_DEF:
3640 case MONO_TOKEN_METHOD_SPEC:
3641 case MONO_TOKEN_MEMBER_REF:
3642 if (!token_bounds_check (ctx->image, token)) {
3643 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));
3648 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));
3652 handle = mono_ldtoken_checked (ctx->image, token, &handle_class, ctx->generic_context, &error);
3656 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)));
3657 mono_error_cleanup (&error);
3660 if (handle_class == mono_defaults.typehandle_class) {
3661 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3662 } else if (handle_class == mono_defaults.methodhandle_class) {
3663 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3664 } else if (handle_class == mono_defaults.fieldhandle_class) {
3665 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3667 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3669 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3673 do_ldobj_value (VerifyContext *ctx, int token)
3676 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3677 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3682 if (!check_underflow (ctx, 1))
3685 value = stack_pop (ctx);
3686 if (!stack_slot_is_managed_pointer (value)
3687 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3688 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3689 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3693 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3694 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3696 /*We have a byval on the stack, but the comparison must be strict. */
3697 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3698 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3700 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3704 do_stobj (VerifyContext *ctx, int token)
3706 ILStackDesc *dest, *src;
3707 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3708 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3713 if (!check_underflow (ctx, 2))
3716 src = stack_pop (ctx);
3717 dest = stack_pop (ctx);
3719 if (stack_slot_is_managed_mutability_pointer (dest))
3720 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3722 if (!stack_slot_is_managed_pointer (dest))
3723 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3725 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3726 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));
3728 if (!verify_stack_type_compatibility (ctx, type, src)) {
3729 char *type_name = mono_type_full_name (type);
3730 char *src_name = stack_slot_full_name (src);
3731 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));
3736 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3737 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3741 do_cpobj (VerifyContext *ctx, int token)
3743 ILStackDesc *dest, *src;
3744 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3748 if (!check_underflow (ctx, 2))
3751 src = stack_pop (ctx);
3752 dest = stack_pop (ctx);
3754 if (!stack_slot_is_managed_pointer (src))
3755 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3757 if (!stack_slot_is_managed_pointer (dest))
3758 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3760 if (stack_slot_is_managed_mutability_pointer (dest))
3761 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3763 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3764 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3766 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3767 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3771 do_initobj (VerifyContext *ctx, int token)
3774 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3778 if (!check_underflow (ctx, 1))
3781 obj = stack_pop (ctx);
3783 if (!stack_slot_is_managed_pointer (obj))
3784 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3786 if (stack_slot_is_managed_mutability_pointer (obj))
3787 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3789 stack = mono_type_get_type_byval (obj->type);
3790 if (MONO_TYPE_IS_REFERENCE (stack)) {
3791 if (!verify_type_compatibility (ctx, stack, type))
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 (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3794 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3795 } else if (!verify_type_compatibility (ctx, stack, type)) {
3796 char *expected_name = mono_type_full_name (type);
3797 char *stack_name = mono_type_full_name (stack);
3799 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));
3800 g_free (expected_name);
3801 g_free (stack_name);
3806 do_newobj (VerifyContext *ctx, int token)
3810 MonoMethodSignature *sig;
3812 gboolean is_delegate = FALSE;
3814 if (!(method = verifier_load_method (ctx, token, "newobj")))
3817 if (!mono_method_is_constructor (method)) {
3818 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3822 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3823 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3825 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3826 char *from = mono_method_full_name (ctx->method, TRUE);
3827 char *to = mono_method_full_name (method, TRUE);
3828 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);
3833 //FIXME use mono_method_get_signature_full
3834 sig = mono_method_signature (method);
3836 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3840 if (!sig->hasthis) {
3841 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature missing hasthis at 0x%04x", ctx->ip_offset));
3845 if (!check_underflow (ctx, sig->param_count))
3848 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3851 ILStackDesc *funptr;
3852 //first arg is object, second arg is fun ptr
3853 if (sig->param_count != 2) {
3854 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3857 funptr = stack_pop (ctx);
3858 value = stack_pop (ctx);
3859 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3861 for (i = sig->param_count - 1; i >= 0; --i) {
3862 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3863 value = stack_pop (ctx);
3864 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3865 char *stack_name = stack_slot_full_name (value);
3866 char *sig_name = mono_type_full_name (sig->params [i]);
3867 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));
3868 g_free (stack_name);
3872 if (stack_slot_is_managed_mutability_pointer (value))
3873 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3877 if (check_overflow (ctx))
3878 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3882 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3888 if (!check_underflow (ctx, 1))
3891 if (!(type = get_boxable_mono_type (ctx, token, opcode)))
3895 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3899 value = stack_pop (ctx);
3900 is_boxed = stack_slot_is_boxed_value (value);
3902 if (stack_slot_is_managed_pointer (value))
3903 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3904 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3905 char *name = stack_slot_full_name (value);
3906 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));
3910 switch (value->type->type) {
3911 case MONO_TYPE_FNPTR:
3913 case MONO_TYPE_TYPEDBYREF:
3914 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3919 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3920 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3924 mono_type_from_opcode (int opcode) {
3932 return &mono_defaults.sbyte_class->byval_arg;
3940 return &mono_defaults.int16_class->byval_arg;
3948 return &mono_defaults.int32_class->byval_arg;
3954 return &mono_defaults.int64_class->byval_arg;
3960 return &mono_defaults.single_class->byval_arg;
3966 return &mono_defaults.double_class->byval_arg;
3972 return &mono_defaults.int_class->byval_arg;
3976 case CEE_LDELEM_REF:
3977 case CEE_STELEM_REF:
3978 return &mono_defaults.object_class->byval_arg;
3981 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3987 do_load_indirect (VerifyContext *ctx, int opcode)
3990 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3992 if (!check_underflow (ctx, 1))
3995 value = stack_pop (ctx);
3996 if (!stack_slot_is_managed_pointer (value)) {
3997 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3998 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4002 if (opcode == CEE_LDIND_REF) {
4003 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
4004 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
4005 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
4007 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
4008 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4009 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4014 do_store_indirect (VerifyContext *ctx, int opcode)
4016 ILStackDesc *addr, *val;
4017 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
4019 if (!check_underflow (ctx, 2))
4022 val = stack_pop (ctx);
4023 addr = stack_pop (ctx);
4025 check_unmanaged_pointer (ctx, addr);
4027 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
4028 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
4032 if (stack_slot_is_managed_mutability_pointer (addr)) {
4033 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
4037 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
4038 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4040 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
4041 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4045 do_newarr (VerifyContext *ctx, int token)
4048 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
4053 if (!check_underflow (ctx, 1))
4056 value = stack_pop (ctx);
4057 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4058 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));
4060 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
4063 /*FIXME handle arrays that are not 0-indexed*/
4065 do_ldlen (VerifyContext *ctx)
4069 if (!check_underflow (ctx, 1))
4072 value = stack_pop (ctx);
4074 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
4075 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
4077 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
4080 /*FIXME handle arrays that are not 0-indexed*/
4081 /*FIXME handle readonly prefix and CMMP*/
4083 do_ldelema (VerifyContext *ctx, int klass_token)
4085 ILStackDesc *index, *array, *res;
4086 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
4092 if (!check_underflow (ctx, 2))
4095 index = stack_pop (ctx);
4096 array = stack_pop (ctx);
4098 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4099 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));
4101 if (!stack_slot_is_null_literal (array)) {
4102 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4103 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4105 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
4106 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
4108 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
4111 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
4115 res = stack_push (ctx);
4116 set_stack_value (ctx, res, type, TRUE);
4117 if (ctx->prefix_set & PREFIX_READONLY) {
4118 ctx->prefix_set &= ~PREFIX_READONLY;
4119 res->stype |= CMMP_MASK;
4124 * FIXME handle arrays that are not 0-indexed
4125 * FIXME handle readonly prefix and CMMP
4128 do_ldelem (VerifyContext *ctx, int opcode, int token)
4130 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4131 ILStackDesc *index, *array;
4133 if (!check_underflow (ctx, 2))
4136 if (opcode == CEE_LDELEM) {
4137 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
4138 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4142 type = mono_type_from_opcode (opcode);
4145 index = stack_pop (ctx);
4146 array = stack_pop (ctx);
4148 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4149 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));
4151 if (!stack_slot_is_null_literal (array)) {
4152 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4153 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));
4155 if (opcode == CEE_LDELEM_REF) {
4156 if (array->type->data.klass->valuetype)
4157 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
4158 type = &array->type->data.klass->byval_arg;
4160 MonoType *candidate = &array->type->data.klass->byval_arg;
4161 if (IS_STRICT_MODE (ctx)) {
4162 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
4163 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
4164 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)) ||
4165 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
4166 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4168 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
4169 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4174 set_stack_value (ctx, stack_push (ctx), type, FALSE);
4179 * FIXME handle arrays that are not 0-indexed
4182 do_stelem (VerifyContext *ctx, int opcode, int token)
4184 ILStackDesc *index, *array, *value;
4186 if (!check_underflow (ctx, 3))
4189 if (opcode == CEE_STELEM) {
4190 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
4191 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4195 type = mono_type_from_opcode (opcode);
4198 value = stack_pop (ctx);
4199 index = stack_pop (ctx);
4200 array = stack_pop (ctx);
4202 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4203 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));
4205 if (!stack_slot_is_null_literal (array)) {
4206 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
4207 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));
4209 if (opcode == CEE_STELEM_REF) {
4210 if (array->type->data.klass->valuetype)
4211 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4212 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
4213 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4217 if (opcode == CEE_STELEM_REF) {
4218 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
4219 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4220 } else if (opcode != CEE_STELEM_REF) {
4221 if (!verify_stack_type_compatibility (ctx, type, value))
4222 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4224 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
4225 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));
4231 do_throw (VerifyContext *ctx)
4233 ILStackDesc *exception;
4234 if (!check_underflow (ctx, 1))
4236 exception = stack_pop (ctx);
4238 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4239 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4241 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
4242 char *name = mono_type_full_name (exception->type);
4243 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));
4246 /*The stack is left empty after a throw*/
4252 do_endfilter (VerifyContext *ctx)
4254 MonoExceptionClause *clause;
4256 if (IS_STRICT_MODE (ctx)) {
4257 if (ctx->eval.size != 1)
4258 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4260 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4261 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4264 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4265 if (IS_STRICT_MODE (ctx)) {
4266 if (ctx->ip_offset != clause->handler_offset - 2)
4267 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4269 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4270 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4273 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4274 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4276 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4283 do_leave (VerifyContext *ctx, int delta)
4285 int target = ((gint32)ctx->ip_offset) + delta;
4286 if (target >= ctx->code_size || target < 0)
4287 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4289 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4290 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4292 ctx->target = target;
4298 * Verify br and br.s opcodes.
4301 do_static_branch (VerifyContext *ctx, int delta)
4303 int target = ctx->ip_offset + delta;
4304 if (target < 0 || target >= ctx->code_size) {
4305 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4309 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4311 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4314 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4318 ctx->target = target;
4322 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4324 int i, base = ctx->ip_offset + 5 + count * 4;
4327 if (!check_underflow (ctx, 1))
4330 value = stack_pop (ctx);
4332 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4333 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4335 for (i = 0; i < count; ++i) {
4336 int target = base + read32 (data + i * 4);
4338 if (target < 0 || target >= ctx->code_size) {
4339 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4343 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4345 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4348 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4351 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4356 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual_)
4361 if (virtual_ && !check_underflow (ctx, 1))
4364 if (!virtual_ && !check_overflow (ctx))
4367 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
4368 method = (MonoMethod *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
4370 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4374 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4375 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4379 if (!(method = verifier_load_method (ctx, token, virtual_ ? "ldvirtfrn" : "ldftn")))
4383 if (mono_method_is_constructor (method))
4384 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4387 ILStackDesc *top = stack_pop (ctx);
4389 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4390 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4392 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4393 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4395 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4396 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4399 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
4400 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);
4402 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4403 top->method = method;
4407 do_sizeof (VerifyContext *ctx, int token)
4411 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4414 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4415 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4419 if (type->type == MONO_TYPE_VOID) {
4420 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4424 if (check_overflow (ctx))
4425 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4428 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4430 do_localloc (VerifyContext *ctx)
4432 if (ctx->eval.size != 1) {
4433 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4437 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4438 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4442 /*TODO verify top type*/
4443 /* top = */ stack_pop (ctx);
4445 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4446 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4450 do_ldstr (VerifyContext *ctx, guint32 token)
4452 GSList *error = NULL;
4453 if (ctx->method->wrapper_type == MONO_WRAPPER_NONE && !image_is_dynamic (ctx->image)) {
4454 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4455 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4459 if (!mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4461 ctx->list = g_slist_concat (ctx->list, error);
4462 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4467 if (check_overflow (ctx))
4468 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4472 do_refanyval (VerifyContext *ctx, int token)
4476 if (!check_underflow (ctx, 1))
4479 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4482 top = stack_pop (ctx);
4484 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4485 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));
4487 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4491 do_refanytype (VerifyContext *ctx)
4495 if (!check_underflow (ctx, 1))
4498 top = stack_pop (ctx);
4500 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4501 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));
4503 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4508 do_mkrefany (VerifyContext *ctx, int token)
4512 if (!check_underflow (ctx, 1))
4515 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4518 top = stack_pop (ctx);
4520 if (stack_slot_is_managed_mutability_pointer (top))
4521 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4523 if (!stack_slot_is_managed_pointer (top)) {
4524 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));
4526 MonoType *stack_type = mono_type_get_type_byval (top->type);
4527 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4528 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4530 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4531 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4534 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4538 do_ckfinite (VerifyContext *ctx)
4541 if (!check_underflow (ctx, 1))
4544 top = stack_pop (ctx);
4546 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4547 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));
4548 stack_push_stack_val (ctx, top);
4552 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4554 * @from holds new values for a given control path
4555 * @to holds the current values of a given control path
4557 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4560 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4564 stack_init (ctx, to);
4567 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4570 stack_copy (&ctx->eval, to);
4572 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4573 stack_copy (to, &ctx->eval);
4576 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4578 if (from->size != to->size) {
4579 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4580 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));
4584 //FIXME we need to preserve CMMP attributes
4585 //FIXME we must take null literals into consideration.
4586 for (i = 0; i < from->size; ++i) {
4587 ILStackDesc *new_slot = from->stack + i;
4588 ILStackDesc *old_slot = to->stack + i;
4589 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4590 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4591 MonoClass *old_class = mono_class_from_mono_type (old_type);
4592 MonoClass *new_class = mono_class_from_mono_type (new_type);
4593 MonoClass *match_class = NULL;
4595 // S := T then U = S (new value is compatible with current value, keep current)
4596 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4597 copy_stack_value (new_slot, old_slot);
4601 // T := S then U = T (old value is compatible with current value, use new)
4602 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4603 copy_stack_value (old_slot, new_slot);
4607 /*Both slots are the same boxed valuetype. Simply copy it.*/
4608 if (stack_slot_is_boxed_value (old_slot) &&
4609 stack_slot_is_boxed_value (new_slot) &&
4610 mono_metadata_type_equal (old_type, new_type)) {
4611 copy_stack_value (new_slot, old_slot);
4615 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4616 char *old_name = stack_slot_full_name (old_slot);
4617 char *new_name = stack_slot_full_name (new_slot);
4618 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));
4624 //both are reference types, use closest common super type
4625 if (!mono_class_from_mono_type (old_type)->valuetype
4626 && !mono_class_from_mono_type (new_type)->valuetype
4627 && !stack_slot_is_managed_pointer (old_slot)
4628 && !stack_slot_is_managed_pointer (new_slot)) {
4630 mono_class_setup_supertypes (old_class);
4631 mono_class_setup_supertypes (new_class);
4633 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4634 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4635 match_class = old_class->supertypes [j];
4640 mono_class_setup_interfaces (old_class, &error);
4641 if (!mono_error_ok (&error)) {
4642 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));
4643 mono_error_cleanup (&error);
4646 mono_class_setup_interfaces (new_class, &error);
4647 if (!mono_error_ok (&error)) {
4648 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));
4649 mono_error_cleanup (&error);
4653 /* if old class is an interface that new class implements */
4654 if (old_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4655 if (verifier_class_is_assignable_from (old_class, new_class)) {
4656 match_class = old_class;
4659 for (j = 0; j < old_class->interface_count; ++j) {
4660 if (verifier_class_is_assignable_from (old_class->interfaces [j], new_class)) {
4661 match_class = old_class->interfaces [j];
4667 if (new_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4668 if (verifier_class_is_assignable_from (new_class, old_class)) {
4669 match_class = new_class;
4672 for (j = 0; j < new_class->interface_count; ++j) {
4673 if (verifier_class_is_assignable_from (new_class->interfaces [j], old_class)) {
4674 match_class = new_class->interfaces [j];
4680 //No decent super type found, use object
4681 match_class = mono_defaults.object_class;
4683 } 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)) {
4684 match_class = mono_defaults.object_class;
4689 char *old_name = stack_slot_full_name (old_slot);
4690 char *new_name = stack_slot_full_name (new_slot);
4691 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));
4695 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4699 g_assert (match_class);
4700 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4701 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4707 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4708 to->flags |= IL_CODE_STACK_MERGED;
4711 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4712 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4715 * is_clause_in_range :
4717 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4720 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4722 if (clause->try_offset >= start && clause->try_offset < end)
4724 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4730 * is_clause_inside_range :
4732 * Returns TRUE if @clause lies completely inside the @start - @end range.
4735 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4737 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4739 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4745 * is_clause_nested :
4747 * Returns TRUE if @nested is nested in @clause.
4750 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4752 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4754 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4755 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4758 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4759 * the each pair of exception must have the following properties:
4760 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4761 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4762 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4765 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4767 /*clause is nested*/
4768 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4769 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4773 /*wrong nesting order.*/
4774 if (is_clause_nested (clause, to_test)) {
4775 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4779 /*mutual protection*/
4780 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4781 /*handlers are not disjoint*/
4782 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4783 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4786 /* handlers are not catch or filter */
4787 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4788 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4795 /*not completelly disjoint*/
4796 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4797 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4798 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4801 #define code_bounds_check(size) \
4802 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4803 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4808 mono_opcode_is_prefix (int op)
4811 case MONO_CEE_UNALIGNED_:
4812 case MONO_CEE_VOLATILE_:
4813 case MONO_CEE_TAIL_:
4814 case MONO_CEE_CONSTRAINED_:
4815 case MONO_CEE_READONLY_:
4822 * FIXME: need to distinguish between valid and verifiable.
4823 * Need to keep track of types on the stack.
4824 * Verify types for opcodes.
4827 mono_method_verify (MonoMethod *method, int level)
4830 const unsigned char *ip, *code_start;
4831 const unsigned char *end;
4832 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4834 int i, n, need_merge = 0, start = 0;
4835 guint ip_offset = 0, prefix = 0;
4836 MonoGenericContext *generic_context = NULL;
4840 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4842 init_verifier_stats ();
4844 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4845 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4849 memset (&ctx, 0, sizeof (VerifyContext));
4851 //FIXME use mono_method_get_signature_full
4852 ctx.signature = mono_method_signature (method);
4853 if (!ctx.signature) {
4854 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4856 finish_collect_stats ();
4859 if (!method->is_generic && !method->klass->is_generic && ctx.signature->has_type_parameters) {
4860 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method and signature don't match in terms of genericity"));
4861 finish_collect_stats ();
4865 ctx.header = mono_method_get_header (method);
4867 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4868 finish_collect_stats ();
4871 ctx.method = method;
4872 code_start = ip = ctx.header->code;
4873 end = ip + ctx.header->code_size;
4874 ctx.image = image = method->klass->image;
4877 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4878 ctx.max_stack = ctx.header->max_stack;
4879 ctx.verifiable = ctx.valid = 1;
4882 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4883 ctx.code_size = ctx.header->code_size;
4884 _MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4886 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4888 ctx.num_locals = ctx.header->num_locals;
4889 ctx.locals = (MonoType **)g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4890 _MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4892 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4893 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4895 ctx.params = g_new (MonoType*, ctx.max_args);
4896 _MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4898 if (ctx.signature->hasthis)
4899 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4900 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4902 if (ctx.signature->is_inflated)
4903 ctx.generic_context = generic_context = mono_method_get_context (method);
4905 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4906 if (method->is_generic)
4907 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4909 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4912 for (i = 0; i < ctx.num_locals; ++i) {
4913 MonoType *uninflated = ctx.locals [i];
4914 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4915 if (!mono_error_ok (&error)) {
4916 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4917 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4919 mono_error_cleanup (&error);
4920 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4926 for (i = 0; i < ctx.max_args; ++i) {
4927 MonoType *uninflated = ctx.params [i];
4928 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4929 if (!mono_error_ok (&error)) {
4930 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4931 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4933 mono_error_cleanup (&error);
4934 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4939 stack_init (&ctx, &ctx.eval);
4941 for (i = 0; i < ctx.num_locals; ++i) {
4942 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4944 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4945 char *name = mono_type_full_name (ctx.locals [i]);
4946 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4953 for (i = 0; i < ctx.max_args; ++i) {
4954 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4957 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4958 char *name = mono_type_full_name (ctx.params [i]);
4959 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4968 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4969 MonoExceptionClause *clause = ctx.header->clauses + i;
4970 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); );
4972 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4973 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4975 if (clause->try_len <= 0)
4976 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4978 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4979 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4981 if (clause->handler_len <= 0)
4982 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4984 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4985 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4987 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4988 if (clause->data.filter_offset > ctx.code_size)
4989 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4991 if (clause->data.filter_offset >= clause->handler_offset)
4992 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4995 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4996 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
5001 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
5002 if (clause->try_offset + clause->try_len < ctx.code_size)
5003 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
5004 if (clause->handler_offset + clause->handler_len < ctx.code_size)
5005 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
5007 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
5008 if (!clause->data.catch_class) {
5009 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
5012 if (!mono_type_is_valid_in_context (&ctx, &clause->data.catch_class->byval_arg))
5015 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
5017 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5018 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
5019 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
5026 original_bb = bb = mono_basic_block_split (method, &error);
5027 if (!mono_error_ok (&error)) {
5028 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
5029 mono_error_cleanup (&error);
5034 while (ip < end && ctx.valid) {
5036 ip_offset = (guint) (ip - code_start);
5038 const unsigned char *ip_copy = ip;
5041 if (ip_offset > bb->end) {
5042 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
5046 if (ip_offset == bb->end)
5049 op_size = mono_opcode_value_and_size (&ip_copy, end, &op);
5050 if (op_size == -1) {
5051 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
5055 if (ADD_IS_GREATER_OR_OVF (ip_offset, op_size, bb->end)) {
5056 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
5060 /*Last Instruction*/
5061 if (ip_offset + op_size == bb->end && mono_opcode_is_prefix (op)) {
5062 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));
5067 ctx.ip_offset = ip_offset = (guint) (ip - code_start);
5069 /*We need to check against fallthrou in and out of protected blocks.
5070 * For fallout we check the once a protected block ends, if the start flag is not set.
5071 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5072 * TODO convert these checks to be done using flags and not this loop
5074 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
5075 MonoExceptionClause *clause = ctx.header->clauses + i;
5077 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
5078 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
5082 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
5083 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
5084 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5086 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5090 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
5091 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
5095 if (clause->handler_offset == ip_offset && start == 0) {
5096 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
5100 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
5101 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
5106 /*This must be done after fallthru detection otherwise it won't happen.*/
5108 /*FIXME remove this once we move all bad branch checking code to use BB only*/
5109 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
5118 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
5119 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
5122 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
5125 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5126 #ifdef MONO_VERIFIER_DEBUG
5129 discode = mono_disasm_code_one (NULL, method, ip, NULL);
5130 discode [strlen (discode) - 1] = 0; /* no \n */
5131 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
5134 dump_stack_state (&ctx.code [ip_offset]);
5135 dump_stack_state (&ctx.eval);
5148 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
5154 code_bounds_check (2);
5155 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
5159 case CEE_ADD_OVF_UN:
5160 do_binop (&ctx, *ip, add_ovf_un_table);
5164 case CEE_SUB_OVF_UN:
5165 do_binop (&ctx, *ip, sub_ovf_un_table);
5172 case CEE_MUL_OVF_UN:
5173 do_binop (&ctx, *ip, bin_ovf_table);
5178 do_binop (&ctx, *ip, add_table);
5183 do_binop (&ctx, *ip, sub_table);
5190 do_binop (&ctx, *ip, bin_op_table);
5199 do_binop (&ctx, *ip, int_bin_op_table);
5206 do_binop (&ctx, *ip, shift_op_table);
5211 if (!check_underflow (&ctx, 1))
5213 stack_pop_safe (&ctx);
5227 /*TODO support definite assignment verification? */
5228 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
5236 store_local (&ctx, *ip - CEE_STLOC_0);
5241 code_bounds_check (2);
5242 store_local (&ctx, ip [1]);
5247 code_bounds_check (2);
5248 store_arg (&ctx, ip [1]);
5262 if (check_overflow (&ctx))
5263 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5268 code_bounds_check (2);
5269 if (check_overflow (&ctx))
5270 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5275 code_bounds_check (5);
5276 if (check_overflow (&ctx))
5277 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
5282 code_bounds_check (9);
5283 if (check_overflow (&ctx))
5284 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
5289 code_bounds_check (5);
5290 if (check_overflow (&ctx))
5291 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5296 code_bounds_check (9);
5297 if (check_overflow (&ctx))
5298 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5303 if (check_overflow (&ctx))
5304 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
5310 code_bounds_check (2);
5311 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
5324 code_bounds_check (2);
5325 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5332 code_bounds_check (5);
5333 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5346 code_bounds_check (5);
5347 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5354 code_bounds_check (2);
5355 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5360 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5366 if (!check_underflow (&ctx, 1))
5368 if (!check_overflow (&ctx))
5370 top = stack_push (&ctx);
5371 copy_stack_value (top, stack_peek (&ctx, 1));
5377 code_bounds_check (5);
5379 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5380 /* token = read32 (ip + 1); */
5381 if (in_any_block (ctx.header, ip_offset))
5382 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5384 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5386 * FIXME: check signature, retval, arguments etc.
5392 code_bounds_check (5);
5393 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5398 code_bounds_check (5);
5399 /* token = read32 (ip + 1); */
5401 * FIXME: check signature, retval, arguments etc.
5402 * FIXME: check requirements for tail call
5404 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5408 code_bounds_check (2);
5409 do_static_branch (&ctx, (signed char)ip [1] + 2);
5417 code_bounds_check (2);
5418 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5424 code_bounds_check (5);
5425 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5433 code_bounds_check (5);
5434 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5441 code_bounds_check (5);
5442 entries = read32 (ip + 1);
5444 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5445 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5448 code_bounds_check (sizeof (guint32) * entries);
5450 do_switch (&ctx, entries, ip);
5451 ip += sizeof (guint32) * entries;
5465 do_load_indirect (&ctx, *ip);
5477 do_store_indirect (&ctx, *ip);
5483 do_unary_math_op (&ctx, *ip);
5493 do_conversion (&ctx, TYPE_I4);
5499 do_conversion (&ctx, TYPE_I8);
5506 do_conversion (&ctx, TYPE_R8);
5512 do_conversion (&ctx, TYPE_NATIVE_INT);
5517 code_bounds_check (5);
5518 do_cpobj (&ctx, read32 (ip + 1));
5523 code_bounds_check (5);
5524 do_ldobj_value (&ctx, read32 (ip + 1));
5529 code_bounds_check (5);
5530 do_ldstr (&ctx, read32 (ip + 1));
5535 code_bounds_check (5);
5536 do_newobj (&ctx, read32 (ip + 1));
5542 code_bounds_check (5);
5543 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5549 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5554 code_bounds_check (5);
5555 do_unbox_value (&ctx, read32 (ip + 1));
5567 code_bounds_check (5);
5568 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5574 code_bounds_check (5);
5575 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5580 code_bounds_check (5);
5581 do_store_field (&ctx, read32 (ip + 1));
5586 code_bounds_check (5);
5587 do_store_static_field (&ctx, read32 (ip + 1));
5592 code_bounds_check (5);
5593 do_stobj (&ctx, read32 (ip + 1));
5597 case CEE_CONV_OVF_I1_UN:
5598 case CEE_CONV_OVF_I2_UN:
5599 case CEE_CONV_OVF_I4_UN:
5600 case CEE_CONV_OVF_U1_UN:
5601 case CEE_CONV_OVF_U2_UN:
5602 case CEE_CONV_OVF_U4_UN:
5603 do_conversion (&ctx, TYPE_I4);
5607 case CEE_CONV_OVF_I8_UN:
5608 case CEE_CONV_OVF_U8_UN:
5609 do_conversion (&ctx, TYPE_I8);
5613 case CEE_CONV_OVF_I_UN:
5614 case CEE_CONV_OVF_U_UN:
5615 do_conversion (&ctx, TYPE_NATIVE_INT);
5620 code_bounds_check (5);
5621 do_box_value (&ctx, read32 (ip + 1));
5626 code_bounds_check (5);
5627 do_newarr (&ctx, read32 (ip + 1));
5637 code_bounds_check (5);
5638 do_ldelema (&ctx, read32 (ip + 1));
5652 case CEE_LDELEM_REF:
5653 do_ldelem (&ctx, *ip, 0);
5664 case CEE_STELEM_REF:
5665 do_stelem (&ctx, *ip, 0);
5670 code_bounds_check (5);
5671 do_ldelem (&ctx, *ip, read32 (ip + 1));
5676 code_bounds_check (5);
5677 do_stelem (&ctx, *ip, read32 (ip + 1));
5682 code_bounds_check (5);
5683 do_unbox_any (&ctx, read32 (ip + 1));
5687 case CEE_CONV_OVF_I1:
5688 case CEE_CONV_OVF_U1:
5689 case CEE_CONV_OVF_I2:
5690 case CEE_CONV_OVF_U2:
5691 case CEE_CONV_OVF_I4:
5692 case CEE_CONV_OVF_U4:
5693 do_conversion (&ctx, TYPE_I4);
5697 case CEE_CONV_OVF_I8:
5698 case CEE_CONV_OVF_U8:
5699 do_conversion (&ctx, TYPE_I8);
5703 case CEE_CONV_OVF_I:
5704 case CEE_CONV_OVF_U:
5705 do_conversion (&ctx, TYPE_NATIVE_INT);
5710 code_bounds_check (5);
5711 do_refanyval (&ctx, read32 (ip + 1));
5721 code_bounds_check (5);
5722 do_mkrefany (&ctx, read32 (ip + 1));
5727 code_bounds_check (5);
5728 do_load_token (&ctx, read32 (ip + 1));
5732 case CEE_ENDFINALLY:
5733 if (!is_correct_endfinally (ctx.header, ip_offset))
5734 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5741 code_bounds_check (5);
5742 do_leave (&ctx, read32 (ip + 1) + 5);
5749 code_bounds_check (2);
5750 do_leave (&ctx, (signed char)ip [1] + 2);
5757 code_bounds_check (2);
5761 code_bounds_check (3);
5762 store_local (&ctx, read16 (ip + 1));
5767 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5775 do_cmp_op (&ctx, cmp_br_op, *ip);
5780 code_bounds_check (3);
5781 store_arg (&ctx, read16 (ip + 1) );
5787 if (!check_overflow (&ctx))
5789 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5790 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5791 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5796 code_bounds_check (5);
5797 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5802 code_bounds_check (5);
5803 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5809 code_bounds_check (3);
5810 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5816 code_bounds_check (3);
5817 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5830 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5834 do_endfilter (&ctx);
5838 case CEE_UNALIGNED_:
5839 code_bounds_check (2);
5840 prefix |= PREFIX_UNALIGNED;
5844 prefix |= PREFIX_VOLATILE;
5848 prefix |= PREFIX_TAIL;
5850 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5851 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5855 code_bounds_check (5);
5856 do_initobj (&ctx, read32 (ip + 1));
5860 case CEE_CONSTRAINED_:
5861 code_bounds_check (5);
5862 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5863 prefix |= PREFIX_CONSTRAINED;
5868 prefix |= PREFIX_READONLY;
5873 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5874 if (!check_underflow (&ctx, 3))
5876 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5881 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5882 if (!check_underflow (&ctx, 3))
5884 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5892 if (!is_correct_rethrow (ctx.header, ip_offset))
5893 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5900 code_bounds_check (5);
5901 do_sizeof (&ctx, read32 (ip + 1));
5905 case CEE_REFANYTYPE:
5906 do_refanytype (&ctx);
5911 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5917 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5921 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5923 if (!ctx.prefix_set) //first prefix
5924 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5925 ctx.prefix_set |= prefix;
5926 ctx.has_flags = TRUE;
5930 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5932 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5933 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5934 if (ctx.prefix_set & PREFIX_READONLY)
5935 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5936 if (ctx.prefix_set & PREFIX_VOLATILE)
5937 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5938 if (ctx.prefix_set & PREFIX_UNALIGNED)
5939 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5940 ctx.prefix_set = prefix = 0;
5941 ctx.has_flags = FALSE;
5945 * if ip != end we overflowed: mark as error.
5947 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5948 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5951 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5952 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5953 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5954 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5955 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5957 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5958 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5960 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5961 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5963 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5964 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));
5967 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5968 char *method_name = mono_method_full_name (ctx.method, TRUE);
5969 char *type = mono_type_get_full_name (ctx.method->klass);
5970 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5971 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));
5973 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5974 g_free (method_name);
5980 for (i = 0; i < ctx.header->code_size; ++i) {
5981 if (ctx.code [i].stack)
5982 g_free (ctx.code [i].stack);
5986 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5988 g_slist_free (ctx.funptrs);
5990 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5991 mono_metadata_free_type ((MonoType *)tmp->data);
5992 g_slist_free (ctx.exception_types);
5994 for (i = 0; i < ctx.num_locals; ++i) {
5996 mono_metadata_free_type (ctx.locals [i]);
5998 for (i = 0; i < ctx.max_args; ++i) {
6000 mono_metadata_free_type (ctx.params [i]);
6004 g_free (ctx.eval.stack);
6007 g_free (ctx.locals);
6008 g_free (ctx.params);
6009 mono_basic_block_free (original_bb);
6010 mono_metadata_free_mh (ctx.header);
6012 finish_collect_stats ();
6017 mono_verify_corlib ()
6019 /* This is a public API function so cannot be removed */
6024 * Returns true if @method needs to be verified.
6028 mono_verifier_is_enabled_for_method (MonoMethod *method)
6030 return mono_verifier_is_enabled_for_class (method->klass) && (method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD);
6034 * Returns true if @klass need to be verified.
6038 mono_verifier_is_enabled_for_class (MonoClass *klass)
6040 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
6044 mono_verifier_is_enabled_for_image (MonoImage *image)
6046 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
6050 * Dynamic methods are not considered full trust since if the user is trusted and need to
6051 * generate unsafe code, make the method skip verification - this is a known good way to do it.
6054 mono_verifier_is_method_full_trust (MonoMethod *method)
6056 return mono_verifier_is_class_full_trust (method->klass) && !method_is_dynamic (method);
6060 * Returns if @klass is under full trust or not.
6062 * TODO This code doesn't take CAS into account.
6064 * Under verify_all all user code must be verifiable if no security option was set
6068 mono_verifier_is_class_full_trust (MonoClass *klass)
6070 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
6071 gboolean trusted_location = !mono_security_core_clr_enabled () ?
6072 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
6074 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
6075 return trusted_location || klass->image == mono_defaults.corlib;
6076 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
6080 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
6082 return mono_method_verify (method,
6083 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
6084 | (!is_fulltrust && !mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
6085 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
6089 get_field_end (MonoClassField *field)
6092 int size = mono_type_size (field->type, &align);
6094 size = 4; /*FIXME Is this a safe bet?*/
6095 return size + field->offset;
6099 verify_class_for_overlapping_reference_fields (MonoClass *klass)
6102 gpointer iter = NULL;
6103 MonoClassField *field;
6104 gboolean is_fulltrust = mono_verifier_is_class_full_trust (klass);
6105 /*We can't skip types with !has_references since this is calculated after we have run.*/
6106 if (!((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
6110 /*We must check for stuff overlapping reference fields.
6111 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
6113 while ((field = mono_class_get_fields (klass, &iter))) {
6114 int fieldEnd = get_field_end (field);
6115 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
6118 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
6121 for (j = i; j < klass->field.count; ++j) {
6122 MonoClassField *other = &klass->fields [j];
6123 int otherEnd = get_field_end (other);
6124 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
6127 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
6130 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
6138 field_hash (gconstpointer key)
6140 const MonoClassField *field = (const MonoClassField *)key;
6141 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
6145 field_equals (gconstpointer _a, gconstpointer _b)
6147 const MonoClassField *a = (const MonoClassField *)_a;
6148 const MonoClassField *b = (const MonoClassField *)_b;
6149 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
6154 verify_class_fields (MonoClass *klass)
6156 gpointer iter = NULL;
6157 MonoClassField *field;
6158 MonoGenericContext *context = mono_class_get_context (klass);
6159 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
6160 if (klass->generic_container)
6161 context = &klass->generic_container->context;
6163 while ((field = mono_class_get_fields (klass, &iter)) != NULL) {
6164 if (!mono_type_is_valid_type_in_context (field->type, context)) {
6165 g_hash_table_destroy (unique_fields);
6168 if (g_hash_table_lookup (unique_fields, field)) {
6169 g_hash_table_destroy (unique_fields);
6172 g_hash_table_insert (unique_fields, field, field);
6174 g_hash_table_destroy (unique_fields);
6179 verify_interfaces (MonoClass *klass)
6182 for (i = 0; i < klass->interface_count; ++i) {
6183 MonoClass *iface = klass->interfaces [i];
6184 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
6191 verify_valuetype_layout_with_target (MonoClass *klass, MonoClass *target_class)
6194 gpointer iter = NULL;
6195 MonoClassField *field;
6196 MonoClass *field_class;
6198 if (!klass->valuetype)
6201 type = klass->byval_arg.type;
6202 /*primitive type fields are not properly decoded*/
6203 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
6206 while ((field = mono_class_get_fields (klass, &iter)) != NULL) {
6210 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
6213 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
6215 if (field_class == target_class || klass == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
6223 verify_valuetype_layout (MonoClass *klass)
6226 res = verify_valuetype_layout_with_target (klass, klass);
6231 recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
6234 MonoClass **constraints;
6235 MonoGenericParamInfo *param_info;
6237 g_assert (mono_type_is_generic_argument (type));
6239 idx = mono_type_get_generic_param_num (type);
6240 if (mono_bitset_test_fast (used_args, idx))
6243 mono_bitset_set_fast (used_args, idx);
6244 param_info = mono_generic_container_get_param_info (gc, idx);
6246 if (!param_info->constraints)
6249 for (constraints = param_info->constraints; *constraints; ++constraints) {
6250 MonoClass *ctr = *constraints;
6251 MonoType *constraint_type = &ctr->byval_arg;
6253 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6260 verify_generic_parameters (MonoClass *klass)
6263 MonoGenericContainer *gc = klass->generic_container;
6264 MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
6266 for (i = 0; i < gc->type_argc; ++i) {
6267 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
6268 MonoClass **constraints;
6270 if (!param_info->constraints)
6273 mono_bitset_clear_all (used_args);
6274 mono_bitset_set_fast (used_args, i);
6276 for (constraints = param_info->constraints; *constraints; ++constraints) {
6277 MonoClass *ctr = *constraints;
6278 MonoType *constraint_type = &ctr->byval_arg;
6280 if (!mono_class_can_access_class (klass, ctr))
6283 if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
6286 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6288 if (ctr->generic_class && !mono_class_is_valid_generic_instantiation (NULL, ctr))
6292 mono_bitset_free (used_args);
6296 mono_bitset_free (used_args);
6301 * Check if the class is verifiable.
6303 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6304 * field and invalid generic instantiation are fatal errors.
6306 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6310 mono_verifier_verify_class (MonoClass *klass)
6312 /*Neither <Module>, object or ifaces have parent.*/
6313 if (!klass->parent &&
6314 klass != mono_defaults.object_class &&
6315 !MONO_CLASS_IS_INTERFACE (klass) &&
6316 (!image_is_dynamic (klass->image) && klass->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
6318 if (klass->parent) {
6319 if (MONO_CLASS_IS_INTERFACE (klass->parent))
6321 if (!klass->generic_class && klass->parent->generic_container)
6323 if (klass->parent->generic_class && !klass->generic_class) {
6324 MonoGenericContext *context = mono_class_get_context (klass);
6325 if (klass->generic_container)
6326 context = &klass->generic_container->context;
6327 if (!mono_type_is_valid_type_in_context (&klass->parent->byval_arg, context))
6331 if (klass->generic_container && (klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
6333 if (klass->generic_container && !verify_generic_parameters (klass))
6335 if (!verify_class_for_overlapping_reference_fields (klass))
6337 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
6339 if (klass->generic_class == NULL && !verify_class_fields (klass))
6341 if (klass->valuetype && !verify_valuetype_layout (klass))
6343 if (!verify_interfaces (klass))
6349 mono_verifier_class_is_valid_generic_instantiation (MonoClass *klass)
6351 return mono_class_is_valid_generic_instantiation (NULL, klass);
6355 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)
6357 if (!method->is_inflated)
6359 return mono_method_is_valid_generic_instantiation (NULL, method);
6365 mono_verifier_verify_class (MonoClass *klass)
6367 /* The verifier was disabled at compile time */
6372 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
6374 /* The verifier was disabled at compile time */
6379 mono_verifier_is_class_full_trust (MonoClass *klass)
6381 /* The verifier was disabled at compile time */
6386 mono_verifier_is_method_full_trust (MonoMethod *method)
6388 /* The verifier was disabled at compile time */
6393 mono_verifier_is_enabled_for_image (MonoImage *image)
6395 /* The verifier was disabled at compile time */
6400 mono_verifier_is_enabled_for_class (MonoClass *klass)
6402 /* The verifier was disabled at compile time */
6407 mono_verifier_is_enabled_for_method (MonoMethod *method)
6409 /* The verifier was disabled at compile time */
6414 mono_method_verify (MonoMethod *method, int level)
6416 /* The verifier was disabled at compile time */
6421 mono_free_verify_list (GSList *list)
6423 /* The verifier was disabled at compile time */
6424 /* will always be null if verifier is disabled */
6428 mono_verifier_class_is_valid_generic_instantiation (MonoClass *klass)
6434 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)