2010-06-02 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / verify.c
blob10a948f238ba72d87f5ef632ec602e89b33e9ba8
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 <string.h>
29 #include <signal.h>
30 #include <ctype.h>
33 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
34 static gboolean verify_all = FALSE;
37 * Set the desired level of checks for the verfier.
40 void
41 mono_verifier_set_mode (MiniVerifierMode mode)
43 verifier_mode = mode;
46 void
47 mono_verifier_enable_verify_all ()
49 verify_all = TRUE;
52 #ifndef DISABLE_VERIFIER
54 * Pull the list of opcodes
56 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
57 a = i,
59 enum {
60 #include "mono/cil/opcode.def"
61 LAST = 0xff
63 #undef OPDEF
65 #ifdef MONO_VERIFIER_DEBUG
66 #define VERIFIER_DEBUG(code) do { code } while (0)
67 #else
68 #define VERIFIER_DEBUG(code)
69 #endif
71 //////////////////////////////////////////////////////////////////
72 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
73 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
74 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
75 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
76 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
77 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
78 do { \
79 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
80 vinfo->info.status = __status; \
81 vinfo->info.message = ( __msg ); \
82 vinfo->exception_type = (__exception); \
83 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
84 } while (0)
86 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
87 #define ADD_VERIFY_ERROR(__ctx, __msg) \
88 do { \
89 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
90 (__ctx)->valid = 0; \
91 } while (0)
93 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
94 do { \
95 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
96 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
97 (__ctx)->verifiable = 0; \
98 if (IS_FAIL_FAST_MODE (__ctx)) \
99 (__ctx)->valid = 0; \
101 } while (0)
103 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
104 do { \
105 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
106 (__ctx)->valid = 0; \
107 } while (0)
109 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
110 do { \
111 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
112 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
113 (__ctx)->verifiable = 0; \
114 if (IS_FAIL_FAST_MODE (__ctx)) \
115 (__ctx)->valid = 0; \
117 } while (0)
119 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
120 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
122 #if SIZEOF_VOID_P == 4
123 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
124 #else
125 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
126 #endif
128 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
129 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
131 /*Flags to be used with ILCodeDesc::flags */
132 enum {
133 /*Instruction has not been processed.*/
134 IL_CODE_FLAG_NOT_PROCESSED = 0,
135 /*Instruction was decoded by mono_method_verify loop.*/
136 IL_CODE_FLAG_SEEN = 1,
137 /*Instruction was target of a branch or is at a protected block boundary.*/
138 IL_CODE_FLAG_WAS_TARGET = 2,
139 /*Used by stack_init to avoid double initialize each entry.*/
140 IL_CODE_FLAG_STACK_INITED = 4,
141 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
142 IL_CODE_STACK_MERGED = 8,
143 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
144 IL_CODE_DELEGATE_SEQUENCE = 0x10,
145 /*This is a delegate created from a ldftn to a non final virtual method*/
146 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
147 /*This is a call to a non final virtual method*/
148 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
151 typedef enum {
152 RESULT_VALID,
153 RESULT_UNVERIFIABLE,
154 RESULT_INVALID
155 } verify_result_t;
157 typedef struct {
158 MonoType *type;
159 int stype;
160 MonoMethod *method;
161 } ILStackDesc;
164 typedef struct {
165 ILStackDesc *stack;
166 guint16 size, max_size;
167 guint16 flags;
168 } ILCodeDesc;
170 typedef struct {
171 int max_args;
172 int max_stack;
173 int verifiable;
174 int valid;
175 int level;
177 int code_size;
178 ILCodeDesc *code;
179 ILCodeDesc eval;
181 MonoType **params;
182 GSList *list;
183 /*Allocated fnptr MonoType that should be freed by us.*/
184 GSList *funptrs;
185 /*Type dup'ed exception types from catch blocks.*/
186 GSList *exception_types;
188 int num_locals;
189 MonoType **locals;
191 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
192 int target;
194 guint32 ip_offset;
195 MonoMethodSignature *signature;
196 MonoMethodHeader *header;
198 MonoGenericContext *generic_context;
199 MonoImage *image;
200 MonoMethod *method;
202 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
203 *on a method that creates a delegate for a non-final virtual method using ldftn*/
204 gboolean has_this_store;
206 /*This flag is used to control if the contructor of the parent class has been called.
207 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
208 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
210 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
211 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
213 gboolean super_ctor_called;
215 guint32 prefix_set;
216 gboolean has_flags;
217 MonoType *constrained_type;
218 } VerifyContext;
220 static void
221 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
223 static int
224 get_stack_type (MonoType *type);
226 static gboolean
227 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
229 static gboolean
230 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
232 static gboolean
233 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
234 //////////////////////////////////////////////////////////////////
238 enum {
239 TYPE_INV = 0, /* leave at 0. */
240 TYPE_I4 = 1,
241 TYPE_I8 = 2,
242 TYPE_NATIVE_INT = 3,
243 TYPE_R8 = 4,
244 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
245 TYPE_PTR = 5,
246 /* value types and classes */
247 TYPE_COMPLEX = 6,
248 /* Number of types, used to define the size of the tables*/
249 TYPE_MAX = 6,
251 /* Used by tables to signal that a result is not verifiable*/
252 NON_VERIFIABLE_RESULT = 0x80,
254 /*Mask used to extract just the type, excluding flags */
255 TYPE_MASK = 0x0F,
257 /* The stack type is a managed pointer, unmask the value to res */
258 POINTER_MASK = 0x100,
260 /*Stack type with the pointer mask*/
261 RAW_TYPE_MASK = 0x10F,
263 /* Controlled Mutability Manager Pointer */
264 CMMP_MASK = 0x200,
266 /* The stack type is a null literal*/
267 NULL_LITERAL_MASK = 0x400,
269 /**Used by ldarg.0 and family to let delegate verification happens.*/
270 THIS_POINTER_MASK = 0x800,
272 /**Signals that this is a boxed value type*/
273 BOXED_MASK = 0x1000,
275 /*This is an unitialized this ref*/
276 UNINIT_THIS_MASK = 0x2000,
279 static const char* const
280 type_names [TYPE_MAX + 1] = {
281 "Invalid",
282 "Int32",
283 "Int64",
284 "Native Int",
285 "Float64",
286 "Native Pointer",
287 "Complex"
290 enum {
291 PREFIX_UNALIGNED = 1,
292 PREFIX_VOLATILE = 2,
293 PREFIX_TAIL = 4,
294 PREFIX_CONSTRAINED = 8,
295 PREFIX_READONLY = 16
297 //////////////////////////////////////////////////////////////////
299 #ifdef ENABLE_VERIFIER_STATS
301 #define MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
302 #define MEM_FREE(amt) do { working_set -= (amt); } while (0)
304 static int allocated_memory;
305 static int working_set;
306 static int max_allocated_memory;
307 static int max_working_set;
308 static int total_allocated_memory;
310 static void
311 finish_collect_stats (void)
313 max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
314 max_working_set = MAX (max_working_set, working_set);
315 total_allocated_memory += allocated_memory;
316 allocated_memory = working_set = 0;
319 static void
320 init_verifier_stats (void)
322 static gboolean inited;
323 if (!inited) {
324 inited = TRUE;
325 mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
326 mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
327 mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
331 #else
333 #define MEM_ALLOC(amt) do {} while (0)
334 #define MEM_FREE(amt) do { } while (0)
336 #define finish_collect_stats()
337 #define init_verifier_stats()
339 #endif
342 //////////////////////////////////////////////////////////////////
345 /*Token validation macros and functions */
346 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
347 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
348 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
349 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
351 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
352 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
353 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
354 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
355 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
356 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
359 * Verify if @token refers to a valid row on int's table.
361 static gboolean
362 token_bounds_check (MonoImage *image, guint32 token)
364 if (image->dynamic)
365 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
366 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
369 static MonoType *
370 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
372 MonoType *res = g_new0 (MonoType, 1);
373 MEM_ALLOC (sizeof (MonoType));
375 //FIXME use mono_method_get_signature_full
376 res->data.method = mono_method_signature (method);
377 res->type = MONO_TYPE_FNPTR;
378 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
379 return res;
383 * mono_type_is_enum_type:
385 * Returns TRUE if @type is an enum type.
387 static gboolean
388 mono_type_is_enum_type (MonoType *type)
390 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
391 return TRUE;
392 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
393 return TRUE;
394 return FALSE;
398 * mono_type_is_value_type:
400 * Returns TRUE if @type is named after @namespace.@name.
403 static gboolean
404 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
406 return type->type == MONO_TYPE_VALUETYPE &&
407 !strcmp (namespace, type->data.klass->name_space) &&
408 !strcmp (name, type->data.klass->name);
412 * Returns TURE if @type is VAR or MVAR
414 static gboolean
415 mono_type_is_generic_argument (MonoType *type)
417 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
421 * mono_type_get_underlying_type_any:
423 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
425 * Returns the underlying type of @type regardless if it is byref or not.
427 static MonoType*
428 mono_type_get_underlying_type_any (MonoType *type)
430 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
431 return mono_class_enum_basetype (type->data.klass);
432 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
433 return mono_class_enum_basetype (type->data.generic_class->container_class);
434 return type;
437 static G_GNUC_UNUSED const char*
438 mono_type_get_stack_name (MonoType *type)
440 return type_names [get_stack_type (type) & TYPE_MASK];
443 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
444 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
446 static gboolean
447 mono_method_is_constructor (MonoMethod *method)
449 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
450 !(method->flags & CTOR_INVALID_FLAGS) &&
451 !strcmp (".ctor", method->name));
454 static gboolean
455 mono_class_has_default_constructor (MonoClass *klass)
457 MonoMethod *method;
458 int i;
460 mono_class_setup_methods (klass);
461 if (klass->exception_type)
462 return FALSE;
464 for (i = 0; i < klass->method.count; ++i) {
465 method = klass->methods [i];
466 if (mono_method_is_constructor (method) &&
467 mono_method_signature (method) &&
468 mono_method_signature (method)->param_count == 0 &&
469 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
470 return TRUE;
472 return FALSE;
476 * Verify if @type is valid for the given @ctx verification context.
477 * this function checks for VAR and MVAR types that are invalid under the current verifier,
479 static gboolean
480 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
482 int i;
483 MonoGenericInst *inst;
485 switch (type->type) {
486 case MONO_TYPE_VAR:
487 case MONO_TYPE_MVAR:
488 if (!context)
489 return FALSE;
490 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
491 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
492 return FALSE;
493 break;
494 case MONO_TYPE_SZARRAY:
495 return mono_type_is_valid_type_in_context (&type->data.klass->byval_arg, context);
496 case MONO_TYPE_ARRAY:
497 return mono_type_is_valid_type_in_context (&type->data.array->eklass->byval_arg, context);
498 case MONO_TYPE_PTR:
499 return mono_type_is_valid_type_in_context (type->data.type, context);
500 case MONO_TYPE_GENERICINST:
501 inst = type->data.generic_class->context.class_inst;
502 if (!inst->is_open)
503 break;
504 for (i = 0; i < inst->type_argc; ++i)
505 if (!mono_type_is_valid_type_in_context (inst->type_argv [i], context))
506 return FALSE;
507 break;
509 return TRUE;
512 /*This function returns NULL if the type is not instantiatable*/
513 static MonoType*
514 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
516 MonoError error;
517 MonoType *result;
519 result = mono_class_inflate_generic_type_checked (type, context, &error);
520 if (!mono_error_ok (&error)) {
521 mono_error_cleanup (&error);
522 return NULL;
524 return result;
528 static gboolean
529 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
531 MonoError error;
532 int i;
534 if (ginst->type_argc != gc->type_argc)
535 return FALSE;
537 for (i = 0; i < gc->type_argc; ++i) {
538 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
539 MonoClass *paramClass;
540 MonoClass **constraints;
542 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
543 continue;
544 if (mono_type_is_generic_argument (ginst->type_argv [i]))
545 continue; //it's not our job to validate type variables
547 paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
549 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
550 return FALSE;
552 /*it's not safe to call mono_class_init from here*/
553 if (paramClass->generic_class && !paramClass->inited) {
554 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
555 return FALSE;
558 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
559 return FALSE;
561 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
562 return FALSE;
564 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
565 return FALSE;
567 if (!param_info->constraints)
568 continue;
570 for (constraints = param_info->constraints; *constraints; ++constraints) {
571 MonoClass *ctr = *constraints;
572 MonoType *inflated;
574 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
575 if (!mono_error_ok (&error)) {
576 mono_error_cleanup (&error);
577 return FALSE;
579 ctr = mono_class_from_mono_type (inflated);
580 mono_metadata_free_type (inflated);
582 if (!mono_class_is_assignable_from_slow (ctr, paramClass))
583 return FALSE;
586 return TRUE;
590 * Return true if @candidate is constraint compatible with @target.
592 * This means that @candidate constraints are a super set of @target constaints
594 static gboolean
595 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoGenericContext *context)
597 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
598 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
600 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
601 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
602 if ((tmask & cmask) != tmask)
603 return FALSE;
605 if (tinfo->constraints) {
606 MonoClass **target_class, **candidate_class;
607 if (!cinfo->constraints)
608 return FALSE;
609 for (target_class = tinfo->constraints; *target_class; ++target_class) {
610 MonoClass *tc;
611 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
612 if (!inflated)
613 return FALSE;
614 tc = mono_class_from_mono_type (inflated);
615 mono_metadata_free_type (inflated);
617 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
618 MonoClass *cc;
619 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
620 if (!inflated)
621 return FALSE;
622 cc = mono_class_from_mono_type (inflated);
623 mono_metadata_free_type (inflated);
625 if (mono_class_is_assignable_from (tc, cc))
626 break;
628 if (!*candidate_class)
629 return FALSE;
632 return TRUE;
635 static MonoGenericParam*
636 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
638 MonoGenericContainer *gc;
639 MonoMethod *method = ctx->method;
640 int num;
642 num = mono_type_get_generic_param_num (type);
644 if (type->type == MONO_TYPE_VAR) {
645 MonoClass *gtd = method->klass;
646 if (gtd->generic_class)
647 gtd = gtd->generic_class->container_class;
648 gc = gtd->generic_container;
649 } else { //MVAR
650 MonoMethod *gmd = method;
651 if (method->is_inflated)
652 gmd = ((MonoMethodInflated*)method)->declaring;
653 gc = mono_method_get_generic_container (gmd);
655 if (!gc)
656 return FALSE;
657 return mono_generic_container_get_param (gc, num);
663 * Verify if @type is valid for the given @ctx verification context.
664 * this function checks for VAR and MVAR types that are invalid under the current verifier,
665 * This means that it either
667 static gboolean
668 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
670 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
673 static gboolean
674 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
676 int i;
677 for (i = 0; i < ginst->type_argc; ++i) {
678 MonoType *type = ginst->type_argv [i];
679 if (!is_valid_type_in_context (ctx, type))
680 return FALSE;
682 return TRUE;
685 static gboolean
686 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
688 int i;
689 for (i = 0; i < ginst->type_argc; ++i) {
690 MonoType *type = ginst->type_argv [i];
691 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
692 MonoGenericParam *candidate;
694 if (!mono_type_is_generic_argument (type))
695 continue;
697 if (!is_valid_type_in_context (ctx, type))
698 return FALSE;
700 candidate = verifier_get_generic_param_from_type (ctx, type);
702 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, context))
703 return FALSE;
705 return TRUE;
708 static gboolean
709 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
711 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
712 MonoGenericInst *ginst = gmethod->context.method_inst;
713 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
714 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
717 static gboolean
718 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
720 MonoGenericClass *gklass = klass->generic_class;
721 MonoGenericInst *ginst = gklass->context.class_inst;
722 MonoGenericContainer *gc = gklass->container_class->generic_container;
723 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
726 static gboolean
727 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
729 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
730 MonoGenericInst *ginst = gmethod->context.method_inst;
731 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
732 if (!gc) /*non-generic inflated method - it's part of a generic type */
733 return TRUE;
734 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
735 return FALSE;
736 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
740 static gboolean
741 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
743 MonoGenericClass *gklass = klass->generic_class;
744 MonoGenericInst *ginst = gklass->context.class_inst;
745 MonoGenericContainer *gc = gklass->container_class->generic_container;
746 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
747 return FALSE;
748 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
751 static gboolean
752 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
754 MonoClass *klass;
756 if (type == NULL) {
757 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
758 return FALSE;
761 if (!is_valid_type_in_context (ctx, type)) {
762 char *str = mono_type_full_name (type);
763 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
764 type->type == MONO_TYPE_VAR ? "!" : "!!",
765 str,
766 type->type == MONO_TYPE_VAR ? "class" : "method",
767 ctx->ip_offset),
768 MONO_EXCEPTION_BAD_IMAGE);
769 g_free (str);
770 return FALSE;
773 klass = mono_class_from_mono_type (type);
774 mono_class_init (klass);
775 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
776 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
777 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic instantiation of type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
778 else
779 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
780 return FALSE;
783 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
784 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
785 return FALSE;
788 if (!klass->generic_class)
789 return TRUE;
791 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
792 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
793 return FALSE;
796 if (!mono_class_repect_method_constraints (ctx, klass)) {
797 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s (generic args don't respect target's constraints) at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
798 return FALSE;
801 return TRUE;
804 static verify_result_t
805 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
807 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
808 return RESULT_INVALID;
810 if (!method->is_inflated)
811 return RESULT_VALID;
813 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
814 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset), MONO_EXCEPTION_UNVERIFIABLE_IL);
815 return RESULT_INVALID;
818 if (!mono_method_repect_method_constraints (ctx, method)) {
819 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s (generic args don't respect target's constraints) at 0x%04x", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset));
820 return RESULT_UNVERIFIABLE;
822 return RESULT_VALID;
826 static MonoClassField*
827 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
828 MonoClassField *field;
829 MonoClass *klass = NULL;
831 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
832 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid field token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
833 return NULL;
836 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
837 if (!field || !field->parent || !klass) {
838 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load field from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
839 return NULL;
842 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
843 return NULL;
845 *out_klass = klass;
846 return field;
849 static MonoMethod*
850 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
851 MonoMethod* method;
853 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
854 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid method token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
855 return NULL;
858 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
860 if (!method) {
861 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load method from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
862 return NULL;
865 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
866 return NULL;
868 return method;
871 static MonoType*
872 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
873 MonoType* type;
875 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
876 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
877 return NULL;
880 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
882 if (!type) {
883 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load type from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
884 return NULL;
887 if (!mono_type_is_valid_in_context (ctx, type))
888 return NULL;
890 return type;
894 /* stack_slot_get_type:
896 * Returns the stack type of @value. This value includes POINTER_MASK.
898 * Use this function to checks that account for a managed pointer.
900 static gint32
901 stack_slot_get_type (ILStackDesc *value)
903 return value->stype & RAW_TYPE_MASK;
906 /* stack_slot_get_underlying_type:
908 * Returns the stack type of @value. This value does not include POINTER_MASK.
910 * Use this function is cases where the fact that the value could be a managed pointer is
911 * irrelevant. For example, field load doesn't care about this fact of type on stack.
913 static gint32
914 stack_slot_get_underlying_type (ILStackDesc *value)
916 return value->stype & TYPE_MASK;
919 /* stack_slot_is_managed_pointer:
921 * Returns TRUE is @value is a managed pointer.
923 static gboolean
924 stack_slot_is_managed_pointer (ILStackDesc *value)
926 return (value->stype & POINTER_MASK) == POINTER_MASK;
929 /* stack_slot_is_managed_mutability_pointer:
931 * Returns TRUE is @value is a managed mutability pointer.
933 static G_GNUC_UNUSED gboolean
934 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
936 return (value->stype & CMMP_MASK) == CMMP_MASK;
939 /* stack_slot_is_null_literal:
941 * Returns TRUE is @value is the null literal.
943 static gboolean
944 stack_slot_is_null_literal (ILStackDesc *value)
946 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
950 /* stack_slot_is_this_pointer:
952 * Returns TRUE is @value is the this literal
954 static gboolean
955 stack_slot_is_this_pointer (ILStackDesc *value)
957 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
960 /* stack_slot_is_boxed_value:
962 * Returns TRUE is @value is a boxed value
964 static gboolean
965 stack_slot_is_boxed_value (ILStackDesc *value)
967 return (value->stype & BOXED_MASK) == BOXED_MASK;
970 static const char *
971 stack_slot_get_name (ILStackDesc *value)
973 return type_names [value->stype & TYPE_MASK];
976 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
977 if (PRED (value)) { \
978 if (!first) \
979 g_string_append (str, ", "); \
980 g_string_append (str, NAME); \
981 first = FALSE; \
982 } } while (0)
984 static char*
985 stack_slot_stack_type_full_name (ILStackDesc *value)
987 GString *str = g_string_new ("");
988 char *result;
989 gboolean has_pred = FALSE, first = TRUE;
991 if ((value->stype & TYPE_MASK) != value->stype) {
992 g_string_append(str, "[");
993 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
994 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
995 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
996 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
997 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
998 has_pred = TRUE;
1001 if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
1002 if (!has_pred)
1003 g_string_append(str, "[");
1004 if (!first)
1005 g_string_append (str, ", ");
1006 g_string_append (str, "unboxed");
1007 has_pred = TRUE;
1010 if (has_pred)
1011 g_string_append(str, "] ");
1013 g_string_append (str, stack_slot_get_name (value));
1014 result = str->str;
1015 g_string_free (str, FALSE);
1016 return result;
1019 static char*
1020 stack_slot_full_name (ILStackDesc *value)
1022 char *type_name = mono_type_full_name (value->type);
1023 char *stack_name = stack_slot_stack_type_full_name (value);
1024 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1025 g_free (type_name);
1026 g_free (stack_name);
1027 return res;
1030 //////////////////////////////////////////////////////////////////
1031 void
1032 mono_free_verify_list (GSList *list)
1034 MonoVerifyInfoExtended *info;
1035 GSList *tmp;
1037 for (tmp = list; tmp; tmp = tmp->next) {
1038 info = tmp->data;
1039 g_free (info->info.message);
1040 g_free (info);
1042 g_slist_free (list);
1045 #define ADD_ERROR(list,msg) \
1046 do { \
1047 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1048 vinfo->info.status = MONO_VERIFY_ERROR; \
1049 vinfo->info.message = (msg); \
1050 (list) = g_slist_prepend ((list), vinfo); \
1051 } while (0)
1053 #define ADD_WARN(list,code,msg) \
1054 do { \
1055 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1056 vinfo->info.status = (code); \
1057 vinfo->info.message = (msg); \
1058 (list) = g_slist_prepend ((list), vinfo); \
1059 } while (0)
1061 #define ADD_INVALID(list,msg) \
1062 do { \
1063 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1064 vinfo->status = MONO_VERIFY_ERROR; \
1065 vinfo->message = (msg); \
1066 (list) = g_slist_prepend ((list), vinfo); \
1067 /*G_BREAKPOINT ();*/ \
1068 goto invalid_cil; \
1069 } while (0)
1071 #define CHECK_STACK_UNDERFLOW(num) \
1072 do { \
1073 if (cur_stack < (num)) \
1074 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1075 } while (0)
1077 #define CHECK_STACK_OVERFLOW() \
1078 do { \
1079 if (cur_stack >= max_stack) \
1080 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1081 } while (0)
1084 static int
1085 in_any_block (MonoMethodHeader *header, guint offset)
1087 int i;
1088 MonoExceptionClause *clause;
1090 for (i = 0; i < header->num_clauses; ++i) {
1091 clause = &header->clauses [i];
1092 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1093 return 1;
1094 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1095 return 1;
1096 if (MONO_OFFSET_IN_FILTER (clause, offset))
1097 return 1;
1099 return 0;
1103 * in_any_exception_block:
1105 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1107 static gboolean
1108 in_any_exception_block (MonoMethodHeader *header, guint offset)
1110 int i;
1111 MonoExceptionClause *clause;
1113 for (i = 0; i < header->num_clauses; ++i) {
1114 clause = &header->clauses [i];
1115 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1116 return TRUE;
1117 if (MONO_OFFSET_IN_FILTER (clause, offset))
1118 return TRUE;
1120 return FALSE;
1124 * is_valid_branch_instruction:
1126 * Verify if it's valid to perform a branch from @offset to @target.
1127 * This should be used with br and brtrue/false.
1128 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1129 * The major diferent from other similiar functions is that branching into a
1130 * finally/fault block is invalid instead of just unverifiable.
1132 static int
1133 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1135 int i;
1136 MonoExceptionClause *clause;
1138 for (i = 0; i < header->num_clauses; ++i) {
1139 clause = &header->clauses [i];
1140 /*branching into a finally block is invalid*/
1141 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1142 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1143 MONO_OFFSET_IN_HANDLER (clause, target))
1144 return 2;
1146 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1147 return 1;
1148 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1149 return 1;
1150 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1151 return 1;
1153 return 0;
1157 * is_valid_cmp_branch_instruction:
1159 * Verify if it's valid to perform a branch from @offset to @target.
1160 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1161 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1163 * The major diferences from other similar functions are that most errors lead to invalid
1164 * code and only branching out of finally, filter or fault clauses is unverifiable.
1166 static int
1167 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1169 int i;
1170 MonoExceptionClause *clause;
1172 for (i = 0; i < header->num_clauses; ++i) {
1173 clause = &header->clauses [i];
1174 /*branching out of a handler or finally*/
1175 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1176 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1177 !MONO_OFFSET_IN_HANDLER (clause, target))
1178 return 1;
1180 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1181 return 2;
1182 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1183 return 2;
1184 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1185 return 2;
1187 return 0;
1191 * A leave can't escape a finally block
1193 static int
1194 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1196 int i;
1197 MonoExceptionClause *clause;
1199 for (i = 0; i < header->num_clauses; ++i) {
1200 clause = &header->clauses [i];
1201 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1202 return 0;
1203 if (MONO_OFFSET_IN_FILTER (clause, offset))
1204 return 0;
1206 return 1;
1210 * A rethrow can't happen outside of a catch handler.
1212 static int
1213 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1215 int i;
1216 MonoExceptionClause *clause;
1218 for (i = 0; i < header->num_clauses; ++i) {
1219 clause = &header->clauses [i];
1220 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1221 return 1;
1222 if (MONO_OFFSET_IN_FILTER (clause, offset))
1223 return 1;
1225 return 0;
1229 * An endfinally can't happen outside of a finally/fault handler.
1231 static int
1232 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1234 int i;
1235 MonoExceptionClause *clause;
1237 for (i = 0; i < header->num_clauses; ++i) {
1238 clause = &header->clauses [i];
1239 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1240 return 1;
1242 return 0;
1247 * An endfilter can only happens inside a filter clause.
1248 * In non-strict mode filter is allowed inside the handler clause too
1250 static MonoExceptionClause *
1251 is_correct_endfilter (VerifyContext *ctx, guint offset)
1253 int i;
1254 MonoExceptionClause *clause;
1256 for (i = 0; i < ctx->header->num_clauses; ++i) {
1257 clause = &ctx->header->clauses [i];
1258 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1259 continue;
1260 if (MONO_OFFSET_IN_FILTER (clause, offset))
1261 return clause;
1262 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1263 return clause;
1265 return NULL;
1270 * Non-strict endfilter can happens inside a try block or any handler block
1272 static int
1273 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1275 int i;
1276 MonoExceptionClause *clause;
1278 for (i = 0; i < ctx->header->num_clauses; ++i) {
1279 clause = &ctx->header->clauses [i];
1280 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1281 return 1;
1283 return 0;
1286 static gboolean
1287 is_valid_bool_arg (ILStackDesc *arg)
1289 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1290 return TRUE;
1293 switch (stack_slot_get_underlying_type (arg)) {
1294 case TYPE_I4:
1295 case TYPE_I8:
1296 case TYPE_NATIVE_INT:
1297 case TYPE_PTR:
1298 return TRUE;
1299 case TYPE_COMPLEX:
1300 g_assert (arg->type);
1301 switch (arg->type->type) {
1302 case MONO_TYPE_CLASS:
1303 case MONO_TYPE_STRING:
1304 case MONO_TYPE_OBJECT:
1305 case MONO_TYPE_SZARRAY:
1306 case MONO_TYPE_ARRAY:
1307 case MONO_TYPE_FNPTR:
1308 case MONO_TYPE_PTR:
1309 return TRUE;
1310 case MONO_TYPE_GENERICINST:
1311 /*We need to check if the container class
1312 * of the generic type is a valuetype, iow:
1313 * is it a "class Foo<T>" or a "struct Foo<T>"?
1315 return !arg->type->data.generic_class->container_class->valuetype;
1317 default:
1318 return FALSE;
1323 /*Type manipulation helper*/
1325 /*Returns the byref version of the supplied MonoType*/
1326 static MonoType*
1327 mono_type_get_type_byref (MonoType *type)
1329 if (type->byref)
1330 return type;
1331 return &mono_class_from_mono_type (type)->this_arg;
1335 /*Returns the byval version of the supplied MonoType*/
1336 static MonoType*
1337 mono_type_get_type_byval (MonoType *type)
1339 if (!type->byref)
1340 return type;
1341 return &mono_class_from_mono_type (type)->byval_arg;
1344 static MonoType*
1345 mono_type_from_stack_slot (ILStackDesc *slot)
1347 if (stack_slot_is_managed_pointer (slot))
1348 return mono_type_get_type_byref (slot->type);
1349 return slot->type;
1352 /*Stack manipulation code*/
1354 static void
1355 ensure_stack_size (ILCodeDesc *stack, int required)
1357 int new_size = 8;
1358 ILStackDesc *tmp;
1360 if (required < stack->max_size)
1361 return;
1363 /* We don't have to worry about the exponential growth since stack_copy prune unused space */
1364 new_size = MAX (8, MAX (required, stack->max_size * 2));
1366 g_assert (new_size >= stack->size);
1367 g_assert (new_size >= required);
1369 tmp = g_new0 (ILStackDesc, new_size);
1370 MEM_ALLOC (sizeof (ILStackDesc) * new_size);
1372 if (stack->stack) {
1373 if (stack->size)
1374 memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
1375 g_free (stack->stack);
1376 MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
1379 stack->stack = tmp;
1380 stack->max_size = new_size;
1383 static void
1384 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1386 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1387 return;
1388 state->size = state->max_size = 0;
1389 state->flags |= IL_CODE_FLAG_STACK_INITED;
1392 static void
1393 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1395 ensure_stack_size (to, from->size);
1396 to->size = from->size;
1398 /*stack copy happens at merge points, which have small stacks*/
1399 if (from->size)
1400 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1403 static void
1404 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1406 to->stype = from->stype;
1407 to->type = from->type;
1408 to->method = from->method;
1411 static int
1412 check_underflow (VerifyContext *ctx, int size)
1414 if (ctx->eval.size < size) {
1415 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1416 return 0;
1418 return 1;
1421 static int
1422 check_overflow (VerifyContext *ctx)
1424 if (ctx->eval.size >= ctx->max_stack) {
1425 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1426 return 0;
1428 return 1;
1431 /*This reject out PTR, FNPTR and TYPEDBYREF*/
1432 static gboolean
1433 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1435 if (stack_slot_get_type (value) == TYPE_PTR) {
1436 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1437 return 0;
1439 return 1;
1442 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
1443 static gboolean
1444 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1446 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1447 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1448 return 0;
1450 return 1;
1453 static ILStackDesc *
1454 stack_push (VerifyContext *ctx)
1456 g_assert (ctx->eval.size < ctx->max_stack);
1457 g_assert (ctx->eval.size <= ctx->eval.max_size);
1459 ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
1461 return & ctx->eval.stack [ctx->eval.size++];
1464 static ILStackDesc *
1465 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1467 ILStackDesc *top = stack_push (ctx);
1468 top->stype = stype;
1469 top->type = type;
1470 return top;
1473 static ILStackDesc *
1474 stack_pop (VerifyContext *ctx)
1476 ILStackDesc *ret;
1477 g_assert (ctx->eval.size > 0);
1478 ret = ctx->eval.stack + --ctx->eval.size;
1479 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
1480 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
1481 return ret;
1484 /* This function allows to safely pop an unititialized this ptr from
1485 * the eval stack without marking the method as unverifiable.
1487 static ILStackDesc *
1488 stack_pop_safe (VerifyContext *ctx)
1490 g_assert (ctx->eval.size > 0);
1491 return ctx->eval.stack + --ctx->eval.size;
1494 /*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
1495 static ILStackDesc*
1496 stack_peek (VerifyContext *ctx, int distance)
1498 g_assert (ctx->eval.size - distance > 0);
1499 return ctx->eval.stack + (ctx->eval.size - 1 - distance);
1502 static ILStackDesc *
1503 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
1505 ILStackDesc *top = stack_push (ctx);
1506 copy_stack_value (top, value);
1507 return top;
1510 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1512 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1513 * */
1514 static MonoType*
1515 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1517 MonoType *type;
1518 MonoClass *class;
1520 if (!(type = verifier_load_type (ctx, token, opcode)))
1521 return NULL;
1523 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1524 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1525 return NULL;
1528 if (type->type == MONO_TYPE_VOID) {
1529 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1530 return NULL;
1533 if (type->type == MONO_TYPE_TYPEDBYREF)
1534 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1536 if (!(class = mono_class_from_mono_type (type)))
1537 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
1539 if (class->generic_container && type->type != MONO_TYPE_GENERICINST)
1540 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the generic type definition in a boxable type position for %s at 0x%04x", opcode, ctx->ip_offset));
1542 check_unverifiable_type (ctx, type);
1543 return type;
1547 /*operation result tables */
1549 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1550 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1551 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1552 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1553 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1554 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1555 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1558 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1559 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1560 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1561 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1562 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1563 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1564 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1567 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1568 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1569 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1570 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1571 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1572 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1573 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1576 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1577 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1578 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1579 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1580 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1581 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1582 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1585 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1586 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1587 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1588 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1589 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1590 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1591 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1594 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1595 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1596 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1597 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1598 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1599 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1600 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1603 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1604 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1605 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1606 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1607 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1608 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1609 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1612 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1613 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1614 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1615 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1616 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1617 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1618 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1621 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1622 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1623 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1624 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1625 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1626 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1627 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1630 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1631 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1632 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1633 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1634 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1635 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1636 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1639 #ifdef MONO_VERIFIER_DEBUG
1641 /*debug helpers */
1642 static void
1643 dump_stack_value (ILStackDesc *value)
1645 printf ("[(%x)(%x)", value->type->type, value->stype);
1647 if (stack_slot_is_this_pointer (value))
1648 printf ("[this] ");
1650 if (stack_slot_is_boxed_value (value))
1651 printf ("[boxed] ");
1653 if (stack_slot_is_null_literal (value))
1654 printf ("[null] ");
1656 if (stack_slot_is_managed_mutability_pointer (value))
1657 printf ("Controled Mutability MP: ");
1659 if (stack_slot_is_managed_pointer (value))
1660 printf ("Managed Pointer to: ");
1662 switch (stack_slot_get_underlying_type (value)) {
1663 case TYPE_INV:
1664 printf ("invalid type]");
1665 return;
1666 case TYPE_I4:
1667 printf ("int32]");
1668 return;
1669 case TYPE_I8:
1670 printf ("int64]");
1671 return;
1672 case TYPE_NATIVE_INT:
1673 printf ("native int]");
1674 return;
1675 case TYPE_R8:
1676 printf ("float64]");
1677 return;
1678 case TYPE_PTR:
1679 printf ("unmanaged pointer]");
1680 return;
1681 case TYPE_COMPLEX:
1682 switch (value->type->type) {
1683 case MONO_TYPE_CLASS:
1684 case MONO_TYPE_VALUETYPE:
1685 printf ("complex] (%s)", value->type->data.klass->name);
1686 return;
1687 case MONO_TYPE_STRING:
1688 printf ("complex] (string)");
1689 return;
1690 case MONO_TYPE_OBJECT:
1691 printf ("complex] (object)");
1692 return;
1693 case MONO_TYPE_SZARRAY:
1694 printf ("complex] (%s [])", value->type->data.klass->name);
1695 return;
1696 case MONO_TYPE_ARRAY:
1697 printf ("complex] (%s [%d %d %d])",
1698 value->type->data.array->eklass->name,
1699 value->type->data.array->rank,
1700 value->type->data.array->numsizes,
1701 value->type->data.array->numlobounds);
1702 return;
1703 case MONO_TYPE_GENERICINST:
1704 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1705 return;
1706 case MONO_TYPE_VAR:
1707 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1708 return;
1709 case MONO_TYPE_MVAR:
1710 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
1711 return;
1712 default: {
1713 //should be a boxed value
1714 char * name = mono_type_full_name (value->type);
1715 printf ("complex] %s", name);
1716 g_free (name);
1717 return;
1720 default:
1721 printf ("unknown stack %x type]\n", value->stype);
1722 g_assert_not_reached ();
1726 static void
1727 dump_stack_state (ILCodeDesc *state)
1729 int i;
1731 printf ("(%d) ", state->size);
1732 for (i = 0; i < state->size; ++i)
1733 dump_stack_value (state->stack + i);
1734 printf ("\n");
1736 #endif
1738 /*Returns TRUE if candidate array type can be assigned to target.
1739 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1741 static gboolean
1742 is_array_type_compatible (MonoType *target, MonoType *candidate)
1744 MonoArrayType *left = target->data.array;
1745 MonoArrayType *right = candidate->data.array;
1747 g_assert (target->type == MONO_TYPE_ARRAY);
1748 g_assert (candidate->type == MONO_TYPE_ARRAY);
1750 if (left->rank != right->rank)
1751 return FALSE;
1753 return mono_class_is_assignable_from (left->eklass, right->eklass);
1756 static int
1757 get_stack_type (MonoType *type)
1759 int mask = 0;
1760 int type_kind = type->type;
1761 if (type->byref)
1762 mask = POINTER_MASK;
1763 /*TODO handle CMMP_MASK */
1765 handle_enum:
1766 switch (type_kind) {
1767 case MONO_TYPE_I1:
1768 case MONO_TYPE_U1:
1769 case MONO_TYPE_BOOLEAN:
1770 case MONO_TYPE_I2:
1771 case MONO_TYPE_U2:
1772 case MONO_TYPE_CHAR:
1773 case MONO_TYPE_I4:
1774 case MONO_TYPE_U4:
1775 return TYPE_I4 | mask;
1777 case MONO_TYPE_I:
1778 case MONO_TYPE_U:
1779 return TYPE_NATIVE_INT | mask;
1781 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1782 case MONO_TYPE_FNPTR:
1783 case MONO_TYPE_PTR:
1784 case MONO_TYPE_TYPEDBYREF:
1785 return TYPE_PTR | mask;
1787 case MONO_TYPE_VAR:
1788 case MONO_TYPE_MVAR:
1790 case MONO_TYPE_CLASS:
1791 case MONO_TYPE_STRING:
1792 case MONO_TYPE_OBJECT:
1793 case MONO_TYPE_SZARRAY:
1794 case MONO_TYPE_ARRAY:
1795 return TYPE_COMPLEX | mask;
1797 case MONO_TYPE_I8:
1798 case MONO_TYPE_U8:
1799 return TYPE_I8 | mask;
1801 case MONO_TYPE_R4:
1802 case MONO_TYPE_R8:
1803 return TYPE_R8 | mask;
1805 case MONO_TYPE_GENERICINST:
1806 case MONO_TYPE_VALUETYPE:
1807 if (mono_type_is_enum_type (type)) {
1808 type = mono_type_get_underlying_type_any (type);
1809 if (!type)
1810 return FALSE;
1811 type_kind = type->type;
1812 goto handle_enum;
1813 } else {
1814 return TYPE_COMPLEX | mask;
1817 default:
1818 return TYPE_INV;
1822 /* convert MonoType to ILStackDesc format (stype) */
1823 static gboolean
1824 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1826 int mask = 0;
1827 int type_kind = type->type;
1829 if (type->byref || take_addr)
1830 mask = POINTER_MASK;
1831 /* TODO handle CMMP_MASK */
1833 handle_enum:
1834 stack->type = type;
1836 switch (type_kind) {
1837 case MONO_TYPE_I1:
1838 case MONO_TYPE_U1:
1839 case MONO_TYPE_BOOLEAN:
1840 case MONO_TYPE_I2:
1841 case MONO_TYPE_U2:
1842 case MONO_TYPE_CHAR:
1843 case MONO_TYPE_I4:
1844 case MONO_TYPE_U4:
1845 stack->stype = TYPE_I4 | mask;
1846 break;
1847 case MONO_TYPE_I:
1848 case MONO_TYPE_U:
1849 stack->stype = TYPE_NATIVE_INT | mask;
1850 break;
1852 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1853 case MONO_TYPE_FNPTR:
1854 case MONO_TYPE_PTR:
1855 case MONO_TYPE_TYPEDBYREF:
1856 stack->stype = TYPE_PTR | mask;
1857 break;
1859 case MONO_TYPE_CLASS:
1860 case MONO_TYPE_STRING:
1861 case MONO_TYPE_OBJECT:
1862 case MONO_TYPE_SZARRAY:
1863 case MONO_TYPE_ARRAY:
1865 case MONO_TYPE_VAR:
1866 case MONO_TYPE_MVAR:
1867 stack->stype = TYPE_COMPLEX | mask;
1868 break;
1870 case MONO_TYPE_I8:
1871 case MONO_TYPE_U8:
1872 stack->stype = TYPE_I8 | mask;
1873 break;
1874 case MONO_TYPE_R4:
1875 case MONO_TYPE_R8:
1876 stack->stype = TYPE_R8 | mask;
1877 break;
1878 case MONO_TYPE_GENERICINST:
1879 case MONO_TYPE_VALUETYPE:
1880 if (mono_type_is_enum_type (type)) {
1881 MonoType *utype = mono_type_get_underlying_type_any (type);
1882 if (!utype) {
1883 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
1884 return FALSE;
1886 type = utype;
1887 type_kind = type->type;
1888 goto handle_enum;
1889 } else {
1890 stack->stype = TYPE_COMPLEX | mask;
1891 break;
1893 default:
1894 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1895 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
1896 return FALSE;
1898 return TRUE;
1902 * init_stack_with_value_at_exception_boundary:
1904 * Initialize the stack and push a given type.
1905 * The instruction is marked as been on the exception boundary.
1907 static void
1908 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
1910 MonoError error;
1911 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
1913 if (!mono_error_ok (&error)) {
1914 char *name = mono_type_get_full_name (klass);
1915 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
1916 g_free (name);
1917 mono_error_cleanup (&error);
1918 return;
1921 if (!ctx->max_stack) {
1922 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
1923 return;
1926 stack_init (ctx, code);
1927 ensure_stack_size (code, 1);
1928 set_stack_value (ctx, code->stack, type, FALSE);
1929 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
1930 code->size = 1;
1931 code->flags |= IL_CODE_FLAG_WAS_TARGET;
1932 if (mono_type_is_generic_argument (type))
1933 code->stack->stype |= BOXED_MASK;
1936 /*Verify if type 'candidate' can be stored in type 'target'.
1938 * If strict, check for the underlying type and not the verification stack types
1940 static gboolean
1941 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1943 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1944 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1946 MonoType *original_candidate = candidate;
1947 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
1949 /*only one is byref */
1950 if (candidate->byref ^ target->byref) {
1951 /* converting from native int to byref*/
1952 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1953 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1954 return TRUE;
1956 return FALSE;
1958 strict |= target->byref;
1959 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
1960 candidate = mono_type_get_underlying_type_any (candidate);
1962 handle_enum:
1963 switch (target->type) {
1964 case MONO_TYPE_VOID:
1965 return candidate->type == MONO_TYPE_VOID;
1966 case MONO_TYPE_I1:
1967 case MONO_TYPE_U1:
1968 case MONO_TYPE_BOOLEAN:
1969 if (strict)
1970 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1971 case MONO_TYPE_I2:
1972 case MONO_TYPE_U2:
1973 case MONO_TYPE_CHAR:
1974 if (strict)
1975 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1976 case MONO_TYPE_I4:
1977 case MONO_TYPE_U4: {
1978 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1979 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1980 if (strict)
1981 return is_native_int || is_int4;
1982 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1985 case MONO_TYPE_I8:
1986 case MONO_TYPE_U8:
1987 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
1989 case MONO_TYPE_R4:
1990 case MONO_TYPE_R8:
1991 if (strict)
1992 return candidate->type == target->type;
1993 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
1995 case MONO_TYPE_I:
1996 case MONO_TYPE_U: {
1997 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1998 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1999 if (strict)
2000 return is_native_int || is_int4;
2001 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2004 case MONO_TYPE_PTR:
2005 if (candidate->type != MONO_TYPE_PTR)
2006 return FALSE;
2007 /* check the underlying type */
2008 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2010 case MONO_TYPE_FNPTR: {
2011 MonoMethodSignature *left, *right;
2012 if (candidate->type != MONO_TYPE_FNPTR)
2013 return FALSE;
2015 left = mono_type_get_signature (target);
2016 right = mono_type_get_signature (candidate);
2017 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2020 case MONO_TYPE_GENERICINST: {
2021 MonoClass *target_klass;
2022 MonoClass *candidate_klass;
2023 if (mono_type_is_enum_type (target)) {
2024 target = mono_type_get_underlying_type_any (target);
2025 if (!target)
2026 return FALSE;
2027 goto handle_enum;
2030 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2031 * to take boxing status into account.
2033 if (mono_type_is_generic_argument (original_candidate))
2034 return FALSE;
2036 target_klass = mono_class_from_mono_type (target);
2037 candidate_klass = mono_class_from_mono_type (candidate);
2038 if (mono_class_is_nullable (target_klass)) {
2039 if (!mono_class_is_nullable (candidate_klass))
2040 return FALSE;
2041 return target_klass == candidate_klass;
2044 return mono_class_is_assignable_from (target_klass, candidate_klass);
2047 case MONO_TYPE_STRING:
2048 return candidate->type == MONO_TYPE_STRING;
2050 case MONO_TYPE_CLASS:
2052 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2053 * to take boxing status into account.
2055 if (mono_type_is_generic_argument (original_candidate))
2056 return FALSE;
2058 if (candidate->type == MONO_TYPE_VALUETYPE)
2059 return FALSE;
2061 /* If candidate is an enum it should return true for System.Enum and supertypes.
2062 * That's why here we use the original type and not the underlying type.
2064 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2066 case MONO_TYPE_OBJECT:
2067 return MONO_TYPE_IS_REFERENCE (candidate);
2069 case MONO_TYPE_SZARRAY: {
2070 MonoClass *left;
2071 MonoClass *right;
2072 if (candidate->type != MONO_TYPE_SZARRAY)
2073 return FALSE;
2075 left = mono_class_from_mono_type (target);
2076 right = mono_class_from_mono_type (candidate);
2078 return mono_class_is_assignable_from (left, right);
2081 case MONO_TYPE_ARRAY:
2082 if (candidate->type != MONO_TYPE_ARRAY)
2083 return FALSE;
2084 return is_array_type_compatible (target, candidate);
2086 case MONO_TYPE_TYPEDBYREF:
2087 return candidate->type == MONO_TYPE_TYPEDBYREF;
2089 case MONO_TYPE_VALUETYPE: {
2090 MonoClass *target_klass;
2091 MonoClass *candidate_klass;
2093 if (candidate->type == MONO_TYPE_CLASS)
2094 return FALSE;
2096 target_klass = mono_class_from_mono_type (target);
2097 candidate_klass = mono_class_from_mono_type (candidate);
2098 if (target_klass == candidate_klass)
2099 return TRUE;
2100 if (mono_type_is_enum_type (target)) {
2101 target = mono_type_get_underlying_type_any (target);
2102 if (!target)
2103 return FALSE;
2104 goto handle_enum;
2106 return FALSE;
2109 case MONO_TYPE_VAR:
2110 if (candidate->type != MONO_TYPE_VAR)
2111 return FALSE;
2112 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2114 case MONO_TYPE_MVAR:
2115 if (candidate->type != MONO_TYPE_MVAR)
2116 return FALSE;
2117 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2119 default:
2120 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2121 g_assert_not_reached ();
2122 return FALSE;
2124 return 1;
2125 #undef IS_ONE_OF3
2126 #undef IS_ONE_OF2
2129 static gboolean
2130 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2132 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2136 * Returns the generic param bound to the context been verified.
2139 static MonoGenericParam*
2140 get_generic_param (VerifyContext *ctx, MonoType *param)
2142 guint16 param_num = mono_type_get_generic_param_num (param);
2143 if (param->type == MONO_TYPE_VAR) {
2144 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2145 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2146 return NULL;
2148 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2151 /*param must be a MVAR */
2152 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2153 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2154 return NULL;
2156 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2160 static gboolean
2161 recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
2163 MonoType *constraint_type = &constraint_class->byval_arg;
2164 if (recursion_level <= 0)
2165 return FALSE;
2167 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
2168 return TRUE;
2170 if (mono_type_is_generic_argument (constraint_type)) {
2171 MonoGenericParam *param = get_generic_param (ctx, constraint_type);
2172 MonoClass **class;
2173 if (!param)
2174 return FALSE;
2175 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2176 if (recursive_boxed_constraint_type_check (ctx, type, *class, recursion_level - 1))
2177 return TRUE;
2180 return FALSE;
2184 * is_compatible_boxed_valuetype:
2186 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2188 * @type The source type. It it tested to be of the proper type.
2189 * @candidate type of the boxed valuetype.
2190 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2191 * @strict if TRUE candidate must be boxed compatible to the target type
2194 static gboolean
2195 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2197 if (!stack_slot_is_boxed_value (stack))
2198 return FALSE;
2199 if (type->byref || candidate->byref)
2200 return FALSE;
2202 if (mono_type_is_generic_argument (candidate)) {
2203 MonoGenericParam *param = get_generic_param (ctx, candidate);
2204 MonoClass **class;
2205 if (!param)
2206 return FALSE;
2208 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2209 /*256 should be enough since there can't be more than 255 generic arguments.*/
2210 if (recursive_boxed_constraint_type_check (ctx, type, *class, 256))
2211 return TRUE;
2215 if (mono_type_is_generic_argument (type))
2216 return FALSE;
2218 if (!strict)
2219 return TRUE;
2221 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2224 static int
2225 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2227 MonoType *candidate = mono_type_from_stack_slot (stack);
2228 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2229 return TRUE;
2231 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2232 return TRUE;
2234 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2235 return FALSE;
2237 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2238 return FALSE;
2240 if (drop_byref)
2241 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2243 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2246 static int
2247 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2249 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2252 static gboolean
2253 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2255 if (candidate->byref ^ target->byref)
2256 return FALSE;
2258 switch (target->type) {
2259 case MONO_TYPE_VOID:
2260 case MONO_TYPE_I1:
2261 case MONO_TYPE_U1:
2262 case MONO_TYPE_BOOLEAN:
2263 case MONO_TYPE_I2:
2264 case MONO_TYPE_U2:
2265 case MONO_TYPE_CHAR:
2266 case MONO_TYPE_I4:
2267 case MONO_TYPE_U4:
2268 case MONO_TYPE_I8:
2269 case MONO_TYPE_U8:
2270 case MONO_TYPE_R4:
2271 case MONO_TYPE_R8:
2272 case MONO_TYPE_I:
2273 case MONO_TYPE_U:
2274 case MONO_TYPE_STRING:
2275 case MONO_TYPE_TYPEDBYREF:
2276 return candidate->type == target->type;
2278 case MONO_TYPE_PTR:
2279 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2281 case MONO_TYPE_FNPTR:
2282 if (candidate->type != MONO_TYPE_FNPTR)
2283 return FALSE;
2284 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2286 case MONO_TYPE_GENERICINST: {
2287 MonoClass *target_klass;
2288 MonoClass *candidate_klass;
2289 target_klass = mono_class_from_mono_type (target);
2290 candidate_klass = mono_class_from_mono_type (candidate);
2291 /*FIXME handle nullables and enum*/
2292 return mono_class_is_assignable_from (target_klass, candidate_klass);
2294 case MONO_TYPE_OBJECT:
2295 return MONO_TYPE_IS_REFERENCE (candidate);
2297 case MONO_TYPE_CLASS:
2298 return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2300 case MONO_TYPE_SZARRAY:
2301 if (candidate->type != MONO_TYPE_SZARRAY)
2302 return FALSE;
2303 return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2305 case MONO_TYPE_ARRAY:
2306 if (candidate->type != MONO_TYPE_ARRAY)
2307 return FALSE;
2308 return is_array_type_compatible (target, candidate);
2310 case MONO_TYPE_VALUETYPE:
2311 /*FIXME handle nullables and enum*/
2312 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2314 case MONO_TYPE_VAR:
2315 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2316 return FALSE;
2318 case MONO_TYPE_MVAR:
2319 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2320 return FALSE;
2322 default:
2323 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2324 g_assert_not_reached ();
2325 return FALSE;
2329 static gboolean
2330 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2332 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2333 return TRUE;
2335 return mono_delegate_type_equal (method, delegate);
2338 static gboolean
2339 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2341 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2342 return TRUE;
2344 return mono_delegate_type_equal (delegate, method);
2348 * mono_delegate_signature_equal:
2350 * Compare two signatures in the way expected by delegates.
2352 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2354 * FIXME can this function be eliminated and proper metadata functionality be used?
2356 static gboolean
2357 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2359 int i;
2360 int method_offset = is_static_ldftn ? 1 : 0;
2362 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2363 return FALSE;
2365 if (delegate_sig->call_convention != method_sig->call_convention)
2366 return FALSE;
2368 for (i = 0; i < delegate_sig->param_count; i++) {
2369 MonoType *p1 = delegate_sig->params [i];
2370 MonoType *p2 = method_sig->params [i + method_offset];
2372 if (!mono_delegate_param_equal (p1, p2))
2373 return FALSE;
2376 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2377 return FALSE;
2379 return TRUE;
2383 * verify_ldftn_delegate:
2385 * Verify properties of ldftn based delegates.
2387 static void
2388 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2390 MonoMethod *method = funptr->method;
2392 /*ldftn non-final virtuals only allowed if method is not static,
2393 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2394 * This rules doesn't apply if the object on stack is a boxed valuetype.
2396 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2397 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2398 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2399 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2401 /*current method must not be static*/
2402 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2403 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2405 /*value is the this pointer, loaded using ldarg.0 */
2406 if (!stack_slot_is_this_pointer (value))
2407 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object argument, it is not the this pointer, to ldftn with virtual method at 0x%04x", ctx->ip_offset));
2409 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2414 * verify_delegate_compatibility:
2416 * Verify delegate creation sequence.
2419 static void
2420 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2422 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2423 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2425 MonoMethod *invoke, *method;
2426 const guint8 *ip = ctx->header->code;
2427 guint32 ip_offset = ctx->ip_offset;
2428 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2430 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2431 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2432 return;
2435 invoke = mono_get_delegate_invoke (delegate);
2436 method = funptr->method;
2438 if (!method || !mono_method_signature (method)) {
2439 char *name = mono_type_get_full_name (delegate);
2440 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2441 g_free (name);
2442 return;
2445 if (!invoke || !mono_method_signature (invoke)) {
2446 char *name = mono_type_get_full_name (delegate);
2447 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2448 g_free (name);
2449 return;
2452 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2454 if (is_static_ldftn)
2455 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2457 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2458 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2459 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2460 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer signature '%s' doesn't match delegate's signature '%s' at 0x%04x", fun_sig, invoke_sig, ctx->ip_offset));
2461 g_free (fun_sig);
2462 g_free (invoke_sig);
2466 * Delegate code sequences:
2467 * [-6] ldftn token
2468 * newobj ...
2471 * [-7] dup
2472 * [-6] ldvirtftn token
2473 * newobj ...
2475 * ldftn sequence:*/
2476 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2477 verify_ldftn_delegate (ctx, delegate, value, funptr);
2478 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2479 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2480 }else {
2481 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2483 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2485 //general tests
2486 if (is_first_arg_bound) {
2487 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
2488 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2489 } else {
2490 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
2491 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
2492 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
2493 } else {
2494 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
2495 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2499 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2500 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2502 #undef IS_VALID_OPCODE
2503 #undef IS_LOAD_FUN_PTR
2506 /* implement the opcode checks*/
2507 static void
2508 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2510 ILStackDesc *top;
2512 if (arg >= ctx->max_args) {
2513 if (take_addr)
2514 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2515 else {
2516 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2517 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2518 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2520 } else if (check_overflow (ctx)) {
2521 /*We must let the value be pushed, otherwise we would get an underflow error*/
2522 check_unverifiable_type (ctx, ctx->params [arg]);
2523 if (ctx->params [arg]->byref && take_addr)
2524 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2525 top = stack_push (ctx);
2526 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
2527 return;
2529 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2530 if (take_addr)
2531 ctx->has_this_store = TRUE;
2532 else
2533 top->stype |= THIS_POINTER_MASK;
2534 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
2535 top->stype |= UNINIT_THIS_MASK;
2540 static void
2541 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2543 if (arg >= ctx->num_locals) {
2544 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2545 } else if (check_overflow (ctx)) {
2546 /*We must let the value be pushed, otherwise we would get an underflow error*/
2547 check_unverifiable_type (ctx, ctx->locals [arg]);
2548 if (ctx->locals [arg]->byref && take_addr)
2549 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2551 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2555 static void
2556 store_arg (VerifyContext *ctx, guint32 arg)
2558 ILStackDesc *value;
2560 if (arg >= ctx->max_args) {
2561 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2562 if (check_underflow (ctx, 1))
2563 stack_pop (ctx);
2564 return;
2567 if (check_underflow (ctx, 1)) {
2568 value = stack_pop (ctx);
2569 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2570 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2573 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2574 ctx->has_this_store = 1;
2577 static void
2578 store_local (VerifyContext *ctx, guint32 arg)
2580 ILStackDesc *value;
2581 if (arg >= ctx->num_locals) {
2582 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2583 return;
2586 /*TODO verify definite assigment */
2587 if (check_underflow (ctx, 1)) {
2588 value = stack_pop(ctx);
2589 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2590 char *expected = mono_type_full_name (ctx->locals [arg]);
2591 char *found = stack_slot_full_name (value);
2592 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
2593 found,
2594 arg,
2595 expected,
2596 ctx->ip_offset));
2597 g_free (expected);
2598 g_free (found);
2603 /*FIXME add and sub needs special care here*/
2604 static void
2605 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2607 ILStackDesc *a, *b, *top;
2608 int idxa, idxb, complexMerge = 0;
2609 unsigned char res;
2611 if (!check_underflow (ctx, 2))
2612 return;
2613 b = stack_pop (ctx);
2614 a = stack_pop (ctx);
2616 idxa = stack_slot_get_underlying_type (a);
2617 if (stack_slot_is_managed_pointer (a)) {
2618 idxa = TYPE_PTR;
2619 complexMerge = 1;
2622 idxb = stack_slot_get_underlying_type (b);
2623 if (stack_slot_is_managed_pointer (b)) {
2624 idxb = TYPE_PTR;
2625 complexMerge = 2;
2628 --idxa;
2629 --idxb;
2630 res = table [idxa][idxb];
2632 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2633 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2635 top = stack_push (ctx);
2636 if (res == TYPE_INV) {
2637 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction applyed to ill formed stack (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
2638 copy_stack_value (top, a);
2639 return;
2642 if (res & NON_VERIFIABLE_RESULT) {
2643 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
2645 res = res & ~NON_VERIFIABLE_RESULT;
2648 if (complexMerge && res == TYPE_PTR) {
2649 if (complexMerge == 1)
2650 copy_stack_value (top, a);
2651 else if (complexMerge == 2)
2652 copy_stack_value (top, b);
2654 * There is no need to merge the type of two pointers.
2655 * The only valid operation is subtraction, that returns a native
2656 * int as result and can be used with any 2 pointer kinds.
2657 * This is valid acording to Patition III 1.1.4
2659 } else
2660 top->stype = res;
2665 static void
2666 do_boolean_branch_op (VerifyContext *ctx, int delta)
2668 int target = ctx->ip_offset + delta;
2669 ILStackDesc *top;
2671 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2673 if (target < 0 || target >= ctx->code_size) {
2674 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2675 return;
2678 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2679 case 1:
2680 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2681 break;
2682 case 2:
2683 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2684 return;
2687 ctx->target = target;
2689 if (!check_underflow (ctx, 1))
2690 return;
2692 top = stack_pop (ctx);
2693 if (!is_valid_bool_arg (top))
2694 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
2696 check_unmanaged_pointer (ctx, top);
2699 static gboolean
2700 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
2702 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
2705 static void
2706 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2708 ILStackDesc *a, *b;
2709 int idxa, idxb;
2710 unsigned char res;
2711 int target = ctx->ip_offset + delta;
2713 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2715 if (target < 0 || target >= ctx->code_size) {
2716 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2717 return;
2720 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2721 case 1: /*FIXME use constants and not magic numbers.*/
2722 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2723 break;
2724 case 2:
2725 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2726 return;
2729 ctx->target = target;
2731 if (!check_underflow (ctx, 2))
2732 return;
2734 b = stack_pop (ctx);
2735 a = stack_pop (ctx);
2737 idxa = stack_slot_get_underlying_type (a);
2738 if (stack_slot_is_managed_pointer (a))
2739 idxa = TYPE_PTR;
2741 idxb = stack_slot_get_underlying_type (b);
2742 if (stack_slot_is_managed_pointer (b))
2743 idxb = TYPE_PTR;
2745 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2746 res = TYPE_INV;
2747 } else {
2748 --idxa;
2749 --idxb;
2750 res = table [idxa][idxb];
2753 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2754 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2756 if (res == TYPE_INV) {
2757 CODE_NOT_VERIFIABLE (ctx,
2758 g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2759 } else if (res & NON_VERIFIABLE_RESULT) {
2760 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2761 res = res & ~NON_VERIFIABLE_RESULT;
2765 static void
2766 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2768 ILStackDesc *a, *b;
2769 int idxa, idxb;
2770 unsigned char res;
2772 if (!check_underflow (ctx, 2))
2773 return;
2774 b = stack_pop (ctx);
2775 a = stack_pop (ctx);
2777 if (opcode == CEE_CGT_UN) {
2778 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2779 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2780 return;
2784 idxa = stack_slot_get_underlying_type (a);
2785 if (stack_slot_is_managed_pointer (a))
2786 idxa = TYPE_PTR;
2788 idxb = stack_slot_get_underlying_type (b);
2789 if (stack_slot_is_managed_pointer (b))
2790 idxb = TYPE_PTR;
2792 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
2793 res = TYPE_INV;
2794 } else {
2795 --idxa;
2796 --idxb;
2797 res = table [idxa][idxb];
2800 if(res == TYPE_INV) {
2801 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2802 } else if (res & NON_VERIFIABLE_RESULT) {
2803 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
2804 res = res & ~NON_VERIFIABLE_RESULT;
2806 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2809 static void
2810 do_ret (VerifyContext *ctx)
2812 MonoType *ret = ctx->signature->ret;
2813 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2814 if (ret->type != MONO_TYPE_VOID) {
2815 ILStackDesc *top;
2816 if (!check_underflow (ctx, 1))
2817 return;
2819 top = stack_pop(ctx);
2821 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2822 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2823 return;
2826 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
2827 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
2830 if (ctx->eval.size > 0) {
2831 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2833 if (in_any_block (ctx->header, ctx->ip_offset))
2834 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2838 * FIXME we need to fix the case of a non-virtual instance method defined in the parent but call using a token pointing to a subclass.
2839 * This is illegal but mono_get_method_full decoded it.
2840 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2842 static void
2843 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
2845 int param_count, i;
2846 MonoMethodSignature *sig;
2847 ILStackDesc *value;
2848 MonoMethod *method;
2849 gboolean virt_check_this = FALSE;
2850 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
2852 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
2853 return;
2855 if (virtual) {
2856 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
2858 if (method->klass->valuetype) // && !constrained ???
2859 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
2861 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
2862 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
2864 } else {
2865 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
2866 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
2868 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED)) {
2869 virt_check_this = TRUE;
2870 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
2874 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
2875 sig = mono_method_get_signature (method, ctx->image, method_token);
2877 if (!sig) {
2878 char *name = mono_type_get_full_name (method->klass);
2879 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name, method->name, ctx->ip_offset));
2880 g_free (name);
2881 return;
2884 param_count = sig->param_count + sig->hasthis;
2885 if (!check_underflow (ctx, param_count))
2886 return;
2888 for (i = sig->param_count - 1; i >= 0; --i) {
2889 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2890 value = stack_pop (ctx);
2891 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
2892 char *stack_name = stack_slot_full_name (value);
2893 char *sig_name = mono_type_full_name (sig->params [i]);
2894 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter with function signature: Calling method with signature (%s) but for argument %d there is a (%s) on stack at 0x%04x", sig_name, i, stack_name, ctx->ip_offset));
2895 g_free (stack_name);
2896 g_free (sig_name);
2899 if (stack_slot_is_managed_mutability_pointer (value))
2900 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of %s at 0x%04x", virtual ? "callvirt" : "call", ctx->ip_offset));
2902 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
2903 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot pass a byref argument to a tail %s at 0x%04x", virtual ? "callvirt" : "call", ctx->ip_offset));
2904 return;
2908 if (sig->hasthis) {
2909 MonoType *type = &method->klass->byval_arg;
2910 ILStackDesc copy;
2912 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
2913 if (!mono_method_is_constructor (ctx->method))
2914 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
2915 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
2916 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor to a type diferent that this or super at 0x%04x", ctx->ip_offset));
2918 ctx->super_ctor_called = TRUE;
2919 value = stack_pop_safe (ctx);
2920 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
2921 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
2922 } else {
2923 value = stack_pop (ctx);
2926 copy_stack_value (&copy, value);
2927 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2928 //Other parts of the code suffer from the same issue of
2929 copy.type = mono_type_get_type_byval (copy.type);
2930 copy.stype &= ~POINTER_MASK;
2932 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
2933 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the call opcode with a non-final virtual method on an object diferent thant the this pointer at 0x%04x", ctx->ip_offset));
2935 if (constrained && virtual) {
2936 if (!stack_slot_is_managed_pointer (value))
2937 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
2938 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
2939 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
2940 copy.stype |= BOXED_MASK;
2941 } else {
2942 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
2943 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a reference type using a managed pointer to the this arg at 0x%04x", ctx->ip_offset));
2945 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
2946 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
2948 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
2949 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
2951 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
2952 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a boxed or literal valuetype to call a valuetype method at 0x%04x", ctx->ip_offset));
2954 if (!verify_stack_type_compatibility (ctx, type, &copy))
2955 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
2957 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
2958 char *name = mono_method_full_name (method, TRUE);
2959 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2960 g_free (name);
2963 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
2964 char *name = mono_method_full_name (method, TRUE);
2965 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2966 g_free (name);
2969 if (sig->ret->type != MONO_TYPE_VOID) {
2970 if (check_overflow (ctx)) {
2971 value = stack_push (ctx);
2972 set_stack_value (ctx, value, sig->ret, FALSE);
2973 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
2974 ctx->prefix_set &= ~PREFIX_READONLY;
2975 value->stype |= CMMP_MASK;
2980 if ((ctx->prefix_set & PREFIX_TAIL)) {
2981 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
2982 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
2983 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
2984 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
2989 static void
2990 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2992 MonoClassField *field;
2993 MonoClass *klass;
2994 if (!check_overflow (ctx))
2995 return;
2996 if (!take_addr)
2997 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
2999 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3000 return;
3002 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3003 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3004 return;
3006 /*taking the address of initonly field only works from the static constructor */
3007 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3008 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3009 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3011 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3012 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3014 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3017 static void
3018 do_store_static_field (VerifyContext *ctx, int token) {
3019 MonoClassField *field;
3020 MonoClass *klass;
3021 ILStackDesc *value;
3022 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3024 if (!check_underflow (ctx, 1))
3025 return;
3027 value = stack_pop (ctx);
3029 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3030 return;
3032 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3033 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3034 return;
3037 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3038 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3039 return;
3042 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3043 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3045 if (!verify_stack_type_compatibility (ctx, field->type, value))
3046 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in static field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3049 static gboolean
3050 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3052 MonoClassField *field;
3053 MonoClass *klass;
3054 gboolean is_pointer;
3056 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3057 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3058 return FALSE;
3060 *ret_field = field;
3061 //the value on stack is going to be used as a pointer
3062 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3064 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3065 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3066 return FALSE;
3068 g_assert (obj->type);
3070 /*The value on the stack must be a subclass of the defining type of the field*/
3071 /* we need to check if we can load the field from the stack value*/
3072 if (is_pointer) {
3073 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3074 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3076 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3077 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3078 } else {
3079 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3080 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a managed pointer to a reference type and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3082 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3083 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3084 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a boxed valuetype and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3086 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE)) {
3087 char *found = stack_slot_full_name (obj);
3088 char *expected = mono_type_full_name (&field->parent->byval_arg);
3089 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected type '%s' but found '%s' referencing the 'this' argument at 0x%04x", expected, found, ctx->ip_offset));
3090 g_free (found);
3091 g_free (expected);
3094 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3095 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3098 check_unmanaged_pointer (ctx, obj);
3099 return TRUE;
3102 static void
3103 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3105 ILStackDesc *obj;
3106 MonoClassField *field;
3108 if (!take_addr)
3109 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3111 if (!check_underflow (ctx, 1))
3112 return;
3113 obj = stack_pop_safe (ctx);
3115 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3116 return;
3118 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3119 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3121 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3122 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3123 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3125 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3128 static void
3129 do_store_field (VerifyContext *ctx, int token)
3131 ILStackDesc *value, *obj;
3132 MonoClassField *field;
3133 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3135 if (!check_underflow (ctx, 2))
3136 return;
3138 value = stack_pop (ctx);
3139 obj = stack_pop_safe (ctx);
3141 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3142 return;
3144 if (!verify_stack_type_compatibility (ctx, field->type, value))
3145 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3148 /*TODO proper handle for Nullable<T>*/
3149 static void
3150 do_box_value (VerifyContext *ctx, int klass_token)
3152 ILStackDesc *value;
3153 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3154 MonoClass *klass;
3156 if (!type)
3157 return;
3159 if (!check_underflow (ctx, 1))
3160 return;
3162 value = stack_pop (ctx);
3163 /*box is a nop for reference types*/
3165 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3166 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3167 return;
3171 if (!verify_stack_type_compatibility (ctx, type, value))
3172 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3174 klass = mono_class_from_mono_type (type);
3175 if (mono_class_is_nullable (klass))
3176 type = &mono_class_get_nullable_param (klass)->byval_arg;
3177 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3180 static void
3181 do_unbox_value (VerifyContext *ctx, int klass_token)
3183 ILStackDesc *value;
3184 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3186 if (!type)
3187 return;
3189 if (!check_underflow (ctx, 1))
3190 return;
3192 if (!mono_class_from_mono_type (type)->valuetype)
3193 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3195 value = stack_pop (ctx);
3197 /*Value should be: a boxed valuetype or a reference type*/
3198 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3199 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3200 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3202 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3203 value->stype |= CMMP_MASK;
3206 static void
3207 do_unbox_any (VerifyContext *ctx, int klass_token)
3209 ILStackDesc *value;
3210 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3212 if (!type)
3213 return;
3215 if (!check_underflow (ctx, 1))
3216 return;
3218 value = stack_pop (ctx);
3220 /*Value should be: a boxed valuetype or a reference type*/
3221 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3222 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3223 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox.any operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3225 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3228 static void
3229 do_unary_math_op (VerifyContext *ctx, int op)
3231 ILStackDesc *value;
3232 if (!check_underflow (ctx, 1))
3233 return;
3234 value = stack_pop (ctx);
3235 switch (stack_slot_get_type (value)) {
3236 case TYPE_I4:
3237 case TYPE_I8:
3238 case TYPE_NATIVE_INT:
3239 break;
3240 case TYPE_R8:
3241 if (op == CEE_NEG)
3242 break;
3243 case TYPE_COMPLEX: /*only enums are ok*/
3244 if (mono_type_is_enum_type (value->type))
3245 break;
3246 default:
3247 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3249 stack_push_stack_val (ctx, value);
3252 static void
3253 do_conversion (VerifyContext *ctx, int kind)
3255 ILStackDesc *value;
3256 if (!check_underflow (ctx, 1))
3257 return;
3258 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 case TYPE_R8:
3265 break;
3266 default:
3267 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type (%s) at stack for conversion operation. Numeric type expected at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3270 switch (kind) {
3271 case TYPE_I4:
3272 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3273 break;
3274 case TYPE_I8:
3275 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3276 break;
3277 case TYPE_R8:
3278 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3279 break;
3280 case TYPE_NATIVE_INT:
3281 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3282 break;
3283 default:
3284 g_error ("unknown type %02x in conversion", kind);
3289 static void
3290 do_load_token (VerifyContext *ctx, int token)
3292 gpointer handle;
3293 MonoClass *handle_class;
3294 if (!check_overflow (ctx))
3295 return;
3296 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3297 if (!handle) {
3298 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3299 return;
3301 if (handle_class == mono_defaults.typehandle_class) {
3302 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3303 } else if (handle_class == mono_defaults.methodhandle_class) {
3304 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3305 } else if (handle_class == mono_defaults.fieldhandle_class) {
3306 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3307 } else {
3308 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3310 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3313 static void
3314 do_ldobj_value (VerifyContext *ctx, int token)
3316 ILStackDesc *value;
3317 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3318 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3320 if (!type)
3321 return;
3323 if (!check_underflow (ctx, 1))
3324 return;
3326 value = stack_pop (ctx);
3327 if (!stack_slot_is_managed_pointer (value)
3328 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3329 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3330 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3331 return;
3334 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3335 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3337 /*We have a byval on the stack, but the comparison must be strict. */
3338 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3339 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3341 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3344 static void
3345 do_stobj (VerifyContext *ctx, int token)
3347 ILStackDesc *dest, *src;
3348 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3349 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3351 if (!type)
3352 return;
3354 if (!check_underflow (ctx, 2))
3355 return;
3357 src = stack_pop (ctx);
3358 dest = stack_pop (ctx);
3360 if (stack_slot_is_managed_mutability_pointer (dest))
3361 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3363 if (!stack_slot_is_managed_pointer (dest))
3364 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3366 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3367 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
3369 if (!verify_stack_type_compatibility (ctx, type, src))
3370 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
3372 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3373 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3376 static void
3377 do_cpobj (VerifyContext *ctx, int token)
3379 ILStackDesc *dest, *src;
3380 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3381 if (!type)
3382 return;
3384 if (!check_underflow (ctx, 2))
3385 return;
3387 src = stack_pop (ctx);
3388 dest = stack_pop (ctx);
3390 if (!stack_slot_is_managed_pointer (src))
3391 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3393 if (!stack_slot_is_managed_pointer (dest))
3394 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3396 if (stack_slot_is_managed_mutability_pointer (dest))
3397 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3399 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3400 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3402 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3403 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3406 static void
3407 do_initobj (VerifyContext *ctx, int token)
3409 ILStackDesc *obj;
3410 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3411 if (!type)
3412 return;
3414 if (!check_underflow (ctx, 1))
3415 return;
3417 obj = stack_pop (ctx);
3419 if (!stack_slot_is_managed_pointer (obj))
3420 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3422 if (stack_slot_is_managed_mutability_pointer (obj))
3423 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3425 stack = mono_type_get_type_byval (obj->type);
3426 if (MONO_TYPE_IS_REFERENCE (stack)) {
3427 if (!verify_type_compatibility (ctx, stack, type))
3428 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3429 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3430 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3431 } else if (!verify_type_compatibility (ctx, stack, type)) {
3432 char *expected_name = mono_type_full_name (type);
3433 char *stack_name = mono_type_full_name (stack);
3435 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Initobj %s not compatible with value on stack %s at 0x%04x", expected_name, stack_name, ctx->ip_offset));
3436 g_free (expected_name);
3437 g_free (stack_name);
3441 static void
3442 do_newobj (VerifyContext *ctx, int token)
3444 ILStackDesc *value;
3445 int i;
3446 MonoMethodSignature *sig;
3447 MonoMethod *method;
3448 gboolean is_delegate = FALSE;
3450 if (!(method = verifier_load_method (ctx, token, "newobj")))
3451 return;
3453 if (!mono_method_is_constructor (method)) {
3454 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3455 return;
3458 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3459 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3461 if (!mono_method_can_access_method_full (ctx->method, method, NULL)) {
3462 char *from = mono_method_full_name (ctx->method, TRUE);
3463 char *to = mono_method_full_name (method, TRUE);
3464 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor %s not visible from %s at 0x%04x", to, from, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3465 g_free (from);
3466 g_free (to);
3469 //FIXME use mono_method_get_signature_full
3470 sig = mono_method_signature (method);
3471 if (!sig) {
3472 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3473 return;
3476 if (!check_underflow (ctx, sig->param_count))
3477 return;
3479 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3481 if (is_delegate) {
3482 ILStackDesc *funptr;
3483 //first arg is object, second arg is fun ptr
3484 if (sig->param_count != 2) {
3485 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3486 return;
3488 funptr = stack_pop (ctx);
3489 value = stack_pop (ctx);
3490 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3491 } else {
3492 for (i = sig->param_count - 1; i >= 0; --i) {
3493 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3494 value = stack_pop (ctx);
3495 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
3496 char *stack_name = stack_slot_full_name (value);
3497 char *sig_name = mono_type_full_name (sig->params [i]);
3498 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with constructor signature: %s X %s at 0x%04x", sig_name, stack_name, ctx->ip_offset));
3499 g_free (stack_name);
3500 g_free (sig_name);
3503 if (stack_slot_is_managed_mutability_pointer (value))
3504 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3508 if (check_overflow (ctx))
3509 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3512 static void
3513 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3514 ILStackDesc *value;
3515 MonoType *type;
3516 gboolean is_boxed;
3517 gboolean do_box;
3519 if (!check_underflow (ctx, 1))
3520 return;
3522 if (!(type = verifier_load_type (ctx, token, opcode)))
3523 return;
3525 if (type->byref) {
3526 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3527 return;
3530 value = stack_pop (ctx);
3531 is_boxed = stack_slot_is_boxed_value (value);
3533 if (stack_slot_is_managed_pointer (value))
3534 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3535 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
3536 char *name = stack_slot_full_name (value);
3537 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a reference type on stack for %s but found %s at 0x%04x", opcode, name, ctx->ip_offset));
3538 g_free (name);
3541 switch (value->type->type) {
3542 case MONO_TYPE_FNPTR:
3543 case MONO_TYPE_PTR:
3544 case MONO_TYPE_TYPEDBYREF:
3545 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3548 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
3549 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
3552 static MonoType *
3553 mono_type_from_opcode (int opcode) {
3554 switch (opcode) {
3555 case CEE_LDIND_I1:
3556 case CEE_LDIND_U1:
3557 case CEE_STIND_I1:
3558 case CEE_LDELEM_I1:
3559 case CEE_LDELEM_U1:
3560 case CEE_STELEM_I1:
3561 return &mono_defaults.sbyte_class->byval_arg;
3563 case CEE_LDIND_I2:
3564 case CEE_LDIND_U2:
3565 case CEE_STIND_I2:
3566 case CEE_LDELEM_I2:
3567 case CEE_LDELEM_U2:
3568 case CEE_STELEM_I2:
3569 return &mono_defaults.int16_class->byval_arg;
3571 case CEE_LDIND_I4:
3572 case CEE_LDIND_U4:
3573 case CEE_STIND_I4:
3574 case CEE_LDELEM_I4:
3575 case CEE_LDELEM_U4:
3576 case CEE_STELEM_I4:
3577 return &mono_defaults.int32_class->byval_arg;
3579 case CEE_LDIND_I8:
3580 case CEE_STIND_I8:
3581 case CEE_LDELEM_I8:
3582 case CEE_STELEM_I8:
3583 return &mono_defaults.int64_class->byval_arg;
3585 case CEE_LDIND_R4:
3586 case CEE_STIND_R4:
3587 case CEE_LDELEM_R4:
3588 case CEE_STELEM_R4:
3589 return &mono_defaults.single_class->byval_arg;
3591 case CEE_LDIND_R8:
3592 case CEE_STIND_R8:
3593 case CEE_LDELEM_R8:
3594 case CEE_STELEM_R8:
3595 return &mono_defaults.double_class->byval_arg;
3597 case CEE_LDIND_I:
3598 case CEE_STIND_I:
3599 case CEE_LDELEM_I:
3600 case CEE_STELEM_I:
3601 return &mono_defaults.int_class->byval_arg;
3603 case CEE_LDIND_REF:
3604 case CEE_STIND_REF:
3605 case CEE_LDELEM_REF:
3606 case CEE_STELEM_REF:
3607 return &mono_defaults.object_class->byval_arg;
3609 default:
3610 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3611 return NULL;
3615 static void
3616 do_load_indirect (VerifyContext *ctx, int opcode)
3618 ILStackDesc *value;
3619 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3621 if (!check_underflow (ctx, 1))
3622 return;
3624 value = stack_pop (ctx);
3625 if (!stack_slot_is_managed_pointer (value)) {
3626 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3627 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3628 return;
3631 if (opcode == CEE_LDIND_REF) {
3632 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3633 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3634 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3635 } else {
3636 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3637 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3638 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3642 static void
3643 do_store_indirect (VerifyContext *ctx, int opcode)
3645 ILStackDesc *addr, *val;
3646 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3648 if (!check_underflow (ctx, 2))
3649 return;
3651 val = stack_pop (ctx);
3652 addr = stack_pop (ctx);
3654 check_unmanaged_pointer (ctx, addr);
3656 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3657 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3658 return;
3661 if (stack_slot_is_managed_mutability_pointer (addr)) {
3662 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3663 return;
3666 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3667 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3669 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3670 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3673 static void
3674 do_newarr (VerifyContext *ctx, int token)
3676 ILStackDesc *value;
3677 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3679 if (!type)
3680 return;
3682 if (!check_underflow (ctx, 1))
3683 return;
3685 value = stack_pop (ctx);
3686 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3687 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Array size type on stack (%s) is not a verifiable type at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3689 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3692 /*FIXME handle arrays that are not 0-indexed*/
3693 static void
3694 do_ldlen (VerifyContext *ctx)
3696 ILStackDesc *value;
3698 if (!check_underflow (ctx, 1))
3699 return;
3701 value = stack_pop (ctx);
3703 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3704 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3706 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3709 /*FIXME handle arrays that are not 0-indexed*/
3710 /*FIXME handle readonly prefix and CMMP*/
3711 static void
3712 do_ldelema (VerifyContext *ctx, int klass_token)
3714 ILStackDesc *index, *array, *res;
3715 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
3716 gboolean valid;
3718 if (!type)
3719 return;
3721 if (!check_underflow (ctx, 2))
3722 return;
3724 index = stack_pop (ctx);
3725 array = stack_pop (ctx);
3727 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3728 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelema is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
3730 if (!stack_slot_is_null_literal (array)) {
3731 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3732 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3733 else {
3734 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3735 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3736 } else {
3737 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3739 if (!valid)
3740 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3744 res = stack_push (ctx);
3745 set_stack_value (ctx, res, type, TRUE);
3746 if (ctx->prefix_set & PREFIX_READONLY) {
3747 ctx->prefix_set &= ~PREFIX_READONLY;
3748 res->stype |= CMMP_MASK;
3753 * FIXME handle arrays that are not 0-indexed
3754 * FIXME handle readonly prefix and CMMP
3756 static void
3757 do_ldelem (VerifyContext *ctx, int opcode, int token)
3759 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3760 ILStackDesc *index, *array;
3761 MonoType *type;
3762 if (!check_underflow (ctx, 2))
3763 return;
3765 if (opcode == CEE_LDELEM) {
3766 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
3767 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3768 return;
3770 } else {
3771 type = mono_type_from_opcode (opcode);
3774 index = stack_pop (ctx);
3775 array = stack_pop (ctx);
3777 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3778 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
3780 if (!stack_slot_is_null_literal (array)) {
3781 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3782 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3783 else {
3784 if (opcode == CEE_LDELEM_REF) {
3785 if (array->type->data.klass->valuetype)
3786 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3787 type = &array->type->data.klass->byval_arg;
3788 } else {
3789 MonoType *candidate = &array->type->data.klass->byval_arg;
3790 if (IS_STRICT_MODE (ctx)) {
3791 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
3792 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
3793 if ((IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I, MONO_TYPE_U)) ||
3794 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
3795 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3797 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
3798 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3803 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3804 #undef IS_ONE_OF2
3808 * FIXME handle arrays that are not 0-indexed
3810 static void
3811 do_stelem (VerifyContext *ctx, int opcode, int token)
3813 ILStackDesc *index, *array, *value;
3814 MonoType *type;
3815 if (!check_underflow (ctx, 3))
3816 return;
3818 if (opcode == CEE_STELEM) {
3819 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
3820 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3821 return;
3823 } else {
3824 type = mono_type_from_opcode (opcode);
3827 value = stack_pop (ctx);
3828 index = stack_pop (ctx);
3829 array = stack_pop (ctx);
3831 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3832 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for stdelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
3834 if (!stack_slot_is_null_literal (array)) {
3835 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3836 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for stelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3837 } else {
3838 if (opcode == CEE_STELEM_REF) {
3839 if (array->type->data.klass->valuetype)
3840 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3841 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3842 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3846 if (opcode == CEE_STELEM_REF) {
3847 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3848 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3849 } else if (opcode != CEE_STELEM_REF) {
3850 if (!verify_stack_type_compatibility (ctx, type, value))
3851 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3853 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
3854 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
3859 static void
3860 do_throw (VerifyContext *ctx)
3862 ILStackDesc *exception;
3863 if (!check_underflow (ctx, 1))
3864 return;
3865 exception = stack_pop (ctx);
3867 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3868 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3870 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
3871 char *name = mono_type_full_name (exception->type);
3872 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type but found unboxed %s at 0x%04x ", name, ctx->ip_offset));
3873 g_free (name);
3875 /*The stack is left empty after a throw*/
3876 ctx->eval.size = 0;
3880 static void
3881 do_endfilter (VerifyContext *ctx)
3883 MonoExceptionClause *clause;
3885 if (IS_STRICT_MODE (ctx)) {
3886 if (ctx->eval.size != 1)
3887 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3889 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
3890 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3893 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3894 if (IS_STRICT_MODE (ctx)) {
3895 if (ctx->ip_offset != clause->handler_offset - 2)
3896 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3897 } else {
3898 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3899 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3901 } else {
3902 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3903 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3904 else
3905 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3908 ctx->eval.size = 0;
3911 static void
3912 do_leave (VerifyContext *ctx, int delta)
3914 int target = ((gint32)ctx->ip_offset) + delta;
3915 if (target >= ctx->code_size || target < 0)
3916 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3918 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
3919 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
3920 ctx->eval.size = 0;
3924 * do_static_branch:
3926 * Verify br and br.s opcodes.
3928 static void
3929 do_static_branch (VerifyContext *ctx, int delta)
3931 int target = ctx->ip_offset + delta;
3932 if (target < 0 || target >= ctx->code_size) {
3933 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
3934 return;
3937 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3938 case 1:
3939 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3940 break;
3941 case 2:
3942 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3943 break;
3946 ctx->target = target;
3949 static void
3950 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
3952 int i, base = ctx->ip_offset + 5 + count * 4;
3953 ILStackDesc *value;
3955 if (!check_underflow (ctx, 1))
3956 return;
3958 value = stack_pop (ctx);
3960 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3961 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
3963 for (i = 0; i < count; ++i) {
3964 int target = base + read32 (data + i * 4);
3966 if (target < 0 || target >= ctx->code_size) {
3967 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
3968 return;
3971 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3972 case 1:
3973 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3974 break;
3975 case 2:
3976 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3977 return;
3979 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
3983 static void
3984 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
3986 ILStackDesc *top;
3987 MonoMethod *method;
3989 if (virtual && !check_underflow (ctx, 1))
3990 return;
3992 if (!virtual && !check_overflow (ctx))
3993 return;
3995 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
3996 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3997 return;
4000 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4001 return;
4003 if (mono_method_is_constructor (method))
4004 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4006 if (virtual) {
4007 ILStackDesc *top = stack_pop (ctx);
4009 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4010 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4012 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4013 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4015 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4016 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4019 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4020 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Loaded method is not visible for ldftn/ldvirtftn at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
4022 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4023 top->method = method;
4026 static void
4027 do_sizeof (VerifyContext *ctx, int token)
4029 MonoType *type;
4031 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4032 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4033 return;
4036 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4037 return;
4039 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4040 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4041 return;
4044 if (type->type == MONO_TYPE_VOID) {
4045 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4046 return;
4049 if (check_overflow (ctx))
4050 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4053 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4054 static void
4055 do_localloc (VerifyContext *ctx)
4057 ILStackDesc *top;
4059 if (ctx->eval.size != 1) {
4060 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4061 return;
4064 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4065 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4066 return;
4069 /*TODO verify top type*/
4070 top = stack_pop (ctx);
4072 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4073 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4076 static void
4077 do_ldstr (VerifyContext *ctx, guint32 token)
4079 GSList *error = NULL;
4080 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4081 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4082 return;
4085 if (!ctx->image->dynamic && !mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), &error)) {
4086 if (error)
4087 ctx->list = g_slist_concat (ctx->list, error);
4088 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4089 return;
4092 if (check_overflow (ctx))
4093 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4096 static void
4097 do_refanyval (VerifyContext *ctx, int token)
4099 ILStackDesc *top;
4100 MonoType *type;
4101 if (!check_underflow (ctx, 1))
4102 return;
4104 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4105 return;
4107 top = stack_pop (ctx);
4109 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4110 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanyval, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4112 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4115 static void
4116 do_refanytype (VerifyContext *ctx)
4118 ILStackDesc *top;
4120 if (!check_underflow (ctx, 1))
4121 return;
4123 top = stack_pop (ctx);
4125 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4126 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanytype, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4128 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4132 static void
4133 do_mkrefany (VerifyContext *ctx, int token)
4135 ILStackDesc *top;
4136 MonoType *type;
4137 if (!check_underflow (ctx, 1))
4138 return;
4140 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4141 return;
4143 top = stack_pop (ctx);
4145 if (stack_slot_is_managed_mutability_pointer (top))
4146 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4148 if (!stack_slot_is_managed_pointer (top)) {
4149 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a managed pointer for mkrefany, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4150 }else {
4151 MonoType *stack_type = mono_type_get_type_byval (top->type);
4152 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4153 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4155 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4156 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4159 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4162 static void
4163 do_ckfinite (VerifyContext *ctx)
4165 ILStackDesc *top;
4166 if (!check_underflow (ctx, 1))
4167 return;
4169 top = stack_pop (ctx);
4171 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4172 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected float32 or float64 on stack for ckfinit but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
4173 stack_push_stack_val (ctx, top);
4176 * merge_stacks:
4177 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4179 * @from holds new values for a given control path
4180 * @to holds the current values of a given control path
4182 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4184 static void
4185 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4187 MonoError error;
4188 int i, j, k;
4189 stack_init (ctx, to);
4191 if (start) {
4192 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4193 from->size = 0;
4194 else
4195 stack_copy (&ctx->eval, to);
4196 goto end_verify;
4197 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4198 stack_copy (to, &ctx->eval);
4199 goto end_verify;
4201 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4203 if (from->size != to->size) {
4204 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4205 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d) at 0x%04x", from->size, to->size, ctx->ip_offset));
4206 goto end_verify;
4209 //FIXME we need to preserve CMMP attributes
4210 //FIXME we must take null literals into consideration.
4211 for (i = 0; i < from->size; ++i) {
4212 ILStackDesc *new_slot = from->stack + i;
4213 ILStackDesc *old_slot = to->stack + i;
4214 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4215 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4216 MonoClass *old_class = mono_class_from_mono_type (old_type);
4217 MonoClass *new_class = mono_class_from_mono_type (new_type);
4218 MonoClass *match_class = NULL;
4220 // S := T then U = S (new value is compatible with current value, keep current)
4221 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4222 copy_stack_value (new_slot, old_slot);
4223 continue;
4226 // T := S then U = T (old value is compatible with current value, use new)
4227 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4228 copy_stack_value (old_slot, new_slot);
4229 continue;
4232 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4233 char *old_name = stack_slot_full_name (old_slot);
4234 char *new_name = stack_slot_full_name (new_slot);
4235 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
4236 g_free (old_name);
4237 g_free (new_name);
4238 goto end_verify;
4241 //both are reference types, use closest common super type
4242 if (!mono_class_from_mono_type (old_type)->valuetype
4243 && !mono_class_from_mono_type (new_type)->valuetype
4244 && !stack_slot_is_managed_pointer (old_slot)
4245 && !stack_slot_is_managed_pointer (new_slot)) {
4247 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4248 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4249 match_class = old_class->supertypes [j];
4250 goto match_found;
4254 mono_class_setup_interfaces (old_class, &error);
4255 if (!mono_error_ok (&error)) {
4256 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot merge stacks due to a TypeLoadException %s at 0x%04x", mono_error_get_message (&error), ctx->ip_offset));
4257 mono_error_cleanup (&error);
4258 goto end_verify;
4260 for (j = 0; j < old_class->interface_count; ++j) {
4261 for (k = 0; k < new_class->interface_count; ++k) {
4262 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4263 match_class = old_class->interfaces [j];
4264 goto match_found;
4269 //No decent super type found, use object
4270 match_class = mono_defaults.object_class;
4271 goto match_found;
4272 } else if (is_compatible_boxed_valuetype (ctx,old_type, new_type, new_slot, FALSE) || is_compatible_boxed_valuetype (ctx, new_type, old_type, old_slot, FALSE)) {
4273 match_class = mono_defaults.object_class;
4274 goto match_found;
4278 char *old_name = stack_slot_full_name (old_slot);
4279 char *new_name = stack_slot_full_name (new_slot);
4280 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
4281 g_free (old_name);
4282 g_free (new_name);
4284 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4285 goto end_verify;
4287 match_found:
4288 g_assert (match_class);
4289 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4290 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4291 continue;
4294 end_verify:
4295 if (external)
4296 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4297 to->flags |= IL_CODE_STACK_MERGED;
4300 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4301 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4304 * is_clause_in_range :
4306 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4308 static gboolean
4309 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4311 if (clause->try_offset >= start && clause->try_offset < end)
4312 return TRUE;
4313 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4314 return TRUE;
4315 return FALSE;
4319 * is_clause_inside_range :
4321 * Returns TRUE if @clause lies completely inside the @start - @end range.
4323 static gboolean
4324 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4326 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4327 return FALSE;
4328 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4329 return FALSE;
4330 return TRUE;
4334 * is_clause_nested :
4336 * Returns TRUE if @nested is nested in @clause.
4338 static gboolean
4339 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4341 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4342 return TRUE;
4343 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4344 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4347 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4348 * the each pair of exception must have the following properties:
4349 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4350 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4351 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4353 static void
4354 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4356 /*clause is nested*/
4357 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4358 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4359 return;
4362 /*wrong nesting order.*/
4363 if (is_clause_nested (clause, to_test)) {
4364 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4365 return;
4368 /*mutual protection*/
4369 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4370 /*handlers are not disjoint*/
4371 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4372 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4373 return;
4375 /* handlers are not catch or filter */
4376 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4377 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4378 return;
4380 /*OK*/
4381 return;
4384 /*not completelly disjoint*/
4385 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4386 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4387 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4390 #define code_bounds_check(size) \
4391 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4392 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4393 break; \
4396 static gboolean
4397 mono_opcode_is_prefix (int op)
4399 switch (op) {
4400 case MONO_CEE_UNALIGNED_:
4401 case MONO_CEE_VOLATILE_:
4402 case MONO_CEE_TAIL_:
4403 case MONO_CEE_CONSTRAINED_:
4404 case MONO_CEE_READONLY_:
4405 return TRUE;
4407 return FALSE;
4411 * FIXME: need to distinguish between valid and verifiable.
4412 * Need to keep track of types on the stack.
4413 * Verify types for opcodes.
4415 GSList*
4416 mono_method_verify (MonoMethod *method, int level)
4418 MonoError error;
4419 const unsigned char *ip, *code_start;
4420 const unsigned char *end;
4421 MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
4423 int i, n, need_merge = 0, start = 0;
4424 guint token, ip_offset = 0, prefix = 0;
4425 MonoGenericContext *generic_context = NULL;
4426 MonoImage *image;
4427 VerifyContext ctx;
4428 GSList *tmp;
4429 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4431 init_verifier_stats ();
4433 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4434 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4435 return NULL;
4438 memset (&ctx, 0, sizeof (VerifyContext));
4440 //FIXME use mono_method_get_signature_full
4441 ctx.signature = mono_method_signature (method);
4442 if (!ctx.signature) {
4443 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4445 finish_collect_stats ();
4446 return ctx.list;
4448 ctx.header = mono_method_get_header (method);
4449 if (!ctx.header) {
4450 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4451 finish_collect_stats ();
4452 return ctx.list;
4454 ctx.method = method;
4455 code_start = ip = ctx.header->code;
4456 end = ip + ctx.header->code_size;
4457 ctx.image = image = method->klass->image;
4460 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4461 ctx.max_stack = ctx.header->max_stack;
4462 ctx.verifiable = ctx.valid = 1;
4463 ctx.level = level;
4465 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4466 ctx.code_size = ctx.header->code_size;
4467 MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
4469 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4471 ctx.num_locals = ctx.header->num_locals;
4472 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4473 MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
4475 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4476 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4478 ctx.params = g_new (MonoType*, ctx.max_args);
4479 MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
4481 if (ctx.signature->hasthis)
4482 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4483 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4485 if (ctx.signature->is_inflated)
4486 ctx.generic_context = generic_context = mono_method_get_context (method);
4488 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4489 if (method->is_generic)
4490 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4491 else
4492 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4495 for (i = 0; i < ctx.num_locals; ++i) {
4496 MonoType *uninflated = ctx.locals [i];
4497 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4498 if (!mono_error_ok (&error)) {
4499 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4500 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4501 g_free (name);
4502 mono_error_cleanup (&error);
4503 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4504 ctx.num_locals = i;
4505 ctx.max_args = 0;
4506 goto cleanup;
4509 for (i = 0; i < ctx.max_args; ++i) {
4510 MonoType *uninflated = ctx.params [i];
4511 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4512 if (!mono_error_ok (&error)) {
4513 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4514 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4515 g_free (name);
4516 mono_error_cleanup (&error);
4517 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4518 ctx.max_args = i;
4519 goto cleanup;
4522 stack_init (&ctx, &ctx.eval);
4524 for (i = 0; i < ctx.num_locals; ++i) {
4525 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
4526 break;
4527 if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
4528 char *name = mono_type_full_name (ctx.locals [i]);
4529 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
4530 g_free (name);
4531 break;
4536 for (i = 0; i < ctx.max_args; ++i) {
4537 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
4538 break;
4540 if (get_stack_type (ctx.params [i]) == TYPE_INV) {
4541 char *name = mono_type_full_name (ctx.params [i]);
4542 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
4543 g_free (name);
4544 break;
4548 if (!ctx.valid)
4549 goto cleanup;
4551 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4552 MonoExceptionClause *clause = ctx.header->clauses + i;
4553 VERIFIER_DEBUG (printf ("clause try %x len %x filter at %x handler at %x len %x\n", clause->try_offset, clause->try_len, clause->data.filter_offset, clause->handler_offset, clause->handler_len); );
4555 if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
4556 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4558 if (clause->try_len <= 0)
4559 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4561 if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
4562 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
4564 if (clause->handler_len <= 0)
4565 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
4567 if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
4568 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4570 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4571 if (clause->data.filter_offset > ctx.code_size)
4572 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
4574 if (clause->data.filter_offset >= clause->handler_offset)
4575 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
4578 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4579 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4581 if (!ctx.valid)
4582 break;
4584 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4585 if (clause->try_offset + clause->try_len < ctx.code_size)
4586 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4587 if (clause->handler_offset + clause->handler_len < ctx.code_size)
4588 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4590 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4591 if (!clause->data.catch_class) {
4592 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
4593 break;
4596 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4598 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4599 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4600 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4604 original_bb = bb = mono_basic_block_split (method, &error);
4605 if (!mono_error_ok (&error)) {
4606 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (&error)));
4607 mono_error_cleanup (&error);
4608 goto cleanup;
4610 g_assert (bb);
4612 while (ip < end && ctx.valid) {
4613 ip_offset = ip - code_start;
4615 const unsigned char *ip_copy = ip;
4616 int size, op;
4618 if (ip_offset > bb->end) {
4619 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
4620 goto cleanup;
4623 if (ip_offset == bb->end)
4624 bb = bb->next;
4626 size = mono_opcode_value_and_size (&ip_copy, end, &op);
4627 if (size == -1) {
4628 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
4629 goto cleanup;
4632 if (ADD_IS_GREATER_OR_OVF (ip_offset, size, bb->end)) {
4633 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
4634 goto cleanup;
4637 /*Last Instruction*/
4638 if (ip_offset + size == bb->end && mono_opcode_is_prefix (op)) {
4639 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets between prefix '%s' and instruction at 0x%04x", mono_opcode_name (op), ip_offset));
4640 goto cleanup;
4643 if (bb->dead) {
4644 /*FIXME remove this once we move all bad branch checking code to use BB only*/
4645 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
4646 ip += size;
4647 continue;
4651 ctx.ip_offset = ip_offset = ip - code_start;
4653 /*We need to check against fallthrou in and out of protected blocks.
4654 * For fallout we check the once a protected block ends, if the start flag is not set.
4655 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4656 * TODO convert these checks to be done using flags and not this loop
4658 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4659 MonoExceptionClause *clause = ctx.header->clauses + i;
4661 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4662 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4663 start = 1;
4666 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4667 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4668 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4669 else
4670 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4671 start = 1;
4674 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4675 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4676 start = 1;
4679 if (clause->handler_offset == ip_offset && start == 0) {
4680 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4681 start = 1;
4684 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
4685 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4686 start = 1;
4690 if (!ctx.valid)
4691 break;
4693 if (need_merge) {
4694 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4695 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4696 need_merge = 0;
4698 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4699 start = 0;
4701 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4702 #ifdef MONO_VERIFIER_DEBUG
4704 char *discode;
4705 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4706 discode [strlen (discode) - 1] = 0; /* no \n */
4707 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4708 g_free (discode);
4710 dump_stack_state (&ctx.code [ip_offset]);
4711 dump_stack_state (&ctx.eval);
4712 #endif
4714 switch (*ip) {
4715 case CEE_NOP:
4716 case CEE_BREAK:
4717 ++ip;
4718 break;
4720 case CEE_LDARG_0:
4721 case CEE_LDARG_1:
4722 case CEE_LDARG_2:
4723 case CEE_LDARG_3:
4724 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4725 ++ip;
4726 break;
4728 case CEE_LDARG_S:
4729 case CEE_LDARGA_S:
4730 code_bounds_check (2);
4731 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4732 ip += 2;
4733 break;
4735 case CEE_ADD_OVF_UN:
4736 do_binop (&ctx, *ip, add_ovf_un_table);
4737 ++ip;
4738 break;
4740 case CEE_SUB_OVF_UN:
4741 do_binop (&ctx, *ip, sub_ovf_un_table);
4742 ++ip;
4743 break;
4745 case CEE_ADD_OVF:
4746 case CEE_SUB_OVF:
4747 case CEE_MUL_OVF:
4748 case CEE_MUL_OVF_UN:
4749 do_binop (&ctx, *ip, bin_ovf_table);
4750 ++ip;
4751 break;
4753 case CEE_ADD:
4754 do_binop (&ctx, *ip, add_table);
4755 ++ip;
4756 break;
4758 case CEE_SUB:
4759 do_binop (&ctx, *ip, sub_table);
4760 ++ip;
4761 break;
4763 case CEE_MUL:
4764 case CEE_DIV:
4765 case CEE_REM:
4766 do_binop (&ctx, *ip, bin_op_table);
4767 ++ip;
4768 break;
4770 case CEE_AND:
4771 case CEE_DIV_UN:
4772 case CEE_OR:
4773 case CEE_REM_UN:
4774 case CEE_XOR:
4775 do_binop (&ctx, *ip, int_bin_op_table);
4776 ++ip;
4777 break;
4779 case CEE_SHL:
4780 case CEE_SHR:
4781 case CEE_SHR_UN:
4782 do_binop (&ctx, *ip, shift_op_table);
4783 ++ip;
4784 break;
4786 case CEE_POP:
4787 if (!check_underflow (&ctx, 1))
4788 break;
4789 stack_pop_safe (&ctx);
4790 ++ip;
4791 break;
4793 case CEE_RET:
4794 do_ret (&ctx);
4795 ++ip;
4796 start = 1;
4797 break;
4799 case CEE_LDLOC_0:
4800 case CEE_LDLOC_1:
4801 case CEE_LDLOC_2:
4802 case CEE_LDLOC_3:
4803 /*TODO support definite assignment verification? */
4804 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
4805 ++ip;
4806 break;
4808 case CEE_STLOC_0:
4809 case CEE_STLOC_1:
4810 case CEE_STLOC_2:
4811 case CEE_STLOC_3:
4812 store_local (&ctx, *ip - CEE_STLOC_0);
4813 ++ip;
4814 break;
4816 case CEE_STLOC_S:
4817 code_bounds_check (2);
4818 store_local (&ctx, ip [1]);
4819 ip += 2;
4820 break;
4822 case CEE_STARG_S:
4823 code_bounds_check (2);
4824 store_arg (&ctx, ip [1]);
4825 ip += 2;
4826 break;
4828 case CEE_LDC_I4_M1:
4829 case CEE_LDC_I4_0:
4830 case CEE_LDC_I4_1:
4831 case CEE_LDC_I4_2:
4832 case CEE_LDC_I4_3:
4833 case CEE_LDC_I4_4:
4834 case CEE_LDC_I4_5:
4835 case CEE_LDC_I4_6:
4836 case CEE_LDC_I4_7:
4837 case CEE_LDC_I4_8:
4838 if (check_overflow (&ctx))
4839 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4840 ++ip;
4841 break;
4843 case CEE_LDC_I4_S:
4844 code_bounds_check (2);
4845 if (check_overflow (&ctx))
4846 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4847 ip += 2;
4848 break;
4850 case CEE_LDC_I4:
4851 code_bounds_check (5);
4852 if (check_overflow (&ctx))
4853 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4854 ip += 5;
4855 break;
4857 case CEE_LDC_I8:
4858 code_bounds_check (9);
4859 if (check_overflow (&ctx))
4860 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4861 ip += 9;
4862 break;
4864 case CEE_LDC_R4:
4865 code_bounds_check (5);
4866 if (check_overflow (&ctx))
4867 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4868 ip += 5;
4869 break;
4871 case CEE_LDC_R8:
4872 code_bounds_check (9);
4873 if (check_overflow (&ctx))
4874 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4875 ip += 9;
4876 break;
4878 case CEE_LDNULL:
4879 if (check_overflow (&ctx))
4880 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4881 ++ip;
4882 break;
4884 case CEE_BEQ_S:
4885 case CEE_BNE_UN_S:
4886 code_bounds_check (2);
4887 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4888 ip += 2;
4889 need_merge = 1;
4890 break;
4892 case CEE_BGE_S:
4893 case CEE_BGT_S:
4894 case CEE_BLE_S:
4895 case CEE_BLT_S:
4896 case CEE_BGE_UN_S:
4897 case CEE_BGT_UN_S:
4898 case CEE_BLE_UN_S:
4899 case CEE_BLT_UN_S:
4900 code_bounds_check (2);
4901 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
4902 ip += 2;
4903 need_merge = 1;
4904 break;
4906 case CEE_BEQ:
4907 case CEE_BNE_UN:
4908 code_bounds_check (5);
4909 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
4910 ip += 5;
4911 need_merge = 1;
4912 break;
4914 case CEE_BGE:
4915 case CEE_BGT:
4916 case CEE_BLE:
4917 case CEE_BLT:
4918 case CEE_BGE_UN:
4919 case CEE_BGT_UN:
4920 case CEE_BLE_UN:
4921 case CEE_BLT_UN:
4922 code_bounds_check (5);
4923 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
4924 ip += 5;
4925 need_merge = 1;
4926 break;
4928 case CEE_LDLOC_S:
4929 case CEE_LDLOCA_S:
4930 code_bounds_check (2);
4931 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
4932 ip += 2;
4933 break;
4935 case CEE_UNUSED99:
4936 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
4937 ++ip;
4938 break;
4940 case CEE_DUP: {
4941 ILStackDesc *top;
4942 if (!check_underflow (&ctx, 1))
4943 break;
4944 if (!check_overflow (&ctx))
4945 break;
4946 top = stack_push (&ctx);
4947 copy_stack_value (top, stack_peek (&ctx, 1));
4948 ++ip;
4949 break;
4952 case CEE_JMP:
4953 code_bounds_check (5);
4954 if (ctx.eval.size)
4955 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
4956 token = read32 (ip + 1);
4957 if (in_any_block (ctx.header, ip_offset))
4958 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
4960 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
4962 * FIXME: check signature, retval, arguments etc.
4964 ip += 5;
4965 break;
4966 case CEE_CALL:
4967 case CEE_CALLVIRT:
4968 code_bounds_check (5);
4969 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
4970 ip += 5;
4971 break;
4973 case CEE_CALLI:
4974 code_bounds_check (5);
4975 token = read32 (ip + 1);
4977 * FIXME: check signature, retval, arguments etc.
4978 * FIXME: check requirements for tail call
4980 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
4981 ip += 5;
4982 break;
4983 case CEE_BR_S:
4984 code_bounds_check (2);
4985 do_static_branch (&ctx, (signed char)ip [1] + 2);
4986 need_merge = 1;
4987 ip += 2;
4988 start = 1;
4989 break;
4991 case CEE_BRFALSE_S:
4992 case CEE_BRTRUE_S:
4993 code_bounds_check (2);
4994 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
4995 ip += 2;
4996 need_merge = 1;
4997 break;
4999 case CEE_BR:
5000 code_bounds_check (5);
5001 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5002 need_merge = 1;
5003 ip += 5;
5004 start = 1;
5005 break;
5007 case CEE_BRFALSE:
5008 case CEE_BRTRUE:
5009 code_bounds_check (5);
5010 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5011 ip += 5;
5012 need_merge = 1;
5013 break;
5015 case CEE_SWITCH: {
5016 guint32 entries;
5017 code_bounds_check (5);
5018 entries = read32 (ip + 1);
5020 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5021 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5023 ip += 5;
5024 code_bounds_check (sizeof (guint32) * entries);
5026 do_switch (&ctx, entries, ip);
5027 ip += sizeof (guint32) * entries;
5028 break;
5030 case CEE_LDIND_I1:
5031 case CEE_LDIND_U1:
5032 case CEE_LDIND_I2:
5033 case CEE_LDIND_U2:
5034 case CEE_LDIND_I4:
5035 case CEE_LDIND_U4:
5036 case CEE_LDIND_I8:
5037 case CEE_LDIND_I:
5038 case CEE_LDIND_R4:
5039 case CEE_LDIND_R8:
5040 case CEE_LDIND_REF:
5041 do_load_indirect (&ctx, *ip);
5042 ++ip;
5043 break;
5045 case CEE_STIND_REF:
5046 case CEE_STIND_I1:
5047 case CEE_STIND_I2:
5048 case CEE_STIND_I4:
5049 case CEE_STIND_I8:
5050 case CEE_STIND_R4:
5051 case CEE_STIND_R8:
5052 case CEE_STIND_I:
5053 do_store_indirect (&ctx, *ip);
5054 ++ip;
5055 break;
5057 case CEE_NOT:
5058 case CEE_NEG:
5059 do_unary_math_op (&ctx, *ip);
5060 ++ip;
5061 break;
5063 case CEE_CONV_I1:
5064 case CEE_CONV_I2:
5065 case CEE_CONV_I4:
5066 case CEE_CONV_U1:
5067 case CEE_CONV_U2:
5068 case CEE_CONV_U4:
5069 do_conversion (&ctx, TYPE_I4);
5070 ++ip;
5071 break;
5073 case CEE_CONV_I8:
5074 case CEE_CONV_U8:
5075 do_conversion (&ctx, TYPE_I8);
5076 ++ip;
5077 break;
5079 case CEE_CONV_R4:
5080 case CEE_CONV_R8:
5081 case CEE_CONV_R_UN:
5082 do_conversion (&ctx, TYPE_R8);
5083 ++ip;
5084 break;
5086 case CEE_CONV_I:
5087 case CEE_CONV_U:
5088 do_conversion (&ctx, TYPE_NATIVE_INT);
5089 ++ip;
5090 break;
5092 case CEE_CPOBJ:
5093 code_bounds_check (5);
5094 do_cpobj (&ctx, read32 (ip + 1));
5095 ip += 5;
5096 break;
5098 case CEE_LDOBJ:
5099 code_bounds_check (5);
5100 do_ldobj_value (&ctx, read32 (ip + 1));
5101 ip += 5;
5102 break;
5104 case CEE_LDSTR:
5105 code_bounds_check (5);
5106 do_ldstr (&ctx, read32 (ip + 1));
5107 ip += 5;
5108 break;
5110 case CEE_NEWOBJ:
5111 code_bounds_check (5);
5112 do_newobj (&ctx, read32 (ip + 1));
5113 ip += 5;
5114 break;
5116 case CEE_CASTCLASS:
5117 case CEE_ISINST:
5118 code_bounds_check (5);
5119 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5120 ip += 5;
5121 break;
5123 case CEE_UNUSED58:
5124 case CEE_UNUSED1:
5125 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5126 ++ip;
5127 break;
5129 case CEE_UNBOX:
5130 code_bounds_check (5);
5131 do_unbox_value (&ctx, read32 (ip + 1));
5132 ip += 5;
5133 break;
5135 case CEE_THROW:
5136 do_throw (&ctx);
5137 start = 1;
5138 ++ip;
5139 break;
5141 case CEE_LDFLD:
5142 case CEE_LDFLDA:
5143 code_bounds_check (5);
5144 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5145 ip += 5;
5146 break;
5148 case CEE_LDSFLD:
5149 case CEE_LDSFLDA:
5150 code_bounds_check (5);
5151 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5152 ip += 5;
5153 break;
5155 case CEE_STFLD:
5156 code_bounds_check (5);
5157 do_store_field (&ctx, read32 (ip + 1));
5158 ip += 5;
5159 break;
5161 case CEE_STSFLD:
5162 code_bounds_check (5);
5163 do_store_static_field (&ctx, read32 (ip + 1));
5164 ip += 5;
5165 break;
5167 case CEE_STOBJ:
5168 code_bounds_check (5);
5169 do_stobj (&ctx, read32 (ip + 1));
5170 ip += 5;
5171 break;
5173 case CEE_CONV_OVF_I1_UN:
5174 case CEE_CONV_OVF_I2_UN:
5175 case CEE_CONV_OVF_I4_UN:
5176 case CEE_CONV_OVF_U1_UN:
5177 case CEE_CONV_OVF_U2_UN:
5178 case CEE_CONV_OVF_U4_UN:
5179 do_conversion (&ctx, TYPE_I4);
5180 ++ip;
5181 break;
5183 case CEE_CONV_OVF_I8_UN:
5184 case CEE_CONV_OVF_U8_UN:
5185 do_conversion (&ctx, TYPE_I8);
5186 ++ip;
5187 break;
5189 case CEE_CONV_OVF_I_UN:
5190 case CEE_CONV_OVF_U_UN:
5191 do_conversion (&ctx, TYPE_NATIVE_INT);
5192 ++ip;
5193 break;
5195 case CEE_BOX:
5196 code_bounds_check (5);
5197 do_box_value (&ctx, read32 (ip + 1));
5198 ip += 5;
5199 break;
5201 case CEE_NEWARR:
5202 code_bounds_check (5);
5203 do_newarr (&ctx, read32 (ip + 1));
5204 ip += 5;
5205 break;
5207 case CEE_LDLEN:
5208 do_ldlen (&ctx);
5209 ++ip;
5210 break;
5212 case CEE_LDELEMA:
5213 code_bounds_check (5);
5214 do_ldelema (&ctx, read32 (ip + 1));
5215 ip += 5;
5216 break;
5218 case CEE_LDELEM_I1:
5219 case CEE_LDELEM_U1:
5220 case CEE_LDELEM_I2:
5221 case CEE_LDELEM_U2:
5222 case CEE_LDELEM_I4:
5223 case CEE_LDELEM_U4:
5224 case CEE_LDELEM_I8:
5225 case CEE_LDELEM_I:
5226 case CEE_LDELEM_R4:
5227 case CEE_LDELEM_R8:
5228 case CEE_LDELEM_REF:
5229 do_ldelem (&ctx, *ip, 0);
5230 ++ip;
5231 break;
5233 case CEE_STELEM_I:
5234 case CEE_STELEM_I1:
5235 case CEE_STELEM_I2:
5236 case CEE_STELEM_I4:
5237 case CEE_STELEM_I8:
5238 case CEE_STELEM_R4:
5239 case CEE_STELEM_R8:
5240 case CEE_STELEM_REF:
5241 do_stelem (&ctx, *ip, 0);
5242 ++ip;
5243 break;
5245 case CEE_LDELEM:
5246 code_bounds_check (5);
5247 do_ldelem (&ctx, *ip, read32 (ip + 1));
5248 ip += 5;
5249 break;
5251 case CEE_STELEM:
5252 code_bounds_check (5);
5253 do_stelem (&ctx, *ip, read32 (ip + 1));
5254 ip += 5;
5255 break;
5257 case CEE_UNBOX_ANY:
5258 code_bounds_check (5);
5259 do_unbox_any (&ctx, read32 (ip + 1));
5260 ip += 5;
5261 break;
5263 case CEE_CONV_OVF_I1:
5264 case CEE_CONV_OVF_U1:
5265 case CEE_CONV_OVF_I2:
5266 case CEE_CONV_OVF_U2:
5267 case CEE_CONV_OVF_I4:
5268 case CEE_CONV_OVF_U4:
5269 do_conversion (&ctx, TYPE_I4);
5270 ++ip;
5271 break;
5273 case CEE_CONV_OVF_I8:
5274 case CEE_CONV_OVF_U8:
5275 do_conversion (&ctx, TYPE_I8);
5276 ++ip;
5277 break;
5279 case CEE_CONV_OVF_I:
5280 case CEE_CONV_OVF_U:
5281 do_conversion (&ctx, TYPE_NATIVE_INT);
5282 ++ip;
5283 break;
5285 case CEE_REFANYVAL:
5286 code_bounds_check (5);
5287 do_refanyval (&ctx, read32 (ip + 1));
5288 ip += 5;
5289 break;
5291 case CEE_CKFINITE:
5292 do_ckfinite (&ctx);
5293 ++ip;
5294 break;
5296 case CEE_MKREFANY:
5297 code_bounds_check (5);
5298 do_mkrefany (&ctx, read32 (ip + 1));
5299 ip += 5;
5300 break;
5302 case CEE_LDTOKEN:
5303 code_bounds_check (5);
5304 do_load_token (&ctx, read32 (ip + 1));
5305 ip += 5;
5306 break;
5308 case CEE_ENDFINALLY:
5309 if (!is_correct_endfinally (ctx.header, ip_offset))
5310 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5311 ctx.eval.size = 0;
5312 start = 1;
5313 ++ip;
5314 break;
5316 case CEE_LEAVE:
5317 code_bounds_check (5);
5318 do_leave (&ctx, read32 (ip + 1) + 5);
5319 ip += 5;
5320 start = 1;
5321 break;
5323 case CEE_LEAVE_S:
5324 code_bounds_check (2);
5325 do_leave (&ctx, (signed char)ip [1] + 2);
5326 ip += 2;
5327 start = 1;
5328 break;
5330 case CEE_PREFIX1:
5331 code_bounds_check (2);
5332 ++ip;
5333 switch (*ip) {
5334 case CEE_STLOC:
5335 code_bounds_check (3);
5336 store_local (&ctx, read16 (ip + 1));
5337 ip += 3;
5338 break;
5340 case CEE_CEQ:
5341 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5342 ++ip;
5343 break;
5345 case CEE_CGT:
5346 case CEE_CGT_UN:
5347 case CEE_CLT:
5348 case CEE_CLT_UN:
5349 do_cmp_op (&ctx, cmp_br_op, *ip);
5350 ++ip;
5351 break;
5353 case CEE_STARG:
5354 code_bounds_check (3);
5355 store_arg (&ctx, read16 (ip + 1) );
5356 ip += 3;
5357 break;
5360 case CEE_ARGLIST:
5361 check_overflow (&ctx);
5362 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5363 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5364 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5365 ++ip;
5366 break;
5368 case CEE_LDFTN:
5369 code_bounds_check (5);
5370 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5371 ip += 5;
5372 break;
5374 case CEE_LDVIRTFTN:
5375 code_bounds_check (5);
5376 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5377 ip += 5;
5378 break;
5380 case CEE_LDARG:
5381 case CEE_LDARGA:
5382 code_bounds_check (3);
5383 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5384 ip += 3;
5385 break;
5387 case CEE_LDLOC:
5388 case CEE_LDLOCA:
5389 code_bounds_check (3);
5390 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5391 ip += 3;
5392 break;
5394 case CEE_LOCALLOC:
5395 do_localloc (&ctx);
5396 ++ip;
5397 break;
5399 case CEE_UNUSED56:
5400 case CEE_UNUSED57:
5401 case CEE_UNUSED70:
5402 case CEE_UNUSED:
5403 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
5404 ++ip;
5405 break;
5406 case CEE_ENDFILTER:
5407 do_endfilter (&ctx);
5408 start = 1;
5409 ++ip;
5410 break;
5411 case CEE_UNALIGNED_:
5412 code_bounds_check (2);
5413 prefix |= PREFIX_UNALIGNED;
5414 ip += 2;
5415 break;
5416 case CEE_VOLATILE_:
5417 prefix |= PREFIX_VOLATILE;
5418 ++ip;
5419 break;
5420 case CEE_TAIL_:
5421 prefix |= PREFIX_TAIL;
5422 ++ip;
5423 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5424 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5425 break;
5427 case CEE_INITOBJ:
5428 code_bounds_check (5);
5429 do_initobj (&ctx, read32 (ip + 1));
5430 ip += 5;
5431 break;
5433 case CEE_CONSTRAINED_:
5434 code_bounds_check (5);
5435 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5436 prefix |= PREFIX_CONSTRAINED;
5437 ip += 5;
5438 break;
5440 case CEE_READONLY_:
5441 prefix |= PREFIX_READONLY;
5442 ip++;
5443 break;
5445 case CEE_CPBLK:
5446 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5447 if (!check_underflow (&ctx, 3))
5448 break;
5449 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5450 ip++;
5451 break;
5453 case CEE_INITBLK:
5454 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5455 if (!check_underflow (&ctx, 3))
5456 break;
5457 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5458 ip++;
5459 break;
5461 case CEE_NO_:
5462 ip += 2;
5463 break;
5464 case CEE_RETHROW:
5465 if (!is_correct_rethrow (ctx.header, ip_offset))
5466 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5467 ctx.eval.size = 0;
5468 start = 1;
5469 ++ip;
5470 break;
5472 case CEE_SIZEOF:
5473 code_bounds_check (5);
5474 do_sizeof (&ctx, read32 (ip + 1));
5475 ip += 5;
5476 break;
5478 case CEE_REFANYTYPE:
5479 do_refanytype (&ctx);
5480 ++ip;
5481 break;
5483 default:
5484 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5485 ++ip;
5487 break;
5489 default:
5490 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5491 ++ip;
5494 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5495 if (prefix) {
5496 if (!ctx.prefix_set) //first prefix
5497 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5498 ctx.prefix_set |= prefix;
5499 ctx.has_flags = TRUE;
5500 prefix = 0;
5501 } else {
5502 if (!ctx.has_flags)
5503 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5505 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5506 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5507 if (ctx.prefix_set & PREFIX_READONLY)
5508 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5509 if (ctx.prefix_set & PREFIX_VOLATILE)
5510 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5511 if (ctx.prefix_set & PREFIX_UNALIGNED)
5512 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5513 ctx.prefix_set = prefix = 0;
5514 ctx.has_flags = FALSE;
5518 * if ip != end we overflowed: mark as error.
5520 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5521 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5524 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5525 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5526 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5527 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5528 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5530 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5531 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5533 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5534 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5536 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5537 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid call to a non-final virtual function in method with stdarg.0 or ldarga.0 at 0x%04x", i));
5540 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5541 char *method_name = mono_method_full_name (ctx.method, TRUE);
5542 char *type = mono_type_get_full_name (ctx.method->klass);
5543 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5544 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor due to a TypeLoadException on base type.", method_name, type));
5545 else
5546 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5547 g_free (method_name);
5548 g_free (type);
5551 cleanup:
5552 if (ctx.code) {
5553 for (i = 0; i < ctx.header->code_size; ++i) {
5554 if (ctx.code [i].stack)
5555 g_free (ctx.code [i].stack);
5559 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5560 g_free (tmp->data);
5561 g_slist_free (ctx.funptrs);
5563 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5564 mono_metadata_free_type (tmp->data);
5565 g_slist_free (ctx.exception_types);
5567 for (i = 0; i < ctx.num_locals; ++i) {
5568 if (ctx.locals [i])
5569 mono_metadata_free_type (ctx.locals [i]);
5571 for (i = 0; i < ctx.max_args; ++i) {
5572 if (ctx.params [i])
5573 mono_metadata_free_type (ctx.params [i]);
5576 if (ctx.eval.stack)
5577 g_free (ctx.eval.stack);
5578 if (ctx.code)
5579 g_free (ctx.code);
5580 g_free (ctx.locals);
5581 g_free (ctx.params);
5582 mono_basic_block_free (original_bb);
5583 mono_metadata_free_mh (ctx.header);
5585 finish_collect_stats ();
5586 return ctx.list;
5589 char*
5590 mono_verify_corlib ()
5592 /* This is a public API function so cannot be removed */
5593 return NULL;
5597 * Returns true if @method needs to be verified.
5600 gboolean
5601 mono_verifier_is_enabled_for_method (MonoMethod *method)
5603 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
5607 * Returns true if @klass need to be verified.
5610 gboolean
5611 mono_verifier_is_enabled_for_class (MonoClass *klass)
5613 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !(klass->image->assembly && klass->image->assembly->in_gac) && klass->image != mono_defaults.corlib);
5616 gboolean
5617 mono_verifier_is_enabled_for_image (MonoImage *image)
5619 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
5622 gboolean
5623 mono_verifier_is_method_full_trust (MonoMethod *method)
5625 return mono_verifier_is_class_full_trust (method->klass);
5629 * Returns if @klass is under full trust or not.
5631 * TODO This code doesn't take CAS into account.
5633 * Under verify_all all user code must be verifiable if no security option was set
5636 gboolean
5637 mono_verifier_is_class_full_trust (MonoClass *klass)
5639 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
5640 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
5641 (klass->image->assembly && klass->image->assembly->in_gac) : mono_security_core_clr_is_platform_image (klass->image);
5643 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
5644 return trusted_location || klass->image == mono_defaults.corlib;
5645 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
5648 GSList*
5649 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5651 return mono_method_verify (method,
5652 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
5653 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
5654 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
5657 static int
5658 get_field_end (MonoClassField *field)
5660 int align;
5661 int size = mono_type_size (field->type, &align);
5662 if (size == 0)
5663 size = 4; /*FIXME Is this a safe bet?*/
5664 return size + field->offset;
5667 static gboolean
5668 verify_class_for_overlapping_reference_fields (MonoClass *class)
5670 int i = 0, j;
5671 gpointer iter = NULL;
5672 MonoClassField *field;
5673 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
5674 /*We can't skip types with !has_references since this is calculated after we have run.*/
5675 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT))
5676 return TRUE;
5679 /*We must check for stuff overlapping reference fields.
5680 The outer loop uses mono_class_get_fields to ensure that MonoClass:fields get inited.
5682 while ((field = mono_class_get_fields (class, &iter))) {
5683 int fieldEnd = get_field_end (field);
5684 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
5685 ++i;
5687 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
5688 continue;
5690 for (j = i; j < class->field.count; ++j) {
5691 MonoClassField *other = &class->fields [j];
5692 int otherEnd = get_field_end (other);
5693 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
5694 continue;
5696 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
5697 continue;
5699 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
5700 return FALSE;
5703 return TRUE;
5706 static guint
5707 field_hash (gconstpointer key)
5709 const MonoClassField *field = key;
5710 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
5713 static gboolean
5714 field_equals (gconstpointer _a, gconstpointer _b)
5716 const MonoClassField *a = _a;
5717 const MonoClassField *b = _b;
5718 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
5722 static gboolean
5723 verify_class_fields (MonoClass *class)
5725 gpointer iter = NULL;
5726 MonoClassField *field;
5727 MonoGenericContext *context = mono_class_get_context (class);
5728 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
5729 if (class->generic_container)
5730 context = &class->generic_container->context;
5732 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5733 if (!mono_type_is_valid_type_in_context (field->type, context)) {
5734 g_hash_table_destroy (unique_fields);
5735 return FALSE;
5737 if (g_hash_table_lookup (unique_fields, field)) {
5738 g_hash_table_destroy (unique_fields);
5739 return FALSE;
5741 g_hash_table_insert (unique_fields, field, field);
5743 g_hash_table_destroy (unique_fields);
5744 return TRUE;
5747 static gboolean
5748 verify_interfaces (MonoClass *class)
5750 int i;
5751 for (i = 0; i < class->interface_count; ++i) {
5752 MonoClass *iface = class->interfaces [i];
5753 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
5754 return FALSE;
5756 return TRUE;
5759 static gboolean
5760 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
5762 int type;
5763 gpointer iter = NULL;
5764 MonoClassField *field;
5765 MonoClass *field_class;
5767 if (!class->valuetype)
5768 return TRUE;
5770 type = class->byval_arg.type;
5771 /*primitive type fields are not properly decoded*/
5772 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
5773 return TRUE;
5775 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
5776 if (!field->type)
5777 return FALSE;
5779 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
5780 continue;
5782 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
5784 if (field_class == target_class || class == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
5785 return FALSE;
5788 return TRUE;
5791 static gboolean
5792 verify_valuetype_layout (MonoClass *class)
5794 gboolean res;
5795 res = verify_valuetype_layout_with_target (class, class);
5796 return res;
5799 static gboolean
5800 verify_generic_parameters (MonoClass *class)
5802 int i;
5803 MonoGenericContainer *gc = class->generic_container;
5805 for (i = 0; i < gc->type_argc; ++i) {
5806 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
5807 MonoClass **constraints;
5809 if (!param_info->constraints)
5810 continue;
5812 for (constraints = param_info->constraints; *constraints; ++constraints) {
5813 MonoClass *ctr = *constraints;
5815 if (!mono_type_is_valid_type_in_context (&ctr->byval_arg, &gc->context))
5816 return FALSE;
5819 return TRUE;
5823 * Check if the class is verifiable.
5825 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
5826 * field and invalid generic instantiation are fatal errors.
5828 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
5831 gboolean
5832 mono_verifier_verify_class (MonoClass *class)
5834 /*Neither <Module>, object or ifaces have parent.*/
5835 if (!class->parent &&
5836 class != mono_defaults.object_class &&
5837 !MONO_CLASS_IS_INTERFACE (class) &&
5838 (!class->image->dynamic && class->type_token != 0x2000001)) /*<Module> is the first type in the assembly*/
5839 return FALSE;
5840 if (class->parent && MONO_CLASS_IS_INTERFACE (class->parent))
5841 return FALSE;
5842 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
5843 return FALSE;
5844 if (class->generic_container && !verify_generic_parameters (class))
5845 return FALSE;
5846 if (!verify_class_for_overlapping_reference_fields (class))
5847 return FALSE;
5848 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
5849 return FALSE;
5850 if (class->generic_class == NULL && !verify_class_fields (class))
5851 return FALSE;
5852 if (class->valuetype && !verify_valuetype_layout (class))
5853 return FALSE;
5854 if (!verify_interfaces (class))
5855 return FALSE;
5856 return TRUE;
5858 #else
5860 gboolean
5861 mono_verifier_verify_class (MonoClass *class)
5863 /* The verifier was disabled at compile time */
5864 return TRUE;
5867 GSList*
5868 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
5870 /* The verifier was disabled at compile time */
5871 return NULL;
5874 gboolean
5875 mono_verifier_is_class_full_trust (MonoClass *klass)
5877 /* The verifier was disabled at compile time */
5878 return TRUE;
5881 gboolean
5882 mono_verifier_is_method_full_trust (MonoMethod *method)
5884 /* The verifier was disabled at compile time */
5885 return TRUE;
5888 gboolean
5889 mono_verifier_is_enabled_for_image (MonoImage *image)
5891 /* The verifier was disabled at compile time */
5892 return FALSE;
5895 gboolean
5896 mono_verifier_is_enabled_for_class (MonoClass *klass)
5898 /* The verifier was disabled at compile time */
5899 return FALSE;
5902 gboolean
5903 mono_verifier_is_enabled_for_method (MonoMethod *method)
5905 /* The verifier was disabled at compile time */
5906 return FALSE;
5909 GSList*
5910 mono_method_verify (MonoMethod *method, int level)
5912 /* The verifier was disabled at compile time */
5913 return NULL;
5916 void
5917 mono_free_verify_list (GSList *list)
5919 /* The verifier was disabled at compile time */
5920 /* will always be null if verifier is disabled */
5923 #endif