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>
18 * Pull the list of opcodes
20 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
24 #include "mono/cil/opcode.def"
29 #ifdef MONO_VERIFIER_DEBUG
30 #define VERIFIER_DEBUG(code) do { code } while (0)
32 #define VERIFIER_DEBUG(code)
35 //////////////////////////////////////////////////////////////////
36 #define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
37 #define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
39 #define ADD_VERIFY_INFO(__ctx, __msg, __status) \
41 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
42 vinfo->status = __status; \
43 vinfo->message = ( __msg ); \
44 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
47 #define ADD_VERIFY_ERROR(__ctx, __msg) \
49 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR); \
53 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
55 if ((__ctx)->verifiable) { \
56 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE); \
57 (__ctx)->verifiable = 0; \
58 if (IS_FAIL_FAST_MODE (__ctx)) \
63 #define UNMASK_TYPE(type) ((type) & TYPE_MASK)
64 #define IS_MANAGED_POINTER(type) (((type) & POINTER_MASK) == POINTER_MASK)
65 #define IS_NULL_LITERAL(type) (((type) & NULL_LITERAL_MASK) == NULL_LITERAL_MASK)
68 IL_CODE_FLAG_NOT_PROCESSED = 0,
104 MonoMethodSignature *signature;
105 MonoMethodHeader *header;
107 MonoGenericContext *generic_context;
112 //////////////////////////////////////////////////////////////////
117 TYPE_INV = 0, /* leave at 0. */
122 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
124 /* value types and classes */
126 /* Number of types, used to define the size of the tables*/
127 TYPE_MAX = 8, /* FIXME: This should probably be 7, but would require all the tables to be updated */
129 /* Used by tables to signal that a result is not verifiable*/
130 NON_VERIFIABLE_RESULT = 0x80,
132 /*Mask used to extract just the type, excluding flags */
135 /* The stack type is a managed pointer, unmask the value to res */
136 POINTER_MASK = 0x100,
138 /* Controlled Mutability Manager Pointer */
141 /* The stack type is a null literal*/
142 NULL_LITERAL_MASK = 0x400,
145 static const char* const
146 type_names [TYPE_MAX] = {
152 "TYPE_PTR", /* FIXME: Give an appropriate name */
157 PREFIX_UNALIGNED = 1,
160 PREFIX_ADDR_MASK = 3,
166 //////////////////////////////////////////////////////////////////
168 mono_free_verify_list (GSList *list)
170 MonoVerifyInfo *info;
173 for (tmp = list; tmp; tmp = tmp->next) {
175 g_free (info->message);
181 #define ADD_ERROR(list,msg) \
183 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
184 vinfo->status = MONO_VERIFY_ERROR; \
185 vinfo->message = (msg); \
186 (list) = g_slist_prepend ((list), vinfo); \
189 #define ADD_WARN(list,code,msg) \
191 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
192 vinfo->status = (code); \
193 vinfo->message = (msg); \
194 (list) = g_slist_prepend ((list), vinfo); \
198 valid_cultures[][9] = {
199 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
200 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
201 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
202 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
203 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
204 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
205 "da-DK", "de-DE", "de-CH", "de-AT",
206 "de-LU", "de-LI", "el-GR", "en-US",
207 "en-GB", "en-AU", "en-CA", "en-NZ",
208 "en-IE", "en-ZA", "en-JM", "en-CB",
209 "en-BZ", "en-TT", "en-ZW", "en-PH",
210 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
211 "es-CR", "es-PA", "es-DO", "es-VE",
212 "es-CO", "es-PE", "es-AR", "es-EC",
213 "es-CL", "es-UY", "es-PY", "es-BO",
214 "es-SV", "es-HN", "es-NI", "es-PR",
215 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
216 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
217 "hu-HU", "is-IS", "it-IT", "it-CH",
218 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
219 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
220 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
221 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
222 "sv-SE", "sv-FI", "th-TH", "tr-TR",
223 "ur-PK", "id-ID", "uk-UA", "be-BY",
224 "sl-SI", "et-EE", "lv-LV", "lt-LT",
225 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
227 "eu-ES", "mk-MK", "af-ZA",
228 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
229 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
230 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
231 "gu-IN", "ta-IN", "te-IN", "kn-IN",
232 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
233 "kok-IN", "syr-SY", "div-MV"
237 is_valid_culture (const char *cname)
243 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
244 if (g_strcasecmp (valid_cultures [i], cname)) {
253 is_valid_assembly_flags (guint32 flags) {
254 /* Metadata: 22.1.2 */
255 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
256 return ((flags == 1) || (flags == 0));
260 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
263 const char *p, *blob_end;
265 if (blob_index >= image->heap_blob.size)
267 p = mono_metadata_blob_heap (image, blob_index);
268 size = mono_metadata_decode_blob_size (p, &blob_end);
269 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
271 if (notnull && !size)
277 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
279 const char *p, *blob_end, *res;
281 if (str_index >= image->heap_strings.size)
283 res = p = mono_metadata_string_heap (image, str_index);
284 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
288 * FIXME: should check it's a valid utf8 string, too.
290 while (p <= blob_end) {
295 return *p? NULL: res;
299 is_valid_cls_ident (const char *p)
302 * FIXME: we need the full unicode glib support for this.
303 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
304 * We do the lame thing for now.
310 if (!isalnum (*p) && *p != '_')
318 is_valid_filename (const char *p)
322 return strpbrk (p, "\\//:")? 0: 1;
326 verify_assembly_table (MonoImage *image, GSList *list, int level)
328 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
329 guint32 cols [MONO_ASSEMBLY_SIZE];
332 if (level & MONO_VERIFY_ERROR) {
334 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
335 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
337 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
338 case ASSEMBLY_HASH_NONE:
339 case ASSEMBLY_HASH_MD5:
340 case ASSEMBLY_HASH_SHA1:
343 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
346 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
347 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
349 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
350 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
352 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
353 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
355 if (strpbrk (p, ":\\/."))
356 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
359 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
360 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
362 if (!is_valid_culture (p))
363 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
370 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
372 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
373 guint32 cols [MONO_ASSEMBLYREF_SIZE];
377 if (level & MONO_VERIFY_ERROR) {
378 for (i = 0; i < t->rows; ++i) {
379 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
380 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
381 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
383 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
384 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
386 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
387 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
389 if (!is_valid_culture (p))
390 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
393 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
394 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
397 if (level & MONO_VERIFY_WARNING) {
398 /* check for duplicated rows */
399 for (i = 0; i < t->rows; ++i) {
406 verify_class_layout_table (MonoImage *image, GSList *list, int level)
408 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
409 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
410 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
413 if (level & MONO_VERIFY_ERROR) {
414 for (i = 0; i < t->rows; ++i) {
415 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
417 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
418 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
420 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
421 if (value & TYPE_ATTRIBUTE_INTERFACE)
422 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
423 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
424 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
425 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
426 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
427 case 0: case 1: case 2: case 4: case 8: case 16:
428 case 32: case 64: case 128: break;
430 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
432 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
434 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
435 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
436 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));
440 * FIXME: we need to check that if class size != 0,
441 * it needs to be greater than the class calculated size.
442 * If parent is a valuetype it also needs to be smaller than
443 * 1 MByte (0x100000 bytes).
444 * To do both these checks we need to load the referenced
445 * assemblies, though (the spec claims we didn't have to, bah).
448 * We need to check that the parent types have the same layout
459 verify_constant_table (MonoImage *image, GSList *list, int level)
461 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
462 guint32 cols [MONO_CONSTANT_SIZE];
464 GHashTable *dups = g_hash_table_new (NULL, NULL);
466 for (i = 0; i < t->rows; ++i) {
467 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
469 if (level & MONO_VERIFY_ERROR)
470 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
471 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
472 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
473 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
475 switch (cols [MONO_CONSTANT_TYPE]) {
476 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
480 if (level & MONO_VERIFY_CLS)
481 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));
482 case MONO_TYPE_BOOLEAN:
490 case MONO_TYPE_STRING:
491 case MONO_TYPE_CLASS:
494 if (level & MONO_VERIFY_ERROR)
495 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
497 if (level & MONO_VERIFY_ERROR) {
498 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
499 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
500 case MONO_HASCONSTANT_FIEDDEF:
501 if (value > image->tables [MONO_TABLE_FIELD].rows)
502 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
504 case MONO_HASCONSTANT_PARAM:
505 if (value > image->tables [MONO_TABLE_PARAM].rows)
506 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
508 case MONO_HASCONSTANT_PROPERTY:
509 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
510 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
513 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
517 if (level & MONO_VERIFY_CLS) {
519 * FIXME: verify types is consistent with the enum type
520 * is parent is an enum.
524 g_hash_table_destroy (dups);
529 verify_event_map_table (MonoImage *image, GSList *list, int level)
531 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
532 guint32 cols [MONO_EVENT_MAP_SIZE];
533 guint32 i, last_event;
534 GHashTable *dups = g_hash_table_new (NULL, NULL);
538 for (i = 0; i < t->rows; ++i) {
539 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
540 if (level & MONO_VERIFY_ERROR)
541 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
542 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
543 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
544 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
545 if (level & MONO_VERIFY_ERROR) {
546 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
547 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
548 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
549 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
551 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
552 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
553 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
557 g_hash_table_destroy (dups);
562 verify_event_table (MonoImage *image, GSList *list, int level)
564 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
565 guint32 cols [MONO_EVENT_SIZE];
569 for (i = 0; i < t->rows; ++i) {
570 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
572 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
573 if (level & MONO_VERIFY_ERROR)
574 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
576 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
577 if (level & MONO_VERIFY_ERROR)
578 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
580 if (level & MONO_VERIFY_CLS) {
581 if (!is_valid_cls_ident (p))
582 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
586 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
587 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
588 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
589 case MONO_TYPEDEFORREF_TYPEDEF:
590 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
591 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
593 case MONO_TYPEDEFORREF_TYPEREF:
594 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
595 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
597 case MONO_TYPEDEFORREF_TYPESPEC:
598 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
599 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
602 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
606 * FIXME: check that there is 1 add and remove row in methodsemantics
607 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
608 * these while checking methodsemantics).
609 * check for duplicated names for the same type [ERROR]
610 * check for CLS duplicate names for the same type [CLS]
617 verify_field_table (MonoImage *image, GSList *list, int level)
619 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
620 guint32 cols [MONO_FIELD_SIZE];
624 for (i = 0; i < t->rows; ++i) {
625 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
627 * Check this field has only one owner and that the owner is not
628 * an interface (done in verify_typedef_table() )
630 flags = cols [MONO_FIELD_FLAGS];
631 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
632 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
633 case FIELD_ATTRIBUTE_PRIVATE:
634 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
635 case FIELD_ATTRIBUTE_ASSEMBLY:
636 case FIELD_ATTRIBUTE_FAMILY:
637 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
638 case FIELD_ATTRIBUTE_PUBLIC:
641 if (level & MONO_VERIFY_ERROR)
642 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
645 if (level & MONO_VERIFY_ERROR) {
646 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
647 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
648 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
649 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
650 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
651 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
653 * FIXME: check there is only one owner in the respective table.
654 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
655 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
656 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
659 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
660 if (level & MONO_VERIFY_ERROR)
661 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
663 if (level & MONO_VERIFY_CLS) {
664 if (!is_valid_cls_ident (p))
665 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
670 * if owner is module needs to be static, access mask needs to be compilercontrolled,
671 * public or private (not allowed in cls mode).
672 * if owner is an enum ...
681 verify_file_table (MonoImage *image, GSList *list, int level)
683 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
684 guint32 cols [MONO_FILE_SIZE];
687 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
689 for (i = 0; i < t->rows; ++i) {
690 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
691 if (level & MONO_VERIFY_ERROR) {
692 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
693 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
694 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
695 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
697 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
698 if (level & MONO_VERIFY_ERROR)
699 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
701 if (level & MONO_VERIFY_ERROR) {
702 if (!is_valid_filename (p))
703 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
704 else if (g_hash_table_lookup (dups, p)) {
705 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
707 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
711 * FIXME: I don't understand what this means:
712 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
713 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
717 if (level & MONO_VERIFY_WARNING) {
718 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
719 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
721 g_hash_table_destroy (dups);
726 verify_moduleref_table (MonoImage *image, GSList *list, int level)
728 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
729 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
730 guint32 cols [MONO_MODULEREF_SIZE];
732 guint32 found, i, j, value;
733 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
735 for (i = 0; i < t->rows; ++i) {
736 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
737 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
738 if (level & MONO_VERIFY_ERROR)
739 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
741 if (level & MONO_VERIFY_ERROR) {
742 if (!is_valid_filename (p))
743 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
744 else if (g_hash_table_lookup (dups, p)) {
745 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
746 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
748 for (j = 0; j < tfile->rows; ++j) {
749 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
750 if ((pf = is_valid_string (image, value, TRUE)))
751 if (strcmp (p, pf) == 0) {
757 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
762 g_hash_table_destroy (dups);
767 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
769 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
770 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
774 for (i = 0; i < t->rows; ++i) {
775 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
776 if (level & MONO_VERIFY_ERROR) {
777 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
778 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
780 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
781 /* FIXME: check it's a valid locals or method sig.*/
789 mono_image_verify_tables (MonoImage *image, int level)
791 GSList *error_list = NULL;
793 error_list = verify_assembly_table (image, error_list, level);
795 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
796 * AssemblyRefProcessor should be ignored,
797 * though we may want to emit a warning, since it should not
798 * be present in a PE file.
800 error_list = verify_assemblyref_table (image, error_list, level);
801 error_list = verify_class_layout_table (image, error_list, level);
802 error_list = verify_constant_table (image, error_list, level);
804 * cutom attribute, declsecurity
806 error_list = verify_event_map_table (image, error_list, level);
807 error_list = verify_event_table (image, error_list, level);
808 error_list = verify_field_table (image, error_list, level);
809 error_list = verify_file_table (image, error_list, level);
810 error_list = verify_moduleref_table (image, error_list, level);
811 error_list = verify_standalonesig_table (image, error_list, level);
813 return g_slist_reverse (error_list);
818 bin_num_table [TYPE_MAX] [TYPE_MAX] = {
819 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
820 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_INV},
821 {TYPE_INV, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
822 {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_INV},
823 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV, TYPE_INV},
824 {TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV},
825 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
826 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
831 TYPE_INV, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_INV, TYPE_INV, TYPE_INV
834 /* reduce the size of this table */
836 bin_int_table [TYPE_MAX] [TYPE_MAX] = {
837 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
838 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
839 {TYPE_INV, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
840 {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
841 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
842 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
843 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
844 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
848 bin_comp_table [TYPE_MAX] [TYPE_MAX] = {
850 {0, 1, 0, 1, 0, 0, 0, 0},
851 {0, 0, 1, 0, 0, 0, 0, 0},
852 {0, 1, 0, 1, 0, 2, 0, 0},
853 {0, 0, 0, 0, 1, 0, 0, 0},
854 {0, 0, 0, 2, 0, 1, 0, 0},
855 {0, 0, 0, 0, 0, 0, 3, 0},
856 {0, 0, 0, 0, 0, 0, 0, 0},
859 /* reduce the size of this table */
861 shift_table [TYPE_MAX] [TYPE_MAX] = {
862 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
863 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
864 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
865 {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
866 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
867 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
868 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
869 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
874 TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_R8, TYPE_COMPLEX
879 TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_R8, TYPE_COMPLEX
882 #define ADD_INVALID(list,msg) \
884 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
885 vinfo->status = MONO_VERIFY_ERROR; \
886 vinfo->message = (msg); \
887 (list) = g_slist_prepend ((list), vinfo); \
888 /*G_BREAKPOINT ();*/ \
892 #define CHECK_STACK_UNDERFLOW(num) \
894 if (cur_stack < (num)) \
895 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
898 #define CHECK_STACK_OVERFLOW() \
900 if (cur_stack >= max_stack) \
901 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
906 type_to_eval_stack_type (MonoType *type, ILStackDesc *stack, int take_addr) {
910 if (type->byref || take_addr) { /* fix double addr issue */
911 stack->stype = TYPE_COMPLEX;
919 case MONO_TYPE_BOOLEAN:
925 stack->stype = TYPE_I4;
930 stack->stype = TYPE_PTR;
932 case MONO_TYPE_CLASS:
933 case MONO_TYPE_STRING:
934 case MONO_TYPE_OBJECT:
935 case MONO_TYPE_SZARRAY:
936 case MONO_TYPE_ARRAY:
937 stack->stype = TYPE_COMPLEX;
941 stack->stype = TYPE_I8;
945 stack->stype = TYPE_R8;
947 case MONO_TYPE_VALUETYPE:
948 if (type->data.klass->enumtype) {
949 t = type->data.klass->enum_basetype->type;
952 stack->stype = TYPE_COMPLEX;
956 g_error ("unknown type %02x in eval stack type", type->type);
962 type_from_op (int ins, ILStackDesc *arg) {
970 /* FIXME: check unverifiable args for TYPE_COMPLEX */
971 return arg->stype = bin_num_table [arg->stype] [arg [1].stype];
977 return arg->stype = bin_int_table [arg->stype] [arg [1].stype];
981 return arg->stype = shift_table [arg->stype] [arg [1].stype];
1002 /* FIXME: handle some specifics with ins->next->type */
1003 return bin_comp_table [arg->stype] [arg [1].stype] ? TYPE_I4: TYPE_INV;
1006 case 256+CEE_CGT_UN:
1008 case 256+CEE_CLT_UN:
1009 return arg->stype = bin_comp_table [arg->stype] [arg [1].stype] ? TYPE_I4: TYPE_INV;
1012 return arg->stype = neg_table [arg->stype];
1014 if (arg->stype >= TYPE_I4 && arg->stype <= TYPE_PTR)
1017 return arg->stype = TYPE_INV;
1024 case CEE_CONV_OVF_I1:
1025 case CEE_CONV_OVF_U1:
1026 case CEE_CONV_OVF_I2:
1027 case CEE_CONV_OVF_U2:
1028 case CEE_CONV_OVF_I4:
1029 case CEE_CONV_OVF_U4:
1030 case CEE_CONV_OVF_I1_UN:
1031 case CEE_CONV_OVF_U1_UN:
1032 case CEE_CONV_OVF_I2_UN:
1033 case CEE_CONV_OVF_U2_UN:
1034 case CEE_CONV_OVF_I4_UN:
1035 case CEE_CONV_OVF_U4_UN:
1036 if (arg->stype == TYPE_INV || arg->stype >= TYPE_COMPLEX)
1037 return arg->stype = TYPE_INV;
1038 return arg->stype = TYPE_I4;
1041 case CEE_CONV_OVF_I:
1042 case CEE_CONV_OVF_U:
1043 case CEE_CONV_OVF_I_UN:
1044 case CEE_CONV_OVF_U_UN:
1045 if (arg->stype == TYPE_INV || arg->stype == TYPE_COMPLEX)
1046 return arg->stype = TYPE_INV;
1047 return arg->stype = TYPE_PTR;
1050 case CEE_CONV_OVF_I8:
1051 case CEE_CONV_OVF_U8:
1052 case CEE_CONV_OVF_I8_UN:
1053 case CEE_CONV_OVF_U8_UN:
1054 return arg->stype = TYPE_I8;
1057 return arg->stype = TYPE_R8;
1059 g_error ("opcode 0x%04x not handled in type from op", ins);
1066 in_any_block (MonoMethodHeader *header, guint offset)
1069 MonoExceptionClause *clause;
1071 for (i = 0; i < header->num_clauses; ++i) {
1072 clause = &header->clauses [i];
1073 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1075 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1077 if (MONO_OFFSET_IN_FILTER (clause, offset))
1084 in_same_block (MonoMethodHeader *header, guint offset, guint target)
1087 MonoExceptionClause *clause;
1089 for (i = 0; i < header->num_clauses; ++i) {
1090 clause = &header->clauses [i];
1091 if (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target))
1093 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1095 /* need to check filter ... */
1101 * A leave can't escape a finally block
1104 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1107 MonoExceptionClause *clause;
1109 for (i = 0; i < header->num_clauses; ++i) {
1110 clause = &header->clauses [i];
1111 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1113 /* need to check filter ... */
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))
1197 can_merge_stack (ILCodeDesc *a, ILCodeDesc *b)
1199 if (!b->flags & IL_CODE_FLAG_SEEN) {
1200 b->flags |= IL_CODE_FLAG_SEEN;
1205 if (a->size != b->size)
1212 is_valid_bool_arg (ILStackDesc *arg)
1214 if (IS_MANAGED_POINTER (arg->stype))
1216 switch (arg->stype) {
1219 case TYPE_NATIVE_INT:
1223 g_assert (arg->type);
1224 switch (arg->type->type) {
1225 case MONO_TYPE_CLASS:
1226 case MONO_TYPE_STRING:
1227 case MONO_TYPE_OBJECT:
1228 case MONO_TYPE_SZARRAY:
1229 case MONO_TYPE_ARRAY:
1230 case MONO_TYPE_FNPTR:
1233 case MONO_TYPE_GENERICINST:
1234 /*We need to check if the container class
1235 * of the generic type is a valuetype, iow:
1236 * is it a "class Foo<T>" or a "struct Foo<T>"?
1238 return !arg->type->data.generic_class->container_class->valuetype;
1247 can_store_type (ILStackDesc *arg, MonoType *type)
1250 if (type->byref && arg->stype != TYPE_COMPLEX)
1254 case MONO_TYPE_VOID:
1258 case MONO_TYPE_BOOLEAN:
1261 case MONO_TYPE_CHAR:
1264 if (arg->stype == TYPE_I4 || arg->stype == TYPE_PTR)
1271 case MONO_TYPE_CLASS:
1272 case MONO_TYPE_STRING:
1273 case MONO_TYPE_OBJECT:
1274 case MONO_TYPE_SZARRAY:
1275 case MONO_TYPE_ARRAY:
1276 return TRUE; /* FIXME */
1279 if (arg->stype == TYPE_I8)
1284 if (arg->stype == TYPE_R8)
1287 case MONO_TYPE_VALUETYPE:
1288 if (type->data.klass->enumtype) {
1289 t = type->data.klass->enum_basetype->type;
1292 if (arg->type->data.klass != type->data.klass)
1297 g_error ("unknown type %02x in store type", type->type);
1303 stind_type (int op, int type) {
1306 return type == TYPE_COMPLEX;
1310 return type == TYPE_I4;
1312 return type == TYPE_I8;
1315 return type == TYPE_R8;
1317 g_assert_not_reached ();
1323 /*Type manipulation helper*/
1325 /*Returns the byref version of the supplied MonoType*/
1327 mono_type_get_type_byref (MonoType *type)
1331 return &mono_class_from_mono_type (type)->this_arg;
1335 /*Returns the byval version of the supplied MonoType*/
1337 mono_type_get_type_byval (MonoType *type)
1341 return &mono_class_from_mono_type (type)->byval_arg;
1345 mono_type_from_stack_slot (ILStackDesc *slot)
1347 if (IS_MANAGED_POINTER (slot->stype))
1348 return mono_type_get_type_byref (slot->type);
1352 /*Stack manipulation code*/
1355 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1358 if (!state->stack) {
1359 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1364 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1366 to->size = from->size;
1367 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1371 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1373 to->stype = from->stype;
1374 to->type = from->type;
1378 check_underflow (VerifyContext *ctx, int size)
1380 if (ctx->eval.size < size) {
1381 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1388 check_overflow (VerifyContext *ctx)
1390 if (ctx->eval.size >= ctx->max_stack) {
1391 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1398 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1400 if (value->stype == TYPE_PTR) {
1401 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1408 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1410 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1411 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1418 static ILStackDesc *
1419 stack_push (VerifyContext *ctx)
1421 return & ctx->eval.stack [ctx->eval.size++];
1424 static ILStackDesc *
1425 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1427 ILStackDesc *top = stack_push (ctx);
1433 static ILStackDesc *
1434 stack_pop (VerifyContext *ctx)
1436 return ctx->eval.stack + --ctx->eval.size;
1439 static inline ILStackDesc *
1440 stack_top (VerifyContext *ctx)
1442 return ctx->eval.stack + (ctx->eval.size - 1);
1445 static inline ILStackDesc *
1446 stack_get (VerifyContext *ctx, int distance)
1448 return ctx->eval.stack + (ctx->eval.size - distance - 1);
1451 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1453 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1456 get_boxable_mono_type (VerifyContext* ctx, int token)
1458 MonoType *type = mono_type_get_full (ctx->image, token, ctx->generic_context);
1461 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
1465 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1466 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
1470 if (type->type == MONO_TYPE_TYPEDBYREF) {
1471 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref at 0x%04x", ctx->ip_offset));
1475 check_unverifiable_type (ctx, type);
1480 /*operation result tables */
1482 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1483 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1484 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1485 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1486 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1487 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1488 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1491 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1492 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1493 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1494 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1495 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1496 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1497 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1500 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1501 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1502 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1503 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1504 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1505 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1506 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1509 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1510 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1511 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1512 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1513 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1514 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1515 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1518 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1519 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1520 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1521 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1522 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1523 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1524 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1527 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1528 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1529 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1530 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1531 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1532 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1533 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1536 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1537 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1538 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1539 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1540 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1541 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1542 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1547 dump_stack_value (ILStackDesc *value)
1549 printf ("[(%d)(%d)", value->type->type, value->stype);
1551 if (value->stype & CMMP_MASK)
1552 printf ("Controled Mutability MP: ");
1554 if (IS_MANAGED_POINTER (value->stype))
1555 printf ("Managed Pointer to: ");
1557 switch (UNMASK_TYPE (value->stype)) {
1559 printf ("invalid type]");
1567 case TYPE_NATIVE_INT:
1568 printf ("native int]");
1571 printf ("float64]");
1574 printf ("unmanaged pointer]");
1577 switch (value->type->type) {
1578 case MONO_TYPE_CLASS:
1579 case MONO_TYPE_VALUETYPE:
1580 printf ("complex] (%s)", value->type->data.klass->name);
1582 case MONO_TYPE_STRING:
1583 printf ("complex] (string)");
1585 case MONO_TYPE_OBJECT:
1586 printf ("complex] (object)");
1588 case MONO_TYPE_SZARRAY:
1589 printf ("complex] (%s [])", value->type->data.klass->name);
1591 case MONO_TYPE_ARRAY:
1592 printf ("complex] (%s [%d %d %d])",
1593 value->type->data.array->eklass->name,
1594 value->type->data.array->rank,
1595 value->type->data.array->numsizes,
1596 value->type->data.array->numlobounds);
1598 case MONO_TYPE_GENERICINST:
1599 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1602 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1604 case MONO_TYPE_MVAR:
1605 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1608 printf ("unknown complex %d type]\n", value->type->type);
1609 g_assert_not_reached ();
1612 printf ("unknown stack %d type]\n", value->stype);
1613 g_assert_not_reached ();
1618 dump_stack_state (ILCodeDesc *state)
1622 printf ("(%d) ", state->size);
1623 for (i = 0; i < state->size; ++i)
1624 dump_stack_value (state->stack + i);
1629 dump_context (VerifyContext *ctx, int code_size)
1633 for (i = 0; i < code_size; ++i) {
1634 if (ctx->code [i].flags & IL_CODE_FLAG_SEEN) {
1635 printf ("opcode [%d]:\n\t", i);
1636 dump_stack_state (&ctx->code [i]);
1641 /*Returns TRUE if candidate array type can be assigned to target.
1642 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1645 is_array_type_compatible (MonoType *target, MonoType *candidate)
1648 MonoArrayType *left = target->data.array;
1649 MonoArrayType *right = candidate->data.array;
1651 g_assert (target->type == MONO_TYPE_ARRAY);
1652 g_assert (candidate->type == MONO_TYPE_ARRAY);
1655 if ((left->rank != right->rank) ||
1656 (left->numsizes != right->numsizes) ||
1657 (left->numlobounds != right->numlobounds))
1660 for (i = 0; i < left->numsizes; ++i)
1661 if (left->sizes [i] != right->sizes [i])
1664 for (i = 0; i < left->numlobounds; ++i)
1665 if (left->lobounds [i] != right->lobounds [i])
1668 return mono_class_is_assignable_from (left->eklass, right->eklass);
1672 get_stack_type (MonoType *type)
1675 int type_kind = type->type;
1677 mask = POINTER_MASK;
1678 /*TODO handle CMMP_MASK */
1681 switch (type_kind) {
1684 case MONO_TYPE_BOOLEAN:
1687 case MONO_TYPE_CHAR:
1690 return TYPE_I4 | mask;
1694 return TYPE_NATIVE_INT | mask;
1696 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1697 case MONO_TYPE_FNPTR:
1699 case MONO_TYPE_TYPEDBYREF:
1700 return TYPE_PTR | mask;
1702 case MONO_TYPE_CLASS:
1703 case MONO_TYPE_STRING:
1704 case MONO_TYPE_OBJECT:
1705 case MONO_TYPE_SZARRAY:
1706 case MONO_TYPE_ARRAY:
1707 case MONO_TYPE_GENERICINST:
1708 return TYPE_COMPLEX | mask;
1712 return TYPE_I8 | mask;
1716 return TYPE_R8 | mask;
1718 case MONO_TYPE_VALUETYPE:
1719 if (type->data.klass->enumtype) {
1720 type = type->data.klass->enum_basetype;
1721 type_kind = type->type;
1724 return TYPE_COMPLEX | mask;
1727 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
1728 g_assert_not_reached ();
1733 /* convert MonoType to ILStackDesc format (stype) */
1735 set_stack_value (ILStackDesc *stack, MonoType *type, int take_addr)
1738 int type_kind = type->type;
1740 if (type->byref || take_addr)
1741 mask = POINTER_MASK;
1742 /* TODO handle CMMP_MASK */
1747 switch (type_kind) {
1750 case MONO_TYPE_BOOLEAN:
1753 case MONO_TYPE_CHAR:
1756 stack->stype = TYPE_I4 | mask;
1760 stack->stype = TYPE_NATIVE_INT | mask;
1763 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1764 case MONO_TYPE_FNPTR:
1766 case MONO_TYPE_TYPEDBYREF:
1767 stack->stype = TYPE_PTR | mask;
1770 case MONO_TYPE_CLASS:
1771 case MONO_TYPE_STRING:
1772 case MONO_TYPE_OBJECT:
1773 case MONO_TYPE_SZARRAY:
1774 case MONO_TYPE_ARRAY:
1776 case MONO_TYPE_GENERICINST:
1778 case MONO_TYPE_MVAR:
1779 stack->stype = TYPE_COMPLEX | mask;
1783 stack->stype = TYPE_I8 | mask;
1787 stack->stype = TYPE_R8 | mask;
1789 case MONO_TYPE_VALUETYPE:
1790 if (type->data.klass->enumtype) {
1791 type = type->data.klass->enum_basetype;
1792 type_kind = type->type;
1795 stack->stype = TYPE_COMPLEX | mask;
1799 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1800 g_assert_not_reached ();
1805 /* Generics validation stuff, should be moved to another metadata/? file */
1807 mono_is_generic_type_compatible (MonoType *target, MonoType *candidate)
1809 if (target->byref != candidate->byref)
1813 switch (target->type) {
1814 case MONO_TYPE_STRING:
1815 if (candidate->type == MONO_TYPE_STRING)
1819 case MONO_TYPE_CLASS:
1820 if (candidate->type != MONO_TYPE_CLASS)
1823 VERIFIER_DEBUG ( printf ("verifying type class %p %p\n", target->data.klass, candidate->data.klass); );
1824 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1826 case MONO_TYPE_OBJECT:
1827 return MONO_TYPE_IS_REFERENCE (candidate);
1829 case MONO_TYPE_SZARRAY:
1830 if (candidate->type != MONO_TYPE_SZARRAY)
1832 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1834 case MONO_TYPE_VALUETYPE:
1835 if (target->data.klass->enumtype) {
1836 target = target->data.klass->enum_basetype;
1839 if (candidate->type != MONO_TYPE_VALUETYPE)
1841 return candidate->data.klass == target->data.klass;
1844 case MONO_TYPE_ARRAY:
1845 if (candidate->type != MONO_TYPE_ARRAY)
1847 return is_array_type_compatible (target, candidate);
1850 VERIFIER_DEBUG ( printf ("unknown target type %d\n", target->type); );
1851 g_assert_not_reached ();
1859 mono_is_generic_instance_compatible (MonoGenericClass *target, MonoGenericClass *candidate, MonoGenericClass *root_candidate) {
1860 MonoGenericContainer *container;
1863 VERIFIER_DEBUG ( printf ("candidate container %p\n", candidate->container_class->generic_container); );
1864 if (target->container_class != candidate->container_class) {
1865 MonoType *param_class;
1866 MonoClass *cand_class;
1867 VERIFIER_DEBUG ( printf ("generic class != target\n"); );
1868 param_class = candidate->context.class_inst->type_argv [0];
1869 VERIFIER_DEBUG ( printf ("param 0 %d\n", param_class->type); );
1870 cand_class = candidate->container_class;
1872 /* We must check if it's an interface type*/
1873 if (MONO_CLASS_IS_INTERFACE (target->container_class)) {
1874 VERIFIER_DEBUG ( printf ("generic type is an interface\n"); );
1877 int iface_count = cand_class->interface_count;
1878 MonoClass **ifaces = cand_class->interfaces;
1880 VERIFIER_DEBUG ( printf ("type has %d interfaces\n", iface_count); );
1881 for (i = 0; i< iface_count; ++i) {
1882 MonoClass *ifc = ifaces[i];
1883 VERIFIER_DEBUG ( printf ("analysing %s\n", ifc->name); );
1884 if (ifc->generic_class) {
1885 VERIFIER_DEBUG ( printf ("interface has generic info\n"); );
1887 if (mono_is_generic_instance_compatible (target, ifc->generic_class, root_candidate)) {
1888 VERIFIER_DEBUG ( printf ("we got compatible stuff!\n"); );
1893 cand_class = cand_class->parent;
1894 } while (cand_class);
1896 VERIFIER_DEBUG ( printf ("don't implements an interface\n"); );
1899 VERIFIER_DEBUG ( printf ("verifying upper classes\n"); );
1901 cand_class = cand_class->parent;
1903 while (cand_class) {
1904 VERIFIER_DEBUG ( printf ("verifying parent class name %s\n", cand_class->name); );
1905 if (cand_class->generic_class) {
1906 VERIFIER_DEBUG ( printf ("super type has generic context\n"); );
1908 /* TODO break loop if target->container_class == cand_class->generic_class->container_class */
1909 return mono_is_generic_instance_compatible (target, cand_class->generic_class, root_candidate);
1911 VERIFIER_DEBUG ( printf ("super class has no generic context\n"); );
1912 cand_class = cand_class->parent;
1918 /* now we verify if the instantiations are compatible*/
1919 if (target->context.class_inst == candidate->context.class_inst) {
1920 VERIFIER_DEBUG ( printf ("generic types are compatible, both have the same instantiation\n"); );
1924 if (target->context.class_inst->type_argc != candidate->context.class_inst->type_argc) {
1925 VERIFIER_DEBUG ( printf ("generic instantiations with different arg counts\n"); );
1929 //verify if open instance -- none should be
1931 container = target->container_class->generic_container;
1933 for (i = 0; i < container->type_argc; ++i) {
1934 MonoGenericParam *param = container->type_params + i;
1935 MonoType *target_type = target->context.class_inst->type_argv [i];
1936 MonoType *candidate_type = candidate->context.class_inst->type_argv [i];
1937 /* We resolve TYPE_VAR types before proceeding */
1939 if (candidate_type->type == MONO_TYPE_VAR) {
1940 MonoGenericParam *var_param = candidate_type->data.generic_param;
1941 candidate_type = root_candidate->context.class_inst->type_argv [var_param->num];
1944 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK) == 0) {
1945 VERIFIER_DEBUG ( printf ("generic type have no variance flag, checking each type %d %d \n",target_type->type, candidate_type->type); );
1947 if (!mono_metadata_type_equal (target_type, candidate_type))
1950 VERIFIER_DEBUG ( printf ("generic type has variance flag, need to perform deeper check\n"); );
1951 /* first we check if they are the same kind */
1952 /* byref generic params are forbiden, but better safe than sorry.*/
1954 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) == GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) {
1955 if (!mono_is_generic_type_compatible (target_type, candidate_type))
1957 /* the attribute must be contravariant */
1958 } else if (!mono_is_generic_type_compatible (candidate_type, target_type))
1967 /*Verify if type 'candidate' can be stored in type 'target'.
1969 * If strict, check for the underlying type and not the verification stack types
1972 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1974 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1975 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1977 VERIFIER_DEBUG ( printf ("checking type compatibility %p %p[%d][%d] %p[%d][%d]\n", ctx, target, target->type, target->byref, candidate, candidate->type, candidate->byref); );
1979 /*only one is byref */
1980 if (candidate->byref ^ target->byref) {
1981 /* converting from native int to byref*/
1982 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1983 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1988 strict |= target->byref;
1991 switch (target->type) {
1994 case MONO_TYPE_BOOLEAN:
1996 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1999 case MONO_TYPE_CHAR:
2001 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
2003 case MONO_TYPE_U4: {
2004 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2005 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2007 return is_native_int || is_int4;
2008 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2013 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
2018 return candidate->type == target->type;
2019 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
2023 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
2024 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
2026 return is_native_int || is_int4;
2027 return is_native_int || get_stack_type (candidate) == TYPE_I4;
2031 if (candidate->type != MONO_TYPE_PTR)
2033 /* check the underlying type */
2034 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
2036 case MONO_TYPE_FNPTR: {
2037 MonoMethodSignature *left, *right;
2038 if (candidate->type != MONO_TYPE_FNPTR)
2041 left = mono_type_get_signature (target);
2042 right = mono_type_get_signature (candidate);
2043 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2046 case MONO_TYPE_GENERICINST: {
2047 MonoGenericClass *left;
2048 MonoGenericClass *right;
2049 if (candidate->type != MONO_TYPE_GENERICINST)
2051 left = target->data.generic_class;
2052 right = candidate->data.generic_class;
2054 return mono_is_generic_instance_compatible (left, right, right);
2057 case MONO_TYPE_STRING:
2058 return candidate->type == MONO_TYPE_STRING;
2060 case MONO_TYPE_CLASS:
2061 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (candidate));
2063 case MONO_TYPE_OBJECT:
2064 return MONO_TYPE_IS_REFERENCE (candidate);
2066 case MONO_TYPE_SZARRAY: {
2069 if (candidate->type != MONO_TYPE_SZARRAY)
2072 left = target->data.klass;
2073 right = candidate->data.klass;
2074 return mono_class_is_assignable_from(left, right);
2077 case MONO_TYPE_ARRAY:
2078 if (candidate->type != MONO_TYPE_ARRAY)
2080 return is_array_type_compatible (target, candidate);
2082 //TODO verify aditional checks that needs to be done
2083 case MONO_TYPE_TYPEDBYREF:
2084 return candidate->type == MONO_TYPE_TYPEDBYREF;
2086 case MONO_TYPE_VALUETYPE:
2087 if (target->data.klass->enumtype) {
2088 target = target->data.klass->enum_basetype;
2091 if (candidate->type != MONO_TYPE_VALUETYPE)
2093 return target->data.klass == candidate->data.klass;
2097 if (candidate->type != MONO_TYPE_VAR)
2099 return candidate->data.generic_param->num == target->data.generic_param->num;
2101 case MONO_TYPE_MVAR:
2102 if (candidate->type != MONO_TYPE_MVAR)
2104 return candidate->data.generic_param->num == target->data.generic_param->num;
2107 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2108 g_assert_not_reached ();
2117 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2119 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2124 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2126 return verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (stack), FALSE);
2129 /* implement the opcode checks*/
2131 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2133 if (arg >= ctx->max_args) {
2135 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2137 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2138 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2139 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2141 } else if (check_overflow (ctx)) {
2142 /*We must let the value be pushed, otherwise we would get an underflow error*/
2143 check_unverifiable_type (ctx, ctx->params [arg]);
2144 if (ctx->params [arg]->byref && take_addr)
2145 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2146 set_stack_value (stack_push (ctx), ctx->params [arg], take_addr);
2151 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2153 if (arg >= ctx->num_locals) {
2154 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2155 } else if (check_overflow (ctx)) {
2156 /*We must let the value be pushed, otherwise we would get an underflow error*/
2157 check_unverifiable_type (ctx, ctx->locals [arg]);
2158 if (ctx->locals [arg]->byref && take_addr)
2159 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2161 set_stack_value (stack_push (ctx), ctx->locals [arg], take_addr);
2166 store_arg (VerifyContext *ctx, guint32 arg)
2170 if (arg >= ctx->max_args) {
2171 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2172 check_underflow (ctx, 1);
2177 if (check_underflow (ctx, 1)) {
2178 value = stack_pop (ctx);
2179 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2180 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2186 store_local (VerifyContext *ctx, guint32 arg)
2189 if (arg >= ctx->num_locals) {
2190 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2194 /*TODO verify definite assigment */
2195 if (check_underflow (ctx, 1)) {
2196 value = stack_pop(ctx);
2197 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2198 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in local store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2204 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2207 int idxa, idxb, complexMerge = 0;
2210 if (!check_underflow (ctx, 2))
2212 a = stack_get (ctx, 1);
2213 b = stack_top (ctx);
2216 if (IS_MANAGED_POINTER (idxa)) {
2222 if (IS_MANAGED_POINTER (idxb)) {
2229 res = table [idxa][idxb];
2231 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2232 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2235 if (res == TYPE_INV) {
2236 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction applyed to ill formed stack (%s x %s)", type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)]));
2240 if (res & NON_VERIFIABLE_RESULT) {
2241 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)",
2242 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)]));
2244 res = res & ~NON_VERIFIABLE_RESULT;
2247 if (complexMerge && res == TYPE_PTR) {
2248 if (complexMerge == 1)
2249 copy_stack_value (stack_top (ctx), a);
2250 else if (complexMerge == 2)
2251 copy_stack_value (stack_top (ctx), b);
2253 * There is no need to merge the type of two pointers.
2254 * The only valid operation is subtraction, that returns a native
2255 * int as result and can be used with any 2 pointer kinds.
2256 * This is valid acording to Patition III 1.1.4
2259 stack_top (ctx)->stype = res;
2265 do_boolean_branch_op (VerifyContext *ctx, int delta)
2267 int target = ctx->ip_offset + delta;
2270 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2272 if (target < 0 || target >= ctx->code_size) {
2273 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2277 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2278 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2282 ctx->target = target;
2284 if (!check_underflow (ctx, 1))
2287 top = stack_pop (ctx);
2288 if (!is_valid_bool_arg (top))
2289 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", type_names [UNMASK_TYPE (stack_get (ctx, -1)->stype)], ctx->ip_offset));
2291 check_unmanaged_pointer (ctx, top);
2296 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2301 int target = ctx->ip_offset + delta;
2303 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2305 if (target < 0 || target >= ctx->code_size) {
2306 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2310 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2311 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2315 ctx->target = target;
2317 if (!check_underflow (ctx, 2))
2320 b = stack_pop (ctx);
2321 a = stack_pop (ctx);
2324 if (IS_MANAGED_POINTER (idxa))
2328 if (IS_MANAGED_POINTER (idxb))
2333 res = table [idxa][idxb];
2335 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2336 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2338 if (res == TYPE_INV) {
2339 ADD_VERIFY_ERROR (ctx,
2340 g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x",
2341 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2342 } else if (res & NON_VERIFIABLE_RESULT) {
2343 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x",
2344 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2345 res = res & ~NON_VERIFIABLE_RESULT;
2350 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX])
2356 if (!check_underflow (ctx, 2))
2358 b = stack_pop (ctx);
2359 a = stack_pop (ctx);
2362 if (IS_MANAGED_POINTER (idxa))
2366 if (IS_MANAGED_POINTER (idxb))
2371 res = table [idxa][idxb];
2373 if(res == TYPE_INV) {
2374 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2375 } else if (res & NON_VERIFIABLE_RESULT) {
2376 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x",
2377 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2378 res = res & ~NON_VERIFIABLE_RESULT;
2380 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2384 do_ret (VerifyContext *ctx)
2386 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2387 if (ctx->signature->ret->type != MONO_TYPE_VOID) {
2389 if (!check_underflow (ctx, 1))
2392 top = stack_pop(ctx);
2394 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2395 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2400 if (ctx->eval.size > 0) {
2401 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2403 if (in_any_block (ctx->header, ctx->ip_offset))
2404 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2407 /* FIXME: we could just load the signature instead of the whole MonoMethod
2408 * TODO handle vararg calls
2409 * TODO handle non virt calls to non-final virtual calls (from the verifiability clause in page 52 of partition 3)
2410 * TODO handle abstract calls
2411 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2412 * TODO handle call invoking virtual methods (only allowed to invoke super)
2415 do_invoke_method (VerifyContext *ctx, int method_token)
2418 MonoMethodSignature *sig;
2420 MonoMethod *method = mono_get_method_full (ctx->image, method_token, NULL, ctx->generic_context);
2423 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method 0x%08x not found at 0x%04x", method_token, ctx->ip_offset));
2427 if (!(sig = mono_method_signature (method)))
2428 sig = mono_method_get_signature (method, ctx->image, method_token);
2430 param_count = sig->param_count + sig->hasthis;
2431 if (!check_underflow (ctx, param_count))
2434 for (i = sig->param_count - 1; i >= 0; --i) {
2435 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2436 value = stack_pop (ctx);
2437 if (!verify_stack_type_compatibility (ctx, sig->params[i], value))
2438 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2442 MonoType * type = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2444 VERIFIER_DEBUG ( printf ("verifying this argument\n"); );
2445 value = stack_pop (ctx);
2446 if (!verify_stack_type_compatibility (ctx, type, value)) {
2447 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature ret at 0x%04x", ctx->ip_offset));
2451 if (!mono_method_can_access_method (ctx->method, method))
2452 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset));
2454 if (sig->ret->type != MONO_TYPE_VOID) {
2455 if (check_overflow (ctx))
2456 set_stack_value (stack_push (ctx), sig->ret, FALSE);
2459 if (sig->ret->type == MONO_TYPE_TYPEDBYREF
2461 || (sig->ret->type == MONO_TYPE_VALUETYPE && !strcmp ("System", sig->ret->data.klass->name_space) && !strcmp ("ArgIterator", sig->ret->data.klass->name)))
2462 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns typedbyref, byref or ArgIterator at 0x%04x", ctx->ip_offset));
2466 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2468 MonoClassField *field;
2471 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2473 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2477 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2478 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
2481 /*taking the address of initonly field only works from the static constructor */
2482 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2483 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
2484 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2486 if (!mono_method_can_access_field (ctx->method, field))
2487 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2489 set_stack_value (stack_push (ctx), field->type, take_addr);
2493 do_store_static_field (VerifyContext *ctx, int token) {
2494 MonoClassField *field;
2498 if (!check_underflow (ctx, 1))
2501 value = stack_pop (ctx);
2503 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2505 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2509 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2510 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
2514 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2515 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
2519 if (!mono_method_can_access_field (ctx->method, field))
2520 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2522 if (!verify_stack_type_compatibility (ctx, field->type, value))
2523 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in static field store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2527 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field)
2529 MonoClassField *field;
2532 /*must be one of: object type, managed pointer, unmanaged pointer (native int) or an instance of a value type */
2533 if (!((obj->stype == TYPE_COMPLEX)
2534 /* the managed reference must be to an object or value type */
2535 || (( IS_MANAGED_POINTER (obj->stype)) && (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX))
2536 || (obj->stype == TYPE_NATIVE_INT)
2537 || (obj->stype == TYPE_PTR)
2538 || (obj->stype == TYPE_COMPLEX))) {
2539 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument %s to load field at 0x%04x", type_names [UNMASK_TYPE (obj->stype)], ctx->ip_offset));
2542 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2544 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2550 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2551 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
2554 g_assert (obj->type);
2556 /*The value on the stack must be a subclass of the defining type of the field*/
2557 /* we need to check if we can load the field from the stack value*/
2558 if (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX) {
2559 MonoType *type = obj->type->byref ? &field->parent->this_arg : &field->parent->byval_arg;
2561 if (!verify_type_compatibility (ctx, type, obj->type)) {
2562 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
2565 if (!mono_method_can_access_field (ctx->method, field))
2566 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2569 if (!mono_method_can_access_field (ctx->method, field))
2570 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2572 if (obj->stype == TYPE_NATIVE_INT)
2573 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
2575 check_unmanaged_pointer (ctx, obj);
2580 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
2583 MonoClassField *field;
2585 if (!check_underflow (ctx, 1))
2587 obj = stack_pop (ctx);
2589 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2592 if (take_addr && field->parent->valuetype && !IS_MANAGED_POINTER (obj->stype))
2593 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
2595 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2596 !(field->parent == ctx->method->klass && (ctx->method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && !strcmp (".ctor", ctx->method->name)))
2597 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2599 set_stack_value (stack_push (ctx), field->type, take_addr);
2603 do_store_field (VerifyContext *ctx, int token)
2605 ILStackDesc *value, *obj;
2606 MonoClassField *field;
2608 if (!check_underflow (ctx, 2))
2611 value = stack_pop (ctx);
2612 obj = stack_pop (ctx);
2614 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2617 if (!verify_stack_type_compatibility (ctx, field->type, value))
2618 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2621 /*TODO proper handle for Nullable<T>*/
2623 do_box_value (VerifyContext *ctx, int klass_token)
2626 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2631 if (!check_underflow (ctx, 1))
2634 value = stack_top (ctx);
2635 /*box is a nop for reference types*/
2636 if (value->stype == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type))
2639 value = stack_pop (ctx);
2641 if (!verify_stack_type_compatibility (ctx, type, value))
2642 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
2644 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (mono_defaults.object_class));
2648 do_unbox_value (VerifyContext *ctx, int klass_token)
2651 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2656 if (!check_underflow (ctx, 1))
2659 value = stack_pop (ctx);
2661 if (value->stype != TYPE_COMPLEX || value->type->type != MONO_TYPE_OBJECT)
2662 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox operation at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2664 //TODO Pushed managed pointer is haver controled mutability (CMMP)
2665 set_stack_value (stack_push (ctx), mono_type_get_type_byref (type), FALSE);
2669 do_unary_math_op (VerifyContext *ctx, int op)
2672 if (!check_underflow (ctx, 1))
2674 value = stack_top (ctx);
2675 switch(value->stype) {
2678 case TYPE_NATIVE_INT:
2683 case TYPE_COMPLEX: /*only enums are ok*/
2684 if (value->type->type == MONO_TYPE_VALUETYPE && value->type->data.klass->enumtype)
2687 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
2692 do_conversion (VerifyContext *ctx, int kind)
2695 if (!check_underflow (ctx, 1))
2697 value = stack_pop (ctx);
2699 switch(value->stype) {
2702 case TYPE_NATIVE_INT:
2706 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for conversion operation at 0x%04x", ctx->ip_offset));
2711 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2714 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
2717 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
2719 case TYPE_NATIVE_INT:
2720 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
2723 g_error ("unknown type %02x in conversion", kind);
2729 do_load_token (VerifyContext *ctx, int token)
2732 MonoClass *handle_class;
2733 if (!check_overflow (ctx))
2735 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
2737 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
2740 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
2744 do_ldobj_value (VerifyContext *ctx, int token)
2747 MonoType *type = get_boxable_mono_type (ctx, token);
2752 if (!check_underflow (ctx, 1))
2755 value = stack_pop (ctx);
2756 if (!IS_MANAGED_POINTER (value->stype)
2757 && value->stype != TYPE_NATIVE_INT
2758 && !(value->stype == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
2759 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2763 if (value->stype == TYPE_NATIVE_INT)
2764 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
2766 /*We have a byval on the stack, but the comparison must be strict. */
2767 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
2768 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
2770 set_stack_value (stack_push (ctx), type, FALSE);
2773 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_PUBLIC)
2774 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
2775 /* TODO implement delegate verification */
2777 do_newobj (VerifyContext *ctx, int token)
2781 MonoMethodSignature *sig;
2782 MonoMethod *method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
2784 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ctx->ip_offset));
2788 if ((method->flags & CTOR_REQUIRED_FLAGS) != CTOR_REQUIRED_FLAGS
2789 || (method->flags & CTOR_INVALID_FLAGS) != 0
2790 || strcmp (".ctor", method->name)) {
2791 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
2795 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
2796 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
2798 sig = mono_method_signature (method);
2799 if (!check_underflow (ctx, sig->param_count))
2802 for (i = sig->param_count - 1; i >= 0; --i) {
2803 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
2804 value = stack_pop (ctx);
2805 if (!verify_stack_type_compatibility (ctx, sig->params [i], value))
2806 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2809 if (check_overflow (ctx))
2810 set_stack_value (stack_push (ctx), &method->klass->byval_arg, FALSE);
2814 do_cast (VerifyContext *ctx, int token) {
2818 if (!check_underflow (ctx, 1))
2821 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
2824 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
2829 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid castclass type at 0x%04x", ctx->ip_offset));
2833 value = stack_top (ctx);
2835 if (IS_MANAGED_POINTER (value->stype))
2836 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for checkast at 0x%04x", ctx->ip_offset));
2837 else if (mono_class_from_mono_type (value->type)->valuetype)
2838 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Value cannot be a valuetype for checkast at 0x%04x", ctx->ip_offset));
2840 switch (value->type->type) {
2841 case MONO_TYPE_FNPTR:
2843 case MONO_TYPE_TYPEDBYREF:
2844 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for checkast at 0x%04x", ctx->ip_offset));
2847 set_stack_value (value, type, FALSE);
2851 mono_type_from_opcode (int opcode) {
2859 return &mono_defaults.sbyte_class->byval_arg;
2867 return &mono_defaults.int16_class->byval_arg;
2875 return &mono_defaults.int32_class->byval_arg;
2881 return &mono_defaults.int64_class->byval_arg;
2887 return &mono_defaults.single_class->byval_arg;
2893 return &mono_defaults.double_class->byval_arg;
2899 return &mono_defaults.int_class->byval_arg;
2903 case CEE_LDELEM_REF:
2904 case CEE_STELEM_REF:
2905 return &mono_defaults.object_class->byval_arg;
2908 g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
2914 do_load_indirect (VerifyContext *ctx, int opcode)
2917 if (!check_underflow (ctx, 1))
2920 value = stack_pop (ctx);
2921 if (!IS_MANAGED_POINTER (value->stype)) {
2922 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
2923 set_stack_value (stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
2927 if (opcode == CEE_LDIND_REF) {
2928 if (UNMASK_TYPE (value->stype) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
2929 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
2930 set_stack_value (stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
2932 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
2933 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2934 set_stack_value (stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
2939 do_store_indirect (VerifyContext *ctx, int opcode)
2941 ILStackDesc *addr, *val;
2942 if (!check_underflow (ctx, 2))
2945 val = stack_pop (ctx);
2946 addr = stack_pop (ctx);
2948 check_unmanaged_pointer (ctx, addr);
2950 if (!IS_MANAGED_POINTER (addr->stype) && addr->stype != TYPE_PTR) {
2951 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
2955 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
2956 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2958 if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (val->type), FALSE))
2959 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2963 do_newarr (VerifyContext *ctx, int token)
2966 MonoType *type = get_boxable_mono_type (ctx, token);
2971 if (!check_underflow (ctx, 1))
2974 value = stack_pop (ctx);
2975 if (value->stype != TYPE_I4 && value->stype != TYPE_NATIVE_INT)
2976 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Array size type on stack (%s) is not a verifiable type at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2978 set_stack_value (stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
2981 /*FIXME handle arrays that are not 0-indexed*/
2983 do_ldlen (VerifyContext *ctx)
2987 if (!check_underflow (ctx, 1))
2990 value = stack_pop (ctx);
2992 if (value->stype != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
2993 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
2995 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
2998 /*FIXME handle arrays that are not 0-indexed*/
2999 /*FIXME handle readonly prefix and CMMP*/
3001 do_ldelema (VerifyContext *ctx, int klass_token)
3003 ILStackDesc *index, *array;
3004 MonoType *type = get_boxable_mono_type (ctx, klass_token);
3010 if (!check_underflow (ctx, 2))
3013 index = stack_pop (ctx);
3014 array = stack_pop (ctx);
3016 if (index->stype != TYPE_I4 && index->stype != TYPE_NATIVE_INT)
3017 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelema is not an int or a native int at 0x%04x", type_names [UNMASK_TYPE (index->stype)], ctx->ip_offset));
3019 if (!IS_NULL_LITERAL (array->stype)) {
3020 if (array->stype != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3021 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", type_names [UNMASK_TYPE (array->stype)], ctx->ip_offset));
3023 if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT)
3024 valid = verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE);
3026 valid = mono_metadata_type_equal (type, &array->type->data.klass->byval_arg);
3028 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
3032 set_stack_value (stack_push (ctx), type, TRUE);
3035 /*FIXME handle arrays that are not 0-indexed*/
3036 /*FIXME handle readonly prefix and CMMP*/
3038 do_ldelem (VerifyContext *ctx, int opcode, int token)
3040 ILStackDesc *index, *array;
3042 if (!check_underflow (ctx, 2))
3045 if (opcode == CEE_LDELEM_ANY) {
3046 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
3048 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3052 type = mono_type_from_opcode (opcode);
3055 index = stack_pop (ctx);
3056 array = stack_pop (ctx);
3058 if (index->stype != TYPE_I4 && index->stype != TYPE_NATIVE_INT)
3059 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelema is not an int or a native int at 0x%04x", type_names [UNMASK_TYPE (index->stype)], ctx->ip_offset));
3061 if (!IS_NULL_LITERAL (array->stype)) {
3062 if (array->stype != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
3063 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelem.X at 0x%04x", type_names [UNMASK_TYPE (array->stype)], ctx->ip_offset));
3065 if (opcode == CEE_LDELEM_REF) {
3066 if (array->type->data.klass->valuetype)
3067 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
3068 type = &array->type->data.klass->byval_arg;
3069 } else if (!verify_type_compatibility_full (ctx, type, &array->type->data.klass->byval_arg, TRUE)) {
3070 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
3075 set_stack_value (stack_push (ctx), type, FALSE);
3078 /*FIXME handle arrays that are not 0-indexed*/
3080 do_stelem (VerifyContext *ctx, int opcode, int token)
3082 ILStackDesc *index, *array, *value;
3084 if (!check_underflow (ctx, 3))
3087 if (opcode == CEE_STELEM_ANY) {
3088 type = mono_type_get_full (ctx->image, token, ctx->generic_context);
3090 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
3094 type = mono_type_from_opcode (opcode);
3097 value = stack_pop (ctx);
3098 index = stack_pop (ctx);
3099 array = stack_pop (ctx);
3101 if (index->stype != TYPE_I4 && index->stype != TYPE_NATIVE_INT)
3102 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for stdelem.X is not an int or a native int at 0x%04x", type_names [UNMASK_TYPE (index->stype)], ctx->ip_offset));
3104 if (!IS_NULL_LITERAL (array->stype)) {
3105 if (array->stype != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
3106 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for stelem.X at 0x%04x", type_names [UNMASK_TYPE (array->stype)], ctx->ip_offset));
3108 if (opcode == CEE_STELEM_REF) {
3109 if (array->type->data.klass->valuetype)
3110 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3111 } else if (!verify_type_compatibility_full (ctx, &array->type->data.klass->byval_arg, type, TRUE)) {
3112 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3117 if (opcode == CEE_STELEM_REF) {
3118 if (mono_class_from_mono_type(value->type)->valuetype)
3119 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
3120 } else if (opcode != CEE_STELEM_REF && !verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (value), FALSE)) {
3121 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
3126 do_throw (VerifyContext *ctx)
3128 ILStackDesc *exception;
3129 if (!check_underflow (ctx, 1))
3131 exception = stack_pop (ctx);
3133 if (!IS_NULL_LITERAL (exception->stype) && !(exception->stype == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
3134 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
3136 /*The stack is left empty after a throw*/
3142 do_endfilter (VerifyContext *ctx)
3144 MonoExceptionClause *clause;
3146 if (IS_STRICT_MODE (ctx)) {
3147 if (ctx->eval.size != 1)
3148 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
3150 if (ctx->eval.size >= 1 && stack_pop (ctx)->stype != TYPE_I4)
3151 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
3154 if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
3155 if (IS_STRICT_MODE (ctx)) {
3156 if (ctx->ip_offset != clause->handler_offset - 2)
3157 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3159 if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
3160 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
3163 if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
3164 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3166 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
3173 /*Merge the stacks and perform compat checks*/
3175 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start)
3179 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
3180 stack_init (ctx, to);
3183 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
3186 stack_copy (&ctx->eval, to);
3188 } else if (to->flags == IL_CODE_FLAG_NOT_PROCESSED) {
3189 stack_copy (to, from);
3192 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
3194 if (from->size != to->size) {
3195 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d\n", from->size, to->size); );
3196 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d)", from->size, to->size));
3200 for (i = 0; i < from->size; ++i) {
3201 ILStackDesc *from_slot = from->stack + i;
3202 ILStackDesc *to_slot = to->stack + i;
3204 if (!verify_type_compatibility (ctx, mono_type_from_stack_slot (to_slot), mono_type_from_stack_slot (from_slot))) {
3205 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, types not compatible at 0x%04x", ctx->ip_offset));
3209 /*TODO we need to choose the base class for merging reference types*/
3210 copy_stack_value (to_slot, from_slot);
3214 to->flags = IL_CODE_FLAG_SEEN;
3219 * FIXME: need to distinguish between valid and verifiable.
3220 * Need to keep track of types on the stack.
3221 * Verify types for opcodes.
3224 mono_method_verify (MonoMethod *method, int level)
3226 const unsigned char *ip;
3227 const unsigned char *end;
3228 const unsigned char *target = NULL; /* branch target */
3229 int i, n, need_merge = 0, start = 0;
3230 guint token, ip_offset = 0, prefix = 0;
3231 MonoGenericContext *generic_context = NULL;
3234 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name, method->klass->name, method->name); );
3236 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3237 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
3241 memset (&ctx, 0, sizeof (VerifyContext));
3243 ctx.signature = mono_method_signature (method);
3244 if (!ctx.signature) {
3245 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
3248 ctx.header = mono_method_get_header (method);
3250 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
3253 ctx.method = method;
3254 ip = ctx.header->code;
3255 end = ip + ctx.header->code_size;
3256 ctx.image = image = method->klass->image;
3259 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
3260 ctx.max_stack = ctx.header->max_stack;
3261 ctx.verifiable = ctx.valid = 1;
3264 ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
3265 ctx.code_size = ctx.header->code_size;
3267 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
3270 ctx.num_locals = ctx.header->num_locals;
3271 ctx.locals = ctx.header->locals;
3274 if (ctx.signature->hasthis) {
3275 ctx.params = g_new0 (MonoType*, ctx.max_args);
3276 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
3277 memcpy (ctx.params + 1, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
3279 ctx.params = ctx.signature->params;
3282 if (ctx.signature->is_inflated)
3283 ctx.generic_context = generic_context = mono_method_get_context (method);
3285 stack_init (&ctx, &ctx.eval);
3288 for (i = 0; i < ctx.header->num_clauses; ++i) {
3289 MonoExceptionClause *clause = ctx.header->clauses + i;
3290 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);
3291 /* catch blocks and filter have the exception on the stack. */
3292 /* must check boundaries for handler_offset and handler_start < handler_start*/
3293 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
3294 ILCodeDesc *code = ctx.code + clause->handler_offset;
3295 stack_init (&ctx, code);
3296 code->stack [0].stype = TYPE_COMPLEX;
3297 code->stack [0].type = &clause->data.catch_class->byval_arg;
3299 code->flags = IL_CODE_FLAG_SEEN;
3301 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3302 ILCodeDesc *code = ctx.code + clause->data.filter_offset;
3303 stack_init (&ctx, code);
3304 code->stack [0].stype = TYPE_COMPLEX;
3305 code->stack [0].type = &mono_defaults.exception_class->byval_arg;
3307 code->flags = IL_CODE_FLAG_SEEN;
3311 while (ip < end && ctx.valid) {
3312 ctx.ip_offset = ip_offset = ip - ctx.header->code;
3314 /*TODO id stack merge fails, we break, should't we - or only on errors??
3315 TODO verify need_merge
3318 VERIFIER_DEBUG ( printf ("extra merge needed! %d \n", ctx.target); );
3319 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE);
3322 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start);
3325 #ifdef MONO_VERIFIER_DEBUG
3328 discode = mono_disasm_code_one (NULL, method, ip, NULL);
3329 discode [strlen (discode) - 1] = 0; /* no \n */
3330 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
3333 dump_stack_state (&ctx.code [ip_offset]);
3346 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
3352 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
3357 do_binop (&ctx, *ip, add_table);
3362 do_binop (&ctx, *ip, sub_table);
3369 do_binop (&ctx, *ip, bin_op_table);
3378 do_binop (&ctx, *ip, int_bin_op_table);
3385 do_binop (&ctx, *ip, shift_op_table);
3390 if (!check_underflow (&ctx, 1))
3406 /*TODO support definite assignment verification? */
3407 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
3415 store_local (&ctx, *ip - CEE_STLOC_0);
3420 store_local (&ctx, ip [1]);
3425 store_arg (&ctx, ip [1]);
3439 if (check_overflow (&ctx))
3440 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3445 if (check_overflow (&ctx))
3446 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3451 if (check_overflow (&ctx))
3452 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
3457 if (check_overflow (&ctx))
3458 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3463 if (check_overflow (&ctx))
3464 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3469 if (check_overflow (&ctx))
3470 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3475 if (check_overflow (&ctx))
3476 stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, &mono_defaults.object_class->byval_arg);
3482 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
3495 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
3502 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
3515 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
3522 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
3526 /* FIXME: warn/error instead? */
3533 if (!check_underflow (&ctx, 1))
3535 if (!check_overflow (&ctx))
3537 top = stack_push (&ctx);
3538 copy_stack_value (top, stack_get (&ctx, 1));
3545 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
3546 token = read32 (ip + 1);
3547 if (in_any_block (ctx.header, ip_offset))
3548 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
3550 * FIXME: check signature, retval, arguments etc.
3556 do_invoke_method (&ctx, read32 (ip + 1));
3561 token = read32 (ip + 1);
3563 * FIXME: check signature, retval, arguments etc.
3568 target = ip + (signed char)ip [1] + 2;
3569 if (target >= end || target < ctx.header->code)
3570 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3571 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3572 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3579 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
3585 target = ip + (gint32)read32 (ip + 1) + 5;
3586 if (target >= end || target < ctx.header->code)
3587 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3588 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3589 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3596 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
3602 n = read32 (ip + 1);
3603 target = ip + sizeof (guint32) * n;
3604 /* FIXME: check that ip is in range (and within the same exception block) */
3605 for (i = 0; i < n; ++i)
3606 if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < ctx.header->code)
3607 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3608 if (!check_underflow (&ctx, 1))
3610 if (stack_pop (&ctx)->stype != TYPE_I4)
3611 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ip_offset));
3612 ip += 5 + sizeof (guint32) * n;
3626 do_load_indirect (&ctx, *ip);
3638 do_store_indirect (&ctx, *ip);
3644 do_unary_math_op (&ctx, *ip);
3648 //TODO: implement proper typecheck
3655 do_conversion (&ctx, TYPE_I4);
3661 do_conversion (&ctx, TYPE_I8);
3668 do_conversion (&ctx, TYPE_R8);
3674 do_conversion (&ctx, TYPE_NATIVE_INT);
3679 token = read32 (ip + 1);
3680 if (!check_underflow (&ctx, 2))
3687 do_ldobj_value (&ctx, read32 (ip + 1));
3692 /*TODO verify if token is a valid string literal*/
3693 token = read32 (ip + 1);
3694 if (check_overflow (&ctx))
3695 stack_push_val (&ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
3700 do_newobj (&ctx, read32 (ip + 1));
3706 do_cast (&ctx, read32 (ip + 1));
3712 ++ip; /* warn, error ? */
3715 do_unbox_value (&ctx, read32 (ip + 1));
3726 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
3732 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
3737 do_store_field (&ctx, read32 (ip + 1));
3742 do_store_static_field (&ctx, read32 (ip + 1));
3746 if (!check_underflow (&ctx, 2))
3749 token = read32 (ip + 1);
3753 case CEE_CONV_OVF_I1_UN:
3754 case CEE_CONV_OVF_I2_UN:
3755 case CEE_CONV_OVF_I4_UN:
3756 case CEE_CONV_OVF_U1_UN:
3757 case CEE_CONV_OVF_U2_UN:
3758 case CEE_CONV_OVF_U4_UN:
3759 do_conversion (&ctx, TYPE_I4);
3763 case CEE_CONV_OVF_I8_UN:
3764 case CEE_CONV_OVF_U8_UN:
3765 do_conversion (&ctx, TYPE_I8);
3769 case CEE_CONV_OVF_I_UN:
3770 case CEE_CONV_OVF_U_UN:
3771 do_conversion (&ctx, TYPE_NATIVE_INT);
3776 do_box_value (&ctx, read32 (ip + 1));
3781 do_newarr (&ctx, read32 (ip + 1));
3791 do_ldelema (&ctx, read32 (ip + 1));
3805 case CEE_LDELEM_REF:
3806 do_ldelem (&ctx, *ip, 0);
3817 case CEE_STELEM_REF:
3818 do_stelem (&ctx, *ip, 0);
3822 case CEE_LDELEM_ANY:
3823 do_ldelem (&ctx, *ip, read32 (ip + 1));
3827 case CEE_STELEM_ANY:
3828 do_stelem (&ctx, *ip, read32 (ip + 1));
3846 ++ip; /* warn, error ? */
3849 case CEE_CONV_OVF_I1:
3850 case CEE_CONV_OVF_U1:
3851 case CEE_CONV_OVF_I2:
3852 case CEE_CONV_OVF_U2:
3853 case CEE_CONV_OVF_I4:
3854 case CEE_CONV_OVF_U4:
3855 do_conversion (&ctx, TYPE_I4);
3859 case CEE_CONV_OVF_I8:
3860 case CEE_CONV_OVF_U8:
3861 do_conversion (&ctx, TYPE_I8);
3865 case CEE_CONV_OVF_I:
3866 case CEE_CONV_OVF_U:
3867 do_conversion (&ctx, TYPE_NATIVE_INT);
3878 ++ip; /* warn, error ? */
3881 if (!check_underflow (&ctx, 1))
3886 if (!check_underflow (&ctx, 1))
3892 ++ip; /* warn, error ? */
3895 if (!check_underflow (&ctx, 1))
3897 token = read32 (ip + 1);
3909 ++ip; /* warn, error ? */
3912 do_load_token (&ctx, read32 (ip + 1));
3917 case CEE_ADD_OVF_UN:
3919 case CEE_MUL_OVF_UN:
3921 case CEE_SUB_OVF_UN:
3922 if (!check_underflow (&ctx, 2))
3927 case CEE_ENDFINALLY:
3928 if (!is_correct_endfinally (ctx.header, ip_offset))
3929 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
3935 target = ip + (gint32)read32(ip + 1) + 5;
3936 if (target >= end || target < ctx.header->code)
3937 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3938 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3939 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3944 target = ip + (signed char)ip [1] + 2;
3945 if (target >= end || target < ctx.header->code)
3946 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3947 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3948 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3990 store_local (&ctx, read16 (ip + 1));
3995 do_cmp_op (&ctx, cmp_br_eq_op);
4003 do_cmp_op (&ctx, cmp_br_op);
4008 store_arg (&ctx, read16 (ip + 1) );
4014 check_overflow (&ctx);
4017 if (!check_overflow (&ctx))
4019 token = read32 (ip + 1);
4021 stack_top (&ctx)->stype = TYPE_PTR;
4025 if (!check_underflow (&ctx, 1))
4027 token = read32 (ip + 1);
4029 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
4030 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ip_offset));
4031 stack_top (&ctx)->stype = TYPE_PTR;
4039 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
4045 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
4050 if (ctx.eval.size != 1)
4051 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ip_offset));
4052 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
4053 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to localloc at 0x%04x", ip_offset));
4054 stack_top (&ctx)->stype = TYPE_COMPLEX;
4061 do_endfilter (&ctx);
4065 case CEE_UNALIGNED_:
4066 prefix |= PREFIX_UNALIGNED;
4070 prefix |= PREFIX_VOLATILE;
4074 prefix |= PREFIX_TAIL;
4076 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
4077 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
4080 if (!check_underflow (&ctx, 1))
4082 token = read32 (ip + 1);
4086 case CEE_CONSTRAINED_:
4087 token = read32 (ip + 1);
4091 if (!check_underflow (&ctx, 3))
4096 if (!check_underflow (&ctx, 3))
4104 if (!is_correct_rethrow (ctx.header, ip_offset))
4105 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
4113 if (!check_overflow (&ctx))
4115 token = read32 (ip + 1);
4117 stack_top (&ctx)->type = &mono_defaults.uint32_class->byval_arg;
4118 stack_top (&ctx)->stype = TYPE_I4;
4121 case CEE_REFANYTYPE:
4122 if (!check_underflow (&ctx, 1))
4136 * if ip != end we overflowed: mark as error.
4138 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
4139 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
4145 for (i = 0; i < ctx.header->code_size; ++i) {
4146 if (ctx.code [i].stack)
4147 g_free (ctx.code [i].stack);
4152 g_free (ctx.eval.stack);
4155 if (ctx.signature->hasthis)
4156 g_free (ctx.params);
4168 const FieldDesc *fields;
4171 static const FieldDesc
4172 typebuilder_fields[] = {
4173 {"tname", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, name)},
4174 {"nspace", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, nspace)},
4175 {"parent", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, parent)},
4176 {"interfaces", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, interfaces)},
4177 {"methods", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, methods)},
4178 {"properties", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, properties)},
4179 {"fields", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, fields)},
4180 {"attrs", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, attrs)},
4181 {"table_idx", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, table_idx)},
4185 static const FieldDesc
4186 modulebuilder_fields[] = {
4187 {"types", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, types)},
4188 {"cattrs", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, cattrs)},
4189 {"guid", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, guid)},
4190 {"table_idx", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, table_idx)},
4194 static const FieldDesc
4195 assemblybuilder_fields[] = {
4196 {"entry_point", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, entry_point)},
4197 {"modules", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, modules)},
4198 {"name", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, name)},
4199 {"resources", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, resources)},
4200 {"version", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, version)},
4201 {"culture", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, culture)},
4205 static const FieldDesc
4206 ctorbuilder_fields[] = {
4207 {"ilgen", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, ilgen)},
4208 {"parameters", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, parameters)},
4209 {"attrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, attrs)},
4210 {"iattrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, iattrs)},
4211 {"table_idx", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, table_idx)},
4212 {"call_conv", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, call_conv)},
4213 {"type", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, type)},
4217 static const FieldDesc
4218 methodbuilder_fields[] = {
4219 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, mhandle)},
4220 {"rtype", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, rtype)},
4221 {"parameters", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, parameters)},
4222 {"attrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, attrs)},
4223 {"iattrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, iattrs)},
4224 {"name", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, name)},
4225 {"table_idx", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, table_idx)},
4226 {"code", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, code)},
4227 {"ilgen", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, ilgen)},
4228 {"type", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, type)},
4229 {"pinfo", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, pinfo)},
4230 {"pi_dll", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dll)},
4231 {"pi_entry", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dllentry)},
4232 {"charset", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, charset)},
4233 {"extra_flags", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, extra_flags)},
4234 {"native_cc", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, native_cc)},
4235 {"call_conv", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, call_conv)},
4239 static const FieldDesc
4240 fieldbuilder_fields[] = {
4241 {"attrs", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, attrs)},
4242 {"type", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, type)},
4243 {"name", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, name)},
4244 {"def_value", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, def_value)},
4245 {"offset", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, offset)},
4246 {"table_idx", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, table_idx)},
4250 static const FieldDesc
4251 propertybuilder_fields[] = {
4252 {"attrs", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, attrs)},
4253 {"name", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, name)},
4254 {"type", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, type)},
4255 {"parameters", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, parameters)},
4256 {"def_value", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, def_value)},
4257 {"set_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, set_method)},
4258 {"get_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, get_method)},
4259 {"table_idx", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, table_idx)},
4263 static const FieldDesc
4264 ilgenerator_fields[] = {
4265 {"code", G_STRUCT_OFFSET (MonoReflectionILGen, code)},
4266 {"code_len", G_STRUCT_OFFSET (MonoReflectionILGen, code_len)},
4267 {"max_stack", G_STRUCT_OFFSET (MonoReflectionILGen, max_stack)},
4268 {"cur_stack", G_STRUCT_OFFSET (MonoReflectionILGen, cur_stack)},
4269 {"locals", G_STRUCT_OFFSET (MonoReflectionILGen, locals)},
4270 {"ex_handlers", G_STRUCT_OFFSET (MonoReflectionILGen, ex_handlers)},
4274 static const FieldDesc
4275 ilexinfo_fields[] = {
4276 {"handlers", G_STRUCT_OFFSET (MonoILExceptionInfo, handlers)},
4277 {"start", G_STRUCT_OFFSET (MonoILExceptionInfo, start)},
4278 {"len", G_STRUCT_OFFSET (MonoILExceptionInfo, len)},
4279 {"end", G_STRUCT_OFFSET (MonoILExceptionInfo, label)},
4283 static const FieldDesc
4284 ilexblock_fields[] = {
4285 {"extype", G_STRUCT_OFFSET (MonoILExceptionBlock, extype)},
4286 {"type", G_STRUCT_OFFSET (MonoILExceptionBlock, type)},
4287 {"start", G_STRUCT_OFFSET (MonoILExceptionBlock, start)},
4288 {"len", G_STRUCT_OFFSET (MonoILExceptionBlock, len)},
4289 {"filter_offset", G_STRUCT_OFFSET (MonoILExceptionBlock, filter_offset)},
4293 static const ClassDesc
4294 emit_classes_to_check [] = {
4295 {"TypeBuilder", typebuilder_fields},
4296 {"ModuleBuilder", modulebuilder_fields},
4297 {"AssemblyBuilder", assemblybuilder_fields},
4298 {"ConstructorBuilder", ctorbuilder_fields},
4299 {"MethodBuilder", methodbuilder_fields},
4300 {"FieldBuilder", fieldbuilder_fields},
4301 {"PropertyBuilder", propertybuilder_fields},
4302 {"ILGenerator", ilgenerator_fields},
4303 {"ILExceptionBlock", ilexblock_fields},
4304 {"ILExceptionInfo", ilexinfo_fields},
4308 static const FieldDesc
4309 monoevent_fields[] = {
4310 {"klass", G_STRUCT_OFFSET (MonoReflectionEvent, klass)},
4311 {"handle", G_STRUCT_OFFSET (MonoReflectionEvent, event)},
4315 static const FieldDesc
4316 monoproperty_fields[] = {
4317 {"klass", G_STRUCT_OFFSET (MonoReflectionProperty, klass)},
4318 {"prop", G_STRUCT_OFFSET (MonoReflectionProperty, property)},
4322 static const FieldDesc
4323 monofield_fields[] = {
4324 {"klass", G_STRUCT_OFFSET (MonoReflectionField, klass)},
4325 {"fhandle", G_STRUCT_OFFSET (MonoReflectionField, field)},
4329 static const FieldDesc
4330 monomethodinfo_fields[] = {
4331 {"parent", G_STRUCT_OFFSET (MonoMethodInfo, parent)},
4332 {"ret", G_STRUCT_OFFSET (MonoMethodInfo, ret)},
4333 {"attrs", G_STRUCT_OFFSET (MonoMethodInfo, attrs)},
4334 {"iattrs", G_STRUCT_OFFSET (MonoMethodInfo, implattrs)},
4338 static const FieldDesc
4339 monopropertyinfo_fields[] = {
4340 {"parent", G_STRUCT_OFFSET (MonoPropertyInfo, parent)},
4341 {"name", G_STRUCT_OFFSET (MonoPropertyInfo, name)},
4342 {"get_method", G_STRUCT_OFFSET (MonoPropertyInfo, get)},
4343 {"set_method", G_STRUCT_OFFSET (MonoPropertyInfo, set)},
4344 {"attrs", G_STRUCT_OFFSET (MonoPropertyInfo, attrs)},
4348 static const FieldDesc
4349 monomethod_fields[] = {
4350 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4354 static const FieldDesc
4355 monocmethod_fields[] = {
4356 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4360 static const FieldDesc
4362 {"ClassImpl", G_STRUCT_OFFSET (MonoReflectionParameter, ClassImpl)},
4363 {"DefaultValueImpl", G_STRUCT_OFFSET (MonoReflectionParameter, DefaultValueImpl)},
4364 {"MemberImpl", G_STRUCT_OFFSET (MonoReflectionParameter, MemberImpl)},
4365 {"NameImpl", G_STRUCT_OFFSET (MonoReflectionParameter, NameImpl)},
4366 {"PositionImpl", G_STRUCT_OFFSET (MonoReflectionParameter, PositionImpl)},
4367 {"AttrsImpl", G_STRUCT_OFFSET (MonoReflectionParameter, AttrsImpl)},
4371 static const ClassDesc
4372 reflection_classes_to_check [] = {
4373 {"MonoEvent", monoevent_fields},
4374 {"MonoProperty", monoproperty_fields},
4375 {"MonoField", monofield_fields},
4376 {"MonoMethodInfo", monomethodinfo_fields},
4377 {"MonoPropertyInfo", monopropertyinfo_fields},
4378 {"MonoMethod", monomethod_fields},
4379 {"MonoCMethod", monocmethod_fields},
4380 {"ParameterInfo", pinfo_fields},
4385 enuminfo_fields[] = {
4386 {"utype", G_STRUCT_OFFSET (MonoEnumInfo, utype)},
4387 {"values", G_STRUCT_OFFSET (MonoEnumInfo, values)},
4388 {"names", G_STRUCT_OFFSET (MonoEnumInfo, names)},
4393 delegate_fields[] = {
4394 {"target_type", G_STRUCT_OFFSET (MonoDelegate, target_type)},
4395 {"m_target", G_STRUCT_OFFSET (MonoDelegate, target)},
4396 {"method_name", G_STRUCT_OFFSET (MonoDelegate, method_name)},
4397 {"method_ptr", G_STRUCT_OFFSET (MonoDelegate, method_ptr)},
4398 {"delegate_trampoline", G_STRUCT_OFFSET (MonoDelegate, delegate_trampoline)},
4399 {"method_info", G_STRUCT_OFFSET (MonoDelegate, method_info)},
4404 multicast_delegate_fields[] = {
4405 {"prev", G_STRUCT_OFFSET (MonoMulticastDelegate, prev)},
4410 async_result_fields[] = {
4411 {"async_state", G_STRUCT_OFFSET (MonoAsyncResult, async_state)},
4412 {"handle", G_STRUCT_OFFSET (MonoAsyncResult, handle)},
4413 {"async_delegate", G_STRUCT_OFFSET (MonoAsyncResult, async_delegate)},
4414 {"data", G_STRUCT_OFFSET (MonoAsyncResult, data)},
4415 {"sync_completed", G_STRUCT_OFFSET (MonoAsyncResult, sync_completed)},
4416 {"completed", G_STRUCT_OFFSET (MonoAsyncResult, completed)},
4417 {"endinvoke_called", G_STRUCT_OFFSET (MonoAsyncResult, endinvoke_called)},
4418 {"async_callback", G_STRUCT_OFFSET (MonoAsyncResult, async_callback)},
4423 exception_fields[] = {
4424 {"trace_ips", G_STRUCT_OFFSET (MonoException, trace_ips)},
4425 {"inner_exception", G_STRUCT_OFFSET (MonoException, inner_ex)},
4426 {"message", G_STRUCT_OFFSET (MonoException, message)},
4427 {"help_link", G_STRUCT_OFFSET (MonoException, help_link)},
4428 {"class_name", G_STRUCT_OFFSET (MonoException, class_name)},
4429 {"stack_trace", G_STRUCT_OFFSET (MonoException, stack_trace)},
4430 {"remote_stack_trace", G_STRUCT_OFFSET (MonoException, remote_stack_trace)},
4431 {"remote_stack_index", G_STRUCT_OFFSET (MonoException, remote_stack_index)},
4432 {"hresult", G_STRUCT_OFFSET (MonoException, hresult)},
4433 {"source", G_STRUCT_OFFSET (MonoException, source)},
4437 static const ClassDesc
4438 system_classes_to_check [] = {
4439 {"Exception", exception_fields},
4440 {"MonoEnumInfo", enuminfo_fields},
4441 {"Delegate", delegate_fields},
4442 {"MulticastDelegate", multicast_delegate_fields},
4447 stack_frame_fields [] = {
4448 {"ilOffset", G_STRUCT_OFFSET (MonoStackFrame, il_offset)},
4449 {"nativeOffset", G_STRUCT_OFFSET (MonoStackFrame, native_offset)},
4450 {"methodBase", G_STRUCT_OFFSET (MonoStackFrame, method)},
4451 {"fileName", G_STRUCT_OFFSET (MonoStackFrame, filename)},
4452 {"lineNumber", G_STRUCT_OFFSET (MonoStackFrame, line)},
4453 {"columnNumber", G_STRUCT_OFFSET (MonoStackFrame, column)},
4457 static const ClassDesc
4458 system_diagnostics_classes_to_check [] = {
4459 {"StackFrame", stack_frame_fields},
4464 mono_method_message_fields[] = {
4465 {"method", G_STRUCT_OFFSET (MonoMethodMessage, method)},
4466 {"args", G_STRUCT_OFFSET (MonoMethodMessage, args)},
4467 {"names", G_STRUCT_OFFSET (MonoMethodMessage, names)},
4468 {"arg_types", G_STRUCT_OFFSET (MonoMethodMessage, arg_types)},
4469 {"ctx", G_STRUCT_OFFSET (MonoMethodMessage, ctx)},
4470 {"rval", G_STRUCT_OFFSET (MonoMethodMessage, rval)},
4471 {"exc", G_STRUCT_OFFSET (MonoMethodMessage, exc)},
4475 static const ClassDesc
4476 messaging_classes_to_check [] = {
4477 {"AsyncResult", async_result_fields},
4478 {"MonoMethodMessage", mono_method_message_fields},
4483 transparent_proxy_fields[] = {
4484 {"_rp", G_STRUCT_OFFSET (MonoTransparentProxy, rp)},
4485 {"_class", G_STRUCT_OFFSET (MonoTransparentProxy, remote_class)},
4490 real_proxy_fields[] = {
4491 {"class_to_proxy", G_STRUCT_OFFSET (MonoRealProxy, class_to_proxy)},
4495 static const ClassDesc
4496 proxy_classes_to_check [] = {
4497 {"TransparentProxy", transparent_proxy_fields},
4498 {"RealProxy", real_proxy_fields},
4503 wait_handle_fields[] = {
4504 {"os_handle", G_STRUCT_OFFSET (MonoWaitHandle, handle)},
4505 {"disposed", G_STRUCT_OFFSET (MonoWaitHandle, disposed)},
4511 {"system_thread_handle", G_STRUCT_OFFSET (MonoThread, handle)},
4512 {"threadpool_thread", G_STRUCT_OFFSET (MonoThread, threadpool_thread)},
4513 {"state", G_STRUCT_OFFSET (MonoThread, state)},
4514 {"abort_exc", G_STRUCT_OFFSET (MonoThread, abort_exc)},
4515 {"abort_state", G_STRUCT_OFFSET (MonoThread, abort_state)},
4516 {"thread_id", G_STRUCT_OFFSET (MonoThread, tid)},
4520 static const ClassDesc
4521 threading_classes_to_check [] = {
4522 {"Thread", thread_fields},
4523 {"WaitHandle", wait_handle_fields},
4527 static const FieldDesc
4529 {"datetime_format", G_STRUCT_OFFSET (MonoCultureInfo, datetime_format)},
4530 {"number_format", G_STRUCT_OFFSET (MonoCultureInfo, number_format)},
4531 {"textinfo", G_STRUCT_OFFSET (MonoCultureInfo, textinfo)},
4532 {"name", G_STRUCT_OFFSET (MonoCultureInfo, name)},
4533 {"displayname", G_STRUCT_OFFSET (MonoCultureInfo, displayname)},
4534 {"englishname", G_STRUCT_OFFSET (MonoCultureInfo, englishname)},
4535 {"nativename", G_STRUCT_OFFSET (MonoCultureInfo, nativename)},
4536 {"iso3lang", G_STRUCT_OFFSET (MonoCultureInfo, iso3lang)},
4537 {"iso2lang", G_STRUCT_OFFSET (MonoCultureInfo, iso2lang)},
4538 {"icu_name", G_STRUCT_OFFSET (MonoCultureInfo, icu_name)},
4539 {"win3lang", G_STRUCT_OFFSET (MonoCultureInfo, win3lang)},
4540 {"compareinfo", G_STRUCT_OFFSET (MonoCultureInfo, compareinfo)},
4544 static const FieldDesc
4545 dtfinfo_fields[] = {
4546 {"amDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, AMDesignator)},
4547 {"pmDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, PMDesignator)},
4548 {"dayNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, DayNames)},
4549 {"monthNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, MonthNames)},
4553 static const FieldDesc
4555 {"decimalFormats", G_STRUCT_OFFSET (MonoNumberFormatInfo, decimalFormats)},
4556 {"currencySymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, currencySymbol)},
4557 {"percentSymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, percentSymbol)},
4558 {"positiveSign", G_STRUCT_OFFSET (MonoNumberFormatInfo, positiveSign)},
4562 static const FieldDesc
4563 compinfo_fields[] = {
4564 {"lcid", G_STRUCT_OFFSET (MonoCompareInfo, lcid)},
4565 {"ICU_collator", G_STRUCT_OFFSET (MonoCompareInfo, ICU_collator)},
4569 static const FieldDesc
4570 sortkey_fields[] = {
4571 {"str", G_STRUCT_OFFSET (MonoSortKey, str)},
4572 {"options", G_STRUCT_OFFSET (MonoSortKey, options)},
4573 {"key", G_STRUCT_OFFSET (MonoSortKey, key)},
4574 {"lcid", G_STRUCT_OFFSET (MonoSortKey, lcid)},
4578 static const ClassDesc
4579 globalization_classes_to_check [] = {
4580 {"CultureInfo", cinfo_fields},
4581 {"DateTimeFormatInfo", dtfinfo_fields},
4582 {"NumberFormatInfo", nfinfo_fields},
4583 {"CompareInfo", compinfo_fields},
4584 {"SortKey", sortkey_fields},
4588 static const FieldDesc
4589 safe_handle_fields[] ={
4590 {"handle", G_STRUCT_OFFSET (MonoSafeHandle, handle)},
4594 static const ClassDesc interop_classes_to_check [] = {
4595 {"SafeHandle", safe_handle_fields},
4601 const ClassDesc *types;
4604 static const NameSpaceDesc
4605 namespaces_to_check[] = {
4606 {"System.Runtime.Remoting.Proxies", proxy_classes_to_check},
4607 {"System.Runtime.Remoting.Messaging", messaging_classes_to_check},
4608 {"System.Reflection.Emit", emit_classes_to_check},
4609 {"System.Reflection", reflection_classes_to_check},
4610 {"System.Threading", threading_classes_to_check},
4611 {"System.Diagnostics", system_diagnostics_classes_to_check},
4612 {"System", system_classes_to_check},
4613 {"System.Globalization", globalization_classes_to_check},
4614 {"System.Runtime.InteropServices", interop_classes_to_check},
4619 check_corlib (MonoImage *corlib)
4622 MonoClassField *field;
4623 const FieldDesc *fdesc;
4624 const ClassDesc *cdesc;
4625 const NameSpaceDesc *ndesc;
4627 GString *result = NULL;
4629 for (ndesc = namespaces_to_check; ndesc->name; ++ndesc) {
4630 for (cdesc = ndesc->types; cdesc->name; ++cdesc) {
4631 klass = mono_class_from_name (corlib, ndesc->name, cdesc->name);
4634 result = g_string_new ("");
4635 g_string_append_printf (result, "Cannot find class %s\n", cdesc->name);
4638 mono_class_init (klass);
4640 * FIXME: we should also check the size of valuetypes, or
4641 * we're going to have trouble when we access them in arrays.
4643 if (klass->valuetype)
4644 struct_offset = sizeof (MonoObject);
4647 for (fdesc = cdesc->fields; fdesc->name; ++fdesc) {
4648 field = mono_class_get_field_from_name (klass, fdesc->name);
4649 if (!field || (field->offset != (fdesc->offset + struct_offset))) {
4651 result = g_string_new ("");
4652 g_string_append_printf (result, "field `%s' mismatch in class %s (%ld + %ld != %ld)\n", fdesc->name, cdesc->name, (long) fdesc->offset, (long)struct_offset, (long) (field?field->offset:-1));
4658 return g_string_free (result, FALSE);
4663 mono_verify_corlib () {
4664 return check_corlib (mono_defaults.corlib);