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"
30 #define VERIFIER_DEBUG(code)
32 #define VERIFIER_DEBUG(code) do { code } while (0)
35 //////////////////////////////////////////////////////////////////
36 #define ADD_VERIFY_INFO(__ctx, __msg, __status) \
38 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
39 vinfo->status = __status; \
40 vinfo->message = ( __msg ); \
41 (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
44 #define ADD_VERIFY_ERROR(__ctx, __msg) \
46 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR); \
50 #define CODE_NOT_VERIFIABLE(__ctx, __msg) \
52 if ((__ctx)->verifiable) { \
53 ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE); \
54 (__ctx)->verifiable = 0; \
58 #define UNMASK_TYPE(type) ((type) & TYPE_MASK)
59 #define IS_MANAGED_POINTER(type) (((type) & POINTER_MASK) == POINTER_MASK)
62 IL_CODE_FLAG_NOT_PROCESSED = 0,
97 MonoMethodSignature *signature;
98 MonoMethodHeader *header;
100 MonoGenericContext *generic_context;
105 //////////////////////////////////////////////////////////////////
110 TYPE_INV = 0, /* leave at 0. */
115 /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
117 /* value types and classes */
119 /* Number of types, used to define the size of the tables*/
120 TYPE_MAX = 8, /* FIXME: This should probably be 7, but would require all the tables to be updated */
122 /* Used by tables to signal that a result is not verifiable*/
123 NON_VERIFIABLE_RESULT = 0x80,
125 /*Mask used to extract just the type, excluding flags */
128 /* The stack type is a managed pointer, unmask the value to res */
129 POINTER_MASK = 0x100,
131 /* Controlled Mutability Manager Pointer */
135 static const char* const
136 type_names [TYPE_MAX] = {
142 "TYPE_PTR", /* FIXME: Give an appropriate name */
147 PREFIX_UNALIGNED = 1,
150 PREFIX_ADDR_MASK = 3,
156 //////////////////////////////////////////////////////////////////
158 mono_free_verify_list (GSList *list)
160 MonoVerifyInfo *info;
163 for (tmp = list; tmp; tmp = tmp->next) {
165 g_free (info->message);
171 #define ADD_ERROR(list,msg) \
173 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
174 vinfo->status = MONO_VERIFY_ERROR; \
175 vinfo->message = (msg); \
176 (list) = g_slist_prepend ((list), vinfo); \
179 #define ADD_WARN(list,code,msg) \
181 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
182 vinfo->status = (code); \
183 vinfo->message = (msg); \
184 (list) = g_slist_prepend ((list), vinfo); \
188 valid_cultures[][9] = {
189 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
190 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
191 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
192 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
193 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
194 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
195 "da-DK", "de-DE", "de-CH", "de-AT",
196 "de-LU", "de-LI", "el-GR", "en-US",
197 "en-GB", "en-AU", "en-CA", "en-NZ",
198 "en-IE", "en-ZA", "en-JM", "en-CB",
199 "en-BZ", "en-TT", "en-ZW", "en-PH",
200 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
201 "es-CR", "es-PA", "es-DO", "es-VE",
202 "es-CO", "es-PE", "es-AR", "es-EC",
203 "es-CL", "es-UY", "es-PY", "es-BO",
204 "es-SV", "es-HN", "es-NI", "es-PR",
205 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
206 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
207 "hu-HU", "is-IS", "it-IT", "it-CH",
208 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
209 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
210 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
211 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
212 "sv-SE", "sv-FI", "th-TH", "tr-TR",
213 "ur-PK", "id-ID", "uk-UA", "be-BY",
214 "sl-SI", "et-EE", "lv-LV", "lt-LT",
215 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
217 "eu-ES", "mk-MK", "af-ZA",
218 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
219 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
220 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
221 "gu-IN", "ta-IN", "te-IN", "kn-IN",
222 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
223 "kok-IN", "syr-SY", "div-MV"
227 is_valid_culture (const char *cname)
233 for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
234 if (g_strcasecmp (valid_cultures [i], cname)) {
243 is_valid_assembly_flags (guint32 flags) {
244 /* Metadata: 22.1.2 */
245 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
246 return ((flags == 1) || (flags == 0));
250 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
253 const char *p, *blob_end;
255 if (blob_index >= image->heap_blob.size)
257 p = mono_metadata_blob_heap (image, blob_index);
258 size = mono_metadata_decode_blob_size (p, &blob_end);
259 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
261 if (notnull && !size)
267 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
269 const char *p, *blob_end, *res;
271 if (str_index >= image->heap_strings.size)
273 res = p = mono_metadata_string_heap (image, str_index);
274 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
278 * FIXME: should check it's a valid utf8 string, too.
280 while (p <= blob_end) {
285 return *p? NULL: res;
289 is_valid_cls_ident (const char *p)
292 * FIXME: we need the full unicode glib support for this.
293 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
294 * We do the lame thing for now.
300 if (!isalnum (*p) && *p != '_')
308 is_valid_filename (const char *p)
312 return strpbrk (p, "\\//:")? 0: 1;
316 verify_assembly_table (MonoImage *image, GSList *list, int level)
318 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
319 guint32 cols [MONO_ASSEMBLY_SIZE];
322 if (level & MONO_VERIFY_ERROR) {
324 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
325 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
327 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
328 case ASSEMBLY_HASH_NONE:
329 case ASSEMBLY_HASH_MD5:
330 case ASSEMBLY_HASH_SHA1:
333 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
336 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
337 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
339 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
340 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
342 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
343 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
345 if (strpbrk (p, ":\\/."))
346 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
349 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
350 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
352 if (!is_valid_culture (p))
353 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
360 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
362 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
363 guint32 cols [MONO_ASSEMBLYREF_SIZE];
367 if (level & MONO_VERIFY_ERROR) {
368 for (i = 0; i < t->rows; ++i) {
369 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
370 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
371 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
373 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
374 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
376 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
377 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
379 if (!is_valid_culture (p))
380 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
383 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
384 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
387 if (level & MONO_VERIFY_WARNING) {
388 /* check for duplicated rows */
389 for (i = 0; i < t->rows; ++i) {
396 verify_class_layout_table (MonoImage *image, GSList *list, int level)
398 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
399 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
400 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
403 if (level & MONO_VERIFY_ERROR) {
404 for (i = 0; i < t->rows; ++i) {
405 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
407 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
408 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
410 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
411 if (value & TYPE_ATTRIBUTE_INTERFACE)
412 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
413 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
414 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
415 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
416 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
417 case 0: case 1: case 2: case 4: case 8: case 16:
418 case 32: case 64: case 128: break;
420 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
422 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
424 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
425 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
426 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));
430 * FIXME: we need to check that if class size != 0,
431 * it needs to be greater than the class calculated size.
432 * If parent is a valuetype it also needs to be smaller than
433 * 1 MByte (0x100000 bytes).
434 * To do both these checks we need to load the referenced
435 * assemblies, though (the spec claims we didn't have to, bah).
438 * We need to check that the parent types have the same layout
449 verify_constant_table (MonoImage *image, GSList *list, int level)
451 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
452 guint32 cols [MONO_CONSTANT_SIZE];
454 GHashTable *dups = g_hash_table_new (NULL, NULL);
456 for (i = 0; i < t->rows; ++i) {
457 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
459 if (level & MONO_VERIFY_ERROR)
460 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
461 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
462 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
463 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
465 switch (cols [MONO_CONSTANT_TYPE]) {
466 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
470 if (level & MONO_VERIFY_CLS)
471 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));
472 case MONO_TYPE_BOOLEAN:
480 case MONO_TYPE_STRING:
481 case MONO_TYPE_CLASS:
484 if (level & MONO_VERIFY_ERROR)
485 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
487 if (level & MONO_VERIFY_ERROR) {
488 value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
489 switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
490 case MONO_HASCONSTANT_FIEDDEF:
491 if (value > image->tables [MONO_TABLE_FIELD].rows)
492 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
494 case MONO_HASCONSTANT_PARAM:
495 if (value > image->tables [MONO_TABLE_PARAM].rows)
496 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
498 case MONO_HASCONSTANT_PROPERTY:
499 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
500 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
503 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
507 if (level & MONO_VERIFY_CLS) {
509 * FIXME: verify types is consistent with the enum type
510 * is parent is an enum.
514 g_hash_table_destroy (dups);
519 verify_event_map_table (MonoImage *image, GSList *list, int level)
521 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
522 guint32 cols [MONO_EVENT_MAP_SIZE];
523 guint32 i, last_event;
524 GHashTable *dups = g_hash_table_new (NULL, NULL);
528 for (i = 0; i < t->rows; ++i) {
529 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
530 if (level & MONO_VERIFY_ERROR)
531 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
532 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
533 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
534 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
535 if (level & MONO_VERIFY_ERROR) {
536 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
537 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
538 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
539 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
541 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
542 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
543 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
547 g_hash_table_destroy (dups);
552 verify_event_table (MonoImage *image, GSList *list, int level)
554 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
555 guint32 cols [MONO_EVENT_SIZE];
559 for (i = 0; i < t->rows; ++i) {
560 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
562 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
563 if (level & MONO_VERIFY_ERROR)
564 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
566 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
567 if (level & MONO_VERIFY_ERROR)
568 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
570 if (level & MONO_VERIFY_CLS) {
571 if (!is_valid_cls_ident (p))
572 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
576 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
577 value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
578 switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
579 case MONO_TYPEDEFORREF_TYPEDEF:
580 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
581 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
583 case MONO_TYPEDEFORREF_TYPEREF:
584 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
585 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
587 case MONO_TYPEDEFORREF_TYPESPEC:
588 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
589 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
592 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
596 * FIXME: check that there is 1 add and remove row in methodsemantics
597 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
598 * these while checking methodsemantics).
599 * check for duplicated names for the same type [ERROR]
600 * check for CLS duplicate names for the same type [CLS]
607 verify_field_table (MonoImage *image, GSList *list, int level)
609 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
610 guint32 cols [MONO_FIELD_SIZE];
614 for (i = 0; i < t->rows; ++i) {
615 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
617 * Check this field has only one owner and that the owner is not
618 * an interface (done in verify_typedef_table() )
620 flags = cols [MONO_FIELD_FLAGS];
621 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
622 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
623 case FIELD_ATTRIBUTE_PRIVATE:
624 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
625 case FIELD_ATTRIBUTE_ASSEMBLY:
626 case FIELD_ATTRIBUTE_FAMILY:
627 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
628 case FIELD_ATTRIBUTE_PUBLIC:
631 if (level & MONO_VERIFY_ERROR)
632 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
635 if (level & MONO_VERIFY_ERROR) {
636 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
637 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
638 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
639 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
640 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
641 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
643 * FIXME: check there is only one owner in the respective table.
644 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
645 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
646 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
649 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
650 if (level & MONO_VERIFY_ERROR)
651 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
653 if (level & MONO_VERIFY_CLS) {
654 if (!is_valid_cls_ident (p))
655 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
660 * if owner is module needs to be static, access mask needs to be compilercontrolled,
661 * public or private (not allowed in cls mode).
662 * if owner is an enum ...
671 verify_file_table (MonoImage *image, GSList *list, int level)
673 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
674 guint32 cols [MONO_FILE_SIZE];
677 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
679 for (i = 0; i < t->rows; ++i) {
680 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
681 if (level & MONO_VERIFY_ERROR) {
682 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
683 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
684 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
685 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
687 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
688 if (level & MONO_VERIFY_ERROR)
689 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
691 if (level & MONO_VERIFY_ERROR) {
692 if (!is_valid_filename (p))
693 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
694 else if (g_hash_table_lookup (dups, p)) {
695 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
697 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
701 * FIXME: I don't understand what this means:
702 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
703 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
707 if (level & MONO_VERIFY_WARNING) {
708 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
709 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
711 g_hash_table_destroy (dups);
716 verify_moduleref_table (MonoImage *image, GSList *list, int level)
718 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
719 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
720 guint32 cols [MONO_MODULEREF_SIZE];
722 guint32 found, i, j, value;
723 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
725 for (i = 0; i < t->rows; ++i) {
726 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
727 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
728 if (level & MONO_VERIFY_ERROR)
729 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
731 if (level & MONO_VERIFY_ERROR) {
732 if (!is_valid_filename (p))
733 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
734 else if (g_hash_table_lookup (dups, p)) {
735 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
736 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
738 for (j = 0; j < tfile->rows; ++j) {
739 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
740 if ((pf = is_valid_string (image, value, TRUE)))
741 if (strcmp (p, pf) == 0) {
747 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
752 g_hash_table_destroy (dups);
757 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
759 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
760 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
764 for (i = 0; i < t->rows; ++i) {
765 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
766 if (level & MONO_VERIFY_ERROR) {
767 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
768 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
770 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
771 /* FIXME: check it's a valid locals or method sig.*/
779 mono_image_verify_tables (MonoImage *image, int level)
781 GSList *error_list = NULL;
783 error_list = verify_assembly_table (image, error_list, level);
785 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
786 * AssemblyRefProcessor should be ignored,
787 * though we may want to emit a warning, since it should not
788 * be present in a PE file.
790 error_list = verify_assemblyref_table (image, error_list, level);
791 error_list = verify_class_layout_table (image, error_list, level);
792 error_list = verify_constant_table (image, error_list, level);
794 * cutom attribute, declsecurity
796 error_list = verify_event_map_table (image, error_list, level);
797 error_list = verify_event_table (image, error_list, level);
798 error_list = verify_field_table (image, error_list, level);
799 error_list = verify_file_table (image, error_list, level);
800 error_list = verify_moduleref_table (image, error_list, level);
801 error_list = verify_standalonesig_table (image, error_list, level);
803 return g_slist_reverse (error_list);
808 bin_num_table [TYPE_MAX] [TYPE_MAX] = {
809 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
810 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_INV},
811 {TYPE_INV, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
812 {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_INV},
813 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV, TYPE_INV},
814 {TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_COMPLEX, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV},
815 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
816 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
821 TYPE_INV, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_INV, TYPE_INV, TYPE_INV
824 /* reduce the size of this table */
826 bin_int_table [TYPE_MAX] [TYPE_MAX] = {
827 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
828 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
829 {TYPE_INV, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
830 {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
831 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
832 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
833 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
834 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
838 bin_comp_table [TYPE_MAX] [TYPE_MAX] = {
840 {0, 1, 0, 1, 0, 0, 0, 0},
841 {0, 0, 1, 0, 0, 0, 0, 0},
842 {0, 1, 0, 1, 0, 2, 0, 0},
843 {0, 0, 0, 0, 1, 0, 0, 0},
844 {0, 0, 0, 2, 0, 1, 0, 0},
845 {0, 0, 0, 0, 0, 0, 3, 0},
846 {0, 0, 0, 0, 0, 0, 0, 0},
849 /* reduce the size of this table */
851 shift_table [TYPE_MAX] [TYPE_MAX] = {
852 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
853 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
854 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
855 {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
856 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
857 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
858 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
859 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
864 TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_R8, TYPE_COMPLEX
869 TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_R8, TYPE_COMPLEX
872 #define ADD_INVALID(list,msg) \
874 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
875 vinfo->status = MONO_VERIFY_ERROR; \
876 vinfo->message = (msg); \
877 (list) = g_slist_prepend ((list), vinfo); \
878 /*G_BREAKPOINT ();*/ \
882 #define CHECK_STACK_UNDERFLOW(num) \
884 if (cur_stack < (num)) \
885 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
888 #define CHECK_STACK_OVERFLOW() \
890 if (cur_stack >= max_stack) \
891 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
896 type_to_eval_stack_type (MonoType *type, ILStackDesc *stack, int take_addr) {
900 if (type->byref || take_addr) { /* fix double addr issue */
901 stack->stype = TYPE_COMPLEX;
909 case MONO_TYPE_BOOLEAN:
915 stack->stype = TYPE_I4;
920 stack->stype = TYPE_PTR;
922 case MONO_TYPE_CLASS:
923 case MONO_TYPE_STRING:
924 case MONO_TYPE_OBJECT:
925 case MONO_TYPE_SZARRAY:
926 case MONO_TYPE_ARRAY:
927 stack->stype = TYPE_COMPLEX;
931 stack->stype = TYPE_I8;
935 stack->stype = TYPE_R8;
937 case MONO_TYPE_VALUETYPE:
938 if (type->data.klass->enumtype) {
939 t = type->data.klass->enum_basetype->type;
942 stack->stype = TYPE_COMPLEX;
946 g_error ("unknown type %02x in eval stack type", type->type);
952 type_from_op (int ins, ILStackDesc *arg) {
960 /* FIXME: check unverifiable args for TYPE_COMPLEX */
961 return arg->stype = bin_num_table [arg->stype] [arg [1].stype];
967 return arg->stype = bin_int_table [arg->stype] [arg [1].stype];
971 return arg->stype = shift_table [arg->stype] [arg [1].stype];
992 /* FIXME: handle some specifics with ins->next->type */
993 return bin_comp_table [arg->stype] [arg [1].stype] ? TYPE_I4: TYPE_INV;
999 return arg->stype = bin_comp_table [arg->stype] [arg [1].stype] ? TYPE_I4: TYPE_INV;
1002 return arg->stype = neg_table [arg->stype];
1004 if (arg->stype >= TYPE_I4 && arg->stype <= TYPE_PTR)
1007 return arg->stype = TYPE_INV;
1014 case CEE_CONV_OVF_I1:
1015 case CEE_CONV_OVF_U1:
1016 case CEE_CONV_OVF_I2:
1017 case CEE_CONV_OVF_U2:
1018 case CEE_CONV_OVF_I4:
1019 case CEE_CONV_OVF_U4:
1020 case CEE_CONV_OVF_I1_UN:
1021 case CEE_CONV_OVF_U1_UN:
1022 case CEE_CONV_OVF_I2_UN:
1023 case CEE_CONV_OVF_U2_UN:
1024 case CEE_CONV_OVF_I4_UN:
1025 case CEE_CONV_OVF_U4_UN:
1026 if (arg->stype == TYPE_INV || arg->stype >= TYPE_COMPLEX)
1027 return arg->stype = TYPE_INV;
1028 return arg->stype = TYPE_I4;
1031 case CEE_CONV_OVF_I:
1032 case CEE_CONV_OVF_U:
1033 case CEE_CONV_OVF_I_UN:
1034 case CEE_CONV_OVF_U_UN:
1035 if (arg->stype == TYPE_INV || arg->stype == TYPE_COMPLEX)
1036 return arg->stype = TYPE_INV;
1037 return arg->stype = TYPE_PTR;
1040 case CEE_CONV_OVF_I8:
1041 case CEE_CONV_OVF_U8:
1042 case CEE_CONV_OVF_I8_UN:
1043 case CEE_CONV_OVF_U8_UN:
1044 return arg->stype = TYPE_I8;
1047 return arg->stype = TYPE_R8;
1049 g_error ("opcode 0x%04x not handled in type from op", ins);
1056 in_any_block (MonoMethodHeader *header, guint offset)
1059 MonoExceptionClause *clause;
1061 for (i = 0; i < header->num_clauses; ++i) {
1062 clause = &header->clauses [i];
1063 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
1065 if (MONO_OFFSET_IN_HANDLER (clause, offset))
1067 /* need to check filter ... */
1073 in_same_block (MonoMethodHeader *header, guint offset, guint target)
1076 MonoExceptionClause *clause;
1078 for (i = 0; i < header->num_clauses; ++i) {
1079 clause = &header->clauses [i];
1080 if (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target))
1082 if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
1084 /* need to check filter ... */
1090 * A leave can't escape a finally block
1093 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1096 MonoExceptionClause *clause;
1098 for (i = 0; i < header->num_clauses; ++i) {
1099 clause = &header->clauses [i];
1100 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1102 /* need to check filter ... */
1108 can_merge_stack (ILCodeDesc *a, ILCodeDesc *b)
1110 if (!b->flags & IL_CODE_FLAG_SEEN) {
1111 b->flags |= IL_CODE_FLAG_SEEN;
1116 if (a->size != b->size)
1123 is_valid_bool_arg (ILStackDesc *arg)
1125 if (IS_MANAGED_POINTER (arg->stype))
1127 switch (arg->stype) {
1130 case TYPE_NATIVE_INT:
1134 g_assert (arg->type);
1135 switch (arg->type->type) {
1136 case MONO_TYPE_CLASS:
1137 case MONO_TYPE_STRING:
1138 case MONO_TYPE_OBJECT:
1139 case MONO_TYPE_SZARRAY:
1140 case MONO_TYPE_ARRAY:
1141 case MONO_TYPE_FNPTR:
1144 case MONO_TYPE_GENERICINST:
1145 /*We need to check if the container class
1146 * of the generic type is a valuetype, iow:
1147 * is it a "class Foo<T>" or a "struct Foo<T>"?
1149 return !arg->type->data.generic_class->container_class->valuetype;
1158 can_store_type (ILStackDesc *arg, MonoType *type)
1161 if (type->byref && arg->stype != TYPE_COMPLEX)
1165 case MONO_TYPE_VOID:
1169 case MONO_TYPE_BOOLEAN:
1172 case MONO_TYPE_CHAR:
1175 if (arg->stype == TYPE_I4 || arg->stype == TYPE_PTR)
1182 case MONO_TYPE_CLASS:
1183 case MONO_TYPE_STRING:
1184 case MONO_TYPE_OBJECT:
1185 case MONO_TYPE_SZARRAY:
1186 case MONO_TYPE_ARRAY:
1187 return TRUE; /* FIXME */
1190 if (arg->stype == TYPE_I8)
1195 if (arg->stype == TYPE_R8)
1198 case MONO_TYPE_VALUETYPE:
1199 if (type->data.klass->enumtype) {
1200 t = type->data.klass->enum_basetype->type;
1203 if (arg->type->data.klass != type->data.klass)
1208 g_error ("unknown type %02x in store type", type->type);
1214 stind_type (int op, int type) {
1217 return type == TYPE_COMPLEX;
1221 return type == TYPE_I4;
1223 return type == TYPE_I8;
1226 return type == TYPE_R8;
1228 g_assert_not_reached ();
1234 /*Type manipulation helper*/
1236 /*Returns the byref version of the supplied MonoType*/
1238 mono_type_get_type_byref (MonoType *type)
1240 return &mono_class_from_mono_type (type)->this_arg;
1244 /*Returns the byval version of the supplied MonoType*/
1246 mono_type_get_type_byval (MonoType *type)
1248 return &mono_class_from_mono_type (type)->byval_arg;
1252 /*Stack manipulation code*/
1255 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1258 if (!state->stack) {
1259 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1264 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1266 to->size = from->size;
1267 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1271 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1273 to->stype = from->stype;
1274 to->type = from->type;
1278 check_underflow (VerifyContext *ctx, int size)
1280 if (ctx->eval.size < size) {
1281 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1288 check_overflow (VerifyContext *ctx)
1290 if (ctx->eval.size >= ctx->max_stack) {
1291 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1298 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1300 if (value->stype == TYPE_PTR) {
1301 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1308 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1310 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1311 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1314 if (type->type == MONO_TYPE_TYPEDBYREF) {
1315 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("TypedByRef is not a verifiable type at 0x%04x", ctx->ip_offset));
1322 static ILStackDesc *
1323 stack_push (VerifyContext *ctx)
1325 return & ctx->eval.stack [ctx->eval.size++];
1328 static ILStackDesc *
1329 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1331 ILStackDesc *top = stack_push (ctx);
1337 static ILStackDesc *
1338 stack_pop (VerifyContext *ctx)
1340 return ctx->eval.stack + --ctx->eval.size;
1343 static inline ILStackDesc *
1344 stack_top (VerifyContext *ctx)
1346 return ctx->eval.stack + (ctx->eval.size - 1);
1349 static inline ILStackDesc *
1350 stack_get (VerifyContext *ctx, int distance)
1352 return ctx->eval.stack + (ctx->eval.size - distance - 1);
1355 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1357 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1360 get_boxable_mono_type (VerifyContext* ctx, int token)
1362 MonoType *type = mono_type_get_full (ctx->image, token, ctx->generic_context);
1365 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
1369 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1370 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
1374 check_unverifiable_type (ctx, type);
1379 /*operation result tables */
1381 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1382 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1383 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1384 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1385 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1386 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1387 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1390 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1391 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1392 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1393 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1394 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1395 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1396 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1399 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1400 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1401 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1402 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1403 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1404 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1405 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1408 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1409 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1410 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1411 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1412 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1413 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1414 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1417 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1418 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1419 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1420 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1421 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1422 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1423 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1426 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1427 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1428 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1429 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1430 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1431 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1432 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1435 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1436 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1437 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1438 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1439 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1440 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1441 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1446 dump_stack_value (ILStackDesc *value)
1448 printf ("[(%d)(%d)", value->type->type, value->stype);
1450 if (value->stype & CMMP_MASK)
1451 printf ("Controled Mutability MP: ");
1453 if (IS_MANAGED_POINTER (value->stype))
1454 printf ("Managed Pointer to: ");
1456 switch (UNMASK_TYPE (value->stype)) {
1458 printf ("invalid type]");
1466 case TYPE_NATIVE_INT:
1467 printf ("native int]");
1470 printf ("float64]");
1473 printf ("unmanaged pointer]");
1476 switch (value->type->type) {
1477 case MONO_TYPE_CLASS:
1478 case MONO_TYPE_VALUETYPE:
1479 printf ("complex] (%s)", value->type->data.klass->name);
1481 case MONO_TYPE_STRING:
1482 printf ("complex] (string)");
1484 case MONO_TYPE_OBJECT:
1485 printf ("complex] (object)");
1487 case MONO_TYPE_SZARRAY:
1488 printf ("complex] (%s [])", value->type->data.klass->name);
1490 case MONO_TYPE_ARRAY:
1491 printf ("complex] (%s [%d %d %d])",
1492 value->type->data.array->eklass->name,
1493 value->type->data.array->rank,
1494 value->type->data.array->numsizes,
1495 value->type->data.array->numlobounds);
1497 case MONO_TYPE_GENERICINST:
1498 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1501 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1503 case MONO_TYPE_MVAR:
1504 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1507 printf ("unknown complex %d type]\n", value->type->type);
1508 g_assert_not_reached ();
1511 printf ("unknown stack %d type]\n", value->stype);
1512 g_assert_not_reached ();
1517 dump_stack_state (ILCodeDesc *state)
1521 printf ("(%d) ", state->size);
1522 for (i = 0; i < state->size; ++i)
1523 dump_stack_value (state->stack + i);
1528 dump_context (VerifyContext *ctx, int code_size)
1532 for (i = 0; i < code_size; ++i) {
1533 if (ctx->code [i].flags & IL_CODE_FLAG_SEEN) {
1534 printf ("opcode [%d]:\n\t", i);
1535 dump_stack_state (&ctx->code [i]);
1540 /*Returns TRUE if candidate array type can be assigned to target.
1541 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1544 is_array_type_compatible (MonoType *target, MonoType *candidate)
1547 MonoArrayType *left = target->data.array;
1548 MonoArrayType *right = candidate->data.array;
1550 g_assert (target->type == MONO_TYPE_ARRAY);
1551 g_assert (candidate->type == MONO_TYPE_ARRAY);
1554 if ((left->rank != right->rank) ||
1555 (left->numsizes != right->numsizes) ||
1556 (left->numlobounds != right->numlobounds))
1559 for (i = 0; i < left->numsizes; ++i)
1560 if (left->sizes [i] != right->sizes [i])
1563 for (i = 0; i < left->numlobounds; ++i)
1564 if (left->lobounds [i] != right->lobounds [i])
1567 return mono_class_is_assignable_from (left->eklass, right->eklass);
1571 get_stack_type (MonoType *type)
1574 int type_kind = type->type;
1576 mask = POINTER_MASK;
1577 /*TODO handle CMMP_MASK */
1580 switch (type_kind) {
1583 case MONO_TYPE_BOOLEAN:
1586 case MONO_TYPE_CHAR:
1589 return TYPE_I4 | mask;
1593 return TYPE_NATIVE_INT | mask;
1595 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1596 case MONO_TYPE_FNPTR:
1598 case MONO_TYPE_TYPEDBYREF:
1599 return TYPE_PTR | mask;
1601 case MONO_TYPE_CLASS:
1602 case MONO_TYPE_STRING:
1603 case MONO_TYPE_OBJECT:
1604 case MONO_TYPE_SZARRAY:
1605 case MONO_TYPE_ARRAY:
1606 case MONO_TYPE_GENERICINST:
1607 return TYPE_COMPLEX | mask;
1611 return TYPE_I8 | mask;
1615 return TYPE_R8 | mask;
1617 case MONO_TYPE_VALUETYPE:
1618 if (type->data.klass->enumtype) {
1619 type = type->data.klass->enum_basetype;
1620 type_kind = type->type;
1623 return TYPE_COMPLEX | mask;
1626 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
1627 g_assert_not_reached ();
1632 /* convert MonoType to ILStackDesc format (stype) */
1634 set_stack_value (ILStackDesc *stack, MonoType *type, int take_addr)
1637 int type_kind = type->type;
1639 if (type->byref || take_addr)
1640 mask = POINTER_MASK;
1641 /* TODO handle CMMP_MASK */
1646 switch (type_kind) {
1649 case MONO_TYPE_BOOLEAN:
1652 case MONO_TYPE_CHAR:
1655 stack->stype = TYPE_I4 | mask;
1659 stack->stype = TYPE_NATIVE_INT | mask;
1662 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1663 case MONO_TYPE_FNPTR:
1665 case MONO_TYPE_TYPEDBYREF:
1666 stack->stype = TYPE_PTR | mask;
1669 case MONO_TYPE_CLASS:
1670 case MONO_TYPE_STRING:
1671 case MONO_TYPE_OBJECT:
1672 case MONO_TYPE_SZARRAY:
1673 case MONO_TYPE_ARRAY:
1675 case MONO_TYPE_GENERICINST:
1677 case MONO_TYPE_MVAR:
1678 stack->stype = TYPE_COMPLEX | mask;
1682 stack->stype = TYPE_I8 | mask;
1686 stack->stype = TYPE_R8 | mask;
1688 case MONO_TYPE_VALUETYPE:
1689 if (type->data.klass->enumtype) {
1690 type = type->data.klass->enum_basetype;
1691 type_kind = type->type;
1694 stack->stype = TYPE_COMPLEX | mask;
1698 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1699 g_assert_not_reached ();
1704 /* Generics validation stuff, should be moved to another metadata/? file */
1706 mono_is_generic_type_compatible (MonoType *target, MonoType *candidate)
1708 if (target->byref != candidate->byref)
1712 switch (target->type) {
1713 case MONO_TYPE_STRING:
1714 if (candidate->type == MONO_TYPE_STRING)
1718 case MONO_TYPE_CLASS:
1719 if (candidate->type != MONO_TYPE_CLASS)
1722 VERIFIER_DEBUG ( printf ("verifying type class %p %p\n", target->data.klass, candidate->data.klass); );
1723 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1725 case MONO_TYPE_OBJECT:
1726 return MONO_TYPE_IS_REFERENCE (candidate);
1728 case MONO_TYPE_SZARRAY:
1729 if (candidate->type != MONO_TYPE_SZARRAY)
1731 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1733 case MONO_TYPE_VALUETYPE:
1734 if (target->data.klass->enumtype) {
1735 target = target->data.klass->enum_basetype;
1738 if (candidate->type != MONO_TYPE_VALUETYPE)
1740 return candidate->data.klass == target->data.klass;
1743 case MONO_TYPE_ARRAY:
1744 if (candidate->type != MONO_TYPE_ARRAY)
1746 return is_array_type_compatible (target, candidate);
1749 VERIFIER_DEBUG ( printf ("unknown target type %d\n", target->type); );
1750 g_assert_not_reached ();
1758 mono_is_generic_instance_compatible (MonoGenericClass *target, MonoGenericClass *candidate, MonoGenericClass *root_candidate) {
1759 MonoGenericContainer *container;
1762 VERIFIER_DEBUG ( printf ("candidate container %p\n", candidate->container_class->generic_container); );
1763 if (target->container_class != candidate->container_class) {
1764 MonoType *param_class;
1765 MonoClass *cand_class;
1766 VERIFIER_DEBUG ( printf ("generic class != target\n"); );
1767 param_class = candidate->context.class_inst->type_argv [0];
1768 VERIFIER_DEBUG ( printf ("param 0 %d\n", param_class->type); );
1769 cand_class = candidate->container_class;
1771 /* We must check if it's an interface type*/
1772 if (MONO_CLASS_IS_INTERFACE (target->container_class)) {
1773 VERIFIER_DEBUG ( printf ("generic type is an interface\n"); );
1776 int iface_count = cand_class->interface_count;
1777 MonoClass **ifaces = cand_class->interfaces;
1779 VERIFIER_DEBUG ( printf ("type has %d interfaces\n", iface_count); );
1780 for (i = 0; i< iface_count; ++i) {
1781 MonoClass *ifc = ifaces[i];
1782 VERIFIER_DEBUG ( printf ("analysing %s\n", ifc->name); );
1783 if (ifc->generic_class) {
1784 VERIFIER_DEBUG ( printf ("interface has generic info\n"); );
1786 if (mono_is_generic_instance_compatible (target, ifc->generic_class, root_candidate)) {
1787 VERIFIER_DEBUG ( printf ("we got compatible stuff!\n"); );
1792 cand_class = cand_class->parent;
1793 } while (cand_class);
1795 VERIFIER_DEBUG ( printf ("don't implements an interface\n"); );
1798 VERIFIER_DEBUG ( printf ("verifying upper classes\n"); );
1800 cand_class = cand_class->parent;
1802 while (cand_class) {
1803 VERIFIER_DEBUG ( printf ("verifying parent class name %s\n", cand_class->name); );
1804 if (cand_class->generic_class) {
1805 VERIFIER_DEBUG ( printf ("super type has generic context\n"); );
1807 /* TODO break loop if target->container_class == cand_class->generic_class->container_class */
1808 return mono_is_generic_instance_compatible (target, cand_class->generic_class, root_candidate);
1810 VERIFIER_DEBUG ( printf ("super class has no generic context\n"); );
1811 cand_class = cand_class->parent;
1817 /* now we verify if the instantiations are compatible*/
1818 if (target->context.class_inst == candidate->context.class_inst) {
1819 VERIFIER_DEBUG ( printf ("generic types are compatible, both have the same instantiation\n"); );
1823 if (target->context.class_inst->type_argc != candidate->context.class_inst->type_argc) {
1824 VERIFIER_DEBUG ( printf ("generic instantiations with diferent arg counts\n"); );
1828 //verify if open instance -- none should be
1830 container = target->container_class->generic_container;
1832 for (i = 0; i < container->type_argc; ++i) {
1833 MonoGenericParam *param = container->type_params + i;
1834 MonoType *target_type = target->context.class_inst->type_argv [i];
1835 MonoType *candidate_type = candidate->context.class_inst->type_argv [i];
1836 /* We resolve TYPE_VAR types before proceeding */
1838 if (candidate_type->type == MONO_TYPE_VAR) {
1839 MonoGenericParam *var_param = candidate_type->data.generic_param;
1840 candidate_type = root_candidate->context.class_inst->type_argv [var_param->num];
1843 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK) == 0) {
1844 VERIFIER_DEBUG ( printf ("generic type have no variance flag, checking each type %d %d \n",target_type->type, candidate_type->type); );
1846 if (!mono_metadata_type_equal (target_type, candidate_type))
1849 VERIFIER_DEBUG ( printf ("generic type has variance flag, need to perform deeper check\n"); );
1850 /* first we check if they are the same kind */
1851 /* byref generic params are forbiden, but better safe than sorry.*/
1853 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) == GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) {
1854 if (!mono_is_generic_type_compatible (target_type, candidate_type))
1856 /* the attribute must be contravariant */
1857 } else if (!mono_is_generic_type_compatible (candidate_type, target_type))
1866 /*Verify if type 'candidate' can be stored in type 'target'.
1868 * If strict, check for the underlying type and not the verification stack types
1871 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict) {
1872 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1873 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1875 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); );
1877 /*only one is byref */
1878 if (candidate->byref ^ target->byref) {
1879 /* converting from native int to byref*/
1880 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1881 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1888 switch (target->type) {
1891 case MONO_TYPE_BOOLEAN:
1893 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1896 case MONO_TYPE_CHAR:
1898 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1899 return get_stack_type (candidate) == TYPE_I4;
1902 case MONO_TYPE_U4: {
1903 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1904 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1906 return is_native_int || is_int4;
1907 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1912 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
1917 return candidate->type == target->type;
1918 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
1922 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1923 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1924 return is_native_int || is_int4;
1928 if (candidate->type != MONO_TYPE_PTR)
1930 /* check the underlying type */
1931 return verify_stack_type_compatibility (ctx, target->data.type, candidate->data.type, TRUE);
1933 case MONO_TYPE_FNPTR: {
1934 MonoMethodSignature *left, *right;
1935 if (candidate->type != MONO_TYPE_FNPTR)
1938 left = mono_type_get_signature (target);
1939 right = mono_type_get_signature (candidate);
1940 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
1943 case MONO_TYPE_GENERICINST: {
1944 MonoGenericClass *left;
1945 MonoGenericClass *right;
1946 if (candidate->type != MONO_TYPE_GENERICINST)
1948 left = target->data.generic_class;
1949 right = candidate->data.generic_class;
1951 return mono_is_generic_instance_compatible (left, right, right);
1954 case MONO_TYPE_STRING:
1955 return candidate->type == MONO_TYPE_STRING;
1957 case MONO_TYPE_CLASS:
1958 if (candidate->type != MONO_TYPE_CLASS)
1960 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1962 case MONO_TYPE_OBJECT:
1963 return MONO_TYPE_IS_REFERENCE (candidate);
1965 case MONO_TYPE_SZARRAY: {
1968 if (candidate->type != MONO_TYPE_SZARRAY)
1971 left = target->data.klass;
1972 right = candidate->data.klass;
1973 return mono_class_is_assignable_from(left, right);
1976 case MONO_TYPE_ARRAY:
1977 if (candidate->type != MONO_TYPE_ARRAY)
1979 return is_array_type_compatible (target, candidate);
1981 //TODO verify aditional checks that needs to be done
1982 case MONO_TYPE_TYPEDBYREF:
1983 return candidate->type == MONO_TYPE_TYPEDBYREF;
1985 case MONO_TYPE_VALUETYPE:
1986 if (target->data.klass->enumtype) {
1987 target = target->data.klass->enum_basetype;
1990 if (candidate->type != MONO_TYPE_VALUETYPE)
1992 return target->data.klass == candidate->data.klass;
1996 if (candidate->type != MONO_TYPE_VAR)
1998 return candidate->data.generic_param->num == target->data.generic_param->num;
2000 case MONO_TYPE_MVAR:
2001 if (candidate->type != MONO_TYPE_MVAR)
2003 return candidate->data.generic_param->num == target->data.generic_param->num;
2006 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2007 g_assert_not_reached ();
2016 verify_type_compat (VerifyContext *ctx, MonoType *type, ILStackDesc *stack) {
2017 int stack_type = stack->stype;
2018 VERIFIER_DEBUG ( printf ("checking compatibility %p %p[%d] %p[%d]\n", ctx, type, type->type, stack, stack_type ); );
2020 /*only one is byref */
2021 if (type->byref ^ IS_MANAGED_POINTER (stack_type)) {
2022 /* converting from native int to byref*/
2023 if (type->byref && stack_type == TYPE_NATIVE_INT) {
2024 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
2030 return verify_stack_type_compatibility (ctx, type, mono_type_get_type_byref (stack->type), TRUE);
2033 switch (type->type) {
2036 case MONO_TYPE_BOOLEAN:
2039 case MONO_TYPE_CHAR:
2042 return stack_type == TYPE_I4;
2046 return stack_type == TYPE_I8;
2050 return stack_type == TYPE_R8;
2054 return stack_type == TYPE_NATIVE_INT || stack_type == TYPE_I4;
2057 if (stack_type != TYPE_PTR || stack->type->type != MONO_TYPE_PTR)
2059 return verify_stack_type_compatibility (ctx, type->data.type, stack->type->data.type, TRUE);
2061 case MONO_TYPE_FNPTR: {
2062 MonoMethodSignature *left, *right;
2063 if (stack_type != TYPE_PTR || stack->type->type != MONO_TYPE_FNPTR)
2066 left = mono_type_get_signature (type);
2067 right = mono_type_get_signature (stack->type);
2068 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
2071 case MONO_TYPE_GENERICINST: {
2072 MonoGenericClass *left;
2073 MonoGenericClass *right;
2074 if (stack_type != TYPE_COMPLEX)
2076 g_assert (stack->type);
2077 if (stack->type->type != MONO_TYPE_GENERICINST)
2079 left = type->data.generic_class;
2080 right = stack->type->data.generic_class;
2082 return mono_is_generic_instance_compatible (left, right, right);
2085 case MONO_TYPE_STRING:
2086 if (stack_type != TYPE_COMPLEX)
2088 g_assert (stack->type);
2089 return stack->type->type == MONO_TYPE_STRING;
2091 case MONO_TYPE_CLASS:
2092 if (stack_type != TYPE_COMPLEX)
2094 g_assert (stack->type);
2095 if (stack->type->type != MONO_TYPE_CLASS)
2098 return mono_class_is_assignable_from (type->data.klass, stack->type->data.klass);
2100 case MONO_TYPE_OBJECT:
2101 if (stack_type != TYPE_COMPLEX)
2103 g_assert (stack->type);
2104 return MONO_TYPE_IS_REFERENCE (stack->type);
2106 case MONO_TYPE_SZARRAY: {
2109 if (stack_type != TYPE_COMPLEX)
2112 g_assert (stack->type);
2114 if (stack->type->type != type->type)
2116 left = type->data.klass ;
2117 right = stack->type->data.klass;
2118 return mono_class_is_assignable_from (left, right);
2121 case MONO_TYPE_ARRAY:
2122 if (stack_type != TYPE_COMPLEX)
2124 g_assert (stack->type);
2125 if (stack->type->type != MONO_TYPE_ARRAY)
2127 return is_array_type_compatible (type, stack->type);
2129 /*TODO verify aditional checks that needs to be done */
2130 case MONO_TYPE_TYPEDBYREF:
2131 if (stack_type != TYPE_PTR)
2133 g_assert (stack->type);
2134 if (stack->type->type != MONO_TYPE_TYPEDBYREF)
2138 case MONO_TYPE_VALUETYPE:
2139 if (type->data.klass->enumtype) {
2140 type = type->data.klass->enum_basetype;
2143 if (stack_type != TYPE_COMPLEX)
2145 g_assert (stack->type);
2146 if (stack->type->type != MONO_TYPE_VALUETYPE)
2148 return stack->type->data.klass == type->data.klass;
2152 if (stack_type != TYPE_COMPLEX)
2154 g_assert (stack->type);
2155 if (stack->type->type != MONO_TYPE_VAR)
2157 return stack->type->data.generic_param->num == type->data.generic_param->num;
2159 case MONO_TYPE_MVAR:
2160 if (stack_type != TYPE_COMPLEX)
2162 g_assert (stack->type);
2163 if (stack->type->type != MONO_TYPE_MVAR)
2165 return stack->type->data.generic_param->num == type->data.generic_param->num;
2168 printf("unknown store type %d\n", type->type);
2169 g_assert_not_reached ();
2175 /* implement the opcode checks*/
2177 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2179 if (arg >= ctx->max_args) {
2181 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2183 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2184 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2185 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2187 } else if (check_overflow (ctx)) {
2188 /*We must let the value be pushed, otherwise we would get an underflow error*/
2189 check_unverifiable_type (ctx, ctx->params [arg]);
2190 if (ctx->params [arg]->byref && take_addr)
2191 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2192 set_stack_value (stack_push (ctx), ctx->params [arg], take_addr);
2197 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2199 if (arg >= ctx->num_locals) {
2200 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2201 } else if (check_overflow (ctx)) {
2202 /*We must let the value be pushed, otherwise we would get an underflow error*/
2203 check_unverifiable_type (ctx, ctx->locals [arg]);
2204 if (ctx->locals [arg]->byref && take_addr)
2205 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2207 set_stack_value (stack_push (ctx), ctx->locals [arg], take_addr);
2212 store_arg (VerifyContext *ctx, guint32 arg)
2216 if (arg >= ctx->max_args) {
2217 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2218 check_underflow (ctx, 1);
2223 if (check_underflow (ctx, 1)) {
2224 value = stack_pop (ctx);
2225 if (!verify_type_compat (ctx, ctx->params [arg], value)) {
2226 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));
2232 store_local (VerifyContext *ctx, guint32 arg)
2235 if (arg >= ctx->num_locals) {
2236 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2240 /*TODO verify definite assigment */
2241 if (check_underflow (ctx, 1)) {
2242 value = stack_pop(ctx);
2243 if (!verify_type_compat (ctx, ctx->locals [arg], value)) {
2244 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));
2250 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2253 int idxa, idxb, complexMerge = 0;
2256 if (!check_underflow (ctx, 2))
2258 a = stack_get (ctx, 1);
2259 b = stack_top (ctx);
2262 if (IS_MANAGED_POINTER (idxa)) {
2268 if (IS_MANAGED_POINTER (idxb)) {
2275 res = table [idxa][idxb];
2277 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2278 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2281 if (res == TYPE_INV) {
2282 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)]));
2286 if (res & NON_VERIFIABLE_RESULT) {
2287 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)",
2288 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)]));
2290 res = res & ~NON_VERIFIABLE_RESULT;
2293 if (complexMerge && res == TYPE_PTR) {
2294 if (complexMerge == 1)
2295 copy_stack_value (stack_top (ctx), a);
2296 else if (complexMerge == 2)
2297 copy_stack_value (stack_top (ctx), b);
2299 * There is no need to merge the type of two pointers.
2300 * The only valid operation is subtraction, that returns a native
2301 * int as result and can be used with any 2 pointer kinds.
2302 * This is valid acording to Patition III 1.1.4
2305 stack_top (ctx)->stype = res;
2311 do_boolean_branch_op (VerifyContext *ctx, int delta)
2313 int target = ctx->ip_offset + delta;
2316 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2318 if (target < 0 || target >= ctx->code_size) {
2319 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2323 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2324 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2328 ctx->target = target;
2330 if (!check_underflow (ctx, 1))
2333 top = stack_pop (ctx);
2334 if (!is_valid_bool_arg (top))
2335 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));
2337 check_unmanaged_pointer (ctx, top);
2342 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2347 int target = ctx->ip_offset + delta;
2349 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2351 if (target < 0 || target >= ctx->code_size) {
2352 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2356 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2357 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2361 ctx->target = target;
2363 if (!check_underflow (ctx, 2))
2366 b = stack_pop (ctx);
2367 a = stack_pop (ctx);
2370 if (IS_MANAGED_POINTER (idxa))
2374 if (IS_MANAGED_POINTER (idxb))
2379 res = table [idxa][idxb];
2381 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2382 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2384 if (res == TYPE_INV) {
2385 ADD_VERIFY_ERROR (ctx,
2386 g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x",
2387 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2388 } else if (res & NON_VERIFIABLE_RESULT) {
2389 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x",
2390 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2391 res = res & ~NON_VERIFIABLE_RESULT;
2396 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX])
2402 if (!check_underflow (ctx, 2))
2404 b = stack_pop (ctx);
2405 a = stack_pop (ctx);
2408 if (IS_MANAGED_POINTER (idxa))
2412 if (IS_MANAGED_POINTER (idxb))
2417 res = table [idxa][idxb];
2419 if(res == TYPE_INV) {
2420 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));
2421 } else if (res & NON_VERIFIABLE_RESULT) {
2422 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x",
2423 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2424 res = res & ~NON_VERIFIABLE_RESULT;
2426 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2430 do_ret (VerifyContext *ctx)
2432 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2433 if (ctx->signature->ret->type != MONO_TYPE_VOID) {
2435 if (!check_underflow (ctx, 1))
2438 top = stack_pop(ctx);
2440 if (!verify_type_compat (ctx, ctx->signature->ret, top)) {
2441 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2446 if (ctx->eval.size > 0) {
2447 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2449 if (in_any_block (ctx->header, ctx->ip_offset))
2450 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2453 /* FIXME: we could just load the signature instead of the whole MonoMethod
2454 * TODO handle vararg calls
2455 * TODO handle non virt calls to non-final virtual calls (from the verifiability clause in page 52 of partition 3)
2456 * TODO handle abstract calls
2457 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2458 * TODO handle call invoking virtual methods (only allowed to invoke super)
2461 do_invoke_method (VerifyContext *ctx, int method_token)
2464 MonoMethodSignature *sig;
2466 MonoMethod *method = mono_get_method_full (ctx->image, method_token, NULL, ctx->generic_context);
2469 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method 0x%08x not found at 0x%04x", method_token, ctx->ip_offset));
2473 if (!(sig = mono_method_signature (method)))
2474 sig = mono_method_get_signature (method, ctx->image, method_token);
2476 param_count = sig->param_count + sig->hasthis;
2477 if (!check_underflow (ctx, param_count))
2480 for (i = sig->param_count - 1; i >= 0; --i) {
2481 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2482 value = stack_pop (ctx);
2483 if (!verify_type_compat (ctx, sig->params[i], value)) {
2484 if (sig->params [i]->type == MONO_TYPE_TYPEDBYREF) {
2485 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type for a call parameter at 0x%04x", ctx->ip_offset));
2488 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2493 MonoType * type = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2495 VERIFIER_DEBUG ( printf ("verifying this argument\n"); );
2496 value = stack_pop (ctx);
2497 if (!verify_type_compat (ctx, type, value)) {
2498 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature ret at 0x%04x", ctx->ip_offset));
2502 if (!mono_method_can_access_method (ctx->method, method))
2503 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset));
2505 if (sig->ret->type != MONO_TYPE_VOID) {
2506 if (check_overflow (ctx))
2507 set_stack_value (stack_push (ctx), sig->ret, FALSE);
2512 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2514 MonoClassField *field;
2517 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2519 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2523 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2524 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
2527 /*taking the address of initonly field only works from the static constructor */
2528 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2529 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
2530 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2532 if (!mono_method_can_access_field (ctx->method, field))
2533 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2535 set_stack_value (stack_push (ctx), field->type, take_addr);
2539 do_store_static_field (VerifyContext *ctx, int token) {
2540 MonoClassField *field;
2544 if (!check_underflow (ctx, 1))
2547 value = stack_pop (ctx);
2549 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2551 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2555 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2556 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
2560 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2561 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static 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));
2568 if (!verify_type_compat (ctx, field->type, value))
2569 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));
2573 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field)
2575 MonoClassField *field;
2578 /*must be one of: object type, managed pointer, unmanaged pointer (native int) or an instance of a value type */
2579 if (!((obj->stype == TYPE_COMPLEX)
2580 /* the managed reference must be to an object or value type */
2581 || (( IS_MANAGED_POINTER (obj->stype)) && (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX))
2582 || (obj->stype == TYPE_NATIVE_INT)
2583 || (obj->stype == TYPE_PTR)
2584 || (obj->stype == TYPE_COMPLEX))) {
2585 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));
2588 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2590 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2596 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2597 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
2600 g_assert (obj->type);
2602 /*The value on the stack must be a subclass of the defining type of the field*/
2603 /* we need to check if we can load the field from the stack value*/
2604 if (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX) {
2605 MonoType *type = obj->type->byref ? &field->parent->this_arg : &field->parent->byval_arg;
2607 if (!verify_stack_type_compatibility (ctx, type, obj->type, FALSE)) {
2608 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
2611 if (!mono_method_can_access_field (ctx->method, field))
2612 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2615 if (!mono_method_can_access_field (ctx->method, field))
2616 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2618 if (obj->stype == TYPE_NATIVE_INT)
2619 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
2621 check_unmanaged_pointer (ctx, obj);
2626 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
2629 MonoClassField *field;
2631 if (!check_underflow (ctx, 1))
2633 obj = stack_pop (ctx);
2635 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2638 if (take_addr && field->parent->valuetype && !IS_MANAGED_POINTER (obj->stype))
2639 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
2641 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2642 !(field->parent == ctx->method->klass && (ctx->method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && !strcmp (".ctor", ctx->method->name)))
2643 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2645 set_stack_value (stack_push (ctx), field->type, take_addr);
2649 do_store_field (VerifyContext *ctx, int token)
2651 ILStackDesc *value, *obj;
2652 MonoClassField *field;
2654 if (!check_underflow (ctx, 2))
2657 value = stack_pop (ctx);
2658 obj = stack_pop (ctx);
2660 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2663 if (!verify_type_compat (ctx, field->type, value))
2664 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));
2667 /*TODO proper handle for Nullable<T>*/
2669 do_box_value (VerifyContext *ctx, int klass_token)
2672 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2677 if (!check_underflow (ctx, 1))
2680 value = stack_top (ctx);
2681 /*box is a nop for reference types*/
2682 if (value->stype == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type))
2685 value = stack_pop (ctx);
2687 if (!verify_type_compat (ctx, type, value))
2688 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
2690 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (mono_defaults.object_class));
2694 do_unbox_value (VerifyContext *ctx, int klass_token)
2697 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2702 if (!check_underflow (ctx, 1))
2705 value = stack_pop (ctx);
2707 if (value->stype != TYPE_COMPLEX || value->type->type != MONO_TYPE_OBJECT)
2708 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));
2710 //TODO Pushed managed pointer is haver controled mutability (CMMP)
2711 set_stack_value (stack_push (ctx), mono_type_get_type_byref (type), FALSE);
2715 do_unary_math_op (VerifyContext *ctx, int op)
2718 if (!check_underflow (ctx, 1))
2720 value = stack_top (ctx);
2721 switch(value->stype) {
2724 case TYPE_NATIVE_INT:
2729 case TYPE_COMPLEX: /*only enums are ok*/
2730 if (value->type->type == MONO_TYPE_VALUETYPE && value->type->data.klass->enumtype)
2733 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
2738 do_conversion (VerifyContext *ctx, int kind)
2741 if (!check_underflow (ctx, 1))
2743 value = stack_pop (ctx);
2745 switch(value->stype) {
2748 case TYPE_NATIVE_INT:
2752 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for conversion operation at 0x%04x", ctx->ip_offset));
2757 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2760 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
2763 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
2765 case TYPE_NATIVE_INT:
2766 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
2769 g_error ("unknown type %02x in conversion", kind);
2775 do_load_token (VerifyContext *ctx, int token)
2778 MonoClass *handle_class;
2779 if (!check_overflow (ctx))
2781 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
2783 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
2786 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
2790 do_ldobj_value (VerifyContext *ctx, int token)
2793 MonoType *type = get_boxable_mono_type (ctx, token);
2798 if (!check_underflow (ctx, 1))
2801 value = stack_pop (ctx);
2802 if (!IS_MANAGED_POINTER (value->stype)
2803 && value->stype != TYPE_NATIVE_INT
2804 && !(value->stype == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
2805 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2809 if (value->stype == TYPE_NATIVE_INT)
2810 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
2812 /*We have a byval on the stack, we */
2813 if (!verify_stack_type_compatibility (ctx, type, mono_type_get_type_byval (value->type), TRUE))
2814 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
2816 set_stack_value (stack_push (ctx), type, FALSE);
2820 do_newarr (VerifyContext *ctx, int token)
2823 MonoType *type = get_boxable_mono_type (ctx, token);
2828 if (!check_underflow (ctx, 1))
2831 value = stack_pop (ctx);
2832 if (value->stype != TYPE_I4 && value->stype != TYPE_NATIVE_INT)
2833 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));
2835 set_stack_value (stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
2838 /*Merge the stacks and perform compat checks*/
2840 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start)
2844 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
2845 stack_init (ctx, to);
2848 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
2851 stack_copy (&ctx->eval, to);
2853 } else if (to->flags == IL_CODE_FLAG_NOT_PROCESSED) {
2854 stack_copy (to, from);
2857 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
2859 if (from->size != to->size) {
2860 VERIFIER_DEBUG ( printf ("diferent stack sizes %d x %d\n", from->size, to->size); );
2861 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, diferent sizes (%d x %d)", from->size, to->size));
2865 for (i = 0; i < from->size; ++i) {
2866 ILStackDesc *from_slot = from->stack + i;
2867 ILStackDesc *to_slot = to->stack + i;
2868 int from_stype = from_slot->stype;
2869 int to_stype = to_slot->stype;
2871 #define IS_NATIVE_INT_AND_I4(val0, val1) (UNMASK_TYPE(val0) == TYPE_NATIVE_INT && \
2872 UNMASK_TYPE(val1->stype) == TYPE_I4 && (val1->type->type == MONO_TYPE_I4 || val1->type->type == MONO_TYPE_U4))
2873 /* This is the only case of merging between verification types.
2874 * Both stack values must be either native int or int4, and both must be either byref or not.*/
2875 if ((IS_NATIVE_INT_AND_I4 (from_stype, to_slot) || IS_NATIVE_INT_AND_I4 (to_stype, from_slot)) &&
2876 !(IS_MANAGED_POINTER (from_stype) ^ IS_MANAGED_POINTER (to_stype))) {
2877 printf ("----is native int\n");
2878 to_slot->stype = TYPE_NATIVE_INT;
2879 if (UNMASK_TYPE(from_stype) == TYPE_NATIVE_INT)
2880 to_slot->type = from_slot->type;
2883 #undef IS_NATIVE_INT_OR_I4
2885 if (from_stype != to_stype) {
2886 VERIFIER_DEBUG ( printf ("diferent stack types %d x %d\n", from_stype, to_stype); );
2887 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, diferent verification types (%s x %s)",
2888 type_names [UNMASK_TYPE (from_stype)], type_names [UNMASK_TYPE (to_stype)]));
2892 if (IS_MANAGED_POINTER (from_stype)) {
2893 from_stype = UNMASK_TYPE (from_stype);
2894 to_stype = UNMASK_TYPE (to_stype);
2896 if (from_slot->type && !verify_stack_type_compatibility (ctx, to_slot->type, from_slot->type, TRUE)) {
2897 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, managed pointer types not compatible"));
2900 copy_stack_value (to_slot, from_slot);
2904 if (from_stype == TYPE_COMPLEX) {
2905 if (!to->stack [i].type) {
2906 ctx->verifiable = 0;
2908 } else if (!verify_type_compat (ctx, to_slot->type, from_slot)) {
2909 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, types not compatible"));
2912 /*TODO we need to choose the base class for merging */
2913 copy_stack_value (to_slot, from_slot);
2918 copy_stack_value (to_slot, from_slot);
2922 to->flags = IL_CODE_FLAG_SEEN;
2927 * FIXME: need to distinguish between valid and verifiable.
2928 * Need to keep track of types on the stack.
2929 * Verify types for opcodes.
2932 mono_method_verify (MonoMethod *method, int level)
2934 MonoMethodSignature *csig;
2935 const unsigned char *ip;
2936 const unsigned char *end;
2937 const unsigned char *target = NULL; /* branch target */
2938 int i, n, need_merge = 0, start = 0;
2939 guint token, ip_offset = 0, prefix = 0;
2940 MonoMethod *cmethod;
2941 MonoGenericContext *generic_context = NULL;
2944 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s\n", method->klass->name, method->name); );
2946 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2947 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
2951 memset (&ctx, 0, sizeof (VerifyContext));
2953 ctx.signature = mono_method_signature (method);
2954 if (!ctx.signature) {
2955 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
2958 ctx.header = mono_method_get_header (method);
2960 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
2963 ctx.method = method;
2964 ip = ctx.header->code;
2965 end = ip + ctx.header->code_size;
2966 ctx.image = image = method->klass->image;
2969 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
2970 ctx.max_stack = ctx.header->max_stack;
2971 ctx.verifiable = ctx.valid = 1;
2973 ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
2974 ctx.code_size = ctx.header->code_size;
2976 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
2979 ctx.num_locals = ctx.header->num_locals;
2980 ctx.locals = ctx.header->locals;
2983 if (ctx.signature->hasthis) {
2984 ctx.params = g_new0 (MonoType*, ctx.max_args);
2985 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2986 memcpy (ctx.params + 1, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
2988 ctx.params = ctx.signature->params;
2991 if (ctx.signature->is_inflated)
2992 ctx.generic_context = generic_context = mono_method_get_context (method);
2994 stack_init (&ctx, &ctx.eval);
2997 for (i = 0; i < ctx.header->num_clauses; ++i) {
2998 MonoExceptionClause *clause = ctx.header->clauses + i;
2999 /* catch blocks and filter have the exception on the stack. */
3000 /* must check boundaries for handler_offset and handler_start < handler_start*/
3001 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
3002 ILCodeDesc *code = ctx.code + clause->handler_offset;
3003 stack_init (&ctx, code);
3004 code->stack [0].stype = TYPE_COMPLEX;
3005 code->stack [0].type = &clause->data.catch_class->byval_arg;
3007 code->flags = IL_CODE_FLAG_SEEN;
3009 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3010 ILCodeDesc *code = ctx.code + clause->data.filter_offset;
3011 stack_init (&ctx, code);
3012 code->stack [0].stype = TYPE_COMPLEX;
3013 code->stack [0].type = &mono_defaults.exception_class->byval_arg;
3015 code->flags = IL_CODE_FLAG_SEEN;
3019 while (ip < end && ctx.valid) {
3020 ctx.ip_offset = ip_offset = ip - ctx.header->code;
3022 /*TODO id stack merge fails, we break, should't we - or only on errors??
3023 TODO verify need_merge
3026 VERIFIER_DEBUG ( printf ("extra merge needed! %d \n", ctx.target); );
3027 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE);
3030 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start);
3033 /*TODO rename to zero */
3037 discode = mono_disasm_code_one (NULL, method, ip, NULL);
3038 discode [strlen (discode) - 1] = 0; /* no \n */
3039 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
3042 dump_stack_state(&ctx.code[ip_offset]);
3055 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
3061 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
3066 do_binop (&ctx, *ip, add_table);
3071 do_binop (&ctx, *ip, sub_table);
3078 do_binop (&ctx, *ip, bin_op_table);
3087 do_binop (&ctx, *ip, int_bin_op_table);
3094 do_binop (&ctx, *ip, shift_op_table);
3099 if (!check_underflow (&ctx, 1))
3115 /*TODO support definite assignment verification? */
3116 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
3124 store_local (&ctx, *ip - CEE_STLOC_0);
3129 store_local (&ctx, ip [1]);
3134 store_arg (&ctx, ip [1]);
3148 if (check_overflow (&ctx))
3149 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3154 if (check_overflow (&ctx))
3155 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3160 if (check_overflow (&ctx))
3161 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
3166 if (check_overflow (&ctx))
3167 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3172 if (check_overflow (&ctx))
3173 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3178 if (check_overflow (&ctx))
3179 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3184 if (check_overflow (&ctx))
3185 stack_push_val (&ctx,TYPE_COMPLEX, &mono_defaults.object_class->byval_arg);
3191 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
3204 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
3211 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
3224 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
3231 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
3235 /* FIXME: warn/error instead? */
3242 if (!check_underflow (&ctx, 1))
3244 if (!check_overflow (&ctx))
3246 top = stack_push (&ctx);
3247 copy_stack_value (top, stack_get (&ctx, 1));
3254 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
3255 token = read32 (ip + 1);
3256 if (in_any_block (ctx.header, ip_offset))
3257 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
3259 * FIXME: check signature, retval, arguments etc.
3265 do_invoke_method (&ctx, read32 (ip + 1));
3270 token = read32 (ip + 1);
3272 * FIXME: check signature, retval, arguments etc.
3277 target = ip + (signed char)ip [1] + 2;
3278 if (target >= end || target < ctx.header->code)
3279 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3280 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3281 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3288 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
3294 target = ip + (gint32)read32 (ip + 1) + 5;
3295 if (target >= end || target < ctx.header->code)
3296 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3297 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3298 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3305 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
3311 n = read32 (ip + 1);
3312 target = ip + sizeof (guint32) * n;
3313 /* FIXME: check that ip is in range (and within the same exception block) */
3314 for (i = 0; i < n; ++i)
3315 if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < ctx.header->code)
3316 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3317 if (!check_underflow (&ctx, 1))
3319 if (stack_pop (&ctx)->stype != TYPE_I4)
3320 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ip_offset));
3321 ip += 5 + sizeof (guint32) * n;
3334 if (!check_underflow (&ctx, 1))
3336 if (stack_top (&ctx)->stype != TYPE_PTR && stack_top (&ctx)->stype != TYPE_COMPLEX)
3337 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldind at 0x%04x", ip_offset));
3338 stack_top (&ctx)->stype = ldind_type [*ip - CEE_LDIND_I1];
3348 if (!check_underflow (&ctx, 2))
3351 if (stack_top (&ctx)->stype != TYPE_PTR && stack_top (&ctx)->stype != TYPE_COMPLEX)
3352 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid pointer argument to stind at 0x%04x", ip_offset));
3353 if (!stind_type (*ip, stack_get (&ctx, -1)->stype))
3354 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Incompatible value argument to stind at 0x%04x", ip_offset));
3360 do_unary_math_op (&ctx, *ip);
3364 //TODO: implement proper typecheck
3371 do_conversion (&ctx, TYPE_I4);
3377 do_conversion (&ctx, TYPE_I8);
3384 do_conversion (&ctx, TYPE_R8);
3390 do_conversion (&ctx, TYPE_NATIVE_INT);
3395 token = read32 (ip + 1);
3396 if (!check_underflow (&ctx, 2))
3403 do_ldobj_value (&ctx, read32 (ip + 1));
3408 /*TODO verify if token is a valid string literal*/
3409 token = read32 (ip + 1);
3410 if (check_overflow (&ctx))
3411 stack_push_val (&ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
3415 token = read32 (ip + 1);
3417 * FIXME: we could just load the signature ...
3419 cmethod = mono_get_method_full (image, token, NULL, generic_context);
3421 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ip_offset));
3422 csig = mono_method_signature (cmethod);
3423 if (!check_underflow (&ctx, csig->param_count))
3425 ctx.eval.size -= csig->param_count;
3426 if (check_overflow (&ctx))
3427 stack_push_val (&ctx, cmethod->klass->valuetype? TYPE_COMPLEX: TYPE_COMPLEX, &cmethod->klass->byval_arg);
3433 token = read32 (ip + 1);
3434 if (!check_underflow (&ctx, 1))
3440 ++ip; /* warn, error ? */
3443 do_unbox_value (&ctx, read32 (ip + 1));
3447 if (!check_underflow (&ctx, 1))
3456 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
3462 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
3467 do_store_field (&ctx, read32 (ip + 1));
3472 do_store_static_field (&ctx, read32 (ip + 1));
3476 if (!check_underflow (&ctx, 2))
3479 token = read32 (ip + 1);
3483 case CEE_CONV_OVF_I1_UN:
3484 case CEE_CONV_OVF_I2_UN:
3485 case CEE_CONV_OVF_I4_UN:
3486 case CEE_CONV_OVF_U1_UN:
3487 case CEE_CONV_OVF_U2_UN:
3488 case CEE_CONV_OVF_U4_UN:
3489 do_conversion (&ctx, TYPE_I4);
3493 case CEE_CONV_OVF_I8_UN:
3494 case CEE_CONV_OVF_U8_UN:
3495 do_conversion (&ctx, TYPE_I8);
3499 case CEE_CONV_OVF_I_UN:
3500 case CEE_CONV_OVF_U_UN:
3501 do_conversion (&ctx, TYPE_NATIVE_INT);
3506 do_box_value (&ctx, read32 (ip + 1));
3511 do_newarr (&ctx, read32 (ip + 1));
3516 if (!check_underflow (&ctx, 1))
3518 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3519 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldlen at 0x%04x", ip_offset));
3520 stack_top (&ctx)->type = &mono_defaults.int32_class->byval_arg; /* FIXME: use a native int type */
3521 stack_top (&ctx)->stype = TYPE_PTR;
3525 if (check_underflow (&ctx, 2))
3528 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3529 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelema at 0x%04x", ip_offset));
3530 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3531 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
3532 stack_top (&ctx)->stype = TYPE_COMPLEX;
3533 token = read32 (ip + 1);
3546 case CEE_LDELEM_REF:
3547 if (!check_underflow (&ctx, 2))
3550 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3551 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelem at 0x%04x", ip_offset));
3552 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3553 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
3554 stack_top (&ctx)->stype = ldelem_type [*ip - CEE_LDELEM_I1];
3564 case CEE_STELEM_REF:
3565 if (!check_underflow (&ctx, 3))
3570 case CEE_LDELEM_ANY:
3571 case CEE_STELEM_ANY:
3586 ++ip; /* warn, error ? */
3589 case CEE_CONV_OVF_I1:
3590 case CEE_CONV_OVF_U1:
3591 case CEE_CONV_OVF_I2:
3592 case CEE_CONV_OVF_U2:
3593 case CEE_CONV_OVF_I4:
3594 case CEE_CONV_OVF_U4:
3595 do_conversion (&ctx, TYPE_I4);
3599 case CEE_CONV_OVF_I8:
3600 case CEE_CONV_OVF_U8:
3601 do_conversion (&ctx, TYPE_I8);
3605 case CEE_CONV_OVF_I:
3606 case CEE_CONV_OVF_U:
3607 do_conversion (&ctx, TYPE_NATIVE_INT);
3618 ++ip; /* warn, error ? */
3621 if (!check_underflow (&ctx, 1))
3626 if (!check_underflow (&ctx, 1))
3632 ++ip; /* warn, error ? */
3635 if (!check_underflow (&ctx, 1))
3637 token = read32 (ip + 1);
3649 ++ip; /* warn, error ? */
3652 do_load_token (&ctx, read32 (ip + 1));
3657 case CEE_ADD_OVF_UN:
3659 case CEE_MUL_OVF_UN:
3661 case CEE_SUB_OVF_UN:
3662 if (!check_underflow (&ctx, 2))
3667 case CEE_ENDFINALLY:
3672 target = ip + (gint32)read32(ip + 1) + 5;
3673 if (target >= end || target < ctx.header->code)
3674 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3675 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3676 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3681 target = ip + (signed char)ip [1] + 2;
3682 if (target >= end || target < ctx.header->code)
3683 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3684 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3685 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3690 if (!check_underflow (&ctx, 2))
3733 store_local (&ctx, read16 (ip + 1));
3738 do_cmp_op (&ctx, cmp_br_eq_op);
3746 do_cmp_op (&ctx, cmp_br_op);
3751 store_arg (&ctx, read16 (ip + 1) );
3757 check_overflow (&ctx);
3760 if (!check_overflow (&ctx))
3762 token = read32 (ip + 1);
3764 stack_top (&ctx)->stype = TYPE_PTR;
3768 if (!check_underflow (&ctx, 1))
3770 token = read32 (ip + 1);
3772 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3773 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ip_offset));
3774 stack_top (&ctx)->stype = TYPE_PTR;
3782 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
3788 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
3793 if (ctx.eval.size != 1)
3794 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ip_offset));
3795 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3796 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to localloc at 0x%04x", ip_offset));
3797 stack_top (&ctx)->stype = TYPE_COMPLEX;
3804 if (ctx.eval.size != 1)
3805 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Stack must have only filter result in endfilter at 0x%04x", ip_offset));
3808 case CEE_UNALIGNED_:
3809 prefix |= PREFIX_UNALIGNED;
3813 prefix |= PREFIX_VOLATILE;
3817 prefix |= PREFIX_TAIL;
3819 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
3820 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
3823 if (!check_underflow (&ctx, 1))
3825 token = read32 (ip + 1);
3829 case CEE_CONSTRAINED_:
3830 token = read32 (ip + 1);
3834 if (!check_underflow (&ctx, 3))
3839 if (!check_underflow (&ctx, 3))
3853 if (!check_overflow (&ctx))
3855 token = read32 (ip + 1);
3857 stack_top (&ctx)->type = &mono_defaults.uint32_class->byval_arg;
3858 stack_top (&ctx)->stype = TYPE_I4;
3861 case CEE_REFANYTYPE:
3862 if (!check_underflow (&ctx, 1))
3876 * if ip != end we overflowed: mark as error.
3878 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
3879 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
3885 for (i = 0; i < ctx.header->code_size; ++i) {
3886 if (ctx.code [i].stack)
3887 g_free (ctx.code [i].stack);
3892 g_free (ctx.eval.stack);
3895 if (ctx.signature->hasthis)
3896 g_free (ctx.params);
3908 const FieldDesc *fields;
3911 static const FieldDesc
3912 typebuilder_fields[] = {
3913 {"tname", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, name)},
3914 {"nspace", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, nspace)},
3915 {"parent", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, parent)},
3916 {"interfaces", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, interfaces)},
3917 {"methods", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, methods)},
3918 {"properties", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, properties)},
3919 {"fields", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, fields)},
3920 {"attrs", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, attrs)},
3921 {"table_idx", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, table_idx)},
3925 static const FieldDesc
3926 modulebuilder_fields[] = {
3927 {"types", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, types)},
3928 {"cattrs", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, cattrs)},
3929 {"guid", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, guid)},
3930 {"table_idx", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, table_idx)},
3934 static const FieldDesc
3935 assemblybuilder_fields[] = {
3936 {"entry_point", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, entry_point)},
3937 {"modules", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, modules)},
3938 {"name", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, name)},
3939 {"resources", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, resources)},
3940 {"version", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, version)},
3941 {"culture", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, culture)},
3945 static const FieldDesc
3946 ctorbuilder_fields[] = {
3947 {"ilgen", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, ilgen)},
3948 {"parameters", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, parameters)},
3949 {"attrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, attrs)},
3950 {"iattrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, iattrs)},
3951 {"table_idx", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, table_idx)},
3952 {"call_conv", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, call_conv)},
3953 {"type", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, type)},
3957 static const FieldDesc
3958 methodbuilder_fields[] = {
3959 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, mhandle)},
3960 {"rtype", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, rtype)},
3961 {"parameters", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, parameters)},
3962 {"attrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, attrs)},
3963 {"iattrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, iattrs)},
3964 {"name", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, name)},
3965 {"table_idx", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, table_idx)},
3966 {"code", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, code)},
3967 {"ilgen", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, ilgen)},
3968 {"type", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, type)},
3969 {"pinfo", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, pinfo)},
3970 {"pi_dll", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dll)},
3971 {"pi_entry", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dllentry)},
3972 {"charset", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, charset)},
3973 {"extra_flags", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, extra_flags)},
3974 {"native_cc", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, native_cc)},
3975 {"call_conv", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, call_conv)},
3979 static const FieldDesc
3980 fieldbuilder_fields[] = {
3981 {"attrs", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, attrs)},
3982 {"type", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, type)},
3983 {"name", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, name)},
3984 {"def_value", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, def_value)},
3985 {"offset", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, offset)},
3986 {"table_idx", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, table_idx)},
3990 static const FieldDesc
3991 propertybuilder_fields[] = {
3992 {"attrs", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, attrs)},
3993 {"name", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, name)},
3994 {"type", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, type)},
3995 {"parameters", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, parameters)},
3996 {"def_value", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, def_value)},
3997 {"set_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, set_method)},
3998 {"get_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, get_method)},
3999 {"table_idx", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, table_idx)},
4003 static const FieldDesc
4004 ilgenerator_fields[] = {
4005 {"code", G_STRUCT_OFFSET (MonoReflectionILGen, code)},
4006 {"code_len", G_STRUCT_OFFSET (MonoReflectionILGen, code_len)},
4007 {"max_stack", G_STRUCT_OFFSET (MonoReflectionILGen, max_stack)},
4008 {"cur_stack", G_STRUCT_OFFSET (MonoReflectionILGen, cur_stack)},
4009 {"locals", G_STRUCT_OFFSET (MonoReflectionILGen, locals)},
4010 {"ex_handlers", G_STRUCT_OFFSET (MonoReflectionILGen, ex_handlers)},
4014 static const FieldDesc
4015 ilexinfo_fields[] = {
4016 {"handlers", G_STRUCT_OFFSET (MonoILExceptionInfo, handlers)},
4017 {"start", G_STRUCT_OFFSET (MonoILExceptionInfo, start)},
4018 {"len", G_STRUCT_OFFSET (MonoILExceptionInfo, len)},
4019 {"end", G_STRUCT_OFFSET (MonoILExceptionInfo, label)},
4023 static const FieldDesc
4024 ilexblock_fields[] = {
4025 {"extype", G_STRUCT_OFFSET (MonoILExceptionBlock, extype)},
4026 {"type", G_STRUCT_OFFSET (MonoILExceptionBlock, type)},
4027 {"start", G_STRUCT_OFFSET (MonoILExceptionBlock, start)},
4028 {"len", G_STRUCT_OFFSET (MonoILExceptionBlock, len)},
4029 {"filter_offset", G_STRUCT_OFFSET (MonoILExceptionBlock, filter_offset)},
4033 static const ClassDesc
4034 emit_classes_to_check [] = {
4035 {"TypeBuilder", typebuilder_fields},
4036 {"ModuleBuilder", modulebuilder_fields},
4037 {"AssemblyBuilder", assemblybuilder_fields},
4038 {"ConstructorBuilder", ctorbuilder_fields},
4039 {"MethodBuilder", methodbuilder_fields},
4040 {"FieldBuilder", fieldbuilder_fields},
4041 {"PropertyBuilder", propertybuilder_fields},
4042 {"ILGenerator", ilgenerator_fields},
4043 {"ILExceptionBlock", ilexblock_fields},
4044 {"ILExceptionInfo", ilexinfo_fields},
4048 static const FieldDesc
4049 monoevent_fields[] = {
4050 {"klass", G_STRUCT_OFFSET (MonoReflectionEvent, klass)},
4051 {"handle", G_STRUCT_OFFSET (MonoReflectionEvent, event)},
4055 static const FieldDesc
4056 monoproperty_fields[] = {
4057 {"klass", G_STRUCT_OFFSET (MonoReflectionProperty, klass)},
4058 {"prop", G_STRUCT_OFFSET (MonoReflectionProperty, property)},
4062 static const FieldDesc
4063 monofield_fields[] = {
4064 {"klass", G_STRUCT_OFFSET (MonoReflectionField, klass)},
4065 {"fhandle", G_STRUCT_OFFSET (MonoReflectionField, field)},
4069 static const FieldDesc
4070 monomethodinfo_fields[] = {
4071 {"parent", G_STRUCT_OFFSET (MonoMethodInfo, parent)},
4072 {"ret", G_STRUCT_OFFSET (MonoMethodInfo, ret)},
4073 {"attrs", G_STRUCT_OFFSET (MonoMethodInfo, attrs)},
4074 {"iattrs", G_STRUCT_OFFSET (MonoMethodInfo, implattrs)},
4078 static const FieldDesc
4079 monopropertyinfo_fields[] = {
4080 {"parent", G_STRUCT_OFFSET (MonoPropertyInfo, parent)},
4081 {"name", G_STRUCT_OFFSET (MonoPropertyInfo, name)},
4082 {"get_method", G_STRUCT_OFFSET (MonoPropertyInfo, get)},
4083 {"set_method", G_STRUCT_OFFSET (MonoPropertyInfo, set)},
4084 {"attrs", G_STRUCT_OFFSET (MonoPropertyInfo, attrs)},
4088 static const FieldDesc
4089 monomethod_fields[] = {
4090 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4094 static const FieldDesc
4095 monocmethod_fields[] = {
4096 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4100 static const FieldDesc
4102 {"ClassImpl", G_STRUCT_OFFSET (MonoReflectionParameter, ClassImpl)},
4103 {"DefaultValueImpl", G_STRUCT_OFFSET (MonoReflectionParameter, DefaultValueImpl)},
4104 {"MemberImpl", G_STRUCT_OFFSET (MonoReflectionParameter, MemberImpl)},
4105 {"NameImpl", G_STRUCT_OFFSET (MonoReflectionParameter, NameImpl)},
4106 {"PositionImpl", G_STRUCT_OFFSET (MonoReflectionParameter, PositionImpl)},
4107 {"AttrsImpl", G_STRUCT_OFFSET (MonoReflectionParameter, AttrsImpl)},
4111 static const ClassDesc
4112 reflection_classes_to_check [] = {
4113 {"MonoEvent", monoevent_fields},
4114 {"MonoProperty", monoproperty_fields},
4115 {"MonoField", monofield_fields},
4116 {"MonoMethodInfo", monomethodinfo_fields},
4117 {"MonoPropertyInfo", monopropertyinfo_fields},
4118 {"MonoMethod", monomethod_fields},
4119 {"MonoCMethod", monocmethod_fields},
4120 {"ParameterInfo", pinfo_fields},
4125 enuminfo_fields[] = {
4126 {"utype", G_STRUCT_OFFSET (MonoEnumInfo, utype)},
4127 {"values", G_STRUCT_OFFSET (MonoEnumInfo, values)},
4128 {"names", G_STRUCT_OFFSET (MonoEnumInfo, names)},
4133 delegate_fields[] = {
4134 {"target_type", G_STRUCT_OFFSET (MonoDelegate, target_type)},
4135 {"m_target", G_STRUCT_OFFSET (MonoDelegate, target)},
4136 {"method_name", G_STRUCT_OFFSET (MonoDelegate, method_name)},
4137 {"method_ptr", G_STRUCT_OFFSET (MonoDelegate, method_ptr)},
4138 {"delegate_trampoline", G_STRUCT_OFFSET (MonoDelegate, delegate_trampoline)},
4139 {"method_info", G_STRUCT_OFFSET (MonoDelegate, method_info)},
4144 multicast_delegate_fields[] = {
4145 {"prev", G_STRUCT_OFFSET (MonoMulticastDelegate, prev)},
4150 async_result_fields[] = {
4151 {"async_state", G_STRUCT_OFFSET (MonoAsyncResult, async_state)},
4152 {"handle", G_STRUCT_OFFSET (MonoAsyncResult, handle)},
4153 {"async_delegate", G_STRUCT_OFFSET (MonoAsyncResult, async_delegate)},
4154 {"data", G_STRUCT_OFFSET (MonoAsyncResult, data)},
4155 {"sync_completed", G_STRUCT_OFFSET (MonoAsyncResult, sync_completed)},
4156 {"completed", G_STRUCT_OFFSET (MonoAsyncResult, completed)},
4157 {"endinvoke_called", G_STRUCT_OFFSET (MonoAsyncResult, endinvoke_called)},
4158 {"async_callback", G_STRUCT_OFFSET (MonoAsyncResult, async_callback)},
4163 exception_fields[] = {
4164 {"trace_ips", G_STRUCT_OFFSET (MonoException, trace_ips)},
4165 {"inner_exception", G_STRUCT_OFFSET (MonoException, inner_ex)},
4166 {"message", G_STRUCT_OFFSET (MonoException, message)},
4167 {"help_link", G_STRUCT_OFFSET (MonoException, help_link)},
4168 {"class_name", G_STRUCT_OFFSET (MonoException, class_name)},
4169 {"stack_trace", G_STRUCT_OFFSET (MonoException, stack_trace)},
4170 {"remote_stack_trace", G_STRUCT_OFFSET (MonoException, remote_stack_trace)},
4171 {"remote_stack_index", G_STRUCT_OFFSET (MonoException, remote_stack_index)},
4172 {"hresult", G_STRUCT_OFFSET (MonoException, hresult)},
4173 {"source", G_STRUCT_OFFSET (MonoException, source)},
4177 static const ClassDesc
4178 system_classes_to_check [] = {
4179 {"Exception", exception_fields},
4180 {"MonoEnumInfo", enuminfo_fields},
4181 {"Delegate", delegate_fields},
4182 {"MulticastDelegate", multicast_delegate_fields},
4187 stack_frame_fields [] = {
4188 {"ilOffset", G_STRUCT_OFFSET (MonoStackFrame, il_offset)},
4189 {"nativeOffset", G_STRUCT_OFFSET (MonoStackFrame, native_offset)},
4190 {"methodBase", G_STRUCT_OFFSET (MonoStackFrame, method)},
4191 {"fileName", G_STRUCT_OFFSET (MonoStackFrame, filename)},
4192 {"lineNumber", G_STRUCT_OFFSET (MonoStackFrame, line)},
4193 {"columnNumber", G_STRUCT_OFFSET (MonoStackFrame, column)},
4197 static const ClassDesc
4198 system_diagnostics_classes_to_check [] = {
4199 {"StackFrame", stack_frame_fields},
4204 mono_method_message_fields[] = {
4205 {"method", G_STRUCT_OFFSET (MonoMethodMessage, method)},
4206 {"args", G_STRUCT_OFFSET (MonoMethodMessage, args)},
4207 {"names", G_STRUCT_OFFSET (MonoMethodMessage, names)},
4208 {"arg_types", G_STRUCT_OFFSET (MonoMethodMessage, arg_types)},
4209 {"ctx", G_STRUCT_OFFSET (MonoMethodMessage, ctx)},
4210 {"rval", G_STRUCT_OFFSET (MonoMethodMessage, rval)},
4211 {"exc", G_STRUCT_OFFSET (MonoMethodMessage, exc)},
4215 static const ClassDesc
4216 messaging_classes_to_check [] = {
4217 {"AsyncResult", async_result_fields},
4218 {"MonoMethodMessage", mono_method_message_fields},
4223 transparent_proxy_fields[] = {
4224 {"_rp", G_STRUCT_OFFSET (MonoTransparentProxy, rp)},
4225 {"_class", G_STRUCT_OFFSET (MonoTransparentProxy, remote_class)},
4230 real_proxy_fields[] = {
4231 {"class_to_proxy", G_STRUCT_OFFSET (MonoRealProxy, class_to_proxy)},
4235 static const ClassDesc
4236 proxy_classes_to_check [] = {
4237 {"TransparentProxy", transparent_proxy_fields},
4238 {"RealProxy", real_proxy_fields},
4243 wait_handle_fields[] = {
4244 {"os_handle", G_STRUCT_OFFSET (MonoWaitHandle, handle)},
4245 {"disposed", G_STRUCT_OFFSET (MonoWaitHandle, disposed)},
4251 {"system_thread_handle", G_STRUCT_OFFSET (MonoThread, handle)},
4252 {"threadpool_thread", G_STRUCT_OFFSET (MonoThread, threadpool_thread)},
4253 {"state", G_STRUCT_OFFSET (MonoThread, state)},
4254 {"abort_exc", G_STRUCT_OFFSET (MonoThread, abort_exc)},
4255 {"abort_state", G_STRUCT_OFFSET (MonoThread, abort_state)},
4256 {"thread_id", G_STRUCT_OFFSET (MonoThread, tid)},
4260 static const ClassDesc
4261 threading_classes_to_check [] = {
4262 {"Thread", thread_fields},
4263 {"WaitHandle", wait_handle_fields},
4267 static const FieldDesc
4269 {"datetime_format", G_STRUCT_OFFSET (MonoCultureInfo, datetime_format)},
4270 {"number_format", G_STRUCT_OFFSET (MonoCultureInfo, number_format)},
4271 {"textinfo", G_STRUCT_OFFSET (MonoCultureInfo, textinfo)},
4272 {"name", G_STRUCT_OFFSET (MonoCultureInfo, name)},
4273 {"displayname", G_STRUCT_OFFSET (MonoCultureInfo, displayname)},
4274 {"englishname", G_STRUCT_OFFSET (MonoCultureInfo, englishname)},
4275 {"nativename", G_STRUCT_OFFSET (MonoCultureInfo, nativename)},
4276 {"iso3lang", G_STRUCT_OFFSET (MonoCultureInfo, iso3lang)},
4277 {"iso2lang", G_STRUCT_OFFSET (MonoCultureInfo, iso2lang)},
4278 {"icu_name", G_STRUCT_OFFSET (MonoCultureInfo, icu_name)},
4279 {"win3lang", G_STRUCT_OFFSET (MonoCultureInfo, win3lang)},
4280 {"compareinfo", G_STRUCT_OFFSET (MonoCultureInfo, compareinfo)},
4284 static const FieldDesc
4285 dtfinfo_fields[] = {
4286 {"amDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, AMDesignator)},
4287 {"pmDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, PMDesignator)},
4288 {"dayNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, DayNames)},
4289 {"monthNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, MonthNames)},
4293 static const FieldDesc
4295 {"decimalFormats", G_STRUCT_OFFSET (MonoNumberFormatInfo, decimalFormats)},
4296 {"currencySymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, currencySymbol)},
4297 {"percentSymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, percentSymbol)},
4298 {"positiveSign", G_STRUCT_OFFSET (MonoNumberFormatInfo, positiveSign)},
4302 static const FieldDesc
4303 compinfo_fields[] = {
4304 {"lcid", G_STRUCT_OFFSET (MonoCompareInfo, lcid)},
4305 {"ICU_collator", G_STRUCT_OFFSET (MonoCompareInfo, ICU_collator)},
4309 static const FieldDesc
4310 sortkey_fields[] = {
4311 {"str", G_STRUCT_OFFSET (MonoSortKey, str)},
4312 {"options", G_STRUCT_OFFSET (MonoSortKey, options)},
4313 {"key", G_STRUCT_OFFSET (MonoSortKey, key)},
4314 {"lcid", G_STRUCT_OFFSET (MonoSortKey, lcid)},
4318 static const ClassDesc
4319 globalization_classes_to_check [] = {
4320 {"CultureInfo", cinfo_fields},
4321 {"DateTimeFormatInfo", dtfinfo_fields},
4322 {"NumberFormatInfo", nfinfo_fields},
4323 {"CompareInfo", compinfo_fields},
4324 {"SortKey", sortkey_fields},
4328 static const FieldDesc
4329 safe_handle_fields[] ={
4330 {"handle", G_STRUCT_OFFSET (MonoSafeHandle, handle)},
4334 static const ClassDesc interop_classes_to_check [] = {
4335 {"SafeHandle", safe_handle_fields},
4341 const ClassDesc *types;
4344 static const NameSpaceDesc
4345 namespaces_to_check[] = {
4346 {"System.Runtime.Remoting.Proxies", proxy_classes_to_check},
4347 {"System.Runtime.Remoting.Messaging", messaging_classes_to_check},
4348 {"System.Reflection.Emit", emit_classes_to_check},
4349 {"System.Reflection", reflection_classes_to_check},
4350 {"System.Threading", threading_classes_to_check},
4351 {"System.Diagnostics", system_diagnostics_classes_to_check},
4352 {"System", system_classes_to_check},
4353 {"System.Globalization", globalization_classes_to_check},
4354 {"System.Runtime.InteropServices", interop_classes_to_check},
4359 check_corlib (MonoImage *corlib)
4362 MonoClassField *field;
4363 const FieldDesc *fdesc;
4364 const ClassDesc *cdesc;
4365 const NameSpaceDesc *ndesc;
4367 GString *result = NULL;
4369 for (ndesc = namespaces_to_check; ndesc->name; ++ndesc) {
4370 for (cdesc = ndesc->types; cdesc->name; ++cdesc) {
4371 klass = mono_class_from_name (corlib, ndesc->name, cdesc->name);
4374 result = g_string_new ("");
4375 g_string_append_printf (result, "Cannot find class %s\n", cdesc->name);
4378 mono_class_init (klass);
4380 * FIXME: we should also check the size of valuetypes, or
4381 * we're going to have trouble when we access them in arrays.
4383 if (klass->valuetype)
4384 struct_offset = sizeof (MonoObject);
4387 for (fdesc = cdesc->fields; fdesc->name; ++fdesc) {
4388 field = mono_class_get_field_from_name (klass, fdesc->name);
4389 if (!field || (field->offset != (fdesc->offset + struct_offset))) {
4391 result = g_string_new ("");
4392 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));
4398 return g_string_free (result, FALSE);
4403 mono_verify_corlib () {
4404 return check_corlib (mono_defaults.corlib);