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 = 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 = 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 *class = mono_method_get_wrapper_data (ctx->method, (guint32)token);
1010 type = class ? &class->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) {
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 (!(class = 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 (class->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 (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2451 if (recursive_boxed_constraint_type_check (ctx, type, *class, 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 (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2484 /*256 should be enough since there can't be more than 255 generic arguments.*/
2485 if (recursive_boxed_constraint_type_check (ctx, type, *class, 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 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2522 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2524 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2528 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2530 if (candidate->byref ^ target->byref)
2533 switch (target->type) {
2534 case MONO_TYPE_VOID:
2537 case MONO_TYPE_BOOLEAN:
2540 case MONO_TYPE_CHAR:
2549 case MONO_TYPE_STRING:
2550 case MONO_TYPE_TYPEDBYREF:
2551 return candidate->type == target->type;
2554 if (candidate->type != MONO_TYPE_PTR)
2556 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2558 case MONO_TYPE_FNPTR:
2559 if (candidate->type != MONO_TYPE_FNPTR)
2561 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2563 case MONO_TYPE_GENERICINST: {
2564 MonoClass *target_klass;
2565 MonoClass *candidate_klass;
2566 target_klass = mono_class_from_mono_type (target);
2567 candidate_klass = mono_class_from_mono_type (candidate);
2568 /*FIXME handle nullables and enum*/
2569 return verifier_class_is_assignable_from (target_klass, candidate_klass);
2571 case MONO_TYPE_OBJECT:
2572 return MONO_TYPE_IS_REFERENCE (candidate);
2574 case MONO_TYPE_CLASS:
2575 return verifier_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2577 case MONO_TYPE_SZARRAY:
2578 if (candidate->type != MONO_TYPE_SZARRAY)
2580 return verifier_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2582 case MONO_TYPE_ARRAY:
2583 if (candidate->type != MONO_TYPE_ARRAY)
2585 return is_array_type_compatible (target, candidate);
2587 case MONO_TYPE_VALUETYPE:
2588 /*FIXME handle nullables and enum*/
2589 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2592 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2595 case MONO_TYPE_MVAR:
2596 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2600 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2601 g_assert_not_reached ();
2607 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2609 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2612 return mono_delegate_type_equal (method, delegate);
2616 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2618 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2621 return mono_delegate_type_equal (delegate, method);
2625 * mono_delegate_signature_equal:
2627 * Compare two signatures in the way expected by delegates.
2629 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2631 * FIXME can this function be eliminated and proper metadata functionality be used?
2634 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2637 int method_offset = is_static_ldftn ? 1 : 0;
2639 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2642 if (delegate_sig->call_convention != method_sig->call_convention)
2645 for (i = 0; i < delegate_sig->param_count; i++) {
2646 MonoType *p1 = delegate_sig->params [i];
2647 MonoType *p2 = method_sig->params [i + method_offset];
2649 if (!mono_delegate_param_equal (p1, p2))
2653 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2660 mono_verifier_is_signature_compatible (MonoMethodSignature *target, MonoMethodSignature *candidate)
2662 return mono_delegate_signature_equal (target, candidate, FALSE);
2666 * verify_ldftn_delegate:
2668 * Verify properties of ldftn based delegates.
2671 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2673 MonoMethod *method = funptr->method;
2675 /*ldftn non-final virtuals only allowed if method is not static,
2676 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2677 * This rules doesn't apply if the object on stack is a boxed valuetype.
2679 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2680 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2681 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2682 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2684 /*current method must not be static*/
2685 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2686 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2688 /*value is the this pointer, loaded using ldarg.0 */
2689 if (!stack_slot_is_this_pointer (value))
2690 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));
2692 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2697 * verify_delegate_compatibility:
2699 * Verify delegate creation sequence.
2703 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2705 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2706 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2708 MonoMethod *invoke, *method;
2709 const guint8 *ip = ctx->header->code;
2710 guint32 ip_offset = ctx->ip_offset;
2711 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2713 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2714 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2718 invoke = mono_get_delegate_invoke (delegate);
2719 method = funptr->method;
2721 if (!method || !mono_method_signature (method)) {
2722 char *name = mono_type_get_full_name (delegate);
2723 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2728 if (!invoke || !mono_method_signature (invoke)) {
2729 char *name = mono_type_get_full_name (delegate);
2730 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2735 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2737 if (is_static_ldftn)
2738 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2740 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2741 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2742 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2743 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));
2745 g_free (invoke_sig);
2749 * Delegate code sequences:
2755 * [-6] ldvirtftn token
2759 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2760 verify_ldftn_delegate (ctx, delegate, value, funptr);
2761 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2762 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2764 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2766 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2769 if (is_first_arg_bound) {
2770 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2771 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2773 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2774 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2775 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2777 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2778 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2782 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2783 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2785 #undef IS_VALID_OPCODE
2786 #undef IS_LOAD_FUN_PTR
2789 /* implement the opcode checks*/
2791 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2795 if (arg >= ctx->max_args) {
2797 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2799 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2800 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2801 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2803 } else if (check_overflow (ctx)) {
2804 /*We must let the value be pushed, otherwise we would get an underflow error*/
2805 check_unverifiable_type (ctx, ctx->params [arg]);
2806 if (ctx->params [arg]->byref && take_addr)
2807 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2808 top = stack_push (ctx);
2809 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2812 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2814 ctx->has_this_store = TRUE;
2816 top->stype |= THIS_POINTER_MASK;
2817 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2818 top->stype |= UNINIT_THIS_MASK;
2824 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2826 if (arg >= ctx->num_locals) {
2827 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2828 } else if (check_overflow (ctx)) {
2829 /*We must let the value be pushed, otherwise we would get an underflow error*/
2830 check_unverifiable_type (ctx, ctx->locals [arg]);
2831 if (ctx->locals [arg]->byref && take_addr)
2832 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2834 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2839 store_arg (VerifyContext *ctx, guint32 arg)
2843 if (arg >= ctx->max_args) {
2844 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2845 if (check_underflow (ctx, 1))
2850 if (check_underflow (ctx, 1)) {
2851 value = stack_pop (ctx);
2852 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2853 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2856 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2857 ctx->has_this_store = 1;
2861 store_local (VerifyContext *ctx, guint32 arg)
2864 if (arg >= ctx->num_locals) {
2865 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2869 /*TODO verify definite assigment */
2870 if (!check_underflow (ctx, 1))
2873 value = stack_pop (ctx);
2874 if (ctx->locals [arg]->byref && stack_slot_is_managed_mutability_pointer (value))
2875 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));
2877 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2878 char *expected = mono_type_full_name (ctx->locals [arg]);
2879 char *found = stack_slot_full_name (value);
2880 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2890 /*FIXME add and sub needs special care here*/
2892 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2894 ILStackDesc *a, *b, *top;
2895 int idxa, idxb, complexMerge = 0;
2898 if (!check_underflow (ctx, 2))
2900 b = stack_pop (ctx);
2901 a = stack_pop (ctx);
2903 idxa = stack_slot_get_underlying_type (a);
2904 if (stack_slot_is_managed_pointer (a)) {
2909 idxb = stack_slot_get_underlying_type (b);
2910 if (stack_slot_is_managed_pointer (b)) {
2917 res = table [idxa][idxb];
2919 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2920 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2922 top = stack_push (ctx);
2923 if (res == TYPE_INV) {
2924 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)));
2925 copy_stack_value (top, a);
2929 if (res & NON_VERIFIABLE_RESULT) {
2930 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)));
2932 res = res & ~NON_VERIFIABLE_RESULT;
2935 if (complexMerge && res == TYPE_PTR) {
2936 if (complexMerge == 1)
2937 copy_stack_value (top, a);
2938 else if (complexMerge == 2)
2939 copy_stack_value (top, b);
2941 * There is no need to merge the type of two pointers.
2942 * The only valid operation is subtraction, that returns a native
2943 * int as result and can be used with any 2 pointer kinds.
2944 * This is valid acording to Patition III 1.1.4
2953 do_boolean_branch_op (VerifyContext *ctx, int delta)
2955 int target = ctx->ip_offset + delta;
2958 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2960 if (target < 0 || target >= ctx->code_size) {
2961 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2965 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2967 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2970 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2974 ctx->target = target;
2976 if (!check_underflow (ctx, 1))
2979 top = stack_pop (ctx);
2980 if (!is_valid_bool_arg (top))
2981 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));
2983 check_unmanaged_pointer (ctx, top);
2987 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2989 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2993 stack_slot_is_reference_value (ILStackDesc *slot)
2995 return stack_slot_get_type (slot) == TYPE_COMPLEX && (MONO_TYPE_IS_REFERENCE (slot->type) || stack_slot_is_boxed_value (slot));
2999 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
3004 int target = ctx->ip_offset + delta;
3006 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3008 if (target < 0 || target >= ctx->code_size) {
3009 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3013 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3014 case 1: /*FIXME use constants and not magic numbers.*/
3015 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3018 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3022 ctx->target = target;
3024 if (!check_underflow (ctx, 2))
3027 b = stack_pop (ctx);
3028 a = stack_pop (ctx);
3030 idxa = stack_slot_get_underlying_type (a);
3031 if (stack_slot_is_managed_pointer (a))
3034 idxb = stack_slot_get_underlying_type (b);
3035 if (stack_slot_is_managed_pointer (b))
3038 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3043 res = table [idxa][idxb];
3046 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
3047 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3049 if (res == TYPE_INV) {
3050 CODE_NOT_VERIFIABLE (ctx,
3051 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));
3052 } else if (res & NON_VERIFIABLE_RESULT) {
3053 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));
3054 res = res & ~NON_VERIFIABLE_RESULT;
3059 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
3065 if (!check_underflow (ctx, 2))
3067 b = stack_pop (ctx);
3068 a = stack_pop (ctx);
3070 if (opcode == CEE_CGT_UN) {
3071 if ((stack_slot_is_reference_value (a) && stack_slot_is_null_literal (b)) ||
3072 (stack_slot_is_reference_value (b) && stack_slot_is_null_literal (a))) {
3073 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3078 idxa = stack_slot_get_underlying_type (a);
3079 if (stack_slot_is_managed_pointer (a))
3082 idxb = stack_slot_get_underlying_type (b);
3083 if (stack_slot_is_managed_pointer (b))
3086 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3091 res = table [idxa][idxb];
3094 if(res == TYPE_INV) {
3095 char *left_type = stack_slot_full_name (a);
3096 char *right_type = stack_slot_full_name (b);
3097 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));
3099 g_free (right_type);
3100 } else if (res & NON_VERIFIABLE_RESULT) {
3101 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));
3102 res = res & ~NON_VERIFIABLE_RESULT;
3104 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3108 do_ret (VerifyContext *ctx)
3110 MonoType *ret = ctx->signature->ret;
3111 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3112 if (ret->type != MONO_TYPE_VOID) {
3114 if (!check_underflow (ctx, 1))
3117 top = stack_pop(ctx);
3119 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3120 char *ret_type = mono_type_full_name (ctx->signature->ret);
3121 char *stack_type = stack_slot_full_name (top);
3122 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));
3123 g_free (stack_type);
3128 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3129 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3132 if (ctx->eval.size > 0) {
3133 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3135 if (in_any_block (ctx->header, ctx->ip_offset))
3136 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3140 * 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.
3141 * This is illegal but mono_get_method_full decoded it.
3142 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3145 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
3149 MonoMethodSignature *sig;
3152 gboolean virt_check_this = FALSE;
3153 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3155 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
3159 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3161 if (method->klass->valuetype) // && !constrained ???
3162 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3164 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3165 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3168 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3169 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3171 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
3172 virt_check_this = TRUE;
3173 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3177 if (!(sig = mono_method_get_signature_checked (method, ctx->image, method_token, ctx->generic_context, &error))) {
3178 mono_error_cleanup (&error);
3179 sig = mono_method_get_signature_checked (method, ctx->image, method_token, NULL, &error);
3183 char *name = mono_type_get_full_name (method->klass);
3184 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)));
3185 mono_error_cleanup (&error);
3190 param_count = sig->param_count + sig->hasthis;
3191 if (!check_underflow (ctx, param_count))
3194 for (i = sig->param_count - 1; i >= 0; --i) {
3195 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3196 value = stack_pop (ctx);
3197 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
3198 char *stack_name = stack_slot_full_name (value);
3199 char *sig_name = mono_type_full_name (sig->params [i]);
3200 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));
3201 g_free (stack_name);
3205 if (stack_slot_is_managed_mutability_pointer (value))
3206 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));
3208 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3209 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));
3215 MonoType *type = &method->klass->byval_arg;
3218 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3219 if (IS_STRICT_MODE (ctx) && !mono_method_is_constructor (ctx->method))
3220 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3221 if (IS_STRICT_MODE (ctx) && method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3222 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));
3224 ctx->super_ctor_called = TRUE;
3225 value = stack_pop_safe (ctx);
3226 if (IS_STRICT_MODE (ctx) && (value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3227 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3229 value = stack_pop (ctx);
3232 copy_stack_value (©, value);
3233 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3234 //Other parts of the code suffer from the same issue of
3235 copy.type = mono_type_get_type_byval (copy.type);
3236 copy.stype &= ~POINTER_MASK;
3238 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3239 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));
3241 if (constrained && virtual) {
3242 if (!stack_slot_is_managed_pointer (value))
3243 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3244 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), mono_type_get_underlying_type (ctx->constrained_type), TRUE))
3245 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3246 copy.stype |= BOXED_MASK;
3247 copy.type = ctx->constrained_type;
3249 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3250 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));
3252 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3253 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3255 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3256 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3258 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3259 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));
3261 if (!verify_stack_type_compatibility (ctx, type, ©)) {
3262 char *expected = mono_type_full_name (type);
3263 char *effective = stack_slot_full_name (©);
3264 char *method_name = mono_method_full_name (method, TRUE);
3265 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",
3266 expected, effective, method_name, ctx->ip_offset));
3267 g_free (method_name);
3272 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
3273 char *name = mono_method_full_name (method, TRUE);
3274 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3278 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
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 if (sig->ret->type != MONO_TYPE_VOID) {
3285 if (!mono_type_is_valid_in_context (ctx, sig->ret))
3288 if (check_overflow (ctx)) {
3289 value = stack_push (ctx);
3290 set_stack_value (ctx, value, sig->ret, FALSE);
3291 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3292 ctx->prefix_set &= ~PREFIX_READONLY;
3293 value->stype |= CMMP_MASK;
3298 if ((ctx->prefix_set & PREFIX_TAIL)) {
3299 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3300 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3301 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3302 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3308 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3310 MonoClassField *field;
3312 if (!check_overflow (ctx))
3315 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3317 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3320 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3321 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3324 /*taking the address of initonly field only works from the static constructor */
3325 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3326 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3327 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3329 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3330 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3332 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3336 do_store_static_field (VerifyContext *ctx, int token) {
3337 MonoClassField *field;
3340 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3342 if (!check_underflow (ctx, 1))
3345 value = stack_pop (ctx);
3347 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3350 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3351 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3355 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3356 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3360 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3361 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3363 if (!verify_stack_type_compatibility (ctx, field->type, value)) {
3364 char *stack_name = stack_slot_full_name (value);
3365 char *field_name = mono_type_full_name (field->type);
3366 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3367 field_name, stack_name, ctx->ip_offset));
3368 g_free (field_name);
3369 g_free (stack_name);
3374 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3376 MonoClassField *field;
3378 gboolean is_pointer;
3380 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3381 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3385 //the value on stack is going to be used as a pointer
3386 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3388 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3389 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3392 g_assert (obj->type);
3394 /*The value on the stack must be a subclass of the defining type of the field*/
3395 /* we need to check if we can load the field from the stack value*/
3397 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3398 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3400 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3401 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3403 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3404 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));
3406 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3407 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3408 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));
3410 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3411 char *found = stack_slot_full_name (obj);
3412 char *expected = mono_type_full_name (&field->parent->byval_arg);
3413 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));
3418 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3419 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3422 check_unmanaged_pointer (ctx, obj);
3427 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3430 MonoClassField *field;
3433 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3435 if (!check_underflow (ctx, 1))
3437 obj = stack_pop_safe (ctx);
3439 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3442 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3443 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3445 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3446 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3447 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3449 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3453 do_store_field (VerifyContext *ctx, int token)
3455 ILStackDesc *value, *obj;
3456 MonoClassField *field;
3457 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3459 if (!check_underflow (ctx, 2))
3462 value = stack_pop (ctx);
3463 obj = stack_pop_safe (ctx);
3465 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3468 if (!verify_stack_type_compatibility (ctx, field->type, value))
3469 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3472 /*TODO proper handle for Nullable<T>*/
3474 do_box_value (VerifyContext *ctx, int klass_token)
3477 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3483 if (!check_underflow (ctx, 1))
3486 value = stack_pop (ctx);
3487 /*box is a nop for reference types*/
3489 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3490 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3495 if (!verify_stack_type_compatibility (ctx, type, value))
3496 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3498 klass = mono_class_from_mono_type (type);
3499 if (mono_class_is_nullable (klass))
3500 type = &mono_class_get_nullable_param (klass)->byval_arg;
3501 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3505 do_unbox_value (VerifyContext *ctx, int klass_token)
3508 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3513 if (!check_underflow (ctx, 1))
3516 if (!mono_class_from_mono_type (type)->valuetype)
3517 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3519 value = stack_pop (ctx);
3521 /*Value should be: a boxed valuetype or a reference type*/
3522 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3523 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3524 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));
3526 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3527 value->stype |= CMMP_MASK;
3531 do_unbox_any (VerifyContext *ctx, int klass_token)
3534 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3539 if (!check_underflow (ctx, 1))
3542 value = stack_pop (ctx);
3544 /*Value should be: a boxed valuetype or a reference type*/
3545 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3546 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3547 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));
3549 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3553 do_unary_math_op (VerifyContext *ctx, int op)
3556 if (!check_underflow (ctx, 1))
3558 value = stack_pop (ctx);
3559 switch (stack_slot_get_type (value)) {
3562 case TYPE_NATIVE_INT:
3567 case TYPE_COMPLEX: /*only enums are ok*/
3568 if (mono_type_is_enum_type (value->type))
3571 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3573 stack_push_stack_val (ctx, value);
3577 do_conversion (VerifyContext *ctx, int kind)
3580 if (!check_underflow (ctx, 1))
3582 value = stack_pop (ctx);
3584 switch (stack_slot_get_type (value)) {
3587 case TYPE_NATIVE_INT:
3591 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));
3596 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3599 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3602 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3604 case TYPE_NATIVE_INT:
3605 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3608 g_error ("unknown type %02x in conversion", kind);
3614 do_load_token (VerifyContext *ctx, int token)
3618 MonoClass *handle_class;
3619 if (!check_overflow (ctx))
3622 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
3623 handle = mono_method_get_wrapper_data (ctx->method, token);
3624 handle_class = mono_method_get_wrapper_data (ctx->method, token + 1);
3625 if (handle_class == mono_defaults.typehandle_class)
3626 handle = &((MonoClass*)handle)->byval_arg;
3628 switch (token & 0xff000000) {
3629 case MONO_TOKEN_TYPE_DEF:
3630 case MONO_TOKEN_TYPE_REF:
3631 case MONO_TOKEN_TYPE_SPEC:
3632 case MONO_TOKEN_FIELD_DEF:
3633 case MONO_TOKEN_METHOD_DEF:
3634 case MONO_TOKEN_METHOD_SPEC:
3635 case MONO_TOKEN_MEMBER_REF:
3636 if (!token_bounds_check (ctx->image, token)) {
3637 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));
3642 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));
3646 handle = mono_ldtoken_checked (ctx->image, token, &handle_class, ctx->generic_context, &error);
3650 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)));
3651 mono_error_cleanup (&error);
3654 if (handle_class == mono_defaults.typehandle_class) {
3655 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3656 } else if (handle_class == mono_defaults.methodhandle_class) {
3657 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3658 } else if (handle_class == mono_defaults.fieldhandle_class) {
3659 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3661 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3663 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3667 do_ldobj_value (VerifyContext *ctx, int token)
3670 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3671 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3676 if (!check_underflow (ctx, 1))
3679 value = stack_pop (ctx);
3680 if (!stack_slot_is_managed_pointer (value)
3681 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3682 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3683 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3687 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3688 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3690 /*We have a byval on the stack, but the comparison must be strict. */
3691 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3692 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3694 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3698 do_stobj (VerifyContext *ctx, int token)
3700 ILStackDesc *dest, *src;
3701 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3702 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3707 if (!check_underflow (ctx, 2))
3710 src = stack_pop (ctx);
3711 dest = stack_pop (ctx);
3713 if (stack_slot_is_managed_mutability_pointer (dest))
3714 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3716 if (!stack_slot_is_managed_pointer (dest))
3717 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3719 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3720 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));
3722 if (!verify_stack_type_compatibility (ctx, type, src)) {
3723 char *type_name = mono_type_full_name (type);
3724 char *src_name = stack_slot_full_name (src);
3725 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));
3730 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3731 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3735 do_cpobj (VerifyContext *ctx, int token)
3737 ILStackDesc *dest, *src;
3738 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3742 if (!check_underflow (ctx, 2))
3745 src = stack_pop (ctx);
3746 dest = stack_pop (ctx);
3748 if (!stack_slot_is_managed_pointer (src))
3749 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3751 if (!stack_slot_is_managed_pointer (dest))
3752 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3754 if (stack_slot_is_managed_mutability_pointer (dest))
3755 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3757 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3758 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3760 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3761 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3765 do_initobj (VerifyContext *ctx, int token)
3768 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3772 if (!check_underflow (ctx, 1))
3775 obj = stack_pop (ctx);
3777 if (!stack_slot_is_managed_pointer (obj))
3778 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3780 if (stack_slot_is_managed_mutability_pointer (obj))
3781 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3783 stack = mono_type_get_type_byval (obj->type);
3784 if (MONO_TYPE_IS_REFERENCE (stack)) {
3785 if (!verify_type_compatibility (ctx, stack, type))
3786 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3787 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3788 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3789 } else if (!verify_type_compatibility (ctx, stack, type)) {
3790 char *expected_name = mono_type_full_name (type);
3791 char *stack_name = mono_type_full_name (stack);
3793 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));
3794 g_free (expected_name);
3795 g_free (stack_name);
3800 do_newobj (VerifyContext *ctx, int token)
3804 MonoMethodSignature *sig;
3806 gboolean is_delegate = FALSE;
3808 if (!(method = verifier_load_method (ctx, token, "newobj")))
3811 if (!mono_method_is_constructor (method)) {
3812 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3816 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3817 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3819 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3820 char *from = mono_method_full_name (ctx->method, TRUE);
3821 char *to = mono_method_full_name (method, TRUE);
3822 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);
3827 //FIXME use mono_method_get_signature_full
3828 sig = mono_method_signature (method);
3830 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3834 if (!sig->hasthis) {
3835 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature missing hasthis at 0x%04x", ctx->ip_offset));
3839 if (!check_underflow (ctx, sig->param_count))
3842 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3845 ILStackDesc *funptr;
3846 //first arg is object, second arg is fun ptr
3847 if (sig->param_count != 2) {
3848 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3851 funptr = stack_pop (ctx);
3852 value = stack_pop (ctx);
3853 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3855 for (i = sig->param_count - 1; i >= 0; --i) {
3856 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3857 value = stack_pop (ctx);
3858 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3859 char *stack_name = stack_slot_full_name (value);
3860 char *sig_name = mono_type_full_name (sig->params [i]);
3861 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));
3862 g_free (stack_name);
3866 if (stack_slot_is_managed_mutability_pointer (value))
3867 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3871 if (check_overflow (ctx))
3872 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3876 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3882 if (!check_underflow (ctx, 1))
3885 if (!(type = get_boxable_mono_type (ctx, token, opcode)))
3889 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3893 value = stack_pop (ctx);
3894 is_boxed = stack_slot_is_boxed_value (value);
3896 if (stack_slot_is_managed_pointer (value))
3897 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3898 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3899 char *name = stack_slot_full_name (value);
3900 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));
3904 switch (value->type->type) {
3905 case MONO_TYPE_FNPTR:
3907 case MONO_TYPE_TYPEDBYREF:
3908 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3913 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3914 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3918 mono_type_from_opcode (int opcode) {
3926 return &mono_defaults.sbyte_class->byval_arg;
3934 return &mono_defaults.int16_class->byval_arg;
3942 return &mono_defaults.int32_class->byval_arg;
3948 return &mono_defaults.int64_class->byval_arg;
3954 return &mono_defaults.single_class->byval_arg;
3960 return &mono_defaults.double_class->byval_arg;
3966 return &mono_defaults.int_class->byval_arg;
3970 case CEE_LDELEM_REF:
3971 case CEE_STELEM_REF:
3972 return &mono_defaults.object_class->byval_arg;
3975 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3981 do_load_indirect (VerifyContext *ctx, int opcode)
3984 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3986 if (!check_underflow (ctx, 1))
3989 value = stack_pop (ctx);
3990 if (!stack_slot_is_managed_pointer (value)) {
3991 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3992 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3996 if (opcode == CEE_LDIND_REF) {
3997 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3998 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3999 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
4001 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
4002 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4003 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4008 do_store_indirect (VerifyContext *ctx, int opcode)
4010 ILStackDesc *addr, *val;
4011 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
4013 if (!check_underflow (ctx, 2))
4016 val = stack_pop (ctx);
4017 addr = stack_pop (ctx);
4019 check_unmanaged_pointer (ctx, addr);
4021 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
4022 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
4026 if (stack_slot_is_managed_mutability_pointer (addr)) {
4027 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
4031 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
4032 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4034 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
4035 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4039 do_newarr (VerifyContext *ctx, int token)
4042 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
4047 if (!check_underflow (ctx, 1))
4050 value = stack_pop (ctx);
4051 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4052 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));
4054 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
4057 /*FIXME handle arrays that are not 0-indexed*/
4059 do_ldlen (VerifyContext *ctx)
4063 if (!check_underflow (ctx, 1))
4066 value = stack_pop (ctx);
4068 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
4069 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
4071 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
4074 /*FIXME handle arrays that are not 0-indexed*/
4075 /*FIXME handle readonly prefix and CMMP*/
4077 do_ldelema (VerifyContext *ctx, int klass_token)
4079 ILStackDesc *index, *array, *res;
4080 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
4086 if (!check_underflow (ctx, 2))
4089 index = stack_pop (ctx);
4090 array = stack_pop (ctx);
4092 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4093 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));
4095 if (!stack_slot_is_null_literal (array)) {
4096 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4097 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4099 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
4100 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
4102 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
4105 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
4109 res = stack_push (ctx);
4110 set_stack_value (ctx, res, type, TRUE);
4111 if (ctx->prefix_set & PREFIX_READONLY) {
4112 ctx->prefix_set &= ~PREFIX_READONLY;
4113 res->stype |= CMMP_MASK;
4118 * FIXME handle arrays that are not 0-indexed
4119 * FIXME handle readonly prefix and CMMP
4122 do_ldelem (VerifyContext *ctx, int opcode, int token)
4124 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4125 ILStackDesc *index, *array;
4127 if (!check_underflow (ctx, 2))
4130 if (opcode == CEE_LDELEM) {
4131 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
4132 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4136 type = mono_type_from_opcode (opcode);
4139 index = stack_pop (ctx);
4140 array = stack_pop (ctx);
4142 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4143 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));
4145 if (!stack_slot_is_null_literal (array)) {
4146 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4147 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));
4149 if (opcode == CEE_LDELEM_REF) {
4150 if (array->type->data.klass->valuetype)
4151 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
4152 type = &array->type->data.klass->byval_arg;
4154 MonoType *candidate = &array->type->data.klass->byval_arg;
4155 if (IS_STRICT_MODE (ctx)) {
4156 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
4157 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
4158 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)) ||
4159 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
4160 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4162 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
4163 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4168 set_stack_value (ctx, stack_push (ctx), type, FALSE);
4173 * FIXME handle arrays that are not 0-indexed
4176 do_stelem (VerifyContext *ctx, int opcode, int token)
4178 ILStackDesc *index, *array, *value;
4180 if (!check_underflow (ctx, 3))
4183 if (opcode == CEE_STELEM) {
4184 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
4185 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4189 type = mono_type_from_opcode (opcode);
4192 value = stack_pop (ctx);
4193 index = stack_pop (ctx);
4194 array = stack_pop (ctx);
4196 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4197 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));
4199 if (!stack_slot_is_null_literal (array)) {
4200 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
4201 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));
4203 if (opcode == CEE_STELEM_REF) {
4204 if (array->type->data.klass->valuetype)
4205 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4206 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
4207 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4211 if (opcode == CEE_STELEM_REF) {
4212 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
4213 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4214 } else if (opcode != CEE_STELEM_REF) {
4215 if (!verify_stack_type_compatibility (ctx, type, value))
4216 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4218 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
4219 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));
4225 do_throw (VerifyContext *ctx)
4227 ILStackDesc *exception;
4228 if (!check_underflow (ctx, 1))
4230 exception = stack_pop (ctx);
4232 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4233 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4235 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
4236 char *name = mono_type_full_name (exception->type);
4237 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));
4240 /*The stack is left empty after a throw*/
4246 do_endfilter (VerifyContext *ctx)
4248 MonoExceptionClause *clause;
4250 if (IS_STRICT_MODE (ctx)) {
4251 if (ctx->eval.size != 1)
4252 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4254 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4255 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4258 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4259 if (IS_STRICT_MODE (ctx)) {
4260 if (ctx->ip_offset != clause->handler_offset - 2)
4261 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4263 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4264 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4267 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4268 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4270 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4277 do_leave (VerifyContext *ctx, int delta)
4279 int target = ((gint32)ctx->ip_offset) + delta;
4280 if (target >= ctx->code_size || target < 0)
4281 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4283 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4284 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4286 ctx->target = target;
4292 * Verify br and br.s opcodes.
4295 do_static_branch (VerifyContext *ctx, int delta)
4297 int target = ctx->ip_offset + delta;
4298 if (target < 0 || target >= ctx->code_size) {
4299 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4303 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4305 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4308 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4312 ctx->target = target;
4316 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4318 int i, base = ctx->ip_offset + 5 + count * 4;
4321 if (!check_underflow (ctx, 1))
4324 value = stack_pop (ctx);
4326 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4327 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4329 for (i = 0; i < count; ++i) {
4330 int target = base + read32 (data + i * 4);
4332 if (target < 0 || target >= ctx->code_size) {
4333 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4337 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4339 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4342 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4345 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4350 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4355 if (virtual && !check_underflow (ctx, 1))
4358 if (!virtual && !check_overflow (ctx))
4361 if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
4362 method = mono_method_get_wrapper_data (ctx->method, (guint32)token);
4364 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4368 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4369 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4373 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4377 if (mono_method_is_constructor (method))
4378 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4381 ILStackDesc *top = stack_pop (ctx);
4383 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4384 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4386 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4387 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4389 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4390 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4393 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
4394 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);
4396 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4397 top->method = method;
4401 do_sizeof (VerifyContext *ctx, int token)
4405 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4408 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4409 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4413 if (type->type == MONO_TYPE_VOID) {
4414 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4418 if (check_overflow (ctx))
4419 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4422 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4424 do_localloc (VerifyContext *ctx)
4426 if (ctx->eval.size != 1) {
4427 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4431 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4432 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4436 /*TODO verify top type*/
4437 /* top = */ stack_pop (ctx);
4439 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4440 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4444 do_ldstr (VerifyContext *ctx, guint32 token)
4446 GSList *error = NULL;
4447 if (ctx->method->wrapper_type == MONO_WRAPPER_NONE && !image_is_dynamic (ctx->image)) {
4448 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4449 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4453 if (!mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4455 ctx->list = g_slist_concat (ctx->list, error);
4456 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4461 if (check_overflow (ctx))
4462 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4466 do_refanyval (VerifyContext *ctx, int token)
4470 if (!check_underflow (ctx, 1))
4473 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4476 top = stack_pop (ctx);
4478 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4479 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));
4481 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4485 do_refanytype (VerifyContext *ctx)
4489 if (!check_underflow (ctx, 1))
4492 top = stack_pop (ctx);
4494 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4495 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));
4497 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4502 do_mkrefany (VerifyContext *ctx, int token)
4506 if (!check_underflow (ctx, 1))
4509 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4512 top = stack_pop (ctx);
4514 if (stack_slot_is_managed_mutability_pointer (top))
4515 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4517 if (!stack_slot_is_managed_pointer (top)) {
4518 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));
4520 MonoType *stack_type = mono_type_get_type_byval (top->type);
4521 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4522 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4524 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4525 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4528 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4532 do_ckfinite (VerifyContext *ctx)
4535 if (!check_underflow (ctx, 1))
4538 top = stack_pop (ctx);
4540 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4541 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));
4542 stack_push_stack_val (ctx, top);
4546 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4548 * @from holds new values for a given control path
4549 * @to holds the current values of a given control path
4551 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4554 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4558 stack_init (ctx, to);
4561 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4564 stack_copy (&ctx->eval, to);
4566 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4567 stack_copy (to, &ctx->eval);
4570 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4572 if (from->size != to->size) {
4573 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4574 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));
4578 //FIXME we need to preserve CMMP attributes
4579 //FIXME we must take null literals into consideration.
4580 for (i = 0; i < from->size; ++i) {
4581 ILStackDesc *new_slot = from->stack + i;
4582 ILStackDesc *old_slot = to->stack + i;
4583 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4584 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4585 MonoClass *old_class = mono_class_from_mono_type (old_type);
4586 MonoClass *new_class = mono_class_from_mono_type (new_type);
4587 MonoClass *match_class = NULL;
4589 // S := T then U = S (new value is compatible with current value, keep current)
4590 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4591 copy_stack_value (new_slot, old_slot);
4595 // T := S then U = T (old value is compatible with current value, use new)
4596 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4597 copy_stack_value (old_slot, new_slot);
4601 /*Both slots are the same boxed valuetype. Simply copy it.*/
4602 if (stack_slot_is_boxed_value (old_slot) &&
4603 stack_slot_is_boxed_value (new_slot) &&
4604 mono_metadata_type_equal (old_type, new_type)) {
4605 copy_stack_value (new_slot, old_slot);
4609 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4610 char *old_name = stack_slot_full_name (old_slot);
4611 char *new_name = stack_slot_full_name (new_slot);
4612 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));
4618 //both are reference types, use closest common super type
4619 if (!mono_class_from_mono_type (old_type)->valuetype
4620 && !mono_class_from_mono_type (new_type)->valuetype
4621 && !stack_slot_is_managed_pointer (old_slot)
4622 && !stack_slot_is_managed_pointer (new_slot)) {
4624 mono_class_setup_supertypes (old_class);
4625 mono_class_setup_supertypes (new_class);
4627 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4628 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4629 match_class = old_class->supertypes [j];
4634 mono_class_setup_interfaces (old_class, &error);
4635 if (!mono_error_ok (&error)) {
4636 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));
4637 mono_error_cleanup (&error);
4640 mono_class_setup_interfaces (new_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);
4647 /* if old class is an interface that new class implements */
4648 if (old_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4649 if (verifier_class_is_assignable_from (old_class, new_class)) {
4650 match_class = old_class;
4653 for (j = 0; j < old_class->interface_count; ++j) {
4654 if (verifier_class_is_assignable_from (old_class->interfaces [j], new_class)) {
4655 match_class = old_class->interfaces [j];
4661 if (new_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
4662 if (verifier_class_is_assignable_from (new_class, old_class)) {
4663 match_class = new_class;
4666 for (j = 0; j < new_class->interface_count; ++j) {
4667 if (verifier_class_is_assignable_from (new_class->interfaces [j], old_class)) {
4668 match_class = new_class->interfaces [j];
4674 //No decent super type found, use object
4675 match_class = mono_defaults.object_class;
4677 } 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)) {
4678 match_class = mono_defaults.object_class;
4683 char *old_name = stack_slot_full_name (old_slot);
4684 char *new_name = stack_slot_full_name (new_slot);
4685 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));
4689 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4693 g_assert (match_class);
4694 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4695 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4701 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4702 to->flags |= IL_CODE_STACK_MERGED;
4705 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4706 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4709 * is_clause_in_range :
4711 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4714 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4716 if (clause->try_offset >= start && clause->try_offset < end)
4718 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4724 * is_clause_inside_range :
4726 * Returns TRUE if @clause lies completely inside the @start - @end range.
4729 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4731 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4733 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4739 * is_clause_nested :
4741 * Returns TRUE if @nested is nested in @clause.
4744 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4746 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4748 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4749 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4752 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4753 * the each pair of exception must have the following properties:
4754 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4755 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4756 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4759 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4761 /*clause is nested*/
4762 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4763 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4767 /*wrong nesting order.*/
4768 if (is_clause_nested (clause, to_test)) {
4769 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4773 /*mutual protection*/
4774 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4775 /*handlers are not disjoint*/
4776 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4777 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4780 /* handlers are not catch or filter */
4781 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4782 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4789 /*not completelly disjoint*/
4790 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4791 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4792 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4795 #define code_bounds_check(size) \
4796 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4797 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4802 mono_opcode_is_prefix (int op)
4805 case MONO_CEE_UNALIGNED_:
4806 case MONO_CEE_VOLATILE_:
4807 case MONO_CEE_TAIL_:
4808 case MONO_CEE_CONSTRAINED_:
4809 case MONO_CEE_READONLY_:
4816 * FIXME: need to distinguish between valid and verifiable.
4817 * Need to keep track of types on the stack.
4818 * Verify types for opcodes.
4821 mono_method_verify (MonoMethod *method, int level)
4824 const unsigned char *ip, *code_start;
4825 const unsigned char *end;
4826 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4828 int i, n, need_merge = 0, start = 0;
4829 guint ip_offset = 0, prefix = 0;
4830 MonoGenericContext *generic_context = NULL;
4834 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4836 init_verifier_stats ();
4838 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4839 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4843 memset (&ctx, 0, sizeof (VerifyContext));
4845 //FIXME use mono_method_get_signature_full
4846 ctx.signature = mono_method_signature (method);
4847 if (!ctx.signature) {
4848 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4850 finish_collect_stats ();
4853 if (!method->is_generic && !method->klass->is_generic && ctx.signature->has_type_parameters) {
4854 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method and signature don't match in terms of genericity"));
4855 finish_collect_stats ();
4859 ctx.header = mono_method_get_header (method);
4861 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4862 finish_collect_stats ();
4865 ctx.method = method;
4866 code_start = ip = ctx.header->code;
4867 end = ip + ctx.header->code_size;
4868 ctx.image = image = method->klass->image;
4871 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4872 ctx.max_stack = ctx.header->max_stack;
4873 ctx.verifiable = ctx.valid = 1;
4876 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4877 ctx.code_size = ctx.header->code_size;
4878 _MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4880 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4882 ctx.num_locals = ctx.header->num_locals;
4883 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4884 _MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4886 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4887 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4889 ctx.params = g_new (MonoType*, ctx.max_args);
4890 _MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4892 if (ctx.signature->hasthis)
4893 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4894 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4896 if (ctx.signature->is_inflated)
4897 ctx.generic_context = generic_context = mono_method_get_context (method);
4899 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4900 if (method->is_generic)
4901 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4903 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4906 for (i = 0; i < ctx.num_locals; ++i) {
4907 MonoType *uninflated = ctx.locals [i];
4908 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4909 if (!mono_error_ok (&error)) {
4910 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4911 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4913 mono_error_cleanup (&error);
4914 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4920 for (i = 0; i < ctx.max_args; ++i) {
4921 MonoType *uninflated = ctx.params [i];
4922 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4923 if (!mono_error_ok (&error)) {
4924 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4925 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4927 mono_error_cleanup (&error);
4928 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4933 stack_init (&ctx, &ctx.eval);
4935 for (i = 0; i < ctx.num_locals; ++i) {
4936 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4938 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4939 char *name = mono_type_full_name (ctx.locals [i]);
4940 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4947 for (i = 0; i < ctx.max_args; ++i) {
4948 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4951 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4952 char *name = mono_type_full_name (ctx.params [i]);
4953 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4962 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4963 MonoExceptionClause *clause = ctx.header->clauses + i;
4964 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); );
4966 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4967 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4969 if (clause->try_len <= 0)
4970 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4972 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4973 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4975 if (clause->handler_len <= 0)
4976 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4978 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4979 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4981 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4982 if (clause->data.filter_offset > ctx.code_size)
4983 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4985 if (clause->data.filter_offset >= clause->handler_offset)
4986 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4989 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4990 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4995 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4996 if (clause->try_offset + clause->try_len < ctx.code_size)
4997 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4998 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4999 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
5001 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
5002 if (!clause->data.catch_class) {
5003 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
5006 if (!mono_type_is_valid_in_context (&ctx, &clause->data.catch_class->byval_arg))
5009 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
5011 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5012 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
5013 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
5020 original_bb = bb = mono_basic_block_split (method, &error);
5021 if (!mono_error_ok (&error)) {
5022 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
5023 mono_error_cleanup (&error);
5028 while (ip < end && ctx.valid) {
5030 ip_offset = (guint) (ip - code_start);
5032 const unsigned char *ip_copy = ip;
5035 if (ip_offset > bb->end) {
5036 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
5040 if (ip_offset == bb->end)
5043 op_size = mono_opcode_value_and_size (&ip_copy, end, &op);
5044 if (op_size == -1) {
5045 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
5049 if (ADD_IS_GREATER_OR_OVF (ip_offset, op_size, bb->end)) {
5050 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
5054 /*Last Instruction*/
5055 if (ip_offset + op_size == bb->end && mono_opcode_is_prefix (op)) {
5056 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));
5061 ctx.ip_offset = ip_offset = (guint) (ip - code_start);
5063 /*We need to check against fallthrou in and out of protected blocks.
5064 * For fallout we check the once a protected block ends, if the start flag is not set.
5065 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5066 * TODO convert these checks to be done using flags and not this loop
5068 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
5069 MonoExceptionClause *clause = ctx.header->clauses + i;
5071 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
5072 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
5076 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
5077 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
5078 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5080 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5084 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
5085 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
5089 if (clause->handler_offset == ip_offset && start == 0) {
5090 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
5094 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
5095 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
5100 /*This must be done after fallthru detection otherwise it won't happen.*/
5102 /*FIXME remove this once we move all bad branch checking code to use BB only*/
5103 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
5112 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
5113 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
5116 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
5119 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5120 #ifdef MONO_VERIFIER_DEBUG
5123 discode = mono_disasm_code_one (NULL, method, ip, NULL);
5124 discode [strlen (discode) - 1] = 0; /* no \n */
5125 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
5128 dump_stack_state (&ctx.code [ip_offset]);
5129 dump_stack_state (&ctx.eval);
5142 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
5148 code_bounds_check (2);
5149 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
5153 case CEE_ADD_OVF_UN:
5154 do_binop (&ctx, *ip, add_ovf_un_table);
5158 case CEE_SUB_OVF_UN:
5159 do_binop (&ctx, *ip, sub_ovf_un_table);
5166 case CEE_MUL_OVF_UN:
5167 do_binop (&ctx, *ip, bin_ovf_table);
5172 do_binop (&ctx, *ip, add_table);
5177 do_binop (&ctx, *ip, sub_table);
5184 do_binop (&ctx, *ip, bin_op_table);
5193 do_binop (&ctx, *ip, int_bin_op_table);
5200 do_binop (&ctx, *ip, shift_op_table);
5205 if (!check_underflow (&ctx, 1))
5207 stack_pop_safe (&ctx);
5221 /*TODO support definite assignment verification? */
5222 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
5230 store_local (&ctx, *ip - CEE_STLOC_0);
5235 code_bounds_check (2);
5236 store_local (&ctx, ip [1]);
5241 code_bounds_check (2);
5242 store_arg (&ctx, ip [1]);
5256 if (check_overflow (&ctx))
5257 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5262 code_bounds_check (2);
5263 if (check_overflow (&ctx))
5264 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5269 code_bounds_check (5);
5270 if (check_overflow (&ctx))
5271 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
5276 code_bounds_check (9);
5277 if (check_overflow (&ctx))
5278 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
5283 code_bounds_check (5);
5284 if (check_overflow (&ctx))
5285 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5290 code_bounds_check (9);
5291 if (check_overflow (&ctx))
5292 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5297 if (check_overflow (&ctx))
5298 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
5304 code_bounds_check (2);
5305 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
5318 code_bounds_check (2);
5319 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5326 code_bounds_check (5);
5327 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5340 code_bounds_check (5);
5341 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5348 code_bounds_check (2);
5349 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5354 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5360 if (!check_underflow (&ctx, 1))
5362 if (!check_overflow (&ctx))
5364 top = stack_push (&ctx);
5365 copy_stack_value (top, stack_peek (&ctx, 1));
5371 code_bounds_check (5);
5373 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5374 /* token = read32 (ip + 1); */
5375 if (in_any_block (ctx.header, ip_offset))
5376 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5378 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5380 * FIXME: check signature, retval, arguments etc.
5386 code_bounds_check (5);
5387 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5392 code_bounds_check (5);
5393 /* token = read32 (ip + 1); */
5395 * FIXME: check signature, retval, arguments etc.
5396 * FIXME: check requirements for tail call
5398 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5402 code_bounds_check (2);
5403 do_static_branch (&ctx, (signed char)ip [1] + 2);
5411 code_bounds_check (2);
5412 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5418 code_bounds_check (5);
5419 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5427 code_bounds_check (5);
5428 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5435 code_bounds_check (5);
5436 entries = read32 (ip + 1);
5438 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5439 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5442 code_bounds_check (sizeof (guint32) * entries);
5444 do_switch (&ctx, entries, ip);
5445 ip += sizeof (guint32) * entries;
5459 do_load_indirect (&ctx, *ip);
5471 do_store_indirect (&ctx, *ip);
5477 do_unary_math_op (&ctx, *ip);
5487 do_conversion (&ctx, TYPE_I4);
5493 do_conversion (&ctx, TYPE_I8);
5500 do_conversion (&ctx, TYPE_R8);
5506 do_conversion (&ctx, TYPE_NATIVE_INT);
5511 code_bounds_check (5);
5512 do_cpobj (&ctx, read32 (ip + 1));
5517 code_bounds_check (5);
5518 do_ldobj_value (&ctx, read32 (ip + 1));
5523 code_bounds_check (5);
5524 do_ldstr (&ctx, read32 (ip + 1));
5529 code_bounds_check (5);
5530 do_newobj (&ctx, read32 (ip + 1));
5536 code_bounds_check (5);
5537 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5543 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5548 code_bounds_check (5);
5549 do_unbox_value (&ctx, read32 (ip + 1));
5561 code_bounds_check (5);
5562 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5568 code_bounds_check (5);
5569 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5574 code_bounds_check (5);
5575 do_store_field (&ctx, read32 (ip + 1));
5580 code_bounds_check (5);
5581 do_store_static_field (&ctx, read32 (ip + 1));
5586 code_bounds_check (5);
5587 do_stobj (&ctx, read32 (ip + 1));
5591 case CEE_CONV_OVF_I1_UN:
5592 case CEE_CONV_OVF_I2_UN:
5593 case CEE_CONV_OVF_I4_UN:
5594 case CEE_CONV_OVF_U1_UN:
5595 case CEE_CONV_OVF_U2_UN:
5596 case CEE_CONV_OVF_U4_UN:
5597 do_conversion (&ctx, TYPE_I4);
5601 case CEE_CONV_OVF_I8_UN:
5602 case CEE_CONV_OVF_U8_UN:
5603 do_conversion (&ctx, TYPE_I8);
5607 case CEE_CONV_OVF_I_UN:
5608 case CEE_CONV_OVF_U_UN:
5609 do_conversion (&ctx, TYPE_NATIVE_INT);
5614 code_bounds_check (5);
5615 do_box_value (&ctx, read32 (ip + 1));
5620 code_bounds_check (5);
5621 do_newarr (&ctx, read32 (ip + 1));
5631 code_bounds_check (5);
5632 do_ldelema (&ctx, read32 (ip + 1));
5646 case CEE_LDELEM_REF:
5647 do_ldelem (&ctx, *ip, 0);
5658 case CEE_STELEM_REF:
5659 do_stelem (&ctx, *ip, 0);
5664 code_bounds_check (5);
5665 do_ldelem (&ctx, *ip, read32 (ip + 1));
5670 code_bounds_check (5);
5671 do_stelem (&ctx, *ip, read32 (ip + 1));
5676 code_bounds_check (5);
5677 do_unbox_any (&ctx, read32 (ip + 1));
5681 case CEE_CONV_OVF_I1:
5682 case CEE_CONV_OVF_U1:
5683 case CEE_CONV_OVF_I2:
5684 case CEE_CONV_OVF_U2:
5685 case CEE_CONV_OVF_I4:
5686 case CEE_CONV_OVF_U4:
5687 do_conversion (&ctx, TYPE_I4);
5691 case CEE_CONV_OVF_I8:
5692 case CEE_CONV_OVF_U8:
5693 do_conversion (&ctx, TYPE_I8);
5697 case CEE_CONV_OVF_I:
5698 case CEE_CONV_OVF_U:
5699 do_conversion (&ctx, TYPE_NATIVE_INT);
5704 code_bounds_check (5);
5705 do_refanyval (&ctx, read32 (ip + 1));
5715 code_bounds_check (5);
5716 do_mkrefany (&ctx, read32 (ip + 1));
5721 code_bounds_check (5);
5722 do_load_token (&ctx, read32 (ip + 1));
5726 case CEE_ENDFINALLY:
5727 if (!is_correct_endfinally (ctx.header, ip_offset))
5728 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5735 code_bounds_check (5);
5736 do_leave (&ctx, read32 (ip + 1) + 5);
5743 code_bounds_check (2);
5744 do_leave (&ctx, (signed char)ip [1] + 2);
5751 code_bounds_check (2);
5755 code_bounds_check (3);
5756 store_local (&ctx, read16 (ip + 1));
5761 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5769 do_cmp_op (&ctx, cmp_br_op, *ip);
5774 code_bounds_check (3);
5775 store_arg (&ctx, read16 (ip + 1) );
5781 if (!check_overflow (&ctx))
5783 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5784 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5785 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5790 code_bounds_check (5);
5791 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5796 code_bounds_check (5);
5797 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5803 code_bounds_check (3);
5804 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5810 code_bounds_check (3);
5811 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5824 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5828 do_endfilter (&ctx);
5832 case CEE_UNALIGNED_:
5833 code_bounds_check (2);
5834 prefix |= PREFIX_UNALIGNED;
5838 prefix |= PREFIX_VOLATILE;
5842 prefix |= PREFIX_TAIL;
5844 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5845 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5849 code_bounds_check (5);
5850 do_initobj (&ctx, read32 (ip + 1));
5854 case CEE_CONSTRAINED_:
5855 code_bounds_check (5);
5856 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5857 prefix |= PREFIX_CONSTRAINED;
5862 prefix |= PREFIX_READONLY;
5867 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5868 if (!check_underflow (&ctx, 3))
5870 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5875 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5876 if (!check_underflow (&ctx, 3))
5878 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5886 if (!is_correct_rethrow (ctx.header, ip_offset))
5887 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5894 code_bounds_check (5);
5895 do_sizeof (&ctx, read32 (ip + 1));
5899 case CEE_REFANYTYPE:
5900 do_refanytype (&ctx);
5905 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5911 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5915 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5917 if (!ctx.prefix_set) //first prefix
5918 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5919 ctx.prefix_set |= prefix;
5920 ctx.has_flags = TRUE;
5924 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5926 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5927 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5928 if (ctx.prefix_set & PREFIX_READONLY)
5929 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5930 if (ctx.prefix_set & PREFIX_VOLATILE)
5931 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5932 if (ctx.prefix_set & PREFIX_UNALIGNED)
5933 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5934 ctx.prefix_set = prefix = 0;
5935 ctx.has_flags = FALSE;
5939 * if ip != end we overflowed: mark as error.
5941 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5942 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5945 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5946 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5947 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5948 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5949 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5951 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5952 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5954 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5955 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5957 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5958 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));
5961 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5962 char *method_name = mono_method_full_name (ctx.method, TRUE);
5963 char *type = mono_type_get_full_name (ctx.method->klass);
5964 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5965 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));
5967 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5968 g_free (method_name);
5974 for (i = 0; i < ctx.header->code_size; ++i) {
5975 if (ctx.code [i].stack)
5976 g_free (ctx.code [i].stack);
5980 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5982 g_slist_free (ctx.funptrs);
5984 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5985 mono_metadata_free_type (tmp->data);
5986 g_slist_free (ctx.exception_types);
5988 for (i = 0; i < ctx.num_locals; ++i) {
5990 mono_metadata_free_type (ctx.locals [i]);
5992 for (i = 0; i < ctx.max_args; ++i) {
5994 mono_metadata_free_type (ctx.params [i]);
5998 g_free (ctx.eval.stack);
6001 g_free (ctx.locals);
6002 g_free (ctx.params);
6003 mono_basic_block_free (original_bb);
6004 mono_metadata_free_mh (ctx.header);
6006 finish_collect_stats ();
6011 mono_verify_corlib ()
6013 /* This is a public API function so cannot be removed */
6018 * Returns true if @method needs to be verified.
6022 mono_verifier_is_enabled_for_method (MonoMethod *method)
6024 return mono_verifier_is_enabled_for_class (method->klass) && (method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD);
6028 * Returns true if @klass need to be verified.
6032 mono_verifier_is_enabled_for_class (MonoClass *klass)
6034 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
6038 mono_verifier_is_enabled_for_image (MonoImage *image)
6040 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
6044 * Dynamic methods are not considered full trust since if the user is trusted and need to
6045 * generate unsafe code, make the method skip verification - this is a known good way to do it.
6048 mono_verifier_is_method_full_trust (MonoMethod *method)
6050 return mono_verifier_is_class_full_trust (method->klass) && !method_is_dynamic (method);
6054 * Returns if @klass is under full trust or not.
6056 * TODO This code doesn't take CAS into account.
6058 * Under verify_all all user code must be verifiable if no security option was set
6062 mono_verifier_is_class_full_trust (MonoClass *klass)
6064 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
6065 gboolean trusted_location = !mono_security_core_clr_enabled () ?
6066 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
6068 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
6069 return trusted_location || klass->image == mono_defaults.corlib;
6070 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
6074 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
6076 return mono_method_verify (method,
6077 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
6078 | (!is_fulltrust && !mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
6079 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
6083 get_field_end (MonoClassField *field)
6086 int size = mono_type_size (field->type, &align);
6088 size = 4; /*FIXME Is this a safe bet?*/
6089 return size + field->offset;
6093 verify_class_for_overlapping_reference_fields (MonoClass *class)
6096 gpointer iter = NULL;
6097 MonoClassField *field;
6098 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
6099 /*We can't skip types with !has_references since this is calculated after we have run.*/
6100 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
6104 /*We must check for stuff overlapping reference fields.
6105 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
6107 while ((field = mono_class_get_fields (class, &iter))) {
6108 int fieldEnd = get_field_end (field);
6109 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
6112 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
6115 for (j = i; j < class->field.count; ++j) {
6116 MonoClassField *other = &class->fields [j];
6117 int otherEnd = get_field_end (other);
6118 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
6121 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
6124 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
6132 field_hash (gconstpointer key)
6134 const MonoClassField *field = key;
6135 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
6139 field_equals (gconstpointer _a, gconstpointer _b)
6141 const MonoClassField *a = _a;
6142 const MonoClassField *b = _b;
6143 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
6148 verify_class_fields (MonoClass *class)
6150 gpointer iter = NULL;
6151 MonoClassField *field;
6152 MonoGenericContext *context = mono_class_get_context (class);
6153 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
6154 if (class->generic_container)
6155 context = &class->generic_container->context;
6157 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
6158 if (!mono_type_is_valid_type_in_context (field->type, context)) {
6159 g_hash_table_destroy (unique_fields);
6162 if (g_hash_table_lookup (unique_fields, field)) {
6163 g_hash_table_destroy (unique_fields);
6166 g_hash_table_insert (unique_fields, field, field);
6168 g_hash_table_destroy (unique_fields);
6173 verify_interfaces (MonoClass *class)
6176 for (i = 0; i < class->interface_count; ++i) {
6177 MonoClass *iface = class->interfaces [i];
6178 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
6185 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
6188 gpointer iter = NULL;
6189 MonoClassField *field;
6190 MonoClass *field_class;
6192 if (!class->valuetype)
6195 type = class->byval_arg.type;
6196 /*primitive type fields are not properly decoded*/
6197 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
6200 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
6204 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
6207 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
6209 if (field_class == target_class || class == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
6217 verify_valuetype_layout (MonoClass *class)
6220 res = verify_valuetype_layout_with_target (class, class);
6225 recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
6228 MonoClass **constraints;
6229 MonoGenericParamInfo *param_info;
6231 g_assert (mono_type_is_generic_argument (type));
6233 idx = mono_type_get_generic_param_num (type);
6234 if (mono_bitset_test_fast (used_args, idx))
6237 mono_bitset_set_fast (used_args, idx);
6238 param_info = mono_generic_container_get_param_info (gc, idx);
6240 if (!param_info->constraints)
6243 for (constraints = param_info->constraints; *constraints; ++constraints) {
6244 MonoClass *ctr = *constraints;
6245 MonoType *constraint_type = &ctr->byval_arg;
6247 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6254 verify_generic_parameters (MonoClass *class)
6257 MonoGenericContainer *gc = class->generic_container;
6258 MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
6260 for (i = 0; i < gc->type_argc; ++i) {
6261 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
6262 MonoClass **constraints;
6264 if (!param_info->constraints)
6267 mono_bitset_clear_all (used_args);
6268 mono_bitset_set_fast (used_args, i);
6270 for (constraints = param_info->constraints; *constraints; ++constraints) {
6271 MonoClass *ctr = *constraints;
6272 MonoType *constraint_type = &ctr->byval_arg;
6274 if (!mono_class_can_access_class (class, ctr))
6277 if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
6280 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
6282 if (ctr->generic_class && !mono_class_is_valid_generic_instantiation (NULL, ctr))
6286 mono_bitset_free (used_args);
6290 mono_bitset_free (used_args);
6295 * Check if the class is verifiable.
6297 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6298 * field and invalid generic instantiation are fatal errors.
6300 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6304 mono_verifier_verify_class (MonoClass *class)
6306 /*Neither <Module>, object or ifaces have parent.*/
6307 if (!class->parent &&
6308 class != mono_defaults.object_class &&
6309 !MONO_CLASS_IS_INTERFACE (class) &&
6310 (!image_is_dynamic (class->image) && class->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
6312 if (class->parent) {
6313 if (MONO_CLASS_IS_INTERFACE (class->parent))
6315 if (!class->generic_class && class->parent->generic_container)
6317 if (class->parent->generic_class && !class->generic_class) {
6318 MonoGenericContext *context = mono_class_get_context (class);
6319 if (class->generic_container)
6320 context = &class->generic_container->context;
6321 if (!mono_type_is_valid_type_in_context (&class->parent->byval_arg, context))
6325 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
6327 if (class->generic_container && !verify_generic_parameters (class))
6329 if (!verify_class_for_overlapping_reference_fields (class))
6331 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
6333 if (class->generic_class == NULL && !verify_class_fields (class))
6335 if (class->valuetype && !verify_valuetype_layout (class))
6337 if (!verify_interfaces (class))
6343 mono_verifier_class_is_valid_generic_instantiation (MonoClass *class)
6345 return mono_class_is_valid_generic_instantiation (NULL, class);
6349 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)
6351 if (!method->is_inflated)
6353 return mono_method_is_valid_generic_instantiation (NULL, method);
6359 mono_verifier_verify_class (MonoClass *class)
6361 /* The verifier was disabled at compile time */
6366 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
6368 /* The verifier was disabled at compile time */
6373 mono_verifier_is_class_full_trust (MonoClass *klass)
6375 /* The verifier was disabled at compile time */
6380 mono_verifier_is_method_full_trust (MonoMethod *method)
6382 /* The verifier was disabled at compile time */
6387 mono_verifier_is_enabled_for_image (MonoImage *image)
6389 /* The verifier was disabled at compile time */
6394 mono_verifier_is_enabled_for_class (MonoClass *klass)
6396 /* The verifier was disabled at compile time */
6401 mono_verifier_is_enabled_for_method (MonoMethod *method)
6403 /* The verifier was disabled at compile time */
6408 mono_method_verify (MonoMethod *method, int level)
6410 /* The verifier was disabled at compile time */
6415 mono_free_verify_list (GSList *list)
6417 /* The verifier was disabled at compile time */
6418 /* will always be null if verifier is disabled */
6422 mono_verifier_class_is_valid_generic_instantiation (MonoClass *class)
6428 mono_verifier_is_method_valid_generic_instantiation (MonoMethod *method)