5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
12 #include <mono/metadata/object-internals.h>
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/verify-internals.h>
15 #include <mono/metadata/opcodes.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/reflection.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/metadata.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/class-internals.h>
23 #include <mono/metadata/security-manager.h>
24 #include <mono/metadata/security-core-clr.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/mono-basic-block.h>
27 #include <mono/utils/mono-counters.h>
28 #include <mono/utils/monobitset.h>
34 static MiniVerifierMode verifier_mode
= MONO_VERIFIER_MODE_OFF
;
35 static gboolean verify_all
= FALSE
;
38 * Set the desired level of checks for the verfier.
42 mono_verifier_set_mode (MiniVerifierMode mode
)
48 mono_verifier_enable_verify_all ()
53 #ifndef DISABLE_VERIFIER
55 * Pull the list of opcodes
57 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
61 #include "mono/cil/opcode.def"
66 #ifdef MONO_VERIFIER_DEBUG
67 #define VERIFIER_DEBUG(code) do { code } while (0)
69 #define VERIFIER_DEBUG(code)
72 //////////////////////////////////////////////////////////////////
73 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
74 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
75 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
76 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
77 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
78 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
80 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
81 vinfo->info.status = __status; \
82 vinfo->info.message = ( __msg ); \
83 vinfo->exception_type = (__exception); \
84 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
87 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
88 #define ADD_VERIFY_ERROR(__ctx, __msg) \
90 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
94 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
96 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
97 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
98 (__ctx)->verifiable = 0; \
99 if (IS_FAIL_FAST_MODE (__ctx)) \
100 (__ctx)->valid = 0; \
104 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
106 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
107 (__ctx)->valid = 0; \
110 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
112 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
113 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
114 (__ctx)->verifiable = 0; \
115 if (IS_FAIL_FAST_MODE (__ctx)) \
116 (__ctx)->valid = 0; \
120 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
121 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
123 #if SIZEOF_VOID_P == 4
124 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
126 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
129 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
130 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
132 /*Flags to be used with ILCodeDesc::flags */
134 /*Instruction has not been processed.*/
135 IL_CODE_FLAG_NOT_PROCESSED
= 0,
136 /*Instruction was decoded by mono_method_verify loop.*/
137 IL_CODE_FLAG_SEEN
= 1,
138 /*Instruction was target of a branch or is at a protected block boundary.*/
139 IL_CODE_FLAG_WAS_TARGET
= 2,
140 /*Used by stack_init to avoid double initialize each entry.*/
141 IL_CODE_FLAG_STACK_INITED
= 4,
142 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
143 IL_CODE_STACK_MERGED
= 8,
144 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
145 IL_CODE_DELEGATE_SEQUENCE
= 0x10,
146 /*This is a delegate created from a ldftn to a non final virtual method*/
147 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
= 0x20,
148 /*This is a call to a non final virtual method*/
149 IL_CODE_CALL_NONFINAL_VIRTUAL
= 0x40,
167 guint16 size
, max_size
;
184 /*Allocated fnptr MonoType that should be freed by us.*/
186 /*Type dup'ed exception types from catch blocks.*/
187 GSList
*exception_types
;
192 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
196 MonoMethodSignature
*signature
;
197 MonoMethodHeader
*header
;
199 MonoGenericContext
*generic_context
;
203 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
204 *on a method that creates a delegate for a non-final virtual method using ldftn*/
205 gboolean has_this_store
;
207 /*This flag is used to control if the contructor of the parent class has been called.
208 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
209 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
211 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
212 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
214 gboolean super_ctor_called
;
218 MonoType
*constrained_type
;
222 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
);
225 get_stack_type (MonoType
*type
);
228 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
);
231 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
);
234 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
);
235 //////////////////////////////////////////////////////////////////
240 TYPE_INV
= 0, /* leave at 0. */
245 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
247 /* value types and classes */
249 /* Number of types, used to define the size of the tables*/
252 /* Used by tables to signal that a result is not verifiable*/
253 NON_VERIFIABLE_RESULT
= 0x80,
255 /*Mask used to extract just the type, excluding flags */
258 /* The stack type is a managed pointer, unmask the value to res */
259 POINTER_MASK
= 0x100,
261 /*Stack type with the pointer mask*/
262 RAW_TYPE_MASK
= 0x10F,
264 /* Controlled Mutability Manager Pointer */
267 /* The stack type is a null literal*/
268 NULL_LITERAL_MASK
= 0x400,
270 /**Used by ldarg.0 and family to let delegate verification happens.*/
271 THIS_POINTER_MASK
= 0x800,
273 /**Signals that this is a boxed value type*/
276 /*This is an unitialized this ref*/
277 UNINIT_THIS_MASK
= 0x2000,
280 static const char* const
281 type_names
[TYPE_MAX
+ 1] = {
292 PREFIX_UNALIGNED
= 1,
295 PREFIX_CONSTRAINED
= 8,
298 //////////////////////////////////////////////////////////////////
300 #ifdef ENABLE_VERIFIER_STATS
302 #define MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
303 #define MEM_FREE(amt) do { working_set -= (amt); } while (0)
305 static int allocated_memory
;
306 static int working_set
;
307 static int max_allocated_memory
;
308 static int max_working_set
;
309 static int total_allocated_memory
;
312 finish_collect_stats (void)
314 max_allocated_memory
= MAX (max_allocated_memory
, allocated_memory
);
315 max_working_set
= MAX (max_working_set
, working_set
);
316 total_allocated_memory
+= allocated_memory
;
317 allocated_memory
= working_set
= 0;
321 init_verifier_stats (void)
323 static gboolean inited
;
326 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &max_allocated_memory
);
327 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &max_working_set
);
328 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &total_allocated_memory
);
334 #define MEM_ALLOC(amt) do {} while (0)
335 #define MEM_FREE(amt) do { } while (0)
337 #define finish_collect_stats()
338 #define init_verifier_stats()
343 //////////////////////////////////////////////////////////////////
346 /*Token validation macros and functions */
347 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
348 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
349 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
350 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
352 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
353 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
354 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
355 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
356 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
357 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
360 * Verify if @token refers to a valid row on int's table.
363 token_bounds_check (MonoImage
*image
, guint32 token
)
366 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage
*)image
, token
);
367 return image
->tables
[mono_metadata_token_table (token
)].rows
>= mono_metadata_token_index (token
);
371 mono_type_create_fnptr_from_mono_method (VerifyContext
*ctx
, MonoMethod
*method
)
373 MonoType
*res
= g_new0 (MonoType
, 1);
374 MEM_ALLOC (sizeof (MonoType
));
376 //FIXME use mono_method_get_signature_full
377 res
->data
.method
= mono_method_signature (method
);
378 res
->type
= MONO_TYPE_FNPTR
;
379 ctx
->funptrs
= g_slist_prepend (ctx
->funptrs
, res
);
384 * mono_type_is_enum_type:
386 * Returns TRUE if @type is an enum type.
389 mono_type_is_enum_type (MonoType
*type
)
391 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
393 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
399 * mono_type_is_value_type:
401 * Returns TRUE if @type is named after @namespace.@name.
405 mono_type_is_value_type (MonoType
*type
, const char *namespace, const char *name
)
407 return type
->type
== MONO_TYPE_VALUETYPE
&&
408 !strcmp (namespace, type
->data
.klass
->name_space
) &&
409 !strcmp (name
, type
->data
.klass
->name
);
413 * Returns TURE if @type is VAR or MVAR
416 mono_type_is_generic_argument (MonoType
*type
)
418 return type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
;
422 * mono_type_get_underlying_type_any:
424 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
426 * Returns the underlying type of @type regardless if it is byref or not.
429 mono_type_get_underlying_type_any (MonoType
*type
)
431 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
432 return mono_class_enum_basetype (type
->data
.klass
);
433 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
434 return mono_class_enum_basetype (type
->data
.generic_class
->container_class
);
438 static G_GNUC_UNUSED
const char*
439 mono_type_get_stack_name (MonoType
*type
)
441 return type_names
[get_stack_type (type
) & TYPE_MASK
];
444 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
445 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
448 mono_method_is_constructor (MonoMethod
*method
)
450 return ((method
->flags
& CTOR_REQUIRED_FLAGS
) == CTOR_REQUIRED_FLAGS
&&
451 !(method
->flags
& CTOR_INVALID_FLAGS
) &&
452 !strcmp (".ctor", method
->name
));
456 mono_class_has_default_constructor (MonoClass
*klass
)
461 mono_class_setup_methods (klass
);
462 if (klass
->exception_type
)
465 for (i
= 0; i
< klass
->method
.count
; ++i
) {
466 method
= klass
->methods
[i
];
467 if (mono_method_is_constructor (method
) &&
468 mono_method_signature (method
) &&
469 mono_method_signature (method
)->param_count
== 0 &&
470 (method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
)
477 * Verify if @type is valid for the given @ctx verification context.
478 * this function checks for VAR and MVAR types that are invalid under the current verifier,
481 mono_type_is_valid_type_in_context (MonoType
*type
, MonoGenericContext
*context
)
484 MonoGenericInst
*inst
;
486 switch (type
->type
) {
491 inst
= type
->type
== MONO_TYPE_VAR
? context
->class_inst
: context
->method_inst
;
492 if (!inst
|| mono_type_get_generic_param_num (type
) >= inst
->type_argc
)
495 case MONO_TYPE_SZARRAY
:
496 return mono_type_is_valid_type_in_context (&type
->data
.klass
->byval_arg
, context
);
497 case MONO_TYPE_ARRAY
:
498 return mono_type_is_valid_type_in_context (&type
->data
.array
->eklass
->byval_arg
, context
);
500 return mono_type_is_valid_type_in_context (type
->data
.type
, context
);
501 case MONO_TYPE_GENERICINST
:
502 inst
= type
->data
.generic_class
->context
.class_inst
;
505 for (i
= 0; i
< inst
->type_argc
; ++i
)
506 if (!mono_type_is_valid_type_in_context (inst
->type_argv
[i
], context
))
513 /*This function returns NULL if the type is not instantiatable*/
515 verifier_inflate_type (VerifyContext
*ctx
, MonoType
*type
, MonoGenericContext
*context
)
520 result
= mono_class_inflate_generic_type_checked (type
, context
, &error
);
521 if (!mono_error_ok (&error
)) {
522 mono_error_cleanup (&error
);
530 is_valid_generic_instantiation (MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
535 if (ginst
->type_argc
!= gc
->type_argc
)
538 for (i
= 0; i
< gc
->type_argc
; ++i
) {
539 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
540 MonoClass
*paramClass
;
541 MonoClass
**constraints
;
543 if (!param_info
->constraints
&& !(param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
))
545 if (mono_type_is_generic_argument (ginst
->type_argv
[i
]))
546 continue; //it's not our job to validate type variables
548 paramClass
= mono_class_from_mono_type (ginst
->type_argv
[i
]);
550 if (paramClass
->exception_type
!= MONO_EXCEPTION_NONE
)
553 /*it's not safe to call mono_class_init from here*/
554 if (paramClass
->generic_class
&& !paramClass
->inited
) {
555 if (!mono_class_is_valid_generic_instantiation (NULL
, paramClass
))
559 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
) && (!paramClass
->valuetype
|| mono_class_is_nullable (paramClass
)))
562 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT
) && paramClass
->valuetype
)
565 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT
) && !paramClass
->valuetype
&& !mono_class_has_default_constructor (paramClass
))
568 if (!param_info
->constraints
)
571 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
572 MonoClass
*ctr
= *constraints
;
575 inflated
= mono_class_inflate_generic_type_checked (&ctr
->byval_arg
, context
, &error
);
576 if (!mono_error_ok (&error
)) {
577 mono_error_cleanup (&error
);
580 ctr
= mono_class_from_mono_type (inflated
);
581 mono_metadata_free_type (inflated
);
583 if (!mono_class_is_assignable_from_slow (ctr
, paramClass
))
591 * Return true if @candidate is constraint compatible with @target.
593 * This means that @candidate constraints are a super set of @target constaints
596 mono_generic_param_is_constraint_compatible (VerifyContext
*ctx
, MonoGenericParam
*target
, MonoGenericParam
*candidate
, MonoClass
*candidate_param_class
, MonoGenericContext
*context
)
598 MonoGenericParamInfo
*tinfo
= mono_generic_param_info (target
);
599 MonoGenericParamInfo
*cinfo
= mono_generic_param_info (candidate
);
601 int tmask
= tinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
602 int cmask
= cinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
603 if ((tmask
& cmask
) != tmask
)
606 if (tinfo
->constraints
) {
607 MonoClass
**target_class
, **candidate_class
;
608 for (target_class
= tinfo
->constraints
; *target_class
; ++target_class
) {
610 MonoType
*inflated
= verifier_inflate_type (ctx
, &(*target_class
)->byval_arg
, context
);
613 tc
= mono_class_from_mono_type (inflated
);
614 mono_metadata_free_type (inflated
);
617 * A constraint from @target might inflate into @candidate itself and in that case we don't need
618 * check it's constraints since it satisfy the constraint by itself.
620 if (mono_metadata_type_equal (&tc
->byval_arg
, &candidate_param_class
->byval_arg
))
623 if (!cinfo
->constraints
)
626 for (candidate_class
= cinfo
->constraints
; *candidate_class
; ++candidate_class
) {
628 inflated
= verifier_inflate_type (ctx
, &(*candidate_class
)->byval_arg
, ctx
->generic_context
);
631 cc
= mono_class_from_mono_type (inflated
);
632 mono_metadata_free_type (inflated
);
634 if (mono_class_is_assignable_from (tc
, cc
))
637 if (!*candidate_class
)
644 static MonoGenericParam
*
645 verifier_get_generic_param_from_type (VerifyContext
*ctx
, MonoType
*type
)
647 MonoGenericContainer
*gc
;
648 MonoMethod
*method
= ctx
->method
;
651 num
= mono_type_get_generic_param_num (type
);
653 if (type
->type
== MONO_TYPE_VAR
) {
654 MonoClass
*gtd
= method
->klass
;
655 if (gtd
->generic_class
)
656 gtd
= gtd
->generic_class
->container_class
;
657 gc
= gtd
->generic_container
;
659 MonoMethod
*gmd
= method
;
660 if (method
->is_inflated
)
661 gmd
= ((MonoMethodInflated
*)method
)->declaring
;
662 gc
= mono_method_get_generic_container (gmd
);
666 return mono_generic_container_get_param (gc
, num
);
672 * Verify if @type is valid for the given @ctx verification context.
673 * this function checks for VAR and MVAR types that are invalid under the current verifier,
674 * This means that it either
677 is_valid_type_in_context (VerifyContext
*ctx
, MonoType
*type
)
679 return mono_type_is_valid_type_in_context (type
, ctx
->generic_context
);
683 is_valid_generic_instantiation_in_context (VerifyContext
*ctx
, MonoGenericInst
*ginst
)
686 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
687 MonoType
*type
= ginst
->type_argv
[i
];
688 if (!is_valid_type_in_context (ctx
, type
))
695 generic_arguments_respect_constraints (VerifyContext
*ctx
, MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
698 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
699 MonoType
*type
= ginst
->type_argv
[i
];
700 MonoGenericParam
*target
= mono_generic_container_get_param (gc
, i
);
701 MonoGenericParam
*candidate
;
702 MonoClass
*candidate_class
;
704 if (!mono_type_is_generic_argument (type
))
707 if (!is_valid_type_in_context (ctx
, type
))
710 candidate
= verifier_get_generic_param_from_type (ctx
, type
);
711 candidate_class
= mono_class_from_mono_type (type
);
713 if (!mono_generic_param_is_constraint_compatible (ctx
, target
, candidate
, candidate_class
, context
))
720 mono_method_repect_method_constraints (VerifyContext
*ctx
, MonoMethod
*method
)
722 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
723 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
724 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
725 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gmethod
->context
, ginst
);
729 mono_class_repect_method_constraints (VerifyContext
*ctx
, MonoClass
*klass
)
731 MonoGenericClass
*gklass
= klass
->generic_class
;
732 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
733 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
734 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gklass
->context
, ginst
);
738 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
)
740 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
741 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
742 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
743 if (!gc
) /*non-generic inflated method - it's part of a generic type */
745 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
747 return is_valid_generic_instantiation (gc
, &gmethod
->context
, ginst
);
752 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
)
754 MonoGenericClass
*gklass
= klass
->generic_class
;
755 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
756 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
757 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
759 return is_valid_generic_instantiation (gc
, &gklass
->context
, ginst
);
763 mono_type_is_valid_in_context (VerifyContext
*ctx
, MonoType
*type
)
768 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid null type at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
772 if (!is_valid_type_in_context (ctx
, type
)) {
773 char *str
= mono_type_full_name (type
);
774 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
775 type
->type
== MONO_TYPE_VAR
? "!" : "!!",
777 type
->type
== MONO_TYPE_VAR
? "class" : "method",
779 MONO_EXCEPTION_BAD_IMAGE
);
784 klass
= mono_class_from_mono_type (type
);
785 mono_class_init (klass
);
786 if (mono_loader_get_last_error () || klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
787 if (klass
->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, klass
))
788 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
);
790 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
);
794 if (klass
->generic_class
&& klass
->generic_class
->container_class
->exception_type
!= MONO_EXCEPTION_NONE
) {
795 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
);
799 if (!klass
->generic_class
)
802 if (!mono_class_is_valid_generic_instantiation (ctx
, klass
)) {
803 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
);
807 if (!mono_class_repect_method_constraints (ctx
, klass
)) {
808 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
);
815 static verify_result_t
816 mono_method_is_valid_in_context (VerifyContext
*ctx
, MonoMethod
*method
)
818 if (!mono_type_is_valid_in_context (ctx
, &method
->klass
->byval_arg
))
819 return RESULT_INVALID
;
821 if (!method
->is_inflated
)
824 if (!mono_method_is_valid_generic_instantiation (ctx
, method
)) {
825 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
);
826 return RESULT_INVALID
;
829 if (!mono_method_repect_method_constraints (ctx
, method
)) {
830 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
));
831 return RESULT_UNVERIFIABLE
;
837 static MonoClassField
*
838 verifier_load_field (VerifyContext
*ctx
, int token
, MonoClass
**out_klass
, const char *opcode
) {
839 MonoClassField
*field
;
840 MonoClass
*klass
= NULL
;
842 if (!IS_FIELD_DEF_OR_REF (token
) || !token_bounds_check (ctx
->image
, token
)) {
843 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
);
847 field
= mono_field_from_token (ctx
->image
, token
, &klass
, ctx
->generic_context
);
848 if (!field
|| !field
->parent
|| !klass
) {
849 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
);
853 if (!mono_type_is_valid_in_context (ctx
, &klass
->byval_arg
))
861 verifier_load_method (VerifyContext
*ctx
, int token
, const char *opcode
) {
864 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
865 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
);
869 method
= mono_get_method_full (ctx
->image
, token
, NULL
, ctx
->generic_context
);
872 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
);
876 if (mono_method_is_valid_in_context (ctx
, method
) == RESULT_INVALID
)
883 verifier_load_type (VerifyContext
*ctx
, int token
, const char *opcode
) {
886 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
887 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
891 type
= mono_type_get_full (ctx
->image
, token
, ctx
->generic_context
);
894 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
);
898 if (!mono_type_is_valid_in_context (ctx
, type
))
905 /* stack_slot_get_type:
907 * Returns the stack type of @value. This value includes POINTER_MASK.
909 * Use this function to checks that account for a managed pointer.
912 stack_slot_get_type (ILStackDesc
*value
)
914 return value
->stype
& RAW_TYPE_MASK
;
917 /* stack_slot_get_underlying_type:
919 * Returns the stack type of @value. This value does not include POINTER_MASK.
921 * Use this function is cases where the fact that the value could be a managed pointer is
922 * irrelevant. For example, field load doesn't care about this fact of type on stack.
925 stack_slot_get_underlying_type (ILStackDesc
*value
)
927 return value
->stype
& TYPE_MASK
;
930 /* stack_slot_is_managed_pointer:
932 * Returns TRUE is @value is a managed pointer.
935 stack_slot_is_managed_pointer (ILStackDesc
*value
)
937 return (value
->stype
& POINTER_MASK
) == POINTER_MASK
;
940 /* stack_slot_is_managed_mutability_pointer:
942 * Returns TRUE is @value is a managed mutability pointer.
944 static G_GNUC_UNUSED gboolean
945 stack_slot_is_managed_mutability_pointer (ILStackDesc
*value
)
947 return (value
->stype
& CMMP_MASK
) == CMMP_MASK
;
950 /* stack_slot_is_null_literal:
952 * Returns TRUE is @value is the null literal.
955 stack_slot_is_null_literal (ILStackDesc
*value
)
957 return (value
->stype
& NULL_LITERAL_MASK
) == NULL_LITERAL_MASK
;
961 /* stack_slot_is_this_pointer:
963 * Returns TRUE is @value is the this literal
966 stack_slot_is_this_pointer (ILStackDesc
*value
)
968 return (value
->stype
& THIS_POINTER_MASK
) == THIS_POINTER_MASK
;
971 /* stack_slot_is_boxed_value:
973 * Returns TRUE is @value is a boxed value
976 stack_slot_is_boxed_value (ILStackDesc
*value
)
978 return (value
->stype
& BOXED_MASK
) == BOXED_MASK
;
982 stack_slot_get_name (ILStackDesc
*value
)
984 return type_names
[value
->stype
& TYPE_MASK
];
987 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
988 if (PRED (value)) { \
990 g_string_append (str, ", "); \
991 g_string_append (str, NAME); \
996 stack_slot_stack_type_full_name (ILStackDesc
*value
)
998 GString
*str
= g_string_new ("");
1000 gboolean has_pred
= FALSE
, first
= TRUE
;
1002 if ((value
->stype
& TYPE_MASK
) != value
->stype
) {
1003 g_string_append(str
, "[");
1004 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer
, "this");
1005 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value
, "boxed");
1006 APPEND_WITH_PREDICATE (stack_slot_is_null_literal
, "null");
1007 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer
, "cmmp");
1008 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer
, "mp");
1012 if (mono_type_is_generic_argument (value
->type
) && !stack_slot_is_boxed_value (value
)) {
1014 g_string_append(str
, "[");
1016 g_string_append (str
, ", ");
1017 g_string_append (str
, "unboxed");
1022 g_string_append(str
, "] ");
1024 g_string_append (str
, stack_slot_get_name (value
));
1026 g_string_free (str
, FALSE
);
1031 stack_slot_full_name (ILStackDesc
*value
)
1033 char *type_name
= mono_type_full_name (value
->type
);
1034 char *stack_name
= stack_slot_stack_type_full_name (value
);
1035 char *res
= g_strdup_printf ("%s (%s)", type_name
, stack_name
);
1037 g_free (stack_name
);
1041 //////////////////////////////////////////////////////////////////
1043 mono_free_verify_list (GSList
*list
)
1045 MonoVerifyInfoExtended
*info
;
1048 for (tmp
= list
; tmp
; tmp
= tmp
->next
) {
1050 g_free (info
->info
.message
);
1053 g_slist_free (list
);
1056 #define ADD_ERROR(list,msg) \
1058 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1059 vinfo->info.status = MONO_VERIFY_ERROR; \
1060 vinfo->info.message = (msg); \
1061 (list) = g_slist_prepend ((list), vinfo); \
1064 #define ADD_WARN(list,code,msg) \
1066 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1067 vinfo->info.status = (code); \
1068 vinfo->info.message = (msg); \
1069 (list) = g_slist_prepend ((list), vinfo); \
1072 #define ADD_INVALID(list,msg) \
1074 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1075 vinfo->status = MONO_VERIFY_ERROR; \
1076 vinfo->message = (msg); \
1077 (list) = g_slist_prepend ((list), vinfo); \
1078 /*G_BREAKPOINT ();*/ \
1082 #define CHECK_STACK_UNDERFLOW(num) \
1084 if (cur_stack < (num)) \
1085 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1088 #define CHECK_STACK_OVERFLOW() \
1090 if (cur_stack >= max_stack) \
1091 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1096 in_any_block (MonoMethodHeader
*header
, guint offset
)
1099 MonoExceptionClause
*clause
;
1101 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1102 clause
= &header
->clauses
[i
];
1103 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1105 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1107 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1114 * in_any_exception_block:
1116 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1119 in_any_exception_block (MonoMethodHeader
*header
, guint offset
)
1122 MonoExceptionClause
*clause
;
1124 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1125 clause
= &header
->clauses
[i
];
1126 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1128 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1135 * is_valid_branch_instruction:
1137 * Verify if it's valid to perform a branch from @offset to @target.
1138 * This should be used with br and brtrue/false.
1139 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1140 * The major diferent from other similiar functions is that branching into a
1141 * finally/fault block is invalid instead of just unverifiable.
1144 is_valid_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1147 MonoExceptionClause
*clause
;
1149 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1150 clause
= &header
->clauses
[i
];
1151 /*branching into a finally block is invalid*/
1152 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
) &&
1153 !MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1154 MONO_OFFSET_IN_HANDLER (clause
, target
))
1157 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1159 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1161 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1168 * is_valid_cmp_branch_instruction:
1170 * Verify if it's valid to perform a branch from @offset to @target.
1171 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1172 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1174 * The major diferences from other similar functions are that most errors lead to invalid
1175 * code and only branching out of finally, filter or fault clauses is unverifiable.
1178 is_valid_cmp_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1181 MonoExceptionClause
*clause
;
1183 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1184 clause
= &header
->clauses
[i
];
1185 /*branching out of a handler or finally*/
1186 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
&&
1187 MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1188 !MONO_OFFSET_IN_HANDLER (clause
, target
))
1191 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1193 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1195 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1202 * A leave can't escape a finally block
1205 is_correct_leave (MonoMethodHeader
*header
, guint offset
, guint target
)
1208 MonoExceptionClause
*clause
;
1210 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1211 clause
= &header
->clauses
[i
];
1212 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
&& MONO_OFFSET_IN_HANDLER (clause
, offset
) && !MONO_OFFSET_IN_HANDLER (clause
, target
))
1214 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1221 * A rethrow can't happen outside of a catch handler.
1224 is_correct_rethrow (MonoMethodHeader
*header
, guint offset
)
1227 MonoExceptionClause
*clause
;
1229 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1230 clause
= &header
->clauses
[i
];
1231 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1233 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1240 * An endfinally can't happen outside of a finally/fault handler.
1243 is_correct_endfinally (MonoMethodHeader
*header
, guint offset
)
1246 MonoExceptionClause
*clause
;
1248 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1249 clause
= &header
->clauses
[i
];
1250 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) && (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
))
1258 * An endfilter can only happens inside a filter clause.
1259 * In non-strict mode filter is allowed inside the handler clause too
1261 static MonoExceptionClause
*
1262 is_correct_endfilter (VerifyContext
*ctx
, guint offset
)
1265 MonoExceptionClause
*clause
;
1267 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1268 clause
= &ctx
->header
->clauses
[i
];
1269 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_FILTER
)
1271 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1273 if (!IS_STRICT_MODE (ctx
) && MONO_OFFSET_IN_HANDLER (clause
, offset
))
1281 * Non-strict endfilter can happens inside a try block or any handler block
1284 is_unverifiable_endfilter (VerifyContext
*ctx
, guint offset
)
1287 MonoExceptionClause
*clause
;
1289 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1290 clause
= &ctx
->header
->clauses
[i
];
1291 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1298 is_valid_bool_arg (ILStackDesc
*arg
)
1300 if (stack_slot_is_managed_pointer (arg
) || stack_slot_is_boxed_value (arg
) || stack_slot_is_null_literal (arg
))
1304 switch (stack_slot_get_underlying_type (arg
)) {
1307 case TYPE_NATIVE_INT
:
1311 g_assert (arg
->type
);
1312 switch (arg
->type
->type
) {
1313 case MONO_TYPE_CLASS
:
1314 case MONO_TYPE_STRING
:
1315 case MONO_TYPE_OBJECT
:
1316 case MONO_TYPE_SZARRAY
:
1317 case MONO_TYPE_ARRAY
:
1318 case MONO_TYPE_FNPTR
:
1321 case MONO_TYPE_GENERICINST
:
1322 /*We need to check if the container class
1323 * of the generic type is a valuetype, iow:
1324 * is it a "class Foo<T>" or a "struct Foo<T>"?
1326 return !arg
->type
->data
.generic_class
->container_class
->valuetype
;
1334 /*Type manipulation helper*/
1336 /*Returns the byref version of the supplied MonoType*/
1338 mono_type_get_type_byref (MonoType
*type
)
1342 return &mono_class_from_mono_type (type
)->this_arg
;
1346 /*Returns the byval version of the supplied MonoType*/
1348 mono_type_get_type_byval (MonoType
*type
)
1352 return &mono_class_from_mono_type (type
)->byval_arg
;
1356 mono_type_from_stack_slot (ILStackDesc
*slot
)
1358 if (stack_slot_is_managed_pointer (slot
))
1359 return mono_type_get_type_byref (slot
->type
);
1363 /*Stack manipulation code*/
1366 ensure_stack_size (ILCodeDesc
*stack
, int required
)
1371 if (required
< stack
->max_size
)
1374 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1375 new_size
= MAX (8, MAX (required
, stack
->max_size
* 2));
1377 g_assert (new_size
>= stack
->size
);
1378 g_assert (new_size
>= required
);
1380 tmp
= g_new0 (ILStackDesc
, new_size
);
1381 MEM_ALLOC (sizeof (ILStackDesc
) * new_size
);
1385 memcpy (tmp
, stack
->stack
, stack
->size
* sizeof (ILStackDesc
));
1386 g_free (stack
->stack
);
1387 MEM_FREE (sizeof (ILStackDesc
) * stack
->max_size
);
1391 stack
->max_size
= new_size
;
1395 stack_init (VerifyContext
*ctx
, ILCodeDesc
*state
)
1397 if (state
->flags
& IL_CODE_FLAG_STACK_INITED
)
1399 state
->size
= state
->max_size
= 0;
1400 state
->flags
|= IL_CODE_FLAG_STACK_INITED
;
1404 stack_copy (ILCodeDesc
*to
, ILCodeDesc
*from
)
1406 ensure_stack_size (to
, from
->size
);
1407 to
->size
= from
->size
;
1409 /*stack copy happens at merge points, which have small stacks*/
1411 memcpy (to
->stack
, from
->stack
, sizeof (ILStackDesc
) * from
->size
);
1415 copy_stack_value (ILStackDesc
*to
, ILStackDesc
*from
)
1417 to
->stype
= from
->stype
;
1418 to
->type
= from
->type
;
1419 to
->method
= from
->method
;
1423 check_underflow (VerifyContext
*ctx
, int size
)
1425 if (ctx
->eval
.size
< size
) {
1426 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size
, ctx
->eval
.size
, ctx
->ip_offset
));
1433 check_overflow (VerifyContext
*ctx
)
1435 if (ctx
->eval
.size
>= ctx
->max_stack
) {
1436 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx
->eval
.size
+ 1, ctx
->ip_offset
));
1442 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1444 check_unmanaged_pointer (VerifyContext
*ctx
, ILStackDesc
*value
)
1446 if (stack_slot_get_type (value
) == TYPE_PTR
) {
1447 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
1453 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1455 check_unverifiable_type (VerifyContext
*ctx
, MonoType
*type
)
1457 if (type
->type
== MONO_TYPE_PTR
|| type
->type
== MONO_TYPE_FNPTR
) {
1458 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
1464 static ILStackDesc
*
1465 stack_push (VerifyContext
*ctx
)
1467 g_assert (ctx
->eval
.size
< ctx
->max_stack
);
1468 g_assert (ctx
->eval
.size
<= ctx
->eval
.max_size
);
1470 ensure_stack_size (&ctx
->eval
, ctx
->eval
.size
+ 1);
1472 return & ctx
->eval
.stack
[ctx
->eval
.size
++];
1475 static ILStackDesc
*
1476 stack_push_val (VerifyContext
*ctx
, int stype
, MonoType
*type
)
1478 ILStackDesc
*top
= stack_push (ctx
);
1484 static ILStackDesc
*
1485 stack_pop (VerifyContext
*ctx
)
1488 g_assert (ctx
->eval
.size
> 0);
1489 ret
= ctx
->eval
.stack
+ --ctx
->eval
.size
;
1490 if ((ret
->stype
& UNINIT_THIS_MASK
) == UNINIT_THIS_MASK
)
1491 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx
->ip_offset
));
1495 /* This function allows to safely pop an unititialized this ptr from
1496 * the eval stack without marking the method as unverifiable.
1498 static ILStackDesc
*
1499 stack_pop_safe (VerifyContext
*ctx
)
1501 g_assert (ctx
->eval
.size
> 0);
1502 return ctx
->eval
.stack
+ --ctx
->eval
.size
;
1505 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1507 stack_peek (VerifyContext
*ctx
, int distance
)
1509 g_assert (ctx
->eval
.size
- distance
> 0);
1510 return ctx
->eval
.stack
+ (ctx
->eval
.size
- 1 - distance
);
1513 static ILStackDesc
*
1514 stack_push_stack_val (VerifyContext
*ctx
, ILStackDesc
*value
)
1516 ILStackDesc
*top
= stack_push (ctx
);
1517 copy_stack_value (top
, value
);
1521 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1523 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1526 get_boxable_mono_type (VerifyContext
* ctx
, int token
, const char *opcode
)
1531 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
1534 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
1535 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1539 if (type
->type
== MONO_TYPE_VOID
) {
1540 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1544 if (type
->type
== MONO_TYPE_TYPEDBYREF
)
1545 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1547 if (!(class = mono_class_from_mono_type (type
)))
1548 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1550 if (class->generic_container
&& type
->type
!= MONO_TYPE_GENERICINST
)
1551 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
));
1553 check_unverifiable_type (ctx
, type
);
1558 /*operation result tables */
1560 static const unsigned char bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1561 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1562 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1563 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1564 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1565 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1566 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1569 static const unsigned char add_table
[TYPE_MAX
][TYPE_MAX
] = {
1570 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1571 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1572 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1573 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1574 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1575 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1578 static const unsigned char sub_table
[TYPE_MAX
][TYPE_MAX
] = {
1579 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1580 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1581 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1582 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1583 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1584 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1587 static const unsigned char int_bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1588 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1589 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1590 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1591 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1592 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1593 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1596 static const unsigned char shift_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1597 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1598 {TYPE_I8
, TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1599 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1600 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1601 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1602 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1605 static const unsigned char cmp_br_op
[TYPE_MAX
][TYPE_MAX
] = {
1606 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1607 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1608 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1609 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
1610 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
1611 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1614 static const unsigned char cmp_br_eq_op
[TYPE_MAX
][TYPE_MAX
] = {
1615 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1616 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1617 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1618 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
1619 {TYPE_INV
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
1620 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
},
1623 static const unsigned char add_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
1624 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1625 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1626 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1627 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1628 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1629 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1632 static const unsigned char sub_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
1633 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1634 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1635 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1636 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1637 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1638 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1641 static const unsigned char bin_ovf_table
[TYPE_MAX
][TYPE_MAX
] = {
1642 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1643 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1644 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1645 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1646 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1647 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1650 #ifdef MONO_VERIFIER_DEBUG
1654 dump_stack_value (ILStackDesc
*value
)
1656 printf ("[(%x)(%x)", value
->type
->type
, value
->stype
);
1658 if (stack_slot_is_this_pointer (value
))
1661 if (stack_slot_is_boxed_value (value
))
1662 printf ("[boxed] ");
1664 if (stack_slot_is_null_literal (value
))
1667 if (stack_slot_is_managed_mutability_pointer (value
))
1668 printf ("Controled Mutability MP: ");
1670 if (stack_slot_is_managed_pointer (value
))
1671 printf ("Managed Pointer to: ");
1673 switch (stack_slot_get_underlying_type (value
)) {
1675 printf ("invalid type]");
1683 case TYPE_NATIVE_INT
:
1684 printf ("native int]");
1687 printf ("float64]");
1690 printf ("unmanaged pointer]");
1693 switch (value
->type
->type
) {
1694 case MONO_TYPE_CLASS
:
1695 case MONO_TYPE_VALUETYPE
:
1696 printf ("complex] (%s)", value
->type
->data
.klass
->name
);
1698 case MONO_TYPE_STRING
:
1699 printf ("complex] (string)");
1701 case MONO_TYPE_OBJECT
:
1702 printf ("complex] (object)");
1704 case MONO_TYPE_SZARRAY
:
1705 printf ("complex] (%s [])", value
->type
->data
.klass
->name
);
1707 case MONO_TYPE_ARRAY
:
1708 printf ("complex] (%s [%d %d %d])",
1709 value
->type
->data
.array
->eklass
->name
,
1710 value
->type
->data
.array
->rank
,
1711 value
->type
->data
.array
->numsizes
,
1712 value
->type
->data
.array
->numlobounds
);
1714 case MONO_TYPE_GENERICINST
:
1715 printf ("complex] (inst of %s )", value
->type
->data
.generic_class
->container_class
->name
);
1718 printf ("complex] (type generic param !%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
1720 case MONO_TYPE_MVAR
:
1721 printf ("complex] (method generic param !!%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
1724 //should be a boxed value
1725 char * name
= mono_type_full_name (value
->type
);
1726 printf ("complex] %s", name
);
1732 printf ("unknown stack %x type]\n", value
->stype
);
1733 g_assert_not_reached ();
1738 dump_stack_state (ILCodeDesc
*state
)
1742 printf ("(%d) ", state
->size
);
1743 for (i
= 0; i
< state
->size
; ++i
)
1744 dump_stack_value (state
->stack
+ i
);
1749 /*Returns TRUE if candidate array type can be assigned to target.
1750 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1753 is_array_type_compatible (MonoType
*target
, MonoType
*candidate
)
1755 MonoArrayType
*left
= target
->data
.array
;
1756 MonoArrayType
*right
= candidate
->data
.array
;
1758 g_assert (target
->type
== MONO_TYPE_ARRAY
);
1759 g_assert (candidate
->type
== MONO_TYPE_ARRAY
);
1761 if (left
->rank
!= right
->rank
)
1764 return mono_class_is_assignable_from (left
->eklass
, right
->eklass
);
1768 get_stack_type (MonoType
*type
)
1771 int type_kind
= type
->type
;
1773 mask
= POINTER_MASK
;
1774 /*TODO handle CMMP_MASK */
1777 switch (type_kind
) {
1780 case MONO_TYPE_BOOLEAN
:
1783 case MONO_TYPE_CHAR
:
1786 return TYPE_I4
| mask
;
1790 return TYPE_NATIVE_INT
| mask
;
1792 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1793 case MONO_TYPE_FNPTR
:
1795 case MONO_TYPE_TYPEDBYREF
:
1796 return TYPE_PTR
| mask
;
1799 case MONO_TYPE_MVAR
:
1801 case MONO_TYPE_CLASS
:
1802 case MONO_TYPE_STRING
:
1803 case MONO_TYPE_OBJECT
:
1804 case MONO_TYPE_SZARRAY
:
1805 case MONO_TYPE_ARRAY
:
1806 return TYPE_COMPLEX
| mask
;
1810 return TYPE_I8
| mask
;
1814 return TYPE_R8
| mask
;
1816 case MONO_TYPE_GENERICINST
:
1817 case MONO_TYPE_VALUETYPE
:
1818 if (mono_type_is_enum_type (type
)) {
1819 type
= mono_type_get_underlying_type_any (type
);
1822 type_kind
= type
->type
;
1825 return TYPE_COMPLEX
| mask
;
1833 /* convert MonoType to ILStackDesc format (stype) */
1835 set_stack_value (VerifyContext
*ctx
, ILStackDesc
*stack
, MonoType
*type
, int take_addr
)
1838 int type_kind
= type
->type
;
1840 if (type
->byref
|| take_addr
)
1841 mask
= POINTER_MASK
;
1842 /* TODO handle CMMP_MASK */
1847 switch (type_kind
) {
1850 case MONO_TYPE_BOOLEAN
:
1853 case MONO_TYPE_CHAR
:
1856 stack
->stype
= TYPE_I4
| mask
;
1860 stack
->stype
= TYPE_NATIVE_INT
| mask
;
1863 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1864 case MONO_TYPE_FNPTR
:
1866 case MONO_TYPE_TYPEDBYREF
:
1867 stack
->stype
= TYPE_PTR
| mask
;
1870 case MONO_TYPE_CLASS
:
1871 case MONO_TYPE_STRING
:
1872 case MONO_TYPE_OBJECT
:
1873 case MONO_TYPE_SZARRAY
:
1874 case MONO_TYPE_ARRAY
:
1877 case MONO_TYPE_MVAR
:
1878 stack
->stype
= TYPE_COMPLEX
| mask
;
1883 stack
->stype
= TYPE_I8
| mask
;
1887 stack
->stype
= TYPE_R8
| mask
;
1889 case MONO_TYPE_GENERICINST
:
1890 case MONO_TYPE_VALUETYPE
:
1891 if (mono_type_is_enum_type (type
)) {
1892 MonoType
*utype
= mono_type_get_underlying_type_any (type
);
1894 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not resolve underlying type of %x at %d", type
->type
, ctx
->ip_offset
));
1898 type_kind
= type
->type
;
1901 stack
->stype
= TYPE_COMPLEX
| mask
;
1905 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type
->type
); );
1906 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type
->type
, ctx
->ip_offset
));
1913 * init_stack_with_value_at_exception_boundary:
1915 * Initialize the stack and push a given type.
1916 * The instruction is marked as been on the exception boundary.
1919 init_stack_with_value_at_exception_boundary (VerifyContext
*ctx
, ILCodeDesc
*code
, MonoClass
*klass
)
1922 MonoType
*type
= mono_class_inflate_generic_type_checked (&klass
->byval_arg
, ctx
->generic_context
, &error
);
1924 if (!mono_error_ok (&error
)) {
1925 char *name
= mono_type_get_full_name (klass
);
1926 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid class %s used for exception", name
));
1928 mono_error_cleanup (&error
);
1932 if (!ctx
->max_stack
) {
1933 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack overflow at 0x%04x", ctx
->ip_offset
));
1937 stack_init (ctx
, code
);
1938 ensure_stack_size (code
, 1);
1939 set_stack_value (ctx
, code
->stack
, type
, FALSE
);
1940 ctx
->exception_types
= g_slist_prepend (ctx
->exception_types
, type
);
1942 code
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
1943 if (mono_type_is_generic_argument (type
))
1944 code
->stack
->stype
|= BOXED_MASK
;
1947 /*Verify if type 'candidate' can be stored in type 'target'.
1949 * If strict, check for the underlying type and not the verification stack types
1952 verify_type_compatibility_full (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
, gboolean strict
)
1954 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1955 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1957 MonoType
*original_candidate
= candidate
;
1958 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target
), mono_type_full_name (candidate
), strict
); );
1960 /*only one is byref */
1961 if (candidate
->byref
^ target
->byref
) {
1962 /* converting from native int to byref*/
1963 if (get_stack_type (candidate
) == TYPE_NATIVE_INT
&& target
->byref
) {
1964 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("using byref native int at 0x%04x", ctx
->ip_offset
));
1969 strict
|= target
->byref
;
1970 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
1971 candidate
= mono_type_get_underlying_type_any (candidate
);
1974 switch (target
->type
) {
1975 case MONO_TYPE_VOID
:
1976 return candidate
->type
== MONO_TYPE_VOID
;
1979 case MONO_TYPE_BOOLEAN
:
1981 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I1
, MONO_TYPE_U1
, MONO_TYPE_BOOLEAN
);
1984 case MONO_TYPE_CHAR
:
1986 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I2
, MONO_TYPE_U2
, MONO_TYPE_CHAR
);
1988 case MONO_TYPE_U4
: {
1989 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
1990 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
1992 return is_native_int
|| is_int4
;
1993 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
1998 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I8
, MONO_TYPE_U8
);
2003 return candidate
->type
== target
->type
;
2004 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_R4
, MONO_TYPE_R8
);
2008 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
2009 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2011 return is_native_int
|| is_int4
;
2012 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2016 if (candidate
->type
!= MONO_TYPE_PTR
)
2018 /* check the underlying type */
2019 return verify_type_compatibility_full (ctx
, target
->data
.type
, candidate
->data
.type
, TRUE
);
2021 case MONO_TYPE_FNPTR
: {
2022 MonoMethodSignature
*left
, *right
;
2023 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2026 left
= mono_type_get_signature (target
);
2027 right
= mono_type_get_signature (candidate
);
2028 return mono_metadata_signature_equal (left
, right
) && left
->call_convention
== right
->call_convention
;
2031 case MONO_TYPE_GENERICINST
: {
2032 MonoClass
*target_klass
;
2033 MonoClass
*candidate_klass
;
2034 if (mono_type_is_enum_type (target
)) {
2035 target
= mono_type_get_underlying_type_any (target
);
2041 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2042 * to take boxing status into account.
2044 if (mono_type_is_generic_argument (original_candidate
))
2047 target_klass
= mono_class_from_mono_type (target
);
2048 candidate_klass
= mono_class_from_mono_type (candidate
);
2049 if (mono_class_is_nullable (target_klass
)) {
2050 if (!mono_class_is_nullable (candidate_klass
))
2052 return target_klass
== candidate_klass
;
2055 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2058 case MONO_TYPE_STRING
:
2059 return candidate
->type
== MONO_TYPE_STRING
;
2061 case MONO_TYPE_CLASS
:
2063 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2064 * to take boxing status into account.
2066 if (mono_type_is_generic_argument (original_candidate
))
2069 if (candidate
->type
== MONO_TYPE_VALUETYPE
)
2072 /* If candidate is an enum it should return true for System.Enum and supertypes.
2073 * That's why here we use the original type and not the underlying type.
2075 return mono_class_is_assignable_from (target
->data
.klass
, mono_class_from_mono_type (original_candidate
));
2077 case MONO_TYPE_OBJECT
:
2078 return MONO_TYPE_IS_REFERENCE (candidate
);
2080 case MONO_TYPE_SZARRAY
: {
2083 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2086 left
= mono_class_from_mono_type (target
);
2087 right
= mono_class_from_mono_type (candidate
);
2089 return mono_class_is_assignable_from (left
, right
);
2092 case MONO_TYPE_ARRAY
:
2093 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2095 return is_array_type_compatible (target
, candidate
);
2097 case MONO_TYPE_TYPEDBYREF
:
2098 return candidate
->type
== MONO_TYPE_TYPEDBYREF
;
2100 case MONO_TYPE_VALUETYPE
: {
2101 MonoClass
*target_klass
;
2102 MonoClass
*candidate_klass
;
2104 if (candidate
->type
== MONO_TYPE_CLASS
)
2107 target_klass
= mono_class_from_mono_type (target
);
2108 candidate_klass
= mono_class_from_mono_type (candidate
);
2109 if (target_klass
== candidate_klass
)
2111 if (mono_type_is_enum_type (target
)) {
2112 target
= mono_type_get_underlying_type_any (target
);
2121 if (candidate
->type
!= MONO_TYPE_VAR
)
2123 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2125 case MONO_TYPE_MVAR
:
2126 if (candidate
->type
!= MONO_TYPE_MVAR
)
2128 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2131 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target
->type
); );
2132 g_assert_not_reached ();
2141 verify_type_compatibility (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
)
2143 return verify_type_compatibility_full (ctx
, target
, candidate
, FALSE
);
2147 * Returns the generic param bound to the context been verified.
2150 static MonoGenericParam
*
2151 get_generic_param (VerifyContext
*ctx
, MonoType
*param
)
2153 guint16 param_num
= mono_type_get_generic_param_num (param
);
2154 if (param
->type
== MONO_TYPE_VAR
) {
2155 if (!ctx
->generic_context
->class_inst
|| ctx
->generic_context
->class_inst
->type_argc
<= param_num
) {
2156 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic type argument %d", param_num
));
2159 return ctx
->generic_context
->class_inst
->type_argv
[param_num
]->data
.generic_param
;
2162 /*param must be a MVAR */
2163 if (!ctx
->generic_context
->method_inst
|| ctx
->generic_context
->method_inst
->type_argc
<= param_num
) {
2164 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic method argument %d", param_num
));
2167 return ctx
->generic_context
->method_inst
->type_argv
[param_num
]->data
.generic_param
;
2172 recursive_boxed_constraint_type_check (VerifyContext
*ctx
, MonoType
*type
, MonoClass
*constraint_class
, int recursion_level
)
2174 MonoType
*constraint_type
= &constraint_class
->byval_arg
;
2175 if (recursion_level
<= 0)
2178 if (verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (constraint_type
), FALSE
))
2181 if (mono_type_is_generic_argument (constraint_type
)) {
2182 MonoGenericParam
*param
= get_generic_param (ctx
, constraint_type
);
2186 for (class = mono_generic_param_info (param
)->constraints
; class && *class; ++class) {
2187 if (recursive_boxed_constraint_type_check (ctx
, type
, *class, recursion_level
- 1))
2195 * is_compatible_boxed_valuetype:
2197 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2199 * @type The source type. It it tested to be of the proper type.
2200 * @candidate type of the boxed valuetype.
2201 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2202 * @strict if TRUE candidate must be boxed compatible to the target type
2206 is_compatible_boxed_valuetype (VerifyContext
*ctx
, MonoType
*type
, MonoType
*candidate
, ILStackDesc
*stack
, gboolean strict
)
2208 if (!stack_slot_is_boxed_value (stack
))
2210 if (type
->byref
|| candidate
->byref
)
2213 if (mono_type_is_generic_argument (candidate
)) {
2214 MonoGenericParam
*param
= get_generic_param (ctx
, candidate
);
2219 for (class = mono_generic_param_info (param
)->constraints
; class && *class; ++class) {
2220 /*256 should be enough since there can't be more than 255 generic arguments.*/
2221 if (recursive_boxed_constraint_type_check (ctx
, type
, *class, 256))
2226 if (mono_type_is_generic_argument (type
))
2232 return MONO_TYPE_IS_REFERENCE (type
) && mono_class_is_assignable_from (mono_class_from_mono_type (type
), mono_class_from_mono_type (candidate
));
2236 verify_stack_type_compatibility_full (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
, gboolean drop_byref
, gboolean valuetype_must_be_boxed
)
2238 MonoType
*candidate
= mono_type_from_stack_slot (stack
);
2239 if (MONO_TYPE_IS_REFERENCE (type
) && !type
->byref
&& stack_slot_is_null_literal (stack
))
2242 if (is_compatible_boxed_valuetype (ctx
, type
, candidate
, stack
, TRUE
))
2245 if (valuetype_must_be_boxed
&& !stack_slot_is_boxed_value (stack
) && !MONO_TYPE_IS_REFERENCE (candidate
))
2248 if (!valuetype_must_be_boxed
&& stack_slot_is_boxed_value (stack
))
2252 return verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (candidate
), FALSE
);
2254 return verify_type_compatibility_full (ctx
, type
, candidate
, FALSE
);
2258 verify_stack_type_compatibility (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
)
2260 return verify_stack_type_compatibility_full (ctx
, type
, stack
, FALSE
, FALSE
);
2264 mono_delegate_type_equal (MonoType
*target
, MonoType
*candidate
)
2266 if (candidate
->byref
^ target
->byref
)
2269 switch (target
->type
) {
2270 case MONO_TYPE_VOID
:
2273 case MONO_TYPE_BOOLEAN
:
2276 case MONO_TYPE_CHAR
:
2285 case MONO_TYPE_STRING
:
2286 case MONO_TYPE_TYPEDBYREF
:
2287 return candidate
->type
== target
->type
;
2290 return mono_delegate_type_equal (target
->data
.type
, candidate
->data
.type
);
2292 case MONO_TYPE_FNPTR
:
2293 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2295 return mono_delegate_signature_equal (mono_type_get_signature (target
), mono_type_get_signature (candidate
), FALSE
);
2297 case MONO_TYPE_GENERICINST
: {
2298 MonoClass
*target_klass
;
2299 MonoClass
*candidate_klass
;
2300 target_klass
= mono_class_from_mono_type (target
);
2301 candidate_klass
= mono_class_from_mono_type (candidate
);
2302 /*FIXME handle nullables and enum*/
2303 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2305 case MONO_TYPE_OBJECT
:
2306 return MONO_TYPE_IS_REFERENCE (candidate
);
2308 case MONO_TYPE_CLASS
:
2309 return mono_class_is_assignable_from(target
->data
.klass
, mono_class_from_mono_type (candidate
));
2311 case MONO_TYPE_SZARRAY
:
2312 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2314 return mono_class_is_assignable_from (mono_class_from_mono_type (target
)->element_class
, mono_class_from_mono_type (candidate
)->element_class
);
2316 case MONO_TYPE_ARRAY
:
2317 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2319 return is_array_type_compatible (target
, candidate
);
2321 case MONO_TYPE_VALUETYPE
:
2322 /*FIXME handle nullables and enum*/
2323 return mono_class_from_mono_type (candidate
) == mono_class_from_mono_type (target
);
2326 return candidate
->type
== MONO_TYPE_VAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2329 case MONO_TYPE_MVAR
:
2330 return candidate
->type
== MONO_TYPE_MVAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2334 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target
->type
); );
2335 g_assert_not_reached ();
2341 mono_delegate_param_equal (MonoType
*delegate
, MonoType
*method
)
2343 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2346 return mono_delegate_type_equal (method
, delegate
);
2350 mono_delegate_ret_equal (MonoType
*delegate
, MonoType
*method
)
2352 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2355 return mono_delegate_type_equal (delegate
, method
);
2359 * mono_delegate_signature_equal:
2361 * Compare two signatures in the way expected by delegates.
2363 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2365 * FIXME can this function be eliminated and proper metadata functionality be used?
2368 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
)
2371 int method_offset
= is_static_ldftn
? 1 : 0;
2373 if (delegate_sig
->param_count
+ method_offset
!= method_sig
->param_count
)
2376 if (delegate_sig
->call_convention
!= method_sig
->call_convention
)
2379 for (i
= 0; i
< delegate_sig
->param_count
; i
++) {
2380 MonoType
*p1
= delegate_sig
->params
[i
];
2381 MonoType
*p2
= method_sig
->params
[i
+ method_offset
];
2383 if (!mono_delegate_param_equal (p1
, p2
))
2387 if (!mono_delegate_ret_equal (delegate_sig
->ret
, method_sig
->ret
))
2394 * verify_ldftn_delegate:
2396 * Verify properties of ldftn based delegates.
2399 verify_ldftn_delegate (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2401 MonoMethod
*method
= funptr
->method
;
2403 /*ldftn non-final virtuals only allowed if method is not static,
2404 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2405 * This rules doesn't apply if the object on stack is a boxed valuetype.
2407 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !(method
->klass
->flags
& TYPE_ATTRIBUTE_SEALED
) && !stack_slot_is_boxed_value (value
)) {
2408 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2409 if (IS_FAIL_FAST_MODE (ctx
) && ctx
->has_this_store
)
2410 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx
->ip_offset
));
2412 /*current method must not be static*/
2413 if (ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)
2414 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx
->ip_offset
));
2416 /*value is the this pointer, loaded using ldarg.0 */
2417 if (!stack_slot_is_this_pointer (value
))
2418 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
));
2420 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
;
2425 * verify_delegate_compatibility:
2427 * Verify delegate creation sequence.
2431 verify_delegate_compatibility (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2433 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2434 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2436 MonoMethod
*invoke
, *method
;
2437 const guint8
*ip
= ctx
->header
->code
;
2438 guint32 ip_offset
= ctx
->ip_offset
;
2439 gboolean is_static_ldftn
= FALSE
, is_first_arg_bound
= FALSE
;
2441 if (stack_slot_get_type (funptr
) != TYPE_PTR
|| !funptr
->method
) {
2442 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx
->ip_offset
));
2446 invoke
= mono_get_delegate_invoke (delegate
);
2447 method
= funptr
->method
;
2449 if (!method
|| !mono_method_signature (method
)) {
2450 char *name
= mono_type_get_full_name (delegate
);
2451 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name
, ctx
->ip_offset
));
2456 if (!invoke
|| !mono_method_signature (invoke
)) {
2457 char *name
= mono_type_get_full_name (delegate
);
2458 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name
, ctx
->ip_offset
));
2463 is_static_ldftn
= (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) && method
->flags
& METHOD_ATTRIBUTE_STATIC
;
2465 if (is_static_ldftn
)
2466 is_first_arg_bound
= mono_method_signature (invoke
)->param_count
+ 1 == mono_method_signature (method
)->param_count
;
2468 if (!mono_delegate_signature_equal (mono_method_signature (invoke
), mono_method_signature (method
), is_first_arg_bound
)) {
2469 char *fun_sig
= mono_signature_get_desc (mono_method_signature (method
), FALSE
);
2470 char *invoke_sig
= mono_signature_get_desc (mono_method_signature (invoke
), FALSE
);
2471 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
));
2473 g_free (invoke_sig
);
2477 * Delegate code sequences:
2483 * [-6] ldvirtftn token
2487 if (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) {
2488 verify_ldftn_delegate (ctx
, delegate
, value
, funptr
);
2489 } else if (ip_offset
> 6 && IS_VALID_OPCODE (7, CEE_DUP
) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN
)) {
2490 ctx
->code
[ip_offset
- 6].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2492 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx
->ip_offset
));
2494 ctx
->code
[ip_offset
].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2497 if (is_first_arg_bound
) {
2498 if (mono_method_signature (method
)->param_count
== 0 || !verify_stack_type_compatibility_full (ctx
, mono_method_signature (method
)->params
[0], value
, FALSE
, TRUE
))
2499 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2501 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
2502 if (!stack_slot_is_null_literal (value
) && !is_first_arg_bound
)
2503 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx
->ip_offset
));
2505 if (!verify_stack_type_compatibility_full (ctx
, &method
->klass
->byval_arg
, value
, FALSE
, TRUE
) && !stack_slot_is_null_literal (value
))
2506 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2510 if (stack_slot_get_type (value
) != TYPE_COMPLEX
)
2511 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx
->ip_offset
));
2513 #undef IS_VALID_OPCODE
2514 #undef IS_LOAD_FUN_PTR
2517 /* implement the opcode checks*/
2519 push_arg (VerifyContext
*ctx
, unsigned int arg
, int take_addr
)
2523 if (arg
>= ctx
->max_args
) {
2525 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
2527 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
2528 if (check_overflow (ctx
)) //FIXME: what sane value could we ever push?
2529 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
2531 } else if (check_overflow (ctx
)) {
2532 /*We must let the value be pushed, otherwise we would get an underflow error*/
2533 check_unverifiable_type (ctx
, ctx
->params
[arg
]);
2534 if (ctx
->params
[arg
]->byref
&& take_addr
)
2535 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
2536 top
= stack_push (ctx
);
2537 if (!set_stack_value (ctx
, top
, ctx
->params
[arg
], take_addr
))
2540 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
2542 ctx
->has_this_store
= TRUE
;
2544 top
->stype
|= THIS_POINTER_MASK
;
2545 if (mono_method_is_constructor (ctx
->method
) && !ctx
->super_ctor_called
&& !ctx
->method
->klass
->valuetype
)
2546 top
->stype
|= UNINIT_THIS_MASK
;
2552 push_local (VerifyContext
*ctx
, guint32 arg
, int take_addr
)
2554 if (arg
>= ctx
->num_locals
) {
2555 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local %d", arg
+ 1));
2556 } else if (check_overflow (ctx
)) {
2557 /*We must let the value be pushed, otherwise we would get an underflow error*/
2558 check_unverifiable_type (ctx
, ctx
->locals
[arg
]);
2559 if (ctx
->locals
[arg
]->byref
&& take_addr
)
2560 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
2562 set_stack_value (ctx
, stack_push (ctx
), ctx
->locals
[arg
], take_addr
);
2567 store_arg (VerifyContext
*ctx
, guint32 arg
)
2571 if (arg
>= ctx
->max_args
) {
2572 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
2573 if (check_underflow (ctx
, 1))
2578 if (check_underflow (ctx
, 1)) {
2579 value
= stack_pop (ctx
);
2580 if (!verify_stack_type_compatibility (ctx
, ctx
->params
[arg
], value
)) {
2581 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
2584 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
))
2585 ctx
->has_this_store
= 1;
2589 store_local (VerifyContext
*ctx
, guint32 arg
)
2592 if (arg
>= ctx
->num_locals
) {
2593 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
2597 /*TODO verify definite assigment */
2598 if (check_underflow (ctx
, 1)) {
2599 value
= stack_pop(ctx
);
2600 if (!verify_stack_type_compatibility (ctx
, ctx
->locals
[arg
], value
)) {
2601 char *expected
= mono_type_full_name (ctx
->locals
[arg
]);
2602 char *found
= stack_slot_full_name (value
);
2603 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2614 /*FIXME add and sub needs special care here*/
2616 do_binop (VerifyContext
*ctx
, unsigned int opcode
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
2618 ILStackDesc
*a
, *b
, *top
;
2619 int idxa
, idxb
, complexMerge
= 0;
2622 if (!check_underflow (ctx
, 2))
2624 b
= stack_pop (ctx
);
2625 a
= stack_pop (ctx
);
2627 idxa
= stack_slot_get_underlying_type (a
);
2628 if (stack_slot_is_managed_pointer (a
)) {
2633 idxb
= stack_slot_get_underlying_type (b
);
2634 if (stack_slot_is_managed_pointer (b
)) {
2641 res
= table
[idxa
][idxb
];
2643 VERIFIER_DEBUG ( printf ("binop res %d\n", res
); );
2644 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
2646 top
= stack_push (ctx
);
2647 if (res
== TYPE_INV
) {
2648 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
)));
2649 copy_stack_value (top
, a
);
2653 if (res
& NON_VERIFIABLE_RESULT
) {
2654 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
)));
2656 res
= res
& ~NON_VERIFIABLE_RESULT
;
2659 if (complexMerge
&& res
== TYPE_PTR
) {
2660 if (complexMerge
== 1)
2661 copy_stack_value (top
, a
);
2662 else if (complexMerge
== 2)
2663 copy_stack_value (top
, b
);
2665 * There is no need to merge the type of two pointers.
2666 * The only valid operation is subtraction, that returns a native
2667 * int as result and can be used with any 2 pointer kinds.
2668 * This is valid acording to Patition III 1.1.4
2677 do_boolean_branch_op (VerifyContext
*ctx
, int delta
)
2679 int target
= ctx
->ip_offset
+ delta
;
2682 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
2684 if (target
< 0 || target
>= ctx
->code_size
) {
2685 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx
->ip_offset
));
2689 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
2691 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2694 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2698 ctx
->target
= target
;
2700 if (!check_underflow (ctx
, 1))
2703 top
= stack_pop (ctx
);
2704 if (!is_valid_bool_arg (top
))
2705 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
));
2707 check_unmanaged_pointer (ctx
, top
);
2711 stack_slot_is_complex_type_not_reference_type (ILStackDesc
*slot
)
2713 return stack_slot_get_type (slot
) == TYPE_COMPLEX
&& !MONO_TYPE_IS_REFERENCE (slot
->type
) && !stack_slot_is_boxed_value (slot
);
2717 do_branch_op (VerifyContext
*ctx
, signed int delta
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
2722 int target
= ctx
->ip_offset
+ delta
;
2724 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
2726 if (target
< 0 || target
>= ctx
->code_size
) {
2727 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
2731 switch (is_valid_cmp_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
2732 case 1: /*FIXME use constants and not magic numbers.*/
2733 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2736 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2740 ctx
->target
= target
;
2742 if (!check_underflow (ctx
, 2))
2745 b
= stack_pop (ctx
);
2746 a
= stack_pop (ctx
);
2748 idxa
= stack_slot_get_underlying_type (a
);
2749 if (stack_slot_is_managed_pointer (a
))
2752 idxb
= stack_slot_get_underlying_type (b
);
2753 if (stack_slot_is_managed_pointer (b
))
2756 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
2761 res
= table
[idxa
][idxb
];
2764 VERIFIER_DEBUG ( printf ("branch res %d\n", res
); );
2765 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
2767 if (res
== TYPE_INV
) {
2768 CODE_NOT_VERIFIABLE (ctx
,
2769 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
));
2770 } else if (res
& NON_VERIFIABLE_RESULT
) {
2771 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
));
2772 res
= res
& ~NON_VERIFIABLE_RESULT
;
2777 do_cmp_op (VerifyContext
*ctx
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
], guint32 opcode
)
2783 if (!check_underflow (ctx
, 2))
2785 b
= stack_pop (ctx
);
2786 a
= stack_pop (ctx
);
2788 if (opcode
== CEE_CGT_UN
) {
2789 if (stack_slot_get_type (a
) == TYPE_COMPLEX
&& stack_slot_get_type (b
) == TYPE_COMPLEX
) {
2790 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
2795 idxa
= stack_slot_get_underlying_type (a
);
2796 if (stack_slot_is_managed_pointer (a
))
2799 idxb
= stack_slot_get_underlying_type (b
);
2800 if (stack_slot_is_managed_pointer (b
))
2803 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
2808 res
= table
[idxa
][idxb
];
2811 if(res
== TYPE_INV
) {
2812 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a
), stack_slot_get_name (b
), ctx
->ip_offset
));
2813 } else if (res
& NON_VERIFIABLE_RESULT
) {
2814 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
));
2815 res
= res
& ~NON_VERIFIABLE_RESULT
;
2817 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
2821 do_ret (VerifyContext
*ctx
)
2823 MonoType
*ret
= ctx
->signature
->ret
;
2824 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2825 if (ret
->type
!= MONO_TYPE_VOID
) {
2827 if (!check_underflow (ctx
, 1))
2830 top
= stack_pop(ctx
);
2832 if (!verify_stack_type_compatibility (ctx
, ctx
->signature
->ret
, top
)) {
2833 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx
->ip_offset
));
2837 if (ret
->byref
|| ret
->type
== MONO_TYPE_TYPEDBYREF
|| mono_type_is_value_type (ret
, "System", "ArgIterator") || mono_type_is_value_type (ret
, "System", "RuntimeArgumentHandle"))
2838 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx
->ip_offset
));
2841 if (ctx
->eval
.size
> 0) {
2842 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx
->eval
.size
, ctx
->ip_offset
));
2844 if (in_any_block (ctx
->header
, ctx
->ip_offset
))
2845 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx
->ip_offset
));
2849 * 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.
2850 * This is illegal but mono_get_method_full decoded it.
2851 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2854 do_invoke_method (VerifyContext
*ctx
, int method_token
, gboolean
virtual)
2857 MonoMethodSignature
*sig
;
2860 gboolean virt_check_this
= FALSE
;
2861 gboolean constrained
= ctx
->prefix_set
& PREFIX_CONSTRAINED
;
2863 if (!(method
= verifier_load_method (ctx
, method_token
, virtual ? "callvirt" : "call")))
2867 CLEAR_PREFIX (ctx
, PREFIX_CONSTRAINED
);
2869 if (method
->klass
->valuetype
) // && !constrained ???
2870 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx
->ip_offset
));
2872 if ((method
->flags
& METHOD_ATTRIBUTE_STATIC
))
2873 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx
->ip_offset
));
2876 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
2877 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx
->ip_offset
));
2879 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !(method
->klass
->flags
& TYPE_ATTRIBUTE_SEALED
)) {
2880 virt_check_this
= TRUE
;
2881 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_CALL_NONFINAL_VIRTUAL
;
2885 if (!(sig
= mono_method_get_signature_full (method
, ctx
->image
, method_token
, ctx
->generic_context
)))
2886 sig
= mono_method_get_signature (method
, ctx
->image
, method_token
);
2889 char *name
= mono_type_get_full_name (method
->klass
);
2890 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name
, method
->name
, ctx
->ip_offset
));
2895 param_count
= sig
->param_count
+ sig
->hasthis
;
2896 if (!check_underflow (ctx
, param_count
))
2899 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
2900 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i
); );
2901 value
= stack_pop (ctx
);
2902 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
2903 char *stack_name
= stack_slot_full_name (value
);
2904 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
2905 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
));
2906 g_free (stack_name
);
2910 if (stack_slot_is_managed_mutability_pointer (value
))
2911 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
));
2913 if ((ctx
->prefix_set
& PREFIX_TAIL
) && stack_slot_is_managed_pointer (value
)) {
2914 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
));
2920 MonoType
*type
= &method
->klass
->byval_arg
;
2923 if (mono_method_is_constructor (method
) && !method
->klass
->valuetype
) {
2924 if (!mono_method_is_constructor (ctx
->method
))
2925 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx
->ip_offset
));
2926 if (method
->klass
!= ctx
->method
->klass
->parent
&& method
->klass
!= ctx
->method
->klass
)
2927 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor to a type diferent that this or super at 0x%04x", ctx
->ip_offset
));
2929 ctx
->super_ctor_called
= TRUE
;
2930 value
= stack_pop_safe (ctx
);
2931 if ((value
->stype
& THIS_POINTER_MASK
) != THIS_POINTER_MASK
)
2932 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx
->ip_offset
));
2934 value
= stack_pop (ctx
);
2937 copy_stack_value (©
, value
);
2938 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2939 //Other parts of the code suffer from the same issue of
2940 copy
.type
= mono_type_get_type_byval (copy
.type
);
2941 copy
.stype
&= ~POINTER_MASK
;
2943 if (virt_check_this
&& !stack_slot_is_this_pointer (value
) && !(method
->klass
->valuetype
|| stack_slot_is_boxed_value (value
)))
2944 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use the call opcode with a non-final virtual method on an object diferent thant the this pointer at 0x%04x", ctx
->ip_offset
));
2946 if (constrained
&& virtual) {
2947 if (!stack_slot_is_managed_pointer (value
))
2948 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx
->ip_offset
));
2949 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value
->type
), ctx
->constrained_type
, TRUE
))
2950 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx
->ip_offset
));
2951 copy
.stype
|= BOXED_MASK
;
2953 if (stack_slot_is_managed_pointer (value
) && !mono_class_from_mono_type (value
->type
)->valuetype
)
2954 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
));
2956 if (!virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !method
->klass
->valuetype
&& !stack_slot_is_boxed_value (value
))
2957 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx
->ip_offset
));
2959 if (virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !stack_slot_is_boxed_value (value
))
2960 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx
->ip_offset
));
2962 if (method
->klass
->valuetype
&& (stack_slot_is_boxed_value (value
) || !stack_slot_is_managed_pointer (value
)))
2963 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
));
2965 if (!verify_stack_type_compatibility (ctx
, type
, ©
)) {
2966 char *expected
= mono_type_full_name (type
);
2967 char *effective
= stack_slot_full_name (©
);
2968 char *method_name
= mono_method_full_name (method
, TRUE
);
2969 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",
2970 expected
, effective
, method_name
, ctx
->ip_offset
));
2971 g_free (method_name
);
2976 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, mono_class_from_mono_type (value
->type
))) {
2977 char *name
= mono_method_full_name (method
, TRUE
);
2978 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
2982 } else if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
2983 char *name
= mono_method_full_name (method
, TRUE
);
2984 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
2988 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
2989 if (check_overflow (ctx
)) {
2990 value
= stack_push (ctx
);
2991 set_stack_value (ctx
, value
, sig
->ret
, FALSE
);
2992 if ((ctx
->prefix_set
& PREFIX_READONLY
) && method
->klass
->rank
&& !strcmp (method
->name
, "Address")) {
2993 ctx
->prefix_set
&= ~PREFIX_READONLY
;
2994 value
->stype
|= CMMP_MASK
;
2999 if ((ctx
->prefix_set
& PREFIX_TAIL
)) {
3000 if (!mono_delegate_ret_equal (mono_method_signature (ctx
->method
)->ret
, sig
->ret
))
3001 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx
->ip_offset
));
3002 if (ctx
->header
->code
[ctx
->ip_offset
+ 5] != CEE_RET
)
3003 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx
->ip_offset
));
3009 do_push_static_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3011 MonoClassField
*field
;
3013 if (!check_overflow (ctx
))
3016 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3018 if (!(field
= verifier_load_field (ctx
, token
, &klass
, take_addr
? "ldsflda" : "ldsfld")))
3021 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3022 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx
->ip_offset
));
3025 /*taking the address of initonly field only works from the static constructor */
3026 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3027 !(field
->parent
== ctx
->method
->klass
&& (ctx
->method
->flags
& (METHOD_ATTRIBUTE_SPECIAL_NAME
| METHOD_ATTRIBUTE_STATIC
)) && !strcmp (".cctor", ctx
->method
->name
)))
3028 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3030 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3031 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3033 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3037 do_store_static_field (VerifyContext
*ctx
, int token
) {
3038 MonoClassField
*field
;
3041 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3043 if (!check_underflow (ctx
, 1))
3046 value
= stack_pop (ctx
);
3048 if (!(field
= verifier_load_field (ctx
, token
, &klass
, "stsfld")))
3051 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3052 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx
->ip_offset
));
3056 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3057 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx
->ip_offset
));
3061 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3062 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3064 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
)) {
3065 char *stack_name
= stack_slot_full_name (value
);
3066 char *field_name
= mono_type_full_name (field
->type
);
3067 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3068 field_name
, stack_name
, ctx
->ip_offset
));
3069 g_free (field_name
);
3070 g_free (stack_name
);
3075 check_is_valid_type_for_field_ops (VerifyContext
*ctx
, int token
, ILStackDesc
*obj
, MonoClassField
**ret_field
, const char *opcode
)
3077 MonoClassField
*field
;
3079 gboolean is_pointer
;
3081 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3082 if (!(field
= verifier_load_field (ctx
, token
, &klass
, opcode
)))
3086 //the value on stack is going to be used as a pointer
3087 is_pointer
= stack_slot_get_type (obj
) == TYPE_PTR
|| (stack_slot_get_type (obj
) == TYPE_NATIVE_INT
&& !get_stack_type (&field
->parent
->byval_arg
));
3089 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3090 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx
->ip_offset
));
3093 g_assert (obj
->type
);
3095 /*The value on the stack must be a subclass of the defining type of the field*/
3096 /* we need to check if we can load the field from the stack value*/
3098 if (stack_slot_get_underlying_type (obj
) == TYPE_NATIVE_INT
)
3099 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx
->ip_offset
));
3101 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3102 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3104 if (!field
->parent
->valuetype
&& stack_slot_is_managed_pointer (obj
))
3105 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
));
3107 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3108 if (field
->parent
->valuetype
&& stack_slot_is_boxed_value (obj
))
3109 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
));
3111 if (!stack_slot_is_null_literal (obj
) && !verify_stack_type_compatibility_full (ctx
, &field
->parent
->byval_arg
, obj
, TRUE
, FALSE
)) {
3112 char *found
= stack_slot_full_name (obj
);
3113 char *expected
= mono_type_full_name (&field
->parent
->byval_arg
);
3114 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
));
3119 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, mono_class_from_mono_type (obj
->type
)))
3120 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3123 check_unmanaged_pointer (ctx
, obj
);
3128 do_push_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3131 MonoClassField
*field
;
3134 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3136 if (!check_underflow (ctx
, 1))
3138 obj
= stack_pop_safe (ctx
);
3140 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, take_addr
? "ldflda" : "ldfld"))
3143 if (take_addr
&& field
->parent
->valuetype
&& !stack_slot_is_managed_pointer (obj
))
3144 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx
->ip_offset
));
3146 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3147 !(field
->parent
== ctx
->method
->klass
&& mono_method_is_constructor (ctx
->method
)))
3148 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3150 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3154 do_store_field (VerifyContext
*ctx
, int token
)
3156 ILStackDesc
*value
, *obj
;
3157 MonoClassField
*field
;
3158 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3160 if (!check_underflow (ctx
, 2))
3163 value
= stack_pop (ctx
);
3164 obj
= stack_pop_safe (ctx
);
3166 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, "stfld"))
3169 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3170 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3173 /*TODO proper handle for Nullable<T>*/
3175 do_box_value (VerifyContext
*ctx
, int klass_token
)
3178 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "box");
3184 if (!check_underflow (ctx
, 1))
3187 value
= stack_pop (ctx
);
3188 /*box is a nop for reference types*/
3190 if (stack_slot_get_underlying_type (value
) == TYPE_COMPLEX
&& MONO_TYPE_IS_REFERENCE (value
->type
) && MONO_TYPE_IS_REFERENCE (type
)) {
3191 stack_push_stack_val (ctx
, value
)->stype
|= BOXED_MASK
;
3196 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3197 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx
->ip_offset
));
3199 klass
= mono_class_from_mono_type (type
);
3200 if (mono_class_is_nullable (klass
))
3201 type
= &mono_class_get_nullable_param (klass
)->byval_arg
;
3202 stack_push_val (ctx
, TYPE_COMPLEX
| BOXED_MASK
, type
);
3206 do_unbox_value (VerifyContext
*ctx
, int klass_token
)
3209 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox");
3214 if (!check_underflow (ctx
, 1))
3217 if (!mono_class_from_mono_type (type
)->valuetype
)
3218 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx
->ip_offset
));
3220 value
= stack_pop (ctx
);
3222 /*Value should be: a boxed valuetype or a reference type*/
3223 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3224 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3225 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
));
3227 set_stack_value (ctx
, value
= stack_push (ctx
), mono_type_get_type_byref (type
), FALSE
);
3228 value
->stype
|= CMMP_MASK
;
3232 do_unbox_any (VerifyContext
*ctx
, int klass_token
)
3235 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox.any");
3240 if (!check_underflow (ctx
, 1))
3243 value
= stack_pop (ctx
);
3245 /*Value should be: a boxed valuetype or a reference type*/
3246 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3247 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3248 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
));
3250 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3254 do_unary_math_op (VerifyContext
*ctx
, int op
)
3257 if (!check_underflow (ctx
, 1))
3259 value
= stack_pop (ctx
);
3260 switch (stack_slot_get_type (value
)) {
3263 case TYPE_NATIVE_INT
:
3268 case TYPE_COMPLEX
: /*only enums are ok*/
3269 if (mono_type_is_enum_type (value
->type
))
3272 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx
->ip_offset
));
3274 stack_push_stack_val (ctx
, value
);
3278 do_conversion (VerifyContext
*ctx
, int kind
)
3281 if (!check_underflow (ctx
, 1))
3283 value
= stack_pop (ctx
);
3285 switch (stack_slot_get_type (value
)) {
3288 case TYPE_NATIVE_INT
:
3292 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
));
3297 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3300 stack_push_val (ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
3303 stack_push_val (ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
3305 case TYPE_NATIVE_INT
:
3306 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3309 g_error ("unknown type %02x in conversion", kind
);
3315 do_load_token (VerifyContext
*ctx
, int token
)
3318 MonoClass
*handle_class
;
3319 if (!check_overflow (ctx
))
3322 switch (token
& 0xff000000) {
3323 case MONO_TOKEN_TYPE_DEF
:
3324 case MONO_TOKEN_TYPE_REF
:
3325 case MONO_TOKEN_TYPE_SPEC
:
3326 case MONO_TOKEN_FIELD_DEF
:
3327 case MONO_TOKEN_METHOD_DEF
:
3328 case MONO_TOKEN_METHOD_SPEC
:
3329 case MONO_TOKEN_MEMBER_REF
:
3330 if (!token_bounds_check (ctx
->image
, token
)) {
3331 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
));
3336 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
));
3340 handle
= mono_ldtoken (ctx
->image
, token
, &handle_class
, ctx
->generic_context
);
3342 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token
, ctx
->ip_offset
));
3345 if (handle_class
== mono_defaults
.typehandle_class
) {
3346 mono_type_is_valid_in_context (ctx
, (MonoType
*)handle
);
3347 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
3348 mono_method_is_valid_in_context (ctx
, (MonoMethod
*)handle
);
3349 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
3350 mono_type_is_valid_in_context (ctx
, &((MonoClassField
*)handle
)->parent
->byval_arg
);
3352 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
3354 stack_push_val (ctx
, TYPE_COMPLEX
, mono_class_get_type (handle_class
));
3358 do_ldobj_value (VerifyContext
*ctx
, int token
)
3361 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "ldobj");
3362 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3367 if (!check_underflow (ctx
, 1))
3370 value
= stack_pop (ctx
);
3371 if (!stack_slot_is_managed_pointer (value
)
3372 && stack_slot_get_type (value
) != TYPE_NATIVE_INT
3373 && !(stack_slot_get_type (value
) == TYPE_PTR
&& value
->type
->type
!= MONO_TYPE_FNPTR
)) {
3374 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3378 if (stack_slot_get_type (value
) == TYPE_NATIVE_INT
)
3379 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx
->ip_offset
));
3381 /*We have a byval on the stack, but the comparison must be strict. */
3382 if (!verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (value
->type
), TRUE
))
3383 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx
->ip_offset
));
3385 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3389 do_stobj (VerifyContext
*ctx
, int token
)
3391 ILStackDesc
*dest
, *src
;
3392 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "stobj");
3393 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3398 if (!check_underflow (ctx
, 2))
3401 src
= stack_pop (ctx
);
3402 dest
= stack_pop (ctx
);
3404 if (stack_slot_is_managed_mutability_pointer (dest
))
3405 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx
->ip_offset
));
3407 if (!stack_slot_is_managed_pointer (dest
))
3408 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx
->ip_offset
));
3410 if (stack_slot_is_boxed_value (src
) && !MONO_TYPE_IS_REFERENCE (src
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3411 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
));
3413 if (!verify_stack_type_compatibility (ctx
, type
, src
)) {
3414 char *type_name
= mono_type_full_name (type
);
3415 char *src_name
= stack_slot_full_name (src
);
3416 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
));
3421 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3422 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3426 do_cpobj (VerifyContext
*ctx
, int token
)
3428 ILStackDesc
*dest
, *src
;
3429 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "cpobj");
3433 if (!check_underflow (ctx
, 2))
3436 src
= stack_pop (ctx
);
3437 dest
= stack_pop (ctx
);
3439 if (!stack_slot_is_managed_pointer (src
))
3440 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx
->ip_offset
));
3442 if (!stack_slot_is_managed_pointer (dest
))
3443 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx
->ip_offset
));
3445 if (stack_slot_is_managed_mutability_pointer (dest
))
3446 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx
->ip_offset
));
3448 if (!verify_type_compatibility (ctx
, type
, mono_type_get_type_byval (src
->type
)))
3449 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3451 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3452 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3456 do_initobj (VerifyContext
*ctx
, int token
)
3459 MonoType
*stack
, *type
= get_boxable_mono_type (ctx
, token
, "initobj");
3463 if (!check_underflow (ctx
, 1))
3466 obj
= stack_pop (ctx
);
3468 if (!stack_slot_is_managed_pointer (obj
))
3469 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx
->ip_offset
));
3471 if (stack_slot_is_managed_mutability_pointer (obj
))
3472 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx
->ip_offset
));
3474 stack
= mono_type_get_type_byval (obj
->type
);
3475 if (MONO_TYPE_IS_REFERENCE (stack
)) {
3476 if (!verify_type_compatibility (ctx
, stack
, type
))
3477 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3478 else if (IS_STRICT_MODE (ctx
) && !mono_metadata_type_equal (type
, stack
))
3479 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3480 } else if (!verify_type_compatibility (ctx
, stack
, type
)) {
3481 char *expected_name
= mono_type_full_name (type
);
3482 char *stack_name
= mono_type_full_name (stack
);
3484 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
));
3485 g_free (expected_name
);
3486 g_free (stack_name
);
3491 do_newobj (VerifyContext
*ctx
, int token
)
3495 MonoMethodSignature
*sig
;
3497 gboolean is_delegate
= FALSE
;
3499 if (!(method
= verifier_load_method (ctx
, token
, "newobj")))
3502 if (!mono_method_is_constructor (method
)) {
3503 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token
, ctx
->ip_offset
));
3507 if (method
->klass
->flags
& (TYPE_ATTRIBUTE_ABSTRACT
| TYPE_ATTRIBUTE_INTERFACE
))
3508 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx
->ip_offset
));
3510 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3511 char *from
= mono_method_full_name (ctx
->method
, TRUE
);
3512 char *to
= mono_method_full_name (method
, TRUE
);
3513 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
);
3518 //FIXME use mono_method_get_signature_full
3519 sig
= mono_method_signature (method
);
3521 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx
->ip_offset
));
3525 if (!check_underflow (ctx
, sig
->param_count
))
3528 is_delegate
= method
->klass
->parent
== mono_defaults
.multicastdelegate_class
;
3531 ILStackDesc
*funptr
;
3532 //first arg is object, second arg is fun ptr
3533 if (sig
->param_count
!= 2) {
3534 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx
->ip_offset
));
3537 funptr
= stack_pop (ctx
);
3538 value
= stack_pop (ctx
);
3539 verify_delegate_compatibility (ctx
, method
->klass
, value
, funptr
);
3541 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3542 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i
); );
3543 value
= stack_pop (ctx
);
3544 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3545 char *stack_name
= stack_slot_full_name (value
);
3546 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3547 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
));
3548 g_free (stack_name
);
3552 if (stack_slot_is_managed_mutability_pointer (value
))
3553 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx
->ip_offset
));
3557 if (check_overflow (ctx
))
3558 set_stack_value (ctx
, stack_push (ctx
), &method
->klass
->byval_arg
, FALSE
);
3562 do_cast (VerifyContext
*ctx
, int token
, const char *opcode
) {
3568 if (!check_underflow (ctx
, 1))
3571 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
3575 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid %s type at 0x%04x", opcode
, ctx
->ip_offset
));
3579 value
= stack_pop (ctx
);
3580 is_boxed
= stack_slot_is_boxed_value (value
);
3582 if (stack_slot_is_managed_pointer (value
))
3583 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
3584 else if (!MONO_TYPE_IS_REFERENCE (value
->type
) && !is_boxed
) {
3585 char *name
= stack_slot_full_name (value
);
3586 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
));
3590 switch (value
->type
->type
) {
3591 case MONO_TYPE_FNPTR
:
3593 case MONO_TYPE_TYPEDBYREF
:
3594 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
3597 do_box
= is_boxed
|| mono_type_is_generic_argument(type
) || mono_class_from_mono_type (type
)->valuetype
;
3598 stack_push_val (ctx
, TYPE_COMPLEX
| (do_box
? BOXED_MASK
: 0), type
);
3602 mono_type_from_opcode (int opcode
) {
3610 return &mono_defaults
.sbyte_class
->byval_arg
;
3618 return &mono_defaults
.int16_class
->byval_arg
;
3626 return &mono_defaults
.int32_class
->byval_arg
;
3632 return &mono_defaults
.int64_class
->byval_arg
;
3638 return &mono_defaults
.single_class
->byval_arg
;
3644 return &mono_defaults
.double_class
->byval_arg
;
3650 return &mono_defaults
.int_class
->byval_arg
;
3654 case CEE_LDELEM_REF
:
3655 case CEE_STELEM_REF
:
3656 return &mono_defaults
.object_class
->byval_arg
;
3659 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode
);
3665 do_load_indirect (VerifyContext
*ctx
, int opcode
)
3668 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3670 if (!check_underflow (ctx
, 1))
3673 value
= stack_pop (ctx
);
3674 if (!stack_slot_is_managed_pointer (value
)) {
3675 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx
->ip_offset
));
3676 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
3680 if (opcode
== CEE_LDIND_REF
) {
3681 if (stack_slot_get_underlying_type (value
) != TYPE_COMPLEX
|| mono_class_from_mono_type (value
->type
)->valuetype
)
3682 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx
->ip_offset
));
3683 set_stack_value (ctx
, stack_push (ctx
), mono_type_get_type_byval (value
->type
), FALSE
);
3685 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (value
->type
), TRUE
))
3686 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
3687 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
3692 do_store_indirect (VerifyContext
*ctx
, int opcode
)
3694 ILStackDesc
*addr
, *val
;
3695 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3697 if (!check_underflow (ctx
, 2))
3700 val
= stack_pop (ctx
);
3701 addr
= stack_pop (ctx
);
3703 check_unmanaged_pointer (ctx
, addr
);
3705 if (!stack_slot_is_managed_pointer (addr
) && stack_slot_get_type (addr
) != TYPE_PTR
) {
3706 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx
->ip_offset
));
3710 if (stack_slot_is_managed_mutability_pointer (addr
)) {
3711 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx
->ip_offset
));
3715 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (addr
->type
), TRUE
))
3716 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
3718 if (!verify_stack_type_compatibility (ctx
, mono_type_from_opcode (opcode
), val
))
3719 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
3723 do_newarr (VerifyContext
*ctx
, int token
)
3726 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "newarr");
3731 if (!check_underflow (ctx
, 1))
3734 value
= stack_pop (ctx
);
3735 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
3736 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
));
3738 set_stack_value (ctx
, stack_push (ctx
), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type
), 1)), FALSE
);
3741 /*FIXME handle arrays that are not 0-indexed*/
3743 do_ldlen (VerifyContext
*ctx
)
3747 if (!check_underflow (ctx
, 1))
3750 value
= stack_pop (ctx
);
3752 if (stack_slot_get_type (value
) != TYPE_COMPLEX
|| value
->type
->type
!= MONO_TYPE_SZARRAY
)
3753 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx
->ip_offset
));
3755 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3758 /*FIXME handle arrays that are not 0-indexed*/
3759 /*FIXME handle readonly prefix and CMMP*/
3761 do_ldelema (VerifyContext
*ctx
, int klass_token
)
3763 ILStackDesc
*index
, *array
, *res
;
3764 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "ldelema");
3770 if (!check_underflow (ctx
, 2))
3773 index
= stack_pop (ctx
);
3774 array
= stack_pop (ctx
);
3776 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
3777 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
));
3779 if (!stack_slot_is_null_literal (array
)) {
3780 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
3781 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array
), ctx
->ip_offset
));
3783 if (get_stack_type (type
) == TYPE_I4
|| get_stack_type (type
) == TYPE_NATIVE_INT
) {
3784 valid
= verify_type_compatibility_full (ctx
, type
, &array
->type
->data
.klass
->byval_arg
, TRUE
);
3786 valid
= mono_metadata_type_equal (type
, &array
->type
->data
.klass
->byval_arg
);
3789 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx
->ip_offset
));
3793 res
= stack_push (ctx
);
3794 set_stack_value (ctx
, res
, type
, TRUE
);
3795 if (ctx
->prefix_set
& PREFIX_READONLY
) {
3796 ctx
->prefix_set
&= ~PREFIX_READONLY
;
3797 res
->stype
|= CMMP_MASK
;
3802 * FIXME handle arrays that are not 0-indexed
3803 * FIXME handle readonly prefix and CMMP
3806 do_ldelem (VerifyContext
*ctx
, int opcode
, int token
)
3808 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3809 ILStackDesc
*index
, *array
;
3811 if (!check_underflow (ctx
, 2))
3814 if (opcode
== CEE_LDELEM
) {
3815 if (!(type
= verifier_load_type (ctx
, token
, "ldelem.any"))) {
3816 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
3820 type
= mono_type_from_opcode (opcode
);
3823 index
= stack_pop (ctx
);
3824 array
= stack_pop (ctx
);
3826 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
3827 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
));
3829 if (!stack_slot_is_null_literal (array
)) {
3830 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
3831 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
));
3833 if (opcode
== CEE_LDELEM_REF
) {
3834 if (array
->type
->data
.klass
->valuetype
)
3835 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx
->ip_offset
));
3836 type
= &array
->type
->data
.klass
->byval_arg
;
3838 MonoType
*candidate
= &array
->type
->data
.klass
->byval_arg
;
3839 if (IS_STRICT_MODE (ctx
)) {
3840 MonoType
*underlying_type
= mono_type_get_underlying_type_any (type
);
3841 MonoType
*underlying_candidate
= mono_type_get_underlying_type_any (candidate
);
3842 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
)) ||
3843 (IS_ONE_OF2 (underlying_candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
) && IS_ONE_OF2 (underlying_type
->type
, MONO_TYPE_I
, MONO_TYPE_U
)))
3844 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
3846 if (!verify_type_compatibility_full (ctx
, type
, candidate
, TRUE
))
3847 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
3852 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3857 * FIXME handle arrays that are not 0-indexed
3860 do_stelem (VerifyContext
*ctx
, int opcode
, int token
)
3862 ILStackDesc
*index
, *array
, *value
;
3864 if (!check_underflow (ctx
, 3))
3867 if (opcode
== CEE_STELEM
) {
3868 if (!(type
= verifier_load_type (ctx
, token
, "stelem.any"))) {
3869 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
3873 type
= mono_type_from_opcode (opcode
);
3876 value
= stack_pop (ctx
);
3877 index
= stack_pop (ctx
);
3878 array
= stack_pop (ctx
);
3880 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
3881 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
));
3883 if (!stack_slot_is_null_literal (array
)) {
3884 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
) {
3885 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
));
3887 if (opcode
== CEE_STELEM_REF
) {
3888 if (array
->type
->data
.klass
->valuetype
)
3889 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
3890 } else if (!verify_type_compatibility_full (ctx
, &array
->type
->data
.klass
->byval_arg
, type
, TRUE
)) {
3891 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
3895 if (opcode
== CEE_STELEM_REF
) {
3896 if (!stack_slot_is_boxed_value (value
) && mono_class_from_mono_type (value
->type
)->valuetype
)
3897 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
3898 } else if (opcode
!= CEE_STELEM_REF
) {
3899 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3900 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
3902 if (stack_slot_is_boxed_value (value
) && !MONO_TYPE_IS_REFERENCE (value
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3903 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
));
3909 do_throw (VerifyContext
*ctx
)
3911 ILStackDesc
*exception
;
3912 if (!check_underflow (ctx
, 1))
3914 exception
= stack_pop (ctx
);
3916 if (!stack_slot_is_null_literal (exception
) && !(stack_slot_get_type (exception
) == TYPE_COMPLEX
&& !mono_class_from_mono_type (exception
->type
)->valuetype
))
3917 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx
->ip_offset
));
3919 if (mono_type_is_generic_argument (exception
->type
) && !stack_slot_is_boxed_value (exception
)) {
3920 char *name
= mono_type_full_name (exception
->type
);
3921 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
));
3924 /*The stack is left empty after a throw*/
3930 do_endfilter (VerifyContext
*ctx
)
3932 MonoExceptionClause
*clause
;
3934 if (IS_STRICT_MODE (ctx
)) {
3935 if (ctx
->eval
.size
!= 1)
3936 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx
->ip_offset
));
3938 if (ctx
->eval
.size
>= 1 && stack_slot_get_type (stack_pop (ctx
)) != TYPE_I4
)
3939 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx
->ip_offset
));
3942 if ((clause
= is_correct_endfilter (ctx
, ctx
->ip_offset
))) {
3943 if (IS_STRICT_MODE (ctx
)) {
3944 if (ctx
->ip_offset
!= clause
->handler_offset
- 2)
3945 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
3947 if ((ctx
->ip_offset
!= clause
->handler_offset
- 2) && !MONO_OFFSET_IN_HANDLER (clause
, ctx
->ip_offset
))
3948 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
3951 if (IS_STRICT_MODE (ctx
) && !is_unverifiable_endfilter (ctx
, ctx
->ip_offset
))
3952 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
3954 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
3961 do_leave (VerifyContext
*ctx
, int delta
)
3963 int target
= ((gint32
)ctx
->ip_offset
) + delta
;
3964 if (target
>= ctx
->code_size
|| target
< 0)
3965 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
3967 if (!is_correct_leave (ctx
->header
, ctx
->ip_offset
, target
))
3968 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx
->ip_offset
));
3975 * Verify br and br.s opcodes.
3978 do_static_branch (VerifyContext
*ctx
, int delta
)
3980 int target
= ctx
->ip_offset
+ delta
;
3981 if (target
< 0 || target
>= ctx
->code_size
) {
3982 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("branch target out of code at 0x%04x", ctx
->ip_offset
));
3986 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3988 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3991 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3995 ctx
->target
= target
;
3999 do_switch (VerifyContext
*ctx
, int count
, const unsigned char *data
)
4001 int i
, base
= ctx
->ip_offset
+ 5 + count
* 4;
4004 if (!check_underflow (ctx
, 1))
4007 value
= stack_pop (ctx
);
4009 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
4010 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx
->ip_offset
));
4012 for (i
= 0; i
< count
; ++i
) {
4013 int target
= base
+ read32 (data
+ i
* 4);
4015 if (target
< 0 || target
>= ctx
->code_size
) {
4016 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x out of code at 0x%04x", i
, ctx
->ip_offset
));
4020 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
4022 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4025 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
4028 merge_stacks (ctx
, &ctx
->eval
, &ctx
->code
[target
], FALSE
, TRUE
);
4033 do_load_function_ptr (VerifyContext
*ctx
, guint32 token
, gboolean
virtual)
4038 if (virtual && !check_underflow (ctx
, 1))
4041 if (!virtual && !check_overflow (ctx
))
4044 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4045 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4049 if (!(method
= verifier_load_method (ctx
, token
, virtual ? "ldvirtfrn" : "ldftn")))
4052 if (mono_method_is_constructor (method
))
4053 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx
->ip_offset
));
4056 ILStackDesc
*top
= stack_pop (ctx
);
4058 if (stack_slot_get_type (top
) != TYPE_COMPLEX
|| top
->type
->type
== MONO_TYPE_VALUETYPE
)
4059 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx
->ip_offset
));
4061 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
)
4062 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx
->ip_offset
));
4064 if (!verify_stack_type_compatibility (ctx
, &method
->klass
->byval_arg
, top
))
4065 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx
->ip_offset
));
4068 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
))
4069 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
);
4071 top
= stack_push_val(ctx
, TYPE_PTR
, mono_type_create_fnptr_from_mono_method (ctx
, method
));
4072 top
->method
= method
;
4076 do_sizeof (VerifyContext
*ctx
, int token
)
4080 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4081 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4085 if (!(type
= verifier_load_type (ctx
, token
, "sizeof")))
4088 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
4089 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx
->ip_offset
));
4093 if (type
->type
== MONO_TYPE_VOID
) {
4094 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx
->ip_offset
));
4098 if (check_overflow (ctx
))
4099 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.uint32_class
->byval_arg
, FALSE
);
4102 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4104 do_localloc (VerifyContext
*ctx
)
4108 if (ctx
->eval
.size
!= 1) {
4109 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4113 if (in_any_exception_block (ctx
->header
, ctx
->ip_offset
)) {
4114 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4118 /*TODO verify top type*/
4119 top
= stack_pop (ctx
);
4121 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.int_class
->byval_arg
, FALSE
);
4122 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx
->ip_offset
));
4126 do_ldstr (VerifyContext
*ctx
, guint32 token
)
4128 GSList
*error
= NULL
;
4129 if (mono_metadata_token_code (token
) != MONO_TOKEN_STRING
) {
4130 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4134 if (!ctx
->image
->dynamic
&& !mono_verifier_verify_string_signature (ctx
->image
, mono_metadata_token_index (token
), &error
)) {
4136 ctx
->list
= g_slist_concat (ctx
->list
, error
);
4137 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string index %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4141 if (check_overflow (ctx
))
4142 stack_push_val (ctx
, TYPE_COMPLEX
, &mono_defaults
.string_class
->byval_arg
);
4146 do_refanyval (VerifyContext
*ctx
, int token
)
4150 if (!check_underflow (ctx
, 1))
4153 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4156 top
= stack_pop (ctx
);
4158 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4159 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
));
4161 set_stack_value (ctx
, stack_push (ctx
), type
, TRUE
);
4165 do_refanytype (VerifyContext
*ctx
)
4169 if (!check_underflow (ctx
, 1))
4172 top
= stack_pop (ctx
);
4174 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4175 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
));
4177 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typehandle_class
->byval_arg
, FALSE
);
4182 do_mkrefany (VerifyContext
*ctx
, int token
)
4186 if (!check_underflow (ctx
, 1))
4189 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4192 top
= stack_pop (ctx
);
4194 if (stack_slot_is_managed_mutability_pointer (top
))
4195 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx
->ip_offset
));
4197 if (!stack_slot_is_managed_pointer (top
)) {
4198 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
));
4200 MonoType
*stack_type
= mono_type_get_type_byval (top
->type
);
4201 if (MONO_TYPE_IS_REFERENCE (type
) && !mono_metadata_type_equal (type
, stack_type
))
4202 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4204 if (!MONO_TYPE_IS_REFERENCE (type
) && !verify_type_compatibility_full (ctx
, type
, stack_type
, TRUE
))
4205 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4208 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typed_reference_class
->byval_arg
, FALSE
);
4212 do_ckfinite (VerifyContext
*ctx
)
4215 if (!check_underflow (ctx
, 1))
4218 top
= stack_pop (ctx
);
4220 if (stack_slot_get_underlying_type (top
) != TYPE_R8
)
4221 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
));
4222 stack_push_stack_val (ctx
, top
);
4226 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4228 * @from holds new values for a given control path
4229 * @to holds the current values of a given control path
4231 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4234 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
)
4238 stack_init (ctx
, to
);
4241 if (to
->flags
== IL_CODE_FLAG_NOT_PROCESSED
)
4244 stack_copy (&ctx
->eval
, to
);
4246 } else if (!(to
->flags
& IL_CODE_STACK_MERGED
)) {
4247 stack_copy (to
, &ctx
->eval
);
4250 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from
->size
, to
->size
); );
4252 if (from
->size
!= to
->size
) {
4253 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from
->size
, to
->size
, ctx
->ip_offset
); );
4254 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
));
4258 //FIXME we need to preserve CMMP attributes
4259 //FIXME we must take null literals into consideration.
4260 for (i
= 0; i
< from
->size
; ++i
) {
4261 ILStackDesc
*new_slot
= from
->stack
+ i
;
4262 ILStackDesc
*old_slot
= to
->stack
+ i
;
4263 MonoType
*new_type
= mono_type_from_stack_slot (new_slot
);
4264 MonoType
*old_type
= mono_type_from_stack_slot (old_slot
);
4265 MonoClass
*old_class
= mono_class_from_mono_type (old_type
);
4266 MonoClass
*new_class
= mono_class_from_mono_type (new_type
);
4267 MonoClass
*match_class
= NULL
;
4269 // S := T then U = S (new value is compatible with current value, keep current)
4270 if (verify_stack_type_compatibility (ctx
, old_type
, new_slot
)) {
4271 copy_stack_value (new_slot
, old_slot
);
4275 // T := S then U = T (old value is compatible with current value, use new)
4276 if (verify_stack_type_compatibility (ctx
, new_type
, old_slot
)) {
4277 copy_stack_value (old_slot
, new_slot
);
4281 if (mono_type_is_generic_argument (old_type
) || mono_type_is_generic_argument (new_type
)) {
4282 char *old_name
= stack_slot_full_name (old_slot
);
4283 char *new_name
= stack_slot_full_name (new_slot
);
4284 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
));
4290 //both are reference types, use closest common super type
4291 if (!mono_class_from_mono_type (old_type
)->valuetype
4292 && !mono_class_from_mono_type (new_type
)->valuetype
4293 && !stack_slot_is_managed_pointer (old_slot
)
4294 && !stack_slot_is_managed_pointer (new_slot
)) {
4296 for (j
= MIN (old_class
->idepth
, new_class
->idepth
) - 1; j
> 0; --j
) {
4297 if (mono_metadata_type_equal (&old_class
->supertypes
[j
]->byval_arg
, &new_class
->supertypes
[j
]->byval_arg
)) {
4298 match_class
= old_class
->supertypes
[j
];
4303 mono_class_setup_interfaces (old_class
, &error
);
4304 if (!mono_error_ok (&error
)) {
4305 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
));
4306 mono_error_cleanup (&error
);
4309 for (j
= 0; j
< old_class
->interface_count
; ++j
) {
4310 for (k
= 0; k
< new_class
->interface_count
; ++k
) {
4311 if (mono_metadata_type_equal (&old_class
->interfaces
[j
]->byval_arg
, &new_class
->interfaces
[k
]->byval_arg
)) {
4312 match_class
= old_class
->interfaces
[j
];
4318 //No decent super type found, use object
4319 match_class
= mono_defaults
.object_class
;
4321 } 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
)) {
4322 match_class
= mono_defaults
.object_class
;
4327 char *old_name
= stack_slot_full_name (old_slot
);
4328 char *new_name
= stack_slot_full_name (new_slot
);
4329 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
));
4333 set_stack_value (ctx
, old_slot
, &new_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4337 g_assert (match_class
);
4338 set_stack_value (ctx
, old_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4339 set_stack_value (ctx
, new_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4345 to
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
4346 to
->flags
|= IL_CODE_STACK_MERGED
;
4349 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4350 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4353 * is_clause_in_range :
4355 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4358 is_clause_in_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4360 if (clause
->try_offset
>= start
&& clause
->try_offset
< end
)
4362 if (HANDLER_START (clause
) >= start
&& HANDLER_START (clause
) < end
)
4368 * is_clause_inside_range :
4370 * Returns TRUE if @clause lies completely inside the @start - @end range.
4373 is_clause_inside_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4375 if (clause
->try_offset
< start
|| (clause
->try_offset
+ clause
->try_len
) > end
)
4377 if (HANDLER_START (clause
) < start
|| (clause
->handler_offset
+ clause
->handler_len
) > end
)
4383 * is_clause_nested :
4385 * Returns TRUE if @nested is nested in @clause.
4388 is_clause_nested (MonoExceptionClause
*clause
, MonoExceptionClause
*nested
)
4390 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (nested
, clause
->data
.filter_offset
, clause
->handler_offset
))
4392 return is_clause_inside_range (nested
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4393 is_clause_inside_range (nested
, clause
->handler_offset
, clause
->handler_offset
+ clause
->handler_len
);
4396 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4397 * the each pair of exception must have the following properties:
4398 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4399 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4400 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4403 verify_clause_relationship (VerifyContext
*ctx
, MonoExceptionClause
*clause
, MonoExceptionClause
*to_test
)
4405 /*clause is nested*/
4406 if (to_test
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (clause
, to_test
->data
.filter_offset
, to_test
->handler_offset
)) {
4407 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clause inside filter"));
4411 /*wrong nesting order.*/
4412 if (is_clause_nested (clause
, to_test
)) {
4413 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4417 /*mutual protection*/
4418 if (clause
->try_offset
== to_test
->try_offset
&& clause
->try_len
== to_test
->try_len
) {
4419 /*handlers are not disjoint*/
4420 if (is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) {
4421 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception handlers overlap"));
4424 /* handlers are not catch or filter */
4425 if (!IS_CATCH_OR_FILTER (clause
) || !IS_CATCH_OR_FILTER (to_test
)) {
4426 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4433 /*not completelly disjoint*/
4434 if ((is_clause_in_range (to_test
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4435 is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) && !is_clause_nested (to_test
, clause
))
4436 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses overlap"));
4439 #define code_bounds_check(size) \
4440 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4441 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4446 mono_opcode_is_prefix (int op
)
4449 case MONO_CEE_UNALIGNED_
:
4450 case MONO_CEE_VOLATILE_
:
4451 case MONO_CEE_TAIL_
:
4452 case MONO_CEE_CONSTRAINED_
:
4453 case MONO_CEE_READONLY_
:
4460 * FIXME: need to distinguish between valid and verifiable.
4461 * Need to keep track of types on the stack.
4462 * Verify types for opcodes.
4465 mono_method_verify (MonoMethod
*method
, int level
)
4468 const unsigned char *ip
, *code_start
;
4469 const unsigned char *end
;
4470 MonoSimpleBasicBlock
*bb
= NULL
, *original_bb
= NULL
;
4472 int i
, n
, need_merge
= 0, start
= 0;
4473 guint token
, ip_offset
= 0, prefix
= 0;
4474 MonoGenericContext
*generic_context
= NULL
;
4478 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method
->klass
->name_space
, method
->klass
->name
, method
->name
); );
4480 init_verifier_stats ();
4482 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4483 (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))) {
4487 memset (&ctx
, 0, sizeof (VerifyContext
));
4489 //FIXME use mono_method_get_signature_full
4490 ctx
.signature
= mono_method_signature (method
);
4491 if (!ctx
.signature
) {
4492 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method signature"));
4494 finish_collect_stats ();
4497 ctx
.header
= mono_method_get_header (method
);
4499 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method header"));
4500 finish_collect_stats ();
4503 ctx
.method
= method
;
4504 code_start
= ip
= ctx
.header
->code
;
4505 end
= ip
+ ctx
.header
->code_size
;
4506 ctx
.image
= image
= method
->klass
->image
;
4509 ctx
.max_args
= ctx
.signature
->param_count
+ ctx
.signature
->hasthis
;
4510 ctx
.max_stack
= ctx
.header
->max_stack
;
4511 ctx
.verifiable
= ctx
.valid
= 1;
4514 ctx
.code
= g_new (ILCodeDesc
, ctx
.header
->code_size
);
4515 ctx
.code_size
= ctx
.header
->code_size
;
4516 MEM_ALLOC (sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4518 memset(ctx
.code
, 0, sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4520 ctx
.num_locals
= ctx
.header
->num_locals
;
4521 ctx
.locals
= g_memdup (ctx
.header
->locals
, sizeof (MonoType
*) * ctx
.header
->num_locals
);
4522 MEM_ALLOC (sizeof (MonoType
*) * ctx
.header
->num_locals
);
4524 if (ctx
.num_locals
> 0 && !ctx
.header
->init_locals
)
4525 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Method with locals variable but without init locals set"));
4527 ctx
.params
= g_new (MonoType
*, ctx
.max_args
);
4528 MEM_ALLOC (sizeof (MonoType
*) * ctx
.max_args
);
4530 if (ctx
.signature
->hasthis
)
4531 ctx
.params
[0] = method
->klass
->valuetype
? &method
->klass
->this_arg
: &method
->klass
->byval_arg
;
4532 memcpy (ctx
.params
+ ctx
.signature
->hasthis
, ctx
.signature
->params
, sizeof (MonoType
*) * ctx
.signature
->param_count
);
4534 if (ctx
.signature
->is_inflated
)
4535 ctx
.generic_context
= generic_context
= mono_method_get_context (method
);
4537 if (!generic_context
&& (method
->klass
->generic_container
|| method
->is_generic
)) {
4538 if (method
->is_generic
)
4539 ctx
.generic_context
= generic_context
= &(mono_method_get_generic_container (method
)->context
);
4541 ctx
.generic_context
= generic_context
= &method
->klass
->generic_container
->context
;
4544 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4545 MonoType
*uninflated
= ctx
.locals
[i
];
4546 ctx
.locals
[i
] = mono_class_inflate_generic_type_checked (ctx
.locals
[i
], ctx
.generic_context
, &error
);
4547 if (!mono_error_ok (&error
)) {
4548 char *name
= mono_type_full_name (ctx
.locals
[i
] ? ctx
.locals
[i
] : uninflated
);
4549 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %d of type %s", i
, name
));
4551 mono_error_cleanup (&error
);
4552 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4558 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4559 MonoType
*uninflated
= ctx
.params
[i
];
4560 ctx
.params
[i
] = mono_class_inflate_generic_type_checked (ctx
.params
[i
], ctx
.generic_context
, &error
);
4561 if (!mono_error_ok (&error
)) {
4562 char *name
= mono_type_full_name (ctx
.params
[i
] ? ctx
.params
[i
] : uninflated
);
4563 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %d of type %s", i
, name
));
4565 mono_error_cleanup (&error
);
4566 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4571 stack_init (&ctx
, &ctx
.eval
);
4573 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4574 if (!mono_type_is_valid_in_context (&ctx
, ctx
.locals
[i
]))
4576 if (get_stack_type (ctx
.locals
[i
]) == TYPE_INV
) {
4577 char *name
= mono_type_full_name (ctx
.locals
[i
]);
4578 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %i of type %s", i
, name
));
4585 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4586 if (!mono_type_is_valid_in_context (&ctx
, ctx
.params
[i
]))
4589 if (get_stack_type (ctx
.params
[i
]) == TYPE_INV
) {
4590 char *name
= mono_type_full_name (ctx
.params
[i
]);
4591 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %i of type %s", i
, name
));
4600 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
4601 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
4602 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
); );
4604 if (clause
->try_offset
> ctx
.code_size
|| ADD_IS_GREATER_OR_OVF (clause
->try_offset
, clause
->try_len
, ctx
.code_size
))
4605 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause out of bounds at 0x%04x", clause
->try_offset
));
4607 if (clause
->try_len
<= 0)
4608 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
4610 if (clause
->handler_offset
> ctx
.code_size
|| ADD_IS_GREATER_OR_OVF (clause
->handler_offset
, clause
->handler_len
, ctx
.code_size
))
4611 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause
->try_offset
));
4613 if (clause
->handler_len
<= 0)
4614 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause
->try_offset
));
4616 if (clause
->try_offset
< clause
->handler_offset
&& ADD_IS_GREATER_OR_OVF (clause
->try_offset
, clause
->try_len
, HANDLER_START (clause
)))
4617 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause
->try_offset
, clause
->handler_offset
));
4619 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
4620 if (clause
->data
.filter_offset
> ctx
.code_size
)
4621 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause
->try_offset
));
4623 if (clause
->data
.filter_offset
>= clause
->handler_offset
)
4624 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause
->data
.filter_offset
));
4627 for (n
= i
+ 1; n
< ctx
.header
->num_clauses
&& ctx
.valid
; ++n
)
4628 verify_clause_relationship (&ctx
, clause
, ctx
.header
->clauses
+ n
);
4633 ctx
.code
[clause
->try_offset
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4634 if (clause
->try_offset
+ clause
->try_len
< ctx
.code_size
)
4635 ctx
.code
[clause
->try_offset
+ clause
->try_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4636 if (clause
->handler_offset
+ clause
->handler_len
< ctx
.code_size
)
4637 ctx
.code
[clause
->handler_offset
+ clause
->handler_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4639 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
) {
4640 if (!clause
->data
.catch_class
) {
4641 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Catch clause %d with invalid type", i
));
4645 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, clause
->data
.catch_class
);
4647 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
4648 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->data
.filter_offset
, mono_defaults
.exception_class
);
4649 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, mono_defaults
.exception_class
);
4653 original_bb
= bb
= mono_basic_block_split (method
, &error
);
4654 if (!mono_error_ok (&error
)) {
4655 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error
)));
4656 mono_error_cleanup (&error
);
4661 while (ip
< end
&& ctx
.valid
) {
4662 ip_offset
= ip
- code_start
;
4664 const unsigned char *ip_copy
= ip
;
4667 if (ip_offset
> bb
->end
) {
4668 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb
->end
, ip_offset
));
4672 if (ip_offset
== bb
->end
)
4675 size
= mono_opcode_value_and_size (&ip_copy
, end
, &op
);
4677 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ip_offset
));
4681 if (ADD_IS_GREATER_OR_OVF (ip_offset
, size
, bb
->end
)) {
4682 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset
));
4686 /*Last Instruction*/
4687 if (ip_offset
+ size
== bb
->end
&& mono_opcode_is_prefix (op
)) {
4688 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
));
4693 /*FIXME remove this once we move all bad branch checking code to use BB only*/
4694 ctx
.code
[ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
4700 ctx
.ip_offset
= ip_offset
= ip
- code_start
;
4702 /*We need to check against fallthrou in and out of protected blocks.
4703 * For fallout we check the once a protected block ends, if the start flag is not set.
4704 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4705 * TODO convert these checks to be done using flags and not this loop
4707 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
4708 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
4710 if ((clause
->try_offset
+ clause
->try_len
== ip_offset
) && start
== 0) {
4711 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset
));
4715 if ((clause
->handler_offset
+ clause
->handler_len
== ip_offset
) && start
== 0) {
4716 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
4717 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
4719 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
4723 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& clause
->handler_offset
== ip_offset
&& start
== 0) {
4724 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset
));
4728 if (clause
->handler_offset
== ip_offset
&& start
== 0) {
4729 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset
));
4733 if (clause
->try_offset
== ip_offset
&& ctx
.eval
.size
> 0 && start
== 0) {
4734 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset
));
4743 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx
.target
); );
4744 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ctx
.target
], FALSE
, TRUE
);
4747 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ip_offset
], start
, FALSE
);
4750 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4751 #ifdef MONO_VERIFIER_DEBUG
4754 discode
= mono_disasm_code_one (NULL
, method
, ip
, NULL
);
4755 discode
[strlen (discode
) - 1] = 0; /* no \n */
4756 g_print ("[%d] %-29s (%d)\n", ip_offset
, discode
, ctx
.eval
.size
);
4759 dump_stack_state (&ctx
.code
[ip_offset
]);
4760 dump_stack_state (&ctx
.eval
);
4773 push_arg (&ctx
, *ip
- CEE_LDARG_0
, FALSE
);
4779 code_bounds_check (2);
4780 push_arg (&ctx
, ip
[1], *ip
== CEE_LDARGA_S
);
4784 case CEE_ADD_OVF_UN
:
4785 do_binop (&ctx
, *ip
, add_ovf_un_table
);
4789 case CEE_SUB_OVF_UN
:
4790 do_binop (&ctx
, *ip
, sub_ovf_un_table
);
4797 case CEE_MUL_OVF_UN
:
4798 do_binop (&ctx
, *ip
, bin_ovf_table
);
4803 do_binop (&ctx
, *ip
, add_table
);
4808 do_binop (&ctx
, *ip
, sub_table
);
4815 do_binop (&ctx
, *ip
, bin_op_table
);
4824 do_binop (&ctx
, *ip
, int_bin_op_table
);
4831 do_binop (&ctx
, *ip
, shift_op_table
);
4836 if (!check_underflow (&ctx
, 1))
4838 stack_pop_safe (&ctx
);
4852 /*TODO support definite assignment verification? */
4853 push_local (&ctx
, *ip
- CEE_LDLOC_0
, FALSE
);
4861 store_local (&ctx
, *ip
- CEE_STLOC_0
);
4866 code_bounds_check (2);
4867 store_local (&ctx
, ip
[1]);
4872 code_bounds_check (2);
4873 store_arg (&ctx
, ip
[1]);
4887 if (check_overflow (&ctx
))
4888 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
4893 code_bounds_check (2);
4894 if (check_overflow (&ctx
))
4895 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
4900 code_bounds_check (5);
4901 if (check_overflow (&ctx
))
4902 stack_push_val (&ctx
,TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
4907 code_bounds_check (9);
4908 if (check_overflow (&ctx
))
4909 stack_push_val (&ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
4914 code_bounds_check (5);
4915 if (check_overflow (&ctx
))
4916 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
4921 code_bounds_check (9);
4922 if (check_overflow (&ctx
))
4923 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
4928 if (check_overflow (&ctx
))
4929 stack_push_val (&ctx
, TYPE_COMPLEX
| NULL_LITERAL_MASK
, &mono_defaults
.object_class
->byval_arg
);
4935 code_bounds_check (2);
4936 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_eq_op
);
4949 code_bounds_check (2);
4950 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_op
);
4957 code_bounds_check (5);
4958 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_eq_op
);
4971 code_bounds_check (5);
4972 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_op
);
4979 code_bounds_check (2);
4980 push_local (&ctx
, ip
[1], *ip
== CEE_LDLOCA_S
);
4985 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
4991 if (!check_underflow (&ctx
, 1))
4993 if (!check_overflow (&ctx
))
4995 top
= stack_push (&ctx
);
4996 copy_stack_value (top
, stack_peek (&ctx
, 1));
5002 code_bounds_check (5);
5004 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset
));
5005 token
= read32 (ip
+ 1);
5006 if (in_any_block (ctx
.header
, ip_offset
))
5007 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset
));
5009 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx
.ip_offset
));
5011 * FIXME: check signature, retval, arguments etc.
5017 code_bounds_check (5);
5018 do_invoke_method (&ctx
, read32 (ip
+ 1), *ip
== CEE_CALLVIRT
);
5023 code_bounds_check (5);
5024 token
= read32 (ip
+ 1);
5026 * FIXME: check signature, retval, arguments etc.
5027 * FIXME: check requirements for tail call
5029 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx
.ip_offset
));
5033 code_bounds_check (2);
5034 do_static_branch (&ctx
, (signed char)ip
[1] + 2);
5042 code_bounds_check (2);
5043 do_boolean_branch_op (&ctx
, (signed char)ip
[1] + 2);
5049 code_bounds_check (5);
5050 do_static_branch (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5058 code_bounds_check (5);
5059 do_boolean_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5066 code_bounds_check (5);
5067 entries
= read32 (ip
+ 1);
5069 if (entries
> 0xFFFFFFFFU
/ sizeof (guint32
))
5070 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries
, ctx
.ip_offset
));
5073 code_bounds_check (sizeof (guint32
) * entries
);
5075 do_switch (&ctx
, entries
, ip
);
5076 ip
+= sizeof (guint32
) * entries
;
5090 do_load_indirect (&ctx
, *ip
);
5102 do_store_indirect (&ctx
, *ip
);
5108 do_unary_math_op (&ctx
, *ip
);
5118 do_conversion (&ctx
, TYPE_I4
);
5124 do_conversion (&ctx
, TYPE_I8
);
5131 do_conversion (&ctx
, TYPE_R8
);
5137 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5142 code_bounds_check (5);
5143 do_cpobj (&ctx
, read32 (ip
+ 1));
5148 code_bounds_check (5);
5149 do_ldobj_value (&ctx
, read32 (ip
+ 1));
5154 code_bounds_check (5);
5155 do_ldstr (&ctx
, read32 (ip
+ 1));
5160 code_bounds_check (5);
5161 do_newobj (&ctx
, read32 (ip
+ 1));
5167 code_bounds_check (5);
5168 do_cast (&ctx
, read32 (ip
+ 1), *ip
== CEE_CASTCLASS
? "castclass" : "isinst");
5174 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
5179 code_bounds_check (5);
5180 do_unbox_value (&ctx
, read32 (ip
+ 1));
5192 code_bounds_check (5);
5193 do_push_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDFLDA
);
5199 code_bounds_check (5);
5200 do_push_static_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDSFLDA
);
5205 code_bounds_check (5);
5206 do_store_field (&ctx
, read32 (ip
+ 1));
5211 code_bounds_check (5);
5212 do_store_static_field (&ctx
, read32 (ip
+ 1));
5217 code_bounds_check (5);
5218 do_stobj (&ctx
, read32 (ip
+ 1));
5222 case CEE_CONV_OVF_I1_UN
:
5223 case CEE_CONV_OVF_I2_UN
:
5224 case CEE_CONV_OVF_I4_UN
:
5225 case CEE_CONV_OVF_U1_UN
:
5226 case CEE_CONV_OVF_U2_UN
:
5227 case CEE_CONV_OVF_U4_UN
:
5228 do_conversion (&ctx
, TYPE_I4
);
5232 case CEE_CONV_OVF_I8_UN
:
5233 case CEE_CONV_OVF_U8_UN
:
5234 do_conversion (&ctx
, TYPE_I8
);
5238 case CEE_CONV_OVF_I_UN
:
5239 case CEE_CONV_OVF_U_UN
:
5240 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5245 code_bounds_check (5);
5246 do_box_value (&ctx
, read32 (ip
+ 1));
5251 code_bounds_check (5);
5252 do_newarr (&ctx
, read32 (ip
+ 1));
5262 code_bounds_check (5);
5263 do_ldelema (&ctx
, read32 (ip
+ 1));
5277 case CEE_LDELEM_REF
:
5278 do_ldelem (&ctx
, *ip
, 0);
5289 case CEE_STELEM_REF
:
5290 do_stelem (&ctx
, *ip
, 0);
5295 code_bounds_check (5);
5296 do_ldelem (&ctx
, *ip
, read32 (ip
+ 1));
5301 code_bounds_check (5);
5302 do_stelem (&ctx
, *ip
, read32 (ip
+ 1));
5307 code_bounds_check (5);
5308 do_unbox_any (&ctx
, read32 (ip
+ 1));
5312 case CEE_CONV_OVF_I1
:
5313 case CEE_CONV_OVF_U1
:
5314 case CEE_CONV_OVF_I2
:
5315 case CEE_CONV_OVF_U2
:
5316 case CEE_CONV_OVF_I4
:
5317 case CEE_CONV_OVF_U4
:
5318 do_conversion (&ctx
, TYPE_I4
);
5322 case CEE_CONV_OVF_I8
:
5323 case CEE_CONV_OVF_U8
:
5324 do_conversion (&ctx
, TYPE_I8
);
5328 case CEE_CONV_OVF_I
:
5329 case CEE_CONV_OVF_U
:
5330 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5335 code_bounds_check (5);
5336 do_refanyval (&ctx
, read32 (ip
+ 1));
5346 code_bounds_check (5);
5347 do_mkrefany (&ctx
, read32 (ip
+ 1));
5352 code_bounds_check (5);
5353 do_load_token (&ctx
, read32 (ip
+ 1));
5357 case CEE_ENDFINALLY
:
5358 if (!is_correct_endfinally (ctx
.header
, ip_offset
))
5359 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx
.ip_offset
));
5366 code_bounds_check (5);
5367 do_leave (&ctx
, read32 (ip
+ 1) + 5);
5373 code_bounds_check (2);
5374 do_leave (&ctx
, (signed char)ip
[1] + 2);
5380 code_bounds_check (2);
5384 code_bounds_check (3);
5385 store_local (&ctx
, read16 (ip
+ 1));
5390 do_cmp_op (&ctx
, cmp_br_eq_op
, *ip
);
5398 do_cmp_op (&ctx
, cmp_br_op
, *ip
);
5403 code_bounds_check (3);
5404 store_arg (&ctx
, read16 (ip
+ 1) );
5410 if (!check_overflow (&ctx
))
5412 if (ctx
.signature
->call_convention
!= MONO_CALL_VARARG
)
5413 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx
.ip_offset
));
5414 set_stack_value (&ctx
, stack_push (&ctx
), &mono_defaults
.argumenthandle_class
->byval_arg
, FALSE
);
5419 code_bounds_check (5);
5420 do_load_function_ptr (&ctx
, read32 (ip
+ 1), FALSE
);
5425 code_bounds_check (5);
5426 do_load_function_ptr (&ctx
, read32 (ip
+ 1), TRUE
);
5432 code_bounds_check (3);
5433 push_arg (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDARGA
);
5439 code_bounds_check (3);
5440 push_local (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDLOCA
);
5453 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
5457 do_endfilter (&ctx
);
5461 case CEE_UNALIGNED_
:
5462 code_bounds_check (2);
5463 prefix
|= PREFIX_UNALIGNED
;
5467 prefix
|= PREFIX_VOLATILE
;
5471 prefix
|= PREFIX_TAIL
;
5473 if (ip
< end
&& (*ip
!= CEE_CALL
&& *ip
!= CEE_CALLI
&& *ip
!= CEE_CALLVIRT
))
5474 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset
));
5478 code_bounds_check (5);
5479 do_initobj (&ctx
, read32 (ip
+ 1));
5483 case CEE_CONSTRAINED_
:
5484 code_bounds_check (5);
5485 ctx
.constrained_type
= get_boxable_mono_type (&ctx
, read32 (ip
+ 1), "constrained.");
5486 prefix
|= PREFIX_CONSTRAINED
;
5491 prefix
|= PREFIX_READONLY
;
5496 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5497 if (!check_underflow (&ctx
, 3))
5499 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5504 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5505 if (!check_underflow (&ctx
, 3))
5507 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5515 if (!is_correct_rethrow (ctx
.header
, ip_offset
))
5516 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx
.ip_offset
));
5523 code_bounds_check (5);
5524 do_sizeof (&ctx
, read32 (ip
+ 1));
5528 case CEE_REFANYTYPE
:
5529 do_refanytype (&ctx
);
5534 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip
, ctx
.ip_offset
));
5540 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ctx
.ip_offset
));
5544 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5546 if (!ctx
.prefix_set
) //first prefix
5547 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5548 ctx
.prefix_set
|= prefix
;
5549 ctx
.has_flags
= TRUE
;
5553 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5555 if (ctx
.prefix_set
& PREFIX_CONSTRAINED
)
5556 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx
.ip_offset
));
5557 if (ctx
.prefix_set
& PREFIX_READONLY
)
5558 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx
.ip_offset
));
5559 if (ctx
.prefix_set
& PREFIX_VOLATILE
)
5560 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx
.ip_offset
));
5561 if (ctx
.prefix_set
& PREFIX_UNALIGNED
)
5562 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx
.ip_offset
));
5563 ctx
.prefix_set
= prefix
= 0;
5564 ctx
.has_flags
= FALSE
;
5568 * if ip != end we overflowed: mark as error.
5570 if ((ip
!= end
|| !start
) && ctx
.verifiable
&& !ctx
.list
) {
5571 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset
));
5574 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5575 for (i
= 0; i
< ctx
.code_size
&& i
< ip_offset
; ++i
) {
5576 if (ctx
.code
[i
].flags
& IL_CODE_FLAG_WAS_TARGET
) {
5577 if (!(ctx
.code
[i
].flags
& IL_CODE_FLAG_SEEN
))
5578 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i
));
5580 if (ctx
.code
[i
].flags
& IL_CODE_DELEGATE_SEQUENCE
)
5581 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i
));
5583 if ((ctx
.code
[i
].flags
& IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5584 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i
));
5586 if ((ctx
.code
[i
].flags
& IL_CODE_CALL_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5587 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
));
5590 if (mono_method_is_constructor (ctx
.method
) && !ctx
.super_ctor_called
&& !ctx
.method
->klass
->valuetype
&& ctx
.method
->klass
!= mono_defaults
.object_class
) {
5591 char *method_name
= mono_method_full_name (ctx
.method
, TRUE
);
5592 char *type
= mono_type_get_full_name (ctx
.method
->klass
);
5593 if (ctx
.method
->klass
->parent
&& ctx
.method
->klass
->parent
->exception_type
!= MONO_EXCEPTION_NONE
)
5594 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
));
5596 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name
, type
));
5597 g_free (method_name
);
5603 for (i
= 0; i
< ctx
.header
->code_size
; ++i
) {
5604 if (ctx
.code
[i
].stack
)
5605 g_free (ctx
.code
[i
].stack
);
5609 for (tmp
= ctx
.funptrs
; tmp
; tmp
= tmp
->next
)
5611 g_slist_free (ctx
.funptrs
);
5613 for (tmp
= ctx
.exception_types
; tmp
; tmp
= tmp
->next
)
5614 mono_metadata_free_type (tmp
->data
);
5615 g_slist_free (ctx
.exception_types
);
5617 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
5619 mono_metadata_free_type (ctx
.locals
[i
]);
5621 for (i
= 0; i
< ctx
.max_args
; ++i
) {
5623 mono_metadata_free_type (ctx
.params
[i
]);
5627 g_free (ctx
.eval
.stack
);
5630 g_free (ctx
.locals
);
5631 g_free (ctx
.params
);
5632 mono_basic_block_free (original_bb
);
5633 mono_metadata_free_mh (ctx
.header
);
5635 finish_collect_stats ();
5640 mono_verify_corlib ()
5642 /* This is a public API function so cannot be removed */
5647 * Returns true if @method needs to be verified.
5651 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
5653 return mono_verifier_is_enabled_for_class (method
->klass
) && method
->wrapper_type
== MONO_WRAPPER_NONE
;
5657 * Returns true if @klass need to be verified.
5661 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
5663 return verify_all
|| (verifier_mode
> MONO_VERIFIER_MODE_OFF
&& !(klass
->image
->assembly
&& klass
->image
->assembly
->in_gac
) && klass
->image
!= mono_defaults
.corlib
);
5667 mono_verifier_is_enabled_for_image (MonoImage
*image
)
5669 return verify_all
|| verifier_mode
> MONO_VERIFIER_MODE_OFF
;
5673 mono_verifier_is_method_full_trust (MonoMethod
*method
)
5675 return mono_verifier_is_class_full_trust (method
->klass
);
5679 * Returns if @klass is under full trust or not.
5681 * TODO This code doesn't take CAS into account.
5683 * Under verify_all all user code must be verifiable if no security option was set
5687 mono_verifier_is_class_full_trust (MonoClass
*klass
)
5689 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5690 gboolean trusted_location
= (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR
) ?
5691 (klass
->image
->assembly
&& klass
->image
->assembly
->in_gac
) : mono_security_core_clr_is_platform_image (klass
->image
);
5693 if (verify_all
&& verifier_mode
== MONO_VERIFIER_MODE_OFF
)
5694 return trusted_location
|| klass
->image
== mono_defaults
.corlib
;
5695 return verifier_mode
< MONO_VERIFIER_MODE_VERIFIABLE
|| trusted_location
|| klass
->image
== mono_defaults
.corlib
;
5699 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
5701 return mono_method_verify (method
,
5702 (verifier_mode
!= MONO_VERIFIER_MODE_STRICT
? MONO_VERIFY_NON_STRICT
: 0)
5703 | (!mono_verifier_is_method_full_trust (method
) ? MONO_VERIFY_FAIL_FAST
: 0)
5704 | (skip_visibility
? MONO_VERIFY_SKIP_VISIBILITY
: 0));
5708 get_field_end (MonoClassField
*field
)
5711 int size
= mono_type_size (field
->type
, &align
);
5713 size
= 4; /*FIXME Is this a safe bet?*/
5714 return size
+ field
->offset
;
5718 verify_class_for_overlapping_reference_fields (MonoClass
*class)
5721 gpointer iter
= NULL
;
5722 MonoClassField
*field
;
5723 gboolean is_fulltrust
= mono_verifier_is_class_full_trust (class);
5724 /*We can't skip types with !has_references since this is calculated after we have run.*/
5725 if (!((class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
))
5729 /*We must check for stuff overlapping reference fields.
5730 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
5732 while ((field
= mono_class_get_fields (class, &iter
))) {
5733 int fieldEnd
= get_field_end (field
);
5734 gboolean is_valuetype
= !MONO_TYPE_IS_REFERENCE (field
->type
);
5737 if (mono_field_is_deleted (field
) || (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
5740 for (j
= i
; j
< class->field
.count
; ++j
) {
5741 MonoClassField
*other
= &class->fields
[j
];
5742 int otherEnd
= get_field_end (other
);
5743 if (mono_field_is_deleted (other
) || (is_valuetype
&& !MONO_TYPE_IS_REFERENCE (other
->type
)) || (other
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
5746 if (!is_valuetype
&& MONO_TYPE_IS_REFERENCE (other
->type
) && field
->offset
== other
->offset
&& is_fulltrust
)
5749 if ((otherEnd
> field
->offset
&& otherEnd
<= fieldEnd
) || (other
->offset
>= field
->offset
&& other
->offset
< fieldEnd
))
5757 field_hash (gconstpointer key
)
5759 const MonoClassField
*field
= key
;
5760 return g_str_hash (field
->name
) ^ mono_metadata_type_hash (field
->type
); /**/
5764 field_equals (gconstpointer _a
, gconstpointer _b
)
5766 const MonoClassField
*a
= _a
;
5767 const MonoClassField
*b
= _b
;
5768 return !strcmp (a
->name
, b
->name
) && mono_metadata_type_equal (a
->type
, b
->type
);
5773 verify_class_fields (MonoClass
*class)
5775 gpointer iter
= NULL
;
5776 MonoClassField
*field
;
5777 MonoGenericContext
*context
= mono_class_get_context (class);
5778 GHashTable
*unique_fields
= g_hash_table_new_full (&field_hash
, &field_equals
, NULL
, NULL
);
5779 if (class->generic_container
)
5780 context
= &class->generic_container
->context
;
5782 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
5783 if (!mono_type_is_valid_type_in_context (field
->type
, context
)) {
5784 g_hash_table_destroy (unique_fields
);
5787 if (g_hash_table_lookup (unique_fields
, field
)) {
5788 g_hash_table_destroy (unique_fields
);
5791 g_hash_table_insert (unique_fields
, field
, field
);
5793 g_hash_table_destroy (unique_fields
);
5798 verify_interfaces (MonoClass
*class)
5801 for (i
= 0; i
< class->interface_count
; ++i
) {
5802 MonoClass
*iface
= class->interfaces
[i
];
5803 if (!(iface
->flags
& TYPE_ATTRIBUTE_INTERFACE
))
5810 verify_valuetype_layout_with_target (MonoClass
*class, MonoClass
*target_class
)
5813 gpointer iter
= NULL
;
5814 MonoClassField
*field
;
5815 MonoClass
*field_class
;
5817 if (!class->valuetype
)
5820 type
= class->byval_arg
.type
;
5821 /*primitive type fields are not properly decoded*/
5822 if ((type
>= MONO_TYPE_BOOLEAN
&& type
<= MONO_TYPE_R8
) || (type
>= MONO_TYPE_I
&& type
<= MONO_TYPE_U
))
5825 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
5829 if (field
->type
->attrs
& (FIELD_ATTRIBUTE_STATIC
| FIELD_ATTRIBUTE_HAS_FIELD_RVA
))
5832 field_class
= mono_class_get_generic_type_definition (mono_class_from_mono_type (field
->type
));
5834 if (field_class
== target_class
|| class == field_class
|| !verify_valuetype_layout_with_target (field_class
, target_class
))
5842 verify_valuetype_layout (MonoClass
*class)
5845 res
= verify_valuetype_layout_with_target (class, class);
5850 recursive_mark_constraint_args (MonoBitSet
*used_args
, MonoGenericContainer
*gc
, MonoType
*type
)
5853 MonoClass
**constraints
;
5854 MonoGenericParamInfo
*param_info
;
5856 g_assert (mono_type_is_generic_argument (type
));
5858 idx
= mono_type_get_generic_param_num (type
);
5859 if (mono_bitset_test_fast (used_args
, idx
))
5862 mono_bitset_set_fast (used_args
, idx
);
5863 param_info
= mono_generic_container_get_param_info (gc
, idx
);
5865 if (!param_info
->constraints
)
5868 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
5869 MonoClass
*ctr
= *constraints
;
5870 MonoType
*constraint_type
= &ctr
->byval_arg
;
5872 if (mono_type_is_generic_argument (constraint_type
) && !recursive_mark_constraint_args (used_args
, gc
, constraint_type
))
5879 verify_generic_parameters (MonoClass
*class)
5882 MonoGenericContainer
*gc
= class->generic_container
;
5883 MonoBitSet
*used_args
= mono_bitset_new (gc
->type_argc
, 0);
5885 for (i
= 0; i
< gc
->type_argc
; ++i
) {
5886 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
5887 MonoClass
**constraints
;
5889 if (!param_info
->constraints
)
5892 mono_bitset_clear_all (used_args
);
5893 mono_bitset_set_fast (used_args
, i
);
5895 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
5896 MonoClass
*ctr
= *constraints
;
5897 MonoType
*constraint_type
= &ctr
->byval_arg
;
5899 if (!mono_type_is_valid_type_in_context (constraint_type
, &gc
->context
))
5902 if (mono_type_is_generic_argument (constraint_type
) && !recursive_mark_constraint_args (used_args
, gc
, constraint_type
))
5906 mono_bitset_free (used_args
);
5910 mono_bitset_free (used_args
);
5915 * Check if the class is verifiable.
5917 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5918 * field and invalid generic instantiation are fatal errors.
5920 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5924 mono_verifier_verify_class (MonoClass
*class)
5926 /*Neither <Module>, object or ifaces have parent.*/
5927 if (!class->parent
&&
5928 class != mono_defaults
.object_class
&&
5929 !MONO_CLASS_IS_INTERFACE (class) &&
5930 (!class->image
->dynamic
&& class->type_token
!= 0x2000001)) /*<Module> is the first type in the assembly*/
5932 if (class->parent
&& MONO_CLASS_IS_INTERFACE (class->parent
))
5934 if (class->generic_container
&& (class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
)
5936 if (class->generic_container
&& !verify_generic_parameters (class))
5938 if (!verify_class_for_overlapping_reference_fields (class))
5940 if (class->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, class))
5942 if (class->generic_class
== NULL
&& !verify_class_fields (class))
5944 if (class->valuetype
&& !verify_valuetype_layout (class))
5946 if (!verify_interfaces (class))
5953 mono_verifier_verify_class (MonoClass
*class)
5955 /* The verifier was disabled at compile time */
5960 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
5962 /* The verifier was disabled at compile time */
5967 mono_verifier_is_class_full_trust (MonoClass
*klass
)
5969 /* The verifier was disabled at compile time */
5974 mono_verifier_is_method_full_trust (MonoMethod
*method
)
5976 /* The verifier was disabled at compile time */
5981 mono_verifier_is_enabled_for_image (MonoImage
*image
)
5983 /* The verifier was disabled at compile time */
5988 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
5990 /* The verifier was disabled at compile time */
5995 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
5997 /* The verifier was disabled at compile time */
6002 mono_method_verify (MonoMethod
*method
, int level
)
6004 /* The verifier was disabled at compile time */
6009 mono_free_verify_list (GSList
*list
)
6011 /* The verifier was disabled at compile time */
6012 /* will always be null if verifier is disabled */