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 g_assert (ctx
->eval
.size
< ctx
->max_stack
);
2024 return & ctx
->eval
.stack
[ctx
->eval
.size
++];
2027 static ILStackDesc
*
2028 stack_push_val (VerifyContext
*ctx
, int stype
, MonoType
*type
)
2030 ILStackDesc
*top
= stack_push (ctx
);
2036 static ILStackDesc
*
2037 stack_pop (VerifyContext
*ctx
)
2040 g_assert (ctx
->eval
.size
> 0);
2041 ret
= ctx
->eval
.stack
+ --ctx
->eval
.size
;
2042 if ((ret
->stype
& UNINIT_THIS_MASK
) == UNINIT_THIS_MASK
)
2043 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx
->ip_offset
));
2047 /* This function allows to safely pop an unititialized this ptr from
2048 * the eval stack without marking the method as unverifiable.
2050 static ILStackDesc
*
2051 stack_pop_safe (VerifyContext
*ctx
)
2053 g_assert (ctx
->eval
.size
> 0);
2054 return ctx
->eval
.stack
+ --ctx
->eval
.size
;
2057 static ILStackDesc
*
2058 stack_push_stack_val (VerifyContext
*ctx
, ILStackDesc
*value
)
2060 ILStackDesc
*top
= stack_push (ctx
);
2061 copy_stack_value (top
, value
);
2065 /* Returns the MonoType associated with the token, or NULL if it is invalid.
2067 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
2070 get_boxable_mono_type (VerifyContext
* ctx
, int token
, const char *opcode
)
2075 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
2078 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
2079 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2083 if (type
->type
== MONO_TYPE_VOID
) {
2084 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2088 if (type
->type
== MONO_TYPE_TYPEDBYREF
)
2089 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2091 if (!(class = mono_class_from_mono_type (type
)))
2092 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2094 if (class->generic_container
&& type
->type
!= MONO_TYPE_GENERICINST
)
2095 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
));
2097 check_unverifiable_type (ctx
, type
);
2102 /*operation result tables */
2104 static const unsigned char bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2105 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2106 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2107 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2108 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2109 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2110 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2113 static const unsigned char add_table
[TYPE_MAX
][TYPE_MAX
] = {
2114 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2115 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2116 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2117 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2118 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2119 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2122 static const unsigned char sub_table
[TYPE_MAX
][TYPE_MAX
] = {
2123 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2124 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2125 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2126 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2127 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2128 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2131 static const unsigned char int_bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2132 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2133 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2134 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2135 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2136 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2137 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2140 static const unsigned char shift_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2141 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2142 {TYPE_I8
, TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2143 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2144 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2145 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2146 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2149 static const unsigned char cmp_br_op
[TYPE_MAX
][TYPE_MAX
] = {
2150 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2151 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2152 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2153 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
2154 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
2155 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2158 static const unsigned char cmp_br_eq_op
[TYPE_MAX
][TYPE_MAX
] = {
2159 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2160 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2161 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2162 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
2163 {TYPE_INV
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
2164 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
},
2167 static const unsigned char add_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
2168 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2169 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2170 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2171 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2172 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2173 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2176 static const unsigned char sub_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
2177 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2178 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2179 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2180 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2181 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2182 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2185 static const unsigned char bin_ovf_table
[TYPE_MAX
][TYPE_MAX
] = {
2186 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2187 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2188 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2189 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2190 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2191 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2194 #ifdef MONO_VERIFIER_DEBUG
2198 dump_stack_value (ILStackDesc
*value
)
2200 printf ("[(%x)(%x)", value
->type
->type
, value
->stype
);
2202 if (stack_slot_is_this_pointer (value
))
2205 if (stack_slot_is_boxed_value (value
))
2206 printf ("[boxed] ");
2208 if (stack_slot_is_null_literal (value
))
2211 if (stack_slot_is_managed_mutability_pointer (value
))
2212 printf ("Controled Mutability MP: ");
2214 if (stack_slot_is_managed_pointer (value
))
2215 printf ("Managed Pointer to: ");
2217 switch (stack_slot_get_underlying_type (value
)) {
2219 printf ("invalid type]");
2227 case TYPE_NATIVE_INT
:
2228 printf ("native int]");
2231 printf ("float64]");
2234 printf ("unmanaged pointer]");
2237 switch (value
->type
->type
) {
2238 case MONO_TYPE_CLASS
:
2239 case MONO_TYPE_VALUETYPE
:
2240 printf ("complex] (%s)", value
->type
->data
.klass
->name
);
2242 case MONO_TYPE_STRING
:
2243 printf ("complex] (string)");
2245 case MONO_TYPE_OBJECT
:
2246 printf ("complex] (object)");
2248 case MONO_TYPE_SZARRAY
:
2249 printf ("complex] (%s [])", value
->type
->data
.klass
->name
);
2251 case MONO_TYPE_ARRAY
:
2252 printf ("complex] (%s [%d %d %d])",
2253 value
->type
->data
.array
->eklass
->name
,
2254 value
->type
->data
.array
->rank
,
2255 value
->type
->data
.array
->numsizes
,
2256 value
->type
->data
.array
->numlobounds
);
2258 case MONO_TYPE_GENERICINST
:
2259 printf ("complex] (inst of %s )", value
->type
->data
.generic_class
->container_class
->name
);
2262 printf ("complex] (type generic param !%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
2264 case MONO_TYPE_MVAR
:
2265 printf ("complex] (method generic param !!%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
2268 //should be a boxed value
2269 char * name
= mono_type_full_name (value
->type
);
2270 printf ("complex] %s", name
);
2276 printf ("unknown stack %x type]\n", value
->stype
);
2277 g_assert_not_reached ();
2282 dump_stack_state (ILCodeDesc
*state
)
2286 printf ("(%d) ", state
->size
);
2287 for (i
= 0; i
< state
->size
; ++i
)
2288 dump_stack_value (state
->stack
+ i
);
2293 /*Returns TRUE if candidate array type can be assigned to target.
2294 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
2297 is_array_type_compatible (MonoType
*target
, MonoType
*candidate
)
2299 MonoArrayType
*left
= target
->data
.array
;
2300 MonoArrayType
*right
= candidate
->data
.array
;
2302 g_assert (target
->type
== MONO_TYPE_ARRAY
);
2303 g_assert (candidate
->type
== MONO_TYPE_ARRAY
);
2305 if (left
->rank
!= right
->rank
)
2308 return mono_class_is_assignable_from (left
->eklass
, right
->eklass
);
2312 get_stack_type (MonoType
*type
)
2315 int type_kind
= type
->type
;
2317 mask
= POINTER_MASK
;
2318 /*TODO handle CMMP_MASK */
2321 switch (type_kind
) {
2324 case MONO_TYPE_BOOLEAN
:
2327 case MONO_TYPE_CHAR
:
2330 return TYPE_I4
| mask
;
2334 return TYPE_NATIVE_INT
| mask
;
2336 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
2337 case MONO_TYPE_FNPTR
:
2339 case MONO_TYPE_TYPEDBYREF
:
2340 return TYPE_PTR
| mask
;
2343 case MONO_TYPE_MVAR
:
2345 case MONO_TYPE_CLASS
:
2346 case MONO_TYPE_STRING
:
2347 case MONO_TYPE_OBJECT
:
2348 case MONO_TYPE_SZARRAY
:
2349 case MONO_TYPE_ARRAY
:
2350 return TYPE_COMPLEX
| mask
;
2352 case MONO_TYPE_GENERICINST
:
2353 if (mono_type_is_enum_type (type
)) {
2354 type
= mono_type_get_underlying_type_any (type
);
2355 type_kind
= type
->type
;
2358 return TYPE_COMPLEX
| mask
;
2363 return TYPE_I8
| mask
;
2367 return TYPE_R8
| mask
;
2369 case MONO_TYPE_VALUETYPE
:
2370 if (mono_type_is_enum_type (type
)) {
2371 type
= mono_type_get_underlying_type_any (type
);
2372 type_kind
= type
->type
;
2375 return TYPE_COMPLEX
| mask
;
2379 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type
->type
); );
2380 g_assert_not_reached ();
2385 /* convert MonoType to ILStackDesc format (stype) */
2387 set_stack_value (VerifyContext
*ctx
, ILStackDesc
*stack
, MonoType
*type
, int take_addr
)
2390 int type_kind
= type
->type
;
2392 if (type
->byref
|| take_addr
)
2393 mask
= POINTER_MASK
;
2394 /* TODO handle CMMP_MASK */
2399 switch (type_kind
) {
2402 case MONO_TYPE_BOOLEAN
:
2405 case MONO_TYPE_CHAR
:
2408 stack
->stype
= TYPE_I4
| mask
;
2412 stack
->stype
= TYPE_NATIVE_INT
| mask
;
2415 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2416 case MONO_TYPE_FNPTR
:
2418 case MONO_TYPE_TYPEDBYREF
:
2419 stack
->stype
= TYPE_PTR
| mask
;
2422 case MONO_TYPE_CLASS
:
2423 case MONO_TYPE_STRING
:
2424 case MONO_TYPE_OBJECT
:
2425 case MONO_TYPE_SZARRAY
:
2426 case MONO_TYPE_ARRAY
:
2429 case MONO_TYPE_MVAR
:
2430 stack
->stype
= TYPE_COMPLEX
| mask
;
2433 case MONO_TYPE_GENERICINST
:
2434 if (mono_type_is_enum_type (type
)) {
2435 type
= mono_type_get_underlying_type_any (type
);
2436 type_kind
= type
->type
;
2439 stack
->stype
= TYPE_COMPLEX
| mask
;
2445 stack
->stype
= TYPE_I8
| mask
;
2449 stack
->stype
= TYPE_R8
| mask
;
2451 case MONO_TYPE_VALUETYPE
:
2452 if (mono_type_is_enum_type (type
)) {
2453 type
= mono_type_get_underlying_type_any (type
);
2454 type_kind
= type
->type
;
2457 stack
->stype
= TYPE_COMPLEX
| mask
;
2461 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type
->type
); );
2462 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type
->type
, ctx
->ip_offset
));
2469 * init_stack_with_value_at_exception_boundary:
2471 * Initialize the stack and push a given type.
2472 * The instruction is marked as been on the exception boundary.
2475 init_stack_with_value_at_exception_boundary (VerifyContext
*ctx
, ILCodeDesc
*code
, MonoClass
*klass
)
2478 MonoType
*type
= mono_class_inflate_generic_type_checked (&klass
->byval_arg
, ctx
->generic_context
, &error
);
2480 if (!mono_error_ok (&error
)) {
2481 char *name
= mono_type_get_full_name (klass
);
2482 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid class %s used for exception", name
));
2484 mono_error_cleanup (&error
);
2488 stack_init (ctx
, code
);
2489 set_stack_value (ctx
, code
->stack
, type
, FALSE
);
2490 ctx
->exception_types
= g_slist_prepend (ctx
->exception_types
, type
);
2492 code
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
2493 if (mono_type_is_generic_argument (type
))
2494 code
->stack
->stype
|= BOXED_MASK
;
2497 /*Verify if type 'candidate' can be stored in type 'target'.
2499 * If strict, check for the underlying type and not the verification stack types
2502 verify_type_compatibility_full (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
, gboolean strict
)
2504 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2505 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2507 MonoType
*original_candidate
= candidate
;
2508 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target
), mono_type_full_name (candidate
), strict
); );
2510 /*only one is byref */
2511 if (candidate
->byref
^ target
->byref
) {
2512 /* converting from native int to byref*/
2513 if (get_stack_type (candidate
) == TYPE_NATIVE_INT
&& target
->byref
) {
2514 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("using byref native int at 0x%04x", ctx
->ip_offset
));
2519 strict
|= target
->byref
;
2520 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2521 candidate
= mono_type_get_underlying_type_any (candidate
);
2524 switch (target
->type
) {
2525 case MONO_TYPE_VOID
:
2526 return candidate
->type
== MONO_TYPE_VOID
;
2529 case MONO_TYPE_BOOLEAN
:
2531 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I1
, MONO_TYPE_U1
, MONO_TYPE_BOOLEAN
);
2534 case MONO_TYPE_CHAR
:
2536 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I2
, MONO_TYPE_U2
, MONO_TYPE_CHAR
);
2538 case MONO_TYPE_U4
: {
2539 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2540 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2542 return is_native_int
|| is_int4
;
2543 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2548 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I8
, MONO_TYPE_U8
);
2553 return candidate
->type
== target
->type
;
2554 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_R4
, MONO_TYPE_R8
);
2558 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2559 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2561 return is_native_int
|| is_int4
;
2562 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2566 if (candidate
->type
!= MONO_TYPE_PTR
)
2568 /* check the underlying type */
2569 return verify_type_compatibility_full (ctx
, target
->data
.type
, candidate
->data
.type
, TRUE
);
2571 case MONO_TYPE_FNPTR
: {
2572 MonoMethodSignature
*left
, *right
;
2573 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2576 left
= mono_type_get_signature (target
);
2577 right
= mono_type_get_signature (candidate
);
2578 return mono_metadata_signature_equal (left
, right
) && left
->call_convention
== right
->call_convention
;
2581 case MONO_TYPE_GENERICINST
: {
2582 MonoClass
*target_klass
;
2583 MonoClass
*candidate_klass
;
2584 if (mono_type_is_enum_type (target
)) {
2585 target
= mono_type_get_underlying_type_any (target
);
2588 target_klass
= mono_class_from_mono_type (target
);
2589 candidate_klass
= mono_class_from_mono_type (candidate
);
2590 if (mono_class_is_nullable (target_klass
)) {
2591 if (!mono_class_is_nullable (candidate_klass
))
2593 return target_klass
== candidate_klass
;
2596 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2599 case MONO_TYPE_STRING
:
2600 return candidate
->type
== MONO_TYPE_STRING
;
2602 case MONO_TYPE_CLASS
:
2604 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2605 * to take boxing status into account.
2607 if (mono_type_is_generic_argument (original_candidate
))
2609 /* If candidate is an enum it should return true for System.Enum and supertypes.
2610 * That's why here we use the original type and not the underlying type.
2612 return mono_class_is_assignable_from (target
->data
.klass
, mono_class_from_mono_type (original_candidate
));
2614 case MONO_TYPE_OBJECT
:
2615 return MONO_TYPE_IS_REFERENCE (candidate
);
2617 case MONO_TYPE_SZARRAY
: {
2620 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2623 left
= mono_class_from_mono_type (target
)->element_class
;
2624 right
= mono_class_from_mono_type (candidate
)->element_class
;
2625 return mono_class_is_assignable_from (left
, right
);
2628 case MONO_TYPE_ARRAY
:
2629 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2631 return is_array_type_compatible (target
, candidate
);
2633 case MONO_TYPE_TYPEDBYREF
:
2634 return candidate
->type
== MONO_TYPE_TYPEDBYREF
;
2636 case MONO_TYPE_VALUETYPE
: {
2637 MonoClass
*target_klass
= mono_class_from_mono_type (target
);
2638 MonoClass
*candidate_klass
= mono_class_from_mono_type (candidate
);
2640 if (target_klass
== candidate_klass
)
2642 if (mono_type_is_enum_type (target
)) {
2643 target
= mono_type_get_underlying_type_any (target
);
2650 if (candidate
->type
!= MONO_TYPE_VAR
)
2652 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2654 case MONO_TYPE_MVAR
:
2655 if (candidate
->type
!= MONO_TYPE_MVAR
)
2657 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2660 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target
->type
); );
2661 g_assert_not_reached ();
2670 verify_type_compatibility (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
)
2672 return verify_type_compatibility_full (ctx
, target
, candidate
, FALSE
);
2676 * Returns the generic param bound to the context been verified.
2679 static MonoGenericParam
*
2680 get_generic_param (VerifyContext
*ctx
, MonoType
*param
)
2682 guint16 param_num
= mono_type_get_generic_param_num (param
);
2683 if (param
->type
== MONO_TYPE_VAR
) {
2684 if (!ctx
->generic_context
->class_inst
|| ctx
->generic_context
->class_inst
->type_argc
<= param_num
) {
2685 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic type argument %d", param_num
));
2688 return ctx
->generic_context
->class_inst
->type_argv
[param_num
]->data
.generic_param
;
2691 /*param must be a MVAR */
2692 if (!ctx
->generic_context
->method_inst
|| ctx
->generic_context
->method_inst
->type_argc
<= param_num
) {
2693 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic method argument %d", param_num
));
2696 return ctx
->generic_context
->method_inst
->type_argv
[param_num
]->data
.generic_param
;
2700 * is_compatible_boxed_valuetype:
2702 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2704 * @type The source type. It it tested to be of the proper type.
2705 * @candidate type of the boxed valuetype.
2706 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2707 * @strict if TRUE candidate must be boxed compatible to the target type
2711 is_compatible_boxed_valuetype (VerifyContext
*ctx
, MonoType
*type
, MonoType
*candidate
, ILStackDesc
*stack
, gboolean strict
)
2713 if (!stack_slot_is_boxed_value (stack
))
2715 if (type
->byref
|| candidate
->byref
)
2718 if (mono_type_is_generic_argument (candidate
)) {
2719 MonoGenericParam
*param
= get_generic_param (ctx
, candidate
);
2721 for (class = mono_generic_param_info (param
)->constraints
; class && *class; ++class) {
2722 if (verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (& (*class)->byval_arg
), FALSE
))
2727 if (mono_type_is_generic_argument (type
))
2733 return MONO_TYPE_IS_REFERENCE (type
) && mono_class_is_assignable_from (mono_class_from_mono_type (type
), mono_class_from_mono_type (candidate
));
2737 verify_stack_type_compatibility_full (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
, gboolean drop_byref
, gboolean valuetype_must_be_boxed
)
2739 MonoType
*candidate
= mono_type_from_stack_slot (stack
);
2740 if (MONO_TYPE_IS_REFERENCE (type
) && !type
->byref
&& stack_slot_is_null_literal (stack
))
2743 if (is_compatible_boxed_valuetype (ctx
, type
, candidate
, stack
, TRUE
))
2746 if (valuetype_must_be_boxed
&& !stack_slot_is_boxed_value (stack
) && !MONO_TYPE_IS_REFERENCE (candidate
))
2749 if (!valuetype_must_be_boxed
&& stack_slot_is_boxed_value (stack
))
2753 return verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (candidate
), FALSE
);
2755 return verify_type_compatibility_full (ctx
, type
, candidate
, FALSE
);
2759 verify_stack_type_compatibility (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
)
2761 return verify_stack_type_compatibility_full (ctx
, type
, stack
, FALSE
, FALSE
);
2765 mono_delegate_type_equal (MonoType
*target
, MonoType
*candidate
)
2767 if (candidate
->byref
^ target
->byref
)
2770 switch (target
->type
) {
2771 case MONO_TYPE_VOID
:
2774 case MONO_TYPE_BOOLEAN
:
2777 case MONO_TYPE_CHAR
:
2786 case MONO_TYPE_STRING
:
2787 case MONO_TYPE_TYPEDBYREF
:
2788 return candidate
->type
== target
->type
;
2791 return mono_delegate_type_equal (target
->data
.type
, candidate
->data
.type
);
2793 case MONO_TYPE_FNPTR
:
2794 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2796 return mono_delegate_signature_equal (mono_type_get_signature (target
), mono_type_get_signature (candidate
), FALSE
);
2798 case MONO_TYPE_GENERICINST
: {
2799 MonoClass
*target_klass
;
2800 MonoClass
*candidate_klass
;
2801 target_klass
= mono_class_from_mono_type (target
);
2802 candidate_klass
= mono_class_from_mono_type (candidate
);
2803 /*FIXME handle nullables and enum*/
2804 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2806 case MONO_TYPE_OBJECT
:
2807 return MONO_TYPE_IS_REFERENCE (candidate
);
2809 case MONO_TYPE_CLASS
:
2810 return mono_class_is_assignable_from(target
->data
.klass
, mono_class_from_mono_type (candidate
));
2812 case MONO_TYPE_SZARRAY
:
2813 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2815 return mono_class_is_assignable_from (mono_class_from_mono_type (target
)->element_class
, mono_class_from_mono_type (candidate
)->element_class
);
2817 case MONO_TYPE_ARRAY
:
2818 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2820 return is_array_type_compatible (target
, candidate
);
2822 case MONO_TYPE_VALUETYPE
:
2823 /*FIXME handle nullables and enum*/
2824 return mono_class_from_mono_type (candidate
) == mono_class_from_mono_type (target
);
2827 return candidate
->type
== MONO_TYPE_VAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2830 case MONO_TYPE_MVAR
:
2831 return candidate
->type
== MONO_TYPE_MVAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2835 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target
->type
); );
2836 g_assert_not_reached ();
2842 mono_delegate_param_equal (MonoType
*delegate
, MonoType
*method
)
2844 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2847 return mono_delegate_type_equal (method
, delegate
);
2851 mono_delegate_ret_equal (MonoType
*delegate
, MonoType
*method
)
2853 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2856 return mono_delegate_type_equal (delegate
, method
);
2860 * mono_delegate_signature_equal:
2862 * Compare two signatures in the way expected by delegates.
2864 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2866 * FIXME can this function be eliminated and proper metadata functionality be used?
2869 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
)
2872 int method_offset
= is_static_ldftn
? 1 : 0;
2874 if (delegate_sig
->param_count
+ method_offset
!= method_sig
->param_count
)
2877 if (delegate_sig
->call_convention
!= method_sig
->call_convention
)
2880 for (i
= 0; i
< delegate_sig
->param_count
; i
++) {
2881 MonoType
*p1
= delegate_sig
->params
[i
];
2882 MonoType
*p2
= method_sig
->params
[i
+ method_offset
];
2884 if (!mono_delegate_param_equal (p1
, p2
))
2888 if (!mono_delegate_ret_equal (delegate_sig
->ret
, method_sig
->ret
))
2895 * verify_ldftn_delegate:
2897 * Verify properties of ldftn based delegates.
2900 verify_ldftn_delegate (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2902 MonoMethod
*method
= funptr
->method
;
2904 /*ldftn non-final virtuals only allowed if method is not static,
2905 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2906 * This rules doesn't apply if the object on stack is a boxed valuetype.
2908 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !(method
->klass
->flags
& TYPE_ATTRIBUTE_SEALED
) && !stack_slot_is_boxed_value (value
)) {
2909 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2910 if (IS_FAIL_FAST_MODE (ctx
) && ctx
->has_this_store
)
2911 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx
->ip_offset
));
2913 /*current method must not be static*/
2914 if (ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)
2915 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx
->ip_offset
));
2917 /*value is the this pointer, loaded using ldarg.0 */
2918 if (!stack_slot_is_this_pointer (value
))
2919 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
));
2921 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
;
2926 * verify_delegate_compatibility:
2928 * Verify delegate creation sequence.
2932 verify_delegate_compatibility (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2934 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2935 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2937 MonoMethod
*invoke
, *method
;
2938 const guint8
*ip
= ctx
->header
->code
;
2939 guint32 ip_offset
= ctx
->ip_offset
;
2940 gboolean is_static_ldftn
= FALSE
, is_first_arg_bound
= FALSE
;
2942 if (stack_slot_get_type (funptr
) != TYPE_PTR
|| !funptr
->method
) {
2943 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx
->ip_offset
));
2947 invoke
= mono_get_delegate_invoke (delegate
);
2948 method
= funptr
->method
;
2950 is_static_ldftn
= (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) && method
->flags
& METHOD_ATTRIBUTE_STATIC
;
2952 if (is_static_ldftn
)
2953 is_first_arg_bound
= mono_method_signature (invoke
)->param_count
+ 1 == mono_method_signature (method
)->param_count
;
2955 if (!mono_delegate_signature_equal (mono_method_signature (invoke
), mono_method_signature (method
), is_first_arg_bound
)) {
2956 char *fun_sig
= mono_signature_get_desc (mono_method_signature (method
), FALSE
);
2957 char *invoke_sig
= mono_signature_get_desc (mono_method_signature (invoke
), FALSE
);
2958 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
));
2960 g_free (invoke_sig
);
2964 * Delegate code sequences:
2970 * [-6] ldvirtftn token
2974 if (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) {
2975 verify_ldftn_delegate (ctx
, delegate
, value
, funptr
);
2976 } else if (ip_offset
> 6 && IS_VALID_OPCODE (7, CEE_DUP
) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN
)) {
2977 ctx
->code
[ip_offset
- 6].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2979 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx
->ip_offset
));
2981 ctx
->code
[ip_offset
].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2984 if (is_first_arg_bound
) {
2985 if (mono_method_signature (method
)->param_count
== 0 || !verify_stack_type_compatibility_full (ctx
, mono_method_signature (method
)->params
[0], value
, FALSE
, TRUE
))
2986 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2988 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
2989 if (!stack_slot_is_null_literal (value
) && !is_first_arg_bound
)
2990 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx
->ip_offset
));
2992 if (!verify_stack_type_compatibility_full (ctx
, &method
->klass
->byval_arg
, value
, FALSE
, TRUE
) && !stack_slot_is_null_literal (value
))
2993 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2997 if (stack_slot_get_type (value
) != TYPE_COMPLEX
)
2998 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx
->ip_offset
));
3000 #undef IS_VALID_OPCODE
3001 #undef IS_LOAD_FUN_PTR
3004 /* implement the opcode checks*/
3006 push_arg (VerifyContext
*ctx
, unsigned int arg
, int take_addr
)
3010 if (arg
>= ctx
->max_args
) {
3012 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
3014 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
3015 if (check_overflow (ctx
)) //FIXME: what sane value could we ever push?
3016 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3018 } else if (check_overflow (ctx
)) {
3019 /*We must let the value be pushed, otherwise we would get an underflow error*/
3020 check_unverifiable_type (ctx
, ctx
->params
[arg
]);
3021 if (ctx
->params
[arg
]->byref
&& take_addr
)
3022 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
3023 top
= stack_push (ctx
);
3024 if (!set_stack_value (ctx
, top
, ctx
->params
[arg
], take_addr
))
3027 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3029 ctx
->has_this_store
= TRUE
;
3031 top
->stype
|= THIS_POINTER_MASK
;
3032 if (mono_method_is_constructor (ctx
->method
) && !ctx
->super_ctor_called
&& !ctx
->method
->klass
->valuetype
)
3033 top
->stype
|= UNINIT_THIS_MASK
;
3039 push_local (VerifyContext
*ctx
, guint32 arg
, int take_addr
)
3041 if (arg
>= ctx
->num_locals
) {
3042 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local %d", arg
+ 1));
3043 } else if (check_overflow (ctx
)) {
3044 /*We must let the value be pushed, otherwise we would get an underflow error*/
3045 check_unverifiable_type (ctx
, ctx
->locals
[arg
]);
3046 if (ctx
->locals
[arg
]->byref
&& take_addr
)
3047 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
3049 set_stack_value (ctx
, stack_push (ctx
), ctx
->locals
[arg
], take_addr
);
3054 store_arg (VerifyContext
*ctx
, guint32 arg
)
3058 if (arg
>= ctx
->max_args
) {
3059 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
3060 if (check_underflow (ctx
, 1))
3065 if (check_underflow (ctx
, 1)) {
3066 value
= stack_pop (ctx
);
3067 if (!verify_stack_type_compatibility (ctx
, ctx
->params
[arg
], value
)) {
3068 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3071 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
))
3072 ctx
->has_this_store
= 1;
3076 store_local (VerifyContext
*ctx
, guint32 arg
)
3079 if (arg
>= ctx
->num_locals
) {
3080 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
3084 /*TODO verify definite assigment */
3085 if (check_underflow (ctx
, 1)) {
3086 value
= stack_pop(ctx
);
3087 if (!verify_stack_type_compatibility (ctx
, ctx
->locals
[arg
], value
)) {
3088 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
3089 stack_slot_get_name (value
),
3090 mono_type_get_stack_name (ctx
->locals
[arg
]),
3096 /*FIXME add and sub needs special care here*/
3098 do_binop (VerifyContext
*ctx
, unsigned int opcode
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
3100 ILStackDesc
*a
, *b
, *top
;
3101 int idxa
, idxb
, complexMerge
= 0;
3104 if (!check_underflow (ctx
, 2))
3106 b
= stack_pop (ctx
);
3107 a
= stack_pop (ctx
);
3109 idxa
= stack_slot_get_underlying_type (a
);
3110 if (stack_slot_is_managed_pointer (a
)) {
3115 idxb
= stack_slot_get_underlying_type (b
);
3116 if (stack_slot_is_managed_pointer (b
)) {
3123 res
= table
[idxa
][idxb
];
3125 VERIFIER_DEBUG ( printf ("binop res %d\n", res
); );
3126 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
3128 top
= stack_push (ctx
);
3129 if (res
== TYPE_INV
) {
3130 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
)));
3131 copy_stack_value (top
, a
);
3135 if (res
& NON_VERIFIABLE_RESULT
) {
3136 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
)));
3138 res
= res
& ~NON_VERIFIABLE_RESULT
;
3141 if (complexMerge
&& res
== TYPE_PTR
) {
3142 if (complexMerge
== 1)
3143 copy_stack_value (top
, a
);
3144 else if (complexMerge
== 2)
3145 copy_stack_value (top
, b
);
3147 * There is no need to merge the type of two pointers.
3148 * The only valid operation is subtraction, that returns a native
3149 * int as result and can be used with any 2 pointer kinds.
3150 * This is valid acording to Patition III 1.1.4
3159 do_boolean_branch_op (VerifyContext
*ctx
, int delta
)
3161 int target
= ctx
->ip_offset
+ delta
;
3164 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
3166 if (target
< 0 || target
>= ctx
->code_size
) {
3167 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx
->ip_offset
));
3171 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3173 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3176 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3180 ctx
->target
= target
;
3182 if (!check_underflow (ctx
, 1))
3185 top
= stack_pop (ctx
);
3186 if (!is_valid_bool_arg (top
))
3187 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
));
3189 check_unmanaged_pointer (ctx
, top
);
3193 stack_slot_is_complex_type_not_reference_type (ILStackDesc
*slot
)
3195 return stack_slot_get_type (slot
) == TYPE_COMPLEX
&& !MONO_TYPE_IS_REFERENCE (slot
->type
) && !stack_slot_is_boxed_value (slot
);
3199 do_branch_op (VerifyContext
*ctx
, signed int delta
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
3204 int target
= ctx
->ip_offset
+ delta
;
3206 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
3208 if (target
< 0 || target
>= ctx
->code_size
) {
3209 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
3213 switch (is_valid_cmp_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3214 case 1: /*FIXME use constants and not magic numbers.*/
3215 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3218 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3222 ctx
->target
= target
;
3224 if (!check_underflow (ctx
, 2))
3227 b
= stack_pop (ctx
);
3228 a
= stack_pop (ctx
);
3230 idxa
= stack_slot_get_underlying_type (a
);
3231 if (stack_slot_is_managed_pointer (a
))
3234 idxb
= stack_slot_get_underlying_type (b
);
3235 if (stack_slot_is_managed_pointer (b
))
3238 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3243 res
= table
[idxa
][idxb
];
3246 VERIFIER_DEBUG ( printf ("branch res %d\n", res
); );
3247 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
3249 if (res
== TYPE_INV
) {
3250 CODE_NOT_VERIFIABLE (ctx
,
3251 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
));
3252 } else if (res
& NON_VERIFIABLE_RESULT
) {
3253 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
));
3254 res
= res
& ~NON_VERIFIABLE_RESULT
;
3259 do_cmp_op (VerifyContext
*ctx
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
], guint32 opcode
)
3265 if (!check_underflow (ctx
, 2))
3267 b
= stack_pop (ctx
);
3268 a
= stack_pop (ctx
);
3270 if (opcode
== CEE_CGT_UN
) {
3271 if (stack_slot_get_type (a
) == TYPE_COMPLEX
&& stack_slot_get_type (b
) == TYPE_COMPLEX
) {
3272 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3277 idxa
= stack_slot_get_underlying_type (a
);
3278 if (stack_slot_is_managed_pointer (a
))
3281 idxb
= stack_slot_get_underlying_type (b
);
3282 if (stack_slot_is_managed_pointer (b
))
3285 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3290 res
= table
[idxa
][idxb
];
3293 if(res
== TYPE_INV
) {
3294 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
));
3295 } else if (res
& NON_VERIFIABLE_RESULT
) {
3296 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
));
3297 res
= res
& ~NON_VERIFIABLE_RESULT
;
3299 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3303 do_ret (VerifyContext
*ctx
)
3305 MonoType
*ret
= ctx
->signature
->ret
;
3306 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3307 if (ret
->type
!= MONO_TYPE_VOID
) {
3309 if (!check_underflow (ctx
, 1))
3312 top
= stack_pop(ctx
);
3314 if (!verify_stack_type_compatibility (ctx
, ctx
->signature
->ret
, top
)) {
3315 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx
->ip_offset
));
3319 if (ret
->byref
|| ret
->type
== MONO_TYPE_TYPEDBYREF
|| mono_type_is_value_type (ret
, "System", "ArgIterator") || mono_type_is_value_type (ret
, "System", "RuntimeArgumentHandle"))
3320 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx
->ip_offset
));
3323 if (ctx
->eval
.size
> 0) {
3324 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx
->eval
.size
, ctx
->ip_offset
));
3326 if (in_any_block (ctx
->header
, ctx
->ip_offset
))
3327 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx
->ip_offset
));
3331 * 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.
3332 * This is illegal but mono_get_method_full decoded it.
3333 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3336 do_invoke_method (VerifyContext
*ctx
, int method_token
, gboolean
virtual)
3339 MonoMethodSignature
*sig
;
3342 gboolean virt_check_this
= FALSE
;
3343 gboolean constrained
= ctx
->prefix_set
& PREFIX_CONSTRAINED
;
3345 if (!(method
= verifier_load_method (ctx
, method_token
, virtual ? "callvirt" : "call")))
3349 CLEAR_PREFIX (ctx
, PREFIX_CONSTRAINED
);
3351 if (method
->klass
->valuetype
) // && !constrained ???
3352 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx
->ip_offset
));
3354 if ((method
->flags
& METHOD_ATTRIBUTE_STATIC
))
3355 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx
->ip_offset
));
3358 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
3359 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx
->ip_offset
));
3361 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
)) {
3362 virt_check_this
= TRUE
;
3363 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_CALL_NONFINAL_VIRTUAL
;
3367 if (!(sig
= mono_method_get_signature_full (method
, ctx
->image
, method_token
, ctx
->generic_context
)))
3368 sig
= mono_method_get_signature (method
, ctx
->image
, method_token
);
3370 param_count
= sig
->param_count
+ sig
->hasthis
;
3371 if (!check_underflow (ctx
, param_count
))
3374 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3375 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i
); );
3376 value
= stack_pop (ctx
);
3377 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3378 char *stack_name
= stack_slot_full_name (value
);
3379 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3380 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
));
3381 g_free (stack_name
);
3385 if (stack_slot_is_managed_mutability_pointer (value
))
3386 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
));
3388 if ((ctx
->prefix_set
& PREFIX_TAIL
) && stack_slot_is_managed_pointer (value
)) {
3389 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
));
3395 MonoType
*type
= &method
->klass
->byval_arg
;
3398 if (mono_method_is_constructor (method
) && !method
->klass
->valuetype
) {
3399 if (!mono_method_is_constructor (ctx
->method
))
3400 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx
->ip_offset
));
3401 if (method
->klass
!= ctx
->method
->klass
->parent
&& method
->klass
!= ctx
->method
->klass
)
3402 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
));
3404 ctx
->super_ctor_called
= TRUE
;
3405 value
= stack_pop_safe (ctx
);
3406 if ((value
->stype
& THIS_POINTER_MASK
) != THIS_POINTER_MASK
)
3407 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx
->ip_offset
));
3409 value
= stack_pop (ctx
);
3412 copy_stack_value (©
, value
);
3413 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3414 //Other parts of the code suffer from the same issue of
3415 copy
.type
= mono_type_get_type_byval (copy
.type
);
3416 copy
.stype
&= ~POINTER_MASK
;
3418 if (virt_check_this
&& !stack_slot_is_this_pointer (value
) && !(method
->klass
->valuetype
|| stack_slot_is_boxed_value (value
)))
3419 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
));
3421 if (constrained
&& virtual) {
3422 if (!stack_slot_is_managed_pointer (value
))
3423 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx
->ip_offset
));
3424 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value
->type
), ctx
->constrained_type
, TRUE
))
3425 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx
->ip_offset
));
3426 copy
.stype
|= BOXED_MASK
;
3428 if (stack_slot_is_managed_pointer (value
) && !mono_class_from_mono_type (value
->type
)->valuetype
)
3429 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
));
3431 if (!virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !method
->klass
->valuetype
&& !stack_slot_is_boxed_value (value
))
3432 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx
->ip_offset
));
3434 if (virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !stack_slot_is_boxed_value (value
))
3435 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx
->ip_offset
));
3437 if (method
->klass
->valuetype
&& (stack_slot_is_boxed_value (value
) || !stack_slot_is_managed_pointer (value
)))
3438 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
));
3440 if (!verify_stack_type_compatibility (ctx
, type
, ©
))
3441 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx
->ip_offset
));
3443 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, mono_class_from_mono_type (value
->type
))) {
3444 char *name
= mono_method_full_name (method
, TRUE
);
3445 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3449 } else if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3450 char *name
= mono_method_full_name (method
, TRUE
);
3451 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3455 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3456 if (check_overflow (ctx
)) {
3457 value
= stack_push (ctx
);
3458 set_stack_value (ctx
, value
, sig
->ret
, FALSE
);
3459 if ((ctx
->prefix_set
& PREFIX_READONLY
) && method
->klass
->rank
&& !strcmp (method
->name
, "Address")) {
3460 ctx
->prefix_set
&= ~PREFIX_READONLY
;
3461 value
->stype
|= CMMP_MASK
;
3466 if ((ctx
->prefix_set
& PREFIX_TAIL
)) {
3467 if (!mono_delegate_ret_equal (mono_method_signature (ctx
->method
)->ret
, sig
->ret
))
3468 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx
->ip_offset
));
3469 if (ctx
->header
->code
[ctx
->ip_offset
+ 5] != CEE_RET
)
3470 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx
->ip_offset
));
3476 do_push_static_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3478 MonoClassField
*field
;
3480 if (!check_overflow (ctx
))
3483 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3485 if (!(field
= verifier_load_field (ctx
, token
, &klass
, take_addr
? "ldsflda" : "ldsfld")))
3488 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3489 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx
->ip_offset
));
3492 /*taking the address of initonly field only works from the static constructor */
3493 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3494 !(field
->parent
== ctx
->method
->klass
&& (ctx
->method
->flags
& (METHOD_ATTRIBUTE_SPECIAL_NAME
| METHOD_ATTRIBUTE_STATIC
)) && !strcmp (".cctor", ctx
->method
->name
)))
3495 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3497 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3498 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3500 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3504 do_store_static_field (VerifyContext
*ctx
, int token
) {
3505 MonoClassField
*field
;
3508 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3510 if (!check_underflow (ctx
, 1))
3513 value
= stack_pop (ctx
);
3515 if (!(field
= verifier_load_field (ctx
, token
, &klass
, "stsfld")))
3518 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3519 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx
->ip_offset
));
3523 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3524 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx
->ip_offset
));
3528 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3529 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3531 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3532 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
));
3536 check_is_valid_type_for_field_ops (VerifyContext
*ctx
, int token
, ILStackDesc
*obj
, MonoClassField
**ret_field
, const char *opcode
)
3538 MonoClassField
*field
;
3540 gboolean is_pointer
;
3542 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3543 if (!(field
= verifier_load_field (ctx
, token
, &klass
, opcode
)))
3547 //the value on stack is going to be used as a pointer
3548 is_pointer
= stack_slot_get_type (obj
) == TYPE_PTR
|| (stack_slot_get_type (obj
) == TYPE_NATIVE_INT
&& !get_stack_type (&field
->parent
->byval_arg
));
3550 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3551 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx
->ip_offset
));
3554 g_assert (obj
->type
);
3556 /*The value on the stack must be a subclass of the defining type of the field*/
3557 /* we need to check if we can load the field from the stack value*/
3559 if (stack_slot_get_underlying_type (obj
) == TYPE_NATIVE_INT
)
3560 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx
->ip_offset
));
3562 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3563 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3565 if (!field
->parent
->valuetype
&& stack_slot_is_managed_pointer (obj
))
3566 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
));
3568 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3569 if (field
->parent
->valuetype
&& stack_slot_is_boxed_value (obj
))
3570 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
));
3572 if (!stack_slot_is_null_literal (obj
) && !verify_stack_type_compatibility_full (ctx
, &field
->parent
->byval_arg
, obj
, TRUE
, FALSE
))
3573 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx
->ip_offset
));
3575 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, mono_class_from_mono_type (obj
->type
)))
3576 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3579 check_unmanaged_pointer (ctx
, obj
);
3584 do_push_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3587 MonoClassField
*field
;
3590 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3592 if (!check_underflow (ctx
, 1))
3594 obj
= stack_pop_safe (ctx
);
3596 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, take_addr
? "ldflda" : "ldfld"))
3599 if (take_addr
&& field
->parent
->valuetype
&& !stack_slot_is_managed_pointer (obj
))
3600 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx
->ip_offset
));
3602 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3603 !(field
->parent
== ctx
->method
->klass
&& mono_method_is_constructor (ctx
->method
)))
3604 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3606 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3610 do_store_field (VerifyContext
*ctx
, int token
)
3612 ILStackDesc
*value
, *obj
;
3613 MonoClassField
*field
;
3614 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3616 if (!check_underflow (ctx
, 2))
3619 value
= stack_pop (ctx
);
3620 obj
= stack_pop_safe (ctx
);
3622 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, "stfld"))
3625 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3626 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3629 /*TODO proper handle for Nullable<T>*/
3631 do_box_value (VerifyContext
*ctx
, int klass_token
)
3634 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "box");
3640 if (!check_underflow (ctx
, 1))
3643 value
= stack_pop (ctx
);
3644 /*box is a nop for reference types*/
3646 if (stack_slot_get_underlying_type (value
) == TYPE_COMPLEX
&& MONO_TYPE_IS_REFERENCE (value
->type
) && MONO_TYPE_IS_REFERENCE (type
)) {
3647 stack_push_stack_val (ctx
, value
)->stype
|= BOXED_MASK
;
3652 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3653 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx
->ip_offset
));
3655 klass
= mono_class_from_mono_type (type
);
3656 if (mono_class_is_nullable (klass
))
3657 type
= &mono_class_get_nullable_param (klass
)->byval_arg
;
3658 stack_push_val (ctx
, TYPE_COMPLEX
| BOXED_MASK
, type
);
3662 do_unbox_value (VerifyContext
*ctx
, int klass_token
)
3665 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox");
3670 if (!check_underflow (ctx
, 1))
3673 if (!mono_class_from_mono_type (type
)->valuetype
)
3674 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx
->ip_offset
));
3676 value
= stack_pop (ctx
);
3678 /*Value should be: a boxed valuetype or a reference type*/
3679 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3680 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3681 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
));
3683 set_stack_value (ctx
, value
= stack_push (ctx
), mono_type_get_type_byref (type
), FALSE
);
3684 value
->stype
|= CMMP_MASK
;
3688 do_unbox_any (VerifyContext
*ctx
, int klass_token
)
3691 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox.any");
3696 if (!check_underflow (ctx
, 1))
3699 value
= stack_pop (ctx
);
3701 /*Value should be: a boxed valuetype or a reference type*/
3702 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3703 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3704 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
));
3706 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3710 do_unary_math_op (VerifyContext
*ctx
, int op
)
3713 if (!check_underflow (ctx
, 1))
3715 value
= stack_pop (ctx
);
3716 switch (stack_slot_get_type (value
)) {
3719 case TYPE_NATIVE_INT
:
3724 case TYPE_COMPLEX
: /*only enums are ok*/
3725 if (mono_type_is_enum_type (value
->type
))
3728 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx
->ip_offset
));
3730 stack_push_stack_val (ctx
, value
);
3734 do_conversion (VerifyContext
*ctx
, int kind
)
3737 if (!check_underflow (ctx
, 1))
3739 value
= stack_pop (ctx
);
3741 switch (stack_slot_get_type (value
)) {
3744 case TYPE_NATIVE_INT
:
3748 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
));
3753 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3756 stack_push_val (ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
3759 stack_push_val (ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
3761 case TYPE_NATIVE_INT
:
3762 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3765 g_error ("unknown type %02x in conversion", kind
);
3771 do_load_token (VerifyContext
*ctx
, int token
)
3774 MonoClass
*handle_class
;
3775 if (!check_overflow (ctx
))
3777 handle
= mono_ldtoken (ctx
->image
, token
, &handle_class
, ctx
->generic_context
);
3779 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token
, ctx
->ip_offset
));
3782 if (handle_class
== mono_defaults
.typehandle_class
) {
3783 mono_type_is_valid_in_context (ctx
, (MonoType
*)handle
);
3784 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
3785 mono_method_is_valid_in_context (ctx
, (MonoMethod
*)handle
);
3786 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
3787 mono_type_is_valid_in_context (ctx
, &((MonoClassField
*)handle
)->parent
->byval_arg
);
3789 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
3791 stack_push_val (ctx
, TYPE_COMPLEX
, mono_class_get_type (handle_class
));
3795 do_ldobj_value (VerifyContext
*ctx
, int token
)
3798 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "ldobj");
3799 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3804 if (!check_underflow (ctx
, 1))
3807 value
= stack_pop (ctx
);
3808 if (!stack_slot_is_managed_pointer (value
)
3809 && stack_slot_get_type (value
) != TYPE_NATIVE_INT
3810 && !(stack_slot_get_type (value
) == TYPE_PTR
&& value
->type
->type
!= MONO_TYPE_FNPTR
)) {
3811 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3815 if (stack_slot_get_type (value
) == TYPE_NATIVE_INT
)
3816 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx
->ip_offset
));
3818 /*We have a byval on the stack, but the comparison must be strict. */
3819 if (!verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (value
->type
), TRUE
))
3820 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx
->ip_offset
));
3822 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3826 do_stobj (VerifyContext
*ctx
, int token
)
3828 ILStackDesc
*dest
, *src
;
3829 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "stobj");
3830 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3835 if (!check_underflow (ctx
, 2))
3838 src
= stack_pop (ctx
);
3839 dest
= stack_pop (ctx
);
3841 if (stack_slot_is_managed_mutability_pointer (dest
))
3842 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx
->ip_offset
));
3844 if (!stack_slot_is_managed_pointer (dest
))
3845 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx
->ip_offset
));
3847 if (stack_slot_is_boxed_value (src
) && !MONO_TYPE_IS_REFERENCE (src
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3848 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
));
3850 if (!verify_stack_type_compatibility (ctx
, type
, src
))
3851 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3853 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3854 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3858 do_cpobj (VerifyContext
*ctx
, int token
)
3860 ILStackDesc
*dest
, *src
;
3861 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "cpobj");
3865 if (!check_underflow (ctx
, 2))
3868 src
= stack_pop (ctx
);
3869 dest
= stack_pop (ctx
);
3871 if (!stack_slot_is_managed_pointer (src
))
3872 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx
->ip_offset
));
3874 if (!stack_slot_is_managed_pointer (dest
))
3875 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx
->ip_offset
));
3877 if (stack_slot_is_managed_mutability_pointer (dest
))
3878 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx
->ip_offset
));
3880 if (!verify_type_compatibility (ctx
, type
, mono_type_get_type_byval (src
->type
)))
3881 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3883 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3884 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3888 do_initobj (VerifyContext
*ctx
, int token
)
3891 MonoType
*stack
, *type
= get_boxable_mono_type (ctx
, token
, "initobj");
3895 if (!check_underflow (ctx
, 1))
3898 obj
= stack_pop (ctx
);
3900 if (!stack_slot_is_managed_pointer (obj
))
3901 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx
->ip_offset
));
3903 if (stack_slot_is_managed_mutability_pointer (obj
))
3904 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx
->ip_offset
));
3906 stack
= mono_type_get_type_byval (obj
->type
);
3907 if (MONO_TYPE_IS_REFERENCE (stack
)) {
3908 if (!verify_type_compatibility (ctx
, stack
, type
))
3909 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3910 else if (IS_STRICT_MODE (ctx
) && !mono_metadata_type_equal (type
, stack
))
3911 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3912 } else if (!verify_type_compatibility (ctx
, stack
, type
)) {
3913 char *expected_name
= mono_type_full_name (type
);
3914 char *stack_name
= mono_type_full_name (stack
);
3916 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
));
3917 g_free (expected_name
);
3918 g_free (stack_name
);
3923 do_newobj (VerifyContext
*ctx
, int token
)
3927 MonoMethodSignature
*sig
;
3929 gboolean is_delegate
= FALSE
;
3931 if (!(method
= verifier_load_method (ctx
, token
, "newobj")))
3934 if (!mono_method_is_constructor (method
)) {
3935 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token
, ctx
->ip_offset
));
3939 if (method
->klass
->flags
& (TYPE_ATTRIBUTE_ABSTRACT
| TYPE_ATTRIBUTE_INTERFACE
))
3940 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx
->ip_offset
));
3942 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3943 char *from
= mono_method_full_name (ctx
->method
, TRUE
);
3944 char *to
= mono_method_full_name (method
, TRUE
);
3945 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
);
3950 //FIXME use mono_method_get_signature_full
3951 sig
= mono_method_signature (method
);
3952 if (!check_underflow (ctx
, sig
->param_count
))
3955 is_delegate
= method
->klass
->parent
== mono_defaults
.multicastdelegate_class
;
3958 ILStackDesc
*funptr
;
3959 //first arg is object, second arg is fun ptr
3960 if (sig
->param_count
!= 2) {
3961 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx
->ip_offset
));
3964 funptr
= stack_pop (ctx
);
3965 value
= stack_pop (ctx
);
3966 verify_delegate_compatibility (ctx
, method
->klass
, value
, funptr
);
3968 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3969 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i
); );
3970 value
= stack_pop (ctx
);
3971 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3972 char *stack_name
= stack_slot_full_name (value
);
3973 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3974 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
));
3975 g_free (stack_name
);
3979 if (stack_slot_is_managed_mutability_pointer (value
))
3980 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx
->ip_offset
));
3984 if (check_overflow (ctx
))
3985 set_stack_value (ctx
, stack_push (ctx
), &method
->klass
->byval_arg
, FALSE
);
3989 do_cast (VerifyContext
*ctx
, int token
, const char *opcode
) {
3995 if (!check_underflow (ctx
, 1))
3998 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
4002 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid %s type at 0x%04x", opcode
, ctx
->ip_offset
));
4006 value
= stack_pop (ctx
);
4007 is_boxed
= stack_slot_is_boxed_value (value
);
4009 if (stack_slot_is_managed_pointer (value
))
4010 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
4011 else if (!MONO_TYPE_IS_REFERENCE (value
->type
) && !is_boxed
) {
4012 char *name
= stack_slot_full_name (value
);
4013 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
));
4017 switch (value
->type
->type
) {
4018 case MONO_TYPE_FNPTR
:
4020 case MONO_TYPE_TYPEDBYREF
:
4021 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
4024 do_box
= is_boxed
|| mono_type_is_generic_argument(type
) || mono_class_from_mono_type (type
)->valuetype
;
4025 stack_push_val (ctx
, TYPE_COMPLEX
| (do_box
? BOXED_MASK
: 0), type
);
4029 mono_type_from_opcode (int opcode
) {
4037 return &mono_defaults
.sbyte_class
->byval_arg
;
4045 return &mono_defaults
.int16_class
->byval_arg
;
4053 return &mono_defaults
.int32_class
->byval_arg
;
4059 return &mono_defaults
.int64_class
->byval_arg
;
4065 return &mono_defaults
.single_class
->byval_arg
;
4071 return &mono_defaults
.double_class
->byval_arg
;
4077 return &mono_defaults
.int_class
->byval_arg
;
4081 case CEE_LDELEM_REF
:
4082 case CEE_STELEM_REF
:
4083 return &mono_defaults
.object_class
->byval_arg
;
4086 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode
);
4092 do_load_indirect (VerifyContext
*ctx
, int opcode
)
4095 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
4097 if (!check_underflow (ctx
, 1))
4100 value
= stack_pop (ctx
);
4101 if (!stack_slot_is_managed_pointer (value
)) {
4102 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx
->ip_offset
));
4103 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
4107 if (opcode
== CEE_LDIND_REF
) {
4108 if (stack_slot_get_underlying_type (value
) != TYPE_COMPLEX
|| mono_class_from_mono_type (value
->type
)->valuetype
)
4109 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx
->ip_offset
));
4110 set_stack_value (ctx
, stack_push (ctx
), mono_type_get_type_byval (value
->type
), FALSE
);
4112 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (value
->type
), TRUE
))
4113 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4114 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
4119 do_store_indirect (VerifyContext
*ctx
, int opcode
)
4121 ILStackDesc
*addr
, *val
;
4122 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
4124 if (!check_underflow (ctx
, 2))
4127 val
= stack_pop (ctx
);
4128 addr
= stack_pop (ctx
);
4130 check_unmanaged_pointer (ctx
, addr
);
4132 if (!stack_slot_is_managed_pointer (addr
) && stack_slot_get_type (addr
) != TYPE_PTR
) {
4133 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx
->ip_offset
));
4137 if (stack_slot_is_managed_mutability_pointer (addr
)) {
4138 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx
->ip_offset
));
4142 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (addr
->type
), TRUE
))
4143 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4145 if (!verify_stack_type_compatibility (ctx
, mono_type_from_opcode (opcode
), val
))
4146 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4150 do_newarr (VerifyContext
*ctx
, int token
)
4153 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "newarr");
4158 if (!check_underflow (ctx
, 1))
4161 value
= stack_pop (ctx
);
4162 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4163 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
));
4165 set_stack_value (ctx
, stack_push (ctx
), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type
), 1)), FALSE
);
4168 /*FIXME handle arrays that are not 0-indexed*/
4170 do_ldlen (VerifyContext
*ctx
)
4174 if (!check_underflow (ctx
, 1))
4177 value
= stack_pop (ctx
);
4179 if (stack_slot_get_type (value
) != TYPE_COMPLEX
|| value
->type
->type
!= MONO_TYPE_SZARRAY
)
4180 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx
->ip_offset
));
4182 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
4185 /*FIXME handle arrays that are not 0-indexed*/
4186 /*FIXME handle readonly prefix and CMMP*/
4188 do_ldelema (VerifyContext
*ctx
, int klass_token
)
4190 ILStackDesc
*index
, *array
, *res
;
4191 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "ldelema");
4197 if (!check_underflow (ctx
, 2))
4200 index
= stack_pop (ctx
);
4201 array
= stack_pop (ctx
);
4203 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4204 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
));
4206 if (!stack_slot_is_null_literal (array
)) {
4207 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4208 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array
), ctx
->ip_offset
));
4210 if (get_stack_type (type
) == TYPE_I4
|| get_stack_type (type
) == TYPE_NATIVE_INT
) {
4211 valid
= verify_type_compatibility_full (ctx
, type
, &array
->type
->data
.klass
->byval_arg
, TRUE
);
4213 valid
= mono_metadata_type_equal (type
, &array
->type
->data
.klass
->byval_arg
);
4216 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx
->ip_offset
));
4220 res
= stack_push (ctx
);
4221 set_stack_value (ctx
, res
, type
, TRUE
);
4222 if (ctx
->prefix_set
& PREFIX_READONLY
) {
4223 ctx
->prefix_set
&= ~PREFIX_READONLY
;
4224 res
->stype
|= CMMP_MASK
;
4229 * FIXME handle arrays that are not 0-indexed
4230 * FIXME handle readonly prefix and CMMP
4233 do_ldelem (VerifyContext
*ctx
, int opcode
, int token
)
4235 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4236 ILStackDesc
*index
, *array
;
4238 if (!check_underflow (ctx
, 2))
4241 if (opcode
== CEE_LDELEM
) {
4242 if (!(type
= verifier_load_type (ctx
, token
, "ldelem.any"))) {
4243 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4247 type
= mono_type_from_opcode (opcode
);
4250 index
= stack_pop (ctx
);
4251 array
= stack_pop (ctx
);
4253 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4254 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
));
4256 if (!stack_slot_is_null_literal (array
)) {
4257 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4258 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
));
4260 if (opcode
== CEE_LDELEM_REF
) {
4261 if (array
->type
->data
.klass
->valuetype
)
4262 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx
->ip_offset
));
4263 type
= &array
->type
->data
.klass
->byval_arg
;
4265 MonoType
*candidate
= &array
->type
->data
.klass
->byval_arg
;
4266 if (IS_STRICT_MODE (ctx
)) {
4267 MonoType
*underlying_type
= mono_type_get_underlying_type_any (type
);
4268 MonoType
*underlying_candidate
= mono_type_get_underlying_type_any (candidate
);
4269 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
)) ||
4270 (IS_ONE_OF2 (underlying_candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
) && IS_ONE_OF2 (underlying_type
->type
, MONO_TYPE_I
, MONO_TYPE_U
)))
4271 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4273 if (!verify_type_compatibility_full (ctx
, type
, candidate
, TRUE
))
4274 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4279 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
4284 * FIXME handle arrays that are not 0-indexed
4287 do_stelem (VerifyContext
*ctx
, int opcode
, int token
)
4289 ILStackDesc
*index
, *array
, *value
;
4291 if (!check_underflow (ctx
, 3))
4294 if (opcode
== CEE_STELEM
) {
4295 if (!(type
= verifier_load_type (ctx
, token
, "stelem.any"))) {
4296 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4300 type
= mono_type_from_opcode (opcode
);
4303 value
= stack_pop (ctx
);
4304 index
= stack_pop (ctx
);
4305 array
= stack_pop (ctx
);
4307 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4308 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
));
4310 if (!stack_slot_is_null_literal (array
)) {
4311 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
) {
4312 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
));
4314 if (opcode
== CEE_STELEM_REF
) {
4315 if (array
->type
->data
.klass
->valuetype
)
4316 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4317 } else if (!verify_type_compatibility_full (ctx
, &array
->type
->data
.klass
->byval_arg
, type
, TRUE
)) {
4318 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4322 if (opcode
== CEE_STELEM_REF
) {
4323 if (!stack_slot_is_boxed_value (value
) && mono_class_from_mono_type (value
->type
)->valuetype
)
4324 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4325 } else if (opcode
!= CEE_STELEM_REF
) {
4326 if (!verify_stack_type_compatibility (ctx
, type
, value
))
4327 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4329 if (stack_slot_is_boxed_value (value
) && !MONO_TYPE_IS_REFERENCE (value
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
4330 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
));
4336 do_throw (VerifyContext
*ctx
)
4338 ILStackDesc
*exception
;
4339 if (!check_underflow (ctx
, 1))
4341 exception
= stack_pop (ctx
);
4343 if (!stack_slot_is_null_literal (exception
) && !(stack_slot_get_type (exception
) == TYPE_COMPLEX
&& !mono_class_from_mono_type (exception
->type
)->valuetype
))
4344 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx
->ip_offset
));
4346 if (mono_type_is_generic_argument (exception
->type
) && !stack_slot_is_boxed_value (exception
)) {
4347 char *name
= mono_type_full_name (exception
->type
);
4348 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
));
4351 /*The stack is left empty after a throw*/
4357 do_endfilter (VerifyContext
*ctx
)
4359 MonoExceptionClause
*clause
;
4361 if (IS_STRICT_MODE (ctx
)) {
4362 if (ctx
->eval
.size
!= 1)
4363 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx
->ip_offset
));
4365 if (ctx
->eval
.size
>= 1 && stack_slot_get_type (stack_pop (ctx
)) != TYPE_I4
)
4366 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx
->ip_offset
));
4369 if ((clause
= is_correct_endfilter (ctx
, ctx
->ip_offset
))) {
4370 if (IS_STRICT_MODE (ctx
)) {
4371 if (ctx
->ip_offset
!= clause
->handler_offset
- 2)
4372 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4374 if ((ctx
->ip_offset
!= clause
->handler_offset
- 2) && !MONO_OFFSET_IN_HANDLER (clause
, ctx
->ip_offset
))
4375 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4378 if (IS_STRICT_MODE (ctx
) && !is_unverifiable_endfilter (ctx
, ctx
->ip_offset
))
4379 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4381 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4388 do_leave (VerifyContext
*ctx
, int delta
)
4390 int target
= ((gint32
)ctx
->ip_offset
) + delta
;
4391 if (target
>= ctx
->code_size
|| target
< 0)
4392 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
4394 if (!is_correct_leave (ctx
->header
, ctx
->ip_offset
, target
))
4395 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx
->ip_offset
));
4402 * Verify br and br.s opcodes.
4405 do_static_branch (VerifyContext
*ctx
, int delta
)
4407 int target
= ctx
->ip_offset
+ delta
;
4408 if (target
< 0 || target
>= ctx
->code_size
) {
4409 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("branch target out of code at 0x%04x", ctx
->ip_offset
));
4413 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4415 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4418 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4422 ctx
->target
= target
;
4426 do_switch (VerifyContext
*ctx
, int count
, const unsigned char *data
)
4428 int i
, base
= ctx
->ip_offset
+ 5 + count
* 4;
4431 if (!check_underflow (ctx
, 1))
4434 value
= stack_pop (ctx
);
4436 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4437 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx
->ip_offset
));
4439 for (i
= 0; i
< count
; ++i
) {
4440 int target
= base
+ read32 (data
+ i
* 4);
4442 if (target
< 0 || target
>= ctx
->code_size
) {
4443 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x out of code at 0x%04x", i
, ctx
->ip_offset
));
4447 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4449 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4452 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4455 merge_stacks (ctx
, &ctx
->eval
, &ctx
->code
[target
], FALSE
, TRUE
);
4460 do_load_function_ptr (VerifyContext
*ctx
, guint32 token
, gboolean
virtual)
4465 if (virtual && !check_underflow (ctx
, 1))
4468 if (!virtual && !check_overflow (ctx
))
4471 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4472 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4476 if (!(method
= verifier_load_method (ctx
, token
, virtual ? "ldvirtfrn" : "ldftn")))
4479 if (mono_method_is_constructor (method
))
4480 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx
->ip_offset
));
4483 ILStackDesc
*top
= stack_pop (ctx
);
4485 if (stack_slot_get_type (top
) != TYPE_COMPLEX
|| top
->type
->type
== MONO_TYPE_VALUETYPE
)
4486 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx
->ip_offset
));
4488 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
)
4489 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx
->ip_offset
));
4491 if (!verify_stack_type_compatibility (ctx
, &method
->klass
->byval_arg
, top
))
4492 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx
->ip_offset
));
4495 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
))
4496 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
);
4498 top
= stack_push_val(ctx
, TYPE_PTR
, mono_type_create_fnptr_from_mono_method (ctx
, method
));
4499 top
->method
= method
;
4503 do_sizeof (VerifyContext
*ctx
, int token
)
4507 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4508 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4512 if (!(type
= verifier_load_type (ctx
, token
, "sizeof")))
4515 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
4516 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx
->ip_offset
));
4520 if (type
->type
== MONO_TYPE_VOID
) {
4521 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx
->ip_offset
));
4525 if (check_overflow (ctx
))
4526 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.uint32_class
->byval_arg
, FALSE
);
4529 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4531 do_localloc (VerifyContext
*ctx
)
4535 if (ctx
->eval
.size
!= 1) {
4536 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4540 if (in_any_exception_block (ctx
->header
, ctx
->ip_offset
)) {
4541 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4545 /*TODO verify top type*/
4546 top
= stack_pop (ctx
);
4548 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.int_class
->byval_arg
, FALSE
);
4549 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx
->ip_offset
));
4553 do_ldstr (VerifyContext
*ctx
, guint32 token
)
4555 if (mono_metadata_token_code (token
) != MONO_TOKEN_STRING
) {
4556 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4560 if (!ctx
->image
->dynamic
&& mono_metadata_token_index (token
) >= ctx
->image
->heap_us
.size
) {
4561 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string index %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4565 if (check_overflow (ctx
))
4566 stack_push_val (ctx
, TYPE_COMPLEX
, &mono_defaults
.string_class
->byval_arg
);
4570 do_refanyval (VerifyContext
*ctx
, int token
)
4574 if (!check_underflow (ctx
, 1))
4577 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4580 top
= stack_pop (ctx
);
4582 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4583 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
));
4585 set_stack_value (ctx
, stack_push (ctx
), type
, TRUE
);
4589 do_refanytype (VerifyContext
*ctx
)
4593 if (!check_underflow (ctx
, 1))
4596 top
= stack_pop (ctx
);
4598 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4599 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
));
4601 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typehandle_class
->byval_arg
, FALSE
);
4606 do_mkrefany (VerifyContext
*ctx
, int token
)
4610 if (!check_underflow (ctx
, 1))
4613 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4616 top
= stack_pop (ctx
);
4618 if (stack_slot_is_managed_mutability_pointer (top
))
4619 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx
->ip_offset
));
4621 if (!stack_slot_is_managed_pointer (top
)) {
4622 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
));
4624 MonoType
*stack_type
= mono_type_get_type_byval (top
->type
);
4625 if (MONO_TYPE_IS_REFERENCE (type
) && !mono_metadata_type_equal (type
, stack_type
))
4626 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4628 if (!MONO_TYPE_IS_REFERENCE (type
) && !verify_type_compatibility_full (ctx
, type
, stack_type
, TRUE
))
4629 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4632 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typed_reference_class
->byval_arg
, FALSE
);
4636 do_ckfinite (VerifyContext
*ctx
)
4639 if (!check_underflow (ctx
, 1))
4642 top
= stack_pop (ctx
);
4644 if (stack_slot_get_underlying_type (top
) != TYPE_R8
)
4645 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
));
4646 stack_push_stack_val (ctx
, top
);
4650 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4652 * @from holds new values for a given control path
4653 * @to holds the current values of a given control path
4655 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4658 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
)
4661 stack_init (ctx
, to
);
4664 if (to
->flags
== IL_CODE_FLAG_NOT_PROCESSED
)
4667 stack_copy (&ctx
->eval
, to
);
4669 } else if (!(to
->flags
& IL_CODE_STACK_MERGED
)) {
4670 stack_copy (to
, &ctx
->eval
);
4673 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from
->size
, to
->size
); );
4675 if (from
->size
!= to
->size
) {
4676 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from
->size
, to
->size
, ctx
->ip_offset
); );
4677 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
));
4681 //FIXME we need to preserve CMMP attributes
4682 //FIXME we must take null literals into consideration.
4683 for (i
= 0; i
< from
->size
; ++i
) {
4684 ILStackDesc
*new_slot
= from
->stack
+ i
;
4685 ILStackDesc
*old_slot
= to
->stack
+ i
;
4686 MonoType
*new_type
= mono_type_from_stack_slot (new_slot
);
4687 MonoType
*old_type
= mono_type_from_stack_slot (old_slot
);
4688 MonoClass
*old_class
= mono_class_from_mono_type (old_type
);
4689 MonoClass
*new_class
= mono_class_from_mono_type (new_type
);
4690 MonoClass
*match_class
= NULL
;
4692 // S := T then U = S (new value is compatible with current value, keep current)
4693 if (verify_stack_type_compatibility (ctx
, old_type
, new_slot
)) {
4694 copy_stack_value (new_slot
, old_slot
);
4698 // T := S then U = T (old value is compatible with current value, use new)
4699 if (verify_stack_type_compatibility (ctx
, new_type
, old_slot
)) {
4700 copy_stack_value (old_slot
, new_slot
);
4704 if (mono_type_is_generic_argument (old_type
) || mono_type_is_generic_argument (new_type
)) {
4705 char *old_name
= stack_slot_full_name (old_slot
);
4706 char *new_name
= stack_slot_full_name (new_slot
);
4707 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
));
4713 //both are reference types, use closest common super type
4714 if (!mono_class_from_mono_type (old_type
)->valuetype
4715 && !mono_class_from_mono_type (new_type
)->valuetype
4716 && !stack_slot_is_managed_pointer (old_slot
)
4717 && !stack_slot_is_managed_pointer (new_slot
)) {
4719 for (j
= MIN (old_class
->idepth
, new_class
->idepth
) - 1; j
> 0; --j
) {
4720 if (mono_metadata_type_equal (&old_class
->supertypes
[j
]->byval_arg
, &new_class
->supertypes
[j
]->byval_arg
)) {
4721 match_class
= old_class
->supertypes
[j
];
4726 mono_class_setup_interfaces (old_class
);
4727 for (j
= 0; j
< old_class
->interface_count
; ++j
) {
4728 for (k
= 0; k
< new_class
->interface_count
; ++k
) {
4729 if (mono_metadata_type_equal (&old_class
->interfaces
[j
]->byval_arg
, &new_class
->interfaces
[k
]->byval_arg
)) {
4730 match_class
= old_class
->interfaces
[j
];
4736 //No decent super type found, use object
4737 match_class
= mono_defaults
.object_class
;
4739 } 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
)) {
4740 match_class
= mono_defaults
.object_class
;
4745 char *old_name
= stack_slot_full_name (old_slot
);
4746 char *new_name
= stack_slot_full_name (new_slot
);
4747 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
));
4751 set_stack_value (ctx
, old_slot
, &new_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4755 g_assert (match_class
);
4756 set_stack_value (ctx
, old_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4757 set_stack_value (ctx
, new_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4763 to
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
4764 to
->flags
|= IL_CODE_STACK_MERGED
;
4767 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4768 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4771 * is_clause_in_range :
4773 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4776 is_clause_in_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4778 if (clause
->try_offset
>= start
&& clause
->try_offset
< end
)
4780 if (HANDLER_START (clause
) >= start
&& HANDLER_START (clause
) < end
)
4786 * is_clause_inside_range :
4788 * Returns TRUE if @clause lies completely inside the @start - @end range.
4791 is_clause_inside_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4793 if (clause
->try_offset
< start
|| (clause
->try_offset
+ clause
->try_len
) > end
)
4795 if (HANDLER_START (clause
) < start
|| (clause
->handler_offset
+ clause
->handler_len
) > end
)
4801 * is_clause_nested :
4803 * Returns TRUE if @nested is nested in @clause.
4806 is_clause_nested (MonoExceptionClause
*clause
, MonoExceptionClause
*nested
)
4808 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (nested
, clause
->data
.filter_offset
, clause
->handler_offset
))
4810 return is_clause_inside_range (nested
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4811 is_clause_inside_range (nested
, clause
->handler_offset
, clause
->handler_offset
+ clause
->handler_len
);
4814 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4815 * the each pair of exception must have the following properties:
4816 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4817 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4818 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4821 verify_clause_relationship (VerifyContext
*ctx
, MonoExceptionClause
*clause
, MonoExceptionClause
*to_test
)
4823 /*clause is nested*/
4824 if (to_test
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (clause
, to_test
->data
.filter_offset
, to_test
->handler_offset
)) {
4825 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clause inside filter"));
4829 /*wrong nesting order.*/
4830 if (is_clause_nested (clause
, to_test
)) {
4831 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4835 /*mutual protection*/
4836 if (clause
->try_offset
== to_test
->try_offset
&& clause
->try_len
== to_test
->try_len
) {
4837 /*handlers are not disjoint*/
4838 if (is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) {
4839 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception handlers overlap"));
4842 /* handlers are not catch or filter */
4843 if (!IS_CATCH_OR_FILTER (clause
) || !IS_CATCH_OR_FILTER (to_test
)) {
4844 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4851 /*not completelly disjoint*/
4852 if ((is_clause_in_range (to_test
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4853 is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) && !is_clause_nested (to_test
, clause
))
4854 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses overlap"));
4857 #define code_bounds_check(size) \
4858 if (ip + size > end) {\
4859 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4864 * FIXME: need to distinguish between valid and verifiable.
4865 * Need to keep track of types on the stack.
4866 * Verify types for opcodes.
4869 mono_method_verify (MonoMethod
*method
, int level
)
4872 const unsigned char *ip
;
4873 const unsigned char *end
;
4874 int i
, n
, need_merge
= 0, start
= 0;
4875 guint token
, ip_offset
= 0, prefix
= 0;
4876 MonoGenericContext
*generic_context
= NULL
;
4880 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method
->klass
->name_space
, method
->klass
->name
, method
->name
); );
4882 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4883 (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))) {
4887 memset (&ctx
, 0, sizeof (VerifyContext
));
4889 //FIXME use mono_method_get_signature_full
4890 ctx
.signature
= mono_method_signature (method
);
4891 if (!ctx
.signature
) {
4892 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method signature"));
4895 ctx
.header
= mono_method_get_header (method
);
4897 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method header"));
4900 ctx
.method
= method
;
4901 ip
= ctx
.header
->code
;
4902 end
= ip
+ ctx
.header
->code_size
;
4903 ctx
.image
= image
= method
->klass
->image
;
4906 ctx
.max_args
= ctx
.signature
->param_count
+ ctx
.signature
->hasthis
;
4907 ctx
.max_stack
= ctx
.header
->max_stack
;
4908 ctx
.verifiable
= ctx
.valid
= 1;
4911 ctx
.code
= g_new (ILCodeDesc
, ctx
.header
->code_size
);
4912 ctx
.code_size
= ctx
.header
->code_size
;
4914 memset(ctx
.code
, 0, sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4917 ctx
.num_locals
= ctx
.header
->num_locals
;
4918 ctx
.locals
= g_memdup (ctx
.header
->locals
, sizeof (MonoType
*) * ctx
.header
->num_locals
);
4920 if (ctx
.num_locals
> 0 && !ctx
.header
->init_locals
)
4921 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Method with locals variable but without init locals set"));
4923 ctx
.params
= g_new (MonoType
*, ctx
.max_args
);
4924 if (ctx
.signature
->hasthis
)
4925 ctx
.params
[0] = method
->klass
->valuetype
? &method
->klass
->this_arg
: &method
->klass
->byval_arg
;
4926 memcpy (ctx
.params
+ ctx
.signature
->hasthis
, ctx
.signature
->params
, sizeof (MonoType
*) * ctx
.signature
->param_count
);
4928 if (ctx
.signature
->is_inflated
)
4929 ctx
.generic_context
= generic_context
= mono_method_get_context (method
);
4931 if (!generic_context
&& (method
->klass
->generic_container
|| method
->is_generic
)) {
4932 if (method
->is_generic
)
4933 ctx
.generic_context
= generic_context
= &(mono_method_get_generic_container (method
)->context
);
4935 ctx
.generic_context
= generic_context
= &method
->klass
->generic_container
->context
;
4938 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4939 MonoType
*uninflated
= ctx
.locals
[i
];
4940 ctx
.locals
[i
] = mono_class_inflate_generic_type_checked (ctx
.locals
[i
], ctx
.generic_context
, &error
);
4941 if (!mono_error_ok (&error
)) {
4942 char *name
= mono_type_full_name (ctx
.locals
[i
] ? ctx
.locals
[i
] : uninflated
);
4943 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %d of type %s", i
, name
));
4945 mono_error_cleanup (&error
);
4946 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4952 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4953 MonoType
*uninflated
= ctx
.params
[i
];
4954 ctx
.params
[i
] = mono_class_inflate_generic_type_checked (ctx
.params
[i
], ctx
.generic_context
, &error
);
4955 if (!mono_error_ok (&error
)) {
4956 char *name
= mono_type_full_name (ctx
.params
[i
] ? ctx
.params
[i
] : uninflated
);
4957 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %d of type %s", i
, name
));
4959 mono_error_cleanup (&error
);
4960 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4965 stack_init (&ctx
, &ctx
.eval
);
4967 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4968 if (!mono_type_is_valid_in_context (&ctx
, ctx
.locals
[i
]))
4972 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4973 if (!mono_type_is_valid_in_context (&ctx
, ctx
.params
[i
]))
4980 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
4981 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
4982 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
); );
4984 if (clause
->try_offset
> ctx
.code_size
|| clause
->try_offset
+ clause
->try_len
> ctx
.code_size
)
4985 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause out of bounds at 0x%04x", clause
->try_offset
));
4987 if (clause
->try_len
<= 0)
4988 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
4990 if (clause
->handler_offset
> ctx
.code_size
|| clause
->handler_offset
+ clause
->handler_len
> ctx
.code_size
)
4991 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause
->try_offset
));
4993 if (clause
->handler_len
<= 0)
4994 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
4996 if (clause
->try_offset
< clause
->handler_offset
&& clause
->try_offset
+ clause
->try_len
> HANDLER_START (clause
))
4997 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause
->try_offset
, clause
->handler_offset
));
4999 for (n
= i
+ 1; n
< ctx
.header
->num_clauses
&& ctx
.valid
; ++n
)
5000 verify_clause_relationship (&ctx
, clause
, ctx
.header
->clauses
+ n
);
5005 ctx
.code
[clause
->try_offset
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
5006 if (clause
->try_offset
+ clause
->try_len
< ctx
.code_size
)
5007 ctx
.code
[clause
->try_offset
+ clause
->try_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
5008 if (clause
->handler_offset
+ clause
->handler_len
< ctx
.code_size
)
5009 ctx
.code
[clause
->handler_offset
+ clause
->handler_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
5011 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
) {
5012 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, clause
->data
.catch_class
);
5014 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
5015 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->data
.filter_offset
, mono_defaults
.exception_class
);
5016 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, mono_defaults
.exception_class
);
5020 while (ip
< end
&& ctx
.valid
) {
5021 ctx
.ip_offset
= ip_offset
= ip
- ctx
.header
->code
;
5023 /*We need to check against fallthrou in and out of protected blocks.
5024 * For fallout we check the once a protected block ends, if the start flag is not set.
5025 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5026 * TODO convert these checks to be done using flags and not this loop
5028 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
5029 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
5031 if ((clause
->try_offset
+ clause
->try_len
== ip_offset
) && start
== 0) {
5032 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset
));
5036 if ((clause
->handler_offset
+ clause
->handler_len
== ip_offset
) && start
== 0) {
5037 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
5038 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5040 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5044 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& clause
->handler_offset
== ip_offset
&& start
== 0) {
5045 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset
));
5049 if (clause
->handler_offset
== ip_offset
&& start
== 0) {
5050 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset
));
5054 if (clause
->try_offset
== ip_offset
&& ctx
.eval
.size
> 0 && start
== 0) {
5055 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset
));
5064 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx
.target
); );
5065 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ctx
.target
], FALSE
, TRUE
);
5068 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ip_offset
], start
, FALSE
);
5071 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5072 #ifdef MONO_VERIFIER_DEBUG
5075 discode
= mono_disasm_code_one (NULL
, method
, ip
, NULL
);
5076 discode
[strlen (discode
) - 1] = 0; /* no \n */
5077 g_print ("[%d] %-29s (%d)\n", ip_offset
, discode
, ctx
.eval
.size
);
5080 dump_stack_state (&ctx
.code
[ip_offset
]);
5081 dump_stack_state (&ctx
.eval
);
5094 push_arg (&ctx
, *ip
- CEE_LDARG_0
, FALSE
);
5100 code_bounds_check (2);
5101 push_arg (&ctx
, ip
[1], *ip
== CEE_LDARGA_S
);
5105 case CEE_ADD_OVF_UN
:
5106 do_binop (&ctx
, *ip
, add_ovf_un_table
);
5110 case CEE_SUB_OVF_UN
:
5111 do_binop (&ctx
, *ip
, sub_ovf_un_table
);
5118 case CEE_MUL_OVF_UN
:
5119 do_binop (&ctx
, *ip
, bin_ovf_table
);
5124 do_binop (&ctx
, *ip
, add_table
);
5129 do_binop (&ctx
, *ip
, sub_table
);
5136 do_binop (&ctx
, *ip
, bin_op_table
);
5145 do_binop (&ctx
, *ip
, int_bin_op_table
);
5152 do_binop (&ctx
, *ip
, shift_op_table
);
5157 if (!check_underflow (&ctx
, 1))
5159 stack_pop_safe (&ctx
);
5173 /*TODO support definite assignment verification? */
5174 push_local (&ctx
, *ip
- CEE_LDLOC_0
, FALSE
);
5182 store_local (&ctx
, *ip
- CEE_STLOC_0
);
5187 code_bounds_check (2);
5188 store_local (&ctx
, ip
[1]);
5193 code_bounds_check (2);
5194 store_arg (&ctx
, ip
[1]);
5208 if (check_overflow (&ctx
))
5209 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5214 code_bounds_check (2);
5215 if (check_overflow (&ctx
))
5216 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5221 code_bounds_check (5);
5222 if (check_overflow (&ctx
))
5223 stack_push_val (&ctx
,TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5228 code_bounds_check (9);
5229 if (check_overflow (&ctx
))
5230 stack_push_val (&ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
5235 code_bounds_check (5);
5236 if (check_overflow (&ctx
))
5237 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5242 code_bounds_check (9);
5243 if (check_overflow (&ctx
))
5244 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5249 if (check_overflow (&ctx
))
5250 stack_push_val (&ctx
, TYPE_COMPLEX
| NULL_LITERAL_MASK
, &mono_defaults
.object_class
->byval_arg
);
5256 code_bounds_check (2);
5257 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_eq_op
);
5270 code_bounds_check (2);
5271 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_op
);
5278 code_bounds_check (5);
5279 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_eq_op
);
5292 code_bounds_check (5);
5293 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_op
);
5300 code_bounds_check (2);
5301 push_local (&ctx
, ip
[1], *ip
== CEE_LDLOCA_S
);
5305 /* FIXME: warn/error instead? */
5312 if (!check_underflow (&ctx
, 1))
5314 if (!check_overflow (&ctx
))
5316 top
= stack_pop_safe (&ctx
);
5317 copy_stack_value (stack_push (&ctx
), top
);
5318 copy_stack_value (stack_push (&ctx
), top
);
5324 code_bounds_check (5);
5326 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset
));
5327 token
= read32 (ip
+ 1);
5328 if (in_any_block (ctx
.header
, ip_offset
))
5329 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset
));
5331 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx
.ip_offset
));
5333 * FIXME: check signature, retval, arguments etc.
5339 code_bounds_check (5);
5340 do_invoke_method (&ctx
, read32 (ip
+ 1), *ip
== CEE_CALLVIRT
);
5345 code_bounds_check (5);
5346 token
= read32 (ip
+ 1);
5348 * FIXME: check signature, retval, arguments etc.
5349 * FIXME: check requirements for tail call
5351 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx
.ip_offset
));
5355 code_bounds_check (2);
5356 do_static_branch (&ctx
, (signed char)ip
[1] + 2);
5364 code_bounds_check (2);
5365 do_boolean_branch_op (&ctx
, (signed char)ip
[1] + 2);
5371 code_bounds_check (5);
5372 do_static_branch (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5380 code_bounds_check (5);
5381 do_boolean_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5387 code_bounds_check (5);
5388 n
= read32 (ip
+ 1);
5389 code_bounds_check (5 + sizeof (guint32
) * n
);
5391 do_switch (&ctx
, n
, (ip
+ 5));
5393 ip
+= 5 + sizeof (guint32
) * n
;
5407 do_load_indirect (&ctx
, *ip
);
5419 do_store_indirect (&ctx
, *ip
);
5425 do_unary_math_op (&ctx
, *ip
);
5435 do_conversion (&ctx
, TYPE_I4
);
5441 do_conversion (&ctx
, TYPE_I8
);
5448 do_conversion (&ctx
, TYPE_R8
);
5454 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5459 code_bounds_check (5);
5460 do_cpobj (&ctx
, read32 (ip
+ 1));
5465 code_bounds_check (5);
5466 do_ldobj_value (&ctx
, read32 (ip
+ 1));
5471 code_bounds_check (5);
5472 do_ldstr (&ctx
, read32 (ip
+ 1));
5477 code_bounds_check (5);
5478 do_newobj (&ctx
, read32 (ip
+ 1));
5484 code_bounds_check (5);
5485 do_cast (&ctx
, read32 (ip
+ 1), *ip
== CEE_CASTCLASS
? "castclass" : "isinst");
5491 ++ip
; /* warn, error ? */
5495 code_bounds_check (5);
5496 do_unbox_value (&ctx
, read32 (ip
+ 1));
5508 code_bounds_check (5);
5509 do_push_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDFLDA
);
5515 code_bounds_check (5);
5516 do_push_static_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDSFLDA
);
5521 code_bounds_check (5);
5522 do_store_field (&ctx
, read32 (ip
+ 1));
5527 code_bounds_check (5);
5528 do_store_static_field (&ctx
, read32 (ip
+ 1));
5533 code_bounds_check (5);
5534 do_stobj (&ctx
, read32 (ip
+ 1));
5538 case CEE_CONV_OVF_I1_UN
:
5539 case CEE_CONV_OVF_I2_UN
:
5540 case CEE_CONV_OVF_I4_UN
:
5541 case CEE_CONV_OVF_U1_UN
:
5542 case CEE_CONV_OVF_U2_UN
:
5543 case CEE_CONV_OVF_U4_UN
:
5544 do_conversion (&ctx
, TYPE_I4
);
5548 case CEE_CONV_OVF_I8_UN
:
5549 case CEE_CONV_OVF_U8_UN
:
5550 do_conversion (&ctx
, TYPE_I8
);
5554 case CEE_CONV_OVF_I_UN
:
5555 case CEE_CONV_OVF_U_UN
:
5556 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5561 code_bounds_check (5);
5562 do_box_value (&ctx
, read32 (ip
+ 1));
5567 code_bounds_check (5);
5568 do_newarr (&ctx
, read32 (ip
+ 1));
5578 code_bounds_check (5);
5579 do_ldelema (&ctx
, read32 (ip
+ 1));
5593 case CEE_LDELEM_REF
:
5594 do_ldelem (&ctx
, *ip
, 0);
5605 case CEE_STELEM_REF
:
5606 do_stelem (&ctx
, *ip
, 0);
5611 code_bounds_check (5);
5612 do_ldelem (&ctx
, *ip
, read32 (ip
+ 1));
5617 code_bounds_check (5);
5618 do_stelem (&ctx
, *ip
, read32 (ip
+ 1));
5623 code_bounds_check (5);
5624 do_unbox_any (&ctx
, read32 (ip
+ 1));
5628 case CEE_CONV_OVF_I1
:
5629 case CEE_CONV_OVF_U1
:
5630 case CEE_CONV_OVF_I2
:
5631 case CEE_CONV_OVF_U2
:
5632 case CEE_CONV_OVF_I4
:
5633 case CEE_CONV_OVF_U4
:
5634 do_conversion (&ctx
, TYPE_I4
);
5638 case CEE_CONV_OVF_I8
:
5639 case CEE_CONV_OVF_U8
:
5640 do_conversion (&ctx
, TYPE_I8
);
5644 case CEE_CONV_OVF_I
:
5645 case CEE_CONV_OVF_U
:
5646 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5651 code_bounds_check (5);
5652 do_refanyval (&ctx
, read32 (ip
+ 1));
5662 code_bounds_check (5);
5663 do_mkrefany (&ctx
, read32 (ip
+ 1));
5668 code_bounds_check (5);
5669 do_load_token (&ctx
, read32 (ip
+ 1));
5673 case CEE_ENDFINALLY
:
5674 if (!is_correct_endfinally (ctx
.header
, ip_offset
))
5675 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx
.ip_offset
));
5682 code_bounds_check (5);
5683 do_leave (&ctx
, read32 (ip
+ 1) + 5);
5689 code_bounds_check (2);
5690 do_leave (&ctx
, (signed char)ip
[1] + 2);
5696 code_bounds_check (2);
5700 code_bounds_check (3);
5701 store_local (&ctx
, read16 (ip
+ 1));
5706 do_cmp_op (&ctx
, cmp_br_eq_op
, *ip
);
5714 do_cmp_op (&ctx
, cmp_br_op
, *ip
);
5719 code_bounds_check (3);
5720 store_arg (&ctx
, read16 (ip
+ 1) );
5726 check_overflow (&ctx
);
5727 if (ctx
.signature
->call_convention
!= MONO_CALL_VARARG
)
5728 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx
.ip_offset
));
5729 set_stack_value (&ctx
, stack_push (&ctx
), &mono_defaults
.argumenthandle_class
->byval_arg
, FALSE
);
5734 code_bounds_check (5);
5735 do_load_function_ptr (&ctx
, read32 (ip
+ 1), FALSE
);
5740 code_bounds_check (5);
5741 do_load_function_ptr (&ctx
, read32 (ip
+ 1), TRUE
);
5751 code_bounds_check (3);
5752 push_arg (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDARGA
);
5758 code_bounds_check (3);
5759 push_local (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDLOCA
);
5772 do_endfilter (&ctx
);
5776 case CEE_UNALIGNED_
:
5777 code_bounds_check (2);
5778 prefix
|= PREFIX_UNALIGNED
;
5782 prefix
|= PREFIX_VOLATILE
;
5786 prefix
|= PREFIX_TAIL
;
5788 if (ip
< end
&& (*ip
!= CEE_CALL
&& *ip
!= CEE_CALLI
&& *ip
!= CEE_CALLVIRT
))
5789 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset
));
5793 code_bounds_check (5);
5794 do_initobj (&ctx
, read32 (ip
+ 1));
5798 case CEE_CONSTRAINED_
:
5799 code_bounds_check (5);
5800 ctx
.constrained_type
= get_boxable_mono_type (&ctx
, read32 (ip
+ 1), "constrained.");
5801 prefix
|= PREFIX_CONSTRAINED
;
5806 prefix
|= PREFIX_READONLY
;
5811 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5812 if (!check_underflow (&ctx
, 3))
5814 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5819 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5820 if (!check_underflow (&ctx
, 3))
5822 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5830 if (!is_correct_rethrow (ctx
.header
, ip_offset
))
5831 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx
.ip_offset
));
5841 code_bounds_check (5);
5842 do_sizeof (&ctx
, read32 (ip
+ 1));
5846 case CEE_REFANYTYPE
:
5847 do_refanytype (&ctx
);
5852 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip
, ctx
.ip_offset
));
5858 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ctx
.ip_offset
));
5862 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5864 if (!ctx
.prefix_set
) //first prefix
5865 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5866 ctx
.prefix_set
|= prefix
;
5867 ctx
.has_flags
= TRUE
;
5871 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5873 if (ctx
.prefix_set
& PREFIX_CONSTRAINED
)
5874 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx
.ip_offset
));
5875 if (ctx
.prefix_set
& PREFIX_READONLY
)
5876 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx
.ip_offset
));
5877 if (ctx
.prefix_set
& PREFIX_VOLATILE
)
5878 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx
.ip_offset
));
5879 if (ctx
.prefix_set
& PREFIX_UNALIGNED
)
5880 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx
.ip_offset
));
5881 ctx
.prefix_set
= prefix
= 0;
5882 ctx
.has_flags
= FALSE
;
5886 * if ip != end we overflowed: mark as error.
5888 if ((ip
!= end
|| !start
) && ctx
.verifiable
&& !ctx
.list
) {
5889 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset
));
5892 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5893 for (i
= 0; i
< ctx
.code_size
&& i
< ip_offset
; ++i
) {
5894 if (ctx
.code
[i
].flags
& IL_CODE_FLAG_WAS_TARGET
) {
5895 if (!(ctx
.code
[i
].flags
& IL_CODE_FLAG_SEEN
))
5896 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i
));
5898 if (ctx
.code
[i
].flags
& IL_CODE_DELEGATE_SEQUENCE
)
5899 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i
));
5901 if ((ctx
.code
[i
].flags
& IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5902 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i
));
5904 if ((ctx
.code
[i
].flags
& IL_CODE_CALL_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5905 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
));
5908 if (mono_method_is_constructor (ctx
.method
) && !ctx
.super_ctor_called
&& !ctx
.method
->klass
->valuetype
&& ctx
.method
->klass
!= mono_defaults
.object_class
) {
5909 char *method_name
= mono_method_full_name (ctx
.method
, TRUE
);
5910 char *type
= mono_type_get_full_name (ctx
.method
->klass
);
5911 if (ctx
.method
->klass
->parent
&& ctx
.method
->klass
->parent
->exception_type
!= MONO_EXCEPTION_NONE
)
5912 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
));
5914 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name
, type
));
5915 g_free (method_name
);
5921 for (i
= 0; i
< ctx
.header
->code_size
; ++i
) {
5922 if (ctx
.code
[i
].stack
)
5923 g_free (ctx
.code
[i
].stack
);
5927 for (tmp
= ctx
.funptrs
; tmp
; tmp
= tmp
->next
)
5929 g_slist_free (ctx
.funptrs
);
5931 for (tmp
= ctx
.exception_types
; tmp
; tmp
= tmp
->next
)
5932 mono_metadata_free_type (tmp
->data
);
5933 g_slist_free (ctx
.exception_types
);
5935 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
5937 mono_metadata_free_type (ctx
.locals
[i
]);
5939 for (i
= 0; i
< ctx
.max_args
; ++i
) {
5941 mono_metadata_free_type (ctx
.params
[i
]);
5945 g_free (ctx
.eval
.stack
);
5948 g_free (ctx
.locals
);
5949 g_free (ctx
.params
);
5955 mono_verify_corlib ()
5957 /* This is a public API function so cannot be removed */
5962 * Returns true if @method needs to be verified.
5966 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
5968 return mono_verifier_is_enabled_for_class (method
->klass
) && method
->wrapper_type
== MONO_WRAPPER_NONE
;
5972 * Returns true if @klass need to be verified.
5976 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
5978 return verify_all
|| (verifier_mode
> MONO_VERIFIER_MODE_OFF
&& !klass
->image
->assembly
->in_gac
&& klass
->image
!= mono_defaults
.corlib
);
5982 mono_verifier_is_enabled_for_image (MonoImage
*image
)
5984 return verify_all
|| verifier_mode
> MONO_VERIFIER_MODE_OFF
;
5988 mono_verifier_is_method_full_trust (MonoMethod
*method
)
5990 return mono_verifier_is_class_full_trust (method
->klass
);
5994 * Returns if @klass is under full trust or not.
5996 * TODO This code doesn't take CAS into account.
5998 * Under verify_all all user code must be verifiable if no security option was set
6002 mono_verifier_is_class_full_trust (MonoClass
*klass
)
6004 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
6005 gboolean trusted_location
= (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR
) ?
6006 klass
->image
->assembly
->in_gac
: mono_security_core_clr_is_platform_image (klass
->image
);
6008 if (verify_all
&& verifier_mode
== MONO_VERIFIER_MODE_OFF
)
6009 return trusted_location
|| klass
->image
== mono_defaults
.corlib
;
6010 return verifier_mode
< MONO_VERIFIER_MODE_VERIFIABLE
|| trusted_location
|| klass
->image
== mono_defaults
.corlib
;
6014 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
6016 return mono_method_verify (method
,
6017 (verifier_mode
!= MONO_VERIFIER_MODE_STRICT
? MONO_VERIFY_NON_STRICT
: 0)
6018 | (!mono_verifier_is_method_full_trust (method
) ? MONO_VERIFY_FAIL_FAST
: 0)
6019 | (skip_visibility
? MONO_VERIFY_SKIP_VISIBILITY
: 0));
6023 get_field_end (MonoClassField
*field
)
6026 int size
= mono_type_size (field
->type
, &align
);
6028 size
= 4; /*FIXME Is this a safe bet?*/
6029 return size
+ field
->offset
;
6033 verify_class_for_overlapping_reference_fields (MonoClass
*class)
6036 gboolean is_fulltrust
= mono_verifier_is_class_full_trust (class);
6037 if (!((class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) || !class->has_references
)
6040 //we must check for stuff overlapping reference fields
6041 for (i
= 0; i
< class->field
.count
; ++i
) {
6042 MonoClassField
*field
= &class->fields
[i
];
6043 int fieldEnd
= get_field_end (field
);
6044 gboolean is_valuetype
= !MONO_TYPE_IS_REFERENCE (field
->type
);
6045 if (mono_field_is_deleted (field
) || (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6048 for (j
= i
+ 1; j
< class->field
.count
; ++j
) {
6049 MonoClassField
*other
= &class->fields
[j
];
6050 int otherEnd
= get_field_end (other
);
6051 if (mono_field_is_deleted (other
) || (is_valuetype
&& !MONO_TYPE_IS_REFERENCE (other
->type
)) || (other
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6054 if (!is_valuetype
&& MONO_TYPE_IS_REFERENCE (other
->type
) && field
->offset
== other
->offset
&& is_fulltrust
)
6057 if ((otherEnd
> field
->offset
&& otherEnd
<= fieldEnd
) || (other
->offset
>= field
->offset
&& other
->offset
< fieldEnd
))
6065 field_hash (gconstpointer key
)
6067 const MonoClassField
*field
= key
;
6068 return g_str_hash (field
->name
) ^ mono_metadata_type_hash (field
->type
); /**/
6072 field_equals (gconstpointer _a
, gconstpointer _b
)
6074 const MonoClassField
*a
= _a
;
6075 const MonoClassField
*b
= _b
;
6076 return !strcmp (a
->name
, b
->name
) && mono_metadata_type_equal (a
->type
, b
->type
);
6081 verify_class_fields (MonoClass
*class)
6083 gpointer iter
= NULL
;
6084 MonoClassField
*field
;
6085 MonoGenericContext
*context
= mono_class_get_context (class);
6086 GHashTable
*unique_fields
= g_hash_table_new_full (&field_hash
, &field_equals
, NULL
, NULL
);
6087 if (class->generic_container
)
6088 context
= &class->generic_container
->context
;
6090 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
6091 if (!mono_type_is_valid_type_in_context (field
->type
, context
)) {
6092 g_hash_table_destroy (unique_fields
);
6095 if (g_hash_table_lookup (unique_fields
, field
)) {
6096 g_hash_table_destroy (unique_fields
);
6099 g_hash_table_insert (unique_fields
, field
, field
);
6101 g_hash_table_destroy (unique_fields
);
6106 verify_interfaces (MonoClass
*class)
6109 for (i
= 0; i
< class->interface_count
; ++i
) {
6110 MonoClass
*iface
= class->interfaces
[i
];
6111 if (!(iface
->flags
& TYPE_ATTRIBUTE_INTERFACE
))
6118 verify_valuetype_layout_with_target (MonoClass
*class, MonoClass
*target_class
)
6121 gpointer iter
= NULL
;
6122 MonoClassField
*field
;
6123 MonoClass
*field_class
;
6125 if (!class->valuetype
)
6128 type
= class->byval_arg
.type
;
6129 /*primitive type fields are not properly decoded*/
6130 if ((type
>= MONO_TYPE_BOOLEAN
&& type
<= MONO_TYPE_R8
) || (type
>= MONO_TYPE_I
&& type
<= MONO_TYPE_U
))
6133 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
6137 if (field
->type
->attrs
& (FIELD_ATTRIBUTE_STATIC
| FIELD_ATTRIBUTE_HAS_FIELD_RVA
))
6140 field_class
= mono_class_get_generic_type_definition (mono_class_from_mono_type (field
->type
));
6142 if (field_class
== target_class
|| !verify_valuetype_layout_with_target (field_class
, target_class
))
6150 verify_valuetype_layout (MonoClass
*class)
6153 res
= verify_valuetype_layout_with_target (class, class);
6158 * Check if the class is verifiable.
6160 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6161 * field and invalid generic instantiation are fatal errors.
6163 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6167 mono_verifier_verify_class (MonoClass
*class)
6169 if (class->generic_container
&& (class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
)
6171 if (!verify_class_for_overlapping_reference_fields (class))
6173 if (class->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, class))
6175 if (class->generic_class
== NULL
&& !verify_class_fields (class))
6177 if (class->valuetype
&& !verify_valuetype_layout (class))
6179 if (!verify_interfaces (class))
6186 mono_verifier_verify_class (MonoClass
*class)
6188 /* The verifier was disabled at compile time */
6193 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
6195 /* The verifier was disabled at compile time */
6200 mono_verifier_is_class_full_trust (MonoClass
*klass
)
6202 /* The verifier was disabled at compile time */
6207 mono_verifier_is_method_full_trust (MonoMethod
*method
)
6209 /* The verifier was disabled at compile time */
6214 mono_verifier_is_enabled_for_image (MonoImage
*image
)
6216 /* The verifier was disabled at compile time */
6221 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
6223 /* The verifier was disabled at compile time */
6228 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
6230 /* The verifier was disabled at compile time */
6235 mono_method_verify (MonoMethod
*method
, int level
)
6237 /* The verifier was disabled at compile time */
6242 mono_free_verify_list (GSList
*list
)
6244 /* The verifier was disabled at compile time */
6245 /* will always be null if verifier is disabled */
6249 mono_image_verify_tables (MonoImage
*image
, int level
)
6251 /* The verifier was disabled at compile time */