5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 #include <mono/metadata/object-internals.h>
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/verify-internals.h>
15 #include <mono/metadata/opcodes.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/reflection.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/metadata.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/class-internals.h>
23 #include <mono/metadata/security-manager.h>
24 #include <mono/metadata/security-core-clr.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/mono-basic-block.h>
27 #include <mono/utils/mono-counters.h>
28 #include <mono/utils/monobitset.h>
33 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
34 static gboolean verify_all = FALSE;
37 * Set the desired level of checks for the verfier.
41 mono_verifier_set_mode (MiniVerifierMode mode)
47 mono_verifier_enable_verify_all ()
52 #ifndef DISABLE_VERIFIER
54 * Pull the list of opcodes
56 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
60 #include "mono/cil/opcode.def"
65 #ifdef MONO_VERIFIER_DEBUG
66 #define VERIFIER_DEBUG(code) do { code } while (0)
68 #define VERIFIER_DEBUG(code)
71 //////////////////////////////////////////////////////////////////
72 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
73 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
74 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
75 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
76 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
77 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
79 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
80 vinfo->info.status = __status; \
81 vinfo->info.message = ( __msg ); \
82 vinfo->exception_type = (__exception); \
83 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
86 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
87 #define ADD_VERIFY_ERROR(__ctx, __msg) \
89 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
93 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
95 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
96 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
97 (__ctx)->verifiable = 0; \
98 if (IS_FAIL_FAST_MODE (__ctx)) \
103 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
105 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
106 (__ctx)->valid = 0; \
109 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
111 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
112 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
113 (__ctx)->verifiable = 0; \
114 if (IS_FAIL_FAST_MODE (__ctx)) \
115 (__ctx)->valid = 0; \
119 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
120 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
122 #if SIZEOF_VOID_P == 4
123 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
125 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
128 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
129 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
131 /*Flags to be used with ILCodeDesc::flags */
133 /*Instruction has not been processed.*/
134 IL_CODE_FLAG_NOT_PROCESSED = 0,
135 /*Instruction was decoded by mono_method_verify loop.*/
136 IL_CODE_FLAG_SEEN = 1,
137 /*Instruction was target of a branch or is at a protected block boundary.*/
138 IL_CODE_FLAG_WAS_TARGET = 2,
139 /*Used by stack_init to avoid double initialize each entry.*/
140 IL_CODE_FLAG_STACK_INITED = 4,
141 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
142 IL_CODE_STACK_MERGED = 8,
143 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
144 IL_CODE_DELEGATE_SEQUENCE = 0x10,
145 /*This is a delegate created from a ldftn to a non final virtual method*/
146 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
147 /*This is a call to a non final virtual method*/
148 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
166 guint16 size, max_size;
183 /*Allocated fnptr MonoType that should be freed by us.*/
185 /*Type dup'ed exception types from catch blocks.*/
186 GSList *exception_types;
191 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
195 MonoMethodSignature *signature;
196 MonoMethodHeader *header;
198 MonoGenericContext *generic_context;
202 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
203 *on a method that creates a delegate for a non-final virtual method using ldftn*/
204 gboolean has_this_store;
206 /*This flag is used to control if the contructor of the parent class has been called.
207 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
208 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
210 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
211 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
213 gboolean super_ctor_called;
217 MonoType *constrained_type;
221 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
224 get_stack_type (MonoType *type);
227 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
230 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
233 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
234 //////////////////////////////////////////////////////////////////
239 TYPE_INV = 0, /* leave at 0. */
244 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
246 /* value types and classes */
248 /* Number of types, used to define the size of the tables*/
251 /* Used by tables to signal that a result is not verifiable*/
252 NON_VERIFIABLE_RESULT = 0x80,
254 /*Mask used to extract just the type, excluding flags */
257 /* The stack type is a managed pointer, unmask the value to res */
258 POINTER_MASK = 0x100,
260 /*Stack type with the pointer mask*/
261 RAW_TYPE_MASK = 0x10F,
263 /* Controlled Mutability Manager Pointer */
266 /* The stack type is a null literal*/
267 NULL_LITERAL_MASK = 0x400,
269 /**Used by ldarg.0 and family to let delegate verification happens.*/
270 THIS_POINTER_MASK = 0x800,
272 /**Signals that this is a boxed value type*/
275 /*This is an unitialized this ref*/
276 UNINIT_THIS_MASK = 0x2000,
279 static const char* const
280 type_names [TYPE_MAX + 1] = {
291 PREFIX_UNALIGNED = 1,
294 PREFIX_CONSTRAINED = 8,
297 //////////////////////////////////////////////////////////////////
299 #ifdef ENABLE_VERIFIER_STATS
301 #define MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
302 #define MEM_FREE(amt) do { working_set -= (amt); } while (0)
304 static int allocated_memory;
305 static int working_set;
306 static int max_allocated_memory;
307 static int max_working_set;
308 static int total_allocated_memory;
311 finish_collect_stats (void)
313 max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
314 max_working_set = MAX (max_working_set, working_set);
315 total_allocated_memory += allocated_memory;
316 allocated_memory = working_set = 0;
320 init_verifier_stats (void)
322 static gboolean inited;
325 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
326 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
327 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
333 #define MEM_ALLOC(amt) do {} while (0)
334 #define MEM_FREE(amt) do { } while (0)
336 #define finish_collect_stats()
337 #define init_verifier_stats()
342 //////////////////////////////////////////////////////////////////
345 /*Token validation macros and functions */
346 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
347 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
348 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
349 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
351 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
352 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
353 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
354 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
355 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
356 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
359 * Verify if @token refers to a valid row on int's table.
362 token_bounds_check (MonoImage *image, guint32 token)
365 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
366 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
370 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
372 MonoType *res = g_new0 (MonoType, 1);
373 MEM_ALLOC (sizeof (MonoType));
375 //FIXME use mono_method_get_signature_full
376 res->data.method = mono_method_signature (method);
377 res->type = MONO_TYPE_FNPTR;
378 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
383 * mono_type_is_enum_type:
385 * Returns TRUE if @type is an enum type.
388 mono_type_is_enum_type (MonoType *type)
390 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
392 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
398 * mono_type_is_value_type:
400 * Returns TRUE if @type is named after @namespace.@name.
404 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
406 return type->type == MONO_TYPE_VALUETYPE &&
407 !strcmp (namespace, type->data.klass->name_space) &&
408 !strcmp (name, type->data.klass->name);
412 * Returns TURE if @type is VAR or MVAR
415 mono_type_is_generic_argument (MonoType *type)
417 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
421 * mono_type_get_underlying_type_any:
423 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
425 * Returns the underlying type of @type regardless if it is byref or not.
428 mono_type_get_underlying_type_any (MonoType *type)
430 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
431 return mono_class_enum_basetype (type->data.klass);
432 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
433 return mono_class_enum_basetype (type->data.generic_class->container_class);
437 static G_GNUC_UNUSED const char*
438 mono_type_get_stack_name (MonoType *type)
440 return type_names [get_stack_type (type) & TYPE_MASK];
443 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
444 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
447 mono_method_is_constructor (MonoMethod *method)
449 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
450 !(method->flags & CTOR_INVALID_FLAGS) &&
451 !strcmp (".ctor", method->name));
455 mono_class_has_default_constructor (MonoClass *klass)
460 mono_class_setup_methods (klass);
461 if (klass->exception_type)
464 for (i = 0; i < klass->method.count; ++i) {
465 method = klass->methods [i];
466 if (mono_method_is_constructor (method) &&
467 mono_method_signature (method) &&
468 mono_method_signature (method)->param_count == 0 &&
469 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
476 * Verify if @type is valid for the given @ctx verification context.
477 * this function checks for VAR and MVAR types that are invalid under the current verifier,
480 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
483 MonoGenericInst *inst;
485 switch (type->type) {
490 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
491 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
494 case MONO_TYPE_SZARRAY:
495 return mono_type_is_valid_type_in_context (&type->data.klass->byval_arg, context);
496 case MONO_TYPE_ARRAY:
497 return mono_type_is_valid_type_in_context (&type->data.array->eklass->byval_arg, context);
499 return mono_type_is_valid_type_in_context (type->data.type, context);
500 case MONO_TYPE_GENERICINST:
501 inst = type->data.generic_class->context.class_inst;
504 for (i = 0; i < inst->type_argc; ++i)
505 if (!mono_type_is_valid_type_in_context (inst->type_argv [i], context))
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, MonoClass *candidate_param_class, 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 for (target_class = tinfo->constraints; *target_class; ++target_class) {
609 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
612 tc = mono_class_from_mono_type (inflated);
613 mono_metadata_free_type (inflated);
616 * A constraint from @target might inflate into @candidate itself and in that case we don't need
617 * check it's constraints since it satisfy the constraint by itself.
619 if (mono_metadata_type_equal (&tc->byval_arg, &candidate_param_class->byval_arg))
622 if (!cinfo->constraints)
625 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
627 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
630 cc = mono_class_from_mono_type (inflated);
631 mono_metadata_free_type (inflated);
633 if (mono_class_is_assignable_from (tc, cc))
636 if (!*candidate_class)
643 static MonoGenericParam*
644 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
646 MonoGenericContainer *gc;
647 MonoMethod *method = ctx->method;
650 num = mono_type_get_generic_param_num (type);
652 if (type->type == MONO_TYPE_VAR) {
653 MonoClass *gtd = method->klass;
654 if (gtd->generic_class)
655 gtd = gtd->generic_class->container_class;
656 gc = gtd->generic_container;
658 MonoMethod *gmd = method;
659 if (method->is_inflated)
660 gmd = ((MonoMethodInflated*)method)->declaring;
661 gc = mono_method_get_generic_container (gmd);
665 return mono_generic_container_get_param (gc, num);
671 * Verify if @type is valid for the given @ctx verification context.
672 * this function checks for VAR and MVAR types that are invalid under the current verifier,
673 * This means that it either
676 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
678 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
682 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
685 for (i = 0; i < ginst->type_argc; ++i) {
686 MonoType *type = ginst->type_argv [i];
687 if (!is_valid_type_in_context (ctx, type))
694 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
697 for (i = 0; i < ginst->type_argc; ++i) {
698 MonoType *type = ginst->type_argv [i];
699 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
700 MonoGenericParam *candidate;
701 MonoClass *candidate_class;
703 if (!mono_type_is_generic_argument (type))
706 if (!is_valid_type_in_context (ctx, type))
709 candidate = verifier_get_generic_param_from_type (ctx, type);
710 candidate_class = mono_class_from_mono_type (type);
712 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, candidate_class, context))
719 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
721 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
722 MonoGenericInst *ginst = gmethod->context.method_inst;
723 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
724 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
728 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
730 MonoGenericClass *gklass = klass->generic_class;
731 MonoGenericInst *ginst = gklass->context.class_inst;
732 MonoGenericContainer *gc = gklass->container_class->generic_container;
733 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
737 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
739 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
740 MonoGenericInst *ginst = gmethod->context.method_inst;
741 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
742 if (!gc) /*non-generic inflated method - it's part of a generic type */
744 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
746 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
751 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
753 MonoGenericClass *gklass = klass->generic_class;
754 MonoGenericInst *ginst = gklass->context.class_inst;
755 MonoGenericContainer *gc = gklass->container_class->generic_container;
756 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
758 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
762 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
767 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
771 if (!is_valid_type_in_context (ctx, type)) {
772 char *str = mono_type_full_name (type);
773 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
774 type->type == MONO_TYPE_VAR ? "!" : "!!",
776 type->type == MONO_TYPE_VAR ? "class" : "method",
778 MONO_EXCEPTION_BAD_IMAGE);
783 klass = mono_class_from_mono_type (type);
784 mono_class_init (klass);
785 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
786 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
787 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);
789 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);
790 mono_loader_clear_error ();
794 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
795 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);
799 if (!klass->generic_class)
802 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
803 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);
807 if (!mono_class_repect_method_constraints (ctx, klass)) {
808 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);
815 static verify_result_t
816 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
818 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
819 return RESULT_INVALID;
821 if (!method->is_inflated)
824 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
825 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);
826 return RESULT_INVALID;
829 if (!mono_method_repect_method_constraints (ctx, method)) {
830 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));
831 return RESULT_UNVERIFIABLE;
837 static MonoClassField*
838 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
839 MonoClassField *field;
840 MonoClass *klass = NULL;
842 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
843 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);
847 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
848 if (!field || !field->parent || !klass || mono_loader_get_last_error ()) {
849 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);
850 mono_loader_clear_error ();
854 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
862 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
865 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
866 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);
870 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
872 if (!method || mono_loader_get_last_error ()) {
873 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);
874 mono_loader_clear_error ();
878 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
885 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
888 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
889 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
893 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
895 if (!type || mono_loader_get_last_error ()) {
896 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);
897 mono_loader_clear_error ();
901 if (!mono_type_is_valid_in_context (ctx, type))
908 /* stack_slot_get_type:
910 * Returns the stack type of @value. This value includes POINTER_MASK.
912 * Use this function to checks that account for a managed pointer.
915 stack_slot_get_type (ILStackDesc *value)
917 return value->stype & RAW_TYPE_MASK;
920 /* stack_slot_get_underlying_type:
922 * Returns the stack type of @value. This value does not include POINTER_MASK.
924 * Use this function is cases where the fact that the value could be a managed pointer is
925 * irrelevant. For example, field load doesn't care about this fact of type on stack.
928 stack_slot_get_underlying_type (ILStackDesc *value)
930 return value->stype & TYPE_MASK;
933 /* stack_slot_is_managed_pointer:
935 * Returns TRUE is @value is a managed pointer.
938 stack_slot_is_managed_pointer (ILStackDesc *value)
940 return (value->stype & POINTER_MASK) == POINTER_MASK;
943 /* stack_slot_is_managed_mutability_pointer:
945 * Returns TRUE is @value is a managed mutability pointer.
947 static G_GNUC_UNUSED gboolean
948 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
950 return (value->stype & CMMP_MASK) == CMMP_MASK;
953 /* stack_slot_is_null_literal:
955 * Returns TRUE is @value is the null literal.
958 stack_slot_is_null_literal (ILStackDesc *value)
960 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
964 /* stack_slot_is_this_pointer:
966 * Returns TRUE is @value is the this literal
969 stack_slot_is_this_pointer (ILStackDesc *value)
971 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
974 /* stack_slot_is_boxed_value:
976 * Returns TRUE is @value is a boxed value
979 stack_slot_is_boxed_value (ILStackDesc *value)
981 return (value->stype & BOXED_MASK) == BOXED_MASK;
985 stack_slot_get_name (ILStackDesc *value)
987 return type_names [value->stype & TYPE_MASK];
990 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
991 if (PRED (value)) { \
993 g_string_append (str, ", "); \
994 g_string_append (str, NAME); \
999 stack_slot_stack_type_full_name (ILStackDesc *value)
1001 GString *str = g_string_new ("");
1003 gboolean has_pred = FALSE, first = TRUE;
1005 if ((value->stype & TYPE_MASK) != value->stype) {
1006 g_string_append(str, "[");
1007 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
1008 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
1009 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
1010 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
1011 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1015 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1017 g_string_append(str, "[");
1019 g_string_append (str, ", ");
1020 g_string_append (str, "unboxed");
1025 g_string_append(str, "] ");
1027 g_string_append (str, stack_slot_get_name (value));
1029 g_string_free (str, FALSE);
1034 stack_slot_full_name (ILStackDesc *value)
1036 char *type_name = mono_type_full_name (value->type);
1037 char *stack_name = stack_slot_stack_type_full_name (value);
1038 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1040 g_free (stack_name);
1044 //////////////////////////////////////////////////////////////////
1046 mono_free_verify_list (GSList *list)
1048 MonoVerifyInfoExtended *info;
1051 for (tmp = list; tmp; tmp = tmp->next) {
1053 g_free (info->info.message);
1056 g_slist_free (list);
1059 #define ADD_ERROR(list,msg) \
1061 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1062 vinfo->info.status = MONO_VERIFY_ERROR; \
1063 vinfo->info.message = (msg); \
1064 (list) = g_slist_prepend ((list), vinfo); \
1067 #define ADD_WARN(list,code,msg) \
1069 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1070 vinfo->info.status = (code); \
1071 vinfo->info.message = (msg); \
1072 (list) = g_slist_prepend ((list), vinfo); \
1075 #define ADD_INVALID(list,msg) \
1077 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1078 vinfo->status = MONO_VERIFY_ERROR; \
1079 vinfo->message = (msg); \
1080 (list) = g_slist_prepend ((list), vinfo); \
1081 /*G_BREAKPOINT ();*/ \
1085 #define CHECK_STACK_UNDERFLOW(num) \
1087 if (cur_stack < (num)) \
1088 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1091 #define CHECK_STACK_OVERFLOW() \
1093 if (cur_stack >= max_stack) \
1094 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1099 in_any_block (MonoMethodHeader *header, guint offset)
1102 MonoExceptionClause *clause;
1104 for (i = 0; i < header->num_clauses; ++i) {
1105 clause = &header->clauses [i];
1106 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1108 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1110 if (MONO_OFFSET_IN_FILTER (clause, offset))
1117 * in_any_exception_block:
1119 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1122 in_any_exception_block (MonoMethodHeader *header, guint offset)
1125 MonoExceptionClause *clause;
1127 for (i = 0; i < header->num_clauses; ++i) {
1128 clause = &header->clauses [i];
1129 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1131 if (MONO_OFFSET_IN_FILTER (clause, offset))
1138 * is_valid_branch_instruction:
1140 * Verify if it's valid to perform a branch from @offset to @target.
1141 * This should be used with br and brtrue/false.
1142 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1143 * The major diferent from other similiar functions is that branching into a
1144 * finally/fault block is invalid instead of just unverifiable.
1147 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1150 MonoExceptionClause *clause;
1152 for (i = 0; i < header->num_clauses; ++i) {
1153 clause = &header->clauses [i];
1154 /*branching into a finally block is invalid*/
1155 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1156 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1157 MONO_OFFSET_IN_HANDLER (clause, target))
1160 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1162 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1164 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1171 * is_valid_cmp_branch_instruction:
1173 * Verify if it's valid to perform a branch from @offset to @target.
1174 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1175 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1177 * The major diferences from other similar functions are that most errors lead to invalid
1178 * code and only branching out of finally, filter or fault clauses is unverifiable.
1181 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1184 MonoExceptionClause *clause;
1186 for (i = 0; i < header->num_clauses; ++i) {
1187 clause = &header->clauses [i];
1188 /*branching out of a handler or finally*/
1189 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1190 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1191 !MONO_OFFSET_IN_HANDLER (clause, target))
1194 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1196 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1198 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1205 * A leave can't escape a finally block
1208 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1211 MonoExceptionClause *clause;
1213 for (i = 0; i < header->num_clauses; ++i) {
1214 clause = &header->clauses [i];
1215 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1217 if (MONO_OFFSET_IN_FILTER (clause, offset))
1224 * A rethrow can't happen outside of a catch handler.
1227 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1230 MonoExceptionClause *clause;
1232 for (i = 0; i < header->num_clauses; ++i) {
1233 clause = &header->clauses [i];
1234 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1236 if (MONO_OFFSET_IN_FILTER (clause, offset))
1243 * An endfinally can't happen outside of a finally/fault handler.
1246 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1249 MonoExceptionClause *clause;
1251 for (i = 0; i < header->num_clauses; ++i) {
1252 clause = &header->clauses [i];
1253 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1261 * An endfilter can only happens inside a filter clause.
1262 * In non-strict mode filter is allowed inside the handler clause too
1264 static MonoExceptionClause *
1265 is_correct_endfilter (VerifyContext *ctx, guint offset)
1268 MonoExceptionClause *clause;
1270 for (i = 0; i < ctx->header->num_clauses; ++i) {
1271 clause = &ctx->header->clauses [i];
1272 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1274 if (MONO_OFFSET_IN_FILTER (clause, offset))
1276 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1284 * Non-strict endfilter can happens inside a try block or any handler block
1287 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1290 MonoExceptionClause *clause;
1292 for (i = 0; i < ctx->header->num_clauses; ++i) {
1293 clause = &ctx->header->clauses [i];
1294 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1301 is_valid_bool_arg (ILStackDesc *arg)
1303 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1307 switch (stack_slot_get_underlying_type (arg)) {
1310 case TYPE_NATIVE_INT:
1314 g_assert (arg->type);
1315 switch (arg->type->type) {
1316 case MONO_TYPE_CLASS:
1317 case MONO_TYPE_STRING:
1318 case MONO_TYPE_OBJECT:
1319 case MONO_TYPE_SZARRAY:
1320 case MONO_TYPE_ARRAY:
1321 case MONO_TYPE_FNPTR:
1324 case MONO_TYPE_GENERICINST:
1325 /*We need to check if the container class
1326 * of the generic type is a valuetype, iow:
1327 * is it a "class Foo<T>" or a "struct Foo<T>"?
1329 return !arg->type->data.generic_class->container_class->valuetype;
1337 /*Type manipulation helper*/
1339 /*Returns the byref version of the supplied MonoType*/
1341 mono_type_get_type_byref (MonoType *type)
1345 return &mono_class_from_mono_type (type)->this_arg;
1349 /*Returns the byval version of the supplied MonoType*/
1351 mono_type_get_type_byval (MonoType *type)
1355 return &mono_class_from_mono_type (type)->byval_arg;
1359 mono_type_from_stack_slot (ILStackDesc *slot)
1361 if (stack_slot_is_managed_pointer (slot))
1362 return mono_type_get_type_byref (slot->type);
1366 /*Stack manipulation code*/
1369 ensure_stack_size (ILCodeDesc *stack, int required)
1374 if (required < stack->max_size)
1377 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1378 new_size = MAX (8, MAX (required, stack->max_size * 2));
1380 g_assert (new_size >= stack->size);
1381 g_assert (new_size >= required);
1383 tmp = g_new0 (ILStackDesc, new_size);
1384 MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1388 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1389 g_free (stack->stack);
1390 MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1394 stack->max_size = new_size;
1398 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1400 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1402 state->size = state->max_size = 0;
1403 state->flags |= IL_CODE_FLAG_STACK_INITED;
1407 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1409 ensure_stack_size (to, from->size);
1410 to->size = from->size;
1412 /*stack copy happens at merge points, which have small stacks*/
1414 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1418 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1420 to->stype = from->stype;
1421 to->type = from->type;
1422 to->method = from->method;
1426 check_underflow (VerifyContext *ctx, int size)
1428 if (ctx->eval.size < size) {
1429 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1436 check_overflow (VerifyContext *ctx)
1438 if (ctx->eval.size >= ctx->max_stack) {
1439 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1445 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1447 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1449 if (stack_slot_get_type (value) == TYPE_PTR) {
1450 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1456 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1458 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1460 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1461 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1467 static ILStackDesc *
1468 stack_push (VerifyContext *ctx)
1470 g_assert (ctx->eval.size < ctx->max_stack);
1471 g_assert (ctx->eval.size <= ctx->eval.max_size);
1473 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1475 return & ctx->eval.stack [ctx->eval.size++];
1478 static ILStackDesc *
1479 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1481 ILStackDesc *top = stack_push (ctx);
1487 static ILStackDesc *
1488 stack_pop (VerifyContext *ctx)
1491 g_assert (ctx->eval.size > 0);
1492 ret = ctx->eval.stack + --ctx->eval.size;
1493 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1494 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1498 /* This function allows to safely pop an unititialized this ptr from
1499 * the eval stack without marking the method as unverifiable.
1501 static ILStackDesc *
1502 stack_pop_safe (VerifyContext *ctx)
1504 g_assert (ctx->eval.size > 0);
1505 return ctx->eval.stack + --ctx->eval.size;
1508 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1510 stack_peek (VerifyContext *ctx, int distance)
1512 g_assert (ctx->eval.size - distance > 0);
1513 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1516 static ILStackDesc *
1517 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1519 ILStackDesc *top = stack_push (ctx);
1520 copy_stack_value (top, value);
1524 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1526 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1529 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1534 if (!(type = verifier_load_type (ctx, token, opcode)))
1537 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1538 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1542 if (type->type == MONO_TYPE_VOID) {
1543 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1547 if (type->type == MONO_TYPE_TYPEDBYREF)
1548 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1550 if (!(class = mono_class_from_mono_type (type)))
1551 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1553 if (class->generic_container && type->type != MONO_TYPE_GENERICINST)
1554 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));
1556 check_unverifiable_type (ctx, type);
1561 /*operation result tables */
1563 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1564 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1565 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1566 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1567 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1568 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1569 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1572 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1573 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1574 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1575 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1576 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1577 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1578 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1581 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1582 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1583 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1584 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1585 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1586 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1587 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1590 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1591 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1592 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1593 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1594 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1595 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1596 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1599 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1600 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1601 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1602 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1603 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1604 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1605 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1608 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1609 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1610 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1611 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1612 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1613 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1614 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1617 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1618 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1619 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1620 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1621 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1622 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1623 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1626 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1627 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1628 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1629 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1630 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1631 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1632 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1635 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1636 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1637 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1638 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1639 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1640 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1641 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1644 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1645 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1646 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1647 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1648 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1649 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1650 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1653 #ifdef MONO_VERIFIER_DEBUG
1657 dump_stack_value (ILStackDesc *value)
1659 printf ("[(%x)(%x)", value->type->type, value->stype);
1661 if (stack_slot_is_this_pointer (value))
1664 if (stack_slot_is_boxed_value (value))
1665 printf ("[boxed] ");
1667 if (stack_slot_is_null_literal (value))
1670 if (stack_slot_is_managed_mutability_pointer (value))
1671 printf ("Controled Mutability MP: ");
1673 if (stack_slot_is_managed_pointer (value))
1674 printf ("Managed Pointer to: ");
1676 switch (stack_slot_get_underlying_type (value)) {
1678 printf ("invalid type]");
1686 case TYPE_NATIVE_INT:
1687 printf ("native int]");
1690 printf ("float64]");
1693 printf ("unmanaged pointer]");
1696 switch (value->type->type) {
1697 case MONO_TYPE_CLASS:
1698 case MONO_TYPE_VALUETYPE:
1699 printf ("complex] (%s)", value->type->data.klass->name);
1701 case MONO_TYPE_STRING:
1702 printf ("complex] (string)");
1704 case MONO_TYPE_OBJECT:
1705 printf ("complex] (object)");
1707 case MONO_TYPE_SZARRAY:
1708 printf ("complex] (%s [])", value->type->data.klass->name);
1710 case MONO_TYPE_ARRAY:
1711 printf ("complex] (%s [%d %d %d])",
1712 value->type->data.array->eklass->name,
1713 value->type->data.array->rank,
1714 value->type->data.array->numsizes,
1715 value->type->data.array->numlobounds);
1717 case MONO_TYPE_GENERICINST:
1718 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1721 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1723 case MONO_TYPE_MVAR:
1724 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1727 //should be a boxed value
1728 char * name = mono_type_full_name (value->type);
1729 printf ("complex] %s", name);
1735 printf ("unknown stack %x type]\n", value->stype);
1736 g_assert_not_reached ();
1741 dump_stack_state (ILCodeDesc *state)
1745 printf ("(%d) ", state->size);
1746 for (i = 0; i < state->size; ++i)
1747 dump_stack_value (state->stack + i);
1752 /*Returns TRUE if candidate array type can be assigned to target.
1753 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1756 is_array_type_compatible (MonoType *target, MonoType *candidate)
1758 MonoArrayType *left = target->data.array;
1759 MonoArrayType *right = candidate->data.array;
1761 g_assert (target->type == MONO_TYPE_ARRAY);
1762 g_assert (candidate->type == MONO_TYPE_ARRAY);
1764 if (left->rank != right->rank)
1767 return mono_class_is_assignable_from (left->eklass, right->eklass);
1771 get_stack_type (MonoType *type)
1774 int type_kind = type->type;
1776 mask = POINTER_MASK;
1777 /*TODO handle CMMP_MASK */
1780 switch (type_kind) {
1783 case MONO_TYPE_BOOLEAN:
1786 case MONO_TYPE_CHAR:
1789 return TYPE_I4 | mask;
1793 return TYPE_NATIVE_INT | mask;
1795 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1796 case MONO_TYPE_FNPTR:
1798 case MONO_TYPE_TYPEDBYREF:
1799 return TYPE_PTR | mask;
1802 case MONO_TYPE_MVAR:
1804 case MONO_TYPE_CLASS:
1805 case MONO_TYPE_STRING:
1806 case MONO_TYPE_OBJECT:
1807 case MONO_TYPE_SZARRAY:
1808 case MONO_TYPE_ARRAY:
1809 return TYPE_COMPLEX | mask;
1813 return TYPE_I8 | mask;
1817 return TYPE_R8 | mask;
1819 case MONO_TYPE_GENERICINST:
1820 case MONO_TYPE_VALUETYPE:
1821 if (mono_type_is_enum_type (type)) {
1822 type = mono_type_get_underlying_type_any (type);
1825 type_kind = type->type;
1828 return TYPE_COMPLEX | mask;
1836 /* convert MonoType to ILStackDesc format (stype) */
1838 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1841 int type_kind = type->type;
1843 if (type->byref || take_addr)
1844 mask = POINTER_MASK;
1845 /* TODO handle CMMP_MASK */
1850 switch (type_kind) {
1853 case MONO_TYPE_BOOLEAN:
1856 case MONO_TYPE_CHAR:
1859 stack->stype = TYPE_I4 | mask;
1863 stack->stype = TYPE_NATIVE_INT | mask;
1866 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1867 case MONO_TYPE_FNPTR:
1869 case MONO_TYPE_TYPEDBYREF:
1870 stack->stype = TYPE_PTR | mask;
1873 case MONO_TYPE_CLASS:
1874 case MONO_TYPE_STRING:
1875 case MONO_TYPE_OBJECT:
1876 case MONO_TYPE_SZARRAY:
1877 case MONO_TYPE_ARRAY:
1880 case MONO_TYPE_MVAR:
1881 stack->stype = TYPE_COMPLEX | mask;
1886 stack->stype = TYPE_I8 | mask;
1890 stack->stype = TYPE_R8 | mask;
1892 case MONO_TYPE_GENERICINST:
1893 case MONO_TYPE_VALUETYPE:
1894 if (mono_type_is_enum_type (type)) {
1895 MonoType *utype = mono_type_get_underlying_type_any (type);
1897 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
1901 type_kind = type->type;
1904 stack->stype = TYPE_COMPLEX | mask;
1908 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1909 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
1916 * init_stack_with_value_at_exception_boundary:
1918 * Initialize the stack and push a given type.
1919 * The instruction is marked as been on the exception boundary.
1922 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
1925 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
1927 if (!mono_error_ok (&error)) {
1928 char *name = mono_type_get_full_name (klass);
1929 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
1931 mono_error_cleanup (&error);
1935 if (!ctx->max_stack) {
1936 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
1940 stack_init (ctx, code);
1941 ensure_stack_size (code, 1);
1942 set_stack_value (ctx, code->stack, type, FALSE);
1943 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
1945 code->flags |= IL_CODE_FLAG_WAS_TARGET;
1946 if (mono_type_is_generic_argument (type))
1947 code->stack->stype |= BOXED_MASK;
1950 /*Verify if type 'candidate' can be stored in type 'target'.
1952 * If strict, check for the underlying type and not the verification stack types
1955 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1957 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1958 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1960 MonoType *original_candidate = candidate;
1961 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
1963 /*only one is byref */
1964 if (candidate->byref ^ target->byref) {
1965 /* converting from native int to byref*/
1966 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1967 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1972 strict |= target->byref;
1973 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
1974 candidate = mono_type_get_underlying_type_any (candidate);
1977 switch (target->type) {
1978 case MONO_TYPE_VOID:
1979 return candidate->type == MONO_TYPE_VOID;
1982 case MONO_TYPE_BOOLEAN:
1984 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1987 case MONO_TYPE_CHAR:
1989 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1991 case MONO_TYPE_U4: {
1992 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1993 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1995 return is_native_int || is_int4;
1996 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2001 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2006 return candidate->type == target->type;
2007 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2011 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2012 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2014 return is_native_int || is_int4;
2015 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2019 if (candidate->type != MONO_TYPE_PTR)
2021 /* check the underlying type */
2022 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2024 case MONO_TYPE_FNPTR: {
2025 MonoMethodSignature *left, *right;
2026 if (candidate->type != MONO_TYPE_FNPTR)
2029 left = mono_type_get_signature (target);
2030 right = mono_type_get_signature (candidate);
2031 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2034 case MONO_TYPE_GENERICINST: {
2035 MonoClass *target_klass;
2036 MonoClass *candidate_klass;
2037 if (mono_type_is_enum_type (target)) {
2038 target = mono_type_get_underlying_type_any (target);
2044 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2045 * to take boxing status into account.
2047 if (mono_type_is_generic_argument (original_candidate))
2050 target_klass = mono_class_from_mono_type (target);
2051 candidate_klass = mono_class_from_mono_type (candidate);
2052 if (mono_class_is_nullable (target_klass)) {
2053 if (!mono_class_is_nullable (candidate_klass))
2055 return target_klass == candidate_klass;
2058 return mono_class_is_assignable_from (target_klass, candidate_klass);
2061 case MONO_TYPE_STRING:
2062 return candidate->type == MONO_TYPE_STRING;
2064 case MONO_TYPE_CLASS:
2066 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2067 * to take boxing status into account.
2069 if (mono_type_is_generic_argument (original_candidate))
2072 if (candidate->type == MONO_TYPE_VALUETYPE)
2075 /* If candidate is an enum it should return true for System.Enum and supertypes.
2076 * That's why here we use the original type and not the underlying type.
2078 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2080 case MONO_TYPE_OBJECT:
2081 return MONO_TYPE_IS_REFERENCE (candidate);
2083 case MONO_TYPE_SZARRAY: {
2086 if (candidate->type != MONO_TYPE_SZARRAY)
2089 left = mono_class_from_mono_type (target);
2090 right = mono_class_from_mono_type (candidate);
2092 return mono_class_is_assignable_from (left, right);
2095 case MONO_TYPE_ARRAY:
2096 if (candidate->type != MONO_TYPE_ARRAY)
2098 return is_array_type_compatible (target, candidate);
2100 case MONO_TYPE_TYPEDBYREF:
2101 return candidate->type == MONO_TYPE_TYPEDBYREF;
2103 case MONO_TYPE_VALUETYPE: {
2104 MonoClass *target_klass;
2105 MonoClass *candidate_klass;
2107 if (candidate->type == MONO_TYPE_CLASS)
2110 target_klass = mono_class_from_mono_type (target);
2111 candidate_klass = mono_class_from_mono_type (candidate);
2112 if (target_klass == candidate_klass)
2114 if (mono_type_is_enum_type (target)) {
2115 target = mono_type_get_underlying_type_any (target);
2124 if (candidate->type != MONO_TYPE_VAR)
2126 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2128 case MONO_TYPE_MVAR:
2129 if (candidate->type != MONO_TYPE_MVAR)
2131 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2134 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2135 g_assert_not_reached ();
2144 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2146 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2150 * Returns the generic param bound to the context been verified.
2153 static MonoGenericParam*
2154 get_generic_param (VerifyContext *ctx, MonoType *param)
2156 guint16 param_num = mono_type_get_generic_param_num (param);
2157 if (param->type == MONO_TYPE_VAR) {
2158 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2159 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2162 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2165 /*param must be a MVAR */
2166 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2167 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2170 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2175 recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
2177 MonoType *constraint_type = &constraint_class->byval_arg;
2178 if (recursion_level <= 0)
2181 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
2184 if (mono_type_is_generic_argument (constraint_type)) {
2185 MonoGenericParam *param = get_generic_param (ctx, constraint_type);
2189 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2190 if (recursive_boxed_constraint_type_check (ctx, type, *class, recursion_level - 1))
2198 * is_compatible_boxed_valuetype:
2200 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2202 * @type The source type. It it tested to be of the proper type.
2203 * @candidate type of the boxed valuetype.
2204 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2205 * @strict if TRUE candidate must be boxed compatible to the target type
2209 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2211 if (!stack_slot_is_boxed_value (stack))
2213 if (type->byref || candidate->byref)
2216 if (mono_type_is_generic_argument (candidate)) {
2217 MonoGenericParam *param = get_generic_param (ctx, candidate);
2222 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2223 /*256 should be enough since there can't be more than 255 generic arguments.*/
2224 if (recursive_boxed_constraint_type_check (ctx, type, *class, 256))
2229 if (mono_type_is_generic_argument (type))
2235 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2239 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2241 MonoType *candidate = mono_type_from_stack_slot (stack);
2242 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2245 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2248 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2251 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2255 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2257 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2261 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2263 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2267 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2269 if (candidate->byref ^ target->byref)
2272 switch (target->type) {
2273 case MONO_TYPE_VOID:
2276 case MONO_TYPE_BOOLEAN:
2279 case MONO_TYPE_CHAR:
2288 case MONO_TYPE_STRING:
2289 case MONO_TYPE_TYPEDBYREF:
2290 return candidate->type == target->type;
2293 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2295 case MONO_TYPE_FNPTR:
2296 if (candidate->type != MONO_TYPE_FNPTR)
2298 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2300 case MONO_TYPE_GENERICINST: {
2301 MonoClass *target_klass;
2302 MonoClass *candidate_klass;
2303 target_klass = mono_class_from_mono_type (target);
2304 candidate_klass = mono_class_from_mono_type (candidate);
2305 /*FIXME handle nullables and enum*/
2306 return mono_class_is_assignable_from (target_klass, candidate_klass);
2308 case MONO_TYPE_OBJECT:
2309 return MONO_TYPE_IS_REFERENCE (candidate);
2311 case MONO_TYPE_CLASS:
2312 return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2314 case MONO_TYPE_SZARRAY:
2315 if (candidate->type != MONO_TYPE_SZARRAY)
2317 return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2319 case MONO_TYPE_ARRAY:
2320 if (candidate->type != MONO_TYPE_ARRAY)
2322 return is_array_type_compatible (target, candidate);
2324 case MONO_TYPE_VALUETYPE:
2325 /*FIXME handle nullables and enum*/
2326 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2329 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2332 case MONO_TYPE_MVAR:
2333 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2337 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2338 g_assert_not_reached ();
2344 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2346 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2349 return mono_delegate_type_equal (method, delegate);
2353 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2355 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2358 return mono_delegate_type_equal (delegate, method);
2362 * mono_delegate_signature_equal:
2364 * Compare two signatures in the way expected by delegates.
2366 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2368 * FIXME can this function be eliminated and proper metadata functionality be used?
2371 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2374 int method_offset = is_static_ldftn ? 1 : 0;
2376 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2379 if (delegate_sig->call_convention != method_sig->call_convention)
2382 for (i = 0; i < delegate_sig->param_count; i++) {
2383 MonoType *p1 = delegate_sig->params [i];
2384 MonoType *p2 = method_sig->params [i + method_offset];
2386 if (!mono_delegate_param_equal (p1, p2))
2390 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2397 * verify_ldftn_delegate:
2399 * Verify properties of ldftn based delegates.
2402 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2404 MonoMethod *method = funptr->method;
2406 /*ldftn non-final virtuals only allowed if method is not static,
2407 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2408 * This rules doesn't apply if the object on stack is a boxed valuetype.
2410 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2411 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2412 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2413 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2415 /*current method must not be static*/
2416 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2417 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2419 /*value is the this pointer, loaded using ldarg.0 */
2420 if (!stack_slot_is_this_pointer (value))
2421 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));
2423 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2428 * verify_delegate_compatibility:
2430 * Verify delegate creation sequence.
2434 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2436 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2437 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2439 MonoMethod *invoke, *method;
2440 const guint8 *ip = ctx->header->code;
2441 guint32 ip_offset = ctx->ip_offset;
2442 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2444 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2445 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2449 invoke = mono_get_delegate_invoke (delegate);
2450 method = funptr->method;
2452 if (!method || !mono_method_signature (method)) {
2453 char *name = mono_type_get_full_name (delegate);
2454 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2459 if (!invoke || !mono_method_signature (invoke)) {
2460 char *name = mono_type_get_full_name (delegate);
2461 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2466 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2468 if (is_static_ldftn)
2469 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2471 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2472 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2473 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2474 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));
2476 g_free (invoke_sig);
2480 * Delegate code sequences:
2486 * [-6] ldvirtftn token
2490 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2491 verify_ldftn_delegate (ctx, delegate, value, funptr);
2492 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2493 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2495 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2497 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2500 if (is_first_arg_bound) {
2501 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2502 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2504 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2505 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2506 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2508 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2509 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2513 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2514 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2516 #undef IS_VALID_OPCODE
2517 #undef IS_LOAD_FUN_PTR
2520 /* implement the opcode checks*/
2522 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2526 if (arg >= ctx->max_args) {
2528 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2530 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2531 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2532 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2534 } else if (check_overflow (ctx)) {
2535 /*We must let the value be pushed, otherwise we would get an underflow error*/
2536 check_unverifiable_type (ctx, ctx->params [arg]);
2537 if (ctx->params [arg]->byref && take_addr)
2538 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2539 top = stack_push (ctx);
2540 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2543 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2545 ctx->has_this_store = TRUE;
2547 top->stype |= THIS_POINTER_MASK;
2548 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2549 top->stype |= UNINIT_THIS_MASK;
2555 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2557 if (arg >= ctx->num_locals) {
2558 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2559 } else if (check_overflow (ctx)) {
2560 /*We must let the value be pushed, otherwise we would get an underflow error*/
2561 check_unverifiable_type (ctx, ctx->locals [arg]);
2562 if (ctx->locals [arg]->byref && take_addr)
2563 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2565 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2570 store_arg (VerifyContext *ctx, guint32 arg)
2574 if (arg >= ctx->max_args) {
2575 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2576 if (check_underflow (ctx, 1))
2581 if (check_underflow (ctx, 1)) {
2582 value = stack_pop (ctx);
2583 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2584 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2587 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2588 ctx->has_this_store = 1;
2592 store_local (VerifyContext *ctx, guint32 arg)
2595 if (arg >= ctx->num_locals) {
2596 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2600 /*TODO verify definite assigment */
2601 if (check_underflow (ctx, 1)) {
2602 value = stack_pop(ctx);
2603 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2604 char *expected = mono_type_full_name (ctx->locals [arg]);
2605 char *found = stack_slot_full_name (value);
2606 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2617 /*FIXME add and sub needs special care here*/
2619 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2621 ILStackDesc *a, *b, *top;
2622 int idxa, idxb, complexMerge = 0;
2625 if (!check_underflow (ctx, 2))
2627 b = stack_pop (ctx);
2628 a = stack_pop (ctx);
2630 idxa = stack_slot_get_underlying_type (a);
2631 if (stack_slot_is_managed_pointer (a)) {
2636 idxb = stack_slot_get_underlying_type (b);
2637 if (stack_slot_is_managed_pointer (b)) {
2644 res = table [idxa][idxb];
2646 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2647 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2649 top = stack_push (ctx);
2650 if (res == TYPE_INV) {
2651 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)));
2652 copy_stack_value (top, a);
2656 if (res & NON_VERIFIABLE_RESULT) {
2657 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)));
2659 res = res & ~NON_VERIFIABLE_RESULT;
2662 if (complexMerge && res == TYPE_PTR) {
2663 if (complexMerge == 1)
2664 copy_stack_value (top, a);
2665 else if (complexMerge == 2)
2666 copy_stack_value (top, b);
2668 * There is no need to merge the type of two pointers.
2669 * The only valid operation is subtraction, that returns a native
2670 * int as result and can be used with any 2 pointer kinds.
2671 * This is valid acording to Patition III 1.1.4
2680 do_boolean_branch_op (VerifyContext *ctx, int delta)
2682 int target = ctx->ip_offset + delta;
2685 VERIFIER_DEBUG ( printf ("boolean 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 ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2692 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
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, 1))
2706 top = stack_pop (ctx);
2707 if (!is_valid_bool_arg (top))
2708 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));
2710 check_unmanaged_pointer (ctx, top);
2714 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2716 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2720 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2725 int target = ctx->ip_offset + delta;
2727 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2729 if (target < 0 || target >= ctx->code_size) {
2730 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2734 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2735 case 1: /*FIXME use constants and not magic numbers.*/
2736 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2739 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2743 ctx->target = target;
2745 if (!check_underflow (ctx, 2))
2748 b = stack_pop (ctx);
2749 a = stack_pop (ctx);
2751 idxa = stack_slot_get_underlying_type (a);
2752 if (stack_slot_is_managed_pointer (a))
2755 idxb = stack_slot_get_underlying_type (b);
2756 if (stack_slot_is_managed_pointer (b))
2759 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2764 res = table [idxa][idxb];
2767 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2768 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2770 if (res == TYPE_INV) {
2771 CODE_NOT_VERIFIABLE (ctx,
2772 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));
2773 } else if (res & NON_VERIFIABLE_RESULT) {
2774 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));
2775 res = res & ~NON_VERIFIABLE_RESULT;
2780 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2786 if (!check_underflow (ctx, 2))
2788 b = stack_pop (ctx);
2789 a = stack_pop (ctx);
2791 if (opcode == CEE_CGT_UN) {
2792 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2793 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2798 idxa = stack_slot_get_underlying_type (a);
2799 if (stack_slot_is_managed_pointer (a))
2802 idxb = stack_slot_get_underlying_type (b);
2803 if (stack_slot_is_managed_pointer (b))
2806 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2811 res = table [idxa][idxb];
2814 if(res == TYPE_INV) {
2815 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));
2816 } else if (res & NON_VERIFIABLE_RESULT) {
2817 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));
2818 res = res & ~NON_VERIFIABLE_RESULT;
2820 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2824 do_ret (VerifyContext *ctx)
2826 MonoType *ret = ctx->signature->ret;
2827 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2828 if (ret->type != MONO_TYPE_VOID) {
2830 if (!check_underflow (ctx, 1))
2833 top = stack_pop(ctx);
2835 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2836 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2840 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
2841 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
2844 if (ctx->eval.size > 0) {
2845 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2847 if (in_any_block (ctx->header, ctx->ip_offset))
2848 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2852 * 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.
2853 * This is illegal but mono_get_method_full decoded it.
2854 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2857 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
2860 MonoMethodSignature *sig;
2863 gboolean virt_check_this = FALSE;
2864 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
2866 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
2870 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
2872 if (method->klass->valuetype) // && !constrained ???
2873 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
2875 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
2876 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
2879 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
2880 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
2882 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
2883 virt_check_this = TRUE;
2884 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
2888 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
2889 sig = mono_method_get_signature (method, ctx->image, method_token);
2892 char *name = mono_type_get_full_name (method->klass);
2893 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name, method->name, ctx->ip_offset));
2898 param_count = sig->param_count + sig->hasthis;
2899 if (!check_underflow (ctx, param_count))
2902 for (i = sig->param_count - 1; i >= 0; --i) {
2903 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2904 value = stack_pop (ctx);
2905 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
2906 char *stack_name = stack_slot_full_name (value);
2907 char *sig_name = mono_type_full_name (sig->params [i]);
2908 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));
2909 g_free (stack_name);
2913 if (stack_slot_is_managed_mutability_pointer (value))
2914 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));
2916 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
2917 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));
2923 MonoType *type = &method->klass->byval_arg;
2926 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
2927 if (!mono_method_is_constructor (ctx->method))
2928 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
2929 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
2930 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));
2932 ctx->super_ctor_called = TRUE;
2933 value = stack_pop_safe (ctx);
2934 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
2935 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
2937 value = stack_pop (ctx);
2940 copy_stack_value (©, value);
2941 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2942 //Other parts of the code suffer from the same issue of
2943 copy.type = mono_type_get_type_byval (copy.type);
2944 copy.stype &= ~POINTER_MASK;
2946 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
2947 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));
2949 if (constrained && virtual) {
2950 if (!stack_slot_is_managed_pointer (value))
2951 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
2952 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
2953 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
2954 copy.stype |= BOXED_MASK;
2956 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
2957 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));
2959 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
2960 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
2962 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
2963 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
2965 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
2966 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));
2968 if (!verify_stack_type_compatibility (ctx, type, ©)) {
2969 char *expected = mono_type_full_name (type);
2970 char *effective = stack_slot_full_name (©);
2971 char *method_name = mono_method_full_name (method, TRUE);
2972 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature expected '%s' but got '%s' for a call to '%s' at 0x%04x",
2973 expected, effective, method_name, ctx->ip_offset));
2974 g_free (method_name);
2979 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
2980 char *name = mono_method_full_name (method, TRUE);
2981 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2985 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
2986 char *name = mono_method_full_name (method, TRUE);
2987 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2991 if (sig->ret->type != MONO_TYPE_VOID) {
2992 if (check_overflow (ctx)) {
2993 value = stack_push (ctx);
2994 set_stack_value (ctx, value, sig->ret, FALSE);
2995 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
2996 ctx->prefix_set &= ~PREFIX_READONLY;
2997 value->stype |= CMMP_MASK;
3002 if ((ctx->prefix_set & PREFIX_TAIL)) {
3003 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3004 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3005 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3006 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3012 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3014 MonoClassField *field;
3016 if (!check_overflow (ctx))
3019 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3021 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3024 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3025 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3028 /*taking the address of initonly field only works from the static constructor */
3029 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3030 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3031 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3033 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3034 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3036 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3040 do_store_static_field (VerifyContext *ctx, int token) {
3041 MonoClassField *field;
3044 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3046 if (!check_underflow (ctx, 1))
3049 value = stack_pop (ctx);
3051 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3054 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3055 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3059 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3060 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3064 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3065 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3067 if (!verify_stack_type_compatibility (ctx, field->type, value)) {
3068 char *stack_name = stack_slot_full_name (value);
3069 char *field_name = mono_type_full_name (field->type);
3070 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3071 field_name, stack_name, ctx->ip_offset));
3072 g_free (field_name);
3073 g_free (stack_name);
3078 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3080 MonoClassField *field;
3082 gboolean is_pointer;
3084 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3085 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3089 //the value on stack is going to be used as a pointer
3090 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3092 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3093 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3096 g_assert (obj->type);
3098 /*The value on the stack must be a subclass of the defining type of the field*/
3099 /* we need to check if we can load the field from the stack value*/
3101 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3102 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3104 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3105 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3107 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3108 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));
3110 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3111 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3112 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));
3114 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3115 char *found = stack_slot_full_name (obj);
3116 char *expected = mono_type_full_name (&field->parent->byval_arg);
3117 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));
3122 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3123 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3126 check_unmanaged_pointer (ctx, obj);
3131 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3134 MonoClassField *field;
3137 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3139 if (!check_underflow (ctx, 1))
3141 obj = stack_pop_safe (ctx);
3143 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3146 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3147 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3149 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3150 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3151 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3153 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3157 do_store_field (VerifyContext *ctx, int token)
3159 ILStackDesc *value, *obj;
3160 MonoClassField *field;
3161 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3163 if (!check_underflow (ctx, 2))
3166 value = stack_pop (ctx);
3167 obj = stack_pop_safe (ctx);
3169 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3172 if (!verify_stack_type_compatibility (ctx, field->type, value))
3173 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3176 /*TODO proper handle for Nullable<T>*/
3178 do_box_value (VerifyContext *ctx, int klass_token)
3181 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3187 if (!check_underflow (ctx, 1))
3190 value = stack_pop (ctx);
3191 /*box is a nop for reference types*/
3193 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3194 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3199 if (!verify_stack_type_compatibility (ctx, type, value))
3200 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3202 klass = mono_class_from_mono_type (type);
3203 if (mono_class_is_nullable (klass))
3204 type = &mono_class_get_nullable_param (klass)->byval_arg;
3205 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3209 do_unbox_value (VerifyContext *ctx, int klass_token)
3212 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3217 if (!check_underflow (ctx, 1))
3220 if (!mono_class_from_mono_type (type)->valuetype)
3221 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3223 value = stack_pop (ctx);
3225 /*Value should be: a boxed valuetype or a reference type*/
3226 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3227 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3228 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));
3230 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3231 value->stype |= CMMP_MASK;
3235 do_unbox_any (VerifyContext *ctx, int klass_token)
3238 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3243 if (!check_underflow (ctx, 1))
3246 value = stack_pop (ctx);
3248 /*Value should be: a boxed valuetype or a reference type*/
3249 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3250 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3251 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));
3253 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3257 do_unary_math_op (VerifyContext *ctx, int op)
3260 if (!check_underflow (ctx, 1))
3262 value = stack_pop (ctx);
3263 switch (stack_slot_get_type (value)) {
3266 case TYPE_NATIVE_INT:
3271 case TYPE_COMPLEX: /*only enums are ok*/
3272 if (mono_type_is_enum_type (value->type))
3275 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3277 stack_push_stack_val (ctx, value);
3281 do_conversion (VerifyContext *ctx, int kind)
3284 if (!check_underflow (ctx, 1))
3286 value = stack_pop (ctx);
3288 switch (stack_slot_get_type (value)) {
3291 case TYPE_NATIVE_INT:
3295 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));
3300 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3303 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3306 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3308 case TYPE_NATIVE_INT:
3309 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3312 g_error ("unknown type %02x in conversion", kind);
3318 do_load_token (VerifyContext *ctx, int token)
3321 MonoClass *handle_class;
3322 if (!check_overflow (ctx))
3325 switch (token & 0xff000000) {
3326 case MONO_TOKEN_TYPE_DEF:
3327 case MONO_TOKEN_TYPE_REF:
3328 case MONO_TOKEN_TYPE_SPEC:
3329 case MONO_TOKEN_FIELD_DEF:
3330 case MONO_TOKEN_METHOD_DEF:
3331 case MONO_TOKEN_METHOD_SPEC:
3332 case MONO_TOKEN_MEMBER_REF:
3333 if (!token_bounds_check (ctx->image, token)) {
3334 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Table index out of range 0x%x for token %x for ldtoken at 0x%04x", mono_metadata_token_index (token), token, ctx->ip_offset));
3339 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid table 0x%x for token 0x%x for ldtoken at 0x%04x", mono_metadata_token_table (token), token, ctx->ip_offset));
3343 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3345 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3348 if (handle_class == mono_defaults.typehandle_class) {
3349 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3350 } else if (handle_class == mono_defaults.methodhandle_class) {
3351 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3352 } else if (handle_class == mono_defaults.fieldhandle_class) {
3353 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3355 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3357 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3361 do_ldobj_value (VerifyContext *ctx, int token)
3364 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3365 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3370 if (!check_underflow (ctx, 1))
3373 value = stack_pop (ctx);
3374 if (!stack_slot_is_managed_pointer (value)
3375 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3376 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3377 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3381 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3382 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3384 /*We have a byval on the stack, but the comparison must be strict. */
3385 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3386 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3388 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3392 do_stobj (VerifyContext *ctx, int token)
3394 ILStackDesc *dest, *src;
3395 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3396 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3401 if (!check_underflow (ctx, 2))
3404 src = stack_pop (ctx);
3405 dest = stack_pop (ctx);
3407 if (stack_slot_is_managed_mutability_pointer (dest))
3408 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3410 if (!stack_slot_is_managed_pointer (dest))
3411 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3413 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3414 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));
3416 if (!verify_stack_type_compatibility (ctx, type, src)) {
3417 char *type_name = mono_type_full_name (type);
3418 char *src_name = stack_slot_full_name (src);
3419 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token '%s' and source '%s' of stobj don't match ' at 0x%04x", type_name, src_name, ctx->ip_offset));
3424 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3425 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3429 do_cpobj (VerifyContext *ctx, int token)
3431 ILStackDesc *dest, *src;
3432 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3436 if (!check_underflow (ctx, 2))
3439 src = stack_pop (ctx);
3440 dest = stack_pop (ctx);
3442 if (!stack_slot_is_managed_pointer (src))
3443 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3445 if (!stack_slot_is_managed_pointer (dest))
3446 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3448 if (stack_slot_is_managed_mutability_pointer (dest))
3449 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3451 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3452 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3454 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3455 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3459 do_initobj (VerifyContext *ctx, int token)
3462 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3466 if (!check_underflow (ctx, 1))
3469 obj = stack_pop (ctx);
3471 if (!stack_slot_is_managed_pointer (obj))
3472 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3474 if (stack_slot_is_managed_mutability_pointer (obj))
3475 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3477 stack = mono_type_get_type_byval (obj->type);
3478 if (MONO_TYPE_IS_REFERENCE (stack)) {
3479 if (!verify_type_compatibility (ctx, stack, type))
3480 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3481 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3482 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3483 } else if (!verify_type_compatibility (ctx, stack, type)) {
3484 char *expected_name = mono_type_full_name (type);
3485 char *stack_name = mono_type_full_name (stack);
3487 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));
3488 g_free (expected_name);
3489 g_free (stack_name);
3494 do_newobj (VerifyContext *ctx, int token)
3498 MonoMethodSignature *sig;
3500 gboolean is_delegate = FALSE;
3502 if (!(method = verifier_load_method (ctx, token, "newobj")))
3505 if (!mono_method_is_constructor (method)) {
3506 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3510 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3511 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3513 if (!mono_method_can_access_method_full (ctx->method, method, NULL)) {
3514 char *from = mono_method_full_name (ctx->method, TRUE);
3515 char *to = mono_method_full_name (method, TRUE);
3516 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);
3521 //FIXME use mono_method_get_signature_full
3522 sig = mono_method_signature (method);
3524 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3528 if (!check_underflow (ctx, sig->param_count))
3531 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3534 ILStackDesc *funptr;
3535 //first arg is object, second arg is fun ptr
3536 if (sig->param_count != 2) {
3537 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3540 funptr = stack_pop (ctx);
3541 value = stack_pop (ctx);
3542 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3544 for (i = sig->param_count - 1; i >= 0; --i) {
3545 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3546 value = stack_pop (ctx);
3547 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3548 char *stack_name = stack_slot_full_name (value);
3549 char *sig_name = mono_type_full_name (sig->params [i]);
3550 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));
3551 g_free (stack_name);
3555 if (stack_slot_is_managed_mutability_pointer (value))
3556 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3560 if (check_overflow (ctx))
3561 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3565 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3571 if (!check_underflow (ctx, 1))
3574 if (!(type = verifier_load_type (ctx, token, opcode)))
3578 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3582 value = stack_pop (ctx);
3583 is_boxed = stack_slot_is_boxed_value (value);
3585 if (stack_slot_is_managed_pointer (value))
3586 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3587 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3588 char *name = stack_slot_full_name (value);
3589 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));
3593 switch (value->type->type) {
3594 case MONO_TYPE_FNPTR:
3596 case MONO_TYPE_TYPEDBYREF:
3597 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3600 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3601 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3605 mono_type_from_opcode (int opcode) {
3613 return &mono_defaults.sbyte_class->byval_arg;
3621 return &mono_defaults.int16_class->byval_arg;
3629 return &mono_defaults.int32_class->byval_arg;
3635 return &mono_defaults.int64_class->byval_arg;
3641 return &mono_defaults.single_class->byval_arg;
3647 return &mono_defaults.double_class->byval_arg;
3653 return &mono_defaults.int_class->byval_arg;
3657 case CEE_LDELEM_REF:
3658 case CEE_STELEM_REF:
3659 return &mono_defaults.object_class->byval_arg;
3662 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3668 do_load_indirect (VerifyContext *ctx, int opcode)
3671 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3673 if (!check_underflow (ctx, 1))
3676 value = stack_pop (ctx);
3677 if (!stack_slot_is_managed_pointer (value)) {
3678 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3679 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3683 if (opcode == CEE_LDIND_REF) {
3684 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3685 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3686 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3688 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3689 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3690 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3695 do_store_indirect (VerifyContext *ctx, int opcode)
3697 ILStackDesc *addr, *val;
3698 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3700 if (!check_underflow (ctx, 2))
3703 val = stack_pop (ctx);
3704 addr = stack_pop (ctx);
3706 check_unmanaged_pointer (ctx, addr);
3708 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3709 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3713 if (stack_slot_is_managed_mutability_pointer (addr)) {
3714 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3718 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3719 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3721 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3722 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3726 do_newarr (VerifyContext *ctx, int token)
3729 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3734 if (!check_underflow (ctx, 1))
3737 value = stack_pop (ctx);
3738 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3739 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));
3741 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3744 /*FIXME handle arrays that are not 0-indexed*/
3746 do_ldlen (VerifyContext *ctx)
3750 if (!check_underflow (ctx, 1))
3753 value = stack_pop (ctx);
3755 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3756 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3758 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3761 /*FIXME handle arrays that are not 0-indexed*/
3762 /*FIXME handle readonly prefix and CMMP*/
3764 do_ldelema (VerifyContext *ctx, int klass_token)
3766 ILStackDesc *index, *array, *res;
3767 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
3773 if (!check_underflow (ctx, 2))
3776 index = stack_pop (ctx);
3777 array = stack_pop (ctx);
3779 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3780 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));
3782 if (!stack_slot_is_null_literal (array)) {
3783 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3784 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3786 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3787 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3789 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3792 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3796 res = stack_push (ctx);
3797 set_stack_value (ctx, res, type, TRUE);
3798 if (ctx->prefix_set & PREFIX_READONLY) {
3799 ctx->prefix_set &= ~PREFIX_READONLY;
3800 res->stype |= CMMP_MASK;
3805 * FIXME handle arrays that are not 0-indexed
3806 * FIXME handle readonly prefix and CMMP
3809 do_ldelem (VerifyContext *ctx, int opcode, int token)
3811 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3812 ILStackDesc *index, *array;
3814 if (!check_underflow (ctx, 2))
3817 if (opcode == CEE_LDELEM) {
3818 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
3819 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3823 type = mono_type_from_opcode (opcode);
3826 index = stack_pop (ctx);
3827 array = stack_pop (ctx);
3829 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3830 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));
3832 if (!stack_slot_is_null_literal (array)) {
3833 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3834 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));
3836 if (opcode == CEE_LDELEM_REF) {
3837 if (array->type->data.klass->valuetype)
3838 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3839 type = &array->type->data.klass->byval_arg;
3841 MonoType *candidate = &array->type->data.klass->byval_arg;
3842 if (IS_STRICT_MODE (ctx)) {
3843 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
3844 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
3845 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)) ||
3846 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
3847 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3849 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
3850 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3855 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3860 * FIXME handle arrays that are not 0-indexed
3863 do_stelem (VerifyContext *ctx, int opcode, int token)
3865 ILStackDesc *index, *array, *value;
3867 if (!check_underflow (ctx, 3))
3870 if (opcode == CEE_STELEM) {
3871 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
3872 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3876 type = mono_type_from_opcode (opcode);
3879 value = stack_pop (ctx);
3880 index = stack_pop (ctx);
3881 array = stack_pop (ctx);
3883 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3884 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));
3886 if (!stack_slot_is_null_literal (array)) {
3887 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3888 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));
3890 if (opcode == CEE_STELEM_REF) {
3891 if (array->type->data.klass->valuetype)
3892 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3893 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3894 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3898 if (opcode == CEE_STELEM_REF) {
3899 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3900 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3901 } else if (opcode != CEE_STELEM_REF) {
3902 if (!verify_stack_type_compatibility (ctx, type, value))
3903 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3905 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
3906 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));
3912 do_throw (VerifyContext *ctx)
3914 ILStackDesc *exception;
3915 if (!check_underflow (ctx, 1))
3917 exception = stack_pop (ctx);
3919 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3920 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3922 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
3923 char *name = mono_type_full_name (exception->type);
3924 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));
3927 /*The stack is left empty after a throw*/
3933 do_endfilter (VerifyContext *ctx)
3935 MonoExceptionClause *clause;
3937 if (IS_STRICT_MODE (ctx)) {
3938 if (ctx->eval.size != 1)
3939 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3941 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
3942 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3945 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3946 if (IS_STRICT_MODE (ctx)) {
3947 if (ctx->ip_offset != clause->handler_offset - 2)
3948 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3950 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3951 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3954 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3955 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3957 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3964 do_leave (VerifyContext *ctx, int delta)
3966 int target = ((gint32)ctx->ip_offset) + delta;
3967 if (target >= ctx->code_size || target < 0)
3968 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3970 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
3971 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
3973 ctx->target = target;
3979 * Verify br and br.s opcodes.
3982 do_static_branch (VerifyContext *ctx, int delta)
3984 int target = ctx->ip_offset + delta;
3985 if (target < 0 || target >= ctx->code_size) {
3986 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
3990 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3992 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3995 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3999 ctx->target = target;
4003 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4005 int i, base = ctx->ip_offset + 5 + count * 4;
4008 if (!check_underflow (ctx, 1))
4011 value = stack_pop (ctx);
4013 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4014 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4016 for (i = 0; i < count; ++i) {
4017 int target = base + read32 (data + i * 4);
4019 if (target < 0 || target >= ctx->code_size) {
4020 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4024 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4026 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4029 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4032 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4037 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4042 if (virtual && !check_underflow (ctx, 1))
4045 if (!virtual && !check_overflow (ctx))
4048 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4049 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4053 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4056 if (mono_method_is_constructor (method))
4057 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4060 ILStackDesc *top = stack_pop (ctx);
4062 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4063 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4065 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4066 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4068 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4069 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4072 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4073 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);
4075 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4076 top->method = method;
4080 do_sizeof (VerifyContext *ctx, int token)
4084 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4085 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4089 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4092 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4093 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4097 if (type->type == MONO_TYPE_VOID) {
4098 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4102 if (check_overflow (ctx))
4103 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4106 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4108 do_localloc (VerifyContext *ctx)
4112 if (ctx->eval.size != 1) {
4113 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4117 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4118 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4122 /*TODO verify top type*/
4123 top = stack_pop (ctx);
4125 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4126 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4130 do_ldstr (VerifyContext *ctx, guint32 token)
4132 GSList *error = NULL;
4133 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4134 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4138 if (!ctx->image->dynamic && !mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4140 ctx->list = g_slist_concat (ctx->list, error);
4141 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4145 if (check_overflow (ctx))
4146 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4150 do_refanyval (VerifyContext *ctx, int token)
4154 if (!check_underflow (ctx, 1))
4157 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4160 top = stack_pop (ctx);
4162 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4163 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));
4165 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4169 do_refanytype (VerifyContext *ctx)
4173 if (!check_underflow (ctx, 1))
4176 top = stack_pop (ctx);
4178 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4179 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));
4181 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4186 do_mkrefany (VerifyContext *ctx, int token)
4190 if (!check_underflow (ctx, 1))
4193 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4196 top = stack_pop (ctx);
4198 if (stack_slot_is_managed_mutability_pointer (top))
4199 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4201 if (!stack_slot_is_managed_pointer (top)) {
4202 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));
4204 MonoType *stack_type = mono_type_get_type_byval (top->type);
4205 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4206 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4208 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4209 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4212 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4216 do_ckfinite (VerifyContext *ctx)
4219 if (!check_underflow (ctx, 1))
4222 top = stack_pop (ctx);
4224 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4225 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));
4226 stack_push_stack_val (ctx, top);
4230 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4232 * @from holds new values for a given control path
4233 * @to holds the current values of a given control path
4235 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4238 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4242 stack_init (ctx, to);
4245 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4248 stack_copy (&ctx->eval, to);
4250 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4251 stack_copy (to, &ctx->eval);
4254 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4256 if (from->size != to->size) {
4257 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4258 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));
4262 //FIXME we need to preserve CMMP attributes
4263 //FIXME we must take null literals into consideration.
4264 for (i = 0; i < from->size; ++i) {
4265 ILStackDesc *new_slot = from->stack + i;
4266 ILStackDesc *old_slot = to->stack + i;
4267 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4268 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4269 MonoClass *old_class = mono_class_from_mono_type (old_type);
4270 MonoClass *new_class = mono_class_from_mono_type (new_type);
4271 MonoClass *match_class = NULL;
4273 // S := T then U = S (new value is compatible with current value, keep current)
4274 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4275 copy_stack_value (new_slot, old_slot);
4279 // T := S then U = T (old value is compatible with current value, use new)
4280 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4281 copy_stack_value (old_slot, new_slot);
4285 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4286 char *old_name = stack_slot_full_name (old_slot);
4287 char *new_name = stack_slot_full_name (new_slot);
4288 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));
4294 //both are reference types, use closest common super type
4295 if (!mono_class_from_mono_type (old_type)->valuetype
4296 && !mono_class_from_mono_type (new_type)->valuetype
4297 && !stack_slot_is_managed_pointer (old_slot)
4298 && !stack_slot_is_managed_pointer (new_slot)) {
4300 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4301 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4302 match_class = old_class->supertypes [j];
4307 mono_class_setup_interfaces (old_class, &error);
4308 if (!mono_error_ok (&error)) {
4309 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));
4310 mono_error_cleanup (&error);
4313 for (j = 0; j < old_class->interface_count; ++j) {
4314 for (k = 0; k < new_class->interface_count; ++k) {
4315 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4316 match_class = old_class->interfaces [j];
4322 //No decent super type found, use object
4323 match_class = mono_defaults.object_class;
4325 } 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)) {
4326 match_class = mono_defaults.object_class;
4331 char *old_name = stack_slot_full_name (old_slot);
4332 char *new_name = stack_slot_full_name (new_slot);
4333 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));
4337 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4341 g_assert (match_class);
4342 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4343 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4349 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4350 to->flags |= IL_CODE_STACK_MERGED;
4353 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4354 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4357 * is_clause_in_range :
4359 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4362 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4364 if (clause->try_offset >= start && clause->try_offset < end)
4366 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4372 * is_clause_inside_range :
4374 * Returns TRUE if @clause lies completely inside the @start - @end range.
4377 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4379 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4381 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4387 * is_clause_nested :
4389 * Returns TRUE if @nested is nested in @clause.
4392 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4394 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4396 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4397 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4400 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4401 * the each pair of exception must have the following properties:
4402 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4403 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4404 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4407 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4409 /*clause is nested*/
4410 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4411 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4415 /*wrong nesting order.*/
4416 if (is_clause_nested (clause, to_test)) {
4417 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4421 /*mutual protection*/
4422 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4423 /*handlers are not disjoint*/
4424 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4425 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4428 /* handlers are not catch or filter */
4429 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4430 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4437 /*not completelly disjoint*/
4438 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4439 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4440 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4443 #define code_bounds_check(size) \
4444 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4445 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4450 mono_opcode_is_prefix (int op)
4453 case MONO_CEE_UNALIGNED_:
4454 case MONO_CEE_VOLATILE_:
4455 case MONO_CEE_TAIL_:
4456 case MONO_CEE_CONSTRAINED_:
4457 case MONO_CEE_READONLY_:
4464 * FIXME: need to distinguish between valid and verifiable.
4465 * Need to keep track of types on the stack.
4466 * Verify types for opcodes.
4469 mono_method_verify (MonoMethod *method, int level)
4472 const unsigned char *ip, *code_start;
4473 const unsigned char *end;
4474 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4476 int i, n, need_merge = 0, start = 0;
4477 guint token, ip_offset = 0, prefix = 0;
4478 MonoGenericContext *generic_context = NULL;
4482 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4484 init_verifier_stats ();
4486 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4487 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4491 memset (&ctx, 0, sizeof (VerifyContext));
4493 //FIXME use mono_method_get_signature_full
4494 ctx.signature = mono_method_signature (method);
4495 if (!ctx.signature) {
4496 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4498 finish_collect_stats ();
4501 ctx.header = mono_method_get_header (method);
4503 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4504 finish_collect_stats ();
4507 ctx.method = method;
4508 code_start = ip = ctx.header->code;
4509 end = ip + ctx.header->code_size;
4510 ctx.image = image = method->klass->image;
4513 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4514 ctx.max_stack = ctx.header->max_stack;
4515 ctx.verifiable = ctx.valid = 1;
4518 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4519 ctx.code_size = ctx.header->code_size;
4520 MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4522 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4524 ctx.num_locals = ctx.header->num_locals;
4525 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4526 MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4528 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4529 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4531 ctx.params = g_new (MonoType*, ctx.max_args);
4532 MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4534 if (ctx.signature->hasthis)
4535 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4536 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4538 if (ctx.signature->is_inflated)
4539 ctx.generic_context = generic_context = mono_method_get_context (method);
4541 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4542 if (method->is_generic)
4543 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4545 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4548 for (i = 0; i < ctx.num_locals; ++i) {
4549 MonoType *uninflated = ctx.locals [i];
4550 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4551 if (!mono_error_ok (&error)) {
4552 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4553 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4555 mono_error_cleanup (&error);
4556 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4562 for (i = 0; i < ctx.max_args; ++i) {
4563 MonoType *uninflated = ctx.params [i];
4564 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4565 if (!mono_error_ok (&error)) {
4566 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4567 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4569 mono_error_cleanup (&error);
4570 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4575 stack_init (&ctx, &ctx.eval);
4577 for (i = 0; i < ctx.num_locals; ++i) {
4578 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4580 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4581 char *name = mono_type_full_name (ctx.locals [i]);
4582 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4589 for (i = 0; i < ctx.max_args; ++i) {
4590 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4593 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4594 char *name = mono_type_full_name (ctx.params [i]);
4595 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4604 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4605 MonoExceptionClause *clause = ctx.header->clauses + i;
4606 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); );
4608 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4609 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4611 if (clause->try_len <= 0)
4612 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4614 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4615 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4617 if (clause->handler_len <= 0)
4618 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4620 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4621 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4623 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4624 if (clause->data.filter_offset > ctx.code_size)
4625 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4627 if (clause->data.filter_offset >= clause->handler_offset)
4628 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4631 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4632 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4637 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4638 if (clause->try_offset + clause->try_len < ctx.code_size)
4639 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4640 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4641 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4643 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4644 if (!clause->data.catch_class) {
4645 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
4649 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4651 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4652 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4653 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4657 original_bb = bb = mono_basic_block_split (method, &error);
4658 if (!mono_error_ok (&error)) {
4659 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
4660 mono_error_cleanup (&error);
4665 while (ip < end && ctx.valid) {
4666 ip_offset = ip - code_start;
4668 const unsigned char *ip_copy = ip;
4671 if (ip_offset > bb->end) {
4672 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
4676 if (ip_offset == bb->end)
4679 size = mono_opcode_value_and_size (&ip_copy, end, &op);
4681 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
4685 if (ADD_IS_GREATER_OR_OVF (ip_offset, size, bb->end)) {
4686 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
4690 /*Last Instruction*/
4691 if (ip_offset + size == bb->end && mono_opcode_is_prefix (op)) {
4692 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));
4697 /*FIXME remove this once we move all bad branch checking code to use BB only*/
4698 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
4704 ctx.ip_offset = ip_offset = ip - code_start;
4706 /*We need to check against fallthrou in and out of protected blocks.
4707 * For fallout we check the once a protected block ends, if the start flag is not set.
4708 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4709 * TODO convert these checks to be done using flags and not this loop
4711 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4712 MonoExceptionClause *clause = ctx.header->clauses + i;
4714 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4715 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4719 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4720 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4721 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4723 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4727 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4728 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4732 if (clause->handler_offset == ip_offset && start == 0) {
4733 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4737 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
4738 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4747 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4748 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4751 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4754 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4755 #ifdef MONO_VERIFIER_DEBUG
4758 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4759 discode [strlen (discode) - 1] = 0; /* no \n */
4760 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4763 dump_stack_state (&ctx.code [ip_offset]);
4764 dump_stack_state (&ctx.eval);
4777 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4783 code_bounds_check (2);
4784 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4788 case CEE_ADD_OVF_UN:
4789 do_binop (&ctx, *ip, add_ovf_un_table);
4793 case CEE_SUB_OVF_UN:
4794 do_binop (&ctx, *ip, sub_ovf_un_table);
4801 case CEE_MUL_OVF_UN:
4802 do_binop (&ctx, *ip, bin_ovf_table);
4807 do_binop (&ctx, *ip, add_table);
4812 do_binop (&ctx, *ip, sub_table);
4819 do_binop (&ctx, *ip, bin_op_table);
4828 do_binop (&ctx, *ip, int_bin_op_table);
4835 do_binop (&ctx, *ip, shift_op_table);
4840 if (!check_underflow (&ctx, 1))
4842 stack_pop_safe (&ctx);
4856 /*TODO support definite assignment verification? */
4857 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
4865 store_local (&ctx, *ip - CEE_STLOC_0);
4870 code_bounds_check (2);
4871 store_local (&ctx, ip [1]);
4876 code_bounds_check (2);
4877 store_arg (&ctx, ip [1]);
4891 if (check_overflow (&ctx))
4892 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4897 code_bounds_check (2);
4898 if (check_overflow (&ctx))
4899 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4904 code_bounds_check (5);
4905 if (check_overflow (&ctx))
4906 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4911 code_bounds_check (9);
4912 if (check_overflow (&ctx))
4913 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4918 code_bounds_check (5);
4919 if (check_overflow (&ctx))
4920 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4925 code_bounds_check (9);
4926 if (check_overflow (&ctx))
4927 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4932 if (check_overflow (&ctx))
4933 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4939 code_bounds_check (2);
4940 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4953 code_bounds_check (2);
4954 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
4961 code_bounds_check (5);
4962 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
4975 code_bounds_check (5);
4976 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
4983 code_bounds_check (2);
4984 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
4989 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
4995 if (!check_underflow (&ctx, 1))
4997 if (!check_overflow (&ctx))
4999 top = stack_push (&ctx);
5000 copy_stack_value (top, stack_peek (&ctx, 1));
5006 code_bounds_check (5);
5008 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5009 token = read32 (ip + 1);
5010 if (in_any_block (ctx.header, ip_offset))
5011 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5013 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5015 * FIXME: check signature, retval, arguments etc.
5021 code_bounds_check (5);
5022 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5027 code_bounds_check (5);
5028 token = read32 (ip + 1);
5030 * FIXME: check signature, retval, arguments etc.
5031 * FIXME: check requirements for tail call
5033 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5037 code_bounds_check (2);
5038 do_static_branch (&ctx, (signed char)ip [1] + 2);
5046 code_bounds_check (2);
5047 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5053 code_bounds_check (5);
5054 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5062 code_bounds_check (5);
5063 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5070 code_bounds_check (5);
5071 entries = read32 (ip + 1);
5073 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5074 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5077 code_bounds_check (sizeof (guint32) * entries);
5079 do_switch (&ctx, entries, ip);
5080 ip += sizeof (guint32) * entries;
5094 do_load_indirect (&ctx, *ip);
5106 do_store_indirect (&ctx, *ip);
5112 do_unary_math_op (&ctx, *ip);
5122 do_conversion (&ctx, TYPE_I4);
5128 do_conversion (&ctx, TYPE_I8);
5135 do_conversion (&ctx, TYPE_R8);
5141 do_conversion (&ctx, TYPE_NATIVE_INT);
5146 code_bounds_check (5);
5147 do_cpobj (&ctx, read32 (ip + 1));
5152 code_bounds_check (5);
5153 do_ldobj_value (&ctx, read32 (ip + 1));
5158 code_bounds_check (5);
5159 do_ldstr (&ctx, read32 (ip + 1));
5164 code_bounds_check (5);
5165 do_newobj (&ctx, read32 (ip + 1));
5171 code_bounds_check (5);
5172 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5178 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5183 code_bounds_check (5);
5184 do_unbox_value (&ctx, read32 (ip + 1));
5196 code_bounds_check (5);
5197 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5203 code_bounds_check (5);
5204 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5209 code_bounds_check (5);
5210 do_store_field (&ctx, read32 (ip + 1));
5215 code_bounds_check (5);
5216 do_store_static_field (&ctx, read32 (ip + 1));
5221 code_bounds_check (5);
5222 do_stobj (&ctx, read32 (ip + 1));
5226 case CEE_CONV_OVF_I1_UN:
5227 case CEE_CONV_OVF_I2_UN:
5228 case CEE_CONV_OVF_I4_UN:
5229 case CEE_CONV_OVF_U1_UN:
5230 case CEE_CONV_OVF_U2_UN:
5231 case CEE_CONV_OVF_U4_UN:
5232 do_conversion (&ctx, TYPE_I4);
5236 case CEE_CONV_OVF_I8_UN:
5237 case CEE_CONV_OVF_U8_UN:
5238 do_conversion (&ctx, TYPE_I8);
5242 case CEE_CONV_OVF_I_UN:
5243 case CEE_CONV_OVF_U_UN:
5244 do_conversion (&ctx, TYPE_NATIVE_INT);
5249 code_bounds_check (5);
5250 do_box_value (&ctx, read32 (ip + 1));
5255 code_bounds_check (5);
5256 do_newarr (&ctx, read32 (ip + 1));
5266 code_bounds_check (5);
5267 do_ldelema (&ctx, read32 (ip + 1));
5281 case CEE_LDELEM_REF:
5282 do_ldelem (&ctx, *ip, 0);
5293 case CEE_STELEM_REF:
5294 do_stelem (&ctx, *ip, 0);
5299 code_bounds_check (5);
5300 do_ldelem (&ctx, *ip, read32 (ip + 1));
5305 code_bounds_check (5);
5306 do_stelem (&ctx, *ip, read32 (ip + 1));
5311 code_bounds_check (5);
5312 do_unbox_any (&ctx, read32 (ip + 1));
5316 case CEE_CONV_OVF_I1:
5317 case CEE_CONV_OVF_U1:
5318 case CEE_CONV_OVF_I2:
5319 case CEE_CONV_OVF_U2:
5320 case CEE_CONV_OVF_I4:
5321 case CEE_CONV_OVF_U4:
5322 do_conversion (&ctx, TYPE_I4);
5326 case CEE_CONV_OVF_I8:
5327 case CEE_CONV_OVF_U8:
5328 do_conversion (&ctx, TYPE_I8);
5332 case CEE_CONV_OVF_I:
5333 case CEE_CONV_OVF_U:
5334 do_conversion (&ctx, TYPE_NATIVE_INT);
5339 code_bounds_check (5);
5340 do_refanyval (&ctx, read32 (ip + 1));
5350 code_bounds_check (5);
5351 do_mkrefany (&ctx, read32 (ip + 1));
5356 code_bounds_check (5);
5357 do_load_token (&ctx, read32 (ip + 1));
5361 case CEE_ENDFINALLY:
5362 if (!is_correct_endfinally (ctx.header, ip_offset))
5363 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5370 code_bounds_check (5);
5371 do_leave (&ctx, read32 (ip + 1) + 5);
5378 code_bounds_check (2);
5379 do_leave (&ctx, (signed char)ip [1] + 2);
5386 code_bounds_check (2);
5390 code_bounds_check (3);
5391 store_local (&ctx, read16 (ip + 1));
5396 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5404 do_cmp_op (&ctx, cmp_br_op, *ip);
5409 code_bounds_check (3);
5410 store_arg (&ctx, read16 (ip + 1) );
5416 if (!check_overflow (&ctx))
5418 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5419 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5420 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5425 code_bounds_check (5);
5426 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5431 code_bounds_check (5);
5432 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5438 code_bounds_check (3);
5439 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5445 code_bounds_check (3);
5446 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5459 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5463 do_endfilter (&ctx);
5467 case CEE_UNALIGNED_:
5468 code_bounds_check (2);
5469 prefix |= PREFIX_UNALIGNED;
5473 prefix |= PREFIX_VOLATILE;
5477 prefix |= PREFIX_TAIL;
5479 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5480 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5484 code_bounds_check (5);
5485 do_initobj (&ctx, read32 (ip + 1));
5489 case CEE_CONSTRAINED_:
5490 code_bounds_check (5);
5491 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5492 prefix |= PREFIX_CONSTRAINED;
5497 prefix |= PREFIX_READONLY;
5502 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5503 if (!check_underflow (&ctx, 3))
5505 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5510 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5511 if (!check_underflow (&ctx, 3))
5513 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5521 if (!is_correct_rethrow (ctx.header, ip_offset))
5522 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5529 code_bounds_check (5);
5530 do_sizeof (&ctx, read32 (ip + 1));
5534 case CEE_REFANYTYPE:
5535 do_refanytype (&ctx);
5540 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5546 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5550 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5552 if (!ctx.prefix_set) //first prefix
5553 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5554 ctx.prefix_set |= prefix;
5555 ctx.has_flags = TRUE;
5559 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5561 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5562 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5563 if (ctx.prefix_set & PREFIX_READONLY)
5564 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5565 if (ctx.prefix_set & PREFIX_VOLATILE)
5566 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5567 if (ctx.prefix_set & PREFIX_UNALIGNED)
5568 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5569 ctx.prefix_set = prefix = 0;
5570 ctx.has_flags = FALSE;
5574 * if ip != end we overflowed: mark as error.
5576 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5577 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5580 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5581 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5582 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5583 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5584 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5586 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5587 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5589 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5590 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5592 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5593 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));
5596 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5597 char *method_name = mono_method_full_name (ctx.method, TRUE);
5598 char *type = mono_type_get_full_name (ctx.method->klass);
5599 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5600 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));
5602 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5603 g_free (method_name);
5609 for (i = 0; i < ctx.header->code_size; ++i) {
5610 if (ctx.code [i].stack)
5611 g_free (ctx.code [i].stack);
5615 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5617 g_slist_free (ctx.funptrs);
5619 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5620 mono_metadata_free_type (tmp->data);
5621 g_slist_free (ctx.exception_types);
5623 for (i = 0; i < ctx.num_locals; ++i) {
5625 mono_metadata_free_type (ctx.locals [i]);
5627 for (i = 0; i < ctx.max_args; ++i) {
5629 mono_metadata_free_type (ctx.params [i]);
5633 g_free (ctx.eval.stack);
5636 g_free (ctx.locals);
5637 g_free (ctx.params);
5638 mono_basic_block_free (original_bb);
5639 mono_metadata_free_mh (ctx.header);
5641 finish_collect_stats ();
5646 mono_verify_corlib ()
5648 /* This is a public API function so cannot be removed */
5653 * Returns true if @method needs to be verified.
5657 mono_verifier_is_enabled_for_method (MonoMethod *method)
5659 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5663 * Returns true if @klass need to be verified.
5667 mono_verifier_is_enabled_for_class (MonoClass *klass)
5669 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
5673 mono_verifier_is_enabled_for_image (MonoImage *image)
5675 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
5679 mono_verifier_is_method_full_trust (MonoMethod *method)
5681 return mono_verifier_is_class_full_trust (method->klass);
5685 * Returns if @klass is under full trust or not.
5687 * TODO This code doesn't take CAS into account.
5689 * Under verify_all all user code must be verifiable if no security option was set
5693 mono_verifier_is_class_full_trust (MonoClass *klass)
5695 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5696 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
5697 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
5699 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5700 return trusted_location || klass->image == mono_defaults.corlib;
5701 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
5705 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5707 return mono_method_verify (method,
5708 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5709 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5710 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5714 get_field_end (MonoClassField *field)
5717 int size = mono_type_size (field->type, &align);
5719 size = 4; /*FIXME Is this a safe bet?*/
5720 return size + field->offset;
5724 verify_class_for_overlapping_reference_fields (MonoClass *class)
5727 gpointer iter = NULL;
5728 MonoClassField *field;
5729 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5730 /*We can't skip types with !has_references since this is calculated after we have run.*/
5731 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
5735 /*We must check for stuff overlapping reference fields.
5736 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
5738 while ((field = mono_class_get_fields (class, &iter))) {
5739 int fieldEnd = get_field_end (field);
5740 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5743 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5746 for (j = i; j < class->field.count; ++j) {
5747 MonoClassField *other = &class->fields [j];
5748 int otherEnd = get_field_end (other);
5749 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5752 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5755 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5763 field_hash (gconstpointer key)
5765 const MonoClassField *field = key;
5766 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
5770 field_equals (gconstpointer _a, gconstpointer _b)
5772 const MonoClassField *a = _a;
5773 const MonoClassField *b = _b;
5774 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
5779 verify_class_fields (MonoClass *class)
5781 gpointer iter = NULL;
5782 MonoClassField *field;
5783 MonoGenericContext *context = mono_class_get_context (class);
5784 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
5785 if (class->generic_container)
5786 context = &class->generic_container->context;
5788 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5789 if (!mono_type_is_valid_type_in_context (field->type, context)) {
5790 g_hash_table_destroy (unique_fields);
5793 if (g_hash_table_lookup (unique_fields, field)) {
5794 g_hash_table_destroy (unique_fields);
5797 g_hash_table_insert (unique_fields, field, field);
5799 g_hash_table_destroy (unique_fields);
5804 verify_interfaces (MonoClass *class)
5807 for (i = 0; i < class->interface_count; ++i) {
5808 MonoClass *iface = class->interfaces [i];
5809 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
5816 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
5819 gpointer iter = NULL;
5820 MonoClassField *field;
5821 MonoClass *field_class;
5823 if (!class->valuetype)
5826 type = class->byval_arg.type;
5827 /*primitive type fields are not properly decoded*/
5828 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
5831 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5835 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
5838 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
5840 if (field_class == target_class || class == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
5848 verify_valuetype_layout (MonoClass *class)
5851 res = verify_valuetype_layout_with_target (class, class);
5856 recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
5859 MonoClass **constraints;
5860 MonoGenericParamInfo *param_info;
5862 g_assert (mono_type_is_generic_argument (type));
5864 idx = mono_type_get_generic_param_num (type);
5865 if (mono_bitset_test_fast (used_args, idx))
5868 mono_bitset_set_fast (used_args, idx);
5869 param_info = mono_generic_container_get_param_info (gc, idx);
5871 if (!param_info->constraints)
5874 for (constraints = param_info->constraints; *constraints; ++constraints) {
5875 MonoClass *ctr = *constraints;
5876 MonoType *constraint_type = &ctr->byval_arg;
5878 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
5885 verify_generic_parameters (MonoClass *class)
5888 MonoGenericContainer *gc = class->generic_container;
5889 MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
5891 for (i = 0; i < gc->type_argc; ++i) {
5892 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
5893 MonoClass **constraints;
5895 if (!param_info->constraints)
5898 mono_bitset_clear_all (used_args);
5899 mono_bitset_set_fast (used_args, i);
5901 for (constraints = param_info->constraints; *constraints; ++constraints) {
5902 MonoClass *ctr = *constraints;
5903 MonoType *constraint_type = &ctr->byval_arg;
5905 if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
5908 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
5912 mono_bitset_free (used_args);
5916 mono_bitset_free (used_args);
5921 * Check if the class is verifiable.
5923 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5924 * field and invalid generic instantiation are fatal errors.
5926 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5930 mono_verifier_verify_class (MonoClass *class)
5932 /*Neither <Module>, object or ifaces have parent.*/
5933 if (!class->parent &&
5934 class != mono_defaults.object_class &&
5935 !MONO_CLASS_IS_INTERFACE (class) &&
5936 (!class->image->dynamic && class->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
5938 if (class->parent && MONO_CLASS_IS_INTERFACE (class->parent))
5940 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
5942 if (class->generic_container && !verify_generic_parameters (class))
5944 if (!verify_class_for_overlapping_reference_fields (class))
5946 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
5948 if (class->generic_class == NULL && !verify_class_fields (class))
5950 if (class->valuetype && !verify_valuetype_layout (class))
5952 if (!verify_interfaces (class))
5959 mono_verifier_verify_class (MonoClass *class)
5961 /* The verifier was disabled at compile time */
5966 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5968 /* The verifier was disabled at compile time */
5973 mono_verifier_is_class_full_trust (MonoClass *klass)
5975 /* The verifier was disabled at compile time */
5980 mono_verifier_is_method_full_trust (MonoMethod *method)
5982 /* The verifier was disabled at compile time */
5987 mono_verifier_is_enabled_for_image (MonoImage *image)
5989 /* The verifier was disabled at compile time */
5994 mono_verifier_is_enabled_for_class (MonoClass *klass)
5996 /* The verifier was disabled at compile time */
6001 mono_verifier_is_enabled_for_method (MonoMethod *method)
6003 /* The verifier was disabled at compile time */
6008 mono_method_verify (MonoMethod *method, int level)
6010 /* The verifier was disabled at compile time */
6015 mono_free_verify_list (GSList *list)
6017 /* The verifier was disabled at compile time */
6018 /* will always be null if verifier is disabled */