2 #include <mono/metadata/object.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>
14 * Pull the list of opcodes
16 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
20 #include "mono/cil/opcode.def"
26 mono_free_verify_list (GSList *list)
31 for (tmp = list; tmp; tmp = tmp->next) {
33 g_free (info->message);
39 #define ADD_ERROR(list,msg) \
41 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
42 vinfo->status = MONO_VERIFY_ERROR; \
43 vinfo->message = (msg); \
44 (list) = g_slist_prepend ((list), vinfo); \
47 #define ADD_WARN(list,code,msg) \
49 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
50 vinfo->status = (code); \
51 vinfo->message = (msg); \
52 (list) = g_slist_prepend ((list), vinfo); \
55 static const char* const
57 "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
58 "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
59 "ar-YE", "ar-SY", "ar-JO", "ar-LB",
60 "ar-KW", "ar-AE", "ar-BH", "ar-QA",
61 "bg-BG", "ca-ES", "zh-TW", "zh-CN",
62 "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
63 "da-DK", "de-DE", "de-CH", "de-AT",
64 "de-LU", "de-LI", "el-GR", "en-US",
65 "en-GB", "en-AU", "en-CA", "en-NZ",
66 "en-IE", "en-ZA", "en-JM", "en-CB",
67 "en-BZ", "en-TT", "en-ZW", "en-PH",
68 "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
69 "es-CR", "es-PA", "es-DO", "es-VE",
70 "es-CO", "es-PE", "es-AR", "es-EC",
71 "es-CL", "es-UY", "es-PY", "es-BO",
72 "es-SV", "es-HN", "es-NI", "es-PR",
73 "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
74 "Fr-CH", "fr-LU", "fr-MC", "he-IL",
75 "hu-HU", "is-IS", "it-IT", "it-CH",
76 "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
77 "nb-NO", "nn-NO", "pl-PL", "pt-BR",
78 "pt-PT", "ro-RO", "ru-RU", "hr-HR",
79 "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
80 "sv-SE", "sv-FI", "th-TH", "tr-TR",
81 "ur-PK", "id-ID", "uk-UA", "be-BY",
82 "sl-SI", "et-EE", "lv-LV", "lt-LT",
83 "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
85 "eu-ES", "mk-MK", "af-ZA",
86 "ka-GE", "fo-FO", "hi-IN", "ms-MY",
87 "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
88 "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
89 "gu-IN", "ta-IN", "te-IN", "kn-IN",
90 "mr-IN", "sa-IN", "mn-MN", "gl-ES",
91 "kok-IN", "syr-SY", "div-MV",
96 is_valid_culture (const char *cname)
102 for (i = 0; !found && valid_cultures [i]; ++i) {
103 if (g_strcasecmp (valid_cultures [i], cname))
110 is_valid_assembly_flags (guint32 flags) {
111 /* Metadata: 22.1.2 */
112 flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
113 return ((flags == 1) || (flags == 0));
117 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
120 const char *p, *blob_end;
122 if (blob_index >= image->heap_blob.size)
124 p = mono_metadata_blob_heap (image, blob_index);
125 size = mono_metadata_decode_blob_size (p, &blob_end);
126 if (blob_index + size + (blob_end-p) > image->heap_blob.size)
128 if (notnull && !size)
134 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
136 const char *p, *blob_end, *res;
138 if (str_index >= image->heap_strings.size)
140 res = p = mono_metadata_string_heap (image, str_index);
141 blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
145 * FIXME: should check it's a valid utf8 string, too.
147 while (p <= blob_end) {
152 return *p? NULL: res;
156 is_valid_cls_ident (const char *p)
159 * FIXME: we need the full unicode glib support for this.
160 * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
161 * We do the lame thing for now.
167 if (!isalnum (*p) && *p != '_')
175 is_valid_filename (const char *p)
179 return strpbrk (p, "\\//:")? 0: 1;
183 verify_assembly_table (MonoImage *image, GSList *list, int level)
185 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
186 guint32 cols [MONO_ASSEMBLY_SIZE];
189 if (level & MONO_VERIFY_ERROR) {
191 ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
192 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
194 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
195 case ASSEMBLY_HASH_NONE:
196 case ASSEMBLY_HASH_MD5:
197 case ASSEMBLY_HASH_SHA1:
200 ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
203 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
204 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
206 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
207 ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
209 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
210 ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
212 if (strpbrk (p, ":\\/."))
213 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
216 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
217 ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
219 if (!is_valid_culture (p))
220 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
227 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
229 MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
230 guint32 cols [MONO_ASSEMBLYREF_SIZE];
234 if (level & MONO_VERIFY_ERROR) {
235 for (i = 0; i < t->rows; ++i) {
236 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
237 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
238 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
240 if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
241 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
243 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
244 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
246 if (!is_valid_culture (p))
247 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
250 if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
251 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
254 if (level & MONO_VERIFY_WARNING) {
255 /* check for duplicated rows */
256 for (i = 0; i < t->rows; ++i) {
263 verify_class_layout_table (MonoImage *image, GSList *list, int level)
265 MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
266 MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
267 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
270 if (level & MONO_VERIFY_ERROR) {
271 for (i = 0; i < t->rows; ++i) {
272 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
274 if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
275 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
277 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
278 if (value & TYPE_ATTRIBUTE_INTERFACE)
279 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
280 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
281 ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
282 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
283 switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
284 case 0: case 1: case 2: case 4: case 8: case 16:
285 case 32: case 64: case 128: break;
287 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
289 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
291 * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
292 if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
293 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));
297 * FIXME: we need to check that if class size != 0,
298 * it needs to be greater than the class calculated size.
299 * If parent is a valuetype it also needs to be smaller than
300 * 1 MByte (0x100000 bytes).
301 * To do both these checks we need to load the referenced
302 * assemblies, though (the spec claims we didn't have to, bah).
305 * We need to check that the parent types have the samme layout
316 verify_constant_table (MonoImage *image, GSList *list, int level)
318 MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
319 guint32 cols [MONO_CONSTANT_SIZE];
321 GHashTable *dups = g_hash_table_new (g_direct_hash, g_direct_equal);
323 for (i = 0; i < t->rows; ++i) {
324 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
326 if (level & MONO_VERIFY_ERROR)
327 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
328 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
329 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
330 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
332 switch (cols [MONO_CONSTANT_TYPE]) {
333 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
337 if (level & MONO_VERIFY_CLS)
338 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));
339 case MONO_TYPE_BOOLEAN:
347 case MONO_TYPE_STRING:
348 case MONO_TYPE_CLASS:
351 if (level & MONO_VERIFY_ERROR)
352 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
354 if (level & MONO_VERIFY_ERROR) {
355 value = cols [MONO_CONSTANT_PARENT] >> HASCONSTANT_BITS;
356 switch (cols [MONO_CONSTANT_PARENT] & HASCONSTANT_MASK) {
357 case HASCONSTANT_FIEDDEF:
358 if (value > image->tables [MONO_TABLE_FIELD].rows)
359 ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
361 case HASCONSTANT_PARAM:
362 if (value > image->tables [MONO_TABLE_PARAM].rows)
363 ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
365 case HASCONSTANT_PROPERTY:
366 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
367 ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
370 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
374 if (level & MONO_VERIFY_CLS) {
376 * FIXME: verify types is consistent with the enum type
377 * is parent is an enum.
381 g_hash_table_destroy (dups);
386 verify_event_map_table (MonoImage *image, GSList *list, int level)
388 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
389 guint32 cols [MONO_EVENT_MAP_SIZE];
390 guint32 i, last_event;
391 GHashTable *dups = g_hash_table_new (g_direct_hash, g_direct_equal);
395 for (i = 0; i < t->rows; ++i) {
396 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
397 if (level & MONO_VERIFY_ERROR)
398 if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
399 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
400 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
401 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
402 if (level & MONO_VERIFY_ERROR) {
403 if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
404 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
405 if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
406 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
408 if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
409 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
410 last_event = cols [MONO_EVENT_MAP_EVENTLIST];
414 g_hash_table_destroy (dups);
419 verify_event_table (MonoImage *image, GSList *list, int level)
421 MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
422 guint32 cols [MONO_EVENT_SIZE];
426 for (i = 0; i < t->rows; ++i) {
427 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
429 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
430 if (level & MONO_VERIFY_ERROR)
431 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
433 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
434 if (level & MONO_VERIFY_ERROR)
435 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
437 if (level & MONO_VERIFY_CLS) {
438 if (!is_valid_cls_ident (p))
439 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
443 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
444 value = cols [MONO_EVENT_TYPE] >> TYPEDEFORREF_BITS;
445 switch (cols [MONO_EVENT_TYPE] & TYPEDEFORREF_MASK) {
446 case TYPEDEFORREF_TYPEDEF:
447 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
448 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
450 case TYPEDEFORREF_TYPEREF:
451 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
452 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
454 case TYPEDEFORREF_TYPESPEC:
455 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
456 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
459 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
463 * FIXME: check that there is 1 add and remove row in methodsemantics
464 * and 0 or 1 raise and 0 or more other (maybe it's better to check for
465 * these while checking methodsemantics).
466 * check for duplicated names for the same type [ERROR]
467 * check for CLS duplicate names for the same type [CLS]
474 verify_field_table (MonoImage *image, GSList *list, int level)
476 MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
477 guint32 cols [MONO_FIELD_SIZE];
481 for (i = 0; i < t->rows; ++i) {
482 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
484 * Check this field has only one owner and that the owner is not
485 * an interface (done in verify_typedef_table() )
487 flags = cols [MONO_FIELD_FLAGS];
488 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
489 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
490 case FIELD_ATTRIBUTE_PRIVATE:
491 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
492 case FIELD_ATTRIBUTE_ASSEMBLY:
493 case FIELD_ATTRIBUTE_FAMILY:
494 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
495 case FIELD_ATTRIBUTE_PUBLIC:
498 if (level & MONO_VERIFY_ERROR)
499 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
502 if (level & MONO_VERIFY_ERROR) {
503 if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
504 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
505 if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
506 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
507 if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
508 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
510 * FIXME: check there is only ono owner in the respective table.
511 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
512 * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
513 * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
516 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
517 if (level & MONO_VERIFY_ERROR)
518 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
520 if (level & MONO_VERIFY_CLS) {
521 if (!is_valid_cls_ident (p))
522 ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
527 * if owner is module needs to be static, access mask needs to be compilercontrolled,
528 * public or private (not allowed in cls mode).
529 * if owner is an enum ...
538 verify_file_table (MonoImage *image, GSList *list, int level)
540 MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
541 guint32 cols [MONO_FILE_SIZE];
544 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
546 for (i = 0; i < t->rows; ++i) {
547 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
548 if (level & MONO_VERIFY_ERROR) {
549 if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
550 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
551 if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
552 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
554 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
555 if (level & MONO_VERIFY_ERROR)
556 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
558 if (level & MONO_VERIFY_ERROR) {
559 if (!is_valid_filename (p))
560 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
561 else if (g_hash_table_lookup (dups, p)) {
562 ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
564 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
568 * FIXME: I don't understand what this means:
569 * If this module contains a row in the Assembly table (that is, if this module "holds the manifest")
570 * then there shall not be any row in the File table for this module - i.e., no self-reference [ERROR]
574 if (level & MONO_VERIFY_WARNING) {
575 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
576 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
578 g_hash_table_destroy (dups);
583 verify_moduleref_table (MonoImage *image, GSList *list, int level)
585 MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
586 MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
587 guint32 cols [MONO_MODULEREF_SIZE];
589 guint32 found, i, j, value;
590 GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
592 for (i = 0; i < t->rows; ++i) {
593 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
594 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
595 if (level & MONO_VERIFY_ERROR)
596 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
598 if (level & MONO_VERIFY_ERROR) {
599 if (!is_valid_filename (p))
600 ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
601 else if (g_hash_table_lookup (dups, p)) {
602 ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
603 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
605 for (j = 0; j < tfile->rows; ++j) {
606 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
607 if ((pf = is_valid_string (image, value, TRUE)))
608 if (strcmp (p, pf) == 0) {
614 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
619 g_hash_table_destroy (dups);
624 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
626 MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
627 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
631 for (i = 0; i < t->rows; ++i) {
632 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
633 if (level & MONO_VERIFY_ERROR) {
634 if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
635 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
637 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
638 /* FIXME: check it's a valid locals or method sig.*/
646 mono_image_verify_tables (MonoImage *image, int level)
648 GSList *error_list = NULL;
650 error_list = verify_assembly_table (image, error_list, level);
652 * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
653 * AssemblyRefProcessor should be ignored,
654 * though we may want to emit a warning, since it should not
655 * be present in a PE file.
657 error_list = verify_assemblyref_table (image, error_list, level);
658 error_list = verify_class_layout_table (image, error_list, level);
659 error_list = verify_constant_table (image, error_list, level);
661 * cutom attribute, declsecurity
663 error_list = verify_event_map_table (image, error_list, level);
664 error_list = verify_event_table (image, error_list, level);
665 error_list = verify_field_table (image, error_list, level);
666 error_list = verify_file_table (image, error_list, level);
667 error_list = verify_moduleref_table (image, error_list, level);
668 error_list = verify_standalonesig_table (image, error_list, level);
670 return g_slist_reverse (error_list);
674 TYPE_INVALID = 0, /* leave at 0. */
684 const static unsigned char
685 valid_binops [TYPE_MAX] [TYPE_MAX] = {
687 {TYPE_INVALID, TYPE_INT32, TYPE_INVALID, TYPE_NINT, TYPE_INVALID, TYPE_MANP},
688 {TYPE_INVALID, TYPE_INVALID, TYPE_INT64, TYPE_INVALID},
689 {TYPE_INVALID, TYPE_NINT, TYPE_INVALID, TYPE_NINT, TYPE_INVALID, TYPE_MANP},
690 {TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_FLOAT},
691 {TYPE_INVALID, TYPE_MANP, TYPE_INVALID, TYPE_MANP, TYPE_INVALID, TYPE_NINT},
693 /* int32 */ /* int64 */ /* native */ /* float */ /* managed p */ /* objref */
696 const static unsigned char
697 valid_unnops [TYPE_MAX] = {
698 TYPE_INVALID, TYPE_INT32, TYPE_INT64, TYPE_NINT, TYPE_FLOAT, TYPE_INVALID
699 /* int32 */ /* int64 */ /* native */ /* float */ /* managed p */ /* objref */
702 /* note: the resulting type is always a boolean */
703 const static unsigned char
704 valid_bincomp [TYPE_MAX] [TYPE_MAX] = {
706 {TYPE_INVALID, TYPE_INT32, TYPE_INVALID, TYPE_INT32, TYPE_INVALID},
707 {TYPE_INVALID, TYPE_INVALID, TYPE_INT32, TYPE_INVALID},
708 {TYPE_INVALID, TYPE_INT32, TYPE_INVALID, TYPE_INT32, TYPE_INVALID, TYPE_INT32},
709 {TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INT32},
710 {TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INT32, TYPE_INVALID, TYPE_INT32},
711 {TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INVALID, TYPE_INT32}
712 /* int32 */ /* int64 */ /* native */ /* float */ /* managed p */ /* objref */
715 const static unsigned char
716 valid_intops [TYPE_MAX] [TYPE_MAX] = {
718 {TYPE_INVALID, TYPE_INT32, TYPE_INVALID, TYPE_NINT},
719 {TYPE_INVALID, TYPE_INVALID, TYPE_INT64},
720 {TYPE_INVALID, TYPE_NINT, TYPE_INVALID, TYPE_NINT},
724 const static unsigned char
725 valid_shiftops [TYPE_MAX] [TYPE_MAX] = {
727 {TYPE_INVALID, TYPE_INT32, TYPE_INVALID, TYPE_INT32},
728 {TYPE_INVALID, TYPE_INT64, TYPE_INVALID, TYPE_INT64},
729 {TYPE_INVALID, TYPE_NINT, TYPE_INVALID, TYPE_NINT},
733 #define ADD_INVALID(list,msg) \
735 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1); \
736 vinfo->status = MONO_VERIFY_ERROR; \
737 vinfo->message = (msg); \
738 (list) = g_slist_prepend ((list), vinfo); \
739 /*G_BREAKPOINT ();*/ \
743 #define CHECK_STACK_UNDERFLOW(num) \
745 if (cur_stack < (num)) \
746 ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
749 #define CHECK_STACK_OVERFLOW() \
751 if (cur_stack >= max_stack) \
752 ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
756 PREFIX_UNALIGNED = 1,
759 PREFIX_ADDR_MASK = 3,
779 in_any_block (MonoMethodHeader *header, guint offset)
782 MonoExceptionClause *clause;
784 for (i = 0; i < header->num_clauses; ++i) {
785 clause = &header->clauses [i];
786 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
788 if (MONO_OFFSET_IN_HANDLER (clause, offset))
790 /* need to check filter ... */
796 in_same_block (MonoMethodHeader *header, guint offset, guint target)
799 MonoExceptionClause *clause;
801 for (i = 0; i < header->num_clauses; ++i) {
802 clause = &header->clauses [i];
803 if (MONO_OFFSET_IN_CLAUSE (clause, offset) && !MONO_OFFSET_IN_CLAUSE (clause, target))
805 if (MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
807 /* need to check filter ... */
813 * A leave can't escape a finally block
816 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
819 MonoExceptionClause *clause;
821 for (i = 0; i < header->num_clauses; ++i) {
822 clause = &header->clauses [i];
823 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
825 /* need to check filter ... */
831 can_merge_stack (ILCodeDesc *a, ILCodeDesc *b)
833 if (!b->flags & CODE_SEEN) {
834 b->flags |= CODE_SEEN;
835 b->stack_count = a->stack_count;
839 if (a->stack_count != b->stack_count)
846 * FIXME: need to distinguish between valid and verifiable.
847 * Need to keep track of types on the stack.
848 * Verify types for opcodes.
851 mono_method_verify (MonoMethod *method, int level)
853 MonoMethodHeader *header;
854 MonoMethodSignature *signature, *csig;
857 register const unsigned char *ip;
858 register const unsigned char *end;
859 const unsigned char *target; /* branch target */
860 int max_args, max_stack, cur_stack, i, n, need_merge, start;
861 guint32 token, ip_offset;
862 char *local_state = NULL;
867 if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
868 (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
871 signature = method->signature;
872 header = ((MonoMethodNormal *)method)->header;
874 end = ip + header->code_size;
875 max_args = method->signature->param_count + method->signature->hasthis;
876 max_stack = header->max_stack;
877 need_merge = cur_stack = 0;
879 image = method->klass->image;
880 code = g_new0 (ILCodeDesc, header->code_size);
882 if (header->num_locals) {
883 local_state = g_new (char, header->num_locals);
884 memset (local_state, header->init_locals, header->num_locals);
886 /*g_print ("Method %s.%s::%s\n", method->klass->name_space, method->klass->name, method->name);*/
888 for (i = 0; i < header->num_clauses; ++i) {
889 MonoExceptionClause *clause = &header->clauses [i];
890 /* catch blocks have the exception on the stack. */
891 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
892 code [clause->handler_offset].stack_count = 1;
893 code [clause->handler_offset].flags |= CODE_SEEN;
898 ip_offset = ip - header->code;
899 if (start || !(code [ip_offset].flags & CODE_SEEN)) {
901 /* g_print ("setting stack of IL_%04x to %d\n", ip_offset, 0); */
902 cur_stack = code [ip_offset].stack_count;
904 code [ip_offset].stack_count = cur_stack;
906 code [ip_offset].flags |= CODE_SEEN;
909 if (code [ip_offset].stack_count != cur_stack)
910 ADD_INVALID (list, g_strdup_printf ("Cannot merge stack states at 0x%04x", ip_offset));
914 if (!can_merge_stack (&code [ip_offset], &code [target - header->code]))
915 ADD_INVALID (list, g_strdup_printf ("Cannot merge stack states at 0x%04x", ip_offset));
921 discode = mono_disasm_code_one (NULL, method, ip);
922 discode [strlen (discode) - 1] = 0; /* no \n */
923 g_print ("%-29s (%d)\n", discode, cur_stack);
937 if (*ip - CEE_LDARG_0 >= max_args)
938 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", *ip - CEE_LDARG_0, ip_offset));
939 CHECK_STACK_OVERFLOW ();
947 if (*ip - CEE_LDLOC_0 >= header->num_locals)
948 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", *ip - CEE_LDLOC_0, ip_offset));
949 if (0 && !local_state [*ip - CEE_LDLOC_0])
950 ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", *ip - CEE_LDLOC_0, ip_offset));
951 CHECK_STACK_OVERFLOW ();
959 if (*ip - CEE_STLOC_0 >= header->num_locals)
960 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", *ip - CEE_STLOC_0, ip_offset));
961 local_state [*ip - CEE_STLOC_0] = 1;
962 CHECK_STACK_UNDERFLOW (1);
968 if (ip [1] >= max_args)
969 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", ip [1], ip_offset));
970 CHECK_STACK_OVERFLOW ();
975 if (ip [1] >= max_args)
976 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", ip [1], ip_offset));
977 CHECK_STACK_UNDERFLOW (1);
983 if (ip [1] >= header->num_locals)
984 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", ip [1], ip_offset));
985 /* no need to check if the var is initialized if the address is taken */
986 if (0 && *ip == CEE_LDLOC_S && !local_state [ip [1]])
987 ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", ip [1], ip_offset));
988 CHECK_STACK_OVERFLOW ();
993 if (ip [1] >= header->num_locals)
994 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", ip [1], ip_offset));
995 local_state [ip [1]] = 1;
996 CHECK_STACK_UNDERFLOW (1);
1001 CHECK_STACK_OVERFLOW ();
1015 CHECK_STACK_OVERFLOW ();
1020 CHECK_STACK_OVERFLOW ();
1025 CHECK_STACK_OVERFLOW ();
1030 CHECK_STACK_OVERFLOW ();
1035 CHECK_STACK_OVERFLOW ();
1040 CHECK_STACK_OVERFLOW ();
1044 case CEE_UNUSED99: ++ip; break; /* warn/error instead? */
1046 CHECK_STACK_UNDERFLOW (1);
1047 CHECK_STACK_OVERFLOW ();
1052 CHECK_STACK_UNDERFLOW (1);
1058 ADD_INVALID (list, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
1059 token = read32 (ip + 1);
1060 if (in_any_block (header, ip_offset))
1061 ADD_INVALID (list, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
1063 * FIXME: check signature, retval, arguments etc.
1069 token = read32 (ip + 1);
1071 * FIXME: we could just load the signature ...
1073 cmethod = mono_get_method (image, token, NULL);
1075 ADD_INVALID (list, g_strdup_printf ("Method 0x%08x not found at 0x%04x", token, ip_offset));
1076 csig = cmethod->signature;
1077 CHECK_STACK_UNDERFLOW (csig->param_count + csig->hasthis);
1078 cur_stack -= csig->param_count + csig->hasthis;
1079 if (csig->ret->type != MONO_TYPE_VOID) {
1080 CHECK_STACK_OVERFLOW ();
1086 token = read32 (ip + 1);
1088 * FIXME: check signature, retval, arguments etc.
1093 if (signature->ret->type != MONO_TYPE_VOID) {
1094 CHECK_STACK_UNDERFLOW (1);
1098 ADD_INVALID (list, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", cur_stack, ip_offset));
1100 if (in_any_block (header, ip_offset))
1101 ADD_INVALID (list, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ip_offset));
1105 target = ip + (signed char)ip [1] + 2;
1106 if (target >= end || target < header->code)
1107 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1108 if (!in_same_block (header, ip_offset, target - header->code))
1109 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1115 target = ip + (signed char)ip [1] + 2;
1116 if (target >= end || target < header->code)
1117 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1118 if (!in_same_block (header, ip_offset, target - header->code))
1119 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1120 CHECK_STACK_UNDERFLOW (1);
1135 target = ip + (signed char)ip [1] + 2;
1136 if (target >= end || target < header->code)
1137 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1138 if (!in_same_block (header, ip_offset, target - header->code))
1139 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1140 CHECK_STACK_UNDERFLOW (2);
1146 target = ip + (gint32)read32 (ip + 1) + 5;
1147 if (target >= end || target < header->code)
1148 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1149 if (!in_same_block (header, ip_offset, target - header->code))
1150 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1156 target = ip + (gint32)read32 (ip + 1) + 5;
1157 if (target >= end || target < header->code)
1158 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1159 if (!in_same_block (header, ip_offset, target - header->code))
1160 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1161 CHECK_STACK_UNDERFLOW (1);
1176 target = ip + (gint32)read32 (ip + 1) + 5;
1177 if (target >= end || target < header->code)
1178 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1179 if (!in_same_block (header, ip_offset, target - header->code))
1180 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1181 CHECK_STACK_UNDERFLOW (2);
1187 n = read32 (ip + 1);
1188 target = ip + sizeof (guint32) * n;
1189 /* FIXME: check that ip is in range (and within the same exception block) */
1190 for (i = 0; i < n; ++i)
1191 if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < header->code)
1192 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1193 CHECK_STACK_UNDERFLOW (1);
1195 ip += 5 + sizeof (guint32) * n;
1208 CHECK_STACK_UNDERFLOW (1);
1218 CHECK_STACK_UNDERFLOW (2);
1235 CHECK_STACK_UNDERFLOW (2);
1249 CHECK_STACK_UNDERFLOW (1);
1253 token = read32 (ip + 1);
1254 CHECK_STACK_UNDERFLOW (2);
1259 token = read32 (ip + 1);
1260 CHECK_STACK_UNDERFLOW (1);
1264 token = read32 (ip + 1);
1265 CHECK_STACK_OVERFLOW ();
1270 token = read32 (ip + 1);
1272 * FIXME: we could just load the signature ...
1274 cmethod = mono_get_method (image, token, NULL);
1276 ADD_INVALID (list, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ip_offset));
1277 csig = cmethod->signature;
1278 CHECK_STACK_UNDERFLOW (csig->param_count);
1279 cur_stack -= csig->param_count;
1280 CHECK_STACK_OVERFLOW ();
1286 token = read32 (ip + 1);
1287 CHECK_STACK_UNDERFLOW (1);
1291 CHECK_STACK_UNDERFLOW (1);
1296 ++ip; /* warn, error ? */
1299 token = read32 (ip + 1);
1300 CHECK_STACK_UNDERFLOW (1);
1304 CHECK_STACK_UNDERFLOW (1);
1310 CHECK_STACK_UNDERFLOW (1);
1311 token = read32 (ip + 1);
1315 CHECK_STACK_UNDERFLOW (1);
1316 token = read32 (ip + 1);
1320 CHECK_STACK_UNDERFLOW (2);
1322 token = read32 (ip + 1);
1326 CHECK_STACK_OVERFLOW ();
1327 token = read32 (ip + 1);
1332 CHECK_STACK_OVERFLOW ();
1333 token = read32 (ip + 1);
1338 CHECK_STACK_UNDERFLOW (1);
1340 token = read32 (ip + 1);
1344 CHECK_STACK_UNDERFLOW (2);
1346 token = read32 (ip + 1);
1349 case CEE_CONV_OVF_I1_UN:
1350 case CEE_CONV_OVF_I2_UN:
1351 case CEE_CONV_OVF_I4_UN:
1352 case CEE_CONV_OVF_I8_UN:
1353 case CEE_CONV_OVF_U1_UN:
1354 case CEE_CONV_OVF_U2_UN:
1355 case CEE_CONV_OVF_U4_UN:
1356 case CEE_CONV_OVF_U8_UN:
1357 case CEE_CONV_OVF_I_UN:
1358 case CEE_CONV_OVF_U_UN:
1359 CHECK_STACK_UNDERFLOW (1);
1363 CHECK_STACK_UNDERFLOW (1);
1364 token = read32 (ip + 1);
1368 CHECK_STACK_UNDERFLOW (1);
1369 token = read32 (ip + 1);
1373 CHECK_STACK_UNDERFLOW (1);
1377 CHECK_STACK_UNDERFLOW (2);
1379 token = read32 (ip + 1);
1392 case CEE_LDELEM_REF:
1393 CHECK_STACK_UNDERFLOW (2);
1404 case CEE_STELEM_REF:
1405 CHECK_STACK_UNDERFLOW (3);
1425 ++ip; /* warn, error ? */
1427 case CEE_CONV_OVF_I1:
1428 case CEE_CONV_OVF_U1:
1429 case CEE_CONV_OVF_I2:
1430 case CEE_CONV_OVF_U2:
1431 case CEE_CONV_OVF_I4:
1432 case CEE_CONV_OVF_U4:
1433 case CEE_CONV_OVF_I8:
1434 case CEE_CONV_OVF_U8:
1435 CHECK_STACK_UNDERFLOW (1);
1445 ++ip; /* warn, error ? */
1448 CHECK_STACK_UNDERFLOW (1);
1452 CHECK_STACK_UNDERFLOW (1);
1457 ++ip; /* warn, error ? */
1460 CHECK_STACK_UNDERFLOW (1);
1461 token = read32 (ip + 1);
1473 ++ip; /* warn, error ? */
1476 CHECK_STACK_OVERFLOW ();
1477 token = read32 (ip + 1);
1484 case CEE_CONV_OVF_I:
1485 case CEE_CONV_OVF_U:
1486 CHECK_STACK_UNDERFLOW (1);
1490 case CEE_ADD_OVF_UN:
1492 case CEE_MUL_OVF_UN:
1494 case CEE_SUB_OVF_UN:
1495 CHECK_STACK_UNDERFLOW (2);
1499 case CEE_ENDFINALLY:
1504 target = ip + (gint32)read32(ip + 1) + 5;
1505 if (target >= end || target < header->code)
1506 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1507 if (!is_correct_leave (header, ip_offset, target - header->code))
1508 ADD_INVALID (list, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
1513 target = ip + (signed char)ip [1] + 2;
1514 if (target >= end || target < header->code)
1515 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1516 if (!is_correct_leave (header, ip_offset, target - header->code))
1517 ADD_INVALID (list, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
1522 CHECK_STACK_UNDERFLOW (2);
1527 CHECK_STACK_UNDERFLOW (1);
1568 CHECK_STACK_OVERFLOW ();
1576 CHECK_STACK_UNDERFLOW (2);
1581 CHECK_STACK_OVERFLOW ();
1582 token = read32 (ip + 1);
1587 CHECK_STACK_UNDERFLOW (1);
1588 token = read32 (ip + 1);
1597 if (read16 (ip + 1) >= max_args)
1598 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", read16 (ip + 1), ip_offset));
1599 CHECK_STACK_OVERFLOW ();
1604 if (read16 (ip + 1) >= max_args)
1605 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", read16(ip + 1), ip_offset));
1606 CHECK_STACK_UNDERFLOW (1);
1612 if (read16 (ip + 1) >= header->num_locals)
1613 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", read16 (ip + 1), ip_offset));
1614 /* no need to check if the var is initialized if the address is taken */
1615 if (0 && *ip == CEE_LDLOC && !local_state [read16 (ip + 1)])
1616 ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", read16 (ip + 1), ip_offset));
1617 CHECK_STACK_OVERFLOW ();
1622 if (read16 (ip + 1) >= header->num_locals)
1623 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", read16 (ip + 1), ip_offset));
1624 local_state [read16 (ip + 1)] = 1;
1625 CHECK_STACK_UNDERFLOW (1);
1631 ADD_INVALID (list, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ip_offset));
1639 ADD_INVALID (list, g_strdup_printf ("Stack must have only filter result in endfilter at 0x%04x", ip_offset));
1642 case CEE_UNALIGNED_:
1643 prefix |= PREFIX_UNALIGNED;
1647 prefix |= PREFIX_VOLATILE;
1651 prefix |= PREFIX_TAIL;
1653 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
1654 ADD_INVALID (list, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
1657 CHECK_STACK_UNDERFLOW (1);
1658 token = read32 (ip + 1);
1666 CHECK_STACK_UNDERFLOW (3);
1670 CHECK_STACK_UNDERFLOW (3);
1683 CHECK_STACK_OVERFLOW ();
1684 token = read32 (ip + 1);
1688 case CEE_REFANYTYPE:
1689 CHECK_STACK_UNDERFLOW (1);
1703 * FIXME: if ip != end we overflowed: mark as error.
1707 g_free (local_state);
1719 const FieldDesc *fields;
1722 static const FieldDesc
1723 typebuilder_fields[] = {
1724 {"tname", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, name)},
1725 {"nspace", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, nspace)},
1726 {"parent", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, parent)},
1727 {"interfaces", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, interfaces)},
1728 {"methods", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, methods)},
1729 {"properties", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, properties)},
1730 {"fields", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, fields)},
1731 {"attrs", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, attrs)},
1732 {"table_idx", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, table_idx)},
1736 static const FieldDesc
1737 modulebuilder_fields[] = {
1738 {"types", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, types)},
1739 {"cattrs", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, cattrs)},
1740 {"guid", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, guid)},
1741 {"table_idx", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, table_idx)},
1745 static const FieldDesc
1746 assemblybuilder_fields[] = {
1747 {"entry_point", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, entry_point)},
1748 {"modules", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, modules)},
1749 {"name", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, name)},
1753 static const FieldDesc
1754 ctorbuilder_fields[] = {
1755 {"ilgen", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, ilgen)},
1756 {"parameters", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, parameters)},
1757 {"attrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, attrs)},
1758 {"iattrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, iattrs)},
1759 {"table_idx", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, table_idx)},
1760 {"call_conv", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, call_conv)},
1761 {"type", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, type)},
1765 static const FieldDesc
1766 methodbuilder_fields[] = {
1767 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, mhandle)},
1768 {"rtype", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, rtype)},
1769 {"parameters", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, parameters)},
1770 {"attrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, attrs)},
1771 {"iattrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, iattrs)},
1772 {"name", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, name)},
1773 {"table_idx", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, table_idx)},
1774 {"code", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, code)},
1775 {"ilgen", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, ilgen)},
1776 {"type", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, type)},
1777 {"pinfo", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, pinfo)},
1778 {"pi_dll", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dll)},
1779 {"pi_entry", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dllentry)},
1780 {"ncharset", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, charset)},
1781 {"native_cc", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, native_cc)},
1782 {"call_conv", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, call_conv)},
1786 static const FieldDesc
1787 fieldbuilder_fields[] = {
1788 {"attrs", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, attrs)},
1789 {"type", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, type)},
1790 {"name", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, name)},
1791 {"def_value", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, def_value)},
1792 {"offset", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, offset)},
1793 {"table_idx", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, table_idx)},
1797 static const FieldDesc
1798 propertybuilder_fields[] = {
1799 {"attrs", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, attrs)},
1800 {"name", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, name)},
1801 {"type", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, type)},
1802 {"parameters", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, parameters)},
1803 {"def_value", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, def_value)},
1804 {"set_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, set_method)},
1805 {"get_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, get_method)},
1806 {"table_idx", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, table_idx)},
1810 static const FieldDesc
1811 ilgenerator_fields[] = {
1812 {"code", G_STRUCT_OFFSET (MonoReflectionILGen, code)},
1813 {"mbuilder", G_STRUCT_OFFSET (MonoReflectionILGen, mbuilder)},
1814 {"code_len", G_STRUCT_OFFSET (MonoReflectionILGen, code_len)},
1815 {"max_stack", G_STRUCT_OFFSET (MonoReflectionILGen, max_stack)},
1816 {"cur_stack", G_STRUCT_OFFSET (MonoReflectionILGen, cur_stack)},
1817 {"locals", G_STRUCT_OFFSET (MonoReflectionILGen, locals)},
1818 {"ex_handlers", G_STRUCT_OFFSET (MonoReflectionILGen, ex_handlers)},
1822 static const FieldDesc
1823 ilexinfo_fields[] = {
1824 {"handlers", G_STRUCT_OFFSET (MonoILExceptionInfo, handlers)},
1825 {"start", G_STRUCT_OFFSET (MonoILExceptionInfo, start)},
1826 {"len", G_STRUCT_OFFSET (MonoILExceptionInfo, len)},
1827 {"end", G_STRUCT_OFFSET (MonoILExceptionInfo, label)},
1831 static const FieldDesc
1832 ilexblock_fields[] = {
1833 {"extype", G_STRUCT_OFFSET (MonoILExceptionBlock, extype)},
1834 {"type", G_STRUCT_OFFSET (MonoILExceptionBlock, type)},
1835 {"start", G_STRUCT_OFFSET (MonoILExceptionBlock, start)},
1836 {"len", G_STRUCT_OFFSET (MonoILExceptionBlock, len)},
1837 {"filter_offset", G_STRUCT_OFFSET (MonoILExceptionBlock, filter_offset)},
1841 static const ClassDesc
1842 emit_classes_to_check [] = {
1843 {"TypeBuilder", typebuilder_fields},
1844 {"ModuleBuilder", modulebuilder_fields},
1845 {"AssemblyBuilder", assemblybuilder_fields},
1846 {"ConstructorBuilder", ctorbuilder_fields},
1847 {"MethodBuilder", methodbuilder_fields},
1848 {"FieldBuilder", fieldbuilder_fields},
1849 {"PropertyBuilder", propertybuilder_fields},
1850 {"ILGenerator", ilgenerator_fields},
1851 {"ILExceptionBlock", ilexblock_fields},
1852 {"ILExceptionInfo", ilexinfo_fields},
1856 static const FieldDesc
1857 monoevent_fields[] = {
1858 {"klass", G_STRUCT_OFFSET (MonoReflectionEvent, klass)},
1859 {"handle", G_STRUCT_OFFSET (MonoReflectionEvent, event)},
1863 static const FieldDesc
1864 monoproperty_fields[] = {
1865 {"klass", G_STRUCT_OFFSET (MonoReflectionProperty, klass)},
1866 {"prop", G_STRUCT_OFFSET (MonoReflectionProperty, property)},
1870 static const FieldDesc
1871 monofield_fields[] = {
1872 {"klass", G_STRUCT_OFFSET (MonoReflectionField, klass)},
1873 {"fhandle", G_STRUCT_OFFSET (MonoReflectionField, field)},
1877 static const FieldDesc
1878 monomethodinfo_fields[] = {
1879 {"parent", G_STRUCT_OFFSET (MonoMethodInfo, parent)},
1880 {"ret", G_STRUCT_OFFSET (MonoMethodInfo, ret)},
1881 {"attrs", G_STRUCT_OFFSET (MonoMethodInfo, attrs)},
1882 {"iattrs", G_STRUCT_OFFSET (MonoMethodInfo, implattrs)},
1886 static const FieldDesc
1887 monopropertyinfo_fields[] = {
1888 {"parent", G_STRUCT_OFFSET (MonoPropertyInfo, parent)},
1889 {"name", G_STRUCT_OFFSET (MonoPropertyInfo, name)},
1890 {"get_method", G_STRUCT_OFFSET (MonoPropertyInfo, get)},
1891 {"set_method", G_STRUCT_OFFSET (MonoPropertyInfo, set)},
1892 {"attrs", G_STRUCT_OFFSET (MonoPropertyInfo, attrs)},
1896 static const FieldDesc
1897 monofieldinfo_fields[] = {
1898 {"parent", G_STRUCT_OFFSET (MonoFieldInfo, parent)},
1899 {"type", G_STRUCT_OFFSET (MonoFieldInfo, type)},
1900 {"name", G_STRUCT_OFFSET (MonoFieldInfo, name)},
1901 {"attrs", G_STRUCT_OFFSET (MonoFieldInfo, attrs)},
1905 static const FieldDesc
1906 monomethod_fields[] = {
1907 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
1911 static const FieldDesc
1912 monocmethod_fields[] = {
1913 {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
1917 static const FieldDesc
1919 {"ClassImpl", G_STRUCT_OFFSET (MonoReflectionParameter, ClassImpl)},
1920 {"DefaultValueImpl", G_STRUCT_OFFSET (MonoReflectionParameter, DefaultValueImpl)},
1921 {"MemberImpl", G_STRUCT_OFFSET (MonoReflectionParameter, MemberImpl)},
1922 {"NameImpl", G_STRUCT_OFFSET (MonoReflectionParameter, NameImpl)},
1923 {"PositionImpl", G_STRUCT_OFFSET (MonoReflectionParameter, PositionImpl)},
1924 {"AttrsImpl", G_STRUCT_OFFSET (MonoReflectionParameter, AttrsImpl)},
1928 static const ClassDesc
1929 reflection_classes_to_check [] = {
1930 {"MonoEvent", monoevent_fields},
1931 {"MonoProperty", monoproperty_fields},
1932 {"MonoField", monofield_fields},
1933 {"MonoMethodInfo", monomethodinfo_fields},
1934 {"MonoPropertyInfo", monopropertyinfo_fields},
1935 {"MonoFieldInfo", monofieldinfo_fields},
1936 {"MonoMethod", monomethod_fields},
1937 {"MonoCMethod", monocmethod_fields},
1938 {"ParameterInfo", pinfo_fields},
1943 enuminfo_fields[] = {
1944 {"utype", G_STRUCT_OFFSET (MonoEnumInfo, utype)},
1945 {"values", G_STRUCT_OFFSET (MonoEnumInfo, values)},
1946 {"names", G_STRUCT_OFFSET (MonoEnumInfo, names)},
1951 delegate_fields[] = {
1952 {"target_type", G_STRUCT_OFFSET (MonoDelegate, target_type)},
1953 {"m_target", G_STRUCT_OFFSET (MonoDelegate, target)},
1954 {"method_name", G_STRUCT_OFFSET (MonoDelegate, method_name)},
1955 {"method_ptr", G_STRUCT_OFFSET (MonoDelegate, method_ptr)},
1956 {"delegate_trampoline", G_STRUCT_OFFSET (MonoDelegate, delegate_trampoline)},
1957 {"method_info", G_STRUCT_OFFSET (MonoDelegate, method_info)},
1962 multicast_delegate_fields[] = {
1963 {"prev", G_STRUCT_OFFSET (MonoMulticastDelegate, prev)},
1968 async_result_fields[] = {
1969 {"async_state", G_STRUCT_OFFSET (MonoAsyncResult, async_state)},
1970 {"handle", G_STRUCT_OFFSET (MonoAsyncResult, handle)},
1971 {"async_delegate", G_STRUCT_OFFSET (MonoAsyncResult, async_delegate)},
1972 {"data", G_STRUCT_OFFSET (MonoAsyncResult, data)},
1973 {"sync_completed", G_STRUCT_OFFSET (MonoAsyncResult, sync_completed)},
1974 {"completed", G_STRUCT_OFFSET (MonoAsyncResult, completed)},
1975 {"endinvoke_called", G_STRUCT_OFFSET (MonoAsyncResult, endinvoke_called)},
1980 exception_fields[] = {
1981 {"trace_ips", G_STRUCT_OFFSET (MonoException, trace_ips)},
1982 {"inner_exception", G_STRUCT_OFFSET (MonoException, inner_ex)},
1983 {"message", G_STRUCT_OFFSET (MonoException, message)},
1984 {"help_link", G_STRUCT_OFFSET (MonoException, help_link)},
1985 {"class_name", G_STRUCT_OFFSET (MonoException, class_name)},
1986 {"stack_trace", G_STRUCT_OFFSET (MonoException, stack_trace)},
1987 {"remote_stack_trace", G_STRUCT_OFFSET (MonoException, remote_stack_trace)},
1988 {"remote_stack_index", G_STRUCT_OFFSET (MonoException, remote_stack_index)},
1989 {"hresult", G_STRUCT_OFFSET (MonoException, hresult)},
1990 {"source", G_STRUCT_OFFSET (MonoException, source)},
1994 static const ClassDesc
1995 system_classes_to_check [] = {
1996 {"Exception", exception_fields},
1997 {"MonoEnumInfo", enuminfo_fields},
1998 {"Delegate", delegate_fields},
1999 {"MulticastDelegate", multicast_delegate_fields},
2004 stack_frame_fields [] = {
2005 {"ilOffset", G_STRUCT_OFFSET (MonoStackFrame, il_offset)},
2006 {"nativeOffset", G_STRUCT_OFFSET (MonoStackFrame, native_offset)},
2007 {"methodBase", G_STRUCT_OFFSET (MonoStackFrame, method)},
2008 {"fileName", G_STRUCT_OFFSET (MonoStackFrame, filename)},
2009 {"lineNumber", G_STRUCT_OFFSET (MonoStackFrame, line)},
2010 {"columnNumber", G_STRUCT_OFFSET (MonoStackFrame, column)},
2014 static const ClassDesc
2015 system_diagnostics_classes_to_check [] = {
2016 {"StackFrame", stack_frame_fields},
2021 mono_method_message_fields[] = {
2022 {"method", G_STRUCT_OFFSET (MonoMethodMessage, method)},
2023 {"args", G_STRUCT_OFFSET (MonoMethodMessage, args)},
2024 {"names", G_STRUCT_OFFSET (MonoMethodMessage, names)},
2025 {"arg_types", G_STRUCT_OFFSET (MonoMethodMessage, arg_types)},
2026 {"ctx", G_STRUCT_OFFSET (MonoMethodMessage, ctx)},
2027 {"rval", G_STRUCT_OFFSET (MonoMethodMessage, rval)},
2028 {"exc", G_STRUCT_OFFSET (MonoMethodMessage, exc)},
2032 static const ClassDesc
2033 messaging_classes_to_check [] = {
2034 {"AsyncResult", async_result_fields},
2035 {"MonoMethodMessage", mono_method_message_fields},
2040 transparent_proxy_fields[] = {
2041 {"_rp", G_STRUCT_OFFSET (MonoTransparentProxy, rp)},
2042 {"_class", G_STRUCT_OFFSET (MonoTransparentProxy, klass)},
2047 real_proxy_fields[] = {
2048 {"class_to_proxy", G_STRUCT_OFFSET (MonoRealProxy, class_to_proxy)},
2052 static const ClassDesc
2053 proxy_classes_to_check [] = {
2054 {"TransparentProxy", transparent_proxy_fields},
2055 {"RealProxy", real_proxy_fields},
2060 wait_handle_fields[] = {
2061 {"os_handle", G_STRUCT_OFFSET (MonoWaitHandle, handle)},
2062 {"disposed", G_STRUCT_OFFSET (MonoWaitHandle, disposed)},
2066 static const ClassDesc
2067 threading_classes_to_check [] = {
2068 {"WaitHandle", wait_handle_fields},
2074 const ClassDesc *types;
2077 static const NameSpaceDesc
2078 namespaces_to_check[] = {
2079 {"System.Runtime.Remoting.Proxies", proxy_classes_to_check},
2080 {"System.Runtime.Remoting.Messaging", messaging_classes_to_check},
2081 {"System.Reflection.Emit", emit_classes_to_check},
2082 {"System.Reflection", reflection_classes_to_check},
2083 {"System.Threading", threading_classes_to_check},
2084 {"System.Diagnostics", system_diagnostics_classes_to_check},
2085 {"System", system_classes_to_check},
2090 check_corlib (MonoImage *corlib)
2093 MonoClassField *field;
2094 const FieldDesc *fdesc;
2095 const ClassDesc *cdesc;
2096 const NameSpaceDesc *ndesc;
2099 for (ndesc = namespaces_to_check; ndesc->name; ++ndesc) {
2100 for (cdesc = ndesc->types; cdesc->name; ++cdesc) {
2101 klass = mono_class_from_name (corlib, ndesc->name, cdesc->name);
2103 return g_strdup_printf ("Cannot find class %s", cdesc->name);
2104 mono_class_init (klass);
2106 * FIXME: we should also check the size of valuetypes, or
2107 * we're going to have trouble when we access them in arrays.
2109 if (klass->valuetype)
2113 for (fdesc = cdesc->fields; fdesc->name; ++fdesc) {
2114 field = mono_class_get_field_from_name (klass, fdesc->name);
2115 if (!field || (field->offset != (fdesc->offset + struct_offset)))
2116 return g_strdup_printf ("field `%s' mismatch in class %s (%ld != %ld)", fdesc->name, cdesc->name, (long) fdesc->offset, (long) (field?field->offset:-1));
2124 mono_verify_corlib () {
2125 return check_corlib (mono_defaults.corlib);