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 if (candidate->type != MONO_TYPE_CLASS)
1974 return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
1976 case MONO_TYPE_OBJECT:
1977 return MONO_TYPE_IS_REFERENCE (candidate);
1979 case MONO_TYPE_SZARRAY: {
1982 if (candidate->type != MONO_TYPE_SZARRAY)
1985 left = target->data.klass;
1986 right = candidate->data.klass;
1987 return mono_class_is_assignable_from(left, right);
1990 case MONO_TYPE_ARRAY:
1991 if (candidate->type != MONO_TYPE_ARRAY)
1993 return is_array_type_compatible (target, candidate);
1995 //TODO verify aditional checks that needs to be done
1996 case MONO_TYPE_TYPEDBYREF:
1997 return candidate->type == MONO_TYPE_TYPEDBYREF;
1999 case MONO_TYPE_VALUETYPE:
2000 if (target->data.klass->enumtype) {
2001 target = target->data.klass->enum_basetype;
2004 if (candidate->type != MONO_TYPE_VALUETYPE)
2006 return target->data.klass == candidate->data.klass;
2010 if (candidate->type != MONO_TYPE_VAR)
2012 return candidate->data.generic_param->num == target->data.generic_param->num;
2014 case MONO_TYPE_MVAR:
2015 if (candidate->type != MONO_TYPE_MVAR)
2017 return candidate->data.generic_param->num == target->data.generic_param->num;
2020 VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
2021 g_assert_not_reached ();
2030 verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
2032 return verify_type_compatibility_full (ctx, target, candidate, FALSE);
2037 verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
2039 return verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (stack), FALSE);
2042 /* implement the opcode checks*/
2044 push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
2046 if (arg >= ctx->max_args) {
2048 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2050 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
2051 if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
2052 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2054 } else if (check_overflow (ctx)) {
2055 /*We must let the value be pushed, otherwise we would get an underflow error*/
2056 check_unverifiable_type (ctx, ctx->params [arg]);
2057 if (ctx->params [arg]->byref && take_addr)
2058 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2059 set_stack_value (stack_push (ctx), ctx->params [arg], take_addr);
2064 push_local (VerifyContext *ctx, guint32 arg, int take_addr)
2066 if (arg >= ctx->num_locals) {
2067 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
2068 } else if (check_overflow (ctx)) {
2069 /*We must let the value be pushed, otherwise we would get an underflow error*/
2070 check_unverifiable_type (ctx, ctx->locals [arg]);
2071 if (ctx->locals [arg]->byref && take_addr)
2072 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
2074 set_stack_value (stack_push (ctx), ctx->locals [arg], take_addr);
2079 store_arg (VerifyContext *ctx, guint32 arg)
2083 if (arg >= ctx->max_args) {
2084 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
2085 check_underflow (ctx, 1);
2090 if (check_underflow (ctx, 1)) {
2091 value = stack_pop (ctx);
2092 if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
2093 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));
2099 store_local (VerifyContext *ctx, guint32 arg)
2102 if (arg >= ctx->num_locals) {
2103 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
2107 /*TODO verify definite assigment */
2108 if (check_underflow (ctx, 1)) {
2109 value = stack_pop(ctx);
2110 if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
2111 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));
2117 do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
2120 int idxa, idxb, complexMerge = 0;
2123 if (!check_underflow (ctx, 2))
2125 a = stack_get (ctx, 1);
2126 b = stack_top (ctx);
2129 if (IS_MANAGED_POINTER (idxa)) {
2135 if (IS_MANAGED_POINTER (idxb)) {
2142 res = table [idxa][idxb];
2144 VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
2145 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2148 if (res == TYPE_INV) {
2149 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)]));
2153 if (res & NON_VERIFIABLE_RESULT) {
2154 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)",
2155 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)]));
2157 res = res & ~NON_VERIFIABLE_RESULT;
2160 if (complexMerge && res == TYPE_PTR) {
2161 if (complexMerge == 1)
2162 copy_stack_value (stack_top (ctx), a);
2163 else if (complexMerge == 2)
2164 copy_stack_value (stack_top (ctx), b);
2166 * There is no need to merge the type of two pointers.
2167 * The only valid operation is subtraction, that returns a native
2168 * int as result and can be used with any 2 pointer kinds.
2169 * This is valid acording to Patition III 1.1.4
2172 stack_top (ctx)->stype = res;
2178 do_boolean_branch_op (VerifyContext *ctx, int delta)
2180 int target = ctx->ip_offset + delta;
2183 VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2185 if (target < 0 || target >= ctx->code_size) {
2186 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
2190 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2191 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2195 ctx->target = target;
2197 if (!check_underflow (ctx, 1))
2200 top = stack_pop (ctx);
2201 if (!is_valid_bool_arg (top))
2202 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));
2204 check_unmanaged_pointer (ctx, top);
2209 do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
2214 int target = ctx->ip_offset + delta;
2216 VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
2218 if (target < 0 || target >= ctx->code_size) {
2219 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
2223 if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
2224 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
2228 ctx->target = target;
2230 if (!check_underflow (ctx, 2))
2233 b = stack_pop (ctx);
2234 a = stack_pop (ctx);
2237 if (IS_MANAGED_POINTER (idxa))
2241 if (IS_MANAGED_POINTER (idxb))
2246 res = table [idxa][idxb];
2248 VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
2249 VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
2251 if (res == TYPE_INV) {
2252 ADD_VERIFY_ERROR (ctx,
2253 g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x",
2254 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2255 } else if (res & NON_VERIFIABLE_RESULT) {
2256 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x",
2257 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2258 res = res & ~NON_VERIFIABLE_RESULT;
2263 do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX])
2269 if (!check_underflow (ctx, 2))
2271 b = stack_pop (ctx);
2272 a = stack_pop (ctx);
2275 if (IS_MANAGED_POINTER (idxa))
2279 if (IS_MANAGED_POINTER (idxb))
2284 res = table [idxa][idxb];
2286 if(res == TYPE_INV) {
2287 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));
2288 } else if (res & NON_VERIFIABLE_RESULT) {
2289 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x",
2290 type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
2291 res = res & ~NON_VERIFIABLE_RESULT;
2293 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2297 do_ret (VerifyContext *ctx)
2299 VERIFIER_DEBUG ( printf ("checking ret\n"); );
2300 if (ctx->signature->ret->type != MONO_TYPE_VOID) {
2302 if (!check_underflow (ctx, 1))
2305 top = stack_pop(ctx);
2307 if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
2308 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
2313 if (ctx->eval.size > 0) {
2314 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
2316 if (in_any_block (ctx->header, ctx->ip_offset))
2317 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
2320 /* FIXME: we could just load the signature instead of the whole MonoMethod
2321 * TODO handle vararg calls
2322 * TODO handle non virt calls to non-final virtual calls (from the verifiability clause in page 52 of partition 3)
2323 * TODO handle abstract calls
2324 * TODO handle calling .ctor outside one or calling the .ctor for other class but super
2325 * TODO handle call invoking virtual methods (only allowed to invoke super)
2328 do_invoke_method (VerifyContext *ctx, int method_token)
2331 MonoMethodSignature *sig;
2333 MonoMethod *method = mono_get_method_full (ctx->image, method_token, NULL, ctx->generic_context);
2336 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method 0x%08x not found at 0x%04x", method_token, ctx->ip_offset));
2340 if (!(sig = mono_method_signature (method)))
2341 sig = mono_method_get_signature (method, ctx->image, method_token);
2343 param_count = sig->param_count + sig->hasthis;
2344 if (!check_underflow (ctx, param_count))
2347 for (i = sig->param_count - 1; i >= 0; --i) {
2348 VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
2349 value = stack_pop (ctx);
2350 if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
2351 if (sig->params [i]->type == MONO_TYPE_TYPEDBYREF) {
2352 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type for a call parameter at 0x%04x", ctx->ip_offset));
2355 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2360 MonoType * type = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2362 VERIFIER_DEBUG ( printf ("verifying this argument\n"); );
2363 value = stack_pop (ctx);
2364 if (!verify_stack_type_compatibility (ctx, type, value)) {
2365 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature ret at 0x%04x", ctx->ip_offset));
2369 if (!mono_method_can_access_method (ctx->method, method))
2370 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset));
2372 if (sig->ret->type != MONO_TYPE_VOID) {
2373 if (check_overflow (ctx))
2374 set_stack_value (stack_push (ctx), sig->ret, FALSE);
2379 do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
2381 MonoClassField *field;
2384 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2386 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2390 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2391 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
2394 /*taking the address of initonly field only works from the static constructor */
2395 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2396 !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
2397 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2399 if (!mono_method_can_access_field (ctx->method, field))
2400 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2402 set_stack_value (stack_push (ctx), field->type, take_addr);
2406 do_store_static_field (VerifyContext *ctx, int token) {
2407 MonoClassField *field;
2411 if (!check_underflow (ctx, 1))
2414 value = stack_pop (ctx);
2416 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2418 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2422 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
2423 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
2427 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2428 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
2432 if (!mono_method_can_access_field (ctx->method, field))
2433 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2435 if (!verify_stack_type_compatibility (ctx, field->type, value))
2436 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));
2440 check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field)
2442 MonoClassField *field;
2445 /*must be one of: object type, managed pointer, unmanaged pointer (native int) or an instance of a value type */
2446 if (!((obj->stype == TYPE_COMPLEX)
2447 /* the managed reference must be to an object or value type */
2448 || (( IS_MANAGED_POINTER (obj->stype)) && (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX))
2449 || (obj->stype == TYPE_NATIVE_INT)
2450 || (obj->stype == TYPE_PTR)
2451 || (obj->stype == TYPE_COMPLEX))) {
2452 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));
2455 field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
2457 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
2463 if (field->type->type == MONO_TYPE_TYPEDBYREF) {
2464 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
2467 g_assert (obj->type);
2469 /*The value on the stack must be a subclass of the defining type of the field*/
2470 /* we need to check if we can load the field from the stack value*/
2471 if (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX) {
2472 MonoType *type = obj->type->byref ? &field->parent->this_arg : &field->parent->byval_arg;
2474 if (!verify_type_compatibility (ctx, type, obj->type)) {
2475 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
2478 if (!mono_method_can_access_field (ctx->method, field))
2479 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2482 if (!mono_method_can_access_field (ctx->method, field))
2483 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
2485 if (obj->stype == TYPE_NATIVE_INT)
2486 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
2488 check_unmanaged_pointer (ctx, obj);
2493 do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
2496 MonoClassField *field;
2498 if (!check_underflow (ctx, 1))
2500 obj = stack_pop (ctx);
2502 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2505 if (take_addr && field->parent->valuetype && !IS_MANAGED_POINTER (obj->stype))
2506 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
2508 if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
2509 !(field->parent == ctx->method->klass && (ctx->method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && !strcmp (".ctor", ctx->method->name)))
2510 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
2512 set_stack_value (stack_push (ctx), field->type, take_addr);
2516 do_store_field (VerifyContext *ctx, int token)
2518 ILStackDesc *value, *obj;
2519 MonoClassField *field;
2521 if (!check_underflow (ctx, 2))
2524 value = stack_pop (ctx);
2525 obj = stack_pop (ctx);
2527 if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
2530 if (!verify_stack_type_compatibility (ctx, field->type, value))
2531 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));
2534 /*TODO proper handle for Nullable<T>*/
2536 do_box_value (VerifyContext *ctx, int klass_token)
2539 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2544 if (!check_underflow (ctx, 1))
2547 value = stack_top (ctx);
2548 /*box is a nop for reference types*/
2549 if (value->stype == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type))
2552 value = stack_pop (ctx);
2554 if (!verify_stack_type_compatibility (ctx, type, value))
2555 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
2557 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (mono_defaults.object_class));
2561 do_unbox_value (VerifyContext *ctx, int klass_token)
2564 MonoType *type = get_boxable_mono_type (ctx, klass_token);
2569 if (!check_underflow (ctx, 1))
2572 value = stack_pop (ctx);
2574 if (value->stype != TYPE_COMPLEX || value->type->type != MONO_TYPE_OBJECT)
2575 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));
2577 //TODO Pushed managed pointer is haver controled mutability (CMMP)
2578 set_stack_value (stack_push (ctx), mono_type_get_type_byref (type), FALSE);
2582 do_unary_math_op (VerifyContext *ctx, int op)
2585 if (!check_underflow (ctx, 1))
2587 value = stack_top (ctx);
2588 switch(value->stype) {
2591 case TYPE_NATIVE_INT:
2596 case TYPE_COMPLEX: /*only enums are ok*/
2597 if (value->type->type == MONO_TYPE_VALUETYPE && value->type->data.klass->enumtype)
2600 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
2605 do_conversion (VerifyContext *ctx, int kind)
2608 if (!check_underflow (ctx, 1))
2610 value = stack_pop (ctx);
2612 switch(value->stype) {
2615 case TYPE_NATIVE_INT:
2619 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for conversion operation at 0x%04x", ctx->ip_offset));
2624 stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
2627 stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
2630 stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
2632 case TYPE_NATIVE_INT:
2633 stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
2636 g_error ("unknown type %02x in conversion", kind);
2642 do_load_token (VerifyContext *ctx, int token)
2645 MonoClass *handle_class;
2646 if (!check_overflow (ctx))
2648 handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
2650 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
2653 stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
2657 do_ldobj_value (VerifyContext *ctx, int token)
2660 MonoType *type = get_boxable_mono_type (ctx, token);
2665 if (!check_underflow (ctx, 1))
2668 value = stack_pop (ctx);
2669 if (!IS_MANAGED_POINTER (value->stype)
2670 && value->stype != TYPE_NATIVE_INT
2671 && !(value->stype == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
2672 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2676 if (value->stype == TYPE_NATIVE_INT)
2677 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
2679 /*We have a byval on the stack, but the comparison must be strict. */
2680 if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
2681 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
2683 set_stack_value (stack_push (ctx), type, FALSE);
2686 #define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_PUBLIC)
2687 #define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
2688 /* TODO implement delegate verification */
2690 do_newobj (VerifyContext *ctx, int token)
2694 MonoMethodSignature *sig;
2695 MonoMethod *method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
2697 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ctx->ip_offset));
2701 if ((method->flags & CTOR_REQUIRED_FLAGS) != CTOR_REQUIRED_FLAGS
2702 || (method->flags & CTOR_INVALID_FLAGS) != 0
2703 || strcmp (".ctor", method->name)) {
2704 ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
2708 if (method->klass->flags & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
2709 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", token, ctx->ip_offset));
2711 sig = mono_method_signature (method);
2712 if (!check_underflow (ctx, sig->param_count))
2715 for (i = sig->param_count - 1; i >= 0; --i) {
2716 VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
2717 value = stack_pop (ctx);
2718 if (!verify_stack_type_compatibility (ctx, sig->params [i], value))
2719 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
2722 if (check_overflow (ctx))
2723 set_stack_value (stack_push (ctx), &method->klass->byval_arg, FALSE);
2727 get_indirect_op_mono_type (int opcode) {
2732 return &mono_defaults.sbyte_class->byval_arg;
2737 return &mono_defaults.int16_class->byval_arg;
2742 return &mono_defaults.int32_class->byval_arg;
2746 return &mono_defaults.int64_class->byval_arg;
2750 return &mono_defaults.single_class->byval_arg;
2754 return &mono_defaults.double_class->byval_arg;
2758 return &mono_defaults.int_class->byval_arg;
2762 return &mono_defaults.object_class->byval_arg;
2765 g_error ("unknown opcode %02x in get_indirect_op_mono_type ", opcode);
2771 do_load_indirect (VerifyContext *ctx, int opcode)
2774 if (!check_underflow (ctx, 1))
2777 value = stack_pop (ctx);
2778 if (!IS_MANAGED_POINTER (value->stype)) {
2779 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
2780 set_stack_value (stack_push (ctx), get_indirect_op_mono_type (opcode), FALSE);
2784 if (opcode == CEE_LDIND_REF) {
2785 if (UNMASK_TYPE (value->stype) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
2786 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
2787 set_stack_value (stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
2789 if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (opcode), mono_type_get_type_byval (value->type), TRUE))
2790 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2791 set_stack_value (stack_push (ctx), get_indirect_op_mono_type (opcode), FALSE);
2796 do_store_indirect (VerifyContext *ctx, int opcode)
2798 ILStackDesc *addr, *val;
2799 if (!check_underflow (ctx, 2))
2802 val = stack_pop (ctx);
2803 addr = stack_pop (ctx);
2805 check_unmanaged_pointer (ctx, addr);
2807 if (!IS_MANAGED_POINTER (addr->stype) && addr->stype != TYPE_PTR) {
2808 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
2812 if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (opcode), mono_type_get_type_byval (addr->type), TRUE))
2813 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2815 if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (opcode), mono_type_get_type_byval (val->type), FALSE))
2816 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
2820 do_newarr (VerifyContext *ctx, int token)
2823 MonoType *type = get_boxable_mono_type (ctx, token);
2828 if (!check_underflow (ctx, 1))
2831 value = stack_pop (ctx);
2832 if (value->stype != TYPE_I4 && value->stype != TYPE_NATIVE_INT)
2833 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Array size type on stack (%s) is not a verifiable type at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
2835 set_stack_value (stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
2838 /*Merge the stacks and perform compat checks*/
2840 merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start)
2844 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
2845 stack_init (ctx, to);
2848 if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
2851 stack_copy (&ctx->eval, to);
2853 } else if (to->flags == IL_CODE_FLAG_NOT_PROCESSED) {
2854 stack_copy (to, from);
2857 VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
2859 if (from->size != to->size) {
2860 VERIFIER_DEBUG ( printf ("different stack sizes %d x %d\n", from->size, to->size); );
2861 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d)", from->size, to->size));
2865 for (i = 0; i < from->size; ++i) {
2866 ILStackDesc *from_slot = from->stack + i;
2867 ILStackDesc *to_slot = to->stack + i;
2869 if (!verify_type_compatibility (ctx, mono_type_from_stack_slot (to_slot), mono_type_from_stack_slot (from_slot))) {
2870 CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, types not compatible at 0x%04x", ctx->ip_offset));
2874 /*TODO we need to choose the base class for merging reference types*/
2875 copy_stack_value (to_slot, from_slot);
2879 to->flags = IL_CODE_FLAG_SEEN;
2884 * FIXME: need to distinguish between valid and verifiable.
2885 * Need to keep track of types on the stack.
2886 * Verify types for opcodes.
2889 mono_method_verify (MonoMethod *method, int level)
2891 const unsigned char *ip;
2892 const unsigned char *end;
2893 const unsigned char *target = NULL; /* branch target */
2894 int i, n, need_merge = 0, start = 0;
2895 guint token, ip_offset = 0, prefix = 0;
2896 MonoGenericContext *generic_context = NULL;
2899 VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name, method->klass->name, method->name); );
2901 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2902 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
2906 memset (&ctx, 0, sizeof (VerifyContext));
2908 ctx.signature = mono_method_signature (method);
2909 if (!ctx.signature) {
2910 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
2913 ctx.header = mono_method_get_header (method);
2915 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
2918 ctx.method = method;
2919 ip = ctx.header->code;
2920 end = ip + ctx.header->code_size;
2921 ctx.image = image = method->klass->image;
2924 ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
2925 ctx.max_stack = ctx.header->max_stack;
2926 ctx.verifiable = ctx.valid = 1;
2928 ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
2929 ctx.code_size = ctx.header->code_size;
2931 memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
2934 ctx.num_locals = ctx.header->num_locals;
2935 ctx.locals = ctx.header->locals;
2938 if (ctx.signature->hasthis) {
2939 ctx.params = g_new0 (MonoType*, ctx.max_args);
2940 ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
2941 memcpy (ctx.params + 1, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
2943 ctx.params = ctx.signature->params;
2946 if (ctx.signature->is_inflated)
2947 ctx.generic_context = generic_context = mono_method_get_context (method);
2949 stack_init (&ctx, &ctx.eval);
2952 for (i = 0; i < ctx.header->num_clauses; ++i) {
2953 MonoExceptionClause *clause = ctx.header->clauses + i;
2954 /* catch blocks and filter have the exception on the stack. */
2955 /* must check boundaries for handler_offset and handler_start < handler_start*/
2956 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2957 ILCodeDesc *code = ctx.code + clause->handler_offset;
2958 stack_init (&ctx, code);
2959 code->stack [0].stype = TYPE_COMPLEX;
2960 code->stack [0].type = &clause->data.catch_class->byval_arg;
2962 code->flags = IL_CODE_FLAG_SEEN;
2964 else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2965 ILCodeDesc *code = ctx.code + clause->data.filter_offset;
2966 stack_init (&ctx, code);
2967 code->stack [0].stype = TYPE_COMPLEX;
2968 code->stack [0].type = &mono_defaults.exception_class->byval_arg;
2970 code->flags = IL_CODE_FLAG_SEEN;
2974 while (ip < end && ctx.valid) {
2975 ctx.ip_offset = ip_offset = ip - ctx.header->code;
2977 /*TODO id stack merge fails, we break, should't we - or only on errors??
2978 TODO verify need_merge
2981 VERIFIER_DEBUG ( printf ("extra merge needed! %d \n", ctx.target); );
2982 merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE);
2985 merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start);
2988 #ifdef MONO_VERIFIER_DEBUG
2991 discode = mono_disasm_code_one (NULL, method, ip, NULL);
2992 discode [strlen (discode) - 1] = 0; /* no \n */
2993 g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
2996 dump_stack_state (&ctx.code [ip_offset]);
3009 push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
3015 push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
3020 do_binop (&ctx, *ip, add_table);
3025 do_binop (&ctx, *ip, sub_table);
3032 do_binop (&ctx, *ip, bin_op_table);
3041 do_binop (&ctx, *ip, int_bin_op_table);
3048 do_binop (&ctx, *ip, shift_op_table);
3053 if (!check_underflow (&ctx, 1))
3069 /*TODO support definite assignment verification? */
3070 push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
3078 store_local (&ctx, *ip - CEE_STLOC_0);
3083 store_local (&ctx, ip [1]);
3088 store_arg (&ctx, ip [1]);
3102 if (check_overflow (&ctx))
3103 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3108 if (check_overflow (&ctx))
3109 stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
3114 if (check_overflow (&ctx))
3115 stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
3120 if (check_overflow (&ctx))
3121 stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
3126 if (check_overflow (&ctx))
3127 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3132 if (check_overflow (&ctx))
3133 stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
3138 if (check_overflow (&ctx))
3139 stack_push_val (&ctx,TYPE_COMPLEX, &mono_defaults.object_class->byval_arg);
3145 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
3158 do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
3165 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
3178 do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
3185 push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
3189 /* FIXME: warn/error instead? */
3196 if (!check_underflow (&ctx, 1))
3198 if (!check_overflow (&ctx))
3200 top = stack_push (&ctx);
3201 copy_stack_value (top, stack_get (&ctx, 1));
3208 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
3209 token = read32 (ip + 1);
3210 if (in_any_block (ctx.header, ip_offset))
3211 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
3213 * FIXME: check signature, retval, arguments etc.
3219 do_invoke_method (&ctx, read32 (ip + 1));
3224 token = read32 (ip + 1);
3226 * FIXME: check signature, retval, arguments etc.
3231 target = ip + (signed char)ip [1] + 2;
3232 if (target >= end || target < ctx.header->code)
3233 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3234 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3235 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3242 do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
3248 target = ip + (gint32)read32 (ip + 1) + 5;
3249 if (target >= end || target < ctx.header->code)
3250 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3251 if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
3252 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
3259 do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
3265 n = read32 (ip + 1);
3266 target = ip + sizeof (guint32) * n;
3267 /* FIXME: check that ip is in range (and within the same exception block) */
3268 for (i = 0; i < n; ++i)
3269 if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < ctx.header->code)
3270 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3271 if (!check_underflow (&ctx, 1))
3273 if (stack_pop (&ctx)->stype != TYPE_I4)
3274 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ip_offset));
3275 ip += 5 + sizeof (guint32) * n;
3289 do_load_indirect (&ctx, *ip);
3301 do_store_indirect (&ctx, *ip);
3307 do_unary_math_op (&ctx, *ip);
3311 //TODO: implement proper typecheck
3318 do_conversion (&ctx, TYPE_I4);
3324 do_conversion (&ctx, TYPE_I8);
3331 do_conversion (&ctx, TYPE_R8);
3337 do_conversion (&ctx, TYPE_NATIVE_INT);
3342 token = read32 (ip + 1);
3343 if (!check_underflow (&ctx, 2))
3350 do_ldobj_value (&ctx, read32 (ip + 1));
3355 /*TODO verify if token is a valid string literal*/
3356 token = read32 (ip + 1);
3357 if (check_overflow (&ctx))
3358 stack_push_val (&ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
3363 do_newobj (&ctx, read32 (ip + 1));
3369 token = read32 (ip + 1);
3370 if (!check_underflow (&ctx, 1))
3376 ++ip; /* warn, error ? */
3379 do_unbox_value (&ctx, read32 (ip + 1));
3383 if (!check_underflow (&ctx, 1))
3392 do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
3398 do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
3403 do_store_field (&ctx, read32 (ip + 1));
3408 do_store_static_field (&ctx, read32 (ip + 1));
3412 if (!check_underflow (&ctx, 2))
3415 token = read32 (ip + 1);
3419 case CEE_CONV_OVF_I1_UN:
3420 case CEE_CONV_OVF_I2_UN:
3421 case CEE_CONV_OVF_I4_UN:
3422 case CEE_CONV_OVF_U1_UN:
3423 case CEE_CONV_OVF_U2_UN:
3424 case CEE_CONV_OVF_U4_UN:
3425 do_conversion (&ctx, TYPE_I4);
3429 case CEE_CONV_OVF_I8_UN:
3430 case CEE_CONV_OVF_U8_UN:
3431 do_conversion (&ctx, TYPE_I8);
3435 case CEE_CONV_OVF_I_UN:
3436 case CEE_CONV_OVF_U_UN:
3437 do_conversion (&ctx, TYPE_NATIVE_INT);
3442 do_box_value (&ctx, read32 (ip + 1));
3447 do_newarr (&ctx, read32 (ip + 1));
3452 if (!check_underflow (&ctx, 1))
3454 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3455 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldlen at 0x%04x", ip_offset));
3456 stack_top (&ctx)->type = &mono_defaults.int32_class->byval_arg; /* FIXME: use a native int type */
3457 stack_top (&ctx)->stype = TYPE_PTR;
3461 if (check_underflow (&ctx, 2))
3464 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3465 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelema at 0x%04x", ip_offset));
3466 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3467 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
3468 stack_top (&ctx)->stype = TYPE_COMPLEX;
3469 token = read32 (ip + 1);
3482 case CEE_LDELEM_REF:
3483 if (!check_underflow (&ctx, 2))
3486 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3487 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelem at 0x%04x", ip_offset));
3488 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3489 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
3490 stack_top (&ctx)->stype = ldelem_type [*ip - CEE_LDELEM_I1];
3500 case CEE_STELEM_REF:
3501 if (!check_underflow (&ctx, 3))
3506 case CEE_LDELEM_ANY:
3507 case CEE_STELEM_ANY:
3522 ++ip; /* warn, error ? */
3525 case CEE_CONV_OVF_I1:
3526 case CEE_CONV_OVF_U1:
3527 case CEE_CONV_OVF_I2:
3528 case CEE_CONV_OVF_U2:
3529 case CEE_CONV_OVF_I4:
3530 case CEE_CONV_OVF_U4:
3531 do_conversion (&ctx, TYPE_I4);
3535 case CEE_CONV_OVF_I8:
3536 case CEE_CONV_OVF_U8:
3537 do_conversion (&ctx, TYPE_I8);
3541 case CEE_CONV_OVF_I:
3542 case CEE_CONV_OVF_U:
3543 do_conversion (&ctx, TYPE_NATIVE_INT);
3554 ++ip; /* warn, error ? */
3557 if (!check_underflow (&ctx, 1))
3562 if (!check_underflow (&ctx, 1))
3568 ++ip; /* warn, error ? */
3571 if (!check_underflow (&ctx, 1))
3573 token = read32 (ip + 1);
3585 ++ip; /* warn, error ? */
3588 do_load_token (&ctx, read32 (ip + 1));
3593 case CEE_ADD_OVF_UN:
3595 case CEE_MUL_OVF_UN:
3597 case CEE_SUB_OVF_UN:
3598 if (!check_underflow (&ctx, 2))
3603 case CEE_ENDFINALLY:
3608 target = ip + (gint32)read32(ip + 1) + 5;
3609 if (target >= end || target < ctx.header->code)
3610 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3611 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3612 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3617 target = ip + (signed char)ip [1] + 2;
3618 if (target >= end || target < ctx.header->code)
3619 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
3620 if (!is_correct_leave (ctx.header, ip_offset, target - ctx.header->code))
3621 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
3663 store_local (&ctx, read16 (ip + 1));
3668 do_cmp_op (&ctx, cmp_br_eq_op);
3676 do_cmp_op (&ctx, cmp_br_op);
3681 store_arg (&ctx, read16 (ip + 1) );
3687 check_overflow (&ctx);
3690 if (!check_overflow (&ctx))
3692 token = read32 (ip + 1);
3694 stack_top (&ctx)->stype = TYPE_PTR;
3698 if (!check_underflow (&ctx, 1))
3700 token = read32 (ip + 1);
3702 if (stack_top (&ctx)->stype != TYPE_COMPLEX)
3703 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ip_offset));
3704 stack_top (&ctx)->stype = TYPE_PTR;
3712 push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
3718 push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
3723 if (ctx.eval.size != 1)
3724 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ip_offset));
3725 if (stack_top (&ctx)->stype != TYPE_I4 && stack_top (&ctx)->stype != TYPE_PTR)
3726 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to localloc at 0x%04x", ip_offset));
3727 stack_top (&ctx)->stype = TYPE_COMPLEX;
3734 if (ctx.eval.size != 1)
3735 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Stack must have only filter result in endfilter at 0x%04x", ip_offset));
3738 case CEE_UNALIGNED_:
3739 prefix |= PREFIX_UNALIGNED;
3743 prefix |= PREFIX_VOLATILE;
3747 prefix |= PREFIX_TAIL;
3749 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
3750 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
3753 if (!check_underflow (&ctx, 1))
3755 token = read32 (ip + 1);
3759 case CEE_CONSTRAINED_:
3760 token = read32 (ip + 1);
3764 if (!check_underflow (&ctx, 3))
3769 if (!check_underflow (&ctx, 3))
3783 if (!check_overflow (&ctx))
3785 token = read32 (ip + 1);
3787 stack_top (&ctx)->type = &mono_defaults.uint32_class->byval_arg;
3788 stack_top (&ctx)->stype = TYPE_I4;
3791 case CEE_REFANYTYPE:
3792 if (!check_underflow (&ctx, 1))
3806 * if ip != end we overflowed: mark as error.
3808 if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
3809 ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
3815 for (i = 0; i < ctx.header->code_size; ++i) {
3816 if (ctx.code [i].stack)
3817 g_free (ctx.code [i].stack);
3822 g_free (ctx.eval.stack);
3825 if (ctx.signature->hasthis)
3826 g_free (ctx.params);
3838 const FieldDesc *fields;
3841 static const FieldDesc
3842 typebuilder_fields[] = {
3843 {"tname", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, name)},
3844 {"nspace", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, nspace)},
3845 {"parent", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, parent)},
3846 {"interfaces", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, interfaces)},
3847 {"methods", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, methods)},
3848 {"properties", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, properties)},
3849 {"fields", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, fields)},
3850 {"attrs", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, attrs)},
3851 {"table_idx", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, table_idx)},
3855 static const FieldDesc
3856 modulebuilder_fields[] = {
3857 {"types", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, types)},
3858 {"cattrs", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, cattrs)},
3859 {"guid", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, guid)},
3860 {"table_idx", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, table_idx)},
3864 static const FieldDesc
3865 assemblybuilder_fields[] = {
3866 {"entry_point", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, entry_point)},
3867 {"modules", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, modules)},
3868 {"name", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, name)},
3869 {"resources", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, resources)},
3870 {"version", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, version)},
3871 {"culture", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, culture)},
3875 static const FieldDesc
3876 ctorbuilder_fields[] = {
3877 {"ilgen", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, ilgen)},
3878 {"parameters", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, parameters)},
3879 {"attrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, attrs)},
3880 {"iattrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, iattrs)},
3881 {"table_idx", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, table_idx)},
3882 {"call_conv", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, call_conv)},
3883 {"type", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, type)},
3887 static const FieldDesc
3888 methodbuilder_fields[] = {
3889 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, mhandle)},
3890 {"rtype", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, rtype)},
3891 {"parameters", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, parameters)},
3892 {"attrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, attrs)},
3893 {"iattrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, iattrs)},
3894 {"name", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, name)},
3895 {"table_idx", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, table_idx)},
3896 {"code", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, code)},
3897 {"ilgen", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, ilgen)},
3898 {"type", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, type)},
3899 {"pinfo", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, pinfo)},
3900 {"pi_dll", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dll)},
3901 {"pi_entry", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dllentry)},
3902 {"charset", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, charset)},
3903 {"extra_flags", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, extra_flags)},
3904 {"native_cc", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, native_cc)},
3905 {"call_conv", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, call_conv)},
3909 static const FieldDesc
3910 fieldbuilder_fields[] = {
3911 {"attrs", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, attrs)},
3912 {"type", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, type)},
3913 {"name", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, name)},
3914 {"def_value", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, def_value)},
3915 {"offset", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, offset)},
3916 {"table_idx", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, table_idx)},
3920 static const FieldDesc
3921 propertybuilder_fields[] = {
3922 {"attrs", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, attrs)},
3923 {"name", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, name)},
3924 {"type", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, type)},
3925 {"parameters", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, parameters)},
3926 {"def_value", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, def_value)},
3927 {"set_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, set_method)},
3928 {"get_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, get_method)},
3929 {"table_idx", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, table_idx)},
3933 static const FieldDesc
3934 ilgenerator_fields[] = {
3935 {"code", G_STRUCT_OFFSET (MonoReflectionILGen, code)},
3936 {"code_len", G_STRUCT_OFFSET (MonoReflectionILGen, code_len)},
3937 {"max_stack", G_STRUCT_OFFSET (MonoReflectionILGen, max_stack)},
3938 {"cur_stack", G_STRUCT_OFFSET (MonoReflectionILGen, cur_stack)},
3939 {"locals", G_STRUCT_OFFSET (MonoReflectionILGen, locals)},
3940 {"ex_handlers", G_STRUCT_OFFSET (MonoReflectionILGen, ex_handlers)},
3944 static const FieldDesc
3945 ilexinfo_fields[] = {
3946 {"handlers", G_STRUCT_OFFSET (MonoILExceptionInfo, handlers)},
3947 {"start", G_STRUCT_OFFSET (MonoILExceptionInfo, start)},
3948 {"len", G_STRUCT_OFFSET (MonoILExceptionInfo, len)},
3949 {"end", G_STRUCT_OFFSET (MonoILExceptionInfo, label)},
3953 static const FieldDesc
3954 ilexblock_fields[] = {
3955 {"extype", G_STRUCT_OFFSET (MonoILExceptionBlock, extype)},
3956 {"type", G_STRUCT_OFFSET (MonoILExceptionBlock, type)},
3957 {"start", G_STRUCT_OFFSET (MonoILExceptionBlock, start)},
3958 {"len", G_STRUCT_OFFSET (MonoILExceptionBlock, len)},
3959 {"filter_offset", G_STRUCT_OFFSET (MonoILExceptionBlock, filter_offset)},
3963 static const ClassDesc
3964 emit_classes_to_check [] = {
3965 {"TypeBuilder", typebuilder_fields},
3966 {"ModuleBuilder", modulebuilder_fields},
3967 {"AssemblyBuilder", assemblybuilder_fields},
3968 {"ConstructorBuilder", ctorbuilder_fields},
3969 {"MethodBuilder", methodbuilder_fields},
3970 {"FieldBuilder", fieldbuilder_fields},
3971 {"PropertyBuilder", propertybuilder_fields},
3972 {"ILGenerator", ilgenerator_fields},
3973 {"ILExceptionBlock", ilexblock_fields},
3974 {"ILExceptionInfo", ilexinfo_fields},
3978 static const FieldDesc
3979 monoevent_fields[] = {
3980 {"klass", G_STRUCT_OFFSET (MonoReflectionEvent, klass)},
3981 {"handle", G_STRUCT_OFFSET (MonoReflectionEvent, event)},
3985 static const FieldDesc
3986 monoproperty_fields[] = {
3987 {"klass", G_STRUCT_OFFSET (MonoReflectionProperty, klass)},
3988 {"prop", G_STRUCT_OFFSET (MonoReflectionProperty, property)},
3992 static const FieldDesc
3993 monofield_fields[] = {
3994 {"klass", G_STRUCT_OFFSET (MonoReflectionField, klass)},
3995 {"fhandle", G_STRUCT_OFFSET (MonoReflectionField, field)},
3999 static const FieldDesc
4000 monomethodinfo_fields[] = {
4001 {"parent", G_STRUCT_OFFSET (MonoMethodInfo, parent)},
4002 {"ret", G_STRUCT_OFFSET (MonoMethodInfo, ret)},
4003 {"attrs", G_STRUCT_OFFSET (MonoMethodInfo, attrs)},
4004 {"iattrs", G_STRUCT_OFFSET (MonoMethodInfo, implattrs)},
4008 static const FieldDesc
4009 monopropertyinfo_fields[] = {
4010 {"parent", G_STRUCT_OFFSET (MonoPropertyInfo, parent)},
4011 {"name", G_STRUCT_OFFSET (MonoPropertyInfo, name)},
4012 {"get_method", G_STRUCT_OFFSET (MonoPropertyInfo, get)},
4013 {"set_method", G_STRUCT_OFFSET (MonoPropertyInfo, set)},
4014 {"attrs", G_STRUCT_OFFSET (MonoPropertyInfo, attrs)},
4018 static const FieldDesc
4019 monomethod_fields[] = {
4020 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4024 static const FieldDesc
4025 monocmethod_fields[] = {
4026 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
4030 static const FieldDesc
4032 {"ClassImpl", G_STRUCT_OFFSET (MonoReflectionParameter, ClassImpl)},
4033 {"DefaultValueImpl", G_STRUCT_OFFSET (MonoReflectionParameter, DefaultValueImpl)},
4034 {"MemberImpl", G_STRUCT_OFFSET (MonoReflectionParameter, MemberImpl)},
4035 {"NameImpl", G_STRUCT_OFFSET (MonoReflectionParameter, NameImpl)},
4036 {"PositionImpl", G_STRUCT_OFFSET (MonoReflectionParameter, PositionImpl)},
4037 {"AttrsImpl", G_STRUCT_OFFSET (MonoReflectionParameter, AttrsImpl)},
4041 static const ClassDesc
4042 reflection_classes_to_check [] = {
4043 {"MonoEvent", monoevent_fields},
4044 {"MonoProperty", monoproperty_fields},
4045 {"MonoField", monofield_fields},
4046 {"MonoMethodInfo", monomethodinfo_fields},
4047 {"MonoPropertyInfo", monopropertyinfo_fields},
4048 {"MonoMethod", monomethod_fields},
4049 {"MonoCMethod", monocmethod_fields},
4050 {"ParameterInfo", pinfo_fields},
4055 enuminfo_fields[] = {
4056 {"utype", G_STRUCT_OFFSET (MonoEnumInfo, utype)},
4057 {"values", G_STRUCT_OFFSET (MonoEnumInfo, values)},
4058 {"names", G_STRUCT_OFFSET (MonoEnumInfo, names)},
4063 delegate_fields[] = {
4064 {"target_type", G_STRUCT_OFFSET (MonoDelegate, target_type)},
4065 {"m_target", G_STRUCT_OFFSET (MonoDelegate, target)},
4066 {"method_name", G_STRUCT_OFFSET (MonoDelegate, method_name)},
4067 {"method_ptr", G_STRUCT_OFFSET (MonoDelegate, method_ptr)},
4068 {"delegate_trampoline", G_STRUCT_OFFSET (MonoDelegate, delegate_trampoline)},
4069 {"method_info", G_STRUCT_OFFSET (MonoDelegate, method_info)},
4074 multicast_delegate_fields[] = {
4075 {"prev", G_STRUCT_OFFSET (MonoMulticastDelegate, prev)},
4080 async_result_fields[] = {
4081 {"async_state", G_STRUCT_OFFSET (MonoAsyncResult, async_state)},
4082 {"handle", G_STRUCT_OFFSET (MonoAsyncResult, handle)},
4083 {"async_delegate", G_STRUCT_OFFSET (MonoAsyncResult, async_delegate)},
4084 {"data", G_STRUCT_OFFSET (MonoAsyncResult, data)},
4085 {"sync_completed", G_STRUCT_OFFSET (MonoAsyncResult, sync_completed)},
4086 {"completed", G_STRUCT_OFFSET (MonoAsyncResult, completed)},
4087 {"endinvoke_called", G_STRUCT_OFFSET (MonoAsyncResult, endinvoke_called)},
4088 {"async_callback", G_STRUCT_OFFSET (MonoAsyncResult, async_callback)},
4093 exception_fields[] = {
4094 {"trace_ips", G_STRUCT_OFFSET (MonoException, trace_ips)},
4095 {"inner_exception", G_STRUCT_OFFSET (MonoException, inner_ex)},
4096 {"message", G_STRUCT_OFFSET (MonoException, message)},
4097 {"help_link", G_STRUCT_OFFSET (MonoException, help_link)},
4098 {"class_name", G_STRUCT_OFFSET (MonoException, class_name)},
4099 {"stack_trace", G_STRUCT_OFFSET (MonoException, stack_trace)},
4100 {"remote_stack_trace", G_STRUCT_OFFSET (MonoException, remote_stack_trace)},
4101 {"remote_stack_index", G_STRUCT_OFFSET (MonoException, remote_stack_index)},
4102 {"hresult", G_STRUCT_OFFSET (MonoException, hresult)},
4103 {"source", G_STRUCT_OFFSET (MonoException, source)},
4107 static const ClassDesc
4108 system_classes_to_check [] = {
4109 {"Exception", exception_fields},
4110 {"MonoEnumInfo", enuminfo_fields},
4111 {"Delegate", delegate_fields},
4112 {"MulticastDelegate", multicast_delegate_fields},
4117 stack_frame_fields [] = {
4118 {"ilOffset", G_STRUCT_OFFSET (MonoStackFrame, il_offset)},
4119 {"nativeOffset", G_STRUCT_OFFSET (MonoStackFrame, native_offset)},
4120 {"methodBase", G_STRUCT_OFFSET (MonoStackFrame, method)},
4121 {"fileName", G_STRUCT_OFFSET (MonoStackFrame, filename)},
4122 {"lineNumber", G_STRUCT_OFFSET (MonoStackFrame, line)},
4123 {"columnNumber", G_STRUCT_OFFSET (MonoStackFrame, column)},
4127 static const ClassDesc
4128 system_diagnostics_classes_to_check [] = {
4129 {"StackFrame", stack_frame_fields},
4134 mono_method_message_fields[] = {
4135 {"method", G_STRUCT_OFFSET (MonoMethodMessage, method)},
4136 {"args", G_STRUCT_OFFSET (MonoMethodMessage, args)},
4137 {"names", G_STRUCT_OFFSET (MonoMethodMessage, names)},
4138 {"arg_types", G_STRUCT_OFFSET (MonoMethodMessage, arg_types)},
4139 {"ctx", G_STRUCT_OFFSET (MonoMethodMessage, ctx)},
4140 {"rval", G_STRUCT_OFFSET (MonoMethodMessage, rval)},
4141 {"exc", G_STRUCT_OFFSET (MonoMethodMessage, exc)},
4145 static const ClassDesc
4146 messaging_classes_to_check [] = {
4147 {"AsyncResult", async_result_fields},
4148 {"MonoMethodMessage", mono_method_message_fields},
4153 transparent_proxy_fields[] = {
4154 {"_rp", G_STRUCT_OFFSET (MonoTransparentProxy, rp)},
4155 {"_class", G_STRUCT_OFFSET (MonoTransparentProxy, remote_class)},
4160 real_proxy_fields[] = {
4161 {"class_to_proxy", G_STRUCT_OFFSET (MonoRealProxy, class_to_proxy)},
4165 static const ClassDesc
4166 proxy_classes_to_check [] = {
4167 {"TransparentProxy", transparent_proxy_fields},
4168 {"RealProxy", real_proxy_fields},
4173 wait_handle_fields[] = {
4174 {"os_handle", G_STRUCT_OFFSET (MonoWaitHandle, handle)},
4175 {"disposed", G_STRUCT_OFFSET (MonoWaitHandle, disposed)},
4181 {"system_thread_handle", G_STRUCT_OFFSET (MonoThread, handle)},
4182 {"threadpool_thread", G_STRUCT_OFFSET (MonoThread, threadpool_thread)},
4183 {"state", G_STRUCT_OFFSET (MonoThread, state)},
4184 {"abort_exc", G_STRUCT_OFFSET (MonoThread, abort_exc)},
4185 {"abort_state", G_STRUCT_OFFSET (MonoThread, abort_state)},
4186 {"thread_id", G_STRUCT_OFFSET (MonoThread, tid)},
4190 static const ClassDesc
4191 threading_classes_to_check [] = {
4192 {"Thread", thread_fields},
4193 {"WaitHandle", wait_handle_fields},
4197 static const FieldDesc
4199 {"datetime_format", G_STRUCT_OFFSET (MonoCultureInfo, datetime_format)},
4200 {"number_format", G_STRUCT_OFFSET (MonoCultureInfo, number_format)},
4201 {"textinfo", G_STRUCT_OFFSET (MonoCultureInfo, textinfo)},
4202 {"name", G_STRUCT_OFFSET (MonoCultureInfo, name)},
4203 {"displayname", G_STRUCT_OFFSET (MonoCultureInfo, displayname)},
4204 {"englishname", G_STRUCT_OFFSET (MonoCultureInfo, englishname)},
4205 {"nativename", G_STRUCT_OFFSET (MonoCultureInfo, nativename)},
4206 {"iso3lang", G_STRUCT_OFFSET (MonoCultureInfo, iso3lang)},
4207 {"iso2lang", G_STRUCT_OFFSET (MonoCultureInfo, iso2lang)},
4208 {"icu_name", G_STRUCT_OFFSET (MonoCultureInfo, icu_name)},
4209 {"win3lang", G_STRUCT_OFFSET (MonoCultureInfo, win3lang)},
4210 {"compareinfo", G_STRUCT_OFFSET (MonoCultureInfo, compareinfo)},
4214 static const FieldDesc
4215 dtfinfo_fields[] = {
4216 {"amDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, AMDesignator)},
4217 {"pmDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, PMDesignator)},
4218 {"dayNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, DayNames)},
4219 {"monthNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, MonthNames)},
4223 static const FieldDesc
4225 {"decimalFormats", G_STRUCT_OFFSET (MonoNumberFormatInfo, decimalFormats)},
4226 {"currencySymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, currencySymbol)},
4227 {"percentSymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, percentSymbol)},
4228 {"positiveSign", G_STRUCT_OFFSET (MonoNumberFormatInfo, positiveSign)},
4232 static const FieldDesc
4233 compinfo_fields[] = {
4234 {"lcid", G_STRUCT_OFFSET (MonoCompareInfo, lcid)},
4235 {"ICU_collator", G_STRUCT_OFFSET (MonoCompareInfo, ICU_collator)},
4239 static const FieldDesc
4240 sortkey_fields[] = {
4241 {"str", G_STRUCT_OFFSET (MonoSortKey, str)},
4242 {"options", G_STRUCT_OFFSET (MonoSortKey, options)},
4243 {"key", G_STRUCT_OFFSET (MonoSortKey, key)},
4244 {"lcid", G_STRUCT_OFFSET (MonoSortKey, lcid)},
4248 static const ClassDesc
4249 globalization_classes_to_check [] = {
4250 {"CultureInfo", cinfo_fields},
4251 {"DateTimeFormatInfo", dtfinfo_fields},
4252 {"NumberFormatInfo", nfinfo_fields},
4253 {"CompareInfo", compinfo_fields},
4254 {"SortKey", sortkey_fields},
4258 static const FieldDesc
4259 safe_handle_fields[] ={
4260 {"handle", G_STRUCT_OFFSET (MonoSafeHandle, handle)},
4264 static const ClassDesc interop_classes_to_check [] = {
4265 {"SafeHandle", safe_handle_fields},
4271 const ClassDesc *types;
4274 static const NameSpaceDesc
4275 namespaces_to_check[] = {
4276 {"System.Runtime.Remoting.Proxies", proxy_classes_to_check},
4277 {"System.Runtime.Remoting.Messaging", messaging_classes_to_check},
4278 {"System.Reflection.Emit", emit_classes_to_check},
4279 {"System.Reflection", reflection_classes_to_check},
4280 {"System.Threading", threading_classes_to_check},
4281 {"System.Diagnostics", system_diagnostics_classes_to_check},
4282 {"System", system_classes_to_check},
4283 {"System.Globalization", globalization_classes_to_check},
4284 {"System.Runtime.InteropServices", interop_classes_to_check},
4289 check_corlib (MonoImage *corlib)
4292 MonoClassField *field;
4293 const FieldDesc *fdesc;
4294 const ClassDesc *cdesc;
4295 const NameSpaceDesc *ndesc;
4297 GString *result = NULL;
4299 for (ndesc = namespaces_to_check; ndesc->name; ++ndesc) {
4300 for (cdesc = ndesc->types; cdesc->name; ++cdesc) {
4301 klass = mono_class_from_name (corlib, ndesc->name, cdesc->name);
4304 result = g_string_new ("");
4305 g_string_append_printf (result, "Cannot find class %s\n", cdesc->name);
4308 mono_class_init (klass);
4310 * FIXME: we should also check the size of valuetypes, or
4311 * we're going to have trouble when we access them in arrays.
4313 if (klass->valuetype)
4314 struct_offset = sizeof (MonoObject);
4317 for (fdesc = cdesc->fields; fdesc->name; ++fdesc) {
4318 field = mono_class_get_field_from_name (klass, fdesc->name);
4319 if (!field || (field->offset != (fdesc->offset + struct_offset))) {
4321 result = g_string_new ("");
4322 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));
4328 return g_string_free (result, FALSE);
4333 mono_verify_corlib () {
4334 return check_corlib (mono_defaults.corlib);