2 #include <mono/metadata/object-internals.h>
3 #include <mono/metadata/verify.h>
4 #include <mono/metadata/verify-internals.h>
5 #include <mono/metadata/opcodes.h>
6 #include <mono/metadata/tabledefs.h>
7 #include <mono/metadata/reflection.h>
8 #include <mono/metadata/debug-helpers.h>
9 #include <mono/metadata/mono-endian.h>
10 #include <mono/metadata/metadata.h>
11 #include <mono/metadata/metadata-internals.h>
12 #include <mono/metadata/class-internals.h>
13 #include <mono/metadata/tokentype.h>
20 * Pull the list of opcodes
22 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
26 #include "mono/cil/opcode.def"
31 #ifdef MONO_VERIFIER_DEBUG
32 #define VERIFIER_DEBUG(code) do { code } while (0)
34 #define VERIFIER_DEBUG(code)
37 //////////////////////////////////////////////////////////////////
38 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
39 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
40 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
41 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
42 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
43 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
45 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
46 vinfo->info.status = __status; \
47 vinfo->info.message = ( __msg ); \
48 vinfo->exception_type = (__exception); \
49 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
52 #define ADD_VERIFY_ERROR(__ctx, __msg) \
54 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
58 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
60 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
61 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
62 (__ctx)->verifiable = 0; \
63 if (IS_FAIL_FAST_MODE (__ctx)) \
68 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
70 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
74 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
76 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
77 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
78 (__ctx)->verifiable = 0; \
79 if (IS_FAIL_FAST_MODE (__ctx)) \
83 /*Flags to be used with ILCodeDesc::flags */
85 /*Instruction has not been processed.*/
86 IL_CODE_FLAG_NOT_PROCESSED = 0,
87 /*Instruction was decoded by mono_method_verify loop.*/
88 IL_CODE_FLAG_SEEN = 1,
89 /*Instruction was target of a branch or is at a protected block boundary.*/
90 IL_CODE_FLAG_WAS_TARGET = 2,
91 /*Used by stack_init to avoid double initialize each entry.*/
92 IL_CODE_FLAG_STACK_INITED = 4,
93 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
94 IL_CODE_STACK_MERGED = 8,
95 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
96 IL_CODE_DELEGATE_SEQUENCE = 0x10,
97 /*This is a delegate created from a ldftn to a non final virtual method*/
98 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
99 /*This is a call to a non final virtual method*/
100 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
129 /*Allocated fnptr MonoType that should be freed by us.*/
131 /*Type dup'ed exception types from catch blocks.*/
132 GSList *exception_types;
137 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
141 MonoMethodSignature *signature;
142 MonoMethodHeader *header;
144 MonoGenericContext *generic_context;
148 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
149 *on a method that creates a delegate for a non-final virtual method using ldftn*/
150 gboolean has_this_store;
152 /*This flag is used to control if the contructor of the parent class has been called.
153 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
154 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
156 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
157 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
159 gboolean super_ctor_called;
163 MonoType *constrained_type;
167 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
170 get_stack_type (MonoType *type);
173 mono_delegate_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2);
176 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
179 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
180 //////////////////////////////////////////////////////////////////
185 TYPE_INV = 0, /* leave at 0. */
190 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
192 /* value types and classes */
194 /* Number of types, used to define the size of the tables*/
197 /* Used by tables to signal that a result is not verifiable*/
198 NON_VERIFIABLE_RESULT = 0x80,
200 /*Mask used to extract just the type, excluding flags */
203 /* The stack type is a managed pointer, unmask the value to res */
204 POINTER_MASK = 0x100,
206 /*Stack type with the pointer mask*/
207 RAW_TYPE_MASK = 0x10F,
209 /* Controlled Mutability Manager Pointer */
212 /* The stack type is a null literal*/
213 NULL_LITERAL_MASK = 0x400,
215 /**Used by ldarg.0 and family to let delegate verification happens.*/
216 THIS_POINTER_MASK = 0x800,
218 /**Signals that this is a boxed value type*/
221 /*This is an unitialized this ref*/
222 UNINIT_THIS_MASK = 0x2000,
225 static const char* const
226 type_names [TYPE_MAX + 1] = {
237 PREFIX_UNALIGNED = 1,
240 PREFIX_CONSTRAINED = 8,
243 //////////////////////////////////////////////////////////////////
246 /*Token validation macros and functions */
247 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
248 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
249 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
250 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
252 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
253 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
254 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
255 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
256 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
257 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
260 * Verify if @token refers to a valid row on int's table.
263 token_bounds_check (MonoImage *image, guint32 token)
266 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
267 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
271 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
273 MonoType *res = g_new0 (MonoType, 1);
274 //FIXME use mono_method_get_signature_full
275 res->data.method = mono_method_signature (method);
276 res->type = MONO_TYPE_FNPTR;
277 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
282 * mono_type_is_enum_type:
284 * Returns TRUE if @type is an enum type.
287 mono_type_is_enum_type (MonoType *type)
289 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
291 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
297 * mono_type_is_value_type:
299 * Returns TRUE if @type is named after @namespace.@name.
303 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
305 return type->type == MONO_TYPE_VALUETYPE &&
306 !strcmp (namespace, type->data.klass->name_space) &&
307 !strcmp (name, type->data.klass->name);
311 * Returns TURE if @type is VAR or MVAR
314 mono_type_is_generic_argument (MonoType *type)
316 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
320 * mono_type_get_underlying_type_any:
322 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
324 * Returns the underlying type of @type regardless if it is byref or not.
327 mono_type_get_underlying_type_any (MonoType *type)
329 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
330 return type->data.klass->enum_basetype;
331 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
332 return type->data.generic_class->container_class->enum_basetype;
337 mono_type_get_stack_name (MonoType *type)
339 return type_names [get_stack_type (type) & TYPE_MASK];
342 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
343 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
346 mono_method_is_constructor (MonoMethod *method)
348 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
349 !(method->flags & CTOR_INVALID_FLAGS) &&
350 !strcmp (".ctor", method->name));
354 mono_class_has_default_constructor (MonoClass *klass)
359 mono_class_setup_methods (klass);
361 for (i = 0; i < klass->method.count; ++i) {
362 method = klass->methods [i];
363 if (mono_method_is_constructor (method) &&
364 mono_method_signature (method)->param_count == 0 &&
365 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
372 mono_class_interface_implements_interface (MonoClass *candidate, MonoClass *iface)
375 if (candidate == iface)
377 for (i = 0; i < candidate->interface_count; ++i) {
378 if (candidate->interfaces [i] == iface || mono_class_interface_implements_interface (candidate->interfaces [i], iface))
385 * Test if @candidate is a subtype of @target using the minimal possible information
386 * TODO move the code for non finished TypeBuilders to here.
389 mono_class_is_constraint_compatible (MonoClass *candidate, MonoClass *target)
391 if (candidate == target)
393 if (target == mono_defaults.object_class)
396 //setup_supertypes don't mono_class_init anything
397 mono_class_setup_supertypes (candidate);
398 mono_class_setup_supertypes (target);
400 if (mono_class_has_parent (candidate, target))
403 //if target is not a supertype it must be an interface
404 if (!MONO_CLASS_IS_INTERFACE (target))
407 if (candidate->image->dynamic && !candidate->wastypebuilder) {
408 MonoReflectionTypeBuilder *tb = candidate->reflection_info;
410 if (tb->interfaces) {
411 for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
412 MonoReflectionType *iface = mono_array_get (tb->interfaces, MonoReflectionType*, j);
413 MonoClass *ifaceClass = mono_class_from_mono_type (iface->type);
414 if (mono_class_is_constraint_compatible (ifaceClass, target)) {
421 return mono_class_interface_implements_interface (candidate, target);
425 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
429 if (ginst->type_argc != gc->type_argc)
432 for (i = 0; i < gc->type_argc; ++i) {
433 MonoGenericParam *param = &gc->type_params [i];
434 MonoClass *paramClass;
435 MonoClass **constraints;
437 if (!param->constraints && !(param->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
439 if (mono_type_is_generic_argument (ginst->type_argv [i]))
440 continue; //it's not our job to validate type variables
442 paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
444 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
447 /*it's not safe to call mono_class_init from here*/
448 if (paramClass->generic_class && !paramClass->inited) {
449 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
453 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
456 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
459 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
462 if (!param->constraints)
465 for (constraints = param->constraints; *constraints; ++constraints) {
466 MonoClass *ctr = *constraints;
469 inflated = mono_class_inflate_generic_type (&ctr->byval_arg, context);
470 ctr = mono_class_from_mono_type (inflated);
471 mono_metadata_free_type (inflated);
473 if (!mono_class_is_constraint_compatible (paramClass, ctr))
481 * Verify if @type is valid for the given @ctx verification context.
482 * this function checks for VAR and MVAR types that are invalid under the current verifier,
483 * This means that it either
486 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
488 if (mono_type_is_generic_argument (type) && !ctx->generic_context)
490 if (type->type == MONO_TYPE_VAR) {
491 if (!ctx->generic_context->class_inst)
493 if (type->data.generic_param->num >= ctx->generic_context->class_inst->type_argc)
495 } else if (type->type == MONO_TYPE_MVAR) {
496 if (!ctx->generic_context->method_inst)
498 if (type->data.generic_param->num >= ctx->generic_context->method_inst->type_argc)
505 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
508 for (i = 0; i < ginst->type_argc; ++i) {
509 MonoType *type = ginst->type_argv [i];
510 if (!is_valid_type_in_context (ctx, type))
517 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
519 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
520 MonoGenericInst *ginst = gmethod->context.method_inst;
521 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
522 if (!gc) /*non-generic inflated method - it's part of a generic type */
524 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
526 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
531 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
533 MonoGenericClass *gklass = klass->generic_class;
534 MonoGenericInst *ginst = gklass->context.class_inst;
535 MonoGenericContainer *gc = gklass->container_class->generic_container;
536 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
538 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
542 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
546 if (!is_valid_type_in_context (ctx, type)) {
547 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type %x at 0x%04x", type->type, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
551 klass = mono_class_from_mono_type (type);
552 mono_class_init (klass);
553 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
554 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
555 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);
557 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);
560 if (!klass->generic_class)
563 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
564 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);
568 if (!is_valid_generic_instantiation_in_context (ctx, mono_class_get_context (klass)->class_inst)) {
569 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_BAD_IMAGE);
576 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
578 MonoGenericInst *ginst;
580 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg)) {
581 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid method %s.%s::%s at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
585 if (!method->is_inflated)
588 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
589 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic 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);
593 ginst = mono_method_get_context (method)->method_inst;
594 if (ginst && !is_valid_generic_instantiation_in_context (ctx, ginst)) {
595 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic instantiation of method %s.%s::%s at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
602 static MonoClassField*
603 verifier_load_field (VerifyContext *ctx, int token, MonoClass **klass, const char *opcode) {
604 MonoClassField *field;
606 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
607 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);
611 field = mono_field_from_token (ctx->image, token, klass, ctx->generic_context);
613 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);
617 if (!mono_type_is_valid_in_context (ctx, &field->parent->byval_arg))
624 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
627 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
628 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);
632 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
635 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);
639 if (!mono_method_is_valid_in_context (ctx, method))
646 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
649 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
650 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
654 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
657 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);
661 if (!mono_type_is_valid_in_context (ctx, type))
668 /* stack_slot_get_type:
670 * Returns the stack type of @value. This value includes POINTER_MASK.
672 * Use this function to checks that account for a managed pointer.
675 stack_slot_get_type (ILStackDesc *value)
677 return value->stype & RAW_TYPE_MASK;
680 /* stack_slot_get_underlying_type:
682 * Returns the stack type of @value. This value does not include POINTER_MASK.
684 * Use this function is cases where the fact that the value could be a managed pointer is
685 * irrelevant. For example, field load doesn't care about this fact of type on stack.
688 stack_slot_get_underlying_type (ILStackDesc *value)
690 return value->stype & TYPE_MASK;
693 /* stack_slot_is_managed_pointer:
695 * Returns TRUE is @value is a managed pointer.
698 stack_slot_is_managed_pointer (ILStackDesc *value)
700 return (value->stype & POINTER_MASK) == POINTER_MASK;
703 /* stack_slot_is_managed_mutability_pointer:
705 * Returns TRUE is @value is a managed mutability pointer.
707 static G_GNUC_UNUSED gboolean
708 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
710 return (value->stype & CMMP_MASK) == CMMP_MASK;
713 /* stack_slot_is_null_literal:
715 * Returns TRUE is @value is the null literal.
718 stack_slot_is_null_literal (ILStackDesc *value)
720 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
724 /* stack_slot_is_this_pointer:
726 * Returns TRUE is @value is the this literal
729 stack_slot_is_this_pointer (ILStackDesc *value)
731 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
734 /* stack_slot_is_boxed_value:
736 * Returns TRUE is @value is a boxed value
739 stack_slot_is_boxed_value (ILStackDesc *value)
741 return (value->stype & BOXED_MASK) == BOXED_MASK;
745 stack_slot_get_name (ILStackDesc *value)
747 return type_names [value->stype & TYPE_MASK];
749 //////////////////////////////////////////////////////////////////
751 mono_free_verify_list (GSList *list)
753 MonoVerifyInfoExtended *info;
756 for (tmp = list; tmp; tmp = tmp->next) {
758 g_free (info->info.message);
764 #define ADD_ERROR(list,msg) \
766 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
767 vinfo->info.status = MONO_VERIFY_ERROR; \
768 vinfo->info.message = (msg); \
769 (list) = g_slist_prepend ((list), vinfo); \
772 #define ADD_WARN(list,code,msg) \
774 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
775 vinfo->info.status = (code); \
776 vinfo->info.message = (msg); \
777 (list) = g_slist_prepend ((list), vinfo); \
781 valid_cultures[][9] = {
782 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
783 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
784 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
785 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
786 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
787 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
788 "da-DK", "de-DE", "de-CH", "de-AT",
789 "de-LU", "de-LI", "el-GR", "en-US",
790 "en-GB", "en-AU", "en-CA", "en-NZ",
791 "en-IE", "en-ZA", "en-JM", "en-CB",
792 "en-BZ", "en-TT", "en-ZW", "en-PH",
793 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
794 "es-CR", "es-PA", "es-DO", "es-VE",
795 "es-CO", "es-PE", "es-AR", "es-EC",
796 "es-CL", "es-UY", "es-PY", "es-BO",
797 "es-SV", "es-HN", "es-NI", "es-PR",
798 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
799 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
800 "hu-HU", "is-IS", "it-IT", "it-CH",
801 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
802 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
803 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
804 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
805 "sv-SE", "sv-FI", "th-TH", "tr-TR",
806 "ur-PK", "id-ID", "uk-UA", "be-BY",
807 "sl-SI", "et-EE", "lv-LV", "lt-LT",
808 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
810 "eu-ES", "mk-MK", "af-ZA",
811 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
812 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
813 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
814 "gu-IN", "ta-IN", "te-IN", "kn-IN",
815 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
816 "kok-IN", "syr-SY", "div-MV"
820 is_valid_culture (const char *cname)
826 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
827 if (g_strcasecmp (valid_cultures [i], cname)) {
836 is_valid_assembly_flags (guint32 flags) {
837 /* Metadata: 22.1.2 */
838 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
839 return ((flags == 1) || (flags == 0));
843 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
846 const char *p, *blob_end;
848 if (blob_index >= image->heap_blob.size)
850 p = mono_metadata_blob_heap (image, blob_index);
851 size = mono_metadata_decode_blob_size (p, &blob_end);
852 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
854 if (notnull && !size)
860 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
862 const char *p, *blob_end, *res;
864 if (str_index >= image->heap_strings.size)
866 res = p = mono_metadata_string_heap (image, str_index);
867 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
871 * FIXME: should check it's a valid utf8 string, too.
873 while (p <= blob_end) {
878 return *p? NULL: res;
882 is_valid_cls_ident (const char *p)
885 * FIXME: we need the full unicode glib support for this.
886 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
887 * We do the lame thing for now.
893 if (!isalnum (*p) && *p != '_')
901 is_valid_filename (const char *p)
905 return strpbrk (p, "\\//:")? 0: 1;
909 verify_assembly_table (MonoImage *image, GSList *list, int level)
911 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
912 guint32 cols [MONO_ASSEMBLY_SIZE];
915 if (level & MONO_VERIFY_ERROR) {
917 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
918 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
920 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
921 case ASSEMBLY_HASH_NONE:
922 case ASSEMBLY_HASH_MD5:
923 case ASSEMBLY_HASH_SHA1:
926 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
929 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
930 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
932 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
933 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
935 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
936 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
938 if (strpbrk (p, ":\\/."))
939 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
942 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
943 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
945 if (!is_valid_culture (p))
946 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
953 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
955 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
956 guint32 cols [MONO_ASSEMBLYREF_SIZE];
960 if (level & MONO_VERIFY_ERROR) {
961 for (i = 0; i < t->rows; ++i) {
962 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
963 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
964 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
966 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
967 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
969 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
970 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
972 if (!is_valid_culture (p))
973 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
976 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
977 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
980 if (level & MONO_VERIFY_WARNING) {
981 /* check for duplicated rows */
982 for (i = 0; i < t->rows; ++i) {
989 verify_class_layout_table (MonoImage *image, GSList *list, int level)
991 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
992 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
993 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
996 if (level & MONO_VERIFY_ERROR) {
997 for (i = 0; i < t->rows; ++i) {
998 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
1000 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
1001 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
1003 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
1004 if (value & TYPE_ATTRIBUTE_INTERFACE)
1005 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
1006 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
1007 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
1008 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
1009 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
1010 case 0: case 1: case 2: case 4: case 8: case 16:
1011 case 32: case 64: case 128: break;
1013 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
1015 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1017 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
1018 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
1019 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));
1023 * FIXME: we need to check that if class size != 0,
1024 * it needs to be greater than the class calculated size.
1025 * If parent is a valuetype it also needs to be smaller than
1026 * 1 MByte (0x100000 bytes).
1027 * To do both these checks we need to load the referenced
1028 * assemblies, though (the spec claims we didn't have to, bah).
1031 * We need to check that the parent types have the same layout
1042 verify_constant_table (MonoImage *image, GSList *list, int level)
1044 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
1045 guint32 cols [MONO_CONSTANT_SIZE];
1047 GHashTable *dups = g_hash_table_new (NULL, NULL);
1049 for (i = 0; i < t->rows; ++i) {
1050 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
1052 if (level & MONO_VERIFY_ERROR)
1053 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
1054 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
1055 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
1056 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
1058 switch (cols [MONO_CONSTANT_TYPE]) {
1059 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
1063 if (level & MONO_VERIFY_CLS)
1064 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));
1065 case MONO_TYPE_BOOLEAN:
1066 case MONO_TYPE_CHAR:
1073 case MONO_TYPE_STRING:
1074 case MONO_TYPE_CLASS:
1077 if (level & MONO_VERIFY_ERROR)
1078 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
1080 if (level & MONO_VERIFY_ERROR) {
1081 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
1082 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
1083 case MONO_HASCONSTANT_FIEDDEF:
1084 if (value > image->tables [MONO_TABLE_FIELD].rows)
1085 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
1087 case MONO_HASCONSTANT_PARAM:
1088 if (value > image->tables [MONO_TABLE_PARAM].rows)
1089 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
1091 case MONO_HASCONSTANT_PROPERTY:
1092 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
1093 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
1096 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
1100 if (level & MONO_VERIFY_CLS) {
1102 * FIXME: verify types is consistent with the enum type
1103 * is parent is an enum.
1107 g_hash_table_destroy (dups);
1112 verify_event_map_table (MonoImage *image, GSList *list, int level)
1114 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
1115 guint32 cols [MONO_EVENT_MAP_SIZE];
1116 guint32 i, last_event;
1117 GHashTable *dups = g_hash_table_new (NULL, NULL);
1121 for (i = 0; i < t->rows; ++i) {
1122 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
1123 if (level & MONO_VERIFY_ERROR)
1124 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
1125 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1126 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
1127 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
1128 if (level & MONO_VERIFY_ERROR) {
1129 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
1130 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1131 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
1132 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
1134 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
1135 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
1136 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
1140 g_hash_table_destroy (dups);
1145 verify_event_table (MonoImage *image, GSList *list, int level)
1147 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
1148 guint32 cols [MONO_EVENT_SIZE];
1152 for (i = 0; i < t->rows; ++i) {
1153 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
1155 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
1156 if (level & MONO_VERIFY_ERROR)
1157 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
1159 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
1160 if (level & MONO_VERIFY_ERROR)
1161 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
1163 if (level & MONO_VERIFY_CLS) {
1164 if (!is_valid_cls_ident (p))
1165 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
1169 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
1170 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
1171 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
1172 case MONO_TYPEDEFORREF_TYPEDEF:
1173 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
1174 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1176 case MONO_TYPEDEFORREF_TYPEREF:
1177 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
1178 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1180 case MONO_TYPEDEFORREF_TYPESPEC:
1181 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
1182 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1185 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1189 * FIXME: check that there is 1 add and remove row in methodsemantics
1190 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
1191 * these while checking methodsemantics).
1192 * check for duplicated names for the same type [ERROR]
1193 * check for CLS duplicate names for the same type [CLS]
1200 verify_field_table (MonoImage *image, GSList *list, int level)
1202 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
1203 guint32 cols [MONO_FIELD_SIZE];
1207 for (i = 0; i < t->rows; ++i) {
1208 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
1210 * Check this field has only one owner and that the owner is not
1211 * an interface (done in verify_typedef_table() )
1213 flags = cols [MONO_FIELD_FLAGS];
1214 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
1215 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
1216 case FIELD_ATTRIBUTE_PRIVATE:
1217 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
1218 case FIELD_ATTRIBUTE_ASSEMBLY:
1219 case FIELD_ATTRIBUTE_FAMILY:
1220 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
1221 case FIELD_ATTRIBUTE_PUBLIC:
1224 if (level & MONO_VERIFY_ERROR)
1225 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
1228 if (level & MONO_VERIFY_ERROR) {
1229 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
1230 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
1231 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
1232 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
1233 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
1234 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
1236 * FIXME: check there is only one owner in the respective table.
1237 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
1238 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
1239 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
1242 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
1243 if (level & MONO_VERIFY_ERROR)
1244 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
1246 if (level & MONO_VERIFY_CLS) {
1247 if (!is_valid_cls_ident (p))
1248 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
1253 * if owner is module needs to be static, access mask needs to be compilercontrolled,
1254 * public or private (not allowed in cls mode).
1255 * if owner is an enum ...
1264 verify_file_table (MonoImage *image, GSList *list, int level)
1266 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1267 guint32 cols [MONO_FILE_SIZE];
1270 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1272 for (i = 0; i < t->rows; ++i) {
1273 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
1274 if (level & MONO_VERIFY_ERROR) {
1275 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
1276 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
1277 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
1278 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
1280 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
1281 if (level & MONO_VERIFY_ERROR)
1282 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
1284 if (level & MONO_VERIFY_ERROR) {
1285 if (!is_valid_filename (p))
1286 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
1287 else if (g_hash_table_lookup (dups, p)) {
1288 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
1290 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1294 * FIXME: I don't understand what this means:
1295 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
1296 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
1300 if (level & MONO_VERIFY_WARNING) {
1301 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
1302 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
1304 g_hash_table_destroy (dups);
1309 verify_moduleref_table (MonoImage *image, GSList *list, int level)
1311 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
1312 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
1313 guint32 cols [MONO_MODULEREF_SIZE];
1315 guint32 found, i, j, value;
1316 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1318 for (i = 0; i < t->rows; ++i) {
1319 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
1320 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
1321 if (level & MONO_VERIFY_ERROR)
1322 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
1324 if (level & MONO_VERIFY_ERROR) {
1325 if (!is_valid_filename (p))
1326 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
1327 else if (g_hash_table_lookup (dups, p)) {
1328 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
1329 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1331 for (j = 0; j < tfile->rows; ++j) {
1332 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
1333 if ((pf = is_valid_string (image, value, TRUE)))
1334 if (strcmp (p, pf) == 0) {
1340 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
1345 g_hash_table_destroy (dups);
1350 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
1352 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
1353 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1357 for (i = 0; i < t->rows; ++i) {
1358 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1359 if (level & MONO_VERIFY_ERROR) {
1360 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
1361 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
1363 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
1364 /* FIXME: check it's a valid locals or method sig.*/
1372 mono_image_verify_tables (MonoImage *image, int level)
1374 GSList *error_list = NULL;
1376 error_list = verify_assembly_table (image, error_list, level);
1378 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
1379 * AssemblyRefProcessor should be ignored,
1380 * though we may want to emit a warning, since it should not
1381 * be present in a PE file.
1383 error_list = verify_assemblyref_table (image, error_list, level);
1384 error_list = verify_class_layout_table (image, error_list, level);
1385 error_list = verify_constant_table (image, error_list, level);
1387 * cutom attribute, declsecurity
1389 error_list = verify_event_map_table (image, error_list, level);
1390 error_list = verify_event_table (image, error_list, level);
1391 error_list = verify_field_table (image, error_list, level);
1392 error_list = verify_file_table (image, error_list, level);
1393 error_list = verify_moduleref_table (image, error_list, level);
1394 error_list = verify_standalonesig_table (image, error_list, level);
1396 return g_slist_reverse (error_list);
1399 #define ADD_INVALID(list,msg) \
1401 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1402 vinfo->status = MONO_VERIFY_ERROR; \
1403 vinfo->message = (msg); \
1404 (list) = g_slist_prepend ((list), vinfo); \
1405 /*G_BREAKPOINT ();*/ \
1409 #define CHECK_STACK_UNDERFLOW(num) \
1411 if (cur_stack < (num)) \
1412 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1415 #define CHECK_STACK_OVERFLOW() \
1417 if (cur_stack >= max_stack) \
1418 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1423 in_any_block (MonoMethodHeader *header, guint offset)
1426 MonoExceptionClause *clause;
1428 for (i = 0; i < header->num_clauses; ++i) {
1429 clause = &header->clauses [i];
1430 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1432 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1434 if (MONO_OFFSET_IN_FILTER (clause, offset))
1441 * in_any_exception_block:
1443 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1446 in_any_exception_block (MonoMethodHeader *header, guint offset)
1449 MonoExceptionClause *clause;
1451 for (i = 0; i < header->num_clauses; ++i) {
1452 clause = &header->clauses [i];
1453 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1455 if (MONO_OFFSET_IN_FILTER (clause, offset))
1462 * is_valid_branch_instruction:
1464 * Verify if it's valid to perform a branch from @offset to @target.
1465 * This should be used with br and brtrue/false.
1466 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1467 * The major diferent from other similiar functions is that branching into a
1468 * finally/fault block is invalid instead of just unverifiable.
1471 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1474 MonoExceptionClause *clause;
1476 for (i = 0; i < header->num_clauses; ++i) {
1477 clause = &header->clauses [i];
1478 /*branching into a finally block is invalid*/
1479 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1480 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1481 MONO_OFFSET_IN_HANDLER (clause, target))
1484 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1486 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1488 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1495 * is_valid_cmp_branch_instruction:
1497 * Verify if it's valid to perform a branch from @offset to @target.
1498 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1499 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1501 * The major diferences from other similar functions are that most errors lead to invalid
1502 * code and only branching out of finally, filter or fault clauses is unverifiable.
1505 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1508 MonoExceptionClause *clause;
1510 for (i = 0; i < header->num_clauses; ++i) {
1511 clause = &header->clauses [i];
1512 /*branching out of a handler or finally*/
1513 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1514 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1515 !MONO_OFFSET_IN_HANDLER (clause, target))
1518 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1520 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1522 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1529 * A leave can't escape a finally block
1532 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1535 MonoExceptionClause *clause;
1537 for (i = 0; i < header->num_clauses; ++i) {
1538 clause = &header->clauses [i];
1539 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1541 if (MONO_OFFSET_IN_FILTER (clause, offset))
1548 * A rethrow can't happen outside of a catch handler.
1551 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1554 MonoExceptionClause *clause;
1556 for (i = 0; i < header->num_clauses; ++i) {
1557 clause = &header->clauses [i];
1558 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1560 if (MONO_OFFSET_IN_FILTER (clause, offset))
1567 * An endfinally can't happen outside of a finally/fault handler.
1570 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1573 MonoExceptionClause *clause;
1575 for (i = 0; i < header->num_clauses; ++i) {
1576 clause = &header->clauses [i];
1577 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1585 * An endfilter can only happens inside a filter clause.
1586 * In non-strict mode filter is allowed inside the handler clause too
1588 static MonoExceptionClause *
1589 is_correct_endfilter (VerifyContext *ctx, guint offset)
1592 MonoExceptionClause *clause;
1594 for (i = 0; i < ctx->header->num_clauses; ++i) {
1595 clause = &ctx->header->clauses [i];
1596 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1598 if (MONO_OFFSET_IN_FILTER (clause, offset))
1600 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1608 * Non-strict endfilter can happens inside a try block or any handler block
1611 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1614 MonoExceptionClause *clause;
1616 for (i = 0; i < ctx->header->num_clauses; ++i) {
1617 clause = &ctx->header->clauses [i];
1618 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1625 is_valid_bool_arg (ILStackDesc *arg)
1627 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1631 switch (stack_slot_get_underlying_type (arg)) {
1634 case TYPE_NATIVE_INT:
1638 g_assert (arg->type);
1639 switch (arg->type->type) {
1640 case MONO_TYPE_CLASS:
1641 case MONO_TYPE_STRING:
1642 case MONO_TYPE_OBJECT:
1643 case MONO_TYPE_SZARRAY:
1644 case MONO_TYPE_ARRAY:
1645 case MONO_TYPE_FNPTR:
1648 case MONO_TYPE_GENERICINST:
1649 /*We need to check if the container class
1650 * of the generic type is a valuetype, iow:
1651 * is it a "class Foo<T>" or a "struct Foo<T>"?
1653 return !arg->type->data.generic_class->container_class->valuetype;
1661 /*Type manipulation helper*/
1663 /*Returns the byref version of the supplied MonoType*/
1665 mono_type_get_type_byref (MonoType *type)
1669 return &mono_class_from_mono_type (type)->this_arg;
1673 /*Returns the byval version of the supplied MonoType*/
1675 mono_type_get_type_byval (MonoType *type)
1679 return &mono_class_from_mono_type (type)->byval_arg;
1683 mono_type_from_stack_slot (ILStackDesc *slot)
1685 if (stack_slot_is_managed_pointer (slot))
1686 return mono_type_get_type_byref (slot->type);
1690 /*Stack manipulation code*/
1693 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1695 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1698 state->flags |= IL_CODE_FLAG_STACK_INITED;
1700 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1704 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1706 to->size = from->size;
1707 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1711 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1713 to->stype = from->stype;
1714 to->type = from->type;
1715 to->method = from->method;
1719 check_underflow (VerifyContext *ctx, int size)
1721 if (ctx->eval.size < size) {
1722 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1729 check_overflow (VerifyContext *ctx)
1731 if (ctx->eval.size >= ctx->max_stack) {
1732 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1739 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1741 if (stack_slot_get_type (value) == TYPE_PTR) {
1742 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1749 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1751 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1752 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1759 static ILStackDesc *
1760 stack_push (VerifyContext *ctx)
1762 return & ctx->eval.stack [ctx->eval.size++];
1765 static ILStackDesc *
1766 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1768 ILStackDesc *top = stack_push (ctx);
1774 static ILStackDesc *
1775 stack_pop (VerifyContext *ctx)
1777 ILStackDesc *ret = ctx->eval.stack + --ctx->eval.size;
1778 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1779 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1783 /* This function allows to safely pop an unititialized this ptr from
1784 * the eval stack without marking the method as unverifiable.
1786 static ILStackDesc *
1787 stack_pop_safe (VerifyContext *ctx)
1789 return ctx->eval.stack + --ctx->eval.size;
1792 static ILStackDesc *
1793 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1795 ILStackDesc *top = stack_push (ctx);
1796 copy_stack_value (top, value);
1800 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1802 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1805 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1810 if (!(type = verifier_load_type (ctx, token, opcode)))
1813 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1814 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1818 if (type->type == MONO_TYPE_VOID) {
1819 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1823 if (type->type == MONO_TYPE_TYPEDBYREF)
1824 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1826 check_unverifiable_type (ctx, type);
1831 /*operation result tables */
1833 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1834 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1835 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1836 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1837 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1838 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1839 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1842 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1843 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1844 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1845 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1846 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1847 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1848 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1851 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1852 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1853 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1854 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1855 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1856 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1857 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1860 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1861 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1862 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1863 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1864 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1865 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1866 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1869 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1870 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1871 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1872 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1873 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1874 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1875 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1878 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1879 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1880 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1881 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1882 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1883 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1884 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1887 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1888 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1889 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1890 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1891 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1892 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1893 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1896 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1897 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1898 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1899 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1900 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1901 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1902 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1905 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1906 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1907 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1908 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1909 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1910 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1911 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1914 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1915 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1916 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1917 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1918 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1919 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1920 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1923 #ifdef MONO_VERIFIER_DEBUG
1927 dump_stack_value (ILStackDesc *value)
1929 printf ("[(%x)(%x)", value->type->type, value->stype);
1931 if (stack_slot_is_this_pointer (value))
1934 if (stack_slot_is_boxed_value (value))
1935 printf ("[boxed] ");
1937 if (stack_slot_is_null_literal (value))
1940 if (stack_slot_is_managed_mutability_pointer (value))
1941 printf ("Controled Mutability MP: ");
1943 if (stack_slot_is_managed_pointer (value))
1944 printf ("Managed Pointer to: ");
1946 switch (stack_slot_get_underlying_type (value)) {
1948 printf ("invalid type]");
1956 case TYPE_NATIVE_INT:
1957 printf ("native int]");
1960 printf ("float64]");
1963 printf ("unmanaged pointer]");
1966 switch (value->type->type) {
1967 case MONO_TYPE_CLASS:
1968 case MONO_TYPE_VALUETYPE:
1969 printf ("complex] (%s)", value->type->data.klass->name);
1971 case MONO_TYPE_STRING:
1972 printf ("complex] (string)");
1974 case MONO_TYPE_OBJECT:
1975 printf ("complex] (object)");
1977 case MONO_TYPE_SZARRAY:
1978 printf ("complex] (%s [])", value->type->data.klass->name);
1980 case MONO_TYPE_ARRAY:
1981 printf ("complex] (%s [%d %d %d])",
1982 value->type->data.array->eklass->name,
1983 value->type->data.array->rank,
1984 value->type->data.array->numsizes,
1985 value->type->data.array->numlobounds);
1987 case MONO_TYPE_GENERICINST:
1988 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1991 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1993 case MONO_TYPE_MVAR:
1994 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1997 //should be a boxed value
1998 char * name = mono_type_full_name (value->type);
1999 printf ("complex] %s", name);
2005 printf ("unknown stack %x type]\n", value->stype);
2006 g_assert_not_reached ();
2011 dump_stack_state (ILCodeDesc *state)
2015 printf ("(%d) ", state->size);
2016 for (i = 0; i < state->size; ++i)
2017 dump_stack_value (state->stack + i);
2022 /*Returns TRUE if candidate array type can be assigned to target.
2023 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
2026 is_array_type_compatible (MonoType *target, MonoType *candidate)
2029 MonoArrayType *left = target->data.array;
2030 MonoArrayType *right = candidate->data.array;
2032 g_assert (target->type == MONO_TYPE_ARRAY);
2033 g_assert (candidate->type == MONO_TYPE_ARRAY);
2036 if ((left->rank != right->rank) ||
2037 (left->numsizes != right->numsizes) ||
2038 (left->numlobounds != right->numlobounds))
2041 for (i = 0; i < left->numsizes; ++i)
2042 if (left->sizes [i] != right->sizes [i])
2045 for (i = 0; i < left->numlobounds; ++i)
2046 if (left->lobounds [i] != right->lobounds [i])
2049 return mono_class_is_assignable_from (left->eklass, right->eklass);
2053 get_stack_type (MonoType *type)
2056 int type_kind = type->type;
2058 mask = POINTER_MASK;
2059 /*TODO handle CMMP_MASK */
2062 switch (type_kind) {
2065 case MONO_TYPE_BOOLEAN:
2068 case MONO_TYPE_CHAR:
2071 return TYPE_I4 | mask;
2075 return TYPE_NATIVE_INT | mask;
2077 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
2078 case MONO_TYPE_FNPTR:
2080 case MONO_TYPE_TYPEDBYREF:
2081 return TYPE_PTR | mask;
2084 case MONO_TYPE_MVAR:
2086 case MONO_TYPE_CLASS:
2087 case MONO_TYPE_STRING:
2088 case MONO_TYPE_OBJECT:
2089 case MONO_TYPE_SZARRAY:
2090 case MONO_TYPE_ARRAY:
2091 return TYPE_COMPLEX | mask;
2093 case MONO_TYPE_GENERICINST:
2094 if (mono_type_is_enum_type (type)) {
2095 type = mono_type_get_underlying_type_any (type);
2096 type_kind = type->type;
2099 return TYPE_COMPLEX | mask;
2104 return TYPE_I8 | mask;
2108 return TYPE_R8 | mask;
2110 case MONO_TYPE_VALUETYPE:
2111 if (mono_type_is_enum_type (type)) {
2112 type = mono_type_get_underlying_type_any (type);
2113 type_kind = type->type;
2116 return TYPE_COMPLEX | mask;
2120 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
2121 g_assert_not_reached ();
2126 /* convert MonoType to ILStackDesc format (stype) */
2128 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
2131 int type_kind = type->type;
2133 if (type->byref || take_addr)
2134 mask = POINTER_MASK;
2135 /* TODO handle CMMP_MASK */
2140 switch (type_kind) {
2143 case MONO_TYPE_BOOLEAN:
2146 case MONO_TYPE_CHAR:
2149 stack->stype = TYPE_I4 | mask;
2153 stack->stype = TYPE_NATIVE_INT | mask;
2156 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2157 case MONO_TYPE_FNPTR:
2159 case MONO_TYPE_TYPEDBYREF:
2160 stack->stype = TYPE_PTR | mask;
2163 case MONO_TYPE_CLASS:
2164 case MONO_TYPE_STRING:
2165 case MONO_TYPE_OBJECT:
2166 case MONO_TYPE_SZARRAY:
2167 case MONO_TYPE_ARRAY:
2170 case MONO_TYPE_MVAR:
2171 stack->stype = TYPE_COMPLEX | mask;
2174 case MONO_TYPE_GENERICINST:
2175 if (mono_type_is_enum_type (type)) {
2176 type = mono_type_get_underlying_type_any (type);
2177 type_kind = type->type;
2180 stack->stype = TYPE_COMPLEX | mask;
2186 stack->stype = TYPE_I8 | mask;
2190 stack->stype = TYPE_R8 | mask;
2192 case MONO_TYPE_VALUETYPE:
2193 if (mono_type_is_enum_type (type)) {
2194 type = mono_type_get_underlying_type_any (type);
2195 type_kind = type->type;
2198 stack->stype = TYPE_COMPLEX | mask;
2202 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
2203 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
2210 * init_stack_with_value_at_exception_boundary:
2212 * Initialize the stack and push a given type.
2213 * The instruction is marked as been on the exception boundary.
2216 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
2218 MonoType *type = mono_class_inflate_generic_type (&klass->byval_arg, ctx->generic_context);
2219 stack_init (ctx, code);
2220 set_stack_value (ctx, code->stack, type, FALSE);
2221 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
2223 code->flags |= IL_CODE_FLAG_WAS_TARGET;
2226 /*Verify if type 'candidate' can be stored in type 'target'.
2228 * If strict, check for the underlying type and not the verification stack types
2231 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
2233 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2234 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2236 MonoType *original_candidate = candidate;
2237 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); );
2239 /*only one is byref */
2240 if (candidate->byref ^ target->byref) {
2241 /* converting from native int to byref*/
2242 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2243 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2248 strict |= target->byref;
2249 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2250 candidate = mono_type_get_underlying_type_any (candidate);
2253 switch (target->type) {
2254 case MONO_TYPE_VOID:
2255 return candidate->type == MONO_TYPE_VOID;
2258 case MONO_TYPE_BOOLEAN:
2260 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2263 case MONO_TYPE_CHAR:
2265 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2267 case MONO_TYPE_U4: {
2268 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2269 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2271 return is_native_int || is_int4;
2272 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2277 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2282 return candidate->type == target->type;
2283 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2287 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2288 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2290 return is_native_int || is_int4;
2291 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2295 if (!IS_STRICT_MODE (ctx) && IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U))
2297 if (candidate->type != MONO_TYPE_PTR)
2299 /* check the underlying type */
2300 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2302 case MONO_TYPE_FNPTR: {
2303 MonoMethodSignature *left, *right;
2304 if (candidate->type != MONO_TYPE_FNPTR)
2307 left = mono_type_get_signature (target);
2308 right = mono_type_get_signature (candidate);
2309 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2312 case MONO_TYPE_GENERICINST: {
2313 MonoClass *target_klass;
2314 MonoClass *candidate_klass;
2315 if (mono_type_is_enum_type (target)) {
2316 target = mono_type_get_underlying_type_any (target);
2319 target_klass = mono_class_from_mono_type (target);
2320 candidate_klass = mono_class_from_mono_type (candidate);
2321 if (mono_class_is_nullable (target_klass)) {
2322 if (!mono_class_is_nullable (candidate_klass))
2324 return target_klass == candidate_klass;
2327 return mono_class_is_assignable_from (target_klass, candidate_klass);
2330 case MONO_TYPE_STRING:
2331 return candidate->type == MONO_TYPE_STRING;
2333 case MONO_TYPE_CLASS:
2335 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2336 * to take boxing status into account.
2338 if (mono_type_is_generic_argument (original_candidate))
2340 /* If candidate is an enum it should return true for System.Enum and supertypes.
2341 * That's why here we use the original type and not the underlying type.
2343 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2345 case MONO_TYPE_OBJECT:
2346 return MONO_TYPE_IS_REFERENCE (candidate);
2348 case MONO_TYPE_SZARRAY: {
2351 if (candidate->type != MONO_TYPE_SZARRAY)
2354 left = target->data.array->eklass;
2355 right = candidate->data.array->eklass;
2356 return mono_class_is_assignable_from(left, right);
2359 case MONO_TYPE_ARRAY:
2360 if (candidate->type != MONO_TYPE_ARRAY)
2362 return is_array_type_compatible (target, candidate);
2364 case MONO_TYPE_TYPEDBYREF:
2365 return candidate->type == MONO_TYPE_TYPEDBYREF;
2367 case MONO_TYPE_VALUETYPE:
2368 if (candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass)
2370 if (mono_type_is_enum_type (target)) {
2371 target = mono_type_get_underlying_type_any (target);
2377 if (candidate->type != MONO_TYPE_VAR)
2379 return candidate->data.generic_param->num == target->data.generic_param->num;
2381 case MONO_TYPE_MVAR:
2382 if (candidate->type != MONO_TYPE_MVAR)
2384 return candidate->data.generic_param->num == target->data.generic_param->num;
2387 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2388 g_assert_not_reached ();
2397 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2399 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2403 * Returns the generic param bound to the context been verified.
2406 static MonoGenericParam*
2407 get_generic_param (VerifyContext *ctx, MonoType *param)
2409 guint16 param_num = param->data.generic_param->num;
2410 if (param->type == MONO_TYPE_VAR) {
2411 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2412 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2415 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2418 /*param must be a MVAR */
2419 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2420 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2423 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2427 * is_compatible_boxed_valuetype:
2429 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2431 * @type The source type. It it tested to be of the proper type.
2432 * @candidate type of the boxed valuetype.
2433 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2434 * @type_must_be_object if TRUE @type must be System.Object, otherwise can be any reference type.
2438 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean type_must_be_object)
2440 if (mono_type_is_generic_argument (candidate) && stack_slot_is_boxed_value (stack) && !type->byref) {
2441 MonoGenericParam *param = get_generic_param (ctx, candidate);
2443 for (class = param->constraints; class && *class; ++class) {
2444 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (& (*class)->byval_arg), FALSE))
2449 if (!type_must_be_object && !MONO_TYPE_IS_REFERENCE (type))
2451 return !type->byref && !candidate->byref && stack_slot_is_boxed_value (stack);
2455 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean strict, gboolean drop_byref)
2457 MonoType *candidate = mono_type_from_stack_slot (stack);
2458 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2461 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2465 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), strict);
2467 return verify_type_compatibility_full (ctx, type, candidate, strict);
2471 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2473 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2477 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2479 if (candidate->byref ^ target->byref)
2482 switch (target->type) {
2483 case MONO_TYPE_VOID:
2486 case MONO_TYPE_BOOLEAN:
2489 case MONO_TYPE_CHAR:
2498 case MONO_TYPE_STRING:
2499 case MONO_TYPE_TYPEDBYREF:
2500 return candidate->type == target->type;
2503 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2505 case MONO_TYPE_FNPTR:
2506 if (candidate->type != MONO_TYPE_FNPTR)
2508 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate));
2510 case MONO_TYPE_GENERICINST: {
2511 MonoClass *target_klass;
2512 MonoClass *candidate_klass;
2513 target_klass = mono_class_from_mono_type (target);
2514 candidate_klass = mono_class_from_mono_type (candidate);
2515 /*FIXME handle nullables and enum*/
2516 return mono_class_is_assignable_from (target_klass, candidate_klass);
2518 case MONO_TYPE_OBJECT:
2519 return MONO_TYPE_IS_REFERENCE (candidate);
2521 case MONO_TYPE_CLASS:
2522 if (candidate->type != MONO_TYPE_CLASS)
2524 return mono_class_is_assignable_from(target->data.klass, candidate->data.klass);
2526 case MONO_TYPE_SZARRAY:
2527 if (candidate->type != MONO_TYPE_SZARRAY)
2529 return mono_class_is_assignable_from (target->data.array->eklass, candidate->data.array->eklass);
2531 case MONO_TYPE_ARRAY:
2532 if (candidate->type != MONO_TYPE_ARRAY)
2534 return is_array_type_compatible (target, candidate);
2536 case MONO_TYPE_VALUETYPE:
2537 /*FIXME handle nullables and enum*/
2538 return candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass;
2541 return candidate->type == MONO_TYPE_VAR && target->data.generic_param->num == candidate->data.generic_param->num;
2544 case MONO_TYPE_MVAR:
2545 return candidate->type == MONO_TYPE_MVAR && target->data.generic_param->num == candidate->data.generic_param->num;
2549 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2550 g_assert_not_reached ();
2556 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2558 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2561 return mono_delegate_type_equal (method, delegate);
2565 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2567 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2570 return mono_delegate_type_equal (delegate, method);
2574 * mono_delegate_signature_equal:
2576 * Compare two signatures in the way expected by delegates.
2578 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2580 * FIXME can this function be eliminated and proper metadata functionality be used?
2583 mono_delegate_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2586 if (sig1->param_count != sig2->param_count)
2589 if (sig1->call_convention != sig2->call_convention)
2592 for (i = 0; i < sig1->param_count; i++) {
2593 MonoType *p1 = sig1->params [i];
2594 MonoType *p2 = sig2->params [i];
2596 if (!mono_delegate_param_equal (p1, p2))
2600 if (!mono_delegate_ret_equal (sig1->ret, sig2->ret))
2607 * verify_ldftn_delegate:
2609 * Verify properties of ldftn based delegates.
2612 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2614 MonoMethod *method = funptr->method;
2616 /*ldftn non-final virtuals only allowed if method is not static,
2617 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2618 * This rules doesn't apply if the object on stack is a boxed valuetype.
2620 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2621 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2622 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2623 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2625 /*current method must not be static*/
2626 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2627 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2629 /*value is the this pointer, loaded using ldarg.0 */
2630 if (!stack_slot_is_this_pointer (value))
2631 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));
2633 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2638 * verify_delegate_compatibility:
2640 * Verify delegate creation sequence.
2644 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2646 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2647 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2649 MonoMethod *invoke, *method;
2650 const guint8 *ip = ctx->header->code;
2651 guint32 ip_offset = ctx->ip_offset;
2653 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2654 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2658 invoke = mono_get_delegate_invoke (delegate);
2659 method = funptr->method;
2661 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method)))
2662 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer parameter for delegate constructor has diferent signature at 0x%04x", ctx->ip_offset));
2665 * Delegate code sequences:
2671 * [-6] ldvirtftn token
2675 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2676 verify_ldftn_delegate (ctx, delegate, value, funptr);
2677 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2678 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2680 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2682 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2685 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, value) && !stack_slot_is_null_literal (value))
2686 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2688 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2689 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2691 #undef IS_VALID_OPCODE
2692 #undef IS_LOAD_FUN_PTR
2695 /* implement the opcode checks*/
2697 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2701 if (arg >= ctx->max_args) {
2703 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2705 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2706 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2707 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2709 } else if (check_overflow (ctx)) {
2710 /*We must let the value be pushed, otherwise we would get an underflow error*/
2711 check_unverifiable_type (ctx, ctx->params [arg]);
2712 if (ctx->params [arg]->byref && take_addr)
2713 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2714 top = stack_push (ctx);
2715 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2718 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2720 ctx->has_this_store = TRUE;
2722 top->stype |= THIS_POINTER_MASK;
2723 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2724 top->stype |= UNINIT_THIS_MASK;
2730 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2732 if (arg >= ctx->num_locals) {
2733 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2734 } else if (check_overflow (ctx)) {
2735 /*We must let the value be pushed, otherwise we would get an underflow error*/
2736 check_unverifiable_type (ctx, ctx->locals [arg]);
2737 if (ctx->locals [arg]->byref && take_addr)
2738 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2740 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2745 store_arg (VerifyContext *ctx, guint32 arg)
2749 if (arg >= ctx->max_args) {
2750 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2751 if (check_underflow (ctx, 1))
2756 if (check_underflow (ctx, 1)) {
2757 value = stack_pop (ctx);
2758 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2759 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2762 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2763 ctx->has_this_store = 1;
2767 store_local (VerifyContext *ctx, guint32 arg)
2770 if (arg >= ctx->num_locals) {
2771 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2775 /*TODO verify definite assigment */
2776 if (check_underflow (ctx, 1)) {
2777 value = stack_pop(ctx);
2778 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2779 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
2780 stack_slot_get_name (value),
2781 mono_type_get_stack_name (ctx->locals [arg]),
2787 /*FIXME add and sub needs special care here*/
2789 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2791 ILStackDesc *a, *b, *top;
2792 int idxa, idxb, complexMerge = 0;
2795 if (!check_underflow (ctx, 2))
2797 b = stack_pop (ctx);
2798 a = stack_pop (ctx);
2800 idxa = stack_slot_get_underlying_type (a);
2801 if (stack_slot_is_managed_pointer (a)) {
2806 idxb = stack_slot_get_underlying_type (b);
2807 if (stack_slot_is_managed_pointer (b)) {
2814 res = table [idxa][idxb];
2816 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2817 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2819 top = stack_push (ctx);
2820 if (res == TYPE_INV) {
2821 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)));
2822 copy_stack_value (top, a);
2826 if (res & NON_VERIFIABLE_RESULT) {
2827 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)));
2829 res = res & ~NON_VERIFIABLE_RESULT;
2832 if (complexMerge && res == TYPE_PTR) {
2833 if (complexMerge == 1)
2834 copy_stack_value (top, a);
2835 else if (complexMerge == 2)
2836 copy_stack_value (top, b);
2838 * There is no need to merge the type of two pointers.
2839 * The only valid operation is subtraction, that returns a native
2840 * int as result and can be used with any 2 pointer kinds.
2841 * This is valid acording to Patition III 1.1.4
2850 do_boolean_branch_op (VerifyContext *ctx, int delta)
2852 int target = ctx->ip_offset + delta;
2855 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2857 if (target < 0 || target >= ctx->code_size) {
2858 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2862 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2864 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2867 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2871 ctx->target = target;
2873 if (!check_underflow (ctx, 1))
2876 top = stack_pop (ctx);
2877 if (!is_valid_bool_arg (top))
2878 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));
2880 check_unmanaged_pointer (ctx, top);
2884 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2886 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type);
2890 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2895 int target = ctx->ip_offset + delta;
2897 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2899 if (target < 0 || target >= ctx->code_size) {
2900 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2904 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2905 case 1: /*FIXME use constants and not magic numbers.*/
2906 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2909 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2913 ctx->target = target;
2915 if (!check_underflow (ctx, 2))
2918 b = stack_pop (ctx);
2919 a = stack_pop (ctx);
2921 idxa = stack_slot_get_underlying_type (a);
2922 if (stack_slot_is_managed_pointer (a))
2925 idxb = stack_slot_get_underlying_type (b);
2926 if (stack_slot_is_managed_pointer (b))
2929 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2934 res = table [idxa][idxb];
2937 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2938 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2940 if (res == TYPE_INV) {
2941 CODE_NOT_VERIFIABLE (ctx,
2942 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));
2943 } else if (res & NON_VERIFIABLE_RESULT) {
2944 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));
2945 res = res & ~NON_VERIFIABLE_RESULT;
2950 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2956 if (!check_underflow (ctx, 2))
2958 b = stack_pop (ctx);
2959 a = stack_pop (ctx);
2961 if (opcode == CEE_CGT_UN) {
2962 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2963 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2968 idxa = stack_slot_get_underlying_type (a);
2969 if (stack_slot_is_managed_pointer (a))
2972 idxb = stack_slot_get_underlying_type (b);
2973 if (stack_slot_is_managed_pointer (b))
2976 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2981 res = table [idxa][idxb];
2984 if(res == TYPE_INV) {
2985 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));
2986 } else if (res & NON_VERIFIABLE_RESULT) {
2987 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));
2988 res = res & ~NON_VERIFIABLE_RESULT;
2990 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2994 do_ret (VerifyContext *ctx)
2996 MonoType *ret = ctx->signature->ret;
2997 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2998 if (ret->type != MONO_TYPE_VOID) {
3000 if (!check_underflow (ctx, 1))
3003 top = stack_pop(ctx);
3005 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3006 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
3010 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3011 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3014 if (ctx->eval.size > 0) {
3015 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3017 if (in_any_block (ctx->header, ctx->ip_offset))
3018 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3022 * 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.
3023 * This is illegal but mono_get_method_full decoded it.
3024 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3027 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
3030 MonoMethodSignature *sig;
3033 gboolean virt_check_this = FALSE;
3034 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3036 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
3040 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3042 if (method->klass->valuetype) // && !constrained ???
3043 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3045 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3046 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3049 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3050 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3052 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL)) {
3053 virt_check_this = TRUE;
3054 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3058 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
3059 sig = mono_method_get_signature (method, ctx->image, method_token);
3061 param_count = sig->param_count + sig->hasthis;
3062 if (!check_underflow (ctx, param_count))
3065 for (i = sig->param_count - 1; i >= 0; --i) {
3066 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3067 value = stack_pop (ctx);
3068 if (!verify_stack_type_compatibility (ctx, sig->params[i], value))
3069 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
3071 if (stack_slot_is_managed_mutability_pointer (value))
3072 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));
3074 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3075 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));
3081 MonoType *type = &method->klass->byval_arg;
3084 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3085 if (!mono_method_is_constructor (ctx->method))
3086 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3087 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3088 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));
3090 ctx->super_ctor_called = TRUE;
3091 value = stack_pop_safe (ctx);
3092 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3093 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3095 value = stack_pop (ctx);
3098 copy_stack_value (©, value);
3099 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3100 //Other parts of the code suffer from the same issue of
3101 copy.type = mono_type_get_type_byval (copy.type);
3102 copy.stype &= ~POINTER_MASK;
3104 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3105 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));
3107 if (constrained && virtual) {
3108 if (!stack_slot_is_managed_pointer (value))
3109 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3110 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
3111 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3112 copy.stype |= BOXED_MASK;
3114 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3115 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));
3117 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3118 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3120 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3121 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3123 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3124 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));
3126 if (!verify_stack_type_compatibility (ctx, type, ©))
3127 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
3129 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, value->type->data.klass))
3130 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3132 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
3133 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3135 if (sig->ret->type != MONO_TYPE_VOID) {
3136 if (check_overflow (ctx)) {
3137 value = stack_push (ctx);
3138 set_stack_value (ctx, value, sig->ret, FALSE);
3139 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3140 ctx->prefix_set &= ~PREFIX_READONLY;
3141 value->stype |= CMMP_MASK;
3146 if ((ctx->prefix_set & PREFIX_TAIL)) {
3147 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3148 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3149 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3150 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3156 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3158 MonoClassField *field;
3161 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3163 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3166 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3167 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3170 /*taking the address of initonly field only works from the static constructor */
3171 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3172 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3173 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3175 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3176 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3178 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3182 do_store_static_field (VerifyContext *ctx, int token) {
3183 MonoClassField *field;
3186 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3188 if (!check_underflow (ctx, 1))
3191 value = stack_pop (ctx);
3193 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3196 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3197 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3201 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3202 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3206 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3207 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3209 if (!verify_stack_type_compatibility (ctx, field->type, value))
3210 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));
3214 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3216 MonoClassField *field;
3218 gboolean is_pointer;
3220 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3221 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3225 //the value on stack is going to be used as a pointer
3226 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3228 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3229 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3232 g_assert (obj->type);
3234 /*The value on the stack must be a subclass of the defining type of the field*/
3235 /* we need to check if we can load the field from the stack value*/
3237 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3238 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3240 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3241 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3243 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3244 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));
3246 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3247 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3248 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));
3250 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, FALSE, TRUE))
3251 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3253 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3254 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3257 check_unmanaged_pointer (ctx, obj);
3262 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3265 MonoClassField *field;
3268 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3270 if (!check_underflow (ctx, 1))
3272 obj = stack_pop_safe (ctx);
3274 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3277 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3278 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3280 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3281 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3282 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3284 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3288 do_store_field (VerifyContext *ctx, int token)
3290 ILStackDesc *value, *obj;
3291 MonoClassField *field;
3292 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3294 if (!check_underflow (ctx, 2))
3297 value = stack_pop (ctx);
3298 obj = stack_pop_safe (ctx);
3300 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3303 if (!verify_stack_type_compatibility (ctx, field->type, value))
3304 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3307 /*TODO proper handle for Nullable<T>*/
3309 do_box_value (VerifyContext *ctx, int klass_token)
3312 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3317 if (!check_underflow (ctx, 1))
3320 value = stack_pop (ctx);
3321 /*box is a nop for reference types*/
3323 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3324 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3329 if (!verify_stack_type_compatibility (ctx, type, value))
3330 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3332 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3336 do_unbox_value (VerifyContext *ctx, int klass_token)
3339 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3344 if (!check_underflow (ctx, 1))
3347 if (!mono_class_from_mono_type (type)->valuetype)
3348 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3350 value = stack_pop (ctx);
3352 /*Value should be: a boxed valuetype or a reference type*/
3353 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3354 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3355 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));
3357 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3358 value->stype |= CMMP_MASK;
3362 do_unbox_any (VerifyContext *ctx, int klass_token)
3365 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3370 if (!check_underflow (ctx, 1))
3373 value = stack_pop (ctx);
3375 /*Value should be: a boxed valuetype or a reference type*/
3376 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3377 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3378 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));
3380 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3384 do_unary_math_op (VerifyContext *ctx, int op)
3387 if (!check_underflow (ctx, 1))
3389 value = stack_pop (ctx);
3390 switch (stack_slot_get_type (value)) {
3393 case TYPE_NATIVE_INT:
3398 case TYPE_COMPLEX: /*only enums are ok*/
3399 if (mono_type_is_enum_type (value->type))
3402 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3404 stack_push_stack_val (ctx, value);
3408 do_conversion (VerifyContext *ctx, int kind)
3411 if (!check_underflow (ctx, 1))
3413 value = stack_pop (ctx);
3415 switch (stack_slot_get_type (value)) {
3418 case TYPE_NATIVE_INT:
3422 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));
3427 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3430 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3433 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3435 case TYPE_NATIVE_INT:
3436 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3439 g_error ("unknown type %02x in conversion", kind);
3445 do_load_token (VerifyContext *ctx, int token)
3448 MonoClass *handle_class;
3449 if (!check_overflow (ctx))
3451 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3453 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3456 if (handle_class == mono_defaults.typehandle_class) {
3457 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3458 } else if (handle_class == mono_defaults.methodhandle_class) {
3459 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3460 } else if (handle_class == mono_defaults.fieldhandle_class) {
3461 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3463 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3465 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3469 do_ldobj_value (VerifyContext *ctx, int token)
3472 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3473 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3478 if (!check_underflow (ctx, 1))
3481 value = stack_pop (ctx);
3482 if (!stack_slot_is_managed_pointer (value)
3483 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3484 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3485 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3489 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3490 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3492 /*We have a byval on the stack, but the comparison must be strict. */
3493 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3494 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3496 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3500 do_stobj (VerifyContext *ctx, int token)
3502 ILStackDesc *dest, *src;
3503 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3504 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3509 if (!check_underflow (ctx, 2))
3512 src = stack_pop (ctx);
3513 dest = stack_pop (ctx);
3515 if (stack_slot_is_managed_mutability_pointer (dest))
3516 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3518 if (!stack_slot_is_managed_pointer (dest))
3519 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3521 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3522 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));
3524 if (!verify_stack_type_compatibility (ctx, type, src))
3525 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
3527 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3528 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3532 do_cpobj (VerifyContext *ctx, int token)
3534 ILStackDesc *dest, *src;
3535 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3539 if (!check_underflow (ctx, 2))
3542 src = stack_pop (ctx);
3543 dest = stack_pop (ctx);
3545 if (!stack_slot_is_managed_pointer (src))
3546 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3548 if (!stack_slot_is_managed_pointer (dest))
3549 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3551 if (stack_slot_is_managed_mutability_pointer (dest))
3552 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3554 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3555 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3557 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3558 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3562 do_initobj (VerifyContext *ctx, int token)
3565 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3569 if (!check_underflow (ctx, 1))
3572 obj = stack_pop (ctx);
3574 if (!stack_slot_is_managed_pointer (obj))
3575 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3577 if (stack_slot_is_managed_mutability_pointer (obj))
3578 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3580 stack = mono_type_get_type_byval (obj->type);
3581 if (MONO_TYPE_IS_REFERENCE (stack)) {
3582 if (!verify_type_compatibility (ctx, stack, type))
3583 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3584 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3585 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3586 } else if (!verify_type_compatibility (ctx, stack, type)) {
3587 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3592 do_newobj (VerifyContext *ctx, int token)
3596 MonoMethodSignature *sig;
3598 gboolean is_delegate = FALSE;
3600 if (!(method = verifier_load_method (ctx, token, "newobj")))
3603 if (!mono_method_is_constructor (method)) {
3604 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3608 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3609 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3611 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
3612 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor not visible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3614 //FIXME use mono_method_get_signature_full
3615 sig = mono_method_signature (method);
3616 if (!check_underflow (ctx, sig->param_count))
3619 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3622 ILStackDesc *funptr;
3623 //first arg is object, second arg is fun ptr
3624 if (sig->param_count != 2) {
3625 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3628 funptr = stack_pop (ctx);
3629 value = stack_pop (ctx);
3630 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3632 for (i = sig->param_count - 1; i >= 0; --i) {
3633 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3634 value = stack_pop (ctx);
3635 if (!verify_stack_type_compatibility (ctx, sig->params [i], value))
3636 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
3638 if (stack_slot_is_managed_mutability_pointer (value))
3639 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3643 if (check_overflow (ctx))
3644 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3648 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3654 if (!check_underflow (ctx, 1))
3657 if (!(type = verifier_load_type (ctx, token, opcode)))
3661 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3665 value = stack_pop (ctx);
3666 is_boxed = stack_slot_is_boxed_value (value);
3668 if (stack_slot_is_managed_pointer (value))
3669 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3670 else if (mono_class_from_mono_type (value->type)->valuetype && !is_boxed)
3671 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Value cannot be a valuetype for %s at 0x%04x", opcode, ctx->ip_offset));
3673 switch (value->type->type) {
3674 case MONO_TYPE_FNPTR:
3676 case MONO_TYPE_TYPEDBYREF:
3677 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3680 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3681 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3685 mono_type_from_opcode (int opcode) {
3693 return &mono_defaults.sbyte_class->byval_arg;
3701 return &mono_defaults.int16_class->byval_arg;
3709 return &mono_defaults.int32_class->byval_arg;
3715 return &mono_defaults.int64_class->byval_arg;
3721 return &mono_defaults.single_class->byval_arg;
3727 return &mono_defaults.double_class->byval_arg;
3733 return &mono_defaults.int_class->byval_arg;
3737 case CEE_LDELEM_REF:
3738 case CEE_STELEM_REF:
3739 return &mono_defaults.object_class->byval_arg;
3742 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3748 do_load_indirect (VerifyContext *ctx, int opcode)
3751 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3753 if (!check_underflow (ctx, 1))
3756 value = stack_pop (ctx);
3757 if (!stack_slot_is_managed_pointer (value)) {
3758 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3759 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3763 if (opcode == CEE_LDIND_REF) {
3764 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3765 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3766 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3768 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3769 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3770 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3775 do_store_indirect (VerifyContext *ctx, int opcode)
3777 ILStackDesc *addr, *val;
3778 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3780 if (!check_underflow (ctx, 2))
3783 val = stack_pop (ctx);
3784 addr = stack_pop (ctx);
3786 check_unmanaged_pointer (ctx, addr);
3788 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3789 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3793 if (stack_slot_is_managed_mutability_pointer (addr)) {
3794 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3798 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3799 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3801 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3802 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3806 do_newarr (VerifyContext *ctx, int token)
3809 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3814 if (!check_underflow (ctx, 1))
3817 value = stack_pop (ctx);
3818 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3819 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));
3821 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3824 /*FIXME handle arrays that are not 0-indexed*/
3826 do_ldlen (VerifyContext *ctx)
3830 if (!check_underflow (ctx, 1))
3833 value = stack_pop (ctx);
3835 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3836 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3838 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3841 /*FIXME handle arrays that are not 0-indexed*/
3842 /*FIXME handle readonly prefix and CMMP*/
3844 do_ldelema (VerifyContext *ctx, int klass_token)
3846 ILStackDesc *index, *array, *res;
3847 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
3853 if (!check_underflow (ctx, 2))
3856 index = stack_pop (ctx);
3857 array = stack_pop (ctx);
3859 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3860 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));
3862 if (!stack_slot_is_null_literal (array)) {
3863 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3864 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3866 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3867 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3869 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3872 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3876 res = stack_push (ctx);
3877 set_stack_value (ctx, res, type, TRUE);
3878 if (ctx->prefix_set & PREFIX_READONLY) {
3879 ctx->prefix_set &= ~PREFIX_READONLY;
3880 res->stype |= CMMP_MASK;
3885 * FIXME handle arrays that are not 0-indexed
3886 * FIXME handle readonly prefix and CMMP
3889 do_ldelem (VerifyContext *ctx, int opcode, int token)
3891 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3892 ILStackDesc *index, *array;
3894 if (!check_underflow (ctx, 2))
3897 if (opcode == CEE_LDELEM_ANY) {
3898 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
3899 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3903 type = mono_type_from_opcode (opcode);
3906 index = stack_pop (ctx);
3907 array = stack_pop (ctx);
3909 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3910 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));
3912 if (!stack_slot_is_null_literal (array)) {
3913 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3914 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));
3916 if (opcode == CEE_LDELEM_REF) {
3917 if (array->type->data.klass->valuetype)
3918 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3919 type = &array->type->data.klass->byval_arg;
3921 MonoType *candidate = &array->type->data.klass->byval_arg;
3922 if (IS_STRICT_MODE (ctx)) {
3923 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
3924 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
3925 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)) ||
3926 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
3927 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3929 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
3930 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3935 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3940 * FIXME handle arrays that are not 0-indexed
3943 do_stelem (VerifyContext *ctx, int opcode, int token)
3945 ILStackDesc *index, *array, *value;
3947 if (!check_underflow (ctx, 3))
3950 if (opcode == CEE_STELEM_ANY) {
3951 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
3952 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3956 type = mono_type_from_opcode (opcode);
3959 value = stack_pop (ctx);
3960 index = stack_pop (ctx);
3961 array = stack_pop (ctx);
3963 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3964 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));
3966 if (!stack_slot_is_null_literal (array)) {
3967 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3968 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));
3970 if (opcode == CEE_STELEM_REF) {
3971 if (array->type->data.klass->valuetype)
3972 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3973 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3974 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3978 if (opcode == CEE_STELEM_REF) {
3979 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3980 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3981 } else if (opcode != CEE_STELEM_REF) {
3982 if (!verify_stack_type_compatibility (ctx, type, value))
3983 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3985 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
3986 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));
3992 do_throw (VerifyContext *ctx)
3994 ILStackDesc *exception;
3995 if (!check_underflow (ctx, 1))
3997 exception = stack_pop (ctx);
3999 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4000 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4002 /*The stack is left empty after a throw*/
4008 do_endfilter (VerifyContext *ctx)
4010 MonoExceptionClause *clause;
4012 if (IS_STRICT_MODE (ctx)) {
4013 if (ctx->eval.size != 1)
4014 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4016 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4017 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4020 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4021 if (IS_STRICT_MODE (ctx)) {
4022 if (ctx->ip_offset != clause->handler_offset - 2)
4023 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4025 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4026 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4029 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4030 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4032 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4039 do_leave (VerifyContext *ctx, int delta)
4041 int target = ((gint32)ctx->ip_offset) + delta;
4042 if (target >= ctx->code_size || target < 0)
4043 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4045 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4046 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4053 * Verify br and br.s opcodes.
4056 do_static_branch (VerifyContext *ctx, int delta)
4058 int target = ctx->ip_offset + delta;
4059 if (target < 0 || target >= ctx->code_size) {
4060 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4064 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4066 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4069 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4073 ctx->target = target;
4077 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4079 int i, base = ctx->ip_offset + 5 + count * 4;
4082 if (!check_underflow (ctx, 1))
4085 value = stack_pop (ctx);
4087 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4088 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4090 for (i = 0; i < count; ++i) {
4091 int target = base + read32 (data + i * 4);
4093 if (target < 0 || target >= ctx->code_size) {
4094 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4098 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4100 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4103 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4106 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4111 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4116 if (virtual && !check_underflow (ctx, 1))
4119 if (!virtual && !check_overflow (ctx))
4122 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4123 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4127 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4130 if (mono_method_is_constructor (method))
4131 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4134 ILStackDesc *top = stack_pop (ctx);
4136 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4137 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4139 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4140 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4142 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4143 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4146 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4147 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);
4149 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4150 top->method = method;
4154 do_sizeof (VerifyContext *ctx, int token)
4158 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4159 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4163 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4166 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4167 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4171 if (type->type == MONO_TYPE_VOID) {
4172 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4176 if (check_overflow (ctx))
4177 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4180 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4182 do_localloc (VerifyContext *ctx)
4186 if (ctx->eval.size != 1) {
4187 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4191 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4192 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4196 /*TODO verify top type*/
4197 top = stack_pop (ctx);
4199 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4200 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4204 do_ldstr (VerifyContext *ctx, guint32 token)
4206 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4207 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4211 if (mono_metadata_token_index (token) >= ctx->image->heap_us.size) {
4212 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4216 if (check_overflow (ctx))
4217 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4221 do_refanyval (VerifyContext *ctx, int token)
4225 if (!check_underflow (ctx, 1))
4228 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4231 top = stack_pop (ctx);
4233 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4234 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));
4236 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4240 do_refanytype (VerifyContext *ctx)
4244 if (!check_underflow (ctx, 1))
4247 top = stack_pop (ctx);
4249 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4250 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));
4252 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4257 do_mkrefany (VerifyContext *ctx, int token)
4261 if (!check_underflow (ctx, 1))
4264 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4267 top = stack_pop (ctx);
4269 if (stack_slot_is_managed_mutability_pointer (top))
4270 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4272 if (!stack_slot_is_managed_pointer (top)) {
4273 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));
4275 MonoType *stack_type = mono_type_get_type_byval (top->type);
4276 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4277 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4279 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4280 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4283 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4287 do_ckfinite (VerifyContext *ctx)
4290 if (!check_underflow (ctx, 1))
4293 top = stack_pop (ctx);
4295 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4296 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));
4297 stack_push_stack_val (ctx, top);
4301 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4303 * @from holds new values for a given control path
4304 * @to holds the current values of a given control path
4306 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4309 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4312 stack_init (ctx, to);
4315 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4318 stack_copy (&ctx->eval, to);
4320 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4321 stack_copy (to, &ctx->eval);
4324 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4326 if (from->size != to->size) {
4327 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4328 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));
4332 //FIXME we need to preserve CMMP attributes
4333 //FIXME we must take null literals into consideration.
4334 for (i = 0; i < from->size; ++i) {
4335 ILStackDesc *new_slot = from->stack + i;
4336 ILStackDesc *old_slot = to->stack + i;
4337 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4338 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4339 MonoClass *old_class = mono_class_from_mono_type (old_type);
4340 MonoClass *new_class = mono_class_from_mono_type (new_type);
4341 MonoClass *match_class = NULL;
4343 // S := T then U = S (new value is compatible with current value, keep current)
4344 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4345 copy_stack_value (new_slot, old_slot);
4349 // T := S then U = T (old value is compatible with current value, use new)
4350 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4351 copy_stack_value (old_slot, new_slot);
4355 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4356 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible old [%s] new [%s] at 0x%04x", i, stack_slot_get_name (old_slot), stack_slot_get_name (new_slot), ctx->ip_offset));
4360 //both are reference types, use closest common super type
4361 if (!mono_class_from_mono_type (old_type)->valuetype
4362 && !mono_class_from_mono_type (new_type)->valuetype
4363 && !stack_slot_is_managed_pointer (old_slot)
4364 && !stack_slot_is_managed_pointer (new_slot)) {
4366 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4367 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4368 match_class = old_class->supertypes [j];
4373 for (j = 0; j < old_class->interface_count; ++j) {
4374 for (k = 0; k < new_class->interface_count; ++k) {
4375 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4376 match_class = old_class->interfaces [j];
4382 //No decent super type found, use object
4383 match_class = mono_defaults.object_class;
4385 } 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)) {
4386 match_class = mono_defaults.object_class;
4390 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible old [%s] new [%s] at 0x%04x", i, stack_slot_get_name (old_slot), stack_slot_get_name (new_slot), ctx->ip_offset));
4391 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4395 g_assert (match_class);
4396 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4397 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4403 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4404 to->flags |= IL_CODE_STACK_MERGED;
4407 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4408 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4411 * is_clause_in_range :
4413 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4416 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4418 if (clause->try_offset >= start && clause->try_offset < end)
4420 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4426 * is_clause_inside_range :
4428 * Returns TRUE if @clause lies completely inside the @start - @end range.
4431 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4433 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4435 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4441 * is_clause_nested :
4443 * Returns TRUE if @nested is nested in @clause.
4446 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4448 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4450 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4451 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4454 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4455 * the each pair of exception must have the following properties:
4456 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4457 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4458 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4461 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4463 /*clause is nested*/
4464 if (is_clause_nested (to_test, clause)) {
4465 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4466 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4471 /*wrong nesting order.*/
4472 if (is_clause_nested (clause, to_test)) {
4473 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4477 /*mutual protection*/
4478 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4479 /*handlers are not disjoint*/
4480 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4481 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4484 /* handlers are not catch or filter */
4485 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4486 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4493 /*not completelly disjoint*/
4494 if (is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4495 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len))
4496 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4499 #define code_bounds_check(size) \
4500 if (ip + size > end) {\
4501 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4506 * FIXME: need to distinguish between valid and verifiable.
4507 * Need to keep track of types on the stack.
4508 * Verify types for opcodes.
4511 mono_method_verify (MonoMethod *method, int level)
4513 const unsigned char *ip;
4514 const unsigned char *end;
4515 int i, n, need_merge = 0, start = 0;
4516 guint token, ip_offset = 0, prefix = 0;
4517 MonoGenericContext *generic_context = NULL;
4521 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4523 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4524 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4528 memset (&ctx, 0, sizeof (VerifyContext));
4530 //FIXME use mono_method_get_signature_full
4531 ctx.signature = mono_method_signature (method);
4532 if (!ctx.signature) {
4533 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4536 ctx.header = mono_method_get_header (method);
4538 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4541 ctx.method = method;
4542 ip = ctx.header->code;
4543 end = ip + ctx.header->code_size;
4544 ctx.image = image = method->klass->image;
4547 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4548 ctx.max_stack = ctx.header->max_stack;
4549 ctx.verifiable = ctx.valid = 1;
4552 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4553 ctx.code_size = ctx.header->code_size;
4555 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4558 ctx.num_locals = ctx.header->num_locals;
4559 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4561 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4562 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4564 ctx.params = g_new (MonoType*, ctx.max_args);
4565 if (ctx.signature->hasthis)
4566 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4567 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4569 if (ctx.signature->is_inflated)
4570 ctx.generic_context = generic_context = mono_method_get_context (method);
4572 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4573 if (method->is_generic)
4574 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4576 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4579 for (i = 0; i < ctx.num_locals; ++i)
4580 ctx.locals [i] = mono_class_inflate_generic_type (ctx.locals [i], ctx.generic_context);
4581 for (i = 0; i < ctx.max_args; ++i)
4582 ctx.params [i] = mono_class_inflate_generic_type (ctx.params [i], ctx.generic_context);
4583 stack_init (&ctx, &ctx.eval);
4585 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4586 MonoExceptionClause *clause = ctx.header->clauses + i;
4587 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); );
4589 if (clause->try_offset > ctx.code_size)
4590 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4592 if (clause->try_len <= 0)
4593 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4595 if (clause->handler_offset > ctx.code_size)
4596 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4598 if (clause->handler_len <= 0)
4599 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4601 if (clause->try_offset < clause->handler_offset && clause->try_offset + clause->try_len > HANDLER_START (clause))
4602 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4604 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4605 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4610 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4611 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4612 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4614 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4615 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4617 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4618 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4619 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4623 while (ip < end && ctx.valid) {
4624 ctx.ip_offset = ip_offset = ip - ctx.header->code;
4626 /*We need to check against fallthrou in and out of protected blocks.
4627 * For fallout we check the once a protected block ends, if the start flag is not set.
4628 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4629 * TODO convert these checks to be done using flags and not this loop
4631 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4632 MonoExceptionClause *clause = ctx.header->clauses + i;
4634 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4635 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4639 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4640 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4641 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4643 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4647 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4648 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4652 if (clause->handler_offset == ip_offset && start == 0) {
4653 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4657 if (clause->try_offset == ip_offset && ctx.eval.size > 0) {
4658 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4667 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4668 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4671 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4674 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4675 #ifdef MONO_VERIFIER_DEBUG
4678 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4679 discode [strlen (discode) - 1] = 0; /* no \n */
4680 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4683 dump_stack_state (&ctx.code [ip_offset]);
4684 dump_stack_state (&ctx.eval);
4697 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4703 code_bounds_check (2);
4704 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4708 case CEE_ADD_OVF_UN:
4709 do_binop (&ctx, *ip, add_ovf_un_table);
4713 case CEE_SUB_OVF_UN:
4714 do_binop (&ctx, *ip, sub_ovf_un_table);
4721 case CEE_MUL_OVF_UN:
4722 do_binop (&ctx, *ip, bin_ovf_table);
4727 do_binop (&ctx, *ip, add_table);
4732 do_binop (&ctx, *ip, sub_table);
4739 do_binop (&ctx, *ip, bin_op_table);
4748 do_binop (&ctx, *ip, int_bin_op_table);
4755 do_binop (&ctx, *ip, shift_op_table);
4760 if (!check_underflow (&ctx, 1))
4762 stack_pop_safe (&ctx);
4776 /*TODO support definite assignment verification? */
4777 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
4785 store_local (&ctx, *ip - CEE_STLOC_0);
4790 code_bounds_check (2);
4791 store_local (&ctx, ip [1]);
4796 code_bounds_check (2);
4797 store_arg (&ctx, ip [1]);
4811 if (check_overflow (&ctx))
4812 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4817 code_bounds_check (2);
4818 if (check_overflow (&ctx))
4819 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4824 code_bounds_check (5);
4825 if (check_overflow (&ctx))
4826 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4831 code_bounds_check (9);
4832 if (check_overflow (&ctx))
4833 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4838 code_bounds_check (5);
4839 if (check_overflow (&ctx))
4840 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4845 code_bounds_check (9);
4846 if (check_overflow (&ctx))
4847 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4852 if (check_overflow (&ctx))
4853 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4859 code_bounds_check (2);
4860 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4873 code_bounds_check (2);
4874 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
4881 code_bounds_check (5);
4882 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
4895 code_bounds_check (5);
4896 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
4903 code_bounds_check (2);
4904 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
4908 /* FIXME: warn/error instead? */
4915 if (!check_underflow (&ctx, 1))
4917 if (!check_overflow (&ctx))
4919 top = stack_pop_safe (&ctx);
4920 copy_stack_value (stack_push (&ctx), top);
4921 copy_stack_value (stack_push (&ctx), top);
4927 code_bounds_check (5);
4929 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
4930 token = read32 (ip + 1);
4931 if (in_any_block (ctx.header, ip_offset))
4932 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
4934 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
4936 * FIXME: check signature, retval, arguments etc.
4942 code_bounds_check (5);
4943 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
4948 code_bounds_check (5);
4949 token = read32 (ip + 1);
4951 * FIXME: check signature, retval, arguments etc.
4952 * FIXME: check requirements for tail call
4954 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
4958 code_bounds_check (2);
4959 do_static_branch (&ctx, (signed char)ip [1] + 2);
4967 code_bounds_check (2);
4968 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
4974 code_bounds_check (5);
4975 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
4983 code_bounds_check (5);
4984 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
4990 code_bounds_check (5);
4991 n = read32 (ip + 1);
4992 code_bounds_check (5 + sizeof (guint32) * n);
4994 do_switch (&ctx, n, (ip + 5));
4996 ip += 5 + sizeof (guint32) * n;
5010 do_load_indirect (&ctx, *ip);
5022 do_store_indirect (&ctx, *ip);
5028 do_unary_math_op (&ctx, *ip);
5038 do_conversion (&ctx, TYPE_I4);
5044 do_conversion (&ctx, TYPE_I8);
5051 do_conversion (&ctx, TYPE_R8);
5057 do_conversion (&ctx, TYPE_NATIVE_INT);
5062 code_bounds_check (5);
5063 do_cpobj (&ctx, read32 (ip + 1));
5068 code_bounds_check (5);
5069 do_ldobj_value (&ctx, read32 (ip + 1));
5074 code_bounds_check (5);
5075 do_ldstr (&ctx, read32 (ip + 1));
5080 code_bounds_check (5);
5081 do_newobj (&ctx, read32 (ip + 1));
5087 code_bounds_check (5);
5088 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5094 ++ip; /* warn, error ? */
5098 code_bounds_check (5);
5099 do_unbox_value (&ctx, read32 (ip + 1));
5111 code_bounds_check (5);
5112 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5118 code_bounds_check (5);
5119 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5124 code_bounds_check (5);
5125 do_store_field (&ctx, read32 (ip + 1));
5130 code_bounds_check (5);
5131 do_store_static_field (&ctx, read32 (ip + 1));
5136 code_bounds_check (5);
5137 do_stobj (&ctx, read32 (ip + 1));
5141 case CEE_CONV_OVF_I1_UN:
5142 case CEE_CONV_OVF_I2_UN:
5143 case CEE_CONV_OVF_I4_UN:
5144 case CEE_CONV_OVF_U1_UN:
5145 case CEE_CONV_OVF_U2_UN:
5146 case CEE_CONV_OVF_U4_UN:
5147 do_conversion (&ctx, TYPE_I4);
5151 case CEE_CONV_OVF_I8_UN:
5152 case CEE_CONV_OVF_U8_UN:
5153 do_conversion (&ctx, TYPE_I8);
5157 case CEE_CONV_OVF_I_UN:
5158 case CEE_CONV_OVF_U_UN:
5159 do_conversion (&ctx, TYPE_NATIVE_INT);
5164 code_bounds_check (5);
5165 do_box_value (&ctx, read32 (ip + 1));
5170 code_bounds_check (5);
5171 do_newarr (&ctx, read32 (ip + 1));
5181 code_bounds_check (5);
5182 do_ldelema (&ctx, read32 (ip + 1));
5196 case CEE_LDELEM_REF:
5197 do_ldelem (&ctx, *ip, 0);
5208 case CEE_STELEM_REF:
5209 do_stelem (&ctx, *ip, 0);
5213 case CEE_LDELEM_ANY:
5214 code_bounds_check (5);
5215 do_ldelem (&ctx, *ip, read32 (ip + 1));
5219 case CEE_STELEM_ANY:
5220 code_bounds_check (5);
5221 do_stelem (&ctx, *ip, read32 (ip + 1));
5226 code_bounds_check (5);
5227 do_unbox_any (&ctx, read32 (ip + 1));
5231 case CEE_CONV_OVF_I1:
5232 case CEE_CONV_OVF_U1:
5233 case CEE_CONV_OVF_I2:
5234 case CEE_CONV_OVF_U2:
5235 case CEE_CONV_OVF_I4:
5236 case CEE_CONV_OVF_U4:
5237 do_conversion (&ctx, TYPE_I4);
5241 case CEE_CONV_OVF_I8:
5242 case CEE_CONV_OVF_U8:
5243 do_conversion (&ctx, TYPE_I8);
5247 case CEE_CONV_OVF_I:
5248 case CEE_CONV_OVF_U:
5249 do_conversion (&ctx, TYPE_NATIVE_INT);
5254 code_bounds_check (5);
5255 do_refanyval (&ctx, read32 (ip + 1));
5265 code_bounds_check (5);
5266 do_mkrefany (&ctx, read32 (ip + 1));
5271 code_bounds_check (5);
5272 do_load_token (&ctx, read32 (ip + 1));
5276 case CEE_ENDFINALLY:
5277 if (!is_correct_endfinally (ctx.header, ip_offset))
5278 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5285 code_bounds_check (5);
5286 do_leave (&ctx, read32 (ip + 1) + 5);
5292 code_bounds_check (2);
5293 do_leave (&ctx, (signed char)ip [1] + 2);
5299 code_bounds_check (2);
5303 code_bounds_check (3);
5304 store_local (&ctx, read16 (ip + 1));
5309 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5317 do_cmp_op (&ctx, cmp_br_op, *ip);
5322 code_bounds_check (3);
5323 store_arg (&ctx, read16 (ip + 1) );
5329 check_overflow (&ctx);
5330 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5331 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5332 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5337 code_bounds_check (5);
5338 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5343 code_bounds_check (5);
5344 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5354 code_bounds_check (3);
5355 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5361 code_bounds_check (3);
5362 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5375 do_endfilter (&ctx);
5379 case CEE_UNALIGNED_:
5380 code_bounds_check (2);
5381 prefix |= PREFIX_UNALIGNED;
5385 prefix |= PREFIX_VOLATILE;
5389 prefix |= PREFIX_TAIL;
5391 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5392 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5396 code_bounds_check (5);
5397 do_initobj (&ctx, read32 (ip + 1));
5401 case CEE_CONSTRAINED_:
5402 code_bounds_check (5);
5403 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5404 prefix |= PREFIX_CONSTRAINED;
5409 prefix |= PREFIX_READONLY;
5414 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5415 if (!check_underflow (&ctx, 3))
5417 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5422 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5423 if (!check_underflow (&ctx, 3))
5425 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5433 if (!is_correct_rethrow (ctx.header, ip_offset))
5434 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5443 code_bounds_check (5);
5444 do_sizeof (&ctx, read32 (ip + 1));
5448 case CEE_REFANYTYPE:
5449 do_refanytype (&ctx);
5454 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5460 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5464 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5466 if (!ctx.prefix_set) //first prefix
5467 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5468 ctx.prefix_set |= prefix;
5469 ctx.has_flags = TRUE;
5473 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5475 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5476 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5477 if (ctx.prefix_set & PREFIX_READONLY)
5478 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5479 if (ctx.prefix_set & PREFIX_VOLATILE)
5480 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5481 if (ctx.prefix_set & PREFIX_UNALIGNED)
5482 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5483 ctx.prefix_set = prefix = 0;
5484 ctx.has_flags = FALSE;
5488 * if ip != end we overflowed: mark as error.
5490 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5491 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5494 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5495 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5496 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5497 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5498 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5500 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5501 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5503 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5504 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5506 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5507 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));
5510 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class)
5511 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor not calling super\n"));
5514 for (i = 0; i < ctx.header->code_size; ++i) {
5515 if (ctx.code [i].stack)
5516 g_free (ctx.code [i].stack);
5520 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5522 g_slist_free (ctx.funptrs);
5524 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5525 mono_metadata_free_type (tmp->data);
5526 g_slist_free (ctx.exception_types);
5528 for (i = 0; i < ctx.num_locals; ++i)
5529 mono_metadata_free_type (ctx.locals [i]);
5530 for (i = 0; i < ctx.max_args; ++i)
5531 mono_metadata_free_type (ctx.params [i]);
5534 g_free (ctx.eval.stack);
5537 g_free (ctx.locals);
5538 g_free (ctx.params);
5544 mono_verify_corlib ()
5546 /* This is a public API function so cannot be removed */
5550 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
5551 static gboolean verify_all = FALSE;
5554 * Set the desired level of checks for the verfier.
5558 mono_verifier_set_mode (MiniVerifierMode mode)
5560 verifier_mode = mode;
5564 mono_verifier_enable_verify_all ()
5570 * Returns true if @method needs to be verified.
5574 mono_verifier_is_enabled_for_method (MonoMethod *method)
5576 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5580 * Returns true if @klass need to be verified.
5584 mono_verifier_is_enabled_for_class (MonoClass *klass)
5586 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !klass->image->assembly->in_gac && klass->image != mono_defaults.corlib);
5590 mono_verifier_is_method_full_trust (MonoMethod *method)
5592 return mono_verifier_is_class_full_trust (method->klass);
5596 * Returns if @klass is under full trust or not.
5598 * TODO This code doesn't take CAS into account.
5600 * This value is only pertinent to assembly verification and has
5601 * nothing to do with CoreClr security.
5603 * Under verify_all all user code must be verifiable if no security option was set
5607 mono_verifier_is_class_full_trust (MonoClass *klass)
5609 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5610 return klass->image->assembly->in_gac || klass->image == mono_defaults.corlib;
5611 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || klass->image->assembly->in_gac || klass->image == mono_defaults.corlib;
5615 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5617 return mono_method_verify (method,
5618 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5619 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5620 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5624 get_field_end (MonoClassField *field)
5627 int size = mono_type_size (field->type, &align);
5629 size = 4; /*FIXME Is this a safe bet?*/
5630 return size + field->offset;
5634 verify_class_for_overlapping_reference_fields (MonoClass *class)
5637 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5638 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) || !class->has_references)
5641 //we must check for stuff overlapping reference fields
5642 for (i = 0; i < class->field.count; ++i) {
5643 MonoClassField *field = &class->fields [i];
5644 int fieldEnd = get_field_end (field);
5645 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5646 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5649 for (j = i + 1; j < class->field.count; ++j) {
5650 MonoClassField *other = &class->fields [j];
5651 int otherEnd = get_field_end (other);
5652 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5655 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5658 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5666 * Check if the class is verifiable.
5668 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5669 * field and invalid generic instantiation are fatal errors.
5671 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5675 mono_verifier_verify_class (MonoClass *class)
5677 if (!verify_class_for_overlapping_reference_fields (class))
5680 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))