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)
40 #define ADD_VERIFY_INFO(__ctx, __msg, __status) \
42 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
43 vinfo->status = __status; \
44 vinfo->message = ( __msg ); \
45 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
48 #define ADD_VERIFY_ERROR(__ctx, __msg) \
50 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR); \
54 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
56 if ((__ctx)->verifiable) { \
57 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE); \
58 (__ctx)->verifiable = 0; \
59 if (IS_FAIL_FAST_MODE (__ctx)) \
64 /*Flags to be used with ILCodeDesc::flags */
66 /*Instruction has not been processed.*/
67 IL_CODE_FLAG_NOT_PROCESSED = 0,
68 /*Instruction was decoded by mono_method_verify loop.*/
69 IL_CODE_FLAG_SEEN = 1,
70 /*Instruction was target of a branch or is at a protected block boundary.*/
71 IL_CODE_FLAG_WAS_TARGET = 2,
72 /*Used by stack_init to avoid double initialize each entry.*/
73 IL_CODE_FLAG_STACK_INITED = 4,
74 /*Used by merge_stacks to decide if it should just copy the eval stack.*/
75 IL_CODE_STACK_MERGED = 8,
76 /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
77 IL_CODE_DELEGATE_SEQUENCE = 0x10,
78 /*This is a delegate created from a ldftn to a non final virtual method*/
79 IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
108 /*Allocated fnptr MonoType that should be freed by us.*/
114 /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
118 MonoMethodSignature *signature;
119 MonoMethodHeader *header;
121 MonoGenericContext *generic_context;
125 /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
126 *on a method that creates a delegate for a non-final virtual method using ldftn*/
127 gboolean has_this_store;
131 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start, gboolean external);
133 //////////////////////////////////////////////////////////////////
138 TYPE_INV = 0, /* leave at 0. */
143 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
145 /* value types and classes */
147 /* Number of types, used to define the size of the tables*/
150 /* Used by tables to signal that a result is not verifiable*/
151 NON_VERIFIABLE_RESULT = 0x80,
153 /*Mask used to extract just the type, excluding flags */
156 /* The stack type is a managed pointer, unmask the value to res */
157 POINTER_MASK = 0x100,
159 /*Stack type with the pointer mask*/
160 RAW_TYPE_MASK = 0x10F,
162 /* Controlled Mutability Manager Pointer */
165 /* The stack type is a null literal*/
166 NULL_LITERAL_MASK = 0x400,
168 /**Used by ldarg.0 and family to let delegate verification happens.*/
169 THIS_POINTER_MASK = 0x800,
171 /**Signals that this is a boxed value type*/
176 static const char* const
177 type_names [TYPE_MAX + 1] = {
188 PREFIX_UNALIGNED = 1,
191 PREFIX_ADDR_MASK = 3,
194 //////////////////////////////////////////////////////////////////
197 /*Token validation macros and functions */
198 #define IS_MEMBER_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
199 #define IS_METHOD_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
200 #define IS_METHOD_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_METHODSPEC)
202 #define IS_TYPE_REF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEREF)
203 #define IS_TYPE_DEF(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
204 #define IS_TYPE_SPEC(token) (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC)
205 #define IS_METHOD_DEF_OR_REF_OR_SPEC(token) (IS_METHOD_DEF (token) || IS_MEMBER_REF (token) || IS_METHOD_SPEC (token))
206 #define IS_TYPE_DEF_OR_REF_OR_SPEC(token) (IS_TYPE_DEF (token) || IS_TYPE_REF (token) || IS_TYPE_SPEC (token))
209 * Verify if @token refers to a valid row on int's table.
212 token_bounds_check (MonoImage *image, guint32 token)
214 return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token);
218 mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
220 MonoType *res = g_new0 (MonoType, 1);
221 res->data.method = mono_method_signature (method);
222 res->type = MONO_TYPE_FNPTR;
223 ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
227 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME)
228 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
231 mono_method_is_constructor (MonoMethod *method)
233 return ((method->flags & CTOR_REQUIRED_FLAGS) == CTOR_REQUIRED_FLAGS &&
234 !(method->flags & CTOR_INVALID_FLAGS) &&
235 !strcmp (".ctor", method->name));
239 /* stack_slot_get_type:
241 * Returns the stack type of @value. This value includes POINTER_MASK.
243 * Use this function to checks that account for a managed pointer.
246 stack_slot_get_type (ILStackDesc *value)
248 return value->stype & RAW_TYPE_MASK;
251 /* stack_slot_get_underlying_type:
253 * Returns the stack type of @value. This value does not include POINTER_MASK.
255 * Use this function is cases where the fact that the value could be a managed pointer is
256 * irrelevant. For example, field load doesn't care about this fact of type on stack.
259 stack_slot_get_underlying_type (ILStackDesc *value)
261 return value->stype & TYPE_MASK;
264 /* stack_slot_is_managed_pointer:
266 * Returns TRUE is @value is a managed pointer.
269 stack_slot_is_managed_pointer (ILStackDesc *value)
271 return (value->stype & POINTER_MASK) == POINTER_MASK;
274 /* stack_slot_is_managed_mutability_pointer:
276 * Returns TRUE is @value is a managed mutability pointer.
279 stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
281 return (value->stype & CMMP_MASK) == CMMP_MASK;
284 /* stack_slot_is_null_literal:
286 * Returns TRUE is @value is the null literal.
289 stack_slot_is_null_literal (ILStackDesc *value)
291 return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
295 /* stack_slot_is_this_pointer:
297 * Returns TRUE is @value is the this literal
300 stack_slot_is_this_pointer (ILStackDesc *value)
302 return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
305 /* stack_slot_is_boxed_value:
307 * Returns TRUE is @value is a boxed value
310 stack_slot_is_boxed_value (ILStackDesc *value)
312 return (value->stype & BOXED_MASK) == BOXED_MASK;
316 stack_slot_get_name (ILStackDesc *value)
318 return type_names [value->stype & TYPE_MASK];
320 //////////////////////////////////////////////////////////////////
322 mono_free_verify_list (GSList *list)
324 MonoVerifyInfo *info;
327 for (tmp = list; tmp; tmp = tmp->next) {
329 g_free (info->message);
335 #define ADD_ERROR(list,msg) \
337 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
338 vinfo->status = MONO_VERIFY_ERROR; \
339 vinfo->message = (msg); \
340 (list) = g_slist_prepend ((list), vinfo); \
343 #define ADD_WARN(list,code,msg) \
345 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
346 vinfo->status = (code); \
347 vinfo->message = (msg); \
348 (list) = g_slist_prepend ((list), vinfo); \
352 valid_cultures[][9] = {
353 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
354 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
355 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
356 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
357 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
358 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
359 "da-DK", "de-DE", "de-CH", "de-AT",
360 "de-LU", "de-LI", "el-GR", "en-US",
361 "en-GB", "en-AU", "en-CA", "en-NZ",
362 "en-IE", "en-ZA", "en-JM", "en-CB",
363 "en-BZ", "en-TT", "en-ZW", "en-PH",
364 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
365 "es-CR", "es-PA", "es-DO", "es-VE",
366 "es-CO", "es-PE", "es-AR", "es-EC",
367 "es-CL", "es-UY", "es-PY", "es-BO",
368 "es-SV", "es-HN", "es-NI", "es-PR",
369 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
370 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
371 "hu-HU", "is-IS", "it-IT", "it-CH",
372 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
373 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
374 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
375 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
376 "sv-SE", "sv-FI", "th-TH", "tr-TR",
377 "ur-PK", "id-ID", "uk-UA", "be-BY",
378 "sl-SI", "et-EE", "lv-LV", "lt-LT",
379 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
381 "eu-ES", "mk-MK", "af-ZA",
382 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
383 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
384 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
385 "gu-IN", "ta-IN", "te-IN", "kn-IN",
386 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
387 "kok-IN", "syr-SY", "div-MV"
391 is_valid_culture (const char *cname)
397 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
398 if (g_strcasecmp (valid_cultures [i], cname)) {
407 is_valid_assembly_flags (guint32 flags) {
408 /* Metadata: 22.1.2 */
409 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
410 return ((flags == 1) || (flags == 0));
414 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
417 const char *p, *blob_end;
419 if (blob_index >= image->heap_blob.size)
421 p = mono_metadata_blob_heap (image, blob_index);
422 size = mono_metadata_decode_blob_size (p, &blob_end);
423 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
425 if (notnull && !size)
431 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
433 const char *p, *blob_end, *res;
435 if (str_index >= image->heap_strings.size)
437 res = p = mono_metadata_string_heap (image, str_index);
438 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
442 * FIXME: should check it's a valid utf8 string, too.
444 while (p <= blob_end) {
449 return *p? NULL: res;
453 is_valid_cls_ident (const char *p)
456 * FIXME: we need the full unicode glib support for this.
457 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
458 * We do the lame thing for now.
464 if (!isalnum (*p) && *p != '_')
472 is_valid_filename (const char *p)
476 return strpbrk (p, "\\//:")? 0: 1;
480 verify_assembly_table (MonoImage *image, GSList *list, int level)
482 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
483 guint32 cols [MONO_ASSEMBLY_SIZE];
486 if (level & MONO_VERIFY_ERROR) {
488 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
489 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
491 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
492 case ASSEMBLY_HASH_NONE:
493 case ASSEMBLY_HASH_MD5:
494 case ASSEMBLY_HASH_SHA1:
497 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
500 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
501 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
503 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
504 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
506 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
507 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
509 if (strpbrk (p, ":\\/."))
510 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
513 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
514 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
516 if (!is_valid_culture (p))
517 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
524 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
526 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
527 guint32 cols [MONO_ASSEMBLYREF_SIZE];
531 if (level & MONO_VERIFY_ERROR) {
532 for (i = 0; i < t->rows; ++i) {
533 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
534 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
535 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
537 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
538 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
540 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
541 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
543 if (!is_valid_culture (p))
544 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
547 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
548 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
551 if (level & MONO_VERIFY_WARNING) {
552 /* check for duplicated rows */
553 for (i = 0; i < t->rows; ++i) {
560 verify_class_layout_table (MonoImage *image, GSList *list, int level)
562 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
563 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
564 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
567 if (level & MONO_VERIFY_ERROR) {
568 for (i = 0; i < t->rows; ++i) {
569 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
571 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
572 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
574 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
575 if (value & TYPE_ATTRIBUTE_INTERFACE)
576 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
577 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
578 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
579 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
580 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
581 case 0: case 1: case 2: case 4: case 8: case 16:
582 case 32: case 64: case 128: break;
584 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
586 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
588 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
589 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
590 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));
594 * FIXME: we need to check that if class size != 0,
595 * it needs to be greater than the class calculated size.
596 * If parent is a valuetype it also needs to be smaller than
597 * 1 MByte (0x100000 bytes).
598 * To do both these checks we need to load the referenced
599 * assemblies, though (the spec claims we didn't have to, bah).
602 * We need to check that the parent types have the same layout
613 verify_constant_table (MonoImage *image, GSList *list, int level)
615 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
616 guint32 cols [MONO_CONSTANT_SIZE];
618 GHashTable *dups = g_hash_table_new (NULL, NULL);
620 for (i = 0; i < t->rows; ++i) {
621 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
623 if (level & MONO_VERIFY_ERROR)
624 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
625 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
626 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
627 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
629 switch (cols [MONO_CONSTANT_TYPE]) {
630 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
634 if (level & MONO_VERIFY_CLS)
635 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));
636 case MONO_TYPE_BOOLEAN:
644 case MONO_TYPE_STRING:
645 case MONO_TYPE_CLASS:
648 if (level & MONO_VERIFY_ERROR)
649 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
651 if (level & MONO_VERIFY_ERROR) {
652 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
653 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
654 case MONO_HASCONSTANT_FIEDDEF:
655 if (value > image->tables [MONO_TABLE_FIELD].rows)
656 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
658 case MONO_HASCONSTANT_PARAM:
659 if (value > image->tables [MONO_TABLE_PARAM].rows)
660 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
662 case MONO_HASCONSTANT_PROPERTY:
663 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
664 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
667 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
671 if (level & MONO_VERIFY_CLS) {
673 * FIXME: verify types is consistent with the enum type
674 * is parent is an enum.
678 g_hash_table_destroy (dups);
683 verify_event_map_table (MonoImage *image, GSList *list, int level)
685 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
686 guint32 cols [MONO_EVENT_MAP_SIZE];
687 guint32 i, last_event;
688 GHashTable *dups = g_hash_table_new (NULL, NULL);
692 for (i = 0; i < t->rows; ++i) {
693 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
694 if (level & MONO_VERIFY_ERROR)
695 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
696 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
697 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
698 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
699 if (level & MONO_VERIFY_ERROR) {
700 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
701 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
702 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
703 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
705 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
706 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
707 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
711 g_hash_table_destroy (dups);
716 verify_event_table (MonoImage *image, GSList *list, int level)
718 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
719 guint32 cols [MONO_EVENT_SIZE];
723 for (i = 0; i < t->rows; ++i) {
724 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
726 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
727 if (level & MONO_VERIFY_ERROR)
728 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
730 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
731 if (level & MONO_VERIFY_ERROR)
732 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
734 if (level & MONO_VERIFY_CLS) {
735 if (!is_valid_cls_ident (p))
736 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
740 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
741 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
742 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
743 case MONO_TYPEDEFORREF_TYPEDEF:
744 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
745 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
747 case MONO_TYPEDEFORREF_TYPEREF:
748 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
749 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
751 case MONO_TYPEDEFORREF_TYPESPEC:
752 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
753 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
756 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
760 * FIXME: check that there is 1 add and remove row in methodsemantics
761 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
762 * these while checking methodsemantics).
763 * check for duplicated names for the same type [ERROR]
764 * check for CLS duplicate names for the same type [CLS]
771 verify_field_table (MonoImage *image, GSList *list, int level)
773 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
774 guint32 cols [MONO_FIELD_SIZE];
778 for (i = 0; i < t->rows; ++i) {
779 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
781 * Check this field has only one owner and that the owner is not
782 * an interface (done in verify_typedef_table() )
784 flags = cols [MONO_FIELD_FLAGS];
785 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
786 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
787 case FIELD_ATTRIBUTE_PRIVATE:
788 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
789 case FIELD_ATTRIBUTE_ASSEMBLY:
790 case FIELD_ATTRIBUTE_FAMILY:
791 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
792 case FIELD_ATTRIBUTE_PUBLIC:
795 if (level & MONO_VERIFY_ERROR)
796 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
799 if (level & MONO_VERIFY_ERROR) {
800 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
801 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
802 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
803 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
804 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
805 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
807 * FIXME: check there is only one owner in the respective table.
808 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
809 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
810 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
813 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
814 if (level & MONO_VERIFY_ERROR)
815 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
817 if (level & MONO_VERIFY_CLS) {
818 if (!is_valid_cls_ident (p))
819 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
824 * if owner is module needs to be static, access mask needs to be compilercontrolled,
825 * public or private (not allowed in cls mode).
826 * if owner is an enum ...
835 verify_file_table (MonoImage *image, GSList *list, int level)
837 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
838 guint32 cols [MONO_FILE_SIZE];
841 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
843 for (i = 0; i < t->rows; ++i) {
844 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
845 if (level & MONO_VERIFY_ERROR) {
846 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
847 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
848 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
849 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
851 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
852 if (level & MONO_VERIFY_ERROR)
853 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
855 if (level & MONO_VERIFY_ERROR) {
856 if (!is_valid_filename (p))
857 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
858 else if (g_hash_table_lookup (dups, p)) {
859 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
861 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
865 * FIXME: I don't understand what this means:
866 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
867 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
871 if (level & MONO_VERIFY_WARNING) {
872 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
873 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
875 g_hash_table_destroy (dups);
880 verify_moduleref_table (MonoImage *image, GSList *list, int level)
882 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
883 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
884 guint32 cols [MONO_MODULEREF_SIZE];
886 guint32 found, i, j, value;
887 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
889 for (i = 0; i < t->rows; ++i) {
890 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
891 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
892 if (level & MONO_VERIFY_ERROR)
893 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
895 if (level & MONO_VERIFY_ERROR) {
896 if (!is_valid_filename (p))
897 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
898 else if (g_hash_table_lookup (dups, p)) {
899 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
900 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
902 for (j = 0; j < tfile->rows; ++j) {
903 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
904 if ((pf = is_valid_string (image, value, TRUE)))
905 if (strcmp (p, pf) == 0) {
911 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
916 g_hash_table_destroy (dups);
921 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
923 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
924 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
928 for (i = 0; i < t->rows; ++i) {
929 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
930 if (level & MONO_VERIFY_ERROR) {
931 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
932 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
934 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
935 /* FIXME: check it's a valid locals or method sig.*/
943 mono_image_verify_tables (MonoImage *image, int level)
945 GSList *error_list = NULL;
947 error_list = verify_assembly_table (image, error_list, level);
949 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
950 * AssemblyRefProcessor should be ignored,
951 * though we may want to emit a warning, since it should not
952 * be present in a PE file.
954 error_list = verify_assemblyref_table (image, error_list, level);
955 error_list = verify_class_layout_table (image, error_list, level);
956 error_list = verify_constant_table (image, error_list, level);
958 * cutom attribute, declsecurity
960 error_list = verify_event_map_table (image, error_list, level);
961 error_list = verify_event_table (image, error_list, level);
962 error_list = verify_field_table (image, error_list, level);
963 error_list = verify_file_table (image, error_list, level);
964 error_list = verify_moduleref_table (image, error_list, level);
965 error_list = verify_standalonesig_table (image, error_list, level);
967 return g_slist_reverse (error_list);
970 #define ADD_INVALID(list,msg) \
972 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
973 vinfo->status = MONO_VERIFY_ERROR; \
974 vinfo->message = (msg); \
975 (list) = g_slist_prepend ((list), vinfo); \
976 /*G_BREAKPOINT ();*/ \
980 #define CHECK_STACK_UNDERFLOW(num) \
982 if (cur_stack < (num)) \
983 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
986 #define CHECK_STACK_OVERFLOW() \
988 if (cur_stack >= max_stack) \
989 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
994 in_any_block (MonoMethodHeader *header, guint offset)
997 MonoExceptionClause *clause;
999 for (i = 0; i < header->num_clauses; ++i) {
1000 clause = &header->clauses [i];
1001 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1003 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1005 if (MONO_OFFSET_IN_FILTER (clause, offset))
1012 * in_any_exception_block:
1014 * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
1017 in_any_exception_block (MonoMethodHeader *header, guint offset)
1020 MonoExceptionClause *clause;
1022 for (i = 0; i < header->num_clauses; ++i) {
1023 clause = &header->clauses [i];
1024 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1026 if (MONO_OFFSET_IN_FILTER (clause, offset))
1033 * is_valid_branch_instruction:
1035 * Verify if it's valid to perform a branch from @offset to @target.
1036 * This should be used with br and brtrue/false.
1037 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1038 * The major diferent from other similiar functions is that branching into a
1039 * finally/fault block is invalid instead of just unverifiable.
1042 is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1045 MonoExceptionClause *clause;
1047 for (i = 0; i < header->num_clauses; ++i) {
1048 clause = &header->clauses [i];
1049 /*branching into a finally block is invalid*/
1050 if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
1051 !MONO_OFFSET_IN_HANDLER (clause, offset) &&
1052 MONO_OFFSET_IN_HANDLER (clause, target))
1055 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1057 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1059 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1066 * is_valid_cmp_branch_instruction:
1068 * Verify if it's valid to perform a branch from @offset to @target.
1069 * This should be used with binary comparison branching instruction, like beq, bge and similars.
1070 * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
1072 * The major diferences from other similar functions are that most errors lead to invalid
1073 * code and only branching out of finally, filter or fault clauses is unverifiable.
1076 is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
1079 MonoExceptionClause *clause;
1081 for (i = 0; i < header->num_clauses; ++i) {
1082 clause = &header->clauses [i];
1083 /*branching out of a handler or finally*/
1084 if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
1085 MONO_OFFSET_IN_HANDLER (clause, offset) &&
1086 !MONO_OFFSET_IN_HANDLER (clause, target))
1089 if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
1091 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1093 if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
1100 * A leave can't escape a finally block
1103 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1106 MonoExceptionClause *clause;
1108 for (i = 0; i < header->num_clauses; ++i) {
1109 clause = &header->clauses [i];
1110 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1112 if (MONO_OFFSET_IN_FILTER (clause, offset))
1119 * A rethrow can't happen outside of a catch handler.
1122 is_correct_rethrow (MonoMethodHeader *header, guint offset)
1125 MonoExceptionClause *clause;
1127 for (i = 0; i < header->num_clauses; ++i) {
1128 clause = &header->clauses [i];
1129 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1131 if (MONO_OFFSET_IN_FILTER (clause, offset))
1138 * An endfinally can't happen outside of a finally/fault handler.
1141 is_correct_endfinally (MonoMethodHeader *header, guint offset)
1144 MonoExceptionClause *clause;
1146 for (i = 0; i < header->num_clauses; ++i) {
1147 clause = &header->clauses [i];
1148 if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
1156 * An endfilter can only happens inside a filter clause.
1157 * In non-strict mode filter is allowed inside the handler clause too
1159 static MonoExceptionClause *
1160 is_correct_endfilter (VerifyContext *ctx, guint offset)
1163 MonoExceptionClause *clause;
1165 for (i = 0; i < ctx->header->num_clauses; ++i) {
1166 clause = &ctx->header->clauses [i];
1167 if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
1169 if (MONO_OFFSET_IN_FILTER (clause, offset))
1171 if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
1179 * Non-strict endfilter can happens inside a try block or any handler block
1182 is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
1185 MonoExceptionClause *clause;
1187 for (i = 0; i < ctx->header->num_clauses; ++i) {
1188 clause = &ctx->header->clauses [i];
1189 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1196 is_valid_bool_arg (ILStackDesc *arg)
1198 if (stack_slot_is_managed_pointer (arg))
1200 switch (stack_slot_get_underlying_type (arg)) {
1203 case TYPE_NATIVE_INT:
1207 g_assert (arg->type);
1208 switch (arg->type->type) {
1209 case MONO_TYPE_CLASS:
1210 case MONO_TYPE_STRING:
1211 case MONO_TYPE_OBJECT:
1212 case MONO_TYPE_SZARRAY:
1213 case MONO_TYPE_ARRAY:
1214 case MONO_TYPE_FNPTR:
1217 case MONO_TYPE_GENERICINST:
1218 /*We need to check if the container class
1219 * of the generic type is a valuetype, iow:
1220 * is it a "class Foo<T>" or a "struct Foo<T>"?
1222 return !arg->type->data.generic_class->container_class->valuetype;
1230 /*Type manipulation helper*/
1232 /*Returns the byref version of the supplied MonoType*/
1234 mono_type_get_type_byref (MonoType *type)
1238 return &mono_class_from_mono_type (type)->this_arg;
1242 /*Returns the byval version of the supplied MonoType*/
1244 mono_type_get_type_byval (MonoType *type)
1248 return &mono_class_from_mono_type (type)->byval_arg;
1252 mono_type_from_stack_slot (ILStackDesc *slot)
1254 if (stack_slot_is_managed_pointer (slot))
1255 return mono_type_get_type_byref (slot->type);
1259 /*Stack manipulation code*/
1262 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1264 if (state->flags & IL_CODE_FLAG_STACK_INITED)
1267 state->flags |= IL_CODE_FLAG_STACK_INITED;
1269 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1273 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1275 to->size = from->size;
1276 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1280 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1282 to->stype = from->stype;
1283 to->type = from->type;
1287 check_underflow (VerifyContext *ctx, int size)
1289 if (ctx->eval.size < size) {
1290 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1297 check_overflow (VerifyContext *ctx)
1299 if (ctx->eval.size >= ctx->max_stack) {
1300 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1307 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1309 if (stack_slot_get_type (value) == TYPE_PTR) {
1310 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1317 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1319 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1320 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1327 static ILStackDesc *
1328 stack_push (VerifyContext *ctx)
1330 return & ctx->eval.stack [ctx->eval.size++];
1333 static ILStackDesc *
1334 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1336 ILStackDesc *top = stack_push (ctx);
1342 static ILStackDesc *
1343 stack_pop (VerifyContext *ctx)
1345 return ctx->eval.stack + --ctx->eval.size;
1348 static inline ILStackDesc *
1349 stack_top (VerifyContext *ctx)
1351 return ctx->eval.stack + (ctx->eval.size - 1);
1354 static inline ILStackDesc *
1355 stack_get (VerifyContext *ctx, int distance)
1357 return ctx->eval.stack + (ctx->eval.size - distance - 1);
1360 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1362 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1365 get_boxable_mono_type (VerifyContext* ctx, int token)
1368 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
1369 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset));
1373 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
1376 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
1380 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1381 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
1385 if (type->type == MONO_TYPE_VOID) {
1386 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
1390 if (type->type == MONO_TYPE_TYPEDBYREF)
1391 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref at 0x%04x", ctx->ip_offset));
1393 check_unverifiable_type (ctx, type);
1398 /*operation result tables */
1400 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1401 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1402 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1403 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1404 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1405 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1406 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1409 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1410 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1411 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1412 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1413 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1414 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1415 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1418 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1419 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1420 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1421 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1422 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1423 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1424 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1427 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1428 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1429 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1430 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1431 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1432 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1433 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1436 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1437 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1438 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1439 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1440 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1441 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1442 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1445 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1446 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1447 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1448 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1449 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1450 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1451 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1454 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1455 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1456 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1457 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1458 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1459 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1460 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1463 static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1464 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1465 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1466 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1467 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1468 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1469 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1472 static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
1473 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1474 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1475 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1476 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1477 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1478 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1481 static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
1482 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1483 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1484 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1485 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1486 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1487 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1493 dump_stack_value (ILStackDesc *value)
1495 printf ("[(%x)(%x)", value->type->type, value->stype);
1497 if (stack_slot_is_this_pointer (value))
1500 if (stack_slot_is_boxed_value (value))
1501 printf ("[boxed] ");
1503 if (stack_slot_is_null_literal (value))
1506 if (stack_slot_is_managed_mutability_pointer (value))
1507 printf ("Controled Mutability MP: ");
1509 if (stack_slot_is_managed_pointer (value))
1510 printf ("Managed Pointer to: ");
1512 switch (stack_slot_get_underlying_type (value)) {
1514 printf ("invalid type]");
1522 case TYPE_NATIVE_INT:
1523 printf ("native int]");
1526 printf ("float64]");
1529 printf ("unmanaged pointer]");
1532 switch (value->type->type) {
1533 case MONO_TYPE_CLASS:
1534 case MONO_TYPE_VALUETYPE:
1535 printf ("complex] (%s)", value->type->data.klass->name);
1537 case MONO_TYPE_STRING:
1538 printf ("complex] (string)");
1540 case MONO_TYPE_OBJECT:
1541 printf ("complex] (object)");
1543 case MONO_TYPE_SZARRAY:
1544 printf ("complex] (%s [])", value->type->data.klass->name);
1546 case MONO_TYPE_ARRAY:
1547 printf ("complex] (%s [%d %d %d])",
1548 value->type->data.array->eklass->name,
1549 value->type->data.array->rank,
1550 value->type->data.array->numsizes,
1551 value->type->data.array->numlobounds);
1553 case MONO_TYPE_GENERICINST:
1554 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1557 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1559 case MONO_TYPE_MVAR:
1560 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1563 //should be a boxed value
1564 char * name = mono_type_full_name (value->type);
1565 printf ("complex] %s", name);
1571 printf ("unknown stack %x type]\n", value->stype);
1572 g_assert_not_reached ();
1577 dump_stack_state (ILCodeDesc *state)
1581 printf ("(%d) ", state->size);
1582 for (i = 0; i < state->size; ++i)
1583 dump_stack_value (state->stack + i);
1587 /*Returns TRUE if candidate array type can be assigned to target.
1588 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1591 is_array_type_compatible (MonoType *target, MonoType *candidate)
1594 MonoArrayType *left = target->data.array;
1595 MonoArrayType *right = candidate->data.array;
1597 g_assert (target->type == MONO_TYPE_ARRAY);
1598 g_assert (candidate->type == MONO_TYPE_ARRAY);
1601 if ((left->rank != right->rank) ||
1602 (left->numsizes != right->numsizes) ||
1603 (left->numlobounds != right->numlobounds))
1606 for (i = 0; i < left->numsizes; ++i)
1607 if (left->sizes [i] != right->sizes [i])
1610 for (i = 0; i < left->numlobounds; ++i)
1611 if (left->lobounds [i] != right->lobounds [i])
1614 return mono_class_is_assignable_from (left->eklass, right->eklass);
1618 get_stack_type (MonoType *type)
1621 int type_kind = type->type;
1623 mask = POINTER_MASK;
1624 /*TODO handle CMMP_MASK */
1627 switch (type_kind) {
1630 case MONO_TYPE_BOOLEAN:
1633 case MONO_TYPE_CHAR:
1636 return TYPE_I4 | mask;
1640 return TYPE_NATIVE_INT | mask;
1642 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1643 case MONO_TYPE_FNPTR:
1645 case MONO_TYPE_TYPEDBYREF:
1646 return TYPE_PTR | mask;
1649 case MONO_TYPE_MVAR:
1651 case MONO_TYPE_CLASS:
1652 case MONO_TYPE_STRING:
1653 case MONO_TYPE_OBJECT:
1654 case MONO_TYPE_SZARRAY:
1655 case MONO_TYPE_ARRAY:
1656 case MONO_TYPE_GENERICINST:
1657 return TYPE_COMPLEX | mask;
1661 return TYPE_I8 | mask;
1665 return TYPE_R8 | mask;
1667 case MONO_TYPE_VALUETYPE:
1668 if (type->data.klass->enumtype) {
1669 type = type->data.klass->enum_basetype;
1670 type_kind = type->type;
1673 return TYPE_COMPLEX | mask;
1676 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
1677 g_assert_not_reached ();
1682 /* convert MonoType to ILStackDesc format (stype) */
1684 set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
1687 int type_kind = type->type;
1689 if (type->byref || take_addr)
1690 mask = POINTER_MASK;
1691 /* TODO handle CMMP_MASK */
1696 switch (type_kind) {
1699 case MONO_TYPE_BOOLEAN:
1702 case MONO_TYPE_CHAR:
1705 stack->stype = TYPE_I4 | mask;
1709 stack->stype = TYPE_NATIVE_INT | mask;
1712 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1713 case MONO_TYPE_FNPTR:
1715 case MONO_TYPE_TYPEDBYREF:
1716 stack->stype = TYPE_PTR | mask;
1719 case MONO_TYPE_CLASS:
1720 case MONO_TYPE_STRING:
1721 case MONO_TYPE_OBJECT:
1722 case MONO_TYPE_SZARRAY:
1723 case MONO_TYPE_ARRAY:
1725 case MONO_TYPE_GENERICINST:
1727 case MONO_TYPE_MVAR:
1728 stack->stype = TYPE_COMPLEX | mask;
1732 stack->stype = TYPE_I8 | mask;
1736 stack->stype = TYPE_R8 | mask;
1738 case MONO_TYPE_VALUETYPE:
1739 if (type->data.klass->enumtype) {
1740 type = type->data.klass->enum_basetype;
1741 type_kind = type->type;
1744 stack->stype = TYPE_COMPLEX | mask;
1748 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1749 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
1756 * init_stack_with_value_at_exception_boundary:
1758 * Initialize the stack and push a given type.
1759 * The instruction is marked as been on the exception boundary.
1762 init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
1764 stack_init (ctx, code);
1765 set_stack_value (ctx, code->stack, &klass->byval_arg, FALSE);
1767 code->flags |= IL_CODE_FLAG_WAS_TARGET;
1770 /* Generics validation stuff, should be moved to another metadata/? file */
1772 mono_is_generic_type_compatible (MonoType *target, MonoType *candidate)
1774 if (target->byref != candidate->byref)
1778 switch (target->type) {
1779 case MONO_TYPE_STRING:
1780 if (candidate->type == MONO_TYPE_STRING)
1784 case MONO_TYPE_CLASS:
1785 if (candidate->type != MONO_TYPE_CLASS)
1788 VERIFIER_DEBUG ( printf ("verifying type class %p %p\n", target->data.klass, candidate->data.klass); );
1789 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1791 case MONO_TYPE_OBJECT:
1792 return MONO_TYPE_IS_REFERENCE (candidate);
1794 case MONO_TYPE_SZARRAY:
1795 if (candidate->type != MONO_TYPE_SZARRAY)
1797 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1799 case MONO_TYPE_VALUETYPE:
1800 if (target->data.klass->enumtype) {
1801 target = target->data.klass->enum_basetype;
1804 if (candidate->type != MONO_TYPE_VALUETYPE)
1806 return candidate->data.klass == target->data.klass;
1809 case MONO_TYPE_ARRAY:
1810 if (candidate->type != MONO_TYPE_ARRAY)
1812 return is_array_type_compatible (target, candidate);
1815 VERIFIER_DEBUG ( printf ("unknown target type %d\n", target->type); );
1816 g_assert_not_reached ();
1824 mono_is_generic_instance_compatible (MonoGenericClass *target, MonoGenericClass *candidate, MonoGenericClass *root_candidate) {
1825 MonoGenericContainer *container;
1828 VERIFIER_DEBUG ( printf ("candidate container %p\n", candidate->container_class->generic_container); );
1829 if (target->container_class != candidate->container_class) {
1830 MonoType *param_class;
1831 MonoClass *cand_class;
1832 VERIFIER_DEBUG ( printf ("generic class != target\n"); );
1833 param_class = candidate->context.class_inst->type_argv [0];
1834 VERIFIER_DEBUG ( printf ("param 0 %d\n", param_class->type); );
1835 cand_class = candidate->container_class;
1837 /* We must check if it's an interface type*/
1838 if (MONO_CLASS_IS_INTERFACE (target->container_class)) {
1839 VERIFIER_DEBUG ( printf ("generic type is an interface\n"); );
1842 int iface_count = cand_class->interface_count;
1843 MonoClass **ifaces = cand_class->interfaces;
1845 VERIFIER_DEBUG ( printf ("type has %d interfaces\n", iface_count); );
1846 for (i = 0; i< iface_count; ++i) {
1847 MonoClass *ifc = ifaces[i];
1848 VERIFIER_DEBUG ( printf ("analysing %s\n", ifc->name); );
1849 if (ifc->generic_class) {
1850 VERIFIER_DEBUG ( printf ("interface has generic info\n"); );
1852 if (mono_is_generic_instance_compatible (target, ifc->generic_class, root_candidate)) {
1853 VERIFIER_DEBUG ( printf ("we got compatible stuff!\n"); );
1858 cand_class = cand_class->parent;
1859 } while (cand_class);
1861 VERIFIER_DEBUG ( printf ("don't implements an interface\n"); );
1864 VERIFIER_DEBUG ( printf ("verifying upper classes\n"); );
1866 cand_class = cand_class->parent;
1868 while (cand_class) {
1869 VERIFIER_DEBUG ( printf ("verifying parent class name %s\n", cand_class->name); );
1870 if (cand_class->generic_class) {
1871 VERIFIER_DEBUG ( printf ("super type has generic context\n"); );
1873 /* TODO break loop if target->container_class == cand_class->generic_class->container_class */
1874 return mono_is_generic_instance_compatible (target, cand_class->generic_class, root_candidate);
1876 VERIFIER_DEBUG ( printf ("super class has no generic context\n"); );
1877 cand_class = cand_class->parent;
1883 /* now we verify if the instantiations are compatible*/
1884 if (target->context.class_inst == candidate->context.class_inst) {
1885 VERIFIER_DEBUG ( printf ("generic types are compatible, both have the same instantiation\n"); );
1889 if (target->context.class_inst->type_argc != candidate->context.class_inst->type_argc) {
1890 VERIFIER_DEBUG ( printf ("generic instantiations with different arg counts\n"); );
1894 //verify if open instance -- none should be
1896 container = target->container_class->generic_container;
1898 for (i = 0; i < container->type_argc; ++i) {
1899 MonoGenericParam *param = container->type_params + i;
1900 MonoType *target_type = target->context.class_inst->type_argv [i];
1901 MonoType *candidate_type = candidate->context.class_inst->type_argv [i];
1902 /* We resolve TYPE_VAR types before proceeding */
1904 if (candidate_type->type == MONO_TYPE_VAR) {
1905 MonoGenericParam *var_param = candidate_type->data.generic_param;
1906 candidate_type = root_candidate->context.class_inst->type_argv [var_param->num];
1909 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK) == 0) {
1910 VERIFIER_DEBUG ( printf ("generic type have no variance flag, checking each type %d %d \n",target_type->type, candidate_type->type); );
1912 if (!mono_metadata_type_equal (target_type, candidate_type))
1915 VERIFIER_DEBUG ( printf ("generic type has variance flag, need to perform deeper check\n"); );
1916 /* first we check if they are the same kind */
1917 /* byref generic params are forbiden, but better safe than sorry.*/
1919 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) == GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) {
1920 if (!mono_is_generic_type_compatible (target_type, candidate_type))
1922 /* the attribute must be contravariant */
1923 } else if (!mono_is_generic_type_compatible (candidate_type, target_type))
1932 /*Verify if type 'candidate' can be stored in type 'target'.
1934 * If strict, check for the underlying type and not the verification stack types
1937 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1939 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1940 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1942 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); );
1944 /*only one is byref */
1945 if (candidate->byref ^ target->byref) {
1946 /* converting from native int to byref*/
1947 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1948 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1953 strict |= target->byref;
1956 switch (target->type) {
1959 case MONO_TYPE_BOOLEAN:
1961 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1964 case MONO_TYPE_CHAR:
1966 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1968 case MONO_TYPE_U4: {
1969 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1970 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1972 return is_native_int || is_int4;
1973 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1978 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
1983 return candidate->type == target->type;
1984 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
1988 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1989 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1991 return is_native_int || is_int4;
1992 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1996 if (candidate->type != MONO_TYPE_PTR)
1998 /* check the underlying type */
1999 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2001 case MONO_TYPE_FNPTR: {
2002 MonoMethodSignature *left, *right;
2003 if (candidate->type != MONO_TYPE_FNPTR)
2006 left = mono_type_get_signature (target);
2007 right = mono_type_get_signature (candidate);
2008 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2011 case MONO_TYPE_GENERICINST: {
2012 MonoGenericClass *left;
2013 MonoGenericClass *right;
2014 if (candidate->type != MONO_TYPE_GENERICINST)
2015 return mono_class_is_assignable_from (mono_class_from_mono_type (target), mono_class_from_mono_type (candidate));
2016 left = target->data.generic_class;
2017 right = candidate->data.generic_class;
2019 return mono_is_generic_instance_compatible (left, right, right);
2022 case MONO_TYPE_STRING:
2023 return candidate->type == MONO_TYPE_STRING;
2025 case MONO_TYPE_CLASS:
2026 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (candidate));
2028 case MONO_TYPE_OBJECT:
2029 return MONO_TYPE_IS_REFERENCE (candidate);
2031 case MONO_TYPE_SZARRAY: {
2034 if (candidate->type != MONO_TYPE_SZARRAY)
2037 left = target->data.klass;
2038 right = candidate->data.klass;
2039 return mono_class_is_assignable_from(left, right);
2042 case MONO_TYPE_ARRAY:
2043 if (candidate->type != MONO_TYPE_ARRAY)
2045 return is_array_type_compatible (target, candidate);
2047 //TODO verify aditional checks that needs to be done
2048 case MONO_TYPE_TYPEDBYREF:
2049 return candidate->type == MONO_TYPE_TYPEDBYREF;
2051 case MONO_TYPE_VALUETYPE:
2052 if (candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass)
2054 if (target->data.klass->enumtype) {
2055 target = target->data.klass->enum_basetype;
2061 if (candidate->type != MONO_TYPE_VAR)
2063 return candidate->data.generic_param->num == target->data.generic_param->num;
2065 case MONO_TYPE_MVAR:
2066 if (candidate->type != MONO_TYPE_MVAR)
2068 return candidate->data.generic_param->num == target->data.generic_param->num;
2071 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2072 g_assert_not_reached ();
2081 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2083 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2088 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2090 MonoType *candidate = mono_type_from_stack_slot (stack);
2091 if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
2094 if (type->type == MONO_TYPE_OBJECT && mono_class_from_mono_type (candidate)->valuetype && !candidate->byref && stack_slot_is_boxed_value (stack))
2097 return verify_type_compatibility_full (ctx, type, candidate, FALSE);
2101 * mono_delegate_signature_equal:
2103 * Compare two signatures in the way expected by delegates.
2105 * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
2107 * FIXME can this function be eliminated and proper metadata functionality be used?
2110 mono_delegate_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2114 //FIXME do we need to check for explicit_this?
2115 //We don't check for has_this since this is irrelevant for delegate signatures
2116 if (sig1->param_count != sig2->param_count)
2119 if (sig1->generic_param_count != sig2->generic_param_count)
2122 for (i = 0; i < sig1->param_count; i++) {
2123 MonoType *p1 = sig1->params [i];
2124 MonoType *p2 = sig2->params [i];
2126 if (!mono_metadata_type_equal_full (p1, p2, TRUE))
2130 if (!mono_metadata_type_equal_full (sig1->ret, sig2->ret, TRUE))
2137 * verify_ldftn_delegate:
2139 * Verify properties of ldftn based delegates.
2142 verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2144 MonoMethod *method = funptr->method;
2146 /*ldftn non-final virtuals only allowed if method is not static,
2147 * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
2148 * This rules doesn't apply if the object on stack is a boxed valuetype.
2150 if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !stack_slot_is_boxed_value (value)) {
2151 /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
2152 if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
2153 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
2155 /*current method must not be static*/
2156 if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
2157 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
2159 /*value is the this pointer, loaded using ldarg.0 */
2160 if (!stack_slot_is_this_pointer (value))
2161 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));
2163 ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
2168 * verify_delegate_compatibility:
2170 * Verify delegate creation sequence.
2174 verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
2176 #define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
2177 #define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
2179 MonoMethod *invoke, *method;
2180 const guint8 *ip = ctx->header->code;
2181 guint32 ip_offset = ctx->ip_offset;
2183 if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
2184 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at %d", ctx->ip_offset));
2188 invoke = mono_get_delegate_invoke (delegate);
2189 method = funptr->method;
2191 if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method)))
2192 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer parameter for delegate constructor has diferent signature at %d", ctx->ip_offset));
2195 * Delegate code sequences:
2201 * [-6] ldvirtftn token
2205 if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
2206 verify_ldftn_delegate (ctx, delegate, value, funptr);
2207 } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
2208 ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
2210 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
2212 ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
2215 if (!verify_type_compatibility (ctx, &method->klass->byval_arg, value->type) && !stack_slot_is_null_literal (value))
2216 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
2218 if (stack_slot_get_type (value) != TYPE_COMPLEX)
2219 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
2221 #undef IS_VALID_OPCODE
2222 #undef IS_LOAD_FUN_PTR
2225 /* implement the opcode checks*/
2227 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2229 if (arg >= ctx->max_args) {
2231 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2233 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2234 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2235 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2237 } else if (check_overflow (ctx)) {
2238 /*We must let the value be pushed, otherwise we would get an underflow error*/
2239 check_unverifiable_type (ctx, ctx->params [arg]);
2240 if (ctx->params [arg]->byref && take_addr)
2241 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2242 if (!set_stack_value (ctx, stack_push (ctx), ctx->params [arg], take_addr))
2245 if (arg == 0 && !take_addr && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
2246 stack_top (ctx)->stype |= THIS_POINTER_MASK;
2251 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2253 if (arg >= ctx->num_locals) {
2254 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2255 } else if (check_overflow (ctx)) {
2256 /*We must let the value be pushed, otherwise we would get an underflow error*/
2257 check_unverifiable_type (ctx, ctx->locals [arg]);
2258 if (ctx->locals [arg]->byref && take_addr)
2259 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2261 set_stack_value (ctx, stack_push (ctx), ctx->locals [arg], take_addr);
2266 store_arg (VerifyContext *ctx, guint32 arg)
2270 if (arg >= ctx->max_args) {
2271 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2272 check_underflow (ctx, 1);
2277 if (check_underflow (ctx, 1)) {
2278 value = stack_pop (ctx);
2279 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2280 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2284 ctx->has_this_store = 1;
2288 store_local (VerifyContext *ctx, guint32 arg)
2291 if (arg >= ctx->num_locals) {
2292 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2296 /*TODO verify definite assigment */
2297 if (check_underflow (ctx, 1)) {
2298 value = stack_pop(ctx);
2299 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2300 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in local store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2305 /*FIXME add and sub needs special care here*/
2307 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2310 int idxa, idxb, complexMerge = 0;
2313 if (!check_underflow (ctx, 2))
2315 a = stack_get (ctx, 1);
2316 b = stack_top (ctx);
2318 idxa = stack_slot_get_underlying_type (a);
2319 if (stack_slot_is_managed_pointer (a)) {
2324 idxb = stack_slot_get_underlying_type (b);
2325 if (stack_slot_is_managed_pointer (b)) {
2332 res = table [idxa][idxb];
2334 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2335 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2338 if (res == TYPE_INV) {
2339 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)));
2343 if (res & NON_VERIFIABLE_RESULT) {
2344 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)));
2346 res = res & ~NON_VERIFIABLE_RESULT;
2349 if (complexMerge && res == TYPE_PTR) {
2350 if (complexMerge == 1)
2351 copy_stack_value (stack_top (ctx), a);
2352 else if (complexMerge == 2)
2353 copy_stack_value (stack_top (ctx), b);
2355 * There is no need to merge the type of two pointers.
2356 * The only valid operation is subtraction, that returns a native
2357 * int as result and can be used with any 2 pointer kinds.
2358 * This is valid acording to Patition III 1.1.4
2361 stack_top (ctx)->stype = res;
2367 do_boolean_branch_op (VerifyContext *ctx, int delta)
2369 int target = ctx->ip_offset + delta;
2372 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2374 if (target < 0 || target >= ctx->code_size) {
2375 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2379 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2381 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2384 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2388 ctx->target = target;
2390 if (!check_underflow (ctx, 1))
2393 top = stack_pop (ctx);
2394 if (!is_valid_bool_arg (top))
2395 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));
2397 check_unmanaged_pointer (ctx, top);
2402 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2407 int target = ctx->ip_offset + delta;
2409 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2411 if (target < 0 || target >= ctx->code_size) {
2412 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2416 switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
2418 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2421 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2425 ctx->target = target;
2427 if (!check_underflow (ctx, 2))
2430 b = stack_pop (ctx);
2431 a = stack_pop (ctx);
2433 idxa = stack_slot_get_underlying_type (a);
2434 if (stack_slot_is_managed_pointer (a))
2437 idxb = stack_slot_get_underlying_type (b);
2438 if (stack_slot_is_managed_pointer (b))
2443 res = table [idxa][idxb];
2445 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2446 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2448 if (res == TYPE_INV) {
2449 ADD_VERIFY_ERROR (ctx,
2450 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));
2451 } else if (res & NON_VERIFIABLE_RESULT) {
2452 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));
2453 res = res & ~NON_VERIFIABLE_RESULT;
2458 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
2464 if (!check_underflow (ctx, 2))
2466 b = stack_pop (ctx);
2467 a = stack_pop (ctx);
2469 if (opcode == CEE_CGT_UN) {
2470 if (stack_slot_get_type (a) == TYPE_COMPLEX && stack_slot_get_type (b) == TYPE_COMPLEX) {
2471 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2476 idxa = stack_slot_get_underlying_type (a);
2477 if (stack_slot_is_managed_pointer (a))
2480 idxb = stack_slot_get_underlying_type (b);
2481 if (stack_slot_is_managed_pointer (b))
2486 res = table [idxa][idxb];
2488 if(res == TYPE_INV) {
2489 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));
2490 } else if (res & NON_VERIFIABLE_RESULT) {
2491 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));
2492 res = res & ~NON_VERIFIABLE_RESULT;
2494 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2498 do_ret (VerifyContext *ctx)
2500 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2501 if (ctx->signature->ret->type != MONO_TYPE_VOID) {
2503 if (!check_underflow (ctx, 1))
2506 top = stack_pop(ctx);
2508 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2509 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2514 if (ctx->eval.size > 0) {
2515 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2517 if (in_any_block (ctx->header, ctx->ip_offset))
2518 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2521 /* FIXME: we could just load the signature instead of the whole MonoMethod
2522 * TODO handle vararg calls
2523 * TODO handle non virt calls to non-final virtual calls (from the verifiability clause in page 52 of partition 3)
2524 * TODO handle abstract calls
2525 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2526 * TODO handle call invoking virtual methods (only allowed to invoke super)
2529 do_invoke_method (VerifyContext *ctx, int method_token)
2532 MonoMethodSignature *sig;
2534 MonoMethod *method = mono_get_method_full (ctx->image, method_token, NULL, ctx->generic_context);
2537 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method 0x%08x not found at 0x%04x", method_token, ctx->ip_offset));
2541 if (!(sig = mono_method_signature (method)))
2542 sig = mono_method_get_signature (method, ctx->image, method_token);
2544 param_count = sig->param_count + sig->hasthis;
2545 if (!check_underflow (ctx, param_count))
2548 for (i = sig->param_count - 1; i >= 0; --i) {
2549 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2550 value = stack_pop (ctx);
2551 if (!verify_stack_type_compatibility (ctx, sig->params[i], value))
2552 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2556 MonoType * type = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2558 VERIFIER_DEBUG ( printf ("verifying this argument\n"); );
2559 value = stack_pop (ctx);
2560 if (!verify_stack_type_compatibility (ctx, type, value)) {
2561 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
2565 if (!mono_method_can_access_method (ctx->method, method))
2566 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset));
2568 if (sig->ret->type != MONO_TYPE_VOID) {
2569 if (check_overflow (ctx))
2570 set_stack_value (ctx, stack_push (ctx), sig->ret, FALSE);
2573 if (sig->ret->type == MONO_TYPE_TYPEDBYREF
2575 || (sig->ret->type == MONO_TYPE_VALUETYPE && !strcmp ("System", sig->ret->data.klass->name_space) && !strcmp ("ArgIterator", sig->ret->data.klass->name)))
2576 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns typedbyref, byref or ArgIterator at 0x%04x", ctx->ip_offset));
2580 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2582 MonoClassField *field;
2585 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2587 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2591 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2592 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
2595 /*taking the address of initonly field only works from the static constructor */
2596 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2597 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
2598 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2600 if (!mono_method_can_access_field (ctx->method, field))
2601 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2603 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
2607 do_store_static_field (VerifyContext *ctx, int token) {
2608 MonoClassField *field;
2612 if (!check_underflow (ctx, 1))
2615 value = stack_pop (ctx);
2617 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2619 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2623 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2624 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
2628 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2629 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
2633 if (!mono_method_can_access_field (ctx->method, field))
2634 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2636 if (!verify_stack_type_compatibility (ctx, field->type, value))
2637 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));
2641 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field)
2643 MonoClassField *field;
2646 /*must be one of: complex type, managed pointer (to complex type), unmanaged pointer (native int) or an instance of a value type */
2647 if (!( stack_slot_get_underlying_type (obj) == TYPE_COMPLEX
2648 || stack_slot_get_type (obj) == TYPE_NATIVE_INT
2649 || stack_slot_get_type (obj) == TYPE_PTR)) {
2650 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument %s to load field at 0x%04x", stack_slot_get_name (obj), ctx->ip_offset));
2653 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2655 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2661 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2662 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
2665 g_assert (obj->type);
2667 /*The value on the stack must be a subclass of the defining type of the field*/
2668 /* we need to check if we can load the field from the stack value*/
2669 if (stack_slot_get_underlying_type (obj) == TYPE_COMPLEX) {
2670 if (!field->parent->valuetype && stack_slot_is_managed_pointer (obj))
2671 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));
2673 /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
2674 if (field->parent->valuetype && stack_slot_is_boxed_value (obj))
2675 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));
2677 if (!stack_slot_is_null_literal (obj) && !verify_type_compatibility (ctx, &field->parent->byval_arg, mono_type_get_type_byval (obj->type)))
2678 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
2680 if (!mono_method_can_access_field (ctx->method, field))
2681 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2684 if (!mono_method_can_access_field (ctx->method, field))
2685 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2687 if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
2688 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
2690 check_unmanaged_pointer (ctx, obj);
2695 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
2698 MonoClassField *field;
2700 if (!check_underflow (ctx, 1))
2702 obj = stack_pop (ctx);
2704 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2707 if (take_addr && field->parent->valuetype && !stack_slot_is_managed_pointer (obj))
2708 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
2710 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2711 !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
2712 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2714 set_stack_value (ctx, stack_push (ctx), field->type, take_addr);
2718 do_store_field (VerifyContext *ctx, int token)
2720 ILStackDesc *value, *obj;
2721 MonoClassField *field;
2723 if (!check_underflow (ctx, 2))
2726 value = stack_pop (ctx);
2727 obj = stack_pop (ctx);
2729 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2732 if (!verify_stack_type_compatibility (ctx, field->type, value))
2733 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2736 /*TODO proper handle for Nullable<T>*/
2738 do_box_value (VerifyContext *ctx, int klass_token)
2741 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2746 if (!check_underflow (ctx, 1))
2749 value = stack_top (ctx);
2750 /*box is a nop for reference types*/
2752 if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
2753 value->stype |= BOXED_MASK;
2757 value = stack_pop (ctx);
2759 if (!verify_stack_type_compatibility (ctx, type, value))
2760 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
2762 stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
2766 do_unbox_value (VerifyContext *ctx, int klass_token)
2769 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2774 if (!check_underflow (ctx, 1))
2777 if (!mono_class_from_mono_type (type)->valuetype)
2778 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
2780 value = stack_pop (ctx);
2782 /*Value should be: a boxed valuetype or a reference type*/
2783 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
2784 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
2785 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));
2787 //TODO Pushed managed pointer is controled mutability (CMMP)
2788 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byref (type), FALSE);
2792 do_unbox_any (VerifyContext *ctx, int klass_token)
2795 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2800 if (!check_underflow (ctx, 1))
2803 value = stack_pop (ctx);
2805 /*Value should be: a boxed valuetype or a reference type*/
2806 if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
2807 (stack_slot_is_boxed_value (value) || !mono_class_from_mono_type (value->type)->valuetype)))
2808 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));
2810 set_stack_value (ctx, stack_push (ctx), type, FALSE);
2814 do_unary_math_op (VerifyContext *ctx, int op)
2817 if (!check_underflow (ctx, 1))
2819 value = stack_top (ctx);
2820 switch (stack_slot_get_type (value)) {
2823 case TYPE_NATIVE_INT:
2828 case TYPE_COMPLEX: /*only enums are ok*/
2829 if (value->type->type == MONO_TYPE_VALUETYPE && value->type->data.klass->enumtype)
2832 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
2837 do_conversion (VerifyContext *ctx, int kind)
2840 if (!check_underflow (ctx, 1))
2842 value = stack_pop (ctx);
2844 switch (stack_slot_get_type (value)) {
2847 case TYPE_NATIVE_INT:
2851 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type (%s) at stack for conversion operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2856 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2859 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
2862 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
2864 case TYPE_NATIVE_INT:
2865 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
2868 g_error ("unknown type %02x in conversion", kind);
2874 do_load_token (VerifyContext *ctx, int token)
2877 MonoClass *handle_class;
2878 if (!check_overflow (ctx))
2880 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
2882 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
2885 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
2889 do_ldobj_value (VerifyContext *ctx, int token)
2892 MonoType *type = get_boxable_mono_type (ctx, token);
2897 if (!check_underflow (ctx, 1))
2900 value = stack_pop (ctx);
2901 if (!stack_slot_is_managed_pointer (value)
2902 && stack_slot_get_type (value) != TYPE_NATIVE_INT
2903 && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
2904 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
2908 if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
2909 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
2911 /*We have a byval on the stack, but the comparison must be strict. */
2912 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
2913 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
2915 set_stack_value (ctx, stack_push (ctx), type, FALSE);
2919 do_stobj (VerifyContext *ctx, int token)
2921 ILStackDesc *dest, *src;
2922 MonoType *type = get_boxable_mono_type (ctx, token);
2926 if (!check_underflow (ctx, 2))
2929 src = stack_pop (ctx);
2930 dest = stack_pop (ctx);
2932 if (!stack_slot_is_managed_pointer (dest))
2933 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
2935 if (!verify_stack_type_compatibility (ctx, type, src))
2936 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match at 0x%04x", ctx->ip_offset));
2938 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
2939 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
2943 do_cpobj (VerifyContext *ctx, int token)
2945 ILStackDesc *dest, *src;
2946 MonoType *type = get_boxable_mono_type (ctx, token);
2950 if (!check_underflow (ctx, 2))
2953 src = stack_pop (ctx);
2954 dest = stack_pop (ctx);
2956 if (!stack_slot_is_managed_pointer (src))
2957 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
2959 if (!stack_slot_is_managed_pointer (dest))
2960 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
2962 if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
2963 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
2965 if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
2966 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
2970 do_initobj (VerifyContext *ctx, int token)
2973 MonoType *stack, *type = get_boxable_mono_type (ctx, token);
2977 if (!check_underflow (ctx, 1))
2980 obj = stack_pop (ctx);
2982 if (!stack_slot_is_managed_pointer (obj))
2983 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
2985 stack = mono_type_get_type_byval (obj->type);
2986 if (MONO_TYPE_IS_REFERENCE (stack)) {
2987 if (!verify_type_compatibility (ctx, stack, type))
2988 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
2989 else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
2990 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
2991 } else if (!verify_type_compatibility (ctx, stack, type)) {
2992 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
2996 /* TODO implement access verification */
2998 do_newobj (VerifyContext *ctx, int token)
3002 MonoMethodSignature *sig;
3003 MonoMethod *method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
3004 gboolean is_delegate = FALSE;
3007 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ctx->ip_offset));
3011 if (!mono_method_is_constructor (method)) {
3012 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
3016 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
3017 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
3019 sig = mono_method_signature (method);
3020 if (!check_underflow (ctx, sig->param_count))
3023 is_delegate = method->klass->parent == mono_defaults.multicastdelegate_class;
3026 ILStackDesc *funptr;
3027 //first arg is object, second arg is fun ptr
3028 if (sig->param_count != 2) {
3029 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
3032 funptr = stack_pop (ctx);
3033 value = stack_pop (ctx);
3034 verify_delegate_compatibility (ctx, method->klass, value, funptr);
3036 for (i = sig->param_count - 1; i >= 0; --i) {
3037 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
3038 value = stack_pop (ctx);
3039 if (!verify_stack_type_compatibility (ctx, sig->params [i], value))
3040 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
3044 if (check_overflow (ctx))
3045 set_stack_value (ctx, stack_push (ctx), &method->klass->byval_arg, FALSE);
3049 do_cast (VerifyContext *ctx, int token) {
3054 if (!check_underflow (ctx, 1))
3057 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
3060 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3065 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid castclass type at 0x%04x", ctx->ip_offset));
3069 value = stack_pop (ctx);
3070 is_boxed = stack_slot_is_boxed_value (value);
3072 if (stack_slot_is_managed_pointer (value))
3073 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for checkcast at 0x%04x", ctx->ip_offset));
3074 else if (mono_class_from_mono_type (value->type)->valuetype && !is_boxed)
3075 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Value cannot be a valuetype for checkast at 0x%04x", ctx->ip_offset));
3077 switch (value->type->type) {
3078 case MONO_TYPE_FNPTR:
3080 case MONO_TYPE_TYPEDBYREF:
3081 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for checkast at 0x%04x", ctx->ip_offset));
3084 stack_push_val (ctx, TYPE_COMPLEX | (is_boxed ? BOXED_MASK : 0), type);
3088 mono_type_from_opcode (int opcode) {
3096 return &mono_defaults.sbyte_class->byval_arg;
3104 return &mono_defaults.int16_class->byval_arg;
3112 return &mono_defaults.int32_class->byval_arg;
3118 return &mono_defaults.int64_class->byval_arg;
3124 return &mono_defaults.single_class->byval_arg;
3130 return &mono_defaults.double_class->byval_arg;
3136 return &mono_defaults.int_class->byval_arg;
3140 case CEE_LDELEM_REF:
3141 case CEE_STELEM_REF:
3142 return &mono_defaults.object_class->byval_arg;
3145 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
3151 do_load_indirect (VerifyContext *ctx, int opcode)
3154 if (!check_underflow (ctx, 1))
3157 value = stack_pop (ctx);
3158 if (!stack_slot_is_managed_pointer (value)) {
3159 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
3160 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3164 if (opcode == CEE_LDIND_REF) {
3165 if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
3166 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
3167 set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
3169 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
3170 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3171 set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
3176 do_store_indirect (VerifyContext *ctx, int opcode)
3178 ILStackDesc *addr, *val;
3179 if (!check_underflow (ctx, 2))
3182 val = stack_pop (ctx);
3183 addr = stack_pop (ctx);
3185 check_unmanaged_pointer (ctx, addr);
3187 if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
3188 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
3192 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
3193 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3195 if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
3196 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
3200 do_newarr (VerifyContext *ctx, int token)
3203 MonoType *type = get_boxable_mono_type (ctx, token);
3208 if (!check_underflow (ctx, 1))
3211 value = stack_pop (ctx);
3212 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3213 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));
3215 set_stack_value (ctx, stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
3218 /*FIXME handle arrays that are not 0-indexed*/
3220 do_ldlen (VerifyContext *ctx)
3224 if (!check_underflow (ctx, 1))
3227 value = stack_pop (ctx);
3229 if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
3230 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
3232 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
3235 /*FIXME handle arrays that are not 0-indexed*/
3236 /*FIXME handle readonly prefix and CMMP*/
3238 do_ldelema (VerifyContext *ctx, int klass_token)
3240 ILStackDesc *index, *array;
3241 MonoType *type = get_boxable_mono_type (ctx, klass_token);
3247 if (!check_underflow (ctx, 2))
3250 index = stack_pop (ctx);
3251 array = stack_pop (ctx);
3253 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3254 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));
3256 if (!stack_slot_is_null_literal (array)) {
3257 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3258 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
3260 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
3261 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3263 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3266 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3270 set_stack_value (ctx, stack_push (ctx), type, TRUE);
3273 /*FIXME handle arrays that are not 0-indexed*/
3274 /*FIXME handle readonly prefix and CMMP*/
3276 do_ldelem (VerifyContext *ctx, int opcode, int token)
3278 ILStackDesc *index, *array;
3280 if (!check_underflow (ctx, 2))
3283 if (opcode == CEE_LDELEM_ANY) {
3284 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
3286 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3290 type = mono_type_from_opcode (opcode);
3293 index = stack_pop (ctx);
3294 array = stack_pop (ctx);
3296 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3297 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));
3299 if (!stack_slot_is_null_literal (array)) {
3300 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3301 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));
3303 if (opcode == CEE_LDELEM_REF) {
3304 if (array->type->data.klass->valuetype)
3305 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3306 type = &array->type->data.klass->byval_arg;
3307 } else if (!verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE)) {
3308 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3313 set_stack_value (ctx, stack_push (ctx), type, FALSE);
3316 /*FIXME handle arrays that are not 0-indexed*/
3318 do_stelem (VerifyContext *ctx, int opcode, int token)
3320 ILStackDesc *index, *array, *value;
3322 if (!check_underflow (ctx, 3))
3325 if (opcode == CEE_STELEM_ANY) {
3326 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
3328 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3332 type = mono_type_from_opcode (opcode);
3335 value = stack_pop (ctx);
3336 index = stack_pop (ctx);
3337 array = stack_pop (ctx);
3339 if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
3340 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));
3342 if (!stack_slot_is_null_literal (array)) {
3343 if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3344 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));
3346 if (opcode == CEE_STELEM_REF) {
3347 if (array->type->data.klass->valuetype)
3348 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3349 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3350 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3355 if (opcode == CEE_STELEM_REF) {
3356 if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
3357 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3358 } else if (opcode != CEE_STELEM_REF && !verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (value), FALSE)) {
3359 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3364 do_throw (VerifyContext *ctx)
3366 ILStackDesc *exception;
3367 if (!check_underflow (ctx, 1))
3369 exception = stack_pop (ctx);
3371 if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3372 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3374 /*The stack is left empty after a throw*/
3380 do_endfilter (VerifyContext *ctx)
3382 MonoExceptionClause *clause;
3384 if (IS_STRICT_MODE (ctx)) {
3385 if (ctx->eval.size != 1)
3386 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3388 if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
3389 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3392 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3393 if (IS_STRICT_MODE (ctx)) {
3394 if (ctx->ip_offset != clause->handler_offset - 2)
3395 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3397 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3398 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3401 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3402 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3404 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3411 do_leave (VerifyContext *ctx, int delta)
3413 int target = ((gint32)ctx->ip_offset) + delta;
3414 if (target >= ctx->code_size || target < 0)
3415 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
3417 if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
3418 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
3424 * Verify br and br.s opcodes.
3427 do_static_branch (VerifyContext *ctx, int delta)
3429 int target = ctx->ip_offset + delta;
3430 if (target < 0 || target >= ctx->code_size) {
3431 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
3435 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3437 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3440 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
3444 ctx->target = target;
3448 do_switch (VerifyContext *ctx, int count, const unsigned char *data)
3450 int i, base = ctx->ip_offset + 5 + count * 4;
3453 if (!check_underflow (ctx, 1))
3456 value = stack_pop (ctx);
3458 if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
3459 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
3461 for (i = 0; i < count; ++i) {
3462 int target = base + read32 (data + i * 4);
3464 if (target < 0 || target >= ctx->code_size)
3465 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
3467 switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
3469 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3472 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
3475 merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
3479 /*TODO add visibility checks*/
3481 do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual)
3486 if (virtual && !check_underflow (ctx, 1))
3489 if (!virtual && !check_overflow (ctx))
3492 if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
3493 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset));
3497 method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
3500 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve ldftn token %x at 0x%04x", token, ctx->ip_offset));
3504 if (mono_method_is_constructor (method))
3505 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
3508 ILStackDesc *top = stack_pop (ctx);
3510 if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
3511 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
3513 if (method->flags & METHOD_ATTRIBUTE_STATIC)
3514 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
3516 if (!verify_type_compatibility (ctx, &method->klass->byval_arg, top->type))
3517 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
3520 top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
3521 top->method = method;
3525 do_sizeof (VerifyContext *ctx, int token)
3529 if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
3530 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid type token %x at 0x%04x", token, ctx->ip_offset));
3534 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
3537 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3541 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
3542 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
3546 if (type->type == MONO_TYPE_VOID) {
3547 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
3551 if (check_overflow (ctx))
3552 set_stack_value (ctx, stack_push (ctx), &mono_defaults.uint32_class->byval_arg, FALSE);
3555 /* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
3557 do_localloc (VerifyContext *ctx)
3559 if (ctx->eval.size != 1) {
3560 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
3564 if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
3565 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
3569 set_stack_value (ctx, stack_top (ctx), &mono_defaults.int_class->byval_arg, FALSE);
3570 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
3573 /*Merge the stacks and perform compat checks*/
3575 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start, gboolean external)
3578 stack_init (ctx, to);
3581 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
3584 stack_copy (&ctx->eval, to);
3586 } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
3587 stack_copy (to, &ctx->eval);
3590 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
3592 if (from->size != to->size) {
3593 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
3594 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));
3598 for (i = 0; i < from->size; ++i) {
3599 ILStackDesc *from_slot = from->stack + i;
3600 ILStackDesc *to_slot = to->stack + i;
3602 if (!verify_type_compatibility (ctx, mono_type_from_stack_slot (to_slot), mono_type_from_stack_slot (from_slot))) {
3603 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, types not compatible at 0x%04x", ctx->ip_offset));
3607 /*TODO we need to choose the base class for merging reference types*/
3608 copy_stack_value (to_slot, from_slot);
3613 to->flags |= IL_CODE_FLAG_WAS_TARGET;
3614 to->flags |= IL_CODE_STACK_MERGED;
3617 #define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
3618 #define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
3621 * is_clause_in_range :
3623 * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
3626 is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
3628 if (clause->try_offset >= start && clause->try_offset < end)
3630 if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
3636 * is_clause_inside_range :
3638 * Returns TRUE if @clause lies completely inside the @start - @end range.
3641 is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
3643 if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
3645 if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
3651 * is_clause_nested :
3653 * Returns TRUE if @nested is nested in @clause.
3656 is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
3658 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
3660 return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
3661 is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
3664 /* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
3665 * the each pair of exception must have the following properties:
3666 * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
3667 * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
3668 * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
3671 verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
3673 /*clause is nested*/
3674 if (is_clause_nested (to_test, clause)) {
3675 if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3676 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
3681 /*wrong nesting order.*/
3682 if (is_clause_nested (clause, to_test)) {
3683 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
3687 /*mutual protection*/
3688 if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
3689 /*handlers are not disjoint*/
3690 if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
3691 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
3694 /* handlers are not catch or filter */
3695 if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
3696 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
3703 /*not completelly disjoint*/
3704 if (is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
3705 is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len))
3706 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
3710 * FIXME: need to distinguish between valid and verifiable.
3711 * Need to keep track of types on the stack.
3712 * Verify types for opcodes.
3715 mono_method_verify (MonoMethod *method, int level)
3717 const unsigned char *ip;
3718 const unsigned char *end;
3719 int i, n, need_merge = 0, start = 0;
3720 guint token, ip_offset = 0, prefix = 0, prefix_list = 0;
3721 MonoGenericContext *generic_context = NULL;
3726 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name, method->klass->name, method->name); );
3728 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3729 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
3733 memset (&ctx, 0, sizeof (VerifyContext));
3735 ctx.signature = mono_method_signature (method);
3736 if (!ctx.signature) {
3737 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
3740 ctx.header = mono_method_get_header (method);
3742 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
3745 ctx.method = method;
3746 ip = ctx.header->code;
3747 end = ip + ctx.header->code_size;
3748 ctx.image = image = method->klass->image;
3751 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
3752 ctx.max_stack = ctx.header->max_stack;
3753 ctx.verifiable = ctx.valid = 1;
3756 ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
3757 ctx.code_size = ctx.header->code_size;
3759 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
3762 ctx.num_locals = ctx.header->num_locals;
3763 ctx.locals = ctx.header->locals;
3766 if (ctx.signature->hasthis) {
3767 ctx.params = g_new0 (MonoType*, ctx.max_args);
3768 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
3769 memcpy (ctx.params + 1, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
3771 ctx.params = ctx.signature->params;
3774 if (ctx.signature->is_inflated)
3775 ctx.generic_context = generic_context = mono_method_get_context (method);
3777 stack_init (&ctx, &ctx.eval);
3779 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
3780 MonoExceptionClause *clause = ctx.header->clauses + i;
3781 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); );
3783 if (clause->try_offset > ctx.code_size)
3784 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
3786 if (clause->try_len <= 0)
3787 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
3789 if (clause->handler_offset > ctx.code_size)
3790 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
3792 if (clause->handler_len <= 0)
3793 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
3795 if (clause->try_offset < clause->handler_offset && clause->try_offset + clause->try_len > HANDLER_START (clause))
3796 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
3798 for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
3799 verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
3804 ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
3805 ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
3806 ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
3808 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
3809 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
3811 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3812 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
3813 init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
3817 while (ip < end && ctx.valid) {
3818 ctx.ip_offset = ip_offset = ip - ctx.header->code;
3820 /*We need to check against fallthrou in and out of protected blocks.
3821 * For fallout we check the once a protected block ends, if the start flag is not set.
3822 * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
3823 * TODO convert these checks to be done using flags and not this loop
3825 for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
3826 MonoExceptionClause *clause = ctx.header->clauses + i;
3828 if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
3829 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
3833 if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
3834 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
3835 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
3837 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
3841 if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
3842 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
3846 if (clause->handler_offset == ip_offset && start == 0) {
3847 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
3851 if (clause->try_offset == ip_offset && ctx.eval.size > 0) {
3852 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
3860 /*TODO id stack merge fails, we break, should't we - or only on errors??
3861 TODO verify need_merge
3864 VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
3865 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
3868 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
3871 /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
3873 prefix_list |= prefix;
3876 ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
3880 #ifdef MONO_VERIFIER_DEBUG
3883 discode = mono_disasm_code_one (NULL, method, ip, NULL);
3884 discode [strlen (discode) - 1] = 0; /* no \n */
3885 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
3888 dump_stack_state (&ctx.code [ip_offset]);
3889 dump_stack_state (&ctx.eval);
3902 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
3908 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
3912 case CEE_ADD_OVF_UN:
3913 do_binop (&ctx, *ip, add_ovf_un_table);
3917 case CEE_SUB_OVF_UN:
3918 do_binop (&ctx, *ip, sub_ovf_un_table);
3925 case CEE_MUL_OVF_UN:
3926 do_binop (&ctx, *ip, bin_ovf_table);
3931 do_binop (&ctx, *ip, add_table);
3936 do_binop (&ctx, *ip, sub_table);
3943 do_binop (&ctx, *ip, bin_op_table);
3952 do_binop (&ctx, *ip, int_bin_op_table);
3959 do_binop (&ctx, *ip, shift_op_table);
3964 if (!check_underflow (&ctx, 1))
3980 /*TODO support definite assignment verification? */
3981 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
3989 store_local (&ctx, *ip - CEE_STLOC_0);
3994 store_local (&ctx, ip [1]);
3999 store_arg (&ctx, ip [1]);
4013 if (check_overflow (&ctx))
4014 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4019 if (check_overflow (&ctx))
4020 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
4025 if (check_overflow (&ctx))
4026 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
4031 if (check_overflow (&ctx))
4032 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
4037 if (check_overflow (&ctx))
4038 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4043 if (check_overflow (&ctx))
4044 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
4049 if (check_overflow (&ctx))
4050 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
4056 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
4069 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
4076 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
4089 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
4096 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
4100 /* FIXME: warn/error instead? */
4107 if (!check_underflow (&ctx, 1))
4109 if (!check_overflow (&ctx))
4111 top = stack_push (&ctx);
4112 copy_stack_value (top, stack_get (&ctx, 1));
4119 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
4120 token = read32 (ip + 1);
4121 if (in_any_block (ctx.header, ip_offset))
4122 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
4124 * FIXME: check signature, retval, arguments etc.
4130 do_invoke_method (&ctx, read32 (ip + 1));
4135 token = read32 (ip + 1);
4137 * FIXME: check signature, retval, arguments etc.
4142 do_static_branch (&ctx, (signed char)ip [1] + 2);
4150 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
4156 do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
4164 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
4170 n = read32 (ip + 1);
4171 do_switch (&ctx, n, (ip + 5));
4173 ip += 5 + sizeof (guint32) * n;
4187 do_load_indirect (&ctx, *ip);
4199 do_store_indirect (&ctx, *ip);
4205 do_unary_math_op (&ctx, *ip);
4209 //TODO: implement proper typecheck
4216 do_conversion (&ctx, TYPE_I4);
4222 do_conversion (&ctx, TYPE_I8);
4229 do_conversion (&ctx, TYPE_R8);
4235 do_conversion (&ctx, TYPE_NATIVE_INT);
4240 do_cpobj (&ctx, read32 (ip + 1));
4245 do_ldobj_value (&ctx, read32 (ip + 1));
4250 /*TODO verify if token is a valid string literal*/
4251 token = read32 (ip + 1);
4252 if (check_overflow (&ctx))
4253 stack_push_val (&ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
4258 do_newobj (&ctx, read32 (ip + 1));
4264 do_cast (&ctx, read32 (ip + 1));
4270 ++ip; /* warn, error ? */
4273 do_unbox_value (&ctx, read32 (ip + 1));
4285 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
4291 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
4296 do_store_field (&ctx, read32 (ip + 1));
4301 do_store_static_field (&ctx, read32 (ip + 1));
4306 do_stobj (&ctx, read32 (ip + 1));
4310 case CEE_CONV_OVF_I1_UN:
4311 case CEE_CONV_OVF_I2_UN:
4312 case CEE_CONV_OVF_I4_UN:
4313 case CEE_CONV_OVF_U1_UN:
4314 case CEE_CONV_OVF_U2_UN:
4315 case CEE_CONV_OVF_U4_UN:
4316 do_conversion (&ctx, TYPE_I4);
4320 case CEE_CONV_OVF_I8_UN:
4321 case CEE_CONV_OVF_U8_UN:
4322 do_conversion (&ctx, TYPE_I8);
4326 case CEE_CONV_OVF_I_UN:
4327 case CEE_CONV_OVF_U_UN:
4328 do_conversion (&ctx, TYPE_NATIVE_INT);
4333 do_box_value (&ctx, read32 (ip + 1));
4338 do_newarr (&ctx, read32 (ip + 1));
4348 do_ldelema (&ctx, read32 (ip + 1));
4362 case CEE_LDELEM_REF:
4363 do_ldelem (&ctx, *ip, 0);
4374 case CEE_STELEM_REF:
4375 do_stelem (&ctx, *ip, 0);
4379 case CEE_LDELEM_ANY:
4380 do_ldelem (&ctx, *ip, read32 (ip + 1));
4384 case CEE_STELEM_ANY:
4385 do_stelem (&ctx, *ip, read32 (ip + 1));
4390 do_unbox_any (&ctx, read32 (ip + 1));
4407 ++ip; /* warn, error ? */
4410 case CEE_CONV_OVF_I1:
4411 case CEE_CONV_OVF_U1:
4412 case CEE_CONV_OVF_I2:
4413 case CEE_CONV_OVF_U2:
4414 case CEE_CONV_OVF_I4:
4415 case CEE_CONV_OVF_U4:
4416 do_conversion (&ctx, TYPE_I4);
4420 case CEE_CONV_OVF_I8:
4421 case CEE_CONV_OVF_U8:
4422 do_conversion (&ctx, TYPE_I8);
4426 case CEE_CONV_OVF_I:
4427 case CEE_CONV_OVF_U:
4428 do_conversion (&ctx, TYPE_NATIVE_INT);
4439 ++ip; /* warn, error ? */
4442 if (!check_underflow (&ctx, 1))
4447 if (!check_underflow (&ctx, 1))
4453 ++ip; /* warn, error ? */
4456 if (!check_underflow (&ctx, 1))
4458 token = read32 (ip + 1);
4470 ++ip; /* warn, error ? */
4473 do_load_token (&ctx, read32 (ip + 1));
4477 case CEE_ENDFINALLY:
4478 if (!is_correct_endfinally (ctx.header, ip_offset))
4479 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
4486 do_leave (&ctx, read32 (ip + 1) + 5);
4492 do_leave (&ctx, (signed char)ip [1] + 2);
4535 store_local (&ctx, read16 (ip + 1));
4540 do_cmp_op (&ctx, cmp_br_eq_op, *ip);
4548 do_cmp_op (&ctx, cmp_br_op, *ip);
4553 store_arg (&ctx, read16 (ip + 1) );
4559 check_overflow (&ctx);
4563 do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
4568 do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
4578 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
4584 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
4597 do_endfilter (&ctx);
4601 case CEE_UNALIGNED_:
4602 prefix |= PREFIX_UNALIGNED;
4606 prefix |= PREFIX_VOLATILE;
4610 prefix |= PREFIX_TAIL;
4612 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
4613 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
4617 do_initobj (&ctx, read32 (ip + 1));
4621 case CEE_CONSTRAINED_:
4622 token = read32 (ip + 1);
4626 if (!check_underflow (&ctx, 3))
4631 if (!check_underflow (&ctx, 3))
4639 if (!is_correct_rethrow (ctx.header, ip_offset))
4640 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
4649 do_sizeof (&ctx, read32 (ip + 1));
4653 case CEE_REFANYTYPE:
4654 if (!check_underflow (&ctx, 1))
4668 * if ip != end we overflowed: mark as error.
4670 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
4671 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
4674 /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
4675 for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
4676 if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
4677 if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
4678 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of intruction at 0x%04x", i));
4680 if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
4681 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
4683 if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
4684 CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
4688 for (i = 0; i < ctx.header->code_size; ++i) {
4689 if (ctx.code [i].stack)
4690 g_free (ctx.code [i].stack);
4694 for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
4696 g_slist_free (ctx.funptrs);
4699 g_free (ctx.eval.stack);
4702 if (ctx.signature->hasthis)
4703 g_free (ctx.params);
4709 mono_verify_corlib ()
4711 /* This is a public API function so cannot be removed */