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)
9 * Copyright 2011 Rodrigo Kumpera
10 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <mono/metadata/object-internals.h>
15 #include <mono/metadata/dynamic-image-internals.h>
16 #include <mono/metadata/verify.h>
17 #include <mono/metadata/verify-internals.h>
18 #include <mono/metadata/opcodes.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/reflection.h>
21 #include <mono/metadata/debug-helpers.h>
22 #include <mono/metadata/mono-endian.h>
23 #include <mono/metadata/metadata.h>
24 #include <mono/metadata/metadata-internals.h>
25 #include <mono/metadata/class-internals.h>
26 #include <mono/metadata/security-manager.h>
27 #include <mono/metadata/security-core-clr.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/mono-basic-block.h>
30 #include <mono/metadata/attrdefs.h>
31 #include <mono/metadata/class-internals.h>
32 #include <mono/utils/mono-counters.h>
33 #include <mono/utils/monobitset.h>
37 static MiniVerifierMode verifier_mode
= MONO_VERIFIER_MODE_OFF
;
38 static gboolean verify_all
= FALSE
;
41 * Set the desired level of checks for the verfier.
45 mono_verifier_set_mode (MiniVerifierMode mode
)
51 mono_verifier_enable_verify_all ()
56 #ifndef DISABLE_VERIFIER
58 * Pull the list of opcodes
60 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
64 #include "mono/cil/opcode.def"
69 #ifdef MONO_VERIFIER_DEBUG
70 #define VERIFIER_DEBUG(code) do { code } while (0)
72 #define VERIFIER_DEBUG(code)
75 //////////////////////////////////////////////////////////////////
76 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
77 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
78 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
79 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
80 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
81 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
83 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
84 vinfo->info.status = __status; \
85 vinfo->info.message = ( __msg ); \
86 vinfo->exception_type = (__exception); \
87 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
90 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
91 #define ADD_VERIFY_ERROR(__ctx, __msg) \
93 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
97 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
99 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
100 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
101 (__ctx)->verifiable = 0; \
102 if (IS_FAIL_FAST_MODE (__ctx)) \
103 (__ctx)->valid = 0; \
107 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
109 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
110 (__ctx)->valid = 0; \
113 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
115 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
116 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
117 (__ctx)->verifiable = 0; \
118 if (IS_FAIL_FAST_MODE (__ctx)) \
119 (__ctx)->valid = 0; \
123 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
124 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
126 #if SIZEOF_VOID_P == 4
127 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
129 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
132 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
133 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
135 /*Flags to be used with ILCodeDesc::flags */
137 /*Instruction has not been processed.*/
138 IL_CODE_FLAG_NOT_PROCESSED
= 0,
139 /*Instruction was decoded by mono_method_verify loop.*/
140 IL_CODE_FLAG_SEEN
= 1,
141 /*Instruction was target of a branch or is at a protected block boundary.*/
142 IL_CODE_FLAG_WAS_TARGET
= 2,
143 /*Used by stack_init to avoid double initialize each entry.*/
144 IL_CODE_FLAG_STACK_INITED
= 4,
145 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
146 IL_CODE_STACK_MERGED
= 8,
147 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
148 IL_CODE_DELEGATE_SEQUENCE
= 0x10,
149 /*This is a delegate created from a ldftn to a non final virtual method*/
150 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
= 0x20,
151 /*This is a call to a non final virtual method*/
152 IL_CODE_CALL_NONFINAL_VIRTUAL
= 0x40,
170 guint16 size
, max_size
;
187 /*Allocated fnptr MonoType that should be freed by us.*/
189 /*Type dup'ed exception types from catch blocks.*/
190 GSList
*exception_types
;
195 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
199 MonoMethodSignature
*signature
;
200 MonoMethodHeader
*header
;
202 MonoGenericContext
*generic_context
;
206 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
207 *on a method that creates a delegate for a non-final virtual method using ldftn*/
208 gboolean has_this_store
;
210 /*This flag is used to control if the contructor of the parent class has been called.
211 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
212 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
214 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
215 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
217 gboolean super_ctor_called
;
221 MonoType
*constrained_type
;
225 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
);
228 get_stack_type (MonoType
*type
);
231 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
);
234 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
);
237 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
);
239 static MonoGenericParam
*
240 verifier_get_generic_param_from_type (VerifyContext
*ctx
, MonoType
*type
);
243 verifier_class_is_assignable_from (MonoClass
*target
, MonoClass
*candidate
);
244 //////////////////////////////////////////////////////////////////
249 TYPE_INV
= 0, /* leave at 0. */
254 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
256 /* value types and classes */
258 /* Number of types, used to define the size of the tables*/
261 /* Used by tables to signal that a result is not verifiable*/
262 NON_VERIFIABLE_RESULT
= 0x80,
264 /*Mask used to extract just the type, excluding flags */
267 /* The stack type is a managed pointer, unmask the value to res */
268 POINTER_MASK
= 0x100,
270 /*Stack type with the pointer mask*/
271 RAW_TYPE_MASK
= 0x10F,
273 /* Controlled Mutability Manager Pointer */
276 /* The stack type is a null literal*/
277 NULL_LITERAL_MASK
= 0x400,
279 /**Used by ldarg.0 and family to let delegate verification happens.*/
280 THIS_POINTER_MASK
= 0x800,
282 /**Signals that this is a boxed value type*/
285 /*This is an unitialized this ref*/
286 UNINIT_THIS_MASK
= 0x2000,
289 static const char* const
290 type_names
[TYPE_MAX
+ 1] = {
301 PREFIX_UNALIGNED
= 1,
304 PREFIX_CONSTRAINED
= 8,
307 //////////////////////////////////////////////////////////////////
309 #ifdef ENABLE_VERIFIER_STATS
311 #define _MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
312 #define _MEM_FREE(amt) do { working_set -= (amt); } while (0)
314 static int allocated_memory
;
315 static int working_set
;
316 static int max_allocated_memory
;
317 static int max_working_set
;
318 static int total_allocated_memory
;
321 finish_collect_stats (void)
323 max_allocated_memory
= MAX (max_allocated_memory
, allocated_memory
);
324 max_working_set
= MAX (max_working_set
, working_set
);
325 total_allocated_memory
+= allocated_memory
;
326 allocated_memory
= working_set
= 0;
330 init_verifier_stats (void)
332 static gboolean inited
;
335 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &max_allocated_memory
);
336 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &max_working_set
);
337 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &total_allocated_memory
);
343 #define _MEM_ALLOC(amt) do {} while (0)
344 #define _MEM_FREE(amt) do { } while (0)
346 #define finish_collect_stats()
347 #define init_verifier_stats()
352 //////////////////////////////////////////////////////////////////
355 /*Token validation macros and functions */
356 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
357 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
358 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
359 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
361 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
362 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
363 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
364 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
365 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
366 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
369 * Verify if @token refers to a valid row on int's table.
372 token_bounds_check (MonoImage
*image
, guint32 token
)
374 if (image_is_dynamic (image
))
375 return mono_dynamic_image_is_valid_token ((MonoDynamicImage
*)image
, token
);
376 return image
->tables
[mono_metadata_token_table (token
)].rows
>= mono_metadata_token_index (token
) && mono_metadata_token_index (token
) > 0;
380 mono_type_create_fnptr_from_mono_method (VerifyContext
*ctx
, MonoMethod
*method
)
382 MonoType
*res
= g_new0 (MonoType
, 1);
383 _MEM_ALLOC (sizeof (MonoType
));
385 //FIXME use mono_method_get_signature_full
386 res
->data
.method
= mono_method_signature (method
);
387 res
->type
= MONO_TYPE_FNPTR
;
388 ctx
->funptrs
= g_slist_prepend (ctx
->funptrs
, res
);
393 * mono_type_is_enum_type:
395 * Returns: TRUE if @type is an enum type.
398 mono_type_is_enum_type (MonoType
*type
)
400 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
402 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
408 * mono_type_is_value_type:
410 * Returns: TRUE if @type is named after @namespace.@name.
414 mono_type_is_value_type (MonoType
*type
, const char *namespace_
, const char *name
)
416 return type
->type
== MONO_TYPE_VALUETYPE
&&
417 !strcmp (namespace_
, type
->data
.klass
->name_space
) &&
418 !strcmp (name
, type
->data
.klass
->name
);
422 * Returns TURE if @type is VAR or MVAR
425 mono_type_is_generic_argument (MonoType
*type
)
427 return type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
;
431 * mono_type_get_underlying_type_any:
433 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
435 * Returns the underlying type of @type regardless if it is byref or not.
438 mono_type_get_underlying_type_any (MonoType
*type
)
440 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
441 return mono_class_enum_basetype (type
->data
.klass
);
442 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
443 return mono_class_enum_basetype (type
->data
.generic_class
->container_class
);
447 static G_GNUC_UNUSED
const char*
448 mono_type_get_stack_name (MonoType
*type
)
450 return type_names
[get_stack_type (type
) & TYPE_MASK
];
453 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
454 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
457 mono_method_is_constructor (MonoMethod
*method
)
459 return ((method
->flags
& CTOR_REQUIRED_FLAGS
) == CTOR_REQUIRED_FLAGS
&&
460 !(method
->flags
& CTOR_INVALID_FLAGS
) &&
461 !strcmp (".ctor", method
->name
));
465 mono_class_has_default_constructor (MonoClass
*klass
)
470 mono_class_setup_methods (klass
);
471 if (mono_class_has_failure (klass
))
474 int mcount
= mono_class_get_method_count (klass
);
475 for (i
= 0; i
< mcount
; ++i
) {
476 method
= klass
->methods
[i
];
477 if (mono_method_is_constructor (method
) &&
478 mono_method_signature (method
) &&
479 mono_method_signature (method
)->param_count
== 0 &&
480 (method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
)
487 * Verify if @type is valid for the given @ctx verification context.
488 * this function checks for VAR and MVAR types that are invalid under the current verifier,
491 mono_type_is_valid_type_in_context_full (MonoType
*type
, MonoGenericContext
*context
, gboolean check_gtd
)
494 MonoGenericInst
*inst
;
496 switch (type
->type
) {
501 inst
= type
->type
== MONO_TYPE_VAR
? context
->class_inst
: context
->method_inst
;
502 if (!inst
|| mono_type_get_generic_param_num (type
) >= inst
->type_argc
)
505 case MONO_TYPE_SZARRAY
:
506 return mono_type_is_valid_type_in_context_full (&type
->data
.klass
->byval_arg
, context
, check_gtd
);
507 case MONO_TYPE_ARRAY
:
508 return mono_type_is_valid_type_in_context_full (&type
->data
.array
->eklass
->byval_arg
, context
, check_gtd
);
510 return mono_type_is_valid_type_in_context_full (type
->data
.type
, context
, check_gtd
);
511 case MONO_TYPE_GENERICINST
:
512 inst
= type
->data
.generic_class
->context
.class_inst
;
515 for (i
= 0; i
< inst
->type_argc
; ++i
)
516 if (!mono_type_is_valid_type_in_context_full (inst
->type_argv
[i
], context
, check_gtd
))
519 case MONO_TYPE_CLASS
:
520 case MONO_TYPE_VALUETYPE
: {
521 MonoClass
*klass
= type
->data
.klass
;
523 * It's possible to encode generic'sh types in such a way that they disguise themselves as class or valuetype.
524 * Fixing the type decoding is really tricky since under some cases this behavior is needed, for example, to
525 * have a 'class' type pointing to a 'genericinst' class.
527 * For the runtime these non canonical (weird) encodings work fine, the worst they can cause is some
528 * reflection oddities which are harmless - to security at least.
530 if (klass
->byval_arg
.type
!= type
->type
)
531 return mono_type_is_valid_type_in_context_full (&klass
->byval_arg
, context
, check_gtd
);
533 if (check_gtd
&& mono_class_is_gtd (klass
))
544 mono_type_is_valid_type_in_context (MonoType
*type
, MonoGenericContext
*context
)
546 return mono_type_is_valid_type_in_context_full (type
, context
, FALSE
);
549 /*This function returns NULL if the type is not instantiatable*/
551 verifier_inflate_type (VerifyContext
*ctx
, MonoType
*type
, MonoGenericContext
*context
)
556 result
= mono_class_inflate_generic_type_checked (type
, context
, &error
);
557 if (!mono_error_ok (&error
)) {
558 mono_error_cleanup (&error
);
564 /*A side note here. We don't need to check if arguments are broken since this
565 is only need to be done by the runtime before realizing the type.
568 is_valid_generic_instantiation (MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
573 if (ginst
->type_argc
!= gc
->type_argc
)
576 for (i
= 0; i
< gc
->type_argc
; ++i
) {
577 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
578 MonoClass
*paramClass
;
579 MonoClass
**constraints
;
580 MonoType
*param_type
= ginst
->type_argv
[i
];
582 /*it's not our job to validate type variables*/
583 if (mono_type_is_generic_argument (param_type
))
586 paramClass
= mono_class_from_mono_type (param_type
);
589 /* A GTD can't be a generic argument.
591 * Due to how types are encoded we must check for the case of a genericinst MonoType and GTD MonoClass.
592 * This happens in cases such as: class Foo<T> { void X() { new Bar<T> (); } }
594 * Open instantiations can have GTDs as this happens when one type is instantiated with others params
595 * and the former has an expansion into the later. For example:
597 * class A<T>: B<K> {}
598 * The type A <K> has a parent B<K>, that is inflated into the GTD B<>.
599 * Since A<K> is open, thus not instantiatable, this is valid.
601 if (mono_class_is_gtd (paramClass
) && param_type
->type
!= MONO_TYPE_GENERICINST
&& !ginst
->is_open
)
604 /*it's not safe to call mono_class_init from here*/
605 if (mono_class_is_ginst (paramClass
) && !paramClass
->inited
) {
606 if (!mono_class_is_valid_generic_instantiation (NULL
, paramClass
))
610 if (!param_info
->constraints
&& !(param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
))
613 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
) && (!paramClass
->valuetype
|| mono_class_is_nullable (paramClass
)))
616 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT
) && paramClass
->valuetype
)
619 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT
) && !paramClass
->valuetype
&& !mono_class_has_default_constructor (paramClass
))
622 if (!param_info
->constraints
)
625 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
626 MonoClass
*ctr
= *constraints
;
629 inflated
= mono_class_inflate_generic_type_checked (&ctr
->byval_arg
, context
, &error
);
630 if (!mono_error_ok (&error
)) {
631 mono_error_cleanup (&error
);
634 ctr
= mono_class_from_mono_type (inflated
);
635 mono_metadata_free_type (inflated
);
637 /*FIXME maybe we need the same this as verifier_class_is_assignable_from*/
638 if (!mono_class_is_assignable_from_slow (ctr
, paramClass
))
646 * mono_generic_param_is_constraint_compatible:
648 * Returns: TRUE if @candidate is constraint compatible with @target.
650 * This means that @candidate constraints are a super set of @target constaints
653 mono_generic_param_is_constraint_compatible (VerifyContext
*ctx
, MonoGenericParam
*target
, MonoGenericParam
*candidate
, MonoClass
*candidate_param_class
, MonoGenericContext
*context
)
655 MonoGenericParamInfo
*tinfo
= mono_generic_param_info (target
);
656 MonoGenericParamInfo
*cinfo
= mono_generic_param_info (candidate
);
657 MonoClass
**candidate_class
;
658 gboolean class_constraint_satisfied
= FALSE
;
659 gboolean valuetype_constraint_satisfied
= FALSE
;
661 int tmask
= tinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
662 int cmask
= cinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
664 if (cinfo
->constraints
) {
665 for (candidate_class
= cinfo
->constraints
; *candidate_class
; ++candidate_class
) {
667 MonoType
*inflated
= verifier_inflate_type (ctx
, &(*candidate_class
)->byval_arg
, ctx
->generic_context
);
670 cc
= mono_class_from_mono_type (inflated
);
671 mono_metadata_free_type (inflated
);
673 if (mono_type_is_reference (&cc
->byval_arg
) && !MONO_CLASS_IS_INTERFACE (cc
))
674 class_constraint_satisfied
= TRUE
;
675 else if (!mono_type_is_reference (&cc
->byval_arg
) && !MONO_CLASS_IS_INTERFACE (cc
))
676 valuetype_constraint_satisfied
= TRUE
;
679 class_constraint_satisfied
|= (cmask
& GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT
) != 0;
680 valuetype_constraint_satisfied
|= (cmask
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
) != 0;
682 if ((tmask
& GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT
) && !class_constraint_satisfied
)
684 if ((tmask
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
) && !valuetype_constraint_satisfied
)
686 if ((tmask
& GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT
) && !((cmask
& GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT
) ||
687 valuetype_constraint_satisfied
)) {
692 if (tinfo
->constraints
) {
693 MonoClass
**target_class
;
694 for (target_class
= tinfo
->constraints
; *target_class
; ++target_class
) {
696 MonoType
*inflated
= verifier_inflate_type (ctx
, &(*target_class
)->byval_arg
, context
);
699 tc
= mono_class_from_mono_type (inflated
);
700 mono_metadata_free_type (inflated
);
703 * A constraint from @target might inflate into @candidate itself and in that case we don't need
704 * check it's constraints since it satisfy the constraint by itself.
706 if (mono_metadata_type_equal (&tc
->byval_arg
, &candidate_param_class
->byval_arg
))
709 if (!cinfo
->constraints
)
712 for (candidate_class
= cinfo
->constraints
; *candidate_class
; ++candidate_class
) {
714 inflated
= verifier_inflate_type (ctx
, &(*candidate_class
)->byval_arg
, ctx
->generic_context
);
717 cc
= mono_class_from_mono_type (inflated
);
718 mono_metadata_free_type (inflated
);
720 if (verifier_class_is_assignable_from (tc
, cc
))
724 * This happens when we have the following:
726 * Bar<K> where K : IFace
727 * Foo<T, U> where T : U where U : IFace
729 * Bar<T> <- T here satisfy K constraint transitively through to U's constraint
732 if (mono_type_is_generic_argument (&cc
->byval_arg
)) {
733 MonoGenericParam
*other_candidate
= verifier_get_generic_param_from_type (ctx
, &cc
->byval_arg
);
735 if (mono_generic_param_is_constraint_compatible (ctx
, target
, other_candidate
, cc
, context
)) {
740 if (!*candidate_class
)
747 static MonoGenericParam
*
748 verifier_get_generic_param_from_type (VerifyContext
*ctx
, MonoType
*type
)
750 MonoGenericContainer
*gc
;
751 MonoMethod
*method
= ctx
->method
;
754 num
= mono_type_get_generic_param_num (type
);
756 if (type
->type
== MONO_TYPE_VAR
) {
757 MonoClass
*gtd
= method
->klass
;
758 if (mono_class_is_ginst (gtd
))
759 gtd
= mono_class_get_generic_class (gtd
)->container_class
;
760 gc
= mono_class_try_get_generic_container (gtd
);
762 MonoMethod
*gmd
= method
;
763 if (method
->is_inflated
)
764 gmd
= ((MonoMethodInflated
*)method
)->declaring
;
765 gc
= mono_method_get_generic_container (gmd
);
769 return mono_generic_container_get_param (gc
, num
);
775 * Verify if @type is valid for the given @ctx verification context.
776 * this function checks for VAR and MVAR types that are invalid under the current verifier,
777 * This means that it either
780 is_valid_type_in_context (VerifyContext
*ctx
, MonoType
*type
)
782 return mono_type_is_valid_type_in_context (type
, ctx
->generic_context
);
786 is_valid_generic_instantiation_in_context (VerifyContext
*ctx
, MonoGenericInst
*ginst
, gboolean check_gtd
)
789 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
790 MonoType
*type
= ginst
->type_argv
[i
];
792 if (!mono_type_is_valid_type_in_context_full (type
, ctx
->generic_context
, TRUE
))
799 generic_arguments_respect_constraints (VerifyContext
*ctx
, MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
802 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
803 MonoType
*type
= ginst
->type_argv
[i
];
804 MonoGenericParam
*target
= mono_generic_container_get_param (gc
, i
);
805 MonoGenericParam
*candidate
;
806 MonoClass
*candidate_class
;
808 if (!mono_type_is_generic_argument (type
))
811 if (!is_valid_type_in_context (ctx
, type
))
814 candidate
= verifier_get_generic_param_from_type (ctx
, type
);
815 candidate_class
= mono_class_from_mono_type (type
);
817 if (!mono_generic_param_is_constraint_compatible (ctx
, target
, candidate
, candidate_class
, context
))
824 mono_method_repect_method_constraints (VerifyContext
*ctx
, MonoMethod
*method
)
826 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
827 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
828 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
829 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gmethod
->context
, ginst
);
833 mono_class_repect_method_constraints (VerifyContext
*ctx
, MonoClass
*klass
)
835 MonoGenericClass
*gklass
= mono_class_get_generic_class (klass
);
836 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
837 MonoGenericContainer
*gc
= mono_class_get_generic_container (gklass
->container_class
);
838 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gklass
->context
, ginst
);
842 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
)
844 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
845 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
846 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
847 if (!gc
) /*non-generic inflated method - it's part of a generic type */
849 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
, TRUE
))
851 return is_valid_generic_instantiation (gc
, &gmethod
->context
, ginst
);
856 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
)
858 MonoGenericClass
*gklass
= mono_class_get_generic_class (klass
);
859 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
860 MonoGenericContainer
*gc
= mono_class_get_generic_container (gklass
->container_class
);
861 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
, TRUE
))
863 return is_valid_generic_instantiation (gc
, &gklass
->context
, ginst
);
867 mono_type_is_valid_in_context (VerifyContext
*ctx
, MonoType
*type
)
872 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid null type at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
876 if (!is_valid_type_in_context (ctx
, type
)) {
877 char *str
= mono_type_full_name (type
);
878 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
879 str
[0] == '!' ? "" : type
->type
== MONO_TYPE_VAR
? "!" : "!!",
881 type
->type
== MONO_TYPE_VAR
? "class" : "method",
883 MONO_EXCEPTION_BAD_IMAGE
);
888 klass
= mono_class_from_mono_type (type
);
889 mono_class_init (klass
);
890 if (mono_class_has_failure (klass
)) {
891 if (mono_class_is_ginst (klass
) && !mono_class_is_valid_generic_instantiation (NULL
, klass
))
892 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
);
894 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
);
898 if (mono_class_is_ginst (klass
) && mono_class_has_failure (mono_class_get_generic_class (klass
)->container_class
)) {
899 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
);
903 if (!mono_class_is_ginst (klass
))
906 if (!mono_class_is_valid_generic_instantiation (ctx
, klass
)) {
907 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
);
911 if (!mono_class_repect_method_constraints (ctx
, klass
)) {
912 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
);
919 static verify_result_t
920 mono_method_is_valid_in_context (VerifyContext
*ctx
, MonoMethod
*method
)
922 if (!mono_type_is_valid_in_context (ctx
, &method
->klass
->byval_arg
))
923 return RESULT_INVALID
;
925 if (!method
->is_inflated
)
928 if (!mono_method_is_valid_generic_instantiation (ctx
, method
)) {
929 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
);
930 return RESULT_INVALID
;
933 if (!mono_method_repect_method_constraints (ctx
, method
)) {
934 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
));
935 return RESULT_UNVERIFIABLE
;
941 static MonoClassField
*
942 verifier_load_field (VerifyContext
*ctx
, int token
, MonoClass
**out_klass
, const char *opcode
) {
944 MonoClassField
*field
;
945 MonoClass
*klass
= NULL
;
947 if (ctx
->method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
948 field
= (MonoClassField
*)mono_method_get_wrapper_data (ctx
->method
, (guint32
)token
);
949 klass
= field
? field
->parent
: NULL
;
951 if (!IS_FIELD_DEF_OR_REF (token
) || !token_bounds_check (ctx
->image
, token
)) {
952 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
);
956 field
= mono_field_from_token_checked (ctx
->image
, token
, &klass
, ctx
->generic_context
, &error
);
957 mono_error_cleanup (&error
); /*FIXME don't swallow the error */
960 if (!field
|| !field
->parent
|| !klass
) {
961 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
);
965 if (!mono_type_is_valid_in_context (ctx
, &klass
->byval_arg
))
968 if (mono_field_get_flags (field
) & FIELD_ATTRIBUTE_LITERAL
) {
969 char *type_name
= mono_type_get_full_name (field
->parent
);
970 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot reference literal field %s::%s at 0x%04x", type_name
, field
->name
, ctx
->ip_offset
));
980 verifier_load_method (VerifyContext
*ctx
, int token
, const char *opcode
) {
984 if (ctx
->method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
985 method
= (MonoMethod
*)mono_method_get_wrapper_data (ctx
->method
, (guint32
)token
);
988 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
989 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
);
993 method
= mono_get_method_checked (ctx
->image
, token
, NULL
, ctx
->generic_context
, &error
);
994 mono_error_cleanup (&error
); /* FIXME don't swallow this error */
998 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
);
1002 if (mono_method_is_valid_in_context (ctx
, method
) == RESULT_INVALID
)
1009 verifier_load_type (VerifyContext
*ctx
, int token
, const char *opcode
) {
1012 if (ctx
->method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
1013 MonoClass
*klass
= (MonoClass
*)mono_method_get_wrapper_data (ctx
->method
, (guint32
)token
);
1014 type
= klass
? &klass
->byval_arg
: NULL
;
1017 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
1018 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
1021 type
= mono_type_get_checked (ctx
->image
, token
, ctx
->generic_context
, &error
);
1022 mono_error_cleanup (&error
); /*FIXME don't swallow the error */
1026 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
);
1030 if (!mono_type_is_valid_in_context (ctx
, type
))
1037 /* stack_slot_get_type:
1039 * Returns the stack type of @value. This value includes POINTER_MASK.
1041 * Use this function to checks that account for a managed pointer.
1044 stack_slot_get_type (ILStackDesc
*value
)
1046 return value
->stype
& RAW_TYPE_MASK
;
1049 /* stack_slot_get_underlying_type:
1051 * Returns the stack type of @value. This value does not include POINTER_MASK.
1053 * Use this function is cases where the fact that the value could be a managed pointer is
1054 * irrelevant. For example, field load doesn't care about this fact of type on stack.
1057 stack_slot_get_underlying_type (ILStackDesc
*value
)
1059 return value
->stype
& TYPE_MASK
;
1062 /* stack_slot_is_managed_pointer:
1064 * Returns TRUE is @value is a managed pointer.
1067 stack_slot_is_managed_pointer (ILStackDesc
*value
)
1069 return (value
->stype
& POINTER_MASK
) == POINTER_MASK
;
1072 /* stack_slot_is_managed_mutability_pointer:
1074 * Returns TRUE is @value is a managed mutability pointer.
1076 static G_GNUC_UNUSED gboolean
1077 stack_slot_is_managed_mutability_pointer (ILStackDesc
*value
)
1079 return (value
->stype
& CMMP_MASK
) == CMMP_MASK
;
1082 /* stack_slot_is_null_literal:
1084 * Returns TRUE is @value is the null literal.
1087 stack_slot_is_null_literal (ILStackDesc
*value
)
1089 return (value
->stype
& NULL_LITERAL_MASK
) == NULL_LITERAL_MASK
;
1093 /* stack_slot_is_this_pointer:
1095 * Returns TRUE is @value is the this literal
1098 stack_slot_is_this_pointer (ILStackDesc
*value
)
1100 return (value
->stype
& THIS_POINTER_MASK
) == THIS_POINTER_MASK
;
1103 /* stack_slot_is_boxed_value:
1105 * Returns TRUE is @value is a boxed value
1108 stack_slot_is_boxed_value (ILStackDesc
*value
)
1110 return (value
->stype
& BOXED_MASK
) == BOXED_MASK
;
1114 stack_slot_get_name (ILStackDesc
*value
)
1116 return type_names
[value
->stype
& TYPE_MASK
];
1119 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
1120 if (PRED (value)) { \
1122 g_string_append (str, ", "); \
1123 g_string_append (str, NAME); \
1128 stack_slot_stack_type_full_name (ILStackDesc
*value
)
1130 GString
*str
= g_string_new ("");
1132 gboolean has_pred
= FALSE
, first
= TRUE
;
1134 if ((value
->stype
& TYPE_MASK
) != value
->stype
) {
1135 g_string_append(str
, "[");
1136 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer
, "this");
1137 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value
, "boxed");
1138 APPEND_WITH_PREDICATE (stack_slot_is_null_literal
, "null");
1139 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer
, "cmmp");
1140 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer
, "mp");
1144 if (mono_type_is_generic_argument (value
->type
) && !stack_slot_is_boxed_value (value
)) {
1146 g_string_append(str
, "[");
1148 g_string_append (str
, ", ");
1149 g_string_append (str
, "unboxed");
1154 g_string_append(str
, "] ");
1156 g_string_append (str
, stack_slot_get_name (value
));
1158 g_string_free (str
, FALSE
);
1163 stack_slot_full_name (ILStackDesc
*value
)
1165 char *type_name
= mono_type_full_name (value
->type
);
1166 char *stack_name
= stack_slot_stack_type_full_name (value
);
1167 char *res
= g_strdup_printf ("%s (%s)", type_name
, stack_name
);
1169 g_free (stack_name
);
1173 //////////////////////////////////////////////////////////////////
1175 mono_free_verify_list (GSList
*list
)
1177 MonoVerifyInfoExtended
*info
;
1180 for (tmp
= list
; tmp
; tmp
= tmp
->next
) {
1181 info
= (MonoVerifyInfoExtended
*)tmp
->data
;
1182 g_free (info
->info
.message
);
1185 g_slist_free (list
);
1188 #define ADD_ERROR(list,msg) \
1190 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1191 vinfo->info.status = MONO_VERIFY_ERROR; \
1192 vinfo->info.message = (msg); \
1193 (list) = g_slist_prepend ((list), vinfo); \
1196 #define ADD_WARN(list,code,msg) \
1198 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1199 vinfo->info.status = (code); \
1200 vinfo->info.message = (msg); \
1201 (list) = g_slist_prepend ((list), vinfo); \
1204 #define ADD_INVALID(list,msg) \
1206 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1207 vinfo->status = MONO_VERIFY_ERROR; \
1208 vinfo->message = (msg); \
1209 (list) = g_slist_prepend ((list), vinfo); \
1210 /*G_BREAKPOINT ();*/ \
1214 #define CHECK_STACK_UNDERFLOW(num) \
1216 if (cur_stack < (num)) \
1217 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1220 #define CHECK_STACK_OVERFLOW() \
1222 if (cur_stack >= max_stack) \
1223 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1228 in_any_block (MonoMethodHeader
*header
, guint offset
)
1231 MonoExceptionClause
*clause
;
1233 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1234 clause
= &header
->clauses
[i
];
1235 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1237 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1239 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1246 * in_any_exception_block:
1248 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1251 in_any_exception_block (MonoMethodHeader
*header
, guint offset
)
1254 MonoExceptionClause
*clause
;
1256 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1257 clause
= &header
->clauses
[i
];
1258 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1260 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1267 * is_valid_branch_instruction:
1269 * Verify if it's valid to perform a branch from @offset to @target.
1270 * This should be used with br and brtrue/false.
1271 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1272 * The major difference from other similiar functions is that branching into a
1273 * finally/fault block is invalid instead of just unverifiable.
1276 is_valid_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1279 MonoExceptionClause
*clause
;
1281 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1282 clause
= &header
->clauses
[i
];
1283 /*branching into a finally block is invalid*/
1284 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
) &&
1285 !MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1286 MONO_OFFSET_IN_HANDLER (clause
, target
))
1289 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1291 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1293 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1300 * is_valid_cmp_branch_instruction:
1302 * Verify if it's valid to perform a branch from @offset to @target.
1303 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1304 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1306 * The major differences from other similar functions are that most errors lead to invalid
1307 * code and only branching out of finally, filter or fault clauses is unverifiable.
1310 is_valid_cmp_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1313 MonoExceptionClause
*clause
;
1315 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1316 clause
= &header
->clauses
[i
];
1317 /*branching out of a handler or finally*/
1318 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
&&
1319 MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1320 !MONO_OFFSET_IN_HANDLER (clause
, target
))
1323 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1325 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1327 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1334 * A leave can't escape a finally block
1337 is_correct_leave (MonoMethodHeader
*header
, guint offset
, guint target
)
1340 MonoExceptionClause
*clause
;
1342 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1343 clause
= &header
->clauses
[i
];
1344 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
&& MONO_OFFSET_IN_HANDLER (clause
, offset
) && !MONO_OFFSET_IN_HANDLER (clause
, target
))
1346 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1353 * A rethrow can't happen outside of a catch handler.
1356 is_correct_rethrow (MonoMethodHeader
*header
, guint offset
)
1359 MonoExceptionClause
*clause
;
1361 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1362 clause
= &header
->clauses
[i
];
1363 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1370 * An endfinally can't happen outside of a finally/fault handler.
1373 is_correct_endfinally (MonoMethodHeader
*header
, guint offset
)
1376 MonoExceptionClause
*clause
;
1378 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1379 clause
= &header
->clauses
[i
];
1380 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) && (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
))
1388 * An endfilter can only happens inside a filter clause.
1389 * In non-strict mode filter is allowed inside the handler clause too
1391 static MonoExceptionClause
*
1392 is_correct_endfilter (VerifyContext
*ctx
, guint offset
)
1395 MonoExceptionClause
*clause
;
1397 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1398 clause
= &ctx
->header
->clauses
[i
];
1399 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_FILTER
)
1401 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1403 if (!IS_STRICT_MODE (ctx
) && MONO_OFFSET_IN_HANDLER (clause
, offset
))
1411 * Non-strict endfilter can happens inside a try block or any handler block
1414 is_unverifiable_endfilter (VerifyContext
*ctx
, guint offset
)
1417 MonoExceptionClause
*clause
;
1419 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1420 clause
= &ctx
->header
->clauses
[i
];
1421 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1428 is_valid_bool_arg (ILStackDesc
*arg
)
1430 if (stack_slot_is_managed_pointer (arg
) || stack_slot_is_boxed_value (arg
) || stack_slot_is_null_literal (arg
))
1434 switch (stack_slot_get_underlying_type (arg
)) {
1437 case TYPE_NATIVE_INT
:
1441 g_assert (arg
->type
);
1442 switch (arg
->type
->type
) {
1443 case MONO_TYPE_CLASS
:
1444 case MONO_TYPE_STRING
:
1445 case MONO_TYPE_OBJECT
:
1446 case MONO_TYPE_SZARRAY
:
1447 case MONO_TYPE_ARRAY
:
1448 case MONO_TYPE_FNPTR
:
1451 case MONO_TYPE_GENERICINST
:
1452 /*We need to check if the container class
1453 * of the generic type is a valuetype, iow:
1454 * is it a "class Foo<T>" or a "struct Foo<T>"?
1456 return !arg
->type
->data
.generic_class
->container_class
->valuetype
;
1466 /*Type manipulation helper*/
1468 /*Returns the byref version of the supplied MonoType*/
1470 mono_type_get_type_byref (MonoType
*type
)
1474 return &mono_class_from_mono_type (type
)->this_arg
;
1478 /*Returns the byval version of the supplied MonoType*/
1480 mono_type_get_type_byval (MonoType
*type
)
1484 return &mono_class_from_mono_type (type
)->byval_arg
;
1488 mono_type_from_stack_slot (ILStackDesc
*slot
)
1490 if (stack_slot_is_managed_pointer (slot
))
1491 return mono_type_get_type_byref (slot
->type
);
1495 /*Stack manipulation code*/
1498 ensure_stack_size (ILCodeDesc
*stack
, int required
)
1503 if (required
< stack
->max_size
)
1506 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1507 new_size
= MAX (8, MAX (required
, stack
->max_size
* 2));
1509 g_assert (new_size
>= stack
->size
);
1510 g_assert (new_size
>= required
);
1512 tmp
= g_new0 (ILStackDesc
, new_size
);
1513 _MEM_ALLOC (sizeof (ILStackDesc
) * new_size
);
1517 memcpy (tmp
, stack
->stack
, stack
->size
* sizeof (ILStackDesc
));
1518 g_free (stack
->stack
);
1519 _MEM_FREE (sizeof (ILStackDesc
) * stack
->max_size
);
1523 stack
->max_size
= new_size
;
1527 stack_init (VerifyContext
*ctx
, ILCodeDesc
*state
)
1529 if (state
->flags
& IL_CODE_FLAG_STACK_INITED
)
1531 state
->size
= state
->max_size
= 0;
1532 state
->flags
|= IL_CODE_FLAG_STACK_INITED
;
1536 stack_copy (ILCodeDesc
*to
, ILCodeDesc
*from
)
1538 ensure_stack_size (to
, from
->size
);
1539 to
->size
= from
->size
;
1541 /*stack copy happens at merge points, which have small stacks*/
1543 memcpy (to
->stack
, from
->stack
, sizeof (ILStackDesc
) * from
->size
);
1547 copy_stack_value (ILStackDesc
*to
, ILStackDesc
*from
)
1549 to
->stype
= from
->stype
;
1550 to
->type
= from
->type
;
1551 to
->method
= from
->method
;
1555 check_underflow (VerifyContext
*ctx
, int size
)
1557 if (ctx
->eval
.size
< size
) {
1558 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size
, ctx
->eval
.size
, ctx
->ip_offset
));
1565 check_overflow (VerifyContext
*ctx
)
1567 if (ctx
->eval
.size
>= ctx
->max_stack
) {
1568 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx
->eval
.size
+ 1, ctx
->ip_offset
));
1574 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1576 check_unmanaged_pointer (VerifyContext
*ctx
, ILStackDesc
*value
)
1578 if (stack_slot_get_type (value
) == TYPE_PTR
) {
1579 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
1585 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1587 check_unverifiable_type (VerifyContext
*ctx
, MonoType
*type
)
1589 if (type
->type
== MONO_TYPE_PTR
|| type
->type
== MONO_TYPE_FNPTR
) {
1590 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
1596 static ILStackDesc
*
1597 stack_push (VerifyContext
*ctx
)
1599 g_assert (ctx
->eval
.size
< ctx
->max_stack
);
1600 g_assert (ctx
->eval
.size
<= ctx
->eval
.max_size
);
1602 ensure_stack_size (&ctx
->eval
, ctx
->eval
.size
+ 1);
1604 return & ctx
->eval
.stack
[ctx
->eval
.size
++];
1607 static ILStackDesc
*
1608 stack_push_val (VerifyContext
*ctx
, int stype
, MonoType
*type
)
1610 ILStackDesc
*top
= stack_push (ctx
);
1616 static ILStackDesc
*
1617 stack_pop (VerifyContext
*ctx
)
1620 g_assert (ctx
->eval
.size
> 0);
1621 ret
= ctx
->eval
.stack
+ --ctx
->eval
.size
;
1622 if ((ret
->stype
& UNINIT_THIS_MASK
) == UNINIT_THIS_MASK
)
1623 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx
->ip_offset
));
1627 /* This function allows to safely pop an unititialized this ptr from
1628 * the eval stack without marking the method as unverifiable.
1630 static ILStackDesc
*
1631 stack_pop_safe (VerifyContext
*ctx
)
1633 g_assert (ctx
->eval
.size
> 0);
1634 return ctx
->eval
.stack
+ --ctx
->eval
.size
;
1637 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1639 stack_peek (VerifyContext
*ctx
, int distance
)
1641 g_assert (ctx
->eval
.size
- distance
> 0);
1642 return ctx
->eval
.stack
+ (ctx
->eval
.size
- 1 - distance
);
1645 static ILStackDesc
*
1646 stack_push_stack_val (VerifyContext
*ctx
, ILStackDesc
*value
)
1648 ILStackDesc
*top
= stack_push (ctx
);
1649 copy_stack_value (top
, value
);
1653 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1655 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1658 get_boxable_mono_type (VerifyContext
* ctx
, int token
, const char *opcode
)
1663 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
1666 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
1667 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1671 if (type
->type
== MONO_TYPE_VOID
) {
1672 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1676 if (type
->type
== MONO_TYPE_TYPEDBYREF
)
1677 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1679 if (!(klass
= mono_class_from_mono_type (type
)))
1680 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1682 if (mono_class_is_gtd (klass
) && type
->type
!= MONO_TYPE_GENERICINST
)
1683 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
));
1685 check_unverifiable_type (ctx
, type
);
1690 /*operation result tables */
1692 static const unsigned char bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1693 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1694 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1695 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1696 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1697 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1698 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1701 static const unsigned char add_table
[TYPE_MAX
][TYPE_MAX
] = {
1702 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1703 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1704 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1705 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1706 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1707 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1710 static const unsigned char sub_table
[TYPE_MAX
][TYPE_MAX
] = {
1711 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1712 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1713 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1714 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1715 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1716 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1719 static const unsigned char int_bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1720 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1721 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1722 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1723 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1724 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1725 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1728 static const unsigned char shift_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1729 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1730 {TYPE_I8
, TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1731 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1732 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1733 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1734 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1737 static const unsigned char cmp_br_op
[TYPE_MAX
][TYPE_MAX
] = {
1738 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1739 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1740 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1741 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
1742 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
1743 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1746 static const unsigned char cmp_br_eq_op
[TYPE_MAX
][TYPE_MAX
] = {
1747 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1748 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1749 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1750 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
1751 {TYPE_INV
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
1752 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
},
1755 static const unsigned char add_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
1756 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1757 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1758 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1759 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1760 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1761 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1764 static const unsigned char sub_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
1765 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1766 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1767 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1768 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1769 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1770 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1773 static const unsigned char bin_ovf_table
[TYPE_MAX
][TYPE_MAX
] = {
1774 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1775 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1776 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1777 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1778 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1779 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1782 #ifdef MONO_VERIFIER_DEBUG
1786 dump_stack_value (ILStackDesc
*value
)
1788 printf ("[(%x)(%x)", value
->type
->type
, value
->stype
);
1790 if (stack_slot_is_this_pointer (value
))
1793 if (stack_slot_is_boxed_value (value
))
1794 printf ("[boxed] ");
1796 if (stack_slot_is_null_literal (value
))
1799 if (stack_slot_is_managed_mutability_pointer (value
))
1800 printf ("Controled Mutability MP: ");
1802 if (stack_slot_is_managed_pointer (value
))
1803 printf ("Managed Pointer to: ");
1805 switch (stack_slot_get_underlying_type (value
)) {
1807 printf ("invalid type]");
1815 case TYPE_NATIVE_INT
:
1816 printf ("native int]");
1819 printf ("float64]");
1822 printf ("unmanaged pointer]");
1825 switch (value
->type
->type
) {
1826 case MONO_TYPE_CLASS
:
1827 case MONO_TYPE_VALUETYPE
:
1828 printf ("complex] (%s)", value
->type
->data
.klass
->name
);
1830 case MONO_TYPE_STRING
:
1831 printf ("complex] (string)");
1833 case MONO_TYPE_OBJECT
:
1834 printf ("complex] (object)");
1836 case MONO_TYPE_SZARRAY
:
1837 printf ("complex] (%s [])", value
->type
->data
.klass
->name
);
1839 case MONO_TYPE_ARRAY
:
1840 printf ("complex] (%s [%d %d %d])",
1841 value
->type
->data
.array
->eklass
->name
,
1842 value
->type
->data
.array
->rank
,
1843 value
->type
->data
.array
->numsizes
,
1844 value
->type
->data
.array
->numlobounds
);
1846 case MONO_TYPE_GENERICINST
:
1847 printf ("complex] (inst of %s )", value
->type
->data
.generic_class
->container_class
->name
);
1850 printf ("complex] (type generic param !%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
1852 case MONO_TYPE_MVAR
:
1853 printf ("complex] (method generic param !!%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
1856 //should be a boxed value
1857 char * name
= mono_type_full_name (value
->type
);
1858 printf ("complex] %s", name
);
1864 printf ("unknown stack %x type]\n", value
->stype
);
1865 g_assert_not_reached ();
1870 dump_stack_state (ILCodeDesc
*state
)
1874 printf ("(%d) ", state
->size
);
1875 for (i
= 0; i
< state
->size
; ++i
)
1876 dump_stack_value (state
->stack
+ i
);
1882 * is_array_type_compatible:
1884 * Returns TRUE if candidate array type can be assigned to target.
1886 * Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1889 is_array_type_compatible (MonoType
*target
, MonoType
*candidate
)
1891 MonoArrayType
*left
= target
->data
.array
;
1892 MonoArrayType
*right
= candidate
->data
.array
;
1894 g_assert (target
->type
== MONO_TYPE_ARRAY
);
1895 g_assert (candidate
->type
== MONO_TYPE_ARRAY
);
1897 if (left
->rank
!= right
->rank
)
1900 return verifier_class_is_assignable_from (left
->eklass
, right
->eklass
);
1904 get_stack_type (MonoType
*type
)
1907 int type_kind
= type
->type
;
1909 mask
= POINTER_MASK
;
1910 /*TODO handle CMMP_MASK */
1913 switch (type_kind
) {
1916 case MONO_TYPE_BOOLEAN
:
1919 case MONO_TYPE_CHAR
:
1922 return TYPE_I4
| mask
;
1926 return TYPE_NATIVE_INT
| mask
;
1928 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1929 case MONO_TYPE_FNPTR
:
1931 case MONO_TYPE_TYPEDBYREF
:
1932 return TYPE_PTR
| mask
;
1935 case MONO_TYPE_MVAR
:
1937 case MONO_TYPE_CLASS
:
1938 case MONO_TYPE_STRING
:
1939 case MONO_TYPE_OBJECT
:
1940 case MONO_TYPE_SZARRAY
:
1941 case MONO_TYPE_ARRAY
:
1942 return TYPE_COMPLEX
| mask
;
1946 return TYPE_I8
| mask
;
1950 return TYPE_R8
| mask
;
1952 case MONO_TYPE_GENERICINST
:
1953 case MONO_TYPE_VALUETYPE
:
1954 if (mono_type_is_enum_type (type
)) {
1955 type
= mono_type_get_underlying_type_any (type
);
1958 type_kind
= type
->type
;
1961 return TYPE_COMPLEX
| mask
;
1969 /* convert MonoType to ILStackDesc format (stype) */
1971 set_stack_value (VerifyContext
*ctx
, ILStackDesc
*stack
, MonoType
*type
, int take_addr
)
1974 int type_kind
= type
->type
;
1976 if (type
->byref
|| take_addr
)
1977 mask
= POINTER_MASK
;
1978 /* TODO handle CMMP_MASK */
1983 switch (type_kind
) {
1986 case MONO_TYPE_BOOLEAN
:
1989 case MONO_TYPE_CHAR
:
1992 stack
->stype
= TYPE_I4
| mask
;
1996 stack
->stype
= TYPE_NATIVE_INT
| mask
;
1999 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2000 case MONO_TYPE_FNPTR
:
2002 case MONO_TYPE_TYPEDBYREF
:
2003 stack
->stype
= TYPE_PTR
| mask
;
2006 case MONO_TYPE_CLASS
:
2007 case MONO_TYPE_STRING
:
2008 case MONO_TYPE_OBJECT
:
2009 case MONO_TYPE_SZARRAY
:
2010 case MONO_TYPE_ARRAY
:
2013 case MONO_TYPE_MVAR
:
2014 stack
->stype
= TYPE_COMPLEX
| mask
;
2019 stack
->stype
= TYPE_I8
| mask
;
2023 stack
->stype
= TYPE_R8
| mask
;
2025 case MONO_TYPE_GENERICINST
:
2026 case MONO_TYPE_VALUETYPE
:
2027 if (mono_type_is_enum_type (type
)) {
2028 MonoType
*utype
= mono_type_get_underlying_type_any (type
);
2030 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not resolve underlying type of %x at %d", type
->type
, ctx
->ip_offset
));
2034 type_kind
= type
->type
;
2037 stack
->stype
= TYPE_COMPLEX
| mask
;
2041 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type
->type
); );
2042 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type
->type
, ctx
->ip_offset
));
2049 * init_stack_with_value_at_exception_boundary:
2051 * Initialize the stack and push a given type.
2052 * The instruction is marked as been on the exception boundary.
2055 init_stack_with_value_at_exception_boundary (VerifyContext
*ctx
, ILCodeDesc
*code
, MonoClass
*klass
)
2058 MonoType
*type
= mono_class_inflate_generic_type_checked (&klass
->byval_arg
, ctx
->generic_context
, &error
);
2060 if (!mono_error_ok (&error
)) {
2061 char *name
= mono_type_get_full_name (klass
);
2062 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid class %s used for exception", name
));
2064 mono_error_cleanup (&error
);
2068 if (!ctx
->max_stack
) {
2069 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack overflow at 0x%04x", ctx
->ip_offset
));
2073 stack_init (ctx
, code
);
2074 ensure_stack_size (code
, 1);
2075 set_stack_value (ctx
, code
->stack
, type
, FALSE
);
2076 ctx
->exception_types
= g_slist_prepend (ctx
->exception_types
, type
);
2078 code
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
2079 if (mono_type_is_generic_argument (type
))
2080 code
->stack
->stype
|= BOXED_MASK
;
2082 /* Class lazy loading functions */
2083 static GENERATE_GET_CLASS_WITH_CACHE (ienumerable
, System
.Collections
.Generic
, IEnumerable`
1)
2084 static GENERATE_GET_CLASS_WITH_CACHE (icollection
, System
.Collections
.Generic
, ICollection`
1)
2085 static GENERATE_GET_CLASS_WITH_CACHE (ireadonly_list
, System
.Collections
.Generic
, IReadOnlyList`
1)
2086 static GENERATE_GET_CLASS_WITH_CACHE (ireadonly_collection
, System
.Collections
.Generic
, IReadOnlyCollection`
1)
2090 get_ienumerable_class (void)
2092 return mono_class_get_ienumerable_class ();
2096 get_icollection_class (void)
2098 return mono_class_get_icollection_class ();
2102 get_ireadonlylist_class (void)
2104 return mono_class_get_ireadonly_list_class ();
2108 get_ireadonlycollection_class (void)
2110 return mono_class_get_ireadonly_collection_class ();
2114 inflate_class_one_arg (MonoClass
*gtype
, MonoClass
*arg0
)
2117 args
[0] = &arg0
->byval_arg
;
2119 return mono_class_bind_generic_parameters (gtype
, 1, args
, FALSE
);
2123 verifier_inflate_and_check_compat (MonoClass
*target
, MonoClass
*gtd
, MonoClass
*arg
)
2126 if (!(tmp
= inflate_class_one_arg (gtd
, arg
)))
2128 if (mono_class_is_variant_compatible (target
, tmp
, TRUE
))
2134 verifier_class_is_assignable_from (MonoClass
*target
, MonoClass
*candidate
)
2136 MonoClass
*iface_gtd
;
2138 if (target
== candidate
)
2141 if (mono_class_has_variant_generic_params (target
)) {
2142 if (MONO_CLASS_IS_INTERFACE (target
)) {
2143 if (MONO_CLASS_IS_INTERFACE (candidate
) && mono_class_is_variant_compatible (target
, candidate
, TRUE
))
2146 if (candidate
->rank
== 1) {
2147 if (verifier_inflate_and_check_compat (target
, mono_defaults
.generic_ilist_class
, candidate
->element_class
))
2149 if (verifier_inflate_and_check_compat (target
, get_icollection_class (), candidate
->element_class
))
2151 if (verifier_inflate_and_check_compat (target
, get_ienumerable_class (), candidate
->element_class
))
2153 if (verifier_inflate_and_check_compat (target
, get_ireadonlylist_class (), candidate
->element_class
))
2155 if (verifier_inflate_and_check_compat (target
, get_ireadonlycollection_class (), candidate
->element_class
))
2160 while (candidate
&& candidate
!= mono_defaults
.object_class
) {
2161 mono_class_setup_interfaces (candidate
, &error
);
2162 if (!mono_error_ok (&error
)) {
2163 mono_error_cleanup (&error
);
2167 /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
2168 for (i
= 0; i
< candidate
->interface_offsets_count
; ++i
) {
2169 MonoClass
*iface
= candidate
->interfaces_packed
[i
];
2170 if (mono_class_is_variant_compatible (target
, iface
, TRUE
))
2174 for (i
= 0; i
< candidate
->interface_count
; ++i
) {
2175 MonoClass
*iface
= candidate
->interfaces
[i
];
2176 if (mono_class_is_variant_compatible (target
, iface
, TRUE
))
2179 candidate
= candidate
->parent
;
2182 } else if (target
->delegate
) {
2183 if (mono_class_is_variant_compatible (target
, candidate
, TRUE
))
2189 if (mono_class_is_assignable_from (target
, candidate
))
2192 if (!MONO_CLASS_IS_INTERFACE (target
) || !mono_class_is_ginst (target
) || candidate
->rank
!= 1)
2195 iface_gtd
= mono_class_get_generic_class (target
)->container_class
;
2196 if (iface_gtd
!= mono_defaults
.generic_ilist_class
&& iface_gtd
!= get_icollection_class () && iface_gtd
!= get_ienumerable_class ())
2199 target
= mono_class_from_mono_type (mono_class_get_generic_class (target
)->context
.class_inst
->type_argv
[0]);
2200 candidate
= candidate
->element_class
;
2205 /*Verify if type 'candidate' can be stored in type 'target'.
2207 * If strict, check for the underlying type and not the verification stack types
2210 verify_type_compatibility_full (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
, gboolean strict
)
2212 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2213 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2215 MonoType
*original_candidate
= candidate
;
2216 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target
), mono_type_full_name (candidate
), strict
); );
2218 /*only one is byref */
2219 if (candidate
->byref
^ target
->byref
) {
2220 /* converting from native int to byref*/
2221 if (get_stack_type (candidate
) == TYPE_NATIVE_INT
&& target
->byref
) {
2222 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("using byref native int at 0x%04x", ctx
->ip_offset
));
2227 strict
|= target
->byref
;
2228 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2229 candidate
= mono_type_get_underlying_type_any (candidate
);
2232 switch (target
->type
) {
2233 case MONO_TYPE_VOID
:
2234 return candidate
->type
== MONO_TYPE_VOID
;
2237 case MONO_TYPE_BOOLEAN
:
2239 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I1
, MONO_TYPE_U1
, MONO_TYPE_BOOLEAN
);
2242 case MONO_TYPE_CHAR
:
2244 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I2
, MONO_TYPE_U2
, MONO_TYPE_CHAR
);
2246 case MONO_TYPE_U4
: {
2247 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2248 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2250 return is_native_int
|| is_int4
;
2251 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2256 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I8
, MONO_TYPE_U8
);
2261 return candidate
->type
== target
->type
;
2262 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_R4
, MONO_TYPE_R8
);
2266 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2267 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2269 return is_native_int
|| is_int4
;
2270 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2274 if (candidate
->type
!= MONO_TYPE_PTR
)
2276 /* check the underlying type */
2277 return verify_type_compatibility_full (ctx
, target
->data
.type
, candidate
->data
.type
, TRUE
);
2279 case MONO_TYPE_FNPTR
: {
2280 MonoMethodSignature
*left
, *right
;
2281 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2284 left
= mono_type_get_signature (target
);
2285 right
= mono_type_get_signature (candidate
);
2286 return mono_metadata_signature_equal (left
, right
) && left
->call_convention
== right
->call_convention
;
2289 case MONO_TYPE_GENERICINST
: {
2290 MonoClass
*target_klass
;
2291 MonoClass
*candidate_klass
;
2292 if (mono_type_is_enum_type (target
)) {
2293 target
= mono_type_get_underlying_type_any (target
);
2299 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2300 * to take boxing status into account.
2302 if (mono_type_is_generic_argument (original_candidate
))
2305 target_klass
= mono_class_from_mono_type (target
);
2306 candidate_klass
= mono_class_from_mono_type (candidate
);
2307 if (mono_class_is_nullable (target_klass
)) {
2308 if (!mono_class_is_nullable (candidate_klass
))
2310 return target_klass
== candidate_klass
;
2312 return verifier_class_is_assignable_from (target_klass
, candidate_klass
);
2315 case MONO_TYPE_STRING
:
2316 return candidate
->type
== MONO_TYPE_STRING
;
2318 case MONO_TYPE_CLASS
:
2320 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2321 * to take boxing status into account.
2323 if (mono_type_is_generic_argument (original_candidate
))
2326 if (candidate
->type
== MONO_TYPE_VALUETYPE
)
2329 /* If candidate is an enum it should return true for System.Enum and supertypes.
2330 * That's why here we use the original type and not the underlying type.
2332 return verifier_class_is_assignable_from (target
->data
.klass
, mono_class_from_mono_type (original_candidate
));
2334 case MONO_TYPE_OBJECT
:
2335 return MONO_TYPE_IS_REFERENCE (candidate
);
2337 case MONO_TYPE_SZARRAY
: {
2340 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2343 left
= mono_class_from_mono_type (target
);
2344 right
= mono_class_from_mono_type (candidate
);
2346 return verifier_class_is_assignable_from (left
, right
);
2349 case MONO_TYPE_ARRAY
:
2350 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2352 return is_array_type_compatible (target
, candidate
);
2354 case MONO_TYPE_TYPEDBYREF
:
2355 return candidate
->type
== MONO_TYPE_TYPEDBYREF
;
2357 case MONO_TYPE_VALUETYPE
: {
2358 MonoClass
*target_klass
;
2359 MonoClass
*candidate_klass
;
2361 if (candidate
->type
== MONO_TYPE_CLASS
)
2364 target_klass
= mono_class_from_mono_type (target
);
2365 candidate_klass
= mono_class_from_mono_type (candidate
);
2366 if (target_klass
== candidate_klass
)
2368 if (mono_type_is_enum_type (target
)) {
2369 target
= mono_type_get_underlying_type_any (target
);
2378 if (candidate
->type
!= MONO_TYPE_VAR
)
2380 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2382 case MONO_TYPE_MVAR
:
2383 if (candidate
->type
!= MONO_TYPE_MVAR
)
2385 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2388 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target
->type
); );
2389 g_assert_not_reached ();
2398 verify_type_compatibility (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
)
2400 return verify_type_compatibility_full (ctx
, target
, candidate
, FALSE
);
2404 * Returns the generic param bound to the context been verified.
2407 static MonoGenericParam
*
2408 get_generic_param (VerifyContext
*ctx
, MonoType
*param
)
2410 guint16 param_num
= mono_type_get_generic_param_num (param
);
2411 if (param
->type
== MONO_TYPE_VAR
) {
2412 if (!ctx
->generic_context
->class_inst
|| ctx
->generic_context
->class_inst
->type_argc
<= param_num
) {
2413 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic type argument %d", param_num
));
2416 return ctx
->generic_context
->class_inst
->type_argv
[param_num
]->data
.generic_param
;
2419 /*param must be a MVAR */
2420 if (!ctx
->generic_context
->method_inst
|| ctx
->generic_context
->method_inst
->type_argc
<= param_num
) {
2421 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic method argument %d", param_num
));
2424 return ctx
->generic_context
->method_inst
->type_argv
[param_num
]->data
.generic_param
;
2429 recursive_boxed_constraint_type_check (VerifyContext
*ctx
, MonoType
*type
, MonoClass
*constraint_class
, int recursion_level
)
2431 MonoType
*constraint_type
= &constraint_class
->byval_arg
;
2432 if (recursion_level
<= 0)
2435 if (verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (constraint_type
), FALSE
))
2438 if (mono_type_is_generic_argument (constraint_type
)) {
2439 MonoGenericParam
*param
= get_generic_param (ctx
, constraint_type
);
2443 for (klass
= mono_generic_param_info (param
)->constraints
; klass
&& *klass
; ++klass
) {
2444 if (recursive_boxed_constraint_type_check (ctx
, type
, *klass
, recursion_level
- 1))
2452 * is_compatible_boxed_valuetype:
2454 * Returns: TRUE if @candidate / @stack is a valid boxed valuetype.
2456 * @type The source type. It it tested to be of the proper type.
2457 * @candidate type of the boxed valuetype.
2458 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2459 * @strict if TRUE candidate must be boxed compatible to the target type
2463 is_compatible_boxed_valuetype (VerifyContext
*ctx
, MonoType
*type
, MonoType
*candidate
, ILStackDesc
*stack
, gboolean strict
)
2465 if (!stack_slot_is_boxed_value (stack
))
2467 if (type
->byref
|| candidate
->byref
)
2470 if (mono_type_is_generic_argument (candidate
)) {
2471 MonoGenericParam
*param
= get_generic_param (ctx
, candidate
);
2476 for (klass
= mono_generic_param_info (param
)->constraints
; klass
&& *klass
; ++klass
) {
2477 /*256 should be enough since there can't be more than 255 generic arguments.*/
2478 if (recursive_boxed_constraint_type_check (ctx
, type
, *klass
, 256))
2483 if (mono_type_is_generic_argument (type
))
2489 return MONO_TYPE_IS_REFERENCE (type
) && verifier_class_is_assignable_from (mono_class_from_mono_type (type
), mono_class_from_mono_type (candidate
));
2493 verify_stack_type_compatibility_full (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
, gboolean drop_byref
, gboolean valuetype_must_be_boxed
)
2495 MonoType
*candidate
= mono_type_from_stack_slot (stack
);
2496 if (MONO_TYPE_IS_REFERENCE (type
) && !type
->byref
&& stack_slot_is_null_literal (stack
))
2499 if (is_compatible_boxed_valuetype (ctx
, type
, candidate
, stack
, TRUE
))
2502 if (valuetype_must_be_boxed
&& !stack_slot_is_boxed_value (stack
) && !MONO_TYPE_IS_REFERENCE (candidate
))
2505 if (!valuetype_must_be_boxed
&& stack_slot_is_boxed_value (stack
))
2509 return verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (candidate
), FALSE
);
2511 /* Handle how Roslyn emit fixed statements by encoding it as byref */
2512 if (type
->byref
&& candidate
->byref
&& (type
->type
== MONO_TYPE_I
) && !mono_type_is_reference (candidate
)) {
2513 if (!IS_STRICT_MODE (ctx
))
2517 return verify_type_compatibility_full (ctx
, type
, candidate
, FALSE
);
2521 verify_stack_type_compatibility (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
)
2523 return verify_stack_type_compatibility_full (ctx
, type
, stack
, FALSE
, FALSE
);
2527 mono_delegate_type_equal (MonoType
*target
, MonoType
*candidate
)
2529 if (candidate
->byref
^ target
->byref
)
2532 switch (target
->type
) {
2533 case MONO_TYPE_VOID
:
2536 case MONO_TYPE_BOOLEAN
:
2539 case MONO_TYPE_CHAR
:
2548 case MONO_TYPE_STRING
:
2549 case MONO_TYPE_TYPEDBYREF
:
2550 return candidate
->type
== target
->type
;
2553 if (candidate
->type
!= MONO_TYPE_PTR
)
2555 return mono_delegate_type_equal (target
->data
.type
, candidate
->data
.type
);
2557 case MONO_TYPE_FNPTR
:
2558 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2560 return mono_delegate_signature_equal (mono_type_get_signature (target
), mono_type_get_signature (candidate
), FALSE
);
2562 case MONO_TYPE_GENERICINST
: {
2563 MonoClass
*target_klass
;
2564 MonoClass
*candidate_klass
;
2565 target_klass
= mono_class_from_mono_type (target
);
2566 candidate_klass
= mono_class_from_mono_type (candidate
);
2567 /*FIXME handle nullables and enum*/
2568 return verifier_class_is_assignable_from (target_klass
, candidate_klass
);
2570 case MONO_TYPE_OBJECT
:
2571 return MONO_TYPE_IS_REFERENCE (candidate
);
2573 case MONO_TYPE_CLASS
:
2574 return verifier_class_is_assignable_from(target
->data
.klass
, mono_class_from_mono_type (candidate
));
2576 case MONO_TYPE_SZARRAY
:
2577 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2579 return verifier_class_is_assignable_from (mono_class_from_mono_type (target
)->element_class
, mono_class_from_mono_type (candidate
)->element_class
);
2581 case MONO_TYPE_ARRAY
:
2582 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2584 return is_array_type_compatible (target
, candidate
);
2586 case MONO_TYPE_VALUETYPE
:
2587 /*FIXME handle nullables and enum*/
2588 return mono_class_from_mono_type (candidate
) == mono_class_from_mono_type (target
);
2591 return candidate
->type
== MONO_TYPE_VAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2594 case MONO_TYPE_MVAR
:
2595 return candidate
->type
== MONO_TYPE_MVAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2599 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target
->type
); );
2600 g_assert_not_reached ();
2606 mono_delegate_param_equal (MonoType
*delegate
, MonoType
*method
)
2608 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2611 return mono_delegate_type_equal (method
, delegate
);
2615 mono_delegate_ret_equal (MonoType
*delegate
, MonoType
*method
)
2617 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2620 return mono_delegate_type_equal (delegate
, method
);
2624 * mono_delegate_signature_equal:
2626 * Compare two signatures in the way expected by delegates.
2628 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2630 * FIXME can this function be eliminated and proper metadata functionality be used?
2633 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
)
2636 int method_offset
= is_static_ldftn
? 1 : 0;
2638 if (delegate_sig
->param_count
+ method_offset
!= method_sig
->param_count
)
2641 if (delegate_sig
->call_convention
!= method_sig
->call_convention
)
2644 for (i
= 0; i
< delegate_sig
->param_count
; i
++) {
2645 MonoType
*p1
= delegate_sig
->params
[i
];
2646 MonoType
*p2
= method_sig
->params
[i
+ method_offset
];
2648 if (!mono_delegate_param_equal (p1
, p2
))
2652 if (!mono_delegate_ret_equal (delegate_sig
->ret
, method_sig
->ret
))
2659 mono_verifier_is_signature_compatible (MonoMethodSignature
*target
, MonoMethodSignature
*candidate
)
2661 return mono_delegate_signature_equal (target
, candidate
, FALSE
);
2665 * verify_ldftn_delegate:
2667 * Verify properties of ldftn based delegates.
2670 verify_ldftn_delegate (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2672 MonoMethod
*method
= funptr
->method
;
2674 /*ldftn non-final virtuals only allowed if method is not static,
2675 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2676 * This rules doesn't apply if the object on stack is a boxed valuetype.
2678 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !mono_class_is_sealed (method
->klass
) && !stack_slot_is_boxed_value (value
)) {
2679 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2680 if (IS_FAIL_FAST_MODE (ctx
) && ctx
->has_this_store
)
2681 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx
->ip_offset
));
2683 /*current method must not be static*/
2684 if (ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)
2685 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx
->ip_offset
));
2687 /*value is the this pointer, loaded using ldarg.0 */
2688 if (!stack_slot_is_this_pointer (value
))
2689 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
));
2691 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
;
2696 * verify_delegate_compatibility:
2698 * Verify delegate creation sequence.
2702 verify_delegate_compatibility (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2704 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2705 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2707 MonoMethod
*invoke
, *method
;
2708 const guint8
*ip
= ctx
->header
->code
;
2709 guint32 ip_offset
= ctx
->ip_offset
;
2710 gboolean is_static_ldftn
= FALSE
, is_first_arg_bound
= FALSE
;
2712 if (stack_slot_get_type (funptr
) != TYPE_PTR
|| !funptr
->method
) {
2713 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx
->ip_offset
));
2717 invoke
= mono_get_delegate_invoke (delegate
);
2718 method
= funptr
->method
;
2720 if (!method
|| !mono_method_signature (method
)) {
2721 char *name
= mono_type_get_full_name (delegate
);
2722 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name
, ctx
->ip_offset
));
2727 if (!invoke
|| !mono_method_signature (invoke
)) {
2728 char *name
= mono_type_get_full_name (delegate
);
2729 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name
, ctx
->ip_offset
));
2734 is_static_ldftn
= (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) && method
->flags
& METHOD_ATTRIBUTE_STATIC
;
2736 if (is_static_ldftn
)
2737 is_first_arg_bound
= mono_method_signature (invoke
)->param_count
+ 1 == mono_method_signature (method
)->param_count
;
2739 if (!mono_delegate_signature_equal (mono_method_signature (invoke
), mono_method_signature (method
), is_first_arg_bound
)) {
2740 char *fun_sig
= mono_signature_get_desc (mono_method_signature (method
), FALSE
);
2741 char *invoke_sig
= mono_signature_get_desc (mono_method_signature (invoke
), FALSE
);
2742 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
));
2744 g_free (invoke_sig
);
2748 * Delegate code sequences:
2754 * [-6] ldvirtftn token
2758 if (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) {
2759 verify_ldftn_delegate (ctx
, delegate
, value
, funptr
);
2760 } else if (ip_offset
> 6 && IS_VALID_OPCODE (7, CEE_DUP
) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN
)) {
2761 ctx
->code
[ip_offset
- 6].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2763 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx
->ip_offset
));
2765 ctx
->code
[ip_offset
].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2768 if (is_first_arg_bound
) {
2769 if (mono_method_signature (method
)->param_count
== 0 || !verify_stack_type_compatibility_full (ctx
, mono_method_signature (method
)->params
[0], value
, FALSE
, TRUE
))
2770 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2772 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
2773 if (!stack_slot_is_null_literal (value
))
2774 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx
->ip_offset
));
2776 if (!verify_stack_type_compatibility_full (ctx
, &method
->klass
->byval_arg
, value
, FALSE
, TRUE
) && !stack_slot_is_null_literal (value
))
2777 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2781 if (stack_slot_get_type (value
) != TYPE_COMPLEX
)
2782 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx
->ip_offset
));
2784 #undef IS_VALID_OPCODE
2785 #undef IS_LOAD_FUN_PTR
2788 /* implement the opcode checks*/
2790 push_arg (VerifyContext
*ctx
, unsigned int arg
, int take_addr
)
2794 if (arg
>= ctx
->max_args
) {
2796 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
2798 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
2799 if (check_overflow (ctx
)) //FIXME: what sane value could we ever push?
2800 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
2802 } else if (check_overflow (ctx
)) {
2803 /*We must let the value be pushed, otherwise we would get an underflow error*/
2804 check_unverifiable_type (ctx
, ctx
->params
[arg
]);
2805 if (ctx
->params
[arg
]->byref
&& take_addr
)
2806 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
2807 top
= stack_push (ctx
);
2808 if (!set_stack_value (ctx
, top
, ctx
->params
[arg
], take_addr
))
2811 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
2813 ctx
->has_this_store
= TRUE
;
2815 top
->stype
|= THIS_POINTER_MASK
;
2816 if (mono_method_is_constructor (ctx
->method
) && !ctx
->super_ctor_called
&& !ctx
->method
->klass
->valuetype
)
2817 top
->stype
|= UNINIT_THIS_MASK
;
2823 push_local (VerifyContext
*ctx
, guint32 arg
, int take_addr
)
2825 if (arg
>= ctx
->num_locals
) {
2826 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local %d", arg
+ 1));
2827 } else if (check_overflow (ctx
)) {
2828 /*We must let the value be pushed, otherwise we would get an underflow error*/
2829 check_unverifiable_type (ctx
, ctx
->locals
[arg
]);
2830 if (ctx
->locals
[arg
]->byref
&& take_addr
)
2831 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
2833 set_stack_value (ctx
, stack_push (ctx
), ctx
->locals
[arg
], take_addr
);
2838 store_arg (VerifyContext
*ctx
, guint32 arg
)
2842 if (arg
>= ctx
->max_args
) {
2843 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
2844 if (check_underflow (ctx
, 1))
2849 if (check_underflow (ctx
, 1)) {
2850 value
= stack_pop (ctx
);
2851 if (!verify_stack_type_compatibility (ctx
, ctx
->params
[arg
], value
)) {
2852 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
2855 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
))
2856 ctx
->has_this_store
= 1;
2860 store_local (VerifyContext
*ctx
, guint32 arg
)
2863 if (arg
>= ctx
->num_locals
) {
2864 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
2868 /*TODO verify definite assigment */
2869 if (!check_underflow (ctx
, 1))
2872 value
= stack_pop (ctx
);
2873 if (ctx
->locals
[arg
]->byref
&& stack_slot_is_managed_mutability_pointer (value
))
2874 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly managed reference when storing on a local variable at 0x%04x", ctx
->ip_offset
));
2876 if (!verify_stack_type_compatibility (ctx
, ctx
->locals
[arg
], value
)) {
2877 char *expected
= mono_type_full_name (ctx
->locals
[arg
]);
2878 char *found
= stack_slot_full_name (value
);
2879 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2889 /*FIXME add and sub needs special care here*/
2891 do_binop (VerifyContext
*ctx
, unsigned int opcode
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
2893 ILStackDesc
*a
, *b
, *top
;
2894 int idxa
, idxb
, complexMerge
= 0;
2897 if (!check_underflow (ctx
, 2))
2899 b
= stack_pop (ctx
);
2900 a
= stack_pop (ctx
);
2902 idxa
= stack_slot_get_underlying_type (a
);
2903 if (stack_slot_is_managed_pointer (a
)) {
2908 idxb
= stack_slot_get_underlying_type (b
);
2909 if (stack_slot_is_managed_pointer (b
)) {
2916 res
= table
[idxa
][idxb
];
2918 VERIFIER_DEBUG ( printf ("binop res %d\n", res
); );
2919 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
2921 top
= stack_push (ctx
);
2922 if (res
== TYPE_INV
) {
2923 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
)));
2924 copy_stack_value (top
, a
);
2928 if (res
& NON_VERIFIABLE_RESULT
) {
2929 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
)));
2931 res
= res
& ~NON_VERIFIABLE_RESULT
;
2934 if (complexMerge
&& res
== TYPE_PTR
) {
2935 if (complexMerge
== 1)
2936 copy_stack_value (top
, a
);
2937 else if (complexMerge
== 2)
2938 copy_stack_value (top
, b
);
2940 * There is no need to merge the type of two pointers.
2941 * The only valid operation is subtraction, that returns a native
2942 * int as result and can be used with any 2 pointer kinds.
2943 * This is valid acording to Patition III 1.1.4
2952 do_boolean_branch_op (VerifyContext
*ctx
, int delta
)
2954 int target
= ctx
->ip_offset
+ delta
;
2957 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
2959 if (target
< 0 || target
>= ctx
->code_size
) {
2960 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx
->ip_offset
));
2964 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
2966 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2969 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2973 ctx
->target
= target
;
2975 if (!check_underflow (ctx
, 1))
2978 top
= stack_pop (ctx
);
2979 if (!is_valid_bool_arg (top
))
2980 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
));
2982 check_unmanaged_pointer (ctx
, top
);
2986 stack_slot_is_complex_type_not_reference_type (ILStackDesc
*slot
)
2988 return stack_slot_get_type (slot
) == TYPE_COMPLEX
&& !MONO_TYPE_IS_REFERENCE (slot
->type
) && !stack_slot_is_boxed_value (slot
);
2992 stack_slot_is_reference_value (ILStackDesc
*slot
)
2994 return stack_slot_get_type (slot
) == TYPE_COMPLEX
&& (MONO_TYPE_IS_REFERENCE (slot
->type
) || stack_slot_is_boxed_value (slot
));
2998 do_branch_op (VerifyContext
*ctx
, signed int delta
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
3003 int target
= ctx
->ip_offset
+ delta
;
3005 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
3007 if (target
< 0 || target
>= ctx
->code_size
) {
3008 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
3012 switch (is_valid_cmp_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3013 case 1: /*FIXME use constants and not magic numbers.*/
3014 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3017 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3021 ctx
->target
= target
;
3023 if (!check_underflow (ctx
, 2))
3026 b
= stack_pop (ctx
);
3027 a
= stack_pop (ctx
);
3029 idxa
= stack_slot_get_underlying_type (a
);
3030 if (stack_slot_is_managed_pointer (a
))
3033 idxb
= stack_slot_get_underlying_type (b
);
3034 if (stack_slot_is_managed_pointer (b
))
3037 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3042 res
= table
[idxa
][idxb
];
3045 VERIFIER_DEBUG ( printf ("branch res %d\n", res
); );
3046 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
3048 if (res
== TYPE_INV
) {
3049 CODE_NOT_VERIFIABLE (ctx
,
3050 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
));
3051 } else if (res
& NON_VERIFIABLE_RESULT
) {
3052 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
));
3053 res
= res
& ~NON_VERIFIABLE_RESULT
;
3058 do_cmp_op (VerifyContext
*ctx
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
], guint32 opcode
)
3064 if (!check_underflow (ctx
, 2))
3066 b
= stack_pop (ctx
);
3067 a
= stack_pop (ctx
);
3069 if (opcode
== CEE_CGT_UN
) {
3070 if ((stack_slot_is_reference_value (a
) && stack_slot_is_null_literal (b
)) ||
3071 (stack_slot_is_reference_value (b
) && stack_slot_is_null_literal (a
))) {
3072 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3077 idxa
= stack_slot_get_underlying_type (a
);
3078 if (stack_slot_is_managed_pointer (a
))
3081 idxb
= stack_slot_get_underlying_type (b
);
3082 if (stack_slot_is_managed_pointer (b
))
3085 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
3090 res
= table
[idxa
][idxb
];
3093 if(res
== TYPE_INV
) {
3094 char *left_type
= stack_slot_full_name (a
);
3095 char *right_type
= stack_slot_full_name (b
);
3096 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", left_type
, right_type
, ctx
->ip_offset
));
3098 g_free (right_type
);
3099 } else if (res
& NON_VERIFIABLE_RESULT
) {
3100 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
));
3101 res
= res
& ~NON_VERIFIABLE_RESULT
;
3103 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3107 do_ret (VerifyContext
*ctx
)
3109 MonoType
*ret
= ctx
->signature
->ret
;
3110 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3111 if (ret
->type
!= MONO_TYPE_VOID
) {
3113 if (!check_underflow (ctx
, 1))
3116 top
= stack_pop(ctx
);
3118 if (!verify_stack_type_compatibility (ctx
, ctx
->signature
->ret
, top
)) {
3119 char *ret_type
= mono_type_full_name (ctx
->signature
->ret
);
3120 char *stack_type
= stack_slot_full_name (top
);
3121 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible return value on stack with method signature, expected '%s' but got '%s' at 0x%04x", ret_type
, stack_type
, ctx
->ip_offset
));
3122 g_free (stack_type
);
3127 if (ret
->byref
|| ret
->type
== MONO_TYPE_TYPEDBYREF
|| mono_type_is_value_type (ret
, "System", "ArgIterator") || mono_type_is_value_type (ret
, "System", "RuntimeArgumentHandle"))
3128 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx
->ip_offset
));
3131 if (ctx
->eval
.size
> 0) {
3132 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx
->eval
.size
, ctx
->ip_offset
));
3134 if (in_any_block (ctx
->header
, ctx
->ip_offset
))
3135 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx
->ip_offset
));
3139 * 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.
3140 * This is illegal but mono_get_method_full decoded it.
3141 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3144 do_invoke_method (VerifyContext
*ctx
, int method_token
, gboolean virtual_
)
3148 MonoMethodSignature
*sig
;
3151 gboolean virt_check_this
= FALSE
;
3152 gboolean constrained
= ctx
->prefix_set
& PREFIX_CONSTRAINED
;
3154 if (!(method
= verifier_load_method (ctx
, method_token
, virtual_
? "callvirt" : "call")))
3158 CLEAR_PREFIX (ctx
, PREFIX_CONSTRAINED
);
3160 if (method
->klass
->valuetype
) // && !constrained ???
3161 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx
->ip_offset
));
3163 if ((method
->flags
& METHOD_ATTRIBUTE_STATIC
))
3164 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx
->ip_offset
));
3167 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
3168 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx
->ip_offset
));
3170 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !mono_class_is_sealed (method
->klass
)) {
3171 virt_check_this
= TRUE
;
3172 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_CALL_NONFINAL_VIRTUAL
;
3176 if (!(sig
= mono_method_get_signature_checked (method
, ctx
->image
, method_token
, ctx
->generic_context
, &error
))) {
3177 mono_error_cleanup (&error
);
3178 sig
= mono_method_get_signature_checked (method
, ctx
->image
, method_token
, NULL
, &error
);
3182 char *name
= mono_type_get_full_name (method
->klass
);
3183 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x due to: %s", name
, method
->name
, ctx
->ip_offset
, mono_error_get_message (&error
)));
3184 mono_error_cleanup (&error
);
3189 param_count
= sig
->param_count
+ sig
->hasthis
;
3190 if (!check_underflow (ctx
, param_count
))
3193 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3194 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i
); );
3195 value
= stack_pop (ctx
);
3196 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3197 char *stack_name
= stack_slot_full_name (value
);
3198 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3199 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
));
3200 g_free (stack_name
);
3204 if (stack_slot_is_managed_mutability_pointer (value
))
3205 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
));
3207 if ((ctx
->prefix_set
& PREFIX_TAIL
) && stack_slot_is_managed_pointer (value
)) {
3208 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
));
3214 MonoType
*type
= &method
->klass
->byval_arg
;
3217 if (mono_method_is_constructor (method
) && !method
->klass
->valuetype
) {
3218 if (IS_STRICT_MODE (ctx
) && !mono_method_is_constructor (ctx
->method
))
3219 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx
->ip_offset
));
3220 if (IS_STRICT_MODE (ctx
) && method
->klass
!= ctx
->method
->klass
->parent
&& method
->klass
!= ctx
->method
->klass
)
3221 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor of a type different from this or super at 0x%04x", ctx
->ip_offset
));
3223 ctx
->super_ctor_called
= TRUE
;
3224 value
= stack_pop_safe (ctx
);
3225 if (IS_STRICT_MODE (ctx
) && (value
->stype
& THIS_POINTER_MASK
) != THIS_POINTER_MASK
)
3226 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx
->ip_offset
));
3228 value
= stack_pop (ctx
);
3231 copy_stack_value (©
, value
);
3232 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3233 //Other parts of the code suffer from the same issue of
3234 copy
.type
= mono_type_get_type_byval (copy
.type
);
3235 copy
.stype
&= ~POINTER_MASK
;
3237 if (virt_check_this
&& !stack_slot_is_this_pointer (value
) && !(method
->klass
->valuetype
|| stack_slot_is_boxed_value (value
)))
3238 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use the call opcode with a non-final virtual method on an object different than the 'this' pointer at 0x%04x", ctx
->ip_offset
));
3240 if (constrained
&& virtual_
) {
3241 if (!stack_slot_is_managed_pointer (value
))
3242 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx
->ip_offset
));
3243 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value
->type
), mono_type_get_underlying_type (ctx
->constrained_type
), TRUE
))
3244 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx
->ip_offset
));
3245 copy
.stype
|= BOXED_MASK
;
3246 copy
.type
= ctx
->constrained_type
;
3248 if (stack_slot_is_managed_pointer (value
) && !mono_class_from_mono_type (value
->type
)->valuetype
)
3249 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
));
3251 if (!virtual_
&& mono_class_from_mono_type (value
->type
)->valuetype
&& !method
->klass
->valuetype
&& !stack_slot_is_boxed_value (value
))
3252 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx
->ip_offset
));
3254 if (virtual_
&& mono_class_from_mono_type (value
->type
)->valuetype
&& !stack_slot_is_boxed_value (value
))
3255 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx
->ip_offset
));
3257 if (method
->klass
->valuetype
&& (stack_slot_is_boxed_value (value
) || !stack_slot_is_managed_pointer (value
)))
3258 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
));
3260 if (!verify_stack_type_compatibility (ctx
, type
, ©
)) {
3261 char *expected
= mono_type_full_name (type
);
3262 char *effective
= stack_slot_full_name (©
);
3263 char *method_name
= mono_method_full_name (method
, TRUE
);
3264 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible this argument on stack with method signature expected '%s' but got '%s' for a call to '%s' at 0x%04x",
3265 expected
, effective
, method_name
, ctx
->ip_offset
));
3266 g_free (method_name
);
3271 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, mono_class_from_mono_type (value
->type
))) {
3272 char *name
= mono_method_full_name (method
, TRUE
);
3273 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3277 } else if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3278 char *name
= mono_method_full_name (method
, TRUE
);
3279 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
3283 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3284 if (!mono_type_is_valid_in_context (ctx
, sig
->ret
))
3287 if (check_overflow (ctx
)) {
3288 value
= stack_push (ctx
);
3289 set_stack_value (ctx
, value
, sig
->ret
, FALSE
);
3290 if ((ctx
->prefix_set
& PREFIX_READONLY
) && method
->klass
->rank
&& !strcmp (method
->name
, "Address")) {
3291 ctx
->prefix_set
&= ~PREFIX_READONLY
;
3292 value
->stype
|= CMMP_MASK
;
3297 if ((ctx
->prefix_set
& PREFIX_TAIL
)) {
3298 if (!mono_delegate_ret_equal (mono_method_signature (ctx
->method
)->ret
, sig
->ret
))
3299 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx
->ip_offset
));
3300 if (ctx
->header
->code
[ctx
->ip_offset
+ 5] != CEE_RET
)
3301 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx
->ip_offset
));
3307 do_push_static_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3309 MonoClassField
*field
;
3311 if (!check_overflow (ctx
))
3314 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3316 if (!(field
= verifier_load_field (ctx
, token
, &klass
, take_addr
? "ldsflda" : "ldsfld")))
3319 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3320 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx
->ip_offset
));
3323 /*taking the address of initonly field only works from the static constructor */
3324 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3325 !(field
->parent
== ctx
->method
->klass
&& (ctx
->method
->flags
& (METHOD_ATTRIBUTE_SPECIAL_NAME
| METHOD_ATTRIBUTE_STATIC
)) && !strcmp (".cctor", ctx
->method
->name
)))
3326 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3328 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3329 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3331 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3335 do_store_static_field (VerifyContext
*ctx
, int token
) {
3336 MonoClassField
*field
;
3339 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3341 if (!check_underflow (ctx
, 1))
3344 value
= stack_pop (ctx
);
3346 if (!(field
= verifier_load_field (ctx
, token
, &klass
, "stsfld")))
3349 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3350 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx
->ip_offset
));
3354 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3355 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx
->ip_offset
));
3359 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3360 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3362 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
)) {
3363 char *stack_name
= stack_slot_full_name (value
);
3364 char *field_name
= mono_type_full_name (field
->type
);
3365 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3366 field_name
, stack_name
, ctx
->ip_offset
));
3367 g_free (field_name
);
3368 g_free (stack_name
);
3373 check_is_valid_type_for_field_ops (VerifyContext
*ctx
, int token
, ILStackDesc
*obj
, MonoClassField
**ret_field
, const char *opcode
)
3375 MonoClassField
*field
;
3377 gboolean is_pointer
;
3379 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3380 if (!(field
= verifier_load_field (ctx
, token
, &klass
, opcode
)))
3384 //the value on stack is going to be used as a pointer
3385 is_pointer
= stack_slot_get_type (obj
) == TYPE_PTR
|| (stack_slot_get_type (obj
) == TYPE_NATIVE_INT
&& !get_stack_type (&field
->parent
->byval_arg
));
3387 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3388 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx
->ip_offset
));
3391 g_assert (obj
->type
);
3393 /*The value on the stack must be a subclass of the defining type of the field*/
3394 /* we need to check if we can load the field from the stack value*/
3396 if (stack_slot_get_underlying_type (obj
) == TYPE_NATIVE_INT
)
3397 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx
->ip_offset
));
3399 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3400 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3402 if (!field
->parent
->valuetype
&& stack_slot_is_managed_pointer (obj
))
3403 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
));
3405 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3406 if (field
->parent
->valuetype
&& stack_slot_is_boxed_value (obj
))
3407 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
));
3409 if (!stack_slot_is_null_literal (obj
) && !verify_stack_type_compatibility_full (ctx
, &field
->parent
->byval_arg
, obj
, TRUE
, FALSE
)) {
3410 char *found
= stack_slot_full_name (obj
);
3411 char *expected
= mono_type_full_name (&field
->parent
->byval_arg
);
3412 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Expected type '%s' but found '%s' referencing the 'this' argument at 0x%04x", expected
, found
, ctx
->ip_offset
));
3417 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, mono_class_from_mono_type (obj
->type
)))
3418 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3421 check_unmanaged_pointer (ctx
, obj
);
3426 do_push_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3429 MonoClassField
*field
;
3432 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3434 if (!check_underflow (ctx
, 1))
3436 obj
= stack_pop_safe (ctx
);
3438 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, take_addr
? "ldflda" : "ldfld"))
3441 if (take_addr
&& field
->parent
->valuetype
&& !stack_slot_is_managed_pointer (obj
))
3442 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx
->ip_offset
));
3444 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3445 !(field
->parent
== ctx
->method
->klass
&& mono_method_is_constructor (ctx
->method
)))
3446 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3448 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3452 do_store_field (VerifyContext
*ctx
, int token
)
3454 ILStackDesc
*value
, *obj
;
3455 MonoClassField
*field
;
3456 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3458 if (!check_underflow (ctx
, 2))
3461 value
= stack_pop (ctx
);
3462 obj
= stack_pop_safe (ctx
);
3464 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, "stfld"))
3467 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3468 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3471 /*TODO proper handle for Nullable<T>*/
3473 do_box_value (VerifyContext
*ctx
, int klass_token
)
3476 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "box");
3482 if (!check_underflow (ctx
, 1))
3485 value
= stack_pop (ctx
);
3486 /*box is a nop for reference types*/
3488 if (stack_slot_get_underlying_type (value
) == TYPE_COMPLEX
&& MONO_TYPE_IS_REFERENCE (value
->type
) && MONO_TYPE_IS_REFERENCE (type
)) {
3489 stack_push_stack_val (ctx
, value
)->stype
|= BOXED_MASK
;
3494 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3495 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx
->ip_offset
));
3497 klass
= mono_class_from_mono_type (type
);
3498 if (mono_class_is_nullable (klass
))
3499 type
= &mono_class_get_nullable_param (klass
)->byval_arg
;
3500 stack_push_val (ctx
, TYPE_COMPLEX
| BOXED_MASK
, type
);
3504 do_unbox_value (VerifyContext
*ctx
, int klass_token
)
3507 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox");
3512 if (!check_underflow (ctx
, 1))
3515 if (!mono_class_from_mono_type (type
)->valuetype
)
3516 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx
->ip_offset
));
3518 value
= stack_pop (ctx
);
3520 /*Value should be: a boxed valuetype or a reference type*/
3521 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3522 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3523 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
));
3525 set_stack_value (ctx
, value
= stack_push (ctx
), mono_type_get_type_byref (type
), FALSE
);
3526 value
->stype
|= CMMP_MASK
;
3530 do_unbox_any (VerifyContext
*ctx
, int klass_token
)
3533 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox.any");
3538 if (!check_underflow (ctx
, 1))
3541 value
= stack_pop (ctx
);
3543 /*Value should be: a boxed valuetype or a reference type*/
3544 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3545 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3546 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
));
3548 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3552 do_unary_math_op (VerifyContext
*ctx
, int op
)
3555 if (!check_underflow (ctx
, 1))
3557 value
= stack_pop (ctx
);
3558 switch (stack_slot_get_type (value
)) {
3561 case TYPE_NATIVE_INT
:
3566 case TYPE_COMPLEX
: /*only enums are ok*/
3567 if (mono_type_is_enum_type (value
->type
))
3570 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx
->ip_offset
));
3572 stack_push_stack_val (ctx
, value
);
3576 do_conversion (VerifyContext
*ctx
, int kind
)
3579 if (!check_underflow (ctx
, 1))
3581 value
= stack_pop (ctx
);
3583 switch (stack_slot_get_type (value
)) {
3586 case TYPE_NATIVE_INT
:
3590 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
));
3595 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3598 stack_push_val (ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
3601 stack_push_val (ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
3603 case TYPE_NATIVE_INT
:
3604 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3607 g_error ("unknown type %02x in conversion", kind
);
3613 do_load_token (VerifyContext
*ctx
, int token
)
3617 MonoClass
*handle_class
;
3618 if (!check_overflow (ctx
))
3621 if (ctx
->method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
3622 handle
= mono_method_get_wrapper_data (ctx
->method
, token
);
3623 handle_class
= (MonoClass
*)mono_method_get_wrapper_data (ctx
->method
, token
+ 1);
3624 if (handle_class
== mono_defaults
.typehandle_class
)
3625 handle
= &((MonoClass
*)handle
)->byval_arg
;
3627 switch (token
& 0xff000000) {
3628 case MONO_TOKEN_TYPE_DEF
:
3629 case MONO_TOKEN_TYPE_REF
:
3630 case MONO_TOKEN_TYPE_SPEC
:
3631 case MONO_TOKEN_FIELD_DEF
:
3632 case MONO_TOKEN_METHOD_DEF
:
3633 case MONO_TOKEN_METHOD_SPEC
:
3634 case MONO_TOKEN_MEMBER_REF
:
3635 if (!token_bounds_check (ctx
->image
, token
)) {
3636 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Table index out of range 0x%x for token %x for ldtoken at 0x%04x", mono_metadata_token_index (token
), token
, ctx
->ip_offset
));
3641 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid table 0x%x for token 0x%x for ldtoken at 0x%04x", mono_metadata_token_table (token
), token
, ctx
->ip_offset
));
3645 handle
= mono_ldtoken_checked (ctx
->image
, token
, &handle_class
, ctx
->generic_context
, &error
);
3649 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x due to %s", token
, ctx
->ip_offset
, mono_error_get_message (&error
)));
3650 mono_error_cleanup (&error
);
3653 if (handle_class
== mono_defaults
.typehandle_class
) {
3654 mono_type_is_valid_in_context (ctx
, (MonoType
*)handle
);
3655 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
3656 mono_method_is_valid_in_context (ctx
, (MonoMethod
*)handle
);
3657 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
3658 mono_type_is_valid_in_context (ctx
, &((MonoClassField
*)handle
)->parent
->byval_arg
);
3660 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
3662 stack_push_val (ctx
, TYPE_COMPLEX
, mono_class_get_type (handle_class
));
3666 do_ldobj_value (VerifyContext
*ctx
, int token
)
3669 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "ldobj");
3670 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3675 if (!check_underflow (ctx
, 1))
3678 value
= stack_pop (ctx
);
3679 if (!stack_slot_is_managed_pointer (value
)
3680 && stack_slot_get_type (value
) != TYPE_NATIVE_INT
3681 && !(stack_slot_get_type (value
) == TYPE_PTR
&& value
->type
->type
!= MONO_TYPE_FNPTR
)) {
3682 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3686 if (stack_slot_get_type (value
) == TYPE_NATIVE_INT
)
3687 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx
->ip_offset
));
3689 /*We have a byval on the stack, but the comparison must be strict. */
3690 if (!verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (value
->type
), TRUE
))
3691 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx
->ip_offset
));
3693 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3697 do_stobj (VerifyContext
*ctx
, int token
)
3699 ILStackDesc
*dest
, *src
;
3700 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "stobj");
3701 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3706 if (!check_underflow (ctx
, 2))
3709 src
= stack_pop (ctx
);
3710 dest
= stack_pop (ctx
);
3712 if (stack_slot_is_managed_mutability_pointer (dest
))
3713 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx
->ip_offset
));
3715 if (!stack_slot_is_managed_pointer (dest
))
3716 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx
->ip_offset
));
3718 if (stack_slot_is_boxed_value (src
) && !MONO_TYPE_IS_REFERENCE (src
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3719 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
));
3721 if (!verify_stack_type_compatibility (ctx
, type
, src
)) {
3722 char *type_name
= mono_type_full_name (type
);
3723 char *src_name
= stack_slot_full_name (src
);
3724 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token '%s' and source '%s' of stobj don't match ' at 0x%04x", type_name
, src_name
, ctx
->ip_offset
));
3729 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3730 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3734 do_cpobj (VerifyContext
*ctx
, int token
)
3736 ILStackDesc
*dest
, *src
;
3737 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "cpobj");
3741 if (!check_underflow (ctx
, 2))
3744 src
= stack_pop (ctx
);
3745 dest
= stack_pop (ctx
);
3747 if (!stack_slot_is_managed_pointer (src
))
3748 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx
->ip_offset
));
3750 if (!stack_slot_is_managed_pointer (dest
))
3751 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx
->ip_offset
));
3753 if (stack_slot_is_managed_mutability_pointer (dest
))
3754 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx
->ip_offset
));
3756 if (!verify_type_compatibility (ctx
, type
, mono_type_get_type_byval (src
->type
)))
3757 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3759 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3760 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3764 do_initobj (VerifyContext
*ctx
, int token
)
3767 MonoType
*stack
, *type
= get_boxable_mono_type (ctx
, token
, "initobj");
3771 if (!check_underflow (ctx
, 1))
3774 obj
= stack_pop (ctx
);
3776 if (!stack_slot_is_managed_pointer (obj
))
3777 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx
->ip_offset
));
3779 if (stack_slot_is_managed_mutability_pointer (obj
))
3780 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx
->ip_offset
));
3782 stack
= mono_type_get_type_byval (obj
->type
);
3783 if (MONO_TYPE_IS_REFERENCE (stack
)) {
3784 if (!verify_type_compatibility (ctx
, stack
, type
))
3785 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3786 else if (IS_STRICT_MODE (ctx
) && !mono_metadata_type_equal (type
, stack
))
3787 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3788 } else if (!verify_type_compatibility (ctx
, stack
, type
)) {
3789 char *expected_name
= mono_type_full_name (type
);
3790 char *stack_name
= mono_type_full_name (stack
);
3792 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
));
3793 g_free (expected_name
);
3794 g_free (stack_name
);
3799 do_newobj (VerifyContext
*ctx
, int token
)
3803 MonoMethodSignature
*sig
;
3805 gboolean is_delegate
= FALSE
;
3807 if (!(method
= verifier_load_method (ctx
, token
, "newobj")))
3810 if (!mono_method_is_constructor (method
)) {
3811 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token
, ctx
->ip_offset
));
3815 if (mono_class_get_flags (method
->klass
) & (TYPE_ATTRIBUTE_ABSTRACT
| TYPE_ATTRIBUTE_INTERFACE
))
3816 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx
->ip_offset
));
3818 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3819 char *from
= mono_method_full_name (ctx
->method
, TRUE
);
3820 char *to
= mono_method_full_name (method
, TRUE
);
3821 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
);
3826 //FIXME use mono_method_get_signature_full
3827 sig
= mono_method_signature (method
);
3829 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx
->ip_offset
));
3833 if (!sig
->hasthis
) {
3834 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid constructor signature missing hasthis at 0x%04x", ctx
->ip_offset
));
3838 if (!check_underflow (ctx
, sig
->param_count
))
3841 is_delegate
= method
->klass
->parent
== mono_defaults
.multicastdelegate_class
;
3844 ILStackDesc
*funptr
;
3845 //first arg is object, second arg is fun ptr
3846 if (sig
->param_count
!= 2) {
3847 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx
->ip_offset
));
3850 funptr
= stack_pop (ctx
);
3851 value
= stack_pop (ctx
);
3852 verify_delegate_compatibility (ctx
, method
->klass
, value
, funptr
);
3854 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3855 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i
); );
3856 value
= stack_pop (ctx
);
3857 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3858 char *stack_name
= stack_slot_full_name (value
);
3859 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3860 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
));
3861 g_free (stack_name
);
3865 if (stack_slot_is_managed_mutability_pointer (value
))
3866 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx
->ip_offset
));
3870 if (check_overflow (ctx
))
3871 set_stack_value (ctx
, stack_push (ctx
), &method
->klass
->byval_arg
, FALSE
);
3875 do_cast (VerifyContext
*ctx
, int token
, const char *opcode
) {
3881 if (!check_underflow (ctx
, 1))
3884 if (!(type
= get_boxable_mono_type (ctx
, token
, opcode
)))
3888 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid %s type at 0x%04x", opcode
, ctx
->ip_offset
));
3892 value
= stack_pop (ctx
);
3893 is_boxed
= stack_slot_is_boxed_value (value
);
3895 if (stack_slot_is_managed_pointer (value
))
3896 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
3897 else if (!MONO_TYPE_IS_REFERENCE (value
->type
) && !is_boxed
) {
3898 char *name
= stack_slot_full_name (value
);
3899 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
));
3903 switch (value
->type
->type
) {
3904 case MONO_TYPE_FNPTR
:
3906 case MONO_TYPE_TYPEDBYREF
:
3907 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
3912 do_box
= is_boxed
|| mono_type_is_generic_argument(type
) || mono_class_from_mono_type (type
)->valuetype
;
3913 stack_push_val (ctx
, TYPE_COMPLEX
| (do_box
? BOXED_MASK
: 0), type
);
3917 mono_type_from_opcode (int opcode
) {
3925 return &mono_defaults
.sbyte_class
->byval_arg
;
3933 return &mono_defaults
.int16_class
->byval_arg
;
3941 return &mono_defaults
.int32_class
->byval_arg
;
3947 return &mono_defaults
.int64_class
->byval_arg
;
3953 return &mono_defaults
.single_class
->byval_arg
;
3959 return &mono_defaults
.double_class
->byval_arg
;
3965 return &mono_defaults
.int_class
->byval_arg
;
3969 case CEE_LDELEM_REF
:
3970 case CEE_STELEM_REF
:
3971 return &mono_defaults
.object_class
->byval_arg
;
3974 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode
);
3980 do_load_indirect (VerifyContext
*ctx
, int opcode
)
3983 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3985 if (!check_underflow (ctx
, 1))
3988 value
= stack_pop (ctx
);
3989 if (!stack_slot_is_managed_pointer (value
)) {
3990 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx
->ip_offset
));
3991 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
3995 if (opcode
== CEE_LDIND_REF
) {
3996 if (stack_slot_get_underlying_type (value
) != TYPE_COMPLEX
|| mono_class_from_mono_type (value
->type
)->valuetype
)
3997 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx
->ip_offset
));
3998 set_stack_value (ctx
, stack_push (ctx
), mono_type_get_type_byval (value
->type
), FALSE
);
4000 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (value
->type
), TRUE
))
4001 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4002 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
4007 do_store_indirect (VerifyContext
*ctx
, int opcode
)
4009 ILStackDesc
*addr
, *val
;
4010 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
4012 if (!check_underflow (ctx
, 2))
4015 val
= stack_pop (ctx
);
4016 addr
= stack_pop (ctx
);
4018 check_unmanaged_pointer (ctx
, addr
);
4020 if (!stack_slot_is_managed_pointer (addr
) && stack_slot_get_type (addr
) != TYPE_PTR
) {
4021 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx
->ip_offset
));
4025 if (stack_slot_is_managed_mutability_pointer (addr
)) {
4026 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx
->ip_offset
));
4030 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (addr
->type
), TRUE
))
4031 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4033 if (!verify_stack_type_compatibility (ctx
, mono_type_from_opcode (opcode
), val
))
4034 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
4038 do_newarr (VerifyContext
*ctx
, int token
)
4041 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "newarr");
4046 if (!check_underflow (ctx
, 1))
4049 value
= stack_pop (ctx
);
4050 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4051 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
));
4053 set_stack_value (ctx
, stack_push (ctx
), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type
), 1)), FALSE
);
4056 /*FIXME handle arrays that are not 0-indexed*/
4058 do_ldlen (VerifyContext
*ctx
)
4062 if (!check_underflow (ctx
, 1))
4065 value
= stack_pop (ctx
);
4067 if (stack_slot_get_type (value
) != TYPE_COMPLEX
|| value
->type
->type
!= MONO_TYPE_SZARRAY
)
4068 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx
->ip_offset
));
4070 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
4073 /*FIXME handle arrays that are not 0-indexed*/
4074 /*FIXME handle readonly prefix and CMMP*/
4076 do_ldelema (VerifyContext
*ctx
, int klass_token
)
4078 ILStackDesc
*index
, *array
, *res
;
4079 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "ldelema");
4085 if (!check_underflow (ctx
, 2))
4088 index
= stack_pop (ctx
);
4089 array
= stack_pop (ctx
);
4091 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4092 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
));
4094 if (!stack_slot_is_null_literal (array
)) {
4095 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4096 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array
), ctx
->ip_offset
));
4098 if (get_stack_type (type
) == TYPE_I4
|| get_stack_type (type
) == TYPE_NATIVE_INT
) {
4099 valid
= verify_type_compatibility_full (ctx
, type
, &array
->type
->data
.klass
->byval_arg
, TRUE
);
4101 valid
= mono_metadata_type_equal (type
, &array
->type
->data
.klass
->byval_arg
);
4104 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx
->ip_offset
));
4108 res
= stack_push (ctx
);
4109 set_stack_value (ctx
, res
, type
, TRUE
);
4110 if (ctx
->prefix_set
& PREFIX_READONLY
) {
4111 ctx
->prefix_set
&= ~PREFIX_READONLY
;
4112 res
->stype
|= CMMP_MASK
;
4117 * FIXME handle arrays that are not 0-indexed
4118 * FIXME handle readonly prefix and CMMP
4121 do_ldelem (VerifyContext
*ctx
, int opcode
, int token
)
4123 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4124 ILStackDesc
*index
, *array
;
4126 if (!check_underflow (ctx
, 2))
4129 if (opcode
== CEE_LDELEM
) {
4130 if (!(type
= verifier_load_type (ctx
, token
, "ldelem.any"))) {
4131 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4135 type
= mono_type_from_opcode (opcode
);
4138 index
= stack_pop (ctx
);
4139 array
= stack_pop (ctx
);
4141 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4142 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
));
4144 if (!stack_slot_is_null_literal (array
)) {
4145 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
4146 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
));
4148 if (opcode
== CEE_LDELEM_REF
) {
4149 if (array
->type
->data
.klass
->valuetype
)
4150 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx
->ip_offset
));
4151 type
= &array
->type
->data
.klass
->byval_arg
;
4153 MonoType
*candidate
= &array
->type
->data
.klass
->byval_arg
;
4154 if (IS_STRICT_MODE (ctx
)) {
4155 MonoType
*underlying_type
= mono_type_get_underlying_type_any (type
);
4156 MonoType
*underlying_candidate
= mono_type_get_underlying_type_any (candidate
);
4157 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
)) ||
4158 (IS_ONE_OF2 (underlying_candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
) && IS_ONE_OF2 (underlying_type
->type
, MONO_TYPE_I
, MONO_TYPE_U
)))
4159 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4161 if (!verify_type_compatibility_full (ctx
, type
, candidate
, TRUE
))
4162 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
4167 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
4172 * FIXME handle arrays that are not 0-indexed
4175 do_stelem (VerifyContext
*ctx
, int opcode
, int token
)
4177 ILStackDesc
*index
, *array
, *value
;
4179 if (!check_underflow (ctx
, 3))
4182 if (opcode
== CEE_STELEM
) {
4183 if (!(type
= verifier_load_type (ctx
, token
, "stelem.any"))) {
4184 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
4188 type
= mono_type_from_opcode (opcode
);
4191 value
= stack_pop (ctx
);
4192 index
= stack_pop (ctx
);
4193 array
= stack_pop (ctx
);
4195 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
4196 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
));
4198 if (!stack_slot_is_null_literal (array
)) {
4199 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
) {
4200 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
));
4202 if (opcode
== CEE_STELEM_REF
) {
4203 if (array
->type
->data
.klass
->valuetype
)
4204 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4205 } else if (!verify_type_compatibility_full (ctx
, &array
->type
->data
.klass
->byval_arg
, type
, TRUE
)) {
4206 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4210 if (opcode
== CEE_STELEM_REF
) {
4211 if (!stack_slot_is_boxed_value (value
) && mono_class_from_mono_type (value
->type
)->valuetype
)
4212 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
4213 } else if (opcode
!= CEE_STELEM_REF
) {
4214 if (!verify_stack_type_compatibility (ctx
, type
, value
))
4215 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
4217 if (stack_slot_is_boxed_value (value
) && !MONO_TYPE_IS_REFERENCE (value
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
4218 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
));
4224 do_throw (VerifyContext
*ctx
)
4226 ILStackDesc
*exception
;
4227 if (!check_underflow (ctx
, 1))
4229 exception
= stack_pop (ctx
);
4231 if (!stack_slot_is_null_literal (exception
) && !(stack_slot_get_type (exception
) == TYPE_COMPLEX
&& !mono_class_from_mono_type (exception
->type
)->valuetype
))
4232 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx
->ip_offset
));
4234 if (mono_type_is_generic_argument (exception
->type
) && !stack_slot_is_boxed_value (exception
)) {
4235 char *name
= mono_type_full_name (exception
->type
);
4236 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
));
4239 /*The stack is left empty after a throw*/
4245 do_endfilter (VerifyContext
*ctx
)
4247 MonoExceptionClause
*clause
;
4249 if (IS_STRICT_MODE (ctx
)) {
4250 if (ctx
->eval
.size
!= 1)
4251 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx
->ip_offset
));
4253 if (ctx
->eval
.size
>= 1 && stack_slot_get_type (stack_pop (ctx
)) != TYPE_I4
)
4254 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx
->ip_offset
));
4257 if ((clause
= is_correct_endfilter (ctx
, ctx
->ip_offset
))) {
4258 if (IS_STRICT_MODE (ctx
)) {
4259 if (ctx
->ip_offset
!= clause
->handler_offset
- 2)
4260 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4262 if ((ctx
->ip_offset
!= clause
->handler_offset
- 2) && !MONO_OFFSET_IN_HANDLER (clause
, ctx
->ip_offset
))
4263 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
4266 if (IS_STRICT_MODE (ctx
) && !is_unverifiable_endfilter (ctx
, ctx
->ip_offset
))
4267 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4269 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
4276 do_leave (VerifyContext
*ctx
, int delta
)
4278 int target
= ((gint32
)ctx
->ip_offset
) + delta
;
4279 if (target
>= ctx
->code_size
|| target
< 0)
4280 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
4282 if (!is_correct_leave (ctx
->header
, ctx
->ip_offset
, target
))
4283 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx
->ip_offset
));
4285 ctx
->target
= target
;
4291 * Verify br and br.s opcodes.
4294 do_static_branch (VerifyContext
*ctx
, int delta
)
4296 int target
= ctx
->ip_offset
+ delta
;
4297 if (target
< 0 || target
>= ctx
->code_size
) {
4298 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("branch target out of code at 0x%04x", ctx
->ip_offset
));
4302 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4304 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4307 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
4311 ctx
->target
= target
;
4315 do_switch (VerifyContext
*ctx
, int count
, const unsigned char *data
)
4317 int i
, base
= ctx
->ip_offset
+ 5 + count
* 4;
4320 if (!check_underflow (ctx
, 1))
4323 value
= stack_pop (ctx
);
4325 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4326 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx
->ip_offset
));
4328 for (i
= 0; i
< count
; ++i
) {
4329 int target
= base
+ read32 (data
+ i
* 4);
4331 if (target
< 0 || target
>= ctx
->code_size
) {
4332 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x out of code at 0x%04x", i
, ctx
->ip_offset
));
4336 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4338 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4341 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4344 merge_stacks (ctx
, &ctx
->eval
, &ctx
->code
[target
], FALSE
, TRUE
);
4349 do_load_function_ptr (VerifyContext
*ctx
, guint32 token
, gboolean virtual_
)
4354 if (virtual_
&& !check_underflow (ctx
, 1))
4357 if (!virtual_
&& !check_overflow (ctx
))
4360 if (ctx
->method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
4361 method
= (MonoMethod
*)mono_method_get_wrapper_data (ctx
->method
, (guint32
)token
);
4363 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4367 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4368 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4372 if (!(method
= verifier_load_method (ctx
, token
, virtual_
? "ldvirtfrn" : "ldftn")))
4376 if (mono_method_is_constructor (method
))
4377 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx
->ip_offset
));
4380 ILStackDesc
*top
= stack_pop (ctx
);
4382 if (stack_slot_get_type (top
) != TYPE_COMPLEX
|| top
->type
->type
== MONO_TYPE_VALUETYPE
)
4383 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx
->ip_offset
));
4385 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
)
4386 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx
->ip_offset
));
4388 if (!verify_stack_type_compatibility (ctx
, &method
->klass
->byval_arg
, top
))
4389 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx
->ip_offset
));
4392 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
))
4393 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
);
4395 top
= stack_push_val(ctx
, TYPE_PTR
, mono_type_create_fnptr_from_mono_method (ctx
, method
));
4396 top
->method
= method
;
4400 do_sizeof (VerifyContext
*ctx
, int token
)
4404 if (!(type
= verifier_load_type (ctx
, token
, "sizeof")))
4407 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
4408 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx
->ip_offset
));
4412 if (type
->type
== MONO_TYPE_VOID
) {
4413 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx
->ip_offset
));
4417 if (check_overflow (ctx
))
4418 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.uint32_class
->byval_arg
, FALSE
);
4421 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4423 do_localloc (VerifyContext
*ctx
)
4425 if (ctx
->eval
.size
!= 1) {
4426 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4430 if (in_any_exception_block (ctx
->header
, ctx
->ip_offset
)) {
4431 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4435 /*TODO verify top type*/
4436 /* top = */ stack_pop (ctx
);
4438 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.int_class
->byval_arg
, FALSE
);
4439 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx
->ip_offset
));
4443 do_ldstr (VerifyContext
*ctx
, guint32 token
)
4445 GSList
*error
= NULL
;
4446 if (ctx
->method
->wrapper_type
== MONO_WRAPPER_NONE
&& !image_is_dynamic (ctx
->image
)) {
4447 if (mono_metadata_token_code (token
) != MONO_TOKEN_STRING
) {
4448 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4452 if (!mono_verifier_verify_string_signature (ctx
->image
, mono_metadata_token_index (token
), &error
)) {
4454 ctx
->list
= g_slist_concat (ctx
->list
, error
);
4455 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string index %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4460 if (check_overflow (ctx
))
4461 stack_push_val (ctx
, TYPE_COMPLEX
, &mono_defaults
.string_class
->byval_arg
);
4465 do_refanyval (VerifyContext
*ctx
, int token
)
4469 if (!check_underflow (ctx
, 1))
4472 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4475 top
= stack_pop (ctx
);
4477 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4478 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
));
4480 set_stack_value (ctx
, stack_push (ctx
), type
, TRUE
);
4484 do_refanytype (VerifyContext
*ctx
)
4488 if (!check_underflow (ctx
, 1))
4491 top
= stack_pop (ctx
);
4493 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4494 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
));
4496 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typehandle_class
->byval_arg
, FALSE
);
4501 do_mkrefany (VerifyContext
*ctx
, int token
)
4505 if (!check_underflow (ctx
, 1))
4508 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4511 top
= stack_pop (ctx
);
4513 if (stack_slot_is_managed_mutability_pointer (top
))
4514 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx
->ip_offset
));
4516 if (!stack_slot_is_managed_pointer (top
)) {
4517 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
));
4519 MonoType
*stack_type
= mono_type_get_type_byval (top
->type
);
4520 if (MONO_TYPE_IS_REFERENCE (type
) && !mono_metadata_type_equal (type
, stack_type
))
4521 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4523 if (!MONO_TYPE_IS_REFERENCE (type
) && !verify_type_compatibility_full (ctx
, type
, stack_type
, TRUE
))
4524 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4527 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typed_reference_class
->byval_arg
, FALSE
);
4531 do_ckfinite (VerifyContext
*ctx
)
4534 if (!check_underflow (ctx
, 1))
4537 top
= stack_pop (ctx
);
4539 if (stack_slot_get_underlying_type (top
) != TYPE_R8
)
4540 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
));
4541 stack_push_stack_val (ctx
, top
);
4545 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4547 * @from holds new values for a given control path
4548 * @to holds the current values of a given control path
4550 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4553 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
)
4557 stack_init (ctx
, to
);
4560 if (to
->flags
== IL_CODE_FLAG_NOT_PROCESSED
)
4563 stack_copy (&ctx
->eval
, to
);
4565 } else if (!(to
->flags
& IL_CODE_STACK_MERGED
)) {
4566 stack_copy (to
, &ctx
->eval
);
4569 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from
->size
, to
->size
); );
4571 if (from
->size
!= to
->size
) {
4572 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from
->size
, to
->size
, ctx
->ip_offset
); );
4573 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
));
4577 //FIXME we need to preserve CMMP attributes
4578 //FIXME we must take null literals into consideration.
4579 for (i
= 0; i
< from
->size
; ++i
) {
4580 ILStackDesc
*new_slot
= from
->stack
+ i
;
4581 ILStackDesc
*old_slot
= to
->stack
+ i
;
4582 MonoType
*new_type
= mono_type_from_stack_slot (new_slot
);
4583 MonoType
*old_type
= mono_type_from_stack_slot (old_slot
);
4584 MonoClass
*old_class
= mono_class_from_mono_type (old_type
);
4585 MonoClass
*new_class
= mono_class_from_mono_type (new_type
);
4586 MonoClass
*match_class
= NULL
;
4588 // S := T then U = S (new value is compatible with current value, keep current)
4589 if (verify_stack_type_compatibility (ctx
, old_type
, new_slot
)) {
4590 copy_stack_value (new_slot
, old_slot
);
4594 // T := S then U = T (old value is compatible with current value, use new)
4595 if (verify_stack_type_compatibility (ctx
, new_type
, old_slot
)) {
4596 copy_stack_value (old_slot
, new_slot
);
4600 /*Both slots are the same boxed valuetype. Simply copy it.*/
4601 if (stack_slot_is_boxed_value (old_slot
) &&
4602 stack_slot_is_boxed_value (new_slot
) &&
4603 mono_metadata_type_equal (old_type
, new_type
)) {
4604 copy_stack_value (new_slot
, old_slot
);
4608 if (mono_type_is_generic_argument (old_type
) || mono_type_is_generic_argument (new_type
)) {
4609 char *old_name
= stack_slot_full_name (old_slot
);
4610 char *new_name
= stack_slot_full_name (new_slot
);
4611 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
));
4617 //both are reference types, use closest common super type
4618 if (!mono_class_from_mono_type (old_type
)->valuetype
4619 && !mono_class_from_mono_type (new_type
)->valuetype
4620 && !stack_slot_is_managed_pointer (old_slot
)
4621 && !stack_slot_is_managed_pointer (new_slot
)) {
4623 mono_class_setup_supertypes (old_class
);
4624 mono_class_setup_supertypes (new_class
);
4626 for (j
= MIN (old_class
->idepth
, new_class
->idepth
) - 1; j
> 0; --j
) {
4627 if (mono_metadata_type_equal (&old_class
->supertypes
[j
]->byval_arg
, &new_class
->supertypes
[j
]->byval_arg
)) {
4628 match_class
= old_class
->supertypes
[j
];
4633 mono_class_setup_interfaces (old_class
, &error
);
4634 if (!mono_error_ok (&error
)) {
4635 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot merge stacks due to a TypeLoadException %s at 0x%04x", mono_error_get_message (&error
), ctx
->ip_offset
));
4636 mono_error_cleanup (&error
);
4639 mono_class_setup_interfaces (new_class
, &error
);
4640 if (!mono_error_ok (&error
)) {
4641 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot merge stacks due to a TypeLoadException %s at 0x%04x", mono_error_get_message (&error
), ctx
->ip_offset
));
4642 mono_error_cleanup (&error
);
4646 /* if old class is an interface that new class implements */
4647 if (mono_class_is_interface (old_class
)) {
4648 if (verifier_class_is_assignable_from (old_class
, new_class
)) {
4649 match_class
= old_class
;
4652 for (j
= 0; j
< old_class
->interface_count
; ++j
) {
4653 if (verifier_class_is_assignable_from (old_class
->interfaces
[j
], new_class
)) {
4654 match_class
= old_class
->interfaces
[j
];
4660 if (mono_class_is_interface (new_class
)) {
4661 if (verifier_class_is_assignable_from (new_class
, old_class
)) {
4662 match_class
= new_class
;
4665 for (j
= 0; j
< new_class
->interface_count
; ++j
) {
4666 if (verifier_class_is_assignable_from (new_class
->interfaces
[j
], old_class
)) {
4667 match_class
= new_class
->interfaces
[j
];
4673 //No decent super type found, use object
4674 match_class
= mono_defaults
.object_class
;
4676 } 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
)) {
4677 match_class
= mono_defaults
.object_class
;
4682 char *old_name
= stack_slot_full_name (old_slot
);
4683 char *new_name
= stack_slot_full_name (new_slot
);
4684 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
));
4688 set_stack_value (ctx
, old_slot
, &new_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4692 g_assert (match_class
);
4693 set_stack_value (ctx
, old_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4694 set_stack_value (ctx
, new_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4700 to
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
4701 to
->flags
|= IL_CODE_STACK_MERGED
;
4704 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4705 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4708 * is_clause_in_range :
4710 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4713 is_clause_in_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4715 if (clause
->try_offset
>= start
&& clause
->try_offset
< end
)
4717 if (HANDLER_START (clause
) >= start
&& HANDLER_START (clause
) < end
)
4723 * is_clause_inside_range :
4725 * Returns TRUE if @clause lies completely inside the @start - @end range.
4728 is_clause_inside_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4730 if (clause
->try_offset
< start
|| (clause
->try_offset
+ clause
->try_len
) > end
)
4732 if (HANDLER_START (clause
) < start
|| (clause
->handler_offset
+ clause
->handler_len
) > end
)
4738 * is_clause_nested :
4740 * Returns TRUE if @nested is nested in @clause.
4743 is_clause_nested (MonoExceptionClause
*clause
, MonoExceptionClause
*nested
)
4745 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (nested
, clause
->data
.filter_offset
, clause
->handler_offset
))
4747 return is_clause_inside_range (nested
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4748 is_clause_inside_range (nested
, clause
->handler_offset
, clause
->handler_offset
+ clause
->handler_len
);
4751 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4752 * the each pair of exception must have the following properties:
4753 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4754 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4755 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4758 verify_clause_relationship (VerifyContext
*ctx
, MonoExceptionClause
*clause
, MonoExceptionClause
*to_test
)
4760 /*clause is nested*/
4761 if (to_test
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (clause
, to_test
->data
.filter_offset
, to_test
->handler_offset
)) {
4762 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clause inside filter"));
4766 /*wrong nesting order.*/
4767 if (is_clause_nested (clause
, to_test
)) {
4768 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4772 /*mutual protection*/
4773 if (clause
->try_offset
== to_test
->try_offset
&& clause
->try_len
== to_test
->try_len
) {
4774 /*handlers are not disjoint*/
4775 if (is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) {
4776 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception handlers overlap"));
4779 /* handlers are not catch or filter */
4780 if (!IS_CATCH_OR_FILTER (clause
) || !IS_CATCH_OR_FILTER (to_test
)) {
4781 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4788 /*not completelly disjoint*/
4789 if ((is_clause_in_range (to_test
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4790 is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) && !is_clause_nested (to_test
, clause
))
4791 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses overlap"));
4794 #define code_bounds_check(size) \
4795 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4796 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4801 mono_opcode_is_prefix (int op
)
4804 case MONO_CEE_UNALIGNED_
:
4805 case MONO_CEE_VOLATILE_
:
4806 case MONO_CEE_TAIL_
:
4807 case MONO_CEE_CONSTRAINED_
:
4808 case MONO_CEE_READONLY_
:
4815 * FIXME: need to distinguish between valid and verifiable.
4816 * Need to keep track of types on the stack.
4817 * Verify types for opcodes.
4820 mono_method_verify (MonoMethod
*method
, int level
)
4823 const unsigned char *ip
, *code_start
;
4824 const unsigned char *end
;
4825 MonoSimpleBasicBlock
*bb
= NULL
, *original_bb
= NULL
;
4827 int i
, n
, need_merge
= 0, start
= 0;
4828 guint ip_offset
= 0, prefix
= 0;
4829 MonoGenericContext
*generic_context
= NULL
;
4833 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method
->klass
->name_space
, method
->klass
->name
, method
->name
); );
4835 init_verifier_stats ();
4837 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4838 (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))) {
4842 memset (&ctx
, 0, sizeof (VerifyContext
));
4844 //FIXME use mono_method_get_signature_full
4845 ctx
.signature
= mono_method_signature (method
);
4846 if (!ctx
.signature
) {
4847 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method signature"));
4849 finish_collect_stats ();
4852 if (!method
->is_generic
&& !mono_class_is_gtd (method
->klass
) && ctx
.signature
->has_type_parameters
) {
4853 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Method and signature don't match in terms of genericity"));
4854 finish_collect_stats ();
4858 ctx
.header
= mono_method_get_header_checked (method
, &error
);
4860 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method header due to %s", mono_error_get_message (&error
)));
4861 mono_error_cleanup (&error
);
4862 finish_collect_stats ();
4865 ctx
.method
= method
;
4866 code_start
= ip
= ctx
.header
->code
;
4867 end
= ip
+ ctx
.header
->code_size
;
4868 ctx
.image
= image
= method
->klass
->image
;
4871 ctx
.max_args
= ctx
.signature
->param_count
+ ctx
.signature
->hasthis
;
4872 ctx
.max_stack
= ctx
.header
->max_stack
;
4873 ctx
.verifiable
= ctx
.valid
= 1;
4876 ctx
.code
= g_new (ILCodeDesc
, ctx
.header
->code_size
);
4877 ctx
.code_size
= ctx
.header
->code_size
;
4878 _MEM_ALLOC (sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4880 memset(ctx
.code
, 0, sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4882 ctx
.num_locals
= ctx
.header
->num_locals
;
4883 ctx
.locals
= (MonoType
**)g_memdup (ctx
.header
->locals
, sizeof (MonoType
*) * ctx
.header
->num_locals
);
4884 _MEM_ALLOC (sizeof (MonoType
*) * ctx
.header
->num_locals
);
4886 if (ctx
.num_locals
> 0 && !ctx
.header
->init_locals
)
4887 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Method with locals variable but without init locals set"));
4889 ctx
.params
= g_new (MonoType
*, ctx
.max_args
);
4890 _MEM_ALLOC (sizeof (MonoType
*) * ctx
.max_args
);
4892 if (ctx
.signature
->hasthis
)
4893 ctx
.params
[0] = method
->klass
->valuetype
? &method
->klass
->this_arg
: &method
->klass
->byval_arg
;
4894 memcpy (ctx
.params
+ ctx
.signature
->hasthis
, ctx
.signature
->params
, sizeof (MonoType
*) * ctx
.signature
->param_count
);
4896 if (ctx
.signature
->is_inflated
)
4897 ctx
.generic_context
= generic_context
= mono_method_get_context (method
);
4899 if (!generic_context
&& (mono_class_is_gtd (method
->klass
) || method
->is_generic
)) {
4900 if (method
->is_generic
)
4901 ctx
.generic_context
= generic_context
= &(mono_method_get_generic_container (method
)->context
);
4903 ctx
.generic_context
= generic_context
= &mono_class_get_generic_container (method
->klass
)->context
;
4906 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4907 MonoType
*uninflated
= ctx
.locals
[i
];
4908 ctx
.locals
[i
] = mono_class_inflate_generic_type_checked (ctx
.locals
[i
], ctx
.generic_context
, &error
);
4909 if (!mono_error_ok (&error
)) {
4910 char *name
= mono_type_full_name (ctx
.locals
[i
] ? ctx
.locals
[i
] : uninflated
);
4911 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %d of type %s", i
, name
));
4913 mono_error_cleanup (&error
);
4914 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4920 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4921 MonoType
*uninflated
= ctx
.params
[i
];
4922 ctx
.params
[i
] = mono_class_inflate_generic_type_checked (ctx
.params
[i
], ctx
.generic_context
, &error
);
4923 if (!mono_error_ok (&error
)) {
4924 char *name
= mono_type_full_name (ctx
.params
[i
] ? ctx
.params
[i
] : uninflated
);
4925 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %d of type %s", i
, name
));
4927 mono_error_cleanup (&error
);
4928 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4933 stack_init (&ctx
, &ctx
.eval
);
4935 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4936 if (!mono_type_is_valid_in_context (&ctx
, ctx
.locals
[i
]))
4938 if (get_stack_type (ctx
.locals
[i
]) == TYPE_INV
) {
4939 char *name
= mono_type_full_name (ctx
.locals
[i
]);
4940 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %i of type %s", i
, name
));
4947 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4948 if (!mono_type_is_valid_in_context (&ctx
, ctx
.params
[i
]))
4951 if (get_stack_type (ctx
.params
[i
]) == TYPE_INV
) {
4952 char *name
= mono_type_full_name (ctx
.params
[i
]);
4953 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %i of type %s", i
, name
));
4962 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
4963 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
4964 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
); );
4966 if (clause
->try_offset
> ctx
.code_size
|| ADD_IS_GREATER_OR_OVF (clause
->try_offset
, clause
->try_len
, ctx
.code_size
))
4967 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause out of bounds at 0x%04x", clause
->try_offset
));
4969 if (clause
->try_len
<= 0)
4970 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
4972 if (clause
->handler_offset
> ctx
.code_size
|| ADD_IS_GREATER_OR_OVF (clause
->handler_offset
, clause
->handler_len
, ctx
.code_size
))
4973 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause
->try_offset
));
4975 if (clause
->handler_len
<= 0)
4976 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause
->try_offset
));
4978 if (clause
->try_offset
< clause
->handler_offset
&& ADD_IS_GREATER_OR_OVF (clause
->try_offset
, clause
->try_len
, HANDLER_START (clause
)))
4979 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause
->try_offset
, clause
->handler_offset
));
4981 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
4982 if (clause
->data
.filter_offset
> ctx
.code_size
)
4983 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause
->try_offset
));
4985 if (clause
->data
.filter_offset
>= clause
->handler_offset
)
4986 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause
->data
.filter_offset
));
4989 for (n
= i
+ 1; n
< ctx
.header
->num_clauses
&& ctx
.valid
; ++n
)
4990 verify_clause_relationship (&ctx
, clause
, ctx
.header
->clauses
+ n
);
4995 ctx
.code
[clause
->try_offset
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4996 if (clause
->try_offset
+ clause
->try_len
< ctx
.code_size
)
4997 ctx
.code
[clause
->try_offset
+ clause
->try_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4998 if (clause
->handler_offset
+ clause
->handler_len
< ctx
.code_size
)
4999 ctx
.code
[clause
->handler_offset
+ clause
->handler_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
5001 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
) {
5002 if (!clause
->data
.catch_class
) {
5003 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Catch clause %d with invalid type", i
));
5006 if (!mono_type_is_valid_in_context (&ctx
, &clause
->data
.catch_class
->byval_arg
))
5009 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, clause
->data
.catch_class
);
5011 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
5012 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->data
.filter_offset
, mono_defaults
.exception_class
);
5013 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, mono_defaults
.exception_class
);
5020 original_bb
= bb
= mono_basic_block_split (method
, &error
, ctx
.header
);
5021 if (!mono_error_ok (&error
)) {
5022 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error
)));
5023 mono_error_cleanup (&error
);
5028 while (ip
< end
&& ctx
.valid
) {
5030 ip_offset
= (guint
) (ip
- code_start
);
5032 const unsigned char *ip_copy
= ip
;
5035 if (ip_offset
> bb
->end
) {
5036 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb
->end
, ip_offset
));
5040 if (ip_offset
== bb
->end
)
5043 op_size
= mono_opcode_value_and_size (&ip_copy
, end
, &op
);
5044 if (op_size
== -1) {
5045 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ip_offset
));
5049 if (ADD_IS_GREATER_OR_OVF (ip_offset
, op_size
, bb
->end
)) {
5050 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset
));
5054 /*Last Instruction*/
5055 if (ip_offset
+ op_size
== bb
->end
&& mono_opcode_is_prefix (op
)) {
5056 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or EH block targets between prefix '%s' and instruction at 0x%04x", mono_opcode_name (op
), ip_offset
));
5061 ctx
.ip_offset
= ip_offset
= (guint
) (ip
- code_start
);
5063 /*We need to check against fallthrou in and out of protected blocks.
5064 * For fallout we check the once a protected block ends, if the start flag is not set.
5065 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5066 * TODO convert these checks to be done using flags and not this loop
5068 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
5069 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
5071 if ((clause
->try_offset
+ clause
->try_len
== ip_offset
) && start
== 0) {
5072 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset
));
5076 if ((clause
->handler_offset
+ clause
->handler_len
== ip_offset
) && start
== 0) {
5077 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
5078 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5080 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
5084 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& clause
->handler_offset
== ip_offset
&& start
== 0) {
5085 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset
));
5089 if (clause
->handler_offset
== ip_offset
&& start
== 0) {
5090 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset
));
5094 if (clause
->try_offset
== ip_offset
&& ctx
.eval
.size
> 0 && start
== 0) {
5095 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset
));
5100 /*This must be done after fallthru detection otherwise it won't happen.*/
5102 /*FIXME remove this once we move all bad branch checking code to use BB only*/
5103 ctx
.code
[ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5112 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx
.target
); );
5113 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ctx
.target
], FALSE
, TRUE
);
5116 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ip_offset
], start
, FALSE
);
5119 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5120 #ifdef MONO_VERIFIER_DEBUG
5123 discode
= mono_disasm_code_one (NULL
, method
, ip
, NULL
);
5124 discode
[strlen (discode
) - 1] = 0; /* no \n */
5125 g_print ("[%d] %-29s (%d)\n", ip_offset
, discode
, ctx
.eval
.size
);
5128 dump_stack_state (&ctx
.code
[ip_offset
]);
5129 dump_stack_state (&ctx
.eval
);
5142 push_arg (&ctx
, *ip
- CEE_LDARG_0
, FALSE
);
5148 code_bounds_check (2);
5149 push_arg (&ctx
, ip
[1], *ip
== CEE_LDARGA_S
);
5153 case CEE_ADD_OVF_UN
:
5154 do_binop (&ctx
, *ip
, add_ovf_un_table
);
5158 case CEE_SUB_OVF_UN
:
5159 do_binop (&ctx
, *ip
, sub_ovf_un_table
);
5166 case CEE_MUL_OVF_UN
:
5167 do_binop (&ctx
, *ip
, bin_ovf_table
);
5172 do_binop (&ctx
, *ip
, add_table
);
5177 do_binop (&ctx
, *ip
, sub_table
);
5184 do_binop (&ctx
, *ip
, bin_op_table
);
5193 do_binop (&ctx
, *ip
, int_bin_op_table
);
5200 do_binop (&ctx
, *ip
, shift_op_table
);
5205 if (!check_underflow (&ctx
, 1))
5207 stack_pop_safe (&ctx
);
5221 /*TODO support definite assignment verification? */
5222 push_local (&ctx
, *ip
- CEE_LDLOC_0
, FALSE
);
5230 store_local (&ctx
, *ip
- CEE_STLOC_0
);
5235 code_bounds_check (2);
5236 store_local (&ctx
, ip
[1]);
5241 code_bounds_check (2);
5242 store_arg (&ctx
, ip
[1]);
5256 if (check_overflow (&ctx
))
5257 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5262 code_bounds_check (2);
5263 if (check_overflow (&ctx
))
5264 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5269 code_bounds_check (5);
5270 if (check_overflow (&ctx
))
5271 stack_push_val (&ctx
,TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
5276 code_bounds_check (9);
5277 if (check_overflow (&ctx
))
5278 stack_push_val (&ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
5283 code_bounds_check (5);
5284 if (check_overflow (&ctx
))
5285 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5290 code_bounds_check (9);
5291 if (check_overflow (&ctx
))
5292 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
5297 if (check_overflow (&ctx
))
5298 stack_push_val (&ctx
, TYPE_COMPLEX
| NULL_LITERAL_MASK
, &mono_defaults
.object_class
->byval_arg
);
5304 code_bounds_check (2);
5305 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_eq_op
);
5318 code_bounds_check (2);
5319 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_op
);
5326 code_bounds_check (5);
5327 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_eq_op
);
5340 code_bounds_check (5);
5341 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_op
);
5348 code_bounds_check (2);
5349 push_local (&ctx
, ip
[1], *ip
== CEE_LDLOCA_S
);
5354 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
5360 if (!check_underflow (&ctx
, 1))
5362 if (!check_overflow (&ctx
))
5364 top
= stack_push (&ctx
);
5365 copy_stack_value (top
, stack_peek (&ctx
, 1));
5371 code_bounds_check (5);
5373 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset
));
5374 /* token = read32 (ip + 1); */
5375 if (in_any_block (ctx
.header
, ip_offset
))
5376 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset
));
5378 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx
.ip_offset
));
5380 * FIXME: check signature, retval, arguments etc.
5386 code_bounds_check (5);
5387 do_invoke_method (&ctx
, read32 (ip
+ 1), *ip
== CEE_CALLVIRT
);
5392 code_bounds_check (5);
5393 /* token = read32 (ip + 1); */
5395 * FIXME: check signature, retval, arguments etc.
5396 * FIXME: check requirements for tail call
5398 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx
.ip_offset
));
5402 code_bounds_check (2);
5403 do_static_branch (&ctx
, (signed char)ip
[1] + 2);
5411 code_bounds_check (2);
5412 do_boolean_branch_op (&ctx
, (signed char)ip
[1] + 2);
5418 code_bounds_check (5);
5419 do_static_branch (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5427 code_bounds_check (5);
5428 do_boolean_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5435 code_bounds_check (5);
5436 entries
= read32 (ip
+ 1);
5438 if (entries
> 0xFFFFFFFFU
/ sizeof (guint32
))
5439 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries
, ctx
.ip_offset
));
5442 code_bounds_check (sizeof (guint32
) * entries
);
5444 do_switch (&ctx
, entries
, ip
);
5445 ip
+= sizeof (guint32
) * entries
;
5459 do_load_indirect (&ctx
, *ip
);
5471 do_store_indirect (&ctx
, *ip
);
5477 do_unary_math_op (&ctx
, *ip
);
5487 do_conversion (&ctx
, TYPE_I4
);
5493 do_conversion (&ctx
, TYPE_I8
);
5500 do_conversion (&ctx
, TYPE_R8
);
5506 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5511 code_bounds_check (5);
5512 do_cpobj (&ctx
, read32 (ip
+ 1));
5517 code_bounds_check (5);
5518 do_ldobj_value (&ctx
, read32 (ip
+ 1));
5523 code_bounds_check (5);
5524 do_ldstr (&ctx
, read32 (ip
+ 1));
5529 code_bounds_check (5);
5530 do_newobj (&ctx
, read32 (ip
+ 1));
5536 code_bounds_check (5);
5537 do_cast (&ctx
, read32 (ip
+ 1), *ip
== CEE_CASTCLASS
? "castclass" : "isinst");
5543 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
5548 code_bounds_check (5);
5549 do_unbox_value (&ctx
, read32 (ip
+ 1));
5561 code_bounds_check (5);
5562 do_push_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDFLDA
);
5568 code_bounds_check (5);
5569 do_push_static_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDSFLDA
);
5574 code_bounds_check (5);
5575 do_store_field (&ctx
, read32 (ip
+ 1));
5580 code_bounds_check (5);
5581 do_store_static_field (&ctx
, read32 (ip
+ 1));
5586 code_bounds_check (5);
5587 do_stobj (&ctx
, read32 (ip
+ 1));
5591 case CEE_CONV_OVF_I1_UN
:
5592 case CEE_CONV_OVF_I2_UN
:
5593 case CEE_CONV_OVF_I4_UN
:
5594 case CEE_CONV_OVF_U1_UN
:
5595 case CEE_CONV_OVF_U2_UN
:
5596 case CEE_CONV_OVF_U4_UN
:
5597 do_conversion (&ctx
, TYPE_I4
);
5601 case CEE_CONV_OVF_I8_UN
:
5602 case CEE_CONV_OVF_U8_UN
:
5603 do_conversion (&ctx
, TYPE_I8
);
5607 case CEE_CONV_OVF_I_UN
:
5608 case CEE_CONV_OVF_U_UN
:
5609 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5614 code_bounds_check (5);
5615 do_box_value (&ctx
, read32 (ip
+ 1));
5620 code_bounds_check (5);
5621 do_newarr (&ctx
, read32 (ip
+ 1));
5631 code_bounds_check (5);
5632 do_ldelema (&ctx
, read32 (ip
+ 1));
5646 case CEE_LDELEM_REF
:
5647 do_ldelem (&ctx
, *ip
, 0);
5658 case CEE_STELEM_REF
:
5659 do_stelem (&ctx
, *ip
, 0);
5664 code_bounds_check (5);
5665 do_ldelem (&ctx
, *ip
, read32 (ip
+ 1));
5670 code_bounds_check (5);
5671 do_stelem (&ctx
, *ip
, read32 (ip
+ 1));
5676 code_bounds_check (5);
5677 do_unbox_any (&ctx
, read32 (ip
+ 1));
5681 case CEE_CONV_OVF_I1
:
5682 case CEE_CONV_OVF_U1
:
5683 case CEE_CONV_OVF_I2
:
5684 case CEE_CONV_OVF_U2
:
5685 case CEE_CONV_OVF_I4
:
5686 case CEE_CONV_OVF_U4
:
5687 do_conversion (&ctx
, TYPE_I4
);
5691 case CEE_CONV_OVF_I8
:
5692 case CEE_CONV_OVF_U8
:
5693 do_conversion (&ctx
, TYPE_I8
);
5697 case CEE_CONV_OVF_I
:
5698 case CEE_CONV_OVF_U
:
5699 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5704 code_bounds_check (5);
5705 do_refanyval (&ctx
, read32 (ip
+ 1));
5715 code_bounds_check (5);
5716 do_mkrefany (&ctx
, read32 (ip
+ 1));
5721 code_bounds_check (5);
5722 do_load_token (&ctx
, read32 (ip
+ 1));
5726 case CEE_ENDFINALLY
:
5727 if (!is_correct_endfinally (ctx
.header
, ip_offset
))
5728 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx
.ip_offset
));
5735 code_bounds_check (5);
5736 do_leave (&ctx
, read32 (ip
+ 1) + 5);
5743 code_bounds_check (2);
5744 do_leave (&ctx
, (signed char)ip
[1] + 2);
5751 code_bounds_check (2);
5755 code_bounds_check (3);
5756 store_local (&ctx
, read16 (ip
+ 1));
5761 do_cmp_op (&ctx
, cmp_br_eq_op
, *ip
);
5769 do_cmp_op (&ctx
, cmp_br_op
, *ip
);
5774 code_bounds_check (3);
5775 store_arg (&ctx
, read16 (ip
+ 1) );
5781 if (!check_overflow (&ctx
))
5783 if (ctx
.signature
->call_convention
!= MONO_CALL_VARARG
)
5784 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx
.ip_offset
));
5785 set_stack_value (&ctx
, stack_push (&ctx
), &mono_defaults
.argumenthandle_class
->byval_arg
, FALSE
);
5790 code_bounds_check (5);
5791 do_load_function_ptr (&ctx
, read32 (ip
+ 1), FALSE
);
5796 code_bounds_check (5);
5797 do_load_function_ptr (&ctx
, read32 (ip
+ 1), TRUE
);
5803 code_bounds_check (3);
5804 push_arg (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDARGA
);
5810 code_bounds_check (3);
5811 push_local (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDLOCA
);
5824 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
5828 do_endfilter (&ctx
);
5832 case CEE_UNALIGNED_
:
5833 code_bounds_check (2);
5834 prefix
|= PREFIX_UNALIGNED
;
5838 prefix
|= PREFIX_VOLATILE
;
5842 prefix
|= PREFIX_TAIL
;
5844 if (ip
< end
&& (*ip
!= CEE_CALL
&& *ip
!= CEE_CALLI
&& *ip
!= CEE_CALLVIRT
))
5845 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset
));
5849 code_bounds_check (5);
5850 do_initobj (&ctx
, read32 (ip
+ 1));
5854 case CEE_CONSTRAINED_
:
5855 code_bounds_check (5);
5856 ctx
.constrained_type
= get_boxable_mono_type (&ctx
, read32 (ip
+ 1), "constrained.");
5857 prefix
|= PREFIX_CONSTRAINED
;
5862 prefix
|= PREFIX_READONLY
;
5867 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5868 if (!check_underflow (&ctx
, 3))
5870 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5875 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5876 if (!check_underflow (&ctx
, 3))
5878 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5886 if (!is_correct_rethrow (ctx
.header
, ip_offset
))
5887 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx
.ip_offset
));
5894 code_bounds_check (5);
5895 do_sizeof (&ctx
, read32 (ip
+ 1));
5899 case CEE_REFANYTYPE
:
5900 do_refanytype (&ctx
);
5905 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip
, ctx
.ip_offset
));
5911 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ctx
.ip_offset
));
5915 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5917 if (!ctx
.prefix_set
) //first prefix
5918 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5919 ctx
.prefix_set
|= prefix
;
5920 ctx
.has_flags
= TRUE
;
5924 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5926 if (ctx
.prefix_set
& PREFIX_CONSTRAINED
)
5927 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx
.ip_offset
));
5928 if (ctx
.prefix_set
& PREFIX_READONLY
)
5929 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx
.ip_offset
));
5930 if (ctx
.prefix_set
& PREFIX_VOLATILE
)
5931 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx
.ip_offset
));
5932 if (ctx
.prefix_set
& PREFIX_UNALIGNED
)
5933 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx
.ip_offset
));
5934 ctx
.prefix_set
= prefix
= 0;
5935 ctx
.has_flags
= FALSE
;
5939 * if ip != end we overflowed: mark as error.
5941 if ((ip
!= end
|| !start
) && ctx
.verifiable
&& !ctx
.list
) {
5942 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset
));
5945 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5946 for (i
= 0; i
< ctx
.code_size
&& i
< ip_offset
; ++i
) {
5947 if (ctx
.code
[i
].flags
& IL_CODE_FLAG_WAS_TARGET
) {
5948 if (!(ctx
.code
[i
].flags
& IL_CODE_FLAG_SEEN
))
5949 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i
));
5951 if (ctx
.code
[i
].flags
& IL_CODE_DELEGATE_SEQUENCE
)
5952 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i
));
5954 if ((ctx
.code
[i
].flags
& IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5955 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i
));
5957 if ((ctx
.code
[i
].flags
& IL_CODE_CALL_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5958 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
));
5961 if (mono_method_is_constructor (ctx
.method
) && !ctx
.super_ctor_called
&& !ctx
.method
->klass
->valuetype
&& ctx
.method
->klass
!= mono_defaults
.object_class
) {
5962 char *method_name
= mono_method_full_name (ctx
.method
, TRUE
);
5963 char *type
= mono_type_get_full_name (ctx
.method
->klass
);
5964 if (ctx
.method
->klass
->parent
&& mono_class_has_failure (ctx
.method
->klass
->parent
))
5965 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
));
5967 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name
, type
));
5968 g_free (method_name
);
5974 for (i
= 0; i
< ctx
.header
->code_size
; ++i
) {
5975 if (ctx
.code
[i
].stack
)
5976 g_free (ctx
.code
[i
].stack
);
5980 for (tmp
= ctx
.funptrs
; tmp
; tmp
= tmp
->next
)
5982 g_slist_free (ctx
.funptrs
);
5984 for (tmp
= ctx
.exception_types
; tmp
; tmp
= tmp
->next
)
5985 mono_metadata_free_type ((MonoType
*)tmp
->data
);
5986 g_slist_free (ctx
.exception_types
);
5988 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
5990 mono_metadata_free_type (ctx
.locals
[i
]);
5992 for (i
= 0; i
< ctx
.max_args
; ++i
) {
5994 mono_metadata_free_type (ctx
.params
[i
]);
5998 g_free (ctx
.eval
.stack
);
6001 g_free (ctx
.locals
);
6002 g_free (ctx
.params
);
6003 mono_basic_block_free (original_bb
);
6004 mono_metadata_free_mh (ctx
.header
);
6006 finish_collect_stats ();
6011 mono_verify_corlib ()
6013 /* This is a public API function so cannot be removed */
6018 * mono_verifier_is_enabled_for_method:
6019 * @method: the method to probe
6021 * Returns TRUE if @method needs to be verified.
6025 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
6027 return mono_verifier_is_enabled_for_class (method
->klass
) && (method
->wrapper_type
== MONO_WRAPPER_NONE
|| method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
);
6031 * mono_verifier_is_enabled_for_class:
6032 * @klass: The `MonoClass` to probe
6034 * Returns TRUE if @klass need to be verified.
6038 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
6040 return verify_all
|| (verifier_mode
> MONO_VERIFIER_MODE_OFF
&& !(klass
->image
->assembly
&& klass
->image
->assembly
->in_gac
) && klass
->image
!= mono_defaults
.corlib
);
6044 mono_verifier_is_enabled_for_image (MonoImage
*image
)
6046 return verify_all
|| verifier_mode
> MONO_VERIFIER_MODE_OFF
;
6050 * Dynamic methods are not considered full trust since if the user is trusted and need to
6051 * generate unsafe code, make the method skip verification - this is a known good way to do it.
6054 mono_verifier_is_method_full_trust (MonoMethod
*method
)
6056 return mono_verifier_is_class_full_trust (method
->klass
) && !method_is_dynamic (method
);
6060 * Returns if @klass is under full trust or not.
6062 * TODO This code doesn't take CAS into account.
6064 * Under verify_all all user code must be verifiable if no security option was set
6068 mono_verifier_is_class_full_trust (MonoClass
*klass
)
6070 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
6071 gboolean trusted_location
= !mono_security_core_clr_enabled () ?
6072 (klass
->image
->assembly
&& klass
->image
->assembly
->in_gac
) : mono_security_core_clr_is_platform_image (klass
->image
);
6074 if (verify_all
&& verifier_mode
== MONO_VERIFIER_MODE_OFF
)
6075 return trusted_location
|| klass
->image
== mono_defaults
.corlib
;
6076 return verifier_mode
< MONO_VERIFIER_MODE_VERIFIABLE
|| trusted_location
|| klass
->image
== mono_defaults
.corlib
;
6080 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
, gboolean is_fulltrust
)
6082 return mono_method_verify (method
,
6083 (verifier_mode
!= MONO_VERIFIER_MODE_STRICT
? MONO_VERIFY_NON_STRICT
: 0)
6084 | (!is_fulltrust
&& !mono_verifier_is_method_full_trust (method
) ? MONO_VERIFY_FAIL_FAST
: 0)
6085 | (skip_visibility
? MONO_VERIFY_SKIP_VISIBILITY
: 0));
6089 get_field_end (MonoClassField
*field
)
6092 int size
= mono_type_size (field
->type
, &align
);
6094 size
= 4; /*FIXME Is this a safe bet?*/
6095 return size
+ field
->offset
;
6099 verify_class_for_overlapping_reference_fields (MonoClass
*klass
)
6102 gpointer iter
= NULL
;
6103 MonoClassField
*field
;
6104 gboolean is_fulltrust
= mono_verifier_is_class_full_trust (klass
);
6105 /*We can't skip types with !has_references since this is calculated after we have run.*/
6106 if (!mono_class_is_explicit_layout (klass
))
6110 /*We must check for stuff overlapping reference fields.
6111 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
6113 while ((field
= mono_class_get_fields (klass
, &iter
))) {
6114 int fieldEnd
= get_field_end (field
);
6115 gboolean is_valuetype
= !MONO_TYPE_IS_REFERENCE (field
->type
);
6118 if (mono_field_is_deleted (field
) || (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6121 int fcount
= mono_class_get_field_count (klass
);
6122 for (j
= i
; j
< fcount
; ++j
) {
6123 MonoClassField
*other
= &klass
->fields
[j
];
6124 int otherEnd
= get_field_end (other
);
6125 if (mono_field_is_deleted (other
) || (is_valuetype
&& !MONO_TYPE_IS_REFERENCE (other
->type
)) || (other
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
6128 if (!is_valuetype
&& MONO_TYPE_IS_REFERENCE (other
->type
) && field
->offset
== other
->offset
&& is_fulltrust
)
6131 if ((otherEnd
> field
->offset
&& otherEnd
<= fieldEnd
) || (other
->offset
>= field
->offset
&& other
->offset
< fieldEnd
))
6139 field_hash (gconstpointer key
)
6141 const MonoClassField
*field
= (const MonoClassField
*)key
;
6142 return g_str_hash (field
->name
) ^ mono_metadata_type_hash (field
->type
); /**/
6146 field_equals (gconstpointer _a
, gconstpointer _b
)
6148 const MonoClassField
*a
= (const MonoClassField
*)_a
;
6149 const MonoClassField
*b
= (const MonoClassField
*)_b
;
6150 return !strcmp (a
->name
, b
->name
) && mono_metadata_type_equal (a
->type
, b
->type
);
6155 verify_class_fields (MonoClass
*klass
)
6157 gpointer iter
= NULL
;
6158 MonoClassField
*field
;
6159 MonoGenericContext
*context
= mono_class_get_context (klass
);
6160 GHashTable
*unique_fields
= g_hash_table_new_full (&field_hash
, &field_equals
, NULL
, NULL
);
6161 if (mono_class_is_gtd (klass
))
6162 context
= &mono_class_get_generic_container (klass
)->context
;
6164 while ((field
= mono_class_get_fields (klass
, &iter
)) != NULL
) {
6165 if (!mono_type_is_valid_type_in_context (field
->type
, context
)) {
6166 g_hash_table_destroy (unique_fields
);
6169 if (g_hash_table_lookup (unique_fields
, field
)) {
6170 g_hash_table_destroy (unique_fields
);
6173 g_hash_table_insert (unique_fields
, field
, field
);
6175 g_hash_table_destroy (unique_fields
);
6180 verify_interfaces (MonoClass
*klass
)
6183 for (i
= 0; i
< klass
->interface_count
; ++i
) {
6184 MonoClass
*iface
= klass
->interfaces
[i
];
6185 if (!mono_class_get_flags (iface
))
6192 verify_valuetype_layout_with_target (MonoClass
*klass
, MonoClass
*target_class
)
6195 gpointer iter
= NULL
;
6196 MonoClassField
*field
;
6197 MonoClass
*field_class
;
6199 if (!klass
->valuetype
)
6202 type
= klass
->byval_arg
.type
;
6203 /*primitive type fields are not properly decoded*/
6204 if ((type
>= MONO_TYPE_BOOLEAN
&& type
<= MONO_TYPE_R8
) || (type
>= MONO_TYPE_I
&& type
<= MONO_TYPE_U
))
6207 while ((field
= mono_class_get_fields (klass
, &iter
)) != NULL
) {
6211 if (field
->type
->attrs
& (FIELD_ATTRIBUTE_STATIC
| FIELD_ATTRIBUTE_HAS_FIELD_RVA
))
6214 field_class
= mono_class_get_generic_type_definition (mono_class_from_mono_type (field
->type
));
6216 if (field_class
== target_class
|| klass
== field_class
|| !verify_valuetype_layout_with_target (field_class
, target_class
))
6224 verify_valuetype_layout (MonoClass
*klass
)
6227 res
= verify_valuetype_layout_with_target (klass
, klass
);
6232 recursive_mark_constraint_args (MonoBitSet
*used_args
, MonoGenericContainer
*gc
, MonoType
*type
)
6235 MonoClass
**constraints
;
6236 MonoGenericParamInfo
*param_info
;
6238 g_assert (mono_type_is_generic_argument (type
));
6240 idx
= mono_type_get_generic_param_num (type
);
6241 if (mono_bitset_test_fast (used_args
, idx
))
6244 mono_bitset_set_fast (used_args
, idx
);
6245 param_info
= mono_generic_container_get_param_info (gc
, idx
);
6247 if (!param_info
->constraints
)
6250 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
6251 MonoClass
*ctr
= *constraints
;
6252 MonoType
*constraint_type
= &ctr
->byval_arg
;
6254 if (mono_type_is_generic_argument (constraint_type
) && !recursive_mark_constraint_args (used_args
, gc
, constraint_type
))
6261 verify_generic_parameters (MonoClass
*klass
)
6264 MonoGenericContainer
*gc
= mono_class_get_generic_container (klass
);
6265 MonoBitSet
*used_args
= mono_bitset_new (gc
->type_argc
, 0);
6267 for (i
= 0; i
< gc
->type_argc
; ++i
) {
6268 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
6269 MonoClass
**constraints
;
6271 if (!param_info
->constraints
)
6274 mono_bitset_clear_all (used_args
);
6275 mono_bitset_set_fast (used_args
, i
);
6277 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
6278 MonoClass
*ctr
= *constraints
;
6279 MonoType
*constraint_type
= &ctr
->byval_arg
;
6281 if (!mono_class_can_access_class (klass
, ctr
))
6284 if (!mono_type_is_valid_type_in_context (constraint_type
, &gc
->context
))
6287 if (mono_type_is_generic_argument (constraint_type
) && !recursive_mark_constraint_args (used_args
, gc
, constraint_type
))
6289 if (mono_class_is_ginst (ctr
) && !mono_class_is_valid_generic_instantiation (NULL
, ctr
))
6293 mono_bitset_free (used_args
);
6297 mono_bitset_free (used_args
);
6302 * Check if the class is verifiable.
6304 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6305 * field and invalid generic instantiation are fatal errors.
6307 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6311 mono_verifier_verify_class (MonoClass
*klass
)
6313 /*Neither <Module>, object or ifaces have parent.*/
6314 if (!klass
->parent
&&
6315 klass
!= mono_defaults
.object_class
&&
6316 !MONO_CLASS_IS_INTERFACE (klass
) &&
6317 (!image_is_dynamic (klass
->image
) && klass
->type_token
!= 0x2000001)) /*<Module> is the first type in the assembly*/
6319 if (klass
->parent
) {
6320 if (MONO_CLASS_IS_INTERFACE (klass
->parent
))
6322 if (!mono_class_is_ginst (klass
) && mono_class_is_gtd (klass
->parent
))
6324 if (mono_class_is_ginst (klass
->parent
) && !mono_class_is_ginst (klass
)) {
6325 MonoGenericContext
*context
= mono_class_get_context (klass
);
6326 if (mono_class_is_gtd (klass
))
6327 context
= &mono_class_get_generic_container (klass
)->context
;
6328 if (!mono_type_is_valid_type_in_context (&klass
->parent
->byval_arg
, context
))
6332 if (mono_class_is_gtd (klass
) && (mono_class_is_explicit_layout (klass
)))
6334 if (mono_class_is_gtd (klass
) && !verify_generic_parameters (klass
))
6336 if (!verify_class_for_overlapping_reference_fields (klass
))
6338 if (mono_class_is_ginst (klass
) && !mono_class_is_valid_generic_instantiation (NULL
, klass
))
6340 if (!mono_class_is_ginst (klass
) && !verify_class_fields (klass
))
6342 if (klass
->valuetype
&& !verify_valuetype_layout (klass
))
6344 if (!verify_interfaces (klass
))
6350 mono_verifier_class_is_valid_generic_instantiation (MonoClass
*klass
)
6352 return mono_class_is_valid_generic_instantiation (NULL
, klass
);
6356 mono_verifier_is_method_valid_generic_instantiation (MonoMethod
*method
)
6358 if (!method
->is_inflated
)
6360 return mono_method_is_valid_generic_instantiation (NULL
, method
);
6366 mono_verifier_verify_class (MonoClass
*klass
)
6368 /* The verifier was disabled at compile time */
6373 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
, gboolean is_fulltrust
)
6375 /* The verifier was disabled at compile time */
6380 mono_verifier_is_class_full_trust (MonoClass
*klass
)
6382 /* The verifier was disabled at compile time */
6387 mono_verifier_is_method_full_trust (MonoMethod
*method
)
6389 /* The verifier was disabled at compile time */
6394 mono_verifier_is_enabled_for_image (MonoImage
*image
)
6396 /* The verifier was disabled at compile time */
6401 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
6403 /* The verifier was disabled at compile time */
6408 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
6410 /* The verifier was disabled at compile time */
6415 mono_method_verify (MonoMethod
*method
, int level
)
6417 /* The verifier was disabled at compile time */
6422 mono_free_verify_list (GSList
*list
)
6424 /* The verifier was disabled at compile time */
6425 /* will always be null if verifier is disabled */
6429 mono_verifier_class_is_valid_generic_instantiation (MonoClass
*klass
)
6435 mono_verifier_is_method_valid_generic_instantiation (MonoMethod
*method
)