5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 #include <mono/metadata/object-internals.h>
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/verify-internals.h>
15 #include <mono/metadata/opcodes.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/reflection.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/metadata.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/class-internals.h>
23 #include <mono/metadata/security-manager.h>
24 #include <mono/metadata/security-core-clr.h>
25 #include <mono/metadata/tokentype.h>
31 static MiniVerifierMode verifier_mode
= MONO_VERIFIER_MODE_OFF
;
32 static gboolean verify_all
= FALSE
;
35 * Set the desired level of checks for the verfier.
39 mono_verifier_set_mode (MiniVerifierMode mode
)
45 mono_verifier_enable_verify_all ()
50 #ifndef DISABLE_VERIFIER
52 * Pull the list of opcodes
54 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
58 #include "mono/cil/opcode.def"
63 #ifdef MONO_VERIFIER_DEBUG
64 #define VERIFIER_DEBUG(code) do { code } while (0)
66 #define VERIFIER_DEBUG(code)
69 //////////////////////////////////////////////////////////////////
70 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
71 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
72 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
73 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
74 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
75 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
77 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
78 vinfo->info.status = __status; \
79 vinfo->info.message = ( __msg ); \
80 vinfo->exception_type = (__exception); \
81 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
84 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
85 #define ADD_VERIFY_ERROR(__ctx, __msg) \
87 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
91 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
93 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
94 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
95 (__ctx)->verifiable = 0; \
96 if (IS_FAIL_FAST_MODE (__ctx)) \
101 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
103 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
104 (__ctx)->valid = 0; \
107 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
109 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
110 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
111 (__ctx)->verifiable = 0; \
112 if (IS_FAIL_FAST_MODE (__ctx)) \
113 (__ctx)->valid = 0; \
116 /*Flags to be used with ILCodeDesc::flags */
118 /*Instruction has not been processed.*/
119 IL_CODE_FLAG_NOT_PROCESSED
= 0,
120 /*Instruction was decoded by mono_method_verify loop.*/
121 IL_CODE_FLAG_SEEN
= 1,
122 /*Instruction was target of a branch or is at a protected block boundary.*/
123 IL_CODE_FLAG_WAS_TARGET
= 2,
124 /*Used by stack_init to avoid double initialize each entry.*/
125 IL_CODE_FLAG_STACK_INITED
= 4,
126 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
127 IL_CODE_STACK_MERGED
= 8,
128 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
129 IL_CODE_DELEGATE_SEQUENCE
= 0x10,
130 /*This is a delegate created from a ldftn to a non final virtual method*/
131 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
= 0x20,
132 /*This is a call to a non final virtual method*/
133 IL_CODE_CALL_NONFINAL_VIRTUAL
= 0x40,
168 /*Allocated fnptr MonoType that should be freed by us.*/
170 /*Type dup'ed exception types from catch blocks.*/
171 GSList
*exception_types
;
176 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
180 MonoMethodSignature
*signature
;
181 MonoMethodHeader
*header
;
183 MonoGenericContext
*generic_context
;
187 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
188 *on a method that creates a delegate for a non-final virtual method using ldftn*/
189 gboolean has_this_store
;
191 /*This flag is used to control if the contructor of the parent class has been called.
192 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
193 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
195 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
196 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
198 gboolean super_ctor_called
;
202 MonoType
*constrained_type
;
206 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
);
209 get_stack_type (MonoType
*type
);
212 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
);
215 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
);
218 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
);
219 //////////////////////////////////////////////////////////////////
224 TYPE_INV
= 0, /* leave at 0. */
229 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
231 /* value types and classes */
233 /* Number of types, used to define the size of the tables*/
236 /* Used by tables to signal that a result is not verifiable*/
237 NON_VERIFIABLE_RESULT
= 0x80,
239 /*Mask used to extract just the type, excluding flags */
242 /* The stack type is a managed pointer, unmask the value to res */
243 POINTER_MASK
= 0x100,
245 /*Stack type with the pointer mask*/
246 RAW_TYPE_MASK
= 0x10F,
248 /* Controlled Mutability Manager Pointer */
251 /* The stack type is a null literal*/
252 NULL_LITERAL_MASK
= 0x400,
254 /**Used by ldarg.0 and family to let delegate verification happens.*/
255 THIS_POINTER_MASK
= 0x800,
257 /**Signals that this is a boxed value type*/
260 /*This is an unitialized this ref*/
261 UNINIT_THIS_MASK
= 0x2000,
264 static const char* const
265 type_names
[TYPE_MAX
+ 1] = {
276 PREFIX_UNALIGNED
= 1,
279 PREFIX_CONSTRAINED
= 8,
282 //////////////////////////////////////////////////////////////////
285 /*Token validation macros and functions */
286 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
287 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
288 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
289 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
291 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
292 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
293 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
294 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
295 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
296 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
299 * Verify if @token refers to a valid row on int's table.
302 token_bounds_check (MonoImage
*image
, guint32 token
)
305 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage
*)image
, token
);
306 return image
->tables
[mono_metadata_token_table (token
)].rows
>= mono_metadata_token_index (token
);
310 mono_type_create_fnptr_from_mono_method (VerifyContext
*ctx
, MonoMethod
*method
)
312 MonoType
*res
= g_new0 (MonoType
, 1);
313 //FIXME use mono_method_get_signature_full
314 res
->data
.method
= mono_method_signature (method
);
315 res
->type
= MONO_TYPE_FNPTR
;
316 ctx
->funptrs
= g_slist_prepend (ctx
->funptrs
, res
);
321 * mono_type_is_enum_type:
323 * Returns TRUE if @type is an enum type.
326 mono_type_is_enum_type (MonoType
*type
)
328 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
330 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
336 * mono_type_is_value_type:
338 * Returns TRUE if @type is named after @namespace.@name.
342 mono_type_is_value_type (MonoType
*type
, const char *namespace, const char *name
)
344 return type
->type
== MONO_TYPE_VALUETYPE
&&
345 !strcmp (namespace, type
->data
.klass
->name_space
) &&
346 !strcmp (name
, type
->data
.klass
->name
);
350 * Returns TURE if @type is VAR or MVAR
353 mono_type_is_generic_argument (MonoType
*type
)
355 return type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
;
359 * mono_type_get_underlying_type_any:
361 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
363 * Returns the underlying type of @type regardless if it is byref or not.
366 mono_type_get_underlying_type_any (MonoType
*type
)
368 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
369 return mono_class_enum_basetype (type
->data
.klass
);
370 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
371 return mono_class_enum_basetype (type
->data
.generic_class
->container_class
);
376 mono_type_get_stack_name (MonoType
*type
)
378 return type_names
[get_stack_type (type
) & TYPE_MASK
];
381 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
382 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
385 mono_method_is_constructor (MonoMethod
*method
)
387 return ((method
->flags
& CTOR_REQUIRED_FLAGS
) == CTOR_REQUIRED_FLAGS
&&
388 !(method
->flags
& CTOR_INVALID_FLAGS
) &&
389 !strcmp (".ctor", method
->name
));
393 mono_class_has_default_constructor (MonoClass
*klass
)
398 mono_class_setup_methods (klass
);
400 for (i
= 0; i
< klass
->method
.count
; ++i
) {
401 method
= klass
->methods
[i
];
402 if (mono_method_is_constructor (method
) &&
403 mono_method_signature (method
)->param_count
== 0 &&
404 (method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
)
411 mono_class_interface_implements_interface (MonoClass
*candidate
, MonoClass
*iface
)
415 if (candidate
== iface
)
417 mono_class_setup_interfaces (candidate
);
418 for (i
= 0; i
< candidate
->interface_count
; ++i
) {
419 if (candidate
->interfaces
[i
] == iface
|| mono_class_interface_implements_interface (candidate
->interfaces
[i
], iface
))
422 candidate
= candidate
->parent
;
428 * Verify if @type is valid for the given @ctx verification context.
429 * this function checks for VAR and MVAR types that are invalid under the current verifier,
432 mono_type_is_valid_type_in_context (MonoType
*type
, MonoGenericContext
*context
)
435 MonoGenericInst
*inst
;
437 switch (type
->type
) {
442 inst
= type
->type
== MONO_TYPE_VAR
? context
->class_inst
: context
->method_inst
;
443 if (!inst
|| mono_type_get_generic_param_num (type
) >= inst
->type_argc
)
446 case MONO_TYPE_SZARRAY
:
447 return mono_type_is_valid_type_in_context (&type
->data
.klass
->byval_arg
, context
);
448 case MONO_TYPE_ARRAY
:
449 return mono_type_is_valid_type_in_context (&type
->data
.array
->eklass
->byval_arg
, context
);
451 return mono_type_is_valid_type_in_context (type
->data
.type
, context
);
452 case MONO_TYPE_GENERICINST
:
453 inst
= type
->data
.generic_class
->context
.class_inst
;
456 for (i
= 0; i
< inst
->type_argc
; ++i
)
457 if (!mono_type_is_valid_type_in_context (inst
->type_argv
[i
], context
))
464 /*This function returns NULL if the type is not instantiatable*/
466 verifier_inflate_type (VerifyContext
*ctx
, MonoType
*type
, MonoGenericContext
*context
)
471 result
= mono_class_inflate_generic_type_checked (type
, context
, &error
);
472 if (!mono_error_ok (&error
)) {
473 mono_error_cleanup (&error
);
479 * Test if @candidate is a subtype of @target using the minimal possible information
480 * TODO move the code for non finished TypeBuilders to here.
483 mono_class_is_constraint_compatible (MonoClass
*candidate
, MonoClass
*target
)
485 if (candidate
== target
)
487 if (target
== mono_defaults
.object_class
)
490 //setup_supertypes don't mono_class_init anything
491 mono_class_setup_supertypes (candidate
);
492 mono_class_setup_supertypes (target
);
494 if (mono_class_has_parent (candidate
, target
))
497 //if target is not a supertype it must be an interface
498 if (!MONO_CLASS_IS_INTERFACE (target
))
501 if (candidate
->image
->dynamic
&& !candidate
->wastypebuilder
) {
502 MonoReflectionTypeBuilder
*tb
= candidate
->reflection_info
;
504 if (tb
->interfaces
) {
505 for (j
= mono_array_length (tb
->interfaces
) - 1; j
>= 0; --j
) {
506 MonoReflectionType
*iface
= mono_array_get (tb
->interfaces
, MonoReflectionType
*, j
);
507 MonoClass
*ifaceClass
= mono_class_from_mono_type (iface
->type
);
508 if (mono_class_is_constraint_compatible (ifaceClass
, target
)) {
515 return mono_class_interface_implements_interface (candidate
, target
);
519 is_valid_generic_instantiation (MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
524 if (ginst
->type_argc
!= gc
->type_argc
)
527 for (i
= 0; i
< gc
->type_argc
; ++i
) {
528 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
529 MonoClass
*paramClass
;
530 MonoClass
**constraints
;
532 if (!param_info
->constraints
&& !(param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
))
534 if (mono_type_is_generic_argument (ginst
->type_argv
[i
]))
535 continue; //it's not our job to validate type variables
537 paramClass
= mono_class_from_mono_type (ginst
->type_argv
[i
]);
539 if (paramClass
->exception_type
!= MONO_EXCEPTION_NONE
)
542 /*it's not safe to call mono_class_init from here*/
543 if (paramClass
->generic_class
&& !paramClass
->inited
) {
544 if (!mono_class_is_valid_generic_instantiation (NULL
, paramClass
))
548 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
) && (!paramClass
->valuetype
|| mono_class_is_nullable (paramClass
)))
551 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT
) && paramClass
->valuetype
)
554 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT
) && !paramClass
->valuetype
&& !mono_class_has_default_constructor (paramClass
))
557 if (!param_info
->constraints
)
560 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
561 MonoClass
*ctr
= *constraints
;
564 inflated
= mono_class_inflate_generic_type_checked (&ctr
->byval_arg
, context
, &error
);
565 if (!mono_error_ok (&error
)) {
566 mono_error_cleanup (&error
);
569 ctr
= mono_class_from_mono_type (inflated
);
570 mono_metadata_free_type (inflated
);
572 if (!mono_class_is_constraint_compatible (paramClass
, ctr
))
580 * Return true if @candidate is constraint compatible with @target.
582 * This means that @candidate constraints are a super set of @target constaints
585 mono_generic_param_is_constraint_compatible (VerifyContext
*ctx
, MonoGenericParam
*target
, MonoGenericParam
*candidate
, MonoGenericContext
*context
)
587 MonoGenericParamInfo
*tinfo
= mono_generic_param_info (target
);
588 MonoGenericParamInfo
*cinfo
= mono_generic_param_info (candidate
);
590 int tmask
= tinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
591 int cmask
= cinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
592 if ((tmask
& cmask
) != tmask
)
595 if (tinfo
->constraints
) {
596 MonoClass
**target_class
, **candidate_class
;
597 if (!cinfo
->constraints
)
599 for (target_class
= tinfo
->constraints
; *target_class
; ++target_class
) {
601 MonoType
*inflated
= verifier_inflate_type (ctx
, &(*target_class
)->byval_arg
, context
);
604 tc
= mono_class_from_mono_type (inflated
);
605 mono_metadata_free_type (inflated
);
607 for (candidate_class
= cinfo
->constraints
; *candidate_class
; ++candidate_class
) {
609 inflated
= verifier_inflate_type (ctx
, &(*candidate_class
)->byval_arg
, ctx
->generic_context
);
612 cc
= mono_class_from_mono_type (inflated
);
613 mono_metadata_free_type (inflated
);
615 if (mono_class_is_assignable_from (tc
, cc
))
618 if (!*candidate_class
)
625 static MonoGenericParam
*
626 verifier_get_generic_param_from_type (VerifyContext
*ctx
, MonoType
*type
)
628 MonoGenericContainer
*gc
;
629 MonoMethod
*method
= ctx
->method
;
632 num
= mono_type_get_generic_param_num (type
);
634 if (type
->type
== MONO_TYPE_VAR
) {
635 MonoClass
*gtd
= method
->klass
;
636 if (gtd
->generic_class
)
637 gtd
= gtd
->generic_class
->container_class
;
638 gc
= gtd
->generic_container
;
640 MonoMethod
*gmd
= method
;
641 if (method
->is_inflated
)
642 gmd
= ((MonoMethodInflated
*)method
)->declaring
;
643 gc
= mono_method_get_generic_container (gmd
);
647 return mono_generic_container_get_param (gc
, num
);
653 * Verify if @type is valid for the given @ctx verification context.
654 * this function checks for VAR and MVAR types that are invalid under the current verifier,
655 * This means that it either
658 is_valid_type_in_context (VerifyContext
*ctx
, MonoType
*type
)
660 return mono_type_is_valid_type_in_context (type
, ctx
->generic_context
);
664 is_valid_generic_instantiation_in_context (VerifyContext
*ctx
, MonoGenericInst
*ginst
)
667 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
668 MonoType
*type
= ginst
->type_argv
[i
];
669 if (!is_valid_type_in_context (ctx
, type
))
676 generic_arguments_respect_constraints (VerifyContext
*ctx
, MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
679 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
680 MonoType
*type
= ginst
->type_argv
[i
];
681 MonoGenericParam
*target
= mono_generic_container_get_param (gc
, i
);
682 MonoGenericParam
*candidate
;
684 if (!mono_type_is_generic_argument (type
))
687 if (!is_valid_type_in_context (ctx
, type
))
690 candidate
= verifier_get_generic_param_from_type (ctx
, type
);
692 if (!mono_generic_param_is_constraint_compatible (ctx
, target
, candidate
, context
))
699 mono_method_repect_method_constraints (VerifyContext
*ctx
, MonoMethod
*method
)
701 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
702 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
703 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
704 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gmethod
->context
, ginst
);
708 mono_class_repect_method_constraints (VerifyContext
*ctx
, MonoClass
*klass
)
710 MonoGenericClass
*gklass
= klass
->generic_class
;
711 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
712 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
713 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gklass
->context
, ginst
);
717 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
)
719 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
720 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
721 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
722 if (!gc
) /*non-generic inflated method - it's part of a generic type */
724 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
726 return is_valid_generic_instantiation (gc
, &gmethod
->context
, ginst
);
731 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
)
733 MonoGenericClass
*gklass
= klass
->generic_class
;
734 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
735 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
736 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
738 return is_valid_generic_instantiation (gc
, &gklass
->context
, ginst
);
742 mono_type_is_valid_in_context (VerifyContext
*ctx
, MonoType
*type
)
747 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid null type at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
751 if (!is_valid_type_in_context (ctx
, type
)) {
752 char *str
= mono_type_full_name (type
);
753 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
754 type
->type
== MONO_TYPE_VAR
? "!" : "!!",
756 type
->type
== MONO_TYPE_VAR
? "class" : "method",
758 MONO_EXCEPTION_BAD_IMAGE
);
763 klass
= mono_class_from_mono_type (type
);
764 mono_class_init (klass
);
765 if (mono_loader_get_last_error () || klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
766 if (klass
->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, klass
))
767 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
);
769 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
);
773 if (klass
->generic_class
&& klass
->generic_class
->container_class
->exception_type
!= MONO_EXCEPTION_NONE
) {
774 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
);
778 if (!klass
->generic_class
)
781 if (!mono_class_is_valid_generic_instantiation (ctx
, klass
)) {
782 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic type instantiation of type %s.%s at 0x%04x", klass
->name_space
, klass
->name
, ctx
->ip_offset
), MONO_EXCEPTION_TYPE_LOAD
);
786 if (!mono_class_repect_method_constraints (ctx
, klass
)) {
787 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic type instantiation of type %s.%s (generic args don't respect target's constraints) at 0x%04x", klass
->name_space
, klass
->name
, ctx
->ip_offset
), MONO_EXCEPTION_TYPE_LOAD
);
794 static verify_result_t
795 mono_method_is_valid_in_context (VerifyContext
*ctx
, MonoMethod
*method
)
797 if (!mono_type_is_valid_in_context (ctx
, &method
->klass
->byval_arg
))
798 return RESULT_INVALID
;
800 if (!method
->is_inflated
)
803 if (!mono_method_is_valid_generic_instantiation (ctx
, method
)) {
804 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s at 0x%04x", method
->klass
->name_space
, method
->klass
->name
, method
->name
, ctx
->ip_offset
), MONO_EXCEPTION_UNVERIFIABLE_IL
);
805 return RESULT_INVALID
;
808 if (!mono_method_repect_method_constraints (ctx
, method
)) {
809 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s (generic args don't respect target's constraints) at 0x%04x", method
->klass
->name_space
, method
->klass
->name
, method
->name
, ctx
->ip_offset
));
810 return RESULT_UNVERIFIABLE
;
816 static MonoClassField
*
817 verifier_load_field (VerifyContext
*ctx
, int token
, MonoClass
**out_klass
, const char *opcode
) {
818 MonoClassField
*field
;
819 MonoClass
*klass
= NULL
;
821 if (!IS_FIELD_DEF_OR_REF (token
) || !token_bounds_check (ctx
->image
, token
)) {
822 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid field token 0x%08x for %s at 0x%04x", token
, opcode
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
826 field
= mono_field_from_token (ctx
->image
, token
, &klass
, ctx
->generic_context
);
827 if (!field
|| !field
->parent
|| !klass
) {
828 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
);
832 if (!mono_type_is_valid_in_context (ctx
, &klass
->byval_arg
))
840 verifier_load_method (VerifyContext
*ctx
, int token
, const char *opcode
) {
843 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
844 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
);
848 method
= mono_get_method_full (ctx
->image
, token
, NULL
, ctx
->generic_context
);
851 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
);
855 if (mono_method_is_valid_in_context (ctx
, method
) == RESULT_INVALID
)
862 verifier_load_type (VerifyContext
*ctx
, int token
, const char *opcode
) {
865 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
866 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
870 type
= mono_type_get_full (ctx
->image
, token
, ctx
->generic_context
);
873 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
);
877 if (!mono_type_is_valid_in_context (ctx
, type
))
884 /* stack_slot_get_type:
886 * Returns the stack type of @value. This value includes POINTER_MASK.
888 * Use this function to checks that account for a managed pointer.
891 stack_slot_get_type (ILStackDesc
*value
)
893 return value
->stype
& RAW_TYPE_MASK
;
896 /* stack_slot_get_underlying_type:
898 * Returns the stack type of @value. This value does not include POINTER_MASK.
900 * Use this function is cases where the fact that the value could be a managed pointer is
901 * irrelevant. For example, field load doesn't care about this fact of type on stack.
904 stack_slot_get_underlying_type (ILStackDesc
*value
)
906 return value
->stype
& TYPE_MASK
;
909 /* stack_slot_is_managed_pointer:
911 * Returns TRUE is @value is a managed pointer.
914 stack_slot_is_managed_pointer (ILStackDesc
*value
)
916 return (value
->stype
& POINTER_MASK
) == POINTER_MASK
;
919 /* stack_slot_is_managed_mutability_pointer:
921 * Returns TRUE is @value is a managed mutability pointer.
923 static G_GNUC_UNUSED gboolean
924 stack_slot_is_managed_mutability_pointer (ILStackDesc
*value
)
926 return (value
->stype
& CMMP_MASK
) == CMMP_MASK
;
929 /* stack_slot_is_null_literal:
931 * Returns TRUE is @value is the null literal.
934 stack_slot_is_null_literal (ILStackDesc
*value
)
936 return (value
->stype
& NULL_LITERAL_MASK
) == NULL_LITERAL_MASK
;
940 /* stack_slot_is_this_pointer:
942 * Returns TRUE is @value is the this literal
945 stack_slot_is_this_pointer (ILStackDesc
*value
)
947 return (value
->stype
& THIS_POINTER_MASK
) == THIS_POINTER_MASK
;
950 /* stack_slot_is_boxed_value:
952 * Returns TRUE is @value is a boxed value
955 stack_slot_is_boxed_value (ILStackDesc
*value
)
957 return (value
->stype
& BOXED_MASK
) == BOXED_MASK
;
961 stack_slot_get_name (ILStackDesc
*value
)
963 return type_names
[value
->stype
& TYPE_MASK
];
966 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
967 if (PRED (value)) { \
969 g_string_append (str, ", "); \
970 g_string_append (str, NAME); \
975 stack_slot_stack_type_full_name (ILStackDesc
*value
)
977 GString
*str
= g_string_new ("");
980 if ((value
->stype
& TYPE_MASK
) != value
->stype
) {
981 gboolean first
= TRUE
;
982 g_string_append(str
, "[");
983 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer
, "this");
984 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value
, "boxed");
985 APPEND_WITH_PREDICATE (stack_slot_is_null_literal
, "null");
986 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer
, "cmmp");
987 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer
, "mp");
988 g_string_append(str
, "] ");
991 g_string_append (str
, stack_slot_get_name (value
));
993 g_string_free (str
, FALSE
);
998 stack_slot_full_name (ILStackDesc
*value
)
1000 char *type_name
= mono_type_full_name (value
->type
);
1001 char *stack_name
= stack_slot_stack_type_full_name (value
);
1002 char *res
= g_strdup_printf ("%s (%s)", type_name
, stack_name
);
1004 g_free (stack_name
);
1008 //////////////////////////////////////////////////////////////////
1010 mono_free_verify_list (GSList
*list
)
1012 MonoVerifyInfoExtended
*info
;
1015 for (tmp
= list
; tmp
; tmp
= tmp
->next
) {
1017 g_free (info
->info
.message
);
1020 g_slist_free (list
);
1023 #define ADD_ERROR(list,msg) \
1025 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1026 vinfo->info.status = MONO_VERIFY_ERROR; \
1027 vinfo->info.message = (msg); \
1028 (list) = g_slist_prepend ((list), vinfo); \
1031 #define ADD_WARN(list,code,msg) \
1033 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1034 vinfo->info.status = (code); \
1035 vinfo->info.message = (msg); \
1036 (list) = g_slist_prepend ((list), vinfo); \
1040 valid_cultures
[][9] = {
1041 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
1042 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
1043 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
1044 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
1045 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
1046 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
1047 "da-DK", "de-DE", "de-CH", "de-AT",
1048 "de-LU", "de-LI", "el-GR", "en-US",
1049 "en-GB", "en-AU", "en-CA", "en-NZ",
1050 "en-IE", "en-ZA", "en-JM", "en-CB",
1051 "en-BZ", "en-TT", "en-ZW", "en-PH",
1052 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
1053 "es-CR", "es-PA", "es-DO", "es-VE",
1054 "es-CO", "es-PE", "es-AR", "es-EC",
1055 "es-CL", "es-UY", "es-PY", "es-BO",
1056 "es-SV", "es-HN", "es-NI", "es-PR",
1057 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
1058 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
1059 "hu-HU", "is-IS", "it-IT", "it-CH",
1060 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
1061 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
1062 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
1063 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
1064 "sv-SE", "sv-FI", "th-TH", "tr-TR",
1065 "ur-PK", "id-ID", "uk-UA", "be-BY",
1066 "sl-SI", "et-EE", "lv-LV", "lt-LT",
1067 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
1069 "eu-ES", "mk-MK", "af-ZA",
1070 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
1071 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
1072 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
1073 "gu-IN", "ta-IN", "te-IN", "kn-IN",
1074 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
1075 "kok-IN", "syr-SY", "div-MV"
1079 is_valid_culture (const char *cname
)
1084 found
= *cname
== 0;
1085 for (i
= 0; i
< G_N_ELEMENTS (valid_cultures
); ++i
) {
1086 if (g_ascii_strcasecmp (valid_cultures
[i
], cname
)) {
1095 is_valid_assembly_flags (guint32 flags
) {
1096 /* Metadata: 22.1.2 */
1097 flags
&= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
1098 return ((flags
== 1) || (flags
== 0));
1102 is_valid_blob (MonoImage
*image
, guint32 blob_index
, int notnull
)
1105 const char *p
, *blob_end
;
1107 if (blob_index
>= image
->heap_blob
.size
)
1109 p
= mono_metadata_blob_heap (image
, blob_index
);
1110 size
= mono_metadata_decode_blob_size (p
, &blob_end
);
1111 if (blob_index
+ size
+ (blob_end
-p
) > image
->heap_blob
.size
)
1113 if (notnull
&& !size
)
1119 is_valid_string (MonoImage
*image
, guint32 str_index
, int notnull
)
1121 const char *p
, *blob_end
, *res
;
1123 if (str_index
>= image
->heap_strings
.size
)
1125 res
= p
= mono_metadata_string_heap (image
, str_index
);
1126 blob_end
= mono_metadata_string_heap (image
, image
->heap_strings
.size
- 1);
1130 * FIXME: should check it's a valid utf8 string, too.
1132 while (p
<= blob_end
) {
1137 return *p
? NULL
: res
;
1141 is_valid_cls_ident (const char *p
)
1144 * FIXME: we need the full unicode glib support for this.
1145 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
1146 * We do the lame thing for now.
1152 if (!isalnum (*p
) && *p
!= '_')
1160 is_valid_filename (const char *p
)
1164 return strpbrk (p
, "\\//:")? 0: 1;
1168 verify_assembly_table (MonoImage
*image
, GSList
*list
, int level
)
1170 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_ASSEMBLY
];
1171 guint32 cols
[MONO_ASSEMBLY_SIZE
];
1174 if (level
& MONO_VERIFY_ERROR
) {
1176 ADD_ERROR (list
, g_strdup ("Assembly table may only have 0 or 1 rows"));
1177 mono_metadata_decode_row (t
, 0, cols
, MONO_ASSEMBLY_SIZE
);
1179 switch (cols
[MONO_ASSEMBLY_HASH_ALG
]) {
1180 case ASSEMBLY_HASH_NONE
:
1181 case ASSEMBLY_HASH_MD5
:
1182 case ASSEMBLY_HASH_SHA1
:
1185 ADD_ERROR (list
, g_strdup_printf ("Hash algorithm 0x%x unknown", cols
[MONO_ASSEMBLY_HASH_ALG
]));
1188 if (!is_valid_assembly_flags (cols
[MONO_ASSEMBLY_FLAGS
]))
1189 ADD_ERROR (list
, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols
[MONO_ASSEMBLY_FLAGS
]));
1191 if (!is_valid_blob (image
, cols
[MONO_ASSEMBLY_PUBLIC_KEY
], FALSE
))
1192 ADD_ERROR (list
, g_strdup ("Assembly public key is an invalid index"));
1194 if (!(p
= is_valid_string (image
, cols
[MONO_ASSEMBLY_NAME
], TRUE
))) {
1195 ADD_ERROR (list
, g_strdup ("Assembly name is invalid"));
1197 if (strpbrk (p
, ":\\/."))
1198 ADD_ERROR (list
, g_strdup_printf ("Assembly name `%s' contains invalid chars", p
));
1201 if (!(p
= is_valid_string (image
, cols
[MONO_ASSEMBLY_CULTURE
], FALSE
))) {
1202 ADD_ERROR (list
, g_strdup ("Assembly culture is an invalid index"));
1204 if (!is_valid_culture (p
))
1205 ADD_ERROR (list
, g_strdup_printf ("Assembly culture `%s' is invalid", p
));
1212 verify_assemblyref_table (MonoImage
*image
, GSList
*list
, int level
)
1214 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_ASSEMBLYREF
];
1215 guint32 cols
[MONO_ASSEMBLYREF_SIZE
];
1219 if (level
& MONO_VERIFY_ERROR
) {
1220 for (i
= 0; i
< t
->rows
; ++i
) {
1221 mono_metadata_decode_row (t
, i
, cols
, MONO_ASSEMBLYREF_SIZE
);
1222 if (!is_valid_assembly_flags (cols
[MONO_ASSEMBLYREF_FLAGS
]))
1223 ADD_ERROR (list
, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i
+ 1, cols
[MONO_ASSEMBLY_FLAGS
]));
1225 if (!is_valid_blob (image
, cols
[MONO_ASSEMBLYREF_PUBLIC_KEY
], FALSE
))
1226 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i
+ 1));
1228 if (!(p
= is_valid_string (image
, cols
[MONO_ASSEMBLYREF_CULTURE
], FALSE
))) {
1229 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i
+ 1));
1231 if (!is_valid_culture (p
))
1232 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p
, i
+ 1));
1235 if (cols
[MONO_ASSEMBLYREF_HASH_VALUE
] && !is_valid_blob (image
, cols
[MONO_ASSEMBLYREF_HASH_VALUE
], TRUE
))
1236 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i
+ 1));
1239 if (level
& MONO_VERIFY_WARNING
) {
1240 /* check for duplicated rows */
1241 for (i
= 0; i
< t
->rows
; ++i
) {
1248 verify_class_layout_table (MonoImage
*image
, GSList
*list
, int level
)
1250 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_CLASSLAYOUT
];
1251 MonoTableInfo
*tdef
= &image
->tables
[MONO_TABLE_TYPEDEF
];
1252 guint32 cols
[MONO_CLASS_LAYOUT_SIZE
];
1255 if (level
& MONO_VERIFY_ERROR
) {
1256 for (i
= 0; i
< t
->rows
; ++i
) {
1257 mono_metadata_decode_row (t
, i
, cols
, MONO_CLASS_LAYOUT_SIZE
);
1259 if (cols
[MONO_CLASS_LAYOUT_PARENT
] > tdef
->rows
|| !cols
[MONO_CLASS_LAYOUT_PARENT
]) {
1260 ADD_ERROR (list
, g_strdup_printf ("Parent in class layout is invalid in row %d", i
+ 1));
1262 value
= mono_metadata_decode_row_col (tdef
, cols
[MONO_CLASS_LAYOUT_PARENT
] - 1, MONO_TYPEDEF_FLAGS
);
1263 if (value
& TYPE_ATTRIBUTE_INTERFACE
)
1264 ADD_ERROR (list
, g_strdup_printf ("Parent in class layout row %d is an interface", i
+ 1));
1265 if (value
& TYPE_ATTRIBUTE_AUTO_LAYOUT
)
1266 ADD_ERROR (list
, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i
+ 1));
1267 if (value
& TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT
) {
1268 switch (cols
[MONO_CLASS_LAYOUT_PACKING_SIZE
]) {
1269 case 0: case 1: case 2: case 4: case 8: case 16:
1270 case 32: case 64: case 128: break;
1272 ADD_ERROR (list
, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols
[MONO_CLASS_LAYOUT_PACKING_SIZE
], i
+ 1));
1274 } else if (value
& TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) {
1276 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
1277 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
1278 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));
1282 * FIXME: we need to check that if class size != 0,
1283 * it needs to be greater than the class calculated size.
1284 * If parent is a valuetype it also needs to be smaller than
1285 * 1 MByte (0x100000 bytes).
1286 * To do both these checks we need to load the referenced
1287 * assemblies, though (the spec claims we didn't have to, bah).
1290 * We need to check that the parent types have the same layout
1301 verify_constant_table (MonoImage
*image
, GSList
*list
, int level
)
1303 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_CONSTANT
];
1304 guint32 cols
[MONO_CONSTANT_SIZE
];
1306 GHashTable
*dups
= g_hash_table_new (NULL
, NULL
);
1308 for (i
= 0; i
< t
->rows
; ++i
) {
1309 mono_metadata_decode_row (t
, i
, cols
, MONO_CONSTANT_SIZE
);
1311 if (level
& MONO_VERIFY_ERROR
)
1312 if (g_hash_table_lookup (dups
, GUINT_TO_POINTER (cols
[MONO_CONSTANT_PARENT
])))
1313 ADD_ERROR (list
, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols
[MONO_CONSTANT_PARENT
], i
+ 1));
1314 g_hash_table_insert (dups
, GUINT_TO_POINTER (cols
[MONO_CONSTANT_PARENT
]),
1315 GUINT_TO_POINTER (cols
[MONO_CONSTANT_PARENT
]));
1317 switch (cols
[MONO_CONSTANT_TYPE
]) {
1318 case MONO_TYPE_U1
: /* LAMESPEC: it says I1...*/
1322 if (level
& MONO_VERIFY_CLS
)
1323 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));
1324 case MONO_TYPE_BOOLEAN
:
1325 case MONO_TYPE_CHAR
:
1332 case MONO_TYPE_STRING
:
1333 case MONO_TYPE_CLASS
:
1336 if (level
& MONO_VERIFY_ERROR
)
1337 ADD_ERROR (list
, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols
[MONO_CONSTANT_TYPE
], i
+ 1));
1339 if (level
& MONO_VERIFY_ERROR
) {
1340 value
= cols
[MONO_CONSTANT_PARENT
] >> MONO_HASCONSTANT_BITS
;
1341 switch (cols
[MONO_CONSTANT_PARENT
] & MONO_HASCONSTANT_MASK
) {
1342 case MONO_HASCONSTANT_FIEDDEF
:
1343 if (value
> image
->tables
[MONO_TABLE_FIELD
].rows
)
1344 ADD_ERROR (list
, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i
+ 1));
1346 case MONO_HASCONSTANT_PARAM
:
1347 if (value
> image
->tables
[MONO_TABLE_PARAM
].rows
)
1348 ADD_ERROR (list
, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i
+ 1));
1350 case MONO_HASCONSTANT_PROPERTY
:
1351 if (value
> image
->tables
[MONO_TABLE_PROPERTY
].rows
)
1352 ADD_ERROR (list
, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i
+ 1));
1355 ADD_ERROR (list
, g_strdup_printf ("Parent is invalid in Constant row %d", i
+ 1));
1359 if (level
& MONO_VERIFY_CLS
) {
1361 * FIXME: verify types is consistent with the enum type
1362 * is parent is an enum.
1366 g_hash_table_destroy (dups
);
1371 verify_event_map_table (MonoImage
*image
, GSList
*list
, int level
)
1373 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EVENTMAP
];
1374 guint32 cols
[MONO_EVENT_MAP_SIZE
];
1375 guint32 i
, last_event
;
1376 GHashTable
*dups
= g_hash_table_new (NULL
, NULL
);
1380 for (i
= 0; i
< t
->rows
; ++i
) {
1381 mono_metadata_decode_row (t
, i
, cols
, MONO_EVENT_MAP_SIZE
);
1382 if (level
& MONO_VERIFY_ERROR
)
1383 if (g_hash_table_lookup (dups
, GUINT_TO_POINTER (cols
[MONO_EVENT_MAP_PARENT
])))
1384 ADD_ERROR (list
, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols
[MONO_EVENT_MAP_PARENT
], i
+ 1));
1385 g_hash_table_insert (dups
, GUINT_TO_POINTER (cols
[MONO_EVENT_MAP_PARENT
]),
1386 GUINT_TO_POINTER (cols
[MONO_EVENT_MAP_PARENT
]));
1387 if (level
& MONO_VERIFY_ERROR
) {
1388 if (cols
[MONO_EVENT_MAP_PARENT
] > image
->tables
[MONO_TABLE_TYPEDEF
].rows
)
1389 ADD_ERROR (list
, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols
[MONO_EVENT_MAP_PARENT
], i
+ 1));
1390 if (cols
[MONO_EVENT_MAP_EVENTLIST
] > image
->tables
[MONO_TABLE_EVENT
].rows
)
1391 ADD_ERROR (list
, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols
[MONO_EVENT_MAP_EVENTLIST
], i
+ 1));
1393 if (cols
[MONO_EVENT_MAP_EVENTLIST
] <= last_event
)
1394 ADD_ERROR (list
, g_strdup_printf ("EventList overlap in Event Map row %d", i
+ 1));
1395 last_event
= cols
[MONO_EVENT_MAP_EVENTLIST
];
1399 g_hash_table_destroy (dups
);
1404 verify_event_table (MonoImage
*image
, GSList
*list
, int level
)
1406 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EVENT
];
1407 guint32 cols
[MONO_EVENT_SIZE
];
1411 for (i
= 0; i
< t
->rows
; ++i
) {
1412 mono_metadata_decode_row (t
, i
, cols
, MONO_EVENT_SIZE
);
1414 if (cols
[MONO_EVENT_FLAGS
] & ~(EVENT_SPECIALNAME
|EVENT_RTSPECIALNAME
)) {
1415 if (level
& MONO_VERIFY_ERROR
)
1416 ADD_ERROR (list
, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols
[MONO_EVENT_FLAGS
], i
+ 1));
1418 if (!(p
= is_valid_string (image
, cols
[MONO_EVENT_NAME
], TRUE
))) {
1419 if (level
& MONO_VERIFY_ERROR
)
1420 ADD_ERROR (list
, g_strdup_printf ("Invalid name in Event row %d", i
+ 1));
1422 if (level
& MONO_VERIFY_CLS
) {
1423 if (!is_valid_cls_ident (p
))
1424 ADD_WARN (list
, MONO_VERIFY_CLS
, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p
, i
+ 1));
1428 if (level
& MONO_VERIFY_ERROR
&& cols
[MONO_EVENT_TYPE
]) {
1429 value
= cols
[MONO_EVENT_TYPE
] >> MONO_TYPEDEFORREF_BITS
;
1430 switch (cols
[MONO_EVENT_TYPE
] & MONO_TYPEDEFORREF_MASK
) {
1431 case MONO_TYPEDEFORREF_TYPEDEF
:
1432 if (!value
|| value
> image
->tables
[MONO_TABLE_TYPEDEF
].rows
)
1433 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1435 case MONO_TYPEDEFORREF_TYPEREF
:
1436 if (!value
|| value
> image
->tables
[MONO_TABLE_TYPEREF
].rows
)
1437 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1439 case MONO_TYPEDEFORREF_TYPESPEC
:
1440 if (!value
|| value
> image
->tables
[MONO_TABLE_TYPESPEC
].rows
)
1441 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1444 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1448 * FIXME: check that there is 1 add and remove row in methodsemantics
1449 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
1450 * these while checking methodsemantics).
1451 * check for duplicated names for the same type [ERROR]
1452 * check for CLS duplicate names for the same type [CLS]
1459 verify_field_table (MonoImage
*image
, GSList
*list
, int level
)
1461 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_FIELD
];
1462 guint32 cols
[MONO_FIELD_SIZE
];
1466 for (i
= 0; i
< t
->rows
; ++i
) {
1467 mono_metadata_decode_row (t
, i
, cols
, MONO_FIELD_SIZE
);
1469 * Check this field has only one owner and that the owner is not
1470 * an interface (done in verify_typedef_table() )
1472 flags
= cols
[MONO_FIELD_FLAGS
];
1473 switch (flags
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
) {
1474 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED
:
1475 case FIELD_ATTRIBUTE_PRIVATE
:
1476 case FIELD_ATTRIBUTE_FAM_AND_ASSEM
:
1477 case FIELD_ATTRIBUTE_ASSEMBLY
:
1478 case FIELD_ATTRIBUTE_FAMILY
:
1479 case FIELD_ATTRIBUTE_FAM_OR_ASSEM
:
1480 case FIELD_ATTRIBUTE_PUBLIC
:
1483 if (level
& MONO_VERIFY_ERROR
)
1484 ADD_ERROR (list
, g_strdup_printf ("Invalid access mask in Field row %d", i
+ 1));
1487 if (level
& MONO_VERIFY_ERROR
) {
1488 if ((flags
& FIELD_ATTRIBUTE_LITERAL
) && (flags
& FIELD_ATTRIBUTE_INIT_ONLY
))
1489 ADD_ERROR (list
, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i
+ 1));
1490 if ((flags
& FIELD_ATTRIBUTE_LITERAL
) && !(flags
& FIELD_ATTRIBUTE_STATIC
))
1491 ADD_ERROR (list
, g_strdup_printf ("Literal needs also Static set in Field row %d", i
+ 1));
1492 if ((flags
& FIELD_ATTRIBUTE_RT_SPECIAL_NAME
) && !(flags
& FIELD_ATTRIBUTE_SPECIAL_NAME
))
1493 ADD_ERROR (list
, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i
+ 1));
1495 * FIXME: check there is only one owner in the respective table.
1496 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
1497 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
1498 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
1501 if (!(p
= is_valid_string (image
, cols
[MONO_FIELD_NAME
], TRUE
))) {
1502 if (level
& MONO_VERIFY_ERROR
)
1503 ADD_ERROR (list
, g_strdup_printf ("Invalid name in Field row %d", i
+ 1));
1505 if (level
& MONO_VERIFY_CLS
) {
1506 if (!is_valid_cls_ident (p
))
1507 ADD_WARN (list
, MONO_VERIFY_CLS
, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p
, i
+ 1));
1512 * if owner is module needs to be static, access mask needs to be compilercontrolled,
1513 * public or private (not allowed in cls mode).
1514 * if owner is an enum ...
1523 verify_file_table (MonoImage
*image
, GSList
*list
, int level
)
1525 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_FILE
];
1526 guint32 cols
[MONO_FILE_SIZE
];
1529 GHashTable
*dups
= g_hash_table_new (g_str_hash
, g_str_equal
);
1531 for (i
= 0; i
< t
->rows
; ++i
) {
1532 mono_metadata_decode_row (t
, i
, cols
, MONO_FILE_SIZE
);
1533 if (level
& MONO_VERIFY_ERROR
) {
1534 if (cols
[MONO_FILE_FLAGS
] != FILE_CONTAINS_METADATA
&& cols
[MONO_FILE_FLAGS
] != FILE_CONTAINS_NO_METADATA
)
1535 ADD_ERROR (list
, g_strdup_printf ("Invalid flags in File row %d", i
+ 1));
1536 if (!is_valid_blob (image
, cols
[MONO_FILE_HASH_VALUE
], TRUE
))
1537 ADD_ERROR (list
, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i
+ 1));
1539 if (!(p
= is_valid_string (image
, cols
[MONO_FILE_NAME
], TRUE
))) {
1540 if (level
& MONO_VERIFY_ERROR
)
1541 ADD_ERROR (list
, g_strdup_printf ("Invalid name in File row %d", i
+ 1));
1543 if (level
& MONO_VERIFY_ERROR
) {
1544 if (!is_valid_filename (p
))
1545 ADD_ERROR (list
, g_strdup_printf ("Invalid name '%s` in File row %d", p
, i
+ 1));
1546 else if (g_hash_table_lookup (dups
, p
)) {
1547 ADD_ERROR (list
, g_strdup_printf ("Duplicate name '%s` in File row %d", p
, i
+ 1));
1549 g_hash_table_insert (dups
, (gpointer
)p
, (gpointer
)p
);
1553 * FIXME: I don't understand what this means:
1554 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
1555 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
1559 if (level
& MONO_VERIFY_WARNING
) {
1560 if (!t
->rows
&& image
->tables
[MONO_TABLE_EXPORTEDTYPE
].rows
)
1561 ADD_WARN (list
, MONO_VERIFY_WARNING
, g_strdup ("ExportedType table should be empty if File table is empty"));
1563 g_hash_table_destroy (dups
);
1568 verify_moduleref_table (MonoImage
*image
, GSList
*list
, int level
)
1570 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_MODULEREF
];
1571 MonoTableInfo
*tfile
= &image
->tables
[MONO_TABLE_FILE
];
1572 guint32 cols
[MONO_MODULEREF_SIZE
];
1574 guint32 found
, i
, j
, value
;
1575 GHashTable
*dups
= g_hash_table_new (g_str_hash
, g_str_equal
);
1577 for (i
= 0; i
< t
->rows
; ++i
) {
1578 mono_metadata_decode_row (t
, i
, cols
, MONO_MODULEREF_SIZE
);
1579 if (!(p
= is_valid_string (image
, cols
[MONO_MODULEREF_NAME
], TRUE
))) {
1580 if (level
& MONO_VERIFY_ERROR
)
1581 ADD_ERROR (list
, g_strdup_printf ("Invalid name in ModuleRef row %d", i
+ 1));
1583 if (level
& MONO_VERIFY_ERROR
) {
1584 if (!is_valid_filename (p
))
1585 ADD_ERROR (list
, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p
, i
+ 1));
1586 else if (g_hash_table_lookup (dups
, p
)) {
1587 ADD_WARN (list
, MONO_VERIFY_WARNING
, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p
, i
+ 1));
1588 g_hash_table_insert (dups
, (gpointer
)p
, (gpointer
)p
);
1590 for (j
= 0; j
< tfile
->rows
; ++j
) {
1591 value
= mono_metadata_decode_row_col (tfile
, j
, MONO_FILE_NAME
);
1592 if ((pf
= is_valid_string (image
, value
, TRUE
)))
1593 if (strcmp (p
, pf
) == 0) {
1599 ADD_ERROR (list
, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p
, i
+ 1));
1604 g_hash_table_destroy (dups
);
1609 verify_standalonesig_table (MonoImage
*image
, GSList
*list
, int level
)
1611 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_STANDALONESIG
];
1612 guint32 cols
[MONO_STAND_ALONE_SIGNATURE_SIZE
];
1616 for (i
= 0; i
< t
->rows
; ++i
) {
1617 mono_metadata_decode_row (t
, i
, cols
, MONO_STAND_ALONE_SIGNATURE_SIZE
);
1618 if (level
& MONO_VERIFY_ERROR
) {
1619 if (!is_valid_blob (image
, cols
[MONO_STAND_ALONE_SIGNATURE
], TRUE
)) {
1620 ADD_ERROR (list
, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i
+ 1));
1622 p
= mono_metadata_blob_heap (image
, cols
[MONO_STAND_ALONE_SIGNATURE
]);
1623 /* FIXME: check it's a valid locals or method sig.*/
1631 mono_image_verify_tables (MonoImage
*image
, int level
)
1633 GSList
*error_list
= NULL
;
1635 error_list
= verify_assembly_table (image
, error_list
, level
);
1637 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
1638 * AssemblyRefProcessor should be ignored,
1639 * though we may want to emit a warning, since it should not
1640 * be present in a PE file.
1642 error_list
= verify_assemblyref_table (image
, error_list
, level
);
1643 error_list
= verify_class_layout_table (image
, error_list
, level
);
1644 error_list
= verify_constant_table (image
, error_list
, level
);
1646 * cutom attribute, declsecurity
1648 error_list
= verify_event_map_table (image
, error_list
, level
);
1649 error_list
= verify_event_table (image
, error_list
, level
);
1650 error_list
= verify_field_table (image
, error_list
, level
);
1651 error_list
= verify_file_table (image
, error_list
, level
);
1652 error_list
= verify_moduleref_table (image
, error_list
, level
);
1653 error_list
= verify_standalonesig_table (image
, error_list
, level
);
1655 return g_slist_reverse (error_list
);
1658 #define ADD_INVALID(list,msg) \
1660 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1661 vinfo->status = MONO_VERIFY_ERROR; \
1662 vinfo->message = (msg); \
1663 (list) = g_slist_prepend ((list), vinfo); \
1664 /*G_BREAKPOINT ();*/ \
1668 #define CHECK_STACK_UNDERFLOW(num) \
1670 if (cur_stack < (num)) \
1671 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1674 #define CHECK_STACK_OVERFLOW() \
1676 if (cur_stack >= max_stack) \
1677 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1682 in_any_block (MonoMethodHeader
*header
, guint offset
)
1685 MonoExceptionClause
*clause
;
1687 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1688 clause
= &header
->clauses
[i
];
1689 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1691 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1693 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1700 * in_any_exception_block:
1702 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1705 in_any_exception_block (MonoMethodHeader
*header
, guint offset
)
1708 MonoExceptionClause
*clause
;
1710 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1711 clause
= &header
->clauses
[i
];
1712 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1714 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1721 * is_valid_branch_instruction:
1723 * Verify if it's valid to perform a branch from @offset to @target.
1724 * This should be used with br and brtrue/false.
1725 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1726 * The major diferent from other similiar functions is that branching into a
1727 * finally/fault block is invalid instead of just unverifiable.
1730 is_valid_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1733 MonoExceptionClause
*clause
;
1735 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1736 clause
= &header
->clauses
[i
];
1737 /*branching into a finally block is invalid*/
1738 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
) &&
1739 !MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1740 MONO_OFFSET_IN_HANDLER (clause
, target
))
1743 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1745 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1747 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1754 * is_valid_cmp_branch_instruction:
1756 * Verify if it's valid to perform a branch from @offset to @target.
1757 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1758 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1760 * The major diferences from other similar functions are that most errors lead to invalid
1761 * code and only branching out of finally, filter or fault clauses is unverifiable.
1764 is_valid_cmp_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1767 MonoExceptionClause
*clause
;
1769 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1770 clause
= &header
->clauses
[i
];
1771 /*branching out of a handler or finally*/
1772 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
&&
1773 MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1774 !MONO_OFFSET_IN_HANDLER (clause
, target
))
1777 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1779 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1781 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1788 * A leave can't escape a finally block
1791 is_correct_leave (MonoMethodHeader
*header
, guint offset
, guint target
)
1794 MonoExceptionClause
*clause
;
1796 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1797 clause
= &header
->clauses
[i
];
1798 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
&& MONO_OFFSET_IN_HANDLER (clause
, offset
) && !MONO_OFFSET_IN_HANDLER (clause
, target
))
1800 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1807 * A rethrow can't happen outside of a catch handler.
1810 is_correct_rethrow (MonoMethodHeader
*header
, guint offset
)
1813 MonoExceptionClause
*clause
;
1815 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1816 clause
= &header
->clauses
[i
];
1817 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1819 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1826 * An endfinally can't happen outside of a finally/fault handler.
1829 is_correct_endfinally (MonoMethodHeader
*header
, guint offset
)
1832 MonoExceptionClause
*clause
;
1834 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1835 clause
= &header
->clauses
[i
];
1836 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) && (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
))
1844 * An endfilter can only happens inside a filter clause.
1845 * In non-strict mode filter is allowed inside the handler clause too
1847 static MonoExceptionClause
*
1848 is_correct_endfilter (VerifyContext
*ctx
, guint offset
)
1851 MonoExceptionClause
*clause
;
1853 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1854 clause
= &ctx
->header
->clauses
[i
];
1855 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_FILTER
)
1857 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1859 if (!IS_STRICT_MODE (ctx
) && MONO_OFFSET_IN_HANDLER (clause
, offset
))
1867 * Non-strict endfilter can happens inside a try block or any handler block
1870 is_unverifiable_endfilter (VerifyContext
*ctx
, guint offset
)
1873 MonoExceptionClause
*clause
;
1875 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1876 clause
= &ctx
->header
->clauses
[i
];
1877 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1884 is_valid_bool_arg (ILStackDesc
*arg
)
1886 if (stack_slot_is_managed_pointer (arg
) || stack_slot_is_boxed_value (arg
) || stack_slot_is_null_literal (arg
))
1890 switch (stack_slot_get_underlying_type (arg
)) {
1893 case TYPE_NATIVE_INT
:
1897 g_assert (arg
->type
);
1898 switch (arg
->type
->type
) {
1899 case MONO_TYPE_CLASS
:
1900 case MONO_TYPE_STRING
:
1901 case MONO_TYPE_OBJECT
:
1902 case MONO_TYPE_SZARRAY
:
1903 case MONO_TYPE_ARRAY
:
1904 case MONO_TYPE_FNPTR
:
1907 case MONO_TYPE_GENERICINST
:
1908 /*We need to check if the container class
1909 * of the generic type is a valuetype, iow:
1910 * is it a "class Foo<T>" or a "struct Foo<T>"?
1912 return !arg
->type
->data
.generic_class
->container_class
->valuetype
;
1920 /*Type manipulation helper*/
1922 /*Returns the byref version of the supplied MonoType*/
1924 mono_type_get_type_byref (MonoType
*type
)
1928 return &mono_class_from_mono_type (type
)->this_arg
;
1932 /*Returns the byval version of the supplied MonoType*/
1934 mono_type_get_type_byval (MonoType
*type
)
1938 return &mono_class_from_mono_type (type
)->byval_arg
;
1942 mono_type_from_stack_slot (ILStackDesc
*slot
)
1944 if (stack_slot_is_managed_pointer (slot
))
1945 return mono_type_get_type_byref (slot
->type
);
1949 /*Stack manipulation code*/
1952 stack_init (VerifyContext
*ctx
, ILCodeDesc
*state
)
1954 if (state
->flags
& IL_CODE_FLAG_STACK_INITED
)
1957 state
->flags
|= IL_CODE_FLAG_STACK_INITED
;
1959 state
->stack
= g_new0 (ILStackDesc
, ctx
->max_stack
);
1963 stack_copy (ILCodeDesc
*to
, ILCodeDesc
*from
)
1965 to
->size
= from
->size
;
1966 memcpy (to
->stack
, from
->stack
, sizeof (ILStackDesc
) * from
->size
);
1970 copy_stack_value (ILStackDesc
*to
, ILStackDesc
*from
)
1972 to
->stype
= from
->stype
;
1973 to
->type
= from
->type
;
1974 to
->method
= from
->method
;
1978 check_underflow (VerifyContext
*ctx
, int size
)
1980 if (ctx
->eval
.size
< size
) {
1981 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size
, ctx
->eval
.size
, ctx
->ip_offset
));
1988 check_overflow (VerifyContext
*ctx
)
1990 if (ctx
->eval
.size
>= ctx
->max_stack
) {
1991 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx
->eval
.size
+ 1, ctx
->ip_offset
));
1997 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1999 check_unmanaged_pointer (VerifyContext
*ctx
, ILStackDesc
*value
)
2001 if (stack_slot_get_type (value
) == TYPE_PTR
) {
2002 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
2008 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
2010 check_unverifiable_type (VerifyContext
*ctx
, MonoType
*type
)
2012 if (type
->type
== MONO_TYPE_PTR
|| type
->type
== MONO_TYPE_FNPTR
) {
2013 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
2020 static ILStackDesc
*
2021 stack_push (VerifyContext
*ctx
)
2023 return & ctx
->eval
.stack
[ctx
->eval
.size
++];
2026 static ILStackDesc
*
2027 stack_push_val (VerifyContext
*ctx
, int stype
, MonoType
*type
)
2029 ILStackDesc
*top
= stack_push (ctx
);
2035 static ILStackDesc
*
2036 stack_pop (VerifyContext
*ctx
)
2038 ILStackDesc
*ret
= ctx
->eval
.stack
+ --ctx
->eval
.size
;
2039 if ((ret
->stype
& UNINIT_THIS_MASK
) == UNINIT_THIS_MASK
)
2040 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx
->ip_offset
));
2044 /* This function allows to safely pop an unititialized this ptr from
2045 * the eval stack without marking the method as unverifiable.
2047 static ILStackDesc
*
2048 stack_pop_safe (VerifyContext
*ctx
)
2050 return ctx
->eval
.stack
+ --ctx
->eval
.size
;
2053 static ILStackDesc
*
2054 stack_push_stack_val (VerifyContext
*ctx
, ILStackDesc
*value
)
2056 ILStackDesc
*top
= stack_push (ctx
);
2057 copy_stack_value (top
, value
);
2061 /* Returns the MonoType associated with the token, or NULL if it is invalid.
2063 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
2066 get_boxable_mono_type (VerifyContext
* ctx
, int token
, const char *opcode
)
2071 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
2074 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
2075 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2079 if (type
->type
== MONO_TYPE_VOID
) {
2080 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2084 if (type
->type
== MONO_TYPE_TYPEDBYREF
)
2085 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2087 if (!(class = mono_class_from_mono_type (type
)))
2088 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2090 if (class->generic_container
&& type
->type
!= MONO_TYPE_GENERICINST
)
2091 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use the generic type definition in a boxable type position for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2093 check_unverifiable_type (ctx
, type
);
2098 /*operation result tables */
2100 static const unsigned char bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2101 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2102 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2103 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2104 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2105 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2106 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2109 static const unsigned char add_table
[TYPE_MAX
][TYPE_MAX
] = {
2110 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2111 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2112 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2113 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2114 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2115 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2118 static const unsigned char sub_table
[TYPE_MAX
][TYPE_MAX
] = {
2119 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2120 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2121 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2122 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2123 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2124 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2127 static const unsigned char int_bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2128 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2129 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2130 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2131 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2132 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2133 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2136 static const unsigned char shift_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2137 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2138 {TYPE_I8
, TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2139 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2140 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2141 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2142 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2145 static const unsigned char cmp_br_op
[TYPE_MAX
][TYPE_MAX
] = {
2146 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2147 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2148 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2149 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
2150 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
2151 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2154 static const unsigned char cmp_br_eq_op
[TYPE_MAX
][TYPE_MAX
] = {
2155 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2156 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2157 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2158 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
2159 {TYPE_INV
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
2160 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
},
2163 static const unsigned char add_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
2164 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2165 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2166 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2167 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2168 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2169 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2172 static const unsigned char sub_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
2173 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2174 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2175 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2176 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2177 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2178 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2181 static const unsigned char bin_ovf_table
[TYPE_MAX
][TYPE_MAX
] = {
2182 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2183 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2184 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2185 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2186 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2187 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2190 #ifdef MONO_VERIFIER_DEBUG
2194 dump_stack_value (ILStackDesc
*value
)
2196 printf ("[(%x)(%x)", value
->type
->type
, value
->stype
);
2198 if (stack_slot_is_this_pointer (value
))
2201 if (stack_slot_is_boxed_value (value
))
2202 printf ("[boxed] ");
2204 if (stack_slot_is_null_literal (value
))
2207 if (stack_slot_is_managed_mutability_pointer (value
))
2208 printf ("Controled Mutability MP: ");
2210 if (stack_slot_is_managed_pointer (value
))
2211 printf ("Managed Pointer to: ");
2213 switch (stack_slot_get_underlying_type (value
)) {
2215 printf ("invalid type]");
2223 case TYPE_NATIVE_INT
:
2224 printf ("native int]");
2227 printf ("float64]");
2230 printf ("unmanaged pointer]");
2233 switch (value
->type
->type
) {
2234 case MONO_TYPE_CLASS
:
2235 case MONO_TYPE_VALUETYPE
:
2236 printf ("complex] (%s)", value
->type
->data
.klass
->name
);
2238 case MONO_TYPE_STRING
:
2239 printf ("complex] (string)");
2241 case MONO_TYPE_OBJECT
:
2242 printf ("complex] (object)");
2244 case MONO_TYPE_SZARRAY
:
2245 printf ("complex] (%s [])", value
->type
->data
.klass
->name
);
2247 case MONO_TYPE_ARRAY
:
2248 printf ("complex] (%s [%d %d %d])",
2249 value
->type
->data
.array
->eklass
->name
,
2250 value
->type
->data
.array
->rank
,
2251 value
->type
->data
.array
->numsizes
,
2252 value
->type
->data
.array
->numlobounds
);
2254 case MONO_TYPE_GENERICINST
:
2255 printf ("complex] (inst of %s )", value
->type
->data
.generic_class
->container_class
->name
);
2258 printf ("complex] (type generic param !%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
2260 case MONO_TYPE_MVAR
:
2261 printf ("complex] (method generic param !!%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
2264 //should be a boxed value
2265 char * name
= mono_type_full_name (value
->type
);
2266 printf ("complex] %s", name
);
2272 printf ("unknown stack %x type]\n", value
->stype
);
2273 g_assert_not_reached ();
2278 dump_stack_state (ILCodeDesc
*state
)
2282 printf ("(%d) ", state
->size
);
2283 for (i
= 0; i
< state
->size
; ++i
)
2284 dump_stack_value (state
->stack
+ i
);
2289 /*Returns TRUE if candidate array type can be assigned to target.
2290 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
2293 is_array_type_compatible (MonoType
*target
, MonoType
*candidate
)
2295 MonoArrayType
*left
= target
->data
.array
;
2296 MonoArrayType
*right
= candidate
->data
.array
;
2298 g_assert (target
->type
== MONO_TYPE_ARRAY
);
2299 g_assert (candidate
->type
== MONO_TYPE_ARRAY
);
2301 if (left
->rank
!= right
->rank
)
2304 return mono_class_is_assignable_from (left
->eklass
, right
->eklass
);
2308 get_stack_type (MonoType
*type
)
2311 int type_kind
= type
->type
;
2313 mask
= POINTER_MASK
;
2314 /*TODO handle CMMP_MASK */
2317 switch (type_kind
) {
2320 case MONO_TYPE_BOOLEAN
:
2323 case MONO_TYPE_CHAR
:
2326 return TYPE_I4
| mask
;
2330 return TYPE_NATIVE_INT
| mask
;
2332 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
2333 case MONO_TYPE_FNPTR
:
2335 case MONO_TYPE_TYPEDBYREF
:
2336 return TYPE_PTR
| mask
;
2339 case MONO_TYPE_MVAR
:
2341 case MONO_TYPE_CLASS
:
2342 case MONO_TYPE_STRING
:
2343 case MONO_TYPE_OBJECT
:
2344 case MONO_TYPE_SZARRAY
:
2345 case MONO_TYPE_ARRAY
:
2346 return TYPE_COMPLEX
| mask
;
2348 case MONO_TYPE_GENERICINST
:
2349 if (mono_type_is_enum_type (type
)) {
2350 type
= mono_type_get_underlying_type_any (type
);
2351 type_kind
= type
->type
;
2354 return TYPE_COMPLEX
| mask
;
2359 return TYPE_I8
| mask
;
2363 return TYPE_R8
| mask
;
2365 case MONO_TYPE_VALUETYPE
:
2366 if (mono_type_is_enum_type (type
)) {
2367 type
= mono_type_get_underlying_type_any (type
);
2368 type_kind
= type
->type
;
2371 return TYPE_COMPLEX
| mask
;
2375 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type
->type
); );
2376 g_assert_not_reached ();
2381 /* convert MonoType to ILStackDesc format (stype) */
2383 set_stack_value (VerifyContext
*ctx
, ILStackDesc
*stack
, MonoType
*type
, int take_addr
)
2386 int type_kind
= type
->type
;
2388 if (type
->byref
|| take_addr
)
2389 mask
= POINTER_MASK
;
2390 /* TODO handle CMMP_MASK */
2395 switch (type_kind
) {
2398 case MONO_TYPE_BOOLEAN
:
2401 case MONO_TYPE_CHAR
:
2404 stack
->stype
= TYPE_I4
| mask
;
2408 stack
->stype
= TYPE_NATIVE_INT
| mask
;
2411 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2412 case MONO_TYPE_FNPTR
:
2414 case MONO_TYPE_TYPEDBYREF
:
2415 stack
->stype
= TYPE_PTR
| mask
;
2418 case MONO_TYPE_CLASS
:
2419 case MONO_TYPE_STRING
:
2420 case MONO_TYPE_OBJECT
:
2421 case MONO_TYPE_SZARRAY
:
2422 case MONO_TYPE_ARRAY
:
2425 case MONO_TYPE_MVAR
:
2426 stack
->stype
= TYPE_COMPLEX
| mask
;
2429 case MONO_TYPE_GENERICINST
:
2430 if (mono_type_is_enum_type (type
)) {
2431 type
= mono_type_get_underlying_type_any (type
);
2432 type_kind
= type
->type
;
2435 stack
->stype
= TYPE_COMPLEX
| mask
;
2441 stack
->stype
= TYPE_I8
| mask
;
2445 stack
->stype
= TYPE_R8
| mask
;
2447 case MONO_TYPE_VALUETYPE
:
2448 if (mono_type_is_enum_type (type
)) {
2449 type
= mono_type_get_underlying_type_any (type
);
2450 type_kind
= type
->type
;
2453 stack
->stype
= TYPE_COMPLEX
| mask
;
2457 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type
->type
); );
2458 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type
->type
, ctx
->ip_offset
));
2465 * init_stack_with_value_at_exception_boundary:
2467 * Initialize the stack and push a given type.
2468 * The instruction is marked as been on the exception boundary.
2471 init_stack_with_value_at_exception_boundary (VerifyContext
*ctx
, ILCodeDesc
*code
, MonoClass
*klass
)
2474 MonoType
*type
= mono_class_inflate_generic_type_checked (&klass
->byval_arg
, ctx
->generic_context
, &error
);
2476 if (!mono_error_ok (&error
)) {
2477 char *name
= mono_type_get_full_name (klass
);
2478 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid class %s used for exception", name
));
2480 mono_error_cleanup (&error
);
2484 stack_init (ctx
, code
);
2485 set_stack_value (ctx
, code
->stack
, type
, FALSE
);
2486 ctx
->exception_types
= g_slist_prepend (ctx
->exception_types
, type
);
2488 code
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
2489 if (mono_type_is_generic_argument (type
))
2490 code
->stack
->stype
|= BOXED_MASK
;
2493 /*Verify if type 'candidate' can be stored in type 'target'.
2495 * If strict, check for the underlying type and not the verification stack types
2498 verify_type_compatibility_full (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
, gboolean strict
)
2500 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2501 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2503 MonoType
*original_candidate
= candidate
;
2504 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target
), mono_type_full_name (candidate
), strict
); );
2506 /*only one is byref */
2507 if (candidate
->byref
^ target
->byref
) {
2508 /* converting from native int to byref*/
2509 if (get_stack_type (candidate
) == TYPE_NATIVE_INT
&& target
->byref
) {
2510 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("using byref native int at 0x%04x", ctx
->ip_offset
));
2515 strict
|= target
->byref
;
2516 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2517 candidate
= mono_type_get_underlying_type_any (candidate
);
2520 switch (target
->type
) {
2521 case MONO_TYPE_VOID
:
2522 return candidate
->type
== MONO_TYPE_VOID
;
2525 case MONO_TYPE_BOOLEAN
:
2527 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I1
, MONO_TYPE_U1
, MONO_TYPE_BOOLEAN
);
2530 case MONO_TYPE_CHAR
:
2532 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I2
, MONO_TYPE_U2
, MONO_TYPE_CHAR
);
2534 case MONO_TYPE_U4
: {
2535 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2536 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2538 return is_native_int
|| is_int4
;
2539 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2544 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I8
, MONO_TYPE_U8
);
2549 return candidate
->type
== target
->type
;
2550 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_R4
, MONO_TYPE_R8
);
2554 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2555 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2557 return is_native_int
|| is_int4
;
2558 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2562 if (candidate
->type
!= MONO_TYPE_PTR
)
2564 /* check the underlying type */
2565 return verify_type_compatibility_full (ctx
, target
->data
.type
, candidate
->data
.type
, TRUE
);
2567 case MONO_TYPE_FNPTR
: {
2568 MonoMethodSignature
*left
, *right
;
2569 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2572 left
= mono_type_get_signature (target
);
2573 right
= mono_type_get_signature (candidate
);
2574 return mono_metadata_signature_equal (left
, right
) && left
->call_convention
== right
->call_convention
;
2577 case MONO_TYPE_GENERICINST
: {
2578 MonoClass
*target_klass
;
2579 MonoClass
*candidate_klass
;
2580 if (mono_type_is_enum_type (target
)) {
2581 target
= mono_type_get_underlying_type_any (target
);
2584 target_klass
= mono_class_from_mono_type (target
);
2585 candidate_klass
= mono_class_from_mono_type (candidate
);
2586 if (mono_class_is_nullable (target_klass
)) {
2587 if (!mono_class_is_nullable (candidate_klass
))
2589 return target_klass
== candidate_klass
;
2592 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2595 case MONO_TYPE_STRING
:
2596 return candidate
->type
== MONO_TYPE_STRING
;
2598 case MONO_TYPE_CLASS
:
2600 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2601 * to take boxing status into account.
2603 if (mono_type_is_generic_argument (original_candidate
))
2605 /* If candidate is an enum it should return true for System.Enum and supertypes.
2606 * That's why here we use the original type and not the underlying type.
2608 return mono_class_is_assignable_from (target
->data
.klass
, mono_class_from_mono_type (original_candidate
));
2610 case MONO_TYPE_OBJECT
:
2611 return MONO_TYPE_IS_REFERENCE (candidate
);
2613 case MONO_TYPE_SZARRAY
: {
2616 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2619 left
= mono_class_from_mono_type (target
)->element_class
;
2620 right
= mono_class_from_mono_type (candidate
)->element_class
;
2621 return mono_class_is_assignable_from (left
, right
);
2624 case MONO_TYPE_ARRAY
:
2625 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2627 return is_array_type_compatible (target
, candidate
);
2629 case MONO_TYPE_TYPEDBYREF
:
2630 return candidate
->type
== MONO_TYPE_TYPEDBYREF
;
2632 case MONO_TYPE_VALUETYPE
: {
2633 MonoClass
*target_klass
= mono_class_from_mono_type (target
);
2634 MonoClass
*candidate_klass
= mono_class_from_mono_type (candidate
);
2636 if (target_klass
== candidate_klass
)
2638 if (mono_type_is_enum_type (target
)) {
2639 target
= mono_type_get_underlying_type_any (target
);
2646 if (candidate
->type
!= MONO_TYPE_VAR
)
2648 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2650 case MONO_TYPE_MVAR
:
2651 if (candidate
->type
!= MONO_TYPE_MVAR
)
2653 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2656 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target
->type
); );
2657 g_assert_not_reached ();
2666 verify_type_compatibility (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
)
2668 return verify_type_compatibility_full (ctx
, target
, candidate
, FALSE
);
2672 * Returns the generic param bound to the context been verified.
2675 static MonoGenericParam
*
2676 get_generic_param (VerifyContext
*ctx
, MonoType
*param
)
2678 guint16 param_num
= mono_type_get_generic_param_num (param
);
2679 if (param
->type
== MONO_TYPE_VAR
) {
2680 if (!ctx
->generic_context
->class_inst
|| ctx
->generic_context
->class_inst
->type_argc
<= param_num
) {
2681 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic type argument %d", param_num
));
2684 return ctx
->generic_context
->class_inst
->type_argv
[param_num
]->data
.generic_param
;
2687 /*param must be a MVAR */
2688 if (!ctx
->generic_context
->method_inst
|| ctx
->generic_context
->method_inst
->type_argc
<= param_num
) {
2689 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic method argument %d", param_num
));
2692 return ctx
->generic_context
->method_inst
->type_argv
[param_num
]->data
.generic_param
;
2696 * is_compatible_boxed_valuetype:
2698 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2700 * @type The source type. It it tested to be of the proper type.
2701 * @candidate type of the boxed valuetype.
2702 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2703 * @strict if TRUE candidate must be boxed compatible to the target type
2707 is_compatible_boxed_valuetype (VerifyContext
*ctx
, MonoType
*type
, MonoType
*candidate
, ILStackDesc
*stack
, gboolean strict
)
2709 if (!stack_slot_is_boxed_value (stack
))
2711 if (type
->byref
|| candidate
->byref
)
2714 if (mono_type_is_generic_argument (candidate
)) {
2715 MonoGenericParam
*param
= get_generic_param (ctx
, candidate
);
2717 for (class = mono_generic_param_info (param
)->constraints
; class && *class; ++class) {
2718 if (verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (& (*class)->byval_arg
), FALSE
))
2723 if (mono_type_is_generic_argument (type
))
2729 return MONO_TYPE_IS_REFERENCE (type
) && mono_class_is_assignable_from (mono_class_from_mono_type (type
), mono_class_from_mono_type (candidate
));
2733 verify_stack_type_compatibility_full (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
, gboolean drop_byref
, gboolean valuetype_must_be_boxed
)
2735 MonoType
*candidate
= mono_type_from_stack_slot (stack
);
2736 if (MONO_TYPE_IS_REFERENCE (type
) && !type
->byref
&& stack_slot_is_null_literal (stack
))
2739 if (is_compatible_boxed_valuetype (ctx
, type
, candidate
, stack
, TRUE
))
2742 if (valuetype_must_be_boxed
&& !stack_slot_is_boxed_value (stack
) && !MONO_TYPE_IS_REFERENCE (candidate
))
2745 if (!valuetype_must_be_boxed
&& stack_slot_is_boxed_value (stack
))
2749 return verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (candidate
), FALSE
);
2751 return verify_type_compatibility_full (ctx
, type
, candidate
, FALSE
);
2755 verify_stack_type_compatibility (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
)
2757 return verify_stack_type_compatibility_full (ctx
, type
, stack
, FALSE
, FALSE
);
2761 mono_delegate_type_equal (MonoType
*target
, MonoType
*candidate
)
2763 if (candidate
->byref
^ target
->byref
)
2766 switch (target
->type
) {
2767 case MONO_TYPE_VOID
:
2770 case MONO_TYPE_BOOLEAN
:
2773 case MONO_TYPE_CHAR
:
2782 case MONO_TYPE_STRING
:
2783 case MONO_TYPE_TYPEDBYREF
:
2784 return candidate
->type
== target
->type
;
2787 return mono_delegate_type_equal (target
->data
.type
, candidate
->data
.type
);
2789 case MONO_TYPE_FNPTR
:
2790 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2792 return mono_delegate_signature_equal (mono_type_get_signature (target
), mono_type_get_signature (candidate
), FALSE
);
2794 case MONO_TYPE_GENERICINST
: {
2795 MonoClass
*target_klass
;
2796 MonoClass
*candidate_klass
;
2797 target_klass
= mono_class_from_mono_type (target
);
2798 candidate_klass
= mono_class_from_mono_type (candidate
);
2799 /*FIXME handle nullables and enum*/
2800 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2802 case MONO_TYPE_OBJECT
:
2803 return MONO_TYPE_IS_REFERENCE (candidate
);
2805 case MONO_TYPE_CLASS
:
2806 return mono_class_is_assignable_from(target
->data
.klass
, mono_class_from_mono_type (candidate
));
2808 case MONO_TYPE_SZARRAY
:
2809 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2811 return mono_class_is_assignable_from (mono_class_from_mono_type (target
)->element_class
, mono_class_from_mono_type (candidate
)->element_class
);
2813 case MONO_TYPE_ARRAY
:
2814 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2816 return is_array_type_compatible (target
, candidate
);
2818 case MONO_TYPE_VALUETYPE
:
2819 /*FIXME handle nullables and enum*/
2820 return mono_class_from_mono_type (candidate
) == mono_class_from_mono_type (target
);
2823 return candidate
->type
== MONO_TYPE_VAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2826 case MONO_TYPE_MVAR
:
2827 return candidate
->type
== MONO_TYPE_MVAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2831 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target
->type
); );
2832 g_assert_not_reached ();
2838 mono_delegate_param_equal (MonoType
*delegate
, MonoType
*method
)
2840 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2843 return mono_delegate_type_equal (method
, delegate
);
2847 mono_delegate_ret_equal (MonoType
*delegate
, MonoType
*method
)
2849 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2852 return mono_delegate_type_equal (delegate
, method
);
2856 * mono_delegate_signature_equal:
2858 * Compare two signatures in the way expected by delegates.
2860 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2862 * FIXME can this function be eliminated and proper metadata functionality be used?
2865 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
)
2868 int method_offset
= is_static_ldftn
? 1 : 0;
2870 if (delegate_sig
->param_count
+ method_offset
!= method_sig
->param_count
)
2873 if (delegate_sig
->call_convention
!= method_sig
->call_convention
)
2876 for (i
= 0; i
< delegate_sig
->param_count
; i
++) {
2877 MonoType
*p1
= delegate_sig
->params
[i
];
2878 MonoType
*p2
= method_sig
->params
[i
+ method_offset
];
2880 if (!mono_delegate_param_equal (p1
, p2
))
2884 if (!mono_delegate_ret_equal (delegate_sig
->ret
, method_sig
->ret
))
2891 * verify_ldftn_delegate:
2893 * Verify properties of ldftn based delegates.
2896 verify_ldftn_delegate (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2898 MonoMethod
*method
= funptr
->method
;
2900 /*ldftn non-final virtuals only allowed if method is not static,
2901 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2902 * This rules doesn't apply if the object on stack is a boxed valuetype.
2904 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !(method
->klass
->flags
& TYPE_ATTRIBUTE_SEALED
) && !stack_slot_is_boxed_value (value
)) {
2905 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2906 if (IS_FAIL_FAST_MODE (ctx
) && ctx
->has_this_store
)
2907 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx
->ip_offset
));
2909 /*current method must not be static*/
2910 if (ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)
2911 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx
->ip_offset
));
2913 /*value is the this pointer, loaded using ldarg.0 */
2914 if (!stack_slot_is_this_pointer (value
))
2915 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
));
2917 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
;
2922 * verify_delegate_compatibility:
2924 * Verify delegate creation sequence.
2928 verify_delegate_compatibility (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2930 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2931 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2933 MonoMethod
*invoke
, *method
;
2934 const guint8
*ip
= ctx
->header
->code
;
2935 guint32 ip_offset
= ctx
->ip_offset
;
2936 gboolean is_static_ldftn
= FALSE
, is_first_arg_bound
= FALSE
;
2938 if (stack_slot_get_type (funptr
) != TYPE_PTR
|| !funptr
->method
) {
2939 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx
->ip_offset
));
2943 invoke
= mono_get_delegate_invoke (delegate
);
2944 method
= funptr
->method
;
2946 is_static_ldftn
= (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) && method
->flags
& METHOD_ATTRIBUTE_STATIC
;
2948 if (is_static_ldftn
)
2949 is_first_arg_bound
= mono_method_signature (invoke
)->param_count
+ 1 == mono_method_signature (method
)->param_count
;
2951 if (!mono_delegate_signature_equal (mono_method_signature (invoke
), mono_method_signature (method
), is_first_arg_bound
)) {
2952 char *fun_sig
= mono_signature_get_desc (mono_method_signature (method
), FALSE
);
2953 char *invoke_sig
= mono_signature_get_desc (mono_method_signature (invoke
), FALSE
);
2954 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Function pointer signature '%s' doesn't match delegate's signature '%s' at 0x%04x", fun_sig
, invoke_sig
, ctx
->ip_offset
));
2956 g_free (invoke_sig
);
2960 * Delegate code sequences:
2966 * [-6] ldvirtftn token
2970 if (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) {
2971 verify_ldftn_delegate (ctx
, delegate
, value
, funptr
);
2972 } else if (ip_offset
> 6 && IS_VALID_OPCODE (7, CEE_DUP
) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN
)) {
2973 ctx
->code
[ip_offset
- 6].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2975 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx
->ip_offset
));
2977 ctx
->code
[ip_offset
].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2980 if (is_first_arg_bound
) {
2981 if (mono_method_signature (method
)->param_count
== 0 || !verify_stack_type_compatibility_full (ctx
, mono_method_signature (method
)->params
[0], value
, FALSE
, TRUE
))
2982 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2984 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
2985 if (!stack_slot_is_null_literal (value
) && !is_first_arg_bound
)
2986 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx
->ip_offset
));
2988 if (!verify_stack_type_compatibility_full (ctx
, &method
->klass
->byval_arg
, value
, FALSE
, TRUE
) && !stack_slot_is_null_literal (value
))
2989 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2993 if (stack_slot_get_type (value
) != TYPE_COMPLEX
)
2994 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx
->ip_offset
));
2996 #undef IS_VALID_OPCODE
2997 #undef IS_LOAD_FUN_PTR
3000 /* implement the opcode checks*/
3002 push_arg (VerifyContext
*ctx
, unsigned int arg
, int take_addr
)
3006 if (arg
>= ctx
->max_args
) {
3008 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
3010 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
3011 if (check_overflow (ctx
)) //FIXME: what sane value could we ever push?
3012 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3014 } else if (check_overflow (ctx
)) {
3015 /*We must let the value be pushed, otherwise we would get an underflow error*/
3016 check_unverifiable_type (ctx
, ctx
->params
[arg
]);
3017 if (ctx
->params
[arg
]->byref
&& take_addr
)
3018 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
3019 top
= stack_push (ctx
);
3020 if (!set_stack_value (ctx
, top
, ctx
->params
[arg
], take_addr
))
3023 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3025 ctx
->has_this_store
= TRUE
;
3027 top
->stype
|= THIS_POINTER_MASK
;
3028 if (mono_method_is_constructor (ctx
->method
) && !ctx
->super_ctor_called
&& !ctx
->method
->klass
->valuetype
)
3029 top
->stype
|= UNINIT_THIS_MASK
;
3035 push_local (VerifyContext
*ctx
, guint32 arg
, int take_addr
)
3037 if (arg
>= ctx
->num_locals
) {
3038 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local %d", arg
+ 1));
3039 } else if (check_overflow (ctx
)) {
3040 /*We must let the value be pushed, otherwise we would get an underflow error*/
3041 check_unverifiable_type (ctx
, ctx
->locals
[arg
]);
3042 if (ctx
->locals
[arg
]->byref
&& take_addr
)
3043 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
3045 set_stack_value (ctx
, stack_push (ctx
), ctx
->locals
[arg
], take_addr
);
3050 store_arg (VerifyContext
*ctx
, guint32 arg
)
3054 if (arg
>= ctx
->max_args
) {
3055 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
3056 if (check_underflow (ctx
, 1))
3061 if (check_underflow (ctx
, 1)) {
3062 value
= stack_pop (ctx
);
3063 if (!verify_stack_type_compatibility (ctx
, ctx
->params
[arg
], value
)) {
3064 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3067 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
))
3068 ctx
->has_this_store
= 1;
3072 store_local (VerifyContext
*ctx
, guint32 arg
)
3075 if (arg
>= ctx
->num_locals
) {
3076 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
3080 /*TODO verify definite assigment */
3081 if (check_underflow (ctx
, 1)) {
3082 value
= stack_pop(ctx
);
3083 if (!verify_stack_type_compatibility (ctx
, ctx
->locals
[arg
], value
)) {
3084 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
3085 stack_slot_get_name (value
),
3086 mono_type_get_stack_name (ctx
->locals
[arg
]),
3092 /*FIXME add and sub needs special care here*/
3094 do_binop (VerifyContext
*ctx
, unsigned int opcode
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
3096 ILStackDesc
*a
, *b
, *top
;
3097 int idxa
, idxb
, complexMerge
= 0;
3100 if (!check_underflow (ctx
, 2))
3102 b
= stack_pop (ctx
);
3103 a
= stack_pop (ctx
);
3105 idxa
= stack_slot_get_underlying_type (a
);
3106 if (stack_slot_is_managed_pointer (a
)) {
3111 idxb
= stack_slot_get_underlying_type (b
);
3112 if (stack_slot_is_managed_pointer (b
)) {
3119 res
= table
[idxa
][idxb
];
3121 VERIFIER_DEBUG ( printf ("binop res %d\n", res
); );
3122 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
3124 top
= stack_push (ctx
);
3125 if (res
== TYPE_INV
) {
3126 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
)));
3127 copy_stack_value (top
, a
);
3131 if (res
& NON_VERIFIABLE_RESULT
) {
3132 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
)));
3134 res
= res
& ~NON_VERIFIABLE_RESULT
;
3137 if (complexMerge
&& res
== TYPE_PTR
) {
3138 if (complexMerge
== 1)
3139 copy_stack_value (top
, a
);
3140 else if (complexMerge
== 2)
3141 copy_stack_value (top
, b
);
3143 * There is no need to merge the type of two pointers.
3144 * The only valid operation is subtraction, that returns a native
3145 * int as result and can be used with any 2 pointer kinds.
3146 * This is valid acording to Patition III 1.1.4
3155 do_boolean_branch_op (VerifyContext
*ctx
, int delta
)
3157 int target
= ctx
->ip_offset
+ delta
;
3160 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
3162 if (target
< 0 || target
>= ctx
->code_size
) {
3163 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx
->ip_offset
));
3167 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3169 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3172 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3176 ctx
->target
= target
;
3178 if (!check_underflow (ctx
, 1))
3181 top
= stack_pop (ctx
);
3182 if (!is_valid_bool_arg (top
))
3183 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
));
3185 check_unmanaged_pointer (ctx
, top
);
3189 stack_slot_is_complex_type_not_reference_type (ILStackDesc
*slot
)
3191 return stack_slot_get_type (slot
) == TYPE_COMPLEX
&& !MONO_TYPE_IS_REFERENCE (slot
->type
) && !stack_slot_is_boxed_value (slot
);
3195 do_branch_op (VerifyContext
*ctx
, signed int delta
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
3200 int target
= ctx
->ip_offset
+ delta
;
3202 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
3204 if (target
< 0 || target
>= ctx
->code_size
) {
3205 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
3209 switch (is_valid_cmp_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3210 case 1: /*FIXME use constants and not magic numbers.*/
3211 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3214 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3218 ctx
->target
= target
;
3220 if (!check_underflow (ctx
, 2))
3223 b
= stack_pop (ctx
);
3224 a
= stack_pop (ctx
);
3226 idxa
= stack_slot_get_underlying_type (a
);
3227 if (stack_slot_is_managed_pointer (a
))
3230 idxb
= stack_slot_get_underlying_type (b
);
3231 if (stack_slot_is_managed_pointer (b
))
3234 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3239 res
= table
[idxa
][idxb
];
3242 VERIFIER_DEBUG ( printf ("branch res %d\n", res
); );
3243 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
3245 if (res
== TYPE_INV
) {
3246 CODE_NOT_VERIFIABLE (ctx
,
3247 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
));
3248 } else if (res
& NON_VERIFIABLE_RESULT
) {
3249 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
));
3250 res
= res
& ~NON_VERIFIABLE_RESULT
;
3255 do_cmp_op (VerifyContext
*ctx
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
], guint32 opcode
)
3261 if (!check_underflow (ctx
, 2))
3263 b
= stack_pop (ctx
);
3264 a
= stack_pop (ctx
);
3266 if (opcode
== CEE_CGT_UN
) {
3267 if (stack_slot_get_type (a
) == TYPE_COMPLEX
&& stack_slot_get_type (b
) == TYPE_COMPLEX
) {
3268 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3273 idxa
= stack_slot_get_underlying_type (a
);
3274 if (stack_slot_is_managed_pointer (a
))
3277 idxb
= stack_slot_get_underlying_type (b
);
3278 if (stack_slot_is_managed_pointer (b
))
3281 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3286 res
= table
[idxa
][idxb
];
3289 if(res
== TYPE_INV
) {
3290 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
));
3291 } else if (res
& NON_VERIFIABLE_RESULT
) {
3292 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
));
3293 res
= res
& ~NON_VERIFIABLE_RESULT
;
3295 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3299 do_ret (VerifyContext
*ctx
)
3301 MonoType
*ret
= ctx
->signature
->ret
;
3302 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3303 if (ret
->type
!= MONO_TYPE_VOID
) {
3305 if (!check_underflow (ctx
, 1))
3308 top
= stack_pop(ctx
);
3310 if (!verify_stack_type_compatibility (ctx
, ctx
->signature
->ret
, top
)) {
3311 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx
->ip_offset
));
3315 if (ret
->byref
|| ret
->type
== MONO_TYPE_TYPEDBYREF
|| mono_type_is_value_type (ret
, "System", "ArgIterator") || mono_type_is_value_type (ret
, "System", "RuntimeArgumentHandle"))
3316 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx
->ip_offset
));
3319 if (ctx
->eval
.size
> 0) {
3320 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx
->eval
.size
, ctx
->ip_offset
));
3322 if (in_any_block (ctx
->header
, ctx
->ip_offset
))
3323 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx
->ip_offset
));
3327 * 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.
3328 * This is illegal but mono_get_method_full decoded it.
3329 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3332 do_invoke_method (VerifyContext
*ctx
, int method_token
, gboolean
virtual)
3335 MonoMethodSignature
*sig
;
3338 gboolean virt_check_this
= FALSE
;
3339 gboolean constrained
= ctx
->prefix_set
& PREFIX_CONSTRAINED
;
3341 if (!(method
= verifier_load_method (ctx
, method_token
, virtual ? "callvirt" : "call")))
3345 CLEAR_PREFIX (ctx
, PREFIX_CONSTRAINED
);
3347 if (method
->klass
->valuetype
) // && !constrained ???
3348 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx
->ip_offset
));
3350 if ((method
->flags
& METHOD_ATTRIBUTE_STATIC
))
3351 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx
->ip_offset
));
3354 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
3355 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx
->ip_offset
));
3357 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
)) {
3358 virt_check_this
= TRUE
;
3359 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_CALL_NONFINAL_VIRTUAL
;
3363 if (!(sig
= mono_method_get_signature_full (method
, ctx
->image
, method_token
, ctx
->generic_context
)))
3364 sig
= mono_method_get_signature (method
, ctx
->image
, method_token
);
3366 param_count
= sig
->param_count
+ sig
->hasthis
;
3367 if (!check_underflow (ctx
, param_count
))
3370 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3371 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i
); );
3372 value
= stack_pop (ctx
);
3373 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3374 char *stack_name
= stack_slot_full_name (value
);
3375 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3376 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible parameter with function signature: Calling method with signature (%s) but for argument %d there is a (%s) on stack at 0x%04x", sig_name
, i
, stack_name
, ctx
->ip_offset
));
3377 g_free (stack_name
);
3381 if (stack_slot_is_managed_mutability_pointer (value
))
3382 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
));
3384 if ((ctx
->prefix_set
& PREFIX_TAIL
) && stack_slot_is_managed_pointer (value
)) {
3385 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
));
3391 MonoType
*type
= &method
->klass
->byval_arg
;
3394 if (mono_method_is_constructor (method
) && !method
->klass
->valuetype
) {
3395 if (!mono_method_is_constructor (ctx
->method
))
3396 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx
->ip_offset
));
3397 if (method
->klass
!= ctx
->method
->klass
->parent
&& method
->klass
!= ctx
->method
->klass
)
3398 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
));
3400 ctx
->super_ctor_called
= TRUE
;
3401 value
= stack_pop_safe (ctx
);
3402 if ((value
->stype
& THIS_POINTER_MASK
) != THIS_POINTER_MASK
)
3403 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx
->ip_offset
));
3405 value
= stack_pop (ctx
);
3408 copy_stack_value (©
, value
);
3409 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3410 //Other parts of the code suffer from the same issue of
3411 copy
.type
= mono_type_get_type_byval (copy
.type
);
3412 copy
.stype
&= ~POINTER_MASK
;
3414 if (virt_check_this
&& !stack_slot_is_this_pointer (value
) && !(method
->klass
->valuetype
|| stack_slot_is_boxed_value (value
)))
3415 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
));
3417 if (constrained
&& virtual) {
3418 if (!stack_slot_is_managed_pointer (value
))
3419 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx
->ip_offset
));
3420 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value
->type
), ctx
->constrained_type
, TRUE
))
3421 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx
->ip_offset
));
3422 copy
.stype
|= BOXED_MASK
;
3424 if (stack_slot_is_managed_pointer (value
) && !mono_class_from_mono_type (value
->type
)->valuetype
)
3425 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
));
3427 if (!virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !method
->klass
->valuetype
&& !stack_slot_is_boxed_value (value
))
3428 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx
->ip_offset
));
3430 if (virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !stack_slot_is_boxed_value (value
))
3431 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx
->ip_offset
));
3433 if (method
->klass
->valuetype
&& (stack_slot_is_boxed_value (value
) || !stack_slot_is_managed_pointer (value
)))
3434 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
));
3436 if (!verify_stack_type_compatibility (ctx
, type
, ©
))
3437 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx
->ip_offset
));
3439 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, mono_class_from_mono_type (value
->type
))) {
3440 char *name
= mono_method_full_name (method
, TRUE
);
3441 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3445 } else if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3446 char *name
= mono_method_full_name (method
, TRUE
);
3447 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3451 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3452 if (check_overflow (ctx
)) {
3453 value
= stack_push (ctx
);
3454 set_stack_value (ctx
, value
, sig
->ret
, FALSE
);
3455 if ((ctx
->prefix_set
& PREFIX_READONLY
) && method
->klass
->rank
&& !strcmp (method
->name
, "Address")) {
3456 ctx
->prefix_set
&= ~PREFIX_READONLY
;
3457 value
->stype
|= CMMP_MASK
;
3462 if ((ctx
->prefix_set
& PREFIX_TAIL
)) {
3463 if (!mono_delegate_ret_equal (mono_method_signature (ctx
->method
)->ret
, sig
->ret
))
3464 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx
->ip_offset
));
3465 if (ctx
->header
->code
[ctx
->ip_offset
+ 5] != CEE_RET
)
3466 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx
->ip_offset
));
3472 do_push_static_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3474 MonoClassField
*field
;
3477 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3479 if (!(field
= verifier_load_field (ctx
, token
, &klass
, take_addr
? "ldsflda" : "ldsfld")))
3482 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3483 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx
->ip_offset
));
3486 /*taking the address of initonly field only works from the static constructor */
3487 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3488 !(field
->parent
== ctx
->method
->klass
&& (ctx
->method
->flags
& (METHOD_ATTRIBUTE_SPECIAL_NAME
| METHOD_ATTRIBUTE_STATIC
)) && !strcmp (".cctor", ctx
->method
->name
)))
3489 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3491 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3492 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3494 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3498 do_store_static_field (VerifyContext
*ctx
, int token
) {
3499 MonoClassField
*field
;
3502 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3504 if (!check_underflow (ctx
, 1))
3507 value
= stack_pop (ctx
);
3509 if (!(field
= verifier_load_field (ctx
, token
, &klass
, "stsfld")))
3512 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3513 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx
->ip_offset
));
3517 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3518 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx
->ip_offset
));
3522 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3523 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3525 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3526 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
));
3530 check_is_valid_type_for_field_ops (VerifyContext
*ctx
, int token
, ILStackDesc
*obj
, MonoClassField
**ret_field
, const char *opcode
)
3532 MonoClassField
*field
;
3534 gboolean is_pointer
;
3536 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3537 if (!(field
= verifier_load_field (ctx
, token
, &klass
, opcode
)))
3541 //the value on stack is going to be used as a pointer
3542 is_pointer
= stack_slot_get_type (obj
) == TYPE_PTR
|| (stack_slot_get_type (obj
) == TYPE_NATIVE_INT
&& !get_stack_type (&field
->parent
->byval_arg
));
3544 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3545 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx
->ip_offset
));
3548 g_assert (obj
->type
);
3550 /*The value on the stack must be a subclass of the defining type of the field*/
3551 /* we need to check if we can load the field from the stack value*/
3553 if (stack_slot_get_underlying_type (obj
) == TYPE_NATIVE_INT
)
3554 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx
->ip_offset
));
3556 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3557 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3559 if (!field
->parent
->valuetype
&& stack_slot_is_managed_pointer (obj
))
3560 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
));
3562 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3563 if (field
->parent
->valuetype
&& stack_slot_is_boxed_value (obj
))
3564 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
));
3566 if (!stack_slot_is_null_literal (obj
) && !verify_stack_type_compatibility_full (ctx
, &field
->parent
->byval_arg
, obj
, TRUE
, FALSE
))
3567 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx
->ip_offset
));
3569 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, mono_class_from_mono_type (obj
->type
)))
3570 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3573 check_unmanaged_pointer (ctx
, obj
);
3578 do_push_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3581 MonoClassField
*field
;
3584 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3586 if (!check_underflow (ctx
, 1))
3588 obj
= stack_pop_safe (ctx
);
3590 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, take_addr
? "ldflda" : "ldfld"))
3593 if (take_addr
&& field
->parent
->valuetype
&& !stack_slot_is_managed_pointer (obj
))
3594 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx
->ip_offset
));
3596 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3597 !(field
->parent
== ctx
->method
->klass
&& mono_method_is_constructor (ctx
->method
)))
3598 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3600 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3604 do_store_field (VerifyContext
*ctx
, int token
)
3606 ILStackDesc
*value
, *obj
;
3607 MonoClassField
*field
;
3608 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3610 if (!check_underflow (ctx
, 2))
3613 value
= stack_pop (ctx
);
3614 obj
= stack_pop_safe (ctx
);
3616 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, "stfld"))
3619 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3620 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3623 /*TODO proper handle for Nullable<T>*/
3625 do_box_value (VerifyContext
*ctx
, int klass_token
)
3628 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "box");
3634 if (!check_underflow (ctx
, 1))
3637 value
= stack_pop (ctx
);
3638 /*box is a nop for reference types*/
3640 if (stack_slot_get_underlying_type (value
) == TYPE_COMPLEX
&& MONO_TYPE_IS_REFERENCE (value
->type
) && MONO_TYPE_IS_REFERENCE (type
)) {
3641 stack_push_stack_val (ctx
, value
)->stype
|= BOXED_MASK
;
3646 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3647 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx
->ip_offset
));
3649 klass
= mono_class_from_mono_type (type
);
3650 if (mono_class_is_nullable (klass
))
3651 type
= &mono_class_get_nullable_param (klass
)->byval_arg
;
3652 stack_push_val (ctx
, TYPE_COMPLEX
| BOXED_MASK
, type
);
3656 do_unbox_value (VerifyContext
*ctx
, int klass_token
)
3659 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox");
3664 if (!check_underflow (ctx
, 1))
3667 if (!mono_class_from_mono_type (type
)->valuetype
)
3668 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx
->ip_offset
));
3670 value
= stack_pop (ctx
);
3672 /*Value should be: a boxed valuetype or a reference type*/
3673 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3674 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3675 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
));
3677 set_stack_value (ctx
, value
= stack_push (ctx
), mono_type_get_type_byref (type
), FALSE
);
3678 value
->stype
|= CMMP_MASK
;
3682 do_unbox_any (VerifyContext
*ctx
, int klass_token
)
3685 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox.any");
3690 if (!check_underflow (ctx
, 1))
3693 value
= stack_pop (ctx
);
3695 /*Value should be: a boxed valuetype or a reference type*/
3696 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3697 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3698 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
));
3700 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3704 do_unary_math_op (VerifyContext
*ctx
, int op
)
3707 if (!check_underflow (ctx
, 1))
3709 value
= stack_pop (ctx
);
3710 switch (stack_slot_get_type (value
)) {
3713 case TYPE_NATIVE_INT
:
3718 case TYPE_COMPLEX
: /*only enums are ok*/
3719 if (mono_type_is_enum_type (value
->type
))
3722 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx
->ip_offset
));
3724 stack_push_stack_val (ctx
, value
);
3728 do_conversion (VerifyContext
*ctx
, int kind
)
3731 if (!check_underflow (ctx
, 1))
3733 value
= stack_pop (ctx
);
3735 switch (stack_slot_get_type (value
)) {
3738 case TYPE_NATIVE_INT
:
3742 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
));
3747 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3750 stack_push_val (ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
3753 stack_push_val (ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
3755 case TYPE_NATIVE_INT
:
3756 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3759 g_error ("unknown type %02x in conversion", kind
);
3765 do_load_token (VerifyContext
*ctx
, int token
)
3768 MonoClass
*handle_class
;
3769 if (!check_overflow (ctx
))
3771 handle
= mono_ldtoken (ctx
->image
, token
, &handle_class
, ctx
->generic_context
);
3773 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token
, ctx
->ip_offset
));
3776 if (handle_class
== mono_defaults
.typehandle_class
) {
3777 mono_type_is_valid_in_context (ctx
, (MonoType
*)handle
);
3778 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
3779 mono_method_is_valid_in_context (ctx
, (MonoMethod
*)handle
);
3780 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
3781 mono_type_is_valid_in_context (ctx
, &((MonoClassField
*)handle
)->parent
->byval_arg
);
3783 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
3785 stack_push_val (ctx
, TYPE_COMPLEX
, mono_class_get_type (handle_class
));
3789 do_ldobj_value (VerifyContext
*ctx
, int token
)
3792 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "ldobj");
3793 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3798 if (!check_underflow (ctx
, 1))
3801 value
= stack_pop (ctx
);
3802 if (!stack_slot_is_managed_pointer (value
)
3803 && stack_slot_get_type (value
) != TYPE_NATIVE_INT
3804 && !(stack_slot_get_type (value
) == TYPE_PTR
&& value
->type
->type
!= MONO_TYPE_FNPTR
)) {
3805 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3809 if (stack_slot_get_type (value
) == TYPE_NATIVE_INT
)
3810 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx
->ip_offset
));
3812 /*We have a byval on the stack, but the comparison must be strict. */
3813 if (!verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (value
->type
), TRUE
))
3814 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx
->ip_offset
));
3816 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3820 do_stobj (VerifyContext
*ctx
, int token
)
3822 ILStackDesc
*dest
, *src
;
3823 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "stobj");
3824 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3829 if (!check_underflow (ctx
, 2))
3832 src
= stack_pop (ctx
);
3833 dest
= stack_pop (ctx
);
3835 if (stack_slot_is_managed_mutability_pointer (dest
))
3836 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx
->ip_offset
));
3838 if (!stack_slot_is_managed_pointer (dest
))
3839 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx
->ip_offset
));
3841 if (stack_slot_is_boxed_value (src
) && !MONO_TYPE_IS_REFERENCE (src
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3842 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
));
3844 if (!verify_stack_type_compatibility (ctx
, type
, src
))
3845 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3847 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3848 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3852 do_cpobj (VerifyContext
*ctx
, int token
)
3854 ILStackDesc
*dest
, *src
;
3855 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "cpobj");
3859 if (!check_underflow (ctx
, 2))
3862 src
= stack_pop (ctx
);
3863 dest
= stack_pop (ctx
);
3865 if (!stack_slot_is_managed_pointer (src
))
3866 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx
->ip_offset
));
3868 if (!stack_slot_is_managed_pointer (dest
))
3869 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx
->ip_offset
));
3871 if (stack_slot_is_managed_mutability_pointer (dest
))
3872 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx
->ip_offset
));
3874 if (!verify_type_compatibility (ctx
, type
, mono_type_get_type_byval (src
->type
)))
3875 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3877 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3878 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3882 do_initobj (VerifyContext
*ctx
, int token
)
3885 MonoType
*stack
, *type
= get_boxable_mono_type (ctx
, token
, "initobj");
3889 if (!check_underflow (ctx
, 1))
3892 obj
= stack_pop (ctx
);
3894 if (!stack_slot_is_managed_pointer (obj
))
3895 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx
->ip_offset
));
3897 if (stack_slot_is_managed_mutability_pointer (obj
))
3898 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx
->ip_offset
));
3900 stack
= mono_type_get_type_byval (obj
->type
);
3901 if (MONO_TYPE_IS_REFERENCE (stack
)) {
3902 if (!verify_type_compatibility (ctx
, stack
, type
))
3903 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3904 else if (IS_STRICT_MODE (ctx
) && !mono_metadata_type_equal (type
, stack
))
3905 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3906 } else if (!verify_type_compatibility (ctx
, stack
, type
)) {
3907 char *expected_name
= mono_type_full_name (type
);
3908 char *stack_name
= mono_type_full_name (stack
);
3910 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Initobj %s not compatible with value on stack %s at 0x%04x", expected_name
, stack_name
, ctx
->ip_offset
));
3911 g_free (expected_name
);
3912 g_free (stack_name
);
3917 do_newobj (VerifyContext
*ctx
, int token
)
3921 MonoMethodSignature
*sig
;
3923 gboolean is_delegate
= FALSE
;
3925 if (!(method
= verifier_load_method (ctx
, token
, "newobj")))
3928 if (!mono_method_is_constructor (method
)) {
3929 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token
, ctx
->ip_offset
));
3933 if (method
->klass
->flags
& (TYPE_ATTRIBUTE_ABSTRACT
| TYPE_ATTRIBUTE_INTERFACE
))
3934 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx
->ip_offset
));
3936 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3937 char *from
= mono_method_full_name (ctx
->method
, TRUE
);
3938 char *to
= mono_method_full_name (method
, TRUE
);
3939 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Constructor %s not visible from %s at 0x%04x", to
, from
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3944 //FIXME use mono_method_get_signature_full
3945 sig
= mono_method_signature (method
);
3946 if (!check_underflow (ctx
, sig
->param_count
))
3949 is_delegate
= method
->klass
->parent
== mono_defaults
.multicastdelegate_class
;
3952 ILStackDesc
*funptr
;
3953 //first arg is object, second arg is fun ptr
3954 if (sig
->param_count
!= 2) {
3955 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx
->ip_offset
));
3958 funptr
= stack_pop (ctx
);
3959 value
= stack_pop (ctx
);
3960 verify_delegate_compatibility (ctx
, method
->klass
, value
, funptr
);
3962 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3963 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i
); );
3964 value
= stack_pop (ctx
);
3965 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3966 char *stack_name
= stack_slot_full_name (value
);
3967 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3968 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible parameter value with constructor signature: %s X %s at 0x%04x", sig_name
, stack_name
, ctx
->ip_offset
));
3969 g_free (stack_name
);
3973 if (stack_slot_is_managed_mutability_pointer (value
))
3974 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx
->ip_offset
));
3978 if (check_overflow (ctx
))
3979 set_stack_value (ctx
, stack_push (ctx
), &method
->klass
->byval_arg
, FALSE
);
3983 do_cast (VerifyContext
*ctx
, int token
, const char *opcode
) {
3989 if (!check_underflow (ctx
, 1))
3992 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
3996 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid %s type at 0x%04x", opcode
, ctx
->ip_offset
));
4000 value
= stack_pop (ctx
);
4001 is_boxed
= stack_slot_is_boxed_value (value
);
4003 if (stack_slot_is_managed_pointer (value
))
4004 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
4005 else if (!MONO_TYPE_IS_REFERENCE (value
->type
) && !is_boxed
) {
4006 char *name
= stack_slot_full_name (value
);
4007 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Expected a reference type on stack for %s but found %s at 0x%04x", opcode
, name
, ctx
->ip_offset
));
4011 switch (value
->type
->type
) {
4012 case MONO_TYPE_FNPTR
:
4014 case MONO_TYPE_TYPEDBYREF
:
4015 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
4018 do_box
= is_boxed
|| mono_type_is_generic_argument(type
) || mono_class_from_mono_type (type
)->valuetype
;
4019 stack_push_val (ctx
, TYPE_COMPLEX
| (do_box
? BOXED_MASK
: 0), type
);
4023 mono_type_from_opcode (int opcode
) {
4031 return &mono_defaults
.sbyte_class
->byval_arg
;
4039 return &mono_defaults
.int16_class
->byval_arg
;
4047 return &mono_defaults
.int32_class
->byval_arg
;
4053 return &mono_defaults
.int64_class
->byval_arg
;
4059 return &mono_defaults
.single_class
->byval_arg
;
4065 return &mono_defaults
.double_class
->byval_arg
;
4071 return &mono_defaults
.int_class
->byval_arg
;
4075 case CEE_LDELEM_REF
:
4076 case CEE_STELEM_REF
:
4077 return &mono_defaults
.object_class
->byval_arg
;
4080 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode
);
4086 do_load_indirect (VerifyContext
*ctx
, int opcode
)
4089 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
4091 if (!check_underflow (ctx
, 1))
4094 value
= stack_pop (ctx
);
4095 if (!stack_slot_is_managed_pointer (value
)) {
4096 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx
->ip_offset
));
4097 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
4101 if (opcode
== CEE_LDIND_REF
) {
4102 if (stack_slot_get_underlying_type (value
) != TYPE_COMPLEX
|| mono_class_from_mono_type (value
->type
)->valuetype
)
4103 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx
->ip_offset
));
4104 set_stack_value (ctx
, stack_push (ctx
), mono_type_get_type_byval (value
->type
), FALSE
);
4106 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (value
->type
), TRUE
))
4107 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4108 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
4113 do_store_indirect (VerifyContext
*ctx
, int opcode
)
4115 ILStackDesc
*addr
, *val
;
4116 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
4118 if (!check_underflow (ctx
, 2))
4121 val
= stack_pop (ctx
);
4122 addr
= stack_pop (ctx
);
4124 check_unmanaged_pointer (ctx
, addr
);
4126 if (!stack_slot_is_managed_pointer (addr
) && stack_slot_get_type (addr
) != TYPE_PTR
) {
4127 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx
->ip_offset
));
4131 if (stack_slot_is_managed_mutability_pointer (addr
)) {
4132 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx
->ip_offset
));
4136 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (addr
->type
), TRUE
))
4137 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4139 if (!verify_stack_type_compatibility (ctx
, mono_type_from_opcode (opcode
), val
))
4140 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4144 do_newarr (VerifyContext
*ctx
, int token
)
4147 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "newarr");
4152 if (!check_underflow (ctx
, 1))
4155 value
= stack_pop (ctx
);
4156 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4157 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
));
4159 set_stack_value (ctx
, stack_push (ctx
), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type
), 1)), FALSE
);
4162 /*FIXME handle arrays that are not 0-indexed*/
4164 do_ldlen (VerifyContext
*ctx
)
4168 if (!check_underflow (ctx
, 1))
4171 value
= stack_pop (ctx
);
4173 if (stack_slot_get_type (value
) != TYPE_COMPLEX
|| value
->type
->type
!= MONO_TYPE_SZARRAY
)
4174 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx
->ip_offset
));
4176 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
4179 /*FIXME handle arrays that are not 0-indexed*/
4180 /*FIXME handle readonly prefix and CMMP*/
4182 do_ldelema (VerifyContext
*ctx
, int klass_token
)
4184 ILStackDesc
*index
, *array
, *res
;
4185 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "ldelema");
4191 if (!check_underflow (ctx
, 2))
4194 index
= stack_pop (ctx
);
4195 array
= stack_pop (ctx
);
4197 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4198 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
));
4200 if (!stack_slot_is_null_literal (array
)) {
4201 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4202 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array
), ctx
->ip_offset
));
4204 if (get_stack_type (type
) == TYPE_I4
|| get_stack_type (type
) == TYPE_NATIVE_INT
) {
4205 valid
= verify_type_compatibility_full (ctx
, type
, &array
->type
->data
.klass
->byval_arg
, TRUE
);
4207 valid
= mono_metadata_type_equal (type
, &array
->type
->data
.klass
->byval_arg
);
4210 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx
->ip_offset
));
4214 res
= stack_push (ctx
);
4215 set_stack_value (ctx
, res
, type
, TRUE
);
4216 if (ctx
->prefix_set
& PREFIX_READONLY
) {
4217 ctx
->prefix_set
&= ~PREFIX_READONLY
;
4218 res
->stype
|= CMMP_MASK
;
4223 * FIXME handle arrays that are not 0-indexed
4224 * FIXME handle readonly prefix and CMMP
4227 do_ldelem (VerifyContext
*ctx
, int opcode
, int token
)
4229 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4230 ILStackDesc
*index
, *array
;
4232 if (!check_underflow (ctx
, 2))
4235 if (opcode
== CEE_LDELEM
) {
4236 if (!(type
= verifier_load_type (ctx
, token
, "ldelem.any"))) {
4237 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4241 type
= mono_type_from_opcode (opcode
);
4244 index
= stack_pop (ctx
);
4245 array
= stack_pop (ctx
);
4247 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4248 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
));
4250 if (!stack_slot_is_null_literal (array
)) {
4251 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4252 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
));
4254 if (opcode
== CEE_LDELEM_REF
) {
4255 if (array
->type
->data
.klass
->valuetype
)
4256 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx
->ip_offset
));
4257 type
= &array
->type
->data
.klass
->byval_arg
;
4259 MonoType
*candidate
= &array
->type
->data
.klass
->byval_arg
;
4260 if (IS_STRICT_MODE (ctx
)) {
4261 MonoType
*underlying_type
= mono_type_get_underlying_type_any (type
);
4262 MonoType
*underlying_candidate
= mono_type_get_underlying_type_any (candidate
);
4263 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
)) ||
4264 (IS_ONE_OF2 (underlying_candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
) && IS_ONE_OF2 (underlying_type
->type
, MONO_TYPE_I
, MONO_TYPE_U
)))
4265 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4267 if (!verify_type_compatibility_full (ctx
, type
, candidate
, TRUE
))
4268 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4273 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
4278 * FIXME handle arrays that are not 0-indexed
4281 do_stelem (VerifyContext
*ctx
, int opcode
, int token
)
4283 ILStackDesc
*index
, *array
, *value
;
4285 if (!check_underflow (ctx
, 3))
4288 if (opcode
== CEE_STELEM
) {
4289 if (!(type
= verifier_load_type (ctx
, token
, "stelem.any"))) {
4290 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4294 type
= mono_type_from_opcode (opcode
);
4297 value
= stack_pop (ctx
);
4298 index
= stack_pop (ctx
);
4299 array
= stack_pop (ctx
);
4301 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4302 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
));
4304 if (!stack_slot_is_null_literal (array
)) {
4305 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
) {
4306 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
));
4308 if (opcode
== CEE_STELEM_REF
) {
4309 if (array
->type
->data
.klass
->valuetype
)
4310 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4311 } else if (!verify_type_compatibility_full (ctx
, &array
->type
->data
.klass
->byval_arg
, type
, TRUE
)) {
4312 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4316 if (opcode
== CEE_STELEM_REF
) {
4317 if (!stack_slot_is_boxed_value (value
) && mono_class_from_mono_type (value
->type
)->valuetype
)
4318 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4319 } else if (opcode
!= CEE_STELEM_REF
) {
4320 if (!verify_stack_type_compatibility (ctx
, type
, value
))
4321 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4323 if (stack_slot_is_boxed_value (value
) && !MONO_TYPE_IS_REFERENCE (value
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
4324 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
));
4330 do_throw (VerifyContext
*ctx
)
4332 ILStackDesc
*exception
;
4333 if (!check_underflow (ctx
, 1))
4335 exception
= stack_pop (ctx
);
4337 if (!stack_slot_is_null_literal (exception
) && !(stack_slot_get_type (exception
) == TYPE_COMPLEX
&& !mono_class_from_mono_type (exception
->type
)->valuetype
))
4338 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx
->ip_offset
));
4340 if (mono_type_is_generic_argument (exception
->type
) && !stack_slot_is_boxed_value (exception
)) {
4341 char *name
= mono_type_full_name (exception
->type
);
4342 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type on stack for throw, expected reference type but found unboxed %s at 0x%04x ", name
, ctx
->ip_offset
));
4345 /*The stack is left empty after a throw*/
4351 do_endfilter (VerifyContext
*ctx
)
4353 MonoExceptionClause
*clause
;
4355 if (IS_STRICT_MODE (ctx
)) {
4356 if (ctx
->eval
.size
!= 1)
4357 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx
->ip_offset
));
4359 if (ctx
->eval
.size
>= 1 && stack_slot_get_type (stack_pop (ctx
)) != TYPE_I4
)
4360 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx
->ip_offset
));
4363 if ((clause
= is_correct_endfilter (ctx
, ctx
->ip_offset
))) {
4364 if (IS_STRICT_MODE (ctx
)) {
4365 if (ctx
->ip_offset
!= clause
->handler_offset
- 2)
4366 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4368 if ((ctx
->ip_offset
!= clause
->handler_offset
- 2) && !MONO_OFFSET_IN_HANDLER (clause
, ctx
->ip_offset
))
4369 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4372 if (IS_STRICT_MODE (ctx
) && !is_unverifiable_endfilter (ctx
, ctx
->ip_offset
))
4373 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4375 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4382 do_leave (VerifyContext
*ctx
, int delta
)
4384 int target
= ((gint32
)ctx
->ip_offset
) + delta
;
4385 if (target
>= ctx
->code_size
|| target
< 0)
4386 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
4388 if (!is_correct_leave (ctx
->header
, ctx
->ip_offset
, target
))
4389 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx
->ip_offset
));
4396 * Verify br and br.s opcodes.
4399 do_static_branch (VerifyContext
*ctx
, int delta
)
4401 int target
= ctx
->ip_offset
+ delta
;
4402 if (target
< 0 || target
>= ctx
->code_size
) {
4403 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("branch target out of code at 0x%04x", ctx
->ip_offset
));
4407 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4409 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4412 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4416 ctx
->target
= target
;
4420 do_switch (VerifyContext
*ctx
, int count
, const unsigned char *data
)
4422 int i
, base
= ctx
->ip_offset
+ 5 + count
* 4;
4425 if (!check_underflow (ctx
, 1))
4428 value
= stack_pop (ctx
);
4430 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4431 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx
->ip_offset
));
4433 for (i
= 0; i
< count
; ++i
) {
4434 int target
= base
+ read32 (data
+ i
* 4);
4436 if (target
< 0 || target
>= ctx
->code_size
) {
4437 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x out of code at 0x%04x", i
, ctx
->ip_offset
));
4441 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4443 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4446 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4449 merge_stacks (ctx
, &ctx
->eval
, &ctx
->code
[target
], FALSE
, TRUE
);
4454 do_load_function_ptr (VerifyContext
*ctx
, guint32 token
, gboolean
virtual)
4459 if (virtual && !check_underflow (ctx
, 1))
4462 if (!virtual && !check_overflow (ctx
))
4465 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4466 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4470 if (!(method
= verifier_load_method (ctx
, token
, virtual ? "ldvirtfrn" : "ldftn")))
4473 if (mono_method_is_constructor (method
))
4474 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx
->ip_offset
));
4477 ILStackDesc
*top
= stack_pop (ctx
);
4479 if (stack_slot_get_type (top
) != TYPE_COMPLEX
|| top
->type
->type
== MONO_TYPE_VALUETYPE
)
4480 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx
->ip_offset
));
4482 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
)
4483 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx
->ip_offset
));
4485 if (!verify_stack_type_compatibility (ctx
, &method
->klass
->byval_arg
, top
))
4486 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx
->ip_offset
));
4489 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
))
4490 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
);
4492 top
= stack_push_val(ctx
, TYPE_PTR
, mono_type_create_fnptr_from_mono_method (ctx
, method
));
4493 top
->method
= method
;
4497 do_sizeof (VerifyContext
*ctx
, int token
)
4501 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4502 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4506 if (!(type
= verifier_load_type (ctx
, token
, "sizeof")))
4509 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
4510 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx
->ip_offset
));
4514 if (type
->type
== MONO_TYPE_VOID
) {
4515 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx
->ip_offset
));
4519 if (check_overflow (ctx
))
4520 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.uint32_class
->byval_arg
, FALSE
);
4523 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4525 do_localloc (VerifyContext
*ctx
)
4529 if (ctx
->eval
.size
!= 1) {
4530 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4534 if (in_any_exception_block (ctx
->header
, ctx
->ip_offset
)) {
4535 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4539 /*TODO verify top type*/
4540 top
= stack_pop (ctx
);
4542 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.int_class
->byval_arg
, FALSE
);
4543 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx
->ip_offset
));
4547 do_ldstr (VerifyContext
*ctx
, guint32 token
)
4549 if (mono_metadata_token_code (token
) != MONO_TOKEN_STRING
) {
4550 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4554 if (!ctx
->image
->dynamic
&& mono_metadata_token_index (token
) >= ctx
->image
->heap_us
.size
) {
4555 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string index %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4559 if (check_overflow (ctx
))
4560 stack_push_val (ctx
, TYPE_COMPLEX
, &mono_defaults
.string_class
->byval_arg
);
4564 do_refanyval (VerifyContext
*ctx
, int token
)
4568 if (!check_underflow (ctx
, 1))
4571 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4574 top
= stack_pop (ctx
);
4576 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4577 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
));
4579 set_stack_value (ctx
, stack_push (ctx
), type
, TRUE
);
4583 do_refanytype (VerifyContext
*ctx
)
4587 if (!check_underflow (ctx
, 1))
4590 top
= stack_pop (ctx
);
4592 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4593 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
));
4595 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typehandle_class
->byval_arg
, FALSE
);
4600 do_mkrefany (VerifyContext
*ctx
, int token
)
4604 if (!check_underflow (ctx
, 1))
4607 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4610 top
= stack_pop (ctx
);
4612 if (stack_slot_is_managed_mutability_pointer (top
))
4613 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx
->ip_offset
));
4615 if (!stack_slot_is_managed_pointer (top
)) {
4616 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
));
4618 MonoType
*stack_type
= mono_type_get_type_byval (top
->type
);
4619 if (MONO_TYPE_IS_REFERENCE (type
) && !mono_metadata_type_equal (type
, stack_type
))
4620 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4622 if (!MONO_TYPE_IS_REFERENCE (type
) && !verify_type_compatibility_full (ctx
, type
, stack_type
, TRUE
))
4623 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4626 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typed_reference_class
->byval_arg
, FALSE
);
4630 do_ckfinite (VerifyContext
*ctx
)
4633 if (!check_underflow (ctx
, 1))
4636 top
= stack_pop (ctx
);
4638 if (stack_slot_get_underlying_type (top
) != TYPE_R8
)
4639 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
));
4640 stack_push_stack_val (ctx
, top
);
4644 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4646 * @from holds new values for a given control path
4647 * @to holds the current values of a given control path
4649 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4652 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
)
4655 stack_init (ctx
, to
);
4658 if (to
->flags
== IL_CODE_FLAG_NOT_PROCESSED
)
4661 stack_copy (&ctx
->eval
, to
);
4663 } else if (!(to
->flags
& IL_CODE_STACK_MERGED
)) {
4664 stack_copy (to
, &ctx
->eval
);
4667 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from
->size
, to
->size
); );
4669 if (from
->size
!= to
->size
) {
4670 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from
->size
, to
->size
, ctx
->ip_offset
); );
4671 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
));
4675 //FIXME we need to preserve CMMP attributes
4676 //FIXME we must take null literals into consideration.
4677 for (i
= 0; i
< from
->size
; ++i
) {
4678 ILStackDesc
*new_slot
= from
->stack
+ i
;
4679 ILStackDesc
*old_slot
= to
->stack
+ i
;
4680 MonoType
*new_type
= mono_type_from_stack_slot (new_slot
);
4681 MonoType
*old_type
= mono_type_from_stack_slot (old_slot
);
4682 MonoClass
*old_class
= mono_class_from_mono_type (old_type
);
4683 MonoClass
*new_class
= mono_class_from_mono_type (new_type
);
4684 MonoClass
*match_class
= NULL
;
4686 // S := T then U = S (new value is compatible with current value, keep current)
4687 if (verify_stack_type_compatibility (ctx
, old_type
, new_slot
)) {
4688 copy_stack_value (new_slot
, old_slot
);
4692 // T := S then U = T (old value is compatible with current value, use new)
4693 if (verify_stack_type_compatibility (ctx
, new_type
, old_slot
)) {
4694 copy_stack_value (old_slot
, new_slot
);
4698 if (mono_type_is_generic_argument (old_type
) || mono_type_is_generic_argument (new_type
)) {
4699 char *old_name
= stack_slot_full_name (old_slot
);
4700 char *new_name
= stack_slot_full_name (new_slot
);
4701 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i
, old_name
, new_name
, ctx
->ip_offset
));
4707 //both are reference types, use closest common super type
4708 if (!mono_class_from_mono_type (old_type
)->valuetype
4709 && !mono_class_from_mono_type (new_type
)->valuetype
4710 && !stack_slot_is_managed_pointer (old_slot
)
4711 && !stack_slot_is_managed_pointer (new_slot
)) {
4713 for (j
= MIN (old_class
->idepth
, new_class
->idepth
) - 1; j
> 0; --j
) {
4714 if (mono_metadata_type_equal (&old_class
->supertypes
[j
]->byval_arg
, &new_class
->supertypes
[j
]->byval_arg
)) {
4715 match_class
= old_class
->supertypes
[j
];
4720 mono_class_setup_interfaces (old_class
);
4721 for (j
= 0; j
< old_class
->interface_count
; ++j
) {
4722 for (k
= 0; k
< new_class
->interface_count
; ++k
) {
4723 if (mono_metadata_type_equal (&old_class
->interfaces
[j
]->byval_arg
, &new_class
->interfaces
[k
]->byval_arg
)) {
4724 match_class
= old_class
->interfaces
[j
];
4730 //No decent super type found, use object
4731 match_class
= mono_defaults
.object_class
;
4733 } 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
)) {
4734 match_class
= mono_defaults
.object_class
;
4739 char *old_name
= stack_slot_full_name (old_slot
);
4740 char *new_name
= stack_slot_full_name (new_slot
);
4741 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i
, old_name
, new_name
, ctx
->ip_offset
));
4745 set_stack_value (ctx
, old_slot
, &new_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4749 g_assert (match_class
);
4750 set_stack_value (ctx
, old_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4751 set_stack_value (ctx
, new_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4757 to
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
4758 to
->flags
|= IL_CODE_STACK_MERGED
;
4761 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4762 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4765 * is_clause_in_range :
4767 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4770 is_clause_in_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4772 if (clause
->try_offset
>= start
&& clause
->try_offset
< end
)
4774 if (HANDLER_START (clause
) >= start
&& HANDLER_START (clause
) < end
)
4780 * is_clause_inside_range :
4782 * Returns TRUE if @clause lies completely inside the @start - @end range.
4785 is_clause_inside_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4787 if (clause
->try_offset
< start
|| (clause
->try_offset
+ clause
->try_len
) > end
)
4789 if (HANDLER_START (clause
) < start
|| (clause
->handler_offset
+ clause
->handler_len
) > end
)
4795 * is_clause_nested :
4797 * Returns TRUE if @nested is nested in @clause.
4800 is_clause_nested (MonoExceptionClause
*clause
, MonoExceptionClause
*nested
)
4802 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (nested
, clause
->data
.filter_offset
, clause
->handler_offset
))
4804 return is_clause_inside_range (nested
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4805 is_clause_inside_range (nested
, clause
->handler_offset
, clause
->handler_offset
+ clause
->handler_len
);
4808 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4809 * the each pair of exception must have the following properties:
4810 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4811 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4812 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4815 verify_clause_relationship (VerifyContext
*ctx
, MonoExceptionClause
*clause
, MonoExceptionClause
*to_test
)
4817 /*clause is nested*/
4818 if (to_test
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (clause
, to_test
->data
.filter_offset
, to_test
->handler_offset
)) {
4819 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clause inside filter"));
4823 /*wrong nesting order.*/
4824 if (is_clause_nested (clause
, to_test
)) {
4825 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4829 /*mutual protection*/
4830 if (clause
->try_offset
== to_test
->try_offset
&& clause
->try_len
== to_test
->try_len
) {
4831 /*handlers are not disjoint*/
4832 if (is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) {
4833 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception handlers overlap"));
4836 /* handlers are not catch or filter */
4837 if (!IS_CATCH_OR_FILTER (clause
) || !IS_CATCH_OR_FILTER (to_test
)) {
4838 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4845 /*not completelly disjoint*/
4846 if ((is_clause_in_range (to_test
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4847 is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) && !is_clause_nested (to_test
, clause
))
4848 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses overlap"));
4851 #define code_bounds_check(size) \
4852 if (ip + size > end) {\
4853 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4858 * FIXME: need to distinguish between valid and verifiable.
4859 * Need to keep track of types on the stack.
4860 * Verify types for opcodes.
4863 mono_method_verify (MonoMethod
*method
, int level
)
4866 const unsigned char *ip
;
4867 const unsigned char *end
;
4868 int i
, n
, need_merge
= 0, start
= 0;
4869 guint token
, ip_offset
= 0, prefix
= 0;
4870 MonoGenericContext
*generic_context
= NULL
;
4874 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method
->klass
->name_space
, method
->klass
->name
, method
->name
); );
4876 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4877 (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))) {
4881 memset (&ctx
, 0, sizeof (VerifyContext
));
4883 //FIXME use mono_method_get_signature_full
4884 ctx
.signature
= mono_method_signature (method
);
4885 if (!ctx
.signature
) {
4886 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method signature"));
4889 ctx
.header
= mono_method_get_header (method
);
4891 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method header"));
4894 ctx
.method
= method
;
4895 ip
= ctx
.header
->code
;
4896 end
= ip
+ ctx
.header
->code_size
;
4897 ctx
.image
= image
= method
->klass
->image
;
4900 ctx
.max_args
= ctx
.signature
->param_count
+ ctx
.signature
->hasthis
;
4901 ctx
.max_stack
= ctx
.header
->max_stack
;
4902 ctx
.verifiable
= ctx
.valid
= 1;
4905 ctx
.code
= g_new (ILCodeDesc
, ctx
.header
->code_size
);
4906 ctx
.code_size
= ctx
.header
->code_size
;
4908 memset(ctx
.code
, 0, sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4911 ctx
.num_locals
= ctx
.header
->num_locals
;
4912 ctx
.locals
= g_memdup (ctx
.header
->locals
, sizeof (MonoType
*) * ctx
.header
->num_locals
);
4914 if (ctx
.num_locals
> 0 && !ctx
.header
->init_locals
)
4915 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Method with locals variable but without init locals set"));
4917 ctx
.params
= g_new (MonoType
*, ctx
.max_args
);
4918 if (ctx
.signature
->hasthis
)
4919 ctx
.params
[0] = method
->klass
->valuetype
? &method
->klass
->this_arg
: &method
->klass
->byval_arg
;
4920 memcpy (ctx
.params
+ ctx
.signature
->hasthis
, ctx
.signature
->params
, sizeof (MonoType
*) * ctx
.signature
->param_count
);
4922 if (ctx
.signature
->is_inflated
)
4923 ctx
.generic_context
= generic_context
= mono_method_get_context (method
);
4925 if (!generic_context
&& (method
->klass
->generic_container
|| method
->is_generic
)) {
4926 if (method
->is_generic
)
4927 ctx
.generic_context
= generic_context
= &(mono_method_get_generic_container (method
)->context
);
4929 ctx
.generic_context
= generic_context
= &method
->klass
->generic_container
->context
;
4932 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4933 ctx
.locals
[i
] = mono_class_inflate_generic_type_checked (ctx
.locals
[i
], ctx
.generic_context
, &error
);
4934 if (!mono_error_ok (&error
)) {
4935 char *name
= mono_type_full_name (ctx
.locals
[i
]);
4936 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %d of type %s", i
, name
));
4938 mono_error_cleanup (&error
);
4942 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4943 ctx
.params
[i
] = mono_class_inflate_generic_type_checked (ctx
.params
[i
], ctx
.generic_context
, &error
);
4944 if (!mono_error_ok (&error
)) {
4945 char *name
= mono_type_full_name (ctx
.locals
[i
]);
4946 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %d of type %s", i
, name
));
4948 mono_error_cleanup (&error
);
4952 stack_init (&ctx
, &ctx
.eval
);
4954 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4955 if (!mono_type_is_valid_in_context (&ctx
, ctx
.locals
[i
]))
4959 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4960 if (!mono_type_is_valid_in_context (&ctx
, ctx
.params
[i
]))
4967 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
4968 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
4969 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
); );
4971 if (clause
->try_offset
> ctx
.code_size
|| clause
->try_offset
+ clause
->try_len
> ctx
.code_size
)
4972 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause out of bounds at 0x%04x", clause
->try_offset
));
4974 if (clause
->try_len
<= 0)
4975 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
4977 if (clause
->handler_offset
> ctx
.code_size
|| clause
->handler_offset
+ clause
->handler_len
> ctx
.code_size
)
4978 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause
->try_offset
));
4980 if (clause
->handler_len
<= 0)
4981 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
4983 if (clause
->try_offset
< clause
->handler_offset
&& clause
->try_offset
+ clause
->try_len
> HANDLER_START (clause
))
4984 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause
->try_offset
, clause
->handler_offset
));
4986 for (n
= i
+ 1; n
< ctx
.header
->num_clauses
&& ctx
.valid
; ++n
)
4987 verify_clause_relationship (&ctx
, clause
, ctx
.header
->clauses
+ n
);
4992 ctx
.code
[clause
->try_offset
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4993 if (clause
->try_offset
+ clause
->try_len
< ctx
.code_size
)
4994 ctx
.code
[clause
->try_offset
+ clause
->try_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4995 if (clause
->handler_offset
+ clause
->handler_len
< ctx
.code_size
)
4996 ctx
.code
[clause
->handler_offset
+ clause
->handler_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4998 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
) {
4999 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, clause
->data
.catch_class
);
5001 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
5002 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->data
.filter_offset
, mono_defaults
.exception_class
);
5003 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, mono_defaults
.exception_class
);
5007 while (ip
< end
&& ctx
.valid
) {
5008 ctx
.ip_offset
= ip_offset
= ip
- ctx
.header
->code
;
5010 /*We need to check against fallthrou in and out of protected blocks.
5011 * For fallout we check the once a protected block ends, if the start flag is not set.
5012 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5013 * TODO convert these checks to be done using flags and not this loop
5015 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
5016 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
5018 if ((clause
->try_offset
+ clause
->try_len
== ip_offset
) && start
== 0) {
5019 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset
));
5023 if ((clause
->handler_offset
+ clause
->handler_len
== ip_offset
) && start
== 0) {
5024 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
5025 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5027 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5031 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& clause
->handler_offset
== ip_offset
&& start
== 0) {
5032 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset
));
5036 if (clause
->handler_offset
== ip_offset
&& start
== 0) {
5037 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset
));
5041 if (clause
->try_offset
== ip_offset
&& ctx
.eval
.size
> 0 && start
== 0) {
5042 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset
));
5051 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx
.target
); );
5052 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ctx
.target
], FALSE
, TRUE
);
5055 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ip_offset
], start
, FALSE
);
5058 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5059 #ifdef MONO_VERIFIER_DEBUG
5062 discode
= mono_disasm_code_one (NULL
, method
, ip
, NULL
);
5063 discode
[strlen (discode
) - 1] = 0; /* no \n */
5064 g_print ("[%d] %-29s (%d)\n", ip_offset
, discode
, ctx
.eval
.size
);
5067 dump_stack_state (&ctx
.code
[ip_offset
]);
5068 dump_stack_state (&ctx
.eval
);
5081 push_arg (&ctx
, *ip
- CEE_LDARG_0
, FALSE
);
5087 code_bounds_check (2);
5088 push_arg (&ctx
, ip
[1], *ip
== CEE_LDARGA_S
);
5092 case CEE_ADD_OVF_UN
:
5093 do_binop (&ctx
, *ip
, add_ovf_un_table
);
5097 case CEE_SUB_OVF_UN
:
5098 do_binop (&ctx
, *ip
, sub_ovf_un_table
);
5105 case CEE_MUL_OVF_UN
:
5106 do_binop (&ctx
, *ip
, bin_ovf_table
);
5111 do_binop (&ctx
, *ip
, add_table
);
5116 do_binop (&ctx
, *ip
, sub_table
);
5123 do_binop (&ctx
, *ip
, bin_op_table
);
5132 do_binop (&ctx
, *ip
, int_bin_op_table
);
5139 do_binop (&ctx
, *ip
, shift_op_table
);
5144 if (!check_underflow (&ctx
, 1))
5146 stack_pop_safe (&ctx
);
5160 /*TODO support definite assignment verification? */
5161 push_local (&ctx
, *ip
- CEE_LDLOC_0
, FALSE
);
5169 store_local (&ctx
, *ip
- CEE_STLOC_0
);
5174 code_bounds_check (2);
5175 store_local (&ctx
, ip
[1]);
5180 code_bounds_check (2);
5181 store_arg (&ctx
, ip
[1]);
5195 if (check_overflow (&ctx
))
5196 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5201 code_bounds_check (2);
5202 if (check_overflow (&ctx
))
5203 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5208 code_bounds_check (5);
5209 if (check_overflow (&ctx
))
5210 stack_push_val (&ctx
,TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5215 code_bounds_check (9);
5216 if (check_overflow (&ctx
))
5217 stack_push_val (&ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
5222 code_bounds_check (5);
5223 if (check_overflow (&ctx
))
5224 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5229 code_bounds_check (9);
5230 if (check_overflow (&ctx
))
5231 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5236 if (check_overflow (&ctx
))
5237 stack_push_val (&ctx
, TYPE_COMPLEX
| NULL_LITERAL_MASK
, &mono_defaults
.object_class
->byval_arg
);
5243 code_bounds_check (2);
5244 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_eq_op
);
5257 code_bounds_check (2);
5258 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_op
);
5265 code_bounds_check (5);
5266 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_eq_op
);
5279 code_bounds_check (5);
5280 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_op
);
5287 code_bounds_check (2);
5288 push_local (&ctx
, ip
[1], *ip
== CEE_LDLOCA_S
);
5292 /* FIXME: warn/error instead? */
5299 if (!check_underflow (&ctx
, 1))
5301 if (!check_overflow (&ctx
))
5303 top
= stack_pop_safe (&ctx
);
5304 copy_stack_value (stack_push (&ctx
), top
);
5305 copy_stack_value (stack_push (&ctx
), top
);
5311 code_bounds_check (5);
5313 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset
));
5314 token
= read32 (ip
+ 1);
5315 if (in_any_block (ctx
.header
, ip_offset
))
5316 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset
));
5318 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx
.ip_offset
));
5320 * FIXME: check signature, retval, arguments etc.
5326 code_bounds_check (5);
5327 do_invoke_method (&ctx
, read32 (ip
+ 1), *ip
== CEE_CALLVIRT
);
5332 code_bounds_check (5);
5333 token
= read32 (ip
+ 1);
5335 * FIXME: check signature, retval, arguments etc.
5336 * FIXME: check requirements for tail call
5338 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx
.ip_offset
));
5342 code_bounds_check (2);
5343 do_static_branch (&ctx
, (signed char)ip
[1] + 2);
5351 code_bounds_check (2);
5352 do_boolean_branch_op (&ctx
, (signed char)ip
[1] + 2);
5358 code_bounds_check (5);
5359 do_static_branch (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5367 code_bounds_check (5);
5368 do_boolean_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5374 code_bounds_check (5);
5375 n
= read32 (ip
+ 1);
5376 code_bounds_check (5 + sizeof (guint32
) * n
);
5378 do_switch (&ctx
, n
, (ip
+ 5));
5380 ip
+= 5 + sizeof (guint32
) * n
;
5394 do_load_indirect (&ctx
, *ip
);
5406 do_store_indirect (&ctx
, *ip
);
5412 do_unary_math_op (&ctx
, *ip
);
5422 do_conversion (&ctx
, TYPE_I4
);
5428 do_conversion (&ctx
, TYPE_I8
);
5435 do_conversion (&ctx
, TYPE_R8
);
5441 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5446 code_bounds_check (5);
5447 do_cpobj (&ctx
, read32 (ip
+ 1));
5452 code_bounds_check (5);
5453 do_ldobj_value (&ctx
, read32 (ip
+ 1));
5458 code_bounds_check (5);
5459 do_ldstr (&ctx
, read32 (ip
+ 1));
5464 code_bounds_check (5);
5465 do_newobj (&ctx
, read32 (ip
+ 1));
5471 code_bounds_check (5);
5472 do_cast (&ctx
, read32 (ip
+ 1), *ip
== CEE_CASTCLASS
? "castclass" : "isinst");
5478 ++ip
; /* warn, error ? */
5482 code_bounds_check (5);
5483 do_unbox_value (&ctx
, read32 (ip
+ 1));
5495 code_bounds_check (5);
5496 do_push_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDFLDA
);
5502 code_bounds_check (5);
5503 do_push_static_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDSFLDA
);
5508 code_bounds_check (5);
5509 do_store_field (&ctx
, read32 (ip
+ 1));
5514 code_bounds_check (5);
5515 do_store_static_field (&ctx
, read32 (ip
+ 1));
5520 code_bounds_check (5);
5521 do_stobj (&ctx
, read32 (ip
+ 1));
5525 case CEE_CONV_OVF_I1_UN
:
5526 case CEE_CONV_OVF_I2_UN
:
5527 case CEE_CONV_OVF_I4_UN
:
5528 case CEE_CONV_OVF_U1_UN
:
5529 case CEE_CONV_OVF_U2_UN
:
5530 case CEE_CONV_OVF_U4_UN
:
5531 do_conversion (&ctx
, TYPE_I4
);
5535 case CEE_CONV_OVF_I8_UN
:
5536 case CEE_CONV_OVF_U8_UN
:
5537 do_conversion (&ctx
, TYPE_I8
);
5541 case CEE_CONV_OVF_I_UN
:
5542 case CEE_CONV_OVF_U_UN
:
5543 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5548 code_bounds_check (5);
5549 do_box_value (&ctx
, read32 (ip
+ 1));
5554 code_bounds_check (5);
5555 do_newarr (&ctx
, read32 (ip
+ 1));
5565 code_bounds_check (5);
5566 do_ldelema (&ctx
, read32 (ip
+ 1));
5580 case CEE_LDELEM_REF
:
5581 do_ldelem (&ctx
, *ip
, 0);
5592 case CEE_STELEM_REF
:
5593 do_stelem (&ctx
, *ip
, 0);
5598 code_bounds_check (5);
5599 do_ldelem (&ctx
, *ip
, read32 (ip
+ 1));
5604 code_bounds_check (5);
5605 do_stelem (&ctx
, *ip
, read32 (ip
+ 1));
5610 code_bounds_check (5);
5611 do_unbox_any (&ctx
, read32 (ip
+ 1));
5615 case CEE_CONV_OVF_I1
:
5616 case CEE_CONV_OVF_U1
:
5617 case CEE_CONV_OVF_I2
:
5618 case CEE_CONV_OVF_U2
:
5619 case CEE_CONV_OVF_I4
:
5620 case CEE_CONV_OVF_U4
:
5621 do_conversion (&ctx
, TYPE_I4
);
5625 case CEE_CONV_OVF_I8
:
5626 case CEE_CONV_OVF_U8
:
5627 do_conversion (&ctx
, TYPE_I8
);
5631 case CEE_CONV_OVF_I
:
5632 case CEE_CONV_OVF_U
:
5633 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5638 code_bounds_check (5);
5639 do_refanyval (&ctx
, read32 (ip
+ 1));
5649 code_bounds_check (5);
5650 do_mkrefany (&ctx
, read32 (ip
+ 1));
5655 code_bounds_check (5);
5656 do_load_token (&ctx
, read32 (ip
+ 1));
5660 case CEE_ENDFINALLY
:
5661 if (!is_correct_endfinally (ctx
.header
, ip_offset
))
5662 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx
.ip_offset
));
5669 code_bounds_check (5);
5670 do_leave (&ctx
, read32 (ip
+ 1) + 5);
5676 code_bounds_check (2);
5677 do_leave (&ctx
, (signed char)ip
[1] + 2);
5683 code_bounds_check (2);
5687 code_bounds_check (3);
5688 store_local (&ctx
, read16 (ip
+ 1));
5693 do_cmp_op (&ctx
, cmp_br_eq_op
, *ip
);
5701 do_cmp_op (&ctx
, cmp_br_op
, *ip
);
5706 code_bounds_check (3);
5707 store_arg (&ctx
, read16 (ip
+ 1) );
5713 check_overflow (&ctx
);
5714 if (ctx
.signature
->call_convention
!= MONO_CALL_VARARG
)
5715 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx
.ip_offset
));
5716 set_stack_value (&ctx
, stack_push (&ctx
), &mono_defaults
.argumenthandle_class
->byval_arg
, FALSE
);
5721 code_bounds_check (5);
5722 do_load_function_ptr (&ctx
, read32 (ip
+ 1), FALSE
);
5727 code_bounds_check (5);
5728 do_load_function_ptr (&ctx
, read32 (ip
+ 1), TRUE
);
5738 code_bounds_check (3);
5739 push_arg (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDARGA
);
5745 code_bounds_check (3);
5746 push_local (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDLOCA
);
5759 do_endfilter (&ctx
);
5763 case CEE_UNALIGNED_
:
5764 code_bounds_check (2);
5765 prefix
|= PREFIX_UNALIGNED
;
5769 prefix
|= PREFIX_VOLATILE
;
5773 prefix
|= PREFIX_TAIL
;
5775 if (ip
< end
&& (*ip
!= CEE_CALL
&& *ip
!= CEE_CALLI
&& *ip
!= CEE_CALLVIRT
))
5776 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset
));
5780 code_bounds_check (5);
5781 do_initobj (&ctx
, read32 (ip
+ 1));
5785 case CEE_CONSTRAINED_
:
5786 code_bounds_check (5);
5787 ctx
.constrained_type
= get_boxable_mono_type (&ctx
, read32 (ip
+ 1), "constrained.");
5788 prefix
|= PREFIX_CONSTRAINED
;
5793 prefix
|= PREFIX_READONLY
;
5798 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5799 if (!check_underflow (&ctx
, 3))
5801 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5806 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5807 if (!check_underflow (&ctx
, 3))
5809 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5817 if (!is_correct_rethrow (ctx
.header
, ip_offset
))
5818 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx
.ip_offset
));
5828 code_bounds_check (5);
5829 do_sizeof (&ctx
, read32 (ip
+ 1));
5833 case CEE_REFANYTYPE
:
5834 do_refanytype (&ctx
);
5839 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip
, ctx
.ip_offset
));
5845 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ctx
.ip_offset
));
5849 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5851 if (!ctx
.prefix_set
) //first prefix
5852 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5853 ctx
.prefix_set
|= prefix
;
5854 ctx
.has_flags
= TRUE
;
5858 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5860 if (ctx
.prefix_set
& PREFIX_CONSTRAINED
)
5861 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx
.ip_offset
));
5862 if (ctx
.prefix_set
& PREFIX_READONLY
)
5863 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx
.ip_offset
));
5864 if (ctx
.prefix_set
& PREFIX_VOLATILE
)
5865 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx
.ip_offset
));
5866 if (ctx
.prefix_set
& PREFIX_UNALIGNED
)
5867 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx
.ip_offset
));
5868 ctx
.prefix_set
= prefix
= 0;
5869 ctx
.has_flags
= FALSE
;
5873 * if ip != end we overflowed: mark as error.
5875 if ((ip
!= end
|| !start
) && ctx
.verifiable
&& !ctx
.list
) {
5876 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset
));
5879 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5880 for (i
= 0; i
< ctx
.code_size
&& i
< ip_offset
; ++i
) {
5881 if (ctx
.code
[i
].flags
& IL_CODE_FLAG_WAS_TARGET
) {
5882 if (!(ctx
.code
[i
].flags
& IL_CODE_FLAG_SEEN
))
5883 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i
));
5885 if (ctx
.code
[i
].flags
& IL_CODE_DELEGATE_SEQUENCE
)
5886 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i
));
5888 if ((ctx
.code
[i
].flags
& IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5889 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i
));
5891 if ((ctx
.code
[i
].flags
& IL_CODE_CALL_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5892 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
));
5895 if (mono_method_is_constructor (ctx
.method
) && !ctx
.super_ctor_called
&& !ctx
.method
->klass
->valuetype
&& ctx
.method
->klass
!= mono_defaults
.object_class
) {
5896 char *method_name
= mono_method_full_name (ctx
.method
, TRUE
);
5897 char *type
= mono_type_get_full_name (ctx
.method
->klass
);
5898 if (ctx
.method
->klass
->parent
&& ctx
.method
->klass
->parent
->exception_type
!= MONO_EXCEPTION_NONE
)
5899 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Constructor %s for type %s not calling base type ctor due to a TypeLoadException on base type.", method_name
, type
));
5901 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name
, type
));
5902 g_free (method_name
);
5908 for (i
= 0; i
< ctx
.header
->code_size
; ++i
) {
5909 if (ctx
.code
[i
].stack
)
5910 g_free (ctx
.code
[i
].stack
);
5914 for (tmp
= ctx
.funptrs
; tmp
; tmp
= tmp
->next
)
5916 g_slist_free (ctx
.funptrs
);
5918 for (tmp
= ctx
.exception_types
; tmp
; tmp
= tmp
->next
)
5919 mono_metadata_free_type (tmp
->data
);
5920 g_slist_free (ctx
.exception_types
);
5922 for (i
= 0; i
< ctx
.num_locals
; ++i
)
5923 mono_metadata_free_type (ctx
.locals
[i
]);
5924 for (i
= 0; i
< ctx
.max_args
; ++i
)
5925 mono_metadata_free_type (ctx
.params
[i
]);
5928 g_free (ctx
.eval
.stack
);
5931 g_free (ctx
.locals
);
5932 g_free (ctx
.params
);
5938 mono_verify_corlib ()
5940 /* This is a public API function so cannot be removed */
5945 * Returns true if @method needs to be verified.
5949 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
5951 return mono_verifier_is_enabled_for_class (method
->klass
) && method
->wrapper_type
== MONO_WRAPPER_NONE
;
5955 * Returns true if @klass need to be verified.
5959 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
5961 return verify_all
|| (verifier_mode
> MONO_VERIFIER_MODE_OFF
&& !klass
->image
->assembly
->in_gac
&& klass
->image
!= mono_defaults
.corlib
);
5965 mono_verifier_is_enabled_for_image (MonoImage
*image
)
5967 return verify_all
|| verifier_mode
> MONO_VERIFIER_MODE_OFF
;
5971 mono_verifier_is_method_full_trust (MonoMethod
*method
)
5973 return mono_verifier_is_class_full_trust (method
->klass
);
5977 * Returns if @klass is under full trust or not.
5979 * TODO This code doesn't take CAS into account.
5981 * Under verify_all all user code must be verifiable if no security option was set
5985 mono_verifier_is_class_full_trust (MonoClass
*klass
)
5987 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5988 gboolean trusted_location
= (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR
) ?
5989 klass
->image
->assembly
->in_gac
: mono_security_core_clr_is_platform_image (klass
->image
);
5991 if (verify_all
&& verifier_mode
== MONO_VERIFIER_MODE_OFF
)
5992 return trusted_location
|| klass
->image
== mono_defaults
.corlib
;
5993 return verifier_mode
< MONO_VERIFIER_MODE_VERIFIABLE
|| trusted_location
|| klass
->image
== mono_defaults
.corlib
;
5997 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
5999 return mono_method_verify (method
,
6000 (verifier_mode
!= MONO_VERIFIER_MODE_STRICT
? MONO_VERIFY_NON_STRICT
: 0)
6001 | (!mono_verifier_is_method_full_trust (method
) ? MONO_VERIFY_FAIL_FAST
: 0)
6002 | (skip_visibility
? MONO_VERIFY_SKIP_VISIBILITY
: 0));
6006 get_field_end (MonoClassField
*field
)
6009 int size
= mono_type_size (field
->type
, &align
);
6011 size
= 4; /*FIXME Is this a safe bet?*/
6012 return size
+ field
->offset
;
6016 verify_class_for_overlapping_reference_fields (MonoClass
*class)
6019 gboolean is_fulltrust
= mono_verifier_is_class_full_trust (class);
6020 if (!((class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) || !class->has_references
)
6023 //we must check for stuff overlapping reference fields
6024 for (i
= 0; i
< class->field
.count
; ++i
) {
6025 MonoClassField
*field
= &class->fields
[i
];
6026 int fieldEnd
= get_field_end (field
);
6027 gboolean is_valuetype
= !MONO_TYPE_IS_REFERENCE (field
->type
);
6028 if (mono_field_is_deleted (field
) || (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6031 for (j
= i
+ 1; j
< class->field
.count
; ++j
) {
6032 MonoClassField
*other
= &class->fields
[j
];
6033 int otherEnd
= get_field_end (other
);
6034 if (mono_field_is_deleted (other
) || (is_valuetype
&& !MONO_TYPE_IS_REFERENCE (other
->type
)) || (other
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6037 if (!is_valuetype
&& MONO_TYPE_IS_REFERENCE (other
->type
) && field
->offset
== other
->offset
&& is_fulltrust
)
6040 if ((otherEnd
> field
->offset
&& otherEnd
<= fieldEnd
) || (other
->offset
>= field
->offset
&& other
->offset
< fieldEnd
))
6048 field_hash (gconstpointer key
)
6050 const MonoClassField
*field
= key
;
6051 return g_str_hash (field
->name
) ^ mono_metadata_type_hash (field
->type
); /**/
6055 field_equals (gconstpointer _a
, gconstpointer _b
)
6057 const MonoClassField
*a
= _a
;
6058 const MonoClassField
*b
= _b
;
6059 return !strcmp (a
->name
, b
->name
) && mono_metadata_type_equal (a
->type
, b
->type
);
6064 verify_class_fields (MonoClass
*class)
6066 gpointer iter
= NULL
;
6067 MonoClassField
*field
;
6068 MonoGenericContext
*context
= mono_class_get_context (class);
6069 GHashTable
*unique_fields
= g_hash_table_new_full (&field_hash
, &field_equals
, NULL
, NULL
);
6070 if (class->generic_container
)
6071 context
= &class->generic_container
->context
;
6073 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
6074 if (!mono_type_is_valid_type_in_context (field
->type
, context
)) {
6075 g_hash_table_destroy (unique_fields
);
6078 if (g_hash_table_lookup (unique_fields
, field
)) {
6079 g_hash_table_destroy (unique_fields
);
6082 g_hash_table_insert (unique_fields
, field
, field
);
6084 g_hash_table_destroy (unique_fields
);
6089 verify_interfaces (MonoClass
*class)
6092 for (i
= 0; i
< class->interface_count
; ++i
) {
6093 MonoClass
*iface
= class->interfaces
[i
];
6094 if (!(iface
->flags
& TYPE_ATTRIBUTE_INTERFACE
))
6101 verify_valuetype_layout_with_target (MonoClass
*class, MonoClass
*target_class
)
6104 gpointer iter
= NULL
;
6105 MonoClassField
*field
;
6106 MonoClass
*field_class
;
6108 if (!class->valuetype
)
6111 type
= class->byval_arg
.type
;
6112 /*primitive type fields are not properly decoded*/
6113 if ((type
>= MONO_TYPE_BOOLEAN
&& type
<= MONO_TYPE_R8
) || (type
>= MONO_TYPE_I
&& type
<= MONO_TYPE_U
))
6116 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
6120 if (field
->type
->attrs
& (FIELD_ATTRIBUTE_STATIC
| FIELD_ATTRIBUTE_HAS_FIELD_RVA
))
6123 field_class
= mono_class_get_generic_type_definition (mono_class_from_mono_type (field
->type
));
6125 if (field_class
== target_class
|| !verify_valuetype_layout_with_target (field_class
, target_class
))
6133 verify_valuetype_layout (MonoClass
*class)
6136 res
= verify_valuetype_layout_with_target (class, class);
6141 * Check if the class is verifiable.
6143 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6144 * field and invalid generic instantiation are fatal errors.
6146 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6150 mono_verifier_verify_class (MonoClass
*class)
6152 if (class->generic_container
&& (class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
)
6154 if (!verify_class_for_overlapping_reference_fields (class))
6156 if (class->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, class))
6158 if (class->generic_class
== NULL
&& !verify_class_fields (class))
6160 if (class->valuetype
&& !verify_valuetype_layout (class))
6162 if (!verify_interfaces (class))
6169 mono_verifier_verify_class (MonoClass
*class)
6171 /* The verifier was disabled at compile time */
6176 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
6178 /* The verifier was disabled at compile time */
6183 mono_verifier_is_class_full_trust (MonoClass
*klass
)
6185 /* The verifier was disabled at compile time */
6190 mono_verifier_is_method_full_trust (MonoMethod
*method
)
6192 /* The verifier was disabled at compile time */
6197 mono_verifier_is_enabled_for_image (MonoImage
*image
)
6199 /* The verifier was disabled at compile time */
6204 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
6206 /* The verifier was disabled at compile time */
6211 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
6213 /* The verifier was disabled at compile time */
6218 mono_method_verify (MonoMethod
*method
, int level
)
6220 /* The verifier was disabled at compile time */
6225 mono_free_verify_list (GSList
*list
)
6227 /* The verifier was disabled at compile time */
6228 /* will always be null if verifier is disabled */
6232 mono_image_verify_tables (MonoImage
*image
, int level
)
6234 /* The verifier was disabled at compile time */