2009-12-02 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / verify.c
blob62caa40371f08d3c692ff27ab6bb3f9ded99dbbe
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 <string.h>
27 #include <signal.h>
28 #include <ctype.h>
31 static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
32 static gboolean verify_all = FALSE;
35 * Set the desired level of checks for the verfier.
38 void
39 mono_verifier_set_mode (MiniVerifierMode mode)
41 verifier_mode = mode;
44 void
45 mono_verifier_enable_verify_all ()
47 verify_all = TRUE;
50 #ifndef DISABLE_VERIFIER
52 * Pull the list of opcodes
54 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
55 a = i,
57 enum {
58 #include "mono/cil/opcode.def"
59 LAST = 0xff
61 #undef OPDEF
63 #ifdef MONO_VERIFIER_DEBUG
64 #define VERIFIER_DEBUG(code) do { code } while (0)
65 #else
66 #define VERIFIER_DEBUG(code)
67 #endif
69 //////////////////////////////////////////////////////////////////
70 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
71 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
72 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
73 #define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
74 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
75 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
76 do { \
77 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
78 vinfo->info.status = __status; \
79 vinfo->info.message = ( __msg ); \
80 vinfo->exception_type = (__exception); \
81 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
82 } while (0)
84 //TODO support MONO_VERIFY_REPORT_ALL_ERRORS
85 #define ADD_VERIFY_ERROR(__ctx, __msg) \
86 do { \
87 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
88 (__ctx)->valid = 0; \
89 } while (0)
91 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
92 do { \
93 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
94 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
95 (__ctx)->verifiable = 0; \
96 if (IS_FAIL_FAST_MODE (__ctx)) \
97 (__ctx)->valid = 0; \
98 } \
99 } while (0)
101 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
102 do { \
103 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
104 (__ctx)->valid = 0; \
105 } while (0)
107 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
108 do { \
109 if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
110 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
111 (__ctx)->verifiable = 0; \
112 if (IS_FAIL_FAST_MODE (__ctx)) \
113 (__ctx)->valid = 0; \
115 } while (0)
117 #define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
118 #define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
120 #if SIZEOF_VOID_P == 4
121 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
122 #else
123 #define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
124 #endif
126 #define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
127 #define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
129 /*Flags to be used with ILCodeDesc::flags */
130 enum {
131 /*Instruction has not been processed.*/
132 IL_CODE_FLAG_NOT_PROCESSED = 0,
133 /*Instruction was decoded by mono_method_verify loop.*/
134 IL_CODE_FLAG_SEEN = 1,
135 /*Instruction was target of a branch or is at a protected block boundary.*/
136 IL_CODE_FLAG_WAS_TARGET = 2,
137 /*Used by stack_init to avoid double initialize each entry.*/
138 IL_CODE_FLAG_STACK_INITED = 4,
139 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
140 IL_CODE_STACK_MERGED = 8,
141 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
142 IL_CODE_DELEGATE_SEQUENCE = 0x10,
143 /*This is a delegate created from a ldftn to a non final virtual method*/
144 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
145 /*This is a call to a non final virtual method*/
146 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
149 typedef enum {
150 RESULT_VALID,
151 RESULT_UNVERIFIABLE,
152 RESULT_INVALID
153 } verify_result_t;
155 typedef struct {
156 MonoType *type;
157 int stype;
158 MonoMethod *method;
159 } ILStackDesc;
162 typedef struct {
163 ILStackDesc *stack;
164 guint16 size;
165 guint16 flags;
166 } ILCodeDesc;
168 typedef struct {
169 int max_args;
170 int max_stack;
171 int verifiable;
172 int valid;
173 int level;
175 int code_size;
176 ILCodeDesc *code;
177 ILCodeDesc eval;
179 MonoType **params;
180 GSList *list;
181 /*Allocated fnptr MonoType that should be freed by us.*/
182 GSList *funptrs;
183 /*Type dup'ed exception types from catch blocks.*/
184 GSList *exception_types;
186 int num_locals;
187 MonoType **locals;
189 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
190 int target;
192 guint32 ip_offset;
193 MonoMethodSignature *signature;
194 MonoMethodHeader *header;
196 MonoGenericContext *generic_context;
197 MonoImage *image;
198 MonoMethod *method;
200 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
201 *on a method that creates a delegate for a non-final virtual method using ldftn*/
202 gboolean has_this_store;
204 /*This flag is used to control if the contructor of the parent class has been called.
205 *If the this pointer is pushed on the eval stack and it's a reference type constructor and
206 * super_ctor_called is false, the uninitialized flag is set on the pushed value.
208 * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
209 * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
211 gboolean super_ctor_called;
213 guint32 prefix_set;
214 gboolean has_flags;
215 MonoType *constrained_type;
216 } VerifyContext;
218 static void
219 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
221 static int
222 get_stack_type (MonoType *type);
224 static gboolean
225 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
227 static gboolean
228 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
230 static gboolean
231 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
232 //////////////////////////////////////////////////////////////////
236 enum {
237 TYPE_INV = 0, /* leave at 0. */
238 TYPE_I4 = 1,
239 TYPE_I8 = 2,
240 TYPE_NATIVE_INT = 3,
241 TYPE_R8 = 4,
242 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
243 TYPE_PTR = 5,
244 /* value types and classes */
245 TYPE_COMPLEX = 6,
246 /* Number of types, used to define the size of the tables*/
247 TYPE_MAX = 6,
249 /* Used by tables to signal that a result is not verifiable*/
250 NON_VERIFIABLE_RESULT = 0x80,
252 /*Mask used to extract just the type, excluding flags */
253 TYPE_MASK = 0x0F,
255 /* The stack type is a managed pointer, unmask the value to res */
256 POINTER_MASK = 0x100,
258 /*Stack type with the pointer mask*/
259 RAW_TYPE_MASK = 0x10F,
261 /* Controlled Mutability Manager Pointer */
262 CMMP_MASK = 0x200,
264 /* The stack type is a null literal*/
265 NULL_LITERAL_MASK = 0x400,
267 /**Used by ldarg.0 and family to let delegate verification happens.*/
268 THIS_POINTER_MASK = 0x800,
270 /**Signals that this is a boxed value type*/
271 BOXED_MASK = 0x1000,
273 /*This is an unitialized this ref*/
274 UNINIT_THIS_MASK = 0x2000,
277 static const char* const
278 type_names [TYPE_MAX + 1] = {
279 "Invalid",
280 "Int32",
281 "Int64",
282 "Native Int",
283 "Float64",
284 "Native Pointer",
285 "Complex"
288 enum {
289 PREFIX_UNALIGNED = 1,
290 PREFIX_VOLATILE = 2,
291 PREFIX_TAIL = 4,
292 PREFIX_CONSTRAINED = 8,
293 PREFIX_READONLY = 16
295 //////////////////////////////////////////////////////////////////
298 /*Token validation macros and functions */
299 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
300 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
301 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
302 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
304 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
305 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
306 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
307 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
308 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
309 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
312 * Verify if @token refers to a valid row on int's table.
314 static gboolean
315 token_bounds_check (MonoImage *image, guint32 token)
317 if (image->dynamic)
318 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
319 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
322 static MonoType *
323 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
325 MonoType *res = g_new0 (MonoType, 1);
326 //FIXME use mono_method_get_signature_full
327 res->data.method = mono_method_signature (method);
328 res->type = MONO_TYPE_FNPTR;
329 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
330 return res;
334 * mono_type_is_enum_type:
336 * Returns TRUE if @type is an enum type.
338 static gboolean
339 mono_type_is_enum_type (MonoType *type)
341 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
342 return TRUE;
343 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
344 return TRUE;
345 return FALSE;
349 * mono_type_is_value_type:
351 * Returns TRUE if @type is named after @namespace.@name.
354 static gboolean
355 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
357 return type->type == MONO_TYPE_VALUETYPE &&
358 !strcmp (namespace, type->data.klass->name_space) &&
359 !strcmp (name, type->data.klass->name);
363 * Returns TURE if @type is VAR or MVAR
365 static gboolean
366 mono_type_is_generic_argument (MonoType *type)
368 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
372 * mono_type_get_underlying_type_any:
374 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
376 * Returns the underlying type of @type regardless if it is byref or not.
378 static MonoType*
379 mono_type_get_underlying_type_any (MonoType *type)
381 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
382 return mono_class_enum_basetype (type->data.klass);
383 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
384 return mono_class_enum_basetype (type->data.generic_class->container_class);
385 return type;
388 static const char*
389 mono_type_get_stack_name (MonoType *type)
391 return type_names [get_stack_type (type) & TYPE_MASK];
394 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
395 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
397 static gboolean
398 mono_method_is_constructor (MonoMethod *method)
400 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
401 !(method->flags & CTOR_INVALID_FLAGS) &&
402 !strcmp (".ctor", method->name));
405 static gboolean
406 mono_class_has_default_constructor (MonoClass *klass)
408 MonoMethod *method;
409 int i;
411 mono_class_setup_methods (klass);
413 for (i = 0; i < klass->method.count; ++i) {
414 method = klass->methods [i];
415 if (mono_method_is_constructor (method) &&
416 mono_method_signature (method) &&
417 mono_method_signature (method)->param_count == 0 &&
418 (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
419 return TRUE;
421 return FALSE;
424 static gboolean
425 mono_class_interface_implements_interface (MonoClass *candidate, MonoClass *iface)
427 int i;
428 do {
429 if (candidate == iface)
430 return TRUE;
431 mono_class_setup_interfaces (candidate);
432 for (i = 0; i < candidate->interface_count; ++i) {
433 if (candidate->interfaces [i] == iface || mono_class_interface_implements_interface (candidate->interfaces [i], iface))
434 return TRUE;
436 candidate = candidate->parent;
437 } while (candidate);
438 return FALSE;
442 * Verify if @type is valid for the given @ctx verification context.
443 * this function checks for VAR and MVAR types that are invalid under the current verifier,
445 static gboolean
446 mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
448 int i;
449 MonoGenericInst *inst;
451 switch (type->type) {
452 case MONO_TYPE_VAR:
453 case MONO_TYPE_MVAR:
454 if (!context)
455 return FALSE;
456 inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
457 if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
458 return FALSE;
459 break;
460 case MONO_TYPE_SZARRAY:
461 return mono_type_is_valid_type_in_context (&type->data.klass->byval_arg, context);
462 case MONO_TYPE_ARRAY:
463 return mono_type_is_valid_type_in_context (&type->data.array->eklass->byval_arg, context);
464 case MONO_TYPE_PTR:
465 return mono_type_is_valid_type_in_context (type->data.type, context);
466 case MONO_TYPE_GENERICINST:
467 inst = type->data.generic_class->context.class_inst;
468 if (!inst->is_open)
469 break;
470 for (i = 0; i < inst->type_argc; ++i)
471 if (!mono_type_is_valid_type_in_context (inst->type_argv [i], context))
472 return FALSE;
473 break;
475 return TRUE;
478 /*This function returns NULL if the type is not instantiatable*/
479 static MonoType*
480 verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
482 MonoError error;
483 MonoType *result;
485 result = mono_class_inflate_generic_type_checked (type, context, &error);
486 if (!mono_error_ok (&error)) {
487 mono_error_cleanup (&error);
488 return NULL;
490 return result;
493 * Test if @candidate is a subtype of @target using the minimal possible information
494 * TODO move the code for non finished TypeBuilders to here.
496 static gboolean
497 mono_class_is_constraint_compatible (MonoClass *candidate, MonoClass *target)
499 if (candidate == target)
500 return TRUE;
501 if (target == mono_defaults.object_class)
502 return TRUE;
504 //setup_supertypes don't mono_class_init anything
505 mono_class_setup_supertypes (candidate);
506 mono_class_setup_supertypes (target);
508 if (mono_class_has_parent (candidate, target))
509 return TRUE;
511 //if target is not a supertype it must be an interface
512 if (!MONO_CLASS_IS_INTERFACE (target))
513 return FALSE;
515 if (candidate->image->dynamic && !candidate->wastypebuilder) {
516 MonoReflectionTypeBuilder *tb = candidate->reflection_info;
517 int j;
518 if (tb->interfaces) {
519 for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
520 MonoReflectionType *iface = mono_array_get (tb->interfaces, MonoReflectionType*, j);
521 MonoClass *ifaceClass = mono_class_from_mono_type (iface->type);
522 if (mono_class_is_constraint_compatible (ifaceClass, target)) {
523 return TRUE;
527 return FALSE;
529 return mono_class_interface_implements_interface (candidate, target);
532 static gboolean
533 is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
535 MonoError error;
536 int i;
538 if (ginst->type_argc != gc->type_argc)
539 return FALSE;
541 for (i = 0; i < gc->type_argc; ++i) {
542 MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
543 MonoClass *paramClass;
544 MonoClass **constraints;
546 if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
547 continue;
548 if (mono_type_is_generic_argument (ginst->type_argv [i]))
549 continue; //it's not our job to validate type variables
551 paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
553 if (paramClass->exception_type != MONO_EXCEPTION_NONE)
554 return FALSE;
556 /*it's not safe to call mono_class_init from here*/
557 if (paramClass->generic_class && !paramClass->inited) {
558 if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
559 return FALSE;
562 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
563 return FALSE;
565 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
566 return FALSE;
568 if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
569 return FALSE;
571 if (!param_info->constraints)
572 continue;
574 for (constraints = param_info->constraints; *constraints; ++constraints) {
575 MonoClass *ctr = *constraints;
576 MonoType *inflated;
578 inflated = mono_class_inflate_generic_type_checked (&ctr->byval_arg, context, &error);
579 if (!mono_error_ok (&error)) {
580 mono_error_cleanup (&error);
581 return FALSE;
583 ctr = mono_class_from_mono_type (inflated);
584 mono_metadata_free_type (inflated);
586 if (!mono_class_is_constraint_compatible (paramClass, ctr))
587 return FALSE;
590 return TRUE;
594 * Return true if @candidate is constraint compatible with @target.
596 * This means that @candidate constraints are a super set of @target constaints
598 static gboolean
599 mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoGenericContext *context)
601 MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
602 MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
604 int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
605 int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
606 if ((tmask & cmask) != tmask)
607 return FALSE;
609 if (tinfo->constraints) {
610 MonoClass **target_class, **candidate_class;
611 if (!cinfo->constraints)
612 return FALSE;
613 for (target_class = tinfo->constraints; *target_class; ++target_class) {
614 MonoClass *tc;
615 MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
616 if (!inflated)
617 return FALSE;
618 tc = mono_class_from_mono_type (inflated);
619 mono_metadata_free_type (inflated);
621 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
622 MonoClass *cc;
623 inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
624 if (!inflated)
625 return FALSE;
626 cc = mono_class_from_mono_type (inflated);
627 mono_metadata_free_type (inflated);
629 if (mono_class_is_assignable_from (tc, cc))
630 break;
632 if (!*candidate_class)
633 return FALSE;
636 return TRUE;
639 static MonoGenericParam*
640 verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
642 MonoGenericContainer *gc;
643 MonoMethod *method = ctx->method;
644 int num;
646 num = mono_type_get_generic_param_num (type);
648 if (type->type == MONO_TYPE_VAR) {
649 MonoClass *gtd = method->klass;
650 if (gtd->generic_class)
651 gtd = gtd->generic_class->container_class;
652 gc = gtd->generic_container;
653 } else { //MVAR
654 MonoMethod *gmd = method;
655 if (method->is_inflated)
656 gmd = ((MonoMethodInflated*)method)->declaring;
657 gc = mono_method_get_generic_container (gmd);
659 if (!gc)
660 return FALSE;
661 return mono_generic_container_get_param (gc, num);
667 * Verify if @type is valid for the given @ctx verification context.
668 * this function checks for VAR and MVAR types that are invalid under the current verifier,
669 * This means that it either
671 static gboolean
672 is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
674 return mono_type_is_valid_type_in_context (type, ctx->generic_context);
677 static gboolean
678 is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst)
680 int i;
681 for (i = 0; i < ginst->type_argc; ++i) {
682 MonoType *type = ginst->type_argv [i];
683 if (!is_valid_type_in_context (ctx, type))
684 return FALSE;
686 return TRUE;
689 static gboolean
690 generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
692 int i;
693 for (i = 0; i < ginst->type_argc; ++i) {
694 MonoType *type = ginst->type_argv [i];
695 MonoGenericParam *target = mono_generic_container_get_param (gc, i);
696 MonoGenericParam *candidate;
698 if (!mono_type_is_generic_argument (type))
699 continue;
701 if (!is_valid_type_in_context (ctx, type))
702 return FALSE;
704 candidate = verifier_get_generic_param_from_type (ctx, type);
706 if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, context))
707 return FALSE;
709 return TRUE;
712 static gboolean
713 mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
715 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
716 MonoGenericInst *ginst = gmethod->context.method_inst;
717 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
718 return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
721 static gboolean
722 mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
724 MonoGenericClass *gklass = klass->generic_class;
725 MonoGenericInst *ginst = gklass->context.class_inst;
726 MonoGenericContainer *gc = gklass->container_class->generic_container;
727 return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
730 static gboolean
731 mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
733 MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
734 MonoGenericInst *ginst = gmethod->context.method_inst;
735 MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
736 if (!gc) /*non-generic inflated method - it's part of a generic type */
737 return TRUE;
738 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
739 return FALSE;
740 return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
744 static gboolean
745 mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
747 MonoGenericClass *gklass = klass->generic_class;
748 MonoGenericInst *ginst = gklass->context.class_inst;
749 MonoGenericContainer *gc = gklass->container_class->generic_container;
750 if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
751 return FALSE;
752 return is_valid_generic_instantiation (gc, &gklass->context, ginst);
755 static gboolean
756 mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
758 MonoClass *klass;
760 if (type == NULL) {
761 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
762 return FALSE;
765 if (!is_valid_type_in_context (ctx, type)) {
766 char *str = mono_type_full_name (type);
767 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
768 type->type == MONO_TYPE_VAR ? "!" : "!!",
769 str,
770 type->type == MONO_TYPE_VAR ? "class" : "method",
771 ctx->ip_offset),
772 MONO_EXCEPTION_BAD_IMAGE);
773 g_free (str);
774 return FALSE;
777 klass = mono_class_from_mono_type (type);
778 mono_class_init (klass);
779 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
780 if (klass->generic_class && !mono_class_is_valid_generic_instantiation (NULL, klass))
781 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);
782 else
783 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);
784 return FALSE;
787 if (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE) {
788 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);
789 return FALSE;
792 if (!klass->generic_class)
793 return TRUE;
795 if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
796 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);
797 return FALSE;
800 if (!mono_class_repect_method_constraints (ctx, klass)) {
801 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);
802 return FALSE;
805 return TRUE;
808 static verify_result_t
809 mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
811 if (!mono_type_is_valid_in_context (ctx, &method->klass->byval_arg))
812 return RESULT_INVALID;
814 if (!method->is_inflated)
815 return RESULT_VALID;
817 if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
818 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);
819 return RESULT_INVALID;
822 if (!mono_method_repect_method_constraints (ctx, method)) {
823 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));
824 return RESULT_UNVERIFIABLE;
826 return RESULT_VALID;
830 static MonoClassField*
831 verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
832 MonoClassField *field;
833 MonoClass *klass = NULL;
835 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
836 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);
837 return NULL;
840 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
841 if (!field || !field->parent || !klass) {
842 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);
843 return NULL;
846 if (!mono_type_is_valid_in_context (ctx, &klass->byval_arg))
847 return NULL;
849 *out_klass = klass;
850 return field;
853 static MonoMethod*
854 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
855 MonoMethod* method;
857 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
858 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);
859 return NULL;
862 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
864 if (!method) {
865 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);
866 return NULL;
869 if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
870 return NULL;
872 return method;
875 static MonoType*
876 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
877 MonoType* type;
879 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
880 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
881 return NULL;
884 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
886 if (!type) {
887 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);
888 return NULL;
891 if (!mono_type_is_valid_in_context (ctx, type))
892 return NULL;
894 return type;
898 /* stack_slot_get_type:
900 * Returns the stack type of @value. This value includes POINTER_MASK.
902 * Use this function to checks that account for a managed pointer.
904 static gint32
905 stack_slot_get_type (ILStackDesc *value)
907 return value->stype & RAW_TYPE_MASK;
910 /* stack_slot_get_underlying_type:
912 * Returns the stack type of @value. This value does not include POINTER_MASK.
914 * Use this function is cases where the fact that the value could be a managed pointer is
915 * irrelevant. For example, field load doesn't care about this fact of type on stack.
917 static gint32
918 stack_slot_get_underlying_type (ILStackDesc *value)
920 return value->stype & TYPE_MASK;
923 /* stack_slot_is_managed_pointer:
925 * Returns TRUE is @value is a managed pointer.
927 static gboolean
928 stack_slot_is_managed_pointer (ILStackDesc *value)
930 return (value->stype & POINTER_MASK) == POINTER_MASK;
933 /* stack_slot_is_managed_mutability_pointer:
935 * Returns TRUE is @value is a managed mutability pointer.
937 static G_GNUC_UNUSED gboolean
938 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
940 return (value->stype & CMMP_MASK) == CMMP_MASK;
943 /* stack_slot_is_null_literal:
945 * Returns TRUE is @value is the null literal.
947 static gboolean
948 stack_slot_is_null_literal (ILStackDesc *value)
950 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
954 /* stack_slot_is_this_pointer:
956 * Returns TRUE is @value is the this literal
958 static gboolean
959 stack_slot_is_this_pointer (ILStackDesc *value)
961 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
964 /* stack_slot_is_boxed_value:
966 * Returns TRUE is @value is a boxed value
968 static gboolean
969 stack_slot_is_boxed_value (ILStackDesc *value)
971 return (value->stype & BOXED_MASK) == BOXED_MASK;
974 static const char *
975 stack_slot_get_name (ILStackDesc *value)
977 return type_names [value->stype & TYPE_MASK];
980 #define APPEND_WITH_PREDICATE(PRED,NAME) do {\
981 if (PRED (value)) { \
982 if (!first) \
983 g_string_append (str, ", "); \
984 g_string_append (str, NAME); \
985 first = FALSE; \
986 } } while (0)
988 static char*
989 stack_slot_stack_type_full_name (ILStackDesc *value)
991 GString *str = g_string_new ("");
992 char *result;
994 if ((value->stype & TYPE_MASK) != value->stype) {
995 gboolean first = TRUE;
996 g_string_append(str, "[");
997 APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
998 APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
999 APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
1000 APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
1001 APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
1002 g_string_append(str, "] ");
1005 g_string_append (str, stack_slot_get_name (value));
1006 result = str->str;
1007 g_string_free (str, FALSE);
1008 return result;
1011 static char*
1012 stack_slot_full_name (ILStackDesc *value)
1014 char *type_name = mono_type_full_name (value->type);
1015 char *stack_name = stack_slot_stack_type_full_name (value);
1016 char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
1017 g_free (type_name);
1018 g_free (stack_name);
1019 return res;
1022 //////////////////////////////////////////////////////////////////
1023 void
1024 mono_free_verify_list (GSList *list)
1026 MonoVerifyInfoExtended *info;
1027 GSList *tmp;
1029 for (tmp = list; tmp; tmp = tmp->next) {
1030 info = tmp->data;
1031 g_free (info->info.message);
1032 g_free (info);
1034 g_slist_free (list);
1037 #define ADD_ERROR(list,msg) \
1038 do { \
1039 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1040 vinfo->info.status = MONO_VERIFY_ERROR; \
1041 vinfo->info.message = (msg); \
1042 (list) = g_slist_prepend ((list), vinfo); \
1043 } while (0)
1045 #define ADD_WARN(list,code,msg) \
1046 do { \
1047 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1048 vinfo->info.status = (code); \
1049 vinfo->info.message = (msg); \
1050 (list) = g_slist_prepend ((list), vinfo); \
1051 } while (0)
1053 static const char
1054 valid_cultures[][9] = {
1055 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
1056 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
1057 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
1058 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
1059 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
1060 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
1061 "da-DK", "de-DE", "de-CH", "de-AT",
1062 "de-LU", "de-LI", "el-GR", "en-US",
1063 "en-GB", "en-AU", "en-CA", "en-NZ",
1064 "en-IE", "en-ZA", "en-JM", "en-CB",
1065 "en-BZ", "en-TT", "en-ZW", "en-PH",
1066 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
1067 "es-CR", "es-PA", "es-DO", "es-VE",
1068 "es-CO", "es-PE", "es-AR", "es-EC",
1069 "es-CL", "es-UY", "es-PY", "es-BO",
1070 "es-SV", "es-HN", "es-NI", "es-PR",
1071 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
1072 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
1073 "hu-HU", "is-IS", "it-IT", "it-CH",
1074 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
1075 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
1076 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
1077 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
1078 "sv-SE", "sv-FI", "th-TH", "tr-TR",
1079 "ur-PK", "id-ID", "uk-UA", "be-BY",
1080 "sl-SI", "et-EE", "lv-LV", "lt-LT",
1081 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
1082 "Cy-az-AZ",
1083 "eu-ES", "mk-MK", "af-ZA",
1084 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
1085 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
1086 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
1087 "gu-IN", "ta-IN", "te-IN", "kn-IN",
1088 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
1089 "kok-IN", "syr-SY", "div-MV"
1092 static int
1093 is_valid_culture (const char *cname)
1095 int i;
1096 int found;
1098 found = *cname == 0;
1099 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
1100 if (g_ascii_strcasecmp (valid_cultures [i], cname)) {
1101 found = 1;
1102 break;
1105 return found;
1108 static int
1109 is_valid_assembly_flags (guint32 flags) {
1110 /* Metadata: 22.1.2 */
1111 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
1112 return ((flags == 1) || (flags == 0));
1115 static int
1116 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
1118 guint32 size;
1119 const char *p, *blob_end;
1121 if (blob_index >= image->heap_blob.size)
1122 return 0;
1123 p = mono_metadata_blob_heap (image, blob_index);
1124 size = mono_metadata_decode_blob_size (p, &blob_end);
1125 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
1126 return 0;
1127 if (notnull && !size)
1128 return 0;
1129 return 1;
1132 static const char*
1133 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
1135 const char *p, *blob_end, *res;
1137 if (str_index >= image->heap_strings.size)
1138 return NULL;
1139 res = p = mono_metadata_string_heap (image, str_index);
1140 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
1141 if (notnull && !*p)
1142 return 0;
1144 * FIXME: should check it's a valid utf8 string, too.
1146 while (p <= blob_end) {
1147 if (!*p)
1148 return res;
1149 ++p;
1151 return *p? NULL: res;
1154 static int
1155 is_valid_cls_ident (const char *p)
1158 * FIXME: we need the full unicode glib support for this.
1159 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
1160 * We do the lame thing for now.
1162 if (!isalpha (*p))
1163 return 0;
1164 ++p;
1165 while (*p) {
1166 if (!isalnum (*p) && *p != '_')
1167 return 0;
1168 ++p;
1170 return 1;
1173 static int
1174 is_valid_filename (const char *p)
1176 if (!*p)
1177 return 0;
1178 return strpbrk (p, "\\//:")? 0: 1;
1181 static GSList*
1182 verify_assembly_table (MonoImage *image, GSList *list, int level)
1184 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
1185 guint32 cols [MONO_ASSEMBLY_SIZE];
1186 const char *p;
1188 if (level & MONO_VERIFY_ERROR) {
1189 if (t->rows > 1)
1190 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
1191 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
1193 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
1194 case ASSEMBLY_HASH_NONE:
1195 case ASSEMBLY_HASH_MD5:
1196 case ASSEMBLY_HASH_SHA1:
1197 break;
1198 default:
1199 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
1202 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
1203 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
1205 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
1206 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
1208 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
1209 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
1210 } else {
1211 if (strpbrk (p, ":\\/."))
1212 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
1215 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
1216 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
1217 } else {
1218 if (!is_valid_culture (p))
1219 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
1222 return list;
1225 static GSList*
1226 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
1228 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
1229 guint32 cols [MONO_ASSEMBLYREF_SIZE];
1230 const char *p;
1231 int i;
1233 if (level & MONO_VERIFY_ERROR) {
1234 for (i = 0; i < t->rows; ++i) {
1235 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
1236 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
1237 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
1239 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
1240 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
1242 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
1243 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
1244 } else {
1245 if (!is_valid_culture (p))
1246 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
1249 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
1250 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
1253 if (level & MONO_VERIFY_WARNING) {
1254 /* check for duplicated rows */
1255 for (i = 0; i < t->rows; ++i) {
1258 return list;
1261 static GSList*
1262 verify_class_layout_table (MonoImage *image, GSList *list, int level)
1264 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
1265 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
1266 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
1267 guint32 value, i;
1269 if (level & MONO_VERIFY_ERROR) {
1270 for (i = 0; i < t->rows; ++i) {
1271 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
1273 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
1274 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
1275 } else {
1276 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
1277 if (value & TYPE_ATTRIBUTE_INTERFACE)
1278 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
1279 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
1280 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
1281 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
1282 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
1283 case 0: case 1: case 2: case 4: case 8: case 16:
1284 case 32: case 64: case 128: break;
1285 default:
1286 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
1288 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1290 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
1291 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
1292 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid with explicit layout", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
1296 * FIXME: we need to check that if class size != 0,
1297 * it needs to be greater than the class calculated size.
1298 * If parent is a valuetype it also needs to be smaller than
1299 * 1 MByte (0x100000 bytes).
1300 * To do both these checks we need to load the referenced
1301 * assemblies, though (the spec claims we didn't have to, bah).
1304 * We need to check that the parent types have the same layout
1305 * type as well.
1311 return list;
1314 static GSList*
1315 verify_constant_table (MonoImage *image, GSList *list, int level)
1317 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
1318 guint32 cols [MONO_CONSTANT_SIZE];
1319 guint32 value, i;
1320 GHashTable *dups = g_hash_table_new (NULL, NULL);
1322 for (i = 0; i < t->rows; ++i) {
1323 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
1325 if (level & MONO_VERIFY_ERROR)
1326 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
1327 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
1328 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
1329 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
1331 switch (cols [MONO_CONSTANT_TYPE]) {
1332 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
1333 case MONO_TYPE_U2:
1334 case MONO_TYPE_U4:
1335 case MONO_TYPE_U8:
1336 if (level & MONO_VERIFY_CLS)
1337 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Type 0x%x not CLS compliant in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
1338 case MONO_TYPE_BOOLEAN:
1339 case MONO_TYPE_CHAR:
1340 case MONO_TYPE_I1:
1341 case MONO_TYPE_I2:
1342 case MONO_TYPE_I4:
1343 case MONO_TYPE_I8:
1344 case MONO_TYPE_R4:
1345 case MONO_TYPE_R8:
1346 case MONO_TYPE_STRING:
1347 case MONO_TYPE_CLASS:
1348 break;
1349 default:
1350 if (level & MONO_VERIFY_ERROR)
1351 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
1353 if (level & MONO_VERIFY_ERROR) {
1354 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
1355 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
1356 case MONO_HASCONSTANT_FIEDDEF:
1357 if (value > image->tables [MONO_TABLE_FIELD].rows)
1358 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
1359 break;
1360 case MONO_HASCONSTANT_PARAM:
1361 if (value > image->tables [MONO_TABLE_PARAM].rows)
1362 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
1363 break;
1364 case MONO_HASCONSTANT_PROPERTY:
1365 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
1366 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
1367 break;
1368 default:
1369 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
1370 break;
1373 if (level & MONO_VERIFY_CLS) {
1375 * FIXME: verify types is consistent with the enum type
1376 * is parent is an enum.
1380 g_hash_table_destroy (dups);
1381 return list;
1384 static GSList*
1385 verify_event_map_table (MonoImage *image, GSList *list, int level)
1387 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
1388 guint32 cols [MONO_EVENT_MAP_SIZE];
1389 guint32 i, last_event;
1390 GHashTable *dups = g_hash_table_new (NULL, NULL);
1392 last_event = 0;
1394 for (i = 0; i < t->rows; ++i) {
1395 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
1396 if (level & MONO_VERIFY_ERROR)
1397 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
1398 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1399 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
1400 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
1401 if (level & MONO_VERIFY_ERROR) {
1402 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
1403 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
1404 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
1405 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
1407 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
1408 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
1409 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
1413 g_hash_table_destroy (dups);
1414 return list;
1417 static GSList*
1418 verify_event_table (MonoImage *image, GSList *list, int level)
1420 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
1421 guint32 cols [MONO_EVENT_SIZE];
1422 const char *p;
1423 guint32 value, i;
1425 for (i = 0; i < t->rows; ++i) {
1426 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
1428 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
1429 if (level & MONO_VERIFY_ERROR)
1430 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
1432 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
1433 if (level & MONO_VERIFY_ERROR)
1434 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
1435 } else {
1436 if (level & MONO_VERIFY_CLS) {
1437 if (!is_valid_cls_ident (p))
1438 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
1442 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
1443 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
1444 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
1445 case MONO_TYPEDEFORREF_TYPEDEF:
1446 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
1447 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1448 break;
1449 case MONO_TYPEDEFORREF_TYPEREF:
1450 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
1451 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1452 break;
1453 case MONO_TYPEDEFORREF_TYPESPEC:
1454 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
1455 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1456 break;
1457 default:
1458 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
1462 * FIXME: check that there is 1 add and remove row in methodsemantics
1463 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
1464 * these while checking methodsemantics).
1465 * check for duplicated names for the same type [ERROR]
1466 * check for CLS duplicate names for the same type [CLS]
1469 return list;
1472 static GSList*
1473 verify_field_table (MonoImage *image, GSList *list, int level)
1475 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
1476 guint32 cols [MONO_FIELD_SIZE];
1477 const char *p;
1478 guint32 i, flags;
1480 for (i = 0; i < t->rows; ++i) {
1481 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
1483 * Check this field has only one owner and that the owner is not
1484 * an interface (done in verify_typedef_table() )
1486 flags = cols [MONO_FIELD_FLAGS];
1487 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
1488 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
1489 case FIELD_ATTRIBUTE_PRIVATE:
1490 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
1491 case FIELD_ATTRIBUTE_ASSEMBLY:
1492 case FIELD_ATTRIBUTE_FAMILY:
1493 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
1494 case FIELD_ATTRIBUTE_PUBLIC:
1495 break;
1496 default:
1497 if (level & MONO_VERIFY_ERROR)
1498 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
1499 break;
1501 if (level & MONO_VERIFY_ERROR) {
1502 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
1503 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
1504 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
1505 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
1506 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
1507 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
1509 * FIXME: check there is only one owner in the respective table.
1510 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
1511 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
1512 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
1515 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
1516 if (level & MONO_VERIFY_ERROR)
1517 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
1518 } else {
1519 if (level & MONO_VERIFY_CLS) {
1520 if (!is_valid_cls_ident (p))
1521 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
1525 * check signature.
1526 * if owner is module needs to be static, access mask needs to be compilercontrolled,
1527 * public or private (not allowed in cls mode).
1528 * if owner is an enum ...
1533 return list;
1536 static GSList*
1537 verify_file_table (MonoImage *image, GSList *list, int level)
1539 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1540 guint32 cols [MONO_FILE_SIZE];
1541 const char *p;
1542 guint32 i;
1543 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1545 for (i = 0; i < t->rows; ++i) {
1546 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
1547 if (level & MONO_VERIFY_ERROR) {
1548 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
1549 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
1550 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
1551 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
1553 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
1554 if (level & MONO_VERIFY_ERROR)
1555 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
1556 } else {
1557 if (level & MONO_VERIFY_ERROR) {
1558 if (!is_valid_filename (p))
1559 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
1560 else if (g_hash_table_lookup (dups, p)) {
1561 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
1563 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1567 * FIXME: I don't understand what this means:
1568 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
1569 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
1573 if (level & MONO_VERIFY_WARNING) {
1574 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
1575 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
1577 g_hash_table_destroy (dups);
1578 return list;
1581 static GSList*
1582 verify_moduleref_table (MonoImage *image, GSList *list, int level)
1584 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
1585 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
1586 guint32 cols [MONO_MODULEREF_SIZE];
1587 const char *p, *pf;
1588 guint32 found, i, j, value;
1589 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1591 for (i = 0; i < t->rows; ++i) {
1592 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
1593 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
1594 if (level & MONO_VERIFY_ERROR)
1595 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
1596 } else {
1597 if (level & MONO_VERIFY_ERROR) {
1598 if (!is_valid_filename (p))
1599 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
1600 else if (g_hash_table_lookup (dups, p)) {
1601 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
1602 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1603 found = 0;
1604 for (j = 0; j < tfile->rows; ++j) {
1605 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
1606 if ((pf = is_valid_string (image, value, TRUE)))
1607 if (strcmp (p, pf) == 0) {
1608 found = 1;
1609 break;
1612 if (!found)
1613 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
1618 g_hash_table_destroy (dups);
1619 return list;
1622 static GSList*
1623 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
1625 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
1626 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1627 const char *p;
1628 guint32 i;
1630 for (i = 0; i < t->rows; ++i) {
1631 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1632 if (level & MONO_VERIFY_ERROR) {
1633 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
1634 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
1635 } else {
1636 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
1637 /* FIXME: check it's a valid locals or method sig.*/
1641 return list;
1644 GSList*
1645 mono_image_verify_tables (MonoImage *image, int level)
1647 GSList *error_list = NULL;
1649 error_list = verify_assembly_table (image, error_list, level);
1651 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
1652 * AssemblyRefProcessor should be ignored,
1653 * though we may want to emit a warning, since it should not
1654 * be present in a PE file.
1656 error_list = verify_assemblyref_table (image, error_list, level);
1657 error_list = verify_class_layout_table (image, error_list, level);
1658 error_list = verify_constant_table (image, error_list, level);
1660 * cutom attribute, declsecurity
1662 error_list = verify_event_map_table (image, error_list, level);
1663 error_list = verify_event_table (image, error_list, level);
1664 error_list = verify_field_table (image, error_list, level);
1665 error_list = verify_file_table (image, error_list, level);
1666 error_list = verify_moduleref_table (image, error_list, level);
1667 error_list = verify_standalonesig_table (image, error_list, level);
1669 return g_slist_reverse (error_list);
1672 #define ADD_INVALID(list,msg) \
1673 do { \
1674 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1675 vinfo->status = MONO_VERIFY_ERROR; \
1676 vinfo->message = (msg); \
1677 (list) = g_slist_prepend ((list), vinfo); \
1678 /*G_BREAKPOINT ();*/ \
1679 goto invalid_cil; \
1680 } while (0)
1682 #define CHECK_STACK_UNDERFLOW(num) \
1683 do { \
1684 if (cur_stack < (num)) \
1685 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1686 } while (0)
1688 #define CHECK_STACK_OVERFLOW() \
1689 do { \
1690 if (cur_stack >= max_stack) \
1691 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1692 } while (0)
1695 static int
1696 in_any_block (MonoMethodHeader *header, guint offset)
1698 int i;
1699 MonoExceptionClause *clause;
1701 for (i = 0; i < header->num_clauses; ++i) {
1702 clause = &header->clauses [i];
1703 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1704 return 1;
1705 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1706 return 1;
1707 if (MONO_OFFSET_IN_FILTER (clause, offset))
1708 return 1;
1710 return 0;
1714 * in_any_exception_block:
1716 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1718 static gboolean
1719 in_any_exception_block (MonoMethodHeader *header, guint offset)
1721 int i;
1722 MonoExceptionClause *clause;
1724 for (i = 0; i < header->num_clauses; ++i) {
1725 clause = &header->clauses [i];
1726 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1727 return TRUE;
1728 if (MONO_OFFSET_IN_FILTER (clause, offset))
1729 return TRUE;
1731 return FALSE;
1735 * is_valid_branch_instruction:
1737 * Verify if it's valid to perform a branch from @offset to @target.
1738 * This should be used with br and brtrue/false.
1739 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1740 * The major diferent from other similiar functions is that branching into a
1741 * finally/fault block is invalid instead of just unverifiable.
1743 static int
1744 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1746 int i;
1747 MonoExceptionClause *clause;
1749 for (i = 0; i < header->num_clauses; ++i) {
1750 clause = &header->clauses [i];
1751 /*branching into a finally block is invalid*/
1752 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1753 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1754 MONO_OFFSET_IN_HANDLER (clause, target))
1755 return 2;
1757 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1758 return 1;
1759 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1760 return 1;
1761 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1762 return 1;
1764 return 0;
1768 * is_valid_cmp_branch_instruction:
1770 * Verify if it's valid to perform a branch from @offset to @target.
1771 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1772 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1774 * The major diferences from other similar functions are that most errors lead to invalid
1775 * code and only branching out of finally, filter or fault clauses is unverifiable.
1777 static int
1778 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1780 int i;
1781 MonoExceptionClause *clause;
1783 for (i = 0; i < header->num_clauses; ++i) {
1784 clause = &header->clauses [i];
1785 /*branching out of a handler or finally*/
1786 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1787 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1788 !MONO_OFFSET_IN_HANDLER (clause, target))
1789 return 1;
1791 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1792 return 2;
1793 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1794 return 2;
1795 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1796 return 2;
1798 return 0;
1802 * A leave can't escape a finally block
1804 static int
1805 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1807 int i;
1808 MonoExceptionClause *clause;
1810 for (i = 0; i < header->num_clauses; ++i) {
1811 clause = &header->clauses [i];
1812 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1813 return 0;
1814 if (MONO_OFFSET_IN_FILTER (clause, offset))
1815 return 0;
1817 return 1;
1821 * A rethrow can't happen outside of a catch handler.
1823 static int
1824 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1826 int i;
1827 MonoExceptionClause *clause;
1829 for (i = 0; i < header->num_clauses; ++i) {
1830 clause = &header->clauses [i];
1831 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1832 return 1;
1833 if (MONO_OFFSET_IN_FILTER (clause, offset))
1834 return 1;
1836 return 0;
1840 * An endfinally can't happen outside of a finally/fault handler.
1842 static int
1843 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1845 int i;
1846 MonoExceptionClause *clause;
1848 for (i = 0; i < header->num_clauses; ++i) {
1849 clause = &header->clauses [i];
1850 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1851 return 1;
1853 return 0;
1858 * An endfilter can only happens inside a filter clause.
1859 * In non-strict mode filter is allowed inside the handler clause too
1861 static MonoExceptionClause *
1862 is_correct_endfilter (VerifyContext *ctx, guint offset)
1864 int i;
1865 MonoExceptionClause *clause;
1867 for (i = 0; i < ctx->header->num_clauses; ++i) {
1868 clause = &ctx->header->clauses [i];
1869 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1870 continue;
1871 if (MONO_OFFSET_IN_FILTER (clause, offset))
1872 return clause;
1873 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1874 return clause;
1876 return NULL;
1881 * Non-strict endfilter can happens inside a try block or any handler block
1883 static int
1884 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1886 int i;
1887 MonoExceptionClause *clause;
1889 for (i = 0; i < ctx->header->num_clauses; ++i) {
1890 clause = &ctx->header->clauses [i];
1891 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1892 return 1;
1894 return 0;
1897 static gboolean
1898 is_valid_bool_arg (ILStackDesc *arg)
1900 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1901 return TRUE;
1904 switch (stack_slot_get_underlying_type (arg)) {
1905 case TYPE_I4:
1906 case TYPE_I8:
1907 case TYPE_NATIVE_INT:
1908 case TYPE_PTR:
1909 return TRUE;
1910 case TYPE_COMPLEX:
1911 g_assert (arg->type);
1912 switch (arg->type->type) {
1913 case MONO_TYPE_CLASS:
1914 case MONO_TYPE_STRING:
1915 case MONO_TYPE_OBJECT:
1916 case MONO_TYPE_SZARRAY:
1917 case MONO_TYPE_ARRAY:
1918 case MONO_TYPE_FNPTR:
1919 case MONO_TYPE_PTR:
1920 return TRUE;
1921 case MONO_TYPE_GENERICINST:
1922 /*We need to check if the container class
1923 * of the generic type is a valuetype, iow:
1924 * is it a "class Foo<T>" or a "struct Foo<T>"?
1926 return !arg->type->data.generic_class->container_class->valuetype;
1928 default:
1929 return FALSE;
1934 /*Type manipulation helper*/
1936 /*Returns the byref version of the supplied MonoType*/
1937 static MonoType*
1938 mono_type_get_type_byref (MonoType *type)
1940 if (type->byref)
1941 return type;
1942 return &mono_class_from_mono_type (type)->this_arg;
1946 /*Returns the byval version of the supplied MonoType*/
1947 static MonoType*
1948 mono_type_get_type_byval (MonoType *type)
1950 if (!type->byref)
1951 return type;
1952 return &mono_class_from_mono_type (type)->byval_arg;
1955 static MonoType*
1956 mono_type_from_stack_slot (ILStackDesc *slot)
1958 if (stack_slot_is_managed_pointer (slot))
1959 return mono_type_get_type_byref (slot->type);
1960 return slot->type;
1963 /*Stack manipulation code*/
1965 static void
1966 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1968 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1969 return;
1970 state->size = 0;
1971 state->flags |= IL_CODE_FLAG_STACK_INITED;
1972 if (!state->stack)
1973 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1976 static void
1977 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1979 to->size = from->size;
1980 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1983 static void
1984 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1986 to->stype = from->stype;
1987 to->type = from->type;
1988 to->method = from->method;
1991 static int
1992 check_underflow (VerifyContext *ctx, int size)
1994 if (ctx->eval.size < size) {
1995 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1996 return 0;
1998 return 1;
2001 static int
2002 check_overflow (VerifyContext *ctx)
2004 if (ctx->eval.size >= ctx->max_stack) {
2005 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
2006 return 0;
2008 return 1;
2011 /*This reject out PTR, FNPTR and TYPEDBYREF*/
2012 static gboolean
2013 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
2015 if (stack_slot_get_type (value) == TYPE_PTR) {
2016 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
2017 return 0;
2019 return 1;
2022 /*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
2023 static gboolean
2024 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
2026 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
2027 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
2028 return 0;
2030 return 1;
2034 static ILStackDesc *
2035 stack_push (VerifyContext *ctx)
2037 g_assert (ctx->eval.size < ctx->max_stack);
2038 return & ctx->eval.stack [ctx->eval.size++];
2041 static ILStackDesc *
2042 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
2044 ILStackDesc *top = stack_push (ctx);
2045 top->stype = stype;
2046 top->type = type;
2047 return top;
2050 static ILStackDesc *
2051 stack_pop (VerifyContext *ctx)
2053 ILStackDesc *ret;
2054 g_assert (ctx->eval.size > 0);
2055 ret = ctx->eval.stack + --ctx->eval.size;
2056 if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
2057 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
2058 return ret;
2061 /* This function allows to safely pop an unititialized this ptr from
2062 * the eval stack without marking the method as unverifiable.
2064 static ILStackDesc *
2065 stack_pop_safe (VerifyContext *ctx)
2067 g_assert (ctx->eval.size > 0);
2068 return ctx->eval.stack + --ctx->eval.size;
2071 static ILStackDesc *
2072 stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
2074 ILStackDesc *top = stack_push (ctx);
2075 copy_stack_value (top, value);
2076 return top;
2079 /* Returns the MonoType associated with the token, or NULL if it is invalid.
2081 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
2082 * */
2083 static MonoType*
2084 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
2086 MonoType *type;
2087 MonoClass *class;
2089 if (!(type = verifier_load_type (ctx, token, opcode)))
2090 return NULL;
2092 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
2093 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
2094 return NULL;
2097 if (type->type == MONO_TYPE_VOID) {
2098 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
2099 return NULL;
2102 if (type->type == MONO_TYPE_TYPEDBYREF)
2103 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
2105 if (!(class = mono_class_from_mono_type (type)))
2106 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
2108 if (class->generic_container && type->type != MONO_TYPE_GENERICINST)
2109 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));
2111 check_unverifiable_type (ctx, type);
2112 return type;
2116 /*operation result tables */
2118 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
2119 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2120 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2121 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2122 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2123 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2124 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2127 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
2128 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2129 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2130 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2131 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2132 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
2133 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2136 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
2137 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2138 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2139 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2140 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
2141 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
2142 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2145 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
2146 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2147 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2148 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2149 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2150 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2151 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2154 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
2155 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2156 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
2157 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2158 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2159 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2160 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2163 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
2164 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2165 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2166 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2167 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
2168 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
2169 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2172 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
2173 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
2174 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2175 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
2176 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
2177 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
2178 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
2181 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
2182 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2183 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2184 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
2185 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2186 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
2187 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2190 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
2191 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2192 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2193 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2194 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2195 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
2196 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2199 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
2200 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2201 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2202 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
2203 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2204 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2205 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
2208 #ifdef MONO_VERIFIER_DEBUG
2210 /*debug helpers */
2211 static void
2212 dump_stack_value (ILStackDesc *value)
2214 printf ("[(%x)(%x)", value->type->type, value->stype);
2216 if (stack_slot_is_this_pointer (value))
2217 printf ("[this] ");
2219 if (stack_slot_is_boxed_value (value))
2220 printf ("[boxed] ");
2222 if (stack_slot_is_null_literal (value))
2223 printf ("[null] ");
2225 if (stack_slot_is_managed_mutability_pointer (value))
2226 printf ("Controled Mutability MP: ");
2228 if (stack_slot_is_managed_pointer (value))
2229 printf ("Managed Pointer to: ");
2231 switch (stack_slot_get_underlying_type (value)) {
2232 case TYPE_INV:
2233 printf ("invalid type]");
2234 return;
2235 case TYPE_I4:
2236 printf ("int32]");
2237 return;
2238 case TYPE_I8:
2239 printf ("int64]");
2240 return;
2241 case TYPE_NATIVE_INT:
2242 printf ("native int]");
2243 return;
2244 case TYPE_R8:
2245 printf ("float64]");
2246 return;
2247 case TYPE_PTR:
2248 printf ("unmanaged pointer]");
2249 return;
2250 case TYPE_COMPLEX:
2251 switch (value->type->type) {
2252 case MONO_TYPE_CLASS:
2253 case MONO_TYPE_VALUETYPE:
2254 printf ("complex] (%s)", value->type->data.klass->name);
2255 return;
2256 case MONO_TYPE_STRING:
2257 printf ("complex] (string)");
2258 return;
2259 case MONO_TYPE_OBJECT:
2260 printf ("complex] (object)");
2261 return;
2262 case MONO_TYPE_SZARRAY:
2263 printf ("complex] (%s [])", value->type->data.klass->name);
2264 return;
2265 case MONO_TYPE_ARRAY:
2266 printf ("complex] (%s [%d %d %d])",
2267 value->type->data.array->eklass->name,
2268 value->type->data.array->rank,
2269 value->type->data.array->numsizes,
2270 value->type->data.array->numlobounds);
2271 return;
2272 case MONO_TYPE_GENERICINST:
2273 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
2274 return;
2275 case MONO_TYPE_VAR:
2276 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
2277 return;
2278 case MONO_TYPE_MVAR:
2279 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
2280 return;
2281 default: {
2282 //should be a boxed value
2283 char * name = mono_type_full_name (value->type);
2284 printf ("complex] %s", name);
2285 g_free (name);
2286 return;
2289 default:
2290 printf ("unknown stack %x type]\n", value->stype);
2291 g_assert_not_reached ();
2295 static void
2296 dump_stack_state (ILCodeDesc *state)
2298 int i;
2300 printf ("(%d) ", state->size);
2301 for (i = 0; i < state->size; ++i)
2302 dump_stack_value (state->stack + i);
2303 printf ("\n");
2305 #endif
2307 /*Returns TRUE if candidate array type can be assigned to target.
2308 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
2310 static gboolean
2311 is_array_type_compatible (MonoType *target, MonoType *candidate)
2313 MonoArrayType *left = target->data.array;
2314 MonoArrayType *right = candidate->data.array;
2316 g_assert (target->type == MONO_TYPE_ARRAY);
2317 g_assert (candidate->type == MONO_TYPE_ARRAY);
2319 if (left->rank != right->rank)
2320 return FALSE;
2322 return mono_class_is_assignable_from (left->eklass, right->eklass);
2325 static int
2326 get_stack_type (MonoType *type)
2328 int mask = 0;
2329 int type_kind = type->type;
2330 if (type->byref)
2331 mask = POINTER_MASK;
2332 /*TODO handle CMMP_MASK */
2334 handle_enum:
2335 switch (type_kind) {
2336 case MONO_TYPE_I1:
2337 case MONO_TYPE_U1:
2338 case MONO_TYPE_BOOLEAN:
2339 case MONO_TYPE_I2:
2340 case MONO_TYPE_U2:
2341 case MONO_TYPE_CHAR:
2342 case MONO_TYPE_I4:
2343 case MONO_TYPE_U4:
2344 return TYPE_I4 | mask;
2346 case MONO_TYPE_I:
2347 case MONO_TYPE_U:
2348 return TYPE_NATIVE_INT | mask;
2350 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
2351 case MONO_TYPE_FNPTR:
2352 case MONO_TYPE_PTR:
2353 case MONO_TYPE_TYPEDBYREF:
2354 return TYPE_PTR | mask;
2356 case MONO_TYPE_VAR:
2357 case MONO_TYPE_MVAR:
2359 case MONO_TYPE_CLASS:
2360 case MONO_TYPE_STRING:
2361 case MONO_TYPE_OBJECT:
2362 case MONO_TYPE_SZARRAY:
2363 case MONO_TYPE_ARRAY:
2364 return TYPE_COMPLEX | mask;
2366 case MONO_TYPE_GENERICINST:
2367 if (mono_type_is_enum_type (type)) {
2368 type = mono_type_get_underlying_type_any (type);
2369 type_kind = type->type;
2370 goto handle_enum;
2371 } else {
2372 return TYPE_COMPLEX | mask;
2375 case MONO_TYPE_I8:
2376 case MONO_TYPE_U8:
2377 return TYPE_I8 | mask;
2379 case MONO_TYPE_R4:
2380 case MONO_TYPE_R8:
2381 return TYPE_R8 | mask;
2383 case MONO_TYPE_VALUETYPE:
2384 if (mono_type_is_enum_type (type)) {
2385 type = mono_type_get_underlying_type_any (type);
2386 type_kind = type->type;
2387 goto handle_enum;
2388 } else {
2389 return TYPE_COMPLEX | mask;
2392 default:
2393 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
2394 g_assert_not_reached ();
2395 return 0;
2399 /* convert MonoType to ILStackDesc format (stype) */
2400 static gboolean
2401 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
2403 int mask = 0;
2404 int type_kind = type->type;
2406 if (type->byref || take_addr)
2407 mask = POINTER_MASK;
2408 /* TODO handle CMMP_MASK */
2410 handle_enum:
2411 stack->type = type;
2413 switch (type_kind) {
2414 case MONO_TYPE_I1:
2415 case MONO_TYPE_U1:
2416 case MONO_TYPE_BOOLEAN:
2417 case MONO_TYPE_I2:
2418 case MONO_TYPE_U2:
2419 case MONO_TYPE_CHAR:
2420 case MONO_TYPE_I4:
2421 case MONO_TYPE_U4:
2422 stack->stype = TYPE_I4 | mask;
2423 break;
2424 case MONO_TYPE_I:
2425 case MONO_TYPE_U:
2426 stack->stype = TYPE_NATIVE_INT | mask;
2427 break;
2429 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
2430 case MONO_TYPE_FNPTR:
2431 case MONO_TYPE_PTR:
2432 case MONO_TYPE_TYPEDBYREF:
2433 stack->stype = TYPE_PTR | mask;
2434 break;
2436 case MONO_TYPE_CLASS:
2437 case MONO_TYPE_STRING:
2438 case MONO_TYPE_OBJECT:
2439 case MONO_TYPE_SZARRAY:
2440 case MONO_TYPE_ARRAY:
2442 case MONO_TYPE_VAR:
2443 case MONO_TYPE_MVAR:
2444 stack->stype = TYPE_COMPLEX | mask;
2445 break;
2447 case MONO_TYPE_GENERICINST:
2448 if (mono_type_is_enum_type (type)) {
2449 type = mono_type_get_underlying_type_any (type);
2450 type_kind = type->type;
2451 goto handle_enum;
2452 } else {
2453 stack->stype = TYPE_COMPLEX | mask;
2454 break;
2457 case MONO_TYPE_I8:
2458 case MONO_TYPE_U8:
2459 stack->stype = TYPE_I8 | mask;
2460 break;
2461 case MONO_TYPE_R4:
2462 case MONO_TYPE_R8:
2463 stack->stype = TYPE_R8 | mask;
2464 break;
2465 case MONO_TYPE_VALUETYPE:
2466 if (mono_type_is_enum_type (type)) {
2467 type = mono_type_get_underlying_type_any (type);
2468 type_kind = type->type;
2469 goto handle_enum;
2470 } else {
2471 stack->stype = TYPE_COMPLEX | mask;
2472 break;
2474 default:
2475 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
2476 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
2477 return FALSE;
2479 return TRUE;
2483 * init_stack_with_value_at_exception_boundary:
2485 * Initialize the stack and push a given type.
2486 * The instruction is marked as been on the exception boundary.
2488 static void
2489 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
2491 MonoError error;
2492 MonoType *type = mono_class_inflate_generic_type_checked (&klass->byval_arg, ctx->generic_context, &error);
2494 if (!mono_error_ok (&error)) {
2495 char *name = mono_type_get_full_name (klass);
2496 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
2497 g_free (name);
2498 mono_error_cleanup (&error);
2499 return;
2502 stack_init (ctx, code);
2503 set_stack_value (ctx, code->stack, type, FALSE);
2504 ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
2505 code->size = 1;
2506 code->flags |= IL_CODE_FLAG_WAS_TARGET;
2507 if (mono_type_is_generic_argument (type))
2508 code->stack->stype |= BOXED_MASK;
2511 /*Verify if type 'candidate' can be stored in type 'target'.
2513 * If strict, check for the underlying type and not the verification stack types
2515 static gboolean
2516 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
2518 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
2519 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
2521 MonoType *original_candidate = candidate;
2522 VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
2524 /*only one is byref */
2525 if (candidate->byref ^ target->byref) {
2526 /* converting from native int to byref*/
2527 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
2528 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2529 return TRUE;
2531 return FALSE;
2533 strict |= target->byref;
2534 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
2535 candidate = mono_type_get_underlying_type_any (candidate);
2537 handle_enum:
2538 switch (target->type) {
2539 case MONO_TYPE_VOID:
2540 return candidate->type == MONO_TYPE_VOID;
2541 case MONO_TYPE_I1:
2542 case MONO_TYPE_U1:
2543 case MONO_TYPE_BOOLEAN:
2544 if (strict)
2545 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
2546 case MONO_TYPE_I2:
2547 case MONO_TYPE_U2:
2548 case MONO_TYPE_CHAR:
2549 if (strict)
2550 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2551 case MONO_TYPE_I4:
2552 case MONO_TYPE_U4: {
2553 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2554 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2555 if (strict)
2556 return is_native_int || is_int4;
2557 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2560 case MONO_TYPE_I8:
2561 case MONO_TYPE_U8:
2562 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2564 case MONO_TYPE_R4:
2565 case MONO_TYPE_R8:
2566 if (strict)
2567 return candidate->type == target->type;
2568 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2570 case MONO_TYPE_I:
2571 case MONO_TYPE_U: {
2572 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2573 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2574 if (strict)
2575 return is_native_int || is_int4;
2576 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2579 case MONO_TYPE_PTR:
2580 if (candidate->type != MONO_TYPE_PTR)
2581 return FALSE;
2582 /* check the underlying type */
2583 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2585 case MONO_TYPE_FNPTR: {
2586 MonoMethodSignature *left, *right;
2587 if (candidate->type != MONO_TYPE_FNPTR)
2588 return FALSE;
2590 left = mono_type_get_signature (target);
2591 right = mono_type_get_signature (candidate);
2592 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2595 case MONO_TYPE_GENERICINST: {
2596 MonoClass *target_klass;
2597 MonoClass *candidate_klass;
2598 if (mono_type_is_enum_type (target)) {
2599 target = mono_type_get_underlying_type_any (target);
2600 goto handle_enum;
2602 target_klass = mono_class_from_mono_type (target);
2603 candidate_klass = mono_class_from_mono_type (candidate);
2604 if (mono_class_is_nullable (target_klass)) {
2605 if (!mono_class_is_nullable (candidate_klass))
2606 return FALSE;
2607 return target_klass == candidate_klass;
2610 return mono_class_is_assignable_from (target_klass, candidate_klass);
2613 case MONO_TYPE_STRING:
2614 return candidate->type == MONO_TYPE_STRING;
2616 case MONO_TYPE_CLASS:
2618 * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
2619 * to take boxing status into account.
2621 if (mono_type_is_generic_argument (original_candidate))
2622 return FALSE;
2623 /* If candidate is an enum it should return true for System.Enum and supertypes.
2624 * That's why here we use the original type and not the underlying type.
2626 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2628 case MONO_TYPE_OBJECT:
2629 return MONO_TYPE_IS_REFERENCE (candidate);
2631 case MONO_TYPE_SZARRAY: {
2632 MonoClass *left;
2633 MonoClass *right;
2634 if (candidate->type != MONO_TYPE_SZARRAY)
2635 return FALSE;
2637 left = mono_class_from_mono_type (target)->element_class;
2638 right = mono_class_from_mono_type (candidate)->element_class;
2639 return mono_class_is_assignable_from (left, right);
2642 case MONO_TYPE_ARRAY:
2643 if (candidate->type != MONO_TYPE_ARRAY)
2644 return FALSE;
2645 return is_array_type_compatible (target, candidate);
2647 case MONO_TYPE_TYPEDBYREF:
2648 return candidate->type == MONO_TYPE_TYPEDBYREF;
2650 case MONO_TYPE_VALUETYPE: {
2651 MonoClass *target_klass = mono_class_from_mono_type (target);
2652 MonoClass *candidate_klass = mono_class_from_mono_type (candidate);
2654 if (target_klass == candidate_klass)
2655 return TRUE;
2656 if (mono_type_is_enum_type (target)) {
2657 target = mono_type_get_underlying_type_any (target);
2658 goto handle_enum;
2660 return FALSE;
2663 case MONO_TYPE_VAR:
2664 if (candidate->type != MONO_TYPE_VAR)
2665 return FALSE;
2666 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2668 case MONO_TYPE_MVAR:
2669 if (candidate->type != MONO_TYPE_MVAR)
2670 return FALSE;
2671 return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
2673 default:
2674 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2675 g_assert_not_reached ();
2676 return FALSE;
2678 return 1;
2679 #undef IS_ONE_OF3
2680 #undef IS_ONE_OF2
2683 static gboolean
2684 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2686 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2690 * Returns the generic param bound to the context been verified.
2693 static MonoGenericParam*
2694 get_generic_param (VerifyContext *ctx, MonoType *param)
2696 guint16 param_num = mono_type_get_generic_param_num (param);
2697 if (param->type == MONO_TYPE_VAR) {
2698 if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
2699 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
2700 return NULL;
2702 return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
2705 /*param must be a MVAR */
2706 if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
2707 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
2708 return NULL;
2710 return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
2714 * is_compatible_boxed_valuetype:
2716 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2718 * @type The source type. It it tested to be of the proper type.
2719 * @candidate type of the boxed valuetype.
2720 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2721 * @strict if TRUE candidate must be boxed compatible to the target type
2724 static gboolean
2725 is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
2727 if (!stack_slot_is_boxed_value (stack))
2728 return FALSE;
2729 if (type->byref || candidate->byref)
2730 return FALSE;
2732 if (mono_type_is_generic_argument (candidate)) {
2733 MonoGenericParam *param = get_generic_param (ctx, candidate);
2734 MonoClass **class;
2735 for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
2736 if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (& (*class)->byval_arg), FALSE))
2737 return TRUE;
2741 if (mono_type_is_generic_argument (type))
2742 return FALSE;
2744 if (!strict)
2745 return TRUE;
2747 return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
2750 static int
2751 verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
2753 MonoType *candidate = mono_type_from_stack_slot (stack);
2754 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2755 return TRUE;
2757 if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
2758 return TRUE;
2760 if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
2761 return FALSE;
2763 if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
2764 return FALSE;
2766 if (drop_byref)
2767 return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
2769 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2772 static int
2773 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2775 return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
2778 static gboolean
2779 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2781 if (candidate->byref ^ target->byref)
2782 return FALSE;
2784 switch (target->type) {
2785 case MONO_TYPE_VOID:
2786 case MONO_TYPE_I1:
2787 case MONO_TYPE_U1:
2788 case MONO_TYPE_BOOLEAN:
2789 case MONO_TYPE_I2:
2790 case MONO_TYPE_U2:
2791 case MONO_TYPE_CHAR:
2792 case MONO_TYPE_I4:
2793 case MONO_TYPE_U4:
2794 case MONO_TYPE_I8:
2795 case MONO_TYPE_U8:
2796 case MONO_TYPE_R4:
2797 case MONO_TYPE_R8:
2798 case MONO_TYPE_I:
2799 case MONO_TYPE_U:
2800 case MONO_TYPE_STRING:
2801 case MONO_TYPE_TYPEDBYREF:
2802 return candidate->type == target->type;
2804 case MONO_TYPE_PTR:
2805 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2807 case MONO_TYPE_FNPTR:
2808 if (candidate->type != MONO_TYPE_FNPTR)
2809 return FALSE;
2810 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
2812 case MONO_TYPE_GENERICINST: {
2813 MonoClass *target_klass;
2814 MonoClass *candidate_klass;
2815 target_klass = mono_class_from_mono_type (target);
2816 candidate_klass = mono_class_from_mono_type (candidate);
2817 /*FIXME handle nullables and enum*/
2818 return mono_class_is_assignable_from (target_klass, candidate_klass);
2820 case MONO_TYPE_OBJECT:
2821 return MONO_TYPE_IS_REFERENCE (candidate);
2823 case MONO_TYPE_CLASS:
2824 return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
2826 case MONO_TYPE_SZARRAY:
2827 if (candidate->type != MONO_TYPE_SZARRAY)
2828 return FALSE;
2829 return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
2831 case MONO_TYPE_ARRAY:
2832 if (candidate->type != MONO_TYPE_ARRAY)
2833 return FALSE;
2834 return is_array_type_compatible (target, candidate);
2836 case MONO_TYPE_VALUETYPE:
2837 /*FIXME handle nullables and enum*/
2838 return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
2840 case MONO_TYPE_VAR:
2841 return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2842 return FALSE;
2844 case MONO_TYPE_MVAR:
2845 return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
2846 return FALSE;
2848 default:
2849 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2850 g_assert_not_reached ();
2851 return FALSE;
2855 static gboolean
2856 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2858 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2859 return TRUE;
2861 return mono_delegate_type_equal (method, delegate);
2864 static gboolean
2865 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2867 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2868 return TRUE;
2870 return mono_delegate_type_equal (delegate, method);
2874 * mono_delegate_signature_equal:
2876 * Compare two signatures in the way expected by delegates.
2878 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2880 * FIXME can this function be eliminated and proper metadata functionality be used?
2882 static gboolean
2883 mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
2885 int i;
2886 int method_offset = is_static_ldftn ? 1 : 0;
2888 if (delegate_sig->param_count + method_offset != method_sig->param_count)
2889 return FALSE;
2891 if (delegate_sig->call_convention != method_sig->call_convention)
2892 return FALSE;
2894 for (i = 0; i < delegate_sig->param_count; i++) {
2895 MonoType *p1 = delegate_sig->params [i];
2896 MonoType *p2 = method_sig->params [i + method_offset];
2898 if (!mono_delegate_param_equal (p1, p2))
2899 return FALSE;
2902 if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
2903 return FALSE;
2905 return TRUE;
2909 * verify_ldftn_delegate:
2911 * Verify properties of ldftn based delegates.
2913 static void
2914 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2916 MonoMethod *method = funptr->method;
2918 /*ldftn non-final virtuals only allowed if method is not static,
2919 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2920 * This rules doesn't apply if the object on stack is a boxed valuetype.
2922 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2923 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2924 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2925 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2927 /*current method must not be static*/
2928 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2929 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2931 /*value is the this pointer, loaded using ldarg.0 */
2932 if (!stack_slot_is_this_pointer (value))
2933 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));
2935 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2940 * verify_delegate_compatibility:
2942 * Verify delegate creation sequence.
2945 static void
2946 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2948 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2949 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2951 MonoMethod *invoke, *method;
2952 const guint8 *ip = ctx->header->code;
2953 guint32 ip_offset = ctx->ip_offset;
2954 gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
2956 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2957 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2958 return;
2961 invoke = mono_get_delegate_invoke (delegate);
2962 method = funptr->method;
2964 if (!method || !mono_method_signature (method)) {
2965 char *name = mono_type_get_full_name (delegate);
2966 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
2967 g_free (name);
2968 return;
2971 if (!invoke || !mono_method_signature (invoke)) {
2972 char *name = mono_type_get_full_name (delegate);
2973 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
2974 g_free (name);
2975 return;
2978 is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
2980 if (is_static_ldftn)
2981 is_first_arg_bound = mono_method_signature (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
2983 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
2984 char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
2985 char *invoke_sig = mono_signature_get_desc (mono_method_signature (invoke), FALSE);
2986 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));
2987 g_free (fun_sig);
2988 g_free (invoke_sig);
2992 * Delegate code sequences:
2993 * [-6] ldftn token
2994 * newobj ...
2997 * [-7] dup
2998 * [-6] ldvirtftn token
2999 * newobj ...
3001 * ldftn sequence:*/
3002 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
3003 verify_ldftn_delegate (ctx, delegate, value, funptr);
3004 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
3005 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
3006 }else {
3007 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
3009 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
3011 //general tests
3012 if (is_first_arg_bound) {
3013 if (mono_method_signature (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature (method)->params [0], value, FALSE, TRUE))
3014 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
3015 } else {
3016 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
3017 if (!stack_slot_is_null_literal (value) && !is_first_arg_bound)
3018 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
3019 } else {
3020 if (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
3021 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
3025 if (stack_slot_get_type (value) != TYPE_COMPLEX)
3026 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
3028 #undef IS_VALID_OPCODE
3029 #undef IS_LOAD_FUN_PTR
3032 /* implement the opcode checks*/
3033 static void
3034 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
3036 ILStackDesc *top;
3038 if (arg >= ctx->max_args) {
3039 if (take_addr)
3040 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
3041 else {
3042 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
3043 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
3044 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3046 } else if (check_overflow (ctx)) {
3047 /*We must let the value be pushed, otherwise we would get an underflow error*/
3048 check_unverifiable_type (ctx, ctx->params [arg]);
3049 if (ctx->params [arg]->byref && take_addr)
3050 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
3051 top = stack_push (ctx);
3052 if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
3053 return;
3055 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
3056 if (take_addr)
3057 ctx->has_this_store = TRUE;
3058 else
3059 top->stype |= THIS_POINTER_MASK;
3060 if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
3061 top->stype |= UNINIT_THIS_MASK;
3066 static void
3067 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
3069 if (arg >= ctx->num_locals) {
3070 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
3071 } else if (check_overflow (ctx)) {
3072 /*We must let the value be pushed, otherwise we would get an underflow error*/
3073 check_unverifiable_type (ctx, ctx->locals [arg]);
3074 if (ctx->locals [arg]->byref && take_addr)
3075 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
3077 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
3081 static void
3082 store_arg (VerifyContext *ctx, guint32 arg)
3084 ILStackDesc *value;
3086 if (arg >= ctx->max_args) {
3087 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
3088 if (check_underflow (ctx, 1))
3089 stack_pop (ctx);
3090 return;
3093 if (check_underflow (ctx, 1)) {
3094 value = stack_pop (ctx);
3095 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
3096 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3099 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
3100 ctx->has_this_store = 1;
3103 static void
3104 store_local (VerifyContext *ctx, guint32 arg)
3106 ILStackDesc *value;
3107 if (arg >= ctx->num_locals) {
3108 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
3109 return;
3112 /*TODO verify definite assigment */
3113 if (check_underflow (ctx, 1)) {
3114 value = stack_pop(ctx);
3115 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
3116 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
3117 stack_slot_get_name (value),
3118 mono_type_get_stack_name (ctx->locals [arg]),
3119 ctx->ip_offset));
3124 /*FIXME add and sub needs special care here*/
3125 static void
3126 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
3128 ILStackDesc *a, *b, *top;
3129 int idxa, idxb, complexMerge = 0;
3130 unsigned char res;
3132 if (!check_underflow (ctx, 2))
3133 return;
3134 b = stack_pop (ctx);
3135 a = stack_pop (ctx);
3137 idxa = stack_slot_get_underlying_type (a);
3138 if (stack_slot_is_managed_pointer (a)) {
3139 idxa = TYPE_PTR;
3140 complexMerge = 1;
3143 idxb = stack_slot_get_underlying_type (b);
3144 if (stack_slot_is_managed_pointer (b)) {
3145 idxb = TYPE_PTR;
3146 complexMerge = 2;
3149 --idxa;
3150 --idxb;
3151 res = table [idxa][idxb];
3153 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
3154 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3156 top = stack_push (ctx);
3157 if (res == TYPE_INV) {
3158 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)));
3159 copy_stack_value (top, a);
3160 return;
3163 if (res & NON_VERIFIABLE_RESULT) {
3164 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)));
3166 res = res & ~NON_VERIFIABLE_RESULT;
3169 if (complexMerge && res == TYPE_PTR) {
3170 if (complexMerge == 1)
3171 copy_stack_value (top, a);
3172 else if (complexMerge == 2)
3173 copy_stack_value (top, b);
3175 * There is no need to merge the type of two pointers.
3176 * The only valid operation is subtraction, that returns a native
3177 * int as result and can be used with any 2 pointer kinds.
3178 * This is valid acording to Patition III 1.1.4
3180 } else
3181 top->stype = res;
3186 static void
3187 do_boolean_branch_op (VerifyContext *ctx, int delta)
3189 int target = ctx->ip_offset + delta;
3190 ILStackDesc *top;
3192 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3194 if (target < 0 || target >= ctx->code_size) {
3195 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
3196 return;
3199 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3200 case 1:
3201 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3202 break;
3203 case 2:
3204 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3205 return;
3208 ctx->target = target;
3210 if (!check_underflow (ctx, 1))
3211 return;
3213 top = stack_pop (ctx);
3214 if (!is_valid_bool_arg (top))
3215 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));
3217 check_unmanaged_pointer (ctx, top);
3220 static gboolean
3221 stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
3223 return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
3226 static void
3227 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
3229 ILStackDesc *a, *b;
3230 int idxa, idxb;
3231 unsigned char res;
3232 int target = ctx->ip_offset + delta;
3234 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
3236 if (target < 0 || target >= ctx->code_size) {
3237 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3238 return;
3241 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3242 case 1: /*FIXME use constants and not magic numbers.*/
3243 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3244 break;
3245 case 2:
3246 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3247 return;
3250 ctx->target = target;
3252 if (!check_underflow (ctx, 2))
3253 return;
3255 b = stack_pop (ctx);
3256 a = stack_pop (ctx);
3258 idxa = stack_slot_get_underlying_type (a);
3259 if (stack_slot_is_managed_pointer (a))
3260 idxa = TYPE_PTR;
3262 idxb = stack_slot_get_underlying_type (b);
3263 if (stack_slot_is_managed_pointer (b))
3264 idxb = TYPE_PTR;
3266 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3267 res = TYPE_INV;
3268 } else {
3269 --idxa;
3270 --idxb;
3271 res = table [idxa][idxb];
3274 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
3275 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
3277 if (res == TYPE_INV) {
3278 CODE_NOT_VERIFIABLE (ctx,
3279 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));
3280 } else if (res & NON_VERIFIABLE_RESULT) {
3281 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));
3282 res = res & ~NON_VERIFIABLE_RESULT;
3286 static void
3287 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
3289 ILStackDesc *a, *b;
3290 int idxa, idxb;
3291 unsigned char res;
3293 if (!check_underflow (ctx, 2))
3294 return;
3295 b = stack_pop (ctx);
3296 a = stack_pop (ctx);
3298 if (opcode == CEE_CGT_UN) {
3299 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
3300 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3301 return;
3305 idxa = stack_slot_get_underlying_type (a);
3306 if (stack_slot_is_managed_pointer (a))
3307 idxa = TYPE_PTR;
3309 idxb = stack_slot_get_underlying_type (b);
3310 if (stack_slot_is_managed_pointer (b))
3311 idxb = TYPE_PTR;
3313 if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
3314 res = TYPE_INV;
3315 } else {
3316 --idxa;
3317 --idxb;
3318 res = table [idxa][idxb];
3321 if(res == TYPE_INV) {
3322 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));
3323 } else if (res & NON_VERIFIABLE_RESULT) {
3324 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));
3325 res = res & ~NON_VERIFIABLE_RESULT;
3327 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3330 static void
3331 do_ret (VerifyContext *ctx)
3333 MonoType *ret = ctx->signature->ret;
3334 VERIFIER_DEBUG ( printf ("checking ret\n"); );
3335 if (ret->type != MONO_TYPE_VOID) {
3336 ILStackDesc *top;
3337 if (!check_underflow (ctx, 1))
3338 return;
3340 top = stack_pop(ctx);
3342 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
3343 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
3344 return;
3347 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
3348 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
3351 if (ctx->eval.size > 0) {
3352 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
3354 if (in_any_block (ctx->header, ctx->ip_offset))
3355 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
3359 * 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.
3360 * This is illegal but mono_get_method_full decoded it.
3361 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
3363 static void
3364 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
3366 int param_count, i;
3367 MonoMethodSignature *sig;
3368 ILStackDesc *value;
3369 MonoMethod *method;
3370 gboolean virt_check_this = FALSE;
3371 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
3373 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
3374 return;
3376 if (virtual) {
3377 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
3379 if (method->klass->valuetype) // && !constrained ???
3380 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
3382 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
3383 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
3385 } else {
3386 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
3387 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
3389 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL)) {
3390 virt_check_this = TRUE;
3391 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
3395 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
3396 sig = mono_method_get_signature (method, ctx->image, method_token);
3398 if (!sig) {
3399 char *name = mono_type_get_full_name (method->klass);
3400 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x", name, method->name, ctx->ip_offset));
3401 g_free (name);
3402 return;
3405 param_count = sig->param_count + sig->hasthis;
3406 if (!check_underflow (ctx, param_count))
3407 return;
3409 for (i = sig->param_count - 1; i >= 0; --i) {
3410 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
3411 value = stack_pop (ctx);
3412 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
3413 char *stack_name = stack_slot_full_name (value);
3414 char *sig_name = mono_type_full_name (sig->params [i]);
3415 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));
3416 g_free (stack_name);
3417 g_free (sig_name);
3420 if (stack_slot_is_managed_mutability_pointer (value))
3421 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));
3423 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
3424 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));
3425 return;
3429 if (sig->hasthis) {
3430 MonoType *type = &method->klass->byval_arg;
3431 ILStackDesc copy;
3433 if (mono_method_is_constructor (method) && !method->klass->valuetype) {
3434 if (!mono_method_is_constructor (ctx->method))
3435 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
3436 if (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
3437 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));
3439 ctx->super_ctor_called = TRUE;
3440 value = stack_pop_safe (ctx);
3441 if ((value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
3442 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
3443 } else {
3444 value = stack_pop (ctx);
3447 copy_stack_value (&copy, value);
3448 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
3449 //Other parts of the code suffer from the same issue of
3450 copy.type = mono_type_get_type_byval (copy.type);
3451 copy.stype &= ~POINTER_MASK;
3453 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
3454 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a non-final virtual method from an objet diferent thant the this pointer at 0x%04x", ctx->ip_offset));
3456 if (constrained && virtual) {
3457 if (!stack_slot_is_managed_pointer (value))
3458 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
3459 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
3460 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
3461 copy.stype |= BOXED_MASK;
3462 } else {
3463 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
3464 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));
3466 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
3467 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
3469 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
3470 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
3472 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
3473 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));
3475 if (!verify_stack_type_compatibility (ctx, type, &copy))
3476 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
3478 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (value->type))) {
3479 char *name = mono_method_full_name (method, TRUE);
3480 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3481 g_free (name);
3484 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
3485 char *name = mono_method_full_name (method, TRUE);
3486 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3487 g_free (name);
3490 if (sig->ret->type != MONO_TYPE_VOID) {
3491 if (check_overflow (ctx)) {
3492 value = stack_push (ctx);
3493 set_stack_value (ctx, value, sig->ret, FALSE);
3494 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
3495 ctx->prefix_set &= ~PREFIX_READONLY;
3496 value->stype |= CMMP_MASK;
3501 if ((ctx->prefix_set & PREFIX_TAIL)) {
3502 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
3503 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
3504 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
3505 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
3510 static void
3511 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
3513 MonoClassField *field;
3514 MonoClass *klass;
3515 if (!check_overflow (ctx))
3516 return;
3517 if (!take_addr)
3518 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3520 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
3521 return;
3523 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3524 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
3525 return;
3527 /*taking the address of initonly field only works from the static constructor */
3528 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3529 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
3530 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3532 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3533 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3535 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3538 static void
3539 do_store_static_field (VerifyContext *ctx, int token) {
3540 MonoClassField *field;
3541 MonoClass *klass;
3542 ILStackDesc *value;
3543 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
3545 if (!check_underflow (ctx, 1))
3546 return;
3548 value = stack_pop (ctx);
3550 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
3551 return;
3553 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
3554 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
3555 return;
3558 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3559 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
3560 return;
3563 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3564 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3566 if (!verify_stack_type_compatibility (ctx, field->type, value))
3567 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));
3570 static gboolean
3571 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
3573 MonoClassField *field;
3574 MonoClass *klass;
3575 gboolean is_pointer;
3577 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
3578 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
3579 return FALSE;
3581 *ret_field = field;
3582 //the value on stack is going to be used as a pointer
3583 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
3585 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
3586 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
3587 return FALSE;
3589 g_assert (obj->type);
3591 /*The value on the stack must be a subclass of the defining type of the field*/
3592 /* we need to check if we can load the field from the stack value*/
3593 if (is_pointer) {
3594 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
3595 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
3597 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
3598 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3599 } else {
3600 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
3601 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));
3603 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
3604 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
3605 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));
3607 if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE))
3608 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
3610 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (obj->type)))
3611 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
3614 check_unmanaged_pointer (ctx, obj);
3615 return TRUE;
3618 static void
3619 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
3621 ILStackDesc *obj;
3622 MonoClassField *field;
3624 if (!take_addr)
3625 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3627 if (!check_underflow (ctx, 1))
3628 return;
3629 obj = stack_pop_safe (ctx);
3631 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
3632 return;
3634 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
3635 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
3637 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
3638 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
3639 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
3641 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
3644 static void
3645 do_store_field (VerifyContext *ctx, int token)
3647 ILStackDesc *value, *obj;
3648 MonoClassField *field;
3649 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3651 if (!check_underflow (ctx, 2))
3652 return;
3654 value = stack_pop (ctx);
3655 obj = stack_pop_safe (ctx);
3657 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
3658 return;
3660 if (!verify_stack_type_compatibility (ctx, field->type, value))
3661 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3664 /*TODO proper handle for Nullable<T>*/
3665 static void
3666 do_box_value (VerifyContext *ctx, int klass_token)
3668 ILStackDesc *value;
3669 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
3670 MonoClass *klass;
3672 if (!type)
3673 return;
3675 if (!check_underflow (ctx, 1))
3676 return;
3678 value = stack_pop (ctx);
3679 /*box is a nop for reference types*/
3681 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
3682 stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
3683 return;
3687 if (!verify_stack_type_compatibility (ctx, type, value))
3688 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
3690 klass = mono_class_from_mono_type (type);
3691 if (mono_class_is_nullable (klass))
3692 type = &mono_class_get_nullable_param (klass)->byval_arg;
3693 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
3696 static void
3697 do_unbox_value (VerifyContext *ctx, int klass_token)
3699 ILStackDesc *value;
3700 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
3702 if (!type)
3703 return;
3705 if (!check_underflow (ctx, 1))
3706 return;
3708 if (!mono_class_from_mono_type (type)->valuetype)
3709 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
3711 value = stack_pop (ctx);
3713 /*Value should be: a boxed valuetype or a reference type*/
3714 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3715 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3716 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));
3718 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
3719 value->stype |= CMMP_MASK;
3722 static void
3723 do_unbox_any (VerifyContext *ctx, int klass_token)
3725 ILStackDesc *value;
3726 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3728 if (!type)
3729 return;
3731 if (!check_underflow (ctx, 1))
3732 return;
3734 value = stack_pop (ctx);
3736 /*Value should be: a boxed valuetype or a reference type*/
3737 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3738 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3739 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));
3741 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3744 static void
3745 do_unary_math_op (VerifyContext *ctx, int op)
3747 ILStackDesc *value;
3748 if (!check_underflow (ctx, 1))
3749 return;
3750 value = stack_pop (ctx);
3751 switch (stack_slot_get_type (value)) {
3752 case TYPE_I4:
3753 case TYPE_I8:
3754 case TYPE_NATIVE_INT:
3755 break;
3756 case TYPE_R8:
3757 if (op == CEE_NEG)
3758 break;
3759 case TYPE_COMPLEX: /*only enums are ok*/
3760 if (mono_type_is_enum_type (value->type))
3761 break;
3762 default:
3763 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3765 stack_push_stack_val (ctx, value);
3768 static void
3769 do_conversion (VerifyContext *ctx, int kind)
3771 ILStackDesc *value;
3772 if (!check_underflow (ctx, 1))
3773 return;
3774 value = stack_pop (ctx);
3776 switch (stack_slot_get_type (value)) {
3777 case TYPE_I4:
3778 case TYPE_I8:
3779 case TYPE_NATIVE_INT:
3780 case TYPE_R8:
3781 break;
3782 default:
3783 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));
3786 switch (kind) {
3787 case TYPE_I4:
3788 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3789 break;
3790 case TYPE_I8:
3791 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3792 break;
3793 case TYPE_R8:
3794 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3795 break;
3796 case TYPE_NATIVE_INT:
3797 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3798 break;
3799 default:
3800 g_error ("unknown type %02x in conversion", kind);
3805 static void
3806 do_load_token (VerifyContext *ctx, int token)
3808 gpointer handle;
3809 MonoClass *handle_class;
3810 if (!check_overflow (ctx))
3811 return;
3812 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3813 if (!handle) {
3814 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3815 return;
3817 if (handle_class == mono_defaults.typehandle_class) {
3818 mono_type_is_valid_in_context (ctx, (MonoType*)handle);
3819 } else if (handle_class == mono_defaults.methodhandle_class) {
3820 mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
3821 } else if (handle_class == mono_defaults.fieldhandle_class) {
3822 mono_type_is_valid_in_context (ctx, &((MonoClassField*)handle)->parent->byval_arg);
3823 } else {
3824 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3826 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3829 static void
3830 do_ldobj_value (VerifyContext *ctx, int token)
3832 ILStackDesc *value;
3833 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3834 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3836 if (!type)
3837 return;
3839 if (!check_underflow (ctx, 1))
3840 return;
3842 value = stack_pop (ctx);
3843 if (!stack_slot_is_managed_pointer (value)
3844 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3845 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3846 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3847 return;
3850 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3851 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3853 /*We have a byval on the stack, but the comparison must be strict. */
3854 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3855 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3857 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3860 static void
3861 do_stobj (VerifyContext *ctx, int token)
3863 ILStackDesc *dest, *src;
3864 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3865 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3867 if (!type)
3868 return;
3870 if (!check_underflow (ctx, 2))
3871 return;
3873 src = stack_pop (ctx);
3874 dest = stack_pop (ctx);
3876 if (stack_slot_is_managed_mutability_pointer (dest))
3877 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3879 if (!stack_slot_is_managed_pointer (dest))
3880 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3882 if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
3883 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));
3885 if (!verify_stack_type_compatibility (ctx, type, src))
3886 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
3888 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3889 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3892 static void
3893 do_cpobj (VerifyContext *ctx, int token)
3895 ILStackDesc *dest, *src;
3896 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3897 if (!type)
3898 return;
3900 if (!check_underflow (ctx, 2))
3901 return;
3903 src = stack_pop (ctx);
3904 dest = stack_pop (ctx);
3906 if (!stack_slot_is_managed_pointer (src))
3907 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3909 if (!stack_slot_is_managed_pointer (dest))
3910 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3912 if (stack_slot_is_managed_mutability_pointer (dest))
3913 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3915 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3916 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3918 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3919 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3922 static void
3923 do_initobj (VerifyContext *ctx, int token)
3925 ILStackDesc *obj;
3926 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3927 if (!type)
3928 return;
3930 if (!check_underflow (ctx, 1))
3931 return;
3933 obj = stack_pop (ctx);
3935 if (!stack_slot_is_managed_pointer (obj))
3936 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3938 if (stack_slot_is_managed_mutability_pointer (obj))
3939 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3941 stack = mono_type_get_type_byval (obj->type);
3942 if (MONO_TYPE_IS_REFERENCE (stack)) {
3943 if (!verify_type_compatibility (ctx, stack, type))
3944 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3945 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3946 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3947 } else if (!verify_type_compatibility (ctx, stack, type)) {
3948 char *expected_name = mono_type_full_name (type);
3949 char *stack_name = mono_type_full_name (stack);
3951 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));
3952 g_free (expected_name);
3953 g_free (stack_name);
3957 static void
3958 do_newobj (VerifyContext *ctx, int token)
3960 ILStackDesc *value;
3961 int i;
3962 MonoMethodSignature *sig;
3963 MonoMethod *method;
3964 gboolean is_delegate = FALSE;
3966 if (!(method = verifier_load_method (ctx, token, "newobj")))
3967 return;
3969 if (!mono_method_is_constructor (method)) {
3970 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3971 return;
3974 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3975 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3977 if (!mono_method_can_access_method_full (ctx->method, method, NULL)) {
3978 char *from = mono_method_full_name (ctx->method, TRUE);
3979 char *to = mono_method_full_name (method, TRUE);
3980 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);
3981 g_free (from);
3982 g_free (to);
3985 //FIXME use mono_method_get_signature_full
3986 sig = mono_method_signature (method);
3987 if (!sig) {
3988 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
3989 return;
3992 if (!check_underflow (ctx, sig->param_count))
3993 return;
3995 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3997 if (is_delegate) {
3998 ILStackDesc *funptr;
3999 //first arg is object, second arg is fun ptr
4000 if (sig->param_count != 2) {
4001 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
4002 return;
4004 funptr = stack_pop (ctx);
4005 value = stack_pop (ctx);
4006 verify_delegate_compatibility (ctx, method->klass, value, funptr);
4007 } else {
4008 for (i = sig->param_count - 1; i >= 0; --i) {
4009 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
4010 value = stack_pop (ctx);
4011 if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
4012 char *stack_name = stack_slot_full_name (value);
4013 char *sig_name = mono_type_full_name (sig->params [i]);
4014 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));
4015 g_free (stack_name);
4016 g_free (sig_name);
4019 if (stack_slot_is_managed_mutability_pointer (value))
4020 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
4024 if (check_overflow (ctx))
4025 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
4028 static void
4029 do_cast (VerifyContext *ctx, int token, const char *opcode) {
4030 ILStackDesc *value;
4031 MonoType *type;
4032 gboolean is_boxed;
4033 gboolean do_box;
4035 if (!check_underflow (ctx, 1))
4036 return;
4038 if (!(type = verifier_load_type (ctx, token, opcode)))
4039 return;
4041 if (type->byref) {
4042 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
4043 return;
4046 value = stack_pop (ctx);
4047 is_boxed = stack_slot_is_boxed_value (value);
4049 if (stack_slot_is_managed_pointer (value))
4050 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
4051 else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
4052 char *name = stack_slot_full_name (value);
4053 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));
4054 g_free (name);
4057 switch (value->type->type) {
4058 case MONO_TYPE_FNPTR:
4059 case MONO_TYPE_PTR:
4060 case MONO_TYPE_TYPEDBYREF:
4061 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
4064 do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
4065 stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
4068 static MonoType *
4069 mono_type_from_opcode (int opcode) {
4070 switch (opcode) {
4071 case CEE_LDIND_I1:
4072 case CEE_LDIND_U1:
4073 case CEE_STIND_I1:
4074 case CEE_LDELEM_I1:
4075 case CEE_LDELEM_U1:
4076 case CEE_STELEM_I1:
4077 return &mono_defaults.sbyte_class->byval_arg;
4079 case CEE_LDIND_I2:
4080 case CEE_LDIND_U2:
4081 case CEE_STIND_I2:
4082 case CEE_LDELEM_I2:
4083 case CEE_LDELEM_U2:
4084 case CEE_STELEM_I2:
4085 return &mono_defaults.int16_class->byval_arg;
4087 case CEE_LDIND_I4:
4088 case CEE_LDIND_U4:
4089 case CEE_STIND_I4:
4090 case CEE_LDELEM_I4:
4091 case CEE_LDELEM_U4:
4092 case CEE_STELEM_I4:
4093 return &mono_defaults.int32_class->byval_arg;
4095 case CEE_LDIND_I8:
4096 case CEE_STIND_I8:
4097 case CEE_LDELEM_I8:
4098 case CEE_STELEM_I8:
4099 return &mono_defaults.int64_class->byval_arg;
4101 case CEE_LDIND_R4:
4102 case CEE_STIND_R4:
4103 case CEE_LDELEM_R4:
4104 case CEE_STELEM_R4:
4105 return &mono_defaults.single_class->byval_arg;
4107 case CEE_LDIND_R8:
4108 case CEE_STIND_R8:
4109 case CEE_LDELEM_R8:
4110 case CEE_STELEM_R8:
4111 return &mono_defaults.double_class->byval_arg;
4113 case CEE_LDIND_I:
4114 case CEE_STIND_I:
4115 case CEE_LDELEM_I:
4116 case CEE_STELEM_I:
4117 return &mono_defaults.int_class->byval_arg;
4119 case CEE_LDIND_REF:
4120 case CEE_STIND_REF:
4121 case CEE_LDELEM_REF:
4122 case CEE_STELEM_REF:
4123 return &mono_defaults.object_class->byval_arg;
4125 default:
4126 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
4127 return NULL;
4131 static void
4132 do_load_indirect (VerifyContext *ctx, int opcode)
4134 ILStackDesc *value;
4135 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
4137 if (!check_underflow (ctx, 1))
4138 return;
4140 value = stack_pop (ctx);
4141 if (!stack_slot_is_managed_pointer (value)) {
4142 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
4143 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4144 return;
4147 if (opcode == CEE_LDIND_REF) {
4148 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
4149 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
4150 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
4151 } else {
4152 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
4153 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4154 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
4158 static void
4159 do_store_indirect (VerifyContext *ctx, int opcode)
4161 ILStackDesc *addr, *val;
4162 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
4164 if (!check_underflow (ctx, 2))
4165 return;
4167 val = stack_pop (ctx);
4168 addr = stack_pop (ctx);
4170 check_unmanaged_pointer (ctx, addr);
4172 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
4173 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
4174 return;
4177 if (stack_slot_is_managed_mutability_pointer (addr)) {
4178 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
4179 return;
4182 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
4183 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4185 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
4186 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
4189 static void
4190 do_newarr (VerifyContext *ctx, int token)
4192 ILStackDesc *value;
4193 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
4195 if (!type)
4196 return;
4198 if (!check_underflow (ctx, 1))
4199 return;
4201 value = stack_pop (ctx);
4202 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4203 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));
4205 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
4208 /*FIXME handle arrays that are not 0-indexed*/
4209 static void
4210 do_ldlen (VerifyContext *ctx)
4212 ILStackDesc *value;
4214 if (!check_underflow (ctx, 1))
4215 return;
4217 value = stack_pop (ctx);
4219 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
4220 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
4222 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
4225 /*FIXME handle arrays that are not 0-indexed*/
4226 /*FIXME handle readonly prefix and CMMP*/
4227 static void
4228 do_ldelema (VerifyContext *ctx, int klass_token)
4230 ILStackDesc *index, *array, *res;
4231 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
4232 gboolean valid;
4234 if (!type)
4235 return;
4237 if (!check_underflow (ctx, 2))
4238 return;
4240 index = stack_pop (ctx);
4241 array = stack_pop (ctx);
4243 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4244 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));
4246 if (!stack_slot_is_null_literal (array)) {
4247 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4248 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
4249 else {
4250 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
4251 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
4252 } else {
4253 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
4255 if (!valid)
4256 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
4260 res = stack_push (ctx);
4261 set_stack_value (ctx, res, type, TRUE);
4262 if (ctx->prefix_set & PREFIX_READONLY) {
4263 ctx->prefix_set &= ~PREFIX_READONLY;
4264 res->stype |= CMMP_MASK;
4269 * FIXME handle arrays that are not 0-indexed
4270 * FIXME handle readonly prefix and CMMP
4272 static void
4273 do_ldelem (VerifyContext *ctx, int opcode, int token)
4275 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
4276 ILStackDesc *index, *array;
4277 MonoType *type;
4278 if (!check_underflow (ctx, 2))
4279 return;
4281 if (opcode == CEE_LDELEM) {
4282 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
4283 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4284 return;
4286 } else {
4287 type = mono_type_from_opcode (opcode);
4290 index = stack_pop (ctx);
4291 array = stack_pop (ctx);
4293 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4294 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));
4296 if (!stack_slot_is_null_literal (array)) {
4297 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
4298 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));
4299 else {
4300 if (opcode == CEE_LDELEM_REF) {
4301 if (array->type->data.klass->valuetype)
4302 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
4303 type = &array->type->data.klass->byval_arg;
4304 } else {
4305 MonoType *candidate = &array->type->data.klass->byval_arg;
4306 if (IS_STRICT_MODE (ctx)) {
4307 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
4308 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
4309 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)) ||
4310 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
4311 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4313 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
4314 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
4319 set_stack_value (ctx, stack_push (ctx), type, FALSE);
4320 #undef IS_ONE_OF2
4324 * FIXME handle arrays that are not 0-indexed
4326 static void
4327 do_stelem (VerifyContext *ctx, int opcode, int token)
4329 ILStackDesc *index, *array, *value;
4330 MonoType *type;
4331 if (!check_underflow (ctx, 3))
4332 return;
4334 if (opcode == CEE_STELEM) {
4335 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
4336 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
4337 return;
4339 } else {
4340 type = mono_type_from_opcode (opcode);
4343 value = stack_pop (ctx);
4344 index = stack_pop (ctx);
4345 array = stack_pop (ctx);
4347 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
4348 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));
4350 if (!stack_slot_is_null_literal (array)) {
4351 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
4352 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));
4353 } else {
4354 if (opcode == CEE_STELEM_REF) {
4355 if (array->type->data.klass->valuetype)
4356 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4357 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
4358 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4362 if (opcode == CEE_STELEM_REF) {
4363 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
4364 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
4365 } else if (opcode != CEE_STELEM_REF) {
4366 if (!verify_stack_type_compatibility (ctx, type, value))
4367 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
4369 if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
4370 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));
4375 static void
4376 do_throw (VerifyContext *ctx)
4378 ILStackDesc *exception;
4379 if (!check_underflow (ctx, 1))
4380 return;
4381 exception = stack_pop (ctx);
4383 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
4384 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
4386 if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
4387 char *name = mono_type_full_name (exception->type);
4388 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));
4389 g_free (name);
4391 /*The stack is left empty after a throw*/
4392 ctx->eval.size = 0;
4396 static void
4397 do_endfilter (VerifyContext *ctx)
4399 MonoExceptionClause *clause;
4401 if (IS_STRICT_MODE (ctx)) {
4402 if (ctx->eval.size != 1)
4403 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
4405 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
4406 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
4409 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
4410 if (IS_STRICT_MODE (ctx)) {
4411 if (ctx->ip_offset != clause->handler_offset - 2)
4412 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4413 } else {
4414 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
4415 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
4417 } else {
4418 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
4419 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4420 else
4421 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
4424 ctx->eval.size = 0;
4427 static void
4428 do_leave (VerifyContext *ctx, int delta)
4430 int target = ((gint32)ctx->ip_offset) + delta;
4431 if (target >= ctx->code_size || target < 0)
4432 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
4434 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
4435 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
4436 ctx->eval.size = 0;
4440 * do_static_branch:
4442 * Verify br and br.s opcodes.
4444 static void
4445 do_static_branch (VerifyContext *ctx, int delta)
4447 int target = ctx->ip_offset + delta;
4448 if (target < 0 || target >= ctx->code_size) {
4449 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
4450 return;
4453 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4454 case 1:
4455 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4456 break;
4457 case 2:
4458 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
4459 break;
4462 ctx->target = target;
4465 static void
4466 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
4468 int i, base = ctx->ip_offset + 5 + count * 4;
4469 ILStackDesc *value;
4471 if (!check_underflow (ctx, 1))
4472 return;
4474 value = stack_pop (ctx);
4476 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
4477 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
4479 for (i = 0; i < count; ++i) {
4480 int target = base + read32 (data + i * 4);
4482 if (target < 0 || target >= ctx->code_size) {
4483 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
4484 return;
4487 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
4488 case 1:
4489 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4490 break;
4491 case 2:
4492 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
4493 return;
4495 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
4499 static void
4500 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
4502 ILStackDesc *top;
4503 MonoMethod *method;
4505 if (virtual && !check_underflow (ctx, 1))
4506 return;
4508 if (!virtual && !check_overflow (ctx))
4509 return;
4511 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4512 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4513 return;
4516 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
4517 return;
4519 if (mono_method_is_constructor (method))
4520 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
4522 if (virtual) {
4523 ILStackDesc *top = stack_pop (ctx);
4525 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
4526 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
4528 if (method->flags & METHOD_ATTRIBUTE_STATIC)
4529 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
4531 if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
4532 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
4535 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
4536 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);
4538 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
4539 top->method = method;
4542 static void
4543 do_sizeof (VerifyContext *ctx, int token)
4545 MonoType *type;
4547 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
4548 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4549 return;
4552 if (!(type = verifier_load_type (ctx, token, "sizeof")))
4553 return;
4555 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
4556 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
4557 return;
4560 if (type->type == MONO_TYPE_VOID) {
4561 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
4562 return;
4565 if (check_overflow (ctx))
4566 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
4569 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
4570 static void
4571 do_localloc (VerifyContext *ctx)
4573 ILStackDesc *top;
4575 if (ctx->eval.size != 1) {
4576 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4577 return;
4580 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
4581 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
4582 return;
4585 /*TODO verify top type*/
4586 top = stack_pop (ctx);
4588 set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
4589 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
4592 static void
4593 do_ldstr (VerifyContext *ctx, guint32 token)
4595 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
4596 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4597 return;
4600 if (!ctx->image->dynamic && mono_metadata_token_index (token) >= ctx->image->heap_us.size) {
4601 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
4602 return;
4605 if (check_overflow (ctx))
4606 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4609 static void
4610 do_refanyval (VerifyContext *ctx, int token)
4612 ILStackDesc *top;
4613 MonoType *type;
4614 if (!check_underflow (ctx, 1))
4615 return;
4617 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4618 return;
4620 top = stack_pop (ctx);
4622 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4623 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));
4625 set_stack_value (ctx, stack_push (ctx), type, TRUE);
4628 static void
4629 do_refanytype (VerifyContext *ctx)
4631 ILStackDesc *top;
4633 if (!check_underflow (ctx, 1))
4634 return;
4636 top = stack_pop (ctx);
4638 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
4639 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));
4641 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
4645 static void
4646 do_mkrefany (VerifyContext *ctx, int token)
4648 ILStackDesc *top;
4649 MonoType *type;
4650 if (!check_underflow (ctx, 1))
4651 return;
4653 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
4654 return;
4656 top = stack_pop (ctx);
4658 if (stack_slot_is_managed_mutability_pointer (top))
4659 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
4661 if (!stack_slot_is_managed_pointer (top)) {
4662 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));
4663 }else {
4664 MonoType *stack_type = mono_type_get_type_byval (top->type);
4665 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
4666 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4668 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
4669 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
4672 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
4675 static void
4676 do_ckfinite (VerifyContext *ctx)
4678 ILStackDesc *top;
4679 if (!check_underflow (ctx, 1))
4680 return;
4682 top = stack_pop (ctx);
4684 if (stack_slot_get_underlying_type (top) != TYPE_R8)
4685 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));
4686 stack_push_stack_val (ctx, top);
4689 * merge_stacks:
4690 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
4692 * @from holds new values for a given control path
4693 * @to holds the current values of a given control path
4695 * TODO we can eliminate the from argument as all callers pass &ctx->eval
4697 static void
4698 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
4700 int i, j, k;
4701 stack_init (ctx, to);
4703 if (start) {
4704 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
4705 from->size = 0;
4706 else
4707 stack_copy (&ctx->eval, to);
4708 goto end_verify;
4709 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
4710 stack_copy (to, &ctx->eval);
4711 goto end_verify;
4713 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
4715 if (from->size != to->size) {
4716 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
4717 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));
4718 goto end_verify;
4721 //FIXME we need to preserve CMMP attributes
4722 //FIXME we must take null literals into consideration.
4723 for (i = 0; i < from->size; ++i) {
4724 ILStackDesc *new_slot = from->stack + i;
4725 ILStackDesc *old_slot = to->stack + i;
4726 MonoType *new_type = mono_type_from_stack_slot (new_slot);
4727 MonoType *old_type = mono_type_from_stack_slot (old_slot);
4728 MonoClass *old_class = mono_class_from_mono_type (old_type);
4729 MonoClass *new_class = mono_class_from_mono_type (new_type);
4730 MonoClass *match_class = NULL;
4732 // S := T then U = S (new value is compatible with current value, keep current)
4733 if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
4734 copy_stack_value (new_slot, old_slot);
4735 continue;
4738 // T := S then U = T (old value is compatible with current value, use new)
4739 if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
4740 copy_stack_value (old_slot, new_slot);
4741 continue;
4744 if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
4745 char *old_name = stack_slot_full_name (old_slot);
4746 char *new_name = stack_slot_full_name (new_slot);
4747 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));
4748 g_free (old_name);
4749 g_free (new_name);
4750 goto end_verify;
4753 //both are reference types, use closest common super type
4754 if (!mono_class_from_mono_type (old_type)->valuetype
4755 && !mono_class_from_mono_type (new_type)->valuetype
4756 && !stack_slot_is_managed_pointer (old_slot)
4757 && !stack_slot_is_managed_pointer (new_slot)) {
4759 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
4760 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
4761 match_class = old_class->supertypes [j];
4762 goto match_found;
4766 mono_class_setup_interfaces (old_class);
4767 for (j = 0; j < old_class->interface_count; ++j) {
4768 for (k = 0; k < new_class->interface_count; ++k) {
4769 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
4770 match_class = old_class->interfaces [j];
4771 goto match_found;
4776 //No decent super type found, use object
4777 match_class = mono_defaults.object_class;
4778 goto match_found;
4779 } 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)) {
4780 match_class = mono_defaults.object_class;
4781 goto match_found;
4785 char *old_name = stack_slot_full_name (old_slot);
4786 char *new_name = stack_slot_full_name (new_slot);
4787 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));
4788 g_free (old_name);
4789 g_free (new_name);
4791 set_stack_value (ctx, old_slot, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4792 goto end_verify;
4794 match_found:
4795 g_assert (match_class);
4796 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4797 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
4798 continue;
4801 end_verify:
4802 if (external)
4803 to->flags |= IL_CODE_FLAG_WAS_TARGET;
4804 to->flags |= IL_CODE_STACK_MERGED;
4807 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4808 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4811 * is_clause_in_range :
4813 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4815 static gboolean
4816 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4818 if (clause->try_offset >= start && clause->try_offset < end)
4819 return TRUE;
4820 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4821 return TRUE;
4822 return FALSE;
4826 * is_clause_inside_range :
4828 * Returns TRUE if @clause lies completely inside the @start - @end range.
4830 static gboolean
4831 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4833 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4834 return FALSE;
4835 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4836 return FALSE;
4837 return TRUE;
4841 * is_clause_nested :
4843 * Returns TRUE if @nested is nested in @clause.
4845 static gboolean
4846 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4848 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4849 return TRUE;
4850 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4851 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4854 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4855 * the each pair of exception must have the following properties:
4856 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4857 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4858 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4860 static void
4861 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4863 /*clause is nested*/
4864 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
4865 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4866 return;
4869 /*wrong nesting order.*/
4870 if (is_clause_nested (clause, to_test)) {
4871 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4872 return;
4875 /*mutual protection*/
4876 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4877 /*handlers are not disjoint*/
4878 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4879 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4880 return;
4882 /* handlers are not catch or filter */
4883 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4884 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4885 return;
4887 /*OK*/
4888 return;
4891 /*not completelly disjoint*/
4892 if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4893 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
4894 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4897 #define code_bounds_check(size) \
4898 if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
4899 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
4900 break; \
4904 * FIXME: need to distinguish between valid and verifiable.
4905 * Need to keep track of types on the stack.
4906 * Verify types for opcodes.
4908 GSList*
4909 mono_method_verify (MonoMethod *method, int level)
4911 MonoError error;
4912 const unsigned char *ip;
4913 const unsigned char *end;
4914 int i, n, need_merge = 0, start = 0;
4915 guint token, ip_offset = 0, prefix = 0;
4916 MonoGenericContext *generic_context = NULL;
4917 MonoImage *image;
4918 VerifyContext ctx;
4919 GSList *tmp;
4920 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4922 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4923 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4924 return NULL;
4927 memset (&ctx, 0, sizeof (VerifyContext));
4929 //FIXME use mono_method_get_signature_full
4930 ctx.signature = mono_method_signature (method);
4931 if (!ctx.signature) {
4932 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4933 return ctx.list;
4935 ctx.header = mono_method_get_header (method);
4936 if (!ctx.header) {
4937 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4938 return ctx.list;
4940 ctx.method = method;
4941 ip = ctx.header->code;
4942 end = ip + ctx.header->code_size;
4943 ctx.image = image = method->klass->image;
4946 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4947 ctx.max_stack = ctx.header->max_stack;
4948 ctx.verifiable = ctx.valid = 1;
4949 ctx.level = level;
4951 ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
4952 ctx.code_size = ctx.header->code_size;
4954 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4957 ctx.num_locals = ctx.header->num_locals;
4958 ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
4960 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4961 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4963 ctx.params = g_new (MonoType*, ctx.max_args);
4964 if (ctx.signature->hasthis)
4965 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4966 memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4968 if (ctx.signature->is_inflated)
4969 ctx.generic_context = generic_context = mono_method_get_context (method);
4971 if (!generic_context && (method->klass->generic_container || method->is_generic)) {
4972 if (method->is_generic)
4973 ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
4974 else
4975 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4978 for (i = 0; i < ctx.num_locals; ++i) {
4979 MonoType *uninflated = ctx.locals [i];
4980 ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, &error);
4981 if (!mono_error_ok (&error)) {
4982 char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
4983 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
4984 g_free (name);
4985 mono_error_cleanup (&error);
4986 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
4987 ctx.num_locals = i;
4988 ctx.max_args = 0;
4989 goto cleanup;
4992 for (i = 0; i < ctx.max_args; ++i) {
4993 MonoType *uninflated = ctx.params [i];
4994 ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, &error);
4995 if (!mono_error_ok (&error)) {
4996 char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
4997 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
4998 g_free (name);
4999 mono_error_cleanup (&error);
5000 /* we must not free (in cleanup) what was not yet allocated (but only copied) */
5001 ctx.max_args = i;
5002 goto cleanup;
5005 stack_init (&ctx, &ctx.eval);
5007 for (i = 0; i < ctx.num_locals; ++i) {
5008 if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
5009 break;
5012 for (i = 0; i < ctx.max_args; ++i) {
5013 if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
5014 break;
5017 if (!ctx.valid)
5018 goto cleanup;
5020 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
5021 MonoExceptionClause *clause = ctx.header->clauses + i;
5022 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); );
5024 if (clause->try_offset > ctx.code_size || clause->try_offset + clause->try_len > ctx.code_size)
5025 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
5027 if (clause->try_len <= 0)
5028 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
5030 if (clause->handler_offset > ctx.code_size || clause->handler_offset + clause->handler_len > ctx.code_size)
5031 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
5033 if (clause->handler_len <= 0)
5034 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
5036 if (clause->try_offset < clause->handler_offset && clause->try_offset + clause->try_len > HANDLER_START (clause))
5037 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
5039 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
5040 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
5042 if (!ctx.valid)
5043 break;
5045 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
5046 if (clause->try_offset + clause->try_len < ctx.code_size)
5047 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
5048 if (clause->handler_offset + clause->handler_len < ctx.code_size)
5049 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
5051 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
5052 if (!clause->data.catch_class) {
5053 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
5054 break;
5057 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
5059 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
5060 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
5061 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
5065 while (ip < end && ctx.valid) {
5066 ctx.ip_offset = ip_offset = ip - ctx.header->code;
5068 /*We need to check against fallthrou in and out of protected blocks.
5069 * For fallout we check the once a protected block ends, if the start flag is not set.
5070 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
5071 * TODO convert these checks to be done using flags and not this loop
5073 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
5074 MonoExceptionClause *clause = ctx.header->clauses + i;
5076 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
5077 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
5078 start = 1;
5081 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
5082 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
5083 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5084 else
5085 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
5086 start = 1;
5089 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
5090 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
5091 start = 1;
5094 if (clause->handler_offset == ip_offset && start == 0) {
5095 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
5096 start = 1;
5099 if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
5100 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
5101 start = 1;
5105 if (!ctx.valid)
5106 break;
5108 if (need_merge) {
5109 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
5110 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
5111 need_merge = 0;
5113 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
5114 start = 0;
5116 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5117 #ifdef MONO_VERIFIER_DEBUG
5119 char *discode;
5120 discode = mono_disasm_code_one (NULL, method, ip, NULL);
5121 discode [strlen (discode) - 1] = 0; /* no \n */
5122 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
5123 g_free (discode);
5125 dump_stack_state (&ctx.code [ip_offset]);
5126 dump_stack_state (&ctx.eval);
5127 #endif
5129 switch (*ip) {
5130 case CEE_NOP:
5131 case CEE_BREAK:
5132 ++ip;
5133 break;
5135 case CEE_LDARG_0:
5136 case CEE_LDARG_1:
5137 case CEE_LDARG_2:
5138 case CEE_LDARG_3:
5139 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
5140 ++ip;
5141 break;
5143 case CEE_LDARG_S:
5144 case CEE_LDARGA_S:
5145 code_bounds_check (2);
5146 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
5147 ip += 2;
5148 break;
5150 case CEE_ADD_OVF_UN:
5151 do_binop (&ctx, *ip, add_ovf_un_table);
5152 ++ip;
5153 break;
5155 case CEE_SUB_OVF_UN:
5156 do_binop (&ctx, *ip, sub_ovf_un_table);
5157 ++ip;
5158 break;
5160 case CEE_ADD_OVF:
5161 case CEE_SUB_OVF:
5162 case CEE_MUL_OVF:
5163 case CEE_MUL_OVF_UN:
5164 do_binop (&ctx, *ip, bin_ovf_table);
5165 ++ip;
5166 break;
5168 case CEE_ADD:
5169 do_binop (&ctx, *ip, add_table);
5170 ++ip;
5171 break;
5173 case CEE_SUB:
5174 do_binop (&ctx, *ip, sub_table);
5175 ++ip;
5176 break;
5178 case CEE_MUL:
5179 case CEE_DIV:
5180 case CEE_REM:
5181 do_binop (&ctx, *ip, bin_op_table);
5182 ++ip;
5183 break;
5185 case CEE_AND:
5186 case CEE_DIV_UN:
5187 case CEE_OR:
5188 case CEE_REM_UN:
5189 case CEE_XOR:
5190 do_binop (&ctx, *ip, int_bin_op_table);
5191 ++ip;
5192 break;
5194 case CEE_SHL:
5195 case CEE_SHR:
5196 case CEE_SHR_UN:
5197 do_binop (&ctx, *ip, shift_op_table);
5198 ++ip;
5199 break;
5201 case CEE_POP:
5202 if (!check_underflow (&ctx, 1))
5203 break;
5204 stack_pop_safe (&ctx);
5205 ++ip;
5206 break;
5208 case CEE_RET:
5209 do_ret (&ctx);
5210 ++ip;
5211 start = 1;
5212 break;
5214 case CEE_LDLOC_0:
5215 case CEE_LDLOC_1:
5216 case CEE_LDLOC_2:
5217 case CEE_LDLOC_3:
5218 /*TODO support definite assignment verification? */
5219 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
5220 ++ip;
5221 break;
5223 case CEE_STLOC_0:
5224 case CEE_STLOC_1:
5225 case CEE_STLOC_2:
5226 case CEE_STLOC_3:
5227 store_local (&ctx, *ip - CEE_STLOC_0);
5228 ++ip;
5229 break;
5231 case CEE_STLOC_S:
5232 code_bounds_check (2);
5233 store_local (&ctx, ip [1]);
5234 ip += 2;
5235 break;
5237 case CEE_STARG_S:
5238 code_bounds_check (2);
5239 store_arg (&ctx, ip [1]);
5240 ip += 2;
5241 break;
5243 case CEE_LDC_I4_M1:
5244 case CEE_LDC_I4_0:
5245 case CEE_LDC_I4_1:
5246 case CEE_LDC_I4_2:
5247 case CEE_LDC_I4_3:
5248 case CEE_LDC_I4_4:
5249 case CEE_LDC_I4_5:
5250 case CEE_LDC_I4_6:
5251 case CEE_LDC_I4_7:
5252 case CEE_LDC_I4_8:
5253 if (check_overflow (&ctx))
5254 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5255 ++ip;
5256 break;
5258 case CEE_LDC_I4_S:
5259 code_bounds_check (2);
5260 if (check_overflow (&ctx))
5261 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
5262 ip += 2;
5263 break;
5265 case CEE_LDC_I4:
5266 code_bounds_check (5);
5267 if (check_overflow (&ctx))
5268 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
5269 ip += 5;
5270 break;
5272 case CEE_LDC_I8:
5273 code_bounds_check (9);
5274 if (check_overflow (&ctx))
5275 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
5276 ip += 9;
5277 break;
5279 case CEE_LDC_R4:
5280 code_bounds_check (5);
5281 if (check_overflow (&ctx))
5282 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5283 ip += 5;
5284 break;
5286 case CEE_LDC_R8:
5287 code_bounds_check (9);
5288 if (check_overflow (&ctx))
5289 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
5290 ip += 9;
5291 break;
5293 case CEE_LDNULL:
5294 if (check_overflow (&ctx))
5295 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
5296 ++ip;
5297 break;
5299 case CEE_BEQ_S:
5300 case CEE_BNE_UN_S:
5301 code_bounds_check (2);
5302 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
5303 ip += 2;
5304 need_merge = 1;
5305 break;
5307 case CEE_BGE_S:
5308 case CEE_BGT_S:
5309 case CEE_BLE_S:
5310 case CEE_BLT_S:
5311 case CEE_BGE_UN_S:
5312 case CEE_BGT_UN_S:
5313 case CEE_BLE_UN_S:
5314 case CEE_BLT_UN_S:
5315 code_bounds_check (2);
5316 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
5317 ip += 2;
5318 need_merge = 1;
5319 break;
5321 case CEE_BEQ:
5322 case CEE_BNE_UN:
5323 code_bounds_check (5);
5324 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
5325 ip += 5;
5326 need_merge = 1;
5327 break;
5329 case CEE_BGE:
5330 case CEE_BGT:
5331 case CEE_BLE:
5332 case CEE_BLT:
5333 case CEE_BGE_UN:
5334 case CEE_BGT_UN:
5335 case CEE_BLE_UN:
5336 case CEE_BLT_UN:
5337 code_bounds_check (5);
5338 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
5339 ip += 5;
5340 need_merge = 1;
5341 break;
5343 case CEE_LDLOC_S:
5344 case CEE_LDLOCA_S:
5345 code_bounds_check (2);
5346 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
5347 ip += 2;
5348 break;
5350 /* FIXME: warn/error instead? */
5351 case CEE_UNUSED99:
5352 ++ip;
5353 break;
5355 case CEE_DUP: {
5356 ILStackDesc * top;
5357 if (!check_underflow (&ctx, 1))
5358 break;
5359 if (!check_overflow (&ctx))
5360 break;
5361 top = stack_pop_safe (&ctx);
5362 copy_stack_value (stack_push (&ctx), top);
5363 copy_stack_value (stack_push (&ctx), top);
5364 ++ip;
5365 break;
5368 case CEE_JMP:
5369 code_bounds_check (5);
5370 if (ctx.eval.size)
5371 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
5372 token = read32 (ip + 1);
5373 if (in_any_block (ctx.header, ip_offset))
5374 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
5376 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
5378 * FIXME: check signature, retval, arguments etc.
5380 ip += 5;
5381 break;
5382 case CEE_CALL:
5383 case CEE_CALLVIRT:
5384 code_bounds_check (5);
5385 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
5386 ip += 5;
5387 break;
5389 case CEE_CALLI:
5390 code_bounds_check (5);
5391 token = read32 (ip + 1);
5393 * FIXME: check signature, retval, arguments etc.
5394 * FIXME: check requirements for tail call
5396 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
5397 ip += 5;
5398 break;
5399 case CEE_BR_S:
5400 code_bounds_check (2);
5401 do_static_branch (&ctx, (signed char)ip [1] + 2);
5402 need_merge = 1;
5403 ip += 2;
5404 start = 1;
5405 break;
5407 case CEE_BRFALSE_S:
5408 case CEE_BRTRUE_S:
5409 code_bounds_check (2);
5410 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
5411 ip += 2;
5412 need_merge = 1;
5413 break;
5415 case CEE_BR:
5416 code_bounds_check (5);
5417 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
5418 need_merge = 1;
5419 ip += 5;
5420 start = 1;
5421 break;
5423 case CEE_BRFALSE:
5424 case CEE_BRTRUE:
5425 code_bounds_check (5);
5426 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
5427 ip += 5;
5428 need_merge = 1;
5429 break;
5431 case CEE_SWITCH: {
5432 guint32 entries;
5433 code_bounds_check (5);
5434 entries = read32 (ip + 1);
5436 if (entries > 0xFFFFFFFFU / sizeof (guint32))
5437 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
5439 ip += 5;
5440 code_bounds_check (sizeof (guint32) * entries);
5442 do_switch (&ctx, entries, ip);
5443 start = 1;
5444 ip += sizeof (guint32) * entries;
5445 break;
5447 case CEE_LDIND_I1:
5448 case CEE_LDIND_U1:
5449 case CEE_LDIND_I2:
5450 case CEE_LDIND_U2:
5451 case CEE_LDIND_I4:
5452 case CEE_LDIND_U4:
5453 case CEE_LDIND_I8:
5454 case CEE_LDIND_I:
5455 case CEE_LDIND_R4:
5456 case CEE_LDIND_R8:
5457 case CEE_LDIND_REF:
5458 do_load_indirect (&ctx, *ip);
5459 ++ip;
5460 break;
5462 case CEE_STIND_REF:
5463 case CEE_STIND_I1:
5464 case CEE_STIND_I2:
5465 case CEE_STIND_I4:
5466 case CEE_STIND_I8:
5467 case CEE_STIND_R4:
5468 case CEE_STIND_R8:
5469 case CEE_STIND_I:
5470 do_store_indirect (&ctx, *ip);
5471 ++ip;
5472 break;
5474 case CEE_NOT:
5475 case CEE_NEG:
5476 do_unary_math_op (&ctx, *ip);
5477 ++ip;
5478 break;
5480 case CEE_CONV_I1:
5481 case CEE_CONV_I2:
5482 case CEE_CONV_I4:
5483 case CEE_CONV_U1:
5484 case CEE_CONV_U2:
5485 case CEE_CONV_U4:
5486 do_conversion (&ctx, TYPE_I4);
5487 ++ip;
5488 break;
5490 case CEE_CONV_I8:
5491 case CEE_CONV_U8:
5492 do_conversion (&ctx, TYPE_I8);
5493 ++ip;
5494 break;
5496 case CEE_CONV_R4:
5497 case CEE_CONV_R8:
5498 case CEE_CONV_R_UN:
5499 do_conversion (&ctx, TYPE_R8);
5500 ++ip;
5501 break;
5503 case CEE_CONV_I:
5504 case CEE_CONV_U:
5505 do_conversion (&ctx, TYPE_NATIVE_INT);
5506 ++ip;
5507 break;
5509 case CEE_CPOBJ:
5510 code_bounds_check (5);
5511 do_cpobj (&ctx, read32 (ip + 1));
5512 ip += 5;
5513 break;
5515 case CEE_LDOBJ:
5516 code_bounds_check (5);
5517 do_ldobj_value (&ctx, read32 (ip + 1));
5518 ip += 5;
5519 break;
5521 case CEE_LDSTR:
5522 code_bounds_check (5);
5523 do_ldstr (&ctx, read32 (ip + 1));
5524 ip += 5;
5525 break;
5527 case CEE_NEWOBJ:
5528 code_bounds_check (5);
5529 do_newobj (&ctx, read32 (ip + 1));
5530 ip += 5;
5531 break;
5533 case CEE_CASTCLASS:
5534 case CEE_ISINST:
5535 code_bounds_check (5);
5536 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
5537 ip += 5;
5538 break;
5540 case CEE_UNUSED58:
5541 case CEE_UNUSED1:
5542 ++ip; /* warn, error ? */
5543 break;
5545 case CEE_UNBOX:
5546 code_bounds_check (5);
5547 do_unbox_value (&ctx, read32 (ip + 1));
5548 ip += 5;
5549 break;
5551 case CEE_THROW:
5552 do_throw (&ctx);
5553 start = 1;
5554 ++ip;
5555 break;
5557 case CEE_LDFLD:
5558 case CEE_LDFLDA:
5559 code_bounds_check (5);
5560 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
5561 ip += 5;
5562 break;
5564 case CEE_LDSFLD:
5565 case CEE_LDSFLDA:
5566 code_bounds_check (5);
5567 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
5568 ip += 5;
5569 break;
5571 case CEE_STFLD:
5572 code_bounds_check (5);
5573 do_store_field (&ctx, read32 (ip + 1));
5574 ip += 5;
5575 break;
5577 case CEE_STSFLD:
5578 code_bounds_check (5);
5579 do_store_static_field (&ctx, read32 (ip + 1));
5580 ip += 5;
5581 break;
5583 case CEE_STOBJ:
5584 code_bounds_check (5);
5585 do_stobj (&ctx, read32 (ip + 1));
5586 ip += 5;
5587 break;
5589 case CEE_CONV_OVF_I1_UN:
5590 case CEE_CONV_OVF_I2_UN:
5591 case CEE_CONV_OVF_I4_UN:
5592 case CEE_CONV_OVF_U1_UN:
5593 case CEE_CONV_OVF_U2_UN:
5594 case CEE_CONV_OVF_U4_UN:
5595 do_conversion (&ctx, TYPE_I4);
5596 ++ip;
5597 break;
5599 case CEE_CONV_OVF_I8_UN:
5600 case CEE_CONV_OVF_U8_UN:
5601 do_conversion (&ctx, TYPE_I8);
5602 ++ip;
5603 break;
5605 case CEE_CONV_OVF_I_UN:
5606 case CEE_CONV_OVF_U_UN:
5607 do_conversion (&ctx, TYPE_NATIVE_INT);
5608 ++ip;
5609 break;
5611 case CEE_BOX:
5612 code_bounds_check (5);
5613 do_box_value (&ctx, read32 (ip + 1));
5614 ip += 5;
5615 break;
5617 case CEE_NEWARR:
5618 code_bounds_check (5);
5619 do_newarr (&ctx, read32 (ip + 1));
5620 ip += 5;
5621 break;
5623 case CEE_LDLEN:
5624 do_ldlen (&ctx);
5625 ++ip;
5626 break;
5628 case CEE_LDELEMA:
5629 code_bounds_check (5);
5630 do_ldelema (&ctx, read32 (ip + 1));
5631 ip += 5;
5632 break;
5634 case CEE_LDELEM_I1:
5635 case CEE_LDELEM_U1:
5636 case CEE_LDELEM_I2:
5637 case CEE_LDELEM_U2:
5638 case CEE_LDELEM_I4:
5639 case CEE_LDELEM_U4:
5640 case CEE_LDELEM_I8:
5641 case CEE_LDELEM_I:
5642 case CEE_LDELEM_R4:
5643 case CEE_LDELEM_R8:
5644 case CEE_LDELEM_REF:
5645 do_ldelem (&ctx, *ip, 0);
5646 ++ip;
5647 break;
5649 case CEE_STELEM_I:
5650 case CEE_STELEM_I1:
5651 case CEE_STELEM_I2:
5652 case CEE_STELEM_I4:
5653 case CEE_STELEM_I8:
5654 case CEE_STELEM_R4:
5655 case CEE_STELEM_R8:
5656 case CEE_STELEM_REF:
5657 do_stelem (&ctx, *ip, 0);
5658 ++ip;
5659 break;
5661 case CEE_LDELEM:
5662 code_bounds_check (5);
5663 do_ldelem (&ctx, *ip, read32 (ip + 1));
5664 ip += 5;
5665 break;
5667 case CEE_STELEM:
5668 code_bounds_check (5);
5669 do_stelem (&ctx, *ip, read32 (ip + 1));
5670 ip += 5;
5671 break;
5673 case CEE_UNBOX_ANY:
5674 code_bounds_check (5);
5675 do_unbox_any (&ctx, read32 (ip + 1));
5676 ip += 5;
5677 break;
5679 case CEE_CONV_OVF_I1:
5680 case CEE_CONV_OVF_U1:
5681 case CEE_CONV_OVF_I2:
5682 case CEE_CONV_OVF_U2:
5683 case CEE_CONV_OVF_I4:
5684 case CEE_CONV_OVF_U4:
5685 do_conversion (&ctx, TYPE_I4);
5686 ++ip;
5687 break;
5689 case CEE_CONV_OVF_I8:
5690 case CEE_CONV_OVF_U8:
5691 do_conversion (&ctx, TYPE_I8);
5692 ++ip;
5693 break;
5695 case CEE_CONV_OVF_I:
5696 case CEE_CONV_OVF_U:
5697 do_conversion (&ctx, TYPE_NATIVE_INT);
5698 ++ip;
5699 break;
5701 case CEE_REFANYVAL:
5702 code_bounds_check (5);
5703 do_refanyval (&ctx, read32 (ip + 1));
5704 ip += 5;
5705 break;
5707 case CEE_CKFINITE:
5708 do_ckfinite (&ctx);
5709 ++ip;
5710 break;
5712 case CEE_MKREFANY:
5713 code_bounds_check (5);
5714 do_mkrefany (&ctx, read32 (ip + 1));
5715 ip += 5;
5716 break;
5718 case CEE_LDTOKEN:
5719 code_bounds_check (5);
5720 do_load_token (&ctx, read32 (ip + 1));
5721 ip += 5;
5722 break;
5724 case CEE_ENDFINALLY:
5725 if (!is_correct_endfinally (ctx.header, ip_offset))
5726 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
5727 ctx.eval.size = 0;
5728 start = 1;
5729 ++ip;
5730 break;
5732 case CEE_LEAVE:
5733 code_bounds_check (5);
5734 do_leave (&ctx, read32 (ip + 1) + 5);
5735 ip += 5;
5736 start = 1;
5737 break;
5739 case CEE_LEAVE_S:
5740 code_bounds_check (2);
5741 do_leave (&ctx, (signed char)ip [1] + 2);
5742 ip += 2;
5743 start = 1;
5744 break;
5746 case CEE_PREFIX1:
5747 code_bounds_check (2);
5748 ++ip;
5749 switch (*ip) {
5750 case CEE_STLOC:
5751 code_bounds_check (3);
5752 store_local (&ctx, read16 (ip + 1));
5753 ip += 3;
5754 break;
5756 case CEE_CEQ:
5757 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
5758 ++ip;
5759 break;
5761 case CEE_CGT:
5762 case CEE_CGT_UN:
5763 case CEE_CLT:
5764 case CEE_CLT_UN:
5765 do_cmp_op (&ctx, cmp_br_op, *ip);
5766 ++ip;
5767 break;
5769 case CEE_STARG:
5770 code_bounds_check (3);
5771 store_arg (&ctx, read16 (ip + 1) );
5772 ip += 3;
5773 break;
5776 case CEE_ARGLIST:
5777 check_overflow (&ctx);
5778 if (ctx.signature->call_convention != MONO_CALL_VARARG)
5779 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
5780 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
5781 ++ip;
5782 break;
5784 case CEE_LDFTN:
5785 code_bounds_check (5);
5786 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
5787 ip += 5;
5788 break;
5790 case CEE_LDVIRTFTN:
5791 code_bounds_check (5);
5792 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
5793 ip += 5;
5794 break;
5796 case CEE_UNUSED56:
5797 ++ip;
5798 break;
5800 case CEE_LDARG:
5801 case CEE_LDARGA:
5802 code_bounds_check (3);
5803 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
5804 ip += 3;
5805 break;
5807 case CEE_LDLOC:
5808 case CEE_LDLOCA:
5809 code_bounds_check (3);
5810 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
5811 ip += 3;
5812 break;
5814 case CEE_LOCALLOC:
5815 do_localloc (&ctx);
5816 ++ip;
5817 break;
5819 case CEE_UNUSED57:
5820 ++ip;
5821 break;
5822 case CEE_ENDFILTER:
5823 do_endfilter (&ctx);
5824 start = 1;
5825 ++ip;
5826 break;
5827 case CEE_UNALIGNED_:
5828 code_bounds_check (2);
5829 prefix |= PREFIX_UNALIGNED;
5830 ip += 2;
5831 break;
5832 case CEE_VOLATILE_:
5833 prefix |= PREFIX_VOLATILE;
5834 ++ip;
5835 break;
5836 case CEE_TAIL_:
5837 prefix |= PREFIX_TAIL;
5838 ++ip;
5839 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
5840 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5841 break;
5843 case CEE_INITOBJ:
5844 code_bounds_check (5);
5845 do_initobj (&ctx, read32 (ip + 1));
5846 ip += 5;
5847 break;
5849 case CEE_CONSTRAINED_:
5850 code_bounds_check (5);
5851 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5852 prefix |= PREFIX_CONSTRAINED;
5853 ip += 5;
5854 break;
5856 case CEE_READONLY_:
5857 prefix |= PREFIX_READONLY;
5858 ip++;
5859 break;
5861 case CEE_CPBLK:
5862 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5863 if (!check_underflow (&ctx, 3))
5864 break;
5865 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5866 ip++;
5867 break;
5869 case CEE_INITBLK:
5870 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5871 if (!check_underflow (&ctx, 3))
5872 break;
5873 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5874 ip++;
5875 break;
5877 case CEE_NO_:
5878 ip += 2;
5879 break;
5880 case CEE_RETHROW:
5881 if (!is_correct_rethrow (ctx.header, ip_offset))
5882 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5883 ctx.eval.size = 0;
5884 start = 1;
5885 ++ip;
5886 break;
5887 case CEE_UNUSED:
5888 ++ip;
5889 break;
5891 case CEE_SIZEOF:
5892 code_bounds_check (5);
5893 do_sizeof (&ctx, read32 (ip + 1));
5894 ip += 5;
5895 break;
5897 case CEE_REFANYTYPE:
5898 do_refanytype (&ctx);
5899 ++ip;
5900 break;
5902 default:
5903 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
5904 ++ip;
5906 break;
5908 default:
5909 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
5910 ++ip;
5913 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5914 if (prefix) {
5915 if (!ctx.prefix_set) //first prefix
5916 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5917 ctx.prefix_set |= prefix;
5918 ctx.has_flags = TRUE;
5919 prefix = 0;
5920 } else {
5921 if (!ctx.has_flags)
5922 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5924 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5925 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5926 if (ctx.prefix_set & PREFIX_READONLY)
5927 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5928 if (ctx.prefix_set & PREFIX_VOLATILE)
5929 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5930 if (ctx.prefix_set & PREFIX_UNALIGNED)
5931 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5932 ctx.prefix_set = prefix = 0;
5933 ctx.has_flags = FALSE;
5937 * if ip != end we overflowed: mark as error.
5939 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5940 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5943 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5944 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5945 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5946 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5947 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5949 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5950 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5952 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5953 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5955 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5956 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));
5959 if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class) {
5960 char *method_name = mono_method_full_name (ctx.method, TRUE);
5961 char *type = mono_type_get_full_name (ctx.method->klass);
5962 if (ctx.method->klass->parent && ctx.method->klass->parent->exception_type != MONO_EXCEPTION_NONE)
5963 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));
5964 else
5965 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
5966 g_free (method_name);
5967 g_free (type);
5970 cleanup:
5971 if (ctx.code) {
5972 for (i = 0; i < ctx.header->code_size; ++i) {
5973 if (ctx.code [i].stack)
5974 g_free (ctx.code [i].stack);
5978 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5979 g_free (tmp->data);
5980 g_slist_free (ctx.funptrs);
5982 for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
5983 mono_metadata_free_type (tmp->data);
5984 g_slist_free (ctx.exception_types);
5986 for (i = 0; i < ctx.num_locals; ++i) {
5987 if (ctx.locals [i])
5988 mono_metadata_free_type (ctx.locals [i]);
5990 for (i = 0; i < ctx.max_args; ++i) {
5991 if (ctx.params [i])
5992 mono_metadata_free_type (ctx.params [i]);
5995 if (ctx.eval.stack)
5996 g_free (ctx.eval.stack);
5997 if (ctx.code)
5998 g_free (ctx.code);
5999 g_free (ctx.locals);
6000 g_free (ctx.params);
6002 return ctx.list;
6005 char*
6006 mono_verify_corlib ()
6008 /* This is a public API function so cannot be removed */
6009 return NULL;
6013 * Returns true if @method needs to be verified.
6016 gboolean
6017 mono_verifier_is_enabled_for_method (MonoMethod *method)
6019 return mono_verifier_is_enabled_for_class (method->klass) && method->wrapper_type == MONO_WRAPPER_NONE;
6023 * Returns true if @klass need to be verified.
6026 gboolean
6027 mono_verifier_is_enabled_for_class (MonoClass *klass)
6029 return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !klass->image->assembly->in_gac && klass->image != mono_defaults.corlib);
6032 gboolean
6033 mono_verifier_is_enabled_for_image (MonoImage *image)
6035 return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
6038 gboolean
6039 mono_verifier_is_method_full_trust (MonoMethod *method)
6041 return mono_verifier_is_class_full_trust (method->klass);
6045 * Returns if @klass is under full trust or not.
6047 * TODO This code doesn't take CAS into account.
6049 * Under verify_all all user code must be verifiable if no security option was set
6052 gboolean
6053 mono_verifier_is_class_full_trust (MonoClass *klass)
6055 /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
6056 gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
6057 klass->image->assembly->in_gac : mono_security_core_clr_is_platform_image (klass->image);
6059 if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
6060 return trusted_location || klass->image == mono_defaults.corlib;
6061 return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
6064 GSList*
6065 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
6067 return mono_method_verify (method,
6068 (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
6069 | (!mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
6070 | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
6073 static int
6074 get_field_end (MonoClassField *field)
6076 int align;
6077 int size = mono_type_size (field->type, &align);
6078 if (size == 0)
6079 size = 4; /*FIXME Is this a safe bet?*/
6080 return size + field->offset;
6083 static gboolean
6084 verify_class_for_overlapping_reference_fields (MonoClass *class)
6086 int i, j;
6087 gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
6088 if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) || !class->has_references)
6089 return TRUE;
6091 //we must check for stuff overlapping reference fields
6092 for (i = 0; i < class->field.count; ++i) {
6093 MonoClassField *field = &class->fields [i];
6094 int fieldEnd = get_field_end (field);
6095 gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
6096 if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
6097 continue;
6099 for (j = i + 1; j < class->field.count; ++j) {
6100 MonoClassField *other = &class->fields [j];
6101 int otherEnd = get_field_end (other);
6102 if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
6103 continue;
6105 if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
6106 continue;
6108 if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
6109 return FALSE;
6112 return TRUE;
6115 static guint
6116 field_hash (gconstpointer key)
6118 const MonoClassField *field = key;
6119 return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
6122 static gboolean
6123 field_equals (gconstpointer _a, gconstpointer _b)
6125 const MonoClassField *a = _a;
6126 const MonoClassField *b = _b;
6127 return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
6131 static gboolean
6132 verify_class_fields (MonoClass *class)
6134 gpointer iter = NULL;
6135 MonoClassField *field;
6136 MonoGenericContext *context = mono_class_get_context (class);
6137 GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
6138 if (class->generic_container)
6139 context = &class->generic_container->context;
6141 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
6142 if (!mono_type_is_valid_type_in_context (field->type, context)) {
6143 g_hash_table_destroy (unique_fields);
6144 return FALSE;
6146 if (g_hash_table_lookup (unique_fields, field)) {
6147 g_hash_table_destroy (unique_fields);
6148 return FALSE;
6150 g_hash_table_insert (unique_fields, field, field);
6152 g_hash_table_destroy (unique_fields);
6153 return TRUE;
6156 static gboolean
6157 verify_interfaces (MonoClass *class)
6159 int i;
6160 for (i = 0; i < class->interface_count; ++i) {
6161 MonoClass *iface = class->interfaces [i];
6162 if (!(iface->flags & TYPE_ATTRIBUTE_INTERFACE))
6163 return FALSE;
6165 return TRUE;
6168 static gboolean
6169 verify_valuetype_layout_with_target (MonoClass *class, MonoClass *target_class)
6171 int type;
6172 gpointer iter = NULL;
6173 MonoClassField *field;
6174 MonoClass *field_class;
6176 if (!class->valuetype)
6177 return TRUE;
6179 type = class->byval_arg.type;
6180 /*primitive type fields are not properly decoded*/
6181 if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
6182 return TRUE;
6184 while ((field = mono_class_get_fields (class, &iter)) != NULL) {
6185 if (!field->type)
6186 return FALSE;
6188 if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
6189 continue;
6191 field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type (field->type));
6193 if (field_class == target_class || !verify_valuetype_layout_with_target (field_class, target_class))
6194 return FALSE;
6197 return TRUE;
6200 static gboolean
6201 verify_valuetype_layout (MonoClass *class)
6203 gboolean res;
6204 res = verify_valuetype_layout_with_target (class, class);
6205 return res;
6209 * Check if the class is verifiable.
6211 * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
6212 * field and invalid generic instantiation are fatal errors.
6214 * This method must be safe to be called from mono_class_init and all code must be carefull about that.
6217 gboolean
6218 mono_verifier_verify_class (MonoClass *class)
6220 if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
6221 return FALSE;
6222 if (!verify_class_for_overlapping_reference_fields (class))
6223 return FALSE;
6224 if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
6225 return FALSE;
6226 if (class->generic_class == NULL && !verify_class_fields (class))
6227 return FALSE;
6228 if (class->valuetype && !verify_valuetype_layout (class))
6229 return FALSE;
6230 if (!verify_interfaces (class))
6231 return FALSE;
6232 return TRUE;
6234 #else
6236 gboolean
6237 mono_verifier_verify_class (MonoClass *class)
6239 /* The verifier was disabled at compile time */
6240 return TRUE;
6243 GSList*
6244 mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
6246 /* The verifier was disabled at compile time */
6247 return NULL;
6250 gboolean
6251 mono_verifier_is_class_full_trust (MonoClass *klass)
6253 /* The verifier was disabled at compile time */
6254 return TRUE;
6257 gboolean
6258 mono_verifier_is_method_full_trust (MonoMethod *method)
6260 /* The verifier was disabled at compile time */
6261 return TRUE;
6264 gboolean
6265 mono_verifier_is_enabled_for_image (MonoImage *image)
6267 /* The verifier was disabled at compile time */
6268 return FALSE;
6271 gboolean
6272 mono_verifier_is_enabled_for_class (MonoClass *klass)
6274 /* The verifier was disabled at compile time */
6275 return FALSE;
6278 gboolean
6279 mono_verifier_is_enabled_for_method (MonoMethod *method)
6281 /* The verifier was disabled at compile time */
6282 return FALSE;
6285 GSList*
6286 mono_method_verify (MonoMethod *method, int level)
6288 /* The verifier was disabled at compile time */
6289 return NULL;
6292 void
6293 mono_free_verify_list (GSList *list)
6295 /* The verifier was disabled at compile time */
6296 /* will always be null if verifier is disabled */
6299 GSList*
6300 mono_image_verify_tables (MonoImage *image, int level)
6302 /* The verifier was disabled at compile time */
6303 return NULL;
6305 #endif