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>
33 static MiniVerifierMode verifier_mode
= MONO_VERIFIER_MODE_OFF
;
34 static gboolean verify_all
= FALSE
;
37 * Set the desired level of checks for the verfier.
41 mono_verifier_set_mode (MiniVerifierMode mode
)
47 mono_verifier_enable_verify_all ()
52 #ifndef DISABLE_VERIFIER
54 * Pull the list of opcodes
56 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
60 #include "mono/cil/opcode.def"
65 #ifdef MONO_VERIFIER_DEBUG
66 #define VERIFIER_DEBUG(code) do { code } while (0)
68 #define VERIFIER_DEBUG(code)
71 //////////////////////////////////////////////////////////////////
72 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
73 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
74 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
75 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
76 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
77 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
79 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
80 vinfo->info.status = __status; \
81 vinfo->info.message = ( __msg ); \
82 vinfo->exception_type = (__exception); \
83 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
86 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
87 #define ADD_VERIFY_ERROR(__ctx, __msg) \
89 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
93 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
95 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
96 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
97 (__ctx)->verifiable = 0; \
98 if (IS_FAIL_FAST_MODE (__ctx)) \
103 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
105 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
106 (__ctx)->valid = 0; \
109 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
111 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
112 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
113 (__ctx)->verifiable = 0; \
114 if (IS_FAIL_FAST_MODE (__ctx)) \
115 (__ctx)->valid = 0; \
119 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
120 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
122 #if SIZEOF_VOID_P == 4
123 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
125 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
128 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
129 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
131 /*Flags to be used with ILCodeDesc::flags */
133 /*Instruction has not been processed.*/
134 IL_CODE_FLAG_NOT_PROCESSED
= 0,
135 /*Instruction was decoded by mono_method_verify loop.*/
136 IL_CODE_FLAG_SEEN
= 1,
137 /*Instruction was target of a branch or is at a protected block boundary.*/
138 IL_CODE_FLAG_WAS_TARGET
= 2,
139 /*Used by stack_init to avoid double initialize each entry.*/
140 IL_CODE_FLAG_STACK_INITED
= 4,
141 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
142 IL_CODE_STACK_MERGED
= 8,
143 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
144 IL_CODE_DELEGATE_SEQUENCE
= 0x10,
145 /*This is a delegate created from a ldftn to a non final virtual method*/
146 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
= 0x20,
147 /*This is a call to a non final virtual method*/
148 IL_CODE_CALL_NONFINAL_VIRTUAL
= 0x40,
166 guint16 size
, max_size
;
183 /*Allocated fnptr MonoType that should be freed by us.*/
185 /*Type dup'ed exception types from catch blocks.*/
186 GSList
*exception_types
;
191 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
195 MonoMethodSignature
*signature
;
196 MonoMethodHeader
*header
;
198 MonoGenericContext
*generic_context
;
202 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
203 *on a method that creates a delegate for a non-final virtual method using ldftn*/
204 gboolean has_this_store
;
206 /*This flag is used to control if the contructor of the parent class has been called.
207 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
208 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
210 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
211 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
213 gboolean super_ctor_called
;
217 MonoType
*constrained_type
;
221 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
);
224 get_stack_type (MonoType
*type
);
227 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
);
230 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
);
233 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
);
234 //////////////////////////////////////////////////////////////////
239 TYPE_INV
= 0, /* leave at 0. */
244 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
246 /* value types and classes */
248 /* Number of types, used to define the size of the tables*/
251 /* Used by tables to signal that a result is not verifiable*/
252 NON_VERIFIABLE_RESULT
= 0x80,
254 /*Mask used to extract just the type, excluding flags */
257 /* The stack type is a managed pointer, unmask the value to res */
258 POINTER_MASK
= 0x100,
260 /*Stack type with the pointer mask*/
261 RAW_TYPE_MASK
= 0x10F,
263 /* Controlled Mutability Manager Pointer */
266 /* The stack type is a null literal*/
267 NULL_LITERAL_MASK
= 0x400,
269 /**Used by ldarg.0 and family to let delegate verification happens.*/
270 THIS_POINTER_MASK
= 0x800,
272 /**Signals that this is a boxed value type*/
275 /*This is an unitialized this ref*/
276 UNINIT_THIS_MASK
= 0x2000,
279 static const char* const
280 type_names
[TYPE_MAX
+ 1] = {
291 PREFIX_UNALIGNED
= 1,
294 PREFIX_CONSTRAINED
= 8,
297 //////////////////////////////////////////////////////////////////
299 #ifdef ENABLE_VERIFIER_STATS
301 #define MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
302 #define MEM_FREE(amt) do { working_set -= (amt); } while (0)
304 static int allocated_memory
;
305 static int working_set
;
306 static int max_allocated_memory
;
307 static int max_working_set
;
308 static int total_allocated_memory
;
311 finish_collect_stats (void)
313 max_allocated_memory
= MAX (max_allocated_memory
, allocated_memory
);
314 max_working_set
= MAX (max_working_set
, working_set
);
315 total_allocated_memory
+= allocated_memory
;
316 allocated_memory
= working_set
= 0;
320 init_verifier_stats (void)
322 static gboolean inited
;
325 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &max_allocated_memory
);
326 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &max_working_set
);
327 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &total_allocated_memory
);
333 #define MEM_ALLOC(amt) do {} while (0)
334 #define MEM_FREE(amt) do { } while (0)
336 #define finish_collect_stats()
337 #define init_verifier_stats()
342 //////////////////////////////////////////////////////////////////
345 /*Token validation macros and functions */
346 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
347 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
348 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
349 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
351 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
352 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
353 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
354 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
355 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
356 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
359 * Verify if @token refers to a valid row on int's table.
362 token_bounds_check (MonoImage
*image
, guint32 token
)
365 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage
*)image
, token
);
366 return image
->tables
[mono_metadata_token_table (token
)].rows
>= mono_metadata_token_index (token
);
370 mono_type_create_fnptr_from_mono_method (VerifyContext
*ctx
, MonoMethod
*method
)
372 MonoType
*res
= g_new0 (MonoType
, 1);
373 MEM_ALLOC (sizeof (MonoType
));
375 //FIXME use mono_method_get_signature_full
376 res
->data
.method
= mono_method_signature (method
);
377 res
->type
= MONO_TYPE_FNPTR
;
378 ctx
->funptrs
= g_slist_prepend (ctx
->funptrs
, res
);
383 * mono_type_is_enum_type:
385 * Returns TRUE if @type is an enum type.
388 mono_type_is_enum_type (MonoType
*type
)
390 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
392 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
398 * mono_type_is_value_type:
400 * Returns TRUE if @type is named after @namespace.@name.
404 mono_type_is_value_type (MonoType
*type
, const char *namespace, const char *name
)
406 return type
->type
== MONO_TYPE_VALUETYPE
&&
407 !strcmp (namespace, type
->data
.klass
->name_space
) &&
408 !strcmp (name
, type
->data
.klass
->name
);
412 * Returns TURE if @type is VAR or MVAR
415 mono_type_is_generic_argument (MonoType
*type
)
417 return type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
;
421 * mono_type_get_underlying_type_any:
423 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
425 * Returns the underlying type of @type regardless if it is byref or not.
428 mono_type_get_underlying_type_any (MonoType
*type
)
430 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
)
431 return mono_class_enum_basetype (type
->data
.klass
);
432 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
)
433 return mono_class_enum_basetype (type
->data
.generic_class
->container_class
);
437 static G_GNUC_UNUSED
const char*
438 mono_type_get_stack_name (MonoType
*type
)
440 return type_names
[get_stack_type (type
) & TYPE_MASK
];
443 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
444 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
447 mono_method_is_constructor (MonoMethod
*method
)
449 return ((method
->flags
& CTOR_REQUIRED_FLAGS
) == CTOR_REQUIRED_FLAGS
&&
450 !(method
->flags
& CTOR_INVALID_FLAGS
) &&
451 !strcmp (".ctor", method
->name
));
455 mono_class_has_default_constructor (MonoClass
*klass
)
460 mono_class_setup_methods (klass
);
461 if (klass
->exception_type
)
464 for (i
= 0; i
< klass
->method
.count
; ++i
) {
465 method
= klass
->methods
[i
];
466 if (mono_method_is_constructor (method
) &&
467 mono_method_signature (method
) &&
468 mono_method_signature (method
)->param_count
== 0 &&
469 (method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) == METHOD_ATTRIBUTE_PUBLIC
)
476 * Verify if @type is valid for the given @ctx verification context.
477 * this function checks for VAR and MVAR types that are invalid under the current verifier,
480 mono_type_is_valid_type_in_context (MonoType
*type
, MonoGenericContext
*context
)
483 MonoGenericInst
*inst
;
485 switch (type
->type
) {
490 inst
= type
->type
== MONO_TYPE_VAR
? context
->class_inst
: context
->method_inst
;
491 if (!inst
|| mono_type_get_generic_param_num (type
) >= inst
->type_argc
)
494 case MONO_TYPE_SZARRAY
:
495 return mono_type_is_valid_type_in_context (&type
->data
.klass
->byval_arg
, context
);
496 case MONO_TYPE_ARRAY
:
497 return mono_type_is_valid_type_in_context (&type
->data
.array
->eklass
->byval_arg
, context
);
499 return mono_type_is_valid_type_in_context (type
->data
.type
, context
);
500 case MONO_TYPE_GENERICINST
:
501 inst
= type
->data
.generic_class
->context
.class_inst
;
504 for (i
= 0; i
< inst
->type_argc
; ++i
)
505 if (!mono_type_is_valid_type_in_context (inst
->type_argv
[i
], context
))
512 /*This function returns NULL if the type is not instantiatable*/
514 verifier_inflate_type (VerifyContext
*ctx
, MonoType
*type
, MonoGenericContext
*context
)
519 result
= mono_class_inflate_generic_type_checked (type
, context
, &error
);
520 if (!mono_error_ok (&error
)) {
521 mono_error_cleanup (&error
);
529 is_valid_generic_instantiation (MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
534 if (ginst
->type_argc
!= gc
->type_argc
)
537 for (i
= 0; i
< gc
->type_argc
; ++i
) {
538 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
539 MonoClass
*paramClass
;
540 MonoClass
**constraints
;
542 if (!param_info
->constraints
&& !(param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
))
544 if (mono_type_is_generic_argument (ginst
->type_argv
[i
]))
545 continue; //it's not our job to validate type variables
547 paramClass
= mono_class_from_mono_type (ginst
->type_argv
[i
]);
549 if (paramClass
->exception_type
!= MONO_EXCEPTION_NONE
)
552 /*it's not safe to call mono_class_init from here*/
553 if (paramClass
->generic_class
&& !paramClass
->inited
) {
554 if (!mono_class_is_valid_generic_instantiation (NULL
, paramClass
))
558 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
) && (!paramClass
->valuetype
|| mono_class_is_nullable (paramClass
)))
561 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT
) && paramClass
->valuetype
)
564 if ((param_info
->flags
& GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT
) && !paramClass
->valuetype
&& !mono_class_has_default_constructor (paramClass
))
567 if (!param_info
->constraints
)
570 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
571 MonoClass
*ctr
= *constraints
;
574 inflated
= mono_class_inflate_generic_type_checked (&ctr
->byval_arg
, context
, &error
);
575 if (!mono_error_ok (&error
)) {
576 mono_error_cleanup (&error
);
579 ctr
= mono_class_from_mono_type (inflated
);
580 mono_metadata_free_type (inflated
);
582 if (!mono_class_is_assignable_from_slow (ctr
, paramClass
))
590 * Return true if @candidate is constraint compatible with @target.
592 * This means that @candidate constraints are a super set of @target constaints
595 mono_generic_param_is_constraint_compatible (VerifyContext
*ctx
, MonoGenericParam
*target
, MonoGenericParam
*candidate
, MonoGenericContext
*context
)
597 MonoGenericParamInfo
*tinfo
= mono_generic_param_info (target
);
598 MonoGenericParamInfo
*cinfo
= mono_generic_param_info (candidate
);
600 int tmask
= tinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
601 int cmask
= cinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK
;
602 if ((tmask
& cmask
) != tmask
)
605 if (tinfo
->constraints
) {
606 MonoClass
**target_class
, **candidate_class
;
607 if (!cinfo
->constraints
)
609 for (target_class
= tinfo
->constraints
; *target_class
; ++target_class
) {
611 MonoType
*inflated
= verifier_inflate_type (ctx
, &(*target_class
)->byval_arg
, context
);
614 tc
= mono_class_from_mono_type (inflated
);
615 mono_metadata_free_type (inflated
);
617 for (candidate_class
= cinfo
->constraints
; *candidate_class
; ++candidate_class
) {
619 inflated
= verifier_inflate_type (ctx
, &(*candidate_class
)->byval_arg
, ctx
->generic_context
);
622 cc
= mono_class_from_mono_type (inflated
);
623 mono_metadata_free_type (inflated
);
625 if (mono_class_is_assignable_from (tc
, cc
))
628 if (!*candidate_class
)
635 static MonoGenericParam
*
636 verifier_get_generic_param_from_type (VerifyContext
*ctx
, MonoType
*type
)
638 MonoGenericContainer
*gc
;
639 MonoMethod
*method
= ctx
->method
;
642 num
= mono_type_get_generic_param_num (type
);
644 if (type
->type
== MONO_TYPE_VAR
) {
645 MonoClass
*gtd
= method
->klass
;
646 if (gtd
->generic_class
)
647 gtd
= gtd
->generic_class
->container_class
;
648 gc
= gtd
->generic_container
;
650 MonoMethod
*gmd
= method
;
651 if (method
->is_inflated
)
652 gmd
= ((MonoMethodInflated
*)method
)->declaring
;
653 gc
= mono_method_get_generic_container (gmd
);
657 return mono_generic_container_get_param (gc
, num
);
663 * Verify if @type is valid for the given @ctx verification context.
664 * this function checks for VAR and MVAR types that are invalid under the current verifier,
665 * This means that it either
668 is_valid_type_in_context (VerifyContext
*ctx
, MonoType
*type
)
670 return mono_type_is_valid_type_in_context (type
, ctx
->generic_context
);
674 is_valid_generic_instantiation_in_context (VerifyContext
*ctx
, MonoGenericInst
*ginst
)
677 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
678 MonoType
*type
= ginst
->type_argv
[i
];
679 if (!is_valid_type_in_context (ctx
, type
))
686 generic_arguments_respect_constraints (VerifyContext
*ctx
, MonoGenericContainer
*gc
, MonoGenericContext
*context
, MonoGenericInst
*ginst
)
689 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
690 MonoType
*type
= ginst
->type_argv
[i
];
691 MonoGenericParam
*target
= mono_generic_container_get_param (gc
, i
);
692 MonoGenericParam
*candidate
;
694 if (!mono_type_is_generic_argument (type
))
697 if (!is_valid_type_in_context (ctx
, type
))
700 candidate
= verifier_get_generic_param_from_type (ctx
, type
);
702 if (!mono_generic_param_is_constraint_compatible (ctx
, target
, candidate
, context
))
709 mono_method_repect_method_constraints (VerifyContext
*ctx
, MonoMethod
*method
)
711 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
712 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
713 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
714 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gmethod
->context
, ginst
);
718 mono_class_repect_method_constraints (VerifyContext
*ctx
, MonoClass
*klass
)
720 MonoGenericClass
*gklass
= klass
->generic_class
;
721 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
722 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
723 return !gc
|| generic_arguments_respect_constraints (ctx
, gc
, &gklass
->context
, ginst
);
727 mono_method_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoMethod
*method
)
729 MonoMethodInflated
*gmethod
= (MonoMethodInflated
*)method
;
730 MonoGenericInst
*ginst
= gmethod
->context
.method_inst
;
731 MonoGenericContainer
*gc
= mono_method_get_generic_container (gmethod
->declaring
);
732 if (!gc
) /*non-generic inflated method - it's part of a generic type */
734 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
736 return is_valid_generic_instantiation (gc
, &gmethod
->context
, ginst
);
741 mono_class_is_valid_generic_instantiation (VerifyContext
*ctx
, MonoClass
*klass
)
743 MonoGenericClass
*gklass
= klass
->generic_class
;
744 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
745 MonoGenericContainer
*gc
= gklass
->container_class
->generic_container
;
746 if (ctx
&& !is_valid_generic_instantiation_in_context (ctx
, ginst
))
748 return is_valid_generic_instantiation (gc
, &gklass
->context
, ginst
);
752 mono_type_is_valid_in_context (VerifyContext
*ctx
, MonoType
*type
)
757 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid null type at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
761 if (!is_valid_type_in_context (ctx
, type
)) {
762 char *str
= mono_type_full_name (type
);
763 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
764 type
->type
== MONO_TYPE_VAR
? "!" : "!!",
766 type
->type
== MONO_TYPE_VAR
? "class" : "method",
768 MONO_EXCEPTION_BAD_IMAGE
);
773 klass
= mono_class_from_mono_type (type
);
774 mono_class_init (klass
);
775 if (mono_loader_get_last_error () || klass
->exception_type
!= MONO_EXCEPTION_NONE
) {
776 if (klass
->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, klass
))
777 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
);
779 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
);
783 if (klass
->generic_class
&& klass
->generic_class
->container_class
->exception_type
!= MONO_EXCEPTION_NONE
) {
784 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
);
788 if (!klass
->generic_class
)
791 if (!mono_class_is_valid_generic_instantiation (ctx
, klass
)) {
792 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
);
796 if (!mono_class_repect_method_constraints (ctx
, klass
)) {
797 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
);
804 static verify_result_t
805 mono_method_is_valid_in_context (VerifyContext
*ctx
, MonoMethod
*method
)
807 if (!mono_type_is_valid_in_context (ctx
, &method
->klass
->byval_arg
))
808 return RESULT_INVALID
;
810 if (!method
->is_inflated
)
813 if (!mono_method_is_valid_generic_instantiation (ctx
, method
)) {
814 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
);
815 return RESULT_INVALID
;
818 if (!mono_method_repect_method_constraints (ctx
, method
)) {
819 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
));
820 return RESULT_UNVERIFIABLE
;
826 static MonoClassField
*
827 verifier_load_field (VerifyContext
*ctx
, int token
, MonoClass
**out_klass
, const char *opcode
) {
828 MonoClassField
*field
;
829 MonoClass
*klass
= NULL
;
831 if (!IS_FIELD_DEF_OR_REF (token
) || !token_bounds_check (ctx
->image
, token
)) {
832 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
);
836 field
= mono_field_from_token (ctx
->image
, token
, &klass
, ctx
->generic_context
);
837 if (!field
|| !field
->parent
|| !klass
) {
838 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
);
842 if (!mono_type_is_valid_in_context (ctx
, &klass
->byval_arg
))
850 verifier_load_method (VerifyContext
*ctx
, int token
, const char *opcode
) {
853 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
854 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
);
858 method
= mono_get_method_full (ctx
->image
, token
, NULL
, ctx
->generic_context
);
861 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
);
865 if (mono_method_is_valid_in_context (ctx
, method
) == RESULT_INVALID
)
872 verifier_load_type (VerifyContext
*ctx
, int token
, const char *opcode
) {
875 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
876 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
880 type
= mono_type_get_full (ctx
->image
, token
, ctx
->generic_context
);
883 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
);
887 if (!mono_type_is_valid_in_context (ctx
, type
))
894 /* stack_slot_get_type:
896 * Returns the stack type of @value. This value includes POINTER_MASK.
898 * Use this function to checks that account for a managed pointer.
901 stack_slot_get_type (ILStackDesc
*value
)
903 return value
->stype
& RAW_TYPE_MASK
;
906 /* stack_slot_get_underlying_type:
908 * Returns the stack type of @value. This value does not include POINTER_MASK.
910 * Use this function is cases where the fact that the value could be a managed pointer is
911 * irrelevant. For example, field load doesn't care about this fact of type on stack.
914 stack_slot_get_underlying_type (ILStackDesc
*value
)
916 return value
->stype
& TYPE_MASK
;
919 /* stack_slot_is_managed_pointer:
921 * Returns TRUE is @value is a managed pointer.
924 stack_slot_is_managed_pointer (ILStackDesc
*value
)
926 return (value
->stype
& POINTER_MASK
) == POINTER_MASK
;
929 /* stack_slot_is_managed_mutability_pointer:
931 * Returns TRUE is @value is a managed mutability pointer.
933 static G_GNUC_UNUSED gboolean
934 stack_slot_is_managed_mutability_pointer (ILStackDesc
*value
)
936 return (value
->stype
& CMMP_MASK
) == CMMP_MASK
;
939 /* stack_slot_is_null_literal:
941 * Returns TRUE is @value is the null literal.
944 stack_slot_is_null_literal (ILStackDesc
*value
)
946 return (value
->stype
& NULL_LITERAL_MASK
) == NULL_LITERAL_MASK
;
950 /* stack_slot_is_this_pointer:
952 * Returns TRUE is @value is the this literal
955 stack_slot_is_this_pointer (ILStackDesc
*value
)
957 return (value
->stype
& THIS_POINTER_MASK
) == THIS_POINTER_MASK
;
960 /* stack_slot_is_boxed_value:
962 * Returns TRUE is @value is a boxed value
965 stack_slot_is_boxed_value (ILStackDesc
*value
)
967 return (value
->stype
& BOXED_MASK
) == BOXED_MASK
;
971 stack_slot_get_name (ILStackDesc
*value
)
973 return type_names
[value
->stype
& TYPE_MASK
];
976 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
977 if (PRED (value)) { \
979 g_string_append (str, ", "); \
980 g_string_append (str, NAME); \
985 stack_slot_stack_type_full_name (ILStackDesc
*value
)
987 GString
*str
= g_string_new ("");
989 gboolean has_pred
= FALSE
, first
= TRUE
;
991 if ((value
->stype
& TYPE_MASK
) != value
->stype
) {
992 g_string_append(str
, "[");
993 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer
, "this");
994 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value
, "boxed");
995 APPEND_WITH_PREDICATE (stack_slot_is_null_literal
, "null");
996 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer
, "cmmp");
997 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer
, "mp");
1001 if (mono_type_is_generic_argument (value
->type
) && !stack_slot_is_boxed_value (value
)) {
1003 g_string_append(str
, "[");
1005 g_string_append (str
, ", ");
1006 g_string_append (str
, "unboxed");
1011 g_string_append(str
, "] ");
1013 g_string_append (str
, stack_slot_get_name (value
));
1015 g_string_free (str
, FALSE
);
1020 stack_slot_full_name (ILStackDesc
*value
)
1022 char *type_name
= mono_type_full_name (value
->type
);
1023 char *stack_name
= stack_slot_stack_type_full_name (value
);
1024 char *res
= g_strdup_printf ("%s (%s)", type_name
, stack_name
);
1026 g_free (stack_name
);
1030 //////////////////////////////////////////////////////////////////
1032 mono_free_verify_list (GSList
*list
)
1034 MonoVerifyInfoExtended
*info
;
1037 for (tmp
= list
; tmp
; tmp
= tmp
->next
) {
1039 g_free (info
->info
.message
);
1042 g_slist_free (list
);
1045 #define ADD_ERROR(list,msg) \
1047 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1048 vinfo->info.status = MONO_VERIFY_ERROR; \
1049 vinfo->info.message = (msg); \
1050 (list) = g_slist_prepend ((list), vinfo); \
1053 #define ADD_WARN(list,code,msg) \
1055 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1056 vinfo->info.status = (code); \
1057 vinfo->info.message = (msg); \
1058 (list) = g_slist_prepend ((list), vinfo); \
1061 #define ADD_INVALID(list,msg) \
1063 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1064 vinfo->status = MONO_VERIFY_ERROR; \
1065 vinfo->message = (msg); \
1066 (list) = g_slist_prepend ((list), vinfo); \
1067 /*G_BREAKPOINT ();*/ \
1071 #define CHECK_STACK_UNDERFLOW(num) \
1073 if (cur_stack < (num)) \
1074 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1077 #define CHECK_STACK_OVERFLOW() \
1079 if (cur_stack >= max_stack) \
1080 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1085 in_any_block (MonoMethodHeader
*header
, guint offset
)
1088 MonoExceptionClause
*clause
;
1090 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1091 clause
= &header
->clauses
[i
];
1092 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1094 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1096 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1103 * in_any_exception_block:
1105 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1108 in_any_exception_block (MonoMethodHeader
*header
, guint offset
)
1111 MonoExceptionClause
*clause
;
1113 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1114 clause
= &header
->clauses
[i
];
1115 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1117 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1124 * is_valid_branch_instruction:
1126 * Verify if it's valid to perform a branch from @offset to @target.
1127 * This should be used with br and brtrue/false.
1128 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1129 * The major diferent from other similiar functions is that branching into a
1130 * finally/fault block is invalid instead of just unverifiable.
1133 is_valid_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1136 MonoExceptionClause
*clause
;
1138 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1139 clause
= &header
->clauses
[i
];
1140 /*branching into a finally block is invalid*/
1141 if ((clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
) &&
1142 !MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1143 MONO_OFFSET_IN_HANDLER (clause
, target
))
1146 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1148 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1150 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1157 * is_valid_cmp_branch_instruction:
1159 * Verify if it's valid to perform a branch from @offset to @target.
1160 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1161 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1163 * The major diferences from other similar functions are that most errors lead to invalid
1164 * code and only branching out of finally, filter or fault clauses is unverifiable.
1167 is_valid_cmp_branch_instruction (MonoMethodHeader
*header
, guint offset
, guint target
)
1170 MonoExceptionClause
*clause
;
1172 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1173 clause
= &header
->clauses
[i
];
1174 /*branching out of a handler or finally*/
1175 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
&&
1176 MONO_OFFSET_IN_HANDLER (clause
, offset
) &&
1177 !MONO_OFFSET_IN_HANDLER (clause
, target
))
1180 if (clause
->try_offset
!= target
&& (MONO_OFFSET_IN_CLAUSE (clause
, offset
) ^ MONO_OFFSET_IN_CLAUSE (clause
, target
)))
1182 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) ^ MONO_OFFSET_IN_HANDLER (clause
, target
))
1184 if (MONO_OFFSET_IN_FILTER (clause
, offset
) ^ MONO_OFFSET_IN_FILTER (clause
, target
))
1191 * A leave can't escape a finally block
1194 is_correct_leave (MonoMethodHeader
*header
, guint offset
, guint target
)
1197 MonoExceptionClause
*clause
;
1199 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1200 clause
= &header
->clauses
[i
];
1201 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
&& MONO_OFFSET_IN_HANDLER (clause
, offset
) && !MONO_OFFSET_IN_HANDLER (clause
, target
))
1203 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1210 * A rethrow can't happen outside of a catch handler.
1213 is_correct_rethrow (MonoMethodHeader
*header
, guint offset
)
1216 MonoExceptionClause
*clause
;
1218 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1219 clause
= &header
->clauses
[i
];
1220 if (MONO_OFFSET_IN_HANDLER (clause
, offset
))
1222 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1229 * An endfinally can't happen outside of a finally/fault handler.
1232 is_correct_endfinally (MonoMethodHeader
*header
, guint offset
)
1235 MonoExceptionClause
*clause
;
1237 for (i
= 0; i
< header
->num_clauses
; ++i
) {
1238 clause
= &header
->clauses
[i
];
1239 if (MONO_OFFSET_IN_HANDLER (clause
, offset
) && (clause
->flags
== MONO_EXCEPTION_CLAUSE_FAULT
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
))
1247 * An endfilter can only happens inside a filter clause.
1248 * In non-strict mode filter is allowed inside the handler clause too
1250 static MonoExceptionClause
*
1251 is_correct_endfilter (VerifyContext
*ctx
, guint offset
)
1254 MonoExceptionClause
*clause
;
1256 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1257 clause
= &ctx
->header
->clauses
[i
];
1258 if (clause
->flags
!= MONO_EXCEPTION_CLAUSE_FILTER
)
1260 if (MONO_OFFSET_IN_FILTER (clause
, offset
))
1262 if (!IS_STRICT_MODE (ctx
) && MONO_OFFSET_IN_HANDLER (clause
, offset
))
1270 * Non-strict endfilter can happens inside a try block or any handler block
1273 is_unverifiable_endfilter (VerifyContext
*ctx
, guint offset
)
1276 MonoExceptionClause
*clause
;
1278 for (i
= 0; i
< ctx
->header
->num_clauses
; ++i
) {
1279 clause
= &ctx
->header
->clauses
[i
];
1280 if (MONO_OFFSET_IN_CLAUSE (clause
, offset
))
1287 is_valid_bool_arg (ILStackDesc
*arg
)
1289 if (stack_slot_is_managed_pointer (arg
) || stack_slot_is_boxed_value (arg
) || stack_slot_is_null_literal (arg
))
1293 switch (stack_slot_get_underlying_type (arg
)) {
1296 case TYPE_NATIVE_INT
:
1300 g_assert (arg
->type
);
1301 switch (arg
->type
->type
) {
1302 case MONO_TYPE_CLASS
:
1303 case MONO_TYPE_STRING
:
1304 case MONO_TYPE_OBJECT
:
1305 case MONO_TYPE_SZARRAY
:
1306 case MONO_TYPE_ARRAY
:
1307 case MONO_TYPE_FNPTR
:
1310 case MONO_TYPE_GENERICINST
:
1311 /*We need to check if the container class
1312 * of the generic type is a valuetype, iow:
1313 * is it a "class Foo<T>" or a "struct Foo<T>"?
1315 return !arg
->type
->data
.generic_class
->container_class
->valuetype
;
1323 /*Type manipulation helper*/
1325 /*Returns the byref version of the supplied MonoType*/
1327 mono_type_get_type_byref (MonoType
*type
)
1331 return &mono_class_from_mono_type (type
)->this_arg
;
1335 /*Returns the byval version of the supplied MonoType*/
1337 mono_type_get_type_byval (MonoType
*type
)
1341 return &mono_class_from_mono_type (type
)->byval_arg
;
1345 mono_type_from_stack_slot (ILStackDesc
*slot
)
1347 if (stack_slot_is_managed_pointer (slot
))
1348 return mono_type_get_type_byref (slot
->type
);
1352 /*Stack manipulation code*/
1355 ensure_stack_size (ILCodeDesc
*stack
, int required
)
1360 if (required
< stack
->max_size
)
1363 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1364 new_size
= MAX (8, MAX (required
, stack
->max_size
* 2));
1366 g_assert (new_size
>= stack
->size
);
1367 g_assert (new_size
>= required
);
1369 tmp
= g_new0 (ILStackDesc
, new_size
);
1370 MEM_ALLOC (sizeof (ILStackDesc
) * new_size
);
1374 memcpy (tmp
, stack
->stack
, stack
->size
* sizeof (ILStackDesc
));
1375 g_free (stack
->stack
);
1376 MEM_FREE (sizeof (ILStackDesc
) * stack
->max_size
);
1380 stack
->max_size
= new_size
;
1384 stack_init (VerifyContext
*ctx
, ILCodeDesc
*state
)
1386 if (state
->flags
& IL_CODE_FLAG_STACK_INITED
)
1388 state
->size
= state
->max_size
= 0;
1389 state
->flags
|= IL_CODE_FLAG_STACK_INITED
;
1393 stack_copy (ILCodeDesc
*to
, ILCodeDesc
*from
)
1395 ensure_stack_size (to
, from
->size
);
1396 to
->size
= from
->size
;
1398 /*stack copy happens at merge points, which have small stacks*/
1400 memcpy (to
->stack
, from
->stack
, sizeof (ILStackDesc
) * from
->size
);
1404 copy_stack_value (ILStackDesc
*to
, ILStackDesc
*from
)
1406 to
->stype
= from
->stype
;
1407 to
->type
= from
->type
;
1408 to
->method
= from
->method
;
1412 check_underflow (VerifyContext
*ctx
, int size
)
1414 if (ctx
->eval
.size
< size
) {
1415 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size
, ctx
->eval
.size
, ctx
->ip_offset
));
1422 check_overflow (VerifyContext
*ctx
)
1424 if (ctx
->eval
.size
>= ctx
->max_stack
) {
1425 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx
->eval
.size
+ 1, ctx
->ip_offset
));
1431 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1433 check_unmanaged_pointer (VerifyContext
*ctx
, ILStackDesc
*value
)
1435 if (stack_slot_get_type (value
) == TYPE_PTR
) {
1436 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
1442 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1444 check_unverifiable_type (VerifyContext
*ctx
, MonoType
*type
)
1446 if (type
->type
== MONO_TYPE_PTR
|| type
->type
== MONO_TYPE_FNPTR
) {
1447 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx
->ip_offset
));
1453 static ILStackDesc
*
1454 stack_push (VerifyContext
*ctx
)
1456 g_assert (ctx
->eval
.size
< ctx
->max_stack
);
1457 g_assert (ctx
->eval
.size
<= ctx
->eval
.max_size
);
1459 ensure_stack_size (&ctx
->eval
, ctx
->eval
.size
+ 1);
1461 return & ctx
->eval
.stack
[ctx
->eval
.size
++];
1464 static ILStackDesc
*
1465 stack_push_val (VerifyContext
*ctx
, int stype
, MonoType
*type
)
1467 ILStackDesc
*top
= stack_push (ctx
);
1473 static ILStackDesc
*
1474 stack_pop (VerifyContext
*ctx
)
1477 g_assert (ctx
->eval
.size
> 0);
1478 ret
= ctx
->eval
.stack
+ --ctx
->eval
.size
;
1479 if ((ret
->stype
& UNINIT_THIS_MASK
) == UNINIT_THIS_MASK
)
1480 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx
->ip_offset
));
1484 /* This function allows to safely pop an unititialized this ptr from
1485 * the eval stack without marking the method as unverifiable.
1487 static ILStackDesc
*
1488 stack_pop_safe (VerifyContext
*ctx
)
1490 g_assert (ctx
->eval
.size
> 0);
1491 return ctx
->eval
.stack
+ --ctx
->eval
.size
;
1494 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1496 stack_peek (VerifyContext
*ctx
, int distance
)
1498 g_assert (ctx
->eval
.size
- distance
> 0);
1499 return ctx
->eval
.stack
+ (ctx
->eval
.size
- 1 - distance
);
1502 static ILStackDesc
*
1503 stack_push_stack_val (VerifyContext
*ctx
, ILStackDesc
*value
)
1505 ILStackDesc
*top
= stack_push (ctx
);
1506 copy_stack_value (top
, value
);
1510 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1512 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1515 get_boxable_mono_type (VerifyContext
* ctx
, int token
, const char *opcode
)
1520 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
1523 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
1524 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1528 if (type
->type
== MONO_TYPE_VOID
) {
1529 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1533 if (type
->type
== MONO_TYPE_TYPEDBYREF
)
1534 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1536 if (!(class = mono_class_from_mono_type (type
)))
1537 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode
, ctx
->ip_offset
));
1539 if (class->generic_container
&& type
->type
!= MONO_TYPE_GENERICINST
)
1540 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
));
1542 check_unverifiable_type (ctx
, type
);
1547 /*operation result tables */
1549 static const unsigned char bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1550 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1551 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1552 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1553 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1554 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1555 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1558 static const unsigned char add_table
[TYPE_MAX
][TYPE_MAX
] = {
1559 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1560 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1561 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1562 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1563 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1564 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1567 static const unsigned char sub_table
[TYPE_MAX
][TYPE_MAX
] = {
1568 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1569 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1570 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1571 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_R8
, TYPE_INV
, TYPE_INV
},
1572 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1573 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1576 static const unsigned char int_bin_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1577 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1578 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1579 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1580 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1581 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1582 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1585 static const unsigned char shift_op_table
[TYPE_MAX
][TYPE_MAX
] = {
1586 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1587 {TYPE_I8
, TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1588 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1589 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1590 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1591 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1594 static const unsigned char cmp_br_op
[TYPE_MAX
][TYPE_MAX
] = {
1595 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1596 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1597 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1598 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
1599 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
1600 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1603 static const unsigned char cmp_br_eq_op
[TYPE_MAX
][TYPE_MAX
] = {
1604 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1605 {TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1606 {TYPE_I4
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1607 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
, TYPE_INV
, TYPE_INV
},
1608 {TYPE_INV
, TYPE_INV
, TYPE_I4
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_I4
, TYPE_INV
},
1609 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_I4
},
1612 static const unsigned char add_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
1613 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1614 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1615 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1616 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1617 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1618 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1621 static const unsigned char sub_ovf_un_table
[TYPE_MAX
][TYPE_MAX
] = {
1622 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1623 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1624 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1625 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1626 {TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_PTR
| NON_VERIFIABLE_RESULT
, TYPE_INV
, TYPE_NATIVE_INT
| NON_VERIFIABLE_RESULT
, TYPE_INV
},
1627 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1630 static const unsigned char bin_ovf_table
[TYPE_MAX
][TYPE_MAX
] = {
1631 {TYPE_I4
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1632 {TYPE_INV
, TYPE_I8
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1633 {TYPE_NATIVE_INT
, TYPE_INV
, TYPE_NATIVE_INT
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1634 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1635 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1636 {TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
, TYPE_INV
},
1639 #ifdef MONO_VERIFIER_DEBUG
1643 dump_stack_value (ILStackDesc
*value
)
1645 printf ("[(%x)(%x)", value
->type
->type
, value
->stype
);
1647 if (stack_slot_is_this_pointer (value
))
1650 if (stack_slot_is_boxed_value (value
))
1651 printf ("[boxed] ");
1653 if (stack_slot_is_null_literal (value
))
1656 if (stack_slot_is_managed_mutability_pointer (value
))
1657 printf ("Controled Mutability MP: ");
1659 if (stack_slot_is_managed_pointer (value
))
1660 printf ("Managed Pointer to: ");
1662 switch (stack_slot_get_underlying_type (value
)) {
1664 printf ("invalid type]");
1672 case TYPE_NATIVE_INT
:
1673 printf ("native int]");
1676 printf ("float64]");
1679 printf ("unmanaged pointer]");
1682 switch (value
->type
->type
) {
1683 case MONO_TYPE_CLASS
:
1684 case MONO_TYPE_VALUETYPE
:
1685 printf ("complex] (%s)", value
->type
->data
.klass
->name
);
1687 case MONO_TYPE_STRING
:
1688 printf ("complex] (string)");
1690 case MONO_TYPE_OBJECT
:
1691 printf ("complex] (object)");
1693 case MONO_TYPE_SZARRAY
:
1694 printf ("complex] (%s [])", value
->type
->data
.klass
->name
);
1696 case MONO_TYPE_ARRAY
:
1697 printf ("complex] (%s [%d %d %d])",
1698 value
->type
->data
.array
->eklass
->name
,
1699 value
->type
->data
.array
->rank
,
1700 value
->type
->data
.array
->numsizes
,
1701 value
->type
->data
.array
->numlobounds
);
1703 case MONO_TYPE_GENERICINST
:
1704 printf ("complex] (inst of %s )", value
->type
->data
.generic_class
->container_class
->name
);
1707 printf ("complex] (type generic param !%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
1709 case MONO_TYPE_MVAR
:
1710 printf ("complex] (method generic param !!%d - %s) ", value
->type
->data
.generic_param
->num
, mono_generic_param_info (value
->type
->data
.generic_param
)->name
);
1713 //should be a boxed value
1714 char * name
= mono_type_full_name (value
->type
);
1715 printf ("complex] %s", name
);
1721 printf ("unknown stack %x type]\n", value
->stype
);
1722 g_assert_not_reached ();
1727 dump_stack_state (ILCodeDesc
*state
)
1731 printf ("(%d) ", state
->size
);
1732 for (i
= 0; i
< state
->size
; ++i
)
1733 dump_stack_value (state
->stack
+ i
);
1738 /*Returns TRUE if candidate array type can be assigned to target.
1739 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1742 is_array_type_compatible (MonoType
*target
, MonoType
*candidate
)
1744 MonoArrayType
*left
= target
->data
.array
;
1745 MonoArrayType
*right
= candidate
->data
.array
;
1747 g_assert (target
->type
== MONO_TYPE_ARRAY
);
1748 g_assert (candidate
->type
== MONO_TYPE_ARRAY
);
1750 if (left
->rank
!= right
->rank
)
1753 return mono_class_is_assignable_from (left
->eklass
, right
->eklass
);
1757 get_stack_type (MonoType
*type
)
1760 int type_kind
= type
->type
;
1762 mask
= POINTER_MASK
;
1763 /*TODO handle CMMP_MASK */
1766 switch (type_kind
) {
1769 case MONO_TYPE_BOOLEAN
:
1772 case MONO_TYPE_CHAR
:
1775 return TYPE_I4
| mask
;
1779 return TYPE_NATIVE_INT
| mask
;
1781 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1782 case MONO_TYPE_FNPTR
:
1784 case MONO_TYPE_TYPEDBYREF
:
1785 return TYPE_PTR
| mask
;
1788 case MONO_TYPE_MVAR
:
1790 case MONO_TYPE_CLASS
:
1791 case MONO_TYPE_STRING
:
1792 case MONO_TYPE_OBJECT
:
1793 case MONO_TYPE_SZARRAY
:
1794 case MONO_TYPE_ARRAY
:
1795 return TYPE_COMPLEX
| mask
;
1799 return TYPE_I8
| mask
;
1803 return TYPE_R8
| mask
;
1805 case MONO_TYPE_GENERICINST
:
1806 case MONO_TYPE_VALUETYPE
:
1807 if (mono_type_is_enum_type (type
)) {
1808 type
= mono_type_get_underlying_type_any (type
);
1811 type_kind
= type
->type
;
1814 return TYPE_COMPLEX
| mask
;
1822 /* convert MonoType to ILStackDesc format (stype) */
1824 set_stack_value (VerifyContext
*ctx
, ILStackDesc
*stack
, MonoType
*type
, int take_addr
)
1827 int type_kind
= type
->type
;
1829 if (type
->byref
|| take_addr
)
1830 mask
= POINTER_MASK
;
1831 /* TODO handle CMMP_MASK */
1836 switch (type_kind
) {
1839 case MONO_TYPE_BOOLEAN
:
1842 case MONO_TYPE_CHAR
:
1845 stack
->stype
= TYPE_I4
| mask
;
1849 stack
->stype
= TYPE_NATIVE_INT
| mask
;
1852 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1853 case MONO_TYPE_FNPTR
:
1855 case MONO_TYPE_TYPEDBYREF
:
1856 stack
->stype
= TYPE_PTR
| mask
;
1859 case MONO_TYPE_CLASS
:
1860 case MONO_TYPE_STRING
:
1861 case MONO_TYPE_OBJECT
:
1862 case MONO_TYPE_SZARRAY
:
1863 case MONO_TYPE_ARRAY
:
1866 case MONO_TYPE_MVAR
:
1867 stack
->stype
= TYPE_COMPLEX
| mask
;
1872 stack
->stype
= TYPE_I8
| mask
;
1876 stack
->stype
= TYPE_R8
| mask
;
1878 case MONO_TYPE_GENERICINST
:
1879 case MONO_TYPE_VALUETYPE
:
1880 if (mono_type_is_enum_type (type
)) {
1881 MonoType
*utype
= mono_type_get_underlying_type_any (type
);
1883 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not resolve underlying type of %x at %d", type
->type
, ctx
->ip_offset
));
1887 type_kind
= type
->type
;
1890 stack
->stype
= TYPE_COMPLEX
| mask
;
1894 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type
->type
); );
1895 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type
->type
, ctx
->ip_offset
));
1902 * init_stack_with_value_at_exception_boundary:
1904 * Initialize the stack and push a given type.
1905 * The instruction is marked as been on the exception boundary.
1908 init_stack_with_value_at_exception_boundary (VerifyContext
*ctx
, ILCodeDesc
*code
, MonoClass
*klass
)
1911 MonoType
*type
= mono_class_inflate_generic_type_checked (&klass
->byval_arg
, ctx
->generic_context
, &error
);
1913 if (!mono_error_ok (&error
)) {
1914 char *name
= mono_type_get_full_name (klass
);
1915 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid class %s used for exception", name
));
1917 mono_error_cleanup (&error
);
1921 if (!ctx
->max_stack
) {
1922 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack overflow at 0x%04x", ctx
->ip_offset
));
1926 stack_init (ctx
, code
);
1927 ensure_stack_size (code
, 1);
1928 set_stack_value (ctx
, code
->stack
, type
, FALSE
);
1929 ctx
->exception_types
= g_slist_prepend (ctx
->exception_types
, type
);
1931 code
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
1932 if (mono_type_is_generic_argument (type
))
1933 code
->stack
->stype
|= BOXED_MASK
;
1936 /*Verify if type 'candidate' can be stored in type 'target'.
1938 * If strict, check for the underlying type and not the verification stack types
1941 verify_type_compatibility_full (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
, gboolean strict
)
1943 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1944 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1946 MonoType
*original_candidate
= candidate
;
1947 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target
), mono_type_full_name (candidate
), strict
); );
1949 /*only one is byref */
1950 if (candidate
->byref
^ target
->byref
) {
1951 /* converting from native int to byref*/
1952 if (get_stack_type (candidate
) == TYPE_NATIVE_INT
&& target
->byref
) {
1953 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("using byref native int at 0x%04x", ctx
->ip_offset
));
1958 strict
|= target
->byref
;
1959 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
1960 candidate
= mono_type_get_underlying_type_any (candidate
);
1963 switch (target
->type
) {
1964 case MONO_TYPE_VOID
:
1965 return candidate
->type
== MONO_TYPE_VOID
;
1968 case MONO_TYPE_BOOLEAN
:
1970 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I1
, MONO_TYPE_U1
, MONO_TYPE_BOOLEAN
);
1973 case MONO_TYPE_CHAR
:
1975 return IS_ONE_OF3 (candidate
->type
, MONO_TYPE_I2
, MONO_TYPE_U2
, MONO_TYPE_CHAR
);
1977 case MONO_TYPE_U4
: {
1978 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
1979 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
1981 return is_native_int
|| is_int4
;
1982 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
1987 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I8
, MONO_TYPE_U8
);
1992 return candidate
->type
== target
->type
;
1993 return IS_ONE_OF2 (candidate
->type
, MONO_TYPE_R4
, MONO_TYPE_R8
);
1997 gboolean is_native_int
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I
, MONO_TYPE_U
);
1998 gboolean is_int4
= IS_ONE_OF2 (candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
);
2000 return is_native_int
|| is_int4
;
2001 return is_native_int
|| get_stack_type (candidate
) == TYPE_I4
;
2005 if (candidate
->type
!= MONO_TYPE_PTR
)
2007 /* check the underlying type */
2008 return verify_type_compatibility_full (ctx
, target
->data
.type
, candidate
->data
.type
, TRUE
);
2010 case MONO_TYPE_FNPTR
: {
2011 MonoMethodSignature
*left
, *right
;
2012 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2015 left
= mono_type_get_signature (target
);
2016 right
= mono_type_get_signature (candidate
);
2017 return mono_metadata_signature_equal (left
, right
) && left
->call_convention
== right
->call_convention
;
2020 case MONO_TYPE_GENERICINST
: {
2021 MonoClass
*target_klass
;
2022 MonoClass
*candidate_klass
;
2023 if (mono_type_is_enum_type (target
)) {
2024 target
= mono_type_get_underlying_type_any (target
);
2030 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2031 * to take boxing status into account.
2033 if (mono_type_is_generic_argument (original_candidate
))
2036 target_klass
= mono_class_from_mono_type (target
);
2037 candidate_klass
= mono_class_from_mono_type (candidate
);
2038 if (mono_class_is_nullable (target_klass
)) {
2039 if (!mono_class_is_nullable (candidate_klass
))
2041 return target_klass
== candidate_klass
;
2044 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2047 case MONO_TYPE_STRING
:
2048 return candidate
->type
== MONO_TYPE_STRING
;
2050 case MONO_TYPE_CLASS
:
2052 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2053 * to take boxing status into account.
2055 if (mono_type_is_generic_argument (original_candidate
))
2058 if (candidate
->type
== MONO_TYPE_VALUETYPE
)
2061 /* If candidate is an enum it should return true for System.Enum and supertypes.
2062 * That's why here we use the original type and not the underlying type.
2064 return mono_class_is_assignable_from (target
->data
.klass
, mono_class_from_mono_type (original_candidate
));
2066 case MONO_TYPE_OBJECT
:
2067 return MONO_TYPE_IS_REFERENCE (candidate
);
2069 case MONO_TYPE_SZARRAY
: {
2072 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2075 left
= mono_class_from_mono_type (target
);
2076 right
= mono_class_from_mono_type (candidate
);
2078 return mono_class_is_assignable_from (left
, right
);
2081 case MONO_TYPE_ARRAY
:
2082 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2084 return is_array_type_compatible (target
, candidate
);
2086 case MONO_TYPE_TYPEDBYREF
:
2087 return candidate
->type
== MONO_TYPE_TYPEDBYREF
;
2089 case MONO_TYPE_VALUETYPE
: {
2090 MonoClass
*target_klass
;
2091 MonoClass
*candidate_klass
;
2093 if (candidate
->type
== MONO_TYPE_CLASS
)
2096 target_klass
= mono_class_from_mono_type (target
);
2097 candidate_klass
= mono_class_from_mono_type (candidate
);
2098 if (target_klass
== candidate_klass
)
2100 if (mono_type_is_enum_type (target
)) {
2101 target
= mono_type_get_underlying_type_any (target
);
2110 if (candidate
->type
!= MONO_TYPE_VAR
)
2112 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2114 case MONO_TYPE_MVAR
:
2115 if (candidate
->type
!= MONO_TYPE_MVAR
)
2117 return mono_type_get_generic_param_num (candidate
) == mono_type_get_generic_param_num (target
);
2120 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target
->type
); );
2121 g_assert_not_reached ();
2130 verify_type_compatibility (VerifyContext
*ctx
, MonoType
*target
, MonoType
*candidate
)
2132 return verify_type_compatibility_full (ctx
, target
, candidate
, FALSE
);
2136 * Returns the generic param bound to the context been verified.
2139 static MonoGenericParam
*
2140 get_generic_param (VerifyContext
*ctx
, MonoType
*param
)
2142 guint16 param_num
= mono_type_get_generic_param_num (param
);
2143 if (param
->type
== MONO_TYPE_VAR
) {
2144 if (!ctx
->generic_context
->class_inst
|| ctx
->generic_context
->class_inst
->type_argc
<= param_num
) {
2145 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic type argument %d", param_num
));
2148 return ctx
->generic_context
->class_inst
->type_argv
[param_num
]->data
.generic_param
;
2151 /*param must be a MVAR */
2152 if (!ctx
->generic_context
->method_inst
|| ctx
->generic_context
->method_inst
->type_argc
<= param_num
) {
2153 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid generic method argument %d", param_num
));
2156 return ctx
->generic_context
->method_inst
->type_argv
[param_num
]->data
.generic_param
;
2161 recursive_boxed_constraint_type_check (VerifyContext
*ctx
, MonoType
*type
, MonoClass
*constraint_class
, int recursion_level
)
2163 MonoType
*constraint_type
= &constraint_class
->byval_arg
;
2164 if (recursion_level
<= 0)
2167 if (verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (constraint_type
), FALSE
))
2170 if (mono_type_is_generic_argument (constraint_type
)) {
2171 MonoGenericParam
*param
= get_generic_param (ctx
, constraint_type
);
2175 for (class = mono_generic_param_info (param
)->constraints
; class && *class; ++class) {
2176 if (recursive_boxed_constraint_type_check (ctx
, type
, *class, recursion_level
- 1))
2184 * is_compatible_boxed_valuetype:
2186 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2188 * @type The source type. It it tested to be of the proper type.
2189 * @candidate type of the boxed valuetype.
2190 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2191 * @strict if TRUE candidate must be boxed compatible to the target type
2195 is_compatible_boxed_valuetype (VerifyContext
*ctx
, MonoType
*type
, MonoType
*candidate
, ILStackDesc
*stack
, gboolean strict
)
2197 if (!stack_slot_is_boxed_value (stack
))
2199 if (type
->byref
|| candidate
->byref
)
2202 if (mono_type_is_generic_argument (candidate
)) {
2203 MonoGenericParam
*param
= get_generic_param (ctx
, candidate
);
2208 for (class = mono_generic_param_info (param
)->constraints
; class && *class; ++class) {
2209 /*256 should be enough since there can't be more than 255 generic arguments.*/
2210 if (recursive_boxed_constraint_type_check (ctx
, type
, *class, 256))
2215 if (mono_type_is_generic_argument (type
))
2221 return MONO_TYPE_IS_REFERENCE (type
) && mono_class_is_assignable_from (mono_class_from_mono_type (type
), mono_class_from_mono_type (candidate
));
2225 verify_stack_type_compatibility_full (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
, gboolean drop_byref
, gboolean valuetype_must_be_boxed
)
2227 MonoType
*candidate
= mono_type_from_stack_slot (stack
);
2228 if (MONO_TYPE_IS_REFERENCE (type
) && !type
->byref
&& stack_slot_is_null_literal (stack
))
2231 if (is_compatible_boxed_valuetype (ctx
, type
, candidate
, stack
, TRUE
))
2234 if (valuetype_must_be_boxed
&& !stack_slot_is_boxed_value (stack
) && !MONO_TYPE_IS_REFERENCE (candidate
))
2237 if (!valuetype_must_be_boxed
&& stack_slot_is_boxed_value (stack
))
2241 return verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (candidate
), FALSE
);
2243 return verify_type_compatibility_full (ctx
, type
, candidate
, FALSE
);
2247 verify_stack_type_compatibility (VerifyContext
*ctx
, MonoType
*type
, ILStackDesc
*stack
)
2249 return verify_stack_type_compatibility_full (ctx
, type
, stack
, FALSE
, FALSE
);
2253 mono_delegate_type_equal (MonoType
*target
, MonoType
*candidate
)
2255 if (candidate
->byref
^ target
->byref
)
2258 switch (target
->type
) {
2259 case MONO_TYPE_VOID
:
2262 case MONO_TYPE_BOOLEAN
:
2265 case MONO_TYPE_CHAR
:
2274 case MONO_TYPE_STRING
:
2275 case MONO_TYPE_TYPEDBYREF
:
2276 return candidate
->type
== target
->type
;
2279 return mono_delegate_type_equal (target
->data
.type
, candidate
->data
.type
);
2281 case MONO_TYPE_FNPTR
:
2282 if (candidate
->type
!= MONO_TYPE_FNPTR
)
2284 return mono_delegate_signature_equal (mono_type_get_signature (target
), mono_type_get_signature (candidate
), FALSE
);
2286 case MONO_TYPE_GENERICINST
: {
2287 MonoClass
*target_klass
;
2288 MonoClass
*candidate_klass
;
2289 target_klass
= mono_class_from_mono_type (target
);
2290 candidate_klass
= mono_class_from_mono_type (candidate
);
2291 /*FIXME handle nullables and enum*/
2292 return mono_class_is_assignable_from (target_klass
, candidate_klass
);
2294 case MONO_TYPE_OBJECT
:
2295 return MONO_TYPE_IS_REFERENCE (candidate
);
2297 case MONO_TYPE_CLASS
:
2298 return mono_class_is_assignable_from(target
->data
.klass
, mono_class_from_mono_type (candidate
));
2300 case MONO_TYPE_SZARRAY
:
2301 if (candidate
->type
!= MONO_TYPE_SZARRAY
)
2303 return mono_class_is_assignable_from (mono_class_from_mono_type (target
)->element_class
, mono_class_from_mono_type (candidate
)->element_class
);
2305 case MONO_TYPE_ARRAY
:
2306 if (candidate
->type
!= MONO_TYPE_ARRAY
)
2308 return is_array_type_compatible (target
, candidate
);
2310 case MONO_TYPE_VALUETYPE
:
2311 /*FIXME handle nullables and enum*/
2312 return mono_class_from_mono_type (candidate
) == mono_class_from_mono_type (target
);
2315 return candidate
->type
== MONO_TYPE_VAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2318 case MONO_TYPE_MVAR
:
2319 return candidate
->type
== MONO_TYPE_MVAR
&& mono_type_get_generic_param_num (target
) == mono_type_get_generic_param_num (candidate
);
2323 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target
->type
); );
2324 g_assert_not_reached ();
2330 mono_delegate_param_equal (MonoType
*delegate
, MonoType
*method
)
2332 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2335 return mono_delegate_type_equal (method
, delegate
);
2339 mono_delegate_ret_equal (MonoType
*delegate
, MonoType
*method
)
2341 if (mono_metadata_type_equal_full (delegate
, method
, TRUE
))
2344 return mono_delegate_type_equal (delegate
, method
);
2348 * mono_delegate_signature_equal:
2350 * Compare two signatures in the way expected by delegates.
2352 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2354 * FIXME can this function be eliminated and proper metadata functionality be used?
2357 mono_delegate_signature_equal (MonoMethodSignature
*delegate_sig
, MonoMethodSignature
*method_sig
, gboolean is_static_ldftn
)
2360 int method_offset
= is_static_ldftn
? 1 : 0;
2362 if (delegate_sig
->param_count
+ method_offset
!= method_sig
->param_count
)
2365 if (delegate_sig
->call_convention
!= method_sig
->call_convention
)
2368 for (i
= 0; i
< delegate_sig
->param_count
; i
++) {
2369 MonoType
*p1
= delegate_sig
->params
[i
];
2370 MonoType
*p2
= method_sig
->params
[i
+ method_offset
];
2372 if (!mono_delegate_param_equal (p1
, p2
))
2376 if (!mono_delegate_ret_equal (delegate_sig
->ret
, method_sig
->ret
))
2383 * verify_ldftn_delegate:
2385 * Verify properties of ldftn based delegates.
2388 verify_ldftn_delegate (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2390 MonoMethod
*method
= funptr
->method
;
2392 /*ldftn non-final virtuals only allowed if method is not static,
2393 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2394 * This rules doesn't apply if the object on stack is a boxed valuetype.
2396 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !(method
->klass
->flags
& TYPE_ATTRIBUTE_SEALED
) && !stack_slot_is_boxed_value (value
)) {
2397 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2398 if (IS_FAIL_FAST_MODE (ctx
) && ctx
->has_this_store
)
2399 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx
->ip_offset
));
2401 /*current method must not be static*/
2402 if (ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)
2403 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx
->ip_offset
));
2405 /*value is the this pointer, loaded using ldarg.0 */
2406 if (!stack_slot_is_this_pointer (value
))
2407 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
));
2409 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
;
2414 * verify_delegate_compatibility:
2416 * Verify delegate creation sequence.
2420 verify_delegate_compatibility (VerifyContext
*ctx
, MonoClass
*delegate
, ILStackDesc
*value
, ILStackDesc
*funptr
)
2422 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2423 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2425 MonoMethod
*invoke
, *method
;
2426 const guint8
*ip
= ctx
->header
->code
;
2427 guint32 ip_offset
= ctx
->ip_offset
;
2428 gboolean is_static_ldftn
= FALSE
, is_first_arg_bound
= FALSE
;
2430 if (stack_slot_get_type (funptr
) != TYPE_PTR
|| !funptr
->method
) {
2431 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx
->ip_offset
));
2435 invoke
= mono_get_delegate_invoke (delegate
);
2436 method
= funptr
->method
;
2438 if (!method
|| !mono_method_signature (method
)) {
2439 char *name
= mono_type_get_full_name (delegate
);
2440 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name
, ctx
->ip_offset
));
2445 if (!invoke
|| !mono_method_signature (invoke
)) {
2446 char *name
= mono_type_get_full_name (delegate
);
2447 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name
, ctx
->ip_offset
));
2452 is_static_ldftn
= (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) && method
->flags
& METHOD_ATTRIBUTE_STATIC
;
2454 if (is_static_ldftn
)
2455 is_first_arg_bound
= mono_method_signature (invoke
)->param_count
+ 1 == mono_method_signature (method
)->param_count
;
2457 if (!mono_delegate_signature_equal (mono_method_signature (invoke
), mono_method_signature (method
), is_first_arg_bound
)) {
2458 char *fun_sig
= mono_signature_get_desc (mono_method_signature (method
), FALSE
);
2459 char *invoke_sig
= mono_signature_get_desc (mono_method_signature (invoke
), FALSE
);
2460 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
));
2462 g_free (invoke_sig
);
2466 * Delegate code sequences:
2472 * [-6] ldvirtftn token
2476 if (ip_offset
> 5 && IS_LOAD_FUN_PTR (CEE_LDFTN
)) {
2477 verify_ldftn_delegate (ctx
, delegate
, value
, funptr
);
2478 } else if (ip_offset
> 6 && IS_VALID_OPCODE (7, CEE_DUP
) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN
)) {
2479 ctx
->code
[ip_offset
- 6].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2481 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx
->ip_offset
));
2483 ctx
->code
[ip_offset
].flags
|= IL_CODE_DELEGATE_SEQUENCE
;
2486 if (is_first_arg_bound
) {
2487 if (mono_method_signature (method
)->param_count
== 0 || !verify_stack_type_compatibility_full (ctx
, mono_method_signature (method
)->params
[0], value
, FALSE
, TRUE
))
2488 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2490 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
2491 if (!stack_slot_is_null_literal (value
) && !is_first_arg_bound
)
2492 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx
->ip_offset
));
2494 if (!verify_stack_type_compatibility_full (ctx
, &method
->klass
->byval_arg
, value
, FALSE
, TRUE
) && !stack_slot_is_null_literal (value
))
2495 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx
->ip_offset
));
2499 if (stack_slot_get_type (value
) != TYPE_COMPLEX
)
2500 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx
->ip_offset
));
2502 #undef IS_VALID_OPCODE
2503 #undef IS_LOAD_FUN_PTR
2506 /* implement the opcode checks*/
2508 push_arg (VerifyContext
*ctx
, unsigned int arg
, int take_addr
)
2512 if (arg
>= ctx
->max_args
) {
2514 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
2516 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d", arg
+ 1));
2517 if (check_overflow (ctx
)) //FIXME: what sane value could we ever push?
2518 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
2520 } else if (check_overflow (ctx
)) {
2521 /*We must let the value be pushed, otherwise we would get an underflow error*/
2522 check_unverifiable_type (ctx
, ctx
->params
[arg
]);
2523 if (ctx
->params
[arg
]->byref
&& take_addr
)
2524 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
2525 top
= stack_push (ctx
);
2526 if (!set_stack_value (ctx
, top
, ctx
->params
[arg
], take_addr
))
2529 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
2531 ctx
->has_this_store
= TRUE
;
2533 top
->stype
|= THIS_POINTER_MASK
;
2534 if (mono_method_is_constructor (ctx
->method
) && !ctx
->super_ctor_called
&& !ctx
->method
->klass
->valuetype
)
2535 top
->stype
|= UNINIT_THIS_MASK
;
2541 push_local (VerifyContext
*ctx
, guint32 arg
, int take_addr
)
2543 if (arg
>= ctx
->num_locals
) {
2544 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local %d", arg
+ 1));
2545 } else if (check_overflow (ctx
)) {
2546 /*We must let the value be pushed, otherwise we would get an underflow error*/
2547 check_unverifiable_type (ctx
, ctx
->locals
[arg
]);
2548 if (ctx
->locals
[arg
]->byref
&& take_addr
)
2549 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx
->ip_offset
));
2551 set_stack_value (ctx
, stack_push (ctx
), ctx
->locals
[arg
], take_addr
);
2556 store_arg (VerifyContext
*ctx
, guint32 arg
)
2560 if (arg
>= ctx
->max_args
) {
2561 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
2562 if (check_underflow (ctx
, 1))
2567 if (check_underflow (ctx
, 1)) {
2568 value
= stack_pop (ctx
);
2569 if (!verify_stack_type_compatibility (ctx
, ctx
->params
[arg
], value
)) {
2570 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
2573 if (arg
== 0 && !(ctx
->method
->flags
& METHOD_ATTRIBUTE_STATIC
))
2574 ctx
->has_this_store
= 1;
2578 store_local (VerifyContext
*ctx
, guint32 arg
)
2581 if (arg
>= ctx
->num_locals
) {
2582 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg
+ 1, ctx
->ip_offset
));
2586 /*TODO verify definite assigment */
2587 if (check_underflow (ctx
, 1)) {
2588 value
= stack_pop(ctx
);
2589 if (!verify_stack_type_compatibility (ctx
, ctx
->locals
[arg
], value
)) {
2590 char *expected
= mono_type_full_name (ctx
->locals
[arg
]);
2591 char *found
= stack_slot_full_name (value
);
2592 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2603 /*FIXME add and sub needs special care here*/
2605 do_binop (VerifyContext
*ctx
, unsigned int opcode
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
2607 ILStackDesc
*a
, *b
, *top
;
2608 int idxa
, idxb
, complexMerge
= 0;
2611 if (!check_underflow (ctx
, 2))
2613 b
= stack_pop (ctx
);
2614 a
= stack_pop (ctx
);
2616 idxa
= stack_slot_get_underlying_type (a
);
2617 if (stack_slot_is_managed_pointer (a
)) {
2622 idxb
= stack_slot_get_underlying_type (b
);
2623 if (stack_slot_is_managed_pointer (b
)) {
2630 res
= table
[idxa
][idxb
];
2632 VERIFIER_DEBUG ( printf ("binop res %d\n", res
); );
2633 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
2635 top
= stack_push (ctx
);
2636 if (res
== TYPE_INV
) {
2637 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
)));
2638 copy_stack_value (top
, a
);
2642 if (res
& NON_VERIFIABLE_RESULT
) {
2643 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
)));
2645 res
= res
& ~NON_VERIFIABLE_RESULT
;
2648 if (complexMerge
&& res
== TYPE_PTR
) {
2649 if (complexMerge
== 1)
2650 copy_stack_value (top
, a
);
2651 else if (complexMerge
== 2)
2652 copy_stack_value (top
, b
);
2654 * There is no need to merge the type of two pointers.
2655 * The only valid operation is subtraction, that returns a native
2656 * int as result and can be used with any 2 pointer kinds.
2657 * This is valid acording to Patition III 1.1.4
2666 do_boolean_branch_op (VerifyContext
*ctx
, int delta
)
2668 int target
= ctx
->ip_offset
+ delta
;
2671 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
2673 if (target
< 0 || target
>= ctx
->code_size
) {
2674 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx
->ip_offset
));
2678 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
2680 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2683 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2687 ctx
->target
= target
;
2689 if (!check_underflow (ctx
, 1))
2692 top
= stack_pop (ctx
);
2693 if (!is_valid_bool_arg (top
))
2694 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
));
2696 check_unmanaged_pointer (ctx
, top
);
2700 stack_slot_is_complex_type_not_reference_type (ILStackDesc
*slot
)
2702 return stack_slot_get_type (slot
) == TYPE_COMPLEX
&& !MONO_TYPE_IS_REFERENCE (slot
->type
) && !stack_slot_is_boxed_value (slot
);
2706 do_branch_op (VerifyContext
*ctx
, signed int delta
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
])
2711 int target
= ctx
->ip_offset
+ delta
;
2713 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx
->ip_offset
, delta
, target
); );
2715 if (target
< 0 || target
>= ctx
->code_size
) {
2716 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
2720 switch (is_valid_cmp_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
2721 case 1: /*FIXME use constants and not magic numbers.*/
2722 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2725 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
2729 ctx
->target
= target
;
2731 if (!check_underflow (ctx
, 2))
2734 b
= stack_pop (ctx
);
2735 a
= stack_pop (ctx
);
2737 idxa
= stack_slot_get_underlying_type (a
);
2738 if (stack_slot_is_managed_pointer (a
))
2741 idxb
= stack_slot_get_underlying_type (b
);
2742 if (stack_slot_is_managed_pointer (b
))
2745 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
2750 res
= table
[idxa
][idxb
];
2753 VERIFIER_DEBUG ( printf ("branch res %d\n", res
); );
2754 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa
, idxb
); );
2756 if (res
== TYPE_INV
) {
2757 CODE_NOT_VERIFIABLE (ctx
,
2758 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
));
2759 } else if (res
& NON_VERIFIABLE_RESULT
) {
2760 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
));
2761 res
= res
& ~NON_VERIFIABLE_RESULT
;
2766 do_cmp_op (VerifyContext
*ctx
, const unsigned char table
[TYPE_MAX
][TYPE_MAX
], guint32 opcode
)
2772 if (!check_underflow (ctx
, 2))
2774 b
= stack_pop (ctx
);
2775 a
= stack_pop (ctx
);
2777 if (opcode
== CEE_CGT_UN
) {
2778 if (stack_slot_get_type (a
) == TYPE_COMPLEX
&& stack_slot_get_type (b
) == TYPE_COMPLEX
) {
2779 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
2784 idxa
= stack_slot_get_underlying_type (a
);
2785 if (stack_slot_is_managed_pointer (a
))
2788 idxb
= stack_slot_get_underlying_type (b
);
2789 if (stack_slot_is_managed_pointer (b
))
2792 if (stack_slot_is_complex_type_not_reference_type (a
) || stack_slot_is_complex_type_not_reference_type (b
)) {
2797 res
= table
[idxa
][idxb
];
2800 if(res
== TYPE_INV
) {
2801 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
));
2802 } else if (res
& NON_VERIFIABLE_RESULT
) {
2803 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
));
2804 res
= res
& ~NON_VERIFIABLE_RESULT
;
2806 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
2810 do_ret (VerifyContext
*ctx
)
2812 MonoType
*ret
= ctx
->signature
->ret
;
2813 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2814 if (ret
->type
!= MONO_TYPE_VOID
) {
2816 if (!check_underflow (ctx
, 1))
2819 top
= stack_pop(ctx
);
2821 if (!verify_stack_type_compatibility (ctx
, ctx
->signature
->ret
, top
)) {
2822 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx
->ip_offset
));
2826 if (ret
->byref
|| ret
->type
== MONO_TYPE_TYPEDBYREF
|| mono_type_is_value_type (ret
, "System", "ArgIterator") || mono_type_is_value_type (ret
, "System", "RuntimeArgumentHandle"))
2827 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx
->ip_offset
));
2830 if (ctx
->eval
.size
> 0) {
2831 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx
->eval
.size
, ctx
->ip_offset
));
2833 if (in_any_block (ctx
->header
, ctx
->ip_offset
))
2834 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx
->ip_offset
));
2838 * 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.
2839 * This is illegal but mono_get_method_full decoded it.
2840 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2843 do_invoke_method (VerifyContext
*ctx
, int method_token
, gboolean
virtual)
2846 MonoMethodSignature
*sig
;
2849 gboolean virt_check_this
= FALSE
;
2850 gboolean constrained
= ctx
->prefix_set
& PREFIX_CONSTRAINED
;
2852 if (!(method
= verifier_load_method (ctx
, method_token
, virtual ? "callvirt" : "call")))
2856 CLEAR_PREFIX (ctx
, PREFIX_CONSTRAINED
);
2858 if (method
->klass
->valuetype
) // && !constrained ???
2859 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx
->ip_offset
));
2861 if ((method
->flags
& METHOD_ATTRIBUTE_STATIC
))
2862 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx
->ip_offset
));
2865 if (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)
2866 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx
->ip_offset
));
2868 if ((method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && !(method
->flags
& METHOD_ATTRIBUTE_FINAL
) && !(method
->klass
->flags
& TYPE_ATTRIBUTE_SEALED
)) {
2869 virt_check_this
= TRUE
;
2870 ctx
->code
[ctx
->ip_offset
].flags
|= IL_CODE_CALL_NONFINAL_VIRTUAL
;
2874 if (!(sig
= mono_method_get_signature_full (method
, ctx
->image
, method_token
, ctx
->generic_context
)))
2875 sig
= mono_method_get_signature (method
, ctx
->image
, method_token
);
2878 char *name
= mono_type_get_full_name (method
->klass
);
2879 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name
, method
->name
, ctx
->ip_offset
));
2884 param_count
= sig
->param_count
+ sig
->hasthis
;
2885 if (!check_underflow (ctx
, param_count
))
2888 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
2889 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i
); );
2890 value
= stack_pop (ctx
);
2891 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
2892 char *stack_name
= stack_slot_full_name (value
);
2893 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
2894 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
));
2895 g_free (stack_name
);
2899 if (stack_slot_is_managed_mutability_pointer (value
))
2900 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
));
2902 if ((ctx
->prefix_set
& PREFIX_TAIL
) && stack_slot_is_managed_pointer (value
)) {
2903 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
));
2909 MonoType
*type
= &method
->klass
->byval_arg
;
2912 if (mono_method_is_constructor (method
) && !method
->klass
->valuetype
) {
2913 if (!mono_method_is_constructor (ctx
->method
))
2914 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx
->ip_offset
));
2915 if (method
->klass
!= ctx
->method
->klass
->parent
&& method
->klass
!= ctx
->method
->klass
)
2916 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
));
2918 ctx
->super_ctor_called
= TRUE
;
2919 value
= stack_pop_safe (ctx
);
2920 if ((value
->stype
& THIS_POINTER_MASK
) != THIS_POINTER_MASK
)
2921 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx
->ip_offset
));
2923 value
= stack_pop (ctx
);
2926 copy_stack_value (©
, value
);
2927 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2928 //Other parts of the code suffer from the same issue of
2929 copy
.type
= mono_type_get_type_byval (copy
.type
);
2930 copy
.stype
&= ~POINTER_MASK
;
2932 if (virt_check_this
&& !stack_slot_is_this_pointer (value
) && !(method
->klass
->valuetype
|| stack_slot_is_boxed_value (value
)))
2933 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
));
2935 if (constrained
&& virtual) {
2936 if (!stack_slot_is_managed_pointer (value
))
2937 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx
->ip_offset
));
2938 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value
->type
), ctx
->constrained_type
, TRUE
))
2939 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx
->ip_offset
));
2940 copy
.stype
|= BOXED_MASK
;
2942 if (stack_slot_is_managed_pointer (value
) && !mono_class_from_mono_type (value
->type
)->valuetype
)
2943 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
));
2945 if (!virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !method
->klass
->valuetype
&& !stack_slot_is_boxed_value (value
))
2946 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx
->ip_offset
));
2948 if (virtual && mono_class_from_mono_type (value
->type
)->valuetype
&& !stack_slot_is_boxed_value (value
))
2949 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx
->ip_offset
));
2951 if (method
->klass
->valuetype
&& (stack_slot_is_boxed_value (value
) || !stack_slot_is_managed_pointer (value
)))
2952 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
));
2954 if (!verify_stack_type_compatibility (ctx
, type
, ©
))
2955 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx
->ip_offset
));
2957 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, mono_class_from_mono_type (value
->type
))) {
2958 char *name
= mono_method_full_name (method
, TRUE
);
2959 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
2963 } else if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
2964 char *name
= mono_method_full_name (method
, TRUE
);
2965 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Method %s is not accessible at 0x%04x", name
, ctx
->ip_offset
), MONO_EXCEPTION_METHOD_ACCESS
);
2969 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
2970 if (check_overflow (ctx
)) {
2971 value
= stack_push (ctx
);
2972 set_stack_value (ctx
, value
, sig
->ret
, FALSE
);
2973 if ((ctx
->prefix_set
& PREFIX_READONLY
) && method
->klass
->rank
&& !strcmp (method
->name
, "Address")) {
2974 ctx
->prefix_set
&= ~PREFIX_READONLY
;
2975 value
->stype
|= CMMP_MASK
;
2980 if ((ctx
->prefix_set
& PREFIX_TAIL
)) {
2981 if (!mono_delegate_ret_equal (mono_method_signature (ctx
->method
)->ret
, sig
->ret
))
2982 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx
->ip_offset
));
2983 if (ctx
->header
->code
[ctx
->ip_offset
+ 5] != CEE_RET
)
2984 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx
->ip_offset
));
2990 do_push_static_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
2992 MonoClassField
*field
;
2994 if (!check_overflow (ctx
))
2997 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
2999 if (!(field
= verifier_load_field (ctx
, token
, &klass
, take_addr
? "ldsflda" : "ldsfld")))
3002 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3003 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx
->ip_offset
));
3006 /*taking the address of initonly field only works from the static constructor */
3007 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3008 !(field
->parent
== ctx
->method
->klass
&& (ctx
->method
->flags
& (METHOD_ATTRIBUTE_SPECIAL_NAME
| METHOD_ATTRIBUTE_STATIC
)) && !strcmp (".cctor", ctx
->method
->name
)))
3009 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3011 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3012 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3014 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3018 do_store_static_field (VerifyContext
*ctx
, int token
) {
3019 MonoClassField
*field
;
3022 CLEAR_PREFIX (ctx
, PREFIX_VOLATILE
);
3024 if (!check_underflow (ctx
, 1))
3027 value
= stack_pop (ctx
);
3029 if (!(field
= verifier_load_field (ctx
, token
, &klass
, "stsfld")))
3032 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
3033 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx
->ip_offset
));
3037 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3038 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx
->ip_offset
));
3042 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3043 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3045 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3046 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in static field store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3050 check_is_valid_type_for_field_ops (VerifyContext
*ctx
, int token
, ILStackDesc
*obj
, MonoClassField
**ret_field
, const char *opcode
)
3052 MonoClassField
*field
;
3054 gboolean is_pointer
;
3056 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3057 if (!(field
= verifier_load_field (ctx
, token
, &klass
, opcode
)))
3061 //the value on stack is going to be used as a pointer
3062 is_pointer
= stack_slot_get_type (obj
) == TYPE_PTR
|| (stack_slot_get_type (obj
) == TYPE_NATIVE_INT
&& !get_stack_type (&field
->parent
->byval_arg
));
3064 if (field
->type
->type
== MONO_TYPE_TYPEDBYREF
) {
3065 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx
->ip_offset
));
3068 g_assert (obj
->type
);
3070 /*The value on the stack must be a subclass of the defining type of the field*/
3071 /* we need to check if we can load the field from the stack value*/
3073 if (stack_slot_get_underlying_type (obj
) == TYPE_NATIVE_INT
)
3074 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx
->ip_offset
));
3076 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, NULL
))
3077 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3079 if (!field
->parent
->valuetype
&& stack_slot_is_managed_pointer (obj
))
3080 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
));
3082 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3083 if (field
->parent
->valuetype
&& stack_slot_is_boxed_value (obj
))
3084 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
));
3086 if (!stack_slot_is_null_literal (obj
) && !verify_stack_type_compatibility_full (ctx
, &field
->parent
->byval_arg
, obj
, TRUE
, FALSE
)) {
3087 char *found
= stack_slot_full_name (obj
);
3088 char *expected
= mono_type_full_name (&field
->parent
->byval_arg
);
3089 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
));
3094 if (!IS_SKIP_VISIBILITY (ctx
) && !mono_method_can_access_field_full (ctx
->method
, field
, mono_class_from_mono_type (obj
->type
)))
3095 CODE_NOT_VERIFIABLE2 (ctx
, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx
->ip_offset
), MONO_EXCEPTION_FIELD_ACCESS
);
3098 check_unmanaged_pointer (ctx
, obj
);
3103 do_push_field (VerifyContext
*ctx
, int token
, gboolean take_addr
)
3106 MonoClassField
*field
;
3109 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3111 if (!check_underflow (ctx
, 1))
3113 obj
= stack_pop_safe (ctx
);
3115 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, take_addr
? "ldflda" : "ldfld"))
3118 if (take_addr
&& field
->parent
->valuetype
&& !stack_slot_is_managed_pointer (obj
))
3119 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx
->ip_offset
));
3121 if (take_addr
&& (field
->type
->attrs
& FIELD_ATTRIBUTE_INIT_ONLY
) &&
3122 !(field
->parent
== ctx
->method
->klass
&& mono_method_is_constructor (ctx
->method
)))
3123 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx
->ip_offset
));
3125 set_stack_value (ctx
, stack_push (ctx
), field
->type
, take_addr
);
3129 do_store_field (VerifyContext
*ctx
, int token
)
3131 ILStackDesc
*value
, *obj
;
3132 MonoClassField
*field
;
3133 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3135 if (!check_underflow (ctx
, 2))
3138 value
= stack_pop (ctx
);
3139 obj
= stack_pop_safe (ctx
);
3141 if (!check_is_valid_type_for_field_ops (ctx
, token
, obj
, &field
, "stfld"))
3144 if (!verify_stack_type_compatibility (ctx
, field
->type
, value
))
3145 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3148 /*TODO proper handle for Nullable<T>*/
3150 do_box_value (VerifyContext
*ctx
, int klass_token
)
3153 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "box");
3159 if (!check_underflow (ctx
, 1))
3162 value
= stack_pop (ctx
);
3163 /*box is a nop for reference types*/
3165 if (stack_slot_get_underlying_type (value
) == TYPE_COMPLEX
&& MONO_TYPE_IS_REFERENCE (value
->type
) && MONO_TYPE_IS_REFERENCE (type
)) {
3166 stack_push_stack_val (ctx
, value
)->stype
|= BOXED_MASK
;
3171 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3172 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx
->ip_offset
));
3174 klass
= mono_class_from_mono_type (type
);
3175 if (mono_class_is_nullable (klass
))
3176 type
= &mono_class_get_nullable_param (klass
)->byval_arg
;
3177 stack_push_val (ctx
, TYPE_COMPLEX
| BOXED_MASK
, type
);
3181 do_unbox_value (VerifyContext
*ctx
, int klass_token
)
3184 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox");
3189 if (!check_underflow (ctx
, 1))
3192 if (!mono_class_from_mono_type (type
)->valuetype
)
3193 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx
->ip_offset
));
3195 value
= stack_pop (ctx
);
3197 /*Value should be: a boxed valuetype or a reference type*/
3198 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3199 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3200 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
));
3202 set_stack_value (ctx
, value
= stack_push (ctx
), mono_type_get_type_byref (type
), FALSE
);
3203 value
->stype
|= CMMP_MASK
;
3207 do_unbox_any (VerifyContext
*ctx
, int klass_token
)
3210 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "unbox.any");
3215 if (!check_underflow (ctx
, 1))
3218 value
= stack_pop (ctx
);
3220 /*Value should be: a boxed valuetype or a reference type*/
3221 if (!(stack_slot_get_type (value
) == TYPE_COMPLEX
&&
3222 (stack_slot_is_boxed_value (value
) || !mono_class_from_mono_type (value
->type
)->valuetype
)))
3223 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
));
3225 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3229 do_unary_math_op (VerifyContext
*ctx
, int op
)
3232 if (!check_underflow (ctx
, 1))
3234 value
= stack_pop (ctx
);
3235 switch (stack_slot_get_type (value
)) {
3238 case TYPE_NATIVE_INT
:
3243 case TYPE_COMPLEX
: /*only enums are ok*/
3244 if (mono_type_is_enum_type (value
->type
))
3247 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx
->ip_offset
));
3249 stack_push_stack_val (ctx
, value
);
3253 do_conversion (VerifyContext
*ctx
, int kind
)
3256 if (!check_underflow (ctx
, 1))
3258 value
= stack_pop (ctx
);
3260 switch (stack_slot_get_type (value
)) {
3263 case TYPE_NATIVE_INT
:
3267 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
));
3272 stack_push_val (ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
3275 stack_push_val (ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
3278 stack_push_val (ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
3280 case TYPE_NATIVE_INT
:
3281 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3284 g_error ("unknown type %02x in conversion", kind
);
3290 do_load_token (VerifyContext
*ctx
, int token
)
3293 MonoClass
*handle_class
;
3294 if (!check_overflow (ctx
))
3296 handle
= mono_ldtoken (ctx
->image
, token
, &handle_class
, ctx
->generic_context
);
3298 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token
, ctx
->ip_offset
));
3301 if (handle_class
== mono_defaults
.typehandle_class
) {
3302 mono_type_is_valid_in_context (ctx
, (MonoType
*)handle
);
3303 } else if (handle_class
== mono_defaults
.methodhandle_class
) {
3304 mono_method_is_valid_in_context (ctx
, (MonoMethod
*)handle
);
3305 } else if (handle_class
== mono_defaults
.fieldhandle_class
) {
3306 mono_type_is_valid_in_context (ctx
, &((MonoClassField
*)handle
)->parent
->byval_arg
);
3308 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
3310 stack_push_val (ctx
, TYPE_COMPLEX
, mono_class_get_type (handle_class
));
3314 do_ldobj_value (VerifyContext
*ctx
, int token
)
3317 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "ldobj");
3318 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3323 if (!check_underflow (ctx
, 1))
3326 value
= stack_pop (ctx
);
3327 if (!stack_slot_is_managed_pointer (value
)
3328 && stack_slot_get_type (value
) != TYPE_NATIVE_INT
3329 && !(stack_slot_get_type (value
) == TYPE_PTR
&& value
->type
->type
!= MONO_TYPE_FNPTR
)) {
3330 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value
), ctx
->ip_offset
));
3334 if (stack_slot_get_type (value
) == TYPE_NATIVE_INT
)
3335 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx
->ip_offset
));
3337 /*We have a byval on the stack, but the comparison must be strict. */
3338 if (!verify_type_compatibility_full (ctx
, type
, mono_type_get_type_byval (value
->type
), TRUE
))
3339 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx
->ip_offset
));
3341 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3345 do_stobj (VerifyContext
*ctx
, int token
)
3347 ILStackDesc
*dest
, *src
;
3348 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "stobj");
3349 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3354 if (!check_underflow (ctx
, 2))
3357 src
= stack_pop (ctx
);
3358 dest
= stack_pop (ctx
);
3360 if (stack_slot_is_managed_mutability_pointer (dest
))
3361 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx
->ip_offset
));
3363 if (!stack_slot_is_managed_pointer (dest
))
3364 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx
->ip_offset
));
3366 if (stack_slot_is_boxed_value (src
) && !MONO_TYPE_IS_REFERENCE (src
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3367 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
));
3369 if (!verify_stack_type_compatibility (ctx
, type
, src
))
3370 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3372 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3373 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx
->ip_offset
));
3377 do_cpobj (VerifyContext
*ctx
, int token
)
3379 ILStackDesc
*dest
, *src
;
3380 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "cpobj");
3384 if (!check_underflow (ctx
, 2))
3387 src
= stack_pop (ctx
);
3388 dest
= stack_pop (ctx
);
3390 if (!stack_slot_is_managed_pointer (src
))
3391 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx
->ip_offset
));
3393 if (!stack_slot_is_managed_pointer (dest
))
3394 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx
->ip_offset
));
3396 if (stack_slot_is_managed_mutability_pointer (dest
))
3397 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx
->ip_offset
));
3399 if (!verify_type_compatibility (ctx
, type
, mono_type_get_type_byval (src
->type
)))
3400 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3402 if (!verify_type_compatibility (ctx
, mono_type_get_type_byval (dest
->type
), type
))
3403 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx
->ip_offset
));
3407 do_initobj (VerifyContext
*ctx
, int token
)
3410 MonoType
*stack
, *type
= get_boxable_mono_type (ctx
, token
, "initobj");
3414 if (!check_underflow (ctx
, 1))
3417 obj
= stack_pop (ctx
);
3419 if (!stack_slot_is_managed_pointer (obj
))
3420 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx
->ip_offset
));
3422 if (stack_slot_is_managed_mutability_pointer (obj
))
3423 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx
->ip_offset
));
3425 stack
= mono_type_get_type_byval (obj
->type
);
3426 if (MONO_TYPE_IS_REFERENCE (stack
)) {
3427 if (!verify_type_compatibility (ctx
, stack
, type
))
3428 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3429 else if (IS_STRICT_MODE (ctx
) && !mono_metadata_type_equal (type
, stack
))
3430 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx
->ip_offset
));
3431 } else if (!verify_type_compatibility (ctx
, stack
, type
)) {
3432 char *expected_name
= mono_type_full_name (type
);
3433 char *stack_name
= mono_type_full_name (stack
);
3435 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
));
3436 g_free (expected_name
);
3437 g_free (stack_name
);
3442 do_newobj (VerifyContext
*ctx
, int token
)
3446 MonoMethodSignature
*sig
;
3448 gboolean is_delegate
= FALSE
;
3450 if (!(method
= verifier_load_method (ctx
, token
, "newobj")))
3453 if (!mono_method_is_constructor (method
)) {
3454 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token
, ctx
->ip_offset
));
3458 if (method
->klass
->flags
& (TYPE_ATTRIBUTE_ABSTRACT
| TYPE_ATTRIBUTE_INTERFACE
))
3459 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx
->ip_offset
));
3461 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
)) {
3462 char *from
= mono_method_full_name (ctx
->method
, TRUE
);
3463 char *to
= mono_method_full_name (method
, TRUE
);
3464 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
);
3469 //FIXME use mono_method_get_signature_full
3470 sig
= mono_method_signature (method
);
3472 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx
->ip_offset
));
3476 if (!check_underflow (ctx
, sig
->param_count
))
3479 is_delegate
= method
->klass
->parent
== mono_defaults
.multicastdelegate_class
;
3482 ILStackDesc
*funptr
;
3483 //first arg is object, second arg is fun ptr
3484 if (sig
->param_count
!= 2) {
3485 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx
->ip_offset
));
3488 funptr
= stack_pop (ctx
);
3489 value
= stack_pop (ctx
);
3490 verify_delegate_compatibility (ctx
, method
->klass
, value
, funptr
);
3492 for (i
= sig
->param_count
- 1; i
>= 0; --i
) {
3493 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i
); );
3494 value
= stack_pop (ctx
);
3495 if (!verify_stack_type_compatibility (ctx
, sig
->params
[i
], value
)) {
3496 char *stack_name
= stack_slot_full_name (value
);
3497 char *sig_name
= mono_type_full_name (sig
->params
[i
]);
3498 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
));
3499 g_free (stack_name
);
3503 if (stack_slot_is_managed_mutability_pointer (value
))
3504 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx
->ip_offset
));
3508 if (check_overflow (ctx
))
3509 set_stack_value (ctx
, stack_push (ctx
), &method
->klass
->byval_arg
, FALSE
);
3513 do_cast (VerifyContext
*ctx
, int token
, const char *opcode
) {
3519 if (!check_underflow (ctx
, 1))
3522 if (!(type
= verifier_load_type (ctx
, token
, opcode
)))
3526 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid %s type at 0x%04x", opcode
, ctx
->ip_offset
));
3530 value
= stack_pop (ctx
);
3531 is_boxed
= stack_slot_is_boxed_value (value
);
3533 if (stack_slot_is_managed_pointer (value
))
3534 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
3535 else if (!MONO_TYPE_IS_REFERENCE (value
->type
) && !is_boxed
) {
3536 char *name
= stack_slot_full_name (value
);
3537 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
));
3541 switch (value
->type
->type
) {
3542 case MONO_TYPE_FNPTR
:
3544 case MONO_TYPE_TYPEDBYREF
:
3545 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode
, ctx
->ip_offset
));
3548 do_box
= is_boxed
|| mono_type_is_generic_argument(type
) || mono_class_from_mono_type (type
)->valuetype
;
3549 stack_push_val (ctx
, TYPE_COMPLEX
| (do_box
? BOXED_MASK
: 0), type
);
3553 mono_type_from_opcode (int opcode
) {
3561 return &mono_defaults
.sbyte_class
->byval_arg
;
3569 return &mono_defaults
.int16_class
->byval_arg
;
3577 return &mono_defaults
.int32_class
->byval_arg
;
3583 return &mono_defaults
.int64_class
->byval_arg
;
3589 return &mono_defaults
.single_class
->byval_arg
;
3595 return &mono_defaults
.double_class
->byval_arg
;
3601 return &mono_defaults
.int_class
->byval_arg
;
3605 case CEE_LDELEM_REF
:
3606 case CEE_STELEM_REF
:
3607 return &mono_defaults
.object_class
->byval_arg
;
3610 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode
);
3616 do_load_indirect (VerifyContext
*ctx
, int opcode
)
3619 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3621 if (!check_underflow (ctx
, 1))
3624 value
= stack_pop (ctx
);
3625 if (!stack_slot_is_managed_pointer (value
)) {
3626 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx
->ip_offset
));
3627 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
3631 if (opcode
== CEE_LDIND_REF
) {
3632 if (stack_slot_get_underlying_type (value
) != TYPE_COMPLEX
|| mono_class_from_mono_type (value
->type
)->valuetype
)
3633 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx
->ip_offset
));
3634 set_stack_value (ctx
, stack_push (ctx
), mono_type_get_type_byval (value
->type
), FALSE
);
3636 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (value
->type
), TRUE
))
3637 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
3638 set_stack_value (ctx
, stack_push (ctx
), mono_type_from_opcode (opcode
), FALSE
);
3643 do_store_indirect (VerifyContext
*ctx
, int opcode
)
3645 ILStackDesc
*addr
, *val
;
3646 CLEAR_PREFIX (ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
3648 if (!check_underflow (ctx
, 2))
3651 val
= stack_pop (ctx
);
3652 addr
= stack_pop (ctx
);
3654 check_unmanaged_pointer (ctx
, addr
);
3656 if (!stack_slot_is_managed_pointer (addr
) && stack_slot_get_type (addr
) != TYPE_PTR
) {
3657 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx
->ip_offset
));
3661 if (stack_slot_is_managed_mutability_pointer (addr
)) {
3662 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx
->ip_offset
));
3666 if (!verify_type_compatibility_full (ctx
, mono_type_from_opcode (opcode
), mono_type_get_type_byval (addr
->type
), TRUE
))
3667 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
3669 if (!verify_stack_type_compatibility (ctx
, mono_type_from_opcode (opcode
), val
))
3670 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode
, ctx
->ip_offset
));
3674 do_newarr (VerifyContext
*ctx
, int token
)
3677 MonoType
*type
= get_boxable_mono_type (ctx
, token
, "newarr");
3682 if (!check_underflow (ctx
, 1))
3685 value
= stack_pop (ctx
);
3686 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
3687 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
));
3689 set_stack_value (ctx
, stack_push (ctx
), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type
), 1)), FALSE
);
3692 /*FIXME handle arrays that are not 0-indexed*/
3694 do_ldlen (VerifyContext
*ctx
)
3698 if (!check_underflow (ctx
, 1))
3701 value
= stack_pop (ctx
);
3703 if (stack_slot_get_type (value
) != TYPE_COMPLEX
|| value
->type
->type
!= MONO_TYPE_SZARRAY
)
3704 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx
->ip_offset
));
3706 stack_push_val (ctx
, TYPE_NATIVE_INT
, &mono_defaults
.int_class
->byval_arg
);
3709 /*FIXME handle arrays that are not 0-indexed*/
3710 /*FIXME handle readonly prefix and CMMP*/
3712 do_ldelema (VerifyContext
*ctx
, int klass_token
)
3714 ILStackDesc
*index
, *array
, *res
;
3715 MonoType
*type
= get_boxable_mono_type (ctx
, klass_token
, "ldelema");
3721 if (!check_underflow (ctx
, 2))
3724 index
= stack_pop (ctx
);
3725 array
= stack_pop (ctx
);
3727 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
3728 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
));
3730 if (!stack_slot_is_null_literal (array
)) {
3731 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
3732 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array
), ctx
->ip_offset
));
3734 if (get_stack_type (type
) == TYPE_I4
|| get_stack_type (type
) == TYPE_NATIVE_INT
) {
3735 valid
= verify_type_compatibility_full (ctx
, type
, &array
->type
->data
.klass
->byval_arg
, TRUE
);
3737 valid
= mono_metadata_type_equal (type
, &array
->type
->data
.klass
->byval_arg
);
3740 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx
->ip_offset
));
3744 res
= stack_push (ctx
);
3745 set_stack_value (ctx
, res
, type
, TRUE
);
3746 if (ctx
->prefix_set
& PREFIX_READONLY
) {
3747 ctx
->prefix_set
&= ~PREFIX_READONLY
;
3748 res
->stype
|= CMMP_MASK
;
3753 * FIXME handle arrays that are not 0-indexed
3754 * FIXME handle readonly prefix and CMMP
3757 do_ldelem (VerifyContext
*ctx
, int opcode
, int token
)
3759 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3760 ILStackDesc
*index
, *array
;
3762 if (!check_underflow (ctx
, 2))
3765 if (opcode
== CEE_LDELEM
) {
3766 if (!(type
= verifier_load_type (ctx
, token
, "ldelem.any"))) {
3767 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
3771 type
= mono_type_from_opcode (opcode
);
3774 index
= stack_pop (ctx
);
3775 array
= stack_pop (ctx
);
3777 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
3778 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
));
3780 if (!stack_slot_is_null_literal (array
)) {
3781 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
)
3782 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
));
3784 if (opcode
== CEE_LDELEM_REF
) {
3785 if (array
->type
->data
.klass
->valuetype
)
3786 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx
->ip_offset
));
3787 type
= &array
->type
->data
.klass
->byval_arg
;
3789 MonoType
*candidate
= &array
->type
->data
.klass
->byval_arg
;
3790 if (IS_STRICT_MODE (ctx
)) {
3791 MonoType
*underlying_type
= mono_type_get_underlying_type_any (type
);
3792 MonoType
*underlying_candidate
= mono_type_get_underlying_type_any (candidate
);
3793 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
)) ||
3794 (IS_ONE_OF2 (underlying_candidate
->type
, MONO_TYPE_I4
, MONO_TYPE_U4
) && IS_ONE_OF2 (underlying_type
->type
, MONO_TYPE_I
, MONO_TYPE_U
)))
3795 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
3797 if (!verify_type_compatibility_full (ctx
, type
, candidate
, TRUE
))
3798 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx
->ip_offset
));
3803 set_stack_value (ctx
, stack_push (ctx
), type
, FALSE
);
3808 * FIXME handle arrays that are not 0-indexed
3811 do_stelem (VerifyContext
*ctx
, int opcode
, int token
)
3813 ILStackDesc
*index
, *array
, *value
;
3815 if (!check_underflow (ctx
, 3))
3818 if (opcode
== CEE_STELEM
) {
3819 if (!(type
= verifier_load_type (ctx
, token
, "stelem.any"))) {
3820 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token
, ctx
->ip_offset
));
3824 type
= mono_type_from_opcode (opcode
);
3827 value
= stack_pop (ctx
);
3828 index
= stack_pop (ctx
);
3829 array
= stack_pop (ctx
);
3831 if (stack_slot_get_type (index
) != TYPE_I4
&& stack_slot_get_type (index
) != TYPE_NATIVE_INT
)
3832 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
));
3834 if (!stack_slot_is_null_literal (array
)) {
3835 if (stack_slot_get_type (array
) != TYPE_COMPLEX
|| array
->type
->type
!= MONO_TYPE_SZARRAY
) {
3836 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
));
3838 if (opcode
== CEE_STELEM_REF
) {
3839 if (array
->type
->data
.klass
->valuetype
)
3840 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
3841 } else if (!verify_type_compatibility_full (ctx
, &array
->type
->data
.klass
->byval_arg
, type
, TRUE
)) {
3842 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
3846 if (opcode
== CEE_STELEM_REF
) {
3847 if (!stack_slot_is_boxed_value (value
) && mono_class_from_mono_type (value
->type
)->valuetype
)
3848 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx
->ip_offset
));
3849 } else if (opcode
!= CEE_STELEM_REF
) {
3850 if (!verify_stack_type_compatibility (ctx
, type
, value
))
3851 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx
->ip_offset
));
3853 if (stack_slot_is_boxed_value (value
) && !MONO_TYPE_IS_REFERENCE (value
->type
) && !MONO_TYPE_IS_REFERENCE (type
))
3854 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
));
3860 do_throw (VerifyContext
*ctx
)
3862 ILStackDesc
*exception
;
3863 if (!check_underflow (ctx
, 1))
3865 exception
= stack_pop (ctx
);
3867 if (!stack_slot_is_null_literal (exception
) && !(stack_slot_get_type (exception
) == TYPE_COMPLEX
&& !mono_class_from_mono_type (exception
->type
)->valuetype
))
3868 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx
->ip_offset
));
3870 if (mono_type_is_generic_argument (exception
->type
) && !stack_slot_is_boxed_value (exception
)) {
3871 char *name
= mono_type_full_name (exception
->type
);
3872 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
));
3875 /*The stack is left empty after a throw*/
3881 do_endfilter (VerifyContext
*ctx
)
3883 MonoExceptionClause
*clause
;
3885 if (IS_STRICT_MODE (ctx
)) {
3886 if (ctx
->eval
.size
!= 1)
3887 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx
->ip_offset
));
3889 if (ctx
->eval
.size
>= 1 && stack_slot_get_type (stack_pop (ctx
)) != TYPE_I4
)
3890 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx
->ip_offset
));
3893 if ((clause
= is_correct_endfilter (ctx
, ctx
->ip_offset
))) {
3894 if (IS_STRICT_MODE (ctx
)) {
3895 if (ctx
->ip_offset
!= clause
->handler_offset
- 2)
3896 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
3898 if ((ctx
->ip_offset
!= clause
->handler_offset
- 2) && !MONO_OFFSET_IN_HANDLER (clause
, ctx
->ip_offset
))
3899 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx
->ip_offset
));
3902 if (IS_STRICT_MODE (ctx
) && !is_unverifiable_endfilter (ctx
, ctx
->ip_offset
))
3903 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
3905 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx
->ip_offset
));
3912 do_leave (VerifyContext
*ctx
, int delta
)
3914 int target
= ((gint32
)ctx
->ip_offset
) + delta
;
3915 if (target
>= ctx
->code_size
|| target
< 0)
3916 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target out of code at 0x%04x", ctx
->ip_offset
));
3918 if (!is_correct_leave (ctx
->header
, ctx
->ip_offset
, target
))
3919 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx
->ip_offset
));
3926 * Verify br and br.s opcodes.
3929 do_static_branch (VerifyContext
*ctx
, int delta
)
3931 int target
= ctx
->ip_offset
+ delta
;
3932 if (target
< 0 || target
>= ctx
->code_size
) {
3933 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("branch target out of code at 0x%04x", ctx
->ip_offset
));
3937 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3939 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3942 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx
->ip_offset
));
3946 ctx
->target
= target
;
3950 do_switch (VerifyContext
*ctx
, int count
, const unsigned char *data
)
3952 int i
, base
= ctx
->ip_offset
+ 5 + count
* 4;
3955 if (!check_underflow (ctx
, 1))
3958 value
= stack_pop (ctx
);
3960 if (stack_slot_get_type (value
) != TYPE_I4
&& stack_slot_get_type (value
) != TYPE_NATIVE_INT
)
3961 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx
->ip_offset
));
3963 for (i
= 0; i
< count
; ++i
) {
3964 int target
= base
+ read32 (data
+ i
* 4);
3966 if (target
< 0 || target
>= ctx
->code_size
) {
3967 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x out of code at 0x%04x", i
, ctx
->ip_offset
));
3971 switch (is_valid_branch_instruction (ctx
->header
, ctx
->ip_offset
, target
)) {
3973 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
3976 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i
, ctx
->ip_offset
));
3979 merge_stacks (ctx
, &ctx
->eval
, &ctx
->code
[target
], FALSE
, TRUE
);
3984 do_load_function_ptr (VerifyContext
*ctx
, guint32 token
, gboolean
virtual)
3989 if (virtual && !check_underflow (ctx
, 1))
3992 if (!virtual && !check_overflow (ctx
))
3995 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
3996 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4000 if (!(method
= verifier_load_method (ctx
, token
, virtual ? "ldvirtfrn" : "ldftn")))
4003 if (mono_method_is_constructor (method
))
4004 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx
->ip_offset
));
4007 ILStackDesc
*top
= stack_pop (ctx
);
4009 if (stack_slot_get_type (top
) != TYPE_COMPLEX
|| top
->type
->type
== MONO_TYPE_VALUETYPE
)
4010 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx
->ip_offset
));
4012 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
)
4013 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx
->ip_offset
));
4015 if (!verify_stack_type_compatibility (ctx
, &method
->klass
->byval_arg
, top
))
4016 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx
->ip_offset
));
4019 if (!mono_method_can_access_method_full (ctx
->method
, method
, NULL
))
4020 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
);
4022 top
= stack_push_val(ctx
, TYPE_PTR
, mono_type_create_fnptr_from_mono_method (ctx
, method
));
4023 top
->method
= method
;
4027 do_sizeof (VerifyContext
*ctx
, int token
)
4031 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token
) || !token_bounds_check (ctx
->image
, token
)) {
4032 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid type token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4036 if (!(type
= verifier_load_type (ctx
, token
, "sizeof")))
4039 if (type
->byref
&& type
->type
!= MONO_TYPE_TYPEDBYREF
) {
4040 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx
->ip_offset
));
4044 if (type
->type
== MONO_TYPE_VOID
) {
4045 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx
->ip_offset
));
4049 if (check_overflow (ctx
))
4050 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.uint32_class
->byval_arg
, FALSE
);
4053 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4055 do_localloc (VerifyContext
*ctx
)
4059 if (ctx
->eval
.size
!= 1) {
4060 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4064 if (in_any_exception_block (ctx
->header
, ctx
->ip_offset
)) {
4065 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx
->ip_offset
));
4069 /*TODO verify top type*/
4070 top
= stack_pop (ctx
);
4072 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.int_class
->byval_arg
, FALSE
);
4073 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx
->ip_offset
));
4077 do_ldstr (VerifyContext
*ctx
, guint32 token
)
4079 GSList
*error
= NULL
;
4080 if (mono_metadata_token_code (token
) != MONO_TOKEN_STRING
) {
4081 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string token %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4085 if (!ctx
->image
->dynamic
&& !mono_verifier_verify_string_signature (ctx
->image
, mono_metadata_token_index (token
), &error
)) {
4087 ctx
->list
= g_slist_concat (ctx
->list
, error
);
4088 ADD_VERIFY_ERROR2 (ctx
, g_strdup_printf ("Invalid string index %x at 0x%04x", token
, ctx
->ip_offset
), MONO_EXCEPTION_BAD_IMAGE
);
4092 if (check_overflow (ctx
))
4093 stack_push_val (ctx
, TYPE_COMPLEX
, &mono_defaults
.string_class
->byval_arg
);
4097 do_refanyval (VerifyContext
*ctx
, int token
)
4101 if (!check_underflow (ctx
, 1))
4104 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4107 top
= stack_pop (ctx
);
4109 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4110 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
));
4112 set_stack_value (ctx
, stack_push (ctx
), type
, TRUE
);
4116 do_refanytype (VerifyContext
*ctx
)
4120 if (!check_underflow (ctx
, 1))
4123 top
= stack_pop (ctx
);
4125 if (top
->stype
!= TYPE_PTR
|| top
->type
->type
!= MONO_TYPE_TYPEDBYREF
)
4126 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
));
4128 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typehandle_class
->byval_arg
, FALSE
);
4133 do_mkrefany (VerifyContext
*ctx
, int token
)
4137 if (!check_underflow (ctx
, 1))
4140 if (!(type
= get_boxable_mono_type (ctx
, token
, "refanyval")))
4143 top
= stack_pop (ctx
);
4145 if (stack_slot_is_managed_mutability_pointer (top
))
4146 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx
->ip_offset
));
4148 if (!stack_slot_is_managed_pointer (top
)) {
4149 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
));
4151 MonoType
*stack_type
= mono_type_get_type_byval (top
->type
);
4152 if (MONO_TYPE_IS_REFERENCE (type
) && !mono_metadata_type_equal (type
, stack_type
))
4153 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4155 if (!MONO_TYPE_IS_REFERENCE (type
) && !verify_type_compatibility_full (ctx
, type
, stack_type
, TRUE
))
4156 CODE_NOT_VERIFIABLE (ctx
, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx
->ip_offset
));
4159 set_stack_value (ctx
, stack_push (ctx
), &mono_defaults
.typed_reference_class
->byval_arg
, FALSE
);
4163 do_ckfinite (VerifyContext
*ctx
)
4166 if (!check_underflow (ctx
, 1))
4169 top
= stack_pop (ctx
);
4171 if (stack_slot_get_underlying_type (top
) != TYPE_R8
)
4172 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
));
4173 stack_push_stack_val (ctx
, top
);
4177 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4179 * @from holds new values for a given control path
4180 * @to holds the current values of a given control path
4182 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4185 merge_stacks (VerifyContext
*ctx
, ILCodeDesc
*from
, ILCodeDesc
*to
, gboolean start
, gboolean external
)
4189 stack_init (ctx
, to
);
4192 if (to
->flags
== IL_CODE_FLAG_NOT_PROCESSED
)
4195 stack_copy (&ctx
->eval
, to
);
4197 } else if (!(to
->flags
& IL_CODE_STACK_MERGED
)) {
4198 stack_copy (to
, &ctx
->eval
);
4201 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from
->size
, to
->size
); );
4203 if (from
->size
!= to
->size
) {
4204 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from
->size
, to
->size
, ctx
->ip_offset
); );
4205 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
));
4209 //FIXME we need to preserve CMMP attributes
4210 //FIXME we must take null literals into consideration.
4211 for (i
= 0; i
< from
->size
; ++i
) {
4212 ILStackDesc
*new_slot
= from
->stack
+ i
;
4213 ILStackDesc
*old_slot
= to
->stack
+ i
;
4214 MonoType
*new_type
= mono_type_from_stack_slot (new_slot
);
4215 MonoType
*old_type
= mono_type_from_stack_slot (old_slot
);
4216 MonoClass
*old_class
= mono_class_from_mono_type (old_type
);
4217 MonoClass
*new_class
= mono_class_from_mono_type (new_type
);
4218 MonoClass
*match_class
= NULL
;
4220 // S := T then U = S (new value is compatible with current value, keep current)
4221 if (verify_stack_type_compatibility (ctx
, old_type
, new_slot
)) {
4222 copy_stack_value (new_slot
, old_slot
);
4226 // T := S then U = T (old value is compatible with current value, use new)
4227 if (verify_stack_type_compatibility (ctx
, new_type
, old_slot
)) {
4228 copy_stack_value (old_slot
, new_slot
);
4232 if (mono_type_is_generic_argument (old_type
) || mono_type_is_generic_argument (new_type
)) {
4233 char *old_name
= stack_slot_full_name (old_slot
);
4234 char *new_name
= stack_slot_full_name (new_slot
);
4235 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
));
4241 //both are reference types, use closest common super type
4242 if (!mono_class_from_mono_type (old_type
)->valuetype
4243 && !mono_class_from_mono_type (new_type
)->valuetype
4244 && !stack_slot_is_managed_pointer (old_slot
)
4245 && !stack_slot_is_managed_pointer (new_slot
)) {
4247 for (j
= MIN (old_class
->idepth
, new_class
->idepth
) - 1; j
> 0; --j
) {
4248 if (mono_metadata_type_equal (&old_class
->supertypes
[j
]->byval_arg
, &new_class
->supertypes
[j
]->byval_arg
)) {
4249 match_class
= old_class
->supertypes
[j
];
4254 mono_class_setup_interfaces (old_class
, &error
);
4255 if (!mono_error_ok (&error
)) {
4256 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
));
4257 mono_error_cleanup (&error
);
4260 for (j
= 0; j
< old_class
->interface_count
; ++j
) {
4261 for (k
= 0; k
< new_class
->interface_count
; ++k
) {
4262 if (mono_metadata_type_equal (&old_class
->interfaces
[j
]->byval_arg
, &new_class
->interfaces
[k
]->byval_arg
)) {
4263 match_class
= old_class
->interfaces
[j
];
4269 //No decent super type found, use object
4270 match_class
= mono_defaults
.object_class
;
4272 } 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
)) {
4273 match_class
= mono_defaults
.object_class
;
4278 char *old_name
= stack_slot_full_name (old_slot
);
4279 char *new_name
= stack_slot_full_name (new_slot
);
4280 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
));
4284 set_stack_value (ctx
, old_slot
, &new_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4288 g_assert (match_class
);
4289 set_stack_value (ctx
, old_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4290 set_stack_value (ctx
, new_slot
, &match_class
->byval_arg
, stack_slot_is_managed_pointer (old_slot
));
4296 to
->flags
|= IL_CODE_FLAG_WAS_TARGET
;
4297 to
->flags
|= IL_CODE_STACK_MERGED
;
4300 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4301 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4304 * is_clause_in_range :
4306 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4309 is_clause_in_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4311 if (clause
->try_offset
>= start
&& clause
->try_offset
< end
)
4313 if (HANDLER_START (clause
) >= start
&& HANDLER_START (clause
) < end
)
4319 * is_clause_inside_range :
4321 * Returns TRUE if @clause lies completely inside the @start - @end range.
4324 is_clause_inside_range (MonoExceptionClause
*clause
, guint32 start
, guint32 end
)
4326 if (clause
->try_offset
< start
|| (clause
->try_offset
+ clause
->try_len
) > end
)
4328 if (HANDLER_START (clause
) < start
|| (clause
->handler_offset
+ clause
->handler_len
) > end
)
4334 * is_clause_nested :
4336 * Returns TRUE if @nested is nested in @clause.
4339 is_clause_nested (MonoExceptionClause
*clause
, MonoExceptionClause
*nested
)
4341 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (nested
, clause
->data
.filter_offset
, clause
->handler_offset
))
4343 return is_clause_inside_range (nested
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4344 is_clause_inside_range (nested
, clause
->handler_offset
, clause
->handler_offset
+ clause
->handler_len
);
4347 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4348 * the each pair of exception must have the following properties:
4349 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4350 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4351 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4354 verify_clause_relationship (VerifyContext
*ctx
, MonoExceptionClause
*clause
, MonoExceptionClause
*to_test
)
4356 /*clause is nested*/
4357 if (to_test
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& is_clause_inside_range (clause
, to_test
->data
.filter_offset
, to_test
->handler_offset
)) {
4358 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clause inside filter"));
4362 /*wrong nesting order.*/
4363 if (is_clause_nested (clause
, to_test
)) {
4364 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4368 /*mutual protection*/
4369 if (clause
->try_offset
== to_test
->try_offset
&& clause
->try_len
== to_test
->try_len
) {
4370 /*handlers are not disjoint*/
4371 if (is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) {
4372 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception handlers overlap"));
4375 /* handlers are not catch or filter */
4376 if (!IS_CATCH_OR_FILTER (clause
) || !IS_CATCH_OR_FILTER (to_test
)) {
4377 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4384 /*not completelly disjoint*/
4385 if ((is_clause_in_range (to_test
, clause
->try_offset
, clause
->try_offset
+ clause
->try_len
) ||
4386 is_clause_in_range (to_test
, HANDLER_START (clause
), clause
->handler_offset
+ clause
->handler_len
)) && !is_clause_nested (to_test
, clause
))
4387 ADD_VERIFY_ERROR (ctx
, g_strdup_printf ("Exception clauses overlap"));
4390 #define code_bounds_check(size) \
4391 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4392 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4397 mono_opcode_is_prefix (int op
)
4400 case MONO_CEE_UNALIGNED_
:
4401 case MONO_CEE_VOLATILE_
:
4402 case MONO_CEE_TAIL_
:
4403 case MONO_CEE_CONSTRAINED_
:
4404 case MONO_CEE_READONLY_
:
4411 * FIXME: need to distinguish between valid and verifiable.
4412 * Need to keep track of types on the stack.
4413 * Verify types for opcodes.
4416 mono_method_verify (MonoMethod
*method
, int level
)
4419 const unsigned char *ip
, *code_start
;
4420 const unsigned char *end
;
4421 MonoSimpleBasicBlock
*bb
= NULL
, *original_bb
= NULL
;
4423 int i
, n
, need_merge
= 0, start
= 0;
4424 guint token
, ip_offset
= 0, prefix
= 0;
4425 MonoGenericContext
*generic_context
= NULL
;
4429 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method
->klass
->name_space
, method
->klass
->name
, method
->name
); );
4431 init_verifier_stats ();
4433 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4434 (method
->flags
& (METHOD_ATTRIBUTE_PINVOKE_IMPL
| METHOD_ATTRIBUTE_ABSTRACT
))) {
4438 memset (&ctx
, 0, sizeof (VerifyContext
));
4440 //FIXME use mono_method_get_signature_full
4441 ctx
.signature
= mono_method_signature (method
);
4442 if (!ctx
.signature
) {
4443 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method signature"));
4445 finish_collect_stats ();
4448 ctx
.header
= mono_method_get_header (method
);
4450 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Could not decode method header"));
4451 finish_collect_stats ();
4454 ctx
.method
= method
;
4455 code_start
= ip
= ctx
.header
->code
;
4456 end
= ip
+ ctx
.header
->code_size
;
4457 ctx
.image
= image
= method
->klass
->image
;
4460 ctx
.max_args
= ctx
.signature
->param_count
+ ctx
.signature
->hasthis
;
4461 ctx
.max_stack
= ctx
.header
->max_stack
;
4462 ctx
.verifiable
= ctx
.valid
= 1;
4465 ctx
.code
= g_new (ILCodeDesc
, ctx
.header
->code_size
);
4466 ctx
.code_size
= ctx
.header
->code_size
;
4467 MEM_ALLOC (sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4469 memset(ctx
.code
, 0, sizeof (ILCodeDesc
) * ctx
.header
->code_size
);
4471 ctx
.num_locals
= ctx
.header
->num_locals
;
4472 ctx
.locals
= g_memdup (ctx
.header
->locals
, sizeof (MonoType
*) * ctx
.header
->num_locals
);
4473 MEM_ALLOC (sizeof (MonoType
*) * ctx
.header
->num_locals
);
4475 if (ctx
.num_locals
> 0 && !ctx
.header
->init_locals
)
4476 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Method with locals variable but without init locals set"));
4478 ctx
.params
= g_new (MonoType
*, ctx
.max_args
);
4479 MEM_ALLOC (sizeof (MonoType
*) * ctx
.max_args
);
4481 if (ctx
.signature
->hasthis
)
4482 ctx
.params
[0] = method
->klass
->valuetype
? &method
->klass
->this_arg
: &method
->klass
->byval_arg
;
4483 memcpy (ctx
.params
+ ctx
.signature
->hasthis
, ctx
.signature
->params
, sizeof (MonoType
*) * ctx
.signature
->param_count
);
4485 if (ctx
.signature
->is_inflated
)
4486 ctx
.generic_context
= generic_context
= mono_method_get_context (method
);
4488 if (!generic_context
&& (method
->klass
->generic_container
|| method
->is_generic
)) {
4489 if (method
->is_generic
)
4490 ctx
.generic_context
= generic_context
= &(mono_method_get_generic_container (method
)->context
);
4492 ctx
.generic_context
= generic_context
= &method
->klass
->generic_container
->context
;
4495 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4496 MonoType
*uninflated
= ctx
.locals
[i
];
4497 ctx
.locals
[i
] = mono_class_inflate_generic_type_checked (ctx
.locals
[i
], ctx
.generic_context
, &error
);
4498 if (!mono_error_ok (&error
)) {
4499 char *name
= mono_type_full_name (ctx
.locals
[i
] ? ctx
.locals
[i
] : uninflated
);
4500 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %d of type %s", i
, name
));
4502 mono_error_cleanup (&error
);
4503 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4509 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4510 MonoType
*uninflated
= ctx
.params
[i
];
4511 ctx
.params
[i
] = mono_class_inflate_generic_type_checked (ctx
.params
[i
], ctx
.generic_context
, &error
);
4512 if (!mono_error_ok (&error
)) {
4513 char *name
= mono_type_full_name (ctx
.params
[i
] ? ctx
.params
[i
] : uninflated
);
4514 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %d of type %s", i
, name
));
4516 mono_error_cleanup (&error
);
4517 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4522 stack_init (&ctx
, &ctx
.eval
);
4524 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
4525 if (!mono_type_is_valid_in_context (&ctx
, ctx
.locals
[i
]))
4527 if (get_stack_type (ctx
.locals
[i
]) == TYPE_INV
) {
4528 char *name
= mono_type_full_name (ctx
.locals
[i
]);
4529 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid local %i of type %s", i
, name
));
4536 for (i
= 0; i
< ctx
.max_args
; ++i
) {
4537 if (!mono_type_is_valid_in_context (&ctx
, ctx
.params
[i
]))
4540 if (get_stack_type (ctx
.params
[i
]) == TYPE_INV
) {
4541 char *name
= mono_type_full_name (ctx
.params
[i
]);
4542 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid parameter %i of type %s", i
, name
));
4551 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
4552 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
4553 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
); );
4555 if (clause
->try_offset
> ctx
.code_size
|| ADD_IS_GREATER_OR_OVF (clause
->try_offset
, clause
->try_len
, ctx
.code_size
))
4556 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause out of bounds at 0x%04x", clause
->try_offset
));
4558 if (clause
->try_len
<= 0)
4559 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause
->try_offset
));
4561 if (clause
->handler_offset
> ctx
.code_size
|| ADD_IS_GREATER_OR_OVF (clause
->handler_offset
, clause
->handler_len
, ctx
.code_size
))
4562 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause
->try_offset
));
4564 if (clause
->handler_len
<= 0)
4565 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause
->try_offset
));
4567 if (clause
->try_offset
< clause
->handler_offset
&& ADD_IS_GREATER_OR_OVF (clause
->try_offset
, clause
->try_len
, HANDLER_START (clause
)))
4568 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause
->try_offset
, clause
->handler_offset
));
4570 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
4571 if (clause
->data
.filter_offset
> ctx
.code_size
)
4572 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause
->try_offset
));
4574 if (clause
->data
.filter_offset
>= clause
->handler_offset
)
4575 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause
->data
.filter_offset
));
4578 for (n
= i
+ 1; n
< ctx
.header
->num_clauses
&& ctx
.valid
; ++n
)
4579 verify_clause_relationship (&ctx
, clause
, ctx
.header
->clauses
+ n
);
4584 ctx
.code
[clause
->try_offset
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4585 if (clause
->try_offset
+ clause
->try_len
< ctx
.code_size
)
4586 ctx
.code
[clause
->try_offset
+ clause
->try_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4587 if (clause
->handler_offset
+ clause
->handler_len
< ctx
.code_size
)
4588 ctx
.code
[clause
->handler_offset
+ clause
->handler_len
].flags
|= IL_CODE_FLAG_WAS_TARGET
;
4590 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_NONE
) {
4591 if (!clause
->data
.catch_class
) {
4592 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Catch clause %d with invalid type", i
));
4596 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, clause
->data
.catch_class
);
4598 else if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
) {
4599 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->data
.filter_offset
, mono_defaults
.exception_class
);
4600 init_stack_with_value_at_exception_boundary (&ctx
, ctx
.code
+ clause
->handler_offset
, mono_defaults
.exception_class
);
4604 original_bb
= bb
= mono_basic_block_split (method
, &error
);
4605 if (!mono_error_ok (&error
)) {
4606 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error
)));
4607 mono_error_cleanup (&error
);
4612 while (ip
< end
&& ctx
.valid
) {
4613 ip_offset
= ip
- code_start
;
4615 const unsigned char *ip_copy
= ip
;
4618 if (ip_offset
> bb
->end
) {
4619 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb
->end
, ip_offset
));
4623 if (ip_offset
== bb
->end
)
4626 size
= mono_opcode_value_and_size (&ip_copy
, end
, &op
);
4628 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ip_offset
));
4632 if (ADD_IS_GREATER_OR_OVF (ip_offset
, size
, bb
->end
)) {
4633 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset
));
4637 /*Last Instruction*/
4638 if (ip_offset
+ size
== bb
->end
&& mono_opcode_is_prefix (op
)) {
4639 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
));
4644 /*FIXME remove this once we move all bad branch checking code to use BB only*/
4645 ctx
.code
[ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
4651 ctx
.ip_offset
= ip_offset
= ip
- code_start
;
4653 /*We need to check against fallthrou in and out of protected blocks.
4654 * For fallout we check the once a protected block ends, if the start flag is not set.
4655 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4656 * TODO convert these checks to be done using flags and not this loop
4658 for (i
= 0; i
< ctx
.header
->num_clauses
&& ctx
.valid
; ++i
) {
4659 MonoExceptionClause
*clause
= ctx
.header
->clauses
+ i
;
4661 if ((clause
->try_offset
+ clause
->try_len
== ip_offset
) && start
== 0) {
4662 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset
));
4666 if ((clause
->handler_offset
+ clause
->handler_len
== ip_offset
) && start
== 0) {
4667 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
)
4668 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
4670 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset
));
4674 if (clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
&& clause
->handler_offset
== ip_offset
&& start
== 0) {
4675 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset
));
4679 if (clause
->handler_offset
== ip_offset
&& start
== 0) {
4680 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset
));
4684 if (clause
->try_offset
== ip_offset
&& ctx
.eval
.size
> 0 && start
== 0) {
4685 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset
));
4694 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx
.target
); );
4695 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ctx
.target
], FALSE
, TRUE
);
4698 merge_stacks (&ctx
, &ctx
.eval
, &ctx
.code
[ip_offset
], start
, FALSE
);
4701 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4702 #ifdef MONO_VERIFIER_DEBUG
4705 discode
= mono_disasm_code_one (NULL
, method
, ip
, NULL
);
4706 discode
[strlen (discode
) - 1] = 0; /* no \n */
4707 g_print ("[%d] %-29s (%d)\n", ip_offset
, discode
, ctx
.eval
.size
);
4710 dump_stack_state (&ctx
.code
[ip_offset
]);
4711 dump_stack_state (&ctx
.eval
);
4724 push_arg (&ctx
, *ip
- CEE_LDARG_0
, FALSE
);
4730 code_bounds_check (2);
4731 push_arg (&ctx
, ip
[1], *ip
== CEE_LDARGA_S
);
4735 case CEE_ADD_OVF_UN
:
4736 do_binop (&ctx
, *ip
, add_ovf_un_table
);
4740 case CEE_SUB_OVF_UN
:
4741 do_binop (&ctx
, *ip
, sub_ovf_un_table
);
4748 case CEE_MUL_OVF_UN
:
4749 do_binop (&ctx
, *ip
, bin_ovf_table
);
4754 do_binop (&ctx
, *ip
, add_table
);
4759 do_binop (&ctx
, *ip
, sub_table
);
4766 do_binop (&ctx
, *ip
, bin_op_table
);
4775 do_binop (&ctx
, *ip
, int_bin_op_table
);
4782 do_binop (&ctx
, *ip
, shift_op_table
);
4787 if (!check_underflow (&ctx
, 1))
4789 stack_pop_safe (&ctx
);
4803 /*TODO support definite assignment verification? */
4804 push_local (&ctx
, *ip
- CEE_LDLOC_0
, FALSE
);
4812 store_local (&ctx
, *ip
- CEE_STLOC_0
);
4817 code_bounds_check (2);
4818 store_local (&ctx
, ip
[1]);
4823 code_bounds_check (2);
4824 store_arg (&ctx
, ip
[1]);
4838 if (check_overflow (&ctx
))
4839 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
4844 code_bounds_check (2);
4845 if (check_overflow (&ctx
))
4846 stack_push_val (&ctx
, TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
4851 code_bounds_check (5);
4852 if (check_overflow (&ctx
))
4853 stack_push_val (&ctx
,TYPE_I4
, &mono_defaults
.int32_class
->byval_arg
);
4858 code_bounds_check (9);
4859 if (check_overflow (&ctx
))
4860 stack_push_val (&ctx
,TYPE_I8
, &mono_defaults
.int64_class
->byval_arg
);
4865 code_bounds_check (5);
4866 if (check_overflow (&ctx
))
4867 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
4872 code_bounds_check (9);
4873 if (check_overflow (&ctx
))
4874 stack_push_val (&ctx
, TYPE_R8
, &mono_defaults
.double_class
->byval_arg
);
4879 if (check_overflow (&ctx
))
4880 stack_push_val (&ctx
, TYPE_COMPLEX
| NULL_LITERAL_MASK
, &mono_defaults
.object_class
->byval_arg
);
4886 code_bounds_check (2);
4887 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_eq_op
);
4900 code_bounds_check (2);
4901 do_branch_op (&ctx
, (signed char)ip
[1] + 2, cmp_br_op
);
4908 code_bounds_check (5);
4909 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_eq_op
);
4922 code_bounds_check (5);
4923 do_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5, cmp_br_op
);
4930 code_bounds_check (2);
4931 push_local (&ctx
, ip
[1], *ip
== CEE_LDLOCA_S
);
4936 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
4942 if (!check_underflow (&ctx
, 1))
4944 if (!check_overflow (&ctx
))
4946 top
= stack_push (&ctx
);
4947 copy_stack_value (top
, stack_peek (&ctx
, 1));
4953 code_bounds_check (5);
4955 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset
));
4956 token
= read32 (ip
+ 1);
4957 if (in_any_block (ctx
.header
, ip_offset
))
4958 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset
));
4960 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx
.ip_offset
));
4962 * FIXME: check signature, retval, arguments etc.
4968 code_bounds_check (5);
4969 do_invoke_method (&ctx
, read32 (ip
+ 1), *ip
== CEE_CALLVIRT
);
4974 code_bounds_check (5);
4975 token
= read32 (ip
+ 1);
4977 * FIXME: check signature, retval, arguments etc.
4978 * FIXME: check requirements for tail call
4980 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx
.ip_offset
));
4984 code_bounds_check (2);
4985 do_static_branch (&ctx
, (signed char)ip
[1] + 2);
4993 code_bounds_check (2);
4994 do_boolean_branch_op (&ctx
, (signed char)ip
[1] + 2);
5000 code_bounds_check (5);
5001 do_static_branch (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5009 code_bounds_check (5);
5010 do_boolean_branch_op (&ctx
, (gint32
)read32 (ip
+ 1) + 5);
5017 code_bounds_check (5);
5018 entries
= read32 (ip
+ 1);
5020 if (entries
> 0xFFFFFFFFU
/ sizeof (guint32
))
5021 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries
, ctx
.ip_offset
));
5024 code_bounds_check (sizeof (guint32
) * entries
);
5026 do_switch (&ctx
, entries
, ip
);
5027 ip
+= sizeof (guint32
) * entries
;
5041 do_load_indirect (&ctx
, *ip
);
5053 do_store_indirect (&ctx
, *ip
);
5059 do_unary_math_op (&ctx
, *ip
);
5069 do_conversion (&ctx
, TYPE_I4
);
5075 do_conversion (&ctx
, TYPE_I8
);
5082 do_conversion (&ctx
, TYPE_R8
);
5088 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5093 code_bounds_check (5);
5094 do_cpobj (&ctx
, read32 (ip
+ 1));
5099 code_bounds_check (5);
5100 do_ldobj_value (&ctx
, read32 (ip
+ 1));
5105 code_bounds_check (5);
5106 do_ldstr (&ctx
, read32 (ip
+ 1));
5111 code_bounds_check (5);
5112 do_newobj (&ctx
, read32 (ip
+ 1));
5118 code_bounds_check (5);
5119 do_cast (&ctx
, read32 (ip
+ 1), *ip
== CEE_CASTCLASS
? "castclass" : "isinst");
5125 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
5130 code_bounds_check (5);
5131 do_unbox_value (&ctx
, read32 (ip
+ 1));
5143 code_bounds_check (5);
5144 do_push_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDFLDA
);
5150 code_bounds_check (5);
5151 do_push_static_field (&ctx
, read32 (ip
+ 1), *ip
== CEE_LDSFLDA
);
5156 code_bounds_check (5);
5157 do_store_field (&ctx
, read32 (ip
+ 1));
5162 code_bounds_check (5);
5163 do_store_static_field (&ctx
, read32 (ip
+ 1));
5168 code_bounds_check (5);
5169 do_stobj (&ctx
, read32 (ip
+ 1));
5173 case CEE_CONV_OVF_I1_UN
:
5174 case CEE_CONV_OVF_I2_UN
:
5175 case CEE_CONV_OVF_I4_UN
:
5176 case CEE_CONV_OVF_U1_UN
:
5177 case CEE_CONV_OVF_U2_UN
:
5178 case CEE_CONV_OVF_U4_UN
:
5179 do_conversion (&ctx
, TYPE_I4
);
5183 case CEE_CONV_OVF_I8_UN
:
5184 case CEE_CONV_OVF_U8_UN
:
5185 do_conversion (&ctx
, TYPE_I8
);
5189 case CEE_CONV_OVF_I_UN
:
5190 case CEE_CONV_OVF_U_UN
:
5191 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5196 code_bounds_check (5);
5197 do_box_value (&ctx
, read32 (ip
+ 1));
5202 code_bounds_check (5);
5203 do_newarr (&ctx
, read32 (ip
+ 1));
5213 code_bounds_check (5);
5214 do_ldelema (&ctx
, read32 (ip
+ 1));
5228 case CEE_LDELEM_REF
:
5229 do_ldelem (&ctx
, *ip
, 0);
5240 case CEE_STELEM_REF
:
5241 do_stelem (&ctx
, *ip
, 0);
5246 code_bounds_check (5);
5247 do_ldelem (&ctx
, *ip
, read32 (ip
+ 1));
5252 code_bounds_check (5);
5253 do_stelem (&ctx
, *ip
, read32 (ip
+ 1));
5258 code_bounds_check (5);
5259 do_unbox_any (&ctx
, read32 (ip
+ 1));
5263 case CEE_CONV_OVF_I1
:
5264 case CEE_CONV_OVF_U1
:
5265 case CEE_CONV_OVF_I2
:
5266 case CEE_CONV_OVF_U2
:
5267 case CEE_CONV_OVF_I4
:
5268 case CEE_CONV_OVF_U4
:
5269 do_conversion (&ctx
, TYPE_I4
);
5273 case CEE_CONV_OVF_I8
:
5274 case CEE_CONV_OVF_U8
:
5275 do_conversion (&ctx
, TYPE_I8
);
5279 case CEE_CONV_OVF_I
:
5280 case CEE_CONV_OVF_U
:
5281 do_conversion (&ctx
, TYPE_NATIVE_INT
);
5286 code_bounds_check (5);
5287 do_refanyval (&ctx
, read32 (ip
+ 1));
5297 code_bounds_check (5);
5298 do_mkrefany (&ctx
, read32 (ip
+ 1));
5303 code_bounds_check (5);
5304 do_load_token (&ctx
, read32 (ip
+ 1));
5308 case CEE_ENDFINALLY
:
5309 if (!is_correct_endfinally (ctx
.header
, ip_offset
))
5310 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx
.ip_offset
));
5317 code_bounds_check (5);
5318 do_leave (&ctx
, read32 (ip
+ 1) + 5);
5324 code_bounds_check (2);
5325 do_leave (&ctx
, (signed char)ip
[1] + 2);
5331 code_bounds_check (2);
5335 code_bounds_check (3);
5336 store_local (&ctx
, read16 (ip
+ 1));
5341 do_cmp_op (&ctx
, cmp_br_eq_op
, *ip
);
5349 do_cmp_op (&ctx
, cmp_br_op
, *ip
);
5354 code_bounds_check (3);
5355 store_arg (&ctx
, read16 (ip
+ 1) );
5361 check_overflow (&ctx
);
5362 if (ctx
.signature
->call_convention
!= MONO_CALL_VARARG
)
5363 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx
.ip_offset
));
5364 set_stack_value (&ctx
, stack_push (&ctx
), &mono_defaults
.argumenthandle_class
->byval_arg
, FALSE
);
5369 code_bounds_check (5);
5370 do_load_function_ptr (&ctx
, read32 (ip
+ 1), FALSE
);
5375 code_bounds_check (5);
5376 do_load_function_ptr (&ctx
, read32 (ip
+ 1), TRUE
);
5382 code_bounds_check (3);
5383 push_arg (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDARGA
);
5389 code_bounds_check (3);
5390 push_local (&ctx
, read16 (ip
+ 1), *ip
== CEE_LDLOCA
);
5403 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Use of the `unused' opcode"));
5407 do_endfilter (&ctx
);
5411 case CEE_UNALIGNED_
:
5412 code_bounds_check (2);
5413 prefix
|= PREFIX_UNALIGNED
;
5417 prefix
|= PREFIX_VOLATILE
;
5421 prefix
|= PREFIX_TAIL
;
5423 if (ip
< end
&& (*ip
!= CEE_CALL
&& *ip
!= CEE_CALLI
&& *ip
!= CEE_CALLVIRT
))
5424 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset
));
5428 code_bounds_check (5);
5429 do_initobj (&ctx
, read32 (ip
+ 1));
5433 case CEE_CONSTRAINED_
:
5434 code_bounds_check (5);
5435 ctx
.constrained_type
= get_boxable_mono_type (&ctx
, read32 (ip
+ 1), "constrained.");
5436 prefix
|= PREFIX_CONSTRAINED
;
5441 prefix
|= PREFIX_READONLY
;
5446 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5447 if (!check_underflow (&ctx
, 3))
5449 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5454 CLEAR_PREFIX (&ctx
, PREFIX_UNALIGNED
| PREFIX_VOLATILE
);
5455 if (!check_underflow (&ctx
, 3))
5457 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx
.ip_offset
));
5465 if (!is_correct_rethrow (ctx
.header
, ip_offset
))
5466 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx
.ip_offset
));
5473 code_bounds_check (5);
5474 do_sizeof (&ctx
, read32 (ip
+ 1));
5478 case CEE_REFANYTYPE
:
5479 do_refanytype (&ctx
);
5484 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip
, ctx
.ip_offset
));
5490 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip
, ctx
.ip_offset
));
5494 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5496 if (!ctx
.prefix_set
) //first prefix
5497 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5498 ctx
.prefix_set
|= prefix
;
5499 ctx
.has_flags
= TRUE
;
5503 ctx
.code
[ctx
.ip_offset
].flags
|= IL_CODE_FLAG_SEEN
;
5505 if (ctx
.prefix_set
& PREFIX_CONSTRAINED
)
5506 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx
.ip_offset
));
5507 if (ctx
.prefix_set
& PREFIX_READONLY
)
5508 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx
.ip_offset
));
5509 if (ctx
.prefix_set
& PREFIX_VOLATILE
)
5510 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx
.ip_offset
));
5511 if (ctx
.prefix_set
& PREFIX_UNALIGNED
)
5512 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx
.ip_offset
));
5513 ctx
.prefix_set
= prefix
= 0;
5514 ctx
.has_flags
= FALSE
;
5518 * if ip != end we overflowed: mark as error.
5520 if ((ip
!= end
|| !start
) && ctx
.verifiable
&& !ctx
.list
) {
5521 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset
));
5524 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5525 for (i
= 0; i
< ctx
.code_size
&& i
< ip_offset
; ++i
) {
5526 if (ctx
.code
[i
].flags
& IL_CODE_FLAG_WAS_TARGET
) {
5527 if (!(ctx
.code
[i
].flags
& IL_CODE_FLAG_SEEN
))
5528 ADD_VERIFY_ERROR (&ctx
, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i
));
5530 if (ctx
.code
[i
].flags
& IL_CODE_DELEGATE_SEQUENCE
)
5531 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i
));
5533 if ((ctx
.code
[i
].flags
& IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5534 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i
));
5536 if ((ctx
.code
[i
].flags
& IL_CODE_CALL_NONFINAL_VIRTUAL
) && ctx
.has_this_store
)
5537 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
));
5540 if (mono_method_is_constructor (ctx
.method
) && !ctx
.super_ctor_called
&& !ctx
.method
->klass
->valuetype
&& ctx
.method
->klass
!= mono_defaults
.object_class
) {
5541 char *method_name
= mono_method_full_name (ctx
.method
, TRUE
);
5542 char *type
= mono_type_get_full_name (ctx
.method
->klass
);
5543 if (ctx
.method
->klass
->parent
&& ctx
.method
->klass
->parent
->exception_type
!= MONO_EXCEPTION_NONE
)
5544 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
));
5546 CODE_NOT_VERIFIABLE (&ctx
, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name
, type
));
5547 g_free (method_name
);
5553 for (i
= 0; i
< ctx
.header
->code_size
; ++i
) {
5554 if (ctx
.code
[i
].stack
)
5555 g_free (ctx
.code
[i
].stack
);
5559 for (tmp
= ctx
.funptrs
; tmp
; tmp
= tmp
->next
)
5561 g_slist_free (ctx
.funptrs
);
5563 for (tmp
= ctx
.exception_types
; tmp
; tmp
= tmp
->next
)
5564 mono_metadata_free_type (tmp
->data
);
5565 g_slist_free (ctx
.exception_types
);
5567 for (i
= 0; i
< ctx
.num_locals
; ++i
) {
5569 mono_metadata_free_type (ctx
.locals
[i
]);
5571 for (i
= 0; i
< ctx
.max_args
; ++i
) {
5573 mono_metadata_free_type (ctx
.params
[i
]);
5577 g_free (ctx
.eval
.stack
);
5580 g_free (ctx
.locals
);
5581 g_free (ctx
.params
);
5582 mono_basic_block_free (original_bb
);
5583 mono_metadata_free_mh (ctx
.header
);
5585 finish_collect_stats ();
5590 mono_verify_corlib ()
5592 /* This is a public API function so cannot be removed */
5597 * Returns true if @method needs to be verified.
5601 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
5603 return mono_verifier_is_enabled_for_class (method
->klass
) && method
->wrapper_type
== MONO_WRAPPER_NONE
;
5607 * Returns true if @klass need to be verified.
5611 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
5613 return verify_all
|| (verifier_mode
> MONO_VERIFIER_MODE_OFF
&& !(klass
->image
->assembly
&& klass
->image
->assembly
->in_gac
) && klass
->image
!= mono_defaults
.corlib
);
5617 mono_verifier_is_enabled_for_image (MonoImage
*image
)
5619 return verify_all
|| verifier_mode
> MONO_VERIFIER_MODE_OFF
;
5623 mono_verifier_is_method_full_trust (MonoMethod
*method
)
5625 return mono_verifier_is_class_full_trust (method
->klass
);
5629 * Returns if @klass is under full trust or not.
5631 * TODO This code doesn't take CAS into account.
5633 * Under verify_all all user code must be verifiable if no security option was set
5637 mono_verifier_is_class_full_trust (MonoClass
*klass
)
5639 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5640 gboolean trusted_location
= (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR
) ?
5641 (klass
->image
->assembly
&& klass
->image
->assembly
->in_gac
) : mono_security_core_clr_is_platform_image (klass
->image
);
5643 if (verify_all
&& verifier_mode
== MONO_VERIFIER_MODE_OFF
)
5644 return trusted_location
|| klass
->image
== mono_defaults
.corlib
;
5645 return verifier_mode
< MONO_VERIFIER_MODE_VERIFIABLE
|| trusted_location
|| klass
->image
== mono_defaults
.corlib
;
5649 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
5651 return mono_method_verify (method
,
5652 (verifier_mode
!= MONO_VERIFIER_MODE_STRICT
? MONO_VERIFY_NON_STRICT
: 0)
5653 | (!mono_verifier_is_method_full_trust (method
) ? MONO_VERIFY_FAIL_FAST
: 0)
5654 | (skip_visibility
? MONO_VERIFY_SKIP_VISIBILITY
: 0));
5658 get_field_end (MonoClassField
*field
)
5661 int size
= mono_type_size (field
->type
, &align
);
5663 size
= 4; /*FIXME Is this a safe bet?*/
5664 return size
+ field
->offset
;
5668 verify_class_for_overlapping_reference_fields (MonoClass
*class)
5671 gpointer iter
= NULL
;
5672 MonoClassField
*field
;
5673 gboolean is_fulltrust
= mono_verifier_is_class_full_trust (class);
5674 /*We can't skip types with !has_references since this is calculated after we have run.*/
5675 if (!((class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
))
5679 /*We must check for stuff overlapping reference fields.
5680 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
5682 while ((field
= mono_class_get_fields (class, &iter
))) {
5683 int fieldEnd
= get_field_end (field
);
5684 gboolean is_valuetype
= !MONO_TYPE_IS_REFERENCE (field
->type
);
5687 if (mono_field_is_deleted (field
) || (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
5690 for (j
= i
; j
< class->field
.count
; ++j
) {
5691 MonoClassField
*other
= &class->fields
[j
];
5692 int otherEnd
= get_field_end (other
);
5693 if (mono_field_is_deleted (other
) || (is_valuetype
&& !MONO_TYPE_IS_REFERENCE (other
->type
)) || (other
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
5696 if (!is_valuetype
&& MONO_TYPE_IS_REFERENCE (other
->type
) && field
->offset
== other
->offset
&& is_fulltrust
)
5699 if ((otherEnd
> field
->offset
&& otherEnd
<= fieldEnd
) || (other
->offset
>= field
->offset
&& other
->offset
< fieldEnd
))
5707 field_hash (gconstpointer key
)
5709 const MonoClassField
*field
= key
;
5710 return g_str_hash (field
->name
) ^ mono_metadata_type_hash (field
->type
); /**/
5714 field_equals (gconstpointer _a
, gconstpointer _b
)
5716 const MonoClassField
*a
= _a
;
5717 const MonoClassField
*b
= _b
;
5718 return !strcmp (a
->name
, b
->name
) && mono_metadata_type_equal (a
->type
, b
->type
);
5723 verify_class_fields (MonoClass
*class)
5725 gpointer iter
= NULL
;
5726 MonoClassField
*field
;
5727 MonoGenericContext
*context
= mono_class_get_context (class);
5728 GHashTable
*unique_fields
= g_hash_table_new_full (&field_hash
, &field_equals
, NULL
, NULL
);
5729 if (class->generic_container
)
5730 context
= &class->generic_container
->context
;
5732 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
5733 if (!mono_type_is_valid_type_in_context (field
->type
, context
)) {
5734 g_hash_table_destroy (unique_fields
);
5737 if (g_hash_table_lookup (unique_fields
, field
)) {
5738 g_hash_table_destroy (unique_fields
);
5741 g_hash_table_insert (unique_fields
, field
, field
);
5743 g_hash_table_destroy (unique_fields
);
5748 verify_interfaces (MonoClass
*class)
5751 for (i
= 0; i
< class->interface_count
; ++i
) {
5752 MonoClass
*iface
= class->interfaces
[i
];
5753 if (!(iface
->flags
& TYPE_ATTRIBUTE_INTERFACE
))
5760 verify_valuetype_layout_with_target (MonoClass
*class, MonoClass
*target_class
)
5763 gpointer iter
= NULL
;
5764 MonoClassField
*field
;
5765 MonoClass
*field_class
;
5767 if (!class->valuetype
)
5770 type
= class->byval_arg
.type
;
5771 /*primitive type fields are not properly decoded*/
5772 if ((type
>= MONO_TYPE_BOOLEAN
&& type
<= MONO_TYPE_R8
) || (type
>= MONO_TYPE_I
&& type
<= MONO_TYPE_U
))
5775 while ((field
= mono_class_get_fields (class, &iter
)) != NULL
) {
5779 if (field
->type
->attrs
& (FIELD_ATTRIBUTE_STATIC
| FIELD_ATTRIBUTE_HAS_FIELD_RVA
))
5782 field_class
= mono_class_get_generic_type_definition (mono_class_from_mono_type (field
->type
));
5784 if (field_class
== target_class
|| class == field_class
|| !verify_valuetype_layout_with_target (field_class
, target_class
))
5792 verify_valuetype_layout (MonoClass
*class)
5795 res
= verify_valuetype_layout_with_target (class, class);
5800 verify_generic_parameters (MonoClass
*class)
5803 MonoGenericContainer
*gc
= class->generic_container
;
5805 for (i
= 0; i
< gc
->type_argc
; ++i
) {
5806 MonoGenericParamInfo
*param_info
= mono_generic_container_get_param_info (gc
, i
);
5807 MonoClass
**constraints
;
5809 if (!param_info
->constraints
)
5812 for (constraints
= param_info
->constraints
; *constraints
; ++constraints
) {
5813 MonoClass
*ctr
= *constraints
;
5815 if (!mono_type_is_valid_type_in_context (&ctr
->byval_arg
, &gc
->context
))
5823 * Check if the class is verifiable.
5825 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5826 * field and invalid generic instantiation are fatal errors.
5828 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5832 mono_verifier_verify_class (MonoClass
*class)
5834 /*Neither <Module>, object or ifaces have parent.*/
5835 if (!class->parent
&&
5836 class != mono_defaults
.object_class
&&
5837 !MONO_CLASS_IS_INTERFACE (class) &&
5838 (!class->image
->dynamic
&& class->type_token
!= 0x2000001)) /*<Module> is the first type in the assembly*/
5840 if (class->parent
&& MONO_CLASS_IS_INTERFACE (class->parent
))
5842 if (class->generic_container
&& (class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
)
5844 if (class->generic_container
&& !verify_generic_parameters (class))
5846 if (!verify_class_for_overlapping_reference_fields (class))
5848 if (class->generic_class
&& !mono_class_is_valid_generic_instantiation (NULL
, class))
5850 if (class->generic_class
== NULL
&& !verify_class_fields (class))
5852 if (class->valuetype
&& !verify_valuetype_layout (class))
5854 if (!verify_interfaces (class))
5861 mono_verifier_verify_class (MonoClass
*class)
5863 /* The verifier was disabled at compile time */
5868 mono_method_verify_with_current_settings (MonoMethod
*method
, gboolean skip_visibility
)
5870 /* The verifier was disabled at compile time */
5875 mono_verifier_is_class_full_trust (MonoClass
*klass
)
5877 /* The verifier was disabled at compile time */
5882 mono_verifier_is_method_full_trust (MonoMethod
*method
)
5884 /* The verifier was disabled at compile time */
5889 mono_verifier_is_enabled_for_image (MonoImage
*image
)
5891 /* The verifier was disabled at compile time */
5896 mono_verifier_is_enabled_for_class (MonoClass
*klass
)
5898 /* The verifier was disabled at compile time */
5903 mono_verifier_is_enabled_for_method (MonoMethod
*method
)
5905 /* The verifier was disabled at compile time */
5910 mono_method_verify (MonoMethod
*method
, int level
)
5912 /* The verifier was disabled at compile time */
5917 mono_free_verify_list (GSList
*list
)
5919 /* The verifier was disabled at compile time */
5920 /* will always be null if verifier is disabled */