[sgen] One internal allocator per worker thread, to get rid of locking.
[mono-project/dkf.git] / mono / metadata / verify.c
blobc4dc537d98b9a86e02403d649e0e6d1f19761045
1 /*
2 * verify.c:
4 * Author:
5 * Mono Project (http://www.mono-project.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 */
10 #include <config.h>
12 #include <mono/metadata/object-internals.h>
13 #include <mono/metadata/verify.h>
14 #include <mono/metadata/verify-internals.h>
15 #include <mono/metadata/opcodes.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/reflection.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/mono-endian.h>
20 #include <mono/metadata/metadata.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/class-internals.h>
23 #include <mono/metadata/security-manager.h>
24 #include <mono/metadata/security-core-clr.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/mono-basic-block.h>
27 #include <mono/utils/mono-counters.h>
28 #include <mono/utils/monobitset.h>
29 #include <string.h>
30 #include <signal.h>
31 #include <ctype.h>
34 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
35 static gboolean verify_all = FALSE;
38 * Set the desired level of checks for the verfier.
41 void
42 mono_verifier_set_mode (MiniVerifierMode mode)
44 verifier_mode = mode;
47 void
48 mono_verifier_enable_verify_all ()
50 verify_all = TRUE;
53 #ifndef DISABLE_VERIFIER
55 * Pull the list of opcodes
57 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
58 a = i,
60 enum {
61 #include "mono/cil/opcode.def"
62 LAST = 0xff
64 #undef OPDEF
66 #ifdef MONO_VERIFIER_DEBUG
67 #define VERIFIER_DEBUG(code) do { code } while (0)
68 #else
69 #define VERIFIER_DEBUG(code)
70 #endif
72 //////////////////////////////////////////////////////////////////
73 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
74 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
75 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
76 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
77 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
78 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
79 do { \
80 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
81 vinfo->info.status = __status; \
82 vinfo->info.message = ( __msg ); \
83 vinfo->exception_type = (__exception); \
84 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
85 } while (0)
87 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
88 #define ADD_VERIFY_ERROR(__ctx, __msg) \
89 do { \
90 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
91 (__ctx)->valid = 0; \
92 } while (0)
94 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
95 do { \
96 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
97 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
98 (__ctx)->verifiable = 0; \
99 if (IS_FAIL_FAST_MODE (__ctx)) \
100 (__ctx)->valid = 0; \
102 } while (0)
104 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
105 do { \
106 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
107 (__ctx)->valid = 0; \
108 } while (0)
110 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
111 do { \
112 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
113 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
114 (__ctx)->verifiable = 0; \
115 if (IS_FAIL_FAST_MODE (__ctx)) \
116 (__ctx)->valid = 0; \
118 } while (0)
120 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
121 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
123 #if SIZEOF_VOID_P == 4
124 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
125 #else
126 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
127 #endif
129 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
130 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
132 /*Flags to be used with ILCodeDesc::flags */
133 enum {
134 /*Instruction has not been processed.*/
135 IL_CODE_FLAG_NOT_PROCESSED = 0,
136 /*Instruction was decoded by mono_method_verify loop.*/
137 IL_CODE_FLAG_SEEN = 1,
138 /*Instruction was target of a branch or is at a protected block boundary.*/
139 IL_CODE_FLAG_WAS_TARGET = 2,
140 /*Used by stack_init to avoid double initialize each entry.*/
141 IL_CODE_FLAG_STACK_INITED = 4,
142 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
143 IL_CODE_STACK_MERGED = 8,
144 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
145 IL_CODE_DELEGATE_SEQUENCE = 0x10,
146 /*This is a delegate created from a ldftn to a non final virtual method*/
147 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
148 /*This is a call to a non final virtual method*/
149 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
152 typedef enum {
153 RESULT_VALID,
154 RESULT_UNVERIFIABLE,
155 RESULT_INVALID
156 } verify_result_t;
158 typedef struct {
159 MonoType *type;
160 int stype;
161 MonoMethod *method;
162 } ILStackDesc;
165 typedef struct {
166 ILStackDesc *stack;
167 guint16 size, max_size;
168 guint16 flags;
169 } ILCodeDesc;
171 typedef struct {
172 int max_args;
173 int max_stack;
174 int verifiable;
175 int valid;
176 int level;
178 int code_size;
179 ILCodeDesc *code;
180 ILCodeDesc eval;
182 MonoType **params;
183 GSList *list;
184 /*Allocated fnptr MonoType that should be freed by us.*/
185 GSList *funptrs;
186 /*Type dup'ed exception types from catch blocks.*/
187 GSList *exception_types;
189 int num_locals;
190 MonoType **locals;
192 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
193 int target;
195 guint32 ip_offset;
196 MonoMethodSignature *signature;
197 MonoMethodHeader *header;
199 MonoGenericContext *generic_context;
200 MonoImage *image;
201 MonoMethod *method;
203 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
204 *on a method that creates a delegate for a non-final virtual method using ldftn*/
205 gboolean has_this_store;
207 /*This flag is used to control if the contructor of the parent class has been called.
208 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
209 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
211 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
212 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
214 gboolean super_ctor_called;
216 guint32 prefix_set;
217 gboolean has_flags;
218 MonoType *constrained_type;
219 } VerifyContext;
221 static void
222 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
224 static int
225 get_stack_type (MonoType *type);
227 static gboolean
228 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
230 static gboolean
231 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
233 static gboolean
234 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
235 //////////////////////////////////////////////////////////////////
239 enum {
240 TYPE_INV = 0, /* leave at 0. */
241 TYPE_I4 = 1,
242 TYPE_I8 = 2,
243 TYPE_NATIVE_INT = 3,
244 TYPE_R8 = 4,
245 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
246 TYPE_PTR = 5,
247 /* value types and classes */
248 TYPE_COMPLEX = 6,
249 /* Number of types, used to define the size of the tables*/
250 TYPE_MAX = 6,
252 /* Used by tables to signal that a result is not verifiable*/
253 NON_VERIFIABLE_RESULT = 0x80,
255 /*Mask used to extract just the type, excluding flags */
256 TYPE_MASK = 0x0F,
258 /* The stack type is a managed pointer, unmask the value to res */
259 POINTER_MASK = 0x100,
261 /*Stack type with the pointer mask*/
262 RAW_TYPE_MASK = 0x10F,
264 /* Controlled Mutability Manager Pointer */
265 CMMP_MASK = 0x200,
267 /* The stack type is a null literal*/
268 NULL_LITERAL_MASK = 0x400,
270 /**Used by ldarg.0 and family to let delegate verification happens.*/
271 THIS_POINTER_MASK = 0x800,
273 /**Signals that this is a boxed value type*/
274 BOXED_MASK = 0x1000,
276 /*This is an unitialized this ref*/
277 UNINIT_THIS_MASK = 0x2000,
280 static const char* const
281 type_names [TYPE_MAX + 1] = {
282 "Invalid",
283 "Int32",
284 "Int64",
285 "Native Int",
286 "Float64",
287 "Native Pointer",
288 "Complex"
291 enum {
292 PREFIX_UNALIGNED = 1,
293 PREFIX_VOLATILE = 2,
294 PREFIX_TAIL = 4,
295 PREFIX_CONSTRAINED = 8,
296 PREFIX_READONLY = 16
298 //////////////////////////////////////////////////////////////////
300 #ifdef ENABLE_VERIFIER_STATS
302 #define MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
303 #define MEM_FREE(amt) do { working_set -= (amt); } while (0)
305 static int allocated_memory;
306 static int working_set;
307 static int max_allocated_memory;
308 static int max_working_set;
309 static int total_allocated_memory;
311 static void
312 finish_collect_stats (void)
314 max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
315 max_working_set = MAX (max_working_set, working_set);
316 total_allocated_memory += allocated_memory;
317 allocated_memory = working_set = 0;
320 static void
321 init_verifier_stats (void)
323 static gboolean inited;
324 if (!inited) {
325 inited = TRUE;
326 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
327 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
328 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
332 #else
334 #define MEM_ALLOC(amt) do {} while (0)
335 #define MEM_FREE(amt) do { } while (0)
337 #define finish_collect_stats()
338 #define init_verifier_stats()
340 #endif
343 //////////////////////////////////////////////////////////////////
346 /*Token validation macros and functions */
347 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
348 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
349 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
350 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
352 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
353 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
354 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
355 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
356 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
357 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
360 * Verify if @token refers to a valid row on int's table.
362 static gboolean
363 token_bounds_check (MonoImage *image, guint32 token)
365 if (image->dynamic)
366 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
367 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
370 static MonoType *
371 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
373 MonoType *res = g_new0 (MonoType, 1);
374 MEM_ALLOC (sizeof (MonoType));
376 //FIXME use mono_method_get_signature_full
377 res->data.method = mono_method_signature (method);
378 res->type = MONO_TYPE_FNPTR;
379 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
380 return res;
384 * mono_type_is_enum_type:
386 * Returns TRUE if @type is an enum type.
388 static gboolean
389 mono_type_is_enum_type (MonoType *type)
391 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
392 return TRUE;
393 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
394 return TRUE;
395 return FALSE;
399 * mono_type_is_value_type:
401 * Returns TRUE if @type is named after @namespace.@name.
404 static gboolean
405 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
407 return type->type == MONO_TYPE_VALUETYPE &&
408 !strcmp (namespace, type->data.klass->name_space) &&
409 !strcmp (name, type->data.klass->name);
413 * Returns TURE if @type is VAR or MVAR
415 static gboolean
416 mono_type_is_generic_argument (MonoType *type)
418 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
422 * mono_type_get_underlying_type_any:
424 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
426 * Returns the underlying type of @type regardless if it is byref or not.
428 static MonoType*
429 mono_type_get_underlying_type_any (MonoType *type)
431 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
432 return mono_class_enum_basetype (type->data.klass);
433 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
434 return mono_class_enum_basetype (type->data.generic_class->container_class);
435 return type;
438 static G_GNUC_UNUSED const char*
439 mono_type_get_stack_name (MonoType *type)
441 return type_names [get_stack_type (type) & TYPE_MASK];
444 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
445 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
447 static gboolean
448 mono_method_is_constructor (MonoMethod *method)
450 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
451 !(method->flags & CTOR_INVALID_FLAGS) &&
452 !strcmp (".ctor", method->name));
455 static gboolean
456 mono_class_has_default_constructor (MonoClass *klass)
458 MonoMethod *method;
459 int i;
461 mono_class_setup_methods (klass);
462 if (klass->exception_type)
463 return FALSE;
465 for (i = 0; i < klass->method.count; ++i) {
466 method = klass->methods [i];
467 if (mono_method_is_constructor (method) &&
468 mono_method_signature (method) &&
469 mono_method_signature (method)->param_count == 0 &&
470 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
471 return TRUE;
473 return FALSE;
477 * Verify if @type is valid for the given @ctx verification context.
478 * this function checks for VAR and MVAR types that are invalid under the current verifier,
480 static gboolean
481 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
483 int i;
484 MonoGenericInst *inst;
486 switch (type->type) {
487 case MONO_TYPE_VAR:
488 case MONO_TYPE_MVAR:
489 if (!context)
490 return FALSE;
491 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
492 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
493 return FALSE;
494 break;
495 case MONO_TYPE_SZARRAY:
496 return mono_type_is_valid_type_in_context (&type->data.klass->byval_arg, context);
497 case MONO_TYPE_ARRAY:
498 return mono_type_is_valid_type_in_context (&type->data.array->eklass->byval_arg, context);
499 case MONO_TYPE_PTR:
500 return mono_type_is_valid_type_in_context (type->data.type, context);
501 case MONO_TYPE_GENERICINST:
502 inst = type->data.generic_class->context.class_inst;
503 if (!inst->is_open)
504 break;
505 for (i = 0; i < inst->type_argc; ++i)
506 if (!mono_type_is_valid_type_in_context (inst->type_argv [i], context))
507 return FALSE;
508 break;
510 return TRUE;
513 /*This function returns NULL if the type is not instantiatable*/
514 static MonoType*
515 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
517 MonoError error;
518 MonoType *result;
520 result = mono_class_inflate_generic_type_checked (type, context, &error);
521 if (!mono_error_ok (&error)) {
522 mono_error_cleanup (&error);
523 return NULL;
525 return result;
529 static gboolean
530 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
532 MonoError error;
533 int i;
535 if (ginst->type_argc != gc->type_argc)
536 return FALSE;
538 for (i = 0; i < gc->type_argc; ++i) {
539 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
540 MonoClass *paramClass;
541 MonoClass **constraints;
543 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
544 continue;
545 if (mono_type_is_generic_argument (ginst->type_argv [i]))
546 continue; //it's not our job to validate type variables
548 paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
550 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
551 return FALSE;
553 /*it's not safe to call mono_class_init from here*/
554 if (paramClass->generic_class && !paramClass->inited) {
555 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
556 return FALSE;
559 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
560 return FALSE;
562 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
563 return FALSE;
565 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
566 return FALSE;
568 if (!param_info->constraints)
569 continue;
571 for (constraints = param_info->constraints; *constraints; ++constraints) {
572 MonoClass *ctr = *constraints;
573 MonoType *inflated;
575 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
576 if (!mono_error_ok (&error)) {
577 mono_error_cleanup (&error);
578 return FALSE;
580 ctr = mono_class_from_mono_type (inflated);
581 mono_metadata_free_type (inflated);
583 if (!mono_class_is_assignable_from_slow (ctr, paramClass))
584 return FALSE;
587 return TRUE;
591 * Return true if @candidate is constraint compatible with @target.
593 * This means that @candidate constraints are a super set of @target constaints
595 static gboolean
596 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoClass *candidate_param_class, MonoGenericContext *context)
598 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
599 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
601 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
602 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
603 if ((tmask & cmask) != tmask)
604 return FALSE;
606 if (tinfo->constraints) {
607 MonoClass **target_class, **candidate_class;
608 for (target_class = tinfo->constraints; *target_class; ++target_class) {
609 MonoClass *tc;
610 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
611 if (!inflated)
612 return FALSE;
613 tc = mono_class_from_mono_type (inflated);
614 mono_metadata_free_type (inflated);
617 * A constraint from @target might inflate into @candidate itself and in that case we don't need
618 * check it's constraints since it satisfy the constraint by itself.
620 if (mono_metadata_type_equal (&tc->byval_arg, &candidate_param_class->byval_arg))
621 continue;
623 if (!cinfo->constraints)
624 return FALSE;
626 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
627 MonoClass *cc;
628 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
629 if (!inflated)
630 return FALSE;
631 cc = mono_class_from_mono_type (inflated);
632 mono_metadata_free_type (inflated);
634 if (mono_class_is_assignable_from (tc, cc))
635 break;
637 if (!*candidate_class)
638 return FALSE;
641 return TRUE;
644 static MonoGenericParam*
645 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
647 MonoGenericContainer *gc;
648 MonoMethod *method = ctx->method;
649 int num;
651 num = mono_type_get_generic_param_num (type);
653 if (type->type == MONO_TYPE_VAR) {
654 MonoClass *gtd = method->klass;
655 if (gtd->generic_class)
656 gtd = gtd->generic_class->container_class;
657 gc = gtd->generic_container;
658 } else { //MVAR
659 MonoMethod *gmd = method;
660 if (method->is_inflated)
661 gmd = ((MonoMethodInflated*)method)->declaring;
662 gc = mono_method_get_generic_container (gmd);
664 if (!gc)
665 return FALSE;
666 return mono_generic_container_get_param (gc, num);
672 * Verify if @type is valid for the given @ctx verification context.
673 * this function checks for VAR and MVAR types that are invalid under the current verifier,
674 * This means that it either
676 static gboolean
677 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
679 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
682 static gboolean
683 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
685 int i;
686 for (i = 0; i < ginst->type_argc; ++i) {
687 MonoType *type = ginst->type_argv [i];
688 if (!is_valid_type_in_context (ctx, type))
689 return FALSE;
691 return TRUE;
694 static gboolean
695 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
697 int i;
698 for (i = 0; i < ginst->type_argc; ++i) {
699 MonoType *type = ginst->type_argv [i];
700 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
701 MonoGenericParam *candidate;
702 MonoClass *candidate_class;
704 if (!mono_type_is_generic_argument (type))
705 continue;
707 if (!is_valid_type_in_context (ctx, type))
708 return FALSE;
710 candidate = verifier_get_generic_param_from_type (ctx, type);
711 candidate_class = mono_class_from_mono_type (type);
713 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, candidate_class, context))
714 return FALSE;
716 return TRUE;
719 static gboolean
720 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
722 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
723 MonoGenericInst *ginst = gmethod->context.method_inst;
724 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
725 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
728 static gboolean
729 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
731 MonoGenericClass *gklass = klass->generic_class;
732 MonoGenericInst *ginst = gklass->context.class_inst;
733 MonoGenericContainer *gc = gklass->container_class->generic_container;
734 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
737 static gboolean
738 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
740 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
741 MonoGenericInst *ginst = gmethod->context.method_inst;
742 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
743 if (!gc) /*non-generic inflated method - it's part of a generic type */
744 return TRUE;
745 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
746 return FALSE;
747 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
751 static gboolean
752 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
754 MonoGenericClass *gklass = klass->generic_class;
755 MonoGenericInst *ginst = gklass->context.class_inst;
756 MonoGenericContainer *gc = gklass->container_class->generic_container;
757 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
758 return FALSE;
759 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
762 static gboolean
763 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
765 MonoClass *klass;
767 if (type == NULL) {
768 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
769 return FALSE;
772 if (!is_valid_type_in_context (ctx, type)) {
773 char *str = mono_type_full_name (type);
774 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
775 type->type == MONO_TYPE_VAR ? "!" : "!!",
776 str,
777 type->type == MONO_TYPE_VAR ? "class" : "method",
778 ctx->ip_offset),
779 MONO_EXCEPTION_BAD_IMAGE);
780 g_free (str);
781 return FALSE;
784 klass = mono_class_from_mono_type (type);
785 mono_class_init (klass);
786 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
787 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
788 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic instantiation of type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
789 else
790 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
791 return FALSE;
794 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
795 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
796 return FALSE;
799 if (!klass->generic_class)
800 return TRUE;
802 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
803 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
804 return FALSE;
807 if (!mono_class_repect_method_constraints (ctx, klass)) {
808 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s (generic args don't respect target's constraints) at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
809 return FALSE;
812 return TRUE;
815 static verify_result_t
816 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
818 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
819 return RESULT_INVALID;
821 if (!method->is_inflated)
822 return RESULT_VALID;
824 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
825 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset), MONO_EXCEPTION_UNVERIFIABLE_IL);
826 return RESULT_INVALID;
829 if (!mono_method_repect_method_constraints (ctx, method)) {
830 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s (generic args don't respect target's constraints) at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset));
831 return RESULT_UNVERIFIABLE;
833 return RESULT_VALID;
837 static MonoClassField*
838 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
839 MonoClassField *field;
840 MonoClass *klass = NULL;
842 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
843 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid field token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
844 return NULL;
847 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
848 if (!field || !field->parent || !klass) {
849 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load field from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
850 return NULL;
853 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
854 return NULL;
856 *out_klass = klass;
857 return field;
860 static MonoMethod*
861 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
862 MonoMethod* method;
864 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
865 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid method token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
866 return NULL;
869 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
871 if (!method) {
872 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load method from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
873 return NULL;
876 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
877 return NULL;
879 return method;
882 static MonoType*
883 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
884 MonoType* type;
886 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
887 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
888 return NULL;
891 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
893 if (!type) {
894 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load type from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
895 return NULL;
898 if (!mono_type_is_valid_in_context (ctx, type))
899 return NULL;
901 return type;
905 /* stack_slot_get_type:
907 * Returns the stack type of @value. This value includes POINTER_MASK.
909 * Use this function to checks that account for a managed pointer.
911 static gint32
912 stack_slot_get_type (ILStackDesc *value)
914 return value->stype & RAW_TYPE_MASK;
917 /* stack_slot_get_underlying_type:
919 * Returns the stack type of @value. This value does not include POINTER_MASK.
921 * Use this function is cases where the fact that the value could be a managed pointer is
922 * irrelevant. For example, field load doesn't care about this fact of type on stack.
924 static gint32
925 stack_slot_get_underlying_type (ILStackDesc *value)
927 return value->stype & TYPE_MASK;
930 /* stack_slot_is_managed_pointer:
932 * Returns TRUE is @value is a managed pointer.
934 static gboolean
935 stack_slot_is_managed_pointer (ILStackDesc *value)
937 return (value->stype & POINTER_MASK) == POINTER_MASK;
940 /* stack_slot_is_managed_mutability_pointer:
942 * Returns TRUE is @value is a managed mutability pointer.
944 static G_GNUC_UNUSED gboolean
945 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
947 return (value->stype & CMMP_MASK) == CMMP_MASK;
950 /* stack_slot_is_null_literal:
952 * Returns TRUE is @value is the null literal.
954 static gboolean
955 stack_slot_is_null_literal (ILStackDesc *value)
957 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
961 /* stack_slot_is_this_pointer:
963 * Returns TRUE is @value is the this literal
965 static gboolean
966 stack_slot_is_this_pointer (ILStackDesc *value)
968 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
971 /* stack_slot_is_boxed_value:
973 * Returns TRUE is @value is a boxed value
975 static gboolean
976 stack_slot_is_boxed_value (ILStackDesc *value)
978 return (value->stype & BOXED_MASK) == BOXED_MASK;
981 static const char *
982 stack_slot_get_name (ILStackDesc *value)
984 return type_names [value->stype & TYPE_MASK];
987 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
988 if (PRED (value)) { \
989 if (!first) \
990 g_string_append (str, ", "); \
991 g_string_append (str, NAME); \
992 first = FALSE; \
993 } } while (0)
995 static char*
996 stack_slot_stack_type_full_name (ILStackDesc *value)
998 GString *str = g_string_new ("");
999 char *result;
1000 gboolean has_pred = FALSE, first = TRUE;
1002 if ((value->stype & TYPE_MASK) != value->stype) {
1003 g_string_append(str, "[");
1004 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
1005 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
1006 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
1007 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
1008 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1009 has_pred = TRUE;
1012 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1013 if (!has_pred)
1014 g_string_append(str, "[");
1015 if (!first)
1016 g_string_append (str, ", ");
1017 g_string_append (str, "unboxed");
1018 has_pred = TRUE;
1021 if (has_pred)
1022 g_string_append(str, "] ");
1024 g_string_append (str, stack_slot_get_name (value));
1025 result = str->str;
1026 g_string_free (str, FALSE);
1027 return result;
1030 static char*
1031 stack_slot_full_name (ILStackDesc *value)
1033 char *type_name = mono_type_full_name (value->type);
1034 char *stack_name = stack_slot_stack_type_full_name (value);
1035 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1036 g_free (type_name);
1037 g_free (stack_name);
1038 return res;
1041 //////////////////////////////////////////////////////////////////
1042 void
1043 mono_free_verify_list (GSList *list)
1045 MonoVerifyInfoExtended *info;
1046 GSList *tmp;
1048 for (tmp = list; tmp; tmp = tmp->next) {
1049 info = tmp->data;
1050 g_free (info->info.message);
1051 g_free (info);
1053 g_slist_free (list);
1056 #define ADD_ERROR(list,msg) \
1057 do { \
1058 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1059 vinfo->info.status = MONO_VERIFY_ERROR; \
1060 vinfo->info.message = (msg); \
1061 (list) = g_slist_prepend ((list), vinfo); \
1062 } while (0)
1064 #define ADD_WARN(list,code,msg) \
1065 do { \
1066 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1067 vinfo->info.status = (code); \
1068 vinfo->info.message = (msg); \
1069 (list) = g_slist_prepend ((list), vinfo); \
1070 } while (0)
1072 #define ADD_INVALID(list,msg) \
1073 do { \
1074 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1075 vinfo->status = MONO_VERIFY_ERROR; \
1076 vinfo->message = (msg); \
1077 (list) = g_slist_prepend ((list), vinfo); \
1078 /*G_BREAKPOINT ();*/ \
1079 goto invalid_cil; \
1080 } while (0)
1082 #define CHECK_STACK_UNDERFLOW(num) \
1083 do { \
1084 if (cur_stack < (num)) \
1085 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1086 } while (0)
1088 #define CHECK_STACK_OVERFLOW() \
1089 do { \
1090 if (cur_stack >= max_stack) \
1091 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1092 } while (0)
1095 static int
1096 in_any_block (MonoMethodHeader *header, guint offset)
1098 int i;
1099 MonoExceptionClause *clause;
1101 for (i = 0; i < header->num_clauses; ++i) {
1102 clause = &header->clauses [i];
1103 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1104 return 1;
1105 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1106 return 1;
1107 if (MONO_OFFSET_IN_FILTER (clause, offset))
1108 return 1;
1110 return 0;
1114 * in_any_exception_block:
1116 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1118 static gboolean
1119 in_any_exception_block (MonoMethodHeader *header, guint offset)
1121 int i;
1122 MonoExceptionClause *clause;
1124 for (i = 0; i < header->num_clauses; ++i) {
1125 clause = &header->clauses [i];
1126 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1127 return TRUE;
1128 if (MONO_OFFSET_IN_FILTER (clause, offset))
1129 return TRUE;
1131 return FALSE;
1135 * is_valid_branch_instruction:
1137 * Verify if it's valid to perform a branch from @offset to @target.
1138 * This should be used with br and brtrue/false.
1139 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1140 * The major diferent from other similiar functions is that branching into a
1141 * finally/fault block is invalid instead of just unverifiable.
1143 static int
1144 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1146 int i;
1147 MonoExceptionClause *clause;
1149 for (i = 0; i < header->num_clauses; ++i) {
1150 clause = &header->clauses [i];
1151 /*branching into a finally block is invalid*/
1152 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1153 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1154 MONO_OFFSET_IN_HANDLER (clause, target))
1155 return 2;
1157 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1158 return 1;
1159 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1160 return 1;
1161 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1162 return 1;
1164 return 0;
1168 * is_valid_cmp_branch_instruction:
1170 * Verify if it's valid to perform a branch from @offset to @target.
1171 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1172 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1174 * The major diferences from other similar functions are that most errors lead to invalid
1175 * code and only branching out of finally, filter or fault clauses is unverifiable.
1177 static int
1178 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1180 int i;
1181 MonoExceptionClause *clause;
1183 for (i = 0; i < header->num_clauses; ++i) {
1184 clause = &header->clauses [i];
1185 /*branching out of a handler or finally*/
1186 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1187 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1188 !MONO_OFFSET_IN_HANDLER (clause, target))
1189 return 1;
1191 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1192 return 2;
1193 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1194 return 2;
1195 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1196 return 2;
1198 return 0;
1202 * A leave can't escape a finally block
1204 static int
1205 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1207 int i;
1208 MonoExceptionClause *clause;
1210 for (i = 0; i < header->num_clauses; ++i) {
1211 clause = &header->clauses [i];
1212 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1213 return 0;
1214 if (MONO_OFFSET_IN_FILTER (clause, offset))
1215 return 0;
1217 return 1;
1221 * A rethrow can't happen outside of a catch handler.
1223 static int
1224 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1226 int i;
1227 MonoExceptionClause *clause;
1229 for (i = 0; i < header->num_clauses; ++i) {
1230 clause = &header->clauses [i];
1231 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1232 return 1;
1233 if (MONO_OFFSET_IN_FILTER (clause, offset))
1234 return 1;
1236 return 0;
1240 * An endfinally can't happen outside of a finally/fault handler.
1242 static int
1243 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1245 int i;
1246 MonoExceptionClause *clause;
1248 for (i = 0; i < header->num_clauses; ++i) {
1249 clause = &header->clauses [i];
1250 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1251 return 1;
1253 return 0;
1258 * An endfilter can only happens inside a filter clause.
1259 * In non-strict mode filter is allowed inside the handler clause too
1261 static MonoExceptionClause *
1262 is_correct_endfilter (VerifyContext *ctx, guint offset)
1264 int i;
1265 MonoExceptionClause *clause;
1267 for (i = 0; i < ctx->header->num_clauses; ++i) {
1268 clause = &ctx->header->clauses [i];
1269 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1270 continue;
1271 if (MONO_OFFSET_IN_FILTER (clause, offset))
1272 return clause;
1273 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1274 return clause;
1276 return NULL;
1281 * Non-strict endfilter can happens inside a try block or any handler block
1283 static int
1284 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1286 int i;
1287 MonoExceptionClause *clause;
1289 for (i = 0; i < ctx->header->num_clauses; ++i) {
1290 clause = &ctx->header->clauses [i];
1291 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1292 return 1;
1294 return 0;
1297 static gboolean
1298 is_valid_bool_arg (ILStackDesc *arg)
1300 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1301 return TRUE;
1304 switch (stack_slot_get_underlying_type (arg)) {
1305 case TYPE_I4:
1306 case TYPE_I8:
1307 case TYPE_NATIVE_INT:
1308 case TYPE_PTR:
1309 return TRUE;
1310 case TYPE_COMPLEX:
1311 g_assert (arg->type);
1312 switch (arg->type->type) {
1313 case MONO_TYPE_CLASS:
1314 case MONO_TYPE_STRING:
1315 case MONO_TYPE_OBJECT:
1316 case MONO_TYPE_SZARRAY:
1317 case MONO_TYPE_ARRAY:
1318 case MONO_TYPE_FNPTR:
1319 case MONO_TYPE_PTR:
1320 return TRUE;
1321 case MONO_TYPE_GENERICINST:
1322 /*We need to check if the container class
1323 * of the generic type is a valuetype, iow:
1324 * is it a "class Foo<T>" or a "struct Foo<T>"?
1326 return !arg->type->data.generic_class->container_class->valuetype;
1328 default:
1329 return FALSE;
1334 /*Type manipulation helper*/
1336 /*Returns the byref version of the supplied MonoType*/
1337 static MonoType*
1338 mono_type_get_type_byref (MonoType *type)
1340 if (type->byref)
1341 return type;
1342 return &mono_class_from_mono_type (type)->this_arg;
1346 /*Returns the byval version of the supplied MonoType*/
1347 static MonoType*
1348 mono_type_get_type_byval (MonoType *type)
1350 if (!type->byref)
1351 return type;
1352 return &mono_class_from_mono_type (type)->byval_arg;
1355 static MonoType*
1356 mono_type_from_stack_slot (ILStackDesc *slot)
1358 if (stack_slot_is_managed_pointer (slot))
1359 return mono_type_get_type_byref (slot->type);
1360 return slot->type;
1363 /*Stack manipulation code*/
1365 static void
1366 ensure_stack_size (ILCodeDesc *stack, int required)
1368 int new_size = 8;
1369 ILStackDesc *tmp;
1371 if (required < stack->max_size)
1372 return;
1374 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1375 new_size = MAX (8, MAX (required, stack->max_size * 2));
1377 g_assert (new_size >= stack->size);
1378 g_assert (new_size >= required);
1380 tmp = g_new0 (ILStackDesc, new_size);
1381 MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1383 if (stack->stack) {
1384 if (stack->size)
1385 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1386 g_free (stack->stack);
1387 MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1390 stack->stack = tmp;
1391 stack->max_size = new_size;
1394 static void
1395 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1397 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1398 return;
1399 state->size = state->max_size = 0;
1400 state->flags |= IL_CODE_FLAG_STACK_INITED;
1403 static void
1404 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1406 ensure_stack_size (to, from->size);
1407 to->size = from->size;
1409 /*stack copy happens at merge points, which have small stacks*/
1410 if (from->size)
1411 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1414 static void
1415 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1417 to->stype = from->stype;
1418 to->type = from->type;
1419 to->method = from->method;
1422 static int
1423 check_underflow (VerifyContext *ctx, int size)
1425 if (ctx->eval.size < size) {
1426 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1427 return 0;
1429 return 1;
1432 static int
1433 check_overflow (VerifyContext *ctx)
1435 if (ctx->eval.size >= ctx->max_stack) {
1436 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1437 return 0;
1439 return 1;
1442 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1443 static gboolean
1444 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1446 if (stack_slot_get_type (value) == TYPE_PTR) {
1447 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1448 return 0;
1450 return 1;
1453 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1454 static gboolean
1455 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1457 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1458 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1459 return 0;
1461 return 1;
1464 static ILStackDesc *
1465 stack_push (VerifyContext *ctx)
1467 g_assert (ctx->eval.size < ctx->max_stack);
1468 g_assert (ctx->eval.size <= ctx->eval.max_size);
1470 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1472 return & ctx->eval.stack [ctx->eval.size++];
1475 static ILStackDesc *
1476 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1478 ILStackDesc *top = stack_push (ctx);
1479 top->stype = stype;
1480 top->type = type;
1481 return top;
1484 static ILStackDesc *
1485 stack_pop (VerifyContext *ctx)
1487 ILStackDesc *ret;
1488 g_assert (ctx->eval.size > 0);
1489 ret = ctx->eval.stack + --ctx->eval.size;
1490 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1491 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1492 return ret;
1495 /* This function allows to safely pop an unititialized this ptr from
1496 * the eval stack without marking the method as unverifiable.
1498 static ILStackDesc *
1499 stack_pop_safe (VerifyContext *ctx)
1501 g_assert (ctx->eval.size > 0);
1502 return ctx->eval.stack + --ctx->eval.size;
1505 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1506 static ILStackDesc*
1507 stack_peek (VerifyContext *ctx, int distance)
1509 g_assert (ctx->eval.size - distance > 0);
1510 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1513 static ILStackDesc *
1514 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1516 ILStackDesc *top = stack_push (ctx);
1517 copy_stack_value (top, value);
1518 return top;
1521 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1523 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1524 * */
1525 static MonoType*
1526 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1528 MonoType *type;
1529 MonoClass *class;
1531 if (!(type = verifier_load_type (ctx, token, opcode)))
1532 return NULL;
1534 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1535 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1536 return NULL;
1539 if (type->type == MONO_TYPE_VOID) {
1540 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1541 return NULL;
1544 if (type->type == MONO_TYPE_TYPEDBYREF)
1545 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1547 if (!(class = mono_class_from_mono_type (type)))
1548 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1550 if (class->generic_container && type->type != MONO_TYPE_GENERICINST)
1551 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the generic type definition in a boxable type position for %s at 0x%04x", opcode, ctx->ip_offset));
1553 check_unverifiable_type (ctx, type);
1554 return type;
1558 /*operation result tables */
1560 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1561 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1562 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1563 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1564 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1565 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1566 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1569 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1570 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1571 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1572 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1573 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1574 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1575 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1578 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1579 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1580 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1581 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1582 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1583 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1584 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1587 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1588 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1589 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1590 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1591 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1592 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1593 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1596 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1597 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1598 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1599 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1600 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1601 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1602 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1605 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1606 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1607 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1608 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1609 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1610 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1611 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1614 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1615 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1616 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1617 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1618 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1619 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1620 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1623 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1624 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1625 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1626 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1627 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1628 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1629 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1632 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1633 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1634 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1635 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1636 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1637 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1638 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1641 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1642 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1643 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1644 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1645 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1646 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1647 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1650 #ifdef MONO_VERIFIER_DEBUG
1652 /*debug helpers */
1653 static void
1654 dump_stack_value (ILStackDesc *value)
1656 printf ("[(%x)(%x)", value->type->type, value->stype);
1658 if (stack_slot_is_this_pointer (value))
1659 printf ("[this] ");
1661 if (stack_slot_is_boxed_value (value))
1662 printf ("[boxed] ");
1664 if (stack_slot_is_null_literal (value))
1665 printf ("[null] ");
1667 if (stack_slot_is_managed_mutability_pointer (value))
1668 printf ("Controled Mutability MP: ");
1670 if (stack_slot_is_managed_pointer (value))
1671 printf ("Managed Pointer to: ");
1673 switch (stack_slot_get_underlying_type (value)) {
1674 case TYPE_INV:
1675 printf ("invalid type]");
1676 return;
1677 case TYPE_I4:
1678 printf ("int32]");
1679 return;
1680 case TYPE_I8:
1681 printf ("int64]");
1682 return;
1683 case TYPE_NATIVE_INT:
1684 printf ("native int]");
1685 return;
1686 case TYPE_R8:
1687 printf ("float64]");
1688 return;
1689 case TYPE_PTR:
1690 printf ("unmanaged pointer]");
1691 return;
1692 case TYPE_COMPLEX:
1693 switch (value->type->type) {
1694 case MONO_TYPE_CLASS:
1695 case MONO_TYPE_VALUETYPE:
1696 printf ("complex] (%s)", value->type->data.klass->name);
1697 return;
1698 case MONO_TYPE_STRING:
1699 printf ("complex] (string)");
1700 return;
1701 case MONO_TYPE_OBJECT:
1702 printf ("complex] (object)");
1703 return;
1704 case MONO_TYPE_SZARRAY:
1705 printf ("complex] (%s [])", value->type->data.klass->name);
1706 return;
1707 case MONO_TYPE_ARRAY:
1708 printf ("complex] (%s [%d %d %d])",
1709 value->type->data.array->eklass->name,
1710 value->type->data.array->rank,
1711 value->type->data.array->numsizes,
1712 value->type->data.array->numlobounds);
1713 return;
1714 case MONO_TYPE_GENERICINST:
1715 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1716 return;
1717 case MONO_TYPE_VAR:
1718 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1719 return;
1720 case MONO_TYPE_MVAR:
1721 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1722 return;
1723 default: {
1724 //should be a boxed value
1725 char * name = mono_type_full_name (value->type);
1726 printf ("complex] %s", name);
1727 g_free (name);
1728 return;
1731 default:
1732 printf ("unknown stack %x type]\n", value->stype);
1733 g_assert_not_reached ();
1737 static void
1738 dump_stack_state (ILCodeDesc *state)
1740 int i;
1742 printf ("(%d) ", state->size);
1743 for (i = 0; i < state->size; ++i)
1744 dump_stack_value (state->stack + i);
1745 printf ("\n");
1747 #endif
1749 /*Returns TRUE if candidate array type can be assigned to target.
1750 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1752 static gboolean
1753 is_array_type_compatible (MonoType *target, MonoType *candidate)
1755 MonoArrayType *left = target->data.array;
1756 MonoArrayType *right = candidate->data.array;
1758 g_assert (target->type == MONO_TYPE_ARRAY);
1759 g_assert (candidate->type == MONO_TYPE_ARRAY);
1761 if (left->rank != right->rank)
1762 return FALSE;
1764 return mono_class_is_assignable_from (left->eklass, right->eklass);
1767 static int
1768 get_stack_type (MonoType *type)
1770 int mask = 0;
1771 int type_kind = type->type;
1772 if (type->byref)
1773 mask = POINTER_MASK;
1774 /*TODO handle CMMP_MASK */
1776 handle_enum:
1777 switch (type_kind) {
1778 case MONO_TYPE_I1:
1779 case MONO_TYPE_U1:
1780 case MONO_TYPE_BOOLEAN:
1781 case MONO_TYPE_I2:
1782 case MONO_TYPE_U2:
1783 case MONO_TYPE_CHAR:
1784 case MONO_TYPE_I4:
1785 case MONO_TYPE_U4:
1786 return TYPE_I4 | mask;
1788 case MONO_TYPE_I:
1789 case MONO_TYPE_U:
1790 return TYPE_NATIVE_INT | mask;
1792 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1793 case MONO_TYPE_FNPTR:
1794 case MONO_TYPE_PTR:
1795 case MONO_TYPE_TYPEDBYREF:
1796 return TYPE_PTR | mask;
1798 case MONO_TYPE_VAR:
1799 case MONO_TYPE_MVAR:
1801 case MONO_TYPE_CLASS:
1802 case MONO_TYPE_STRING:
1803 case MONO_TYPE_OBJECT:
1804 case MONO_TYPE_SZARRAY:
1805 case MONO_TYPE_ARRAY:
1806 return TYPE_COMPLEX | mask;
1808 case MONO_TYPE_I8:
1809 case MONO_TYPE_U8:
1810 return TYPE_I8 | mask;
1812 case MONO_TYPE_R4:
1813 case MONO_TYPE_R8:
1814 return TYPE_R8 | mask;
1816 case MONO_TYPE_GENERICINST:
1817 case MONO_TYPE_VALUETYPE:
1818 if (mono_type_is_enum_type (type)) {
1819 type = mono_type_get_underlying_type_any (type);
1820 if (!type)
1821 return FALSE;
1822 type_kind = type->type;
1823 goto handle_enum;
1824 } else {
1825 return TYPE_COMPLEX | mask;
1828 default:
1829 return TYPE_INV;
1833 /* convert MonoType to ILStackDesc format (stype) */
1834 static gboolean
1835 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1837 int mask = 0;
1838 int type_kind = type->type;
1840 if (type->byref || take_addr)
1841 mask = POINTER_MASK;
1842 /* TODO handle CMMP_MASK */
1844 handle_enum:
1845 stack->type = type;
1847 switch (type_kind) {
1848 case MONO_TYPE_I1:
1849 case MONO_TYPE_U1:
1850 case MONO_TYPE_BOOLEAN:
1851 case MONO_TYPE_I2:
1852 case MONO_TYPE_U2:
1853 case MONO_TYPE_CHAR:
1854 case MONO_TYPE_I4:
1855 case MONO_TYPE_U4:
1856 stack->stype = TYPE_I4 | mask;
1857 break;
1858 case MONO_TYPE_I:
1859 case MONO_TYPE_U:
1860 stack->stype = TYPE_NATIVE_INT | mask;
1861 break;
1863 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1864 case MONO_TYPE_FNPTR:
1865 case MONO_TYPE_PTR:
1866 case MONO_TYPE_TYPEDBYREF:
1867 stack->stype = TYPE_PTR | mask;
1868 break;
1870 case MONO_TYPE_CLASS:
1871 case MONO_TYPE_STRING:
1872 case MONO_TYPE_OBJECT:
1873 case MONO_TYPE_SZARRAY:
1874 case MONO_TYPE_ARRAY:
1876 case MONO_TYPE_VAR:
1877 case MONO_TYPE_MVAR:
1878 stack->stype = TYPE_COMPLEX | mask;
1879 break;
1881 case MONO_TYPE_I8:
1882 case MONO_TYPE_U8:
1883 stack->stype = TYPE_I8 | mask;
1884 break;
1885 case MONO_TYPE_R4:
1886 case MONO_TYPE_R8:
1887 stack->stype = TYPE_R8 | mask;
1888 break;
1889 case MONO_TYPE_GENERICINST:
1890 case MONO_TYPE_VALUETYPE:
1891 if (mono_type_is_enum_type (type)) {
1892 MonoType *utype = mono_type_get_underlying_type_any (type);
1893 if (!utype) {
1894 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
1895 return FALSE;
1897 type = utype;
1898 type_kind = type->type;
1899 goto handle_enum;
1900 } else {
1901 stack->stype = TYPE_COMPLEX | mask;
1902 break;
1904 default:
1905 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1906 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
1907 return FALSE;
1909 return TRUE;
1913 * init_stack_with_value_at_exception_boundary:
1915 * Initialize the stack and push a given type.
1916 * The instruction is marked as been on the exception boundary.
1918 static void
1919 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
1921 MonoError error;
1922 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
1924 if (!mono_error_ok (&error)) {
1925 char *name = mono_type_get_full_name (klass);
1926 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
1927 g_free (name);
1928 mono_error_cleanup (&error);
1929 return;
1932 if (!ctx->max_stack) {
1933 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
1934 return;
1937 stack_init (ctx, code);
1938 ensure_stack_size (code, 1);
1939 set_stack_value (ctx, code->stack, type, FALSE);
1940 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
1941 code->size = 1;
1942 code->flags |= IL_CODE_FLAG_WAS_TARGET;
1943 if (mono_type_is_generic_argument (type))
1944 code->stack->stype |= BOXED_MASK;
1947 /*Verify if type 'candidate' can be stored in type 'target'.
1949 * If strict, check for the underlying type and not the verification stack types
1951 static gboolean
1952 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1954 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1955 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1957 MonoType *original_candidate = candidate;
1958 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
1960 /*only one is byref */
1961 if (candidate->byref ^ target->byref) {
1962 /* converting from native int to byref*/
1963 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1964 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1965 return TRUE;
1967 return FALSE;
1969 strict |= target->byref;
1970 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
1971 candidate = mono_type_get_underlying_type_any (candidate);
1973 handle_enum:
1974 switch (target->type) {
1975 case MONO_TYPE_VOID:
1976 return candidate->type == MONO_TYPE_VOID;
1977 case MONO_TYPE_I1:
1978 case MONO_TYPE_U1:
1979 case MONO_TYPE_BOOLEAN:
1980 if (strict)
1981 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1982 case MONO_TYPE_I2:
1983 case MONO_TYPE_U2:
1984 case MONO_TYPE_CHAR:
1985 if (strict)
1986 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1987 case MONO_TYPE_I4:
1988 case MONO_TYPE_U4: {
1989 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1990 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1991 if (strict)
1992 return is_native_int || is_int4;
1993 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1996 case MONO_TYPE_I8:
1997 case MONO_TYPE_U8:
1998 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2000 case MONO_TYPE_R4:
2001 case MONO_TYPE_R8:
2002 if (strict)
2003 return candidate->type == target->type;
2004 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2006 case MONO_TYPE_I:
2007 case MONO_TYPE_U: {
2008 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2009 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2010 if (strict)
2011 return is_native_int || is_int4;
2012 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2015 case MONO_TYPE_PTR:
2016 if (candidate->type != MONO_TYPE_PTR)
2017 return FALSE;
2018 /* check the underlying type */
2019 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2021 case MONO_TYPE_FNPTR: {
2022 MonoMethodSignature *left, *right;
2023 if (candidate->type != MONO_TYPE_FNPTR)
2024 return FALSE;
2026 left = mono_type_get_signature (target);
2027 right = mono_type_get_signature (candidate);
2028 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2031 case MONO_TYPE_GENERICINST: {
2032 MonoClass *target_klass;
2033 MonoClass *candidate_klass;
2034 if (mono_type_is_enum_type (target)) {
2035 target = mono_type_get_underlying_type_any (target);
2036 if (!target)
2037 return FALSE;
2038 goto handle_enum;
2041 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2042 * to take boxing status into account.
2044 if (mono_type_is_generic_argument (original_candidate))
2045 return FALSE;
2047 target_klass = mono_class_from_mono_type (target);
2048 candidate_klass = mono_class_from_mono_type (candidate);
2049 if (mono_class_is_nullable (target_klass)) {
2050 if (!mono_class_is_nullable (candidate_klass))
2051 return FALSE;
2052 return target_klass == candidate_klass;
2055 return mono_class_is_assignable_from (target_klass, candidate_klass);
2058 case MONO_TYPE_STRING:
2059 return candidate->type == MONO_TYPE_STRING;
2061 case MONO_TYPE_CLASS:
2063 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2064 * to take boxing status into account.
2066 if (mono_type_is_generic_argument (original_candidate))
2067 return FALSE;
2069 if (candidate->type == MONO_TYPE_VALUETYPE)
2070 return FALSE;
2072 /* If candidate is an enum it should return true for System.Enum and supertypes.
2073 * That's why here we use the original type and not the underlying type.
2075 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2077 case MONO_TYPE_OBJECT:
2078 return MONO_TYPE_IS_REFERENCE (candidate);
2080 case MONO_TYPE_SZARRAY: {
2081 MonoClass *left;
2082 MonoClass *right;
2083 if (candidate->type != MONO_TYPE_SZARRAY)
2084 return FALSE;
2086 left = mono_class_from_mono_type (target);
2087 right = mono_class_from_mono_type (candidate);
2089 return mono_class_is_assignable_from (left, right);
2092 case MONO_TYPE_ARRAY:
2093 if (candidate->type != MONO_TYPE_ARRAY)
2094 return FALSE;
2095 return is_array_type_compatible (target, candidate);
2097 case MONO_TYPE_TYPEDBYREF:
2098 return candidate->type == MONO_TYPE_TYPEDBYREF;
2100 case MONO_TYPE_VALUETYPE: {
2101 MonoClass *target_klass;
2102 MonoClass *candidate_klass;
2104 if (candidate->type == MONO_TYPE_CLASS)
2105 return FALSE;
2107 target_klass = mono_class_from_mono_type (target);
2108 candidate_klass = mono_class_from_mono_type (candidate);
2109 if (target_klass == candidate_klass)
2110 return TRUE;
2111 if (mono_type_is_enum_type (target)) {
2112 target = mono_type_get_underlying_type_any (target);
2113 if (!target)
2114 return FALSE;
2115 goto handle_enum;
2117 return FALSE;
2120 case MONO_TYPE_VAR:
2121 if (candidate->type != MONO_TYPE_VAR)
2122 return FALSE;
2123 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2125 case MONO_TYPE_MVAR:
2126 if (candidate->type != MONO_TYPE_MVAR)
2127 return FALSE;
2128 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2130 default:
2131 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2132 g_assert_not_reached ();
2133 return FALSE;
2135 return 1;
2136 #undef IS_ONE_OF3
2137 #undef IS_ONE_OF2
2140 static gboolean
2141 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2143 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2147 * Returns the generic param bound to the context been verified.
2150 static MonoGenericParam*
2151 get_generic_param (VerifyContext *ctx, MonoType *param)
2153 guint16 param_num = mono_type_get_generic_param_num (param);
2154 if (param->type == MONO_TYPE_VAR) {
2155 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2156 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2157 return NULL;
2159 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2162 /*param must be a MVAR */
2163 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2164 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2165 return NULL;
2167 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2171 static gboolean
2172 recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
2174 MonoType *constraint_type = &constraint_class->byval_arg;
2175 if (recursion_level <= 0)
2176 return FALSE;
2178 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
2179 return TRUE;
2181 if (mono_type_is_generic_argument (constraint_type)) {
2182 MonoGenericParam *param = get_generic_param (ctx, constraint_type);
2183 MonoClass **class;
2184 if (!param)
2185 return FALSE;
2186 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2187 if (recursive_boxed_constraint_type_check (ctx, type, *class, recursion_level - 1))
2188 return TRUE;
2191 return FALSE;
2195 * is_compatible_boxed_valuetype:
2197 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2199 * @type The source type. It it tested to be of the proper type.
2200 * @candidate type of the boxed valuetype.
2201 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2202 * @strict if TRUE candidate must be boxed compatible to the target type
2205 static gboolean
2206 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2208 if (!stack_slot_is_boxed_value (stack))
2209 return FALSE;
2210 if (type->byref || candidate->byref)
2211 return FALSE;
2213 if (mono_type_is_generic_argument (candidate)) {
2214 MonoGenericParam *param = get_generic_param (ctx, candidate);
2215 MonoClass **class;
2216 if (!param)
2217 return FALSE;
2219 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2220 /*256 should be enough since there can't be more than 255 generic arguments.*/
2221 if (recursive_boxed_constraint_type_check (ctx, type, *class, 256))
2222 return TRUE;
2226 if (mono_type_is_generic_argument (type))
2227 return FALSE;
2229 if (!strict)
2230 return TRUE;
2232 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2235 static int
2236 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2238 MonoType *candidate = mono_type_from_stack_slot (stack);
2239 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2240 return TRUE;
2242 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2243 return TRUE;
2245 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2246 return FALSE;
2248 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2249 return FALSE;
2251 if (drop_byref)
2252 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2254 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2257 static int
2258 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2260 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2263 static gboolean
2264 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2266 if (candidate->byref ^ target->byref)
2267 return FALSE;
2269 switch (target->type) {
2270 case MONO_TYPE_VOID:
2271 case MONO_TYPE_I1:
2272 case MONO_TYPE_U1:
2273 case MONO_TYPE_BOOLEAN:
2274 case MONO_TYPE_I2:
2275 case MONO_TYPE_U2:
2276 case MONO_TYPE_CHAR:
2277 case MONO_TYPE_I4:
2278 case MONO_TYPE_U4:
2279 case MONO_TYPE_I8:
2280 case MONO_TYPE_U8:
2281 case MONO_TYPE_R4:
2282 case MONO_TYPE_R8:
2283 case MONO_TYPE_I:
2284 case MONO_TYPE_U:
2285 case MONO_TYPE_STRING:
2286 case MONO_TYPE_TYPEDBYREF:
2287 return candidate->type == target->type;
2289 case MONO_TYPE_PTR:
2290 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2292 case MONO_TYPE_FNPTR:
2293 if (candidate->type != MONO_TYPE_FNPTR)
2294 return FALSE;
2295 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2297 case MONO_TYPE_GENERICINST: {
2298 MonoClass *target_klass;
2299 MonoClass *candidate_klass;
2300 target_klass = mono_class_from_mono_type (target);
2301 candidate_klass = mono_class_from_mono_type (candidate);
2302 /*FIXME handle nullables and enum*/
2303 return mono_class_is_assignable_from (target_klass, candidate_klass);
2305 case MONO_TYPE_OBJECT:
2306 return MONO_TYPE_IS_REFERENCE (candidate);
2308 case MONO_TYPE_CLASS:
2309 return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2311 case MONO_TYPE_SZARRAY:
2312 if (candidate->type != MONO_TYPE_SZARRAY)
2313 return FALSE;
2314 return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2316 case MONO_TYPE_ARRAY:
2317 if (candidate->type != MONO_TYPE_ARRAY)
2318 return FALSE;
2319 return is_array_type_compatible (target, candidate);
2321 case MONO_TYPE_VALUETYPE:
2322 /*FIXME handle nullables and enum*/
2323 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2325 case MONO_TYPE_VAR:
2326 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2327 return FALSE;
2329 case MONO_TYPE_MVAR:
2330 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2331 return FALSE;
2333 default:
2334 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2335 g_assert_not_reached ();
2336 return FALSE;
2340 static gboolean
2341 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2343 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2344 return TRUE;
2346 return mono_delegate_type_equal (method, delegate);
2349 static gboolean
2350 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2352 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2353 return TRUE;
2355 return mono_delegate_type_equal (delegate, method);
2359 * mono_delegate_signature_equal:
2361 * Compare two signatures in the way expected by delegates.
2363 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2365 * FIXME can this function be eliminated and proper metadata functionality be used?
2367 static gboolean
2368 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2370 int i;
2371 int method_offset = is_static_ldftn ? 1 : 0;
2373 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2374 return FALSE;
2376 if (delegate_sig->call_convention != method_sig->call_convention)
2377 return FALSE;
2379 for (i = 0; i < delegate_sig->param_count; i++) {
2380 MonoType *p1 = delegate_sig->params [i];
2381 MonoType *p2 = method_sig->params [i + method_offset];
2383 if (!mono_delegate_param_equal (p1, p2))
2384 return FALSE;
2387 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2388 return FALSE;
2390 return TRUE;
2394 * verify_ldftn_delegate:
2396 * Verify properties of ldftn based delegates.
2398 static void
2399 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2401 MonoMethod *method = funptr->method;
2403 /*ldftn non-final virtuals only allowed if method is not static,
2404 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2405 * This rules doesn't apply if the object on stack is a boxed valuetype.
2407 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2408 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2409 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2410 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2412 /*current method must not be static*/
2413 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2414 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2416 /*value is the this pointer, loaded using ldarg.0 */
2417 if (!stack_slot_is_this_pointer (value))
2418 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object argument, it is not the this pointer, to ldftn with virtual method at 0x%04x", ctx->ip_offset));
2420 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2425 * verify_delegate_compatibility:
2427 * Verify delegate creation sequence.
2430 static void
2431 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2433 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2434 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2436 MonoMethod *invoke, *method;
2437 const guint8 *ip = ctx->header->code;
2438 guint32 ip_offset = ctx->ip_offset;
2439 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2441 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2442 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2443 return;
2446 invoke = mono_get_delegate_invoke (delegate);
2447 method = funptr->method;
2449 if (!method || !mono_method_signature (method)) {
2450 char *name = mono_type_get_full_name (delegate);
2451 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2452 g_free (name);
2453 return;
2456 if (!invoke || !mono_method_signature (invoke)) {
2457 char *name = mono_type_get_full_name (delegate);
2458 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2459 g_free (name);
2460 return;
2463 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2465 if (is_static_ldftn)
2466 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2468 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2469 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2470 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2471 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer signature '%s' doesn't match delegate's signature '%s' at 0x%04x", fun_sig, invoke_sig, ctx->ip_offset));
2472 g_free (fun_sig);
2473 g_free (invoke_sig);
2477 * Delegate code sequences:
2478 * [-6] ldftn token
2479 * newobj ...
2482 * [-7] dup
2483 * [-6] ldvirtftn token
2484 * newobj ...
2486 * ldftn sequence:*/
2487 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2488 verify_ldftn_delegate (ctx, delegate, value, funptr);
2489 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2490 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2491 }else {
2492 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2494 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2496 //general tests
2497 if (is_first_arg_bound) {
2498 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2499 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2500 } else {
2501 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2502 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2503 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2504 } else {
2505 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2506 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2510 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2511 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2513 #undef IS_VALID_OPCODE
2514 #undef IS_LOAD_FUN_PTR
2517 /* implement the opcode checks*/
2518 static void
2519 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2521 ILStackDesc *top;
2523 if (arg >= ctx->max_args) {
2524 if (take_addr)
2525 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2526 else {
2527 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2528 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2529 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2531 } else if (check_overflow (ctx)) {
2532 /*We must let the value be pushed, otherwise we would get an underflow error*/
2533 check_unverifiable_type (ctx, ctx->params [arg]);
2534 if (ctx->params [arg]->byref && take_addr)
2535 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2536 top = stack_push (ctx);
2537 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2538 return;
2540 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2541 if (take_addr)
2542 ctx->has_this_store = TRUE;
2543 else
2544 top->stype |= THIS_POINTER_MASK;
2545 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2546 top->stype |= UNINIT_THIS_MASK;
2551 static void
2552 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2554 if (arg >= ctx->num_locals) {
2555 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2556 } else if (check_overflow (ctx)) {
2557 /*We must let the value be pushed, otherwise we would get an underflow error*/
2558 check_unverifiable_type (ctx, ctx->locals [arg]);
2559 if (ctx->locals [arg]->byref && take_addr)
2560 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2562 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2566 static void
2567 store_arg (VerifyContext *ctx, guint32 arg)
2569 ILStackDesc *value;
2571 if (arg >= ctx->max_args) {
2572 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2573 if (check_underflow (ctx, 1))
2574 stack_pop (ctx);
2575 return;
2578 if (check_underflow (ctx, 1)) {
2579 value = stack_pop (ctx);
2580 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2581 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2584 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2585 ctx->has_this_store = 1;
2588 static void
2589 store_local (VerifyContext *ctx, guint32 arg)
2591 ILStackDesc *value;
2592 if (arg >= ctx->num_locals) {
2593 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2594 return;
2597 /*TODO verify definite assigment */
2598 if (check_underflow (ctx, 1)) {
2599 value = stack_pop(ctx);
2600 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2601 char *expected = mono_type_full_name (ctx->locals [arg]);
2602 char *found = stack_slot_full_name (value);
2603 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2604 found,
2605 arg,
2606 expected,
2607 ctx->ip_offset));
2608 g_free (expected);
2609 g_free (found);
2614 /*FIXME add and sub needs special care here*/
2615 static void
2616 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2618 ILStackDesc *a, *b, *top;
2619 int idxa, idxb, complexMerge = 0;
2620 unsigned char res;
2622 if (!check_underflow (ctx, 2))
2623 return;
2624 b = stack_pop (ctx);
2625 a = stack_pop (ctx);
2627 idxa = stack_slot_get_underlying_type (a);
2628 if (stack_slot_is_managed_pointer (a)) {
2629 idxa = TYPE_PTR;
2630 complexMerge = 1;
2633 idxb = stack_slot_get_underlying_type (b);
2634 if (stack_slot_is_managed_pointer (b)) {
2635 idxb = TYPE_PTR;
2636 complexMerge = 2;
2639 --idxa;
2640 --idxb;
2641 res = table [idxa][idxb];
2643 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2644 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2646 top = stack_push (ctx);
2647 if (res == TYPE_INV) {
2648 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction applyed to ill formed stack (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
2649 copy_stack_value (top, a);
2650 return;
2653 if (res & NON_VERIFIABLE_RESULT) {
2654 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
2656 res = res & ~NON_VERIFIABLE_RESULT;
2659 if (complexMerge && res == TYPE_PTR) {
2660 if (complexMerge == 1)
2661 copy_stack_value (top, a);
2662 else if (complexMerge == 2)
2663 copy_stack_value (top, b);
2665 * There is no need to merge the type of two pointers.
2666 * The only valid operation is subtraction, that returns a native
2667 * int as result and can be used with any 2 pointer kinds.
2668 * This is valid acording to Patition III 1.1.4
2670 } else
2671 top->stype = res;
2676 static void
2677 do_boolean_branch_op (VerifyContext *ctx, int delta)
2679 int target = ctx->ip_offset + delta;
2680 ILStackDesc *top;
2682 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2684 if (target < 0 || target >= ctx->code_size) {
2685 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2686 return;
2689 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2690 case 1:
2691 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2692 break;
2693 case 2:
2694 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2695 return;
2698 ctx->target = target;
2700 if (!check_underflow (ctx, 1))
2701 return;
2703 top = stack_pop (ctx);
2704 if (!is_valid_bool_arg (top))
2705 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
2707 check_unmanaged_pointer (ctx, top);
2710 static gboolean
2711 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2713 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2716 static void
2717 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2719 ILStackDesc *a, *b;
2720 int idxa, idxb;
2721 unsigned char res;
2722 int target = ctx->ip_offset + delta;
2724 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2726 if (target < 0 || target >= ctx->code_size) {
2727 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2728 return;
2731 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2732 case 1: /*FIXME use constants and not magic numbers.*/
2733 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2734 break;
2735 case 2:
2736 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2737 return;
2740 ctx->target = target;
2742 if (!check_underflow (ctx, 2))
2743 return;
2745 b = stack_pop (ctx);
2746 a = stack_pop (ctx);
2748 idxa = stack_slot_get_underlying_type (a);
2749 if (stack_slot_is_managed_pointer (a))
2750 idxa = TYPE_PTR;
2752 idxb = stack_slot_get_underlying_type (b);
2753 if (stack_slot_is_managed_pointer (b))
2754 idxb = TYPE_PTR;
2756 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2757 res = TYPE_INV;
2758 } else {
2759 --idxa;
2760 --idxb;
2761 res = table [idxa][idxb];
2764 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2765 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2767 if (res == TYPE_INV) {
2768 CODE_NOT_VERIFIABLE (ctx,
2769 g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2770 } else if (res & NON_VERIFIABLE_RESULT) {
2771 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2772 res = res & ~NON_VERIFIABLE_RESULT;
2776 static void
2777 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2779 ILStackDesc *a, *b;
2780 int idxa, idxb;
2781 unsigned char res;
2783 if (!check_underflow (ctx, 2))
2784 return;
2785 b = stack_pop (ctx);
2786 a = stack_pop (ctx);
2788 if (opcode == CEE_CGT_UN) {
2789 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2790 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2791 return;
2795 idxa = stack_slot_get_underlying_type (a);
2796 if (stack_slot_is_managed_pointer (a))
2797 idxa = TYPE_PTR;
2799 idxb = stack_slot_get_underlying_type (b);
2800 if (stack_slot_is_managed_pointer (b))
2801 idxb = TYPE_PTR;
2803 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2804 res = TYPE_INV;
2805 } else {
2806 --idxa;
2807 --idxb;
2808 res = table [idxa][idxb];
2811 if(res == TYPE_INV) {
2812 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2813 } else if (res & NON_VERIFIABLE_RESULT) {
2814 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2815 res = res & ~NON_VERIFIABLE_RESULT;
2817 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2820 static void
2821 do_ret (VerifyContext *ctx)
2823 MonoType *ret = ctx->signature->ret;
2824 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2825 if (ret->type != MONO_TYPE_VOID) {
2826 ILStackDesc *top;
2827 if (!check_underflow (ctx, 1))
2828 return;
2830 top = stack_pop(ctx);
2832 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2833 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2834 return;
2837 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
2838 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
2841 if (ctx->eval.size > 0) {
2842 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2844 if (in_any_block (ctx->header, ctx->ip_offset))
2845 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2849 * FIXME we need to fix the case of a non-virtual instance method defined in the parent but call using a token pointing to a subclass.
2850 * This is illegal but mono_get_method_full decoded it.
2851 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2853 static void
2854 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
2856 int param_count, i;
2857 MonoMethodSignature *sig;
2858 ILStackDesc *value;
2859 MonoMethod *method;
2860 gboolean virt_check_this = FALSE;
2861 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
2863 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
2864 return;
2866 if (virtual) {
2867 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
2869 if (method->klass->valuetype) // && !constrained ???
2870 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
2872 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
2873 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
2875 } else {
2876 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
2877 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
2879 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
2880 virt_check_this = TRUE;
2881 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
2885 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
2886 sig = mono_method_get_signature (method, ctx->image, method_token);
2888 if (!sig) {
2889 char *name = mono_type_get_full_name (method->klass);
2890 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name, method->name, ctx->ip_offset));
2891 g_free (name);
2892 return;
2895 param_count = sig->param_count + sig->hasthis;
2896 if (!check_underflow (ctx, param_count))
2897 return;
2899 for (i = sig->param_count - 1; i >= 0; --i) {
2900 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2901 value = stack_pop (ctx);
2902 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
2903 char *stack_name = stack_slot_full_name (value);
2904 char *sig_name = mono_type_full_name (sig->params [i]);
2905 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter with function signature: Calling method with signature (%s) but for argument %d there is a (%s) on stack at 0x%04x", sig_name, i, stack_name, ctx->ip_offset));
2906 g_free (stack_name);
2907 g_free (sig_name);
2910 if (stack_slot_is_managed_mutability_pointer (value))
2911 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of %s at 0x%04x", virtual ? "callvirt" : "call", ctx->ip_offset));
2913 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
2914 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot pass a byref argument to a tail %s at 0x%04x", virtual ? "callvirt" : "call", ctx->ip_offset));
2915 return;
2919 if (sig->hasthis) {
2920 MonoType *type = &method->klass->byval_arg;
2921 ILStackDesc copy;
2923 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
2924 if (!mono_method_is_constructor (ctx->method))
2925 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
2926 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
2927 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor to a type diferent that this or super at 0x%04x", ctx->ip_offset));
2929 ctx->super_ctor_called = TRUE;
2930 value = stack_pop_safe (ctx);
2931 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
2932 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
2933 } else {
2934 value = stack_pop (ctx);
2937 copy_stack_value (&copy, value);
2938 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2939 //Other parts of the code suffer from the same issue of
2940 copy.type = mono_type_get_type_byval (copy.type);
2941 copy.stype &= ~POINTER_MASK;
2943 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
2944 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the call opcode with a non-final virtual method on an object diferent thant the this pointer at 0x%04x", ctx->ip_offset));
2946 if (constrained && virtual) {
2947 if (!stack_slot_is_managed_pointer (value))
2948 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
2949 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
2950 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
2951 copy.stype |= BOXED_MASK;
2952 } else {
2953 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
2954 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a reference type using a managed pointer to the this arg at 0x%04x", ctx->ip_offset));
2956 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
2957 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
2959 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
2960 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
2962 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
2963 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a boxed or literal valuetype to call a valuetype method at 0x%04x", ctx->ip_offset));
2965 if (!verify_stack_type_compatibility (ctx, type, &copy)) {
2966 char *expected = mono_type_full_name (type);
2967 char *effective = stack_slot_full_name (&copy);
2968 char *method_name = mono_method_full_name (method, TRUE);
2969 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature expected '%s' but got '%s' for a call to '%s' at 0x%04x",
2970 expected, effective, method_name, ctx->ip_offset));
2971 g_free (method_name);
2972 g_free (effective);
2973 g_free (expected);
2976 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
2977 char *name = mono_method_full_name (method, TRUE);
2978 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2979 g_free (name);
2982 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
2983 char *name = mono_method_full_name (method, TRUE);
2984 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2985 g_free (name);
2988 if (sig->ret->type != MONO_TYPE_VOID) {
2989 if (check_overflow (ctx)) {
2990 value = stack_push (ctx);
2991 set_stack_value (ctx, value, sig->ret, FALSE);
2992 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
2993 ctx->prefix_set &= ~PREFIX_READONLY;
2994 value->stype |= CMMP_MASK;
2999 if ((ctx->prefix_set & PREFIX_TAIL)) {
3000 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3001 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3002 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3003 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3008 static void
3009 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3011 MonoClassField *field;
3012 MonoClass *klass;
3013 if (!check_overflow (ctx))
3014 return;
3015 if (!take_addr)
3016 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3018 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3019 return;
3021 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3022 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3023 return;
3025 /*taking the address of initonly field only works from the static constructor */
3026 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3027 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3028 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3030 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3031 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3033 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3036 static void
3037 do_store_static_field (VerifyContext *ctx, int token) {
3038 MonoClassField *field;
3039 MonoClass *klass;
3040 ILStackDesc *value;
3041 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3043 if (!check_underflow (ctx, 1))
3044 return;
3046 value = stack_pop (ctx);
3048 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3049 return;
3051 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3052 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3053 return;
3056 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3057 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3058 return;
3061 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3062 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3064 if (!verify_stack_type_compatibility (ctx, field->type, value)) {
3065 char *stack_name = stack_slot_full_name (value);
3066 char *field_name = mono_type_full_name (field->type);
3067 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
3068 field_name, stack_name, ctx->ip_offset));
3069 g_free (field_name);
3070 g_free (stack_name);
3074 static gboolean
3075 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3077 MonoClassField *field;
3078 MonoClass *klass;
3079 gboolean is_pointer;
3081 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3082 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3083 return FALSE;
3085 *ret_field = field;
3086 //the value on stack is going to be used as a pointer
3087 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3089 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3090 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3091 return FALSE;
3093 g_assert (obj->type);
3095 /*The value on the stack must be a subclass of the defining type of the field*/
3096 /* we need to check if we can load the field from the stack value*/
3097 if (is_pointer) {
3098 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3099 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3101 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3102 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3103 } else {
3104 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3105 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a managed pointer to a reference type and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3107 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3108 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3109 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a boxed valuetype and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3111 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3112 char *found = stack_slot_full_name (obj);
3113 char *expected = mono_type_full_name (&field->parent->byval_arg);
3114 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected type '%s' but found '%s' referencing the 'this' argument at 0x%04x", expected, found, ctx->ip_offset));
3115 g_free (found);
3116 g_free (expected);
3119 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3120 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3123 check_unmanaged_pointer (ctx, obj);
3124 return TRUE;
3127 static void
3128 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3130 ILStackDesc *obj;
3131 MonoClassField *field;
3133 if (!take_addr)
3134 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3136 if (!check_underflow (ctx, 1))
3137 return;
3138 obj = stack_pop_safe (ctx);
3140 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3141 return;
3143 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3144 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3146 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3147 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3148 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3150 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3153 static void
3154 do_store_field (VerifyContext *ctx, int token)
3156 ILStackDesc *value, *obj;
3157 MonoClassField *field;
3158 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3160 if (!check_underflow (ctx, 2))
3161 return;
3163 value = stack_pop (ctx);
3164 obj = stack_pop_safe (ctx);
3166 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3167 return;
3169 if (!verify_stack_type_compatibility (ctx, field->type, value))
3170 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3173 /*TODO proper handle for Nullable<T>*/
3174 static void
3175 do_box_value (VerifyContext *ctx, int klass_token)
3177 ILStackDesc *value;
3178 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3179 MonoClass *klass;
3181 if (!type)
3182 return;
3184 if (!check_underflow (ctx, 1))
3185 return;
3187 value = stack_pop (ctx);
3188 /*box is a nop for reference types*/
3190 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3191 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3192 return;
3196 if (!verify_stack_type_compatibility (ctx, type, value))
3197 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3199 klass = mono_class_from_mono_type (type);
3200 if (mono_class_is_nullable (klass))
3201 type = &mono_class_get_nullable_param (klass)->byval_arg;
3202 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3205 static void
3206 do_unbox_value (VerifyContext *ctx, int klass_token)
3208 ILStackDesc *value;
3209 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3211 if (!type)
3212 return;
3214 if (!check_underflow (ctx, 1))
3215 return;
3217 if (!mono_class_from_mono_type (type)->valuetype)
3218 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3220 value = stack_pop (ctx);
3222 /*Value should be: a boxed valuetype or a reference type*/
3223 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3224 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3225 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3227 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3228 value->stype |= CMMP_MASK;
3231 static void
3232 do_unbox_any (VerifyContext *ctx, int klass_token)
3234 ILStackDesc *value;
3235 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3237 if (!type)
3238 return;
3240 if (!check_underflow (ctx, 1))
3241 return;
3243 value = stack_pop (ctx);
3245 /*Value should be: a boxed valuetype or a reference type*/
3246 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3247 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3248 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox.any operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3250 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3253 static void
3254 do_unary_math_op (VerifyContext *ctx, int op)
3256 ILStackDesc *value;
3257 if (!check_underflow (ctx, 1))
3258 return;
3259 value = stack_pop (ctx);
3260 switch (stack_slot_get_type (value)) {
3261 case TYPE_I4:
3262 case TYPE_I8:
3263 case TYPE_NATIVE_INT:
3264 break;
3265 case TYPE_R8:
3266 if (op == CEE_NEG)
3267 break;
3268 case TYPE_COMPLEX: /*only enums are ok*/
3269 if (mono_type_is_enum_type (value->type))
3270 break;
3271 default:
3272 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3274 stack_push_stack_val (ctx, value);
3277 static void
3278 do_conversion (VerifyContext *ctx, int kind)
3280 ILStackDesc *value;
3281 if (!check_underflow (ctx, 1))
3282 return;
3283 value = stack_pop (ctx);
3285 switch (stack_slot_get_type (value)) {
3286 case TYPE_I4:
3287 case TYPE_I8:
3288 case TYPE_NATIVE_INT:
3289 case TYPE_R8:
3290 break;
3291 default:
3292 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type (%s) at stack for conversion operation. Numeric type expected at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3295 switch (kind) {
3296 case TYPE_I4:
3297 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3298 break;
3299 case TYPE_I8:
3300 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3301 break;
3302 case TYPE_R8:
3303 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3304 break;
3305 case TYPE_NATIVE_INT:
3306 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3307 break;
3308 default:
3309 g_error ("unknown type %02x in conversion", kind);
3314 static void
3315 do_load_token (VerifyContext *ctx, int token)
3317 gpointer handle;
3318 MonoClass *handle_class;
3319 if (!check_overflow (ctx))
3320 return;
3322 switch (token & 0xff000000) {
3323 case MONO_TOKEN_TYPE_DEF:
3324 case MONO_TOKEN_TYPE_REF:
3325 case MONO_TOKEN_TYPE_SPEC:
3326 case MONO_TOKEN_FIELD_DEF:
3327 case MONO_TOKEN_METHOD_DEF:
3328 case MONO_TOKEN_METHOD_SPEC:
3329 case MONO_TOKEN_MEMBER_REF:
3330 if (!token_bounds_check (ctx->image, token)) {
3331 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Table index out of range 0x%x for token %x for ldtoken at 0x%04x", mono_metadata_token_index (token), token, ctx->ip_offset));
3332 return;
3334 break;
3335 default:
3336 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid table 0x%x for token 0x%x for ldtoken at 0x%04x", mono_metadata_token_table (token), token, ctx->ip_offset));
3337 return;
3340 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3341 if (!handle) {
3342 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3343 return;
3345 if (handle_class == mono_defaults.typehandle_class) {
3346 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3347 } else if (handle_class == mono_defaults.methodhandle_class) {
3348 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3349 } else if (handle_class == mono_defaults.fieldhandle_class) {
3350 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3351 } else {
3352 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3354 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3357 static void
3358 do_ldobj_value (VerifyContext *ctx, int token)
3360 ILStackDesc *value;
3361 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3362 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3364 if (!type)
3365 return;
3367 if (!check_underflow (ctx, 1))
3368 return;
3370 value = stack_pop (ctx);
3371 if (!stack_slot_is_managed_pointer (value)
3372 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3373 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3374 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3375 return;
3378 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3379 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3381 /*We have a byval on the stack, but the comparison must be strict. */
3382 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3383 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3385 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3388 static void
3389 do_stobj (VerifyContext *ctx, int token)
3391 ILStackDesc *dest, *src;
3392 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3393 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3395 if (!type)
3396 return;
3398 if (!check_underflow (ctx, 2))
3399 return;
3401 src = stack_pop (ctx);
3402 dest = stack_pop (ctx);
3404 if (stack_slot_is_managed_mutability_pointer (dest))
3405 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3407 if (!stack_slot_is_managed_pointer (dest))
3408 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3410 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3411 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
3413 if (!verify_stack_type_compatibility (ctx, type, src)) {
3414 char *type_name = mono_type_full_name (type);
3415 char *src_name = stack_slot_full_name (src);
3416 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token '%s' and source '%s' of stobj don't match ' at 0x%04x", type_name, src_name, ctx->ip_offset));
3417 g_free (type_name);
3418 g_free (src_name);
3421 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3422 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3425 static void
3426 do_cpobj (VerifyContext *ctx, int token)
3428 ILStackDesc *dest, *src;
3429 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3430 if (!type)
3431 return;
3433 if (!check_underflow (ctx, 2))
3434 return;
3436 src = stack_pop (ctx);
3437 dest = stack_pop (ctx);
3439 if (!stack_slot_is_managed_pointer (src))
3440 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3442 if (!stack_slot_is_managed_pointer (dest))
3443 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3445 if (stack_slot_is_managed_mutability_pointer (dest))
3446 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3448 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3449 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3451 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3452 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3455 static void
3456 do_initobj (VerifyContext *ctx, int token)
3458 ILStackDesc *obj;
3459 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3460 if (!type)
3461 return;
3463 if (!check_underflow (ctx, 1))
3464 return;
3466 obj = stack_pop (ctx);
3468 if (!stack_slot_is_managed_pointer (obj))
3469 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3471 if (stack_slot_is_managed_mutability_pointer (obj))
3472 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3474 stack = mono_type_get_type_byval (obj->type);
3475 if (MONO_TYPE_IS_REFERENCE (stack)) {
3476 if (!verify_type_compatibility (ctx, stack, type))
3477 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3478 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3479 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3480 } else if (!verify_type_compatibility (ctx, stack, type)) {
3481 char *expected_name = mono_type_full_name (type);
3482 char *stack_name = mono_type_full_name (stack);
3484 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Initobj %s not compatible with value on stack %s at 0x%04x", expected_name, stack_name, ctx->ip_offset));
3485 g_free (expected_name);
3486 g_free (stack_name);
3490 static void
3491 do_newobj (VerifyContext *ctx, int token)
3493 ILStackDesc *value;
3494 int i;
3495 MonoMethodSignature *sig;
3496 MonoMethod *method;
3497 gboolean is_delegate = FALSE;
3499 if (!(method = verifier_load_method (ctx, token, "newobj")))
3500 return;
3502 if (!mono_method_is_constructor (method)) {
3503 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3504 return;
3507 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3508 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3510 if (!mono_method_can_access_method_full (ctx->method, method, NULL)) {
3511 char *from = mono_method_full_name (ctx->method, TRUE);
3512 char *to = mono_method_full_name (method, TRUE);
3513 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor %s not visible from %s at 0x%04x", to, from, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3514 g_free (from);
3515 g_free (to);
3518 //FIXME use mono_method_get_signature_full
3519 sig = mono_method_signature (method);
3520 if (!sig) {
3521 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3522 return;
3525 if (!check_underflow (ctx, sig->param_count))
3526 return;
3528 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3530 if (is_delegate) {
3531 ILStackDesc *funptr;
3532 //first arg is object, second arg is fun ptr
3533 if (sig->param_count != 2) {
3534 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3535 return;
3537 funptr = stack_pop (ctx);
3538 value = stack_pop (ctx);
3539 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3540 } else {
3541 for (i = sig->param_count - 1; i >= 0; --i) {
3542 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3543 value = stack_pop (ctx);
3544 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3545 char *stack_name = stack_slot_full_name (value);
3546 char *sig_name = mono_type_full_name (sig->params [i]);
3547 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with constructor signature: %s X %s at 0x%04x", sig_name, stack_name, ctx->ip_offset));
3548 g_free (stack_name);
3549 g_free (sig_name);
3552 if (stack_slot_is_managed_mutability_pointer (value))
3553 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3557 if (check_overflow (ctx))
3558 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3561 static void
3562 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3563 ILStackDesc *value;
3564 MonoType *type;
3565 gboolean is_boxed;
3566 gboolean do_box;
3568 if (!check_underflow (ctx, 1))
3569 return;
3571 if (!(type = verifier_load_type (ctx, token, opcode)))
3572 return;
3574 if (type->byref) {
3575 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3576 return;
3579 value = stack_pop (ctx);
3580 is_boxed = stack_slot_is_boxed_value (value);
3582 if (stack_slot_is_managed_pointer (value))
3583 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3584 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3585 char *name = stack_slot_full_name (value);
3586 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a reference type on stack for %s but found %s at 0x%04x", opcode, name, ctx->ip_offset));
3587 g_free (name);
3590 switch (value->type->type) {
3591 case MONO_TYPE_FNPTR:
3592 case MONO_TYPE_PTR:
3593 case MONO_TYPE_TYPEDBYREF:
3594 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3597 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3598 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3601 static MonoType *
3602 mono_type_from_opcode (int opcode) {
3603 switch (opcode) {
3604 case CEE_LDIND_I1:
3605 case CEE_LDIND_U1:
3606 case CEE_STIND_I1:
3607 case CEE_LDELEM_I1:
3608 case CEE_LDELEM_U1:
3609 case CEE_STELEM_I1:
3610 return &mono_defaults.sbyte_class->byval_arg;
3612 case CEE_LDIND_I2:
3613 case CEE_LDIND_U2:
3614 case CEE_STIND_I2:
3615 case CEE_LDELEM_I2:
3616 case CEE_LDELEM_U2:
3617 case CEE_STELEM_I2:
3618 return &mono_defaults.int16_class->byval_arg;
3620 case CEE_LDIND_I4:
3621 case CEE_LDIND_U4:
3622 case CEE_STIND_I4:
3623 case CEE_LDELEM_I4:
3624 case CEE_LDELEM_U4:
3625 case CEE_STELEM_I4:
3626 return &mono_defaults.int32_class->byval_arg;
3628 case CEE_LDIND_I8:
3629 case CEE_STIND_I8:
3630 case CEE_LDELEM_I8:
3631 case CEE_STELEM_I8:
3632 return &mono_defaults.int64_class->byval_arg;
3634 case CEE_LDIND_R4:
3635 case CEE_STIND_R4:
3636 case CEE_LDELEM_R4:
3637 case CEE_STELEM_R4:
3638 return &mono_defaults.single_class->byval_arg;
3640 case CEE_LDIND_R8:
3641 case CEE_STIND_R8:
3642 case CEE_LDELEM_R8:
3643 case CEE_STELEM_R8:
3644 return &mono_defaults.double_class->byval_arg;
3646 case CEE_LDIND_I:
3647 case CEE_STIND_I:
3648 case CEE_LDELEM_I:
3649 case CEE_STELEM_I:
3650 return &mono_defaults.int_class->byval_arg;
3652 case CEE_LDIND_REF:
3653 case CEE_STIND_REF:
3654 case CEE_LDELEM_REF:
3655 case CEE_STELEM_REF:
3656 return &mono_defaults.object_class->byval_arg;
3658 default:
3659 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3660 return NULL;
3664 static void
3665 do_load_indirect (VerifyContext *ctx, int opcode)
3667 ILStackDesc *value;
3668 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3670 if (!check_underflow (ctx, 1))
3671 return;
3673 value = stack_pop (ctx);
3674 if (!stack_slot_is_managed_pointer (value)) {
3675 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3676 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3677 return;
3680 if (opcode == CEE_LDIND_REF) {
3681 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3682 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3683 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3684 } else {
3685 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3686 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3687 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3691 static void
3692 do_store_indirect (VerifyContext *ctx, int opcode)
3694 ILStackDesc *addr, *val;
3695 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3697 if (!check_underflow (ctx, 2))
3698 return;
3700 val = stack_pop (ctx);
3701 addr = stack_pop (ctx);
3703 check_unmanaged_pointer (ctx, addr);
3705 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3706 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3707 return;
3710 if (stack_slot_is_managed_mutability_pointer (addr)) {
3711 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3712 return;
3715 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3716 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3718 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3719 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3722 static void
3723 do_newarr (VerifyContext *ctx, int token)
3725 ILStackDesc *value;
3726 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3728 if (!type)
3729 return;
3731 if (!check_underflow (ctx, 1))
3732 return;
3734 value = stack_pop (ctx);
3735 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3736 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Array size type on stack (%s) is not a verifiable type at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3738 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3741 /*FIXME handle arrays that are not 0-indexed*/
3742 static void
3743 do_ldlen (VerifyContext *ctx)
3745 ILStackDesc *value;
3747 if (!check_underflow (ctx, 1))
3748 return;
3750 value = stack_pop (ctx);
3752 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3753 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3755 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3758 /*FIXME handle arrays that are not 0-indexed*/
3759 /*FIXME handle readonly prefix and CMMP*/
3760 static void
3761 do_ldelema (VerifyContext *ctx, int klass_token)
3763 ILStackDesc *index, *array, *res;
3764 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
3765 gboolean valid;
3767 if (!type)
3768 return;
3770 if (!check_underflow (ctx, 2))
3771 return;
3773 index = stack_pop (ctx);
3774 array = stack_pop (ctx);
3776 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3777 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelema is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
3779 if (!stack_slot_is_null_literal (array)) {
3780 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3781 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3782 else {
3783 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3784 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3785 } else {
3786 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3788 if (!valid)
3789 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3793 res = stack_push (ctx);
3794 set_stack_value (ctx, res, type, TRUE);
3795 if (ctx->prefix_set & PREFIX_READONLY) {
3796 ctx->prefix_set &= ~PREFIX_READONLY;
3797 res->stype |= CMMP_MASK;
3802 * FIXME handle arrays that are not 0-indexed
3803 * FIXME handle readonly prefix and CMMP
3805 static void
3806 do_ldelem (VerifyContext *ctx, int opcode, int token)
3808 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3809 ILStackDesc *index, *array;
3810 MonoType *type;
3811 if (!check_underflow (ctx, 2))
3812 return;
3814 if (opcode == CEE_LDELEM) {
3815 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
3816 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3817 return;
3819 } else {
3820 type = mono_type_from_opcode (opcode);
3823 index = stack_pop (ctx);
3824 array = stack_pop (ctx);
3826 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3827 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
3829 if (!stack_slot_is_null_literal (array)) {
3830 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3831 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3832 else {
3833 if (opcode == CEE_LDELEM_REF) {
3834 if (array->type->data.klass->valuetype)
3835 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3836 type = &array->type->data.klass->byval_arg;
3837 } else {
3838 MonoType *candidate = &array->type->data.klass->byval_arg;
3839 if (IS_STRICT_MODE (ctx)) {
3840 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
3841 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
3842 if ((IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I, MONO_TYPE_U)) ||
3843 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
3844 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3846 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
3847 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3852 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3853 #undef IS_ONE_OF2
3857 * FIXME handle arrays that are not 0-indexed
3859 static void
3860 do_stelem (VerifyContext *ctx, int opcode, int token)
3862 ILStackDesc *index, *array, *value;
3863 MonoType *type;
3864 if (!check_underflow (ctx, 3))
3865 return;
3867 if (opcode == CEE_STELEM) {
3868 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
3869 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3870 return;
3872 } else {
3873 type = mono_type_from_opcode (opcode);
3876 value = stack_pop (ctx);
3877 index = stack_pop (ctx);
3878 array = stack_pop (ctx);
3880 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3881 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for stdelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
3883 if (!stack_slot_is_null_literal (array)) {
3884 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3885 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for stelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3886 } else {
3887 if (opcode == CEE_STELEM_REF) {
3888 if (array->type->data.klass->valuetype)
3889 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3890 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3891 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3895 if (opcode == CEE_STELEM_REF) {
3896 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3897 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3898 } else if (opcode != CEE_STELEM_REF) {
3899 if (!verify_stack_type_compatibility (ctx, type, value))
3900 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3902 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
3903 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
3908 static void
3909 do_throw (VerifyContext *ctx)
3911 ILStackDesc *exception;
3912 if (!check_underflow (ctx, 1))
3913 return;
3914 exception = stack_pop (ctx);
3916 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3917 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3919 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
3920 char *name = mono_type_full_name (exception->type);
3921 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type but found unboxed %s at 0x%04x ", name, ctx->ip_offset));
3922 g_free (name);
3924 /*The stack is left empty after a throw*/
3925 ctx->eval.size = 0;
3929 static void
3930 do_endfilter (VerifyContext *ctx)
3932 MonoExceptionClause *clause;
3934 if (IS_STRICT_MODE (ctx)) {
3935 if (ctx->eval.size != 1)
3936 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3938 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
3939 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3942 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3943 if (IS_STRICT_MODE (ctx)) {
3944 if (ctx->ip_offset != clause->handler_offset - 2)
3945 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3946 } else {
3947 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3948 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3950 } else {
3951 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3952 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3953 else
3954 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3957 ctx->eval.size = 0;
3960 static void
3961 do_leave (VerifyContext *ctx, int delta)
3963 int target = ((gint32)ctx->ip_offset) + delta;
3964 if (target >= ctx->code_size || target < 0)
3965 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3967 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
3968 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
3969 ctx->eval.size = 0;
3973 * do_static_branch:
3975 * Verify br and br.s opcodes.
3977 static void
3978 do_static_branch (VerifyContext *ctx, int delta)
3980 int target = ctx->ip_offset + delta;
3981 if (target < 0 || target >= ctx->code_size) {
3982 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
3983 return;
3986 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3987 case 1:
3988 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3989 break;
3990 case 2:
3991 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3992 break;
3995 ctx->target = target;
3998 static void
3999 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4001 int i, base = ctx->ip_offset + 5 + count * 4;
4002 ILStackDesc *value;
4004 if (!check_underflow (ctx, 1))
4005 return;
4007 value = stack_pop (ctx);
4009 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4010 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4012 for (i = 0; i < count; ++i) {
4013 int target = base + read32 (data + i * 4);
4015 if (target < 0 || target >= ctx->code_size) {
4016 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4017 return;
4020 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4021 case 1:
4022 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4023 break;
4024 case 2:
4025 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4026 return;
4028 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4032 static void
4033 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4035 ILStackDesc *top;
4036 MonoMethod *method;
4038 if (virtual && !check_underflow (ctx, 1))
4039 return;
4041 if (!virtual && !check_overflow (ctx))
4042 return;
4044 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4045 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4046 return;
4049 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4050 return;
4052 if (mono_method_is_constructor (method))
4053 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4055 if (virtual) {
4056 ILStackDesc *top = stack_pop (ctx);
4058 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4059 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4061 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4062 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4064 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4065 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4068 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4069 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Loaded method is not visible for ldftn/ldvirtftn at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
4071 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4072 top->method = method;
4075 static void
4076 do_sizeof (VerifyContext *ctx, int token)
4078 MonoType *type;
4080 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4081 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4082 return;
4085 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4086 return;
4088 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4089 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4090 return;
4093 if (type->type == MONO_TYPE_VOID) {
4094 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4095 return;
4098 if (check_overflow (ctx))
4099 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4102 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4103 static void
4104 do_localloc (VerifyContext *ctx)
4106 ILStackDesc *top;
4108 if (ctx->eval.size != 1) {
4109 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4110 return;
4113 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4114 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4115 return;
4118 /*TODO verify top type*/
4119 top = stack_pop (ctx);
4121 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4122 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4125 static void
4126 do_ldstr (VerifyContext *ctx, guint32 token)
4128 GSList *error = NULL;
4129 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4130 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4131 return;
4134 if (!ctx->image->dynamic && !mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4135 if (error)
4136 ctx->list = g_slist_concat (ctx->list, error);
4137 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4138 return;
4141 if (check_overflow (ctx))
4142 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4145 static void
4146 do_refanyval (VerifyContext *ctx, int token)
4148 ILStackDesc *top;
4149 MonoType *type;
4150 if (!check_underflow (ctx, 1))
4151 return;
4153 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4154 return;
4156 top = stack_pop (ctx);
4158 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4159 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanyval, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4161 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4164 static void
4165 do_refanytype (VerifyContext *ctx)
4167 ILStackDesc *top;
4169 if (!check_underflow (ctx, 1))
4170 return;
4172 top = stack_pop (ctx);
4174 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4175 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanytype, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4177 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4181 static void
4182 do_mkrefany (VerifyContext *ctx, int token)
4184 ILStackDesc *top;
4185 MonoType *type;
4186 if (!check_underflow (ctx, 1))
4187 return;
4189 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4190 return;
4192 top = stack_pop (ctx);
4194 if (stack_slot_is_managed_mutability_pointer (top))
4195 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4197 if (!stack_slot_is_managed_pointer (top)) {
4198 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a managed pointer for mkrefany, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4199 }else {
4200 MonoType *stack_type = mono_type_get_type_byval (top->type);
4201 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4202 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4204 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4205 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4208 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4211 static void
4212 do_ckfinite (VerifyContext *ctx)
4214 ILStackDesc *top;
4215 if (!check_underflow (ctx, 1))
4216 return;
4218 top = stack_pop (ctx);
4220 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4221 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected float32 or float64 on stack for ckfinit but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4222 stack_push_stack_val (ctx, top);
4225 * merge_stacks:
4226 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4228 * @from holds new values for a given control path
4229 * @to holds the current values of a given control path
4231 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4233 static void
4234 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4236 MonoError error;
4237 int i, j, k;
4238 stack_init (ctx, to);
4240 if (start) {
4241 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4242 from->size = 0;
4243 else
4244 stack_copy (&ctx->eval, to);
4245 goto end_verify;
4246 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4247 stack_copy (to, &ctx->eval);
4248 goto end_verify;
4250 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4252 if (from->size != to->size) {
4253 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4254 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d) at 0x%04x", from->size, to->size, ctx->ip_offset));
4255 goto end_verify;
4258 //FIXME we need to preserve CMMP attributes
4259 //FIXME we must take null literals into consideration.
4260 for (i = 0; i < from->size; ++i) {
4261 ILStackDesc *new_slot = from->stack + i;
4262 ILStackDesc *old_slot = to->stack + i;
4263 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4264 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4265 MonoClass *old_class = mono_class_from_mono_type (old_type);
4266 MonoClass *new_class = mono_class_from_mono_type (new_type);
4267 MonoClass *match_class = NULL;
4269 // S := T then U = S (new value is compatible with current value, keep current)
4270 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4271 copy_stack_value (new_slot, old_slot);
4272 continue;
4275 // T := S then U = T (old value is compatible with current value, use new)
4276 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4277 copy_stack_value (old_slot, new_slot);
4278 continue;
4281 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4282 char *old_name = stack_slot_full_name (old_slot);
4283 char *new_name = stack_slot_full_name (new_slot);
4284 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
4285 g_free (old_name);
4286 g_free (new_name);
4287 goto end_verify;
4290 //both are reference types, use closest common super type
4291 if (!mono_class_from_mono_type (old_type)->valuetype
4292 && !mono_class_from_mono_type (new_type)->valuetype
4293 && !stack_slot_is_managed_pointer (old_slot)
4294 && !stack_slot_is_managed_pointer (new_slot)) {
4296 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4297 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4298 match_class = old_class->supertypes [j];
4299 goto match_found;
4303 mono_class_setup_interfaces (old_class, &error);
4304 if (!mono_error_ok (&error)) {
4305 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot merge stacks due to a TypeLoadException %s at 0x%04x", mono_error_get_message (&error), ctx->ip_offset));
4306 mono_error_cleanup (&error);
4307 goto end_verify;
4309 for (j = 0; j < old_class->interface_count; ++j) {
4310 for (k = 0; k < new_class->interface_count; ++k) {
4311 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4312 match_class = old_class->interfaces [j];
4313 goto match_found;
4318 //No decent super type found, use object
4319 match_class = mono_defaults.object_class;
4320 goto match_found;
4321 } else if (is_compatible_boxed_valuetype (ctx,old_type, new_type, new_slot, FALSE) || is_compatible_boxed_valuetype (ctx, new_type, old_type, old_slot, FALSE)) {
4322 match_class = mono_defaults.object_class;
4323 goto match_found;
4327 char *old_name = stack_slot_full_name (old_slot);
4328 char *new_name = stack_slot_full_name (new_slot);
4329 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
4330 g_free (old_name);
4331 g_free (new_name);
4333 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4334 goto end_verify;
4336 match_found:
4337 g_assert (match_class);
4338 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4339 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4340 continue;
4343 end_verify:
4344 if (external)
4345 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4346 to->flags |= IL_CODE_STACK_MERGED;
4349 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4350 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4353 * is_clause_in_range :
4355 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4357 static gboolean
4358 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4360 if (clause->try_offset >= start && clause->try_offset < end)
4361 return TRUE;
4362 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4363 return TRUE;
4364 return FALSE;
4368 * is_clause_inside_range :
4370 * Returns TRUE if @clause lies completely inside the @start - @end range.
4372 static gboolean
4373 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4375 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4376 return FALSE;
4377 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4378 return FALSE;
4379 return TRUE;
4383 * is_clause_nested :
4385 * Returns TRUE if @nested is nested in @clause.
4387 static gboolean
4388 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4390 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4391 return TRUE;
4392 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4393 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4396 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4397 * the each pair of exception must have the following properties:
4398 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4399 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4400 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4402 static void
4403 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4405 /*clause is nested*/
4406 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4407 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4408 return;
4411 /*wrong nesting order.*/
4412 if (is_clause_nested (clause, to_test)) {
4413 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4414 return;
4417 /*mutual protection*/
4418 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4419 /*handlers are not disjoint*/
4420 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4421 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4422 return;
4424 /* handlers are not catch or filter */
4425 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4426 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4427 return;
4429 /*OK*/
4430 return;
4433 /*not completelly disjoint*/
4434 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4435 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4436 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4439 #define code_bounds_check(size) \
4440 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4441 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4442 break; \
4445 static gboolean
4446 mono_opcode_is_prefix (int op)
4448 switch (op) {
4449 case MONO_CEE_UNALIGNED_:
4450 case MONO_CEE_VOLATILE_:
4451 case MONO_CEE_TAIL_:
4452 case MONO_CEE_CONSTRAINED_:
4453 case MONO_CEE_READONLY_:
4454 return TRUE;
4456 return FALSE;
4460 * FIXME: need to distinguish between valid and verifiable.
4461 * Need to keep track of types on the stack.
4462 * Verify types for opcodes.
4464 GSList*
4465 mono_method_verify (MonoMethod *method, int level)
4467 MonoError error;
4468 const unsigned char *ip, *code_start;
4469 const unsigned char *end;
4470 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4472 int i, n, need_merge = 0, start = 0;
4473 guint token, ip_offset = 0, prefix = 0;
4474 MonoGenericContext *generic_context = NULL;
4475 MonoImage *image;
4476 VerifyContext ctx;
4477 GSList *tmp;
4478 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4480 init_verifier_stats ();
4482 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4483 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4484 return NULL;
4487 memset (&ctx, 0, sizeof (VerifyContext));
4489 //FIXME use mono_method_get_signature_full
4490 ctx.signature = mono_method_signature (method);
4491 if (!ctx.signature) {
4492 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4494 finish_collect_stats ();
4495 return ctx.list;
4497 ctx.header = mono_method_get_header (method);
4498 if (!ctx.header) {
4499 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4500 finish_collect_stats ();
4501 return ctx.list;
4503 ctx.method = method;
4504 code_start = ip = ctx.header->code;
4505 end = ip + ctx.header->code_size;
4506 ctx.image = image = method->klass->image;
4509 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4510 ctx.max_stack = ctx.header->max_stack;
4511 ctx.verifiable = ctx.valid = 1;
4512 ctx.level = level;
4514 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4515 ctx.code_size = ctx.header->code_size;
4516 MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4518 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4520 ctx.num_locals = ctx.header->num_locals;
4521 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4522 MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4524 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4525 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4527 ctx.params = g_new (MonoType*, ctx.max_args);
4528 MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4530 if (ctx.signature->hasthis)
4531 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4532 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4534 if (ctx.signature->is_inflated)
4535 ctx.generic_context = generic_context = mono_method_get_context (method);
4537 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4538 if (method->is_generic)
4539 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4540 else
4541 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4544 for (i = 0; i < ctx.num_locals; ++i) {
4545 MonoType *uninflated = ctx.locals [i];
4546 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4547 if (!mono_error_ok (&error)) {
4548 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4549 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4550 g_free (name);
4551 mono_error_cleanup (&error);
4552 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4553 ctx.num_locals = i;
4554 ctx.max_args = 0;
4555 goto cleanup;
4558 for (i = 0; i < ctx.max_args; ++i) {
4559 MonoType *uninflated = ctx.params [i];
4560 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4561 if (!mono_error_ok (&error)) {
4562 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4563 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4564 g_free (name);
4565 mono_error_cleanup (&error);
4566 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4567 ctx.max_args = i;
4568 goto cleanup;
4571 stack_init (&ctx, &ctx.eval);
4573 for (i = 0; i < ctx.num_locals; ++i) {
4574 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4575 break;
4576 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4577 char *name = mono_type_full_name (ctx.locals [i]);
4578 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4579 g_free (name);
4580 break;
4585 for (i = 0; i < ctx.max_args; ++i) {
4586 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4587 break;
4589 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4590 char *name = mono_type_full_name (ctx.params [i]);
4591 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4592 g_free (name);
4593 break;
4597 if (!ctx.valid)
4598 goto cleanup;
4600 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4601 MonoExceptionClause *clause = ctx.header->clauses + i;
4602 VERIFIER_DEBUG (printf ("clause try %x len %x filter at %x handler at %x len %x\n", clause->try_offset, clause->try_len, clause->data.filter_offset, clause->handler_offset, clause->handler_len); );
4604 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4605 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4607 if (clause->try_len <= 0)
4608 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4610 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4611 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4613 if (clause->handler_len <= 0)
4614 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4616 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4617 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4619 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4620 if (clause->data.filter_offset > ctx.code_size)
4621 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4623 if (clause->data.filter_offset >= clause->handler_offset)
4624 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4627 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4628 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4630 if (!ctx.valid)
4631 break;
4633 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4634 if (clause->try_offset + clause->try_len < ctx.code_size)
4635 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4636 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4637 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4639 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4640 if (!clause->data.catch_class) {
4641 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
4642 break;
4645 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4647 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4648 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4649 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4653 original_bb = bb = mono_basic_block_split (method, &error);
4654 if (!mono_error_ok (&error)) {
4655 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
4656 mono_error_cleanup (&error);
4657 goto cleanup;
4659 g_assert (bb);
4661 while (ip < end && ctx.valid) {
4662 ip_offset = ip - code_start;
4664 const unsigned char *ip_copy = ip;
4665 int size, op;
4667 if (ip_offset > bb->end) {
4668 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
4669 goto cleanup;
4672 if (ip_offset == bb->end)
4673 bb = bb->next;
4675 size = mono_opcode_value_and_size (&ip_copy, end, &op);
4676 if (size == -1) {
4677 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
4678 goto cleanup;
4681 if (ADD_IS_GREATER_OR_OVF (ip_offset, size, bb->end)) {
4682 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
4683 goto cleanup;
4686 /*Last Instruction*/
4687 if (ip_offset + size == bb->end && mono_opcode_is_prefix (op)) {
4688 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets between prefix '%s' and instruction at 0x%04x", mono_opcode_name (op), ip_offset));
4689 goto cleanup;
4692 if (bb->dead) {
4693 /*FIXME remove this once we move all bad branch checking code to use BB only*/
4694 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
4695 ip += size;
4696 continue;
4700 ctx.ip_offset = ip_offset = ip - code_start;
4702 /*We need to check against fallthrou in and out of protected blocks.
4703 * For fallout we check the once a protected block ends, if the start flag is not set.
4704 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4705 * TODO convert these checks to be done using flags and not this loop
4707 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4708 MonoExceptionClause *clause = ctx.header->clauses + i;
4710 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4711 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4712 start = 1;
4715 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4716 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4717 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4718 else
4719 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4720 start = 1;
4723 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4724 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4725 start = 1;
4728 if (clause->handler_offset == ip_offset && start == 0) {
4729 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4730 start = 1;
4733 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
4734 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4735 start = 1;
4739 if (!ctx.valid)
4740 break;
4742 if (need_merge) {
4743 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4744 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4745 need_merge = 0;
4747 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4748 start = 0;
4750 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4751 #ifdef MONO_VERIFIER_DEBUG
4753 char *discode;
4754 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4755 discode [strlen (discode) - 1] = 0; /* no \n */
4756 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4757 g_free (discode);
4759 dump_stack_state (&ctx.code [ip_offset]);
4760 dump_stack_state (&ctx.eval);
4761 #endif
4763 switch (*ip) {
4764 case CEE_NOP:
4765 case CEE_BREAK:
4766 ++ip;
4767 break;
4769 case CEE_LDARG_0:
4770 case CEE_LDARG_1:
4771 case CEE_LDARG_2:
4772 case CEE_LDARG_3:
4773 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4774 ++ip;
4775 break;
4777 case CEE_LDARG_S:
4778 case CEE_LDARGA_S:
4779 code_bounds_check (2);
4780 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4781 ip += 2;
4782 break;
4784 case CEE_ADD_OVF_UN:
4785 do_binop (&ctx, *ip, add_ovf_un_table);
4786 ++ip;
4787 break;
4789 case CEE_SUB_OVF_UN:
4790 do_binop (&ctx, *ip, sub_ovf_un_table);
4791 ++ip;
4792 break;
4794 case CEE_ADD_OVF:
4795 case CEE_SUB_OVF:
4796 case CEE_MUL_OVF:
4797 case CEE_MUL_OVF_UN:
4798 do_binop (&ctx, *ip, bin_ovf_table);
4799 ++ip;
4800 break;
4802 case CEE_ADD:
4803 do_binop (&ctx, *ip, add_table);
4804 ++ip;
4805 break;
4807 case CEE_SUB:
4808 do_binop (&ctx, *ip, sub_table);
4809 ++ip;
4810 break;
4812 case CEE_MUL:
4813 case CEE_DIV:
4814 case CEE_REM:
4815 do_binop (&ctx, *ip, bin_op_table);
4816 ++ip;
4817 break;
4819 case CEE_AND:
4820 case CEE_DIV_UN:
4821 case CEE_OR:
4822 case CEE_REM_UN:
4823 case CEE_XOR:
4824 do_binop (&ctx, *ip, int_bin_op_table);
4825 ++ip;
4826 break;
4828 case CEE_SHL:
4829 case CEE_SHR:
4830 case CEE_SHR_UN:
4831 do_binop (&ctx, *ip, shift_op_table);
4832 ++ip;
4833 break;
4835 case CEE_POP:
4836 if (!check_underflow (&ctx, 1))
4837 break;
4838 stack_pop_safe (&ctx);
4839 ++ip;
4840 break;
4842 case CEE_RET:
4843 do_ret (&ctx);
4844 ++ip;
4845 start = 1;
4846 break;
4848 case CEE_LDLOC_0:
4849 case CEE_LDLOC_1:
4850 case CEE_LDLOC_2:
4851 case CEE_LDLOC_3:
4852 /*TODO support definite assignment verification? */
4853 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
4854 ++ip;
4855 break;
4857 case CEE_STLOC_0:
4858 case CEE_STLOC_1:
4859 case CEE_STLOC_2:
4860 case CEE_STLOC_3:
4861 store_local (&ctx, *ip - CEE_STLOC_0);
4862 ++ip;
4863 break;
4865 case CEE_STLOC_S:
4866 code_bounds_check (2);
4867 store_local (&ctx, ip [1]);
4868 ip += 2;
4869 break;
4871 case CEE_STARG_S:
4872 code_bounds_check (2);
4873 store_arg (&ctx, ip [1]);
4874 ip += 2;
4875 break;
4877 case CEE_LDC_I4_M1:
4878 case CEE_LDC_I4_0:
4879 case CEE_LDC_I4_1:
4880 case CEE_LDC_I4_2:
4881 case CEE_LDC_I4_3:
4882 case CEE_LDC_I4_4:
4883 case CEE_LDC_I4_5:
4884 case CEE_LDC_I4_6:
4885 case CEE_LDC_I4_7:
4886 case CEE_LDC_I4_8:
4887 if (check_overflow (&ctx))
4888 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4889 ++ip;
4890 break;
4892 case CEE_LDC_I4_S:
4893 code_bounds_check (2);
4894 if (check_overflow (&ctx))
4895 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4896 ip += 2;
4897 break;
4899 case CEE_LDC_I4:
4900 code_bounds_check (5);
4901 if (check_overflow (&ctx))
4902 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4903 ip += 5;
4904 break;
4906 case CEE_LDC_I8:
4907 code_bounds_check (9);
4908 if (check_overflow (&ctx))
4909 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4910 ip += 9;
4911 break;
4913 case CEE_LDC_R4:
4914 code_bounds_check (5);
4915 if (check_overflow (&ctx))
4916 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4917 ip += 5;
4918 break;
4920 case CEE_LDC_R8:
4921 code_bounds_check (9);
4922 if (check_overflow (&ctx))
4923 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4924 ip += 9;
4925 break;
4927 case CEE_LDNULL:
4928 if (check_overflow (&ctx))
4929 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4930 ++ip;
4931 break;
4933 case CEE_BEQ_S:
4934 case CEE_BNE_UN_S:
4935 code_bounds_check (2);
4936 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4937 ip += 2;
4938 need_merge = 1;
4939 break;
4941 case CEE_BGE_S:
4942 case CEE_BGT_S:
4943 case CEE_BLE_S:
4944 case CEE_BLT_S:
4945 case CEE_BGE_UN_S:
4946 case CEE_BGT_UN_S:
4947 case CEE_BLE_UN_S:
4948 case CEE_BLT_UN_S:
4949 code_bounds_check (2);
4950 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
4951 ip += 2;
4952 need_merge = 1;
4953 break;
4955 case CEE_BEQ:
4956 case CEE_BNE_UN:
4957 code_bounds_check (5);
4958 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
4959 ip += 5;
4960 need_merge = 1;
4961 break;
4963 case CEE_BGE:
4964 case CEE_BGT:
4965 case CEE_BLE:
4966 case CEE_BLT:
4967 case CEE_BGE_UN:
4968 case CEE_BGT_UN:
4969 case CEE_BLE_UN:
4970 case CEE_BLT_UN:
4971 code_bounds_check (5);
4972 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
4973 ip += 5;
4974 need_merge = 1;
4975 break;
4977 case CEE_LDLOC_S:
4978 case CEE_LDLOCA_S:
4979 code_bounds_check (2);
4980 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
4981 ip += 2;
4982 break;
4984 case CEE_UNUSED99:
4985 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
4986 ++ip;
4987 break;
4989 case CEE_DUP: {
4990 ILStackDesc *top;
4991 if (!check_underflow (&ctx, 1))
4992 break;
4993 if (!check_overflow (&ctx))
4994 break;
4995 top = stack_push (&ctx);
4996 copy_stack_value (top, stack_peek (&ctx, 1));
4997 ++ip;
4998 break;
5001 case CEE_JMP:
5002 code_bounds_check (5);
5003 if (ctx.eval.size)
5004 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5005 token = read32 (ip + 1);
5006 if (in_any_block (ctx.header, ip_offset))
5007 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5009 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5011 * FIXME: check signature, retval, arguments etc.
5013 ip += 5;
5014 break;
5015 case CEE_CALL:
5016 case CEE_CALLVIRT:
5017 code_bounds_check (5);
5018 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5019 ip += 5;
5020 break;
5022 case CEE_CALLI:
5023 code_bounds_check (5);
5024 token = read32 (ip + 1);
5026 * FIXME: check signature, retval, arguments etc.
5027 * FIXME: check requirements for tail call
5029 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5030 ip += 5;
5031 break;
5032 case CEE_BR_S:
5033 code_bounds_check (2);
5034 do_static_branch (&ctx, (signed char)ip [1] + 2);
5035 need_merge = 1;
5036 ip += 2;
5037 start = 1;
5038 break;
5040 case CEE_BRFALSE_S:
5041 case CEE_BRTRUE_S:
5042 code_bounds_check (2);
5043 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5044 ip += 2;
5045 need_merge = 1;
5046 break;
5048 case CEE_BR:
5049 code_bounds_check (5);
5050 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5051 need_merge = 1;
5052 ip += 5;
5053 start = 1;
5054 break;
5056 case CEE_BRFALSE:
5057 case CEE_BRTRUE:
5058 code_bounds_check (5);
5059 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5060 ip += 5;
5061 need_merge = 1;
5062 break;
5064 case CEE_SWITCH: {
5065 guint32 entries;
5066 code_bounds_check (5);
5067 entries = read32 (ip + 1);
5069 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5070 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5072 ip += 5;
5073 code_bounds_check (sizeof (guint32) * entries);
5075 do_switch (&ctx, entries, ip);
5076 ip += sizeof (guint32) * entries;
5077 break;
5079 case CEE_LDIND_I1:
5080 case CEE_LDIND_U1:
5081 case CEE_LDIND_I2:
5082 case CEE_LDIND_U2:
5083 case CEE_LDIND_I4:
5084 case CEE_LDIND_U4:
5085 case CEE_LDIND_I8:
5086 case CEE_LDIND_I:
5087 case CEE_LDIND_R4:
5088 case CEE_LDIND_R8:
5089 case CEE_LDIND_REF:
5090 do_load_indirect (&ctx, *ip);
5091 ++ip;
5092 break;
5094 case CEE_STIND_REF:
5095 case CEE_STIND_I1:
5096 case CEE_STIND_I2:
5097 case CEE_STIND_I4:
5098 case CEE_STIND_I8:
5099 case CEE_STIND_R4:
5100 case CEE_STIND_R8:
5101 case CEE_STIND_I:
5102 do_store_indirect (&ctx, *ip);
5103 ++ip;
5104 break;
5106 case CEE_NOT:
5107 case CEE_NEG:
5108 do_unary_math_op (&ctx, *ip);
5109 ++ip;
5110 break;
5112 case CEE_CONV_I1:
5113 case CEE_CONV_I2:
5114 case CEE_CONV_I4:
5115 case CEE_CONV_U1:
5116 case CEE_CONV_U2:
5117 case CEE_CONV_U4:
5118 do_conversion (&ctx, TYPE_I4);
5119 ++ip;
5120 break;
5122 case CEE_CONV_I8:
5123 case CEE_CONV_U8:
5124 do_conversion (&ctx, TYPE_I8);
5125 ++ip;
5126 break;
5128 case CEE_CONV_R4:
5129 case CEE_CONV_R8:
5130 case CEE_CONV_R_UN:
5131 do_conversion (&ctx, TYPE_R8);
5132 ++ip;
5133 break;
5135 case CEE_CONV_I:
5136 case CEE_CONV_U:
5137 do_conversion (&ctx, TYPE_NATIVE_INT);
5138 ++ip;
5139 break;
5141 case CEE_CPOBJ:
5142 code_bounds_check (5);
5143 do_cpobj (&ctx, read32 (ip + 1));
5144 ip += 5;
5145 break;
5147 case CEE_LDOBJ:
5148 code_bounds_check (5);
5149 do_ldobj_value (&ctx, read32 (ip + 1));
5150 ip += 5;
5151 break;
5153 case CEE_LDSTR:
5154 code_bounds_check (5);
5155 do_ldstr (&ctx, read32 (ip + 1));
5156 ip += 5;
5157 break;
5159 case CEE_NEWOBJ:
5160 code_bounds_check (5);
5161 do_newobj (&ctx, read32 (ip + 1));
5162 ip += 5;
5163 break;
5165 case CEE_CASTCLASS:
5166 case CEE_ISINST:
5167 code_bounds_check (5);
5168 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5169 ip += 5;
5170 break;
5172 case CEE_UNUSED58:
5173 case CEE_UNUSED1:
5174 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5175 ++ip;
5176 break;
5178 case CEE_UNBOX:
5179 code_bounds_check (5);
5180 do_unbox_value (&ctx, read32 (ip + 1));
5181 ip += 5;
5182 break;
5184 case CEE_THROW:
5185 do_throw (&ctx);
5186 start = 1;
5187 ++ip;
5188 break;
5190 case CEE_LDFLD:
5191 case CEE_LDFLDA:
5192 code_bounds_check (5);
5193 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5194 ip += 5;
5195 break;
5197 case CEE_LDSFLD:
5198 case CEE_LDSFLDA:
5199 code_bounds_check (5);
5200 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5201 ip += 5;
5202 break;
5204 case CEE_STFLD:
5205 code_bounds_check (5);
5206 do_store_field (&ctx, read32 (ip + 1));
5207 ip += 5;
5208 break;
5210 case CEE_STSFLD:
5211 code_bounds_check (5);
5212 do_store_static_field (&ctx, read32 (ip + 1));
5213 ip += 5;
5214 break;
5216 case CEE_STOBJ:
5217 code_bounds_check (5);
5218 do_stobj (&ctx, read32 (ip + 1));
5219 ip += 5;
5220 break;
5222 case CEE_CONV_OVF_I1_UN:
5223 case CEE_CONV_OVF_I2_UN:
5224 case CEE_CONV_OVF_I4_UN:
5225 case CEE_CONV_OVF_U1_UN:
5226 case CEE_CONV_OVF_U2_UN:
5227 case CEE_CONV_OVF_U4_UN:
5228 do_conversion (&ctx, TYPE_I4);
5229 ++ip;
5230 break;
5232 case CEE_CONV_OVF_I8_UN:
5233 case CEE_CONV_OVF_U8_UN:
5234 do_conversion (&ctx, TYPE_I8);
5235 ++ip;
5236 break;
5238 case CEE_CONV_OVF_I_UN:
5239 case CEE_CONV_OVF_U_UN:
5240 do_conversion (&ctx, TYPE_NATIVE_INT);
5241 ++ip;
5242 break;
5244 case CEE_BOX:
5245 code_bounds_check (5);
5246 do_box_value (&ctx, read32 (ip + 1));
5247 ip += 5;
5248 break;
5250 case CEE_NEWARR:
5251 code_bounds_check (5);
5252 do_newarr (&ctx, read32 (ip + 1));
5253 ip += 5;
5254 break;
5256 case CEE_LDLEN:
5257 do_ldlen (&ctx);
5258 ++ip;
5259 break;
5261 case CEE_LDELEMA:
5262 code_bounds_check (5);
5263 do_ldelema (&ctx, read32 (ip + 1));
5264 ip += 5;
5265 break;
5267 case CEE_LDELEM_I1:
5268 case CEE_LDELEM_U1:
5269 case CEE_LDELEM_I2:
5270 case CEE_LDELEM_U2:
5271 case CEE_LDELEM_I4:
5272 case CEE_LDELEM_U4:
5273 case CEE_LDELEM_I8:
5274 case CEE_LDELEM_I:
5275 case CEE_LDELEM_R4:
5276 case CEE_LDELEM_R8:
5277 case CEE_LDELEM_REF:
5278 do_ldelem (&ctx, *ip, 0);
5279 ++ip;
5280 break;
5282 case CEE_STELEM_I:
5283 case CEE_STELEM_I1:
5284 case CEE_STELEM_I2:
5285 case CEE_STELEM_I4:
5286 case CEE_STELEM_I8:
5287 case CEE_STELEM_R4:
5288 case CEE_STELEM_R8:
5289 case CEE_STELEM_REF:
5290 do_stelem (&ctx, *ip, 0);
5291 ++ip;
5292 break;
5294 case CEE_LDELEM:
5295 code_bounds_check (5);
5296 do_ldelem (&ctx, *ip, read32 (ip + 1));
5297 ip += 5;
5298 break;
5300 case CEE_STELEM:
5301 code_bounds_check (5);
5302 do_stelem (&ctx, *ip, read32 (ip + 1));
5303 ip += 5;
5304 break;
5306 case CEE_UNBOX_ANY:
5307 code_bounds_check (5);
5308 do_unbox_any (&ctx, read32 (ip + 1));
5309 ip += 5;
5310 break;
5312 case CEE_CONV_OVF_I1:
5313 case CEE_CONV_OVF_U1:
5314 case CEE_CONV_OVF_I2:
5315 case CEE_CONV_OVF_U2:
5316 case CEE_CONV_OVF_I4:
5317 case CEE_CONV_OVF_U4:
5318 do_conversion (&ctx, TYPE_I4);
5319 ++ip;
5320 break;
5322 case CEE_CONV_OVF_I8:
5323 case CEE_CONV_OVF_U8:
5324 do_conversion (&ctx, TYPE_I8);
5325 ++ip;
5326 break;
5328 case CEE_CONV_OVF_I:
5329 case CEE_CONV_OVF_U:
5330 do_conversion (&ctx, TYPE_NATIVE_INT);
5331 ++ip;
5332 break;
5334 case CEE_REFANYVAL:
5335 code_bounds_check (5);
5336 do_refanyval (&ctx, read32 (ip + 1));
5337 ip += 5;
5338 break;
5340 case CEE_CKFINITE:
5341 do_ckfinite (&ctx);
5342 ++ip;
5343 break;
5345 case CEE_MKREFANY:
5346 code_bounds_check (5);
5347 do_mkrefany (&ctx, read32 (ip + 1));
5348 ip += 5;
5349 break;
5351 case CEE_LDTOKEN:
5352 code_bounds_check (5);
5353 do_load_token (&ctx, read32 (ip + 1));
5354 ip += 5;
5355 break;
5357 case CEE_ENDFINALLY:
5358 if (!is_correct_endfinally (ctx.header, ip_offset))
5359 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5360 ctx.eval.size = 0;
5361 start = 1;
5362 ++ip;
5363 break;
5365 case CEE_LEAVE:
5366 code_bounds_check (5);
5367 do_leave (&ctx, read32 (ip + 1) + 5);
5368 ip += 5;
5369 start = 1;
5370 break;
5372 case CEE_LEAVE_S:
5373 code_bounds_check (2);
5374 do_leave (&ctx, (signed char)ip [1] + 2);
5375 ip += 2;
5376 start = 1;
5377 break;
5379 case CEE_PREFIX1:
5380 code_bounds_check (2);
5381 ++ip;
5382 switch (*ip) {
5383 case CEE_STLOC:
5384 code_bounds_check (3);
5385 store_local (&ctx, read16 (ip + 1));
5386 ip += 3;
5387 break;
5389 case CEE_CEQ:
5390 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5391 ++ip;
5392 break;
5394 case CEE_CGT:
5395 case CEE_CGT_UN:
5396 case CEE_CLT:
5397 case CEE_CLT_UN:
5398 do_cmp_op (&ctx, cmp_br_op, *ip);
5399 ++ip;
5400 break;
5402 case CEE_STARG:
5403 code_bounds_check (3);
5404 store_arg (&ctx, read16 (ip + 1) );
5405 ip += 3;
5406 break;
5409 case CEE_ARGLIST:
5410 if (!check_overflow (&ctx))
5411 break;
5412 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5413 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5414 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5415 ++ip;
5416 break;
5418 case CEE_LDFTN:
5419 code_bounds_check (5);
5420 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5421 ip += 5;
5422 break;
5424 case CEE_LDVIRTFTN:
5425 code_bounds_check (5);
5426 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5427 ip += 5;
5428 break;
5430 case CEE_LDARG:
5431 case CEE_LDARGA:
5432 code_bounds_check (3);
5433 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5434 ip += 3;
5435 break;
5437 case CEE_LDLOC:
5438 case CEE_LDLOCA:
5439 code_bounds_check (3);
5440 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5441 ip += 3;
5442 break;
5444 case CEE_LOCALLOC:
5445 do_localloc (&ctx);
5446 ++ip;
5447 break;
5449 case CEE_UNUSED56:
5450 case CEE_UNUSED57:
5451 case CEE_UNUSED70:
5452 case CEE_UNUSED:
5453 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5454 ++ip;
5455 break;
5456 case CEE_ENDFILTER:
5457 do_endfilter (&ctx);
5458 start = 1;
5459 ++ip;
5460 break;
5461 case CEE_UNALIGNED_:
5462 code_bounds_check (2);
5463 prefix |= PREFIX_UNALIGNED;
5464 ip += 2;
5465 break;
5466 case CEE_VOLATILE_:
5467 prefix |= PREFIX_VOLATILE;
5468 ++ip;
5469 break;
5470 case CEE_TAIL_:
5471 prefix |= PREFIX_TAIL;
5472 ++ip;
5473 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5474 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5475 break;
5477 case CEE_INITOBJ:
5478 code_bounds_check (5);
5479 do_initobj (&ctx, read32 (ip + 1));
5480 ip += 5;
5481 break;
5483 case CEE_CONSTRAINED_:
5484 code_bounds_check (5);
5485 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5486 prefix |= PREFIX_CONSTRAINED;
5487 ip += 5;
5488 break;
5490 case CEE_READONLY_:
5491 prefix |= PREFIX_READONLY;
5492 ip++;
5493 break;
5495 case CEE_CPBLK:
5496 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5497 if (!check_underflow (&ctx, 3))
5498 break;
5499 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5500 ip++;
5501 break;
5503 case CEE_INITBLK:
5504 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5505 if (!check_underflow (&ctx, 3))
5506 break;
5507 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5508 ip++;
5509 break;
5511 case CEE_NO_:
5512 ip += 2;
5513 break;
5514 case CEE_RETHROW:
5515 if (!is_correct_rethrow (ctx.header, ip_offset))
5516 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5517 ctx.eval.size = 0;
5518 start = 1;
5519 ++ip;
5520 break;
5522 case CEE_SIZEOF:
5523 code_bounds_check (5);
5524 do_sizeof (&ctx, read32 (ip + 1));
5525 ip += 5;
5526 break;
5528 case CEE_REFANYTYPE:
5529 do_refanytype (&ctx);
5530 ++ip;
5531 break;
5533 default:
5534 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5535 ++ip;
5537 break;
5539 default:
5540 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5541 ++ip;
5544 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5545 if (prefix) {
5546 if (!ctx.prefix_set) //first prefix
5547 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5548 ctx.prefix_set |= prefix;
5549 ctx.has_flags = TRUE;
5550 prefix = 0;
5551 } else {
5552 if (!ctx.has_flags)
5553 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5555 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5556 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5557 if (ctx.prefix_set & PREFIX_READONLY)
5558 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5559 if (ctx.prefix_set & PREFIX_VOLATILE)
5560 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5561 if (ctx.prefix_set & PREFIX_UNALIGNED)
5562 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5563 ctx.prefix_set = prefix = 0;
5564 ctx.has_flags = FALSE;
5568 * if ip != end we overflowed: mark as error.
5570 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5571 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5574 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5575 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5576 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5577 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5578 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5580 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5581 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5583 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5584 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5586 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5587 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid call to a non-final virtual function in method with stdarg.0 or ldarga.0 at 0x%04x", i));
5590 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5591 char *method_name = mono_method_full_name (ctx.method, TRUE);
5592 char *type = mono_type_get_full_name (ctx.method->klass);
5593 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5594 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor due to a TypeLoadException on base type.", method_name, type));
5595 else
5596 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5597 g_free (method_name);
5598 g_free (type);
5601 cleanup:
5602 if (ctx.code) {
5603 for (i = 0; i < ctx.header->code_size; ++i) {
5604 if (ctx.code [i].stack)
5605 g_free (ctx.code [i].stack);
5609 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5610 g_free (tmp->data);
5611 g_slist_free (ctx.funptrs);
5613 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5614 mono_metadata_free_type (tmp->data);
5615 g_slist_free (ctx.exception_types);
5617 for (i = 0; i < ctx.num_locals; ++i) {
5618 if (ctx.locals [i])
5619 mono_metadata_free_type (ctx.locals [i]);
5621 for (i = 0; i < ctx.max_args; ++i) {
5622 if (ctx.params [i])
5623 mono_metadata_free_type (ctx.params [i]);
5626 if (ctx.eval.stack)
5627 g_free (ctx.eval.stack);
5628 if (ctx.code)
5629 g_free (ctx.code);
5630 g_free (ctx.locals);
5631 g_free (ctx.params);
5632 mono_basic_block_free (original_bb);
5633 mono_metadata_free_mh (ctx.header);
5635 finish_collect_stats ();
5636 return ctx.list;
5639 char*
5640 mono_verify_corlib ()
5642 /* This is a public API function so cannot be removed */
5643 return NULL;
5647 * Returns true if @method needs to be verified.
5650 gboolean
5651 mono_verifier_is_enabled_for_method (MonoMethod *method)
5653 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5657 * Returns true if @klass need to be verified.
5660 gboolean
5661 mono_verifier_is_enabled_for_class (MonoClass *klass)
5663 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
5666 gboolean
5667 mono_verifier_is_enabled_for_image (MonoImage *image)
5669 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
5672 gboolean
5673 mono_verifier_is_method_full_trust (MonoMethod *method)
5675 return mono_verifier_is_class_full_trust (method->klass);
5679 * Returns if @klass is under full trust or not.
5681 * TODO This code doesn't take CAS into account.
5683 * Under verify_all all user code must be verifiable if no security option was set
5686 gboolean
5687 mono_verifier_is_class_full_trust (MonoClass *klass)
5689 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5690 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
5691 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
5693 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5694 return trusted_location || klass->image == mono_defaults.corlib;
5695 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
5698 GSList*
5699 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5701 return mono_method_verify (method,
5702 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5703 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5704 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5707 static int
5708 get_field_end (MonoClassField *field)
5710 int align;
5711 int size = mono_type_size (field->type, &align);
5712 if (size == 0)
5713 size = 4; /*FIXME Is this a safe bet?*/
5714 return size + field->offset;
5717 static gboolean
5718 verify_class_for_overlapping_reference_fields (MonoClass *class)
5720 int i = 0, j;
5721 gpointer iter = NULL;
5722 MonoClassField *field;
5723 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5724 /*We can't skip types with !has_references since this is calculated after we have run.*/
5725 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
5726 return TRUE;
5729 /*We must check for stuff overlapping reference fields.
5730 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
5732 while ((field = mono_class_get_fields (class, &iter))) {
5733 int fieldEnd = get_field_end (field);
5734 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5735 ++i;
5737 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5738 continue;
5740 for (j = i; j < class->field.count; ++j) {
5741 MonoClassField *other = &class->fields [j];
5742 int otherEnd = get_field_end (other);
5743 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5744 continue;
5746 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5747 continue;
5749 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5750 return FALSE;
5753 return TRUE;
5756 static guint
5757 field_hash (gconstpointer key)
5759 const MonoClassField *field = key;
5760 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
5763 static gboolean
5764 field_equals (gconstpointer _a, gconstpointer _b)
5766 const MonoClassField *a = _a;
5767 const MonoClassField *b = _b;
5768 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
5772 static gboolean
5773 verify_class_fields (MonoClass *class)
5775 gpointer iter = NULL;
5776 MonoClassField *field;
5777 MonoGenericContext *context = mono_class_get_context (class);
5778 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
5779 if (class->generic_container)
5780 context = &class->generic_container->context;
5782 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5783 if (!mono_type_is_valid_type_in_context (field->type, context)) {
5784 g_hash_table_destroy (unique_fields);
5785 return FALSE;
5787 if (g_hash_table_lookup (unique_fields, field)) {
5788 g_hash_table_destroy (unique_fields);
5789 return FALSE;
5791 g_hash_table_insert (unique_fields, field, field);
5793 g_hash_table_destroy (unique_fields);
5794 return TRUE;
5797 static gboolean
5798 verify_interfaces (MonoClass *class)
5800 int i;
5801 for (i = 0; i < class->interface_count; ++i) {
5802 MonoClass *iface = class->interfaces [i];
5803 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
5804 return FALSE;
5806 return TRUE;
5809 static gboolean
5810 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
5812 int type;
5813 gpointer iter = NULL;
5814 MonoClassField *field;
5815 MonoClass *field_class;
5817 if (!class->valuetype)
5818 return TRUE;
5820 type = class->byval_arg.type;
5821 /*primitive type fields are not properly decoded*/
5822 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
5823 return TRUE;
5825 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5826 if (!field->type)
5827 return FALSE;
5829 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
5830 continue;
5832 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
5834 if (field_class == target_class || class == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
5835 return FALSE;
5838 return TRUE;
5841 static gboolean
5842 verify_valuetype_layout (MonoClass *class)
5844 gboolean res;
5845 res = verify_valuetype_layout_with_target (class, class);
5846 return res;
5849 static gboolean
5850 recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
5852 int idx;
5853 MonoClass **constraints;
5854 MonoGenericParamInfo *param_info;
5856 g_assert (mono_type_is_generic_argument (type));
5858 idx = mono_type_get_generic_param_num (type);
5859 if (mono_bitset_test_fast (used_args, idx))
5860 return FALSE;
5862 mono_bitset_set_fast (used_args, idx);
5863 param_info = mono_generic_container_get_param_info (gc, idx);
5865 if (!param_info->constraints)
5866 return TRUE;
5868 for (constraints = param_info->constraints; *constraints; ++constraints) {
5869 MonoClass *ctr = *constraints;
5870 MonoType *constraint_type = &ctr->byval_arg;
5872 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
5873 return FALSE;
5875 return TRUE;
5878 static gboolean
5879 verify_generic_parameters (MonoClass *class)
5881 int i;
5882 MonoGenericContainer *gc = class->generic_container;
5883 MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
5885 for (i = 0; i < gc->type_argc; ++i) {
5886 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
5887 MonoClass **constraints;
5889 if (!param_info->constraints)
5890 continue;
5892 mono_bitset_clear_all (used_args);
5893 mono_bitset_set_fast (used_args, i);
5895 for (constraints = param_info->constraints; *constraints; ++constraints) {
5896 MonoClass *ctr = *constraints;
5897 MonoType *constraint_type = &ctr->byval_arg;
5899 if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
5900 goto fail;
5902 if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
5903 goto fail;
5906 mono_bitset_free (used_args);
5907 return TRUE;
5909 fail:
5910 mono_bitset_free (used_args);
5911 return FALSE;
5915 * Check if the class is verifiable.
5917 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5918 * field and invalid generic instantiation are fatal errors.
5920 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5923 gboolean
5924 mono_verifier_verify_class (MonoClass *class)
5926 /*Neither <Module>, object or ifaces have parent.*/
5927 if (!class->parent &&
5928 class != mono_defaults.object_class &&
5929 !MONO_CLASS_IS_INTERFACE (class) &&
5930 (!class->image->dynamic && class->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
5931 return FALSE;
5932 if (class->parent && MONO_CLASS_IS_INTERFACE (class->parent))
5933 return FALSE;
5934 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
5935 return FALSE;
5936 if (class->generic_container && !verify_generic_parameters (class))
5937 return FALSE;
5938 if (!verify_class_for_overlapping_reference_fields (class))
5939 return FALSE;
5940 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
5941 return FALSE;
5942 if (class->generic_class == NULL && !verify_class_fields (class))
5943 return FALSE;
5944 if (class->valuetype && !verify_valuetype_layout (class))
5945 return FALSE;
5946 if (!verify_interfaces (class))
5947 return FALSE;
5948 return TRUE;
5950 #else
5952 gboolean
5953 mono_verifier_verify_class (MonoClass *class)
5955 /* The verifier was disabled at compile time */
5956 return TRUE;
5959 GSList*
5960 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5962 /* The verifier was disabled at compile time */
5963 return NULL;
5966 gboolean
5967 mono_verifier_is_class_full_trust (MonoClass *klass)
5969 /* The verifier was disabled at compile time */
5970 return TRUE;
5973 gboolean
5974 mono_verifier_is_method_full_trust (MonoMethod *method)
5976 /* The verifier was disabled at compile time */
5977 return TRUE;
5980 gboolean
5981 mono_verifier_is_enabled_for_image (MonoImage *image)
5983 /* The verifier was disabled at compile time */
5984 return FALSE;
5987 gboolean
5988 mono_verifier_is_enabled_for_class (MonoClass *klass)
5990 /* The verifier was disabled at compile time */
5991 return FALSE;
5994 gboolean
5995 mono_verifier_is_enabled_for_method (MonoMethod *method)
5997 /* The verifier was disabled at compile time */
5998 return FALSE;
6001 GSList*
6002 mono_method_verify (MonoMethod *method, int level)
6004 /* The verifier was disabled at compile time */
6005 return NULL;
6008 void
6009 mono_free_verify_list (GSList *list)
6011 /* The verifier was disabled at compile time */
6012 /* will always be null if verifier is disabled */
6015 #endif