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>
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))
512 /*This function returns NULL if the type is not instantiatable*/
514 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
519 result = mono_class_inflate_generic_type_checked (type, context, &error);
520 if (!mono_error_ok (&error)) {
521 mono_error_cleanup (&error);
529 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
534 if (ginst->type_argc != gc->type_argc)
537 for (i = 0; i < gc->type_argc; ++i) {
538 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
539 MonoClass *paramClass;
540 MonoClass **constraints;
542 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
544 if (mono_type_is_generic_argument (ginst->type_argv [i]))
545 continue; //it's not our job to validate type variables
547 paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
549 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
552 /*it's not safe to call mono_class_init from here*/
553 if (paramClass->generic_class && !paramClass->inited) {
554 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
558 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
561 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
564 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
567 if (!param_info->constraints)
570 for (constraints = param_info->constraints; *constraints; ++constraints) {
571 MonoClass *ctr = *constraints;
574 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
575 if (!mono_error_ok (&error)) {
576 mono_error_cleanup (&error);
579 ctr = mono_class_from_mono_type (inflated);
580 mono_metadata_free_type (inflated);
582 if (!mono_class_is_assignable_from_slow (ctr, paramClass))
590 * Return true if @candidate is constraint compatible with @target.
592 * This means that @candidate constraints are a super set of @target constaints
595 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoGenericContext *context)
597 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
598 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
600 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
601 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
602 if ((tmask & cmask) != tmask)
605 if (tinfo->constraints) {
606 MonoClass **target_class, **candidate_class;
607 if (!cinfo->constraints)
609 for (target_class = tinfo->constraints; *target_class; ++target_class) {
611 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
614 tc = mono_class_from_mono_type (inflated);
615 mono_metadata_free_type (inflated);
617 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
619 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
622 cc = mono_class_from_mono_type (inflated);
623 mono_metadata_free_type (inflated);
625 if (mono_class_is_assignable_from (tc, cc))
628 if (!*candidate_class)
635 static MonoGenericParam*
636 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
638 MonoGenericContainer *gc;
639 MonoMethod *method = ctx->method;
642 num = mono_type_get_generic_param_num (type);
644 if (type->type == MONO_TYPE_VAR) {
645 MonoClass *gtd = method->klass;
646 if (gtd->generic_class)
647 gtd = gtd->generic_class->container_class;
648 gc = gtd->generic_container;
650 MonoMethod *gmd = method;
651 if (method->is_inflated)
652 gmd = ((MonoMethodInflated*)method)->declaring;
653 gc = mono_method_get_generic_container (gmd);
657 return mono_generic_container_get_param (gc, num);
663 * Verify if @type is valid for the given @ctx verification context.
664 * this function checks for VAR and MVAR types that are invalid under the current verifier,
665 * This means that it either
668 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
670 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
674 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
677 for (i = 0; i < ginst->type_argc; ++i) {
678 MonoType *type = ginst->type_argv [i];
679 if (!is_valid_type_in_context (ctx, type))
686 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
689 for (i = 0; i < ginst->type_argc; ++i) {
690 MonoType *type = ginst->type_argv [i];
691 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
692 MonoGenericParam *candidate;
694 if (!mono_type_is_generic_argument (type))
697 if (!is_valid_type_in_context (ctx, type))
700 candidate = verifier_get_generic_param_from_type (ctx, type);
702 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, context))
709 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
711 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
712 MonoGenericInst *ginst = gmethod->context.method_inst;
713 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
714 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
718 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
720 MonoGenericClass *gklass = klass->generic_class;
721 MonoGenericInst *ginst = gklass->context.class_inst;
722 MonoGenericContainer *gc = gklass->container_class->generic_container;
723 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
727 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
729 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
730 MonoGenericInst *ginst = gmethod->context.method_inst;
731 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
732 if (!gc) /*non-generic inflated method - it's part of a generic type */
734 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
736 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
741 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
743 MonoGenericClass *gklass = klass->generic_class;
744 MonoGenericInst *ginst = gklass->context.class_inst;
745 MonoGenericContainer *gc = gklass->container_class->generic_container;
746 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
748 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
752 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
757 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
761 if (!is_valid_type_in_context (ctx, type)) {
762 char *str = mono_type_full_name (type);
763 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
764 type->type == MONO_TYPE_VAR ? "!" : "!!",
766 type->type == MONO_TYPE_VAR ? "class" : "method",
768 MONO_EXCEPTION_BAD_IMAGE);
773 klass = mono_class_from_mono_type (type);
774 mono_class_init (klass);
775 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
776 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
777 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);
779 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);
783 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
784 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);
788 if (!klass->generic_class)
791 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
792 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);
796 if (!mono_class_repect_method_constraints (ctx, klass)) {
797 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);
804 static verify_result_t
805 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
807 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
808 return RESULT_INVALID;
810 if (!method->is_inflated)
813 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
814 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);
815 return RESULT_INVALID;
818 if (!mono_method_repect_method_constraints (ctx, method)) {
819 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));
820 return RESULT_UNVERIFIABLE;
826 static MonoClassField*
827 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
828 MonoClassField *field;
829 MonoClass *klass = NULL;
831 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
832 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);
836 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
837 if (!field || !field->parent || !klass) {
838 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);
842 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
850 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
853 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
854 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);
858 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
861 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);
865 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
872 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
875 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
876 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
880 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
883 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);
887 if (!mono_type_is_valid_in_context (ctx, type))
894 /* stack_slot_get_type:
896 * Returns the stack type of @value. This value includes POINTER_MASK.
898 * Use this function to checks that account for a managed pointer.
901 stack_slot_get_type (ILStackDesc *value)
903 return value->stype & RAW_TYPE_MASK;
906 /* stack_slot_get_underlying_type:
908 * Returns the stack type of @value. This value does not include POINTER_MASK.
910 * Use this function is cases where the fact that the value could be a managed pointer is
911 * irrelevant. For example, field load doesn't care about this fact of type on stack.
914 stack_slot_get_underlying_type (ILStackDesc *value)
916 return value->stype & TYPE_MASK;
919 /* stack_slot_is_managed_pointer:
921 * Returns TRUE is @value is a managed pointer.
924 stack_slot_is_managed_pointer (ILStackDesc *value)
926 return (value->stype & POINTER_MASK) == POINTER_MASK;
929 /* stack_slot_is_managed_mutability_pointer:
931 * Returns TRUE is @value is a managed mutability pointer.
933 static G_GNUC_UNUSED gboolean
934 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
936 return (value->stype & CMMP_MASK) == CMMP_MASK;
939 /* stack_slot_is_null_literal:
941 * Returns TRUE is @value is the null literal.
944 stack_slot_is_null_literal (ILStackDesc *value)
946 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
950 /* stack_slot_is_this_pointer:
952 * Returns TRUE is @value is the this literal
955 stack_slot_is_this_pointer (ILStackDesc *value)
957 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
960 /* stack_slot_is_boxed_value:
962 * Returns TRUE is @value is a boxed value
965 stack_slot_is_boxed_value (ILStackDesc *value)
967 return (value->stype & BOXED_MASK) == BOXED_MASK;
971 stack_slot_get_name (ILStackDesc *value)
973 return type_names [value->stype & TYPE_MASK];
976 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
977 if (PRED (value)) { \
979 g_string_append (str, ", "); \
980 g_string_append (str, NAME); \
985 stack_slot_stack_type_full_name (ILStackDesc *value)
987 GString *str = g_string_new ("");
989 gboolean has_pred = FALSE, first = TRUE;
991 if ((value->stype & TYPE_MASK) != value->stype) {
992 g_string_append(str, "[");
993 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
994 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
995 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
996 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
997 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1001 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1003 g_string_append(str, "[");
1005 g_string_append (str, ", ");
1006 g_string_append (str, "unboxed");
1011 g_string_append(str, "] ");
1013 g_string_append (str, stack_slot_get_name (value));
1015 g_string_free (str, FALSE);
1020 stack_slot_full_name (ILStackDesc *value)
1022 char *type_name = mono_type_full_name (value->type);
1023 char *stack_name = stack_slot_stack_type_full_name (value);
1024 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1026 g_free (stack_name);
1030 //////////////////////////////////////////////////////////////////
1032 mono_free_verify_list (GSList *list)
1034 MonoVerifyInfoExtended *info;
1037 for (tmp = list; tmp; tmp = tmp->next) {
1039 g_free (info->info.message);
1042 g_slist_free (list);
1045 #define ADD_ERROR(list,msg) \
1047 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1048 vinfo->info.status = MONO_VERIFY_ERROR; \
1049 vinfo->info.message = (msg); \
1050 (list) = g_slist_prepend ((list), vinfo); \
1053 #define ADD_WARN(list,code,msg) \
1055 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1056 vinfo->info.status = (code); \
1057 vinfo->info.message = (msg); \
1058 (list) = g_slist_prepend ((list), vinfo); \
1061 #define ADD_INVALID(list,msg) \
1063 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1064 vinfo->status = MONO_VERIFY_ERROR; \
1065 vinfo->message = (msg); \
1066 (list) = g_slist_prepend ((list), vinfo); \
1067 /*G_BREAKPOINT ();*/ \
1071 #define CHECK_STACK_UNDERFLOW(num) \
1073 if (cur_stack < (num)) \
1074 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1077 #define CHECK_STACK_OVERFLOW() \
1079 if (cur_stack >= max_stack) \
1080 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1085 in_any_block (MonoMethodHeader *header, guint offset)
1088 MonoExceptionClause *clause;
1090 for (i = 0; i < header->num_clauses; ++i) {
1091 clause = &header->clauses [i];
1092 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1094 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1096 if (MONO_OFFSET_IN_FILTER (clause, offset))
1103 * in_any_exception_block:
1105 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1108 in_any_exception_block (MonoMethodHeader *header, guint offset)
1111 MonoExceptionClause *clause;
1113 for (i = 0; i < header->num_clauses; ++i) {
1114 clause = &header->clauses [i];
1115 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1117 if (MONO_OFFSET_IN_FILTER (clause, offset))
1124 * is_valid_branch_instruction:
1126 * Verify if it's valid to perform a branch from @offset to @target.
1127 * This should be used with br and brtrue/false.
1128 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1129 * The major diferent from other similiar functions is that branching into a
1130 * finally/fault block is invalid instead of just unverifiable.
1133 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1136 MonoExceptionClause *clause;
1138 for (i = 0; i < header->num_clauses; ++i) {
1139 clause = &header->clauses [i];
1140 /*branching into a finally block is invalid*/
1141 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1142 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1143 MONO_OFFSET_IN_HANDLER (clause, target))
1146 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1148 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1150 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1157 * is_valid_cmp_branch_instruction:
1159 * Verify if it's valid to perform a branch from @offset to @target.
1160 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1161 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1163 * The major diferences from other similar functions are that most errors lead to invalid
1164 * code and only branching out of finally, filter or fault clauses is unverifiable.
1167 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1170 MonoExceptionClause *clause;
1172 for (i = 0; i < header->num_clauses; ++i) {
1173 clause = &header->clauses [i];
1174 /*branching out of a handler or finally*/
1175 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1176 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1177 !MONO_OFFSET_IN_HANDLER (clause, target))
1180 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1182 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1184 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1191 * A leave can't escape a finally block
1194 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1197 MonoExceptionClause *clause;
1199 for (i = 0; i < header->num_clauses; ++i) {
1200 clause = &header->clauses [i];
1201 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1203 if (MONO_OFFSET_IN_FILTER (clause, offset))
1210 * A rethrow can't happen outside of a catch handler.
1213 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1216 MonoExceptionClause *clause;
1218 for (i = 0; i < header->num_clauses; ++i) {
1219 clause = &header->clauses [i];
1220 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1222 if (MONO_OFFSET_IN_FILTER (clause, offset))
1229 * An endfinally can't happen outside of a finally/fault handler.
1232 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1235 MonoExceptionClause *clause;
1237 for (i = 0; i < header->num_clauses; ++i) {
1238 clause = &header->clauses [i];
1239 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1247 * An endfilter can only happens inside a filter clause.
1248 * In non-strict mode filter is allowed inside the handler clause too
1250 static MonoExceptionClause *
1251 is_correct_endfilter (VerifyContext *ctx, guint offset)
1254 MonoExceptionClause *clause;
1256 for (i = 0; i < ctx->header->num_clauses; ++i) {
1257 clause = &ctx->header->clauses [i];
1258 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1260 if (MONO_OFFSET_IN_FILTER (clause, offset))
1262 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1270 * Non-strict endfilter can happens inside a try block or any handler block
1273 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1276 MonoExceptionClause *clause;
1278 for (i = 0; i < ctx->header->num_clauses; ++i) {
1279 clause = &ctx->header->clauses [i];
1280 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1287 is_valid_bool_arg (ILStackDesc *arg)
1289 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1293 switch (stack_slot_get_underlying_type (arg)) {
1296 case TYPE_NATIVE_INT:
1300 g_assert (arg->type);
1301 switch (arg->type->type) {
1302 case MONO_TYPE_CLASS:
1303 case MONO_TYPE_STRING:
1304 case MONO_TYPE_OBJECT:
1305 case MONO_TYPE_SZARRAY:
1306 case MONO_TYPE_ARRAY:
1307 case MONO_TYPE_FNPTR:
1310 case MONO_TYPE_GENERICINST:
1311 /*We need to check if the container class
1312 * of the generic type is a valuetype, iow:
1313 * is it a "class Foo<T>" or a "struct Foo<T>"?
1315 return !arg->type->data.generic_class->container_class->valuetype;
1323 /*Type manipulation helper*/
1325 /*Returns the byref version of the supplied MonoType*/
1327 mono_type_get_type_byref (MonoType *type)
1331 return &mono_class_from_mono_type (type)->this_arg;
1335 /*Returns the byval version of the supplied MonoType*/
1337 mono_type_get_type_byval (MonoType *type)
1341 return &mono_class_from_mono_type (type)->byval_arg;
1345 mono_type_from_stack_slot (ILStackDesc *slot)
1347 if (stack_slot_is_managed_pointer (slot))
1348 return mono_type_get_type_byref (slot->type);
1352 /*Stack manipulation code*/
1355 ensure_stack_size (ILCodeDesc *stack, int required)
1360 if (required < stack->max_size)
1363 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1364 new_size = MAX (8, MAX (required, stack->max_size * 2));
1366 g_assert (new_size >= stack->size);
1367 g_assert (new_size >= required);
1369 tmp = g_new0 (ILStackDesc, new_size);
1370 MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1374 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1375 g_free (stack->stack);
1376 MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1380 stack->max_size = new_size;
1384 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1386 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1388 state->size = state->max_size = 0;
1389 state->flags |= IL_CODE_FLAG_STACK_INITED;
1393 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1395 ensure_stack_size (to, from->size);
1396 to->size = from->size;
1398 /*stack copy happens at merge points, which have small stacks*/
1400 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1404 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1406 to->stype = from->stype;
1407 to->type = from->type;
1408 to->method = from->method;
1412 check_underflow (VerifyContext *ctx, int size)
1414 if (ctx->eval.size < size) {
1415 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1422 check_overflow (VerifyContext *ctx)
1424 if (ctx->eval.size >= ctx->max_stack) {
1425 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1431 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1433 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1435 if (stack_slot_get_type (value) == TYPE_PTR) {
1436 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1442 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1444 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1446 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1447 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1453 static ILStackDesc *
1454 stack_push (VerifyContext *ctx)
1456 g_assert (ctx->eval.size < ctx->max_stack);
1457 g_assert (ctx->eval.size <= ctx->eval.max_size);
1459 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1461 return & ctx->eval.stack [ctx->eval.size++];
1464 static ILStackDesc *
1465 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1467 ILStackDesc *top = stack_push (ctx);
1473 static ILStackDesc *
1474 stack_pop (VerifyContext *ctx)
1477 g_assert (ctx->eval.size > 0);
1478 ret = ctx->eval.stack + --ctx->eval.size;
1479 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1480 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1484 /* This function allows to safely pop an unititialized this ptr from
1485 * the eval stack without marking the method as unverifiable.
1487 static ILStackDesc *
1488 stack_pop_safe (VerifyContext *ctx)
1490 g_assert (ctx->eval.size > 0);
1491 return ctx->eval.stack + --ctx->eval.size;
1494 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1496 stack_peek (VerifyContext *ctx, int distance)
1498 g_assert (ctx->eval.size - distance > 0);
1499 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1502 static ILStackDesc *
1503 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1505 ILStackDesc *top = stack_push (ctx);
1506 copy_stack_value (top, value);
1510 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1512 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1515 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1520 if (!(type = verifier_load_type (ctx, token, opcode)))
1523 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1524 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1528 if (type->type == MONO_TYPE_VOID) {
1529 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1533 if (type->type == MONO_TYPE_TYPEDBYREF)
1534 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1536 if (!(class = mono_class_from_mono_type (type)))
1537 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1539 if (class->generic_container && type->type != MONO_TYPE_GENERICINST)
1540 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));
1542 check_unverifiable_type (ctx, type);
1547 /*operation result tables */
1549 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1550 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1551 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1552 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1553 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1554 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1555 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1558 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1559 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1560 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1561 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1562 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1563 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1564 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1567 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1568 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1569 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1570 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1571 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1572 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1573 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1576 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1577 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1578 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1579 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1580 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1581 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1582 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1585 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1586 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1587 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1588 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1589 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1590 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1591 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1594 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1595 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1596 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1597 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1598 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1599 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1600 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1603 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1604 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1605 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1606 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1607 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1608 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1609 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1612 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1613 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1614 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1615 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1616 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1617 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1618 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1621 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1622 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1623 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1624 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1625 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1626 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1627 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1630 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1631 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1632 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1633 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1634 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1635 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1636 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1639 #ifdef MONO_VERIFIER_DEBUG
1643 dump_stack_value (ILStackDesc *value)
1645 printf ("[(%x)(%x)", value->type->type, value->stype);
1647 if (stack_slot_is_this_pointer (value))
1650 if (stack_slot_is_boxed_value (value))
1651 printf ("[boxed] ");
1653 if (stack_slot_is_null_literal (value))
1656 if (stack_slot_is_managed_mutability_pointer (value))
1657 printf ("Controled Mutability MP: ");
1659 if (stack_slot_is_managed_pointer (value))
1660 printf ("Managed Pointer to: ");
1662 switch (stack_slot_get_underlying_type (value)) {
1664 printf ("invalid type]");
1672 case TYPE_NATIVE_INT:
1673 printf ("native int]");
1676 printf ("float64]");
1679 printf ("unmanaged pointer]");
1682 switch (value->type->type) {
1683 case MONO_TYPE_CLASS:
1684 case MONO_TYPE_VALUETYPE:
1685 printf ("complex] (%s)", value->type->data.klass->name);
1687 case MONO_TYPE_STRING:
1688 printf ("complex] (string)");
1690 case MONO_TYPE_OBJECT:
1691 printf ("complex] (object)");
1693 case MONO_TYPE_SZARRAY:
1694 printf ("complex] (%s [])", value->type->data.klass->name);
1696 case MONO_TYPE_ARRAY:
1697 printf ("complex] (%s [%d %d %d])",
1698 value->type->data.array->eklass->name,
1699 value->type->data.array->rank,
1700 value->type->data.array->numsizes,
1701 value->type->data.array->numlobounds);
1703 case MONO_TYPE_GENERICINST:
1704 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1707 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1709 case MONO_TYPE_MVAR:
1710 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1713 //should be a boxed value
1714 char * name = mono_type_full_name (value->type);
1715 printf ("complex] %s", name);
1721 printf ("unknown stack %x type]\n", value->stype);
1722 g_assert_not_reached ();
1727 dump_stack_state (ILCodeDesc *state)
1731 printf ("(%d) ", state->size);
1732 for (i = 0; i < state->size; ++i)
1733 dump_stack_value (state->stack + i);
1738 /*Returns TRUE if candidate array type can be assigned to target.
1739 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1742 is_array_type_compatible (MonoType *target, MonoType *candidate)
1744 MonoArrayType *left = target->data.array;
1745 MonoArrayType *right = candidate->data.array;
1747 g_assert (target->type == MONO_TYPE_ARRAY);
1748 g_assert (candidate->type == MONO_TYPE_ARRAY);
1750 if (left->rank != right->rank)
1753 return mono_class_is_assignable_from (left->eklass, right->eklass);
1757 get_stack_type (MonoType *type)
1760 int type_kind = type->type;
1762 mask = POINTER_MASK;
1763 /*TODO handle CMMP_MASK */
1766 switch (type_kind) {
1769 case MONO_TYPE_BOOLEAN:
1772 case MONO_TYPE_CHAR:
1775 return TYPE_I4 | mask;
1779 return TYPE_NATIVE_INT | mask;
1781 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1782 case MONO_TYPE_FNPTR:
1784 case MONO_TYPE_TYPEDBYREF:
1785 return TYPE_PTR | mask;
1788 case MONO_TYPE_MVAR:
1790 case MONO_TYPE_CLASS:
1791 case MONO_TYPE_STRING:
1792 case MONO_TYPE_OBJECT:
1793 case MONO_TYPE_SZARRAY:
1794 case MONO_TYPE_ARRAY:
1795 return TYPE_COMPLEX | mask;
1799 return TYPE_I8 | mask;
1803 return TYPE_R8 | mask;
1805 case MONO_TYPE_GENERICINST:
1806 case MONO_TYPE_VALUETYPE:
1807 if (mono_type_is_enum_type (type)) {
1808 type = mono_type_get_underlying_type_any (type);
1811 type_kind = type->type;
1814 return TYPE_COMPLEX | mask;
1822 /* convert MonoType to ILStackDesc format (stype) */
1824 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1827 int type_kind = type->type;
1829 if (type->byref || take_addr)
1830 mask = POINTER_MASK;
1831 /* TODO handle CMMP_MASK */
1836 switch (type_kind) {
1839 case MONO_TYPE_BOOLEAN:
1842 case MONO_TYPE_CHAR:
1845 stack->stype = TYPE_I4 | mask;
1849 stack->stype = TYPE_NATIVE_INT | mask;
1852 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1853 case MONO_TYPE_FNPTR:
1855 case MONO_TYPE_TYPEDBYREF:
1856 stack->stype = TYPE_PTR | mask;
1859 case MONO_TYPE_CLASS:
1860 case MONO_TYPE_STRING:
1861 case MONO_TYPE_OBJECT:
1862 case MONO_TYPE_SZARRAY:
1863 case MONO_TYPE_ARRAY:
1866 case MONO_TYPE_MVAR:
1867 stack->stype = TYPE_COMPLEX | mask;
1872 stack->stype = TYPE_I8 | mask;
1876 stack->stype = TYPE_R8 | mask;
1878 case MONO_TYPE_GENERICINST:
1879 case MONO_TYPE_VALUETYPE:
1880 if (mono_type_is_enum_type (type)) {
1881 MonoType *utype = mono_type_get_underlying_type_any (type);
1883 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
1887 type_kind = type->type;
1890 stack->stype = TYPE_COMPLEX | mask;
1894 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1895 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
1902 * init_stack_with_value_at_exception_boundary:
1904 * Initialize the stack and push a given type.
1905 * The instruction is marked as been on the exception boundary.
1908 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
1911 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
1913 if (!mono_error_ok (&error)) {
1914 char *name = mono_type_get_full_name (klass);
1915 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
1917 mono_error_cleanup (&error);
1921 if (!ctx->max_stack) {
1922 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
1926 stack_init (ctx, code);
1927 ensure_stack_size (code, 1);
1928 set_stack_value (ctx, code->stack, type, FALSE);
1929 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
1931 code->flags |= IL_CODE_FLAG_WAS_TARGET;
1932 if (mono_type_is_generic_argument (type))
1933 code->stack->stype |= BOXED_MASK;
1936 /*Verify if type 'candidate' can be stored in type 'target'.
1938 * If strict, check for the underlying type and not the verification stack types
1941 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1943 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1944 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1946 MonoType *original_candidate = candidate;
1947 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
1949 /*only one is byref */
1950 if (candidate->byref ^ target->byref) {
1951 /* converting from native int to byref*/
1952 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1953 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1958 strict |= target->byref;
1959 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
1960 candidate = mono_type_get_underlying_type_any (candidate);
1963 switch (target->type) {
1964 case MONO_TYPE_VOID:
1965 return candidate->type == MONO_TYPE_VOID;
1968 case MONO_TYPE_BOOLEAN:
1970 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1973 case MONO_TYPE_CHAR:
1975 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1977 case MONO_TYPE_U4: {
1978 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1979 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1981 return is_native_int || is_int4;
1982 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1987 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
1992 return candidate->type == target->type;
1993 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
1997 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1998 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2000 return is_native_int || is_int4;
2001 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2005 if (candidate->type != MONO_TYPE_PTR)
2007 /* check the underlying type */
2008 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2010 case MONO_TYPE_FNPTR: {
2011 MonoMethodSignature *left, *right;
2012 if (candidate->type != MONO_TYPE_FNPTR)
2015 left = mono_type_get_signature (target);
2016 right = mono_type_get_signature (candidate);
2017 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2020 case MONO_TYPE_GENERICINST: {
2021 MonoClass *target_klass;
2022 MonoClass *candidate_klass;
2023 if (mono_type_is_enum_type (target)) {
2024 target = mono_type_get_underlying_type_any (target);
2030 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2031 * to take boxing status into account.
2033 if (mono_type_is_generic_argument (original_candidate))
2036 target_klass = mono_class_from_mono_type (target);
2037 candidate_klass = mono_class_from_mono_type (candidate);
2038 if (mono_class_is_nullable (target_klass)) {
2039 if (!mono_class_is_nullable (candidate_klass))
2041 return target_klass == candidate_klass;
2044 return mono_class_is_assignable_from (target_klass, candidate_klass);
2047 case MONO_TYPE_STRING:
2048 return candidate->type == MONO_TYPE_STRING;
2050 case MONO_TYPE_CLASS:
2052 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2053 * to take boxing status into account.
2055 if (mono_type_is_generic_argument (original_candidate))
2058 if (candidate->type == MONO_TYPE_VALUETYPE)
2061 /* If candidate is an enum it should return true for System.Enum and supertypes.
2062 * That's why here we use the original type and not the underlying type.
2064 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2066 case MONO_TYPE_OBJECT:
2067 return MONO_TYPE_IS_REFERENCE (candidate);
2069 case MONO_TYPE_SZARRAY: {
2072 if (candidate->type != MONO_TYPE_SZARRAY)
2075 left = mono_class_from_mono_type (target);
2076 right = mono_class_from_mono_type (candidate);
2078 return mono_class_is_assignable_from (left, right);
2081 case MONO_TYPE_ARRAY:
2082 if (candidate->type != MONO_TYPE_ARRAY)
2084 return is_array_type_compatible (target, candidate);
2086 case MONO_TYPE_TYPEDBYREF:
2087 return candidate->type == MONO_TYPE_TYPEDBYREF;
2089 case MONO_TYPE_VALUETYPE: {
2090 MonoClass *target_klass;
2091 MonoClass *candidate_klass;
2093 if (candidate->type == MONO_TYPE_CLASS)
2096 target_klass = mono_class_from_mono_type (target);
2097 candidate_klass = mono_class_from_mono_type (candidate);
2098 if (target_klass == candidate_klass)
2100 if (mono_type_is_enum_type (target)) {
2101 target = mono_type_get_underlying_type_any (target);
2110 if (candidate->type != MONO_TYPE_VAR)
2112 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2114 case MONO_TYPE_MVAR:
2115 if (candidate->type != MONO_TYPE_MVAR)
2117 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2120 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2121 g_assert_not_reached ();
2130 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2132 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2136 * Returns the generic param bound to the context been verified.
2139 static MonoGenericParam*
2140 get_generic_param (VerifyContext *ctx, MonoType *param)
2142 guint16 param_num = mono_type_get_generic_param_num (param);
2143 if (param->type == MONO_TYPE_VAR) {
2144 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2145 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2148 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2151 /*param must be a MVAR */
2152 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2153 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2156 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2160 * is_compatible_boxed_valuetype:
2162 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2164 * @type The source type. It it tested to be of the proper type.
2165 * @candidate type of the boxed valuetype.
2166 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2167 * @strict if TRUE candidate must be boxed compatible to the target type
2171 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2173 if (!stack_slot_is_boxed_value (stack))
2175 if (type->byref || candidate->byref)
2178 if (mono_type_is_generic_argument (candidate)) {
2179 MonoGenericParam *param = get_generic_param (ctx, candidate);
2181 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2182 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (& (*class)->byval_arg), FALSE))
2187 if (mono_type_is_generic_argument (type))
2193 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2197 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2199 MonoType *candidate = mono_type_from_stack_slot (stack);
2200 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2203 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2206 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2209 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2213 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2215 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2219 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2221 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2225 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2227 if (candidate->byref ^ target->byref)
2230 switch (target->type) {
2231 case MONO_TYPE_VOID:
2234 case MONO_TYPE_BOOLEAN:
2237 case MONO_TYPE_CHAR:
2246 case MONO_TYPE_STRING:
2247 case MONO_TYPE_TYPEDBYREF:
2248 return candidate->type == target->type;
2251 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2253 case MONO_TYPE_FNPTR:
2254 if (candidate->type != MONO_TYPE_FNPTR)
2256 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2258 case MONO_TYPE_GENERICINST: {
2259 MonoClass *target_klass;
2260 MonoClass *candidate_klass;
2261 target_klass = mono_class_from_mono_type (target);
2262 candidate_klass = mono_class_from_mono_type (candidate);
2263 /*FIXME handle nullables and enum*/
2264 return mono_class_is_assignable_from (target_klass, candidate_klass);
2266 case MONO_TYPE_OBJECT:
2267 return MONO_TYPE_IS_REFERENCE (candidate);
2269 case MONO_TYPE_CLASS:
2270 return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2272 case MONO_TYPE_SZARRAY:
2273 if (candidate->type != MONO_TYPE_SZARRAY)
2275 return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2277 case MONO_TYPE_ARRAY:
2278 if (candidate->type != MONO_TYPE_ARRAY)
2280 return is_array_type_compatible (target, candidate);
2282 case MONO_TYPE_VALUETYPE:
2283 /*FIXME handle nullables and enum*/
2284 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2287 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2290 case MONO_TYPE_MVAR:
2291 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2295 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2296 g_assert_not_reached ();
2302 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2304 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2307 return mono_delegate_type_equal (method, delegate);
2311 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2313 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2316 return mono_delegate_type_equal (delegate, method);
2320 * mono_delegate_signature_equal:
2322 * Compare two signatures in the way expected by delegates.
2324 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2326 * FIXME can this function be eliminated and proper metadata functionality be used?
2329 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2332 int method_offset = is_static_ldftn ? 1 : 0;
2334 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2337 if (delegate_sig->call_convention != method_sig->call_convention)
2340 for (i = 0; i < delegate_sig->param_count; i++) {
2341 MonoType *p1 = delegate_sig->params [i];
2342 MonoType *p2 = method_sig->params [i + method_offset];
2344 if (!mono_delegate_param_equal (p1, p2))
2348 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2355 * verify_ldftn_delegate:
2357 * Verify properties of ldftn based delegates.
2360 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2362 MonoMethod *method = funptr->method;
2364 /*ldftn non-final virtuals only allowed if method is not static,
2365 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2366 * This rules doesn't apply if the object on stack is a boxed valuetype.
2368 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2369 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2370 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2371 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2373 /*current method must not be static*/
2374 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2375 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2377 /*value is the this pointer, loaded using ldarg.0 */
2378 if (!stack_slot_is_this_pointer (value))
2379 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));
2381 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2386 * verify_delegate_compatibility:
2388 * Verify delegate creation sequence.
2392 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2394 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2395 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2397 MonoMethod *invoke, *method;
2398 const guint8 *ip = ctx->header->code;
2399 guint32 ip_offset = ctx->ip_offset;
2400 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2402 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2403 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2407 invoke = mono_get_delegate_invoke (delegate);
2408 method = funptr->method;
2410 if (!method || !mono_method_signature (method)) {
2411 char *name = mono_type_get_full_name (delegate);
2412 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2417 if (!invoke || !mono_method_signature (invoke)) {
2418 char *name = mono_type_get_full_name (delegate);
2419 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2424 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2426 if (is_static_ldftn)
2427 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2429 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2430 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2431 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2432 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));
2434 g_free (invoke_sig);
2438 * Delegate code sequences:
2444 * [-6] ldvirtftn token
2448 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2449 verify_ldftn_delegate (ctx, delegate, value, funptr);
2450 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2451 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2453 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2455 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2458 if (is_first_arg_bound) {
2459 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2460 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2462 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2463 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2464 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2466 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2467 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2471 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2472 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2474 #undef IS_VALID_OPCODE
2475 #undef IS_LOAD_FUN_PTR
2478 /* implement the opcode checks*/
2480 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2484 if (arg >= ctx->max_args) {
2486 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2488 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2489 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2490 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2492 } else if (check_overflow (ctx)) {
2493 /*We must let the value be pushed, otherwise we would get an underflow error*/
2494 check_unverifiable_type (ctx, ctx->params [arg]);
2495 if (ctx->params [arg]->byref && take_addr)
2496 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2497 top = stack_push (ctx);
2498 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2501 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2503 ctx->has_this_store = TRUE;
2505 top->stype |= THIS_POINTER_MASK;
2506 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2507 top->stype |= UNINIT_THIS_MASK;
2513 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2515 if (arg >= ctx->num_locals) {
2516 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2517 } else if (check_overflow (ctx)) {
2518 /*We must let the value be pushed, otherwise we would get an underflow error*/
2519 check_unverifiable_type (ctx, ctx->locals [arg]);
2520 if (ctx->locals [arg]->byref && take_addr)
2521 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2523 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2528 store_arg (VerifyContext *ctx, guint32 arg)
2532 if (arg >= ctx->max_args) {
2533 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2534 if (check_underflow (ctx, 1))
2539 if (check_underflow (ctx, 1)) {
2540 value = stack_pop (ctx);
2541 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2542 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2545 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2546 ctx->has_this_store = 1;
2550 store_local (VerifyContext *ctx, guint32 arg)
2553 if (arg >= ctx->num_locals) {
2554 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2558 /*TODO verify definite assigment */
2559 if (check_underflow (ctx, 1)) {
2560 value = stack_pop(ctx);
2561 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2562 char *expected = mono_type_full_name (ctx->locals [arg]);
2563 char *found = stack_slot_full_name (value);
2564 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2575 /*FIXME add and sub needs special care here*/
2577 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2579 ILStackDesc *a, *b, *top;
2580 int idxa, idxb, complexMerge = 0;
2583 if (!check_underflow (ctx, 2))
2585 b = stack_pop (ctx);
2586 a = stack_pop (ctx);
2588 idxa = stack_slot_get_underlying_type (a);
2589 if (stack_slot_is_managed_pointer (a)) {
2594 idxb = stack_slot_get_underlying_type (b);
2595 if (stack_slot_is_managed_pointer (b)) {
2602 res = table [idxa][idxb];
2604 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2605 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2607 top = stack_push (ctx);
2608 if (res == TYPE_INV) {
2609 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)));
2610 copy_stack_value (top, a);
2614 if (res & NON_VERIFIABLE_RESULT) {
2615 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)));
2617 res = res & ~NON_VERIFIABLE_RESULT;
2620 if (complexMerge && res == TYPE_PTR) {
2621 if (complexMerge == 1)
2622 copy_stack_value (top, a);
2623 else if (complexMerge == 2)
2624 copy_stack_value (top, b);
2626 * There is no need to merge the type of two pointers.
2627 * The only valid operation is subtraction, that returns a native
2628 * int as result and can be used with any 2 pointer kinds.
2629 * This is valid acording to Patition III 1.1.4
2638 do_boolean_branch_op (VerifyContext *ctx, int delta)
2640 int target = ctx->ip_offset + delta;
2643 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2645 if (target < 0 || target >= ctx->code_size) {
2646 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2650 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2652 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2655 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2659 ctx->target = target;
2661 if (!check_underflow (ctx, 1))
2664 top = stack_pop (ctx);
2665 if (!is_valid_bool_arg (top))
2666 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));
2668 check_unmanaged_pointer (ctx, top);
2672 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2674 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2678 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2683 int target = ctx->ip_offset + delta;
2685 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2687 if (target < 0 || target >= ctx->code_size) {
2688 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2692 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2693 case 1: /*FIXME use constants and not magic numbers.*/
2694 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2697 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2701 ctx->target = target;
2703 if (!check_underflow (ctx, 2))
2706 b = stack_pop (ctx);
2707 a = stack_pop (ctx);
2709 idxa = stack_slot_get_underlying_type (a);
2710 if (stack_slot_is_managed_pointer (a))
2713 idxb = stack_slot_get_underlying_type (b);
2714 if (stack_slot_is_managed_pointer (b))
2717 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2722 res = table [idxa][idxb];
2725 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2726 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2728 if (res == TYPE_INV) {
2729 CODE_NOT_VERIFIABLE (ctx,
2730 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));
2731 } else if (res & NON_VERIFIABLE_RESULT) {
2732 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));
2733 res = res & ~NON_VERIFIABLE_RESULT;
2738 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2744 if (!check_underflow (ctx, 2))
2746 b = stack_pop (ctx);
2747 a = stack_pop (ctx);
2749 if (opcode == CEE_CGT_UN) {
2750 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2751 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2756 idxa = stack_slot_get_underlying_type (a);
2757 if (stack_slot_is_managed_pointer (a))
2760 idxb = stack_slot_get_underlying_type (b);
2761 if (stack_slot_is_managed_pointer (b))
2764 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2769 res = table [idxa][idxb];
2772 if(res == TYPE_INV) {
2773 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));
2774 } else if (res & NON_VERIFIABLE_RESULT) {
2775 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));
2776 res = res & ~NON_VERIFIABLE_RESULT;
2778 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2782 do_ret (VerifyContext *ctx)
2784 MonoType *ret = ctx->signature->ret;
2785 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2786 if (ret->type != MONO_TYPE_VOID) {
2788 if (!check_underflow (ctx, 1))
2791 top = stack_pop(ctx);
2793 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2794 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2798 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
2799 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
2802 if (ctx->eval.size > 0) {
2803 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2805 if (in_any_block (ctx->header, ctx->ip_offset))
2806 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2810 * 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.
2811 * This is illegal but mono_get_method_full decoded it.
2812 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2815 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
2818 MonoMethodSignature *sig;
2821 gboolean virt_check_this = FALSE;
2822 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
2824 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
2828 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
2830 if (method->klass->valuetype) // && !constrained ???
2831 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
2833 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
2834 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
2837 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
2838 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
2840 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
2841 virt_check_this = TRUE;
2842 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
2846 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
2847 sig = mono_method_get_signature (method, ctx->image, method_token);
2850 char *name = mono_type_get_full_name (method->klass);
2851 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name, method->name, ctx->ip_offset));
2856 param_count = sig->param_count + sig->hasthis;
2857 if (!check_underflow (ctx, param_count))
2860 for (i = sig->param_count - 1; i >= 0; --i) {
2861 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2862 value = stack_pop (ctx);
2863 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
2864 char *stack_name = stack_slot_full_name (value);
2865 char *sig_name = mono_type_full_name (sig->params [i]);
2866 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));
2867 g_free (stack_name);
2871 if (stack_slot_is_managed_mutability_pointer (value))
2872 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));
2874 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
2875 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));
2881 MonoType *type = &method->klass->byval_arg;
2884 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
2885 if (!mono_method_is_constructor (ctx->method))
2886 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
2887 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
2888 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));
2890 ctx->super_ctor_called = TRUE;
2891 value = stack_pop_safe (ctx);
2892 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
2893 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
2895 value = stack_pop (ctx);
2898 copy_stack_value (©, value);
2899 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2900 //Other parts of the code suffer from the same issue of
2901 copy.type = mono_type_get_type_byval (copy.type);
2902 copy.stype &= ~POINTER_MASK;
2904 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
2905 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));
2907 if (constrained && virtual) {
2908 if (!stack_slot_is_managed_pointer (value))
2909 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
2910 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
2911 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
2912 copy.stype |= BOXED_MASK;
2914 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
2915 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));
2917 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
2918 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
2920 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
2921 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
2923 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
2924 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));
2926 if (!verify_stack_type_compatibility (ctx, type, ©))
2927 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
2929 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
2930 char *name = mono_method_full_name (method, TRUE);
2931 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2935 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
2936 char *name = mono_method_full_name (method, TRUE);
2937 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2941 if (sig->ret->type != MONO_TYPE_VOID) {
2942 if (check_overflow (ctx)) {
2943 value = stack_push (ctx);
2944 set_stack_value (ctx, value, sig->ret, FALSE);
2945 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
2946 ctx->prefix_set &= ~PREFIX_READONLY;
2947 value->stype |= CMMP_MASK;
2952 if ((ctx->prefix_set & PREFIX_TAIL)) {
2953 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
2954 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
2955 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
2956 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
2962 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2964 MonoClassField *field;
2966 if (!check_overflow (ctx))
2969 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
2971 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
2974 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2975 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
2978 /*taking the address of initonly field only works from the static constructor */
2979 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2980 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
2981 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2983 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
2984 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
2986 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
2990 do_store_static_field (VerifyContext *ctx, int token) {
2991 MonoClassField *field;
2994 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
2996 if (!check_underflow (ctx, 1))
2999 value = stack_pop (ctx);
3001 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3004 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3005 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3009 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3010 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3014 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3015 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3017 if (!verify_stack_type_compatibility (ctx, field->type, value))
3018 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in static field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3022 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3024 MonoClassField *field;
3026 gboolean is_pointer;
3028 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3029 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3033 //the value on stack is going to be used as a pointer
3034 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3036 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3037 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3040 g_assert (obj->type);
3042 /*The value on the stack must be a subclass of the defining type of the field*/
3043 /* we need to check if we can load the field from the stack value*/
3045 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3046 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3048 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3049 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3051 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3052 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));
3054 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3055 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3056 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));
3058 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3059 char *found = stack_slot_full_name (obj);
3060 char *expected = mono_type_full_name (&field->parent->byval_arg);
3061 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));
3066 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3067 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3070 check_unmanaged_pointer (ctx, obj);
3075 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3078 MonoClassField *field;
3081 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3083 if (!check_underflow (ctx, 1))
3085 obj = stack_pop_safe (ctx);
3087 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3090 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3091 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3093 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3094 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3095 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3097 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3101 do_store_field (VerifyContext *ctx, int token)
3103 ILStackDesc *value, *obj;
3104 MonoClassField *field;
3105 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3107 if (!check_underflow (ctx, 2))
3110 value = stack_pop (ctx);
3111 obj = stack_pop_safe (ctx);
3113 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3116 if (!verify_stack_type_compatibility (ctx, field->type, value))
3117 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3120 /*TODO proper handle for Nullable<T>*/
3122 do_box_value (VerifyContext *ctx, int klass_token)
3125 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3131 if (!check_underflow (ctx, 1))
3134 value = stack_pop (ctx);
3135 /*box is a nop for reference types*/
3137 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3138 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3143 if (!verify_stack_type_compatibility (ctx, type, value))
3144 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3146 klass = mono_class_from_mono_type (type);
3147 if (mono_class_is_nullable (klass))
3148 type = &mono_class_get_nullable_param (klass)->byval_arg;
3149 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3153 do_unbox_value (VerifyContext *ctx, int klass_token)
3156 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3161 if (!check_underflow (ctx, 1))
3164 if (!mono_class_from_mono_type (type)->valuetype)
3165 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3167 value = stack_pop (ctx);
3169 /*Value should be: a boxed valuetype or a reference type*/
3170 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3171 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3172 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));
3174 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3175 value->stype |= CMMP_MASK;
3179 do_unbox_any (VerifyContext *ctx, int klass_token)
3182 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3187 if (!check_underflow (ctx, 1))
3190 value = stack_pop (ctx);
3192 /*Value should be: a boxed valuetype or a reference type*/
3193 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3194 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3195 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));
3197 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3201 do_unary_math_op (VerifyContext *ctx, int op)
3204 if (!check_underflow (ctx, 1))
3206 value = stack_pop (ctx);
3207 switch (stack_slot_get_type (value)) {
3210 case TYPE_NATIVE_INT:
3215 case TYPE_COMPLEX: /*only enums are ok*/
3216 if (mono_type_is_enum_type (value->type))
3219 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3221 stack_push_stack_val (ctx, value);
3225 do_conversion (VerifyContext *ctx, int kind)
3228 if (!check_underflow (ctx, 1))
3230 value = stack_pop (ctx);
3232 switch (stack_slot_get_type (value)) {
3235 case TYPE_NATIVE_INT:
3239 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));
3244 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3247 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3250 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3252 case TYPE_NATIVE_INT:
3253 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3256 g_error ("unknown type %02x in conversion", kind);
3262 do_load_token (VerifyContext *ctx, int token)
3265 MonoClass *handle_class;
3266 if (!check_overflow (ctx))
3268 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3270 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3273 if (handle_class == mono_defaults.typehandle_class) {
3274 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3275 } else if (handle_class == mono_defaults.methodhandle_class) {
3276 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3277 } else if (handle_class == mono_defaults.fieldhandle_class) {
3278 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3280 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3282 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3286 do_ldobj_value (VerifyContext *ctx, int token)
3289 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3290 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3295 if (!check_underflow (ctx, 1))
3298 value = stack_pop (ctx);
3299 if (!stack_slot_is_managed_pointer (value)
3300 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3301 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3302 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3306 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3307 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3309 /*We have a byval on the stack, but the comparison must be strict. */
3310 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3311 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3313 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3317 do_stobj (VerifyContext *ctx, int token)
3319 ILStackDesc *dest, *src;
3320 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3321 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3326 if (!check_underflow (ctx, 2))
3329 src = stack_pop (ctx);
3330 dest = stack_pop (ctx);
3332 if (stack_slot_is_managed_mutability_pointer (dest))
3333 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3335 if (!stack_slot_is_managed_pointer (dest))
3336 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3338 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3339 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));
3341 if (!verify_stack_type_compatibility (ctx, type, src))
3342 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
3344 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3345 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3349 do_cpobj (VerifyContext *ctx, int token)
3351 ILStackDesc *dest, *src;
3352 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3356 if (!check_underflow (ctx, 2))
3359 src = stack_pop (ctx);
3360 dest = stack_pop (ctx);
3362 if (!stack_slot_is_managed_pointer (src))
3363 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3365 if (!stack_slot_is_managed_pointer (dest))
3366 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3368 if (stack_slot_is_managed_mutability_pointer (dest))
3369 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3371 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3372 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3374 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3375 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3379 do_initobj (VerifyContext *ctx, int token)
3382 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3386 if (!check_underflow (ctx, 1))
3389 obj = stack_pop (ctx);
3391 if (!stack_slot_is_managed_pointer (obj))
3392 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3394 if (stack_slot_is_managed_mutability_pointer (obj))
3395 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3397 stack = mono_type_get_type_byval (obj->type);
3398 if (MONO_TYPE_IS_REFERENCE (stack)) {
3399 if (!verify_type_compatibility (ctx, stack, type))
3400 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3401 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3402 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3403 } else if (!verify_type_compatibility (ctx, stack, type)) {
3404 char *expected_name = mono_type_full_name (type);
3405 char *stack_name = mono_type_full_name (stack);
3407 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));
3408 g_free (expected_name);
3409 g_free (stack_name);
3414 do_newobj (VerifyContext *ctx, int token)
3418 MonoMethodSignature *sig;
3420 gboolean is_delegate = FALSE;
3422 if (!(method = verifier_load_method (ctx, token, "newobj")))
3425 if (!mono_method_is_constructor (method)) {
3426 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3430 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3431 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3433 if (!mono_method_can_access_method_full (ctx->method, method, NULL)) {
3434 char *from = mono_method_full_name (ctx->method, TRUE);
3435 char *to = mono_method_full_name (method, TRUE);
3436 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);
3441 //FIXME use mono_method_get_signature_full
3442 sig = mono_method_signature (method);
3444 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3448 if (!check_underflow (ctx, sig->param_count))
3451 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3454 ILStackDesc *funptr;
3455 //first arg is object, second arg is fun ptr
3456 if (sig->param_count != 2) {
3457 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3460 funptr = stack_pop (ctx);
3461 value = stack_pop (ctx);
3462 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3464 for (i = sig->param_count - 1; i >= 0; --i) {
3465 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3466 value = stack_pop (ctx);
3467 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3468 char *stack_name = stack_slot_full_name (value);
3469 char *sig_name = mono_type_full_name (sig->params [i]);
3470 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));
3471 g_free (stack_name);
3475 if (stack_slot_is_managed_mutability_pointer (value))
3476 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3480 if (check_overflow (ctx))
3481 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3485 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3491 if (!check_underflow (ctx, 1))
3494 if (!(type = verifier_load_type (ctx, token, opcode)))
3498 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3502 value = stack_pop (ctx);
3503 is_boxed = stack_slot_is_boxed_value (value);
3505 if (stack_slot_is_managed_pointer (value))
3506 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3507 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3508 char *name = stack_slot_full_name (value);
3509 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));
3513 switch (value->type->type) {
3514 case MONO_TYPE_FNPTR:
3516 case MONO_TYPE_TYPEDBYREF:
3517 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3520 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3521 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3525 mono_type_from_opcode (int opcode) {
3533 return &mono_defaults.sbyte_class->byval_arg;
3541 return &mono_defaults.int16_class->byval_arg;
3549 return &mono_defaults.int32_class->byval_arg;
3555 return &mono_defaults.int64_class->byval_arg;
3561 return &mono_defaults.single_class->byval_arg;
3567 return &mono_defaults.double_class->byval_arg;
3573 return &mono_defaults.int_class->byval_arg;
3577 case CEE_LDELEM_REF:
3578 case CEE_STELEM_REF:
3579 return &mono_defaults.object_class->byval_arg;
3582 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3588 do_load_indirect (VerifyContext *ctx, int opcode)
3591 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3593 if (!check_underflow (ctx, 1))
3596 value = stack_pop (ctx);
3597 if (!stack_slot_is_managed_pointer (value)) {
3598 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3599 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3603 if (opcode == CEE_LDIND_REF) {
3604 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3605 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3606 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3608 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3609 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3610 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3615 do_store_indirect (VerifyContext *ctx, int opcode)
3617 ILStackDesc *addr, *val;
3618 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3620 if (!check_underflow (ctx, 2))
3623 val = stack_pop (ctx);
3624 addr = stack_pop (ctx);
3626 check_unmanaged_pointer (ctx, addr);
3628 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3629 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3633 if (stack_slot_is_managed_mutability_pointer (addr)) {
3634 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3638 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3639 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3641 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3642 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3646 do_newarr (VerifyContext *ctx, int token)
3649 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3654 if (!check_underflow (ctx, 1))
3657 value = stack_pop (ctx);
3658 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3659 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));
3661 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3664 /*FIXME handle arrays that are not 0-indexed*/
3666 do_ldlen (VerifyContext *ctx)
3670 if (!check_underflow (ctx, 1))
3673 value = stack_pop (ctx);
3675 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3676 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3678 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3681 /*FIXME handle arrays that are not 0-indexed*/
3682 /*FIXME handle readonly prefix and CMMP*/
3684 do_ldelema (VerifyContext *ctx, int klass_token)
3686 ILStackDesc *index, *array, *res;
3687 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
3693 if (!check_underflow (ctx, 2))
3696 index = stack_pop (ctx);
3697 array = stack_pop (ctx);
3699 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3700 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));
3702 if (!stack_slot_is_null_literal (array)) {
3703 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3704 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3706 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3707 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3709 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3712 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3716 res = stack_push (ctx);
3717 set_stack_value (ctx, res, type, TRUE);
3718 if (ctx->prefix_set & PREFIX_READONLY) {
3719 ctx->prefix_set &= ~PREFIX_READONLY;
3720 res->stype |= CMMP_MASK;
3725 * FIXME handle arrays that are not 0-indexed
3726 * FIXME handle readonly prefix and CMMP
3729 do_ldelem (VerifyContext *ctx, int opcode, int token)
3731 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3732 ILStackDesc *index, *array;
3734 if (!check_underflow (ctx, 2))
3737 if (opcode == CEE_LDELEM) {
3738 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
3739 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3743 type = mono_type_from_opcode (opcode);
3746 index = stack_pop (ctx);
3747 array = stack_pop (ctx);
3749 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3750 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));
3752 if (!stack_slot_is_null_literal (array)) {
3753 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3754 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));
3756 if (opcode == CEE_LDELEM_REF) {
3757 if (array->type->data.klass->valuetype)
3758 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3759 type = &array->type->data.klass->byval_arg;
3761 MonoType *candidate = &array->type->data.klass->byval_arg;
3762 if (IS_STRICT_MODE (ctx)) {
3763 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
3764 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
3765 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)) ||
3766 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
3767 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3769 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
3770 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3775 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3780 * FIXME handle arrays that are not 0-indexed
3783 do_stelem (VerifyContext *ctx, int opcode, int token)
3785 ILStackDesc *index, *array, *value;
3787 if (!check_underflow (ctx, 3))
3790 if (opcode == CEE_STELEM) {
3791 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
3792 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3796 type = mono_type_from_opcode (opcode);
3799 value = stack_pop (ctx);
3800 index = stack_pop (ctx);
3801 array = stack_pop (ctx);
3803 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3804 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));
3806 if (!stack_slot_is_null_literal (array)) {
3807 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3808 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));
3810 if (opcode == CEE_STELEM_REF) {
3811 if (array->type->data.klass->valuetype)
3812 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3813 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3814 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3818 if (opcode == CEE_STELEM_REF) {
3819 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3820 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3821 } else if (opcode != CEE_STELEM_REF) {
3822 if (!verify_stack_type_compatibility (ctx, type, value))
3823 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3825 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
3826 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));
3832 do_throw (VerifyContext *ctx)
3834 ILStackDesc *exception;
3835 if (!check_underflow (ctx, 1))
3837 exception = stack_pop (ctx);
3839 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3840 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3842 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
3843 char *name = mono_type_full_name (exception->type);
3844 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));
3847 /*The stack is left empty after a throw*/
3853 do_endfilter (VerifyContext *ctx)
3855 MonoExceptionClause *clause;
3857 if (IS_STRICT_MODE (ctx)) {
3858 if (ctx->eval.size != 1)
3859 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3861 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
3862 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3865 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3866 if (IS_STRICT_MODE (ctx)) {
3867 if (ctx->ip_offset != clause->handler_offset - 2)
3868 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3870 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3871 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3874 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3875 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3877 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3884 do_leave (VerifyContext *ctx, int delta)
3886 int target = ((gint32)ctx->ip_offset) + delta;
3887 if (target >= ctx->code_size || target < 0)
3888 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3890 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
3891 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
3898 * Verify br and br.s opcodes.
3901 do_static_branch (VerifyContext *ctx, int delta)
3903 int target = ctx->ip_offset + delta;
3904 if (target < 0 || target >= ctx->code_size) {
3905 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
3909 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3911 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3914 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3918 ctx->target = target;
3922 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
3924 int i, base = ctx->ip_offset + 5 + count * 4;
3927 if (!check_underflow (ctx, 1))
3930 value = stack_pop (ctx);
3932 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3933 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
3935 for (i = 0; i < count; ++i) {
3936 int target = base + read32 (data + i * 4);
3938 if (target < 0 || target >= ctx->code_size) {
3939 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
3943 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3945 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3948 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3951 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
3956 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
3961 if (virtual && !check_underflow (ctx, 1))
3964 if (!virtual && !check_overflow (ctx))
3967 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
3968 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3972 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
3975 if (mono_method_is_constructor (method))
3976 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
3979 ILStackDesc *top = stack_pop (ctx);
3981 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
3982 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
3984 if (method->flags & METHOD_ATTRIBUTE_STATIC)
3985 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
3987 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
3988 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
3991 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
3992 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);
3994 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
3995 top->method = method;
3999 do_sizeof (VerifyContext *ctx, int token)
4003 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4004 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4008 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4011 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4012 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4016 if (type->type == MONO_TYPE_VOID) {
4017 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4021 if (check_overflow (ctx))
4022 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4025 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4027 do_localloc (VerifyContext *ctx)
4031 if (ctx->eval.size != 1) {
4032 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4036 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4037 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4041 /*TODO verify top type*/
4042 top = stack_pop (ctx);
4044 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4045 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4049 do_ldstr (VerifyContext *ctx, guint32 token)
4051 GSList *error = NULL;
4052 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4053 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4057 if (!ctx->image->dynamic && !mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4059 ctx->list = g_slist_concat (ctx->list, error);
4060 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4064 if (check_overflow (ctx))
4065 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4069 do_refanyval (VerifyContext *ctx, int token)
4073 if (!check_underflow (ctx, 1))
4076 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4079 top = stack_pop (ctx);
4081 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4082 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));
4084 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4088 do_refanytype (VerifyContext *ctx)
4092 if (!check_underflow (ctx, 1))
4095 top = stack_pop (ctx);
4097 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4098 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));
4100 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4105 do_mkrefany (VerifyContext *ctx, int token)
4109 if (!check_underflow (ctx, 1))
4112 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4115 top = stack_pop (ctx);
4117 if (stack_slot_is_managed_mutability_pointer (top))
4118 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4120 if (!stack_slot_is_managed_pointer (top)) {
4121 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));
4123 MonoType *stack_type = mono_type_get_type_byval (top->type);
4124 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4125 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4127 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4128 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4131 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4135 do_ckfinite (VerifyContext *ctx)
4138 if (!check_underflow (ctx, 1))
4141 top = stack_pop (ctx);
4143 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4144 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));
4145 stack_push_stack_val (ctx, top);
4149 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4151 * @from holds new values for a given control path
4152 * @to holds the current values of a given control path
4154 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4157 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4161 stack_init (ctx, to);
4164 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4167 stack_copy (&ctx->eval, to);
4169 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4170 stack_copy (to, &ctx->eval);
4173 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4175 if (from->size != to->size) {
4176 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4177 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));
4181 //FIXME we need to preserve CMMP attributes
4182 //FIXME we must take null literals into consideration.
4183 for (i = 0; i < from->size; ++i) {
4184 ILStackDesc *new_slot = from->stack + i;
4185 ILStackDesc *old_slot = to->stack + i;
4186 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4187 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4188 MonoClass *old_class = mono_class_from_mono_type (old_type);
4189 MonoClass *new_class = mono_class_from_mono_type (new_type);
4190 MonoClass *match_class = NULL;
4192 // S := T then U = S (new value is compatible with current value, keep current)
4193 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4194 copy_stack_value (new_slot, old_slot);
4198 // T := S then U = T (old value is compatible with current value, use new)
4199 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4200 copy_stack_value (old_slot, new_slot);
4204 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4205 char *old_name = stack_slot_full_name (old_slot);
4206 char *new_name = stack_slot_full_name (new_slot);
4207 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));
4213 //both are reference types, use closest common super type
4214 if (!mono_class_from_mono_type (old_type)->valuetype
4215 && !mono_class_from_mono_type (new_type)->valuetype
4216 && !stack_slot_is_managed_pointer (old_slot)
4217 && !stack_slot_is_managed_pointer (new_slot)) {
4219 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4220 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4221 match_class = old_class->supertypes [j];
4226 mono_class_setup_interfaces (old_class, &error);
4227 if (!mono_error_ok (&error)) {
4228 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));
4229 mono_error_cleanup (&error);
4232 for (j = 0; j < old_class->interface_count; ++j) {
4233 for (k = 0; k < new_class->interface_count; ++k) {
4234 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4235 match_class = old_class->interfaces [j];
4241 //No decent super type found, use object
4242 match_class = mono_defaults.object_class;
4244 } 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)) {
4245 match_class = mono_defaults.object_class;
4250 char *old_name = stack_slot_full_name (old_slot);
4251 char *new_name = stack_slot_full_name (new_slot);
4252 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));
4256 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4260 g_assert (match_class);
4261 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4262 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4268 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4269 to->flags |= IL_CODE_STACK_MERGED;
4272 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4273 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4276 * is_clause_in_range :
4278 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4281 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4283 if (clause->try_offset >= start && clause->try_offset < end)
4285 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4291 * is_clause_inside_range :
4293 * Returns TRUE if @clause lies completely inside the @start - @end range.
4296 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4298 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4300 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4306 * is_clause_nested :
4308 * Returns TRUE if @nested is nested in @clause.
4311 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4313 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4315 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4316 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4319 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4320 * the each pair of exception must have the following properties:
4321 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4322 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4323 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4326 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4328 /*clause is nested*/
4329 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4330 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4334 /*wrong nesting order.*/
4335 if (is_clause_nested (clause, to_test)) {
4336 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4340 /*mutual protection*/
4341 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4342 /*handlers are not disjoint*/
4343 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4344 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4347 /* handlers are not catch or filter */
4348 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4349 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4356 /*not completelly disjoint*/
4357 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4358 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4359 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4362 #define code_bounds_check(size) \
4363 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4364 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4369 mono_opcode_is_prefix (int op)
4372 case MONO_CEE_UNALIGNED_:
4373 case MONO_CEE_VOLATILE_:
4374 case MONO_CEE_TAIL_:
4375 case MONO_CEE_CONSTRAINED_:
4376 case MONO_CEE_READONLY_:
4383 * FIXME: need to distinguish between valid and verifiable.
4384 * Need to keep track of types on the stack.
4385 * Verify types for opcodes.
4388 mono_method_verify (MonoMethod *method, int level)
4391 const unsigned char *ip, *code_start;
4392 const unsigned char *end;
4393 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4395 int i, n, need_merge = 0, start = 0;
4396 guint token, ip_offset = 0, prefix = 0;
4397 MonoGenericContext *generic_context = NULL;
4401 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4403 init_verifier_stats ();
4405 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4406 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4410 memset (&ctx, 0, sizeof (VerifyContext));
4412 //FIXME use mono_method_get_signature_full
4413 ctx.signature = mono_method_signature (method);
4414 if (!ctx.signature) {
4415 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4417 finish_collect_stats ();
4420 ctx.header = mono_method_get_header (method);
4422 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4423 finish_collect_stats ();
4426 ctx.method = method;
4427 code_start = ip = ctx.header->code;
4428 end = ip + ctx.header->code_size;
4429 ctx.image = image = method->klass->image;
4432 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4433 ctx.max_stack = ctx.header->max_stack;
4434 ctx.verifiable = ctx.valid = 1;
4437 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4438 ctx.code_size = ctx.header->code_size;
4439 MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4441 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4443 ctx.num_locals = ctx.header->num_locals;
4444 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4445 MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4447 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4448 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4450 ctx.params = g_new (MonoType*, ctx.max_args);
4451 MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4453 if (ctx.signature->hasthis)
4454 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4455 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4457 if (ctx.signature->is_inflated)
4458 ctx.generic_context = generic_context = mono_method_get_context (method);
4460 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4461 if (method->is_generic)
4462 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4464 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4467 for (i = 0; i < ctx.num_locals; ++i) {
4468 MonoType *uninflated = ctx.locals [i];
4469 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4470 if (!mono_error_ok (&error)) {
4471 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4472 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4474 mono_error_cleanup (&error);
4475 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4481 for (i = 0; i < ctx.max_args; ++i) {
4482 MonoType *uninflated = ctx.params [i];
4483 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4484 if (!mono_error_ok (&error)) {
4485 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4486 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4488 mono_error_cleanup (&error);
4489 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4494 stack_init (&ctx, &ctx.eval);
4496 for (i = 0; i < ctx.num_locals; ++i) {
4497 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4499 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4500 char *name = mono_type_full_name (ctx.locals [i]);
4501 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4508 for (i = 0; i < ctx.max_args; ++i) {
4509 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4512 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4513 char *name = mono_type_full_name (ctx.params [i]);
4514 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4523 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4524 MonoExceptionClause *clause = ctx.header->clauses + i;
4525 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); );
4527 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4528 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4530 if (clause->try_len <= 0)
4531 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4533 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4534 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4536 if (clause->handler_len <= 0)
4537 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4539 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4540 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4542 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4543 if (clause->data.filter_offset > ctx.code_size)
4544 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4546 if (clause->data.filter_offset >= clause->handler_offset)
4547 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4550 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4551 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4556 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4557 if (clause->try_offset + clause->try_len < ctx.code_size)
4558 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4559 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4560 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4562 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4563 if (!clause->data.catch_class) {
4564 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
4568 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4570 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4571 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4572 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4576 original_bb = bb = mono_basic_block_split (method, &error);
4577 if (!mono_error_ok (&error)) {
4578 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
4579 mono_error_cleanup (&error);
4584 while (ip < end && ctx.valid) {
4585 ip_offset = ip - code_start;
4587 const unsigned char *ip_copy = ip;
4590 if (ip_offset > bb->end) {
4591 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
4595 if (ip_offset == bb->end)
4598 size = mono_opcode_value_and_size (&ip_copy, end, &op);
4600 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
4604 if (ADD_IS_GREATER_OR_OVF (ip_offset, size, bb->end)) {
4605 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
4609 /*Last Instruction*/
4610 if (ip_offset + size == bb->end && mono_opcode_is_prefix (op)) {
4611 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));
4616 /*FIXME remove this once we move all bad branch checking code to use BB only*/
4617 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
4623 ctx.ip_offset = ip_offset = ip - code_start;
4625 /*We need to check against fallthrou in and out of protected blocks.
4626 * For fallout we check the once a protected block ends, if the start flag is not set.
4627 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4628 * TODO convert these checks to be done using flags and not this loop
4630 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4631 MonoExceptionClause *clause = ctx.header->clauses + i;
4633 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4634 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4638 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4639 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4640 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4642 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4646 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4647 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4651 if (clause->handler_offset == ip_offset && start == 0) {
4652 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4656 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
4657 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4666 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4667 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4670 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4673 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4674 #ifdef MONO_VERIFIER_DEBUG
4677 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4678 discode [strlen (discode) - 1] = 0; /* no \n */
4679 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4682 dump_stack_state (&ctx.code [ip_offset]);
4683 dump_stack_state (&ctx.eval);
4696 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4702 code_bounds_check (2);
4703 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4707 case CEE_ADD_OVF_UN:
4708 do_binop (&ctx, *ip, add_ovf_un_table);
4712 case CEE_SUB_OVF_UN:
4713 do_binop (&ctx, *ip, sub_ovf_un_table);
4720 case CEE_MUL_OVF_UN:
4721 do_binop (&ctx, *ip, bin_ovf_table);
4726 do_binop (&ctx, *ip, add_table);
4731 do_binop (&ctx, *ip, sub_table);
4738 do_binop (&ctx, *ip, bin_op_table);
4747 do_binop (&ctx, *ip, int_bin_op_table);
4754 do_binop (&ctx, *ip, shift_op_table);
4759 if (!check_underflow (&ctx, 1))
4761 stack_pop_safe (&ctx);
4775 /*TODO support definite assignment verification? */
4776 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
4784 store_local (&ctx, *ip - CEE_STLOC_0);
4789 code_bounds_check (2);
4790 store_local (&ctx, ip [1]);
4795 code_bounds_check (2);
4796 store_arg (&ctx, ip [1]);
4810 if (check_overflow (&ctx))
4811 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4816 code_bounds_check (2);
4817 if (check_overflow (&ctx))
4818 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4823 code_bounds_check (5);
4824 if (check_overflow (&ctx))
4825 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4830 code_bounds_check (9);
4831 if (check_overflow (&ctx))
4832 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4837 code_bounds_check (5);
4838 if (check_overflow (&ctx))
4839 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4844 code_bounds_check (9);
4845 if (check_overflow (&ctx))
4846 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4851 if (check_overflow (&ctx))
4852 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4858 code_bounds_check (2);
4859 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4872 code_bounds_check (2);
4873 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
4880 code_bounds_check (5);
4881 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
4894 code_bounds_check (5);
4895 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
4902 code_bounds_check (2);
4903 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
4908 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
4914 if (!check_underflow (&ctx, 1))
4916 if (!check_overflow (&ctx))
4918 top = stack_push (&ctx);
4919 copy_stack_value (top, stack_peek (&ctx, 1));
4925 code_bounds_check (5);
4927 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
4928 token = read32 (ip + 1);
4929 if (in_any_block (ctx.header, ip_offset))
4930 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
4932 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
4934 * FIXME: check signature, retval, arguments etc.
4940 code_bounds_check (5);
4941 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
4946 code_bounds_check (5);
4947 token = read32 (ip + 1);
4949 * FIXME: check signature, retval, arguments etc.
4950 * FIXME: check requirements for tail call
4952 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
4956 code_bounds_check (2);
4957 do_static_branch (&ctx, (signed char)ip [1] + 2);
4965 code_bounds_check (2);
4966 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
4972 code_bounds_check (5);
4973 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
4981 code_bounds_check (5);
4982 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
4989 code_bounds_check (5);
4990 entries = read32 (ip + 1);
4992 if (entries > 0xFFFFFFFFU / sizeof (guint32))
4993 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
4996 code_bounds_check (sizeof (guint32) * entries);
4998 do_switch (&ctx, entries, ip);
4999 ip += sizeof (guint32) * entries;
5013 do_load_indirect (&ctx, *ip);
5025 do_store_indirect (&ctx, *ip);
5031 do_unary_math_op (&ctx, *ip);
5041 do_conversion (&ctx, TYPE_I4);
5047 do_conversion (&ctx, TYPE_I8);
5054 do_conversion (&ctx, TYPE_R8);
5060 do_conversion (&ctx, TYPE_NATIVE_INT);
5065 code_bounds_check (5);
5066 do_cpobj (&ctx, read32 (ip + 1));
5071 code_bounds_check (5);
5072 do_ldobj_value (&ctx, read32 (ip + 1));
5077 code_bounds_check (5);
5078 do_ldstr (&ctx, read32 (ip + 1));
5083 code_bounds_check (5);
5084 do_newobj (&ctx, read32 (ip + 1));
5090 code_bounds_check (5);
5091 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5097 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5102 code_bounds_check (5);
5103 do_unbox_value (&ctx, read32 (ip + 1));
5115 code_bounds_check (5);
5116 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5122 code_bounds_check (5);
5123 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5128 code_bounds_check (5);
5129 do_store_field (&ctx, read32 (ip + 1));
5134 code_bounds_check (5);
5135 do_store_static_field (&ctx, read32 (ip + 1));
5140 code_bounds_check (5);
5141 do_stobj (&ctx, read32 (ip + 1));
5145 case CEE_CONV_OVF_I1_UN:
5146 case CEE_CONV_OVF_I2_UN:
5147 case CEE_CONV_OVF_I4_UN:
5148 case CEE_CONV_OVF_U1_UN:
5149 case CEE_CONV_OVF_U2_UN:
5150 case CEE_CONV_OVF_U4_UN:
5151 do_conversion (&ctx, TYPE_I4);
5155 case CEE_CONV_OVF_I8_UN:
5156 case CEE_CONV_OVF_U8_UN:
5157 do_conversion (&ctx, TYPE_I8);
5161 case CEE_CONV_OVF_I_UN:
5162 case CEE_CONV_OVF_U_UN:
5163 do_conversion (&ctx, TYPE_NATIVE_INT);
5168 code_bounds_check (5);
5169 do_box_value (&ctx, read32 (ip + 1));
5174 code_bounds_check (5);
5175 do_newarr (&ctx, read32 (ip + 1));
5185 code_bounds_check (5);
5186 do_ldelema (&ctx, read32 (ip + 1));
5200 case CEE_LDELEM_REF:
5201 do_ldelem (&ctx, *ip, 0);
5212 case CEE_STELEM_REF:
5213 do_stelem (&ctx, *ip, 0);
5218 code_bounds_check (5);
5219 do_ldelem (&ctx, *ip, read32 (ip + 1));
5224 code_bounds_check (5);
5225 do_stelem (&ctx, *ip, read32 (ip + 1));
5230 code_bounds_check (5);
5231 do_unbox_any (&ctx, read32 (ip + 1));
5235 case CEE_CONV_OVF_I1:
5236 case CEE_CONV_OVF_U1:
5237 case CEE_CONV_OVF_I2:
5238 case CEE_CONV_OVF_U2:
5239 case CEE_CONV_OVF_I4:
5240 case CEE_CONV_OVF_U4:
5241 do_conversion (&ctx, TYPE_I4);
5245 case CEE_CONV_OVF_I8:
5246 case CEE_CONV_OVF_U8:
5247 do_conversion (&ctx, TYPE_I8);
5251 case CEE_CONV_OVF_I:
5252 case CEE_CONV_OVF_U:
5253 do_conversion (&ctx, TYPE_NATIVE_INT);
5258 code_bounds_check (5);
5259 do_refanyval (&ctx, read32 (ip + 1));
5269 code_bounds_check (5);
5270 do_mkrefany (&ctx, read32 (ip + 1));
5275 code_bounds_check (5);
5276 do_load_token (&ctx, read32 (ip + 1));
5280 case CEE_ENDFINALLY:
5281 if (!is_correct_endfinally (ctx.header, ip_offset))
5282 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5289 code_bounds_check (5);
5290 do_leave (&ctx, read32 (ip + 1) + 5);
5296 code_bounds_check (2);
5297 do_leave (&ctx, (signed char)ip [1] + 2);
5303 code_bounds_check (2);
5307 code_bounds_check (3);
5308 store_local (&ctx, read16 (ip + 1));
5313 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5321 do_cmp_op (&ctx, cmp_br_op, *ip);
5326 code_bounds_check (3);
5327 store_arg (&ctx, read16 (ip + 1) );
5333 check_overflow (&ctx);
5334 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5335 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5336 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5341 code_bounds_check (5);
5342 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5347 code_bounds_check (5);
5348 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5354 code_bounds_check (3);
5355 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5361 code_bounds_check (3);
5362 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5375 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5379 do_endfilter (&ctx);
5383 case CEE_UNALIGNED_:
5384 code_bounds_check (2);
5385 prefix |= PREFIX_UNALIGNED;
5389 prefix |= PREFIX_VOLATILE;
5393 prefix |= PREFIX_TAIL;
5395 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5396 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5400 code_bounds_check (5);
5401 do_initobj (&ctx, read32 (ip + 1));
5405 case CEE_CONSTRAINED_:
5406 code_bounds_check (5);
5407 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5408 prefix |= PREFIX_CONSTRAINED;
5413 prefix |= PREFIX_READONLY;
5418 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5419 if (!check_underflow (&ctx, 3))
5421 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5426 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5427 if (!check_underflow (&ctx, 3))
5429 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5437 if (!is_correct_rethrow (ctx.header, ip_offset))
5438 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5445 code_bounds_check (5);
5446 do_sizeof (&ctx, read32 (ip + 1));
5450 case CEE_REFANYTYPE:
5451 do_refanytype (&ctx);
5456 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5462 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5466 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5468 if (!ctx.prefix_set) //first prefix
5469 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5470 ctx.prefix_set |= prefix;
5471 ctx.has_flags = TRUE;
5475 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5477 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5478 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5479 if (ctx.prefix_set & PREFIX_READONLY)
5480 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5481 if (ctx.prefix_set & PREFIX_VOLATILE)
5482 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5483 if (ctx.prefix_set & PREFIX_UNALIGNED)
5484 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5485 ctx.prefix_set = prefix = 0;
5486 ctx.has_flags = FALSE;
5490 * if ip != end we overflowed: mark as error.
5492 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5493 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5496 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5497 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5498 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5499 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5500 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5502 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5503 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5505 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5506 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5508 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5509 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));
5512 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5513 char *method_name = mono_method_full_name (ctx.method, TRUE);
5514 char *type = mono_type_get_full_name (ctx.method->klass);
5515 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5516 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));
5518 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5519 g_free (method_name);
5525 for (i = 0; i < ctx.header->code_size; ++i) {
5526 if (ctx.code [i].stack)
5527 g_free (ctx.code [i].stack);
5531 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5533 g_slist_free (ctx.funptrs);
5535 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5536 mono_metadata_free_type (tmp->data);
5537 g_slist_free (ctx.exception_types);
5539 for (i = 0; i < ctx.num_locals; ++i) {
5541 mono_metadata_free_type (ctx.locals [i]);
5543 for (i = 0; i < ctx.max_args; ++i) {
5545 mono_metadata_free_type (ctx.params [i]);
5549 g_free (ctx.eval.stack);
5552 g_free (ctx.locals);
5553 g_free (ctx.params);
5554 mono_basic_block_free (original_bb);
5555 mono_metadata_free_mh (ctx.header);
5557 finish_collect_stats ();
5562 mono_verify_corlib ()
5564 /* This is a public API function so cannot be removed */
5569 * Returns true if @method needs to be verified.
5573 mono_verifier_is_enabled_for_method (MonoMethod *method)
5575 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5579 * Returns true if @klass need to be verified.
5583 mono_verifier_is_enabled_for_class (MonoClass *klass)
5585 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
5589 mono_verifier_is_enabled_for_image (MonoImage *image)
5591 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
5595 mono_verifier_is_method_full_trust (MonoMethod *method)
5597 return mono_verifier_is_class_full_trust (method->klass);
5601 * Returns if @klass is under full trust or not.
5603 * TODO This code doesn't take CAS into account.
5605 * Under verify_all all user code must be verifiable if no security option was set
5609 mono_verifier_is_class_full_trust (MonoClass *klass)
5611 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5612 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
5613 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
5615 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5616 return trusted_location || klass->image == mono_defaults.corlib;
5617 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
5621 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5623 return mono_method_verify (method,
5624 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5625 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5626 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5630 get_field_end (MonoClassField *field)
5633 int size = mono_type_size (field->type, &align);
5635 size = 4; /*FIXME Is this a safe bet?*/
5636 return size + field->offset;
5640 verify_class_for_overlapping_reference_fields (MonoClass *class)
5643 gpointer iter = NULL;
5644 MonoClassField *field;
5645 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5646 /*We can't skip types with !has_references since this is calculated after we have run.*/
5647 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
5651 /*We must check for stuff overlapping reference fields.
5652 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
5654 while ((field = mono_class_get_fields (class, &iter))) {
5655 int fieldEnd = get_field_end (field);
5656 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5659 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5662 for (j = i; j < class->field.count; ++j) {
5663 MonoClassField *other = &class->fields [j];
5664 int otherEnd = get_field_end (other);
5665 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5668 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5671 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5679 field_hash (gconstpointer key)
5681 const MonoClassField *field = key;
5682 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
5686 field_equals (gconstpointer _a, gconstpointer _b)
5688 const MonoClassField *a = _a;
5689 const MonoClassField *b = _b;
5690 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
5695 verify_class_fields (MonoClass *class)
5697 gpointer iter = NULL;
5698 MonoClassField *field;
5699 MonoGenericContext *context = mono_class_get_context (class);
5700 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
5701 if (class->generic_container)
5702 context = &class->generic_container->context;
5704 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5705 if (!mono_type_is_valid_type_in_context (field->type, context)) {
5706 g_hash_table_destroy (unique_fields);
5709 if (g_hash_table_lookup (unique_fields, field)) {
5710 g_hash_table_destroy (unique_fields);
5713 g_hash_table_insert (unique_fields, field, field);
5715 g_hash_table_destroy (unique_fields);
5720 verify_interfaces (MonoClass *class)
5723 for (i = 0; i < class->interface_count; ++i) {
5724 MonoClass *iface = class->interfaces [i];
5725 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
5732 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
5735 gpointer iter = NULL;
5736 MonoClassField *field;
5737 MonoClass *field_class;
5739 if (!class->valuetype)
5742 type = class->byval_arg.type;
5743 /*primitive type fields are not properly decoded*/
5744 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
5747 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5751 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
5754 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
5756 if (field_class == target_class || class == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
5764 verify_valuetype_layout (MonoClass *class)
5767 res = verify_valuetype_layout_with_target (class, class);
5772 * Check if the class is verifiable.
5774 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5775 * field and invalid generic instantiation are fatal errors.
5777 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5781 mono_verifier_verify_class (MonoClass *class)
5783 /*Neither <Module>, object or ifaces have parent.*/
5784 if (!class->parent &&
5785 class != mono_defaults.object_class &&
5786 !MONO_CLASS_IS_INTERFACE (class) &&
5787 (!class->image->dynamic && class->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
5789 if (class->parent && MONO_CLASS_IS_INTERFACE (class->parent))
5791 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
5793 if (!verify_class_for_overlapping_reference_fields (class))
5795 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
5797 if (class->generic_class == NULL && !verify_class_fields (class))
5799 if (class->valuetype && !verify_valuetype_layout (class))
5801 if (!verify_interfaces (class))
5808 mono_verifier_verify_class (MonoClass *class)
5810 /* The verifier was disabled at compile time */
5815 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5817 /* The verifier was disabled at compile time */
5822 mono_verifier_is_class_full_trust (MonoClass *klass)
5824 /* The verifier was disabled at compile time */
5829 mono_verifier_is_method_full_trust (MonoMethod *method)
5831 /* The verifier was disabled at compile time */
5836 mono_verifier_is_enabled_for_image (MonoImage *image)
5838 /* The verifier was disabled at compile time */
5843 mono_verifier_is_enabled_for_class (MonoClass *klass)
5845 /* The verifier was disabled at compile time */
5850 mono_verifier_is_enabled_for_method (MonoMethod *method)
5852 /* The verifier was disabled at compile time */
5857 mono_method_verify (MonoMethod *method, int level)
5859 /* The verifier was disabled at compile time */
5864 mono_free_verify_list (GSList *list)
5866 /* The verifier was disabled at compile time */
5867 /* will always be null if verifier is disabled */