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)
12 #include <mono/metadata/object-internals.h>
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/verify-internals.h>
15 #include <mono/metadata/opcodes.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/reflection.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/metadata.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/class-internals.h>
23 #include <mono/metadata/security-manager.h>
24 #include <mono/metadata/security-core-clr.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/mono-basic-block.h>
27 #include <mono/utils/mono-counters.h>
28 #include <mono/utils/monobitset.h>
33 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
34 static gboolean verify_all = FALSE;
37 * Set the desired level of checks for the verfier.
41 mono_verifier_set_mode (MiniVerifierMode mode)
47 mono_verifier_enable_verify_all ()
52 #ifndef DISABLE_VERIFIER
54 * Pull the list of opcodes
56 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
60 #include "mono/cil/opcode.def"
65 #ifdef MONO_VERIFIER_DEBUG
66 #define VERIFIER_DEBUG(code) do { code } while (0)
68 #define VERIFIER_DEBUG(code)
71 //////////////////////////////////////////////////////////////////
72 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
73 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
74 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
75 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
76 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
77 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
79 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
80 vinfo->info.status = __status; \
81 vinfo->info.message = ( __msg ); \
82 vinfo->exception_type = (__exception); \
83 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
86 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
87 #define ADD_VERIFY_ERROR(__ctx, __msg) \
89 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
93 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
95 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
96 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
97 (__ctx)->verifiable = 0; \
98 if (IS_FAIL_FAST_MODE (__ctx)) \
103 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
105 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
106 (__ctx)->valid = 0; \
109 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
111 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
112 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
113 (__ctx)->verifiable = 0; \
114 if (IS_FAIL_FAST_MODE (__ctx)) \
115 (__ctx)->valid = 0; \
119 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
120 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
122 #if SIZEOF_VOID_P == 4
123 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
125 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
128 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
129 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
131 /*Flags to be used with ILCodeDesc::flags */
133 /*Instruction has not been processed.*/
134 IL_CODE_FLAG_NOT_PROCESSED = 0,
135 /*Instruction was decoded by mono_method_verify loop.*/
136 IL_CODE_FLAG_SEEN = 1,
137 /*Instruction was target of a branch or is at a protected block boundary.*/
138 IL_CODE_FLAG_WAS_TARGET = 2,
139 /*Used by stack_init to avoid double initialize each entry.*/
140 IL_CODE_FLAG_STACK_INITED = 4,
141 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
142 IL_CODE_STACK_MERGED = 8,
143 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
144 IL_CODE_DELEGATE_SEQUENCE = 0x10,
145 /*This is a delegate created from a ldftn to a non final virtual method*/
146 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
147 /*This is a call to a non final virtual method*/
148 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
166 guint16 size, max_size;
183 /*Allocated fnptr MonoType that should be freed by us.*/
185 /*Type dup'ed exception types from catch blocks.*/
186 GSList *exception_types;
191 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
195 MonoMethodSignature *signature;
196 MonoMethodHeader *header;
198 MonoGenericContext *generic_context;
202 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
203 *on a method that creates a delegate for a non-final virtual method using ldftn*/
204 gboolean has_this_store;
206 /*This flag is used to control if the contructor of the parent class has been called.
207 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
208 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
210 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
211 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
213 gboolean super_ctor_called;
217 MonoType *constrained_type;
221 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
224 get_stack_type (MonoType *type);
227 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
230 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
233 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
234 //////////////////////////////////////////////////////////////////
239 TYPE_INV = 0, /* leave at 0. */
244 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
246 /* value types and classes */
248 /* Number of types, used to define the size of the tables*/
251 /* Used by tables to signal that a result is not verifiable*/
252 NON_VERIFIABLE_RESULT = 0x80,
254 /*Mask used to extract just the type, excluding flags */
257 /* The stack type is a managed pointer, unmask the value to res */
258 POINTER_MASK = 0x100,
260 /*Stack type with the pointer mask*/
261 RAW_TYPE_MASK = 0x10F,
263 /* Controlled Mutability Manager Pointer */
266 /* The stack type is a null literal*/
267 NULL_LITERAL_MASK = 0x400,
269 /**Used by ldarg.0 and family to let delegate verification happens.*/
270 THIS_POINTER_MASK = 0x800,
272 /**Signals that this is a boxed value type*/
275 /*This is an unitialized this ref*/
276 UNINIT_THIS_MASK = 0x2000,
279 static const char* const
280 type_names [TYPE_MAX + 1] = {
291 PREFIX_UNALIGNED = 1,
294 PREFIX_CONSTRAINED = 8,
297 //////////////////////////////////////////////////////////////////
299 #ifdef ENABLE_VERIFIER_STATS
301 #define MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
302 #define MEM_FREE(amt) do { working_set -= (amt); } while (0)
304 static int allocated_memory;
305 static int working_set;
306 static int max_allocated_memory;
307 static int max_working_set;
308 static int total_allocated_memory;
311 finish_collect_stats (void)
313 max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
314 max_working_set = MAX (max_working_set, working_set);
315 total_allocated_memory += allocated_memory;
316 allocated_memory = working_set = 0;
320 init_verifier_stats (void)
322 static gboolean inited;
325 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
326 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
327 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
333 #define MEM_ALLOC(amt) do {} while (0)
334 #define MEM_FREE(amt) do { } while (0)
336 #define finish_collect_stats()
337 #define init_verifier_stats()
342 //////////////////////////////////////////////////////////////////
345 /*Token validation macros and functions */
346 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
347 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
348 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
349 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
351 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
352 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
353 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
354 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
355 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
356 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
359 * Verify if @token refers to a valid row on int's table.
362 token_bounds_check (MonoImage *image, guint32 token)
365 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
366 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
370 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
372 MonoType *res = g_new0 (MonoType, 1);
373 MEM_ALLOC (sizeof (MonoType));
375 //FIXME use mono_method_get_signature_full
376 res->data.method = mono_method_signature (method);
377 res->type = MONO_TYPE_FNPTR;
378 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
383 * mono_type_is_enum_type:
385 * Returns TRUE if @type is an enum type.
388 mono_type_is_enum_type (MonoType *type)
390 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
392 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
398 * mono_type_is_value_type:
400 * Returns TRUE if @type is named after @namespace.@name.
404 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
406 return type->type == MONO_TYPE_VALUETYPE &&
407 !strcmp (namespace, type->data.klass->name_space) &&
408 !strcmp (name, type->data.klass->name);
412 * Returns TURE if @type is VAR or MVAR
415 mono_type_is_generic_argument (MonoType *type)
417 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
421 * mono_type_get_underlying_type_any:
423 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
425 * Returns the underlying type of @type regardless if it is byref or not.
428 mono_type_get_underlying_type_any (MonoType *type)
430 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
431 return mono_class_enum_basetype (type->data.klass);
432 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
433 return mono_class_enum_basetype (type->data.generic_class->container_class);
437 static G_GNUC_UNUSED const char*
438 mono_type_get_stack_name (MonoType *type)
440 return type_names [get_stack_type (type) & TYPE_MASK];
443 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
444 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
447 mono_method_is_constructor (MonoMethod *method)
449 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
450 !(method->flags & CTOR_INVALID_FLAGS) &&
451 !strcmp (".ctor", method->name));
455 mono_class_has_default_constructor (MonoClass *klass)
460 mono_class_setup_methods (klass);
461 if (klass->exception_type)
464 for (i = 0; i < klass->method.count; ++i) {
465 method = klass->methods [i];
466 if (mono_method_is_constructor (method) &&
467 mono_method_signature (method) &&
468 mono_method_signature (method)->param_count == 0 &&
469 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
476 * Verify if @type is valid for the given @ctx verification context.
477 * this function checks for VAR and MVAR types that are invalid under the current verifier,
480 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
483 MonoGenericInst *inst;
485 switch (type->type) {
490 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
491 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
494 case MONO_TYPE_SZARRAY:
495 return mono_type_is_valid_type_in_context (&type->data.klass->byval_arg, context);
496 case MONO_TYPE_ARRAY:
497 return mono_type_is_valid_type_in_context (&type->data.array->eklass->byval_arg, context);
499 return mono_type_is_valid_type_in_context (type->data.type, context);
500 case MONO_TYPE_GENERICINST:
501 inst = type->data.generic_class->context.class_inst;
504 for (i = 0; i < inst->type_argc; ++i)
505 if (!mono_type_is_valid_type_in_context (inst->type_argv [i], context))
508 case MONO_TYPE_CLASS:
509 case MONO_TYPE_VALUETYPE: {
510 MonoClass *klass = type->data.klass;
512 * It's possible to encode generic'sh types in such a way that they disguise themselves as class or valuetype.
513 * Fixing the type decoding is really tricky since under some cases this behavior is needed, for example, to
514 * have a 'class' type pointing to a 'genericinst' class.
516 * For the runtime these non canonical (weird) encodings work fine, they worst they can cause is some
517 * reflection oddities which are harmless - to security at least.
519 if (klass->byval_arg.type != type->type)
520 return mono_type_is_valid_type_in_context (&klass->byval_arg, context);
527 /*This function returns NULL if the type is not instantiatable*/
529 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
534 result = mono_class_inflate_generic_type_checked (type, context, &error);
535 if (!mono_error_ok (&error)) {
536 mono_error_cleanup (&error);
544 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
549 if (ginst->type_argc != gc->type_argc)
552 for (i = 0; i < gc->type_argc; ++i) {
553 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
554 MonoClass *paramClass;
555 MonoClass **constraints;
556 MonoType *param_type = ginst->type_argv [i];
558 /*it's not our job to validate type variables*/
559 if (mono_type_is_generic_argument (param_type))
562 paramClass = mono_class_from_mono_type (param_type);
564 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
567 /* A GTD can't be a generic argument.
569 * Due to how types are encoded we must check for the case of a genericinst MonoType and GTD MonoClass.
570 * This happens in cases such as: class Foo<T> { void X() { new Bar<T> (); } }
572 * Open instantiations can have GTDs as this happens when one type is instantiated with others params
573 * and the former has an expansion into the later. For example:
575 * class A<T>: B<K> {}
576 * The type A <K> has a parent B<K>, that is inflated into the GTD B<>.
577 * Since A<K> is open, thus not instantiatable, this is valid.
579 if (paramClass->generic_container && param_type->type != MONO_TYPE_GENERICINST && !ginst->is_open)
582 /*it's not safe to call mono_class_init from here*/
583 if (paramClass->generic_class && !paramClass->inited) {
584 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
588 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
591 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
594 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
597 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
600 if (!param_info->constraints)
603 for (constraints = param_info->constraints; *constraints; ++constraints) {
604 MonoClass *ctr = *constraints;
607 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
608 if (!mono_error_ok (&error)) {
609 mono_error_cleanup (&error);
612 ctr = mono_class_from_mono_type (inflated);
613 mono_metadata_free_type (inflated);
615 if (!mono_class_is_assignable_from_slow (ctr, paramClass))
623 * Return true if @candidate is constraint compatible with @target.
625 * This means that @candidate constraints are a super set of @target constaints
628 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoClass *candidate_param_class, MonoGenericContext *context)
630 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
631 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
633 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
634 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
635 if ((tmask & cmask) != tmask)
638 if (tinfo->constraints) {
639 MonoClass **target_class, **candidate_class;
640 for (target_class = tinfo->constraints; *target_class; ++target_class) {
642 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
645 tc = mono_class_from_mono_type (inflated);
646 mono_metadata_free_type (inflated);
649 * A constraint from @target might inflate into @candidate itself and in that case we don't need
650 * check it's constraints since it satisfy the constraint by itself.
652 if (mono_metadata_type_equal (&tc->byval_arg, &candidate_param_class->byval_arg))
655 if (!cinfo->constraints)
658 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
660 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
663 cc = mono_class_from_mono_type (inflated);
664 mono_metadata_free_type (inflated);
666 if (mono_class_is_assignable_from (tc, cc))
669 if (!*candidate_class)
676 static MonoGenericParam*
677 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
679 MonoGenericContainer *gc;
680 MonoMethod *method = ctx->method;
683 num = mono_type_get_generic_param_num (type);
685 if (type->type == MONO_TYPE_VAR) {
686 MonoClass *gtd = method->klass;
687 if (gtd->generic_class)
688 gtd = gtd->generic_class->container_class;
689 gc = gtd->generic_container;
691 MonoMethod *gmd = method;
692 if (method->is_inflated)
693 gmd = ((MonoMethodInflated*)method)->declaring;
694 gc = mono_method_get_generic_container (gmd);
698 return mono_generic_container_get_param (gc, num);
704 * Verify if @type is valid for the given @ctx verification context.
705 * this function checks for VAR and MVAR types that are invalid under the current verifier,
706 * This means that it either
709 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
711 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
715 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
718 for (i = 0; i < ginst->type_argc; ++i) {
719 MonoType *type = ginst->type_argv [i];
720 if (!is_valid_type_in_context (ctx, type))
727 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
730 for (i = 0; i < ginst->type_argc; ++i) {
731 MonoType *type = ginst->type_argv [i];
732 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
733 MonoGenericParam *candidate;
734 MonoClass *candidate_class;
736 if (!mono_type_is_generic_argument (type))
739 if (!is_valid_type_in_context (ctx, type))
742 candidate = verifier_get_generic_param_from_type (ctx, type);
743 candidate_class = mono_class_from_mono_type (type);
745 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, candidate_class, context))
752 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
754 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
755 MonoGenericInst *ginst = gmethod->context.method_inst;
756 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
757 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
761 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
763 MonoGenericClass *gklass = klass->generic_class;
764 MonoGenericInst *ginst = gklass->context.class_inst;
765 MonoGenericContainer *gc = gklass->container_class->generic_container;
766 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
770 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
772 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
773 MonoGenericInst *ginst = gmethod->context.method_inst;
774 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
775 if (!gc) /*non-generic inflated method - it's part of a generic type */
777 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
779 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
784 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
786 MonoGenericClass *gklass = klass->generic_class;
787 MonoGenericInst *ginst = gklass->context.class_inst;
788 MonoGenericContainer *gc = gklass->container_class->generic_container;
789 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
791 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
795 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
800 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
804 if (!is_valid_type_in_context (ctx, type)) {
805 char *str = mono_type_full_name (type);
806 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
807 type->type == MONO_TYPE_VAR ? "!" : "!!",
809 type->type == MONO_TYPE_VAR ? "class" : "method",
811 MONO_EXCEPTION_BAD_IMAGE);
816 klass = mono_class_from_mono_type (type);
817 mono_class_init (klass);
818 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
819 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
820 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);
822 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);
823 mono_loader_clear_error ();
827 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
828 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);
832 if (!klass->generic_class)
835 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
836 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);
840 if (!mono_class_repect_method_constraints (ctx, klass)) {
841 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);
848 static verify_result_t
849 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
851 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
852 return RESULT_INVALID;
854 if (!method->is_inflated)
857 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
858 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);
859 return RESULT_INVALID;
862 if (!mono_method_repect_method_constraints (ctx, method)) {
863 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));
864 return RESULT_UNVERIFIABLE;
870 static MonoClassField*
871 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
872 MonoClassField *field;
873 MonoClass *klass = NULL;
875 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
876 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);
880 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
881 if (!field || !field->parent || !klass || mono_loader_get_last_error ()) {
882 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);
883 mono_loader_clear_error ();
887 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
895 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
898 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
899 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);
903 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
905 if (!method || mono_loader_get_last_error ()) {
906 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);
907 mono_loader_clear_error ();
911 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
918 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
921 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
922 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
926 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
928 if (!type || mono_loader_get_last_error ()) {
929 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);
930 mono_loader_clear_error ();
934 if (!mono_type_is_valid_in_context (ctx, type))
941 /* stack_slot_get_type:
943 * Returns the stack type of @value. This value includes POINTER_MASK.
945 * Use this function to checks that account for a managed pointer.
948 stack_slot_get_type (ILStackDesc *value)
950 return value->stype & RAW_TYPE_MASK;
953 /* stack_slot_get_underlying_type:
955 * Returns the stack type of @value. This value does not include POINTER_MASK.
957 * Use this function is cases where the fact that the value could be a managed pointer is
958 * irrelevant. For example, field load doesn't care about this fact of type on stack.
961 stack_slot_get_underlying_type (ILStackDesc *value)
963 return value->stype & TYPE_MASK;
966 /* stack_slot_is_managed_pointer:
968 * Returns TRUE is @value is a managed pointer.
971 stack_slot_is_managed_pointer (ILStackDesc *value)
973 return (value->stype & POINTER_MASK) == POINTER_MASK;
976 /* stack_slot_is_managed_mutability_pointer:
978 * Returns TRUE is @value is a managed mutability pointer.
980 static G_GNUC_UNUSED gboolean
981 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
983 return (value->stype & CMMP_MASK) == CMMP_MASK;
986 /* stack_slot_is_null_literal:
988 * Returns TRUE is @value is the null literal.
991 stack_slot_is_null_literal (ILStackDesc *value)
993 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
997 /* stack_slot_is_this_pointer:
999 * Returns TRUE is @value is the this literal
1002 stack_slot_is_this_pointer (ILStackDesc *value)
1004 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
1007 /* stack_slot_is_boxed_value:
1009 * Returns TRUE is @value is a boxed value
1012 stack_slot_is_boxed_value (ILStackDesc *value)
1014 return (value->stype & BOXED_MASK) == BOXED_MASK;
1018 stack_slot_get_name (ILStackDesc *value)
1020 return type_names [value->stype & TYPE_MASK];
1023 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
1024 if (PRED (value)) { \
1026 g_string_append (str, ", "); \
1027 g_string_append (str, NAME); \
1032 stack_slot_stack_type_full_name (ILStackDesc *value)
1034 GString *str = g_string_new ("");
1036 gboolean has_pred = FALSE, first = TRUE;
1038 if ((value->stype & TYPE_MASK) != value->stype) {
1039 g_string_append(str, "[");
1040 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
1041 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
1042 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
1043 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
1044 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1048 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1050 g_string_append(str, "[");
1052 g_string_append (str, ", ");
1053 g_string_append (str, "unboxed");
1058 g_string_append(str, "] ");
1060 g_string_append (str, stack_slot_get_name (value));
1062 g_string_free (str, FALSE);
1067 stack_slot_full_name (ILStackDesc *value)
1069 char *type_name = mono_type_full_name (value->type);
1070 char *stack_name = stack_slot_stack_type_full_name (value);
1071 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1073 g_free (stack_name);
1077 //////////////////////////////////////////////////////////////////
1079 mono_free_verify_list (GSList *list)
1081 MonoVerifyInfoExtended *info;
1084 for (tmp = list; tmp; tmp = tmp->next) {
1086 g_free (info->info.message);
1089 g_slist_free (list);
1092 #define ADD_ERROR(list,msg) \
1094 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1095 vinfo->info.status = MONO_VERIFY_ERROR; \
1096 vinfo->info.message = (msg); \
1097 (list) = g_slist_prepend ((list), vinfo); \
1100 #define ADD_WARN(list,code,msg) \
1102 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1103 vinfo->info.status = (code); \
1104 vinfo->info.message = (msg); \
1105 (list) = g_slist_prepend ((list), vinfo); \
1108 #define ADD_INVALID(list,msg) \
1110 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1111 vinfo->status = MONO_VERIFY_ERROR; \
1112 vinfo->message = (msg); \
1113 (list) = g_slist_prepend ((list), vinfo); \
1114 /*G_BREAKPOINT ();*/ \
1118 #define CHECK_STACK_UNDERFLOW(num) \
1120 if (cur_stack < (num)) \
1121 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1124 #define CHECK_STACK_OVERFLOW() \
1126 if (cur_stack >= max_stack) \
1127 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1132 in_any_block (MonoMethodHeader *header, guint offset)
1135 MonoExceptionClause *clause;
1137 for (i = 0; i < header->num_clauses; ++i) {
1138 clause = &header->clauses [i];
1139 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1141 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1143 if (MONO_OFFSET_IN_FILTER (clause, offset))
1150 * in_any_exception_block:
1152 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1155 in_any_exception_block (MonoMethodHeader *header, guint offset)
1158 MonoExceptionClause *clause;
1160 for (i = 0; i < header->num_clauses; ++i) {
1161 clause = &header->clauses [i];
1162 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1164 if (MONO_OFFSET_IN_FILTER (clause, offset))
1171 * is_valid_branch_instruction:
1173 * Verify if it's valid to perform a branch from @offset to @target.
1174 * This should be used with br and brtrue/false.
1175 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1176 * The major diferent from other similiar functions is that branching into a
1177 * finally/fault block is invalid instead of just unverifiable.
1180 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1183 MonoExceptionClause *clause;
1185 for (i = 0; i < header->num_clauses; ++i) {
1186 clause = &header->clauses [i];
1187 /*branching into a finally block is invalid*/
1188 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1189 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1190 MONO_OFFSET_IN_HANDLER (clause, target))
1193 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1195 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1197 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1204 * is_valid_cmp_branch_instruction:
1206 * Verify if it's valid to perform a branch from @offset to @target.
1207 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1208 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1210 * The major diferences from other similar functions are that most errors lead to invalid
1211 * code and only branching out of finally, filter or fault clauses is unverifiable.
1214 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1217 MonoExceptionClause *clause;
1219 for (i = 0; i < header->num_clauses; ++i) {
1220 clause = &header->clauses [i];
1221 /*branching out of a handler or finally*/
1222 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1223 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1224 !MONO_OFFSET_IN_HANDLER (clause, target))
1227 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1229 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1231 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1238 * A leave can't escape a finally block
1241 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1244 MonoExceptionClause *clause;
1246 for (i = 0; i < header->num_clauses; ++i) {
1247 clause = &header->clauses [i];
1248 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1250 if (MONO_OFFSET_IN_FILTER (clause, offset))
1257 * A rethrow can't happen outside of a catch handler.
1260 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1263 MonoExceptionClause *clause;
1265 for (i = 0; i < header->num_clauses; ++i) {
1266 clause = &header->clauses [i];
1267 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1269 if (MONO_OFFSET_IN_FILTER (clause, offset))
1276 * An endfinally can't happen outside of a finally/fault handler.
1279 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1282 MonoExceptionClause *clause;
1284 for (i = 0; i < header->num_clauses; ++i) {
1285 clause = &header->clauses [i];
1286 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1294 * An endfilter can only happens inside a filter clause.
1295 * In non-strict mode filter is allowed inside the handler clause too
1297 static MonoExceptionClause *
1298 is_correct_endfilter (VerifyContext *ctx, guint offset)
1301 MonoExceptionClause *clause;
1303 for (i = 0; i < ctx->header->num_clauses; ++i) {
1304 clause = &ctx->header->clauses [i];
1305 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1307 if (MONO_OFFSET_IN_FILTER (clause, offset))
1309 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1317 * Non-strict endfilter can happens inside a try block or any handler block
1320 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1323 MonoExceptionClause *clause;
1325 for (i = 0; i < ctx->header->num_clauses; ++i) {
1326 clause = &ctx->header->clauses [i];
1327 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1334 is_valid_bool_arg (ILStackDesc *arg)
1336 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1340 switch (stack_slot_get_underlying_type (arg)) {
1343 case TYPE_NATIVE_INT:
1347 g_assert (arg->type);
1348 switch (arg->type->type) {
1349 case MONO_TYPE_CLASS:
1350 case MONO_TYPE_STRING:
1351 case MONO_TYPE_OBJECT:
1352 case MONO_TYPE_SZARRAY:
1353 case MONO_TYPE_ARRAY:
1354 case MONO_TYPE_FNPTR:
1357 case MONO_TYPE_GENERICINST:
1358 /*We need to check if the container class
1359 * of the generic type is a valuetype, iow:
1360 * is it a "class Foo<T>" or a "struct Foo<T>"?
1362 return !arg->type->data.generic_class->container_class->valuetype;
1370 /*Type manipulation helper*/
1372 /*Returns the byref version of the supplied MonoType*/
1374 mono_type_get_type_byref (MonoType *type)
1378 return &mono_class_from_mono_type (type)->this_arg;
1382 /*Returns the byval version of the supplied MonoType*/
1384 mono_type_get_type_byval (MonoType *type)
1388 return &mono_class_from_mono_type (type)->byval_arg;
1392 mono_type_from_stack_slot (ILStackDesc *slot)
1394 if (stack_slot_is_managed_pointer (slot))
1395 return mono_type_get_type_byref (slot->type);
1399 /*Stack manipulation code*/
1402 ensure_stack_size (ILCodeDesc *stack, int required)
1407 if (required < stack->max_size)
1410 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1411 new_size = MAX (8, MAX (required, stack->max_size * 2));
1413 g_assert (new_size >= stack->size);
1414 g_assert (new_size >= required);
1416 tmp = g_new0 (ILStackDesc, new_size);
1417 MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1421 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1422 g_free (stack->stack);
1423 MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1427 stack->max_size = new_size;
1431 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1433 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1435 state->size = state->max_size = 0;
1436 state->flags |= IL_CODE_FLAG_STACK_INITED;
1440 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1442 ensure_stack_size (to, from->size);
1443 to->size = from->size;
1445 /*stack copy happens at merge points, which have small stacks*/
1447 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1451 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1453 to->stype = from->stype;
1454 to->type = from->type;
1455 to->method = from->method;
1459 check_underflow (VerifyContext *ctx, int size)
1461 if (ctx->eval.size < size) {
1462 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1469 check_overflow (VerifyContext *ctx)
1471 if (ctx->eval.size >= ctx->max_stack) {
1472 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1478 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1480 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1482 if (stack_slot_get_type (value) == TYPE_PTR) {
1483 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1489 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1491 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1493 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1494 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1500 static ILStackDesc *
1501 stack_push (VerifyContext *ctx)
1503 g_assert (ctx->eval.size < ctx->max_stack);
1504 g_assert (ctx->eval.size <= ctx->eval.max_size);
1506 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1508 return & ctx->eval.stack [ctx->eval.size++];
1511 static ILStackDesc *
1512 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1514 ILStackDesc *top = stack_push (ctx);
1520 static ILStackDesc *
1521 stack_pop (VerifyContext *ctx)
1524 g_assert (ctx->eval.size > 0);
1525 ret = ctx->eval.stack + --ctx->eval.size;
1526 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1527 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1531 /* This function allows to safely pop an unititialized this ptr from
1532 * the eval stack without marking the method as unverifiable.
1534 static ILStackDesc *
1535 stack_pop_safe (VerifyContext *ctx)
1537 g_assert (ctx->eval.size > 0);
1538 return ctx->eval.stack + --ctx->eval.size;
1541 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1543 stack_peek (VerifyContext *ctx, int distance)
1545 g_assert (ctx->eval.size - distance > 0);
1546 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1549 static ILStackDesc *
1550 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1552 ILStackDesc *top = stack_push (ctx);
1553 copy_stack_value (top, value);
1557 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1559 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1562 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1567 if (!(type = verifier_load_type (ctx, token, opcode)))
1570 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1571 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1575 if (type->type == MONO_TYPE_VOID) {
1576 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1580 if (type->type == MONO_TYPE_TYPEDBYREF)
1581 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1583 if (!(class = mono_class_from_mono_type (type)))
1584 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1586 if (class->generic_container && type->type != MONO_TYPE_GENERICINST)
1587 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));
1589 check_unverifiable_type (ctx, type);
1594 /*operation result tables */
1596 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1597 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1598 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1599 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1600 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1601 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1602 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1605 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1606 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1607 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1608 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1609 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1610 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1611 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1614 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1615 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1616 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1617 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1618 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1619 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1620 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1623 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1624 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1625 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1626 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1627 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1628 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1629 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1632 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1633 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1634 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1635 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1636 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1637 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1638 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1641 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1642 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1643 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1644 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1645 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1646 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1647 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1650 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1651 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1652 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1653 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1654 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1655 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1656 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1659 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1660 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1661 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1662 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1663 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1664 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1665 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1668 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1669 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1670 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1671 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1672 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1673 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1674 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1677 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1678 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1679 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1680 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1681 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1682 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1683 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1686 #ifdef MONO_VERIFIER_DEBUG
1690 dump_stack_value (ILStackDesc *value)
1692 printf ("[(%x)(%x)", value->type->type, value->stype);
1694 if (stack_slot_is_this_pointer (value))
1697 if (stack_slot_is_boxed_value (value))
1698 printf ("[boxed] ");
1700 if (stack_slot_is_null_literal (value))
1703 if (stack_slot_is_managed_mutability_pointer (value))
1704 printf ("Controled Mutability MP: ");
1706 if (stack_slot_is_managed_pointer (value))
1707 printf ("Managed Pointer to: ");
1709 switch (stack_slot_get_underlying_type (value)) {
1711 printf ("invalid type]");
1719 case TYPE_NATIVE_INT:
1720 printf ("native int]");
1723 printf ("float64]");
1726 printf ("unmanaged pointer]");
1729 switch (value->type->type) {
1730 case MONO_TYPE_CLASS:
1731 case MONO_TYPE_VALUETYPE:
1732 printf ("complex] (%s)", value->type->data.klass->name);
1734 case MONO_TYPE_STRING:
1735 printf ("complex] (string)");
1737 case MONO_TYPE_OBJECT:
1738 printf ("complex] (object)");
1740 case MONO_TYPE_SZARRAY:
1741 printf ("complex] (%s [])", value->type->data.klass->name);
1743 case MONO_TYPE_ARRAY:
1744 printf ("complex] (%s [%d %d %d])",
1745 value->type->data.array->eklass->name,
1746 value->type->data.array->rank,
1747 value->type->data.array->numsizes,
1748 value->type->data.array->numlobounds);
1750 case MONO_TYPE_GENERICINST:
1751 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1754 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1756 case MONO_TYPE_MVAR:
1757 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1760 //should be a boxed value
1761 char * name = mono_type_full_name (value->type);
1762 printf ("complex] %s", name);
1768 printf ("unknown stack %x type]\n", value->stype);
1769 g_assert_not_reached ();
1774 dump_stack_state (ILCodeDesc *state)
1778 printf ("(%d) ", state->size);
1779 for (i = 0; i < state->size; ++i)
1780 dump_stack_value (state->stack + i);
1785 /*Returns TRUE if candidate array type can be assigned to target.
1786 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1789 is_array_type_compatible (MonoType *target, MonoType *candidate)
1791 MonoArrayType *left = target->data.array;
1792 MonoArrayType *right = candidate->data.array;
1794 g_assert (target->type == MONO_TYPE_ARRAY);
1795 g_assert (candidate->type == MONO_TYPE_ARRAY);
1797 if (left->rank != right->rank)
1800 return mono_class_is_assignable_from (left->eklass, right->eklass);
1804 get_stack_type (MonoType *type)
1807 int type_kind = type->type;
1809 mask = POINTER_MASK;
1810 /*TODO handle CMMP_MASK */
1813 switch (type_kind) {
1816 case MONO_TYPE_BOOLEAN:
1819 case MONO_TYPE_CHAR:
1822 return TYPE_I4 | mask;
1826 return TYPE_NATIVE_INT | mask;
1828 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1829 case MONO_TYPE_FNPTR:
1831 case MONO_TYPE_TYPEDBYREF:
1832 return TYPE_PTR | mask;
1835 case MONO_TYPE_MVAR:
1837 case MONO_TYPE_CLASS:
1838 case MONO_TYPE_STRING:
1839 case MONO_TYPE_OBJECT:
1840 case MONO_TYPE_SZARRAY:
1841 case MONO_TYPE_ARRAY:
1842 return TYPE_COMPLEX | mask;
1846 return TYPE_I8 | mask;
1850 return TYPE_R8 | mask;
1852 case MONO_TYPE_GENERICINST:
1853 case MONO_TYPE_VALUETYPE:
1854 if (mono_type_is_enum_type (type)) {
1855 type = mono_type_get_underlying_type_any (type);
1858 type_kind = type->type;
1861 return TYPE_COMPLEX | mask;
1869 /* convert MonoType to ILStackDesc format (stype) */
1871 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1874 int type_kind = type->type;
1876 if (type->byref || take_addr)
1877 mask = POINTER_MASK;
1878 /* TODO handle CMMP_MASK */
1883 switch (type_kind) {
1886 case MONO_TYPE_BOOLEAN:
1889 case MONO_TYPE_CHAR:
1892 stack->stype = TYPE_I4 | mask;
1896 stack->stype = TYPE_NATIVE_INT | mask;
1899 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1900 case MONO_TYPE_FNPTR:
1902 case MONO_TYPE_TYPEDBYREF:
1903 stack->stype = TYPE_PTR | mask;
1906 case MONO_TYPE_CLASS:
1907 case MONO_TYPE_STRING:
1908 case MONO_TYPE_OBJECT:
1909 case MONO_TYPE_SZARRAY:
1910 case MONO_TYPE_ARRAY:
1913 case MONO_TYPE_MVAR:
1914 stack->stype = TYPE_COMPLEX | mask;
1919 stack->stype = TYPE_I8 | mask;
1923 stack->stype = TYPE_R8 | mask;
1925 case MONO_TYPE_GENERICINST:
1926 case MONO_TYPE_VALUETYPE:
1927 if (mono_type_is_enum_type (type)) {
1928 MonoType *utype = mono_type_get_underlying_type_any (type);
1930 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
1934 type_kind = type->type;
1937 stack->stype = TYPE_COMPLEX | mask;
1941 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1942 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
1949 * init_stack_with_value_at_exception_boundary:
1951 * Initialize the stack and push a given type.
1952 * The instruction is marked as been on the exception boundary.
1955 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
1958 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
1960 if (!mono_error_ok (&error)) {
1961 char *name = mono_type_get_full_name (klass);
1962 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
1964 mono_error_cleanup (&error);
1968 if (!ctx->max_stack) {
1969 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
1973 stack_init (ctx, code);
1974 ensure_stack_size (code, 1);
1975 set_stack_value (ctx, code->stack, type, FALSE);
1976 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
1978 code->flags |= IL_CODE_FLAG_WAS_TARGET;
1979 if (mono_type_is_generic_argument (type))
1980 code->stack->stype |= BOXED_MASK;
1983 /*Verify if type 'candidate' can be stored in type 'target'.
1985 * If strict, check for the underlying type and not the verification stack types
1988 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1990 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1991 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1993 MonoType *original_candidate = candidate;
1994 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
1996 /*only one is byref */
1997 if (candidate->byref ^ target->byref) {
1998 /* converting from native int to byref*/
1999 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2000 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2005 strict |= target->byref;
2006 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2007 candidate = mono_type_get_underlying_type_any (candidate);
2010 switch (target->type) {
2011 case MONO_TYPE_VOID:
2012 return candidate->type == MONO_TYPE_VOID;
2015 case MONO_TYPE_BOOLEAN:
2017 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2020 case MONO_TYPE_CHAR:
2022 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2024 case MONO_TYPE_U4: {
2025 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2026 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2028 return is_native_int || is_int4;
2029 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2034 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2039 return candidate->type == target->type;
2040 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2044 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2045 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2047 return is_native_int || is_int4;
2048 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2052 if (candidate->type != MONO_TYPE_PTR)
2054 /* check the underlying type */
2055 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2057 case MONO_TYPE_FNPTR: {
2058 MonoMethodSignature *left, *right;
2059 if (candidate->type != MONO_TYPE_FNPTR)
2062 left = mono_type_get_signature (target);
2063 right = mono_type_get_signature (candidate);
2064 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2067 case MONO_TYPE_GENERICINST: {
2068 MonoClass *target_klass;
2069 MonoClass *candidate_klass;
2070 if (mono_type_is_enum_type (target)) {
2071 target = mono_type_get_underlying_type_any (target);
2077 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2078 * to take boxing status into account.
2080 if (mono_type_is_generic_argument (original_candidate))
2083 target_klass = mono_class_from_mono_type (target);
2084 candidate_klass = mono_class_from_mono_type (candidate);
2085 if (mono_class_is_nullable (target_klass)) {
2086 if (!mono_class_is_nullable (candidate_klass))
2088 return target_klass == candidate_klass;
2091 return mono_class_is_assignable_from (target_klass, candidate_klass);
2094 case MONO_TYPE_STRING:
2095 return candidate->type == MONO_TYPE_STRING;
2097 case MONO_TYPE_CLASS:
2099 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2100 * to take boxing status into account.
2102 if (mono_type_is_generic_argument (original_candidate))
2105 if (candidate->type == MONO_TYPE_VALUETYPE)
2108 /* If candidate is an enum it should return true for System.Enum and supertypes.
2109 * That's why here we use the original type and not the underlying type.
2111 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2113 case MONO_TYPE_OBJECT:
2114 return MONO_TYPE_IS_REFERENCE (candidate);
2116 case MONO_TYPE_SZARRAY: {
2119 if (candidate->type != MONO_TYPE_SZARRAY)
2122 left = mono_class_from_mono_type (target);
2123 right = mono_class_from_mono_type (candidate);
2125 return mono_class_is_assignable_from (left, right);
2128 case MONO_TYPE_ARRAY:
2129 if (candidate->type != MONO_TYPE_ARRAY)
2131 return is_array_type_compatible (target, candidate);
2133 case MONO_TYPE_TYPEDBYREF:
2134 return candidate->type == MONO_TYPE_TYPEDBYREF;
2136 case MONO_TYPE_VALUETYPE: {
2137 MonoClass *target_klass;
2138 MonoClass *candidate_klass;
2140 if (candidate->type == MONO_TYPE_CLASS)
2143 target_klass = mono_class_from_mono_type (target);
2144 candidate_klass = mono_class_from_mono_type (candidate);
2145 if (target_klass == candidate_klass)
2147 if (mono_type_is_enum_type (target)) {
2148 target = mono_type_get_underlying_type_any (target);
2157 if (candidate->type != MONO_TYPE_VAR)
2159 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2161 case MONO_TYPE_MVAR:
2162 if (candidate->type != MONO_TYPE_MVAR)
2164 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2167 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2168 g_assert_not_reached ();
2177 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2179 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2183 * Returns the generic param bound to the context been verified.
2186 static MonoGenericParam*
2187 get_generic_param (VerifyContext *ctx, MonoType *param)
2189 guint16 param_num = mono_type_get_generic_param_num (param);
2190 if (param->type == MONO_TYPE_VAR) {
2191 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2192 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2195 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2198 /*param must be a MVAR */
2199 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2200 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2203 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2208 recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
2210 MonoType *constraint_type = &constraint_class->byval_arg;
2211 if (recursion_level <= 0)
2214 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
2217 if (mono_type_is_generic_argument (constraint_type)) {
2218 MonoGenericParam *param = get_generic_param (ctx, constraint_type);
2222 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2223 if (recursive_boxed_constraint_type_check (ctx, type, *class, recursion_level - 1))
2231 * is_compatible_boxed_valuetype:
2233 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2235 * @type The source type. It it tested to be of the proper type.
2236 * @candidate type of the boxed valuetype.
2237 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2238 * @strict if TRUE candidate must be boxed compatible to the target type
2242 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2244 if (!stack_slot_is_boxed_value (stack))
2246 if (type->byref || candidate->byref)
2249 if (mono_type_is_generic_argument (candidate)) {
2250 MonoGenericParam *param = get_generic_param (ctx, candidate);
2255 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2256 /*256 should be enough since there can't be more than 255 generic arguments.*/
2257 if (recursive_boxed_constraint_type_check (ctx, type, *class, 256))
2262 if (mono_type_is_generic_argument (type))
2268 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2272 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2274 MonoType *candidate = mono_type_from_stack_slot (stack);
2275 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2278 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2281 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2284 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2288 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2290 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2294 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2296 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2300 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2302 if (candidate->byref ^ target->byref)
2305 switch (target->type) {
2306 case MONO_TYPE_VOID:
2309 case MONO_TYPE_BOOLEAN:
2312 case MONO_TYPE_CHAR:
2321 case MONO_TYPE_STRING:
2322 case MONO_TYPE_TYPEDBYREF:
2323 return candidate->type == target->type;
2326 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2328 case MONO_TYPE_FNPTR:
2329 if (candidate->type != MONO_TYPE_FNPTR)
2331 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2333 case MONO_TYPE_GENERICINST: {
2334 MonoClass *target_klass;
2335 MonoClass *candidate_klass;
2336 target_klass = mono_class_from_mono_type (target);
2337 candidate_klass = mono_class_from_mono_type (candidate);
2338 /*FIXME handle nullables and enum*/
2339 return mono_class_is_assignable_from (target_klass, candidate_klass);
2341 case MONO_TYPE_OBJECT:
2342 return MONO_TYPE_IS_REFERENCE (candidate);
2344 case MONO_TYPE_CLASS:
2345 return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2347 case MONO_TYPE_SZARRAY:
2348 if (candidate->type != MONO_TYPE_SZARRAY)
2350 return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2352 case MONO_TYPE_ARRAY:
2353 if (candidate->type != MONO_TYPE_ARRAY)
2355 return is_array_type_compatible (target, candidate);
2357 case MONO_TYPE_VALUETYPE:
2358 /*FIXME handle nullables and enum*/
2359 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2362 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2365 case MONO_TYPE_MVAR:
2366 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2370 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2371 g_assert_not_reached ();
2377 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2379 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2382 return mono_delegate_type_equal (method, delegate);
2386 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2388 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2391 return mono_delegate_type_equal (delegate, method);
2395 * mono_delegate_signature_equal:
2397 * Compare two signatures in the way expected by delegates.
2399 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2401 * FIXME can this function be eliminated and proper metadata functionality be used?
2404 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2407 int method_offset = is_static_ldftn ? 1 : 0;
2409 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2412 if (delegate_sig->call_convention != method_sig->call_convention)
2415 for (i = 0; i < delegate_sig->param_count; i++) {
2416 MonoType *p1 = delegate_sig->params [i];
2417 MonoType *p2 = method_sig->params [i + method_offset];
2419 if (!mono_delegate_param_equal (p1, p2))
2423 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2430 mono_verifier_is_signature_compatible (MonoMethodSignature *target, MonoMethodSignature *candidate)
2432 return mono_delegate_signature_equal (target, candidate, FALSE);
2436 * verify_ldftn_delegate:
2438 * Verify properties of ldftn based delegates.
2441 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2443 MonoMethod *method = funptr->method;
2445 /*ldftn non-final virtuals only allowed if method is not static,
2446 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2447 * This rules doesn't apply if the object on stack is a boxed valuetype.
2449 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2450 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2451 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2452 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2454 /*current method must not be static*/
2455 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2456 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2458 /*value is the this pointer, loaded using ldarg.0 */
2459 if (!stack_slot_is_this_pointer (value))
2460 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));
2462 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2467 * verify_delegate_compatibility:
2469 * Verify delegate creation sequence.
2473 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2475 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2476 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2478 MonoMethod *invoke, *method;
2479 const guint8 *ip = ctx->header->code;
2480 guint32 ip_offset = ctx->ip_offset;
2481 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2483 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2484 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2488 invoke = mono_get_delegate_invoke (delegate);
2489 method = funptr->method;
2491 if (!method || !mono_method_signature (method)) {
2492 char *name = mono_type_get_full_name (delegate);
2493 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2498 if (!invoke || !mono_method_signature (invoke)) {
2499 char *name = mono_type_get_full_name (delegate);
2500 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2505 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2507 if (is_static_ldftn)
2508 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2510 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2511 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2512 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2513 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));
2515 g_free (invoke_sig);
2519 * Delegate code sequences:
2525 * [-6] ldvirtftn token
2529 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2530 verify_ldftn_delegate (ctx, delegate, value, funptr);
2531 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2532 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2534 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2536 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2539 if (is_first_arg_bound) {
2540 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2541 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2543 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2544 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2545 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2547 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2548 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2552 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2553 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2555 #undef IS_VALID_OPCODE
2556 #undef IS_LOAD_FUN_PTR
2559 /* implement the opcode checks*/
2561 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2565 if (arg >= ctx->max_args) {
2567 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2569 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2570 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2571 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2573 } else if (check_overflow (ctx)) {
2574 /*We must let the value be pushed, otherwise we would get an underflow error*/
2575 check_unverifiable_type (ctx, ctx->params [arg]);
2576 if (ctx->params [arg]->byref && take_addr)
2577 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2578 top = stack_push (ctx);
2579 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2582 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2584 ctx->has_this_store = TRUE;
2586 top->stype |= THIS_POINTER_MASK;
2587 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2588 top->stype |= UNINIT_THIS_MASK;
2594 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2596 if (arg >= ctx->num_locals) {
2597 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2598 } else if (check_overflow (ctx)) {
2599 /*We must let the value be pushed, otherwise we would get an underflow error*/
2600 check_unverifiable_type (ctx, ctx->locals [arg]);
2601 if (ctx->locals [arg]->byref && take_addr)
2602 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2604 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2609 store_arg (VerifyContext *ctx, guint32 arg)
2613 if (arg >= ctx->max_args) {
2614 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2615 if (check_underflow (ctx, 1))
2620 if (check_underflow (ctx, 1)) {
2621 value = stack_pop (ctx);
2622 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2623 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2626 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2627 ctx->has_this_store = 1;
2631 store_local (VerifyContext *ctx, guint32 arg)
2634 if (arg >= ctx->num_locals) {
2635 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2639 /*TODO verify definite assigment */
2640 if (check_underflow (ctx, 1)) {
2641 value = stack_pop(ctx);
2642 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2643 char *expected = mono_type_full_name (ctx->locals [arg]);
2644 char *found = stack_slot_full_name (value);
2645 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2656 /*FIXME add and sub needs special care here*/
2658 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2660 ILStackDesc *a, *b, *top;
2661 int idxa, idxb, complexMerge = 0;
2664 if (!check_underflow (ctx, 2))
2666 b = stack_pop (ctx);
2667 a = stack_pop (ctx);
2669 idxa = stack_slot_get_underlying_type (a);
2670 if (stack_slot_is_managed_pointer (a)) {
2675 idxb = stack_slot_get_underlying_type (b);
2676 if (stack_slot_is_managed_pointer (b)) {
2683 res = table [idxa][idxb];
2685 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2686 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2688 top = stack_push (ctx);
2689 if (res == TYPE_INV) {
2690 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)));
2691 copy_stack_value (top, a);
2695 if (res & NON_VERIFIABLE_RESULT) {
2696 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)));
2698 res = res & ~NON_VERIFIABLE_RESULT;
2701 if (complexMerge && res == TYPE_PTR) {
2702 if (complexMerge == 1)
2703 copy_stack_value (top, a);
2704 else if (complexMerge == 2)
2705 copy_stack_value (top, b);
2707 * There is no need to merge the type of two pointers.
2708 * The only valid operation is subtraction, that returns a native
2709 * int as result and can be used with any 2 pointer kinds.
2710 * This is valid acording to Patition III 1.1.4
2719 do_boolean_branch_op (VerifyContext *ctx, int delta)
2721 int target = ctx->ip_offset + delta;
2724 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2726 if (target < 0 || target >= ctx->code_size) {
2727 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2731 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2733 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2736 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2740 ctx->target = target;
2742 if (!check_underflow (ctx, 1))
2745 top = stack_pop (ctx);
2746 if (!is_valid_bool_arg (top))
2747 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));
2749 check_unmanaged_pointer (ctx, top);
2753 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2755 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2759 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2764 int target = ctx->ip_offset + delta;
2766 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2768 if (target < 0 || target >= ctx->code_size) {
2769 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2773 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2774 case 1: /*FIXME use constants and not magic numbers.*/
2775 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2778 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2782 ctx->target = target;
2784 if (!check_underflow (ctx, 2))
2787 b = stack_pop (ctx);
2788 a = stack_pop (ctx);
2790 idxa = stack_slot_get_underlying_type (a);
2791 if (stack_slot_is_managed_pointer (a))
2794 idxb = stack_slot_get_underlying_type (b);
2795 if (stack_slot_is_managed_pointer (b))
2798 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2803 res = table [idxa][idxb];
2806 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2807 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2809 if (res == TYPE_INV) {
2810 CODE_NOT_VERIFIABLE (ctx,
2811 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));
2812 } else if (res & NON_VERIFIABLE_RESULT) {
2813 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));
2814 res = res & ~NON_VERIFIABLE_RESULT;
2819 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2825 if (!check_underflow (ctx, 2))
2827 b = stack_pop (ctx);
2828 a = stack_pop (ctx);
2830 if (opcode == CEE_CGT_UN) {
2831 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2832 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2837 idxa = stack_slot_get_underlying_type (a);
2838 if (stack_slot_is_managed_pointer (a))
2841 idxb = stack_slot_get_underlying_type (b);
2842 if (stack_slot_is_managed_pointer (b))
2845 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2850 res = table [idxa][idxb];
2853 if(res == TYPE_INV) {
2854 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2855 } else if (res & NON_VERIFIABLE_RESULT) {
2856 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));
2857 res = res & ~NON_VERIFIABLE_RESULT;
2859 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2863 do_ret (VerifyContext *ctx)
2865 MonoType *ret = ctx->signature->ret;
2866 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2867 if (ret->type != MONO_TYPE_VOID) {
2869 if (!check_underflow (ctx, 1))
2872 top = stack_pop(ctx);
2874 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2875 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2879 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
2880 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
2883 if (ctx->eval.size > 0) {
2884 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2886 if (in_any_block (ctx->header, ctx->ip_offset))
2887 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2891 * 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.
2892 * This is illegal but mono_get_method_full decoded it.
2893 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2896 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
2899 MonoMethodSignature *sig;
2902 gboolean virt_check_this = FALSE;
2903 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
2905 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
2909 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
2911 if (method->klass->valuetype) // && !constrained ???
2912 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
2914 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
2915 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
2918 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
2919 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
2921 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
2922 virt_check_this = TRUE;
2923 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
2927 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
2928 sig = mono_method_get_signature (method, ctx->image, method_token);
2931 char *name = mono_type_get_full_name (method->klass);
2932 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name, method->name, ctx->ip_offset));
2937 param_count = sig->param_count + sig->hasthis;
2938 if (!check_underflow (ctx, param_count))
2941 for (i = sig->param_count - 1; i >= 0; --i) {
2942 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2943 value = stack_pop (ctx);
2944 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
2945 char *stack_name = stack_slot_full_name (value);
2946 char *sig_name = mono_type_full_name (sig->params [i]);
2947 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));
2948 g_free (stack_name);
2952 if (stack_slot_is_managed_mutability_pointer (value))
2953 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));
2955 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
2956 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));
2962 MonoType *type = &method->klass->byval_arg;
2965 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
2966 if (!mono_method_is_constructor (ctx->method))
2967 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
2968 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
2969 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor to a type diferent that this or super at 0x%04x", ctx->ip_offset));
2971 ctx->super_ctor_called = TRUE;
2972 value = stack_pop_safe (ctx);
2973 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
2974 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
2976 value = stack_pop (ctx);
2979 copy_stack_value (©, value);
2980 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2981 //Other parts of the code suffer from the same issue of
2982 copy.type = mono_type_get_type_byval (copy.type);
2983 copy.stype &= ~POINTER_MASK;
2985 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
2986 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the call opcode with a non-final virtual method on an object diferent thant the this pointer at 0x%04x", ctx->ip_offset));
2988 if (constrained && virtual) {
2989 if (!stack_slot_is_managed_pointer (value))
2990 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
2991 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
2992 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
2993 copy.stype |= BOXED_MASK;
2995 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
2996 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));
2998 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
2999 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3001 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3002 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3004 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3005 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));
3007 if (!verify_stack_type_compatibility (ctx, type, ©)) {
3008 char *expected = mono_type_full_name (type);
3009 char *effective = stack_slot_full_name (©);
3010 char *method_name = mono_method_full_name (method, TRUE);
3011 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",
3012 expected, effective, method_name, ctx->ip_offset));
3013 g_free (method_name);
3018 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
3019 char *name = mono_method_full_name (method, TRUE);
3020 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3024 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3025 char *name = mono_method_full_name (method, TRUE);
3026 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3030 if (sig->ret->type != MONO_TYPE_VOID) {
3031 if (check_overflow (ctx)) {
3032 value = stack_push (ctx);
3033 set_stack_value (ctx, value, sig->ret, FALSE);
3034 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3035 ctx->prefix_set &= ~PREFIX_READONLY;
3036 value->stype |= CMMP_MASK;
3041 if ((ctx->prefix_set & PREFIX_TAIL)) {
3042 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3043 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3044 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3045 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3051 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3053 MonoClassField *field;
3055 if (!check_overflow (ctx))
3058 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3060 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3063 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3064 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3067 /*taking the address of initonly field only works from the static constructor */
3068 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3069 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3070 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3072 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3073 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3075 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3079 do_store_static_field (VerifyContext *ctx, int token) {
3080 MonoClassField *field;
3083 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3085 if (!check_underflow (ctx, 1))
3088 value = stack_pop (ctx);
3090 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3093 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3094 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3098 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3099 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3103 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3104 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3106 if (!verify_stack_type_compatibility (ctx, field->type, value)) {
3107 char *stack_name = stack_slot_full_name (value);
3108 char *field_name = mono_type_full_name (field->type);
3109 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3110 field_name, stack_name, ctx->ip_offset));
3111 g_free (field_name);
3112 g_free (stack_name);
3117 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3119 MonoClassField *field;
3121 gboolean is_pointer;
3123 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3124 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3128 //the value on stack is going to be used as a pointer
3129 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3131 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3132 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3135 g_assert (obj->type);
3137 /*The value on the stack must be a subclass of the defining type of the field*/
3138 /* we need to check if we can load the field from the stack value*/
3140 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3141 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3143 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3144 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3146 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3147 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));
3149 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3150 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3151 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));
3153 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3154 char *found = stack_slot_full_name (obj);
3155 char *expected = mono_type_full_name (&field->parent->byval_arg);
3156 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));
3161 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3162 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3165 check_unmanaged_pointer (ctx, obj);
3170 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3173 MonoClassField *field;
3176 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3178 if (!check_underflow (ctx, 1))
3180 obj = stack_pop_safe (ctx);
3182 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3185 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3186 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3188 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3189 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3190 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3192 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3196 do_store_field (VerifyContext *ctx, int token)
3198 ILStackDesc *value, *obj;
3199 MonoClassField *field;
3200 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3202 if (!check_underflow (ctx, 2))
3205 value = stack_pop (ctx);
3206 obj = stack_pop_safe (ctx);
3208 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3211 if (!verify_stack_type_compatibility (ctx, field->type, value))
3212 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3215 /*TODO proper handle for Nullable<T>*/
3217 do_box_value (VerifyContext *ctx, int klass_token)
3220 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3226 if (!check_underflow (ctx, 1))
3229 value = stack_pop (ctx);
3230 /*box is a nop for reference types*/
3232 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3233 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3238 if (!verify_stack_type_compatibility (ctx, type, value))
3239 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3241 klass = mono_class_from_mono_type (type);
3242 if (mono_class_is_nullable (klass))
3243 type = &mono_class_get_nullable_param (klass)->byval_arg;
3244 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3248 do_unbox_value (VerifyContext *ctx, int klass_token)
3251 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3256 if (!check_underflow (ctx, 1))
3259 if (!mono_class_from_mono_type (type)->valuetype)
3260 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3262 value = stack_pop (ctx);
3264 /*Value should be: a boxed valuetype or a reference type*/
3265 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3266 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3267 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));
3269 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3270 value->stype |= CMMP_MASK;
3274 do_unbox_any (VerifyContext *ctx, int klass_token)
3277 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3282 if (!check_underflow (ctx, 1))
3285 value = stack_pop (ctx);
3287 /*Value should be: a boxed valuetype or a reference type*/
3288 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3289 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3290 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));
3292 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3296 do_unary_math_op (VerifyContext *ctx, int op)
3299 if (!check_underflow (ctx, 1))
3301 value = stack_pop (ctx);
3302 switch (stack_slot_get_type (value)) {
3305 case TYPE_NATIVE_INT:
3310 case TYPE_COMPLEX: /*only enums are ok*/
3311 if (mono_type_is_enum_type (value->type))
3314 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3316 stack_push_stack_val (ctx, value);
3320 do_conversion (VerifyContext *ctx, int kind)
3323 if (!check_underflow (ctx, 1))
3325 value = stack_pop (ctx);
3327 switch (stack_slot_get_type (value)) {
3330 case TYPE_NATIVE_INT:
3334 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));
3339 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3342 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3345 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3347 case TYPE_NATIVE_INT:
3348 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3351 g_error ("unknown type %02x in conversion", kind);
3357 do_load_token (VerifyContext *ctx, int token)
3360 MonoClass *handle_class;
3361 if (!check_overflow (ctx))
3364 switch (token & 0xff000000) {
3365 case MONO_TOKEN_TYPE_DEF:
3366 case MONO_TOKEN_TYPE_REF:
3367 case MONO_TOKEN_TYPE_SPEC:
3368 case MONO_TOKEN_FIELD_DEF:
3369 case MONO_TOKEN_METHOD_DEF:
3370 case MONO_TOKEN_METHOD_SPEC:
3371 case MONO_TOKEN_MEMBER_REF:
3372 if (!token_bounds_check (ctx->image, token)) {
3373 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));
3378 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));
3382 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3384 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3387 if (handle_class == mono_defaults.typehandle_class) {
3388 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3389 } else if (handle_class == mono_defaults.methodhandle_class) {
3390 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3391 } else if (handle_class == mono_defaults.fieldhandle_class) {
3392 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3394 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3396 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3400 do_ldobj_value (VerifyContext *ctx, int token)
3403 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3404 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3409 if (!check_underflow (ctx, 1))
3412 value = stack_pop (ctx);
3413 if (!stack_slot_is_managed_pointer (value)
3414 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3415 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3416 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3420 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3421 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3423 /*We have a byval on the stack, but the comparison must be strict. */
3424 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3425 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3427 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3431 do_stobj (VerifyContext *ctx, int token)
3433 ILStackDesc *dest, *src;
3434 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3435 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3440 if (!check_underflow (ctx, 2))
3443 src = stack_pop (ctx);
3444 dest = stack_pop (ctx);
3446 if (stack_slot_is_managed_mutability_pointer (dest))
3447 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3449 if (!stack_slot_is_managed_pointer (dest))
3450 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3452 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3453 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));
3455 if (!verify_stack_type_compatibility (ctx, type, src)) {
3456 char *type_name = mono_type_full_name (type);
3457 char *src_name = stack_slot_full_name (src);
3458 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));
3463 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3464 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3468 do_cpobj (VerifyContext *ctx, int token)
3470 ILStackDesc *dest, *src;
3471 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3475 if (!check_underflow (ctx, 2))
3478 src = stack_pop (ctx);
3479 dest = stack_pop (ctx);
3481 if (!stack_slot_is_managed_pointer (src))
3482 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3484 if (!stack_slot_is_managed_pointer (dest))
3485 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3487 if (stack_slot_is_managed_mutability_pointer (dest))
3488 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3490 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3491 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3493 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3494 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3498 do_initobj (VerifyContext *ctx, int token)
3501 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3505 if (!check_underflow (ctx, 1))
3508 obj = stack_pop (ctx);
3510 if (!stack_slot_is_managed_pointer (obj))
3511 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3513 if (stack_slot_is_managed_mutability_pointer (obj))
3514 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3516 stack = mono_type_get_type_byval (obj->type);
3517 if (MONO_TYPE_IS_REFERENCE (stack)) {
3518 if (!verify_type_compatibility (ctx, stack, type))
3519 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3520 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3521 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3522 } else if (!verify_type_compatibility (ctx, stack, type)) {
3523 char *expected_name = mono_type_full_name (type);
3524 char *stack_name = mono_type_full_name (stack);
3526 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));
3527 g_free (expected_name);
3528 g_free (stack_name);
3533 do_newobj (VerifyContext *ctx, int token)
3537 MonoMethodSignature *sig;
3539 gboolean is_delegate = FALSE;
3541 if (!(method = verifier_load_method (ctx, token, "newobj")))
3544 if (!mono_method_is_constructor (method)) {
3545 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3549 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3550 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3552 if (!mono_method_can_access_method_full (ctx->method, method, NULL)) {
3553 char *from = mono_method_full_name (ctx->method, TRUE);
3554 char *to = mono_method_full_name (method, TRUE);
3555 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);
3560 //FIXME use mono_method_get_signature_full
3561 sig = mono_method_signature (method);
3563 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3567 if (!sig->hasthis) {
3568 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature missing hasthis at 0x%04x", ctx->ip_offset));
3572 if (!check_underflow (ctx, sig->param_count))
3575 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3578 ILStackDesc *funptr;
3579 //first arg is object, second arg is fun ptr
3580 if (sig->param_count != 2) {
3581 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3584 funptr = stack_pop (ctx);
3585 value = stack_pop (ctx);
3586 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3588 for (i = sig->param_count - 1; i >= 0; --i) {
3589 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3590 value = stack_pop (ctx);
3591 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3592 char *stack_name = stack_slot_full_name (value);
3593 char *sig_name = mono_type_full_name (sig->params [i]);
3594 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));
3595 g_free (stack_name);
3599 if (stack_slot_is_managed_mutability_pointer (value))
3600 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3604 if (check_overflow (ctx))
3605 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3609 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3615 if (!check_underflow (ctx, 1))
3618 if (!(type = get_boxable_mono_type (ctx, token, opcode)))
3622 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3626 value = stack_pop (ctx);
3627 is_boxed = stack_slot_is_boxed_value (value);
3629 if (stack_slot_is_managed_pointer (value))
3630 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3631 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3632 char *name = stack_slot_full_name (value);
3633 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));
3637 switch (value->type->type) {
3638 case MONO_TYPE_FNPTR:
3640 case MONO_TYPE_TYPEDBYREF:
3641 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3644 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3645 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3649 mono_type_from_opcode (int opcode) {
3657 return &mono_defaults.sbyte_class->byval_arg;
3665 return &mono_defaults.int16_class->byval_arg;
3673 return &mono_defaults.int32_class->byval_arg;
3679 return &mono_defaults.int64_class->byval_arg;
3685 return &mono_defaults.single_class->byval_arg;
3691 return &mono_defaults.double_class->byval_arg;
3697 return &mono_defaults.int_class->byval_arg;
3701 case CEE_LDELEM_REF:
3702 case CEE_STELEM_REF:
3703 return &mono_defaults.object_class->byval_arg;
3706 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3712 do_load_indirect (VerifyContext *ctx, int opcode)
3715 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3717 if (!check_underflow (ctx, 1))
3720 value = stack_pop (ctx);
3721 if (!stack_slot_is_managed_pointer (value)) {
3722 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3723 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3727 if (opcode == CEE_LDIND_REF) {
3728 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3729 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3730 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3732 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3733 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3734 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3739 do_store_indirect (VerifyContext *ctx, int opcode)
3741 ILStackDesc *addr, *val;
3742 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3744 if (!check_underflow (ctx, 2))
3747 val = stack_pop (ctx);
3748 addr = stack_pop (ctx);
3750 check_unmanaged_pointer (ctx, addr);
3752 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3753 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3757 if (stack_slot_is_managed_mutability_pointer (addr)) {
3758 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3762 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3763 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3765 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3766 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3770 do_newarr (VerifyContext *ctx, int token)
3773 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3778 if (!check_underflow (ctx, 1))
3781 value = stack_pop (ctx);
3782 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3783 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));
3785 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3788 /*FIXME handle arrays that are not 0-indexed*/
3790 do_ldlen (VerifyContext *ctx)
3794 if (!check_underflow (ctx, 1))
3797 value = stack_pop (ctx);
3799 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3800 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3802 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3805 /*FIXME handle arrays that are not 0-indexed*/
3806 /*FIXME handle readonly prefix and CMMP*/
3808 do_ldelema (VerifyContext *ctx, int klass_token)
3810 ILStackDesc *index, *array, *res;
3811 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
3817 if (!check_underflow (ctx, 2))
3820 index = stack_pop (ctx);
3821 array = stack_pop (ctx);
3823 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3824 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));
3826 if (!stack_slot_is_null_literal (array)) {
3827 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3828 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3830 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3831 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3833 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3836 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3840 res = stack_push (ctx);
3841 set_stack_value (ctx, res, type, TRUE);
3842 if (ctx->prefix_set & PREFIX_READONLY) {
3843 ctx->prefix_set &= ~PREFIX_READONLY;
3844 res->stype |= CMMP_MASK;
3849 * FIXME handle arrays that are not 0-indexed
3850 * FIXME handle readonly prefix and CMMP
3853 do_ldelem (VerifyContext *ctx, int opcode, int token)
3855 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3856 ILStackDesc *index, *array;
3858 if (!check_underflow (ctx, 2))
3861 if (opcode == CEE_LDELEM) {
3862 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
3863 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3867 type = mono_type_from_opcode (opcode);
3870 index = stack_pop (ctx);
3871 array = stack_pop (ctx);
3873 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3874 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));
3876 if (!stack_slot_is_null_literal (array)) {
3877 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3878 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));
3880 if (opcode == CEE_LDELEM_REF) {
3881 if (array->type->data.klass->valuetype)
3882 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3883 type = &array->type->data.klass->byval_arg;
3885 MonoType *candidate = &array->type->data.klass->byval_arg;
3886 if (IS_STRICT_MODE (ctx)) {
3887 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
3888 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
3889 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)) ||
3890 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
3891 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3893 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
3894 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3899 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3904 * FIXME handle arrays that are not 0-indexed
3907 do_stelem (VerifyContext *ctx, int opcode, int token)
3909 ILStackDesc *index, *array, *value;
3911 if (!check_underflow (ctx, 3))
3914 if (opcode == CEE_STELEM) {
3915 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
3916 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3920 type = mono_type_from_opcode (opcode);
3923 value = stack_pop (ctx);
3924 index = stack_pop (ctx);
3925 array = stack_pop (ctx);
3927 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3928 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));
3930 if (!stack_slot_is_null_literal (array)) {
3931 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3932 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));
3934 if (opcode == CEE_STELEM_REF) {
3935 if (array->type->data.klass->valuetype)
3936 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3937 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3938 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3942 if (opcode == CEE_STELEM_REF) {
3943 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3944 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3945 } else if (opcode != CEE_STELEM_REF) {
3946 if (!verify_stack_type_compatibility (ctx, type, value))
3947 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3949 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
3950 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));
3956 do_throw (VerifyContext *ctx)
3958 ILStackDesc *exception;
3959 if (!check_underflow (ctx, 1))
3961 exception = stack_pop (ctx);
3963 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3964 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3966 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
3967 char *name = mono_type_full_name (exception->type);
3968 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));
3971 /*The stack is left empty after a throw*/
3977 do_endfilter (VerifyContext *ctx)
3979 MonoExceptionClause *clause;
3981 if (IS_STRICT_MODE (ctx)) {
3982 if (ctx->eval.size != 1)
3983 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3985 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
3986 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3989 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3990 if (IS_STRICT_MODE (ctx)) {
3991 if (ctx->ip_offset != clause->handler_offset - 2)
3992 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3994 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3995 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3998 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3999 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4001 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4008 do_leave (VerifyContext *ctx, int delta)
4010 int target = ((gint32)ctx->ip_offset) + delta;
4011 if (target >= ctx->code_size || target < 0)
4012 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4014 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4015 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4017 ctx->target = target;
4023 * Verify br and br.s opcodes.
4026 do_static_branch (VerifyContext *ctx, int delta)
4028 int target = ctx->ip_offset + delta;
4029 if (target < 0 || target >= ctx->code_size) {
4030 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4034 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4036 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4039 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4043 ctx->target = target;
4047 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4049 int i, base = ctx->ip_offset + 5 + count * 4;
4052 if (!check_underflow (ctx, 1))
4055 value = stack_pop (ctx);
4057 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4058 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4060 for (i = 0; i < count; ++i) {
4061 int target = base + read32 (data + i * 4);
4063 if (target < 0 || target >= ctx->code_size) {
4064 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4068 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4070 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4073 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4076 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4081 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4086 if (virtual && !check_underflow (ctx, 1))
4089 if (!virtual && !check_overflow (ctx))
4092 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4093 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4097 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4100 if (mono_method_is_constructor (method))
4101 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4104 ILStackDesc *top = stack_pop (ctx);
4106 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4107 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4109 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4110 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4112 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4113 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4116 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4117 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);
4119 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4120 top->method = method;
4124 do_sizeof (VerifyContext *ctx, int token)
4128 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4129 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4133 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4136 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4137 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4141 if (type->type == MONO_TYPE_VOID) {
4142 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4146 if (check_overflow (ctx))
4147 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4150 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4152 do_localloc (VerifyContext *ctx)
4156 if (ctx->eval.size != 1) {
4157 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4161 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4162 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4166 /*TODO verify top type*/
4167 top = stack_pop (ctx);
4169 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4170 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4174 do_ldstr (VerifyContext *ctx, guint32 token)
4176 GSList *error = NULL;
4177 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4178 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4182 if (!ctx->image->dynamic && !mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4184 ctx->list = g_slist_concat (ctx->list, error);
4185 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4189 if (check_overflow (ctx))
4190 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4194 do_refanyval (VerifyContext *ctx, int token)
4198 if (!check_underflow (ctx, 1))
4201 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4204 top = stack_pop (ctx);
4206 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4207 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));
4209 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4213 do_refanytype (VerifyContext *ctx)
4217 if (!check_underflow (ctx, 1))
4220 top = stack_pop (ctx);
4222 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4223 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));
4225 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4230 do_mkrefany (VerifyContext *ctx, int token)
4234 if (!check_underflow (ctx, 1))
4237 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4240 top = stack_pop (ctx);
4242 if (stack_slot_is_managed_mutability_pointer (top))
4243 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4245 if (!stack_slot_is_managed_pointer (top)) {
4246 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));
4248 MonoType *stack_type = mono_type_get_type_byval (top->type);
4249 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4250 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4252 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4253 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4256 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4260 do_ckfinite (VerifyContext *ctx)
4263 if (!check_underflow (ctx, 1))
4266 top = stack_pop (ctx);
4268 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4269 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));
4270 stack_push_stack_val (ctx, top);
4274 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4276 * @from holds new values for a given control path
4277 * @to holds the current values of a given control path
4279 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4282 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4286 stack_init (ctx, to);
4289 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4292 stack_copy (&ctx->eval, to);
4294 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4295 stack_copy (to, &ctx->eval);
4298 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4300 if (from->size != to->size) {
4301 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4302 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));
4306 //FIXME we need to preserve CMMP attributes
4307 //FIXME we must take null literals into consideration.
4308 for (i = 0; i < from->size; ++i) {
4309 ILStackDesc *new_slot = from->stack + i;
4310 ILStackDesc *old_slot = to->stack + i;
4311 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4312 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4313 MonoClass *old_class = mono_class_from_mono_type (old_type);
4314 MonoClass *new_class = mono_class_from_mono_type (new_type);
4315 MonoClass *match_class = NULL;
4317 // S := T then U = S (new value is compatible with current value, keep current)
4318 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4319 copy_stack_value (new_slot, old_slot);
4323 // T := S then U = T (old value is compatible with current value, use new)
4324 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4325 copy_stack_value (old_slot, new_slot);
4329 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4330 char *old_name = stack_slot_full_name (old_slot);
4331 char *new_name = stack_slot_full_name (new_slot);
4332 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));
4338 //both are reference types, use closest common super type
4339 if (!mono_class_from_mono_type (old_type)->valuetype
4340 && !mono_class_from_mono_type (new_type)->valuetype
4341 && !stack_slot_is_managed_pointer (old_slot)
4342 && !stack_slot_is_managed_pointer (new_slot)) {
4344 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4345 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4346 match_class = old_class->supertypes [j];
4351 mono_class_setup_interfaces (old_class, &error);
4352 if (!mono_error_ok (&error)) {
4353 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));
4354 mono_error_cleanup (&error);
4357 for (j = 0; j < old_class->interface_count; ++j) {
4358 for (k = 0; k < new_class->interface_count; ++k) {
4359 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4360 match_class = old_class->interfaces [j];
4366 //No decent super type found, use object
4367 match_class = mono_defaults.object_class;
4369 } 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)) {
4370 match_class = mono_defaults.object_class;
4375 char *old_name = stack_slot_full_name (old_slot);
4376 char *new_name = stack_slot_full_name (new_slot);
4377 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));
4381 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4385 g_assert (match_class);
4386 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4387 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4393 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4394 to->flags |= IL_CODE_STACK_MERGED;
4397 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4398 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4401 * is_clause_in_range :
4403 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4406 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4408 if (clause->try_offset >= start && clause->try_offset < end)
4410 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4416 * is_clause_inside_range :
4418 * Returns TRUE if @clause lies completely inside the @start - @end range.
4421 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4423 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4425 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4431 * is_clause_nested :
4433 * Returns TRUE if @nested is nested in @clause.
4436 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4438 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4440 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4441 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4444 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4445 * the each pair of exception must have the following properties:
4446 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4447 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4448 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4451 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4453 /*clause is nested*/
4454 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4455 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4459 /*wrong nesting order.*/
4460 if (is_clause_nested (clause, to_test)) {
4461 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4465 /*mutual protection*/
4466 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4467 /*handlers are not disjoint*/
4468 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4469 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4472 /* handlers are not catch or filter */
4473 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4474 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4481 /*not completelly disjoint*/
4482 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4483 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4484 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4487 #define code_bounds_check(size) \
4488 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4489 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4494 mono_opcode_is_prefix (int op)
4497 case MONO_CEE_UNALIGNED_:
4498 case MONO_CEE_VOLATILE_:
4499 case MONO_CEE_TAIL_:
4500 case MONO_CEE_CONSTRAINED_:
4501 case MONO_CEE_READONLY_:
4508 * FIXME: need to distinguish between valid and verifiable.
4509 * Need to keep track of types on the stack.
4510 * Verify types for opcodes.
4513 mono_method_verify (MonoMethod *method, int level)
4516 const unsigned char *ip, *code_start;
4517 const unsigned char *end;
4518 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4520 int i, n, need_merge = 0, start = 0;
4521 guint token, ip_offset = 0, prefix = 0;
4522 MonoGenericContext *generic_context = NULL;
4526 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4528 init_verifier_stats ();
4530 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4531 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4535 memset (&ctx, 0, sizeof (VerifyContext));
4537 //FIXME use mono_method_get_signature_full
4538 ctx.signature = mono_method_signature (method);
4539 if (!ctx.signature) {
4540 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4542 finish_collect_stats ();
4545 ctx.header = mono_method_get_header (method);
4547 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4548 finish_collect_stats ();
4551 ctx.method = method;
4552 code_start = ip = ctx.header->code;
4553 end = ip + ctx.header->code_size;
4554 ctx.image = image = method->klass->image;
4557 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4558 ctx.max_stack = ctx.header->max_stack;
4559 ctx.verifiable = ctx.valid = 1;
4562 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4563 ctx.code_size = ctx.header->code_size;
4564 MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4566 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4568 ctx.num_locals = ctx.header->num_locals;
4569 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4570 MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4572 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4573 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4575 ctx.params = g_new (MonoType*, ctx.max_args);
4576 MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4578 if (ctx.signature->hasthis)
4579 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4580 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4582 if (ctx.signature->is_inflated)
4583 ctx.generic_context = generic_context = mono_method_get_context (method);
4585 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4586 if (method->is_generic)
4587 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4589 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4592 for (i = 0; i < ctx.num_locals; ++i) {
4593 MonoType *uninflated = ctx.locals [i];
4594 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4595 if (!mono_error_ok (&error)) {
4596 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4597 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4599 mono_error_cleanup (&error);
4600 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4606 for (i = 0; i < ctx.max_args; ++i) {
4607 MonoType *uninflated = ctx.params [i];
4608 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4609 if (!mono_error_ok (&error)) {
4610 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4611 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4613 mono_error_cleanup (&error);
4614 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4619 stack_init (&ctx, &ctx.eval);
4621 for (i = 0; i < ctx.num_locals; ++i) {
4622 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4624 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4625 char *name = mono_type_full_name (ctx.locals [i]);
4626 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4633 for (i = 0; i < ctx.max_args; ++i) {
4634 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4637 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4638 char *name = mono_type_full_name (ctx.params [i]);
4639 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4648 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4649 MonoExceptionClause *clause = ctx.header->clauses + i;
4650 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); );
4652 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4653 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4655 if (clause->try_len <= 0)
4656 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4658 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4659 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4661 if (clause->handler_len <= 0)
4662 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4664 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4665 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4667 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4668 if (clause->data.filter_offset > ctx.code_size)
4669 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4671 if (clause->data.filter_offset >= clause->handler_offset)
4672 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4675 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4676 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4681 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4682 if (clause->try_offset + clause->try_len < ctx.code_size)
4683 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4684 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4685 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4687 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4688 if (!clause->data.catch_class) {
4689 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
4693 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4695 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4696 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4697 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4701 original_bb = bb = mono_basic_block_split (method, &error);
4702 if (!mono_error_ok (&error)) {
4703 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
4704 mono_error_cleanup (&error);
4709 while (ip < end && ctx.valid) {
4711 ip_offset = ip - code_start;
4713 const unsigned char *ip_copy = ip;
4716 if (ip_offset > bb->end) {
4717 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
4721 if (ip_offset == bb->end)
4724 op_size = mono_opcode_value_and_size (&ip_copy, end, &op);
4725 if (op_size == -1) {
4726 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
4730 if (ADD_IS_GREATER_OR_OVF (ip_offset, op_size, bb->end)) {
4731 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
4735 /*Last Instruction*/
4736 if (ip_offset + op_size == bb->end && mono_opcode_is_prefix (op)) {
4737 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));
4742 ctx.ip_offset = ip_offset = ip - code_start;
4744 /*We need to check against fallthrou in and out of protected blocks.
4745 * For fallout we check the once a protected block ends, if the start flag is not set.
4746 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4747 * TODO convert these checks to be done using flags and not this loop
4749 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4750 MonoExceptionClause *clause = ctx.header->clauses + i;
4752 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4753 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4757 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4758 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4759 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4761 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4765 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4766 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4770 if (clause->handler_offset == ip_offset && start == 0) {
4771 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4775 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
4776 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4781 /*This must be done after fallthru detection otherwise it won't happen.*/
4783 /*FIXME remove this once we move all bad branch checking code to use BB only*/
4784 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
4793 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4794 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4797 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4800 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4801 #ifdef MONO_VERIFIER_DEBUG
4804 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4805 discode [strlen (discode) - 1] = 0; /* no \n */
4806 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4809 dump_stack_state (&ctx.code [ip_offset]);
4810 dump_stack_state (&ctx.eval);
4823 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4829 code_bounds_check (2);
4830 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4834 case CEE_ADD_OVF_UN:
4835 do_binop (&ctx, *ip, add_ovf_un_table);
4839 case CEE_SUB_OVF_UN:
4840 do_binop (&ctx, *ip, sub_ovf_un_table);
4847 case CEE_MUL_OVF_UN:
4848 do_binop (&ctx, *ip, bin_ovf_table);
4853 do_binop (&ctx, *ip, add_table);
4858 do_binop (&ctx, *ip, sub_table);
4865 do_binop (&ctx, *ip, bin_op_table);
4874 do_binop (&ctx, *ip, int_bin_op_table);
4881 do_binop (&ctx, *ip, shift_op_table);
4886 if (!check_underflow (&ctx, 1))
4888 stack_pop_safe (&ctx);
4902 /*TODO support definite assignment verification? */
4903 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
4911 store_local (&ctx, *ip - CEE_STLOC_0);
4916 code_bounds_check (2);
4917 store_local (&ctx, ip [1]);
4922 code_bounds_check (2);
4923 store_arg (&ctx, ip [1]);
4937 if (check_overflow (&ctx))
4938 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4943 code_bounds_check (2);
4944 if (check_overflow (&ctx))
4945 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4950 code_bounds_check (5);
4951 if (check_overflow (&ctx))
4952 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4957 code_bounds_check (9);
4958 if (check_overflow (&ctx))
4959 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4964 code_bounds_check (5);
4965 if (check_overflow (&ctx))
4966 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4971 code_bounds_check (9);
4972 if (check_overflow (&ctx))
4973 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4978 if (check_overflow (&ctx))
4979 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4985 code_bounds_check (2);
4986 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4999 code_bounds_check (2);
5000 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5007 code_bounds_check (5);
5008 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5021 code_bounds_check (5);
5022 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5029 code_bounds_check (2);
5030 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5035 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5041 if (!check_underflow (&ctx, 1))
5043 if (!check_overflow (&ctx))
5045 top = stack_push (&ctx);
5046 copy_stack_value (top, stack_peek (&ctx, 1));
5052 code_bounds_check (5);
5054 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5055 token = read32 (ip + 1);
5056 if (in_any_block (ctx.header, ip_offset))
5057 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5059 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5061 * FIXME: check signature, retval, arguments etc.
5067 code_bounds_check (5);
5068 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5073 code_bounds_check (5);
5074 token = read32 (ip + 1);
5076 * FIXME: check signature, retval, arguments etc.
5077 * FIXME: check requirements for tail call
5079 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5083 code_bounds_check (2);
5084 do_static_branch (&ctx, (signed char)ip [1] + 2);
5092 code_bounds_check (2);
5093 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5099 code_bounds_check (5);
5100 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5108 code_bounds_check (5);
5109 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5116 code_bounds_check (5);
5117 entries = read32 (ip + 1);
5119 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5120 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5123 code_bounds_check (sizeof (guint32) * entries);
5125 do_switch (&ctx, entries, ip);
5126 ip += sizeof (guint32) * entries;
5140 do_load_indirect (&ctx, *ip);
5152 do_store_indirect (&ctx, *ip);
5158 do_unary_math_op (&ctx, *ip);
5168 do_conversion (&ctx, TYPE_I4);
5174 do_conversion (&ctx, TYPE_I8);
5181 do_conversion (&ctx, TYPE_R8);
5187 do_conversion (&ctx, TYPE_NATIVE_INT);
5192 code_bounds_check (5);
5193 do_cpobj (&ctx, read32 (ip + 1));
5198 code_bounds_check (5);
5199 do_ldobj_value (&ctx, read32 (ip + 1));
5204 code_bounds_check (5);
5205 do_ldstr (&ctx, read32 (ip + 1));
5210 code_bounds_check (5);
5211 do_newobj (&ctx, read32 (ip + 1));
5217 code_bounds_check (5);
5218 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5224 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5229 code_bounds_check (5);
5230 do_unbox_value (&ctx, read32 (ip + 1));
5242 code_bounds_check (5);
5243 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5249 code_bounds_check (5);
5250 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5255 code_bounds_check (5);
5256 do_store_field (&ctx, read32 (ip + 1));
5261 code_bounds_check (5);
5262 do_store_static_field (&ctx, read32 (ip + 1));
5267 code_bounds_check (5);
5268 do_stobj (&ctx, read32 (ip + 1));
5272 case CEE_CONV_OVF_I1_UN:
5273 case CEE_CONV_OVF_I2_UN:
5274 case CEE_CONV_OVF_I4_UN:
5275 case CEE_CONV_OVF_U1_UN:
5276 case CEE_CONV_OVF_U2_UN:
5277 case CEE_CONV_OVF_U4_UN:
5278 do_conversion (&ctx, TYPE_I4);
5282 case CEE_CONV_OVF_I8_UN:
5283 case CEE_CONV_OVF_U8_UN:
5284 do_conversion (&ctx, TYPE_I8);
5288 case CEE_CONV_OVF_I_UN:
5289 case CEE_CONV_OVF_U_UN:
5290 do_conversion (&ctx, TYPE_NATIVE_INT);
5295 code_bounds_check (5);
5296 do_box_value (&ctx, read32 (ip + 1));
5301 code_bounds_check (5);
5302 do_newarr (&ctx, read32 (ip + 1));
5312 code_bounds_check (5);
5313 do_ldelema (&ctx, read32 (ip + 1));
5327 case CEE_LDELEM_REF:
5328 do_ldelem (&ctx, *ip, 0);
5339 case CEE_STELEM_REF:
5340 do_stelem (&ctx, *ip, 0);
5345 code_bounds_check (5);
5346 do_ldelem (&ctx, *ip, read32 (ip + 1));
5351 code_bounds_check (5);
5352 do_stelem (&ctx, *ip, read32 (ip + 1));
5357 code_bounds_check (5);
5358 do_unbox_any (&ctx, read32 (ip + 1));
5362 case CEE_CONV_OVF_I1:
5363 case CEE_CONV_OVF_U1:
5364 case CEE_CONV_OVF_I2:
5365 case CEE_CONV_OVF_U2:
5366 case CEE_CONV_OVF_I4:
5367 case CEE_CONV_OVF_U4:
5368 do_conversion (&ctx, TYPE_I4);
5372 case CEE_CONV_OVF_I8:
5373 case CEE_CONV_OVF_U8:
5374 do_conversion (&ctx, TYPE_I8);
5378 case CEE_CONV_OVF_I:
5379 case CEE_CONV_OVF_U:
5380 do_conversion (&ctx, TYPE_NATIVE_INT);
5385 code_bounds_check (5);
5386 do_refanyval (&ctx, read32 (ip + 1));
5396 code_bounds_check (5);
5397 do_mkrefany (&ctx, read32 (ip + 1));
5402 code_bounds_check (5);
5403 do_load_token (&ctx, read32 (ip + 1));
5407 case CEE_ENDFINALLY:
5408 if (!is_correct_endfinally (ctx.header, ip_offset))
5409 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5416 code_bounds_check (5);
5417 do_leave (&ctx, read32 (ip + 1) + 5);
5424 code_bounds_check (2);
5425 do_leave (&ctx, (signed char)ip [1] + 2);
5432 code_bounds_check (2);
5436 code_bounds_check (3);
5437 store_local (&ctx, read16 (ip + 1));
5442 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5450 do_cmp_op (&ctx, cmp_br_op, *ip);
5455 code_bounds_check (3);
5456 store_arg (&ctx, read16 (ip + 1) );
5462 if (!check_overflow (&ctx))
5464 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5465 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5466 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5471 code_bounds_check (5);
5472 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5477 code_bounds_check (5);
5478 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5484 code_bounds_check (3);
5485 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5491 code_bounds_check (3);
5492 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5505 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5509 do_endfilter (&ctx);
5513 case CEE_UNALIGNED_:
5514 code_bounds_check (2);
5515 prefix |= PREFIX_UNALIGNED;
5519 prefix |= PREFIX_VOLATILE;
5523 prefix |= PREFIX_TAIL;
5525 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5526 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5530 code_bounds_check (5);
5531 do_initobj (&ctx, read32 (ip + 1));
5535 case CEE_CONSTRAINED_:
5536 code_bounds_check (5);
5537 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5538 prefix |= PREFIX_CONSTRAINED;
5543 prefix |= PREFIX_READONLY;
5548 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5549 if (!check_underflow (&ctx, 3))
5551 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5556 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5557 if (!check_underflow (&ctx, 3))
5559 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5567 if (!is_correct_rethrow (ctx.header, ip_offset))
5568 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5575 code_bounds_check (5);
5576 do_sizeof (&ctx, read32 (ip + 1));
5580 case CEE_REFANYTYPE:
5581 do_refanytype (&ctx);
5586 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5592 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5596 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5598 if (!ctx.prefix_set) //first prefix
5599 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5600 ctx.prefix_set |= prefix;
5601 ctx.has_flags = TRUE;
5605 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5607 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5608 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5609 if (ctx.prefix_set & PREFIX_READONLY)
5610 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5611 if (ctx.prefix_set & PREFIX_VOLATILE)
5612 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5613 if (ctx.prefix_set & PREFIX_UNALIGNED)
5614 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5615 ctx.prefix_set = prefix = 0;
5616 ctx.has_flags = FALSE;
5620 * if ip != end we overflowed: mark as error.
5622 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5623 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5626 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5627 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5628 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5629 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5630 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5632 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5633 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5635 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5636 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5638 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5639 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));
5642 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5643 char *method_name = mono_method_full_name (ctx.method, TRUE);
5644 char *type = mono_type_get_full_name (ctx.method->klass);
5645 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5646 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));
5648 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5649 g_free (method_name);
5655 for (i = 0; i < ctx.header->code_size; ++i) {
5656 if (ctx.code [i].stack)
5657 g_free (ctx.code [i].stack);
5661 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5663 g_slist_free (ctx.funptrs);
5665 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5666 mono_metadata_free_type (tmp->data);
5667 g_slist_free (ctx.exception_types);
5669 for (i = 0; i < ctx.num_locals; ++i) {
5671 mono_metadata_free_type (ctx.locals [i]);
5673 for (i = 0; i < ctx.max_args; ++i) {
5675 mono_metadata_free_type (ctx.params [i]);
5679 g_free (ctx.eval.stack);
5682 g_free (ctx.locals);
5683 g_free (ctx.params);
5684 mono_basic_block_free (original_bb);
5685 mono_metadata_free_mh (ctx.header);
5687 finish_collect_stats ();
5692 mono_verify_corlib ()
5694 /* This is a public API function so cannot be removed */
5699 * Returns true if @method needs to be verified.
5703 mono_verifier_is_enabled_for_method (MonoMethod *method)
5705 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5709 * Returns true if @klass need to be verified.
5713 mono_verifier_is_enabled_for_class (MonoClass *klass)
5715 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
5719 mono_verifier_is_enabled_for_image (MonoImage *image)
5721 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
5725 mono_verifier_is_method_full_trust (MonoMethod *method)
5727 return mono_verifier_is_class_full_trust (method->klass);
5731 * Returns if @klass is under full trust or not.
5733 * TODO This code doesn't take CAS into account.
5735 * Under verify_all all user code must be verifiable if no security option was set
5739 mono_verifier_is_class_full_trust (MonoClass *klass)
5741 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5742 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
5743 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
5745 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5746 return trusted_location || klass->image == mono_defaults.corlib;
5747 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
5751 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5753 return mono_method_verify (method,
5754 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5755 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5756 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5760 get_field_end (MonoClassField *field)
5763 int size = mono_type_size (field->type, &align);
5765 size = 4; /*FIXME Is this a safe bet?*/
5766 return size + field->offset;
5770 verify_class_for_overlapping_reference_fields (MonoClass *class)
5773 gpointer iter = NULL;
5774 MonoClassField *field;
5775 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5776 /*We can't skip types with !has_references since this is calculated after we have run.*/
5777 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
5781 /*We must check for stuff overlapping reference fields.
5782 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
5784 while ((field = mono_class_get_fields (class, &iter))) {
5785 int fieldEnd = get_field_end (field);
5786 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5789 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5792 for (j = i; j < class->field.count; ++j) {
5793 MonoClassField *other = &class->fields [j];
5794 int otherEnd = get_field_end (other);
5795 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5798 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5801 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5809 field_hash (gconstpointer key)
5811 const MonoClassField *field = key;
5812 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
5816 field_equals (gconstpointer _a, gconstpointer _b)
5818 const MonoClassField *a = _a;
5819 const MonoClassField *b = _b;
5820 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
5825 verify_class_fields (MonoClass *class)
5827 gpointer iter = NULL;
5828 MonoClassField *field;
5829 MonoGenericContext *context = mono_class_get_context (class);
5830 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
5831 if (class->generic_container)
5832 context = &class->generic_container->context;
5834 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5835 if (!mono_type_is_valid_type_in_context (field->type, context)) {
5836 g_hash_table_destroy (unique_fields);
5839 if (g_hash_table_lookup (unique_fields, field)) {
5840 g_hash_table_destroy (unique_fields);
5843 g_hash_table_insert (unique_fields, field, field);
5845 g_hash_table_destroy (unique_fields);
5850 verify_interfaces (MonoClass *class)
5853 for (i = 0; i < class->interface_count; ++i) {
5854 MonoClass *iface = class->interfaces [i];
5855 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
5862 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
5865 gpointer iter = NULL;
5866 MonoClassField *field;
5867 MonoClass *field_class;
5869 if (!class->valuetype)
5872 type = class->byval_arg.type;
5873 /*primitive type fields are not properly decoded*/
5874 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
5877 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5881 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
5884 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
5886 if (field_class == target_class || class == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
5894 verify_valuetype_layout (MonoClass *class)
5897 res = verify_valuetype_layout_with_target (class, class);
5902 recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
5905 MonoClass **constraints;
5906 MonoGenericParamInfo *param_info;
5908 g_assert (mono_type_is_generic_argument (type));
5910 idx = mono_type_get_generic_param_num (type);
5911 if (mono_bitset_test_fast (used_args, idx))
5914 mono_bitset_set_fast (used_args, idx);
5915 param_info = mono_generic_container_get_param_info (gc, idx);
5917 if (!param_info->constraints)
5920 for (constraints = param_info->constraints; *constraints; ++constraints) {
5921 MonoClass *ctr = *constraints;
5922 MonoType *constraint_type = &ctr->byval_arg;
5924 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
5931 verify_generic_parameters (MonoClass *class)
5934 MonoGenericContainer *gc = class->generic_container;
5935 MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
5937 for (i = 0; i < gc->type_argc; ++i) {
5938 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
5939 MonoClass **constraints;
5941 if (!param_info->constraints)
5944 mono_bitset_clear_all (used_args);
5945 mono_bitset_set_fast (used_args, i);
5947 for (constraints = param_info->constraints; *constraints; ++constraints) {
5948 MonoClass *ctr = *constraints;
5949 MonoType *constraint_type = &ctr->byval_arg;
5951 if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
5954 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
5956 if (ctr->generic_class && !mono_class_is_valid_generic_instantiation (NULL, ctr))
5960 mono_bitset_free (used_args);
5964 mono_bitset_free (used_args);
5969 * Check if the class is verifiable.
5971 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5972 * field and invalid generic instantiation are fatal errors.
5974 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5978 mono_verifier_verify_class (MonoClass *class)
5980 /*Neither <Module>, object or ifaces have parent.*/
5981 if (!class->parent &&
5982 class != mono_defaults.object_class &&
5983 !MONO_CLASS_IS_INTERFACE (class) &&
5984 (!class->image->dynamic && class->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
5986 if (class->parent) {
5987 if (MONO_CLASS_IS_INTERFACE (class->parent))
5989 if (!class->generic_class && class->parent->generic_container)
5992 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
5994 if (class->generic_container && !verify_generic_parameters (class))
5996 if (!verify_class_for_overlapping_reference_fields (class))
5998 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
6000 if (class->generic_class == NULL && !verify_class_fields (class))
6002 if (class->valuetype && !verify_valuetype_layout (class))
6004 if (!verify_interfaces (class))
6011 mono_verifier_verify_class (MonoClass *class)
6013 /* The verifier was disabled at compile time */
6018 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
6020 /* The verifier was disabled at compile time */
6025 mono_verifier_is_class_full_trust (MonoClass *klass)
6027 /* The verifier was disabled at compile time */
6032 mono_verifier_is_method_full_trust (MonoMethod *method)
6034 /* The verifier was disabled at compile time */
6039 mono_verifier_is_enabled_for_image (MonoImage *image)
6041 /* The verifier was disabled at compile time */
6046 mono_verifier_is_enabled_for_class (MonoClass *klass)
6048 /* The verifier was disabled at compile time */
6053 mono_verifier_is_enabled_for_method (MonoMethod *method)
6055 /* The verifier was disabled at compile time */
6060 mono_method_verify (MonoMethod *method, int level)
6062 /* The verifier was disabled at compile time */
6067 mono_free_verify_list (GSList *list)
6069 /* The verifier was disabled at compile time */
6070 /* will always be null if verifier is disabled */