2 #include <mono/metadata/object-internals.h>
3 #include <mono/metadata/verify.h>
4 #include <mono/metadata/opcodes.h>
5 #include <mono/metadata/tabledefs.h>
6 #include <mono/metadata/reflection.h>
7 #include <mono/metadata/debug-helpers.h>
8 #include <mono/metadata/mono-endian.h>
9 #include <mono/metadata/metadata.h>
10 #include <mono/metadata/metadata-internals.h>
11 #include <mono/metadata/class-internals.h>
12 #include <mono/metadata/tokentype.h>
19 * Pull the list of opcodes
21 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
25 #include "mono/cil/opcode.def"
30 #ifdef MONO_VERIFIER_DEBUG
31 #define VERIFIER_DEBUG(code) do { code } while (0)
33 #define VERIFIER_DEBUG(code)
36 //////////////////////////////////////////////////////////////////
37 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
38 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
39 #define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
40 #define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
42 #define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
44 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
45 vinfo->info.status = __status; \
46 vinfo->info.message = ( __msg ); \
47 vinfo->exception_type = (__exception); \
48 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
51 #define ADD_VERIFY_ERROR(__ctx, __msg) \
53 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
57 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
59 if ((__ctx)->verifiable) { \
60 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
61 (__ctx)->verifiable = 0; \
62 if (IS_FAIL_FAST_MODE (__ctx)) \
67 #define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
69 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
73 #define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
75 if ((__ctx)->verifiable) { \
76 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
77 (__ctx)->verifiable = 0; \
78 if (IS_FAIL_FAST_MODE (__ctx)) \
82 /*Flags to be used with ILCodeDesc::flags */
84 /*Instruction has not been processed.*/
85 IL_CODE_FLAG_NOT_PROCESSED = 0,
86 /*Instruction was decoded by mono_method_verify loop.*/
87 IL_CODE_FLAG_SEEN = 1,
88 /*Instruction was target of a branch or is at a protected block boundary.*/
89 IL_CODE_FLAG_WAS_TARGET = 2,
90 /*Used by stack_init to avoid double initialize each entry.*/
91 IL_CODE_FLAG_STACK_INITED = 4,
92 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
93 IL_CODE_STACK_MERGED = 8,
94 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
95 IL_CODE_DELEGATE_SEQUENCE = 0x10,
96 /*This is a delegate created from a ldftn to a non final virtual method*/
97 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
98 /*This is a call to a non final virtual method*/
99 IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
128 /*Allocated fnptr MonoType that should be freed by us.*/
134 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
138 MonoMethodSignature *signature;
139 MonoMethodHeader *header;
141 MonoGenericContext *generic_context;
145 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
146 *on a method that creates a delegate for a non-final virtual method using ldftn*/
147 gboolean has_this_store;
151 MonoType *constrained_type;
155 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
158 get_stack_type (MonoType *type);
161 mono_delegate_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2);
162 //////////////////////////////////////////////////////////////////
167 TYPE_INV = 0, /* leave at 0. */
172 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
174 /* value types and classes */
176 /* Number of types, used to define the size of the tables*/
179 /* Used by tables to signal that a result is not verifiable*/
180 NON_VERIFIABLE_RESULT = 0x80,
182 /*Mask used to extract just the type, excluding flags */
185 /* The stack type is a managed pointer, unmask the value to res */
186 POINTER_MASK = 0x100,
188 /*Stack type with the pointer mask*/
189 RAW_TYPE_MASK = 0x10F,
191 /* Controlled Mutability Manager Pointer */
194 /* The stack type is a null literal*/
195 NULL_LITERAL_MASK = 0x400,
197 /**Used by ldarg.0 and family to let delegate verification happens.*/
198 THIS_POINTER_MASK = 0x800,
200 /**Signals that this is a boxed value type*/
205 static const char* const
206 type_names [TYPE_MAX + 1] = {
217 PREFIX_UNALIGNED = 1,
220 PREFIX_CONSTRAINED = 8,
223 //////////////////////////////////////////////////////////////////
226 /*Token validation macros and functions */
227 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
228 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
229 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
230 #define IS_FIELD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_FIELD)
232 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
233 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
234 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
235 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
236 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
237 #define IS_FIELD_DEF_OR_REF(token) (IS_FIELD_DEF (token) || IS_MEMBER_REF (token))
240 * Verify if @token refers to a valid row on int's table.
243 token_bounds_check (MonoImage *image, guint32 token)
246 return mono_reflection_is_valid_dynamic_token ((MonoDynamicImage*)image, token);
247 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
251 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
253 MonoType *res = g_new0 (MonoType, 1);
254 //FIXME use mono_method_get_signature_full
255 res->data.method = mono_method_signature (method);
256 res->type = MONO_TYPE_FNPTR;
257 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
262 * mono_type_is_enum_type:
264 * Returns TRUE if @type is an enum type.
267 mono_type_is_enum_type (MonoType *type)
269 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
271 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
277 * mono_type_is_value_type:
279 * Returns TRUE if @type is named after @namespace.@name.
283 mono_type_is_value_type (MonoType *type, const char *namespace, const char *name)
285 return type->type == MONO_TYPE_VALUETYPE &&
286 !strcmp (namespace, type->data.klass->name_space) &&
287 !strcmp (name, type->data.klass->name);
290 * mono_type_get_underlying_type_any:
292 * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
294 * Returns the underlying type of @type regardless if it is byref or not.
297 mono_type_get_underlying_type_any (MonoType *type)
299 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
300 return type->data.klass->enum_basetype;
301 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
302 return type->data.generic_class->container_class->enum_basetype;
307 mono_type_get_stack_name (MonoType *type)
309 return type_names [get_stack_type (type) & TYPE_MASK];
312 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
313 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
316 mono_method_is_constructor (MonoMethod *method)
318 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
319 !(method->flags & CTOR_INVALID_FLAGS) &&
320 !strcmp (".ctor", method->name));
324 verify_type_load_error(VerifyContext *ctx, MonoClass *klass)
326 mono_class_init (klass);
327 if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
328 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);
332 //TODO verify if the type can be inflated in the current context
336 static MonoClassField*
337 verifier_load_field (VerifyContext *ctx, int token, MonoClass **klass, const char *opcode) {
338 MonoClassField *field;
340 if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
341 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid field token 0x%x08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
345 field = mono_field_from_token (ctx->image, token, klass, ctx->generic_context);
347 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);
351 if (!verify_type_load_error (ctx, field->parent))
358 verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
361 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
362 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);
366 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
369 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);
373 if (!verify_type_load_error (ctx, method->klass))
376 //TODO verify if the method can be inflated in the current context
381 verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
384 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
385 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
389 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
392 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);
396 if (!verify_type_load_error (ctx, mono_class_from_mono_type (type)))
403 /* stack_slot_get_type:
405 * Returns the stack type of @value. This value includes POINTER_MASK.
407 * Use this function to checks that account for a managed pointer.
410 stack_slot_get_type (ILStackDesc *value)
412 return value->stype & RAW_TYPE_MASK;
415 /* stack_slot_get_underlying_type:
417 * Returns the stack type of @value. This value does not include POINTER_MASK.
419 * Use this function is cases where the fact that the value could be a managed pointer is
420 * irrelevant. For example, field load doesn't care about this fact of type on stack.
423 stack_slot_get_underlying_type (ILStackDesc *value)
425 return value->stype & TYPE_MASK;
428 /* stack_slot_is_managed_pointer:
430 * Returns TRUE is @value is a managed pointer.
433 stack_slot_is_managed_pointer (ILStackDesc *value)
435 return (value->stype & POINTER_MASK) == POINTER_MASK;
438 /* stack_slot_is_managed_mutability_pointer:
440 * Returns TRUE is @value is a managed mutability pointer.
442 static G_GNUC_UNUSED gboolean
443 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
445 return (value->stype & CMMP_MASK) == CMMP_MASK;
448 /* stack_slot_is_null_literal:
450 * Returns TRUE is @value is the null literal.
453 stack_slot_is_null_literal (ILStackDesc *value)
455 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
459 /* stack_slot_is_this_pointer:
461 * Returns TRUE is @value is the this literal
464 stack_slot_is_this_pointer (ILStackDesc *value)
466 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
469 /* stack_slot_is_boxed_value:
471 * Returns TRUE is @value is a boxed value
474 stack_slot_is_boxed_value (ILStackDesc *value)
476 return (value->stype & BOXED_MASK) == BOXED_MASK;
480 stack_slot_get_name (ILStackDesc *value)
482 return type_names [value->stype & TYPE_MASK];
484 //////////////////////////////////////////////////////////////////
486 mono_free_verify_list (GSList *list)
488 MonoVerifyInfoExtended *info;
491 for (tmp = list; tmp; tmp = tmp->next) {
493 g_free (info->info.message);
499 #define ADD_ERROR(list,msg) \
501 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
502 vinfo->info.status = MONO_VERIFY_ERROR; \
503 vinfo->info.message = (msg); \
504 (list) = g_slist_prepend ((list), vinfo); \
507 #define ADD_WARN(list,code,msg) \
509 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
510 vinfo->info.status = (code); \
511 vinfo->info.message = (msg); \
512 (list) = g_slist_prepend ((list), vinfo); \
516 valid_cultures[][9] = {
517 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
518 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
519 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
520 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
521 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
522 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
523 "da-DK", "de-DE", "de-CH", "de-AT",
524 "de-LU", "de-LI", "el-GR", "en-US",
525 "en-GB", "en-AU", "en-CA", "en-NZ",
526 "en-IE", "en-ZA", "en-JM", "en-CB",
527 "en-BZ", "en-TT", "en-ZW", "en-PH",
528 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
529 "es-CR", "es-PA", "es-DO", "es-VE",
530 "es-CO", "es-PE", "es-AR", "es-EC",
531 "es-CL", "es-UY", "es-PY", "es-BO",
532 "es-SV", "es-HN", "es-NI", "es-PR",
533 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
534 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
535 "hu-HU", "is-IS", "it-IT", "it-CH",
536 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
537 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
538 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
539 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
540 "sv-SE", "sv-FI", "th-TH", "tr-TR",
541 "ur-PK", "id-ID", "uk-UA", "be-BY",
542 "sl-SI", "et-EE", "lv-LV", "lt-LT",
543 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
545 "eu-ES", "mk-MK", "af-ZA",
546 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
547 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
548 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
549 "gu-IN", "ta-IN", "te-IN", "kn-IN",
550 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
551 "kok-IN", "syr-SY", "div-MV"
555 is_valid_culture (const char *cname)
561 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
562 if (g_strcasecmp (valid_cultures [i], cname)) {
571 is_valid_assembly_flags (guint32 flags) {
572 /* Metadata: 22.1.2 */
573 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
574 return ((flags == 1) || (flags == 0));
578 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
581 const char *p, *blob_end;
583 if (blob_index >= image->heap_blob.size)
585 p = mono_metadata_blob_heap (image, blob_index);
586 size = mono_metadata_decode_blob_size (p, &blob_end);
587 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
589 if (notnull && !size)
595 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
597 const char *p, *blob_end, *res;
599 if (str_index >= image->heap_strings.size)
601 res = p = mono_metadata_string_heap (image, str_index);
602 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
606 * FIXME: should check it's a valid utf8 string, too.
608 while (p <= blob_end) {
613 return *p? NULL: res;
617 is_valid_cls_ident (const char *p)
620 * FIXME: we need the full unicode glib support for this.
621 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
622 * We do the lame thing for now.
628 if (!isalnum (*p) && *p != '_')
636 is_valid_filename (const char *p)
640 return strpbrk (p, "\\//:")? 0: 1;
644 verify_assembly_table (MonoImage *image, GSList *list, int level)
646 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
647 guint32 cols [MONO_ASSEMBLY_SIZE];
650 if (level & MONO_VERIFY_ERROR) {
652 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
653 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
655 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
656 case ASSEMBLY_HASH_NONE:
657 case ASSEMBLY_HASH_MD5:
658 case ASSEMBLY_HASH_SHA1:
661 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
664 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
665 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
667 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
668 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
670 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
671 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
673 if (strpbrk (p, ":\\/."))
674 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
677 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
678 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
680 if (!is_valid_culture (p))
681 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
688 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
690 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
691 guint32 cols [MONO_ASSEMBLYREF_SIZE];
695 if (level & MONO_VERIFY_ERROR) {
696 for (i = 0; i < t->rows; ++i) {
697 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
698 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
699 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
701 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
702 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
704 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
705 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
707 if (!is_valid_culture (p))
708 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
711 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
712 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
715 if (level & MONO_VERIFY_WARNING) {
716 /* check for duplicated rows */
717 for (i = 0; i < t->rows; ++i) {
724 verify_class_layout_table (MonoImage *image, GSList *list, int level)
726 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
727 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
728 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
731 if (level & MONO_VERIFY_ERROR) {
732 for (i = 0; i < t->rows; ++i) {
733 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
735 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
736 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
738 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
739 if (value & TYPE_ATTRIBUTE_INTERFACE)
740 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
741 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
742 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
743 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
744 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
745 case 0: case 1: case 2: case 4: case 8: case 16:
746 case 32: case 64: case 128: break;
748 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
750 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
752 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
753 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
754 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));
758 * FIXME: we need to check that if class size != 0,
759 * it needs to be greater than the class calculated size.
760 * If parent is a valuetype it also needs to be smaller than
761 * 1 MByte (0x100000 bytes).
762 * To do both these checks we need to load the referenced
763 * assemblies, though (the spec claims we didn't have to, bah).
766 * We need to check that the parent types have the same layout
777 verify_constant_table (MonoImage *image, GSList *list, int level)
779 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
780 guint32 cols [MONO_CONSTANT_SIZE];
782 GHashTable *dups = g_hash_table_new (NULL, NULL);
784 for (i = 0; i < t->rows; ++i) {
785 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
787 if (level & MONO_VERIFY_ERROR)
788 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
789 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
790 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
791 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
793 switch (cols [MONO_CONSTANT_TYPE]) {
794 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
798 if (level & MONO_VERIFY_CLS)
799 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));
800 case MONO_TYPE_BOOLEAN:
808 case MONO_TYPE_STRING:
809 case MONO_TYPE_CLASS:
812 if (level & MONO_VERIFY_ERROR)
813 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
815 if (level & MONO_VERIFY_ERROR) {
816 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
817 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
818 case MONO_HASCONSTANT_FIEDDEF:
819 if (value > image->tables [MONO_TABLE_FIELD].rows)
820 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
822 case MONO_HASCONSTANT_PARAM:
823 if (value > image->tables [MONO_TABLE_PARAM].rows)
824 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
826 case MONO_HASCONSTANT_PROPERTY:
827 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
828 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
831 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
835 if (level & MONO_VERIFY_CLS) {
837 * FIXME: verify types is consistent with the enum type
838 * is parent is an enum.
842 g_hash_table_destroy (dups);
847 verify_event_map_table (MonoImage *image, GSList *list, int level)
849 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
850 guint32 cols [MONO_EVENT_MAP_SIZE];
851 guint32 i, last_event;
852 GHashTable *dups = g_hash_table_new (NULL, NULL);
856 for (i = 0; i < t->rows; ++i) {
857 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
858 if (level & MONO_VERIFY_ERROR)
859 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
860 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
861 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
862 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
863 if (level & MONO_VERIFY_ERROR) {
864 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
865 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
866 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
867 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
869 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
870 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
871 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
875 g_hash_table_destroy (dups);
880 verify_event_table (MonoImage *image, GSList *list, int level)
882 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
883 guint32 cols [MONO_EVENT_SIZE];
887 for (i = 0; i < t->rows; ++i) {
888 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
890 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
891 if (level & MONO_VERIFY_ERROR)
892 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
894 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
895 if (level & MONO_VERIFY_ERROR)
896 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
898 if (level & MONO_VERIFY_CLS) {
899 if (!is_valid_cls_ident (p))
900 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
904 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
905 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
906 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
907 case MONO_TYPEDEFORREF_TYPEDEF:
908 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
909 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
911 case MONO_TYPEDEFORREF_TYPEREF:
912 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
913 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
915 case MONO_TYPEDEFORREF_TYPESPEC:
916 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
917 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
920 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
924 * FIXME: check that there is 1 add and remove row in methodsemantics
925 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
926 * these while checking methodsemantics).
927 * check for duplicated names for the same type [ERROR]
928 * check for CLS duplicate names for the same type [CLS]
935 verify_field_table (MonoImage *image, GSList *list, int level)
937 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
938 guint32 cols [MONO_FIELD_SIZE];
942 for (i = 0; i < t->rows; ++i) {
943 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
945 * Check this field has only one owner and that the owner is not
946 * an interface (done in verify_typedef_table() )
948 flags = cols [MONO_FIELD_FLAGS];
949 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
950 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
951 case FIELD_ATTRIBUTE_PRIVATE:
952 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
953 case FIELD_ATTRIBUTE_ASSEMBLY:
954 case FIELD_ATTRIBUTE_FAMILY:
955 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
956 case FIELD_ATTRIBUTE_PUBLIC:
959 if (level & MONO_VERIFY_ERROR)
960 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
963 if (level & MONO_VERIFY_ERROR) {
964 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
965 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
966 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
967 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
968 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
969 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
971 * FIXME: check there is only one owner in the respective table.
972 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
973 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
974 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
977 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
978 if (level & MONO_VERIFY_ERROR)
979 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
981 if (level & MONO_VERIFY_CLS) {
982 if (!is_valid_cls_ident (p))
983 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
988 * if owner is module needs to be static, access mask needs to be compilercontrolled,
989 * public or private (not allowed in cls mode).
990 * if owner is an enum ...
999 verify_file_table (MonoImage *image, GSList *list, int level)
1001 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
1002 guint32 cols [MONO_FILE_SIZE];
1005 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1007 for (i = 0; i < t->rows; ++i) {
1008 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
1009 if (level & MONO_VERIFY_ERROR) {
1010 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
1011 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
1012 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
1013 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
1015 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
1016 if (level & MONO_VERIFY_ERROR)
1017 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
1019 if (level & MONO_VERIFY_ERROR) {
1020 if (!is_valid_filename (p))
1021 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
1022 else if (g_hash_table_lookup (dups, p)) {
1023 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
1025 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1029 * FIXME: I don't understand what this means:
1030 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
1031 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
1035 if (level & MONO_VERIFY_WARNING) {
1036 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
1037 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
1039 g_hash_table_destroy (dups);
1044 verify_moduleref_table (MonoImage *image, GSList *list, int level)
1046 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
1047 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
1048 guint32 cols [MONO_MODULEREF_SIZE];
1050 guint32 found, i, j, value;
1051 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
1053 for (i = 0; i < t->rows; ++i) {
1054 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
1055 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
1056 if (level & MONO_VERIFY_ERROR)
1057 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
1059 if (level & MONO_VERIFY_ERROR) {
1060 if (!is_valid_filename (p))
1061 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
1062 else if (g_hash_table_lookup (dups, p)) {
1063 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
1064 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
1066 for (j = 0; j < tfile->rows; ++j) {
1067 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
1068 if ((pf = is_valid_string (image, value, TRUE)))
1069 if (strcmp (p, pf) == 0) {
1075 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
1080 g_hash_table_destroy (dups);
1085 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
1087 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
1088 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1092 for (i = 0; i < t->rows; ++i) {
1093 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1094 if (level & MONO_VERIFY_ERROR) {
1095 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
1096 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
1098 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
1099 /* FIXME: check it's a valid locals or method sig.*/
1107 mono_image_verify_tables (MonoImage *image, int level)
1109 GSList *error_list = NULL;
1111 error_list = verify_assembly_table (image, error_list, level);
1113 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
1114 * AssemblyRefProcessor should be ignored,
1115 * though we may want to emit a warning, since it should not
1116 * be present in a PE file.
1118 error_list = verify_assemblyref_table (image, error_list, level);
1119 error_list = verify_class_layout_table (image, error_list, level);
1120 error_list = verify_constant_table (image, error_list, level);
1122 * cutom attribute, declsecurity
1124 error_list = verify_event_map_table (image, error_list, level);
1125 error_list = verify_event_table (image, error_list, level);
1126 error_list = verify_field_table (image, error_list, level);
1127 error_list = verify_file_table (image, error_list, level);
1128 error_list = verify_moduleref_table (image, error_list, level);
1129 error_list = verify_standalonesig_table (image, error_list, level);
1131 return g_slist_reverse (error_list);
1134 #define ADD_INVALID(list,msg) \
1136 MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
1137 vinfo->status = MONO_VERIFY_ERROR; \
1138 vinfo->message = (msg); \
1139 (list) = g_slist_prepend ((list), vinfo); \
1140 /*G_BREAKPOINT ();*/ \
1144 #define CHECK_STACK_UNDERFLOW(num) \
1146 if (cur_stack < (num)) \
1147 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
1150 #define CHECK_STACK_OVERFLOW() \
1152 if (cur_stack >= max_stack) \
1153 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
1158 in_any_block (MonoMethodHeader *header, guint offset)
1161 MonoExceptionClause *clause;
1163 for (i = 0; i < header->num_clauses; ++i) {
1164 clause = &header->clauses [i];
1165 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1167 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1169 if (MONO_OFFSET_IN_FILTER (clause, offset))
1176 * in_any_exception_block:
1178 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1181 in_any_exception_block (MonoMethodHeader *header, guint offset)
1184 MonoExceptionClause *clause;
1186 for (i = 0; i < header->num_clauses; ++i) {
1187 clause = &header->clauses [i];
1188 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1190 if (MONO_OFFSET_IN_FILTER (clause, offset))
1197 * is_valid_branch_instruction:
1199 * Verify if it's valid to perform a branch from @offset to @target.
1200 * This should be used with br and brtrue/false.
1201 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1202 * The major diferent from other similiar functions is that branching into a
1203 * finally/fault block is invalid instead of just unverifiable.
1206 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1209 MonoExceptionClause *clause;
1211 for (i = 0; i < header->num_clauses; ++i) {
1212 clause = &header->clauses [i];
1213 /*branching into a finally block is invalid*/
1214 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1215 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1216 MONO_OFFSET_IN_HANDLER (clause, target))
1219 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1221 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1223 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1230 * is_valid_cmp_branch_instruction:
1232 * Verify if it's valid to perform a branch from @offset to @target.
1233 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1234 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1236 * The major diferences from other similar functions are that most errors lead to invalid
1237 * code and only branching out of finally, filter or fault clauses is unverifiable.
1240 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1243 MonoExceptionClause *clause;
1245 for (i = 0; i < header->num_clauses; ++i) {
1246 clause = &header->clauses [i];
1247 /*branching out of a handler or finally*/
1248 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1249 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1250 !MONO_OFFSET_IN_HANDLER (clause, target))
1253 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1255 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1257 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1264 * A leave can't escape a finally block
1267 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1270 MonoExceptionClause *clause;
1272 for (i = 0; i < header->num_clauses; ++i) {
1273 clause = &header->clauses [i];
1274 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1276 if (MONO_OFFSET_IN_FILTER (clause, offset))
1283 * A rethrow can't happen outside of a catch handler.
1286 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1289 MonoExceptionClause *clause;
1291 for (i = 0; i < header->num_clauses; ++i) {
1292 clause = &header->clauses [i];
1293 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1295 if (MONO_OFFSET_IN_FILTER (clause, offset))
1302 * An endfinally can't happen outside of a finally/fault handler.
1305 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1308 MonoExceptionClause *clause;
1310 for (i = 0; i < header->num_clauses; ++i) {
1311 clause = &header->clauses [i];
1312 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1320 * An endfilter can only happens inside a filter clause.
1321 * In non-strict mode filter is allowed inside the handler clause too
1323 static MonoExceptionClause *
1324 is_correct_endfilter (VerifyContext *ctx, guint offset)
1327 MonoExceptionClause *clause;
1329 for (i = 0; i < ctx->header->num_clauses; ++i) {
1330 clause = &ctx->header->clauses [i];
1331 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1333 if (MONO_OFFSET_IN_FILTER (clause, offset))
1335 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1343 * Non-strict endfilter can happens inside a try block or any handler block
1346 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1349 MonoExceptionClause *clause;
1351 for (i = 0; i < ctx->header->num_clauses; ++i) {
1352 clause = &ctx->header->clauses [i];
1353 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1360 is_valid_bool_arg (ILStackDesc *arg)
1362 if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
1366 switch (stack_slot_get_underlying_type (arg)) {
1369 case TYPE_NATIVE_INT:
1373 g_assert (arg->type);
1374 switch (arg->type->type) {
1375 case MONO_TYPE_CLASS:
1376 case MONO_TYPE_STRING:
1377 case MONO_TYPE_OBJECT:
1378 case MONO_TYPE_SZARRAY:
1379 case MONO_TYPE_ARRAY:
1380 case MONO_TYPE_FNPTR:
1383 case MONO_TYPE_GENERICINST:
1384 /*We need to check if the container class
1385 * of the generic type is a valuetype, iow:
1386 * is it a "class Foo<T>" or a "struct Foo<T>"?
1388 return !arg->type->data.generic_class->container_class->valuetype;
1396 /*Type manipulation helper*/
1398 /*Returns the byref version of the supplied MonoType*/
1400 mono_type_get_type_byref (MonoType *type)
1404 return &mono_class_from_mono_type (type)->this_arg;
1408 /*Returns the byval version of the supplied MonoType*/
1410 mono_type_get_type_byval (MonoType *type)
1414 return &mono_class_from_mono_type (type)->byval_arg;
1418 mono_type_from_stack_slot (ILStackDesc *slot)
1420 if (stack_slot_is_managed_pointer (slot))
1421 return mono_type_get_type_byref (slot->type);
1425 /*Stack manipulation code*/
1428 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1430 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1433 state->flags |= IL_CODE_FLAG_STACK_INITED;
1435 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1439 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1441 to->size = from->size;
1442 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1446 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1448 to->stype = from->stype;
1449 to->type = from->type;
1450 to->method = from->method;
1454 check_underflow (VerifyContext *ctx, int size)
1456 if (ctx->eval.size < size) {
1457 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1464 check_overflow (VerifyContext *ctx)
1466 if (ctx->eval.size >= ctx->max_stack) {
1467 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1474 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1476 if (stack_slot_get_type (value) == TYPE_PTR) {
1477 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1484 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1486 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1487 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1494 static ILStackDesc *
1495 stack_push (VerifyContext *ctx)
1497 return & ctx->eval.stack [ctx->eval.size++];
1500 static ILStackDesc *
1501 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1503 ILStackDesc *top = stack_push (ctx);
1509 static ILStackDesc *
1510 stack_pop (VerifyContext *ctx)
1512 return ctx->eval.stack + --ctx->eval.size;
1515 static inline ILStackDesc *
1516 stack_top (VerifyContext *ctx)
1518 return ctx->eval.stack + (ctx->eval.size - 1);
1521 static inline ILStackDesc *
1522 stack_get (VerifyContext *ctx, int distance)
1524 return ctx->eval.stack + (ctx->eval.size - distance - 1);
1527 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1529 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1532 get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
1537 if (!(type = verifier_load_type (ctx, token, opcode)))
1540 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1541 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
1545 if (type->type == MONO_TYPE_VOID) {
1546 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
1550 if (type->type == MONO_TYPE_TYPEDBYREF)
1551 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
1553 check_unverifiable_type (ctx, type);
1558 /*operation result tables */
1560 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1561 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1562 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1563 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1564 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1565 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1566 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1569 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1570 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1571 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1572 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1573 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1574 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1575 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1578 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1579 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1580 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1581 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1582 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1583 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1584 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1587 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1588 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1589 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1590 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1591 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1592 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1593 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1596 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1597 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1598 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1599 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1600 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1601 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1602 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1605 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1606 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1607 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1608 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1609 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1610 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1611 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1614 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1615 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1616 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1617 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1618 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1619 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1620 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1623 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1624 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1625 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1626 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1627 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1628 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1629 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1632 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1633 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1634 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1635 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1636 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1637 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1638 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1641 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1642 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1643 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1644 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1645 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1646 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1647 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1650 #ifdef MONO_VERIFIER_DEBUG
1654 dump_stack_value (ILStackDesc *value)
1656 printf ("[(%x)(%x)", value->type->type, value->stype);
1658 if (stack_slot_is_this_pointer (value))
1661 if (stack_slot_is_boxed_value (value))
1662 printf ("[boxed] ");
1664 if (stack_slot_is_null_literal (value))
1667 if (stack_slot_is_managed_mutability_pointer (value))
1668 printf ("Controled Mutability MP: ");
1670 if (stack_slot_is_managed_pointer (value))
1671 printf ("Managed Pointer to: ");
1673 switch (stack_slot_get_underlying_type (value)) {
1675 printf ("invalid type]");
1683 case TYPE_NATIVE_INT:
1684 printf ("native int]");
1687 printf ("float64]");
1690 printf ("unmanaged pointer]");
1693 switch (value->type->type) {
1694 case MONO_TYPE_CLASS:
1695 case MONO_TYPE_VALUETYPE:
1696 printf ("complex] (%s)", value->type->data.klass->name);
1698 case MONO_TYPE_STRING:
1699 printf ("complex] (string)");
1701 case MONO_TYPE_OBJECT:
1702 printf ("complex] (object)");
1704 case MONO_TYPE_SZARRAY:
1705 printf ("complex] (%s [])", value->type->data.klass->name);
1707 case MONO_TYPE_ARRAY:
1708 printf ("complex] (%s [%d %d %d])",
1709 value->type->data.array->eklass->name,
1710 value->type->data.array->rank,
1711 value->type->data.array->numsizes,
1712 value->type->data.array->numlobounds);
1714 case MONO_TYPE_GENERICINST:
1715 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1718 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1720 case MONO_TYPE_MVAR:
1721 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1724 //should be a boxed value
1725 char * name = mono_type_full_name (value->type);
1726 printf ("complex] %s", name);
1732 printf ("unknown stack %x type]\n", value->stype);
1733 g_assert_not_reached ();
1738 dump_stack_state (ILCodeDesc *state)
1742 printf ("(%d) ", state->size);
1743 for (i = 0; i < state->size; ++i)
1744 dump_stack_value (state->stack + i);
1749 /*Returns TRUE if candidate array type can be assigned to target.
1750 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1753 is_array_type_compatible (MonoType *target, MonoType *candidate)
1756 MonoArrayType *left = target->data.array;
1757 MonoArrayType *right = candidate->data.array;
1759 g_assert (target->type == MONO_TYPE_ARRAY);
1760 g_assert (candidate->type == MONO_TYPE_ARRAY);
1763 if ((left->rank != right->rank) ||
1764 (left->numsizes != right->numsizes) ||
1765 (left->numlobounds != right->numlobounds))
1768 for (i = 0; i < left->numsizes; ++i)
1769 if (left->sizes [i] != right->sizes [i])
1772 for (i = 0; i < left->numlobounds; ++i)
1773 if (left->lobounds [i] != right->lobounds [i])
1776 return mono_class_is_assignable_from (left->eklass, right->eklass);
1780 get_stack_type (MonoType *type)
1783 int type_kind = type->type;
1785 mask = POINTER_MASK;
1786 /*TODO handle CMMP_MASK */
1789 switch (type_kind) {
1792 case MONO_TYPE_BOOLEAN:
1795 case MONO_TYPE_CHAR:
1798 return TYPE_I4 | mask;
1802 return TYPE_NATIVE_INT | mask;
1804 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1805 case MONO_TYPE_FNPTR:
1807 case MONO_TYPE_TYPEDBYREF:
1808 return TYPE_PTR | mask;
1811 case MONO_TYPE_MVAR:
1813 case MONO_TYPE_CLASS:
1814 case MONO_TYPE_STRING:
1815 case MONO_TYPE_OBJECT:
1816 case MONO_TYPE_SZARRAY:
1817 case MONO_TYPE_ARRAY:
1818 return TYPE_COMPLEX | mask;
1820 case MONO_TYPE_GENERICINST:
1821 if (mono_type_is_enum_type (type)) {
1822 type = mono_type_get_underlying_type_any (type);
1823 type_kind = type->type;
1826 return TYPE_COMPLEX | mask;
1831 return TYPE_I8 | mask;
1835 return TYPE_R8 | mask;
1837 case MONO_TYPE_VALUETYPE:
1838 if (mono_type_is_enum_type (type)) {
1839 type = mono_type_get_underlying_type_any (type);
1840 type_kind = type->type;
1843 return TYPE_COMPLEX | mask;
1847 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
1848 g_assert_not_reached ();
1853 /* convert MonoType to ILStackDesc format (stype) */
1855 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1858 int type_kind = type->type;
1860 if (type->byref || take_addr)
1861 mask = POINTER_MASK;
1862 /* TODO handle CMMP_MASK */
1867 switch (type_kind) {
1870 case MONO_TYPE_BOOLEAN:
1873 case MONO_TYPE_CHAR:
1876 stack->stype = TYPE_I4 | mask;
1880 stack->stype = TYPE_NATIVE_INT | mask;
1883 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1884 case MONO_TYPE_FNPTR:
1886 case MONO_TYPE_TYPEDBYREF:
1887 stack->stype = TYPE_PTR | mask;
1890 case MONO_TYPE_CLASS:
1891 case MONO_TYPE_STRING:
1892 case MONO_TYPE_OBJECT:
1893 case MONO_TYPE_SZARRAY:
1894 case MONO_TYPE_ARRAY:
1897 case MONO_TYPE_MVAR:
1898 stack->stype = TYPE_COMPLEX | mask;
1901 case MONO_TYPE_GENERICINST:
1902 if (mono_type_is_enum_type (type)) {
1903 type = mono_type_get_underlying_type_any (type);
1904 type_kind = type->type;
1907 stack->stype = TYPE_COMPLEX | mask;
1913 stack->stype = TYPE_I8 | mask;
1917 stack->stype = TYPE_R8 | mask;
1919 case MONO_TYPE_VALUETYPE:
1920 if (mono_type_is_enum_type (type)) {
1921 type = mono_type_get_underlying_type_any (type);
1922 type_kind = type->type;
1925 stack->stype = TYPE_COMPLEX | mask;
1929 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1930 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
1937 * init_stack_with_value_at_exception_boundary:
1939 * Initialize the stack and push a given type.
1940 * The instruction is marked as been on the exception boundary.
1943 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
1945 stack_init (ctx, code);
1946 set_stack_value (ctx, code->stack, &klass->byval_arg, FALSE);
1948 code->flags |= IL_CODE_FLAG_WAS_TARGET;
1951 /*Verify if type 'candidate' can be stored in type 'target'.
1953 * If strict, check for the underlying type and not the verification stack types
1956 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1958 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1959 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1961 MonoType *original_candidate = candidate;
1962 VERIFIER_DEBUG ( printf ("checking type compatibility %p %p[%x][%x] %p[%x][%x]\n", ctx, target, target->type, target->byref, candidate, candidate->type, candidate->byref); );
1964 /*only one is byref */
1965 if (candidate->byref ^ target->byref) {
1966 /* converting from native int to byref*/
1967 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1968 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1973 strict |= target->byref;
1974 /*From now on we don't care about byref anymore, so it's ok to discard it here*/
1975 candidate = mono_type_get_underlying_type_any (candidate);
1978 switch (target->type) {
1979 case MONO_TYPE_VOID:
1980 return candidate->type == MONO_TYPE_VOID;
1983 case MONO_TYPE_BOOLEAN:
1985 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1988 case MONO_TYPE_CHAR:
1990 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1992 case MONO_TYPE_U4: {
1993 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1994 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1996 return is_native_int || is_int4;
1997 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2002 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2007 return candidate->type == target->type;
2008 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2012 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2013 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2015 return is_native_int || is_int4;
2016 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2020 if (!IS_STRICT_MODE (ctx) && IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U))
2022 if (candidate->type != MONO_TYPE_PTR)
2024 /* check the underlying type */
2025 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2027 case MONO_TYPE_FNPTR: {
2028 MonoMethodSignature *left, *right;
2029 if (candidate->type != MONO_TYPE_FNPTR)
2032 left = mono_type_get_signature (target);
2033 right = mono_type_get_signature (candidate);
2034 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2037 case MONO_TYPE_GENERICINST: {
2038 if (mono_type_is_enum_type (target)) {
2039 target = mono_type_get_underlying_type_any (target);
2042 return mono_class_is_assignable_from (mono_class_from_mono_type (target), mono_class_from_mono_type (candidate));
2045 case MONO_TYPE_STRING:
2046 return candidate->type == MONO_TYPE_STRING;
2048 case MONO_TYPE_CLASS:
2049 /* If candidate is an enum it should return true for System.Enum and supertypes.
2050 * That's why here we use the original type and not the underlying type.
2052 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (original_candidate));
2054 case MONO_TYPE_OBJECT:
2055 return MONO_TYPE_IS_REFERENCE (candidate);
2057 case MONO_TYPE_SZARRAY: {
2060 if (candidate->type != MONO_TYPE_SZARRAY)
2063 left = target->data.klass;
2064 right = candidate->data.klass;
2065 return mono_class_is_assignable_from(left, right);
2068 case MONO_TYPE_ARRAY:
2069 if (candidate->type != MONO_TYPE_ARRAY)
2071 return is_array_type_compatible (target, candidate);
2073 case MONO_TYPE_TYPEDBYREF:
2074 return candidate->type == MONO_TYPE_TYPEDBYREF;
2076 case MONO_TYPE_VALUETYPE:
2077 if (candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass)
2079 if (mono_type_is_enum_type (target)) {
2080 target = mono_type_get_underlying_type_any (target);
2086 if (candidate->type != MONO_TYPE_VAR)
2088 return candidate->data.generic_param->num == target->data.generic_param->num;
2090 case MONO_TYPE_MVAR:
2091 if (candidate->type != MONO_TYPE_MVAR)
2093 return candidate->data.generic_param->num == target->data.generic_param->num;
2096 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2097 g_assert_not_reached ();
2106 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2108 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2112 * is_compatible_boxed_valuetype:
2114 * Returns TRUE if @candidate / @stack is a valid boxed valuetype.
2116 * @type The source type. It it tested to be of the proper type.
2117 * @candidate type of the boxed valuetype.
2118 * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
2119 * @type_must_be_object if TRUE @type must be System.Object, otherwise can be any reference type.
2123 is_compatible_boxed_valuetype (MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean type_must_be_object)
2125 if (type_must_be_object && type->type != MONO_TYPE_OBJECT)
2127 if (!type_must_be_object && !MONO_TYPE_IS_REFERENCE (type))
2129 return !type->byref && !candidate->byref && stack_slot_is_boxed_value (stack);
2133 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2135 MonoType *candidate = mono_type_from_stack_slot (stack);
2136 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2139 if (is_compatible_boxed_valuetype (type, candidate, stack, TRUE))
2142 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2146 mono_delegate_type_equal (MonoType *target, MonoType *candidate)
2148 if (candidate->byref ^ target->byref)
2151 switch (target->type) {
2152 case MONO_TYPE_VOID:
2155 case MONO_TYPE_BOOLEAN:
2158 case MONO_TYPE_CHAR:
2167 case MONO_TYPE_STRING:
2168 case MONO_TYPE_TYPEDBYREF:
2169 return candidate->type == target->type;
2172 return mono_delegate_type_equal (target->data.type, candidate->data.type);
2174 case MONO_TYPE_FNPTR:
2175 if (candidate->type != MONO_TYPE_FNPTR)
2177 return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate));
2179 case MONO_TYPE_GENERICINST:
2181 g_assert_not_reached ();
2184 return candidate->type == MONO_TYPE_STRING;
2186 case MONO_TYPE_OBJECT:
2187 return MONO_TYPE_IS_REFERENCE (candidate);
2189 case MONO_TYPE_CLASS:
2190 if (candidate->type != MONO_TYPE_CLASS)
2192 return mono_class_is_assignable_from(target->data.klass, candidate->data.klass);
2194 case MONO_TYPE_SZARRAY:
2195 if (candidate->type != MONO_TYPE_SZARRAY)
2197 return mono_class_is_assignable_from (target->data.array->eklass, candidate->data.array->eklass);
2199 case MONO_TYPE_ARRAY:
2200 if (candidate->type != MONO_TYPE_ARRAY)
2202 return is_array_type_compatible (target, candidate);
2204 case MONO_TYPE_VALUETYPE:
2205 return candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass;
2209 g_assert_not_reached ();
2212 case MONO_TYPE_MVAR:
2214 g_assert_not_reached ();
2218 VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
2219 g_assert_not_reached ();
2225 mono_delegate_param_equal (MonoType *delegate, MonoType *method)
2227 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2230 return mono_delegate_type_equal (method, delegate);
2234 mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
2236 if (mono_metadata_type_equal_full (delegate, method, TRUE))
2239 return mono_delegate_type_equal (delegate, method);
2243 * mono_delegate_signature_equal:
2245 * Compare two signatures in the way expected by delegates.
2247 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2249 * FIXME can this function be eliminated and proper metadata functionality be used?
2252 mono_delegate_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2255 if (sig1->param_count != sig2->param_count)
2258 if (sig1->generic_param_count != sig2->generic_param_count)
2261 if (sig1->call_convention != sig2->call_convention)
2264 for (i = 0; i < sig1->param_count; i++) {
2265 MonoType *p1 = sig1->params [i];
2266 MonoType *p2 = sig2->params [i];
2268 if (!mono_delegate_param_equal (p1, p2))
2272 if (!mono_delegate_ret_equal (sig1->ret, sig2->ret))
2279 * verify_ldftn_delegate:
2281 * Verify properties of ldftn based delegates.
2284 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2286 MonoMethod *method = funptr->method;
2288 /*ldftn non-final virtuals only allowed if method is not static,
2289 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2290 * This rules doesn't apply if the object on stack is a boxed valuetype.
2292 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !(method->klass->flags & TYPE_ATTRIBUTE_SEALED) && !stack_slot_is_boxed_value (value)) {
2293 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2294 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2295 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2297 /*current method must not be static*/
2298 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2299 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2301 /*value is the this pointer, loaded using ldarg.0 */
2302 if (!stack_slot_is_this_pointer (value))
2303 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));
2305 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2310 * verify_delegate_compatibility:
2312 * Verify delegate creation sequence.
2316 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2318 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2319 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2321 MonoMethod *invoke, *method;
2322 const guint8 *ip = ctx->header->code;
2323 guint32 ip_offset = ctx->ip_offset;
2325 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2326 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
2330 invoke = mono_get_delegate_invoke (delegate);
2331 method = funptr->method;
2333 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method)))
2334 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer parameter for delegate constructor has diferent signature at 0x%04x", ctx->ip_offset));
2337 * Delegate code sequences:
2343 * [-6] ldvirtftn token
2347 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2348 verify_ldftn_delegate (ctx, delegate, value, funptr);
2349 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2350 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2352 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2354 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2357 if (!verify_type_compatibility (ctx, &method->klass->byval_arg, value->type) && !stack_slot_is_null_literal (value))
2358 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2360 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2361 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2363 #undef IS_VALID_OPCODE
2364 #undef IS_LOAD_FUN_PTR
2367 /* implement the opcode checks*/
2369 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2371 if (arg >= ctx->max_args) {
2373 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2375 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2376 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2377 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2379 } else if (check_overflow (ctx)) {
2380 /*We must let the value be pushed, otherwise we would get an underflow error*/
2381 check_unverifiable_type (ctx, ctx->params [arg]);
2382 if (ctx->params [arg]->byref && take_addr)
2383 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2384 if (!set_stack_value (ctx, stack_push (ctx), ctx->params [arg], take_addr))
2387 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
2389 ctx->has_this_store = TRUE;
2391 stack_top (ctx)->stype |= THIS_POINTER_MASK;
2397 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2399 if (arg >= ctx->num_locals) {
2400 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2401 } else if (check_overflow (ctx)) {
2402 /*We must let the value be pushed, otherwise we would get an underflow error*/
2403 check_unverifiable_type (ctx, ctx->locals [arg]);
2404 if (ctx->locals [arg]->byref && take_addr)
2405 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2407 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2412 store_arg (VerifyContext *ctx, guint32 arg)
2416 if (arg >= ctx->max_args) {
2417 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2418 check_underflow (ctx, 1);
2423 if (check_underflow (ctx, 1)) {
2424 value = stack_pop (ctx);
2425 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2426 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2429 if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2430 ctx->has_this_store = 1;
2434 store_local (VerifyContext *ctx, guint32 arg)
2437 if (arg >= ctx->num_locals) {
2438 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2442 /*TODO verify definite assigment */
2443 if (check_underflow (ctx, 1)) {
2444 value = stack_pop(ctx);
2445 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2446 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type [%s], type [%s] was expected in local store at 0x%04x",
2447 stack_slot_get_name (value),
2448 mono_type_get_stack_name (ctx->locals [arg]),
2454 /*FIXME add and sub needs special care here*/
2456 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2459 int idxa, idxb, complexMerge = 0;
2462 if (!check_underflow (ctx, 2))
2464 a = stack_get (ctx, 1);
2465 b = stack_top (ctx);
2467 idxa = stack_slot_get_underlying_type (a);
2468 if (stack_slot_is_managed_pointer (a)) {
2473 idxb = stack_slot_get_underlying_type (b);
2474 if (stack_slot_is_managed_pointer (b)) {
2481 res = table [idxa][idxb];
2483 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2484 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2487 if (res == TYPE_INV) {
2488 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)));
2492 if (res & NON_VERIFIABLE_RESULT) {
2493 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)));
2495 res = res & ~NON_VERIFIABLE_RESULT;
2498 if (complexMerge && res == TYPE_PTR) {
2499 if (complexMerge == 1)
2500 copy_stack_value (stack_top (ctx), a);
2501 else if (complexMerge == 2)
2502 copy_stack_value (stack_top (ctx), b);
2504 * There is no need to merge the type of two pointers.
2505 * The only valid operation is subtraction, that returns a native
2506 * int as result and can be used with any 2 pointer kinds.
2507 * This is valid acording to Patition III 1.1.4
2510 stack_top (ctx)->stype = res;
2516 do_boolean_branch_op (VerifyContext *ctx, int delta)
2518 int target = ctx->ip_offset + delta;
2521 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2523 if (target < 0 || target >= ctx->code_size) {
2524 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2528 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2530 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2533 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2537 ctx->target = target;
2539 if (!check_underflow (ctx, 1))
2542 top = stack_pop (ctx);
2543 if (!is_valid_bool_arg (top))
2544 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", stack_slot_get_name (stack_get (ctx, -1)), ctx->ip_offset));
2546 check_unmanaged_pointer (ctx, top);
2551 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2556 int target = ctx->ip_offset + delta;
2558 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2560 if (target < 0 || target >= ctx->code_size) {
2561 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2565 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2567 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2570 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2574 ctx->target = target;
2576 if (!check_underflow (ctx, 2))
2579 b = stack_pop (ctx);
2580 a = stack_pop (ctx);
2582 idxa = stack_slot_get_underlying_type (a);
2583 if (stack_slot_is_managed_pointer (a))
2586 idxb = stack_slot_get_underlying_type (b);
2587 if (stack_slot_is_managed_pointer (b))
2592 res = table [idxa][idxb];
2594 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2595 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2597 if (res == TYPE_INV) {
2598 CODE_NOT_VERIFIABLE (ctx,
2599 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));
2600 } else if (res & NON_VERIFIABLE_RESULT) {
2601 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));
2602 res = res & ~NON_VERIFIABLE_RESULT;
2607 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2613 if (!check_underflow (ctx, 2))
2615 b = stack_pop (ctx);
2616 a = stack_pop (ctx);
2618 if (opcode == CEE_CGT_UN) {
2619 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2620 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2625 idxa = stack_slot_get_underlying_type (a);
2626 if (stack_slot_is_managed_pointer (a))
2629 idxb = stack_slot_get_underlying_type (b);
2630 if (stack_slot_is_managed_pointer (b))
2635 res = table [idxa][idxb];
2637 if(res == TYPE_INV) {
2638 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));
2639 } else if (res & NON_VERIFIABLE_RESULT) {
2640 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));
2641 res = res & ~NON_VERIFIABLE_RESULT;
2643 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2647 do_ret (VerifyContext *ctx)
2649 MonoType *ret = ctx->signature->ret;
2650 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2651 if (ret->type != MONO_TYPE_VOID) {
2653 if (!check_underflow (ctx, 1))
2656 top = stack_pop(ctx);
2658 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2659 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2663 if (ret->byref || ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
2664 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
2667 if (ctx->eval.size > 0) {
2668 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2670 if (in_any_block (ctx->header, ctx->ip_offset))
2671 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2675 * 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.
2676 * This is illegal but mono_get_method_full decoded it.
2677 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2680 do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual)
2683 MonoMethodSignature *sig;
2686 gboolean virt_check_this = FALSE;
2687 gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
2689 if (!(method = verifier_load_method (ctx, method_token, virtual ? "callvirt" : "call")))
2693 CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
2695 if (method->klass->valuetype) // && !constrained ???
2696 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
2698 if ((method->flags & METHOD_ATTRIBUTE_STATIC))
2699 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
2702 if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
2703 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
2705 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL)) {
2706 virt_check_this = TRUE;
2707 ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
2711 if (!(sig = mono_method_get_signature_full (method, ctx->image, method_token, ctx->generic_context)))
2712 sig = mono_method_get_signature (method, ctx->image, method_token);
2714 param_count = sig->param_count + sig->hasthis;
2715 if (!check_underflow (ctx, param_count))
2718 for (i = sig->param_count - 1; i >= 0; --i) {
2719 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2720 value = stack_pop (ctx);
2721 if (!verify_stack_type_compatibility (ctx, sig->params[i], value))
2722 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2724 if (stack_slot_is_managed_mutability_pointer (value))
2725 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));
2727 if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
2728 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));
2734 MonoType *type = &method->klass->byval_arg;
2736 value = stack_pop (ctx);
2737 copy_stack_value (©, value);
2738 //TODO we should extract this to a 'drop_byref_argument' and use everywhere
2739 //Other parts of the code suffer from the same issue of
2740 copy.type = mono_type_get_type_byval (copy.type);
2741 copy.stype &= ~POINTER_MASK;
2743 if (virt_check_this && !stack_slot_is_this_pointer (value) && !(method->klass->valuetype || stack_slot_is_boxed_value (value)))
2744 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));
2746 if (constrained && virtual) {
2747 if (!stack_slot_is_managed_pointer (value))
2748 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
2749 if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), ctx->constrained_type, TRUE))
2750 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
2751 copy.stype |= BOXED_MASK;
2753 if (stack_slot_is_managed_pointer (value) && !mono_class_from_mono_type (value->type)->valuetype)
2754 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));
2756 if (!virtual && mono_class_from_mono_type (value->type)->valuetype && !method->klass->valuetype && !stack_slot_is_boxed_value (value))
2757 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
2759 if (virtual && mono_class_from_mono_type (value->type)->valuetype && !stack_slot_is_boxed_value (value))
2760 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
2762 if (method->klass->valuetype && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
2763 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));
2765 if (!verify_stack_type_compatibility (ctx, type, ©))
2766 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
2768 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, value->type->data.klass))
2769 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2771 } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
2772 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
2774 if (sig->ret->type != MONO_TYPE_VOID) {
2775 if (check_overflow (ctx)) {
2776 value = stack_push (ctx);
2777 set_stack_value (ctx, value, sig->ret, FALSE);
2778 if ((ctx->prefix_set & PREFIX_READONLY) && method->klass->rank && !strcmp (method->name, "Address")) {
2779 ctx->prefix_set &= ~PREFIX_READONLY;
2780 value->stype |= CMMP_MASK;
2785 if ((ctx->prefix_set & PREFIX_TAIL)) {
2786 if (!mono_delegate_ret_equal (mono_method_signature (ctx->method)->ret, sig->ret))
2787 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
2788 if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
2789 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
2795 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2797 MonoClassField *field;
2800 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
2802 if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
2805 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2806 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
2809 /*taking the address of initonly field only works from the static constructor */
2810 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2811 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
2812 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2814 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
2815 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
2817 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
2821 do_store_static_field (VerifyContext *ctx, int token) {
2822 MonoClassField *field;
2825 CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
2827 if (!check_underflow (ctx, 1))
2830 value = stack_pop (ctx);
2832 if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
2835 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2836 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
2840 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2841 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
2845 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
2846 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
2848 if (!verify_stack_type_compatibility (ctx, field->type, value))
2849 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));
2853 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
2855 MonoClassField *field;
2857 gboolean is_pointer;
2859 /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
2860 if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
2864 //the value on stack is going to be used as a pointer
2865 is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (&field->parent->byval_arg));
2867 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2868 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
2871 g_assert (obj->type);
2873 /*The value on the stack must be a subclass of the defining type of the field*/
2874 /* we need to check if we can load the field from the stack value*/
2876 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
2877 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
2879 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
2880 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
2882 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
2883 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));
2885 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
2886 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
2887 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));
2889 if (!stack_slot_is_null_literal (obj) && !verify_type_compatibility (ctx, &field->parent->byval_arg, mono_type_get_type_byval (obj->type)))
2890 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
2892 if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, obj->type->data.klass))
2893 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
2896 check_unmanaged_pointer (ctx, obj);
2901 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
2904 MonoClassField *field;
2907 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
2909 if (!check_underflow (ctx, 1))
2911 obj = stack_pop (ctx);
2913 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
2916 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
2917 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
2919 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2920 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
2921 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2923 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
2927 do_store_field (VerifyContext *ctx, int token)
2929 ILStackDesc *value, *obj;
2930 MonoClassField *field;
2931 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
2933 if (!check_underflow (ctx, 2))
2936 value = stack_pop (ctx);
2937 obj = stack_pop (ctx);
2939 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
2942 if (!verify_stack_type_compatibility (ctx, field->type, value))
2943 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2946 /*TODO proper handle for Nullable<T>*/
2948 do_box_value (VerifyContext *ctx, int klass_token)
2951 MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
2956 if (!check_underflow (ctx, 1))
2959 value = stack_top (ctx);
2960 /*box is a nop for reference types*/
2962 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
2963 value->stype |= BOXED_MASK;
2967 value = stack_pop (ctx);
2969 if (!verify_stack_type_compatibility (ctx, type, value))
2970 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
2972 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
2976 do_unbox_value (VerifyContext *ctx, int klass_token)
2979 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
2984 if (!check_underflow (ctx, 1))
2987 if (!mono_class_from_mono_type (type)->valuetype)
2988 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
2990 value = stack_pop (ctx);
2992 /*Value should be: a boxed valuetype or a reference type*/
2993 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
2994 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
2995 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));
2997 set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
2998 value->stype |= CMMP_MASK;
3002 do_unbox_any (VerifyContext *ctx, int klass_token)
3005 MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
3010 if (!check_underflow (ctx, 1))
3013 value = stack_pop (ctx);
3015 /*Value should be: a boxed valuetype or a reference type*/
3016 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
3017 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
3018 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));
3020 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3024 do_unary_math_op (VerifyContext *ctx, int op)
3027 if (!check_underflow (ctx, 1))
3029 value = stack_top (ctx);
3030 switch (stack_slot_get_type (value)) {
3033 case TYPE_NATIVE_INT:
3038 case TYPE_COMPLEX: /*only enums are ok*/
3039 if (mono_type_is_enum_type (value->type))
3042 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
3047 do_conversion (VerifyContext *ctx, int kind)
3050 if (!check_underflow (ctx, 1))
3052 value = stack_pop (ctx);
3054 switch (stack_slot_get_type (value)) {
3057 case TYPE_NATIVE_INT:
3061 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));
3066 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3069 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3072 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3074 case TYPE_NATIVE_INT:
3075 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3078 g_error ("unknown type %02x in conversion", kind);
3084 do_load_token (VerifyContext *ctx, int token)
3087 MonoClass *handle_class;
3088 if (!check_overflow (ctx))
3090 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
3092 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
3095 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
3099 do_ldobj_value (VerifyContext *ctx, int token)
3102 MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
3103 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3108 if (!check_underflow (ctx, 1))
3111 value = stack_pop (ctx);
3112 if (!stack_slot_is_managed_pointer (value)
3113 && stack_slot_get_type (value) != TYPE_NATIVE_INT
3114 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
3115 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
3119 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
3120 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
3122 /*We have a byval on the stack, but the comparison must be strict. */
3123 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
3124 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
3126 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3130 do_stobj (VerifyContext *ctx, int token)
3132 ILStackDesc *dest, *src;
3133 MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
3134 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3139 if (!check_underflow (ctx, 2))
3142 src = stack_pop (ctx);
3143 dest = stack_pop (ctx);
3145 if (stack_slot_is_managed_mutability_pointer (dest))
3146 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
3148 if (!stack_slot_is_managed_pointer (dest))
3149 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
3151 if (!verify_stack_type_compatibility (ctx, type, src))
3152 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
3154 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3155 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
3159 do_cpobj (VerifyContext *ctx, int token)
3161 ILStackDesc *dest, *src;
3162 MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
3166 if (!check_underflow (ctx, 2))
3169 src = stack_pop (ctx);
3170 dest = stack_pop (ctx);
3172 if (!stack_slot_is_managed_pointer (src))
3173 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
3175 if (!stack_slot_is_managed_pointer (dest))
3176 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
3178 if (stack_slot_is_managed_mutability_pointer (dest))
3179 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
3181 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
3182 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
3184 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
3185 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
3189 do_initobj (VerifyContext *ctx, int token)
3192 MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
3196 if (!check_underflow (ctx, 1))
3199 obj = stack_pop (ctx);
3201 if (!stack_slot_is_managed_pointer (obj))
3202 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
3204 if (stack_slot_is_managed_mutability_pointer (obj))
3205 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
3207 stack = mono_type_get_type_byval (obj->type);
3208 if (MONO_TYPE_IS_REFERENCE (stack)) {
3209 if (!verify_type_compatibility (ctx, stack, type))
3210 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3211 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
3212 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3213 } else if (!verify_type_compatibility (ctx, stack, type)) {
3214 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
3219 do_newobj (VerifyContext *ctx, int token)
3223 MonoMethodSignature *sig;
3225 gboolean is_delegate = FALSE;
3227 if (!(method = verifier_load_method (ctx, token, "newobj")))
3230 if (!mono_method_is_constructor (method)) {
3231 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3235 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3236 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3238 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
3239 CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor not visible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
3241 //FIXME use mono_method_get_signature_full
3242 sig = mono_method_signature (method);
3243 if (!check_underflow (ctx, sig->param_count))
3246 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3249 ILStackDesc *funptr;
3250 //first arg is object, second arg is fun ptr
3251 if (sig->param_count != 2) {
3252 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3255 funptr = stack_pop (ctx);
3256 value = stack_pop (ctx);
3257 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3259 for (i = sig->param_count - 1; i >= 0; --i) {
3260 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3261 value = stack_pop (ctx);
3262 if (!verify_stack_type_compatibility (ctx, sig->params [i], value))
3263 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
3265 if (stack_slot_is_managed_mutability_pointer (value))
3266 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
3270 if (check_overflow (ctx))
3271 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3275 do_cast (VerifyContext *ctx, int token, const char *opcode) {
3280 if (!check_underflow (ctx, 1))
3283 if (!(type = verifier_load_type (ctx, token, opcode)))
3287 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
3291 value = stack_pop (ctx);
3292 is_boxed = stack_slot_is_boxed_value (value);
3294 if (stack_slot_is_managed_pointer (value))
3295 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3296 else if (mono_class_from_mono_type (value->type)->valuetype && !is_boxed)
3297 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Value cannot be a valuetype for %s at 0x%04x", opcode, ctx->ip_offset));
3299 switch (value->type->type) {
3300 case MONO_TYPE_FNPTR:
3302 case MONO_TYPE_TYPEDBYREF:
3303 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
3306 stack_push_val (ctx, TYPE_COMPLEX | (mono_class_from_mono_type (type)->valuetype || is_boxed ? BOXED_MASK : 0), type);
3310 mono_type_from_opcode (int opcode) {
3318 return &mono_defaults.sbyte_class->byval_arg;
3326 return &mono_defaults.int16_class->byval_arg;
3334 return &mono_defaults.int32_class->byval_arg;
3340 return &mono_defaults.int64_class->byval_arg;
3346 return &mono_defaults.single_class->byval_arg;
3352 return &mono_defaults.double_class->byval_arg;
3358 return &mono_defaults.int_class->byval_arg;
3362 case CEE_LDELEM_REF:
3363 case CEE_STELEM_REF:
3364 return &mono_defaults.object_class->byval_arg;
3367 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3373 do_load_indirect (VerifyContext *ctx, int opcode)
3376 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3378 if (!check_underflow (ctx, 1))
3381 value = stack_pop (ctx);
3382 if (!stack_slot_is_managed_pointer (value)) {
3383 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3384 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3388 if (opcode == CEE_LDIND_REF) {
3389 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3390 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3391 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3393 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3394 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3395 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3400 do_store_indirect (VerifyContext *ctx, int opcode)
3402 ILStackDesc *addr, *val;
3403 CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
3405 if (!check_underflow (ctx, 2))
3408 val = stack_pop (ctx);
3409 addr = stack_pop (ctx);
3411 check_unmanaged_pointer (ctx, addr);
3413 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3414 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3418 if (stack_slot_is_managed_mutability_pointer (addr)) {
3419 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
3423 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3424 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3426 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3427 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3431 do_newarr (VerifyContext *ctx, int token)
3434 MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
3439 if (!check_underflow (ctx, 1))
3442 value = stack_pop (ctx);
3443 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3444 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));
3446 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3449 /*FIXME handle arrays that are not 0-indexed*/
3451 do_ldlen (VerifyContext *ctx)
3455 if (!check_underflow (ctx, 1))
3458 value = stack_pop (ctx);
3460 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3461 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3463 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3466 /*FIXME handle arrays that are not 0-indexed*/
3467 /*FIXME handle readonly prefix and CMMP*/
3469 do_ldelema (VerifyContext *ctx, int klass_token)
3471 ILStackDesc *index, *array, *res;
3472 MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
3478 if (!check_underflow (ctx, 2))
3481 index = stack_pop (ctx);
3482 array = stack_pop (ctx);
3484 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3485 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));
3487 if (!stack_slot_is_null_literal (array)) {
3488 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3489 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3491 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3492 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3494 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3497 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3501 res = stack_push (ctx);
3502 set_stack_value (ctx, res, type, TRUE);
3503 if (ctx->prefix_set & PREFIX_READONLY) {
3504 ctx->prefix_set &= ~PREFIX_READONLY;
3505 res->stype |= CMMP_MASK;
3510 * FIXME handle arrays that are not 0-indexed
3511 * FIXME handle readonly prefix and CMMP
3514 do_ldelem (VerifyContext *ctx, int opcode, int token)
3516 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
3517 ILStackDesc *index, *array;
3519 if (!check_underflow (ctx, 2))
3522 if (opcode == CEE_LDELEM_ANY) {
3523 if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
3524 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3528 type = mono_type_from_opcode (opcode);
3531 index = stack_pop (ctx);
3532 array = stack_pop (ctx);
3534 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3535 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));
3537 if (!stack_slot_is_null_literal (array)) {
3538 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3539 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));
3541 if (opcode == CEE_LDELEM_REF) {
3542 if (array->type->data.klass->valuetype)
3543 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3544 type = &array->type->data.klass->byval_arg;
3546 MonoType *candidate = &array->type->data.klass->byval_arg;
3547 if (IS_STRICT_MODE (ctx)) {
3548 MonoType *underlying_type = mono_type_get_underlying_type_any (type);
3549 MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
3550 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)) ||
3551 (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
3552 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3554 if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
3555 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3560 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3565 * FIXME handle arrays that are not 0-indexed
3568 do_stelem (VerifyContext *ctx, int opcode, int token)
3570 ILStackDesc *index, *array, *value;
3572 if (!check_underflow (ctx, 3))
3575 if (opcode == CEE_STELEM_ANY) {
3576 if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
3577 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3581 type = mono_type_from_opcode (opcode);
3584 value = stack_pop (ctx);
3585 index = stack_pop (ctx);
3586 array = stack_pop (ctx);
3588 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3589 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));
3591 if (!stack_slot_is_null_literal (array)) {
3592 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3593 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));
3595 if (opcode == CEE_STELEM_REF) {
3596 if (array->type->data.klass->valuetype)
3597 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3598 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3599 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3604 if (opcode == CEE_STELEM_REF) {
3605 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3606 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3607 } else if (opcode != CEE_STELEM_REF && !verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (value), FALSE)) {
3608 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3613 do_throw (VerifyContext *ctx)
3615 ILStackDesc *exception;
3616 if (!check_underflow (ctx, 1))
3618 exception = stack_pop (ctx);
3620 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3621 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3623 /*The stack is left empty after a throw*/
3629 do_endfilter (VerifyContext *ctx)
3631 MonoExceptionClause *clause;
3633 if (IS_STRICT_MODE (ctx)) {
3634 if (ctx->eval.size != 1)
3635 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3637 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
3638 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3641 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3642 if (IS_STRICT_MODE (ctx)) {
3643 if (ctx->ip_offset != clause->handler_offset - 2)
3644 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3646 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3647 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3650 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3651 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3653 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3660 do_leave (VerifyContext *ctx, int delta)
3662 int target = ((gint32)ctx->ip_offset) + delta;
3663 if (target >= ctx->code_size || target < 0)
3664 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3666 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
3667 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
3673 * Verify br and br.s opcodes.
3676 do_static_branch (VerifyContext *ctx, int delta)
3678 int target = ctx->ip_offset + delta;
3679 if (target < 0 || target >= ctx->code_size) {
3680 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
3684 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3686 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3689 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3693 ctx->target = target;
3697 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
3699 int i, base = ctx->ip_offset + 5 + count * 4;
3702 if (!check_underflow (ctx, 1))
3705 value = stack_pop (ctx);
3707 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3708 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
3710 for (i = 0; i < count; ++i) {
3711 int target = base + read32 (data + i * 4);
3713 if (target < 0 || target >= ctx->code_size)
3714 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
3716 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3718 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3721 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3724 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
3729 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
3734 if (virtual && !check_underflow (ctx, 1))
3737 if (!virtual && !check_overflow (ctx))
3740 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
3741 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3745 if (!(method = verifier_load_method (ctx, token, virtual ? "ldvirtfrn" : "ldftn")))
3748 if (mono_method_is_constructor (method))
3749 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
3752 ILStackDesc *top = stack_pop (ctx);
3754 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
3755 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
3757 if (method->flags & METHOD_ATTRIBUTE_STATIC)
3758 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
3760 if (!verify_type_compatibility (ctx, &method->klass->byval_arg, top->type))
3761 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
3764 if (!mono_method_can_access_method_full (ctx->method, method, NULL))
3765 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);
3767 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
3768 top->method = method;
3772 do_sizeof (VerifyContext *ctx, int token)
3776 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
3777 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3781 if (!(type = verifier_load_type (ctx, token, "sizeof")))
3784 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
3785 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
3789 if (type->type == MONO_TYPE_VOID) {
3790 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
3794 if (check_overflow (ctx))
3795 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
3798 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
3800 do_localloc (VerifyContext *ctx)
3802 if (ctx->eval.size != 1) {
3803 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
3807 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
3808 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
3812 set_stack_value (ctx, stack_top (ctx), &mono_defaults.int_class->byval_arg, FALSE);
3813 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
3817 do_ldstr (VerifyContext *ctx, guint32 token)
3819 if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
3820 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3824 if (mono_metadata_token_index (token) >= ctx->image->heap_us.size) {
3825 ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
3829 if (check_overflow (ctx))
3830 stack_push_val (ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
3834 do_refanyval (VerifyContext *ctx, int token)
3838 if (!check_underflow (ctx, 1))
3841 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
3844 top = stack_pop (ctx);
3846 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
3847 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));
3849 set_stack_value (ctx, stack_push (ctx), type, TRUE);
3853 do_refanytype (VerifyContext *ctx)
3857 if (!check_underflow (ctx, 1))
3860 top = stack_pop (ctx);
3862 if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
3863 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));
3865 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typehandle_class->byval_arg, FALSE);
3870 do_mkrefany (VerifyContext *ctx, int token)
3874 if (!check_underflow (ctx, 1))
3877 if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
3880 top = stack_pop (ctx);
3882 if (stack_slot_is_managed_mutability_pointer (top))
3883 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
3885 if (!stack_slot_is_managed_pointer (top)) {
3886 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));
3888 MonoType *stack_type = mono_type_get_type_byval (top->type);
3889 if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
3890 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
3892 if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
3893 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
3896 set_stack_value (ctx, stack_push (ctx), &mono_defaults.typed_reference_class->byval_arg, FALSE);
3901 * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
3903 * @from holds new values for a given control path
3904 * @to holds the current values of a given control path
3906 * TODO we can eliminate the from argument as all callers pass &ctx->eval
3909 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
3912 stack_init (ctx, to);
3915 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
3918 stack_copy (&ctx->eval, to);
3920 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
3921 stack_copy (to, &ctx->eval);
3924 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
3926 if (from->size != to->size) {
3927 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
3928 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));
3932 //FIXME we need to preserve CMMP attributes
3933 //FIXME we must take null literals into consideration.
3934 for (i = 0; i < from->size; ++i) {
3935 ILStackDesc *new_slot = from->stack + i;
3936 ILStackDesc *old_slot = to->stack + i;
3937 MonoType *new_type = mono_type_from_stack_slot (new_slot);
3938 MonoType *old_type = mono_type_from_stack_slot (old_slot);
3939 MonoClass *old_class = mono_class_from_mono_type (old_type);
3940 MonoClass *new_class = mono_class_from_mono_type (new_type);
3941 MonoClass *match_class = NULL;
3943 // S := T then U = S (new value is compatible with current value, keep current)
3944 if (verify_type_compatibility (ctx, old_type, new_type)) {
3945 copy_stack_value (new_slot, old_slot);
3949 // T := S then U = T (old value is compatible with current value, use new)
3950 if (verify_type_compatibility (ctx, new_type, old_type)) {
3951 copy_stack_value (old_slot, new_slot);
3955 //both are reference types, use closest common super type
3956 if (!mono_class_from_mono_type (old_type)->valuetype
3957 && !mono_class_from_mono_type (new_type)->valuetype
3958 && !stack_slot_is_managed_pointer (old_slot)
3959 && !stack_slot_is_managed_pointer (new_slot)) {
3961 for (j = MIN (old_class->idepth, new_class->idepth) - 1; j > 0; --j) {
3962 if (mono_metadata_type_equal (&old_class->supertypes [j]->byval_arg, &new_class->supertypes [j]->byval_arg)) {
3963 match_class = old_class->supertypes [j];
3968 for (j = 0; j < old_class->interface_count; ++j) {
3969 for (k = 0; k < new_class->interface_count; ++k) {
3970 if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
3971 match_class = old_class->interfaces [j];
3977 //No decent super type found, use object
3978 match_class = mono_defaults.object_class;
3980 } else if (is_compatible_boxed_valuetype (old_type, new_type, new_slot, FALSE) || is_compatible_boxed_valuetype (new_type, old_type, old_slot, FALSE)) {
3981 match_class = mono_defaults.object_class;
3985 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible old [%s] new [%s] at 0x%04x", i, stack_slot_get_name (old_slot), stack_slot_get_name (new_slot), ctx->ip_offset));
3989 g_assert (match_class);
3990 set_stack_value (ctx, old_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
3991 set_stack_value (ctx, new_slot, &match_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
3997 to->flags |= IL_CODE_FLAG_WAS_TARGET;
3998 to->flags |= IL_CODE_STACK_MERGED;
4001 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
4002 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
4005 * is_clause_in_range :
4007 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
4010 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4012 if (clause->try_offset >= start && clause->try_offset < end)
4014 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
4020 * is_clause_inside_range :
4022 * Returns TRUE if @clause lies completely inside the @start - @end range.
4025 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
4027 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
4029 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
4035 * is_clause_nested :
4037 * Returns TRUE if @nested is nested in @clause.
4040 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
4042 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
4044 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
4045 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
4048 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
4049 * the each pair of exception must have the following properties:
4050 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
4051 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
4052 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
4055 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
4057 /*clause is nested*/
4058 if (is_clause_nested (to_test, clause)) {
4059 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4060 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
4065 /*wrong nesting order.*/
4066 if (is_clause_nested (clause, to_test)) {
4067 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
4071 /*mutual protection*/
4072 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
4073 /*handlers are not disjoint*/
4074 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
4075 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
4078 /* handlers are not catch or filter */
4079 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
4080 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
4087 /*not completelly disjoint*/
4088 if (is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
4089 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len))
4090 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
4094 * FIXME: need to distinguish between valid and verifiable.
4095 * Need to keep track of types on the stack.
4096 * Verify types for opcodes.
4099 mono_method_verify (MonoMethod *method, int level)
4101 const unsigned char *ip;
4102 const unsigned char *end;
4103 int i, n, need_merge = 0, start = 0;
4104 guint token, ip_offset = 0, prefix = 0;
4105 MonoGenericContext *generic_context = NULL;
4110 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
4112 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4113 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
4117 memset (&ctx, 0, sizeof (VerifyContext));
4119 //FIXME use mono_method_get_signature_full
4120 ctx.signature = mono_method_signature (method);
4121 if (!ctx.signature) {
4122 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
4125 ctx.header = mono_method_get_header (method);
4127 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
4130 ctx.method = method;
4131 ip = ctx.header->code;
4132 end = ip + ctx.header->code_size;
4133 ctx.image = image = method->klass->image;
4136 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
4137 ctx.max_stack = ctx.header->max_stack;
4138 ctx.verifiable = ctx.valid = 1;
4141 ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
4142 ctx.code_size = ctx.header->code_size;
4144 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
4147 ctx.num_locals = ctx.header->num_locals;
4148 ctx.locals = ctx.header->locals;
4150 if (ctx.num_locals > 0 && !ctx.header->init_locals)
4151 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
4153 if (ctx.signature->hasthis) {
4154 ctx.params = g_new0 (MonoType*, ctx.max_args);
4155 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
4156 memcpy (ctx.params + 1, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
4158 ctx.params = ctx.signature->params;
4161 if (ctx.signature->is_inflated)
4162 ctx.generic_context = generic_context = mono_method_get_context (method);
4164 if (!generic_context && (method->klass->generic_container || method->generic_container)) {
4165 if (method->generic_container)
4166 ctx.generic_context = generic_context = &method->generic_container->context;
4168 ctx.generic_context = generic_context = &method->klass->generic_container->context;
4171 stack_init (&ctx, &ctx.eval);
4173 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4174 MonoExceptionClause *clause = ctx.header->clauses + i;
4175 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); );
4177 if (clause->try_offset > ctx.code_size)
4178 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4180 if (clause->try_len <= 0)
4181 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4183 if (clause->handler_offset > ctx.code_size)
4184 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
4186 if (clause->handler_len <= 0)
4187 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
4189 if (clause->try_offset < clause->handler_offset && clause->try_offset + clause->try_len > HANDLER_START (clause))
4190 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
4192 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
4193 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
4198 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
4199 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4200 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
4202 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
4203 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
4205 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
4206 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
4207 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
4211 while (ip < end && ctx.valid) {
4212 ctx.ip_offset = ip_offset = ip - ctx.header->code;
4214 /*We need to check against fallthrou in and out of protected blocks.
4215 * For fallout we check the once a protected block ends, if the start flag is not set.
4216 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
4217 * TODO convert these checks to be done using flags and not this loop
4219 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
4220 MonoExceptionClause *clause = ctx.header->clauses + i;
4222 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
4223 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
4227 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
4228 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
4229 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4231 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
4235 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
4236 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
4240 if (clause->handler_offset == ip_offset && start == 0) {
4241 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
4245 if (clause->try_offset == ip_offset && ctx.eval.size > 0) {
4246 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
4255 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
4256 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
4259 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
4262 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
4263 #ifdef MONO_VERIFIER_DEBUG
4266 discode = mono_disasm_code_one (NULL, method, ip, NULL);
4267 discode [strlen (discode) - 1] = 0; /* no \n */
4268 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
4271 dump_stack_state (&ctx.code [ip_offset]);
4272 dump_stack_state (&ctx.eval);
4285 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
4291 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
4295 case CEE_ADD_OVF_UN:
4296 do_binop (&ctx, *ip, add_ovf_un_table);
4300 case CEE_SUB_OVF_UN:
4301 do_binop (&ctx, *ip, sub_ovf_un_table);
4308 case CEE_MUL_OVF_UN:
4309 do_binop (&ctx, *ip, bin_ovf_table);
4314 do_binop (&ctx, *ip, add_table);
4319 do_binop (&ctx, *ip, sub_table);
4326 do_binop (&ctx, *ip, bin_op_table);
4335 do_binop (&ctx, *ip, int_bin_op_table);
4342 do_binop (&ctx, *ip, shift_op_table);
4347 if (!check_underflow (&ctx, 1))
4363 /*TODO support definite assignment verification? */
4364 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
4372 store_local (&ctx, *ip - CEE_STLOC_0);
4377 store_local (&ctx, ip [1]);
4382 store_arg (&ctx, ip [1]);
4396 if (check_overflow (&ctx))
4397 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4402 if (check_overflow (&ctx))
4403 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4408 if (check_overflow (&ctx))
4409 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4414 if (check_overflow (&ctx))
4415 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4420 if (check_overflow (&ctx))
4421 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4426 if (check_overflow (&ctx))
4427 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4432 if (check_overflow (&ctx))
4433 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4439 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4452 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
4459 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
4472 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
4479 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
4483 /* FIXME: warn/error instead? */
4490 if (!check_underflow (&ctx, 1))
4492 if (!check_overflow (&ctx))
4494 top = stack_push (&ctx);
4495 copy_stack_value (top, stack_get (&ctx, 1));
4502 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
4503 token = read32 (ip + 1);
4504 if (in_any_block (ctx.header, ip_offset))
4505 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
4507 * FIXME: check signature, retval, arguments etc.
4513 do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
4518 token = read32 (ip + 1);
4520 * FIXME: check signature, retval, arguments etc.
4521 * FIXME: check requirements for tail call
4523 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
4527 do_static_branch (&ctx, (signed char)ip [1] + 2);
4535 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
4541 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
4549 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
4555 n = read32 (ip + 1);
4556 do_switch (&ctx, n, (ip + 5));
4558 ip += 5 + sizeof (guint32) * n;
4572 do_load_indirect (&ctx, *ip);
4584 do_store_indirect (&ctx, *ip);
4590 do_unary_math_op (&ctx, *ip);
4600 do_conversion (&ctx, TYPE_I4);
4606 do_conversion (&ctx, TYPE_I8);
4613 do_conversion (&ctx, TYPE_R8);
4619 do_conversion (&ctx, TYPE_NATIVE_INT);
4624 do_cpobj (&ctx, read32 (ip + 1));
4629 do_ldobj_value (&ctx, read32 (ip + 1));
4634 do_ldstr (&ctx, read32 (ip + 1));
4639 do_newobj (&ctx, read32 (ip + 1));
4645 do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
4651 ++ip; /* warn, error ? */
4654 do_unbox_value (&ctx, read32 (ip + 1));
4666 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
4672 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
4677 do_store_field (&ctx, read32 (ip + 1));
4682 do_store_static_field (&ctx, read32 (ip + 1));
4687 do_stobj (&ctx, read32 (ip + 1));
4691 case CEE_CONV_OVF_I1_UN:
4692 case CEE_CONV_OVF_I2_UN:
4693 case CEE_CONV_OVF_I4_UN:
4694 case CEE_CONV_OVF_U1_UN:
4695 case CEE_CONV_OVF_U2_UN:
4696 case CEE_CONV_OVF_U4_UN:
4697 do_conversion (&ctx, TYPE_I4);
4701 case CEE_CONV_OVF_I8_UN:
4702 case CEE_CONV_OVF_U8_UN:
4703 do_conversion (&ctx, TYPE_I8);
4707 case CEE_CONV_OVF_I_UN:
4708 case CEE_CONV_OVF_U_UN:
4709 do_conversion (&ctx, TYPE_NATIVE_INT);
4714 do_box_value (&ctx, read32 (ip + 1));
4719 do_newarr (&ctx, read32 (ip + 1));
4729 do_ldelema (&ctx, read32 (ip + 1));
4743 case CEE_LDELEM_REF:
4744 do_ldelem (&ctx, *ip, 0);
4755 case CEE_STELEM_REF:
4756 do_stelem (&ctx, *ip, 0);
4760 case CEE_LDELEM_ANY:
4761 do_ldelem (&ctx, *ip, read32 (ip + 1));
4765 case CEE_STELEM_ANY:
4766 do_stelem (&ctx, *ip, read32 (ip + 1));
4771 do_unbox_any (&ctx, read32 (ip + 1));
4788 ++ip; /* warn, error ? */
4791 case CEE_CONV_OVF_I1:
4792 case CEE_CONV_OVF_U1:
4793 case CEE_CONV_OVF_I2:
4794 case CEE_CONV_OVF_U2:
4795 case CEE_CONV_OVF_I4:
4796 case CEE_CONV_OVF_U4:
4797 do_conversion (&ctx, TYPE_I4);
4801 case CEE_CONV_OVF_I8:
4802 case CEE_CONV_OVF_U8:
4803 do_conversion (&ctx, TYPE_I8);
4807 case CEE_CONV_OVF_I:
4808 case CEE_CONV_OVF_U:
4809 do_conversion (&ctx, TYPE_NATIVE_INT);
4820 ++ip; /* warn, error ? */
4823 do_refanyval (&ctx, read32 (ip + 1));
4827 if (!check_underflow (&ctx, 1))
4833 ++ip; /* warn, error ? */
4837 do_mkrefany (&ctx, read32 (ip + 1));
4850 ++ip; /* warn, error ? */
4853 do_load_token (&ctx, read32 (ip + 1));
4857 case CEE_ENDFINALLY:
4858 if (!is_correct_endfinally (ctx.header, ip_offset))
4859 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
4866 do_leave (&ctx, read32 (ip + 1) + 5);
4872 do_leave (&ctx, (signed char)ip [1] + 2);
4915 store_local (&ctx, read16 (ip + 1));
4920 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
4928 do_cmp_op (&ctx, cmp_br_op, *ip);
4933 store_arg (&ctx, read16 (ip + 1) );
4939 check_overflow (&ctx);
4940 if (ctx.signature->call_convention != MONO_CALL_VARARG)
4941 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
4942 set_stack_value (&ctx, stack_push (&ctx), &mono_defaults.argumenthandle_class->byval_arg, FALSE);
4947 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
4952 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
4962 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
4968 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
4981 do_endfilter (&ctx);
4985 case CEE_UNALIGNED_:
4986 prefix |= PREFIX_UNALIGNED;
4990 prefix |= PREFIX_VOLATILE;
4994 prefix |= PREFIX_TAIL;
4996 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
4997 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
5001 do_initobj (&ctx, read32 (ip + 1));
5005 case CEE_CONSTRAINED_:
5006 ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
5007 prefix |= PREFIX_CONSTRAINED;
5012 prefix |= PREFIX_READONLY;
5017 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5018 if (!check_underflow (&ctx, 3))
5020 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
5025 CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
5026 if (!check_underflow (&ctx, 3))
5028 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
5036 if (!is_correct_rethrow (ctx.header, ip_offset))
5037 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
5046 do_sizeof (&ctx, read32 (ip + 1));
5050 case CEE_REFANYTYPE:
5051 do_refanytype (&ctx);
5064 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
5066 if (!ctx.prefix_set) //first prefix
5067 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5068 ctx.prefix_set |= prefix;
5069 ctx.has_flags = TRUE;
5073 ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
5075 if (ctx.prefix_set & PREFIX_CONSTRAINED)
5076 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
5077 if (ctx.prefix_set & PREFIX_READONLY)
5078 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
5079 if (ctx.prefix_set & PREFIX_VOLATILE)
5080 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
5081 if (ctx.prefix_set & PREFIX_UNALIGNED)
5082 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
5083 ctx.prefix_set = prefix = 0;
5084 ctx.has_flags = FALSE;
5088 * if ip != end we overflowed: mark as error.
5090 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
5091 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
5094 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
5095 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
5096 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
5097 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
5098 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
5100 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
5101 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
5103 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
5104 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
5106 if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
5107 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));
5111 for (i = 0; i < ctx.header->code_size; ++i) {
5112 if (ctx.code [i].stack)
5113 g_free (ctx.code [i].stack);
5117 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
5119 g_slist_free (ctx.funptrs);
5122 g_free (ctx.eval.stack);
5125 if (ctx.signature->hasthis)
5126 g_free (ctx.params);
5132 mono_verify_corlib ()
5134 /* This is a public API function so cannot be removed */