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; \
117 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
118 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
120 #if SIZEOF_VOID_P == 4
121 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
123 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
126 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
127 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
129 /*Flags to be used with ILCodeDesc::flags */
131 /*Instruction has not been processed.*/
132 IL_CODE_FLAG_NOT_PROCESSED
= 0,
133 /*Instruction was decoded by mono_method_verify loop.*/
134 IL_CODE_FLAG_SEEN
= 1,
135 /*Instruction was target of a branch or is at a protected block boundary.*/
136 IL_CODE_FLAG_WAS_TARGET
= 2,
137 /*Used by stack_init to avoid double initialize each entry.*/
138 IL_CODE_FLAG_STACK_INITED
= 4,
139 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
140 IL_CODE_STACK_MERGED
= 8,
141 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
142 IL_CODE_DELEGATE_SEQUENCE
= 0x10,
143 /*This is a delegate created from a ldftn to a non final virtual method*/
144 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
= 0x20,
145 /*This is a call to a non final virtual method*/
146 IL_CODE_CALL_NONFINAL_VIRTUAL
= 0x40,
181 /*Allocated fnptr MonoType that should be freed by us.*/
183 /*Type dup'ed exception types from catch blocks.*/
184 GSList
*exception_types
;
189 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
193 MonoMethodSignature
*signature
;
194 MonoMethodHeader
*header
;
196 MonoGenericContext
*generic_context
;
200 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
201 *on a method that creates a delegate for a non-final virtual method using ldftn*/
202 gboolean has_this_store
;
204 /*This flag is used to control if the contructor of the parent class has been called.
205 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
206 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
208 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
209 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
211 gboolean super_ctor_called
;
215 MonoType
*constrained_type
;
219 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
);
222 get_stack_type (MonoType
*type
);
225 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
);
228 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
);
231 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
);
232 //////////////////////////////////////////////////////////////////
237 TYPE_INV
= 0, /* leave at 0. */
242 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
244 /* value types and classes */
246 /* Number of types, used to define the size of the tables*/
249 /* Used by tables to signal that a result is not verifiable*/
250 NON_VERIFIABLE_RESULT
= 0x80,
252 /*Mask used to extract just the type, excluding flags */
255 /* The stack type is a managed pointer, unmask the value to res */
256 POINTER_MASK
= 0x100,
258 /*Stack type with the pointer mask*/
259 RAW_TYPE_MASK
= 0x10F,
261 /* Controlled Mutability Manager Pointer */
264 /* The stack type is a null literal*/
265 NULL_LITERAL_MASK
= 0x400,
267 /**Used by ldarg.0 and family to let delegate verification happens.*/
268 THIS_POINTER_MASK
= 0x800,
270 /**Signals that this is a boxed value type*/
273 /*This is an unitialized this ref*/
274 UNINIT_THIS_MASK
= 0x2000,
277 static const char* const
278 type_names
[TYPE_MAX
+ 1] = {
289 PREFIX_UNALIGNED
= 1,
292 PREFIX_CONSTRAINED
= 8,
295 //////////////////////////////////////////////////////////////////
298 /*Token validation macros and functions */
299 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
300 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
301 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
302 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
304 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
305 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
306 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
307 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
308 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
309 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
312 * Verify if @token refers to a valid row on int's table.
315 token_bounds_check (MonoImage
*image
, guint32 token
)
318 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage
*)image
, token
);
319 return image
->tables
[mono_metadata_token_table (token
)].rows
>= mono_metadata_token_index (token
);
323 mono_type_create_fnptr_from_mono_method (VerifyContext
*ctx
, MonoMethod
*method
)
325 MonoType
*res
= g_new0 (MonoType
, 1);
326 //FIXME use mono_method_get_signature_full
327 res
->data
.method
= mono_method_signature (method
);
328 res
->type
= MONO_TYPE_FNPTR
;
329 ctx
->funptrs
= g_slist_prepend (ctx
->funptrs
, res
);
334 * mono_type_is_enum_type:
336 * Returns TRUE if @type is an enum type.
339 mono_type_is_enum_type (MonoType
*type
)
341 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
343 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
349 * mono_type_is_value_type:
351 * Returns TRUE if @type is named after @namespace.@name.
355 mono_type_is_value_type (MonoType
*type
, const char *namespace, const char *name
)
357 return type
->type
== MONO_TYPE_VALUETYPE
&&
358 !strcmp (namespace, type
->data
.klass
->name_space
) &&
359 !strcmp (name
, type
->data
.klass
->name
);
363 * Returns TURE if @type is VAR or MVAR
366 mono_type_is_generic_argument (MonoType
*type
)
368 return type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
;
372 * mono_type_get_underlying_type_any:
374 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
376 * Returns the underlying type of @type regardless if it is byref or not.
379 mono_type_get_underlying_type_any (MonoType
*type
)
381 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
382 return mono_class_enum_basetype (type
->data
.klass
);
383 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
384 return mono_class_enum_basetype (type
->data
.generic_class
->container_class
);
389 mono_type_get_stack_name (MonoType
*type
)
391 return type_names
[get_stack_type (type
) & TYPE_MASK
];
394 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
395 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
398 mono_method_is_constructor (MonoMethod
*method
)
400 return ((method
->flags
& CTOR_REQUIRED_FLAGS
) == CTOR_REQUIRED_FLAGS
&&
401 !(method
->flags
& CTOR_INVALID_FLAGS
) &&
402 !strcmp (".ctor", method
->name
));
406 mono_class_has_default_constructor (MonoClass
*klass
)
411 mono_class_setup_methods (klass
);
413 for (i
= 0; i
< klass
->method
.count
; ++i
) {
414 method
= klass
->methods
[i
];
415 if (mono_method_is_constructor (method
) &&
416 mono_method_signature (method
) &&
417 mono_method_signature (method
)->param_count
== 0 &&
418 (method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
)
425 mono_class_interface_implements_interface (MonoClass
*candidate
, MonoClass
*iface
)
429 if (candidate
== iface
)
431 mono_class_setup_interfaces (candidate
);
432 for (i
= 0; i
< candidate
->interface_count
; ++i
) {
433 if (candidate
->interfaces
[i
] == iface
|| mono_class_interface_implements_interface (candidate
->interfaces
[i
], iface
))
436 candidate
= candidate
->parent
;
442 * Verify if @type is valid for the given @ctx verification context.
443 * this function checks for VAR and MVAR types that are invalid under the current verifier,
446 mono_type_is_valid_type_in_context (MonoType
*type
, MonoGenericContext
*context
)
449 MonoGenericInst
*inst
;
451 switch (type
->type
) {
456 inst
= type
->type
== MONO_TYPE_VAR
? context
->class_inst
: context
->method_inst
;
457 if (!inst
|| mono_type_get_generic_param_num (type
) >= inst
->type_argc
)
460 case MONO_TYPE_SZARRAY
:
461 return mono_type_is_valid_type_in_context (&type
->data
.klass
->byval_arg
, context
);
462 case MONO_TYPE_ARRAY
:
463 return mono_type_is_valid_type_in_context (&type
->data
.array
->eklass
->byval_arg
, context
);
465 return mono_type_is_valid_type_in_context (type
->data
.type
, context
);
466 case MONO_TYPE_GENERICINST
:
467 inst
= type
->data
.generic_class
->context
.class_inst
;
470 for (i
= 0; i
< inst
->type_argc
; ++i
)
471 if (!mono_type_is_valid_type_in_context (inst
->type_argv
[i
], context
))
478 /*This function returns NULL if the type is not instantiatable*/
480 verifier_inflate_type (VerifyContext
*ctx
, MonoType
*type
, MonoGenericContext
*context
)
485 result
= mono_class_inflate_generic_type_checked (type
, context
, &error
);
486 if (!mono_error_ok (&error
)) {
487 mono_error_cleanup (&error
);
493 * Test if @candidate is a subtype of @target using the minimal possible information
494 * TODO move the code for non finished TypeBuilders to here.
497 mono_class_is_constraint_compatible (MonoClass
*candidate
, MonoClass
*target
)
499 if (candidate
== target
)
501 if (target
== mono_defaults
.object_class
)
504 //setup_supertypes don't mono_class_init anything
505 mono_class_setup_supertypes (candidate
);
506 mono_class_setup_supertypes (target
);
508 if (mono_class_has_parent (candidate
, target
))
511 //if target is not a supertype it must be an interface
512 if (!MONO_CLASS_IS_INTERFACE (target
))
515 if (candidate
->image
->dynamic
&& !candidate
->wastypebuilder
) {
516 MonoReflectionTypeBuilder
*tb
= candidate
->reflection_info
;
518 if (tb
->interfaces
) {
519 for (j
= mono_array_length (tb
->interfaces
) - 1; j
>= 0; --j
) {
520 MonoReflectionType
*iface
= mono_array_get (tb
->interfaces
, MonoReflectionType
*, j
);
521 MonoClass
*ifaceClass
= mono_class_from_mono_type (iface
->type
);
522 if (mono_class_is_constraint_compatible (ifaceClass
, target
)) {
529 return mono_class_interface_implements_interface (candidate
, target
);
533 is_valid_generic_instantiation (MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
538 if (ginst
->type_argc
!= gc
->type_argc
)
541 for (i
= 0; i
< gc
->type_argc
; ++i
) {
542 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
543 MonoClass
*paramClass
;
544 MonoClass
**constraints
;
546 if (!param_info
->constraints
&& !(param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
))
548 if (mono_type_is_generic_argument (ginst
->type_argv
[i
]))
549 continue; //it's not our job to validate type variables
551 paramClass
= mono_class_from_mono_type (ginst
->type_argv
[i
]);
553 if (paramClass
->exception_type
!= MONO_EXCEPTION_NONE
)
556 /*it's not safe to call mono_class_init from here*/
557 if (paramClass
->generic_class
&& !paramClass
->inited
) {
558 if (!mono_class_is_valid_generic_instantiation (NULL
, paramClass
))
562 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
) && (!paramClass
->valuetype
|| mono_class_is_nullable (paramClass
)))
565 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT
) && paramClass
->valuetype
)
568 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT
) && !paramClass
->valuetype
&& !mono_class_has_default_constructor (paramClass
))
571 if (!param_info
->constraints
)
574 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
575 MonoClass
*ctr
= *constraints
;
578 inflated
= mono_class_inflate_generic_type_checked (&ctr
->byval_arg
, context
, &error
);
579 if (!mono_error_ok (&error
)) {
580 mono_error_cleanup (&error
);
583 ctr
= mono_class_from_mono_type (inflated
);
584 mono_metadata_free_type (inflated
);
586 if (!mono_class_is_constraint_compatible (paramClass
, ctr
))
594 * Return true if @candidate is constraint compatible with @target.
596 * This means that @candidate constraints are a super set of @target constaints
599 mono_generic_param_is_constraint_compatible (VerifyContext
*ctx
, MonoGenericParam
*target
, MonoGenericParam
*candidate
, MonoGenericContext
*context
)
601 MonoGenericParamInfo
*tinfo
= mono_generic_param_info (target
);
602 MonoGenericParamInfo
*cinfo
= mono_generic_param_info (candidate
);
604 int tmask
= tinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
605 int cmask
= cinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
606 if ((tmask
& cmask
) != tmask
)
609 if (tinfo
->constraints
) {
610 MonoClass
**target_class
, **candidate_class
;
611 if (!cinfo
->constraints
)
613 for (target_class
= tinfo
->constraints
; *target_class
; ++target_class
) {
615 MonoType
*inflated
= verifier_inflate_type (ctx
, &(*target_class
)->byval_arg
, context
);
618 tc
= mono_class_from_mono_type (inflated
);
619 mono_metadata_free_type (inflated
);
621 for (candidate_class
= cinfo
->constraints
; *candidate_class
; ++candidate_class
) {
623 inflated
= verifier_inflate_type (ctx
, &(*candidate_class
)->byval_arg
, ctx
->generic_context
);
626 cc
= mono_class_from_mono_type (inflated
);
627 mono_metadata_free_type (inflated
);
629 if (mono_class_is_assignable_from (tc
, cc
))
632 if (!*candidate_class
)
639 static MonoGenericParam
*
640 verifier_get_generic_param_from_type (VerifyContext
*ctx
, MonoType
*type
)
642 MonoGenericContainer
*gc
;
643 MonoMethod
*method
= ctx
->method
;
646 num
= mono_type_get_generic_param_num (type
);
648 if (type
->type
== MONO_TYPE_VAR
) {
649 MonoClass
*gtd
= method
->klass
;
650 if (gtd
->generic_class
)
651 gtd
= gtd
->generic_class
->container_class
;
652 gc
= gtd
->generic_container
;
654 MonoMethod
*gmd
= method
;
655 if (method
->is_inflated
)
656 gmd
= ((MonoMethodInflated
*)method
)->declaring
;
657 gc
= mono_method_get_generic_container (gmd
);
661 return mono_generic_container_get_param (gc
, num
);
667 * Verify if @type is valid for the given @ctx verification context.
668 * this function checks for VAR and MVAR types that are invalid under the current verifier,
669 * This means that it either
672 is_valid_type_in_context (VerifyContext
*ctx
, MonoType
*type
)
674 return mono_type_is_valid_type_in_context (type
, ctx
->generic_context
);
678 is_valid_generic_instantiation_in_context (VerifyContext
*ctx
, MonoGenericInst
*ginst
)
681 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
682 MonoType
*type
= ginst
->type_argv
[i
];
683 if (!is_valid_type_in_context (ctx
, type
))
690 generic_arguments_respect_constraints (VerifyContext
*ctx
, MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
693 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
694 MonoType
*type
= ginst
->type_argv
[i
];
695 MonoGenericParam
*target
= mono_generic_container_get_param (gc
, i
);
696 MonoGenericParam
*candidate
;
698 if (!mono_type_is_generic_argument (type
))
701 if (!is_valid_type_in_context (ctx
, type
))
704 candidate
= verifier_get_generic_param_from_type (ctx
, type
);
706 if (!mono_generic_param_is_constraint_compatible (ctx
, target
, candidate
, context
))
713 mono_method_repect_method_constraints (VerifyContext
*ctx
, MonoMethod
*method
)
715 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
716 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
717 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
718 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gmethod
->context
, ginst
);
722 mono_class_repect_method_constraints (VerifyContext
*ctx
, MonoClass
*klass
)
724 MonoGenericClass
*gklass
= klass
->generic_class
;
725 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
726 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
727 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gklass
->context
, ginst
);
731 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
)
733 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
734 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
735 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
736 if (!gc
) /*non-generic inflated method - it's part of a generic type */
738 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
740 return is_valid_generic_instantiation (gc
, &gmethod
->context
, ginst
);
745 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
)
747 MonoGenericClass
*gklass
= klass
->generic_class
;
748 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
749 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
750 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
752 return is_valid_generic_instantiation (gc
, &gklass
->context
, ginst
);
756 mono_type_is_valid_in_context (VerifyContext
*ctx
, MonoType
*type
)
761 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid null type at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
765 if (!is_valid_type_in_context (ctx
, type
)) {
766 char *str
= mono_type_full_name (type
);
767 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
768 type
->type
== MONO_TYPE_VAR
? "!" : "!!",
770 type
->type
== MONO_TYPE_VAR
? "class" : "method",
772 MONO_EXCEPTION_BAD_IMAGE
);
777 klass
= mono_class_from_mono_type (type
);
778 mono_class_init (klass
);
779 if (mono_loader_get_last_error () || klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
780 if (klass
->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, klass
))
781 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
);
783 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
);
787 if (klass
->generic_class
&& klass
->generic_class
->container_class
->exception_type
!= MONO_EXCEPTION_NONE
) {
788 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
);
792 if (!klass
->generic_class
)
795 if (!mono_class_is_valid_generic_instantiation (ctx
, klass
)) {
796 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
);
800 if (!mono_class_repect_method_constraints (ctx
, klass
)) {
801 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
);
808 static verify_result_t
809 mono_method_is_valid_in_context (VerifyContext
*ctx
, MonoMethod
*method
)
811 if (!mono_type_is_valid_in_context (ctx
, &method
->klass
->byval_arg
))
812 return RESULT_INVALID
;
814 if (!method
->is_inflated
)
817 if (!mono_method_is_valid_generic_instantiation (ctx
, method
)) {
818 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
);
819 return RESULT_INVALID
;
822 if (!mono_method_repect_method_constraints (ctx
, method
)) {
823 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
));
824 return RESULT_UNVERIFIABLE
;
830 static MonoClassField
*
831 verifier_load_field (VerifyContext
*ctx
, int token
, MonoClass
**out_klass
, const char *opcode
) {
832 MonoClassField
*field
;
833 MonoClass
*klass
= NULL
;
835 if (!IS_FIELD_DEF_OR_REF (token
) || !token_bounds_check (ctx
->image
, token
)) {
836 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
);
840 field
= mono_field_from_token (ctx
->image
, token
, &klass
, ctx
->generic_context
);
841 if (!field
|| !field
->parent
|| !klass
) {
842 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
);
846 if (!mono_type_is_valid_in_context (ctx
, &klass
->byval_arg
))
854 verifier_load_method (VerifyContext
*ctx
, int token
, const char *opcode
) {
857 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
858 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
);
862 method
= mono_get_method_full (ctx
->image
, token
, NULL
, ctx
->generic_context
);
865 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
);
869 if (mono_method_is_valid_in_context (ctx
, method
) == RESULT_INVALID
)
876 verifier_load_type (VerifyContext
*ctx
, int token
, const char *opcode
) {
879 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
880 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
884 type
= mono_type_get_full (ctx
->image
, token
, ctx
->generic_context
);
887 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
);
891 if (!mono_type_is_valid_in_context (ctx
, type
))
898 /* stack_slot_get_type:
900 * Returns the stack type of @value. This value includes POINTER_MASK.
902 * Use this function to checks that account for a managed pointer.
905 stack_slot_get_type (ILStackDesc
*value
)
907 return value
->stype
& RAW_TYPE_MASK
;
910 /* stack_slot_get_underlying_type:
912 * Returns the stack type of @value. This value does not include POINTER_MASK.
914 * Use this function is cases where the fact that the value could be a managed pointer is
915 * irrelevant. For example, field load doesn't care about this fact of type on stack.
918 stack_slot_get_underlying_type (ILStackDesc
*value
)
920 return value
->stype
& TYPE_MASK
;
923 /* stack_slot_is_managed_pointer:
925 * Returns TRUE is @value is a managed pointer.
928 stack_slot_is_managed_pointer (ILStackDesc
*value
)
930 return (value
->stype
& POINTER_MASK
) == POINTER_MASK
;
933 /* stack_slot_is_managed_mutability_pointer:
935 * Returns TRUE is @value is a managed mutability pointer.
937 static G_GNUC_UNUSED gboolean
938 stack_slot_is_managed_mutability_pointer (ILStackDesc
*value
)
940 return (value
->stype
& CMMP_MASK
) == CMMP_MASK
;
943 /* stack_slot_is_null_literal:
945 * Returns TRUE is @value is the null literal.
948 stack_slot_is_null_literal (ILStackDesc
*value
)
950 return (value
->stype
& NULL_LITERAL_MASK
) == NULL_LITERAL_MASK
;
954 /* stack_slot_is_this_pointer:
956 * Returns TRUE is @value is the this literal
959 stack_slot_is_this_pointer (ILStackDesc
*value
)
961 return (value
->stype
& THIS_POINTER_MASK
) == THIS_POINTER_MASK
;
964 /* stack_slot_is_boxed_value:
966 * Returns TRUE is @value is a boxed value
969 stack_slot_is_boxed_value (ILStackDesc
*value
)
971 return (value
->stype
& BOXED_MASK
) == BOXED_MASK
;
975 stack_slot_get_name (ILStackDesc
*value
)
977 return type_names
[value
->stype
& TYPE_MASK
];
980 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
981 if (PRED (value)) { \
983 g_string_append (str, ", "); \
984 g_string_append (str, NAME); \
989 stack_slot_stack_type_full_name (ILStackDesc
*value
)
991 GString
*str
= g_string_new ("");
994 if ((value
->stype
& TYPE_MASK
) != value
->stype
) {
995 gboolean first
= TRUE
;
996 g_string_append(str
, "[");
997 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer
, "this");
998 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value
, "boxed");
999 APPEND_WITH_PREDICATE (stack_slot_is_null_literal
, "null");
1000 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer
, "cmmp");
1001 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer
, "mp");
1002 g_string_append(str
, "] ");
1005 g_string_append (str
, stack_slot_get_name (value
));
1007 g_string_free (str
, FALSE
);
1012 stack_slot_full_name (ILStackDesc
*value
)
1014 char *type_name
= mono_type_full_name (value
->type
);
1015 char *stack_name
= stack_slot_stack_type_full_name (value
);
1016 char *res
= g_strdup_printf ("%s (%s)", type_name
, stack_name
);
1018 g_free (stack_name
);
1022 //////////////////////////////////////////////////////////////////
1024 mono_free_verify_list (GSList
*list
)
1026 MonoVerifyInfoExtended
*info
;
1029 for (tmp
= list
; tmp
; tmp
= tmp
->next
) {
1031 g_free (info
->info
.message
);
1034 g_slist_free (list
);
1037 #define ADD_ERROR(list,msg) \
1039 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1040 vinfo->info.status = MONO_VERIFY_ERROR; \
1041 vinfo->info.message = (msg); \
1042 (list) = g_slist_prepend ((list), vinfo); \
1045 #define ADD_WARN(list,code,msg) \
1047 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1048 vinfo->info.status = (code); \
1049 vinfo->info.message = (msg); \
1050 (list) = g_slist_prepend ((list), vinfo); \
1054 valid_cultures
[][9] = {
1055 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
1056 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
1057 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
1058 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
1059 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
1060 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
1061 "da-DK", "de-DE", "de-CH", "de-AT",
1062 "de-LU", "de-LI", "el-GR", "en-US",
1063 "en-GB", "en-AU", "en-CA", "en-NZ",
1064 "en-IE", "en-ZA", "en-JM", "en-CB",
1065 "en-BZ", "en-TT", "en-ZW", "en-PH",
1066 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
1067 "es-CR", "es-PA", "es-DO", "es-VE",
1068 "es-CO", "es-PE", "es-AR", "es-EC",
1069 "es-CL", "es-UY", "es-PY", "es-BO",
1070 "es-SV", "es-HN", "es-NI", "es-PR",
1071 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
1072 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
1073 "hu-HU", "is-IS", "it-IT", "it-CH",
1074 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
1075 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
1076 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
1077 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
1078 "sv-SE", "sv-FI", "th-TH", "tr-TR",
1079 "ur-PK", "id-ID", "uk-UA", "be-BY",
1080 "sl-SI", "et-EE", "lv-LV", "lt-LT",
1081 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
1083 "eu-ES", "mk-MK", "af-ZA",
1084 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
1085 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
1086 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
1087 "gu-IN", "ta-IN", "te-IN", "kn-IN",
1088 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
1089 "kok-IN", "syr-SY", "div-MV"
1093 is_valid_culture (const char *cname
)
1098 found
= *cname
== 0;
1099 for (i
= 0; i
< G_N_ELEMENTS (valid_cultures
); ++i
) {
1100 if (g_ascii_strcasecmp (valid_cultures
[i
], cname
)) {
1109 is_valid_assembly_flags (guint32 flags
) {
1110 /* Metadata: 22.1.2 */
1111 flags
&= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
1112 return ((flags
== 1) || (flags
== 0));
1116 is_valid_blob (MonoImage
*image
, guint32 blob_index
, int notnull
)
1119 const char *p
, *blob_end
;
1121 if (blob_index
>= image
->heap_blob
.size
)
1123 p
= mono_metadata_blob_heap (image
, blob_index
);
1124 size
= mono_metadata_decode_blob_size (p
, &blob_end
);
1125 if (blob_index
+ size
+ (blob_end
-p
) > image
->heap_blob
.size
)
1127 if (notnull
&& !size
)
1133 is_valid_string (MonoImage
*image
, guint32 str_index
, int notnull
)
1135 const char *p
, *blob_end
, *res
;
1137 if (str_index
>= image
->heap_strings
.size
)
1139 res
= p
= mono_metadata_string_heap (image
, str_index
);
1140 blob_end
= mono_metadata_string_heap (image
, image
->heap_strings
.size
- 1);
1144 * FIXME: should check it's a valid utf8 string, too.
1146 while (p
<= blob_end
) {
1151 return *p
? NULL
: res
;
1155 is_valid_cls_ident (const char *p
)
1158 * FIXME: we need the full unicode glib support for this.
1159 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
1160 * We do the lame thing for now.
1166 if (!isalnum (*p
) && *p
!= '_')
1174 is_valid_filename (const char *p
)
1178 return strpbrk (p
, "\\//:")? 0: 1;
1182 verify_assembly_table (MonoImage
*image
, GSList
*list
, int level
)
1184 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_ASSEMBLY
];
1185 guint32 cols
[MONO_ASSEMBLY_SIZE
];
1188 if (level
& MONO_VERIFY_ERROR
) {
1190 ADD_ERROR (list
, g_strdup ("Assembly table may only have 0 or 1 rows"));
1191 mono_metadata_decode_row (t
, 0, cols
, MONO_ASSEMBLY_SIZE
);
1193 switch (cols
[MONO_ASSEMBLY_HASH_ALG
]) {
1194 case ASSEMBLY_HASH_NONE
:
1195 case ASSEMBLY_HASH_MD5
:
1196 case ASSEMBLY_HASH_SHA1
:
1199 ADD_ERROR (list
, g_strdup_printf ("Hash algorithm 0x%x unknown", cols
[MONO_ASSEMBLY_HASH_ALG
]));
1202 if (!is_valid_assembly_flags (cols
[MONO_ASSEMBLY_FLAGS
]))
1203 ADD_ERROR (list
, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols
[MONO_ASSEMBLY_FLAGS
]));
1205 if (!is_valid_blob (image
, cols
[MONO_ASSEMBLY_PUBLIC_KEY
], FALSE
))
1206 ADD_ERROR (list
, g_strdup ("Assembly public key is an invalid index"));
1208 if (!(p
= is_valid_string (image
, cols
[MONO_ASSEMBLY_NAME
], TRUE
))) {
1209 ADD_ERROR (list
, g_strdup ("Assembly name is invalid"));
1211 if (strpbrk (p
, ":\\/."))
1212 ADD_ERROR (list
, g_strdup_printf ("Assembly name `%s' contains invalid chars", p
));
1215 if (!(p
= is_valid_string (image
, cols
[MONO_ASSEMBLY_CULTURE
], FALSE
))) {
1216 ADD_ERROR (list
, g_strdup ("Assembly culture is an invalid index"));
1218 if (!is_valid_culture (p
))
1219 ADD_ERROR (list
, g_strdup_printf ("Assembly culture `%s' is invalid", p
));
1226 verify_assemblyref_table (MonoImage
*image
, GSList
*list
, int level
)
1228 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_ASSEMBLYREF
];
1229 guint32 cols
[MONO_ASSEMBLYREF_SIZE
];
1233 if (level
& MONO_VERIFY_ERROR
) {
1234 for (i
= 0; i
< t
->rows
; ++i
) {
1235 mono_metadata_decode_row (t
, i
, cols
, MONO_ASSEMBLYREF_SIZE
);
1236 if (!is_valid_assembly_flags (cols
[MONO_ASSEMBLYREF_FLAGS
]))
1237 ADD_ERROR (list
, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i
+ 1, cols
[MONO_ASSEMBLY_FLAGS
]));
1239 if (!is_valid_blob (image
, cols
[MONO_ASSEMBLYREF_PUBLIC_KEY
], FALSE
))
1240 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i
+ 1));
1242 if (!(p
= is_valid_string (image
, cols
[MONO_ASSEMBLYREF_CULTURE
], FALSE
))) {
1243 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i
+ 1));
1245 if (!is_valid_culture (p
))
1246 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p
, i
+ 1));
1249 if (cols
[MONO_ASSEMBLYREF_HASH_VALUE
] && !is_valid_blob (image
, cols
[MONO_ASSEMBLYREF_HASH_VALUE
], TRUE
))
1250 ADD_ERROR (list
, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i
+ 1));
1253 if (level
& MONO_VERIFY_WARNING
) {
1254 /* check for duplicated rows */
1255 for (i
= 0; i
< t
->rows
; ++i
) {
1262 verify_class_layout_table (MonoImage
*image
, GSList
*list
, int level
)
1264 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_CLASSLAYOUT
];
1265 MonoTableInfo
*tdef
= &image
->tables
[MONO_TABLE_TYPEDEF
];
1266 guint32 cols
[MONO_CLASS_LAYOUT_SIZE
];
1269 if (level
& MONO_VERIFY_ERROR
) {
1270 for (i
= 0; i
< t
->rows
; ++i
) {
1271 mono_metadata_decode_row (t
, i
, cols
, MONO_CLASS_LAYOUT_SIZE
);
1273 if (cols
[MONO_CLASS_LAYOUT_PARENT
] > tdef
->rows
|| !cols
[MONO_CLASS_LAYOUT_PARENT
]) {
1274 ADD_ERROR (list
, g_strdup_printf ("Parent in class layout is invalid in row %d", i
+ 1));
1276 value
= mono_metadata_decode_row_col (tdef
, cols
[MONO_CLASS_LAYOUT_PARENT
] - 1, MONO_TYPEDEF_FLAGS
);
1277 if (value
& TYPE_ATTRIBUTE_INTERFACE
)
1278 ADD_ERROR (list
, g_strdup_printf ("Parent in class layout row %d is an interface", i
+ 1));
1279 if (value
& TYPE_ATTRIBUTE_AUTO_LAYOUT
)
1280 ADD_ERROR (list
, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i
+ 1));
1281 if (value
& TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT
) {
1282 switch (cols
[MONO_CLASS_LAYOUT_PACKING_SIZE
]) {
1283 case 0: case 1: case 2: case 4: case 8: case 16:
1284 case 32: case 64: case 128: break;
1286 ADD_ERROR (list
, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols
[MONO_CLASS_LAYOUT_PACKING_SIZE
], i
+ 1));
1288 } else if (value
& TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) {
1290 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
1291 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
1292 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));
1296 * FIXME: we need to check that if class size != 0,
1297 * it needs to be greater than the class calculated size.
1298 * If parent is a valuetype it also needs to be smaller than
1299 * 1 MByte (0x100000 bytes).
1300 * To do both these checks we need to load the referenced
1301 * assemblies, though (the spec claims we didn't have to, bah).
1304 * We need to check that the parent types have the same layout
1315 verify_constant_table (MonoImage
*image
, GSList
*list
, int level
)
1317 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_CONSTANT
];
1318 guint32 cols
[MONO_CONSTANT_SIZE
];
1320 GHashTable
*dups
= g_hash_table_new (NULL
, NULL
);
1322 for (i
= 0; i
< t
->rows
; ++i
) {
1323 mono_metadata_decode_row (t
, i
, cols
, MONO_CONSTANT_SIZE
);
1325 if (level
& MONO_VERIFY_ERROR
)
1326 if (g_hash_table_lookup (dups
, GUINT_TO_POINTER (cols
[MONO_CONSTANT_PARENT
])))
1327 ADD_ERROR (list
, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols
[MONO_CONSTANT_PARENT
], i
+ 1));
1328 g_hash_table_insert (dups
, GUINT_TO_POINTER (cols
[MONO_CONSTANT_PARENT
]),
1329 GUINT_TO_POINTER (cols
[MONO_CONSTANT_PARENT
]));
1331 switch (cols
[MONO_CONSTANT_TYPE
]) {
1332 case MONO_TYPE_U1
: /* LAMESPEC: it says I1...*/
1336 if (level
& MONO_VERIFY_CLS
)
1337 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));
1338 case MONO_TYPE_BOOLEAN
:
1339 case MONO_TYPE_CHAR
:
1346 case MONO_TYPE_STRING
:
1347 case MONO_TYPE_CLASS
:
1350 if (level
& MONO_VERIFY_ERROR
)
1351 ADD_ERROR (list
, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols
[MONO_CONSTANT_TYPE
], i
+ 1));
1353 if (level
& MONO_VERIFY_ERROR
) {
1354 value
= cols
[MONO_CONSTANT_PARENT
] >> MONO_HASCONSTANT_BITS
;
1355 switch (cols
[MONO_CONSTANT_PARENT
] & MONO_HASCONSTANT_MASK
) {
1356 case MONO_HASCONSTANT_FIEDDEF
:
1357 if (value
> image
->tables
[MONO_TABLE_FIELD
].rows
)
1358 ADD_ERROR (list
, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i
+ 1));
1360 case MONO_HASCONSTANT_PARAM
:
1361 if (value
> image
->tables
[MONO_TABLE_PARAM
].rows
)
1362 ADD_ERROR (list
, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i
+ 1));
1364 case MONO_HASCONSTANT_PROPERTY
:
1365 if (value
> image
->tables
[MONO_TABLE_PROPERTY
].rows
)
1366 ADD_ERROR (list
, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i
+ 1));
1369 ADD_ERROR (list
, g_strdup_printf ("Parent is invalid in Constant row %d", i
+ 1));
1373 if (level
& MONO_VERIFY_CLS
) {
1375 * FIXME: verify types is consistent with the enum type
1376 * is parent is an enum.
1380 g_hash_table_destroy (dups
);
1385 verify_event_map_table (MonoImage
*image
, GSList
*list
, int level
)
1387 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EVENTMAP
];
1388 guint32 cols
[MONO_EVENT_MAP_SIZE
];
1389 guint32 i
, last_event
;
1390 GHashTable
*dups
= g_hash_table_new (NULL
, NULL
);
1394 for (i
= 0; i
< t
->rows
; ++i
) {
1395 mono_metadata_decode_row (t
, i
, cols
, MONO_EVENT_MAP_SIZE
);
1396 if (level
& MONO_VERIFY_ERROR
)
1397 if (g_hash_table_lookup (dups
, GUINT_TO_POINTER (cols
[MONO_EVENT_MAP_PARENT
])))
1398 ADD_ERROR (list
, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols
[MONO_EVENT_MAP_PARENT
], i
+ 1));
1399 g_hash_table_insert (dups
, GUINT_TO_POINTER (cols
[MONO_EVENT_MAP_PARENT
]),
1400 GUINT_TO_POINTER (cols
[MONO_EVENT_MAP_PARENT
]));
1401 if (level
& MONO_VERIFY_ERROR
) {
1402 if (cols
[MONO_EVENT_MAP_PARENT
] > image
->tables
[MONO_TABLE_TYPEDEF
].rows
)
1403 ADD_ERROR (list
, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols
[MONO_EVENT_MAP_PARENT
], i
+ 1));
1404 if (cols
[MONO_EVENT_MAP_EVENTLIST
] > image
->tables
[MONO_TABLE_EVENT
].rows
)
1405 ADD_ERROR (list
, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols
[MONO_EVENT_MAP_EVENTLIST
], i
+ 1));
1407 if (cols
[MONO_EVENT_MAP_EVENTLIST
] <= last_event
)
1408 ADD_ERROR (list
, g_strdup_printf ("EventList overlap in Event Map row %d", i
+ 1));
1409 last_event
= cols
[MONO_EVENT_MAP_EVENTLIST
];
1413 g_hash_table_destroy (dups
);
1418 verify_event_table (MonoImage
*image
, GSList
*list
, int level
)
1420 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EVENT
];
1421 guint32 cols
[MONO_EVENT_SIZE
];
1425 for (i
= 0; i
< t
->rows
; ++i
) {
1426 mono_metadata_decode_row (t
, i
, cols
, MONO_EVENT_SIZE
);
1428 if (cols
[MONO_EVENT_FLAGS
] & ~(EVENT_SPECIALNAME
|EVENT_RTSPECIALNAME
)) {
1429 if (level
& MONO_VERIFY_ERROR
)
1430 ADD_ERROR (list
, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols
[MONO_EVENT_FLAGS
], i
+ 1));
1432 if (!(p
= is_valid_string (image
, cols
[MONO_EVENT_NAME
], TRUE
))) {
1433 if (level
& MONO_VERIFY_ERROR
)
1434 ADD_ERROR (list
, g_strdup_printf ("Invalid name in Event row %d", i
+ 1));
1436 if (level
& MONO_VERIFY_CLS
) {
1437 if (!is_valid_cls_ident (p
))
1438 ADD_WARN (list
, MONO_VERIFY_CLS
, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p
, i
+ 1));
1442 if (level
& MONO_VERIFY_ERROR
&& cols
[MONO_EVENT_TYPE
]) {
1443 value
= cols
[MONO_EVENT_TYPE
] >> MONO_TYPEDEFORREF_BITS
;
1444 switch (cols
[MONO_EVENT_TYPE
] & MONO_TYPEDEFORREF_MASK
) {
1445 case MONO_TYPEDEFORREF_TYPEDEF
:
1446 if (!value
|| value
> image
->tables
[MONO_TABLE_TYPEDEF
].rows
)
1447 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1449 case MONO_TYPEDEFORREF_TYPEREF
:
1450 if (!value
|| value
> image
->tables
[MONO_TABLE_TYPEREF
].rows
)
1451 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1453 case MONO_TYPEDEFORREF_TYPESPEC
:
1454 if (!value
|| value
> image
->tables
[MONO_TABLE_TYPESPEC
].rows
)
1455 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1458 ADD_ERROR (list
, g_strdup_printf ("Type invalid in Event row %d", i
+ 1));
1462 * FIXME: check that there is 1 add and remove row in methodsemantics
1463 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
1464 * these while checking methodsemantics).
1465 * check for duplicated names for the same type [ERROR]
1466 * check for CLS duplicate names for the same type [CLS]
1473 verify_field_table (MonoImage
*image
, GSList
*list
, int level
)
1475 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_FIELD
];
1476 guint32 cols
[MONO_FIELD_SIZE
];
1480 for (i
= 0; i
< t
->rows
; ++i
) {
1481 mono_metadata_decode_row (t
, i
, cols
, MONO_FIELD_SIZE
);
1483 * Check this field has only one owner and that the owner is not
1484 * an interface (done in verify_typedef_table() )
1486 flags
= cols
[MONO_FIELD_FLAGS
];
1487 switch (flags
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
) {
1488 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED
:
1489 case FIELD_ATTRIBUTE_PRIVATE
:
1490 case FIELD_ATTRIBUTE_FAM_AND_ASSEM
:
1491 case FIELD_ATTRIBUTE_ASSEMBLY
:
1492 case FIELD_ATTRIBUTE_FAMILY
:
1493 case FIELD_ATTRIBUTE_FAM_OR_ASSEM
:
1494 case FIELD_ATTRIBUTE_PUBLIC
:
1497 if (level
& MONO_VERIFY_ERROR
)
1498 ADD_ERROR (list
, g_strdup_printf ("Invalid access mask in Field row %d", i
+ 1));
1501 if (level
& MONO_VERIFY_ERROR
) {
1502 if ((flags
& FIELD_ATTRIBUTE_LITERAL
) && (flags
& FIELD_ATTRIBUTE_INIT_ONLY
))
1503 ADD_ERROR (list
, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i
+ 1));
1504 if ((flags
& FIELD_ATTRIBUTE_LITERAL
) && !(flags
& FIELD_ATTRIBUTE_STATIC
))
1505 ADD_ERROR (list
, g_strdup_printf ("Literal needs also Static set in Field row %d", i
+ 1));
1506 if ((flags
& FIELD_ATTRIBUTE_RT_SPECIAL_NAME
) && !(flags
& FIELD_ATTRIBUTE_SPECIAL_NAME
))
1507 ADD_ERROR (list
, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i
+ 1));
1509 * FIXME: check there is only one owner in the respective table.
1510 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
1511 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
1512 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
1515 if (!(p
= is_valid_string (image
, cols
[MONO_FIELD_NAME
], TRUE
))) {
1516 if (level
& MONO_VERIFY_ERROR
)
1517 ADD_ERROR (list
, g_strdup_printf ("Invalid name in Field row %d", i
+ 1));
1519 if (level
& MONO_VERIFY_CLS
) {
1520 if (!is_valid_cls_ident (p
))
1521 ADD_WARN (list
, MONO_VERIFY_CLS
, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p
, i
+ 1));
1526 * if owner is module needs to be static, access mask needs to be compilercontrolled,
1527 * public or private (not allowed in cls mode).
1528 * if owner is an enum ...
1537 verify_file_table (MonoImage
*image
, GSList
*list
, int level
)
1539 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_FILE
];
1540 guint32 cols
[MONO_FILE_SIZE
];
1543 GHashTable
*dups
= g_hash_table_new (g_str_hash
, g_str_equal
);
1545 for (i
= 0; i
< t
->rows
; ++i
) {
1546 mono_metadata_decode_row (t
, i
, cols
, MONO_FILE_SIZE
);
1547 if (level
& MONO_VERIFY_ERROR
) {
1548 if (cols
[MONO_FILE_FLAGS
] != FILE_CONTAINS_METADATA
&& cols
[MONO_FILE_FLAGS
] != FILE_CONTAINS_NO_METADATA
)
1549 ADD_ERROR (list
, g_strdup_printf ("Invalid flags in File row %d", i
+ 1));
1550 if (!is_valid_blob (image
, cols
[MONO_FILE_HASH_VALUE
], TRUE
))
1551 ADD_ERROR (list
, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i
+ 1));
1553 if (!(p
= is_valid_string (image
, cols
[MONO_FILE_NAME
], TRUE
))) {
1554 if (level
& MONO_VERIFY_ERROR
)
1555 ADD_ERROR (list
, g_strdup_printf ("Invalid name in File row %d", i
+ 1));
1557 if (level
& MONO_VERIFY_ERROR
) {
1558 if (!is_valid_filename (p
))
1559 ADD_ERROR (list
, g_strdup_printf ("Invalid name '%s` in File row %d", p
, i
+ 1));
1560 else if (g_hash_table_lookup (dups
, p
)) {
1561 ADD_ERROR (list
, g_strdup_printf ("Duplicate name '%s` in File row %d", p
, i
+ 1));
1563 g_hash_table_insert (dups
, (gpointer
)p
, (gpointer
)p
);
1567 * FIXME: I don't understand what this means:
1568 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
1569 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
1573 if (level
& MONO_VERIFY_WARNING
) {
1574 if (!t
->rows
&& image
->tables
[MONO_TABLE_EXPORTEDTYPE
].rows
)
1575 ADD_WARN (list
, MONO_VERIFY_WARNING
, g_strdup ("ExportedType table should be empty if File table is empty"));
1577 g_hash_table_destroy (dups
);
1582 verify_moduleref_table (MonoImage
*image
, GSList
*list
, int level
)
1584 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_MODULEREF
];
1585 MonoTableInfo
*tfile
= &image
->tables
[MONO_TABLE_FILE
];
1586 guint32 cols
[MONO_MODULEREF_SIZE
];
1588 guint32 found
, i
, j
, value
;
1589 GHashTable
*dups
= g_hash_table_new (g_str_hash
, g_str_equal
);
1591 for (i
= 0; i
< t
->rows
; ++i
) {
1592 mono_metadata_decode_row (t
, i
, cols
, MONO_MODULEREF_SIZE
);
1593 if (!(p
= is_valid_string (image
, cols
[MONO_MODULEREF_NAME
], TRUE
))) {
1594 if (level
& MONO_VERIFY_ERROR
)
1595 ADD_ERROR (list
, g_strdup_printf ("Invalid name in ModuleRef row %d", i
+ 1));
1597 if (level
& MONO_VERIFY_ERROR
) {
1598 if (!is_valid_filename (p
))
1599 ADD_ERROR (list
, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p
, i
+ 1));
1600 else if (g_hash_table_lookup (dups
, p
)) {
1601 ADD_WARN (list
, MONO_VERIFY_WARNING
, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p
, i
+ 1));
1602 g_hash_table_insert (dups
, (gpointer
)p
, (gpointer
)p
);
1604 for (j
= 0; j
< tfile
->rows
; ++j
) {
1605 value
= mono_metadata_decode_row_col (tfile
, j
, MONO_FILE_NAME
);
1606 if ((pf
= is_valid_string (image
, value
, TRUE
)))
1607 if (strcmp (p
, pf
) == 0) {
1613 ADD_ERROR (list
, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p
, i
+ 1));
1618 g_hash_table_destroy (dups
);
1623 verify_standalonesig_table (MonoImage
*image
, GSList
*list
, int level
)
1625 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_STANDALONESIG
];
1626 guint32 cols
[MONO_STAND_ALONE_SIGNATURE_SIZE
];
1630 for (i
= 0; i
< t
->rows
; ++i
) {
1631 mono_metadata_decode_row (t
, i
, cols
, MONO_STAND_ALONE_SIGNATURE_SIZE
);
1632 if (level
& MONO_VERIFY_ERROR
) {
1633 if (!is_valid_blob (image
, cols
[MONO_STAND_ALONE_SIGNATURE
], TRUE
)) {
1634 ADD_ERROR (list
, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i
+ 1));
1636 p
= mono_metadata_blob_heap (image
, cols
[MONO_STAND_ALONE_SIGNATURE
]);
1637 /* FIXME: check it's a valid locals or method sig.*/
1645 mono_image_verify_tables (MonoImage
*image
, int level
)
1647 GSList
*error_list
= NULL
;
1649 error_list
= verify_assembly_table (image
, error_list
, level
);
1651 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
1652 * AssemblyRefProcessor should be ignored,
1653 * though we may want to emit a warning, since it should not
1654 * be present in a PE file.
1656 error_list
= verify_assemblyref_table (image
, error_list
, level
);
1657 error_list
= verify_class_layout_table (image
, error_list
, level
);
1658 error_list
= verify_constant_table (image
, error_list
, level
);
1660 * cutom attribute, declsecurity
1662 error_list
= verify_event_map_table (image
, error_list
, level
);
1663 error_list
= verify_event_table (image
, error_list
, level
);
1664 error_list
= verify_field_table (image
, error_list
, level
);
1665 error_list
= verify_file_table (image
, error_list
, level
);
1666 error_list
= verify_moduleref_table (image
, error_list
, level
);
1667 error_list
= verify_standalonesig_table (image
, error_list
, level
);
1669 return g_slist_reverse (error_list
);
1672 #define ADD_INVALID(list,msg) \
1674 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1675 vinfo->status = MONO_VERIFY_ERROR; \
1676 vinfo->message = (msg); \
1677 (list) = g_slist_prepend ((list), vinfo); \
1678 /*G_BREAKPOINT ();*/ \
1682 #define CHECK_STACK_UNDERFLOW(num) \
1684 if (cur_stack < (num)) \
1685 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1688 #define CHECK_STACK_OVERFLOW() \
1690 if (cur_stack >= max_stack) \
1691 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1696 in_any_block (MonoMethodHeader
*header
, guint offset
)
1699 MonoExceptionClause
*clause
;
1701 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1702 clause
= &header
->clauses
[i
];
1703 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1705 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1707 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1714 * in_any_exception_block:
1716 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1719 in_any_exception_block (MonoMethodHeader
*header
, guint offset
)
1722 MonoExceptionClause
*clause
;
1724 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1725 clause
= &header
->clauses
[i
];
1726 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1728 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1735 * is_valid_branch_instruction:
1737 * Verify if it's valid to perform a branch from @offset to @target.
1738 * This should be used with br and brtrue/false.
1739 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1740 * The major diferent from other similiar functions is that branching into a
1741 * finally/fault block is invalid instead of just unverifiable.
1744 is_valid_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1747 MonoExceptionClause
*clause
;
1749 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1750 clause
= &header
->clauses
[i
];
1751 /*branching into a finally block is invalid*/
1752 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
) &&
1753 !MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1754 MONO_OFFSET_IN_HANDLER (clause
, target
))
1757 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1759 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1761 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1768 * is_valid_cmp_branch_instruction:
1770 * Verify if it's valid to perform a branch from @offset to @target.
1771 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1772 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1774 * The major diferences from other similar functions are that most errors lead to invalid
1775 * code and only branching out of finally, filter or fault clauses is unverifiable.
1778 is_valid_cmp_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1781 MonoExceptionClause
*clause
;
1783 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1784 clause
= &header
->clauses
[i
];
1785 /*branching out of a handler or finally*/
1786 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
&&
1787 MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1788 !MONO_OFFSET_IN_HANDLER (clause
, target
))
1791 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1793 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1795 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1802 * A leave can't escape a finally block
1805 is_correct_leave (MonoMethodHeader
*header
, guint offset
, guint target
)
1808 MonoExceptionClause
*clause
;
1810 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1811 clause
= &header
->clauses
[i
];
1812 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
&& MONO_OFFSET_IN_HANDLER (clause
, offset
) && !MONO_OFFSET_IN_HANDLER (clause
, target
))
1814 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1821 * A rethrow can't happen outside of a catch handler.
1824 is_correct_rethrow (MonoMethodHeader
*header
, guint offset
)
1827 MonoExceptionClause
*clause
;
1829 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1830 clause
= &header
->clauses
[i
];
1831 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1833 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1840 * An endfinally can't happen outside of a finally/fault handler.
1843 is_correct_endfinally (MonoMethodHeader
*header
, guint offset
)
1846 MonoExceptionClause
*clause
;
1848 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1849 clause
= &header
->clauses
[i
];
1850 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) && (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
))
1858 * An endfilter can only happens inside a filter clause.
1859 * In non-strict mode filter is allowed inside the handler clause too
1861 static MonoExceptionClause
*
1862 is_correct_endfilter (VerifyContext
*ctx
, guint offset
)
1865 MonoExceptionClause
*clause
;
1867 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1868 clause
= &ctx
->header
->clauses
[i
];
1869 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_FILTER
)
1871 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1873 if (!IS_STRICT_MODE (ctx
) && MONO_OFFSET_IN_HANDLER (clause
, offset
))
1881 * Non-strict endfilter can happens inside a try block or any handler block
1884 is_unverifiable_endfilter (VerifyContext
*ctx
, guint offset
)
1887 MonoExceptionClause
*clause
;
1889 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1890 clause
= &ctx
->header
->clauses
[i
];
1891 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1898 is_valid_bool_arg (ILStackDesc
*arg
)
1900 if (stack_slot_is_managed_pointer (arg
) || stack_slot_is_boxed_value (arg
) || stack_slot_is_null_literal (arg
))
1904 switch (stack_slot_get_underlying_type (arg
)) {
1907 case TYPE_NATIVE_INT
:
1911 g_assert (arg
->type
);
1912 switch (arg
->type
->type
) {
1913 case MONO_TYPE_CLASS
:
1914 case MONO_TYPE_STRING
:
1915 case MONO_TYPE_OBJECT
:
1916 case MONO_TYPE_SZARRAY
:
1917 case MONO_TYPE_ARRAY
:
1918 case MONO_TYPE_FNPTR
:
1921 case MONO_TYPE_GENERICINST
:
1922 /*We need to check if the container class
1923 * of the generic type is a valuetype, iow:
1924 * is it a "class Foo<T>" or a "struct Foo<T>"?
1926 return !arg
->type
->data
.generic_class
->container_class
->valuetype
;
1934 /*Type manipulation helper*/
1936 /*Returns the byref version of the supplied MonoType*/
1938 mono_type_get_type_byref (MonoType
*type
)
1942 return &mono_class_from_mono_type (type
)->this_arg
;
1946 /*Returns the byval version of the supplied MonoType*/
1948 mono_type_get_type_byval (MonoType
*type
)
1952 return &mono_class_from_mono_type (type
)->byval_arg
;
1956 mono_type_from_stack_slot (ILStackDesc
*slot
)
1958 if (stack_slot_is_managed_pointer (slot
))
1959 return mono_type_get_type_byref (slot
->type
);
1963 /*Stack manipulation code*/
1966 stack_init (VerifyContext
*ctx
, ILCodeDesc
*state
)
1968 if (state
->flags
& IL_CODE_FLAG_STACK_INITED
)
1971 state
->flags
|= IL_CODE_FLAG_STACK_INITED
;
1973 state
->stack
= g_new0 (ILStackDesc
, ctx
->max_stack
);
1977 stack_copy (ILCodeDesc
*to
, ILCodeDesc
*from
)
1979 to
->size
= from
->size
;
1980 memcpy (to
->stack
, from
->stack
, sizeof (ILStackDesc
) * from
->size
);
1984 copy_stack_value (ILStackDesc
*to
, ILStackDesc
*from
)
1986 to
->stype
= from
->stype
;
1987 to
->type
= from
->type
;
1988 to
->method
= from
->method
;
1992 check_underflow (VerifyContext
*ctx
, int size
)
1994 if (ctx
->eval
.size
< size
) {
1995 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size
, ctx
->eval
.size
, ctx
->ip_offset
));
2002 check_overflow (VerifyContext
*ctx
)
2004 if (ctx
->eval
.size
>= ctx
->max_stack
) {
2005 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx
->eval
.size
+ 1, ctx
->ip_offset
));
2011 /*This reject out PTR, FNPTR and TYPEDBYREF*/
2013 check_unmanaged_pointer (VerifyContext
*ctx
, ILStackDesc
*value
)
2015 if (stack_slot_get_type (value
) == TYPE_PTR
) {
2016 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
2022 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
2024 check_unverifiable_type (VerifyContext
*ctx
, MonoType
*type
)
2026 if (type
->type
== MONO_TYPE_PTR
|| type
->type
== MONO_TYPE_FNPTR
) {
2027 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
2034 static ILStackDesc
*
2035 stack_push (VerifyContext
*ctx
)
2037 g_assert (ctx
->eval
.size
< ctx
->max_stack
);
2038 return & ctx
->eval
.stack
[ctx
->eval
.size
++];
2041 static ILStackDesc
*
2042 stack_push_val (VerifyContext
*ctx
, int stype
, MonoType
*type
)
2044 ILStackDesc
*top
= stack_push (ctx
);
2050 static ILStackDesc
*
2051 stack_pop (VerifyContext
*ctx
)
2054 g_assert (ctx
->eval
.size
> 0);
2055 ret
= ctx
->eval
.stack
+ --ctx
->eval
.size
;
2056 if ((ret
->stype
& UNINIT_THIS_MASK
) == UNINIT_THIS_MASK
)
2057 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx
->ip_offset
));
2061 /* This function allows to safely pop an unititialized this ptr from
2062 * the eval stack without marking the method as unverifiable.
2064 static ILStackDesc
*
2065 stack_pop_safe (VerifyContext
*ctx
)
2067 g_assert (ctx
->eval
.size
> 0);
2068 return ctx
->eval
.stack
+ --ctx
->eval
.size
;
2071 static ILStackDesc
*
2072 stack_push_stack_val (VerifyContext
*ctx
, ILStackDesc
*value
)
2074 ILStackDesc
*top
= stack_push (ctx
);
2075 copy_stack_value (top
, value
);
2079 /* Returns the MonoType associated with the token, or NULL if it is invalid.
2081 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
2084 get_boxable_mono_type (VerifyContext
* ctx
, int token
, const char *opcode
)
2089 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
2092 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
2093 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2097 if (type
->type
== MONO_TYPE_VOID
) {
2098 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2102 if (type
->type
== MONO_TYPE_TYPEDBYREF
)
2103 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2105 if (!(class = mono_class_from_mono_type (type
)))
2106 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode
, ctx
->ip_offset
));
2108 if (class->generic_container
&& type
->type
!= MONO_TYPE_GENERICINST
)
2109 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
));
2111 check_unverifiable_type (ctx
, type
);
2116 /*operation result tables */
2118 static const unsigned char bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2119 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2120 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2121 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2122 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2123 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2124 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2127 static const unsigned char add_table
[TYPE_MAX
][TYPE_MAX
] = {
2128 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2129 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2130 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2131 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2132 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2133 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2136 static const unsigned char sub_table
[TYPE_MAX
][TYPE_MAX
] = {
2137 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2138 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2139 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2140 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
2141 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2142 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2145 static const unsigned char int_bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2146 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2147 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2148 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2149 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2150 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2151 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2154 static const unsigned char shift_op_table
[TYPE_MAX
][TYPE_MAX
] = {
2155 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2156 {TYPE_I8
, TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2157 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2158 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2159 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2160 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2163 static const unsigned char cmp_br_op
[TYPE_MAX
][TYPE_MAX
] = {
2164 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2165 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2166 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2167 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
2168 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
2169 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2172 static const unsigned char cmp_br_eq_op
[TYPE_MAX
][TYPE_MAX
] = {
2173 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2174 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2175 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2176 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
2177 {TYPE_INV
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
2178 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
},
2181 static const unsigned char add_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
2182 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2183 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2184 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2185 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2186 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2187 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2190 static const unsigned char sub_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
2191 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2192 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2193 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2194 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2195 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
2196 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2199 static const unsigned char bin_ovf_table
[TYPE_MAX
][TYPE_MAX
] = {
2200 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2201 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2202 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2203 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2204 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2205 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
2208 #ifdef MONO_VERIFIER_DEBUG
2212 dump_stack_value (ILStackDesc
*value
)
2214 printf ("[(%x)(%x)", value
->type
->type
, value
->stype
);
2216 if (stack_slot_is_this_pointer (value
))
2219 if (stack_slot_is_boxed_value (value
))
2220 printf ("[boxed] ");
2222 if (stack_slot_is_null_literal (value
))
2225 if (stack_slot_is_managed_mutability_pointer (value
))
2226 printf ("Controled Mutability MP: ");
2228 if (stack_slot_is_managed_pointer (value
))
2229 printf ("Managed Pointer to: ");
2231 switch (stack_slot_get_underlying_type (value
)) {
2233 printf ("invalid type]");
2241 case TYPE_NATIVE_INT
:
2242 printf ("native int]");
2245 printf ("float64]");
2248 printf ("unmanaged pointer]");
2251 switch (value
->type
->type
) {
2252 case MONO_TYPE_CLASS
:
2253 case MONO_TYPE_VALUETYPE
:
2254 printf ("complex] (%s)", value
->type
->data
.klass
->name
);
2256 case MONO_TYPE_STRING
:
2257 printf ("complex] (string)");
2259 case MONO_TYPE_OBJECT
:
2260 printf ("complex] (object)");
2262 case MONO_TYPE_SZARRAY
:
2263 printf ("complex] (%s [])", value
->type
->data
.klass
->name
);
2265 case MONO_TYPE_ARRAY
:
2266 printf ("complex] (%s [%d %d %d])",
2267 value
->type
->data
.array
->eklass
->name
,
2268 value
->type
->data
.array
->rank
,
2269 value
->type
->data
.array
->numsizes
,
2270 value
->type
->data
.array
->numlobounds
);
2272 case MONO_TYPE_GENERICINST
:
2273 printf ("complex] (inst of %s )", value
->type
->data
.generic_class
->container_class
->name
);
2276 printf ("complex] (type generic param !%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
2278 case MONO_TYPE_MVAR
:
2279 printf ("complex] (method generic param !!%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
2282 //should be a boxed value
2283 char * name
= mono_type_full_name (value
->type
);
2284 printf ("complex] %s", name
);
2290 printf ("unknown stack %x type]\n", value
->stype
);
2291 g_assert_not_reached ();
2296 dump_stack_state (ILCodeDesc
*state
)
2300 printf ("(%d) ", state
->size
);
2301 for (i
= 0; i
< state
->size
; ++i
)
2302 dump_stack_value (state
->stack
+ i
);
2307 /*Returns TRUE if candidate array type can be assigned to target.
2308 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
2311 is_array_type_compatible (MonoType
*target
, MonoType
*candidate
)
2313 MonoArrayType
*left
= target
->data
.array
;
2314 MonoArrayType
*right
= candidate
->data
.array
;
2316 g_assert (target
->type
== MONO_TYPE_ARRAY
);
2317 g_assert (candidate
->type
== MONO_TYPE_ARRAY
);
2319 if (left
->rank
!= right
->rank
)
2322 return mono_class_is_assignable_from (left
->eklass
, right
->eklass
);
2326 get_stack_type (MonoType
*type
)
2329 int type_kind
= type
->type
;
2331 mask
= POINTER_MASK
;
2332 /*TODO handle CMMP_MASK */
2335 switch (type_kind
) {
2338 case MONO_TYPE_BOOLEAN
:
2341 case MONO_TYPE_CHAR
:
2344 return TYPE_I4
| mask
;
2348 return TYPE_NATIVE_INT
| mask
;
2350 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
2351 case MONO_TYPE_FNPTR
:
2353 case MONO_TYPE_TYPEDBYREF
:
2354 return TYPE_PTR
| mask
;
2357 case MONO_TYPE_MVAR
:
2359 case MONO_TYPE_CLASS
:
2360 case MONO_TYPE_STRING
:
2361 case MONO_TYPE_OBJECT
:
2362 case MONO_TYPE_SZARRAY
:
2363 case MONO_TYPE_ARRAY
:
2364 return TYPE_COMPLEX
| mask
;
2366 case MONO_TYPE_GENERICINST
:
2367 if (mono_type_is_enum_type (type
)) {
2368 type
= mono_type_get_underlying_type_any (type
);
2369 type_kind
= type
->type
;
2372 return TYPE_COMPLEX
| mask
;
2377 return TYPE_I8
| mask
;
2381 return TYPE_R8
| mask
;
2383 case MONO_TYPE_VALUETYPE
:
2384 if (mono_type_is_enum_type (type
)) {
2385 type
= mono_type_get_underlying_type_any (type
);
2386 type_kind
= type
->type
;
2389 return TYPE_COMPLEX
| mask
;
2393 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type
->type
); );
2394 g_assert_not_reached ();
2399 /* convert MonoType to ILStackDesc format (stype) */
2401 set_stack_value (VerifyContext
*ctx
, ILStackDesc
*stack
, MonoType
*type
, int take_addr
)
2404 int type_kind
= type
->type
;
2406 if (type
->byref
|| take_addr
)
2407 mask
= POINTER_MASK
;
2408 /* TODO handle CMMP_MASK */
2413 switch (type_kind
) {
2416 case MONO_TYPE_BOOLEAN
:
2419 case MONO_TYPE_CHAR
:
2422 stack
->stype
= TYPE_I4
| mask
;
2426 stack
->stype
= TYPE_NATIVE_INT
| mask
;
2429 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2430 case MONO_TYPE_FNPTR
:
2432 case MONO_TYPE_TYPEDBYREF
:
2433 stack
->stype
= TYPE_PTR
| mask
;
2436 case MONO_TYPE_CLASS
:
2437 case MONO_TYPE_STRING
:
2438 case MONO_TYPE_OBJECT
:
2439 case MONO_TYPE_SZARRAY
:
2440 case MONO_TYPE_ARRAY
:
2443 case MONO_TYPE_MVAR
:
2444 stack
->stype
= TYPE_COMPLEX
| mask
;
2447 case MONO_TYPE_GENERICINST
:
2448 if (mono_type_is_enum_type (type
)) {
2449 type
= mono_type_get_underlying_type_any (type
);
2450 type_kind
= type
->type
;
2453 stack
->stype
= TYPE_COMPLEX
| mask
;
2459 stack
->stype
= TYPE_I8
| mask
;
2463 stack
->stype
= TYPE_R8
| mask
;
2465 case MONO_TYPE_VALUETYPE
:
2466 if (mono_type_is_enum_type (type
)) {
2467 type
= mono_type_get_underlying_type_any (type
);
2468 type_kind
= type
->type
;
2471 stack
->stype
= TYPE_COMPLEX
| mask
;
2475 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type
->type
); );
2476 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type
->type
, ctx
->ip_offset
));
2483 * init_stack_with_value_at_exception_boundary:
2485 * Initialize the stack and push a given type.
2486 * The instruction is marked as been on the exception boundary.
2489 init_stack_with_value_at_exception_boundary (VerifyContext
*ctx
, ILCodeDesc
*code
, MonoClass
*klass
)
2492 MonoType
*type
= mono_class_inflate_generic_type_checked (&klass
->byval_arg
, ctx
->generic_context
, &error
);
2494 if (!mono_error_ok (&error
)) {
2495 char *name
= mono_type_get_full_name (klass
);
2496 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid class %s used for exception", name
));
2498 mono_error_cleanup (&error
);
2502 stack_init (ctx
, code
);
2503 set_stack_value (ctx
, code
->stack
, type
, FALSE
);
2504 ctx
->exception_types
= g_slist_prepend (ctx
->exception_types
, type
);
2506 code
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
2507 if (mono_type_is_generic_argument (type
))
2508 code
->stack
->stype
|= BOXED_MASK
;
2511 /*Verify if type 'candidate' can be stored in type 'target'.
2513 * If strict, check for the underlying type and not the verification stack types
2516 verify_type_compatibility_full (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
, gboolean strict
)
2518 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2519 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2521 MonoType
*original_candidate
= candidate
;
2522 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target
), mono_type_full_name (candidate
), strict
); );
2524 /*only one is byref */
2525 if (candidate
->byref
^ target
->byref
) {
2526 /* converting from native int to byref*/
2527 if (get_stack_type (candidate
) == TYPE_NATIVE_INT
&& target
->byref
) {
2528 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("using byref native int at 0x%04x", ctx
->ip_offset
));
2533 strict
|= target
->byref
;
2534 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2535 candidate
= mono_type_get_underlying_type_any (candidate
);
2538 switch (target
->type
) {
2539 case MONO_TYPE_VOID
:
2540 return candidate
->type
== MONO_TYPE_VOID
;
2543 case MONO_TYPE_BOOLEAN
:
2545 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I1
, MONO_TYPE_U1
, MONO_TYPE_BOOLEAN
);
2548 case MONO_TYPE_CHAR
:
2550 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I2
, MONO_TYPE_U2
, MONO_TYPE_CHAR
);
2552 case MONO_TYPE_U4
: {
2553 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2554 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2556 return is_native_int
|| is_int4
;
2557 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2562 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I8
, MONO_TYPE_U8
);
2567 return candidate
->type
== target
->type
;
2568 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_R4
, MONO_TYPE_R8
);
2572 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2573 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2575 return is_native_int
|| is_int4
;
2576 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2580 if (candidate
->type
!= MONO_TYPE_PTR
)
2582 /* check the underlying type */
2583 return verify_type_compatibility_full (ctx
, target
->data
.type
, candidate
->data
.type
, TRUE
);
2585 case MONO_TYPE_FNPTR
: {
2586 MonoMethodSignature
*left
, *right
;
2587 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2590 left
= mono_type_get_signature (target
);
2591 right
= mono_type_get_signature (candidate
);
2592 return mono_metadata_signature_equal (left
, right
) && left
->call_convention
== right
->call_convention
;
2595 case MONO_TYPE_GENERICINST
: {
2596 MonoClass
*target_klass
;
2597 MonoClass
*candidate_klass
;
2598 if (mono_type_is_enum_type (target
)) {
2599 target
= mono_type_get_underlying_type_any (target
);
2602 target_klass
= mono_class_from_mono_type (target
);
2603 candidate_klass
= mono_class_from_mono_type (candidate
);
2604 if (mono_class_is_nullable (target_klass
)) {
2605 if (!mono_class_is_nullable (candidate_klass
))
2607 return target_klass
== candidate_klass
;
2610 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2613 case MONO_TYPE_STRING
:
2614 return candidate
->type
== MONO_TYPE_STRING
;
2616 case MONO_TYPE_CLASS
:
2618 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2619 * to take boxing status into account.
2621 if (mono_type_is_generic_argument (original_candidate
))
2623 /* If candidate is an enum it should return true for System.Enum and supertypes.
2624 * That's why here we use the original type and not the underlying type.
2626 return mono_class_is_assignable_from (target
->data
.klass
, mono_class_from_mono_type (original_candidate
));
2628 case MONO_TYPE_OBJECT
:
2629 return MONO_TYPE_IS_REFERENCE (candidate
);
2631 case MONO_TYPE_SZARRAY
: {
2634 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2637 left
= mono_class_from_mono_type (target
)->element_class
;
2638 right
= mono_class_from_mono_type (candidate
)->element_class
;
2639 return mono_class_is_assignable_from (left
, right
);
2642 case MONO_TYPE_ARRAY
:
2643 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2645 return is_array_type_compatible (target
, candidate
);
2647 case MONO_TYPE_TYPEDBYREF
:
2648 return candidate
->type
== MONO_TYPE_TYPEDBYREF
;
2650 case MONO_TYPE_VALUETYPE
: {
2651 MonoClass
*target_klass
= mono_class_from_mono_type (target
);
2652 MonoClass
*candidate_klass
= mono_class_from_mono_type (candidate
);
2654 if (target_klass
== candidate_klass
)
2656 if (mono_type_is_enum_type (target
)) {
2657 target
= mono_type_get_underlying_type_any (target
);
2664 if (candidate
->type
!= MONO_TYPE_VAR
)
2666 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2668 case MONO_TYPE_MVAR
:
2669 if (candidate
->type
!= MONO_TYPE_MVAR
)
2671 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2674 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target
->type
); );
2675 g_assert_not_reached ();
2684 verify_type_compatibility (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
)
2686 return verify_type_compatibility_full (ctx
, target
, candidate
, FALSE
);
2690 * Returns the generic param bound to the context been verified.
2693 static MonoGenericParam
*
2694 get_generic_param (VerifyContext
*ctx
, MonoType
*param
)
2696 guint16 param_num
= mono_type_get_generic_param_num (param
);
2697 if (param
->type
== MONO_TYPE_VAR
) {
2698 if (!ctx
->generic_context
->class_inst
|| ctx
->generic_context
->class_inst
->type_argc
<= param_num
) {
2699 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic type argument %d", param_num
));
2702 return ctx
->generic_context
->class_inst
->type_argv
[param_num
]->data
.generic_param
;
2705 /*param must be a MVAR */
2706 if (!ctx
->generic_context
->method_inst
|| ctx
->generic_context
->method_inst
->type_argc
<= param_num
) {
2707 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic method argument %d", param_num
));
2710 return ctx
->generic_context
->method_inst
->type_argv
[param_num
]->data
.generic_param
;
2714 * is_compatible_boxed_valuetype:
2716 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2718 * @type The source type. It it tested to be of the proper type.
2719 * @candidate type of the boxed valuetype.
2720 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2721 * @strict if TRUE candidate must be boxed compatible to the target type
2725 is_compatible_boxed_valuetype (VerifyContext
*ctx
, MonoType
*type
, MonoType
*candidate
, ILStackDesc
*stack
, gboolean strict
)
2727 if (!stack_slot_is_boxed_value (stack
))
2729 if (type
->byref
|| candidate
->byref
)
2732 if (mono_type_is_generic_argument (candidate
)) {
2733 MonoGenericParam
*param
= get_generic_param (ctx
, candidate
);
2735 for (class = mono_generic_param_info (param
)->constraints
; class && *class; ++class) {
2736 if (verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (& (*class)->byval_arg
), FALSE
))
2741 if (mono_type_is_generic_argument (type
))
2747 return MONO_TYPE_IS_REFERENCE (type
) && mono_class_is_assignable_from (mono_class_from_mono_type (type
), mono_class_from_mono_type (candidate
));
2751 verify_stack_type_compatibility_full (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
, gboolean drop_byref
, gboolean valuetype_must_be_boxed
)
2753 MonoType
*candidate
= mono_type_from_stack_slot (stack
);
2754 if (MONO_TYPE_IS_REFERENCE (type
) && !type
->byref
&& stack_slot_is_null_literal (stack
))
2757 if (is_compatible_boxed_valuetype (ctx
, type
, candidate
, stack
, TRUE
))
2760 if (valuetype_must_be_boxed
&& !stack_slot_is_boxed_value (stack
) && !MONO_TYPE_IS_REFERENCE (candidate
))
2763 if (!valuetype_must_be_boxed
&& stack_slot_is_boxed_value (stack
))
2767 return verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (candidate
), FALSE
);
2769 return verify_type_compatibility_full (ctx
, type
, candidate
, FALSE
);
2773 verify_stack_type_compatibility (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
)
2775 return verify_stack_type_compatibility_full (ctx
, type
, stack
, FALSE
, FALSE
);
2779 mono_delegate_type_equal (MonoType
*target
, MonoType
*candidate
)
2781 if (candidate
->byref
^ target
->byref
)
2784 switch (target
->type
) {
2785 case MONO_TYPE_VOID
:
2788 case MONO_TYPE_BOOLEAN
:
2791 case MONO_TYPE_CHAR
:
2800 case MONO_TYPE_STRING
:
2801 case MONO_TYPE_TYPEDBYREF
:
2802 return candidate
->type
== target
->type
;
2805 return mono_delegate_type_equal (target
->data
.type
, candidate
->data
.type
);
2807 case MONO_TYPE_FNPTR
:
2808 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2810 return mono_delegate_signature_equal (mono_type_get_signature (target
), mono_type_get_signature (candidate
), FALSE
);
2812 case MONO_TYPE_GENERICINST
: {
2813 MonoClass
*target_klass
;
2814 MonoClass
*candidate_klass
;
2815 target_klass
= mono_class_from_mono_type (target
);
2816 candidate_klass
= mono_class_from_mono_type (candidate
);
2817 /*FIXME handle nullables and enum*/
2818 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2820 case MONO_TYPE_OBJECT
:
2821 return MONO_TYPE_IS_REFERENCE (candidate
);
2823 case MONO_TYPE_CLASS
:
2824 return mono_class_is_assignable_from(target
->data
.klass
, mono_class_from_mono_type (candidate
));
2826 case MONO_TYPE_SZARRAY
:
2827 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2829 return mono_class_is_assignable_from (mono_class_from_mono_type (target
)->element_class
, mono_class_from_mono_type (candidate
)->element_class
);
2831 case MONO_TYPE_ARRAY
:
2832 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2834 return is_array_type_compatible (target
, candidate
);
2836 case MONO_TYPE_VALUETYPE
:
2837 /*FIXME handle nullables and enum*/
2838 return mono_class_from_mono_type (candidate
) == mono_class_from_mono_type (target
);
2841 return candidate
->type
== MONO_TYPE_VAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2844 case MONO_TYPE_MVAR
:
2845 return candidate
->type
== MONO_TYPE_MVAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2849 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target
->type
); );
2850 g_assert_not_reached ();
2856 mono_delegate_param_equal (MonoType
*delegate
, MonoType
*method
)
2858 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2861 return mono_delegate_type_equal (method
, delegate
);
2865 mono_delegate_ret_equal (MonoType
*delegate
, MonoType
*method
)
2867 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2870 return mono_delegate_type_equal (delegate
, method
);
2874 * mono_delegate_signature_equal:
2876 * Compare two signatures in the way expected by delegates.
2878 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2880 * FIXME can this function be eliminated and proper metadata functionality be used?
2883 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
)
2886 int method_offset
= is_static_ldftn
? 1 : 0;
2888 if (delegate_sig
->param_count
+ method_offset
!= method_sig
->param_count
)
2891 if (delegate_sig
->call_convention
!= method_sig
->call_convention
)
2894 for (i
= 0; i
< delegate_sig
->param_count
; i
++) {
2895 MonoType
*p1
= delegate_sig
->params
[i
];
2896 MonoType
*p2
= method_sig
->params
[i
+ method_offset
];
2898 if (!mono_delegate_param_equal (p1
, p2
))
2902 if (!mono_delegate_ret_equal (delegate_sig
->ret
, method_sig
->ret
))
2909 * verify_ldftn_delegate:
2911 * Verify properties of ldftn based delegates.
2914 verify_ldftn_delegate (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2916 MonoMethod
*method
= funptr
->method
;
2918 /*ldftn non-final virtuals only allowed if method is not static,
2919 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2920 * This rules doesn't apply if the object on stack is a boxed valuetype.
2922 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !(method
->klass
->flags
& TYPE_ATTRIBUTE_SEALED
) && !stack_slot_is_boxed_value (value
)) {
2923 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2924 if (IS_FAIL_FAST_MODE (ctx
) && ctx
->has_this_store
)
2925 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx
->ip_offset
));
2927 /*current method must not be static*/
2928 if (ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)
2929 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx
->ip_offset
));
2931 /*value is the this pointer, loaded using ldarg.0 */
2932 if (!stack_slot_is_this_pointer (value
))
2933 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
));
2935 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
;
2940 * verify_delegate_compatibility:
2942 * Verify delegate creation sequence.
2946 verify_delegate_compatibility (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2948 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2949 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2951 MonoMethod
*invoke
, *method
;
2952 const guint8
*ip
= ctx
->header
->code
;
2953 guint32 ip_offset
= ctx
->ip_offset
;
2954 gboolean is_static_ldftn
= FALSE
, is_first_arg_bound
= FALSE
;
2956 if (stack_slot_get_type (funptr
) != TYPE_PTR
|| !funptr
->method
) {
2957 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx
->ip_offset
));
2961 invoke
= mono_get_delegate_invoke (delegate
);
2962 method
= funptr
->method
;
2964 if (!method
|| !mono_method_signature (method
)) {
2965 char *name
= mono_type_get_full_name (delegate
);
2966 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name
, ctx
->ip_offset
));
2971 if (!invoke
|| !mono_method_signature (invoke
)) {
2972 char *name
= mono_type_get_full_name (delegate
);
2973 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name
, ctx
->ip_offset
));
2978 is_static_ldftn
= (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) && method
->flags
& METHOD_ATTRIBUTE_STATIC
;
2980 if (is_static_ldftn
)
2981 is_first_arg_bound
= mono_method_signature (invoke
)->param_count
+ 1 == mono_method_signature (method
)->param_count
;
2983 if (!mono_delegate_signature_equal (mono_method_signature (invoke
), mono_method_signature (method
), is_first_arg_bound
)) {
2984 char *fun_sig
= mono_signature_get_desc (mono_method_signature (method
), FALSE
);
2985 char *invoke_sig
= mono_signature_get_desc (mono_method_signature (invoke
), FALSE
);
2986 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
));
2988 g_free (invoke_sig
);
2992 * Delegate code sequences:
2998 * [-6] ldvirtftn token
3002 if (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) {
3003 verify_ldftn_delegate (ctx
, delegate
, value
, funptr
);
3004 } else if (ip_offset
> 6 && IS_VALID_OPCODE (7, CEE_DUP
) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN
)) {
3005 ctx
->code
[ip_offset
- 6].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
3007 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx
->ip_offset
));
3009 ctx
->code
[ip_offset
].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
3012 if (is_first_arg_bound
) {
3013 if (mono_method_signature (method
)->param_count
== 0 || !verify_stack_type_compatibility_full (ctx
, mono_method_signature (method
)->params
[0], value
, FALSE
, TRUE
))
3014 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
3016 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
3017 if (!stack_slot_is_null_literal (value
) && !is_first_arg_bound
)
3018 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx
->ip_offset
));
3020 if (!verify_stack_type_compatibility_full (ctx
, &method
->klass
->byval_arg
, value
, FALSE
, TRUE
) && !stack_slot_is_null_literal (value
))
3021 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
3025 if (stack_slot_get_type (value
) != TYPE_COMPLEX
)
3026 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx
->ip_offset
));
3028 #undef IS_VALID_OPCODE
3029 #undef IS_LOAD_FUN_PTR
3032 /* implement the opcode checks*/
3034 push_arg (VerifyContext
*ctx
, unsigned int arg
, int take_addr
)
3038 if (arg
>= ctx
->max_args
) {
3040 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
3042 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
3043 if (check_overflow (ctx
)) //FIXME: what sane value could we ever push?
3044 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3046 } else if (check_overflow (ctx
)) {
3047 /*We must let the value be pushed, otherwise we would get an underflow error*/
3048 check_unverifiable_type (ctx
, ctx
->params
[arg
]);
3049 if (ctx
->params
[arg
]->byref
&& take_addr
)
3050 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
3051 top
= stack_push (ctx
);
3052 if (!set_stack_value (ctx
, top
, ctx
->params
[arg
], take_addr
))
3055 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3057 ctx
->has_this_store
= TRUE
;
3059 top
->stype
|= THIS_POINTER_MASK
;
3060 if (mono_method_is_constructor (ctx
->method
) && !ctx
->super_ctor_called
&& !ctx
->method
->klass
->valuetype
)
3061 top
->stype
|= UNINIT_THIS_MASK
;
3067 push_local (VerifyContext
*ctx
, guint32 arg
, int take_addr
)
3069 if (arg
>= ctx
->num_locals
) {
3070 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local %d", arg
+ 1));
3071 } else if (check_overflow (ctx
)) {
3072 /*We must let the value be pushed, otherwise we would get an underflow error*/
3073 check_unverifiable_type (ctx
, ctx
->locals
[arg
]);
3074 if (ctx
->locals
[arg
]->byref
&& take_addr
)
3075 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
3077 set_stack_value (ctx
, stack_push (ctx
), ctx
->locals
[arg
], take_addr
);
3082 store_arg (VerifyContext
*ctx
, guint32 arg
)
3086 if (arg
>= ctx
->max_args
) {
3087 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
3088 if (check_underflow (ctx
, 1))
3093 if (check_underflow (ctx
, 1)) {
3094 value
= stack_pop (ctx
);
3095 if (!verify_stack_type_compatibility (ctx
, ctx
->params
[arg
], value
)) {
3096 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3099 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
))
3100 ctx
->has_this_store
= 1;
3104 store_local (VerifyContext
*ctx
, guint32 arg
)
3107 if (arg
>= ctx
->num_locals
) {
3108 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
3112 /*TODO verify definite assigment */
3113 if (check_underflow (ctx
, 1)) {
3114 value
= stack_pop(ctx
);
3115 if (!verify_stack_type_compatibility (ctx
, ctx
->locals
[arg
], value
)) {
3116 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
3117 stack_slot_get_name (value
),
3118 mono_type_get_stack_name (ctx
->locals
[arg
]),
3124 /*FIXME add and sub needs special care here*/
3126 do_binop (VerifyContext
*ctx
, unsigned int opcode
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
3128 ILStackDesc
*a
, *b
, *top
;
3129 int idxa
, idxb
, complexMerge
= 0;
3132 if (!check_underflow (ctx
, 2))
3134 b
= stack_pop (ctx
);
3135 a
= stack_pop (ctx
);
3137 idxa
= stack_slot_get_underlying_type (a
);
3138 if (stack_slot_is_managed_pointer (a
)) {
3143 idxb
= stack_slot_get_underlying_type (b
);
3144 if (stack_slot_is_managed_pointer (b
)) {
3151 res
= table
[idxa
][idxb
];
3153 VERIFIER_DEBUG ( printf ("binop res %d\n", res
); );
3154 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
3156 top
= stack_push (ctx
);
3157 if (res
== TYPE_INV
) {
3158 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
)));
3159 copy_stack_value (top
, a
);
3163 if (res
& NON_VERIFIABLE_RESULT
) {
3164 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
)));
3166 res
= res
& ~NON_VERIFIABLE_RESULT
;
3169 if (complexMerge
&& res
== TYPE_PTR
) {
3170 if (complexMerge
== 1)
3171 copy_stack_value (top
, a
);
3172 else if (complexMerge
== 2)
3173 copy_stack_value (top
, b
);
3175 * There is no need to merge the type of two pointers.
3176 * The only valid operation is subtraction, that returns a native
3177 * int as result and can be used with any 2 pointer kinds.
3178 * This is valid acording to Patition III 1.1.4
3187 do_boolean_branch_op (VerifyContext
*ctx
, int delta
)
3189 int target
= ctx
->ip_offset
+ delta
;
3192 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
3194 if (target
< 0 || target
>= ctx
->code_size
) {
3195 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx
->ip_offset
));
3199 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3201 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3204 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3208 ctx
->target
= target
;
3210 if (!check_underflow (ctx
, 1))
3213 top
= stack_pop (ctx
);
3214 if (!is_valid_bool_arg (top
))
3215 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
));
3217 check_unmanaged_pointer (ctx
, top
);
3221 stack_slot_is_complex_type_not_reference_type (ILStackDesc
*slot
)
3223 return stack_slot_get_type (slot
) == TYPE_COMPLEX
&& !MONO_TYPE_IS_REFERENCE (slot
->type
) && !stack_slot_is_boxed_value (slot
);
3227 do_branch_op (VerifyContext
*ctx
, signed int delta
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
3232 int target
= ctx
->ip_offset
+ delta
;
3234 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
3236 if (target
< 0 || target
>= ctx
->code_size
) {
3237 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
3241 switch (is_valid_cmp_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3242 case 1: /*FIXME use constants and not magic numbers.*/
3243 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3246 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3250 ctx
->target
= target
;
3252 if (!check_underflow (ctx
, 2))
3255 b
= stack_pop (ctx
);
3256 a
= stack_pop (ctx
);
3258 idxa
= stack_slot_get_underlying_type (a
);
3259 if (stack_slot_is_managed_pointer (a
))
3262 idxb
= stack_slot_get_underlying_type (b
);
3263 if (stack_slot_is_managed_pointer (b
))
3266 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3271 res
= table
[idxa
][idxb
];
3274 VERIFIER_DEBUG ( printf ("branch res %d\n", res
); );
3275 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
3277 if (res
== TYPE_INV
) {
3278 CODE_NOT_VERIFIABLE (ctx
,
3279 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
));
3280 } else if (res
& NON_VERIFIABLE_RESULT
) {
3281 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
));
3282 res
= res
& ~NON_VERIFIABLE_RESULT
;
3287 do_cmp_op (VerifyContext
*ctx
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
], guint32 opcode
)
3293 if (!check_underflow (ctx
, 2))
3295 b
= stack_pop (ctx
);
3296 a
= stack_pop (ctx
);
3298 if (opcode
== CEE_CGT_UN
) {
3299 if (stack_slot_get_type (a
) == TYPE_COMPLEX
&& stack_slot_get_type (b
) == TYPE_COMPLEX
) {
3300 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3305 idxa
= stack_slot_get_underlying_type (a
);
3306 if (stack_slot_is_managed_pointer (a
))
3309 idxb
= stack_slot_get_underlying_type (b
);
3310 if (stack_slot_is_managed_pointer (b
))
3313 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3318 res
= table
[idxa
][idxb
];
3321 if(res
== TYPE_INV
) {
3322 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
));
3323 } else if (res
& NON_VERIFIABLE_RESULT
) {
3324 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
));
3325 res
= res
& ~NON_VERIFIABLE_RESULT
;
3327 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3331 do_ret (VerifyContext
*ctx
)
3333 MonoType
*ret
= ctx
->signature
->ret
;
3334 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3335 if (ret
->type
!= MONO_TYPE_VOID
) {
3337 if (!check_underflow (ctx
, 1))
3340 top
= stack_pop(ctx
);
3342 if (!verify_stack_type_compatibility (ctx
, ctx
->signature
->ret
, top
)) {
3343 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx
->ip_offset
));
3347 if (ret
->byref
|| ret
->type
== MONO_TYPE_TYPEDBYREF
|| mono_type_is_value_type (ret
, "System", "ArgIterator") || mono_type_is_value_type (ret
, "System", "RuntimeArgumentHandle"))
3348 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx
->ip_offset
));
3351 if (ctx
->eval
.size
> 0) {
3352 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx
->eval
.size
, ctx
->ip_offset
));
3354 if (in_any_block (ctx
->header
, ctx
->ip_offset
))
3355 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx
->ip_offset
));
3359 * 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.
3360 * This is illegal but mono_get_method_full decoded it.
3361 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3364 do_invoke_method (VerifyContext
*ctx
, int method_token
, gboolean
virtual)
3367 MonoMethodSignature
*sig
;
3370 gboolean virt_check_this
= FALSE
;
3371 gboolean constrained
= ctx
->prefix_set
& PREFIX_CONSTRAINED
;
3373 if (!(method
= verifier_load_method (ctx
, method_token
, virtual ? "callvirt" : "call")))
3377 CLEAR_PREFIX (ctx
, PREFIX_CONSTRAINED
);
3379 if (method
->klass
->valuetype
) // && !constrained ???
3380 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx
->ip_offset
));
3382 if ((method
->flags
& METHOD_ATTRIBUTE_STATIC
))
3383 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx
->ip_offset
));
3386 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
3387 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx
->ip_offset
));
3389 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
)) {
3390 virt_check_this
= TRUE
;
3391 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_CALL_NONFINAL_VIRTUAL
;
3395 if (!(sig
= mono_method_get_signature_full (method
, ctx
->image
, method_token
, ctx
->generic_context
)))
3396 sig
= mono_method_get_signature (method
, ctx
->image
, method_token
);
3399 char *name
= mono_type_get_full_name (method
->klass
);
3400 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name
, method
->name
, ctx
->ip_offset
));
3405 param_count
= sig
->param_count
+ sig
->hasthis
;
3406 if (!check_underflow (ctx
, param_count
))
3409 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3410 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i
); );
3411 value
= stack_pop (ctx
);
3412 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3413 char *stack_name
= stack_slot_full_name (value
);
3414 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3415 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
));
3416 g_free (stack_name
);
3420 if (stack_slot_is_managed_mutability_pointer (value
))
3421 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
));
3423 if ((ctx
->prefix_set
& PREFIX_TAIL
) && stack_slot_is_managed_pointer (value
)) {
3424 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
));
3430 MonoType
*type
= &method
->klass
->byval_arg
;
3433 if (mono_method_is_constructor (method
) && !method
->klass
->valuetype
) {
3434 if (!mono_method_is_constructor (ctx
->method
))
3435 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx
->ip_offset
));
3436 if (method
->klass
!= ctx
->method
->klass
->parent
&& method
->klass
!= ctx
->method
->klass
)
3437 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
));
3439 ctx
->super_ctor_called
= TRUE
;
3440 value
= stack_pop_safe (ctx
);
3441 if ((value
->stype
& THIS_POINTER_MASK
) != THIS_POINTER_MASK
)
3442 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx
->ip_offset
));
3444 value
= stack_pop (ctx
);
3447 copy_stack_value (©
, value
);
3448 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3449 //Other parts of the code suffer from the same issue of
3450 copy
.type
= mono_type_get_type_byval (copy
.type
);
3451 copy
.stype
&= ~POINTER_MASK
;
3453 if (virt_check_this
&& !stack_slot_is_this_pointer (value
) && !(method
->klass
->valuetype
|| stack_slot_is_boxed_value (value
)))
3454 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
));
3456 if (constrained
&& virtual) {
3457 if (!stack_slot_is_managed_pointer (value
))
3458 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx
->ip_offset
));
3459 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value
->type
), ctx
->constrained_type
, TRUE
))
3460 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx
->ip_offset
));
3461 copy
.stype
|= BOXED_MASK
;
3463 if (stack_slot_is_managed_pointer (value
) && !mono_class_from_mono_type (value
->type
)->valuetype
)
3464 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
));
3466 if (!virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !method
->klass
->valuetype
&& !stack_slot_is_boxed_value (value
))
3467 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx
->ip_offset
));
3469 if (virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !stack_slot_is_boxed_value (value
))
3470 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx
->ip_offset
));
3472 if (method
->klass
->valuetype
&& (stack_slot_is_boxed_value (value
) || !stack_slot_is_managed_pointer (value
)))
3473 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
));
3475 if (!verify_stack_type_compatibility (ctx
, type
, ©
))
3476 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx
->ip_offset
));
3478 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, mono_class_from_mono_type (value
->type
))) {
3479 char *name
= mono_method_full_name (method
, TRUE
);
3480 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3484 } else if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3485 char *name
= mono_method_full_name (method
, TRUE
);
3486 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3490 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3491 if (check_overflow (ctx
)) {
3492 value
= stack_push (ctx
);
3493 set_stack_value (ctx
, value
, sig
->ret
, FALSE
);
3494 if ((ctx
->prefix_set
& PREFIX_READONLY
) && method
->klass
->rank
&& !strcmp (method
->name
, "Address")) {
3495 ctx
->prefix_set
&= ~PREFIX_READONLY
;
3496 value
->stype
|= CMMP_MASK
;
3501 if ((ctx
->prefix_set
& PREFIX_TAIL
)) {
3502 if (!mono_delegate_ret_equal (mono_method_signature (ctx
->method
)->ret
, sig
->ret
))
3503 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx
->ip_offset
));
3504 if (ctx
->header
->code
[ctx
->ip_offset
+ 5] != CEE_RET
)
3505 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx
->ip_offset
));
3511 do_push_static_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3513 MonoClassField
*field
;
3515 if (!check_overflow (ctx
))
3518 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3520 if (!(field
= verifier_load_field (ctx
, token
, &klass
, take_addr
? "ldsflda" : "ldsfld")))
3523 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3524 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx
->ip_offset
));
3527 /*taking the address of initonly field only works from the static constructor */
3528 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3529 !(field
->parent
== ctx
->method
->klass
&& (ctx
->method
->flags
& (METHOD_ATTRIBUTE_SPECIAL_NAME
| METHOD_ATTRIBUTE_STATIC
)) && !strcmp (".cctor", ctx
->method
->name
)))
3530 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3532 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3533 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3535 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3539 do_store_static_field (VerifyContext
*ctx
, int token
) {
3540 MonoClassField
*field
;
3543 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3545 if (!check_underflow (ctx
, 1))
3548 value
= stack_pop (ctx
);
3550 if (!(field
= verifier_load_field (ctx
, token
, &klass
, "stsfld")))
3553 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3554 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx
->ip_offset
));
3558 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3559 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx
->ip_offset
));
3563 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3564 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3566 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3567 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
));
3571 check_is_valid_type_for_field_ops (VerifyContext
*ctx
, int token
, ILStackDesc
*obj
, MonoClassField
**ret_field
, const char *opcode
)
3573 MonoClassField
*field
;
3575 gboolean is_pointer
;
3577 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3578 if (!(field
= verifier_load_field (ctx
, token
, &klass
, opcode
)))
3582 //the value on stack is going to be used as a pointer
3583 is_pointer
= stack_slot_get_type (obj
) == TYPE_PTR
|| (stack_slot_get_type (obj
) == TYPE_NATIVE_INT
&& !get_stack_type (&field
->parent
->byval_arg
));
3585 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3586 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx
->ip_offset
));
3589 g_assert (obj
->type
);
3591 /*The value on the stack must be a subclass of the defining type of the field*/
3592 /* we need to check if we can load the field from the stack value*/
3594 if (stack_slot_get_underlying_type (obj
) == TYPE_NATIVE_INT
)
3595 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx
->ip_offset
));
3597 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3598 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3600 if (!field
->parent
->valuetype
&& stack_slot_is_managed_pointer (obj
))
3601 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
));
3603 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3604 if (field
->parent
->valuetype
&& stack_slot_is_boxed_value (obj
))
3605 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
));
3607 if (!stack_slot_is_null_literal (obj
) && !verify_stack_type_compatibility_full (ctx
, &field
->parent
->byval_arg
, obj
, TRUE
, FALSE
))
3608 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx
->ip_offset
));
3610 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, mono_class_from_mono_type (obj
->type
)))
3611 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3614 check_unmanaged_pointer (ctx
, obj
);
3619 do_push_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3622 MonoClassField
*field
;
3625 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3627 if (!check_underflow (ctx
, 1))
3629 obj
= stack_pop_safe (ctx
);
3631 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, take_addr
? "ldflda" : "ldfld"))
3634 if (take_addr
&& field
->parent
->valuetype
&& !stack_slot_is_managed_pointer (obj
))
3635 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx
->ip_offset
));
3637 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3638 !(field
->parent
== ctx
->method
->klass
&& mono_method_is_constructor (ctx
->method
)))
3639 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3641 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3645 do_store_field (VerifyContext
*ctx
, int token
)
3647 ILStackDesc
*value
, *obj
;
3648 MonoClassField
*field
;
3649 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3651 if (!check_underflow (ctx
, 2))
3654 value
= stack_pop (ctx
);
3655 obj
= stack_pop_safe (ctx
);
3657 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, "stfld"))
3660 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3661 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3664 /*TODO proper handle for Nullable<T>*/
3666 do_box_value (VerifyContext
*ctx
, int klass_token
)
3669 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "box");
3675 if (!check_underflow (ctx
, 1))
3678 value
= stack_pop (ctx
);
3679 /*box is a nop for reference types*/
3681 if (stack_slot_get_underlying_type (value
) == TYPE_COMPLEX
&& MONO_TYPE_IS_REFERENCE (value
->type
) && MONO_TYPE_IS_REFERENCE (type
)) {
3682 stack_push_stack_val (ctx
, value
)->stype
|= BOXED_MASK
;
3687 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3688 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx
->ip_offset
));
3690 klass
= mono_class_from_mono_type (type
);
3691 if (mono_class_is_nullable (klass
))
3692 type
= &mono_class_get_nullable_param (klass
)->byval_arg
;
3693 stack_push_val (ctx
, TYPE_COMPLEX
| BOXED_MASK
, type
);
3697 do_unbox_value (VerifyContext
*ctx
, int klass_token
)
3700 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox");
3705 if (!check_underflow (ctx
, 1))
3708 if (!mono_class_from_mono_type (type
)->valuetype
)
3709 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx
->ip_offset
));
3711 value
= stack_pop (ctx
);
3713 /*Value should be: a boxed valuetype or a reference type*/
3714 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3715 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3716 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
));
3718 set_stack_value (ctx
, value
= stack_push (ctx
), mono_type_get_type_byref (type
), FALSE
);
3719 value
->stype
|= CMMP_MASK
;
3723 do_unbox_any (VerifyContext
*ctx
, int klass_token
)
3726 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox.any");
3731 if (!check_underflow (ctx
, 1))
3734 value
= stack_pop (ctx
);
3736 /*Value should be: a boxed valuetype or a reference type*/
3737 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3738 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3739 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
));
3741 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3745 do_unary_math_op (VerifyContext
*ctx
, int op
)
3748 if (!check_underflow (ctx
, 1))
3750 value
= stack_pop (ctx
);
3751 switch (stack_slot_get_type (value
)) {
3754 case TYPE_NATIVE_INT
:
3759 case TYPE_COMPLEX
: /*only enums are ok*/
3760 if (mono_type_is_enum_type (value
->type
))
3763 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx
->ip_offset
));
3765 stack_push_stack_val (ctx
, value
);
3769 do_conversion (VerifyContext
*ctx
, int kind
)
3772 if (!check_underflow (ctx
, 1))
3774 value
= stack_pop (ctx
);
3776 switch (stack_slot_get_type (value
)) {
3779 case TYPE_NATIVE_INT
:
3783 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
));
3788 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3791 stack_push_val (ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
3794 stack_push_val (ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
3796 case TYPE_NATIVE_INT
:
3797 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3800 g_error ("unknown type %02x in conversion", kind
);
3806 do_load_token (VerifyContext
*ctx
, int token
)
3809 MonoClass
*handle_class
;
3810 if (!check_overflow (ctx
))
3812 handle
= mono_ldtoken (ctx
->image
, token
, &handle_class
, ctx
->generic_context
);
3814 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token
, ctx
->ip_offset
));
3817 if (handle_class
== mono_defaults
.typehandle_class
) {
3818 mono_type_is_valid_in_context (ctx
, (MonoType
*)handle
);
3819 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
3820 mono_method_is_valid_in_context (ctx
, (MonoMethod
*)handle
);
3821 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
3822 mono_type_is_valid_in_context (ctx
, &((MonoClassField
*)handle
)->parent
->byval_arg
);
3824 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
3826 stack_push_val (ctx
, TYPE_COMPLEX
, mono_class_get_type (handle_class
));
3830 do_ldobj_value (VerifyContext
*ctx
, int token
)
3833 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "ldobj");
3834 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3839 if (!check_underflow (ctx
, 1))
3842 value
= stack_pop (ctx
);
3843 if (!stack_slot_is_managed_pointer (value
)
3844 && stack_slot_get_type (value
) != TYPE_NATIVE_INT
3845 && !(stack_slot_get_type (value
) == TYPE_PTR
&& value
->type
->type
!= MONO_TYPE_FNPTR
)) {
3846 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3850 if (stack_slot_get_type (value
) == TYPE_NATIVE_INT
)
3851 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx
->ip_offset
));
3853 /*We have a byval on the stack, but the comparison must be strict. */
3854 if (!verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (value
->type
), TRUE
))
3855 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx
->ip_offset
));
3857 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3861 do_stobj (VerifyContext
*ctx
, int token
)
3863 ILStackDesc
*dest
, *src
;
3864 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "stobj");
3865 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3870 if (!check_underflow (ctx
, 2))
3873 src
= stack_pop (ctx
);
3874 dest
= stack_pop (ctx
);
3876 if (stack_slot_is_managed_mutability_pointer (dest
))
3877 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx
->ip_offset
));
3879 if (!stack_slot_is_managed_pointer (dest
))
3880 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx
->ip_offset
));
3882 if (stack_slot_is_boxed_value (src
) && !MONO_TYPE_IS_REFERENCE (src
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3883 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
));
3885 if (!verify_stack_type_compatibility (ctx
, type
, src
))
3886 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3888 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3889 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3893 do_cpobj (VerifyContext
*ctx
, int token
)
3895 ILStackDesc
*dest
, *src
;
3896 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "cpobj");
3900 if (!check_underflow (ctx
, 2))
3903 src
= stack_pop (ctx
);
3904 dest
= stack_pop (ctx
);
3906 if (!stack_slot_is_managed_pointer (src
))
3907 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx
->ip_offset
));
3909 if (!stack_slot_is_managed_pointer (dest
))
3910 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx
->ip_offset
));
3912 if (stack_slot_is_managed_mutability_pointer (dest
))
3913 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx
->ip_offset
));
3915 if (!verify_type_compatibility (ctx
, type
, mono_type_get_type_byval (src
->type
)))
3916 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3918 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3919 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3923 do_initobj (VerifyContext
*ctx
, int token
)
3926 MonoType
*stack
, *type
= get_boxable_mono_type (ctx
, token
, "initobj");
3930 if (!check_underflow (ctx
, 1))
3933 obj
= stack_pop (ctx
);
3935 if (!stack_slot_is_managed_pointer (obj
))
3936 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx
->ip_offset
));
3938 if (stack_slot_is_managed_mutability_pointer (obj
))
3939 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx
->ip_offset
));
3941 stack
= mono_type_get_type_byval (obj
->type
);
3942 if (MONO_TYPE_IS_REFERENCE (stack
)) {
3943 if (!verify_type_compatibility (ctx
, stack
, type
))
3944 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3945 else if (IS_STRICT_MODE (ctx
) && !mono_metadata_type_equal (type
, stack
))
3946 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3947 } else if (!verify_type_compatibility (ctx
, stack
, type
)) {
3948 char *expected_name
= mono_type_full_name (type
);
3949 char *stack_name
= mono_type_full_name (stack
);
3951 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
));
3952 g_free (expected_name
);
3953 g_free (stack_name
);
3958 do_newobj (VerifyContext
*ctx
, int token
)
3962 MonoMethodSignature
*sig
;
3964 gboolean is_delegate
= FALSE
;
3966 if (!(method
= verifier_load_method (ctx
, token
, "newobj")))
3969 if (!mono_method_is_constructor (method
)) {
3970 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token
, ctx
->ip_offset
));
3974 if (method
->klass
->flags
& (TYPE_ATTRIBUTE_ABSTRACT
| TYPE_ATTRIBUTE_INTERFACE
))
3975 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx
->ip_offset
));
3977 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3978 char *from
= mono_method_full_name (ctx
->method
, TRUE
);
3979 char *to
= mono_method_full_name (method
, TRUE
);
3980 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
);
3985 //FIXME use mono_method_get_signature_full
3986 sig
= mono_method_signature (method
);
3988 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx
->ip_offset
));
3992 if (!check_underflow (ctx
, sig
->param_count
))
3995 is_delegate
= method
->klass
->parent
== mono_defaults
.multicastdelegate_class
;
3998 ILStackDesc
*funptr
;
3999 //first arg is object, second arg is fun ptr
4000 if (sig
->param_count
!= 2) {
4001 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx
->ip_offset
));
4004 funptr
= stack_pop (ctx
);
4005 value
= stack_pop (ctx
);
4006 verify_delegate_compatibility (ctx
, method
->klass
, value
, funptr
);
4008 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
4009 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i
); );
4010 value
= stack_pop (ctx
);
4011 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
4012 char *stack_name
= stack_slot_full_name (value
);
4013 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
4014 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
));
4015 g_free (stack_name
);
4019 if (stack_slot_is_managed_mutability_pointer (value
))
4020 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx
->ip_offset
));
4024 if (check_overflow (ctx
))
4025 set_stack_value (ctx
, stack_push (ctx
), &method
->klass
->byval_arg
, FALSE
);
4029 do_cast (VerifyContext
*ctx
, int token
, const char *opcode
) {
4035 if (!check_underflow (ctx
, 1))
4038 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
4042 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid %s type at 0x%04x", opcode
, ctx
->ip_offset
));
4046 value
= stack_pop (ctx
);
4047 is_boxed
= stack_slot_is_boxed_value (value
);
4049 if (stack_slot_is_managed_pointer (value
))
4050 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
4051 else if (!MONO_TYPE_IS_REFERENCE (value
->type
) && !is_boxed
) {
4052 char *name
= stack_slot_full_name (value
);
4053 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
));
4057 switch (value
->type
->type
) {
4058 case MONO_TYPE_FNPTR
:
4060 case MONO_TYPE_TYPEDBYREF
:
4061 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
4064 do_box
= is_boxed
|| mono_type_is_generic_argument(type
) || mono_class_from_mono_type (type
)->valuetype
;
4065 stack_push_val (ctx
, TYPE_COMPLEX
| (do_box
? BOXED_MASK
: 0), type
);
4069 mono_type_from_opcode (int opcode
) {
4077 return &mono_defaults
.sbyte_class
->byval_arg
;
4085 return &mono_defaults
.int16_class
->byval_arg
;
4093 return &mono_defaults
.int32_class
->byval_arg
;
4099 return &mono_defaults
.int64_class
->byval_arg
;
4105 return &mono_defaults
.single_class
->byval_arg
;
4111 return &mono_defaults
.double_class
->byval_arg
;
4117 return &mono_defaults
.int_class
->byval_arg
;
4121 case CEE_LDELEM_REF
:
4122 case CEE_STELEM_REF
:
4123 return &mono_defaults
.object_class
->byval_arg
;
4126 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode
);
4132 do_load_indirect (VerifyContext
*ctx
, int opcode
)
4135 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
4137 if (!check_underflow (ctx
, 1))
4140 value
= stack_pop (ctx
);
4141 if (!stack_slot_is_managed_pointer (value
)) {
4142 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx
->ip_offset
));
4143 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
4147 if (opcode
== CEE_LDIND_REF
) {
4148 if (stack_slot_get_underlying_type (value
) != TYPE_COMPLEX
|| mono_class_from_mono_type (value
->type
)->valuetype
)
4149 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx
->ip_offset
));
4150 set_stack_value (ctx
, stack_push (ctx
), mono_type_get_type_byval (value
->type
), FALSE
);
4152 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (value
->type
), TRUE
))
4153 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4154 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
4159 do_store_indirect (VerifyContext
*ctx
, int opcode
)
4161 ILStackDesc
*addr
, *val
;
4162 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
4164 if (!check_underflow (ctx
, 2))
4167 val
= stack_pop (ctx
);
4168 addr
= stack_pop (ctx
);
4170 check_unmanaged_pointer (ctx
, addr
);
4172 if (!stack_slot_is_managed_pointer (addr
) && stack_slot_get_type (addr
) != TYPE_PTR
) {
4173 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx
->ip_offset
));
4177 if (stack_slot_is_managed_mutability_pointer (addr
)) {
4178 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx
->ip_offset
));
4182 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (addr
->type
), TRUE
))
4183 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4185 if (!verify_stack_type_compatibility (ctx
, mono_type_from_opcode (opcode
), val
))
4186 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4190 do_newarr (VerifyContext
*ctx
, int token
)
4193 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "newarr");
4198 if (!check_underflow (ctx
, 1))
4201 value
= stack_pop (ctx
);
4202 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4203 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
));
4205 set_stack_value (ctx
, stack_push (ctx
), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type
), 1)), FALSE
);
4208 /*FIXME handle arrays that are not 0-indexed*/
4210 do_ldlen (VerifyContext
*ctx
)
4214 if (!check_underflow (ctx
, 1))
4217 value
= stack_pop (ctx
);
4219 if (stack_slot_get_type (value
) != TYPE_COMPLEX
|| value
->type
->type
!= MONO_TYPE_SZARRAY
)
4220 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx
->ip_offset
));
4222 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
4225 /*FIXME handle arrays that are not 0-indexed*/
4226 /*FIXME handle readonly prefix and CMMP*/
4228 do_ldelema (VerifyContext
*ctx
, int klass_token
)
4230 ILStackDesc
*index
, *array
, *res
;
4231 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "ldelema");
4237 if (!check_underflow (ctx
, 2))
4240 index
= stack_pop (ctx
);
4241 array
= stack_pop (ctx
);
4243 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4244 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
));
4246 if (!stack_slot_is_null_literal (array
)) {
4247 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4248 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array
), ctx
->ip_offset
));
4250 if (get_stack_type (type
) == TYPE_I4
|| get_stack_type (type
) == TYPE_NATIVE_INT
) {
4251 valid
= verify_type_compatibility_full (ctx
, type
, &array
->type
->data
.klass
->byval_arg
, TRUE
);
4253 valid
= mono_metadata_type_equal (type
, &array
->type
->data
.klass
->byval_arg
);
4256 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx
->ip_offset
));
4260 res
= stack_push (ctx
);
4261 set_stack_value (ctx
, res
, type
, TRUE
);
4262 if (ctx
->prefix_set
& PREFIX_READONLY
) {
4263 ctx
->prefix_set
&= ~PREFIX_READONLY
;
4264 res
->stype
|= CMMP_MASK
;
4269 * FIXME handle arrays that are not 0-indexed
4270 * FIXME handle readonly prefix and CMMP
4273 do_ldelem (VerifyContext
*ctx
, int opcode
, int token
)
4275 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4276 ILStackDesc
*index
, *array
;
4278 if (!check_underflow (ctx
, 2))
4281 if (opcode
== CEE_LDELEM
) {
4282 if (!(type
= verifier_load_type (ctx
, token
, "ldelem.any"))) {
4283 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4287 type
= mono_type_from_opcode (opcode
);
4290 index
= stack_pop (ctx
);
4291 array
= stack_pop (ctx
);
4293 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4294 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
));
4296 if (!stack_slot_is_null_literal (array
)) {
4297 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4298 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
));
4300 if (opcode
== CEE_LDELEM_REF
) {
4301 if (array
->type
->data
.klass
->valuetype
)
4302 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx
->ip_offset
));
4303 type
= &array
->type
->data
.klass
->byval_arg
;
4305 MonoType
*candidate
= &array
->type
->data
.klass
->byval_arg
;
4306 if (IS_STRICT_MODE (ctx
)) {
4307 MonoType
*underlying_type
= mono_type_get_underlying_type_any (type
);
4308 MonoType
*underlying_candidate
= mono_type_get_underlying_type_any (candidate
);
4309 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
)) ||
4310 (IS_ONE_OF2 (underlying_candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
) && IS_ONE_OF2 (underlying_type
->type
, MONO_TYPE_I
, MONO_TYPE_U
)))
4311 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4313 if (!verify_type_compatibility_full (ctx
, type
, candidate
, TRUE
))
4314 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4319 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
4324 * FIXME handle arrays that are not 0-indexed
4327 do_stelem (VerifyContext
*ctx
, int opcode
, int token
)
4329 ILStackDesc
*index
, *array
, *value
;
4331 if (!check_underflow (ctx
, 3))
4334 if (opcode
== CEE_STELEM
) {
4335 if (!(type
= verifier_load_type (ctx
, token
, "stelem.any"))) {
4336 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4340 type
= mono_type_from_opcode (opcode
);
4343 value
= stack_pop (ctx
);
4344 index
= stack_pop (ctx
);
4345 array
= stack_pop (ctx
);
4347 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4348 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
));
4350 if (!stack_slot_is_null_literal (array
)) {
4351 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
) {
4352 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
));
4354 if (opcode
== CEE_STELEM_REF
) {
4355 if (array
->type
->data
.klass
->valuetype
)
4356 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4357 } else if (!verify_type_compatibility_full (ctx
, &array
->type
->data
.klass
->byval_arg
, type
, TRUE
)) {
4358 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4362 if (opcode
== CEE_STELEM_REF
) {
4363 if (!stack_slot_is_boxed_value (value
) && mono_class_from_mono_type (value
->type
)->valuetype
)
4364 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4365 } else if (opcode
!= CEE_STELEM_REF
) {
4366 if (!verify_stack_type_compatibility (ctx
, type
, value
))
4367 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4369 if (stack_slot_is_boxed_value (value
) && !MONO_TYPE_IS_REFERENCE (value
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
4370 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
));
4376 do_throw (VerifyContext
*ctx
)
4378 ILStackDesc
*exception
;
4379 if (!check_underflow (ctx
, 1))
4381 exception
= stack_pop (ctx
);
4383 if (!stack_slot_is_null_literal (exception
) && !(stack_slot_get_type (exception
) == TYPE_COMPLEX
&& !mono_class_from_mono_type (exception
->type
)->valuetype
))
4384 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx
->ip_offset
));
4386 if (mono_type_is_generic_argument (exception
->type
) && !stack_slot_is_boxed_value (exception
)) {
4387 char *name
= mono_type_full_name (exception
->type
);
4388 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
));
4391 /*The stack is left empty after a throw*/
4397 do_endfilter (VerifyContext
*ctx
)
4399 MonoExceptionClause
*clause
;
4401 if (IS_STRICT_MODE (ctx
)) {
4402 if (ctx
->eval
.size
!= 1)
4403 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx
->ip_offset
));
4405 if (ctx
->eval
.size
>= 1 && stack_slot_get_type (stack_pop (ctx
)) != TYPE_I4
)
4406 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx
->ip_offset
));
4409 if ((clause
= is_correct_endfilter (ctx
, ctx
->ip_offset
))) {
4410 if (IS_STRICT_MODE (ctx
)) {
4411 if (ctx
->ip_offset
!= clause
->handler_offset
- 2)
4412 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4414 if ((ctx
->ip_offset
!= clause
->handler_offset
- 2) && !MONO_OFFSET_IN_HANDLER (clause
, ctx
->ip_offset
))
4415 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4418 if (IS_STRICT_MODE (ctx
) && !is_unverifiable_endfilter (ctx
, ctx
->ip_offset
))
4419 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4421 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4428 do_leave (VerifyContext
*ctx
, int delta
)
4430 int target
= ((gint32
)ctx
->ip_offset
) + delta
;
4431 if (target
>= ctx
->code_size
|| target
< 0)
4432 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
4434 if (!is_correct_leave (ctx
->header
, ctx
->ip_offset
, target
))
4435 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx
->ip_offset
));
4442 * Verify br and br.s opcodes.
4445 do_static_branch (VerifyContext
*ctx
, int delta
)
4447 int target
= ctx
->ip_offset
+ delta
;
4448 if (target
< 0 || target
>= ctx
->code_size
) {
4449 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("branch target out of code at 0x%04x", ctx
->ip_offset
));
4453 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4455 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4458 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4462 ctx
->target
= target
;
4466 do_switch (VerifyContext
*ctx
, int count
, const unsigned char *data
)
4468 int i
, base
= ctx
->ip_offset
+ 5 + count
* 4;
4471 if (!check_underflow (ctx
, 1))
4474 value
= stack_pop (ctx
);
4476 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4477 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx
->ip_offset
));
4479 for (i
= 0; i
< count
; ++i
) {
4480 int target
= base
+ read32 (data
+ i
* 4);
4482 if (target
< 0 || target
>= ctx
->code_size
) {
4483 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x out of code at 0x%04x", i
, ctx
->ip_offset
));
4487 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4489 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4492 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4495 merge_stacks (ctx
, &ctx
->eval
, &ctx
->code
[target
], FALSE
, TRUE
);
4500 do_load_function_ptr (VerifyContext
*ctx
, guint32 token
, gboolean
virtual)
4505 if (virtual && !check_underflow (ctx
, 1))
4508 if (!virtual && !check_overflow (ctx
))
4511 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4512 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4516 if (!(method
= verifier_load_method (ctx
, token
, virtual ? "ldvirtfrn" : "ldftn")))
4519 if (mono_method_is_constructor (method
))
4520 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx
->ip_offset
));
4523 ILStackDesc
*top
= stack_pop (ctx
);
4525 if (stack_slot_get_type (top
) != TYPE_COMPLEX
|| top
->type
->type
== MONO_TYPE_VALUETYPE
)
4526 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx
->ip_offset
));
4528 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
)
4529 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx
->ip_offset
));
4531 if (!verify_stack_type_compatibility (ctx
, &method
->klass
->byval_arg
, top
))
4532 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx
->ip_offset
));
4535 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
))
4536 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
);
4538 top
= stack_push_val(ctx
, TYPE_PTR
, mono_type_create_fnptr_from_mono_method (ctx
, method
));
4539 top
->method
= method
;
4543 do_sizeof (VerifyContext
*ctx
, int token
)
4547 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4548 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4552 if (!(type
= verifier_load_type (ctx
, token
, "sizeof")))
4555 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
4556 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx
->ip_offset
));
4560 if (type
->type
== MONO_TYPE_VOID
) {
4561 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx
->ip_offset
));
4565 if (check_overflow (ctx
))
4566 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.uint32_class
->byval_arg
, FALSE
);
4569 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4571 do_localloc (VerifyContext
*ctx
)
4575 if (ctx
->eval
.size
!= 1) {
4576 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4580 if (in_any_exception_block (ctx
->header
, ctx
->ip_offset
)) {
4581 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4585 /*TODO verify top type*/
4586 top
= stack_pop (ctx
);
4588 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.int_class
->byval_arg
, FALSE
);
4589 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx
->ip_offset
));
4593 do_ldstr (VerifyContext
*ctx
, guint32 token
)
4595 if (mono_metadata_token_code (token
) != MONO_TOKEN_STRING
) {
4596 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4600 if (!ctx
->image
->dynamic
&& mono_metadata_token_index (token
) >= ctx
->image
->heap_us
.size
) {
4601 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string index %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4605 if (check_overflow (ctx
))
4606 stack_push_val (ctx
, TYPE_COMPLEX
, &mono_defaults
.string_class
->byval_arg
);
4610 do_refanyval (VerifyContext
*ctx
, int token
)
4614 if (!check_underflow (ctx
, 1))
4617 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4620 top
= stack_pop (ctx
);
4622 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4623 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
));
4625 set_stack_value (ctx
, stack_push (ctx
), type
, TRUE
);
4629 do_refanytype (VerifyContext
*ctx
)
4633 if (!check_underflow (ctx
, 1))
4636 top
= stack_pop (ctx
);
4638 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4639 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
));
4641 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typehandle_class
->byval_arg
, FALSE
);
4646 do_mkrefany (VerifyContext
*ctx
, int token
)
4650 if (!check_underflow (ctx
, 1))
4653 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4656 top
= stack_pop (ctx
);
4658 if (stack_slot_is_managed_mutability_pointer (top
))
4659 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx
->ip_offset
));
4661 if (!stack_slot_is_managed_pointer (top
)) {
4662 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
));
4664 MonoType
*stack_type
= mono_type_get_type_byval (top
->type
);
4665 if (MONO_TYPE_IS_REFERENCE (type
) && !mono_metadata_type_equal (type
, stack_type
))
4666 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4668 if (!MONO_TYPE_IS_REFERENCE (type
) && !verify_type_compatibility_full (ctx
, type
, stack_type
, TRUE
))
4669 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4672 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typed_reference_class
->byval_arg
, FALSE
);
4676 do_ckfinite (VerifyContext
*ctx
)
4679 if (!check_underflow (ctx
, 1))
4682 top
= stack_pop (ctx
);
4684 if (stack_slot_get_underlying_type (top
) != TYPE_R8
)
4685 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
));
4686 stack_push_stack_val (ctx
, top
);
4690 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4692 * @from holds new values for a given control path
4693 * @to holds the current values of a given control path
4695 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4698 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
)
4701 stack_init (ctx
, to
);
4704 if (to
->flags
== IL_CODE_FLAG_NOT_PROCESSED
)
4707 stack_copy (&ctx
->eval
, to
);
4709 } else if (!(to
->flags
& IL_CODE_STACK_MERGED
)) {
4710 stack_copy (to
, &ctx
->eval
);
4713 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from
->size
, to
->size
); );
4715 if (from
->size
!= to
->size
) {
4716 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from
->size
, to
->size
, ctx
->ip_offset
); );
4717 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
));
4721 //FIXME we need to preserve CMMP attributes
4722 //FIXME we must take null literals into consideration.
4723 for (i
= 0; i
< from
->size
; ++i
) {
4724 ILStackDesc
*new_slot
= from
->stack
+ i
;
4725 ILStackDesc
*old_slot
= to
->stack
+ i
;
4726 MonoType
*new_type
= mono_type_from_stack_slot (new_slot
);
4727 MonoType
*old_type
= mono_type_from_stack_slot (old_slot
);
4728 MonoClass
*old_class
= mono_class_from_mono_type (old_type
);
4729 MonoClass
*new_class
= mono_class_from_mono_type (new_type
);
4730 MonoClass
*match_class
= NULL
;
4732 // S := T then U = S (new value is compatible with current value, keep current)
4733 if (verify_stack_type_compatibility (ctx
, old_type
, new_slot
)) {
4734 copy_stack_value (new_slot
, old_slot
);
4738 // T := S then U = T (old value is compatible with current value, use new)
4739 if (verify_stack_type_compatibility (ctx
, new_type
, old_slot
)) {
4740 copy_stack_value (old_slot
, new_slot
);
4744 if (mono_type_is_generic_argument (old_type
) || mono_type_is_generic_argument (new_type
)) {
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
));
4753 //both are reference types, use closest common super type
4754 if (!mono_class_from_mono_type (old_type
)->valuetype
4755 && !mono_class_from_mono_type (new_type
)->valuetype
4756 && !stack_slot_is_managed_pointer (old_slot
)
4757 && !stack_slot_is_managed_pointer (new_slot
)) {
4759 for (j
= MIN (old_class
->idepth
, new_class
->idepth
) - 1; j
> 0; --j
) {
4760 if (mono_metadata_type_equal (&old_class
->supertypes
[j
]->byval_arg
, &new_class
->supertypes
[j
]->byval_arg
)) {
4761 match_class
= old_class
->supertypes
[j
];
4766 mono_class_setup_interfaces (old_class
);
4767 for (j
= 0; j
< old_class
->interface_count
; ++j
) {
4768 for (k
= 0; k
< new_class
->interface_count
; ++k
) {
4769 if (mono_metadata_type_equal (&old_class
->interfaces
[j
]->byval_arg
, &new_class
->interfaces
[k
]->byval_arg
)) {
4770 match_class
= old_class
->interfaces
[j
];
4776 //No decent super type found, use object
4777 match_class
= mono_defaults
.object_class
;
4779 } 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
)) {
4780 match_class
= mono_defaults
.object_class
;
4785 char *old_name
= stack_slot_full_name (old_slot
);
4786 char *new_name
= stack_slot_full_name (new_slot
);
4787 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
));
4791 set_stack_value (ctx
, old_slot
, &new_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4795 g_assert (match_class
);
4796 set_stack_value (ctx
, old_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4797 set_stack_value (ctx
, new_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4803 to
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
4804 to
->flags
|= IL_CODE_STACK_MERGED
;
4807 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4808 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4811 * is_clause_in_range :
4813 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4816 is_clause_in_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4818 if (clause
->try_offset
>= start
&& clause
->try_offset
< end
)
4820 if (HANDLER_START (clause
) >= start
&& HANDLER_START (clause
) < end
)
4826 * is_clause_inside_range :
4828 * Returns TRUE if @clause lies completely inside the @start - @end range.
4831 is_clause_inside_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4833 if (clause
->try_offset
< start
|| (clause
->try_offset
+ clause
->try_len
) > end
)
4835 if (HANDLER_START (clause
) < start
|| (clause
->handler_offset
+ clause
->handler_len
) > end
)
4841 * is_clause_nested :
4843 * Returns TRUE if @nested is nested in @clause.
4846 is_clause_nested (MonoExceptionClause
*clause
, MonoExceptionClause
*nested
)
4848 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (nested
, clause
->data
.filter_offset
, clause
->handler_offset
))
4850 return is_clause_inside_range (nested
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4851 is_clause_inside_range (nested
, clause
->handler_offset
, clause
->handler_offset
+ clause
->handler_len
);
4854 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4855 * the each pair of exception must have the following properties:
4856 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4857 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4858 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4861 verify_clause_relationship (VerifyContext
*ctx
, MonoExceptionClause
*clause
, MonoExceptionClause
*to_test
)
4863 /*clause is nested*/
4864 if (to_test
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (clause
, to_test
->data
.filter_offset
, to_test
->handler_offset
)) {
4865 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clause inside filter"));
4869 /*wrong nesting order.*/
4870 if (is_clause_nested (clause
, to_test
)) {
4871 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4875 /*mutual protection*/
4876 if (clause
->try_offset
== to_test
->try_offset
&& clause
->try_len
== to_test
->try_len
) {
4877 /*handlers are not disjoint*/
4878 if (is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) {
4879 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception handlers overlap"));
4882 /* handlers are not catch or filter */
4883 if (!IS_CATCH_OR_FILTER (clause
) || !IS_CATCH_OR_FILTER (to_test
)) {
4884 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4891 /*not completelly disjoint*/
4892 if ((is_clause_in_range (to_test
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4893 is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) && !is_clause_nested (to_test
, clause
))
4894 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses overlap"));
4897 #define code_bounds_check(size) \
4898 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4899 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4904 * FIXME: need to distinguish between valid and verifiable.
4905 * Need to keep track of types on the stack.
4906 * Verify types for opcodes.
4909 mono_method_verify (MonoMethod
*method
, int level
)
4912 const unsigned char *ip
;
4913 const unsigned char *end
;
4914 int i
, n
, need_merge
= 0, start
= 0;
4915 guint token
, ip_offset
= 0, prefix
= 0;
4916 MonoGenericContext
*generic_context
= NULL
;
4920 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method
->klass
->name_space
, method
->klass
->name
, method
->name
); );
4922 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4923 (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))) {
4927 memset (&ctx
, 0, sizeof (VerifyContext
));
4929 //FIXME use mono_method_get_signature_full
4930 ctx
.signature
= mono_method_signature (method
);
4931 if (!ctx
.signature
) {
4932 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method signature"));
4935 ctx
.header
= mono_method_get_header (method
);
4937 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method header"));
4940 ctx
.method
= method
;
4941 ip
= ctx
.header
->code
;
4942 end
= ip
+ ctx
.header
->code_size
;
4943 ctx
.image
= image
= method
->klass
->image
;
4946 ctx
.max_args
= ctx
.signature
->param_count
+ ctx
.signature
->hasthis
;
4947 ctx
.max_stack
= ctx
.header
->max_stack
;
4948 ctx
.verifiable
= ctx
.valid
= 1;
4951 ctx
.code
= g_new (ILCodeDesc
, ctx
.header
->code_size
);
4952 ctx
.code_size
= ctx
.header
->code_size
;
4954 memset(ctx
.code
, 0, sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4957 ctx
.num_locals
= ctx
.header
->num_locals
;
4958 ctx
.locals
= g_memdup (ctx
.header
->locals
, sizeof (MonoType
*) * ctx
.header
->num_locals
);
4960 if (ctx
.num_locals
> 0 && !ctx
.header
->init_locals
)
4961 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Method with locals variable but without init locals set"));
4963 ctx
.params
= g_new (MonoType
*, ctx
.max_args
);
4964 if (ctx
.signature
->hasthis
)
4965 ctx
.params
[0] = method
->klass
->valuetype
? &method
->klass
->this_arg
: &method
->klass
->byval_arg
;
4966 memcpy (ctx
.params
+ ctx
.signature
->hasthis
, ctx
.signature
->params
, sizeof (MonoType
*) * ctx
.signature
->param_count
);
4968 if (ctx
.signature
->is_inflated
)
4969 ctx
.generic_context
= generic_context
= mono_method_get_context (method
);
4971 if (!generic_context
&& (method
->klass
->generic_container
|| method
->is_generic
)) {
4972 if (method
->is_generic
)
4973 ctx
.generic_context
= generic_context
= &(mono_method_get_generic_container (method
)->context
);
4975 ctx
.generic_context
= generic_context
= &method
->klass
->generic_container
->context
;
4978 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4979 MonoType
*uninflated
= ctx
.locals
[i
];
4980 ctx
.locals
[i
] = mono_class_inflate_generic_type_checked (ctx
.locals
[i
], ctx
.generic_context
, &error
);
4981 if (!mono_error_ok (&error
)) {
4982 char *name
= mono_type_full_name (ctx
.locals
[i
] ? ctx
.locals
[i
] : uninflated
);
4983 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %d of type %s", i
, name
));
4985 mono_error_cleanup (&error
);
4986 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4992 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4993 MonoType
*uninflated
= ctx
.params
[i
];
4994 ctx
.params
[i
] = mono_class_inflate_generic_type_checked (ctx
.params
[i
], ctx
.generic_context
, &error
);
4995 if (!mono_error_ok (&error
)) {
4996 char *name
= mono_type_full_name (ctx
.params
[i
] ? ctx
.params
[i
] : uninflated
);
4997 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %d of type %s", i
, name
));
4999 mono_error_cleanup (&error
);
5000 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
5005 stack_init (&ctx
, &ctx
.eval
);
5007 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
5008 if (!mono_type_is_valid_in_context (&ctx
, ctx
.locals
[i
]))
5012 for (i
= 0; i
< ctx
.max_args
; ++i
) {
5013 if (!mono_type_is_valid_in_context (&ctx
, ctx
.params
[i
]))
5020 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
5021 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
5022 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
); );
5024 if (clause
->try_offset
> ctx
.code_size
|| clause
->try_offset
+ clause
->try_len
> ctx
.code_size
)
5025 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause out of bounds at 0x%04x", clause
->try_offset
));
5027 if (clause
->try_len
<= 0)
5028 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
5030 if (clause
->handler_offset
> ctx
.code_size
|| clause
->handler_offset
+ clause
->handler_len
> ctx
.code_size
)
5031 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause
->try_offset
));
5033 if (clause
->handler_len
<= 0)
5034 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
5036 if (clause
->try_offset
< clause
->handler_offset
&& clause
->try_offset
+ clause
->try_len
> HANDLER_START (clause
))
5037 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause
->try_offset
, clause
->handler_offset
));
5039 for (n
= i
+ 1; n
< ctx
.header
->num_clauses
&& ctx
.valid
; ++n
)
5040 verify_clause_relationship (&ctx
, clause
, ctx
.header
->clauses
+ n
);
5045 ctx
.code
[clause
->try_offset
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
5046 if (clause
->try_offset
+ clause
->try_len
< ctx
.code_size
)
5047 ctx
.code
[clause
->try_offset
+ clause
->try_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
5048 if (clause
->handler_offset
+ clause
->handler_len
< ctx
.code_size
)
5049 ctx
.code
[clause
->handler_offset
+ clause
->handler_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
5051 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
) {
5052 if (!clause
->data
.catch_class
) {
5053 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Catch clause %d with invalid type", i
));
5057 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, clause
->data
.catch_class
);
5059 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
5060 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->data
.filter_offset
, mono_defaults
.exception_class
);
5061 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, mono_defaults
.exception_class
);
5065 while (ip
< end
&& ctx
.valid
) {
5066 ctx
.ip_offset
= ip_offset
= ip
- ctx
.header
->code
;
5068 /*We need to check against fallthrou in and out of protected blocks.
5069 * For fallout we check the once a protected block ends, if the start flag is not set.
5070 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5071 * TODO convert these checks to be done using flags and not this loop
5073 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
5074 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
5076 if ((clause
->try_offset
+ clause
->try_len
== ip_offset
) && start
== 0) {
5077 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset
));
5081 if ((clause
->handler_offset
+ clause
->handler_len
== ip_offset
) && start
== 0) {
5082 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
5083 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5085 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5089 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& clause
->handler_offset
== ip_offset
&& start
== 0) {
5090 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset
));
5094 if (clause
->handler_offset
== ip_offset
&& start
== 0) {
5095 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset
));
5099 if (clause
->try_offset
== ip_offset
&& ctx
.eval
.size
> 0 && start
== 0) {
5100 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset
));
5109 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx
.target
); );
5110 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ctx
.target
], FALSE
, TRUE
);
5113 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ip_offset
], start
, FALSE
);
5116 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5117 #ifdef MONO_VERIFIER_DEBUG
5120 discode
= mono_disasm_code_one (NULL
, method
, ip
, NULL
);
5121 discode
[strlen (discode
) - 1] = 0; /* no \n */
5122 g_print ("[%d] %-29s (%d)\n", ip_offset
, discode
, ctx
.eval
.size
);
5125 dump_stack_state (&ctx
.code
[ip_offset
]);
5126 dump_stack_state (&ctx
.eval
);
5139 push_arg (&ctx
, *ip
- CEE_LDARG_0
, FALSE
);
5145 code_bounds_check (2);
5146 push_arg (&ctx
, ip
[1], *ip
== CEE_LDARGA_S
);
5150 case CEE_ADD_OVF_UN
:
5151 do_binop (&ctx
, *ip
, add_ovf_un_table
);
5155 case CEE_SUB_OVF_UN
:
5156 do_binop (&ctx
, *ip
, sub_ovf_un_table
);
5163 case CEE_MUL_OVF_UN
:
5164 do_binop (&ctx
, *ip
, bin_ovf_table
);
5169 do_binop (&ctx
, *ip
, add_table
);
5174 do_binop (&ctx
, *ip
, sub_table
);
5181 do_binop (&ctx
, *ip
, bin_op_table
);
5190 do_binop (&ctx
, *ip
, int_bin_op_table
);
5197 do_binop (&ctx
, *ip
, shift_op_table
);
5202 if (!check_underflow (&ctx
, 1))
5204 stack_pop_safe (&ctx
);
5218 /*TODO support definite assignment verification? */
5219 push_local (&ctx
, *ip
- CEE_LDLOC_0
, FALSE
);
5227 store_local (&ctx
, *ip
- CEE_STLOC_0
);
5232 code_bounds_check (2);
5233 store_local (&ctx
, ip
[1]);
5238 code_bounds_check (2);
5239 store_arg (&ctx
, ip
[1]);
5253 if (check_overflow (&ctx
))
5254 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5259 code_bounds_check (2);
5260 if (check_overflow (&ctx
))
5261 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5266 code_bounds_check (5);
5267 if (check_overflow (&ctx
))
5268 stack_push_val (&ctx
,TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5273 code_bounds_check (9);
5274 if (check_overflow (&ctx
))
5275 stack_push_val (&ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
5280 code_bounds_check (5);
5281 if (check_overflow (&ctx
))
5282 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5287 code_bounds_check (9);
5288 if (check_overflow (&ctx
))
5289 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5294 if (check_overflow (&ctx
))
5295 stack_push_val (&ctx
, TYPE_COMPLEX
| NULL_LITERAL_MASK
, &mono_defaults
.object_class
->byval_arg
);
5301 code_bounds_check (2);
5302 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_eq_op
);
5315 code_bounds_check (2);
5316 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_op
);
5323 code_bounds_check (5);
5324 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_eq_op
);
5337 code_bounds_check (5);
5338 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_op
);
5345 code_bounds_check (2);
5346 push_local (&ctx
, ip
[1], *ip
== CEE_LDLOCA_S
);
5350 /* FIXME: warn/error instead? */
5357 if (!check_underflow (&ctx
, 1))
5359 if (!check_overflow (&ctx
))
5361 top
= stack_pop_safe (&ctx
);
5362 copy_stack_value (stack_push (&ctx
), top
);
5363 copy_stack_value (stack_push (&ctx
), top
);
5369 code_bounds_check (5);
5371 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset
));
5372 token
= read32 (ip
+ 1);
5373 if (in_any_block (ctx
.header
, ip_offset
))
5374 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset
));
5376 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx
.ip_offset
));
5378 * FIXME: check signature, retval, arguments etc.
5384 code_bounds_check (5);
5385 do_invoke_method (&ctx
, read32 (ip
+ 1), *ip
== CEE_CALLVIRT
);
5390 code_bounds_check (5);
5391 token
= read32 (ip
+ 1);
5393 * FIXME: check signature, retval, arguments etc.
5394 * FIXME: check requirements for tail call
5396 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx
.ip_offset
));
5400 code_bounds_check (2);
5401 do_static_branch (&ctx
, (signed char)ip
[1] + 2);
5409 code_bounds_check (2);
5410 do_boolean_branch_op (&ctx
, (signed char)ip
[1] + 2);
5416 code_bounds_check (5);
5417 do_static_branch (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5425 code_bounds_check (5);
5426 do_boolean_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5433 code_bounds_check (5);
5434 entries
= read32 (ip
+ 1);
5436 if (entries
> 0xFFFFFFFFU
/ sizeof (guint32
))
5437 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries
, ctx
.ip_offset
));
5440 code_bounds_check (sizeof (guint32
) * entries
);
5442 do_switch (&ctx
, entries
, ip
);
5444 ip
+= sizeof (guint32
) * entries
;
5458 do_load_indirect (&ctx
, *ip
);
5470 do_store_indirect (&ctx
, *ip
);
5476 do_unary_math_op (&ctx
, *ip
);
5486 do_conversion (&ctx
, TYPE_I4
);
5492 do_conversion (&ctx
, TYPE_I8
);
5499 do_conversion (&ctx
, TYPE_R8
);
5505 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5510 code_bounds_check (5);
5511 do_cpobj (&ctx
, read32 (ip
+ 1));
5516 code_bounds_check (5);
5517 do_ldobj_value (&ctx
, read32 (ip
+ 1));
5522 code_bounds_check (5);
5523 do_ldstr (&ctx
, read32 (ip
+ 1));
5528 code_bounds_check (5);
5529 do_newobj (&ctx
, read32 (ip
+ 1));
5535 code_bounds_check (5);
5536 do_cast (&ctx
, read32 (ip
+ 1), *ip
== CEE_CASTCLASS
? "castclass" : "isinst");
5542 ++ip
; /* warn, error ? */
5546 code_bounds_check (5);
5547 do_unbox_value (&ctx
, read32 (ip
+ 1));
5559 code_bounds_check (5);
5560 do_push_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDFLDA
);
5566 code_bounds_check (5);
5567 do_push_static_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDSFLDA
);
5572 code_bounds_check (5);
5573 do_store_field (&ctx
, read32 (ip
+ 1));
5578 code_bounds_check (5);
5579 do_store_static_field (&ctx
, read32 (ip
+ 1));
5584 code_bounds_check (5);
5585 do_stobj (&ctx
, read32 (ip
+ 1));
5589 case CEE_CONV_OVF_I1_UN
:
5590 case CEE_CONV_OVF_I2_UN
:
5591 case CEE_CONV_OVF_I4_UN
:
5592 case CEE_CONV_OVF_U1_UN
:
5593 case CEE_CONV_OVF_U2_UN
:
5594 case CEE_CONV_OVF_U4_UN
:
5595 do_conversion (&ctx
, TYPE_I4
);
5599 case CEE_CONV_OVF_I8_UN
:
5600 case CEE_CONV_OVF_U8_UN
:
5601 do_conversion (&ctx
, TYPE_I8
);
5605 case CEE_CONV_OVF_I_UN
:
5606 case CEE_CONV_OVF_U_UN
:
5607 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5612 code_bounds_check (5);
5613 do_box_value (&ctx
, read32 (ip
+ 1));
5618 code_bounds_check (5);
5619 do_newarr (&ctx
, read32 (ip
+ 1));
5629 code_bounds_check (5);
5630 do_ldelema (&ctx
, read32 (ip
+ 1));
5644 case CEE_LDELEM_REF
:
5645 do_ldelem (&ctx
, *ip
, 0);
5656 case CEE_STELEM_REF
:
5657 do_stelem (&ctx
, *ip
, 0);
5662 code_bounds_check (5);
5663 do_ldelem (&ctx
, *ip
, read32 (ip
+ 1));
5668 code_bounds_check (5);
5669 do_stelem (&ctx
, *ip
, read32 (ip
+ 1));
5674 code_bounds_check (5);
5675 do_unbox_any (&ctx
, read32 (ip
+ 1));
5679 case CEE_CONV_OVF_I1
:
5680 case CEE_CONV_OVF_U1
:
5681 case CEE_CONV_OVF_I2
:
5682 case CEE_CONV_OVF_U2
:
5683 case CEE_CONV_OVF_I4
:
5684 case CEE_CONV_OVF_U4
:
5685 do_conversion (&ctx
, TYPE_I4
);
5689 case CEE_CONV_OVF_I8
:
5690 case CEE_CONV_OVF_U8
:
5691 do_conversion (&ctx
, TYPE_I8
);
5695 case CEE_CONV_OVF_I
:
5696 case CEE_CONV_OVF_U
:
5697 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5702 code_bounds_check (5);
5703 do_refanyval (&ctx
, read32 (ip
+ 1));
5713 code_bounds_check (5);
5714 do_mkrefany (&ctx
, read32 (ip
+ 1));
5719 code_bounds_check (5);
5720 do_load_token (&ctx
, read32 (ip
+ 1));
5724 case CEE_ENDFINALLY
:
5725 if (!is_correct_endfinally (ctx
.header
, ip_offset
))
5726 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx
.ip_offset
));
5733 code_bounds_check (5);
5734 do_leave (&ctx
, read32 (ip
+ 1) + 5);
5740 code_bounds_check (2);
5741 do_leave (&ctx
, (signed char)ip
[1] + 2);
5747 code_bounds_check (2);
5751 code_bounds_check (3);
5752 store_local (&ctx
, read16 (ip
+ 1));
5757 do_cmp_op (&ctx
, cmp_br_eq_op
, *ip
);
5765 do_cmp_op (&ctx
, cmp_br_op
, *ip
);
5770 code_bounds_check (3);
5771 store_arg (&ctx
, read16 (ip
+ 1) );
5777 check_overflow (&ctx
);
5778 if (ctx
.signature
->call_convention
!= MONO_CALL_VARARG
)
5779 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx
.ip_offset
));
5780 set_stack_value (&ctx
, stack_push (&ctx
), &mono_defaults
.argumenthandle_class
->byval_arg
, FALSE
);
5785 code_bounds_check (5);
5786 do_load_function_ptr (&ctx
, read32 (ip
+ 1), FALSE
);
5791 code_bounds_check (5);
5792 do_load_function_ptr (&ctx
, read32 (ip
+ 1), TRUE
);
5802 code_bounds_check (3);
5803 push_arg (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDARGA
);
5809 code_bounds_check (3);
5810 push_local (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDLOCA
);
5823 do_endfilter (&ctx
);
5827 case CEE_UNALIGNED_
:
5828 code_bounds_check (2);
5829 prefix
|= PREFIX_UNALIGNED
;
5833 prefix
|= PREFIX_VOLATILE
;
5837 prefix
|= PREFIX_TAIL
;
5839 if (ip
< end
&& (*ip
!= CEE_CALL
&& *ip
!= CEE_CALLI
&& *ip
!= CEE_CALLVIRT
))
5840 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset
));
5844 code_bounds_check (5);
5845 do_initobj (&ctx
, read32 (ip
+ 1));
5849 case CEE_CONSTRAINED_
:
5850 code_bounds_check (5);
5851 ctx
.constrained_type
= get_boxable_mono_type (&ctx
, read32 (ip
+ 1), "constrained.");
5852 prefix
|= PREFIX_CONSTRAINED
;
5857 prefix
|= PREFIX_READONLY
;
5862 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5863 if (!check_underflow (&ctx
, 3))
5865 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5870 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5871 if (!check_underflow (&ctx
, 3))
5873 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5881 if (!is_correct_rethrow (ctx
.header
, ip_offset
))
5882 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx
.ip_offset
));
5892 code_bounds_check (5);
5893 do_sizeof (&ctx
, read32 (ip
+ 1));
5897 case CEE_REFANYTYPE
:
5898 do_refanytype (&ctx
);
5903 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip
, ctx
.ip_offset
));
5909 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ctx
.ip_offset
));
5913 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5915 if (!ctx
.prefix_set
) //first prefix
5916 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5917 ctx
.prefix_set
|= prefix
;
5918 ctx
.has_flags
= TRUE
;
5922 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5924 if (ctx
.prefix_set
& PREFIX_CONSTRAINED
)
5925 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx
.ip_offset
));
5926 if (ctx
.prefix_set
& PREFIX_READONLY
)
5927 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx
.ip_offset
));
5928 if (ctx
.prefix_set
& PREFIX_VOLATILE
)
5929 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx
.ip_offset
));
5930 if (ctx
.prefix_set
& PREFIX_UNALIGNED
)
5931 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx
.ip_offset
));
5932 ctx
.prefix_set
= prefix
= 0;
5933 ctx
.has_flags
= FALSE
;
5937 * if ip != end we overflowed: mark as error.
5939 if ((ip
!= end
|| !start
) && ctx
.verifiable
&& !ctx
.list
) {
5940 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset
));
5943 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5944 for (i
= 0; i
< ctx
.code_size
&& i
< ip_offset
; ++i
) {
5945 if (ctx
.code
[i
].flags
& IL_CODE_FLAG_WAS_TARGET
) {
5946 if (!(ctx
.code
[i
].flags
& IL_CODE_FLAG_SEEN
))
5947 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i
));
5949 if (ctx
.code
[i
].flags
& IL_CODE_DELEGATE_SEQUENCE
)
5950 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i
));
5952 if ((ctx
.code
[i
].flags
& IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5953 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i
));
5955 if ((ctx
.code
[i
].flags
& IL_CODE_CALL_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5956 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
));
5959 if (mono_method_is_constructor (ctx
.method
) && !ctx
.super_ctor_called
&& !ctx
.method
->klass
->valuetype
&& ctx
.method
->klass
!= mono_defaults
.object_class
) {
5960 char *method_name
= mono_method_full_name (ctx
.method
, TRUE
);
5961 char *type
= mono_type_get_full_name (ctx
.method
->klass
);
5962 if (ctx
.method
->klass
->parent
&& ctx
.method
->klass
->parent
->exception_type
!= MONO_EXCEPTION_NONE
)
5963 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
));
5965 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name
, type
));
5966 g_free (method_name
);
5972 for (i
= 0; i
< ctx
.header
->code_size
; ++i
) {
5973 if (ctx
.code
[i
].stack
)
5974 g_free (ctx
.code
[i
].stack
);
5978 for (tmp
= ctx
.funptrs
; tmp
; tmp
= tmp
->next
)
5980 g_slist_free (ctx
.funptrs
);
5982 for (tmp
= ctx
.exception_types
; tmp
; tmp
= tmp
->next
)
5983 mono_metadata_free_type (tmp
->data
);
5984 g_slist_free (ctx
.exception_types
);
5986 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
5988 mono_metadata_free_type (ctx
.locals
[i
]);
5990 for (i
= 0; i
< ctx
.max_args
; ++i
) {
5992 mono_metadata_free_type (ctx
.params
[i
]);
5996 g_free (ctx
.eval
.stack
);
5999 g_free (ctx
.locals
);
6000 g_free (ctx
.params
);
6006 mono_verify_corlib ()
6008 /* This is a public API function so cannot be removed */
6013 * Returns true if @method needs to be verified.
6017 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
6019 return mono_verifier_is_enabled_for_class (method
->klass
) && method
->wrapper_type
== MONO_WRAPPER_NONE
;
6023 * Returns true if @klass need to be verified.
6027 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
6029 return verify_all
|| (verifier_mode
> MONO_VERIFIER_MODE_OFF
&& !klass
->image
->assembly
->in_gac
&& klass
->image
!= mono_defaults
.corlib
);
6033 mono_verifier_is_enabled_for_image (MonoImage
*image
)
6035 return verify_all
|| verifier_mode
> MONO_VERIFIER_MODE_OFF
;
6039 mono_verifier_is_method_full_trust (MonoMethod
*method
)
6041 return mono_verifier_is_class_full_trust (method
->klass
);
6045 * Returns if @klass is under full trust or not.
6047 * TODO This code doesn't take CAS into account.
6049 * Under verify_all all user code must be verifiable if no security option was set
6053 mono_verifier_is_class_full_trust (MonoClass
*klass
)
6055 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
6056 gboolean trusted_location
= (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR
) ?
6057 klass
->image
->assembly
->in_gac
: mono_security_core_clr_is_platform_image (klass
->image
);
6059 if (verify_all
&& verifier_mode
== MONO_VERIFIER_MODE_OFF
)
6060 return trusted_location
|| klass
->image
== mono_defaults
.corlib
;
6061 return verifier_mode
< MONO_VERIFIER_MODE_VERIFIABLE
|| trusted_location
|| klass
->image
== mono_defaults
.corlib
;
6065 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
6067 return mono_method_verify (method
,
6068 (verifier_mode
!= MONO_VERIFIER_MODE_STRICT
? MONO_VERIFY_NON_STRICT
: 0)
6069 | (!mono_verifier_is_method_full_trust (method
) ? MONO_VERIFY_FAIL_FAST
: 0)
6070 | (skip_visibility
? MONO_VERIFY_SKIP_VISIBILITY
: 0));
6074 get_field_end (MonoClassField
*field
)
6077 int size
= mono_type_size (field
->type
, &align
);
6079 size
= 4; /*FIXME Is this a safe bet?*/
6080 return size
+ field
->offset
;
6084 verify_class_for_overlapping_reference_fields (MonoClass
*class)
6087 gboolean is_fulltrust
= mono_verifier_is_class_full_trust (class);
6088 if (!((class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) || !class->has_references
)
6091 //we must check for stuff overlapping reference fields
6092 for (i
= 0; i
< class->field
.count
; ++i
) {
6093 MonoClassField
*field
= &class->fields
[i
];
6094 int fieldEnd
= get_field_end (field
);
6095 gboolean is_valuetype
= !MONO_TYPE_IS_REFERENCE (field
->type
);
6096 if (mono_field_is_deleted (field
) || (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6099 for (j
= i
+ 1; j
< class->field
.count
; ++j
) {
6100 MonoClassField
*other
= &class->fields
[j
];
6101 int otherEnd
= get_field_end (other
);
6102 if (mono_field_is_deleted (other
) || (is_valuetype
&& !MONO_TYPE_IS_REFERENCE (other
->type
)) || (other
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6105 if (!is_valuetype
&& MONO_TYPE_IS_REFERENCE (other
->type
) && field
->offset
== other
->offset
&& is_fulltrust
)
6108 if ((otherEnd
> field
->offset
&& otherEnd
<= fieldEnd
) || (other
->offset
>= field
->offset
&& other
->offset
< fieldEnd
))
6116 field_hash (gconstpointer key
)
6118 const MonoClassField
*field
= key
;
6119 return g_str_hash (field
->name
) ^ mono_metadata_type_hash (field
->type
); /**/
6123 field_equals (gconstpointer _a
, gconstpointer _b
)
6125 const MonoClassField
*a
= _a
;
6126 const MonoClassField
*b
= _b
;
6127 return !strcmp (a
->name
, b
->name
) && mono_metadata_type_equal (a
->type
, b
->type
);
6132 verify_class_fields (MonoClass
*class)
6134 gpointer iter
= NULL
;
6135 MonoClassField
*field
;
6136 MonoGenericContext
*context
= mono_class_get_context (class);
6137 GHashTable
*unique_fields
= g_hash_table_new_full (&field_hash
, &field_equals
, NULL
, NULL
);
6138 if (class->generic_container
)
6139 context
= &class->generic_container
->context
;
6141 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
6142 if (!mono_type_is_valid_type_in_context (field
->type
, context
)) {
6143 g_hash_table_destroy (unique_fields
);
6146 if (g_hash_table_lookup (unique_fields
, field
)) {
6147 g_hash_table_destroy (unique_fields
);
6150 g_hash_table_insert (unique_fields
, field
, field
);
6152 g_hash_table_destroy (unique_fields
);
6157 verify_interfaces (MonoClass
*class)
6160 for (i
= 0; i
< class->interface_count
; ++i
) {
6161 MonoClass
*iface
= class->interfaces
[i
];
6162 if (!(iface
->flags
& TYPE_ATTRIBUTE_INTERFACE
))
6169 verify_valuetype_layout_with_target (MonoClass
*class, MonoClass
*target_class
)
6172 gpointer iter
= NULL
;
6173 MonoClassField
*field
;
6174 MonoClass
*field_class
;
6176 if (!class->valuetype
)
6179 type
= class->byval_arg
.type
;
6180 /*primitive type fields are not properly decoded*/
6181 if ((type
>= MONO_TYPE_BOOLEAN
&& type
<= MONO_TYPE_R8
) || (type
>= MONO_TYPE_I
&& type
<= MONO_TYPE_U
))
6184 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
6188 if (field
->type
->attrs
& (FIELD_ATTRIBUTE_STATIC
| FIELD_ATTRIBUTE_HAS_FIELD_RVA
))
6191 field_class
= mono_class_get_generic_type_definition (mono_class_from_mono_type (field
->type
));
6193 if (field_class
== target_class
|| !verify_valuetype_layout_with_target (field_class
, target_class
))
6201 verify_valuetype_layout (MonoClass
*class)
6204 res
= verify_valuetype_layout_with_target (class, class);
6209 * Check if the class is verifiable.
6211 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6212 * field and invalid generic instantiation are fatal errors.
6214 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6218 mono_verifier_verify_class (MonoClass
*class)
6220 if (class->generic_container
&& (class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
)
6222 if (!verify_class_for_overlapping_reference_fields (class))
6224 if (class->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, class))
6226 if (class->generic_class
== NULL
&& !verify_class_fields (class))
6228 if (class->valuetype
&& !verify_valuetype_layout (class))
6230 if (!verify_interfaces (class))
6237 mono_verifier_verify_class (MonoClass
*class)
6239 /* The verifier was disabled at compile time */
6244 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
6246 /* The verifier was disabled at compile time */
6251 mono_verifier_is_class_full_trust (MonoClass
*klass
)
6253 /* The verifier was disabled at compile time */
6258 mono_verifier_is_method_full_trust (MonoMethod
*method
)
6260 /* The verifier was disabled at compile time */
6265 mono_verifier_is_enabled_for_image (MonoImage
*image
)
6267 /* The verifier was disabled at compile time */
6272 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
6274 /* The verifier was disabled at compile time */
6279 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
6281 /* The verifier was disabled at compile time */
6286 mono_method_verify (MonoMethod
*method
, int level
)
6288 /* The verifier was disabled at compile time */
6293 mono_free_verify_list (GSList
*list
)
6295 /* The verifier was disabled at compile time */
6296 /* will always be null if verifier is disabled */
6300 mono_image_verify_tables (MonoImage
*image
, int level
)
6302 /* The verifier was disabled at compile time */