2 #include <mono/metadata/object-internals.h>
3 #include <mono/metadata/verify.h>
4 #include <mono/metadata/opcodes.h>
5 #include <mono/metadata/tabledefs.h>
6 #include <mono/metadata/reflection.h>
7 #include <mono/metadata/debug-helpers.h>
8 #include <mono/metadata/mono-endian.h>
9 #include <mono/metadata/metadata.h>
10 #include <mono/metadata/metadata-internals.h>
11 #include <mono/metadata/class-internals.h>
12 #include <mono/metadata/tokentype.h>
18 * Pull the list of opcodes
20 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
24 #include "mono/cil/opcode.def"
29 #ifdef MONO_VERIFIER_DEBUG
30 #define VERIFIER_DEBUG(code) do { code } while (0)
32 #define VERIFIER_DEBUG(code)
35 //////////////////////////////////////////////////////////////////
36 #define 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)
1242 return &mono_class_from_mono_type (type)->this_arg;
1246 /*Returns the byval version of the supplied MonoType*/
1248 mono_type_get_type_byval (MonoType *type)
1252 return &mono_class_from_mono_type (type)->byval_arg;
1256 mono_type_from_stack_slot (ILStackDesc *slot)
1258 if (IS_MANAGED_POINTER (slot->stype))
1259 return mono_type_get_type_byref (slot->type);
1263 /*Stack manipulation code*/
1266 stack_init (VerifyContext *ctx, ILCodeDesc *state)
1269 if (!state->stack) {
1270 state->stack = g_new0 (ILStackDesc, ctx->max_stack);
1275 stack_copy (ILCodeDesc *to, ILCodeDesc *from)
1277 to->size = from->size;
1278 memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
1282 copy_stack_value (ILStackDesc *to, ILStackDesc *from)
1284 to->stype = from->stype;
1285 to->type = from->type;
1289 check_underflow (VerifyContext *ctx, int size)
1291 if (ctx->eval.size < size) {
1292 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
1299 check_overflow (VerifyContext *ctx)
1301 if (ctx->eval.size >= ctx->max_stack) {
1302 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
1309 check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
1311 if (value->stype == TYPE_PTR) {
1312 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1319 check_unverifiable_type (VerifyContext *ctx, MonoType *type)
1321 if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
1322 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
1329 static ILStackDesc *
1330 stack_push (VerifyContext *ctx)
1332 return & ctx->eval.stack [ctx->eval.size++];
1335 static ILStackDesc *
1336 stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
1338 ILStackDesc *top = stack_push (ctx);
1344 static ILStackDesc *
1345 stack_pop (VerifyContext *ctx)
1347 return ctx->eval.stack + --ctx->eval.size;
1350 static inline ILStackDesc *
1351 stack_top (VerifyContext *ctx)
1353 return ctx->eval.stack + (ctx->eval.size - 1);
1356 static inline ILStackDesc *
1357 stack_get (VerifyContext *ctx, int distance)
1359 return ctx->eval.stack + (ctx->eval.size - distance - 1);
1362 /* Returns the MonoType associated with the token, or NULL if it is invalid.
1364 * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
1367 get_boxable_mono_type (VerifyContext* ctx, int token)
1369 MonoType *type = mono_type_get_full (ctx->image, token, ctx->generic_context);
1372 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
1376 if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
1377 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
1381 if (type->type == MONO_TYPE_TYPEDBYREF) {
1382 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref at 0x%04x", ctx->ip_offset));
1386 check_unverifiable_type (ctx, type);
1391 /*operation result tables */
1393 static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
1394 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1395 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1396 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1397 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1398 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1399 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1402 static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
1403 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1404 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1405 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
1406 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1407 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
1408 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1411 static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
1412 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1413 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1414 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1415 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
1416 {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
1417 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1420 static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
1421 {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1422 {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1423 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1424 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1425 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1426 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1429 static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
1430 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1431 {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
1432 {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
1433 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1434 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1435 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1438 static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
1439 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1440 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1441 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1442 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1443 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
1444 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1447 static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
1448 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
1449 {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
1450 {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
1451 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
1452 {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
1453 {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
1458 dump_stack_value (ILStackDesc *value)
1460 printf ("[(%d)(%d)", value->type->type, value->stype);
1462 if (value->stype & CMMP_MASK)
1463 printf ("Controled Mutability MP: ");
1465 if (IS_MANAGED_POINTER (value->stype))
1466 printf ("Managed Pointer to: ");
1468 switch (UNMASK_TYPE (value->stype)) {
1470 printf ("invalid type]");
1478 case TYPE_NATIVE_INT:
1479 printf ("native int]");
1482 printf ("float64]");
1485 printf ("unmanaged pointer]");
1488 switch (value->type->type) {
1489 case MONO_TYPE_CLASS:
1490 case MONO_TYPE_VALUETYPE:
1491 printf ("complex] (%s)", value->type->data.klass->name);
1493 case MONO_TYPE_STRING:
1494 printf ("complex] (string)");
1496 case MONO_TYPE_OBJECT:
1497 printf ("complex] (object)");
1499 case MONO_TYPE_SZARRAY:
1500 printf ("complex] (%s [])", value->type->data.klass->name);
1502 case MONO_TYPE_ARRAY:
1503 printf ("complex] (%s [%d %d %d])",
1504 value->type->data.array->eklass->name,
1505 value->type->data.array->rank,
1506 value->type->data.array->numsizes,
1507 value->type->data.array->numlobounds);
1509 case MONO_TYPE_GENERICINST:
1510 printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
1513 printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1515 case MONO_TYPE_MVAR:
1516 printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
1519 printf ("unknown complex %d type]\n", value->type->type);
1520 g_assert_not_reached ();
1523 printf ("unknown stack %d type]\n", value->stype);
1524 g_assert_not_reached ();
1529 dump_stack_state (ILCodeDesc *state)
1533 printf ("(%d) ", state->size);
1534 for (i = 0; i < state->size; ++i)
1535 dump_stack_value (state->stack + i);
1540 dump_context (VerifyContext *ctx, int code_size)
1544 for (i = 0; i < code_size; ++i) {
1545 if (ctx->code [i].flags & IL_CODE_FLAG_SEEN) {
1546 printf ("opcode [%d]:\n\t", i);
1547 dump_stack_state (&ctx->code [i]);
1552 /*Returns TRUE if candidate array type can be assigned to target.
1553 *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
1556 is_array_type_compatible (MonoType *target, MonoType *candidate)
1559 MonoArrayType *left = target->data.array;
1560 MonoArrayType *right = candidate->data.array;
1562 g_assert (target->type == MONO_TYPE_ARRAY);
1563 g_assert (candidate->type == MONO_TYPE_ARRAY);
1566 if ((left->rank != right->rank) ||
1567 (left->numsizes != right->numsizes) ||
1568 (left->numlobounds != right->numlobounds))
1571 for (i = 0; i < left->numsizes; ++i)
1572 if (left->sizes [i] != right->sizes [i])
1575 for (i = 0; i < left->numlobounds; ++i)
1576 if (left->lobounds [i] != right->lobounds [i])
1579 return mono_class_is_assignable_from (left->eklass, right->eklass);
1583 get_stack_type (MonoType *type)
1586 int type_kind = type->type;
1588 mask = POINTER_MASK;
1589 /*TODO handle CMMP_MASK */
1592 switch (type_kind) {
1595 case MONO_TYPE_BOOLEAN:
1598 case MONO_TYPE_CHAR:
1601 return TYPE_I4 | mask;
1605 return TYPE_NATIVE_INT | mask;
1607 /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
1608 case MONO_TYPE_FNPTR:
1610 case MONO_TYPE_TYPEDBYREF:
1611 return TYPE_PTR | mask;
1613 case MONO_TYPE_CLASS:
1614 case MONO_TYPE_STRING:
1615 case MONO_TYPE_OBJECT:
1616 case MONO_TYPE_SZARRAY:
1617 case MONO_TYPE_ARRAY:
1618 case MONO_TYPE_GENERICINST:
1619 return TYPE_COMPLEX | mask;
1623 return TYPE_I8 | mask;
1627 return TYPE_R8 | mask;
1629 case MONO_TYPE_VALUETYPE:
1630 if (type->data.klass->enumtype) {
1631 type = type->data.klass->enum_basetype;
1632 type_kind = type->type;
1635 return TYPE_COMPLEX | mask;
1638 VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
1639 g_assert_not_reached ();
1644 /* convert MonoType to ILStackDesc format (stype) */
1646 set_stack_value (ILStackDesc *stack, MonoType *type, int take_addr)
1649 int type_kind = type->type;
1651 if (type->byref || take_addr)
1652 mask = POINTER_MASK;
1653 /* TODO handle CMMP_MASK */
1658 switch (type_kind) {
1661 case MONO_TYPE_BOOLEAN:
1664 case MONO_TYPE_CHAR:
1667 stack->stype = TYPE_I4 | mask;
1671 stack->stype = TYPE_NATIVE_INT | mask;
1674 /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
1675 case MONO_TYPE_FNPTR:
1677 case MONO_TYPE_TYPEDBYREF:
1678 stack->stype = TYPE_PTR | mask;
1681 case MONO_TYPE_CLASS:
1682 case MONO_TYPE_STRING:
1683 case MONO_TYPE_OBJECT:
1684 case MONO_TYPE_SZARRAY:
1685 case MONO_TYPE_ARRAY:
1687 case MONO_TYPE_GENERICINST:
1689 case MONO_TYPE_MVAR:
1690 stack->stype = TYPE_COMPLEX | mask;
1694 stack->stype = TYPE_I8 | mask;
1698 stack->stype = TYPE_R8 | mask;
1700 case MONO_TYPE_VALUETYPE:
1701 if (type->data.klass->enumtype) {
1702 type = type->data.klass->enum_basetype;
1703 type_kind = type->type;
1706 stack->stype = TYPE_COMPLEX | mask;
1710 VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
1711 g_assert_not_reached ();
1716 /* Generics validation stuff, should be moved to another metadata/? file */
1718 mono_is_generic_type_compatible (MonoType *target, MonoType *candidate)
1720 if (target->byref != candidate->byref)
1724 switch (target->type) {
1725 case MONO_TYPE_STRING:
1726 if (candidate->type == MONO_TYPE_STRING)
1730 case MONO_TYPE_CLASS:
1731 if (candidate->type != MONO_TYPE_CLASS)
1734 VERIFIER_DEBUG ( printf ("verifying type class %p %p\n", target->data.klass, candidate->data.klass); );
1735 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1737 case MONO_TYPE_OBJECT:
1738 return MONO_TYPE_IS_REFERENCE (candidate);
1740 case MONO_TYPE_SZARRAY:
1741 if (candidate->type != MONO_TYPE_SZARRAY)
1743 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1745 case MONO_TYPE_VALUETYPE:
1746 if (target->data.klass->enumtype) {
1747 target = target->data.klass->enum_basetype;
1750 if (candidate->type != MONO_TYPE_VALUETYPE)
1752 return candidate->data.klass == target->data.klass;
1755 case MONO_TYPE_ARRAY:
1756 if (candidate->type != MONO_TYPE_ARRAY)
1758 return is_array_type_compatible (target, candidate);
1761 VERIFIER_DEBUG ( printf ("unknown target type %d\n", target->type); );
1762 g_assert_not_reached ();
1770 mono_is_generic_instance_compatible (MonoGenericClass *target, MonoGenericClass *candidate, MonoGenericClass *root_candidate) {
1771 MonoGenericContainer *container;
1774 VERIFIER_DEBUG ( printf ("candidate container %p\n", candidate->container_class->generic_container); );
1775 if (target->container_class != candidate->container_class) {
1776 MonoType *param_class;
1777 MonoClass *cand_class;
1778 VERIFIER_DEBUG ( printf ("generic class != target\n"); );
1779 param_class = candidate->context.class_inst->type_argv [0];
1780 VERIFIER_DEBUG ( printf ("param 0 %d\n", param_class->type); );
1781 cand_class = candidate->container_class;
1783 /* We must check if it's an interface type*/
1784 if (MONO_CLASS_IS_INTERFACE (target->container_class)) {
1785 VERIFIER_DEBUG ( printf ("generic type is an interface\n"); );
1788 int iface_count = cand_class->interface_count;
1789 MonoClass **ifaces = cand_class->interfaces;
1791 VERIFIER_DEBUG ( printf ("type has %d interfaces\n", iface_count); );
1792 for (i = 0; i< iface_count; ++i) {
1793 MonoClass *ifc = ifaces[i];
1794 VERIFIER_DEBUG ( printf ("analysing %s\n", ifc->name); );
1795 if (ifc->generic_class) {
1796 VERIFIER_DEBUG ( printf ("interface has generic info\n"); );
1798 if (mono_is_generic_instance_compatible (target, ifc->generic_class, root_candidate)) {
1799 VERIFIER_DEBUG ( printf ("we got compatible stuff!\n"); );
1804 cand_class = cand_class->parent;
1805 } while (cand_class);
1807 VERIFIER_DEBUG ( printf ("don't implements an interface\n"); );
1810 VERIFIER_DEBUG ( printf ("verifying upper classes\n"); );
1812 cand_class = cand_class->parent;
1814 while (cand_class) {
1815 VERIFIER_DEBUG ( printf ("verifying parent class name %s\n", cand_class->name); );
1816 if (cand_class->generic_class) {
1817 VERIFIER_DEBUG ( printf ("super type has generic context\n"); );
1819 /* TODO break loop if target->container_class == cand_class->generic_class->container_class */
1820 return mono_is_generic_instance_compatible (target, cand_class->generic_class, root_candidate);
1822 VERIFIER_DEBUG ( printf ("super class has no generic context\n"); );
1823 cand_class = cand_class->parent;
1829 /* now we verify if the instantiations are compatible*/
1830 if (target->context.class_inst == candidate->context.class_inst) {
1831 VERIFIER_DEBUG ( printf ("generic types are compatible, both have the same instantiation\n"); );
1835 if (target->context.class_inst->type_argc != candidate->context.class_inst->type_argc) {
1836 VERIFIER_DEBUG ( printf ("generic instantiations with different arg counts\n"); );
1840 //verify if open instance -- none should be
1842 container = target->container_class->generic_container;
1844 for (i = 0; i < container->type_argc; ++i) {
1845 MonoGenericParam *param = container->type_params + i;
1846 MonoType *target_type = target->context.class_inst->type_argv [i];
1847 MonoType *candidate_type = candidate->context.class_inst->type_argv [i];
1848 /* We resolve TYPE_VAR types before proceeding */
1850 if (candidate_type->type == MONO_TYPE_VAR) {
1851 MonoGenericParam *var_param = candidate_type->data.generic_param;
1852 candidate_type = root_candidate->context.class_inst->type_argv [var_param->num];
1855 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK) == 0) {
1856 VERIFIER_DEBUG ( printf ("generic type have no variance flag, checking each type %d %d \n",target_type->type, candidate_type->type); );
1858 if (!mono_metadata_type_equal (target_type, candidate_type))
1861 VERIFIER_DEBUG ( printf ("generic type has variance flag, need to perform deeper check\n"); );
1862 /* first we check if they are the same kind */
1863 /* byref generic params are forbiden, but better safe than sorry.*/
1865 if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) == GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) {
1866 if (!mono_is_generic_type_compatible (target_type, candidate_type))
1868 /* the attribute must be contravariant */
1869 } else if (!mono_is_generic_type_compatible (candidate_type, target_type))
1878 /*Verify if type 'candidate' can be stored in type 'target'.
1880 * If strict, check for the underlying type and not the verification stack types
1883 verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
1885 #define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
1886 #define IS_ONE_OF2(T, A, B) (T == A || T == B)
1888 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); );
1890 /*only one is byref */
1891 if (candidate->byref ^ target->byref) {
1892 /* converting from native int to byref*/
1893 if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
1894 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
1899 strict |= target->byref;
1902 switch (target->type) {
1905 case MONO_TYPE_BOOLEAN:
1907 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
1910 case MONO_TYPE_CHAR:
1912 return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
1914 case MONO_TYPE_U4: {
1915 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1916 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1918 return is_native_int || is_int4;
1919 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1924 return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
1929 return candidate->type == target->type;
1930 return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
1934 gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
1935 gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
1937 return is_native_int || is_int4;
1938 return is_native_int || get_stack_type (candidate) == TYPE_I4;
1942 if (candidate->type != MONO_TYPE_PTR)
1944 /* check the underlying type */
1945 return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
1947 case MONO_TYPE_FNPTR: {
1948 MonoMethodSignature *left, *right;
1949 if (candidate->type != MONO_TYPE_FNPTR)
1952 left = mono_type_get_signature (target);
1953 right = mono_type_get_signature (candidate);
1954 return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
1957 case MONO_TYPE_GENERICINST: {
1958 MonoGenericClass *left;
1959 MonoGenericClass *right;
1960 if (candidate->type != MONO_TYPE_GENERICINST)
1962 left = target->data.generic_class;
1963 right = candidate->data.generic_class;
1965 return mono_is_generic_instance_compatible (left, right, right);
1968 case MONO_TYPE_STRING:
1969 return candidate->type == MONO_TYPE_STRING;
1971 case MONO_TYPE_CLASS:
1972 return mono_class_is_assignable_from (target->data.klass, mono_class_from_mono_type (candidate));
1974 case MONO_TYPE_OBJECT:
1975 return MONO_TYPE_IS_REFERENCE (candidate);
1977 case MONO_TYPE_SZARRAY: {
1980 if (candidate->type != MONO_TYPE_SZARRAY)
1983 left = target->data.klass;
1984 right = candidate->data.klass;
1985 return mono_class_is_assignable_from(left, right);
1988 case MONO_TYPE_ARRAY:
1989 if (candidate->type != MONO_TYPE_ARRAY)
1991 return is_array_type_compatible (target, candidate);
1993 //TODO verify aditional checks that needs to be done
1994 case MONO_TYPE_TYPEDBYREF:
1995 return candidate->type == MONO_TYPE_TYPEDBYREF;
1997 case MONO_TYPE_VALUETYPE:
1998 if (target->data.klass->enumtype) {
1999 target = target->data.klass->enum_basetype;
2002 if (candidate->type != MONO_TYPE_VALUETYPE)
2004 return target->data.klass == candidate->data.klass;
2008 if (candidate->type != MONO_TYPE_VAR)
2010 return candidate->data.generic_param->num == target->data.generic_param->num;
2012 case MONO_TYPE_MVAR:
2013 if (candidate->type != MONO_TYPE_MVAR)
2015 return candidate->data.generic_param->num == target->data.generic_param->num;
2018 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2019 g_assert_not_reached ();
2028 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2030 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2035 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2037 return verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (stack), FALSE);
2040 /* implement the opcode checks*/
2042 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2044 if (arg >= ctx->max_args) {
2046 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2048 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2049 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2050 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2052 } else if (check_overflow (ctx)) {
2053 /*We must let the value be pushed, otherwise we would get an underflow error*/
2054 check_unverifiable_type (ctx, ctx->params [arg]);
2055 if (ctx->params [arg]->byref && take_addr)
2056 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2057 set_stack_value (stack_push (ctx), ctx->params [arg], take_addr);
2062 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2064 if (arg >= ctx->num_locals) {
2065 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2066 } else if (check_overflow (ctx)) {
2067 /*We must let the value be pushed, otherwise we would get an underflow error*/
2068 check_unverifiable_type (ctx, ctx->locals [arg]);
2069 if (ctx->locals [arg]->byref && take_addr)
2070 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2072 set_stack_value (stack_push (ctx), ctx->locals [arg], take_addr);
2077 store_arg (VerifyContext *ctx, guint32 arg)
2081 if (arg >= ctx->max_args) {
2082 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2083 check_underflow (ctx, 1);
2088 if (check_underflow (ctx, 1)) {
2089 value = stack_pop (ctx);
2090 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2091 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2097 store_local (VerifyContext *ctx, guint32 arg)
2100 if (arg >= ctx->num_locals) {
2101 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2105 /*TODO verify definite assigment */
2106 if (check_underflow (ctx, 1)) {
2107 value = stack_pop(ctx);
2108 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2109 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));
2115 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2118 int idxa, idxb, complexMerge = 0;
2121 if (!check_underflow (ctx, 2))
2123 a = stack_get (ctx, 1);
2124 b = stack_top (ctx);
2127 if (IS_MANAGED_POINTER (idxa)) {
2133 if (IS_MANAGED_POINTER (idxb)) {
2140 res = table [idxa][idxb];
2142 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2143 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2146 if (res == TYPE_INV) {
2147 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)]));
2151 if (res & NON_VERIFIABLE_RESULT) {
2152 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)",
2153 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)]));
2155 res = res & ~NON_VERIFIABLE_RESULT;
2158 if (complexMerge && res == TYPE_PTR) {
2159 if (complexMerge == 1)
2160 copy_stack_value (stack_top (ctx), a);
2161 else if (complexMerge == 2)
2162 copy_stack_value (stack_top (ctx), b);
2164 * There is no need to merge the type of two pointers.
2165 * The only valid operation is subtraction, that returns a native
2166 * int as result and can be used with any 2 pointer kinds.
2167 * This is valid acording to Patition III 1.1.4
2170 stack_top (ctx)->stype = res;
2176 do_boolean_branch_op (VerifyContext *ctx, int delta)
2178 int target = ctx->ip_offset + delta;
2181 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2183 if (target < 0 || target >= ctx->code_size) {
2184 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2188 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2189 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2193 ctx->target = target;
2195 if (!check_underflow (ctx, 1))
2198 top = stack_pop (ctx);
2199 if (!is_valid_bool_arg (top))
2200 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));
2202 check_unmanaged_pointer (ctx, top);
2207 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2212 int target = ctx->ip_offset + delta;
2214 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2216 if (target < 0 || target >= ctx->code_size) {
2217 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2221 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2222 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2226 ctx->target = target;
2228 if (!check_underflow (ctx, 2))
2231 b = stack_pop (ctx);
2232 a = stack_pop (ctx);
2235 if (IS_MANAGED_POINTER (idxa))
2239 if (IS_MANAGED_POINTER (idxb))
2244 res = table [idxa][idxb];
2246 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2247 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2249 if (res == TYPE_INV) {
2250 ADD_VERIFY_ERROR (ctx,
2251 g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x",
2252 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2253 } else if (res & NON_VERIFIABLE_RESULT) {
2254 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x",
2255 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2256 res = res & ~NON_VERIFIABLE_RESULT;
2261 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX])
2267 if (!check_underflow (ctx, 2))
2269 b = stack_pop (ctx);
2270 a = stack_pop (ctx);
2273 if (IS_MANAGED_POINTER (idxa))
2277 if (IS_MANAGED_POINTER (idxb))
2282 res = table [idxa][idxb];
2284 if(res == TYPE_INV) {
2285 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));
2286 } else if (res & NON_VERIFIABLE_RESULT) {
2287 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x",
2288 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2289 res = res & ~NON_VERIFIABLE_RESULT;
2291 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2295 do_ret (VerifyContext *ctx)
2297 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2298 if (ctx->signature->ret->type != MONO_TYPE_VOID) {
2300 if (!check_underflow (ctx, 1))
2303 top = stack_pop(ctx);
2305 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2306 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2311 if (ctx->eval.size > 0) {
2312 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2314 if (in_any_block (ctx->header, ctx->ip_offset))
2315 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2318 /* FIXME: we could just load the signature instead of the whole MonoMethod
2319 * TODO handle vararg calls
2320 * TODO handle non virt calls to non-final virtual calls (from the verifiability clause in page 52 of partition 3)
2321 * TODO handle abstract calls
2322 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2323 * TODO handle call invoking virtual methods (only allowed to invoke super)
2326 do_invoke_method (VerifyContext *ctx, int method_token)
2329 MonoMethodSignature *sig;
2331 MonoMethod *method = mono_get_method_full (ctx->image, method_token, NULL, ctx->generic_context);
2334 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method 0x%08x not found at 0x%04x", method_token, ctx->ip_offset));
2338 if (!(sig = mono_method_signature (method)))
2339 sig = mono_method_get_signature (method, ctx->image, method_token);
2341 param_count = sig->param_count + sig->hasthis;
2342 if (!check_underflow (ctx, param_count))
2345 for (i = sig->param_count - 1; i >= 0; --i) {
2346 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2347 value = stack_pop (ctx);
2348 if (!verify_stack_type_compatibility (ctx, sig->params[i], value))
2349 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2353 MonoType * type = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2355 VERIFIER_DEBUG ( printf ("verifying this argument\n"); );
2356 value = stack_pop (ctx);
2357 if (!verify_stack_type_compatibility (ctx, type, value)) {
2358 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature ret at 0x%04x", ctx->ip_offset));
2362 if (!mono_method_can_access_method (ctx->method, method))
2363 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset));
2365 if (sig->ret->type != MONO_TYPE_VOID) {
2366 if (check_overflow (ctx))
2367 set_stack_value (stack_push (ctx), sig->ret, FALSE);
2370 if (sig->ret->type == MONO_TYPE_TYPEDBYREF
2372 || (sig->ret->type == MONO_TYPE_VALUETYPE && !strcmp ("System", sig->ret->data.klass->name_space) && !strcmp ("ArgIterator", sig->ret->data.klass->name)))
2373 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns typedbyref, byref or ArgIterator at 0x%04x", ctx->ip_offset));
2377 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2379 MonoClassField *field;
2382 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2384 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2388 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2389 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
2392 /*taking the address of initonly field only works from the static constructor */
2393 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2394 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
2395 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2397 if (!mono_method_can_access_field (ctx->method, field))
2398 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2400 set_stack_value (stack_push (ctx), field->type, take_addr);
2404 do_store_static_field (VerifyContext *ctx, int token) {
2405 MonoClassField *field;
2409 if (!check_underflow (ctx, 1))
2412 value = stack_pop (ctx);
2414 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2416 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2420 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2421 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
2425 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2426 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
2430 if (!mono_method_can_access_field (ctx->method, field))
2431 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2433 if (!verify_stack_type_compatibility (ctx, field->type, value))
2434 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));
2438 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field)
2440 MonoClassField *field;
2443 /*must be one of: object type, managed pointer, unmanaged pointer (native int) or an instance of a value type */
2444 if (!((obj->stype == TYPE_COMPLEX)
2445 /* the managed reference must be to an object or value type */
2446 || (( IS_MANAGED_POINTER (obj->stype)) && (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX))
2447 || (obj->stype == TYPE_NATIVE_INT)
2448 || (obj->stype == TYPE_PTR)
2449 || (obj->stype == TYPE_COMPLEX))) {
2450 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));
2453 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2455 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2461 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2462 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
2465 g_assert (obj->type);
2467 /*The value on the stack must be a subclass of the defining type of the field*/
2468 /* we need to check if we can load the field from the stack value*/
2469 if (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX) {
2470 MonoType *type = obj->type->byref ? &field->parent->this_arg : &field->parent->byval_arg;
2472 if (!verify_type_compatibility (ctx, type, obj->type)) {
2473 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
2476 if (!mono_method_can_access_field (ctx->method, field))
2477 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2480 if (!mono_method_can_access_field (ctx->method, field))
2481 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2483 if (obj->stype == TYPE_NATIVE_INT)
2484 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
2486 check_unmanaged_pointer (ctx, obj);
2491 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
2494 MonoClassField *field;
2496 if (!check_underflow (ctx, 1))
2498 obj = stack_pop (ctx);
2500 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2503 if (take_addr && field->parent->valuetype && !IS_MANAGED_POINTER (obj->stype))
2504 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
2506 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2507 !(field->parent == ctx->method->klass && (ctx->method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && !strcmp (".ctor", ctx->method->name)))
2508 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2510 set_stack_value (stack_push (ctx), field->type, take_addr);
2514 do_store_field (VerifyContext *ctx, int token)
2516 ILStackDesc *value, *obj;
2517 MonoClassField *field;
2519 if (!check_underflow (ctx, 2))
2522 value = stack_pop (ctx);
2523 obj = stack_pop (ctx);
2525 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2528 if (!verify_stack_type_compatibility (ctx, field->type, value))
2529 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));
2532 /*TODO proper handle for Nullable<T>*/
2534 do_box_value (VerifyContext *ctx, int klass_token)
2537 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2542 if (!check_underflow (ctx, 1))
2545 value = stack_top (ctx);
2546 /*box is a nop for reference types*/
2547 if (value->stype == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type))
2550 value = stack_pop (ctx);
2552 if (!verify_stack_type_compatibility (ctx, type, value))
2553 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
2555 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (mono_defaults.object_class));
2559 do_unbox_value (VerifyContext *ctx, int klass_token)
2562 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2567 if (!check_underflow (ctx, 1))
2570 value = stack_pop (ctx);
2572 if (value->stype != TYPE_COMPLEX || value->type->type != MONO_TYPE_OBJECT)
2573 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));
2575 //TODO Pushed managed pointer is haver controled mutability (CMMP)
2576 set_stack_value (stack_push (ctx), mono_type_get_type_byref (type), FALSE);
2580 do_unary_math_op (VerifyContext *ctx, int op)
2583 if (!check_underflow (ctx, 1))
2585 value = stack_top (ctx);
2586 switch(value->stype) {
2589 case TYPE_NATIVE_INT:
2594 case TYPE_COMPLEX: /*only enums are ok*/
2595 if (value->type->type == MONO_TYPE_VALUETYPE && value->type->data.klass->enumtype)
2598 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
2603 do_conversion (VerifyContext *ctx, int kind)
2606 if (!check_underflow (ctx, 1))
2608 value = stack_pop (ctx);
2610 switch(value->stype) {
2613 case TYPE_NATIVE_INT:
2617 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for conversion operation at 0x%04x", ctx->ip_offset));
2622 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2625 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
2628 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
2630 case TYPE_NATIVE_INT:
2631 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
2634 g_error ("unknown type %02x in conversion", kind);
2640 do_load_token (VerifyContext *ctx, int token)
2643 MonoClass *handle_class;
2644 if (!check_overflow (ctx))
2646 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
2648 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
2651 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
2655 do_ldobj_value (VerifyContext *ctx, int token)
2658 MonoType *type = get_boxable_mono_type (ctx, token);
2663 if (!check_underflow (ctx, 1))
2666 value = stack_pop (ctx);
2667 if (!IS_MANAGED_POINTER (value->stype)
2668 && value->stype != TYPE_NATIVE_INT
2669 && !(value->stype == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
2670 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2674 if (value->stype == TYPE_NATIVE_INT)
2675 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
2677 /*We have a byval on the stack, but the comparison must be strict. */
2678 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
2679 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
2681 set_stack_value (stack_push (ctx), type, FALSE);
2684 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_PUBLIC)
2685 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
2686 /* TODO implement delegate verification */
2688 do_newobj (VerifyContext *ctx, int token)
2692 MonoMethodSignature *sig;
2693 MonoMethod *method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
2695 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ctx->ip_offset));
2699 if ((method->flags & CTOR_REQUIRED_FLAGS) != CTOR_REQUIRED_FLAGS
2700 || (method->flags & CTOR_INVALID_FLAGS) != 0
2701 || strcmp (".ctor", method->name)) {
2702 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
2706 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
2707 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
2709 sig = mono_method_signature (method);
2710 if (!check_underflow (ctx, sig->param_count))
2713 for (i = sig->param_count - 1; i >= 0; --i) {
2714 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
2715 value = stack_pop (ctx);
2716 if (!verify_stack_type_compatibility (ctx, sig->params [i], value))
2717 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2720 if (check_overflow (ctx))
2721 set_stack_value (stack_push (ctx), &method->klass->byval_arg, FALSE);
2725 get_indirect_op_mono_type (int opcode) {
2730 return &mono_defaults.sbyte_class->byval_arg;
2735 return &mono_defaults.int16_class->byval_arg;
2740 return &mono_defaults.int32_class->byval_arg;
2744 return &mono_defaults.int64_class->byval_arg;
2748 return &mono_defaults.single_class->byval_arg;
2752 return &mono_defaults.double_class->byval_arg;
2756 return &mono_defaults.int_class->byval_arg;
2760 return &mono_defaults.object_class->byval_arg;
2763 g_error ("unknown opcode %02x in get_indirect_op_mono_type ", opcode);
2769 do_load_indirect (VerifyContext *ctx, int opcode)
2772 if (!check_underflow (ctx, 1))
2775 value = stack_pop (ctx);
2776 if (!IS_MANAGED_POINTER (value->stype)) {
2777 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
2778 set_stack_value (stack_push (ctx), get_indirect_op_mono_type (opcode), FALSE);
2782 if (opcode == CEE_LDIND_REF) {
2783 if (UNMASK_TYPE (value->stype) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
2784 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
2785 set_stack_value (stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
2787 if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (opcode), mono_type_get_type_byval (value->type), TRUE))
2788 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2789 set_stack_value (stack_push (ctx), get_indirect_op_mono_type (opcode), FALSE);
2794 do_store_indirect (VerifyContext *ctx, int opcode)
2796 ILStackDesc *addr, *val;
2797 if (!check_underflow (ctx, 2))
2800 val = stack_pop (ctx);
2801 addr = stack_pop (ctx);
2803 check_unmanaged_pointer (ctx, addr);
2805 if (!IS_MANAGED_POINTER (addr->stype) && addr->stype != TYPE_PTR) {
2806 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
2810 if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (opcode), mono_type_get_type_byval (addr->type), TRUE))
2811 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2813 if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (opcode), mono_type_get_type_byval (val->type), FALSE))
2814 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2818 do_newarr (VerifyContext *ctx, int token)
2821 MonoType *type = get_boxable_mono_type (ctx, token);
2826 if (!check_underflow (ctx, 1))
2829 value = stack_pop (ctx);
2830 if (value->stype != TYPE_I4 && value->stype != TYPE_NATIVE_INT)
2831 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));
2833 set_stack_value (stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
2837 do_ldlen (VerifyContext *ctx)
2841 if (!check_underflow (ctx, 1))
2844 value = stack_pop (ctx);
2846 if (value->stype != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
2847 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
2849 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
2852 /*Merge the stacks and perform compat checks*/
2854 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start)
2858 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
2859 stack_init (ctx, to);
2862 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
2865 stack_copy (&ctx->eval, to);
2867 } else if (to->flags == IL_CODE_FLAG_NOT_PROCESSED) {
2868 stack_copy (to, from);
2871 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
2873 if (from->size != to->size) {
2874 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d\n", from->size, to->size); );
2875 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d)", from->size, to->size));
2879 for (i = 0; i < from->size; ++i) {
2880 ILStackDesc *from_slot = from->stack + i;
2881 ILStackDesc *to_slot = to->stack + i;
2883 if (!verify_type_compatibility (ctx, mono_type_from_stack_slot (to_slot), mono_type_from_stack_slot (from_slot))) {
2884 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, types not compatible at 0x%04x", ctx->ip_offset));
2888 /*TODO we need to choose the base class for merging reference types*/
2889 copy_stack_value (to_slot, from_slot);
2893 to->flags = IL_CODE_FLAG_SEEN;
2898 * FIXME: need to distinguish between valid and verifiable.
2899 * Need to keep track of types on the stack.
2900 * Verify types for opcodes.
2903 mono_method_verify (MonoMethod *method, int level)
2905 const unsigned char *ip;
2906 const unsigned char *end;
2907 const unsigned char *target = NULL; /* branch target */
2908 int i, n, need_merge = 0, start = 0;
2909 guint token, ip_offset = 0, prefix = 0;
2910 MonoGenericContext *generic_context = NULL;
2913 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name, method->klass->name, method->name); );
2915 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2916 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
2920 memset (&ctx, 0, sizeof (VerifyContext));
2922 ctx.signature = mono_method_signature (method);
2923 if (!ctx.signature) {
2924 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
2927 ctx.header = mono_method_get_header (method);
2929 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
2932 ctx.method = method;
2933 ip = ctx.header->code;
2934 end = ip + ctx.header->code_size;
2935 ctx.image = image = method->klass->image;
2938 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
2939 ctx.max_stack = ctx.header->max_stack;
2940 ctx.verifiable = ctx.valid = 1;
2942 ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
2943 ctx.code_size = ctx.header->code_size;
2945 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
2948 ctx.num_locals = ctx.header->num_locals;
2949 ctx.locals = ctx.header->locals;
2952 if (ctx.signature->hasthis) {
2953 ctx.params = g_new0 (MonoType*, ctx.max_args);
2954 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2955 memcpy (ctx.params + 1, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
2957 ctx.params = ctx.signature->params;
2960 if (ctx.signature->is_inflated)
2961 ctx.generic_context = generic_context = mono_method_get_context (method);
2963 stack_init (&ctx, &ctx.eval);
2966 for (i = 0; i < ctx.header->num_clauses; ++i) {
2967 MonoExceptionClause *clause = ctx.header->clauses + i;
2968 /* catch blocks and filter have the exception on the stack. */
2969 /* must check boundaries for handler_offset and handler_start < handler_start*/
2970 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2971 ILCodeDesc *code = ctx.code + clause->handler_offset;
2972 stack_init (&ctx, code);
2973 code->stack [0].stype = TYPE_COMPLEX;
2974 code->stack [0].type = &clause->data.catch_class->byval_arg;
2976 code->flags = IL_CODE_FLAG_SEEN;
2978 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2979 ILCodeDesc *code = ctx.code + clause->data.filter_offset;
2980 stack_init (&ctx, code);
2981 code->stack [0].stype = TYPE_COMPLEX;
2982 code->stack [0].type = &mono_defaults.exception_class->byval_arg;
2984 code->flags = IL_CODE_FLAG_SEEN;
2988 while (ip < end && ctx.valid) {
2989 ctx.ip_offset = ip_offset = ip - ctx.header->code;
2991 /*TODO id stack merge fails, we break, should't we - or only on errors??
2992 TODO verify need_merge
2995 VERIFIER_DEBUG ( printf ("extra merge needed! %d \n", ctx.target); );
2996 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE);
2999 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start);
3002 #ifdef MONO_VERIFIER_DEBUG
3005 discode = mono_disasm_code_one (NULL, method, ip, NULL);
3006 discode [strlen (discode) - 1] = 0; /* no \n */
3007 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
3010 dump_stack_state (&ctx.code [ip_offset]);
3023 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
3029 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
3034 do_binop (&ctx, *ip, add_table);
3039 do_binop (&ctx, *ip, sub_table);
3046 do_binop (&ctx, *ip, bin_op_table);
3055 do_binop (&ctx, *ip, int_bin_op_table);
3062 do_binop (&ctx, *ip, shift_op_table);
3067 if (!check_underflow (&ctx, 1))
3083 /*TODO support definite assignment verification? */
3084 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
3092 store_local (&ctx, *ip - CEE_STLOC_0);
3097 store_local (&ctx, ip [1]);
3102 store_arg (&ctx, ip [1]);
3116 if (check_overflow (&ctx))
3117 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3122 if (check_overflow (&ctx))
3123 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3128 if (check_overflow (&ctx))
3129 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
3134 if (check_overflow (&ctx))
3135 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3140 if (check_overflow (&ctx))
3141 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3146 if (check_overflow (&ctx))
3147 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3152 if (check_overflow (&ctx))
3153 stack_push_val (&ctx,TYPE_COMPLEX, &mono_defaults.object_class->byval_arg);
3159 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
3172 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
3179 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
3192 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
3199 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
3203 /* FIXME: warn/error instead? */
3210 if (!check_underflow (&ctx, 1))
3212 if (!check_overflow (&ctx))
3214 top = stack_push (&ctx);
3215 copy_stack_value (top, stack_get (&ctx, 1));
3222 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
3223 token = read32 (ip + 1);
3224 if (in_any_block (ctx.header, ip_offset))
3225 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
3227 * FIXME: check signature, retval, arguments etc.
3233 do_invoke_method (&ctx, read32 (ip + 1));
3238 token = read32 (ip + 1);
3240 * FIXME: check signature, retval, arguments etc.
3245 target = ip + (signed char)ip [1] + 2;
3246 if (target >= end || target < ctx.header->code)
3247 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3248 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3249 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3256 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
3262 target = ip + (gint32)read32 (ip + 1) + 5;
3263 if (target >= end || target < ctx.header->code)
3264 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3265 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3266 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3273 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
3279 n = read32 (ip + 1);
3280 target = ip + sizeof (guint32) * n;
3281 /* FIXME: check that ip is in range (and within the same exception block) */
3282 for (i = 0; i < n; ++i)
3283 if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < ctx.header->code)
3284 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3285 if (!check_underflow (&ctx, 1))
3287 if (stack_pop (&ctx)->stype != TYPE_I4)
3288 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ip_offset));
3289 ip += 5 + sizeof (guint32) * n;
3303 do_load_indirect (&ctx, *ip);
3315 do_store_indirect (&ctx, *ip);
3321 do_unary_math_op (&ctx, *ip);
3325 //TODO: implement proper typecheck
3332 do_conversion (&ctx, TYPE_I4);
3338 do_conversion (&ctx, TYPE_I8);
3345 do_conversion (&ctx, TYPE_R8);
3351 do_conversion (&ctx, TYPE_NATIVE_INT);
3356 token = read32 (ip + 1);
3357 if (!check_underflow (&ctx, 2))
3364 do_ldobj_value (&ctx, read32 (ip + 1));
3369 /*TODO verify if token is a valid string literal*/
3370 token = read32 (ip + 1);
3371 if (check_overflow (&ctx))
3372 stack_push_val (&ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
3377 do_newobj (&ctx, read32 (ip + 1));
3383 token = read32 (ip + 1);
3384 if (!check_underflow (&ctx, 1))
3390 ++ip; /* warn, error ? */
3393 do_unbox_value (&ctx, read32 (ip + 1));
3397 if (!check_underflow (&ctx, 1))
3406 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
3412 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
3417 do_store_field (&ctx, read32 (ip + 1));
3422 do_store_static_field (&ctx, read32 (ip + 1));
3426 if (!check_underflow (&ctx, 2))
3429 token = read32 (ip + 1);
3433 case CEE_CONV_OVF_I1_UN:
3434 case CEE_CONV_OVF_I2_UN:
3435 case CEE_CONV_OVF_I4_UN:
3436 case CEE_CONV_OVF_U1_UN:
3437 case CEE_CONV_OVF_U2_UN:
3438 case CEE_CONV_OVF_U4_UN:
3439 do_conversion (&ctx, TYPE_I4);
3443 case CEE_CONV_OVF_I8_UN:
3444 case CEE_CONV_OVF_U8_UN:
3445 do_conversion (&ctx, TYPE_I8);
3449 case CEE_CONV_OVF_I_UN:
3450 case CEE_CONV_OVF_U_UN:
3451 do_conversion (&ctx, TYPE_NATIVE_INT);
3456 do_box_value (&ctx, read32 (ip + 1));
3461 do_newarr (&ctx, read32 (ip + 1));
3471 if (check_underflow (&ctx, 2))
3474 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3475 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelema at 0x%04x", ip_offset));
3476 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3477 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
3478 stack_top (&ctx)->stype = TYPE_COMPLEX;
3479 token = read32 (ip + 1);
3492 case CEE_LDELEM_REF:
3493 if (!check_underflow (&ctx, 2))
3496 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3497 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelem at 0x%04x", ip_offset));
3498 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3499 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
3500 stack_top (&ctx)->stype = ldelem_type [*ip - CEE_LDELEM_I1];
3510 case CEE_STELEM_REF:
3511 if (!check_underflow (&ctx, 3))
3516 case CEE_LDELEM_ANY:
3517 case CEE_STELEM_ANY:
3532 ++ip; /* warn, error ? */
3535 case CEE_CONV_OVF_I1:
3536 case CEE_CONV_OVF_U1:
3537 case CEE_CONV_OVF_I2:
3538 case CEE_CONV_OVF_U2:
3539 case CEE_CONV_OVF_I4:
3540 case CEE_CONV_OVF_U4:
3541 do_conversion (&ctx, TYPE_I4);
3545 case CEE_CONV_OVF_I8:
3546 case CEE_CONV_OVF_U8:
3547 do_conversion (&ctx, TYPE_I8);
3551 case CEE_CONV_OVF_I:
3552 case CEE_CONV_OVF_U:
3553 do_conversion (&ctx, TYPE_NATIVE_INT);
3564 ++ip; /* warn, error ? */
3567 if (!check_underflow (&ctx, 1))
3572 if (!check_underflow (&ctx, 1))
3578 ++ip; /* warn, error ? */
3581 if (!check_underflow (&ctx, 1))
3583 token = read32 (ip + 1);
3595 ++ip; /* warn, error ? */
3598 do_load_token (&ctx, read32 (ip + 1));
3603 case CEE_ADD_OVF_UN:
3605 case CEE_MUL_OVF_UN:
3607 case CEE_SUB_OVF_UN:
3608 if (!check_underflow (&ctx, 2))
3613 case CEE_ENDFINALLY:
3618 target = ip + (gint32)read32(ip + 1) + 5;
3619 if (target >= end || target < ctx.header->code)
3620 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3621 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3622 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3627 target = ip + (signed char)ip [1] + 2;
3628 if (target >= end || target < ctx.header->code)
3629 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3630 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3631 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3673 store_local (&ctx, read16 (ip + 1));
3678 do_cmp_op (&ctx, cmp_br_eq_op);
3686 do_cmp_op (&ctx, cmp_br_op);
3691 store_arg (&ctx, read16 (ip + 1) );
3697 check_overflow (&ctx);
3700 if (!check_overflow (&ctx))
3702 token = read32 (ip + 1);
3704 stack_top (&ctx)->stype = TYPE_PTR;
3708 if (!check_underflow (&ctx, 1))
3710 token = read32 (ip + 1);
3712 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3713 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ip_offset));
3714 stack_top (&ctx)->stype = TYPE_PTR;
3722 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
3728 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
3733 if (ctx.eval.size != 1)
3734 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ip_offset));
3735 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3736 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to localloc at 0x%04x", ip_offset));
3737 stack_top (&ctx)->stype = TYPE_COMPLEX;
3744 if (ctx.eval.size != 1)
3745 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Stack must have only filter result in endfilter at 0x%04x", ip_offset));
3748 case CEE_UNALIGNED_:
3749 prefix |= PREFIX_UNALIGNED;
3753 prefix |= PREFIX_VOLATILE;
3757 prefix |= PREFIX_TAIL;
3759 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
3760 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
3763 if (!check_underflow (&ctx, 1))
3765 token = read32 (ip + 1);
3769 case CEE_CONSTRAINED_:
3770 token = read32 (ip + 1);
3774 if (!check_underflow (&ctx, 3))
3779 if (!check_underflow (&ctx, 3))
3793 if (!check_overflow (&ctx))
3795 token = read32 (ip + 1);
3797 stack_top (&ctx)->type = &mono_defaults.uint32_class->byval_arg;
3798 stack_top (&ctx)->stype = TYPE_I4;
3801 case CEE_REFANYTYPE:
3802 if (!check_underflow (&ctx, 1))
3816 * if ip != end we overflowed: mark as error.
3818 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
3819 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
3825 for (i = 0; i < ctx.header->code_size; ++i) {
3826 if (ctx.code [i].stack)
3827 g_free (ctx.code [i].stack);
3832 g_free (ctx.eval.stack);
3835 if (ctx.signature->hasthis)
3836 g_free (ctx.params);
3848 const FieldDesc *fields;
3851 static const FieldDesc
3852 typebuilder_fields[] = {
3853 {"tname", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, name)},
3854 {"nspace", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, nspace)},
3855 {"parent", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, parent)},
3856 {"interfaces", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, interfaces)},
3857 {"methods", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, methods)},
3858 {"properties", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, properties)},
3859 {"fields", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, fields)},
3860 {"attrs", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, attrs)},
3861 {"table_idx", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, table_idx)},
3865 static const FieldDesc
3866 modulebuilder_fields[] = {
3867 {"types", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, types)},
3868 {"cattrs", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, cattrs)},
3869 {"guid", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, guid)},
3870 {"table_idx", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, table_idx)},
3874 static const FieldDesc
3875 assemblybuilder_fields[] = {
3876 {"entry_point", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, entry_point)},
3877 {"modules", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, modules)},
3878 {"name", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, name)},
3879 {"resources", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, resources)},
3880 {"version", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, version)},
3881 {"culture", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, culture)},
3885 static const FieldDesc
3886 ctorbuilder_fields[] = {
3887 {"ilgen", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, ilgen)},
3888 {"parameters", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, parameters)},
3889 {"attrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, attrs)},
3890 {"iattrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, iattrs)},
3891 {"table_idx", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, table_idx)},
3892 {"call_conv", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, call_conv)},
3893 {"type", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, type)},
3897 static const FieldDesc
3898 methodbuilder_fields[] = {
3899 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, mhandle)},
3900 {"rtype", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, rtype)},
3901 {"parameters", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, parameters)},
3902 {"attrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, attrs)},
3903 {"iattrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, iattrs)},
3904 {"name", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, name)},
3905 {"table_idx", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, table_idx)},
3906 {"code", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, code)},
3907 {"ilgen", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, ilgen)},
3908 {"type", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, type)},
3909 {"pinfo", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, pinfo)},
3910 {"pi_dll", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dll)},
3911 {"pi_entry", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dllentry)},
3912 {"charset", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, charset)},
3913 {"extra_flags", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, extra_flags)},
3914 {"native_cc", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, native_cc)},
3915 {"call_conv", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, call_conv)},
3919 static const FieldDesc
3920 fieldbuilder_fields[] = {
3921 {"attrs", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, attrs)},
3922 {"type", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, type)},
3923 {"name", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, name)},
3924 {"def_value", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, def_value)},
3925 {"offset", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, offset)},
3926 {"table_idx", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, table_idx)},
3930 static const FieldDesc
3931 propertybuilder_fields[] = {
3932 {"attrs", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, attrs)},
3933 {"name", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, name)},
3934 {"type", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, type)},
3935 {"parameters", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, parameters)},
3936 {"def_value", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, def_value)},
3937 {"set_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, set_method)},
3938 {"get_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, get_method)},
3939 {"table_idx", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, table_idx)},
3943 static const FieldDesc
3944 ilgenerator_fields[] = {
3945 {"code", G_STRUCT_OFFSET (MonoReflectionILGen, code)},
3946 {"code_len", G_STRUCT_OFFSET (MonoReflectionILGen, code_len)},
3947 {"max_stack", G_STRUCT_OFFSET (MonoReflectionILGen, max_stack)},
3948 {"cur_stack", G_STRUCT_OFFSET (MonoReflectionILGen, cur_stack)},
3949 {"locals", G_STRUCT_OFFSET (MonoReflectionILGen, locals)},
3950 {"ex_handlers", G_STRUCT_OFFSET (MonoReflectionILGen, ex_handlers)},
3954 static const FieldDesc
3955 ilexinfo_fields[] = {
3956 {"handlers", G_STRUCT_OFFSET (MonoILExceptionInfo, handlers)},
3957 {"start", G_STRUCT_OFFSET (MonoILExceptionInfo, start)},
3958 {"len", G_STRUCT_OFFSET (MonoILExceptionInfo, len)},
3959 {"end", G_STRUCT_OFFSET (MonoILExceptionInfo, label)},
3963 static const FieldDesc
3964 ilexblock_fields[] = {
3965 {"extype", G_STRUCT_OFFSET (MonoILExceptionBlock, extype)},
3966 {"type", G_STRUCT_OFFSET (MonoILExceptionBlock, type)},
3967 {"start", G_STRUCT_OFFSET (MonoILExceptionBlock, start)},
3968 {"len", G_STRUCT_OFFSET (MonoILExceptionBlock, len)},
3969 {"filter_offset", G_STRUCT_OFFSET (MonoILExceptionBlock, filter_offset)},
3973 static const ClassDesc
3974 emit_classes_to_check [] = {
3975 {"TypeBuilder", typebuilder_fields},
3976 {"ModuleBuilder", modulebuilder_fields},
3977 {"AssemblyBuilder", assemblybuilder_fields},
3978 {"ConstructorBuilder", ctorbuilder_fields},
3979 {"MethodBuilder", methodbuilder_fields},
3980 {"FieldBuilder", fieldbuilder_fields},
3981 {"PropertyBuilder", propertybuilder_fields},
3982 {"ILGenerator", ilgenerator_fields},
3983 {"ILExceptionBlock", ilexblock_fields},
3984 {"ILExceptionInfo", ilexinfo_fields},
3988 static const FieldDesc
3989 monoevent_fields[] = {
3990 {"klass", G_STRUCT_OFFSET (MonoReflectionEvent, klass)},
3991 {"handle", G_STRUCT_OFFSET (MonoReflectionEvent, event)},
3995 static const FieldDesc
3996 monoproperty_fields[] = {
3997 {"klass", G_STRUCT_OFFSET (MonoReflectionProperty, klass)},
3998 {"prop", G_STRUCT_OFFSET (MonoReflectionProperty, property)},
4002 static const FieldDesc
4003 monofield_fields[] = {
4004 {"klass", G_STRUCT_OFFSET (MonoReflectionField, klass)},
4005 {"fhandle", G_STRUCT_OFFSET (MonoReflectionField, field)},
4009 static const FieldDesc
4010 monomethodinfo_fields[] = {
4011 {"parent", G_STRUCT_OFFSET (MonoMethodInfo, parent)},
4012 {"ret", G_STRUCT_OFFSET (MonoMethodInfo, ret)},
4013 {"attrs", G_STRUCT_OFFSET (MonoMethodInfo, attrs)},
4014 {"iattrs", G_STRUCT_OFFSET (MonoMethodInfo, implattrs)},
4018 static const FieldDesc
4019 monopropertyinfo_fields[] = {
4020 {"parent", G_STRUCT_OFFSET (MonoPropertyInfo, parent)},
4021 {"name", G_STRUCT_OFFSET (MonoPropertyInfo, name)},
4022 {"get_method", G_STRUCT_OFFSET (MonoPropertyInfo, get)},
4023 {"set_method", G_STRUCT_OFFSET (MonoPropertyInfo, set)},
4024 {"attrs", G_STRUCT_OFFSET (MonoPropertyInfo, attrs)},
4028 static const FieldDesc
4029 monomethod_fields[] = {
4030 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4034 static const FieldDesc
4035 monocmethod_fields[] = {
4036 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4040 static const FieldDesc
4042 {"ClassImpl", G_STRUCT_OFFSET (MonoReflectionParameter, ClassImpl)},
4043 {"DefaultValueImpl", G_STRUCT_OFFSET (MonoReflectionParameter, DefaultValueImpl)},
4044 {"MemberImpl", G_STRUCT_OFFSET (MonoReflectionParameter, MemberImpl)},
4045 {"NameImpl", G_STRUCT_OFFSET (MonoReflectionParameter, NameImpl)},
4046 {"PositionImpl", G_STRUCT_OFFSET (MonoReflectionParameter, PositionImpl)},
4047 {"AttrsImpl", G_STRUCT_OFFSET (MonoReflectionParameter, AttrsImpl)},
4051 static const ClassDesc
4052 reflection_classes_to_check [] = {
4053 {"MonoEvent", monoevent_fields},
4054 {"MonoProperty", monoproperty_fields},
4055 {"MonoField", monofield_fields},
4056 {"MonoMethodInfo", monomethodinfo_fields},
4057 {"MonoPropertyInfo", monopropertyinfo_fields},
4058 {"MonoMethod", monomethod_fields},
4059 {"MonoCMethod", monocmethod_fields},
4060 {"ParameterInfo", pinfo_fields},
4065 enuminfo_fields[] = {
4066 {"utype", G_STRUCT_OFFSET (MonoEnumInfo, utype)},
4067 {"values", G_STRUCT_OFFSET (MonoEnumInfo, values)},
4068 {"names", G_STRUCT_OFFSET (MonoEnumInfo, names)},
4073 delegate_fields[] = {
4074 {"target_type", G_STRUCT_OFFSET (MonoDelegate, target_type)},
4075 {"m_target", G_STRUCT_OFFSET (MonoDelegate, target)},
4076 {"method_name", G_STRUCT_OFFSET (MonoDelegate, method_name)},
4077 {"method_ptr", G_STRUCT_OFFSET (MonoDelegate, method_ptr)},
4078 {"delegate_trampoline", G_STRUCT_OFFSET (MonoDelegate, delegate_trampoline)},
4079 {"method_info", G_STRUCT_OFFSET (MonoDelegate, method_info)},
4084 multicast_delegate_fields[] = {
4085 {"prev", G_STRUCT_OFFSET (MonoMulticastDelegate, prev)},
4090 async_result_fields[] = {
4091 {"async_state", G_STRUCT_OFFSET (MonoAsyncResult, async_state)},
4092 {"handle", G_STRUCT_OFFSET (MonoAsyncResult, handle)},
4093 {"async_delegate", G_STRUCT_OFFSET (MonoAsyncResult, async_delegate)},
4094 {"data", G_STRUCT_OFFSET (MonoAsyncResult, data)},
4095 {"sync_completed", G_STRUCT_OFFSET (MonoAsyncResult, sync_completed)},
4096 {"completed", G_STRUCT_OFFSET (MonoAsyncResult, completed)},
4097 {"endinvoke_called", G_STRUCT_OFFSET (MonoAsyncResult, endinvoke_called)},
4098 {"async_callback", G_STRUCT_OFFSET (MonoAsyncResult, async_callback)},
4103 exception_fields[] = {
4104 {"trace_ips", G_STRUCT_OFFSET (MonoException, trace_ips)},
4105 {"inner_exception", G_STRUCT_OFFSET (MonoException, inner_ex)},
4106 {"message", G_STRUCT_OFFSET (MonoException, message)},
4107 {"help_link", G_STRUCT_OFFSET (MonoException, help_link)},
4108 {"class_name", G_STRUCT_OFFSET (MonoException, class_name)},
4109 {"stack_trace", G_STRUCT_OFFSET (MonoException, stack_trace)},
4110 {"remote_stack_trace", G_STRUCT_OFFSET (MonoException, remote_stack_trace)},
4111 {"remote_stack_index", G_STRUCT_OFFSET (MonoException, remote_stack_index)},
4112 {"hresult", G_STRUCT_OFFSET (MonoException, hresult)},
4113 {"source", G_STRUCT_OFFSET (MonoException, source)},
4117 static const ClassDesc
4118 system_classes_to_check [] = {
4119 {"Exception", exception_fields},
4120 {"MonoEnumInfo", enuminfo_fields},
4121 {"Delegate", delegate_fields},
4122 {"MulticastDelegate", multicast_delegate_fields},
4127 stack_frame_fields [] = {
4128 {"ilOffset", G_STRUCT_OFFSET (MonoStackFrame, il_offset)},
4129 {"nativeOffset", G_STRUCT_OFFSET (MonoStackFrame, native_offset)},
4130 {"methodBase", G_STRUCT_OFFSET (MonoStackFrame, method)},
4131 {"fileName", G_STRUCT_OFFSET (MonoStackFrame, filename)},
4132 {"lineNumber", G_STRUCT_OFFSET (MonoStackFrame, line)},
4133 {"columnNumber", G_STRUCT_OFFSET (MonoStackFrame, column)},
4137 static const ClassDesc
4138 system_diagnostics_classes_to_check [] = {
4139 {"StackFrame", stack_frame_fields},
4144 mono_method_message_fields[] = {
4145 {"method", G_STRUCT_OFFSET (MonoMethodMessage, method)},
4146 {"args", G_STRUCT_OFFSET (MonoMethodMessage, args)},
4147 {"names", G_STRUCT_OFFSET (MonoMethodMessage, names)},
4148 {"arg_types", G_STRUCT_OFFSET (MonoMethodMessage, arg_types)},
4149 {"ctx", G_STRUCT_OFFSET (MonoMethodMessage, ctx)},
4150 {"rval", G_STRUCT_OFFSET (MonoMethodMessage, rval)},
4151 {"exc", G_STRUCT_OFFSET (MonoMethodMessage, exc)},
4155 static const ClassDesc
4156 messaging_classes_to_check [] = {
4157 {"AsyncResult", async_result_fields},
4158 {"MonoMethodMessage", mono_method_message_fields},
4163 transparent_proxy_fields[] = {
4164 {"_rp", G_STRUCT_OFFSET (MonoTransparentProxy, rp)},
4165 {"_class", G_STRUCT_OFFSET (MonoTransparentProxy, remote_class)},
4170 real_proxy_fields[] = {
4171 {"class_to_proxy", G_STRUCT_OFFSET (MonoRealProxy, class_to_proxy)},
4175 static const ClassDesc
4176 proxy_classes_to_check [] = {
4177 {"TransparentProxy", transparent_proxy_fields},
4178 {"RealProxy", real_proxy_fields},
4183 wait_handle_fields[] = {
4184 {"os_handle", G_STRUCT_OFFSET (MonoWaitHandle, handle)},
4185 {"disposed", G_STRUCT_OFFSET (MonoWaitHandle, disposed)},
4191 {"system_thread_handle", G_STRUCT_OFFSET (MonoThread, handle)},
4192 {"threadpool_thread", G_STRUCT_OFFSET (MonoThread, threadpool_thread)},
4193 {"state", G_STRUCT_OFFSET (MonoThread, state)},
4194 {"abort_exc", G_STRUCT_OFFSET (MonoThread, abort_exc)},
4195 {"abort_state", G_STRUCT_OFFSET (MonoThread, abort_state)},
4196 {"thread_id", G_STRUCT_OFFSET (MonoThread, tid)},
4200 static const ClassDesc
4201 threading_classes_to_check [] = {
4202 {"Thread", thread_fields},
4203 {"WaitHandle", wait_handle_fields},
4207 static const FieldDesc
4209 {"datetime_format", G_STRUCT_OFFSET (MonoCultureInfo, datetime_format)},
4210 {"number_format", G_STRUCT_OFFSET (MonoCultureInfo, number_format)},
4211 {"textinfo", G_STRUCT_OFFSET (MonoCultureInfo, textinfo)},
4212 {"name", G_STRUCT_OFFSET (MonoCultureInfo, name)},
4213 {"displayname", G_STRUCT_OFFSET (MonoCultureInfo, displayname)},
4214 {"englishname", G_STRUCT_OFFSET (MonoCultureInfo, englishname)},
4215 {"nativename", G_STRUCT_OFFSET (MonoCultureInfo, nativename)},
4216 {"iso3lang", G_STRUCT_OFFSET (MonoCultureInfo, iso3lang)},
4217 {"iso2lang", G_STRUCT_OFFSET (MonoCultureInfo, iso2lang)},
4218 {"icu_name", G_STRUCT_OFFSET (MonoCultureInfo, icu_name)},
4219 {"win3lang", G_STRUCT_OFFSET (MonoCultureInfo, win3lang)},
4220 {"compareinfo", G_STRUCT_OFFSET (MonoCultureInfo, compareinfo)},
4224 static const FieldDesc
4225 dtfinfo_fields[] = {
4226 {"amDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, AMDesignator)},
4227 {"pmDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, PMDesignator)},
4228 {"dayNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, DayNames)},
4229 {"monthNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, MonthNames)},
4233 static const FieldDesc
4235 {"decimalFormats", G_STRUCT_OFFSET (MonoNumberFormatInfo, decimalFormats)},
4236 {"currencySymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, currencySymbol)},
4237 {"percentSymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, percentSymbol)},
4238 {"positiveSign", G_STRUCT_OFFSET (MonoNumberFormatInfo, positiveSign)},
4242 static const FieldDesc
4243 compinfo_fields[] = {
4244 {"lcid", G_STRUCT_OFFSET (MonoCompareInfo, lcid)},
4245 {"ICU_collator", G_STRUCT_OFFSET (MonoCompareInfo, ICU_collator)},
4249 static const FieldDesc
4250 sortkey_fields[] = {
4251 {"str", G_STRUCT_OFFSET (MonoSortKey, str)},
4252 {"options", G_STRUCT_OFFSET (MonoSortKey, options)},
4253 {"key", G_STRUCT_OFFSET (MonoSortKey, key)},
4254 {"lcid", G_STRUCT_OFFSET (MonoSortKey, lcid)},
4258 static const ClassDesc
4259 globalization_classes_to_check [] = {
4260 {"CultureInfo", cinfo_fields},
4261 {"DateTimeFormatInfo", dtfinfo_fields},
4262 {"NumberFormatInfo", nfinfo_fields},
4263 {"CompareInfo", compinfo_fields},
4264 {"SortKey", sortkey_fields},
4268 static const FieldDesc
4269 safe_handle_fields[] ={
4270 {"handle", G_STRUCT_OFFSET (MonoSafeHandle, handle)},
4274 static const ClassDesc interop_classes_to_check [] = {
4275 {"SafeHandle", safe_handle_fields},
4281 const ClassDesc *types;
4284 static const NameSpaceDesc
4285 namespaces_to_check[] = {
4286 {"System.Runtime.Remoting.Proxies", proxy_classes_to_check},
4287 {"System.Runtime.Remoting.Messaging", messaging_classes_to_check},
4288 {"System.Reflection.Emit", emit_classes_to_check},
4289 {"System.Reflection", reflection_classes_to_check},
4290 {"System.Threading", threading_classes_to_check},
4291 {"System.Diagnostics", system_diagnostics_classes_to_check},
4292 {"System", system_classes_to_check},
4293 {"System.Globalization", globalization_classes_to_check},
4294 {"System.Runtime.InteropServices", interop_classes_to_check},
4299 check_corlib (MonoImage *corlib)
4302 MonoClassField *field;
4303 const FieldDesc *fdesc;
4304 const ClassDesc *cdesc;
4305 const NameSpaceDesc *ndesc;
4307 GString *result = NULL;
4309 for (ndesc = namespaces_to_check; ndesc->name; ++ndesc) {
4310 for (cdesc = ndesc->types; cdesc->name; ++cdesc) {
4311 klass = mono_class_from_name (corlib, ndesc->name, cdesc->name);
4314 result = g_string_new ("");
4315 g_string_append_printf (result, "Cannot find class %s\n", cdesc->name);
4318 mono_class_init (klass);
4320 * FIXME: we should also check the size of valuetypes, or
4321 * we're going to have trouble when we access them in arrays.
4323 if (klass->valuetype)
4324 struct_offset = sizeof (MonoObject);
4327 for (fdesc = cdesc->fields; fdesc->name; ++fdesc) {
4328 field = mono_class_get_field_from_name (klass, fdesc->name);
4329 if (!field || (field->offset != (fdesc->offset + struct_offset))) {
4331 result = g_string_new ("");
4332 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));
4338 return g_string_free (result, FALSE);
4343 mono_verify_corlib () {
4344 return check_corlib (mono_defaults.corlib);