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)
11 #include <mono/metadata/object-internals.h>
12 #include <mono/metadata/verify.h>
13 #include <mono/metadata/verify-internals.h>
14 #include <mono/metadata/opcodes.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/reflection.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/mono-endian.h>
19 #include <mono/metadata/metadata.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/class-internals.h>
22 #include <mono/metadata/tokentype.h>
29 * Pull the list of opcodes
31 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
35 #include "mono/cil/opcode.def"
40 #ifdef MONO_VERIFIER_DEBUG
41 #define VERIFIER_DEBUG(code) do { code } while (0)
43 #define VERIFIER_DEBUG(code)
46 //////////////////////////////////////////////////////////////////
47 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
48 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
49 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
50 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
51 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
52 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
54 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
55 vinfo->info.status = __status; \
56 vinfo->info.message = ( __msg ); \
57 vinfo->exception_type = (__exception); \
58 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
61 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
62 #define ADD_VERIFY_ERROR(__ctx, __msg) \
64 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
68 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
70 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
71 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
72 (__ctx)->verifiable = 0; \
73 if (IS_FAIL_FAST_MODE (__ctx)) \
78 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
80 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
84 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
86 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
87 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
88 (__ctx)->verifiable = 0; \
89 if (IS_FAIL_FAST_MODE (__ctx)) \
93 /*Flags to be used with ILCodeDesc::flags */
95 /*Instruction has not been processed.*/
96 IL_CODE_FLAG_NOT_PROCESSED = 0,
97 /*Instruction was decoded by mono_method_verify loop.*/
98 IL_CODE_FLAG_SEEN = 1,
99 /*Instruction was target of a branch or is at a protected block boundary.*/
100 IL_CODE_FLAG_WAS_TARGET = 2,
101 /*Used by stack_init to avoid double initialize each entry.*/
102 IL_CODE_FLAG_STACK_INITED = 4,
103 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
104 IL_CODE_STACK_MERGED = 8,
105 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
106 IL_CODE_DELEGATE_SEQUENCE = 0x10,
107 /*This is a delegate created from a ldftn to a non final virtual method*/
108 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
109 /*This is a call to a non final virtual method*/
110 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
145 /*Allocated fnptr MonoType that should be freed by us.*/
147 /*Type dup'ed exception types from catch blocks.*/
148 GSList *exception_types;
153 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
157 MonoMethodSignature *signature;
158 MonoMethodHeader *header;
160 MonoGenericContext *generic_context;
164 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
165 *on a method that creates a delegate for a non-final virtual method using ldftn*/
166 gboolean has_this_store;
168 /*This flag is used to control if the contructor of the parent class has been called.
169 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
170 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
172 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
173 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
175 gboolean super_ctor_called;
179 MonoType *constrained_type;
183 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
186 get_stack_type (MonoType *type);
189 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
192 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
195 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
196 //////////////////////////////////////////////////////////////////
201 TYPE_INV = 0, /* leave at 0. */
206 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
208 /* value types and classes */
210 /* Number of types, used to define the size of the tables*/
213 /* Used by tables to signal that a result is not verifiable*/
214 NON_VERIFIABLE_RESULT = 0x80,
216 /*Mask used to extract just the type, excluding flags */
219 /* The stack type is a managed pointer, unmask the value to res */
220 POINTER_MASK = 0x100,
222 /*Stack type with the pointer mask*/
223 RAW_TYPE_MASK = 0x10F,
225 /* Controlled Mutability Manager Pointer */
228 /* The stack type is a null literal*/
229 NULL_LITERAL_MASK = 0x400,
231 /**Used by ldarg.0 and family to let delegate verification happens.*/
232 THIS_POINTER_MASK = 0x800,
234 /**Signals that this is a boxed value type*/
237 /*This is an unitialized this ref*/
238 UNINIT_THIS_MASK = 0x2000,
241 static const char* const
242 type_names [TYPE_MAX + 1] = {
253 PREFIX_UNALIGNED = 1,
256 PREFIX_CONSTRAINED = 8,
259 //////////////////////////////////////////////////////////////////
262 /*Token validation macros and functions */
263 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
264 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
265 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
266 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
268 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
269 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
270 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
271 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
272 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
273 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
276 * Verify if @token refers to a valid row on int's table.
279 token_bounds_check (MonoImage *image, guint32 token)
282 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
283 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
287 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
289 MonoType *res = g_new0 (MonoType, 1);
290 //FIXME use mono_method_get_signature_full
291 res->data.method = mono_method_signature (method);
292 res->type = MONO_TYPE_FNPTR;
293 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
298 * mono_type_is_enum_type:
300 * Returns TRUE if @type is an enum type.
303 mono_type_is_enum_type (MonoType *type)
305 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
307 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
313 * mono_type_is_value_type:
315 * Returns TRUE if @type is named after @namespace.@name.
319 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
321 return type->type == MONO_TYPE_VALUETYPE &&
322 !strcmp (namespace, type->data.klass->name_space) &&
323 !strcmp (name, type->data.klass->name);
327 * Returns TURE if @type is VAR or MVAR
330 mono_type_is_generic_argument (MonoType *type)
332 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
336 * mono_type_get_underlying_type_any:
338 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
340 * Returns the underlying type of @type regardless if it is byref or not.
343 mono_type_get_underlying_type_any (MonoType *type)
345 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
346 return mono_class_enum_basetype (type->data.klass);
347 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
348 return mono_class_enum_basetype (type->data.generic_class->container_class);
353 mono_type_get_stack_name (MonoType *type)
355 return type_names [get_stack_type (type) & TYPE_MASK];
358 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
359 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
362 mono_method_is_constructor (MonoMethod *method)
364 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
365 !(method->flags & CTOR_INVALID_FLAGS) &&
366 !strcmp (".ctor", method->name));
370 mono_class_has_default_constructor (MonoClass *klass)
375 mono_class_setup_methods (klass);
377 for (i = 0; i < klass->method.count; ++i) {
378 method = klass->methods [i];
379 if (mono_method_is_constructor (method) &&
380 mono_method_signature (method)->param_count == 0 &&
381 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
388 mono_class_interface_implements_interface (MonoClass *candidate, MonoClass *iface)
392 if (candidate == iface)
394 mono_class_setup_interfaces (candidate);
395 for (i = 0; i < candidate->interface_count; ++i) {
396 if (candidate->interfaces [i] == iface || mono_class_interface_implements_interface (candidate->interfaces [i], iface))
399 candidate = candidate->parent;
405 * Test if @candidate is a subtype of @target using the minimal possible information
406 * TODO move the code for non finished TypeBuilders to here.
409 mono_class_is_constraint_compatible (MonoClass *candidate, MonoClass *target)
411 if (candidate == target)
413 if (target == mono_defaults.object_class)
416 //setup_supertypes don't mono_class_init anything
417 mono_class_setup_supertypes (candidate);
418 mono_class_setup_supertypes (target);
420 if (mono_class_has_parent (candidate, target))
423 //if target is not a supertype it must be an interface
424 if (!MONO_CLASS_IS_INTERFACE (target))
427 if (candidate->image->dynamic && !candidate->wastypebuilder) {
428 MonoReflectionTypeBuilder *tb = candidate->reflection_info;
430 if (tb->interfaces) {
431 for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
432 MonoReflectionType *iface = mono_array_get (tb->interfaces, MonoReflectionType*, j);
433 MonoClass *ifaceClass = mono_class_from_mono_type (iface->type);
434 if (mono_class_is_constraint_compatible (ifaceClass, target)) {
441 return mono_class_interface_implements_interface (candidate, target);
445 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
449 if (ginst->type_argc != gc->type_argc)
452 for (i = 0; i < gc->type_argc; ++i) {
453 MonoGenericParam *param = &gc->type_params [i];
454 MonoClass *paramClass;
455 MonoClass **constraints;
457 if (!param->constraints && !(param->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
459 if (mono_type_is_generic_argument (ginst->type_argv [i]))
460 continue; //it's not our job to validate type variables
462 paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
464 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
467 /*it's not safe to call mono_class_init from here*/
468 if (paramClass->generic_class && !paramClass->inited) {
469 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
473 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
476 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
479 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
482 if (!param->constraints)
485 for (constraints = param->constraints; *constraints; ++constraints) {
486 MonoClass *ctr = *constraints;
489 inflated = mono_class_inflate_generic_type (&ctr->byval_arg, context);
490 ctr = mono_class_from_mono_type (inflated);
491 mono_metadata_free_type (inflated);
493 if (!mono_class_is_constraint_compatible (paramClass, ctr))
501 * Return true if @candidate is constraint compatible with @target.
503 * This means that @candidate constraints are a super set of @target constaints
506 mono_generic_param_is_constraint_compatible (MonoGenericParam *target, MonoGenericParam *candidate, MonoGenericContext *context)
508 int tmask = target->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
509 int cmask = candidate->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
510 if ((tmask & cmask) != tmask)
513 if (target->constraints) {
514 MonoClass **target_class, **candidate_class;
515 if (!candidate->constraints)
517 for (target_class = target->constraints; *target_class; ++target_class) {
518 MonoType *inflated = mono_class_inflate_generic_type (&(*target_class)->byval_arg, context);
519 MonoClass *tc = mono_class_from_mono_type (inflated);
520 mono_metadata_free_type (inflated);
522 for (candidate_class = candidate->constraints; *candidate_class; ++candidate_class) {
525 inflated = mono_class_inflate_generic_type (&(*candidate_class)->byval_arg, context);
526 cc = mono_class_from_mono_type (inflated);
527 mono_metadata_free_type (inflated);
529 if (mono_class_is_assignable_from (tc, cc))
532 if (!*candidate_class)
539 static MonoGenericParam*
540 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
542 MonoGenericContainer *gc;
543 MonoMethod *method = ctx->method;
546 num = type->data.generic_param->num;
548 if (type->type == MONO_TYPE_VAR) {
549 MonoClass *gtd = method->klass;
550 if (gtd->generic_class)
551 gtd = gtd->generic_class->container_class;
552 gc = gtd->generic_container;
554 MonoMethod *gmd = method;
555 if (method->is_inflated)
556 gmd = ((MonoMethodInflated*)method)->declaring;
557 gc = mono_method_get_generic_container (gmd);
561 return &gc->type_params [num];
567 * Verify if @type is valid for the given @ctx verification context.
568 * this function checks for VAR and MVAR types that are invalid under the current verifier,
569 * This means that it either
572 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
574 if (mono_type_is_generic_argument (type) && !ctx->generic_context)
576 if (type->type == MONO_TYPE_VAR) {
577 if (!ctx->generic_context->class_inst)
579 if (type->data.generic_param->num >= ctx->generic_context->class_inst->type_argc)
581 } else if (type->type == MONO_TYPE_MVAR) {
582 if (!ctx->generic_context->method_inst)
584 if (type->data.generic_param->num >= ctx->generic_context->method_inst->type_argc)
591 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
594 for (i = 0; i < ginst->type_argc; ++i) {
595 MonoType *type = ginst->type_argv [i];
596 if (!is_valid_type_in_context (ctx, type))
603 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
606 for (i = 0; i < ginst->type_argc; ++i) {
607 MonoType *type = ginst->type_argv [i];
608 MonoGenericParam *target = &gc->type_params [i];
609 MonoGenericParam *candidate;
611 if (!mono_type_is_generic_argument (type))
614 if (!is_valid_type_in_context (ctx, type))
617 candidate = verifier_get_generic_param_from_type (ctx, type);
619 if (!mono_generic_param_is_constraint_compatible (target, candidate, context))
626 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
628 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
629 MonoGenericInst *ginst = gmethod->context.method_inst;
630 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
631 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
635 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
637 MonoGenericClass *gklass = klass->generic_class;
638 MonoGenericInst *ginst = gklass->context.class_inst;
639 MonoGenericContainer *gc = gklass->container_class->generic_container;
640 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
644 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
646 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
647 MonoGenericInst *ginst = gmethod->context.method_inst;
648 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
649 if (!gc) /*non-generic inflated method - it's part of a generic type */
651 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
653 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
658 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
660 MonoGenericClass *gklass = klass->generic_class;
661 MonoGenericInst *ginst = gklass->context.class_inst;
662 MonoGenericContainer *gc = gklass->container_class->generic_container;
663 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
665 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
669 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
673 if (!is_valid_type_in_context (ctx, type)) {
674 char *str = mono_type_full_name (type);
675 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
676 type->type == MONO_TYPE_VAR ? "!" : "!!",
678 type->type == MONO_TYPE_VAR ? "class" : "method",
680 MONO_EXCEPTION_BAD_IMAGE);
685 klass = mono_class_from_mono_type (type);
686 mono_class_init (klass);
687 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
688 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
689 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);
691 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);
695 if (klass->exception_type != MONO_EXCEPTION_NONE || (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE)) {
696 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);
700 if (!klass->generic_class)
703 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
704 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);
708 if (!mono_class_repect_method_constraints (ctx, klass)) {
709 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);
716 static verify_result_t
717 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
719 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
720 return RESULT_INVALID;
722 if (!method->is_inflated)
725 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
726 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);
727 return RESULT_INVALID;
730 if (!mono_method_repect_method_constraints (ctx, method)) {
731 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));
732 return RESULT_UNVERIFIABLE;
738 static MonoClassField*
739 verifier_load_field (VerifyContext *ctx, int token, MonoClass **klass, const char *opcode) {
740 MonoClassField *field;
742 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
743 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid field token 0x%x08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
747 field = mono_field_from_token (ctx->image, token, klass, ctx->generic_context);
749 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);
753 if (!mono_type_is_valid_in_context (ctx, &field->parent->byval_arg))
760 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
763 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
764 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);
768 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
771 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);
775 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
782 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
785 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
786 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
790 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
793 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);
797 if (!mono_type_is_valid_in_context (ctx, type))
804 /* stack_slot_get_type:
806 * Returns the stack type of @value. This value includes POINTER_MASK.
808 * Use this function to checks that account for a managed pointer.
811 stack_slot_get_type (ILStackDesc *value)
813 return value->stype & RAW_TYPE_MASK;
816 /* stack_slot_get_underlying_type:
818 * Returns the stack type of @value. This value does not include POINTER_MASK.
820 * Use this function is cases where the fact that the value could be a managed pointer is
821 * irrelevant. For example, field load doesn't care about this fact of type on stack.
824 stack_slot_get_underlying_type (ILStackDesc *value)
826 return value->stype & TYPE_MASK;
829 /* stack_slot_is_managed_pointer:
831 * Returns TRUE is @value is a managed pointer.
834 stack_slot_is_managed_pointer (ILStackDesc *value)
836 return (value->stype & POINTER_MASK) == POINTER_MASK;
839 /* stack_slot_is_managed_mutability_pointer:
841 * Returns TRUE is @value is a managed mutability pointer.
843 static G_GNUC_UNUSED gboolean
844 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
846 return (value->stype & CMMP_MASK) == CMMP_MASK;
849 /* stack_slot_is_null_literal:
851 * Returns TRUE is @value is the null literal.
854 stack_slot_is_null_literal (ILStackDesc *value)
856 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
860 /* stack_slot_is_this_pointer:
862 * Returns TRUE is @value is the this literal
865 stack_slot_is_this_pointer (ILStackDesc *value)
867 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
870 /* stack_slot_is_boxed_value:
872 * Returns TRUE is @value is a boxed value
875 stack_slot_is_boxed_value (ILStackDesc *value)
877 return (value->stype & BOXED_MASK) == BOXED_MASK;
881 stack_slot_get_name (ILStackDesc *value)
883 return type_names [value->stype & TYPE_MASK];
886 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
887 if (PRED (value)) { \
889 g_string_append (str, ", "); \
890 g_string_append (str, NAME); \
895 stack_slot_stack_type_full_name (ILStackDesc *value)
897 GString *str = g_string_new ("");
900 if ((value->stype & TYPE_MASK) != value->stype) {
901 gboolean first = TRUE;
902 g_string_append(str, "[");
903 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
904 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
905 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
906 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
907 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
908 g_string_append(str, "] ");
911 g_string_append (str, stack_slot_get_name (value));
913 g_string_free (str, FALSE);
918 stack_slot_full_name (ILStackDesc *value)
920 char *type_name = mono_type_full_name (value->type);
921 char *stack_name = stack_slot_stack_type_full_name (value);
922 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
928 //////////////////////////////////////////////////////////////////
930 mono_free_verify_list (GSList *list)
932 MonoVerifyInfoExtended *info;
935 for (tmp = list; tmp; tmp = tmp->next) {
937 g_free (info->info.message);
943 #define ADD_ERROR(list,msg) \
945 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
946 vinfo->info.status = MONO_VERIFY_ERROR; \
947 vinfo->info.message = (msg); \
948 (list) = g_slist_prepend ((list), vinfo); \
951 #define ADD_WARN(list,code,msg) \
953 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
954 vinfo->info.status = (code); \
955 vinfo->info.message = (msg); \
956 (list) = g_slist_prepend ((list), vinfo); \
960 valid_cultures[][9] = {
961 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
962 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
963 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
964 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
965 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
966 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
967 "da-DK", "de-DE", "de-CH", "de-AT",
968 "de-LU", "de-LI", "el-GR", "en-US",
969 "en-GB", "en-AU", "en-CA", "en-NZ",
970 "en-IE", "en-ZA", "en-JM", "en-CB",
971 "en-BZ", "en-TT", "en-ZW", "en-PH",
972 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
973 "es-CR", "es-PA", "es-DO", "es-VE",
974 "es-CO", "es-PE", "es-AR", "es-EC",
975 "es-CL", "es-UY", "es-PY", "es-BO",
976 "es-SV", "es-HN", "es-NI", "es-PR",
977 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
978 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
979 "hu-HU", "is-IS", "it-IT", "it-CH",
980 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
981 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
982 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
983 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
984 "sv-SE", "sv-FI", "th-TH", "tr-TR",
985 "ur-PK", "id-ID", "uk-UA", "be-BY",
986 "sl-SI", "et-EE", "lv-LV", "lt-LT",
987 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
989 "eu-ES", "mk-MK", "af-ZA",
990 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
991 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
992 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
993 "gu-IN", "ta-IN", "te-IN", "kn-IN",
994 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
995 "kok-IN", "syr-SY", "div-MV"
999 is_valid_culture (const char *cname)
1004 found = *cname == 0;
1005 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
1006 if (g_strcasecmp (valid_cultures [i], cname)) {
1015 is_valid_assembly_flags (guint32 flags) {
1016 /* Metadata: 22.1.2 */
1017 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
1018 return ((flags == 1) || (flags == 0));
1022 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
1025 const char *p, *blob_end;
1027 if (blob_index >= image->heap_blob.size)
1029 p = mono_metadata_blob_heap (image, blob_index);
1030 size = mono_metadata_decode_blob_size (p, &blob_end);
1031 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
1033 if (notnull && !size)
1039 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
1041 const char *p, *blob_end, *res;
1043 if (str_index >= image->heap_strings.size)
1045 res = p = mono_metadata_string_heap (image, str_index);
1046 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
1050 * FIXME: should check it's a valid utf8 string, too.
1052 while (p <= blob_end) {
1057 return *p? NULL: res;
1061 is_valid_cls_ident (const char *p)
1064 * FIXME: we need the full unicode glib support for this.
1065 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
1066 * We do the lame thing for now.
1072 if (!isalnum (*p) && *p != '_')
1080 is_valid_filename (const char *p)
1084 return strpbrk (p, "\\//:")? 0: 1;
1088 verify_assembly_table (MonoImage *image, GSList *list, int level)
1090 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
1091 guint32 cols [MONO_ASSEMBLY_SIZE];
1094 if (level & MONO_VERIFY_ERROR) {
1096 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
1097 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
1099 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
1100 case ASSEMBLY_HASH_NONE:
1101 case ASSEMBLY_HASH_MD5:
1102 case ASSEMBLY_HASH_SHA1:
1105 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
1108 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
1109 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
1111 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
1112 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
1114 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
1115 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
1117 if (strpbrk (p, ":\\/."))
1118 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
1121 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
1122 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
1124 if (!is_valid_culture (p))
1125 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
1132 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
1134 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1135 guint32 cols [MONO_ASSEMBLYREF_SIZE];
1139 if (level & MONO_VERIFY_ERROR) {
1140 for (i = 0; i < t->rows; ++i) {
1141 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
1142 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
1143 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
1145 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
1146 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
1148 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
1149 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
1151 if (!is_valid_culture (p))
1152 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
1155 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
1156 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
1159 if (level & MONO_VERIFY_WARNING) {
1160 /* check for duplicated rows */
1161 for (i = 0; i < t->rows; ++i) {
1168 verify_class_layout_table (MonoImage *image, GSList *list, int level)
1170 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
1171 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
1172 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
1175 if (level & MONO_VERIFY_ERROR) {
1176 for (i = 0; i < t->rows; ++i) {
1177 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
1179 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
1180 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
1182 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
1183 if (value & TYPE_ATTRIBUTE_INTERFACE)
1184 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
1185 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
1186 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
1187 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
1188 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
1189 case 0: case 1: case 2: case 4: case 8: case 16:
1190 case 32: case 64: case 128: break;
1192 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
1194 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1196 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
1197 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
1198 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid with explicit layout", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
1202 * FIXME: we need to check that if class size != 0,
1203 * it needs to be greater than the class calculated size.
1204 * If parent is a valuetype it also needs to be smaller than
1205 * 1 MByte (0x100000 bytes).
1206 * To do both these checks we need to load the referenced
1207 * assemblies, though (the spec claims we didn't have to, bah).
1210 * We need to check that the parent types have the same layout
1221 verify_constant_table (MonoImage *image, GSList *list, int level)
1223 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
1224 guint32 cols [MONO_CONSTANT_SIZE];
1226 GHashTable *dups = g_hash_table_new (NULL, NULL);
1228 for (i = 0; i < t->rows; ++i) {
1229 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
1231 if (level & MONO_VERIFY_ERROR)
1232 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
1233 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
1234 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
1235 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
1237 switch (cols [MONO_CONSTANT_TYPE]) {
1238 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
1242 if (level & MONO_VERIFY_CLS)
1243 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Type 0x%x not CLS compliant in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
1244 case MONO_TYPE_BOOLEAN:
1245 case MONO_TYPE_CHAR:
1252 case MONO_TYPE_STRING:
1253 case MONO_TYPE_CLASS:
1256 if (level & MONO_VERIFY_ERROR)
1257 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
1259 if (level & MONO_VERIFY_ERROR) {
1260 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
1261 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
1262 case MONO_HASCONSTANT_FIEDDEF:
1263 if (value > image->tables [MONO_TABLE_FIELD].rows)
1264 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
1266 case MONO_HASCONSTANT_PARAM:
1267 if (value > image->tables [MONO_TABLE_PARAM].rows)
1268 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
1270 case MONO_HASCONSTANT_PROPERTY:
1271 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
1272 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
1275 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
1279 if (level & MONO_VERIFY_CLS) {
1281 * FIXME: verify types is consistent with the enum type
1282 * is parent is an enum.
1286 g_hash_table_destroy (dups);
1291 verify_event_map_table (MonoImage *image, GSList *list, int level)
1293 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
1294 guint32 cols [MONO_EVENT_MAP_SIZE];
1295 guint32 i, last_event;
1296 GHashTable *dups = g_hash_table_new (NULL, NULL);
1300 for (i = 0; i < t->rows; ++i) {
1301 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
1302 if (level & MONO_VERIFY_ERROR)
1303 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
1304 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1305 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
1306 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
1307 if (level & MONO_VERIFY_ERROR) {
1308 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
1309 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1310 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
1311 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
1313 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
1314 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
1315 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
1319 g_hash_table_destroy (dups);
1324 verify_event_table (MonoImage *image, GSList *list, int level)
1326 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
1327 guint32 cols [MONO_EVENT_SIZE];
1331 for (i = 0; i < t->rows; ++i) {
1332 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
1334 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
1335 if (level & MONO_VERIFY_ERROR)
1336 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
1338 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
1339 if (level & MONO_VERIFY_ERROR)
1340 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
1342 if (level & MONO_VERIFY_CLS) {
1343 if (!is_valid_cls_ident (p))
1344 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
1348 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
1349 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
1350 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
1351 case MONO_TYPEDEFORREF_TYPEDEF:
1352 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
1353 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1355 case MONO_TYPEDEFORREF_TYPEREF:
1356 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
1357 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1359 case MONO_TYPEDEFORREF_TYPESPEC:
1360 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
1361 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1364 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1368 * FIXME: check that there is 1 add and remove row in methodsemantics
1369 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
1370 * these while checking methodsemantics).
1371 * check for duplicated names for the same type [ERROR]
1372 * check for CLS duplicate names for the same type [CLS]
1379 verify_field_table (MonoImage *image, GSList *list, int level)
1381 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
1382 guint32 cols [MONO_FIELD_SIZE];
1386 for (i = 0; i < t->rows; ++i) {
1387 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
1389 * Check this field has only one owner and that the owner is not
1390 * an interface (done in verify_typedef_table() )
1392 flags = cols [MONO_FIELD_FLAGS];
1393 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
1394 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
1395 case FIELD_ATTRIBUTE_PRIVATE:
1396 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
1397 case FIELD_ATTRIBUTE_ASSEMBLY:
1398 case FIELD_ATTRIBUTE_FAMILY:
1399 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
1400 case FIELD_ATTRIBUTE_PUBLIC:
1403 if (level & MONO_VERIFY_ERROR)
1404 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
1407 if (level & MONO_VERIFY_ERROR) {
1408 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
1409 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
1410 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
1411 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
1412 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
1413 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
1415 * FIXME: check there is only one owner in the respective table.
1416 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
1417 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
1418 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
1421 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
1422 if (level & MONO_VERIFY_ERROR)
1423 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
1425 if (level & MONO_VERIFY_CLS) {
1426 if (!is_valid_cls_ident (p))
1427 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
1432 * if owner is module needs to be static, access mask needs to be compilercontrolled,
1433 * public or private (not allowed in cls mode).
1434 * if owner is an enum ...
1443 verify_file_table (MonoImage *image, GSList *list, int level)
1445 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1446 guint32 cols [MONO_FILE_SIZE];
1449 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1451 for (i = 0; i < t->rows; ++i) {
1452 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
1453 if (level & MONO_VERIFY_ERROR) {
1454 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
1455 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
1456 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
1457 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
1459 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
1460 if (level & MONO_VERIFY_ERROR)
1461 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
1463 if (level & MONO_VERIFY_ERROR) {
1464 if (!is_valid_filename (p))
1465 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
1466 else if (g_hash_table_lookup (dups, p)) {
1467 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
1469 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1473 * FIXME: I don't understand what this means:
1474 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
1475 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
1479 if (level & MONO_VERIFY_WARNING) {
1480 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
1481 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
1483 g_hash_table_destroy (dups);
1488 verify_moduleref_table (MonoImage *image, GSList *list, int level)
1490 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
1491 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
1492 guint32 cols [MONO_MODULEREF_SIZE];
1494 guint32 found, i, j, value;
1495 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1497 for (i = 0; i < t->rows; ++i) {
1498 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
1499 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
1500 if (level & MONO_VERIFY_ERROR)
1501 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
1503 if (level & MONO_VERIFY_ERROR) {
1504 if (!is_valid_filename (p))
1505 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
1506 else if (g_hash_table_lookup (dups, p)) {
1507 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
1508 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1510 for (j = 0; j < tfile->rows; ++j) {
1511 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
1512 if ((pf = is_valid_string (image, value, TRUE)))
1513 if (strcmp (p, pf) == 0) {
1519 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
1524 g_hash_table_destroy (dups);
1529 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
1531 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
1532 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1536 for (i = 0; i < t->rows; ++i) {
1537 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1538 if (level & MONO_VERIFY_ERROR) {
1539 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
1540 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
1542 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
1543 /* FIXME: check it's a valid locals or method sig.*/
1551 mono_image_verify_tables (MonoImage *image, int level)
1553 GSList *error_list = NULL;
1555 error_list = verify_assembly_table (image, error_list, level);
1557 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
1558 * AssemblyRefProcessor should be ignored,
1559 * though we may want to emit a warning, since it should not
1560 * be present in a PE file.
1562 error_list = verify_assemblyref_table (image, error_list, level);
1563 error_list = verify_class_layout_table (image, error_list, level);
1564 error_list = verify_constant_table (image, error_list, level);
1566 * cutom attribute, declsecurity
1568 error_list = verify_event_map_table (image, error_list, level);
1569 error_list = verify_event_table (image, error_list, level);
1570 error_list = verify_field_table (image, error_list, level);
1571 error_list = verify_file_table (image, error_list, level);
1572 error_list = verify_moduleref_table (image, error_list, level);
1573 error_list = verify_standalonesig_table (image, error_list, level);
1575 return g_slist_reverse (error_list);
1578 #define ADD_INVALID(list,msg) \
1580 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1581 vinfo->status = MONO_VERIFY_ERROR; \
1582 vinfo->message = (msg); \
1583 (list) = g_slist_prepend ((list), vinfo); \
1584 /*G_BREAKPOINT ();*/ \
1588 #define CHECK_STACK_UNDERFLOW(num) \
1590 if (cur_stack < (num)) \
1591 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1594 #define CHECK_STACK_OVERFLOW() \
1596 if (cur_stack >= max_stack) \
1597 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1602 in_any_block (MonoMethodHeader *header, guint offset)
1605 MonoExceptionClause *clause;
1607 for (i = 0; i < header->num_clauses; ++i) {
1608 clause = &header->clauses [i];
1609 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1611 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1613 if (MONO_OFFSET_IN_FILTER (clause, offset))
1620 * in_any_exception_block:
1622 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1625 in_any_exception_block (MonoMethodHeader *header, guint offset)
1628 MonoExceptionClause *clause;
1630 for (i = 0; i < header->num_clauses; ++i) {
1631 clause = &header->clauses [i];
1632 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1634 if (MONO_OFFSET_IN_FILTER (clause, offset))
1641 * is_valid_branch_instruction:
1643 * Verify if it's valid to perform a branch from @offset to @target.
1644 * This should be used with br and brtrue/false.
1645 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1646 * The major diferent from other similiar functions is that branching into a
1647 * finally/fault block is invalid instead of just unverifiable.
1650 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1653 MonoExceptionClause *clause;
1655 for (i = 0; i < header->num_clauses; ++i) {
1656 clause = &header->clauses [i];
1657 /*branching into a finally block is invalid*/
1658 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1659 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1660 MONO_OFFSET_IN_HANDLER (clause, target))
1663 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1665 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1667 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1674 * is_valid_cmp_branch_instruction:
1676 * Verify if it's valid to perform a branch from @offset to @target.
1677 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1678 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1680 * The major diferences from other similar functions are that most errors lead to invalid
1681 * code and only branching out of finally, filter or fault clauses is unverifiable.
1684 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1687 MonoExceptionClause *clause;
1689 for (i = 0; i < header->num_clauses; ++i) {
1690 clause = &header->clauses [i];
1691 /*branching out of a handler or finally*/
1692 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1693 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1694 !MONO_OFFSET_IN_HANDLER (clause, target))
1697 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1699 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1701 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1708 * A leave can't escape a finally block
1711 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1714 MonoExceptionClause *clause;
1716 for (i = 0; i < header->num_clauses; ++i) {
1717 clause = &header->clauses [i];
1718 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1720 if (MONO_OFFSET_IN_FILTER (clause, offset))
1727 * A rethrow can't happen outside of a catch handler.
1730 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1733 MonoExceptionClause *clause;
1735 for (i = 0; i < header->num_clauses; ++i) {
1736 clause = &header->clauses [i];
1737 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1739 if (MONO_OFFSET_IN_FILTER (clause, offset))
1746 * An endfinally can't happen outside of a finally/fault handler.
1749 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1752 MonoExceptionClause *clause;
1754 for (i = 0; i < header->num_clauses; ++i) {
1755 clause = &header->clauses [i];
1756 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1764 * An endfilter can only happens inside a filter clause.
1765 * In non-strict mode filter is allowed inside the handler clause too
1767 static MonoExceptionClause *
1768 is_correct_endfilter (VerifyContext *ctx, guint offset)
1771 MonoExceptionClause *clause;
1773 for (i = 0; i < ctx->header->num_clauses; ++i) {
1774 clause = &ctx->header->clauses [i];
1775 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1777 if (MONO_OFFSET_IN_FILTER (clause, offset))
1779 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1787 * Non-strict endfilter can happens inside a try block or any handler block
1790 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1793 MonoExceptionClause *clause;
1795 for (i = 0; i < ctx->header->num_clauses; ++i) {
1796 clause = &ctx->header->clauses [i];
1797 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1804 is_valid_bool_arg (ILStackDesc *arg)
1806 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1810 switch (stack_slot_get_underlying_type (arg)) {
1813 case TYPE_NATIVE_INT:
1817 g_assert (arg->type);
1818 switch (arg->type->type) {
1819 case MONO_TYPE_CLASS:
1820 case MONO_TYPE_STRING:
1821 case MONO_TYPE_OBJECT:
1822 case MONO_TYPE_SZARRAY:
1823 case MONO_TYPE_ARRAY:
1824 case MONO_TYPE_FNPTR:
1827 case MONO_TYPE_GENERICINST:
1828 /*We need to check if the container class
1829 * of the generic type is a valuetype, iow:
1830 * is it a "class Foo<T>" or a "struct Foo<T>"?
1832 return !arg->type->data.generic_class->container_class->valuetype;
1840 /*Type manipulation helper*/
1842 /*Returns the byref version of the supplied MonoType*/
1844 mono_type_get_type_byref (MonoType *type)
1848 return &mono_class_from_mono_type (type)->this_arg;
1852 /*Returns the byval version of the supplied MonoType*/
1854 mono_type_get_type_byval (MonoType *type)
1858 return &mono_class_from_mono_type (type)->byval_arg;
1862 mono_type_from_stack_slot (ILStackDesc *slot)
1864 if (stack_slot_is_managed_pointer (slot))
1865 return mono_type_get_type_byref (slot->type);
1869 /*Stack manipulation code*/
1872 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1874 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1877 state->flags |= IL_CODE_FLAG_STACK_INITED;
1879 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1883 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1885 to->size = from->size;
1886 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1890 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1892 to->stype = from->stype;
1893 to->type = from->type;
1894 to->method = from->method;
1898 check_underflow (VerifyContext *ctx, int size)
1900 if (ctx->eval.size < size) {
1901 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1908 check_overflow (VerifyContext *ctx)
1910 if (ctx->eval.size >= ctx->max_stack) {
1911 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1917 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1919 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1921 if (stack_slot_get_type (value) == TYPE_PTR) {
1922 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1928 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1930 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1932 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1933 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1940 static ILStackDesc *
1941 stack_push (VerifyContext *ctx)
1943 return & ctx->eval.stack [ctx->eval.size++];
1946 static ILStackDesc *
1947 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1949 ILStackDesc *top = stack_push (ctx);
1955 static ILStackDesc *
1956 stack_pop (VerifyContext *ctx)
1958 ILStackDesc *ret = ctx->eval.stack + --ctx->eval.size;
1959 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1960 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1964 /* This function allows to safely pop an unititialized this ptr from
1965 * the eval stack without marking the method as unverifiable.
1967 static ILStackDesc *
1968 stack_pop_safe (VerifyContext *ctx)
1970 return ctx->eval.stack + --ctx->eval.size;
1973 static ILStackDesc *
1974 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1976 ILStackDesc *top = stack_push (ctx);
1977 copy_stack_value (top, value);
1981 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1983 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1986 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1991 if (!(type = verifier_load_type (ctx, token, opcode)))
1994 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1995 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1999 if (type->type == MONO_TYPE_VOID) {
2000 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
2004 if (type->type == MONO_TYPE_TYPEDBYREF)
2005 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
2007 check_unverifiable_type (ctx, type);
2012 /*operation result tables */
2014 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
2015 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2016 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2017 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2018 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2019 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2020 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2023 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
2024 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2025 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2026 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2027 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2028 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
2029 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2032 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
2033 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2034 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2035 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2036 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2037 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
2038 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2041 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
2042 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2043 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2044 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2045 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2046 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2047 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2050 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
2051 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2052 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
2053 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2054 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2055 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2056 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2059 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
2060 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2061 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2062 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2063 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
2064 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
2065 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2068 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
2069 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2070 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2071 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
2072 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
2073 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
2074 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
2077 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
2078 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2079 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2080 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2081 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2082 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
2083 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2086 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
2087 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2088 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2089 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2090 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2091 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
2092 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2095 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
2096 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2097 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2098 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2099 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2100 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2101 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2104 #ifdef MONO_VERIFIER_DEBUG
2108 dump_stack_value (ILStackDesc *value)
2110 printf ("[(%x)(%x)", value->type->type, value->stype);
2112 if (stack_slot_is_this_pointer (value))
2115 if (stack_slot_is_boxed_value (value))
2116 printf ("[boxed] ");
2118 if (stack_slot_is_null_literal (value))
2121 if (stack_slot_is_managed_mutability_pointer (value))
2122 printf ("Controled Mutability MP: ");
2124 if (stack_slot_is_managed_pointer (value))
2125 printf ("Managed Pointer to: ");
2127 switch (stack_slot_get_underlying_type (value)) {
2129 printf ("invalid type]");
2137 case TYPE_NATIVE_INT:
2138 printf ("native int]");
2141 printf ("float64]");
2144 printf ("unmanaged pointer]");
2147 switch (value->type->type) {
2148 case MONO_TYPE_CLASS:
2149 case MONO_TYPE_VALUETYPE:
2150 printf ("complex] (%s)", value->type->data.klass->name);
2152 case MONO_TYPE_STRING:
2153 printf ("complex] (string)");
2155 case MONO_TYPE_OBJECT:
2156 printf ("complex] (object)");
2158 case MONO_TYPE_SZARRAY:
2159 printf ("complex] (%s [])", value->type->data.klass->name);
2161 case MONO_TYPE_ARRAY:
2162 printf ("complex] (%s [%d %d %d])",
2163 value->type->data.array->eklass->name,
2164 value->type->data.array->rank,
2165 value->type->data.array->numsizes,
2166 value->type->data.array->numlobounds);
2168 case MONO_TYPE_GENERICINST:
2169 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
2172 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
2174 case MONO_TYPE_MVAR:
2175 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
2178 //should be a boxed value
2179 char * name = mono_type_full_name (value->type);
2180 printf ("complex] %s", name);
2186 printf ("unknown stack %x type]\n", value->stype);
2187 g_assert_not_reached ();
2192 dump_stack_state (ILCodeDesc *state)
2196 printf ("(%d) ", state->size);
2197 for (i = 0; i < state->size; ++i)
2198 dump_stack_value (state->stack + i);
2203 /*Returns TRUE if candidate array type can be assigned to target.
2204 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
2207 is_array_type_compatible (MonoType *target, MonoType *candidate)
2209 MonoArrayType *left = target->data.array;
2210 MonoArrayType *right = candidate->data.array;
2212 g_assert (target->type == MONO_TYPE_ARRAY);
2213 g_assert (candidate->type == MONO_TYPE_ARRAY);
2215 if (left->rank != right->rank)
2218 return mono_class_is_assignable_from (left->eklass, right->eklass);
2222 get_stack_type (MonoType *type)
2225 int type_kind = type->type;
2227 mask = POINTER_MASK;
2228 /*TODO handle CMMP_MASK */
2231 switch (type_kind) {
2234 case MONO_TYPE_BOOLEAN:
2237 case MONO_TYPE_CHAR:
2240 return TYPE_I4 | mask;
2244 return TYPE_NATIVE_INT | mask;
2246 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
2247 case MONO_TYPE_FNPTR:
2249 case MONO_TYPE_TYPEDBYREF:
2250 return TYPE_PTR | mask;
2253 case MONO_TYPE_MVAR:
2255 case MONO_TYPE_CLASS:
2256 case MONO_TYPE_STRING:
2257 case MONO_TYPE_OBJECT:
2258 case MONO_TYPE_SZARRAY:
2259 case MONO_TYPE_ARRAY:
2260 return TYPE_COMPLEX | mask;
2262 case MONO_TYPE_GENERICINST:
2263 if (mono_type_is_enum_type (type)) {
2264 type = mono_type_get_underlying_type_any (type);
2265 type_kind = type->type;
2268 return TYPE_COMPLEX | mask;
2273 return TYPE_I8 | mask;
2277 return TYPE_R8 | mask;
2279 case MONO_TYPE_VALUETYPE:
2280 if (mono_type_is_enum_type (type)) {
2281 type = mono_type_get_underlying_type_any (type);
2282 type_kind = type->type;
2285 return TYPE_COMPLEX | mask;
2289 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
2290 g_assert_not_reached ();
2295 /* convert MonoType to ILStackDesc format (stype) */
2297 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
2300 int type_kind = type->type;
2302 if (type->byref || take_addr)
2303 mask = POINTER_MASK;
2304 /* TODO handle CMMP_MASK */
2309 switch (type_kind) {
2312 case MONO_TYPE_BOOLEAN:
2315 case MONO_TYPE_CHAR:
2318 stack->stype = TYPE_I4 | mask;
2322 stack->stype = TYPE_NATIVE_INT | mask;
2325 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2326 case MONO_TYPE_FNPTR:
2328 case MONO_TYPE_TYPEDBYREF:
2329 stack->stype = TYPE_PTR | mask;
2332 case MONO_TYPE_CLASS:
2333 case MONO_TYPE_STRING:
2334 case MONO_TYPE_OBJECT:
2335 case MONO_TYPE_SZARRAY:
2336 case MONO_TYPE_ARRAY:
2339 case MONO_TYPE_MVAR:
2340 stack->stype = TYPE_COMPLEX | mask;
2343 case MONO_TYPE_GENERICINST:
2344 if (mono_type_is_enum_type (type)) {
2345 type = mono_type_get_underlying_type_any (type);
2346 type_kind = type->type;
2349 stack->stype = TYPE_COMPLEX | mask;
2355 stack->stype = TYPE_I8 | mask;
2359 stack->stype = TYPE_R8 | mask;
2361 case MONO_TYPE_VALUETYPE:
2362 if (mono_type_is_enum_type (type)) {
2363 type = mono_type_get_underlying_type_any (type);
2364 type_kind = type->type;
2367 stack->stype = TYPE_COMPLEX | mask;
2371 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
2372 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
2379 * init_stack_with_value_at_exception_boundary:
2381 * Initialize the stack and push a given type.
2382 * The instruction is marked as been on the exception boundary.
2385 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
2387 MonoType *type = mono_class_inflate_generic_type (&klass->byval_arg, ctx->generic_context);
2388 stack_init (ctx, code);
2389 set_stack_value (ctx, code->stack, type, FALSE);
2390 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
2392 code->flags |= IL_CODE_FLAG_WAS_TARGET;
2395 /*Verify if type 'candidate' can be stored in type 'target'.
2397 * If strict, check for the underlying type and not the verification stack types
2400 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
2402 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2403 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2405 MonoType *original_candidate = candidate;
2406 VERIFIER_DEBUG ( printf ("checking type compatibility %p %p[%x][%x] %p[%x][%x]\n", ctx, target, target->type, target->byref, candidate, candidate->type, candidate->byref); );
2408 /*only one is byref */
2409 if (candidate->byref ^ target->byref) {
2410 /* converting from native int to byref*/
2411 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2412 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2417 strict |= target->byref;
2418 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2419 candidate = mono_type_get_underlying_type_any (candidate);
2422 switch (target->type) {
2423 case MONO_TYPE_VOID:
2424 return candidate->type == MONO_TYPE_VOID;
2427 case MONO_TYPE_BOOLEAN:
2429 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2432 case MONO_TYPE_CHAR:
2434 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2436 case MONO_TYPE_U4: {
2437 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2438 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2440 return is_native_int || is_int4;
2441 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2446 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2451 return candidate->type == target->type;
2452 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2456 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2457 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2459 return is_native_int || is_int4;
2460 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2464 if (candidate->type != MONO_TYPE_PTR)
2466 /* check the underlying type */
2467 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2469 case MONO_TYPE_FNPTR: {
2470 MonoMethodSignature *left, *right;
2471 if (candidate->type != MONO_TYPE_FNPTR)
2474 left = mono_type_get_signature (target);
2475 right = mono_type_get_signature (candidate);
2476 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2479 case MONO_TYPE_GENERICINST: {
2480 MonoClass *target_klass;
2481 MonoClass *candidate_klass;
2482 if (mono_type_is_enum_type (target)) {
2483 target = mono_type_get_underlying_type_any (target);
2486 target_klass = mono_class_from_mono_type (target);
2487 candidate_klass = mono_class_from_mono_type (candidate);
2488 if (mono_class_is_nullable (target_klass)) {
2489 if (!mono_class_is_nullable (candidate_klass))
2491 return target_klass == candidate_klass;
2494 return mono_class_is_assignable_from (target_klass, candidate_klass);
2497 case MONO_TYPE_STRING:
2498 return candidate->type == MONO_TYPE_STRING;
2500 case MONO_TYPE_CLASS:
2502 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2503 * to take boxing status into account.
2505 if (mono_type_is_generic_argument (original_candidate))
2507 /* If candidate is an enum it should return true for System.Enum and supertypes.
2508 * That's why here we use the original type and not the underlying type.
2510 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2512 case MONO_TYPE_OBJECT:
2513 return MONO_TYPE_IS_REFERENCE (candidate);
2515 case MONO_TYPE_SZARRAY: {
2518 if (candidate->type != MONO_TYPE_SZARRAY)
2521 left = target->data.array->eklass;
2522 right = candidate->data.array->eklass;
2523 return mono_class_is_assignable_from(left, right);
2526 case MONO_TYPE_ARRAY:
2527 if (candidate->type != MONO_TYPE_ARRAY)
2529 return is_array_type_compatible (target, candidate);
2531 case MONO_TYPE_TYPEDBYREF:
2532 return candidate->type == MONO_TYPE_TYPEDBYREF;
2534 case MONO_TYPE_VALUETYPE:
2535 if (candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass)
2537 if (mono_type_is_enum_type (target)) {
2538 target = mono_type_get_underlying_type_any (target);
2544 if (candidate->type != MONO_TYPE_VAR)
2546 return candidate->data.generic_param->num == target->data.generic_param->num;
2548 case MONO_TYPE_MVAR:
2549 if (candidate->type != MONO_TYPE_MVAR)
2551 return candidate->data.generic_param->num == target->data.generic_param->num;
2554 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2555 g_assert_not_reached ();
2564 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2566 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2570 * Returns the generic param bound to the context been verified.
2573 static MonoGenericParam*
2574 get_generic_param (VerifyContext *ctx, MonoType *param)
2576 guint16 param_num = param->data.generic_param->num;
2577 if (param->type == MONO_TYPE_VAR) {
2578 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2579 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2582 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2585 /*param must be a MVAR */
2586 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2587 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2590 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2594 * is_compatible_boxed_valuetype:
2596 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2598 * @type The source type. It it tested to be of the proper type.
2599 * @candidate type of the boxed valuetype.
2600 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2601 * @strict if TRUE candidate must be boxed compatible to the target type
2605 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2607 if (!stack_slot_is_boxed_value (stack))
2609 if (type->byref || candidate->byref)
2612 if (mono_type_is_generic_argument (candidate)) {
2613 MonoGenericParam *param = get_generic_param (ctx, candidate);
2615 for (class = param->constraints; class && *class; ++class) {
2616 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (& (*class)->byval_arg), FALSE))
2621 if (mono_type_is_generic_argument (type))
2627 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2631 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2633 MonoType *candidate = mono_type_from_stack_slot (stack);
2634 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2637 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2640 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2643 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2647 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2649 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2653 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2655 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2659 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2661 if (candidate->byref ^ target->byref)
2664 switch (target->type) {
2665 case MONO_TYPE_VOID:
2668 case MONO_TYPE_BOOLEAN:
2671 case MONO_TYPE_CHAR:
2680 case MONO_TYPE_STRING:
2681 case MONO_TYPE_TYPEDBYREF:
2682 return candidate->type == target->type;
2685 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2687 case MONO_TYPE_FNPTR:
2688 if (candidate->type != MONO_TYPE_FNPTR)
2690 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2692 case MONO_TYPE_GENERICINST: {
2693 MonoClass *target_klass;
2694 MonoClass *candidate_klass;
2695 target_klass = mono_class_from_mono_type (target);
2696 candidate_klass = mono_class_from_mono_type (candidate);
2697 /*FIXME handle nullables and enum*/
2698 return mono_class_is_assignable_from (target_klass, candidate_klass);
2700 case MONO_TYPE_OBJECT:
2701 return MONO_TYPE_IS_REFERENCE (candidate);
2703 case MONO_TYPE_CLASS:
2704 if (candidate->type != MONO_TYPE_CLASS)
2706 return mono_class_is_assignable_from(target->data.klass, candidate->data.klass);
2708 case MONO_TYPE_SZARRAY:
2709 if (candidate->type != MONO_TYPE_SZARRAY)
2711 return mono_class_is_assignable_from (target->data.array->eklass, candidate->data.array->eklass);
2713 case MONO_TYPE_ARRAY:
2714 if (candidate->type != MONO_TYPE_ARRAY)
2716 return is_array_type_compatible (target, candidate);
2718 case MONO_TYPE_VALUETYPE:
2719 /*FIXME handle nullables and enum*/
2720 return candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass;
2723 return candidate->type == MONO_TYPE_VAR && target->data.generic_param->num == candidate->data.generic_param->num;
2726 case MONO_TYPE_MVAR:
2727 return candidate->type == MONO_TYPE_MVAR && target->data.generic_param->num == candidate->data.generic_param->num;
2731 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2732 g_assert_not_reached ();
2738 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2740 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2743 return mono_delegate_type_equal (method, delegate);
2747 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2749 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2752 return mono_delegate_type_equal (delegate, method);
2756 * mono_delegate_signature_equal:
2758 * Compare two signatures in the way expected by delegates.
2760 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2762 * FIXME can this function be eliminated and proper metadata functionality be used?
2765 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2768 int method_offset = is_static_ldftn ? 1 : 0;
2770 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2773 if (delegate_sig->call_convention != method_sig->call_convention)
2776 for (i = 0; i < delegate_sig->param_count; i++) {
2777 MonoType *p1 = delegate_sig->params [i];
2778 MonoType *p2 = method_sig->params [i + method_offset];
2780 if (!mono_delegate_param_equal (p1, p2))
2784 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2791 * verify_ldftn_delegate:
2793 * Verify properties of ldftn based delegates.
2796 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2798 MonoMethod *method = funptr->method;
2800 /*ldftn non-final virtuals only allowed if method is not static,
2801 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2802 * This rules doesn't apply if the object on stack is a boxed valuetype.
2804 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2805 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2806 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2807 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2809 /*current method must not be static*/
2810 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2811 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2813 /*value is the this pointer, loaded using ldarg.0 */
2814 if (!stack_slot_is_this_pointer (value))
2815 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));
2817 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2822 * verify_delegate_compatibility:
2824 * Verify delegate creation sequence.
2828 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2830 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2831 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2833 MonoMethod *invoke, *method;
2834 const guint8 *ip = ctx->header->code;
2835 guint32 ip_offset = ctx->ip_offset;
2836 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2838 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2839 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2843 invoke = mono_get_delegate_invoke (delegate);
2844 method = funptr->method;
2846 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2848 if (is_static_ldftn)
2849 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2851 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound))
2852 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer parameter for delegate constructor has diferent signature at 0x%04x", ctx->ip_offset));
2855 * Delegate code sequences:
2861 * [-6] ldvirtftn token
2865 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2866 verify_ldftn_delegate (ctx, delegate, value, funptr);
2867 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2868 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2870 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2872 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2875 if (is_first_arg_bound) {
2876 if (!verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2877 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2879 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2880 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2883 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2884 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2886 #undef IS_VALID_OPCODE
2887 #undef IS_LOAD_FUN_PTR
2890 /* implement the opcode checks*/
2892 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2896 if (arg >= ctx->max_args) {
2898 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2900 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2901 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2902 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2904 } else if (check_overflow (ctx)) {
2905 /*We must let the value be pushed, otherwise we would get an underflow error*/
2906 check_unverifiable_type (ctx, ctx->params [arg]);
2907 if (ctx->params [arg]->byref && take_addr)
2908 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2909 top = stack_push (ctx);
2910 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2913 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2915 ctx->has_this_store = TRUE;
2917 top->stype |= THIS_POINTER_MASK;
2918 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2919 top->stype |= UNINIT_THIS_MASK;
2925 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2927 if (arg >= ctx->num_locals) {
2928 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2929 } else if (check_overflow (ctx)) {
2930 /*We must let the value be pushed, otherwise we would get an underflow error*/
2931 check_unverifiable_type (ctx, ctx->locals [arg]);
2932 if (ctx->locals [arg]->byref && take_addr)
2933 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2935 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2940 store_arg (VerifyContext *ctx, guint32 arg)
2944 if (arg >= ctx->max_args) {
2945 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2946 if (check_underflow (ctx, 1))
2951 if (check_underflow (ctx, 1)) {
2952 value = stack_pop (ctx);
2953 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2954 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2957 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2958 ctx->has_this_store = 1;
2962 store_local (VerifyContext *ctx, guint32 arg)
2965 if (arg >= ctx->num_locals) {
2966 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2970 /*TODO verify definite assigment */
2971 if (check_underflow (ctx, 1)) {
2972 value = stack_pop(ctx);
2973 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2974 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
2975 stack_slot_get_name (value),
2976 mono_type_get_stack_name (ctx->locals [arg]),
2982 /*FIXME add and sub needs special care here*/
2984 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2986 ILStackDesc *a, *b, *top;
2987 int idxa, idxb, complexMerge = 0;
2990 if (!check_underflow (ctx, 2))
2992 b = stack_pop (ctx);
2993 a = stack_pop (ctx);
2995 idxa = stack_slot_get_underlying_type (a);
2996 if (stack_slot_is_managed_pointer (a)) {
3001 idxb = stack_slot_get_underlying_type (b);
3002 if (stack_slot_is_managed_pointer (b)) {
3009 res = table [idxa][idxb];
3011 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
3012 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3014 top = stack_push (ctx);
3015 if (res == TYPE_INV) {
3016 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)));
3017 copy_stack_value (top, a);
3021 if (res & NON_VERIFIABLE_RESULT) {
3022 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)));
3024 res = res & ~NON_VERIFIABLE_RESULT;
3027 if (complexMerge && res == TYPE_PTR) {
3028 if (complexMerge == 1)
3029 copy_stack_value (top, a);
3030 else if (complexMerge == 2)
3031 copy_stack_value (top, b);
3033 * There is no need to merge the type of two pointers.
3034 * The only valid operation is subtraction, that returns a native
3035 * int as result and can be used with any 2 pointer kinds.
3036 * This is valid acording to Patition III 1.1.4
3045 do_boolean_branch_op (VerifyContext *ctx, int delta)
3047 int target = ctx->ip_offset + delta;
3050 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3052 if (target < 0 || target >= ctx->code_size) {
3053 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
3057 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3059 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3062 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3066 ctx->target = target;
3068 if (!check_underflow (ctx, 1))
3071 top = stack_pop (ctx);
3072 if (!is_valid_bool_arg (top))
3073 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));
3075 check_unmanaged_pointer (ctx, top);
3079 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
3081 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
3085 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
3090 int target = ctx->ip_offset + delta;
3092 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3094 if (target < 0 || target >= ctx->code_size) {
3095 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3099 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3100 case 1: /*FIXME use constants and not magic numbers.*/
3101 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3104 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3108 ctx->target = target;
3110 if (!check_underflow (ctx, 2))
3113 b = stack_pop (ctx);
3114 a = stack_pop (ctx);
3116 idxa = stack_slot_get_underlying_type (a);
3117 if (stack_slot_is_managed_pointer (a))
3120 idxb = stack_slot_get_underlying_type (b);
3121 if (stack_slot_is_managed_pointer (b))
3124 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3129 res = table [idxa][idxb];
3132 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
3133 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3135 if (res == TYPE_INV) {
3136 CODE_NOT_VERIFIABLE (ctx,
3137 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));
3138 } else if (res & NON_VERIFIABLE_RESULT) {
3139 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));
3140 res = res & ~NON_VERIFIABLE_RESULT;
3145 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
3151 if (!check_underflow (ctx, 2))
3153 b = stack_pop (ctx);
3154 a = stack_pop (ctx);
3156 if (opcode == CEE_CGT_UN) {
3157 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
3158 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3163 idxa = stack_slot_get_underlying_type (a);
3164 if (stack_slot_is_managed_pointer (a))
3167 idxb = stack_slot_get_underlying_type (b);
3168 if (stack_slot_is_managed_pointer (b))
3171 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3176 res = table [idxa][idxb];
3179 if(res == TYPE_INV) {
3180 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));
3181 } else if (res & NON_VERIFIABLE_RESULT) {
3182 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));
3183 res = res & ~NON_VERIFIABLE_RESULT;
3185 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3189 do_ret (VerifyContext *ctx)
3191 MonoType *ret = ctx->signature->ret;
3192 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3193 if (ret->type != MONO_TYPE_VOID) {
3195 if (!check_underflow (ctx, 1))
3198 top = stack_pop(ctx);
3200 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3201 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
3205 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3206 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3209 if (ctx->eval.size > 0) {
3210 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3212 if (in_any_block (ctx->header, ctx->ip_offset))
3213 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3217 * 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.
3218 * This is illegal but mono_get_method_full decoded it.
3219 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3222 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
3225 MonoMethodSignature *sig;
3228 gboolean virt_check_this = FALSE;
3229 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3231 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
3235 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3237 if (method->klass->valuetype) // && !constrained ???
3238 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3240 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3241 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3244 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3245 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3247 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL)) {
3248 virt_check_this = TRUE;
3249 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3253 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
3254 sig = mono_method_get_signature (method, ctx->image, method_token);
3256 param_count = sig->param_count + sig->hasthis;
3257 if (!check_underflow (ctx, param_count))
3260 for (i = sig->param_count - 1; i >= 0; --i) {
3261 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3262 value = stack_pop (ctx);
3263 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
3264 char *stack_name = stack_slot_full_name (value);
3265 char *sig_name = mono_type_full_name (sig->params [i]);
3266 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature: %s X %s at 0x%04x", sig_name, stack_name, ctx->ip_offset));
3267 g_free (stack_name);
3271 if (stack_slot_is_managed_mutability_pointer (value))
3272 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));
3274 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3275 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));
3281 MonoType *type = &method->klass->byval_arg;
3284 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3285 if (!mono_method_is_constructor (ctx->method))
3286 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3287 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3288 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));
3290 ctx->super_ctor_called = TRUE;
3291 value = stack_pop_safe (ctx);
3292 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3293 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3295 value = stack_pop (ctx);
3298 copy_stack_value (©, value);
3299 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3300 //Other parts of the code suffer from the same issue of
3301 copy.type = mono_type_get_type_byval (copy.type);
3302 copy.stype &= ~POINTER_MASK;
3304 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3305 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a non-final virtual method from an objet diferent thant the this pointer at 0x%04x", ctx->ip_offset));
3307 if (constrained && virtual) {
3308 if (!stack_slot_is_managed_pointer (value))
3309 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3310 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
3311 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3312 copy.stype |= BOXED_MASK;
3314 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3315 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));
3317 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3318 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3320 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3321 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3323 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3324 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));
3326 if (!verify_stack_type_compatibility (ctx, type, ©))
3327 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
3329 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, value->type->data.klass))
3330 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3332 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
3333 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3335 if (sig->ret->type != MONO_TYPE_VOID) {
3336 if (check_overflow (ctx)) {
3337 value = stack_push (ctx);
3338 set_stack_value (ctx, value, sig->ret, FALSE);
3339 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3340 ctx->prefix_set &= ~PREFIX_READONLY;
3341 value->stype |= CMMP_MASK;
3346 if ((ctx->prefix_set & PREFIX_TAIL)) {
3347 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3348 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3349 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3350 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3356 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3358 MonoClassField *field;
3361 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3363 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3366 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3367 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3370 /*taking the address of initonly field only works from the static constructor */
3371 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3372 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3373 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3375 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3376 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3378 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3382 do_store_static_field (VerifyContext *ctx, int token) {
3383 MonoClassField *field;
3386 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3388 if (!check_underflow (ctx, 1))
3391 value = stack_pop (ctx);
3393 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3396 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3397 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3401 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3402 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3406 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3407 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3409 if (!verify_stack_type_compatibility (ctx, field->type, value))
3410 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in static field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3414 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3416 MonoClassField *field;
3418 gboolean is_pointer;
3420 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3421 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3425 //the value on stack is going to be used as a pointer
3426 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3428 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3429 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3432 g_assert (obj->type);
3434 /*The value on the stack must be a subclass of the defining type of the field*/
3435 /* we need to check if we can load the field from the stack value*/
3437 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3438 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3440 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3441 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3443 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3444 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));
3446 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3447 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3448 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));
3450 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE))
3451 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3453 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3454 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3457 check_unmanaged_pointer (ctx, obj);
3462 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3465 MonoClassField *field;
3468 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3470 if (!check_underflow (ctx, 1))
3472 obj = stack_pop_safe (ctx);
3474 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3477 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3478 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3480 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3481 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3482 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3484 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3488 do_store_field (VerifyContext *ctx, int token)
3490 ILStackDesc *value, *obj;
3491 MonoClassField *field;
3492 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3494 if (!check_underflow (ctx, 2))
3497 value = stack_pop (ctx);
3498 obj = stack_pop_safe (ctx);
3500 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3503 if (!verify_stack_type_compatibility (ctx, field->type, value))
3504 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3507 /*TODO proper handle for Nullable<T>*/
3509 do_box_value (VerifyContext *ctx, int klass_token)
3512 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3518 if (!check_underflow (ctx, 1))
3521 value = stack_pop (ctx);
3522 /*box is a nop for reference types*/
3524 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3525 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3530 if (!verify_stack_type_compatibility (ctx, type, value))
3531 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3533 klass = mono_class_from_mono_type (type);
3534 if (mono_class_is_nullable (klass))
3535 type = &mono_class_get_nullable_param (klass)->byval_arg;
3536 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3540 do_unbox_value (VerifyContext *ctx, int klass_token)
3543 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3548 if (!check_underflow (ctx, 1))
3551 if (!mono_class_from_mono_type (type)->valuetype)
3552 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3554 value = stack_pop (ctx);
3556 /*Value should be: a boxed valuetype or a reference type*/
3557 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3558 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3559 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));
3561 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3562 value->stype |= CMMP_MASK;
3566 do_unbox_any (VerifyContext *ctx, int klass_token)
3569 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3574 if (!check_underflow (ctx, 1))
3577 value = stack_pop (ctx);
3579 /*Value should be: a boxed valuetype or a reference type*/
3580 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3581 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3582 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));
3584 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3588 do_unary_math_op (VerifyContext *ctx, int op)
3591 if (!check_underflow (ctx, 1))
3593 value = stack_pop (ctx);
3594 switch (stack_slot_get_type (value)) {
3597 case TYPE_NATIVE_INT:
3602 case TYPE_COMPLEX: /*only enums are ok*/
3603 if (mono_type_is_enum_type (value->type))
3606 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3608 stack_push_stack_val (ctx, value);
3612 do_conversion (VerifyContext *ctx, int kind)
3615 if (!check_underflow (ctx, 1))
3617 value = stack_pop (ctx);
3619 switch (stack_slot_get_type (value)) {
3622 case TYPE_NATIVE_INT:
3626 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));
3631 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3634 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3637 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3639 case TYPE_NATIVE_INT:
3640 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3643 g_error ("unknown type %02x in conversion", kind);
3649 do_load_token (VerifyContext *ctx, int token)
3652 MonoClass *handle_class;
3653 if (!check_overflow (ctx))
3655 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3657 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3660 if (handle_class == mono_defaults.typehandle_class) {
3661 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3662 } else if (handle_class == mono_defaults.methodhandle_class) {
3663 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3664 } else if (handle_class == mono_defaults.fieldhandle_class) {
3665 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3667 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3669 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3673 do_ldobj_value (VerifyContext *ctx, int token)
3676 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3677 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3682 if (!check_underflow (ctx, 1))
3685 value = stack_pop (ctx);
3686 if (!stack_slot_is_managed_pointer (value)
3687 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3688 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3689 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3693 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3694 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3696 /*We have a byval on the stack, but the comparison must be strict. */
3697 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3698 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3700 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3704 do_stobj (VerifyContext *ctx, int token)
3706 ILStackDesc *dest, *src;
3707 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3708 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3713 if (!check_underflow (ctx, 2))
3716 src = stack_pop (ctx);
3717 dest = stack_pop (ctx);
3719 if (stack_slot_is_managed_mutability_pointer (dest))
3720 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3722 if (!stack_slot_is_managed_pointer (dest))
3723 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3725 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3726 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
3728 if (!verify_stack_type_compatibility (ctx, type, src))
3729 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
3731 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3732 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3736 do_cpobj (VerifyContext *ctx, int token)
3738 ILStackDesc *dest, *src;
3739 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3743 if (!check_underflow (ctx, 2))
3746 src = stack_pop (ctx);
3747 dest = stack_pop (ctx);
3749 if (!stack_slot_is_managed_pointer (src))
3750 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3752 if (!stack_slot_is_managed_pointer (dest))
3753 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3755 if (stack_slot_is_managed_mutability_pointer (dest))
3756 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3758 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3759 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3761 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3762 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3766 do_initobj (VerifyContext *ctx, int token)
3769 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3773 if (!check_underflow (ctx, 1))
3776 obj = stack_pop (ctx);
3778 if (!stack_slot_is_managed_pointer (obj))
3779 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3781 if (stack_slot_is_managed_mutability_pointer (obj))
3782 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3784 stack = mono_type_get_type_byval (obj->type);
3785 if (MONO_TYPE_IS_REFERENCE (stack)) {
3786 if (!verify_type_compatibility (ctx, stack, type))
3787 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3788 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3789 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3790 } else if (!verify_type_compatibility (ctx, stack, type)) {
3791 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3796 do_newobj (VerifyContext *ctx, int token)
3800 MonoMethodSignature *sig;
3802 gboolean is_delegate = FALSE;
3804 if (!(method = verifier_load_method (ctx, token, "newobj")))
3807 if (!mono_method_is_constructor (method)) {
3808 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3812 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3813 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3815 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
3816 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor not visible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3818 //FIXME use mono_method_get_signature_full
3819 sig = mono_method_signature (method);
3820 if (!check_underflow (ctx, sig->param_count))
3823 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3826 ILStackDesc *funptr;
3827 //first arg is object, second arg is fun ptr
3828 if (sig->param_count != 2) {
3829 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3832 funptr = stack_pop (ctx);
3833 value = stack_pop (ctx);
3834 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3836 for (i = sig->param_count - 1; i >= 0; --i) {
3837 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3838 value = stack_pop (ctx);
3839 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3840 char *stack_name = stack_slot_full_name (value);
3841 char *sig_name = mono_type_full_name (sig->params [i]);
3842 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));
3843 g_free (stack_name);
3847 if (stack_slot_is_managed_mutability_pointer (value))
3848 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3852 if (check_overflow (ctx))
3853 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3857 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3863 if (!check_underflow (ctx, 1))
3866 if (!(type = verifier_load_type (ctx, token, opcode)))
3870 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3874 value = stack_pop (ctx);
3875 is_boxed = stack_slot_is_boxed_value (value);
3877 if (stack_slot_is_managed_pointer (value))
3878 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3879 else if (mono_class_from_mono_type (value->type)->valuetype && !is_boxed)
3880 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Value cannot be a valuetype for %s at 0x%04x", opcode, ctx->ip_offset));
3882 switch (value->type->type) {
3883 case MONO_TYPE_FNPTR:
3885 case MONO_TYPE_TYPEDBYREF:
3886 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3889 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3890 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3894 mono_type_from_opcode (int opcode) {
3902 return &mono_defaults.sbyte_class->byval_arg;
3910 return &mono_defaults.int16_class->byval_arg;
3918 return &mono_defaults.int32_class->byval_arg;
3924 return &mono_defaults.int64_class->byval_arg;
3930 return &mono_defaults.single_class->byval_arg;
3936 return &mono_defaults.double_class->byval_arg;
3942 return &mono_defaults.int_class->byval_arg;
3946 case CEE_LDELEM_REF:
3947 case CEE_STELEM_REF:
3948 return &mono_defaults.object_class->byval_arg;
3951 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3957 do_load_indirect (VerifyContext *ctx, int opcode)
3960 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3962 if (!check_underflow (ctx, 1))
3965 value = stack_pop (ctx);
3966 if (!stack_slot_is_managed_pointer (value)) {
3967 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3968 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3972 if (opcode == CEE_LDIND_REF) {
3973 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3974 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3975 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3977 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3978 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3979 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3984 do_store_indirect (VerifyContext *ctx, int opcode)
3986 ILStackDesc *addr, *val;
3987 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3989 if (!check_underflow (ctx, 2))
3992 val = stack_pop (ctx);
3993 addr = stack_pop (ctx);
3995 check_unmanaged_pointer (ctx, addr);
3997 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3998 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
4002 if (stack_slot_is_managed_mutability_pointer (addr)) {
4003 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
4007 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
4008 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4010 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
4011 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4015 do_newarr (VerifyContext *ctx, int token)
4018 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
4023 if (!check_underflow (ctx, 1))
4026 value = stack_pop (ctx);
4027 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4028 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));
4030 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
4033 /*FIXME handle arrays that are not 0-indexed*/
4035 do_ldlen (VerifyContext *ctx)
4039 if (!check_underflow (ctx, 1))
4042 value = stack_pop (ctx);
4044 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
4045 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
4047 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
4050 /*FIXME handle arrays that are not 0-indexed*/
4051 /*FIXME handle readonly prefix and CMMP*/
4053 do_ldelema (VerifyContext *ctx, int klass_token)
4055 ILStackDesc *index, *array, *res;
4056 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
4062 if (!check_underflow (ctx, 2))
4065 index = stack_pop (ctx);
4066 array = stack_pop (ctx);
4068 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4069 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));
4071 if (!stack_slot_is_null_literal (array)) {
4072 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4073 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4075 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
4076 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
4078 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
4081 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
4085 res = stack_push (ctx);
4086 set_stack_value (ctx, res, type, TRUE);
4087 if (ctx->prefix_set & PREFIX_READONLY) {
4088 ctx->prefix_set &= ~PREFIX_READONLY;
4089 res->stype |= CMMP_MASK;
4094 * FIXME handle arrays that are not 0-indexed
4095 * FIXME handle readonly prefix and CMMP
4098 do_ldelem (VerifyContext *ctx, int opcode, int token)
4100 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4101 ILStackDesc *index, *array;
4103 if (!check_underflow (ctx, 2))
4106 if (opcode == CEE_LDELEM_ANY) {
4107 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
4108 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4112 type = mono_type_from_opcode (opcode);
4115 index = stack_pop (ctx);
4116 array = stack_pop (ctx);
4118 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4119 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));
4121 if (!stack_slot_is_null_literal (array)) {
4122 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4123 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));
4125 if (opcode == CEE_LDELEM_REF) {
4126 if (array->type->data.klass->valuetype)
4127 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
4128 type = &array->type->data.klass->byval_arg;
4130 MonoType *candidate = &array->type->data.klass->byval_arg;
4131 if (IS_STRICT_MODE (ctx)) {
4132 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
4133 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
4134 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)) ||
4135 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
4136 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4138 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
4139 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4144 set_stack_value (ctx, stack_push (ctx), type, FALSE);
4149 * FIXME handle arrays that are not 0-indexed
4152 do_stelem (VerifyContext *ctx, int opcode, int token)
4154 ILStackDesc *index, *array, *value;
4156 if (!check_underflow (ctx, 3))
4159 if (opcode == CEE_STELEM_ANY) {
4160 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
4161 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4165 type = mono_type_from_opcode (opcode);
4168 value = stack_pop (ctx);
4169 index = stack_pop (ctx);
4170 array = stack_pop (ctx);
4172 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4173 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));
4175 if (!stack_slot_is_null_literal (array)) {
4176 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
4177 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));
4179 if (opcode == CEE_STELEM_REF) {
4180 if (array->type->data.klass->valuetype)
4181 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4182 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
4183 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4187 if (opcode == CEE_STELEM_REF) {
4188 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
4189 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4190 } else if (opcode != CEE_STELEM_REF) {
4191 if (!verify_stack_type_compatibility (ctx, type, value))
4192 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4194 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
4195 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));
4201 do_throw (VerifyContext *ctx)
4203 ILStackDesc *exception;
4204 if (!check_underflow (ctx, 1))
4206 exception = stack_pop (ctx);
4208 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4209 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4211 /*The stack is left empty after a throw*/
4217 do_endfilter (VerifyContext *ctx)
4219 MonoExceptionClause *clause;
4221 if (IS_STRICT_MODE (ctx)) {
4222 if (ctx->eval.size != 1)
4223 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4225 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4226 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4229 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4230 if (IS_STRICT_MODE (ctx)) {
4231 if (ctx->ip_offset != clause->handler_offset - 2)
4232 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4234 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4235 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4238 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4239 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4241 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4248 do_leave (VerifyContext *ctx, int delta)
4250 int target = ((gint32)ctx->ip_offset) + delta;
4251 if (target >= ctx->code_size || target < 0)
4252 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4254 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4255 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4262 * Verify br and br.s opcodes.
4265 do_static_branch (VerifyContext *ctx, int delta)
4267 int target = ctx->ip_offset + delta;
4268 if (target < 0 || target >= ctx->code_size) {
4269 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4273 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4275 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4278 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4282 ctx->target = target;
4286 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4288 int i, base = ctx->ip_offset + 5 + count * 4;
4291 if (!check_underflow (ctx, 1))
4294 value = stack_pop (ctx);
4296 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4297 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4299 for (i = 0; i < count; ++i) {
4300 int target = base + read32 (data + i * 4);
4302 if (target < 0 || target >= ctx->code_size) {
4303 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4307 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4309 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4312 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4315 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4320 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4325 if (virtual && !check_underflow (ctx, 1))
4328 if (!virtual && !check_overflow (ctx))
4331 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4332 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4336 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4339 if (mono_method_is_constructor (method))
4340 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4343 ILStackDesc *top = stack_pop (ctx);
4345 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4346 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4348 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4349 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4351 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4352 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4355 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4356 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);
4358 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4359 top->method = method;
4363 do_sizeof (VerifyContext *ctx, int token)
4367 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4368 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4372 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4375 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4376 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4380 if (type->type == MONO_TYPE_VOID) {
4381 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4385 if (check_overflow (ctx))
4386 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4389 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4391 do_localloc (VerifyContext *ctx)
4395 if (ctx->eval.size != 1) {
4396 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4400 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4401 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4405 /*TODO verify top type*/
4406 top = stack_pop (ctx);
4408 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4409 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4413 do_ldstr (VerifyContext *ctx, guint32 token)
4415 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4416 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4420 if (mono_metadata_token_index (token) >= ctx->image->heap_us.size) {
4421 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4425 if (check_overflow (ctx))
4426 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4430 do_refanyval (VerifyContext *ctx, int token)
4434 if (!check_underflow (ctx, 1))
4437 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4440 top = stack_pop (ctx);
4442 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4443 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));
4445 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4449 do_refanytype (VerifyContext *ctx)
4453 if (!check_underflow (ctx, 1))
4456 top = stack_pop (ctx);
4458 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4459 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));
4461 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4466 do_mkrefany (VerifyContext *ctx, int token)
4470 if (!check_underflow (ctx, 1))
4473 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4476 top = stack_pop (ctx);
4478 if (stack_slot_is_managed_mutability_pointer (top))
4479 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4481 if (!stack_slot_is_managed_pointer (top)) {
4482 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));
4484 MonoType *stack_type = mono_type_get_type_byval (top->type);
4485 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4486 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4488 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4489 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4492 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4496 do_ckfinite (VerifyContext *ctx)
4499 if (!check_underflow (ctx, 1))
4502 top = stack_pop (ctx);
4504 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4505 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));
4506 stack_push_stack_val (ctx, top);
4510 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4512 * @from holds new values for a given control path
4513 * @to holds the current values of a given control path
4515 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4518 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4521 stack_init (ctx, to);
4524 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4527 stack_copy (&ctx->eval, to);
4529 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4530 stack_copy (to, &ctx->eval);
4533 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4535 if (from->size != to->size) {
4536 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4537 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));
4541 //FIXME we need to preserve CMMP attributes
4542 //FIXME we must take null literals into consideration.
4543 for (i = 0; i < from->size; ++i) {
4544 ILStackDesc *new_slot = from->stack + i;
4545 ILStackDesc *old_slot = to->stack + i;
4546 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4547 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4548 MonoClass *old_class = mono_class_from_mono_type (old_type);
4549 MonoClass *new_class = mono_class_from_mono_type (new_type);
4550 MonoClass *match_class = NULL;
4552 // S := T then U = S (new value is compatible with current value, keep current)
4553 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4554 copy_stack_value (new_slot, old_slot);
4558 // T := S then U = T (old value is compatible with current value, use new)
4559 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4560 copy_stack_value (old_slot, new_slot);
4564 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4565 char *old_name = stack_slot_full_name (old_slot);
4566 char *new_name = stack_slot_full_name (new_slot);
4567 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));
4573 //both are reference types, use closest common super type
4574 if (!mono_class_from_mono_type (old_type)->valuetype
4575 && !mono_class_from_mono_type (new_type)->valuetype
4576 && !stack_slot_is_managed_pointer (old_slot)
4577 && !stack_slot_is_managed_pointer (new_slot)) {
4579 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4580 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4581 match_class = old_class->supertypes [j];
4586 mono_class_setup_interfaces (old_class);
4587 for (j = 0; j < old_class->interface_count; ++j) {
4588 for (k = 0; k < new_class->interface_count; ++k) {
4589 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4590 match_class = old_class->interfaces [j];
4596 //No decent super type found, use object
4597 match_class = mono_defaults.object_class;
4599 } 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)) {
4600 match_class = mono_defaults.object_class;
4605 char *old_name = stack_slot_full_name (old_slot);
4606 char *new_name = stack_slot_full_name (new_slot);
4607 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));
4611 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4615 g_assert (match_class);
4616 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4617 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4623 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4624 to->flags |= IL_CODE_STACK_MERGED;
4627 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4628 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4631 * is_clause_in_range :
4633 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4636 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4638 if (clause->try_offset >= start && clause->try_offset < end)
4640 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4646 * is_clause_inside_range :
4648 * Returns TRUE if @clause lies completely inside the @start - @end range.
4651 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4653 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4655 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4661 * is_clause_nested :
4663 * Returns TRUE if @nested is nested in @clause.
4666 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4668 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4670 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4671 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4674 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4675 * the each pair of exception must have the following properties:
4676 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4677 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4678 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4681 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4683 /*clause is nested*/
4684 if (is_clause_nested (to_test, clause)) {
4685 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4686 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4691 /*wrong nesting order.*/
4692 if (is_clause_nested (clause, to_test)) {
4693 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4697 /*mutual protection*/
4698 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4699 /*handlers are not disjoint*/
4700 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4701 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4704 /* handlers are not catch or filter */
4705 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4706 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4713 /*not completelly disjoint*/
4714 if (is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4715 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len))
4716 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4719 #define code_bounds_check(size) \
4720 if (ip + size > end) {\
4721 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4726 * FIXME: need to distinguish between valid and verifiable.
4727 * Need to keep track of types on the stack.
4728 * Verify types for opcodes.
4731 mono_method_verify (MonoMethod *method, int level)
4733 const unsigned char *ip;
4734 const unsigned char *end;
4735 int i, n, need_merge = 0, start = 0;
4736 guint token, ip_offset = 0, prefix = 0;
4737 MonoGenericContext *generic_context = NULL;
4741 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4743 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4744 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4748 memset (&ctx, 0, sizeof (VerifyContext));
4750 //FIXME use mono_method_get_signature_full
4751 ctx.signature = mono_method_signature (method);
4752 if (!ctx.signature) {
4753 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4756 ctx.header = mono_method_get_header (method);
4758 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4761 ctx.method = method;
4762 ip = ctx.header->code;
4763 end = ip + ctx.header->code_size;
4764 ctx.image = image = method->klass->image;
4767 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4768 ctx.max_stack = ctx.header->max_stack;
4769 ctx.verifiable = ctx.valid = 1;
4772 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4773 ctx.code_size = ctx.header->code_size;
4775 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4778 ctx.num_locals = ctx.header->num_locals;
4779 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4781 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4782 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4784 ctx.params = g_new (MonoType*, ctx.max_args);
4785 if (ctx.signature->hasthis)
4786 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4787 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4789 if (ctx.signature->is_inflated)
4790 ctx.generic_context = generic_context = mono_method_get_context (method);
4792 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4793 if (method->is_generic)
4794 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4796 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4799 for (i = 0; i < ctx.num_locals; ++i)
4800 ctx.locals [i] = mono_class_inflate_generic_type (ctx.locals [i], ctx.generic_context);
4801 for (i = 0; i < ctx.max_args; ++i)
4802 ctx.params [i] = mono_class_inflate_generic_type (ctx.params [i], ctx.generic_context);
4803 stack_init (&ctx, &ctx.eval);
4805 for (i = 0; i < ctx.num_locals; ++i) {
4806 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i])) {
4807 /*TODO use the last error message to provide better feedback. */
4808 ADD_VERIFY_ERROR2 (&ctx, g_strdup_printf ("Invalid local variable %d", i), MONO_EXCEPTION_BAD_IMAGE);
4813 for (i = 0; i < ctx.max_args; ++i) {
4814 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i])) {
4815 /*TODO use the last error message to provide better feedback. */
4816 ADD_VERIFY_ERROR2 (&ctx, g_strdup_printf ("Invalid parameter %d", i), MONO_EXCEPTION_BAD_IMAGE);
4824 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4825 MonoExceptionClause *clause = ctx.header->clauses + i;
4826 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); );
4828 if (clause->try_offset > ctx.code_size)
4829 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4831 if (clause->try_len <= 0)
4832 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4834 if (clause->handler_offset > ctx.code_size)
4835 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4837 if (clause->handler_len <= 0)
4838 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4840 if (clause->try_offset < clause->handler_offset && clause->try_offset + clause->try_len > HANDLER_START (clause))
4841 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4843 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4844 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4849 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4850 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4851 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4853 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4854 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4856 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4857 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4858 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4862 while (ip < end && ctx.valid) {
4863 ctx.ip_offset = ip_offset = ip - ctx.header->code;
4865 /*We need to check against fallthrou in and out of protected blocks.
4866 * For fallout we check the once a protected block ends, if the start flag is not set.
4867 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4868 * TODO convert these checks to be done using flags and not this loop
4870 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4871 MonoExceptionClause *clause = ctx.header->clauses + i;
4873 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4874 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4878 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4879 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4880 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4882 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4886 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4887 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4891 if (clause->handler_offset == ip_offset && start == 0) {
4892 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4896 if (clause->try_offset == ip_offset && ctx.eval.size > 0) {
4897 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4906 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4907 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4910 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4913 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4914 #ifdef MONO_VERIFIER_DEBUG
4917 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4918 discode [strlen (discode) - 1] = 0; /* no \n */
4919 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4922 dump_stack_state (&ctx.code [ip_offset]);
4923 dump_stack_state (&ctx.eval);
4936 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4942 code_bounds_check (2);
4943 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4947 case CEE_ADD_OVF_UN:
4948 do_binop (&ctx, *ip, add_ovf_un_table);
4952 case CEE_SUB_OVF_UN:
4953 do_binop (&ctx, *ip, sub_ovf_un_table);
4960 case CEE_MUL_OVF_UN:
4961 do_binop (&ctx, *ip, bin_ovf_table);
4966 do_binop (&ctx, *ip, add_table);
4971 do_binop (&ctx, *ip, sub_table);
4978 do_binop (&ctx, *ip, bin_op_table);
4987 do_binop (&ctx, *ip, int_bin_op_table);
4994 do_binop (&ctx, *ip, shift_op_table);
4999 if (!check_underflow (&ctx, 1))
5001 stack_pop_safe (&ctx);
5015 /*TODO support definite assignment verification? */
5016 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
5024 store_local (&ctx, *ip - CEE_STLOC_0);
5029 code_bounds_check (2);
5030 store_local (&ctx, ip [1]);
5035 code_bounds_check (2);
5036 store_arg (&ctx, ip [1]);
5050 if (check_overflow (&ctx))
5051 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5056 code_bounds_check (2);
5057 if (check_overflow (&ctx))
5058 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5063 code_bounds_check (5);
5064 if (check_overflow (&ctx))
5065 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
5070 code_bounds_check (9);
5071 if (check_overflow (&ctx))
5072 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
5077 code_bounds_check (5);
5078 if (check_overflow (&ctx))
5079 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5084 code_bounds_check (9);
5085 if (check_overflow (&ctx))
5086 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5091 if (check_overflow (&ctx))
5092 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
5098 code_bounds_check (2);
5099 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
5112 code_bounds_check (2);
5113 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5120 code_bounds_check (5);
5121 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5134 code_bounds_check (5);
5135 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5142 code_bounds_check (2);
5143 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5147 /* FIXME: warn/error instead? */
5154 if (!check_underflow (&ctx, 1))
5156 if (!check_overflow (&ctx))
5158 top = stack_pop_safe (&ctx);
5159 copy_stack_value (stack_push (&ctx), top);
5160 copy_stack_value (stack_push (&ctx), top);
5166 code_bounds_check (5);
5168 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5169 token = read32 (ip + 1);
5170 if (in_any_block (ctx.header, ip_offset))
5171 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5173 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5175 * FIXME: check signature, retval, arguments etc.
5181 code_bounds_check (5);
5182 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5187 code_bounds_check (5);
5188 token = read32 (ip + 1);
5190 * FIXME: check signature, retval, arguments etc.
5191 * FIXME: check requirements for tail call
5193 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5197 code_bounds_check (2);
5198 do_static_branch (&ctx, (signed char)ip [1] + 2);
5206 code_bounds_check (2);
5207 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5213 code_bounds_check (5);
5214 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5222 code_bounds_check (5);
5223 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5229 code_bounds_check (5);
5230 n = read32 (ip + 1);
5231 code_bounds_check (5 + sizeof (guint32) * n);
5233 do_switch (&ctx, n, (ip + 5));
5235 ip += 5 + sizeof (guint32) * n;
5249 do_load_indirect (&ctx, *ip);
5261 do_store_indirect (&ctx, *ip);
5267 do_unary_math_op (&ctx, *ip);
5277 do_conversion (&ctx, TYPE_I4);
5283 do_conversion (&ctx, TYPE_I8);
5290 do_conversion (&ctx, TYPE_R8);
5296 do_conversion (&ctx, TYPE_NATIVE_INT);
5301 code_bounds_check (5);
5302 do_cpobj (&ctx, read32 (ip + 1));
5307 code_bounds_check (5);
5308 do_ldobj_value (&ctx, read32 (ip + 1));
5313 code_bounds_check (5);
5314 do_ldstr (&ctx, read32 (ip + 1));
5319 code_bounds_check (5);
5320 do_newobj (&ctx, read32 (ip + 1));
5326 code_bounds_check (5);
5327 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5333 ++ip; /* warn, error ? */
5337 code_bounds_check (5);
5338 do_unbox_value (&ctx, read32 (ip + 1));
5350 code_bounds_check (5);
5351 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5357 code_bounds_check (5);
5358 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5363 code_bounds_check (5);
5364 do_store_field (&ctx, read32 (ip + 1));
5369 code_bounds_check (5);
5370 do_store_static_field (&ctx, read32 (ip + 1));
5375 code_bounds_check (5);
5376 do_stobj (&ctx, read32 (ip + 1));
5380 case CEE_CONV_OVF_I1_UN:
5381 case CEE_CONV_OVF_I2_UN:
5382 case CEE_CONV_OVF_I4_UN:
5383 case CEE_CONV_OVF_U1_UN:
5384 case CEE_CONV_OVF_U2_UN:
5385 case CEE_CONV_OVF_U4_UN:
5386 do_conversion (&ctx, TYPE_I4);
5390 case CEE_CONV_OVF_I8_UN:
5391 case CEE_CONV_OVF_U8_UN:
5392 do_conversion (&ctx, TYPE_I8);
5396 case CEE_CONV_OVF_I_UN:
5397 case CEE_CONV_OVF_U_UN:
5398 do_conversion (&ctx, TYPE_NATIVE_INT);
5403 code_bounds_check (5);
5404 do_box_value (&ctx, read32 (ip + 1));
5409 code_bounds_check (5);
5410 do_newarr (&ctx, read32 (ip + 1));
5420 code_bounds_check (5);
5421 do_ldelema (&ctx, read32 (ip + 1));
5435 case CEE_LDELEM_REF:
5436 do_ldelem (&ctx, *ip, 0);
5447 case CEE_STELEM_REF:
5448 do_stelem (&ctx, *ip, 0);
5452 case CEE_LDELEM_ANY:
5453 code_bounds_check (5);
5454 do_ldelem (&ctx, *ip, read32 (ip + 1));
5458 case CEE_STELEM_ANY:
5459 code_bounds_check (5);
5460 do_stelem (&ctx, *ip, read32 (ip + 1));
5465 code_bounds_check (5);
5466 do_unbox_any (&ctx, read32 (ip + 1));
5470 case CEE_CONV_OVF_I1:
5471 case CEE_CONV_OVF_U1:
5472 case CEE_CONV_OVF_I2:
5473 case CEE_CONV_OVF_U2:
5474 case CEE_CONV_OVF_I4:
5475 case CEE_CONV_OVF_U4:
5476 do_conversion (&ctx, TYPE_I4);
5480 case CEE_CONV_OVF_I8:
5481 case CEE_CONV_OVF_U8:
5482 do_conversion (&ctx, TYPE_I8);
5486 case CEE_CONV_OVF_I:
5487 case CEE_CONV_OVF_U:
5488 do_conversion (&ctx, TYPE_NATIVE_INT);
5493 code_bounds_check (5);
5494 do_refanyval (&ctx, read32 (ip + 1));
5504 code_bounds_check (5);
5505 do_mkrefany (&ctx, read32 (ip + 1));
5510 code_bounds_check (5);
5511 do_load_token (&ctx, read32 (ip + 1));
5515 case CEE_ENDFINALLY:
5516 if (!is_correct_endfinally (ctx.header, ip_offset))
5517 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5524 code_bounds_check (5);
5525 do_leave (&ctx, read32 (ip + 1) + 5);
5531 code_bounds_check (2);
5532 do_leave (&ctx, (signed char)ip [1] + 2);
5538 code_bounds_check (2);
5542 code_bounds_check (3);
5543 store_local (&ctx, read16 (ip + 1));
5548 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5556 do_cmp_op (&ctx, cmp_br_op, *ip);
5561 code_bounds_check (3);
5562 store_arg (&ctx, read16 (ip + 1) );
5568 check_overflow (&ctx);
5569 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5570 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5571 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5576 code_bounds_check (5);
5577 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5582 code_bounds_check (5);
5583 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5593 code_bounds_check (3);
5594 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5600 code_bounds_check (3);
5601 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5614 do_endfilter (&ctx);
5618 case CEE_UNALIGNED_:
5619 code_bounds_check (2);
5620 prefix |= PREFIX_UNALIGNED;
5624 prefix |= PREFIX_VOLATILE;
5628 prefix |= PREFIX_TAIL;
5630 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5631 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5635 code_bounds_check (5);
5636 do_initobj (&ctx, read32 (ip + 1));
5640 case CEE_CONSTRAINED_:
5641 code_bounds_check (5);
5642 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5643 prefix |= PREFIX_CONSTRAINED;
5648 prefix |= PREFIX_READONLY;
5653 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5654 if (!check_underflow (&ctx, 3))
5656 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5661 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5662 if (!check_underflow (&ctx, 3))
5664 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5672 if (!is_correct_rethrow (ctx.header, ip_offset))
5673 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5682 code_bounds_check (5);
5683 do_sizeof (&ctx, read32 (ip + 1));
5687 case CEE_REFANYTYPE:
5688 do_refanytype (&ctx);
5693 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5699 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5703 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5705 if (!ctx.prefix_set) //first prefix
5706 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5707 ctx.prefix_set |= prefix;
5708 ctx.has_flags = TRUE;
5712 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5714 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5715 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5716 if (ctx.prefix_set & PREFIX_READONLY)
5717 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5718 if (ctx.prefix_set & PREFIX_VOLATILE)
5719 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5720 if (ctx.prefix_set & PREFIX_UNALIGNED)
5721 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5722 ctx.prefix_set = prefix = 0;
5723 ctx.has_flags = FALSE;
5727 * if ip != end we overflowed: mark as error.
5729 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5730 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5733 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5734 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5735 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5736 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5737 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5739 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5740 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5742 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5743 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5745 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5746 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));
5749 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class)
5750 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor not calling super\n"));
5754 for (i = 0; i < ctx.header->code_size; ++i) {
5755 if (ctx.code [i].stack)
5756 g_free (ctx.code [i].stack);
5760 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5762 g_slist_free (ctx.funptrs);
5764 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5765 mono_metadata_free_type (tmp->data);
5766 g_slist_free (ctx.exception_types);
5768 for (i = 0; i < ctx.num_locals; ++i)
5769 mono_metadata_free_type (ctx.locals [i]);
5770 for (i = 0; i < ctx.max_args; ++i)
5771 mono_metadata_free_type (ctx.params [i]);
5774 g_free (ctx.eval.stack);
5777 g_free (ctx.locals);
5778 g_free (ctx.params);
5784 mono_verify_corlib ()
5786 /* This is a public API function so cannot be removed */
5790 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
5791 static gboolean verify_all = FALSE;
5794 * Set the desired level of checks for the verfier.
5798 mono_verifier_set_mode (MiniVerifierMode mode)
5800 verifier_mode = mode;
5804 mono_verifier_enable_verify_all ()
5810 * Returns true if @method needs to be verified.
5814 mono_verifier_is_enabled_for_method (MonoMethod *method)
5816 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5820 * Returns true if @klass need to be verified.
5824 mono_verifier_is_enabled_for_class (MonoClass *klass)
5826 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !klass->image->assembly->in_gac && klass->image != mono_defaults.corlib);
5830 mono_verifier_is_method_full_trust (MonoMethod *method)
5832 return mono_verifier_is_class_full_trust (method->klass);
5836 * Returns if @klass is under full trust or not.
5838 * TODO This code doesn't take CAS into account.
5840 * This value is only pertinent to assembly verification and has
5841 * nothing to do with CoreClr security.
5843 * Under verify_all all user code must be verifiable if no security option was set
5847 mono_verifier_is_class_full_trust (MonoClass *klass)
5849 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5850 return klass->image->assembly->in_gac || klass->image == mono_defaults.corlib;
5851 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || klass->image->assembly->in_gac || klass->image == mono_defaults.corlib;
5855 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5857 return mono_method_verify (method,
5858 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5859 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5860 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5864 get_field_end (MonoClassField *field)
5867 int size = mono_type_size (field->type, &align);
5869 size = 4; /*FIXME Is this a safe bet?*/
5870 return size + field->offset;
5874 verify_class_for_overlapping_reference_fields (MonoClass *class)
5877 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5878 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) || !class->has_references)
5881 //we must check for stuff overlapping reference fields
5882 for (i = 0; i < class->field.count; ++i) {
5883 MonoClassField *field = &class->fields [i];
5884 int fieldEnd = get_field_end (field);
5885 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5886 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5889 for (j = i + 1; j < class->field.count; ++j) {
5890 MonoClassField *other = &class->fields [j];
5891 int otherEnd = get_field_end (other);
5892 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5895 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5898 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5906 * Check if the class is verifiable.
5908 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5909 * field and invalid generic instantiation are fatal errors.
5911 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5915 mono_verifier_verify_class (MonoClass *class)
5917 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
5919 if (!verify_class_for_overlapping_reference_fields (class))
5922 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))