Copied remotely
[mono.git] / mono / metadata / verify.c
1
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>
13 #include <string.h>
14 #include <signal.h>
15 #include <ctype.h>
16
17 /*
18  * Pull the list of opcodes
19  */
20 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
21         a = i,
22
23 enum {
24 #include "mono/cil/opcode.def"
25         LAST = 0xff
26 };
27 #undef OPDEF
28
29 void
30 mono_free_verify_list (GSList *list)
31 {
32         MonoVerifyInfo* info;
33         GSList *tmp;
34
35         for (tmp = list; tmp; tmp = tmp->next) {
36                 info = tmp->data;
37                 g_free (info->message);
38                 g_free (info);
39         }
40         g_slist_free (list);
41 }
42
43 #define ADD_ERROR(list,msg)     \
44         do {    \
45                 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1);      \
46                 vinfo->status = MONO_VERIFY_ERROR;      \
47                 vinfo->message = (msg); \
48                 (list) = g_slist_prepend ((list), vinfo);       \
49         } while (0)
50
51 #define ADD_WARN(list,code,msg) \
52         do {    \
53                 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1);      \
54                 vinfo->status = (code); \
55                 vinfo->message = (msg); \
56                 (list) = g_slist_prepend ((list), vinfo);       \
57         } while (0)
58
59 static const char* const
60 valid_cultures[] = {
61         "ar-SA", "ar-IQ", "ar-EG", "ar-LY",
62         "ar-DZ", "ar-MA", "ar-TN", "ar-OM",
63         "ar-YE", "ar-SY", "ar-JO", "ar-LB",
64         "ar-KW", "ar-AE", "ar-BH", "ar-QA",
65         "bg-BG", "ca-ES", "zh-TW", "zh-CN",
66         "zh-HK", "zh-SG", "zh-MO", "cs-CZ",
67         "da-DK", "de-DE", "de-CH", "de-AT",
68         "de-LU", "de-LI", "el-GR", "en-US",
69         "en-GB", "en-AU", "en-CA", "en-NZ",
70         "en-IE", "en-ZA", "en-JM", "en-CB",
71         "en-BZ", "en-TT", "en-ZW", "en-PH",
72         "es-ES-Ts", "es-MX", "es-ES-Is", "es-GT",
73         "es-CR", "es-PA", "es-DO", "es-VE",
74         "es-CO", "es-PE", "es-AR", "es-EC",
75         "es-CL", "es-UY", "es-PY", "es-BO",
76         "es-SV", "es-HN", "es-NI", "es-PR",
77         "Fi-FI", "fr-FR", "fr-BE", "fr-CA",
78         "Fr-CH", "fr-LU", "fr-MC", "he-IL",
79         "hu-HU", "is-IS", "it-IT", "it-CH",
80         "Ja-JP", "ko-KR", "nl-NL", "nl-BE",
81         "nb-NO", "nn-NO", "pl-PL", "pt-BR",
82         "pt-PT", "ro-RO", "ru-RU", "hr-HR",
83         "Lt-sr-SP", "Cy-sr-SP", "sk-SK", "sq-AL",
84         "sv-SE", "sv-FI", "th-TH", "tr-TR",
85         "ur-PK", "id-ID", "uk-UA", "be-BY",
86         "sl-SI", "et-EE", "lv-LV", "lt-LT",
87         "fa-IR", "vi-VN", "hy-AM", "Lt-az-AZ",
88         "Cy-az-AZ",
89         "eu-ES", "mk-MK", "af-ZA",
90         "ka-GE", "fo-FO", "hi-IN", "ms-MY",
91         "ms-BN", "kk-KZ", "ky-KZ", "sw-KE",
92         "Lt-uz-UZ", "Cy-uz-UZ", "tt-TA", "pa-IN",
93         "gu-IN", "ta-IN", "te-IN", "kn-IN",
94         "mr-IN", "sa-IN", "mn-MN", "gl-ES",
95         "kok-IN", "syr-SY", "div-MV",
96         NULL
97 };
98
99 static int
100 is_valid_culture (const char *cname)
101 {
102         int i;
103         int found;
104                         
105         found = *cname == 0;
106         for (i = 0; !found && valid_cultures [i]; ++i) {
107                 if (g_strcasecmp (valid_cultures [i], cname))
108                         found = 1;
109         }
110         return found;
111 }
112
113 static int
114 is_valid_assembly_flags (guint32 flags) {
115         /* Metadata: 22.1.2 */
116         flags &= ~(0x8000 | 0x4000); /* ignore reserved bits 0x0030? */
117         return ((flags == 1) || (flags == 0));
118 }
119
120 static int
121 is_valid_blob (MonoImage *image, guint32 blob_index, int notnull)
122 {
123         guint32 size;
124         const char *p, *blob_end;
125         
126         if (blob_index >= image->heap_blob.size)
127                 return 0;
128         p = mono_metadata_blob_heap (image, blob_index);
129         size = mono_metadata_decode_blob_size (p, &blob_end);
130         if (blob_index + size + (blob_end-p) > image->heap_blob.size)
131                 return 0;
132         if (notnull && !size)
133                 return 0;
134         return 1;
135 }
136
137 static const char*
138 is_valid_string (MonoImage *image, guint32 str_index, int notnull)
139 {
140         const char *p, *blob_end, *res;
141         
142         if (str_index >= image->heap_strings.size)
143                 return NULL;
144         res = p = mono_metadata_string_heap (image, str_index);
145         blob_end = mono_metadata_string_heap (image, image->heap_strings.size - 1);
146         if (notnull && !*p)
147                 return 0;
148         /* 
149          * FIXME: should check it's a valid utf8 string, too.
150          */
151         while (p <= blob_end) {
152                 if (!*p)
153                         return res;
154                 ++p;
155         }
156         return *p? NULL: res;
157 }
158
159 static int
160 is_valid_cls_ident (const char *p)
161 {
162         /*
163          * FIXME: we need the full unicode glib support for this.
164          * Check: http://www.unicode.org/unicode/reports/tr15/Identifier.java
165          * We do the lame thing for now.
166          */
167         if (!isalpha (*p))
168                 return 0;
169         ++p;
170         while (*p) {
171                 if (!isalnum (*p) && *p != '_')
172                         return 0;
173                 ++p;
174         }
175         return 1;
176 }
177
178 static int
179 is_valid_filename (const char *p)
180 {
181         if (!*p)
182                 return 0;
183         return strpbrk (p, "\\//:")? 0: 1;
184 }
185
186 static GSList*
187 verify_assembly_table (MonoImage *image, GSList *list, int level)
188 {
189         MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLY];
190         guint32 cols [MONO_ASSEMBLY_SIZE];
191         const char *p;
192
193         if (level & MONO_VERIFY_ERROR) {
194                 if (t->rows > 1)
195                         ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
196                 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
197                 
198                 switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
199                 case ASSEMBLY_HASH_NONE:
200                 case ASSEMBLY_HASH_MD5:
201                 case ASSEMBLY_HASH_SHA1:
202                         break;
203                 default:
204                         ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
205                 }
206                 
207                 if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
208                         ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
209                 
210                 if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
211                         ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
212                 
213                 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
214                         ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
215                 } else {
216                         if (strpbrk (p, ":\\/."))
217                                 ADD_ERROR (list, g_strdup_printf ("Assembly name `%s' contains invalid chars", p));
218                 }
219
220                 if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_CULTURE], FALSE))) {
221                         ADD_ERROR (list, g_strdup ("Assembly culture is an invalid index"));
222                 } else {
223                         if (!is_valid_culture (p))
224                                 ADD_ERROR (list, g_strdup_printf ("Assembly culture `%s' is invalid", p));
225                 }
226         }
227         return list;
228 }
229
230 static GSList*
231 verify_assemblyref_table (MonoImage *image, GSList *list, int level)
232 {
233         MonoTableInfo *t = &image->tables [MONO_TABLE_ASSEMBLYREF];
234         guint32 cols [MONO_ASSEMBLYREF_SIZE];
235         const char *p;
236         int i;
237
238         if (level & MONO_VERIFY_ERROR) {
239                 for (i = 0; i < t->rows; ++i) {
240                         mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
241                         if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
242                                 ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
243                 
244                         if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
245                                 ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
246                 
247                         if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
248                                 ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
249                         } else {
250                                 if (!is_valid_culture (p))
251                                         ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture `%s' in row %d is invalid", p, i + 1));
252                         }
253
254                         if (cols [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob (image, cols [MONO_ASSEMBLYREF_HASH_VALUE], TRUE))
255                                 ADD_ERROR (list, g_strdup_printf ("AssemblyRef hash value in row %d is invalid or not null and empty", i + 1));
256                 }
257         }
258         if (level & MONO_VERIFY_WARNING) {
259                 /* check for duplicated rows */
260                 for (i = 0; i < t->rows; ++i) {
261                 }
262         }
263         return list;
264 }
265
266 static GSList*
267 verify_class_layout_table (MonoImage *image, GSList *list, int level)
268 {
269         MonoTableInfo *t = &image->tables [MONO_TABLE_CLASSLAYOUT];
270         MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
271         guint32 cols [MONO_CLASS_LAYOUT_SIZE];
272         guint32 value, i;
273         
274         if (level & MONO_VERIFY_ERROR) {
275                 for (i = 0; i < t->rows; ++i) {
276                         mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
277
278                         if (cols [MONO_CLASS_LAYOUT_PARENT] > tdef->rows || !cols [MONO_CLASS_LAYOUT_PARENT]) {
279                                 ADD_ERROR (list, g_strdup_printf ("Parent in class layout is invalid in row %d", i + 1));
280                         } else {
281                                 value = mono_metadata_decode_row_col (tdef, cols [MONO_CLASS_LAYOUT_PARENT] - 1, MONO_TYPEDEF_FLAGS);
282                                 if (value & TYPE_ATTRIBUTE_INTERFACE)
283                                         ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is an interface", i + 1));
284                                 if (value & TYPE_ATTRIBUTE_AUTO_LAYOUT)
285                                         ADD_ERROR (list, g_strdup_printf ("Parent in class layout row %d is AutoLayout", i + 1));
286                                 if (value & TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT) {
287                                         switch (cols [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
288                                         case 0: case 1: case 2: case 4: case 8: case 16:
289                                         case 32: case 64: case 128: break;
290                                         default:
291                                                 ADD_ERROR (list, g_strdup_printf ("Packing size %d in class layout row %d is invalid", cols [MONO_CLASS_LAYOUT_PACKING_SIZE], i + 1));
292                                         }
293                                 } else if (value & TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
294                                         /*
295                                          * FIXME: LAMESPEC: it claims it must be 0 (it's 1, instead).
296                                         if (cols [MONO_CLASS_LAYOUT_PACKING_SIZE])
297                                                 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));
298                                         */
299                                 }
300                                 /*
301                                  * FIXME: we need to check that if class size != 0, 
302                                  * it needs to be greater than the class calculated size.
303                                  * If parent is a valuetype it also needs to be smaller than
304                                  * 1 MByte (0x100000 bytes).
305                                  * To do both these checks we need to load the referenced 
306                                  * assemblies, though (the spec claims we didn't have to, bah).
307                                  */
308                                 /* 
309                                  * We need to check that the parent types have the samme layout 
310                                  * type as well.
311                                  */
312                         }
313                 }
314         }
315         
316         return list;
317 }
318
319 static GSList*
320 verify_constant_table (MonoImage *image, GSList *list, int level)
321 {
322         MonoTableInfo *t = &image->tables [MONO_TABLE_CONSTANT];
323         guint32 cols [MONO_CONSTANT_SIZE];
324         guint32 value, i;
325         GHashTable *dups = g_hash_table_new (NULL, NULL);
326         
327         for (i = 0; i < t->rows; ++i) {
328                 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
329
330                 if (level & MONO_VERIFY_ERROR)
331                         if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT])))
332                                 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Constant row %d", cols [MONO_CONSTANT_PARENT], i + 1));
333                 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]),
334                                 GUINT_TO_POINTER (cols [MONO_CONSTANT_PARENT]));
335
336                 switch (cols [MONO_CONSTANT_TYPE]) {
337                 case MONO_TYPE_U1: /* LAMESPEC: it says I1...*/
338                 case MONO_TYPE_U2:
339                 case MONO_TYPE_U4:
340                 case MONO_TYPE_U8:
341                         if (level & MONO_VERIFY_CLS)
342                                 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));
343                 case MONO_TYPE_BOOLEAN:
344                 case MONO_TYPE_CHAR:
345                 case MONO_TYPE_I1:
346                 case MONO_TYPE_I2:
347                 case MONO_TYPE_I4:
348                 case MONO_TYPE_I8:
349                 case MONO_TYPE_R4:
350                 case MONO_TYPE_R8:
351                 case MONO_TYPE_STRING:
352                 case MONO_TYPE_CLASS:
353                         break;
354                 default:
355                         if (level & MONO_VERIFY_ERROR)
356                                 ADD_ERROR (list, g_strdup_printf ("Type 0x%x is invalid in Constant row %d", cols [MONO_CONSTANT_TYPE], i + 1));
357                 }
358                 if (level & MONO_VERIFY_ERROR) {
359                         value = cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS;
360                         switch (cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK) {
361                         case MONO_HASCONSTANT_FIEDDEF:
362                                 if (value > image->tables [MONO_TABLE_FIELD].rows)
363                                         ADD_ERROR (list, g_strdup_printf ("Parent (field) is invalid in Constant row %d", i + 1));
364                                 break;
365                         case MONO_HASCONSTANT_PARAM:
366                                 if (value > image->tables [MONO_TABLE_PARAM].rows)
367                                         ADD_ERROR (list, g_strdup_printf ("Parent (param) is invalid in Constant row %d", i + 1));
368                                 break;
369                         case MONO_HASCONSTANT_PROPERTY:
370                                 if (value > image->tables [MONO_TABLE_PROPERTY].rows)
371                                         ADD_ERROR (list, g_strdup_printf ("Parent (property) is invalid in Constant row %d", i + 1));
372                                 break;
373                         default:
374                                 ADD_ERROR (list, g_strdup_printf ("Parent is invalid in Constant row %d", i + 1));
375                                 break;
376                         }
377                 }
378                 if (level & MONO_VERIFY_CLS) {
379                         /* 
380                          * FIXME: verify types is consistent with the enum type
381                          * is parent is an enum.
382                          */
383                 }
384         }
385         g_hash_table_destroy (dups);
386         return list;
387 }
388
389 static GSList*
390 verify_event_map_table (MonoImage *image, GSList *list, int level)
391 {
392         MonoTableInfo *t = &image->tables [MONO_TABLE_EVENTMAP];
393         guint32 cols [MONO_EVENT_MAP_SIZE];
394         guint32 i, last_event;
395         GHashTable *dups = g_hash_table_new (NULL, NULL);
396
397         last_event = 0;
398
399         for (i = 0; i < t->rows; ++i) {
400                 mono_metadata_decode_row (t, i, cols, MONO_EVENT_MAP_SIZE);
401                 if (level & MONO_VERIFY_ERROR)
402                         if (g_hash_table_lookup (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT])))
403                                 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is duplicated in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
404                 g_hash_table_insert (dups, GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]),
405                                 GUINT_TO_POINTER (cols [MONO_EVENT_MAP_PARENT]));
406                 if (level & MONO_VERIFY_ERROR) {
407                         if (cols [MONO_EVENT_MAP_PARENT] > image->tables [MONO_TABLE_TYPEDEF].rows)
408                                 ADD_ERROR (list, g_strdup_printf ("Parent 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_PARENT], i + 1));
409                         if (cols [MONO_EVENT_MAP_EVENTLIST] > image->tables [MONO_TABLE_EVENT].rows)
410                                 ADD_ERROR (list, g_strdup_printf ("EventList 0x%08x is invalid in Event Map row %d", cols [MONO_EVENT_MAP_EVENTLIST], i + 1));
411
412                         if (cols [MONO_EVENT_MAP_EVENTLIST] <= last_event)
413                                 ADD_ERROR (list, g_strdup_printf ("EventList overlap in Event Map row %d", i + 1));
414                         last_event = cols [MONO_EVENT_MAP_EVENTLIST];
415                 }
416         }
417
418         g_hash_table_destroy (dups);
419         return list;
420 }
421
422 static GSList*
423 verify_event_table (MonoImage *image, GSList *list, int level)
424 {
425         MonoTableInfo *t = &image->tables [MONO_TABLE_EVENT];
426         guint32 cols [MONO_EVENT_SIZE];
427         const char *p;
428         guint32 value, i;
429         
430         for (i = 0; i < t->rows; ++i) {
431                 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
432
433                 if (cols [MONO_EVENT_FLAGS] & ~(EVENT_SPECIALNAME|EVENT_RTSPECIALNAME)) {
434                         if (level & MONO_VERIFY_ERROR)
435                                 ADD_ERROR (list, g_strdup_printf ("Flags 0x%04x invalid in Event row %d", cols [MONO_EVENT_FLAGS], i + 1));
436                 }
437                 if (!(p = is_valid_string (image, cols [MONO_EVENT_NAME], TRUE))) {
438                         if (level & MONO_VERIFY_ERROR)
439                                 ADD_ERROR (list, g_strdup_printf ("Invalid name in Event row %d", i + 1));
440                 } else {
441                         if (level & MONO_VERIFY_CLS) {
442                                 if (!is_valid_cls_ident (p))
443                                         ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
444                         }
445                 }
446                 
447                 if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
448                         value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
449                         switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
450                         case MONO_TYPEDEFORREF_TYPEDEF:
451                                 if (!value || value > image->tables [MONO_TABLE_TYPEDEF].rows)
452                                         ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
453                                 break;
454                         case MONO_TYPEDEFORREF_TYPEREF:
455                                 if (!value || value > image->tables [MONO_TABLE_TYPEREF].rows)
456                                         ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
457                                 break;
458                         case MONO_TYPEDEFORREF_TYPESPEC:
459                                 if (!value || value > image->tables [MONO_TABLE_TYPESPEC].rows)
460                                         ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
461                                 break;
462                         default:
463                                 ADD_ERROR (list, g_strdup_printf ("Type invalid in Event row %d", i + 1));
464                         }
465                 }
466                 /*
467                  * FIXME: check that there is 1 add and remove row in methodsemantics
468                  * and 0 or 1 raise and 0 or more other (maybe it's better to check for 
469                  * these while checking methodsemantics).
470                  * check for duplicated names for the same type [ERROR]
471                  * check for CLS duplicate names for the same type [CLS]
472                  */
473         }
474         return list;
475 }
476
477 static GSList*
478 verify_field_table (MonoImage *image, GSList *list, int level)
479 {
480         MonoTableInfo *t = &image->tables [MONO_TABLE_FIELD];
481         guint32 cols [MONO_FIELD_SIZE];
482         const char *p;
483         guint32 i, flags;
484         
485         for (i = 0; i < t->rows; ++i) {
486                 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
487                 /*
488                  * Check this field has only one owner and that the owner is not 
489                  * an interface (done in verify_typedef_table() )
490                  */
491                 flags = cols [MONO_FIELD_FLAGS];
492                 switch (flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) {
493                 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
494                 case FIELD_ATTRIBUTE_PRIVATE:
495                 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
496                 case FIELD_ATTRIBUTE_ASSEMBLY:
497                 case FIELD_ATTRIBUTE_FAMILY:
498                 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
499                 case FIELD_ATTRIBUTE_PUBLIC:
500                         break;
501                 default:
502                         if (level & MONO_VERIFY_ERROR)
503                                 ADD_ERROR (list, g_strdup_printf ("Invalid access mask in Field row %d", i + 1));
504                         break;
505                 }
506                 if (level & MONO_VERIFY_ERROR) {
507                         if ((flags & FIELD_ATTRIBUTE_LITERAL) && (flags & FIELD_ATTRIBUTE_INIT_ONLY))
508                                 ADD_ERROR (list, g_strdup_printf ("Literal and InitOnly cannot be both set in Field row %d", i + 1));
509                         if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
510                                 ADD_ERROR (list, g_strdup_printf ("Literal needs also Static set in Field row %d", i + 1));
511                         if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
512                                 ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
513                         /*
514                          * FIXME: check there is only ono owner in the respective table.
515                          * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
516                          * if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
517                          * if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
518                          */
519                 }
520                 if (!(p = is_valid_string (image, cols [MONO_FIELD_NAME], TRUE))) {
521                         if (level & MONO_VERIFY_ERROR)
522                                 ADD_ERROR (list, g_strdup_printf ("Invalid name in Field row %d", i + 1));
523                 } else {
524                         if (level & MONO_VERIFY_CLS) {
525                                 if (!is_valid_cls_ident (p))
526                                         ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Field row %d", p, i + 1));
527                         }
528                 }
529                 /*
530                  * check signature.
531                  * if owner is module needs to be static, access mask needs to be compilercontrolled,
532                  * public or private (not allowed in cls mode).
533                  * if owner is an enum ...
534                  */
535                 
536                 
537         }
538         return list;
539 }
540
541 static GSList*
542 verify_file_table (MonoImage *image, GSList *list, int level)
543 {
544         MonoTableInfo *t = &image->tables [MONO_TABLE_FILE];
545         guint32 cols [MONO_FILE_SIZE];
546         const char *p;
547         guint32 i;
548         GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
549         
550         for (i = 0; i < t->rows; ++i) {
551                 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
552                 if (level & MONO_VERIFY_ERROR) {
553                         if (cols [MONO_FILE_FLAGS] != FILE_CONTAINS_METADATA && cols [MONO_FILE_FLAGS] != FILE_CONTAINS_NO_METADATA)
554                                 ADD_ERROR (list, g_strdup_printf ("Invalid flags in File row %d", i + 1));
555                         if (!is_valid_blob (image, cols [MONO_FILE_HASH_VALUE], TRUE))
556                                 ADD_ERROR (list, g_strdup_printf ("File hash value in row %d is invalid or not null and empty", i + 1));
557                 }
558                 if (!(p = is_valid_string (image, cols [MONO_FILE_NAME], TRUE))) {
559                         if (level & MONO_VERIFY_ERROR)
560                                 ADD_ERROR (list, g_strdup_printf ("Invalid name in File row %d", i + 1));
561                 } else {
562                         if (level & MONO_VERIFY_ERROR) {
563                                 if (!is_valid_filename (p))
564                                         ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in File row %d", p, i + 1));
565                                 else if (g_hash_table_lookup (dups, p)) {
566                                         ADD_ERROR (list, g_strdup_printf ("Duplicate name '%s` in File row %d", p, i + 1));
567                                 }
568                                 g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
569                         }
570                 }
571                 /*
572                  * FIXME: I don't understand what this means:
573                  * If this module contains a row in the Assembly table (that is, if this module "holds the manifest") 
574                  * then there shall not be any row in the File table for this module - i.e., no self-reference  [ERROR]
575                  */
576
577         }
578         if (level & MONO_VERIFY_WARNING) {
579                 if (!t->rows && image->tables [MONO_TABLE_EXPORTEDTYPE].rows)
580                         ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup ("ExportedType table should be empty if File table is empty"));
581         }
582         g_hash_table_destroy (dups);
583         return list;
584 }
585
586 static GSList*
587 verify_moduleref_table (MonoImage *image, GSList *list, int level)
588 {
589         MonoTableInfo *t = &image->tables [MONO_TABLE_MODULEREF];
590         MonoTableInfo *tfile = &image->tables [MONO_TABLE_FILE];
591         guint32 cols [MONO_MODULEREF_SIZE];
592         const char *p, *pf;
593         guint32 found, i, j, value;
594         GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
595         
596         for (i = 0; i < t->rows; ++i) {
597                 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
598                 if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
599                         if (level & MONO_VERIFY_ERROR)
600                                 ADD_ERROR (list, g_strdup_printf ("Invalid name in ModuleRef row %d", i + 1));
601                 } else {
602                         if (level & MONO_VERIFY_ERROR) {
603                                 if (!is_valid_filename (p))
604                                         ADD_ERROR (list, g_strdup_printf ("Invalid name '%s` in ModuleRef row %d", p, i + 1));
605                                 else if (g_hash_table_lookup (dups, p)) {
606                                         ADD_WARN (list, MONO_VERIFY_WARNING, g_strdup_printf ("Duplicate name '%s` in ModuleRef row %d", p, i + 1));
607                                         g_hash_table_insert (dups, (gpointer)p, (gpointer)p);
608                                         found = 0;
609                                         for (j = 0; j < tfile->rows; ++j) {
610                                                 value = mono_metadata_decode_row_col (tfile, j, MONO_FILE_NAME);
611                                                 if ((pf = is_valid_string (image, value, TRUE)))
612                                                         if (strcmp (p, pf) == 0) {
613                                                                 found = 1;
614                                                                 break;
615                                                         }
616                                         }
617                                         if (!found)
618                                                 ADD_ERROR (list, g_strdup_printf ("Name '%s` in ModuleRef row %d doesn't have a match in File table", p, i + 1));
619                                 }
620                         }
621                 }
622         }
623         g_hash_table_destroy (dups);
624         return list;
625 }
626
627 static GSList*
628 verify_standalonesig_table (MonoImage *image, GSList *list, int level)
629 {
630         MonoTableInfo *t = &image->tables [MONO_TABLE_STANDALONESIG];
631         guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
632         const char *p;
633         guint32 i;
634
635         for (i = 0; i < t->rows; ++i) {
636                 mono_metadata_decode_row (t, i, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
637                 if (level & MONO_VERIFY_ERROR) {
638                         if (!is_valid_blob (image, cols [MONO_STAND_ALONE_SIGNATURE], TRUE)) {
639                                 ADD_ERROR (list, g_strdup_printf ("Signature is invalid in StandAloneSig row %d", i + 1));
640                         } else {
641                                 p = mono_metadata_blob_heap (image, cols [MONO_STAND_ALONE_SIGNATURE]);
642                                 /* FIXME: check it's a valid locals or method sig.*/
643                         }
644                 }
645         }
646         return list;
647 }
648
649 GSList*
650 mono_image_verify_tables (MonoImage *image, int level)
651 {
652         GSList *error_list = NULL;
653
654         error_list = verify_assembly_table (image, error_list, level);
655         /* 
656          * AssemblyOS, AssemblyProcessor, AssemblyRefOs and
657          * AssemblyRefProcessor should be ignored, 
658          * though we may want to emit a warning, since it should not 
659          * be present in a PE file.
660          */
661         error_list = verify_assemblyref_table (image, error_list, level);
662         error_list = verify_class_layout_table (image, error_list, level);
663         error_list = verify_constant_table (image, error_list, level);
664         /*
665          * cutom attribute, declsecurity 
666          */
667         error_list = verify_event_map_table (image, error_list, level);
668         error_list = verify_event_table (image, error_list, level);
669         error_list = verify_field_table (image, error_list, level);
670         error_list = verify_file_table (image, error_list, level);
671         error_list = verify_moduleref_table (image, error_list, level);
672         error_list = verify_standalonesig_table (image, error_list, level);
673
674         return g_slist_reverse (error_list);
675 }
676
677 enum {
678         TYPE_INV = 0, /* leave at 0. */
679         TYPE_I4  = 1,
680         TYPE_I8  = 2,
681         TYPE_PTR = 3,
682         TYPE_R8  = 4,
683         TYPE_MP  = 5,
684         TYPE_OBJ = 6,
685         TYPE_VT  = 7,
686         TYPE_MAX = 8
687 };
688
689 static const char* 
690 arg_name [TYPE_MAX] = {
691         "Invalid",
692         "Int32",
693         "Int64",
694         "IntPtr",
695         "Double",
696         "Managed Pointer",
697         "ObjRef",
698         "ValueType"
699 };
700
701 static const char
702 bin_num_table [TYPE_MAX] [TYPE_MAX] = {
703         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
704         {TYPE_INV, TYPE_I4,  TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_MP,  TYPE_INV, TYPE_INV},
705         {TYPE_INV, TYPE_INV, TYPE_I8,  TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
706         {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_MP,  TYPE_INV, TYPE_INV},
707         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8,  TYPE_INV, TYPE_INV, TYPE_INV},
708         {TYPE_INV, TYPE_MP,  TYPE_INV, TYPE_MP,  TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV},
709         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
710         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
711 };
712
713 static const char 
714 neg_table [] = {
715         TYPE_INV, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_INV, TYPE_INV, TYPE_INV
716 };
717
718 /* reduce the size of this table */
719 static const char
720 bin_int_table [TYPE_MAX] [TYPE_MAX] = {
721         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
722         {TYPE_INV, TYPE_I4,  TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
723         {TYPE_INV, TYPE_INV, TYPE_I8,  TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
724         {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
725         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
726         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
727         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
728         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
729 };
730
731 static const char
732 bin_comp_table [TYPE_MAX] [TYPE_MAX] = {
733         {0},
734         {0, 1, 0, 1, 0, 0, 0, 0},
735         {0, 0, 1, 0, 0, 0, 0, 0},
736         {0, 1, 0, 1, 0, 2, 0, 0},
737         {0, 0, 0, 0, 1, 0, 0, 0},
738         {0, 0, 0, 2, 0, 1, 0, 0},
739         {0, 0, 0, 0, 0, 0, 3, 0},
740         {0, 0, 0, 0, 0, 0, 0, 0},
741 };
742
743 /* reduce the size of this table */
744 static const char
745 shift_table [TYPE_MAX] [TYPE_MAX] = {
746         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
747         {TYPE_INV, TYPE_I4,  TYPE_INV, TYPE_I4,  TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
748         {TYPE_INV, TYPE_I8,  TYPE_INV, TYPE_I8,  TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
749         {TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_PTR, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
750         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
751         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
752         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
753         {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV}
754 };
755
756 static const char 
757 ldind_type [] = {
758         TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_R8, TYPE_OBJ
759 };
760
761 static const char
762 ldelem_type [] = {
763         TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I4, TYPE_I8, TYPE_PTR, TYPE_R8, TYPE_R8, TYPE_OBJ
764 };
765
766 #define ADD_INVALID(list,msg)   \
767         do {    \
768                 MonoVerifyInfo *vinfo = g_new (MonoVerifyInfo, 1);      \
769                 vinfo->status = MONO_VERIFY_ERROR;      \
770                 vinfo->message = (msg); \
771                 (list) = g_slist_prepend ((list), vinfo);       \
772                 /*G_BREAKPOINT ();*/    \
773                 goto invalid_cil;       \
774         } while (0)
775
776 #define CHECK_STACK_UNDERFLOW(num)      \
777         do {    \
778                 if (cur_stack < (num))  \
779                         ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num)));        \
780         } while (0)
781
782 #define CHECK_STACK_OVERFLOW()  \
783         do {    \
784                 if (cur_stack >= max_stack)     \
785                         ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
786         } while (0)
787
788 enum {
789         PREFIX_UNALIGNED = 1,
790         PREFIX_VOLATILE  = 2,
791         PREFIX_TAIL      = 4,
792         PREFIX_ADDR_MASK = 3,
793         PREFIX_FUNC_MASK = 4
794 };
795
796 enum {
797         CODE_SEEN = 1
798 };
799
800 typedef struct {
801         MonoType *type;
802         int stype;
803 } ILStackDesc;
804
805 typedef struct {
806         ILStackDesc *stack;
807         guint16 stack_count;
808         guint16 flags;
809 } ILCodeDesc;
810
811 static void
812 type_to_eval_stack_type (MonoType *type, ILStackDesc *stack, int take_addr) {
813         int t = type->type;
814
815         stack->type = type;
816         if (type->byref || take_addr) { /* fix double addr issue */
817                 stack->stype = TYPE_MP;
818                 return;
819         }
820
821 handle_enum:
822         switch (t) {
823         case MONO_TYPE_I1:
824         case MONO_TYPE_U1:
825         case MONO_TYPE_BOOLEAN:
826         case MONO_TYPE_I2:
827         case MONO_TYPE_U2:
828         case MONO_TYPE_CHAR:
829         case MONO_TYPE_I4:
830         case MONO_TYPE_U4:
831                 stack->stype = TYPE_I4;
832                 return;
833         case MONO_TYPE_I:
834         case MONO_TYPE_U:
835         case MONO_TYPE_PTR:
836                 stack->stype = TYPE_PTR;
837                 return;
838         case MONO_TYPE_CLASS:
839         case MONO_TYPE_STRING:
840         case MONO_TYPE_OBJECT:
841         case MONO_TYPE_SZARRAY:
842         case MONO_TYPE_ARRAY:    
843                 stack->stype = TYPE_OBJ;
844                 return;
845         case MONO_TYPE_I8:
846         case MONO_TYPE_U8:
847                 stack->stype = TYPE_I8;
848                 return;
849         case MONO_TYPE_R4:
850         case MONO_TYPE_R8:
851                 stack->stype = TYPE_R8;
852                 return;
853         case MONO_TYPE_VALUETYPE:
854                 if (type->data.klass->enumtype) {
855                         t = type->data.klass->enum_basetype->type;
856                         goto handle_enum;
857                 } else {
858                         stack->stype = TYPE_VT;
859                         return;
860                 }
861         default:
862                 g_error ("unknown type %02x in eval stack type", type->type);
863         }
864         return;
865 }
866
867 static int
868 type_from_op (int ins, ILStackDesc *arg) {
869         switch (ins) {
870         /* binops */
871         case CEE_ADD:
872         case CEE_SUB:
873         case CEE_MUL:
874         case CEE_DIV:
875         case CEE_REM:
876                 /* FIXME: check unverifiable args for TYPE_MP */
877                 return arg->stype = bin_num_table [arg->stype] [arg [1].stype];
878         case CEE_DIV_UN:
879         case CEE_REM_UN:
880         case CEE_AND:
881         case CEE_OR:
882         case CEE_XOR:
883                 return arg->stype = bin_int_table [arg->stype] [arg [1].stype];
884         case CEE_SHL:
885         case CEE_SHR:
886         case CEE_SHR_UN:
887                 return arg->stype = shift_table [arg->stype] [arg [1].stype];
888         case CEE_BEQ_S:
889         case CEE_BGE_S:
890         case CEE_BGT_S:
891         case CEE_BLE_S:
892         case CEE_BLT_S:
893         case CEE_BNE_UN_S:
894         case CEE_BGE_UN_S:
895         case CEE_BGT_UN_S:
896         case CEE_BLE_UN_S:
897         case CEE_BLT_UN_S:
898         case CEE_BEQ:
899         case CEE_BGE:
900         case CEE_BGT:
901         case CEE_BLE:
902         case CEE_BLT:
903         case CEE_BNE_UN:
904         case CEE_BGE_UN:
905         case CEE_BGT_UN:
906         case CEE_BLE_UN:
907         case CEE_BLT_UN:
908                 /* FIXME: handle some specifics with ins->next->type */
909                 return bin_comp_table [arg->stype] [arg [1].stype] ? TYPE_I4: TYPE_INV;
910         case 256+CEE_CEQ:
911         case 256+CEE_CGT:
912         case 256+CEE_CGT_UN:
913         case 256+CEE_CLT:
914         case 256+CEE_CLT_UN:
915                 return arg->stype = bin_comp_table [arg->stype] [arg [1].stype] ? TYPE_I4: TYPE_INV;
916         /* unops */
917         case CEE_NEG:
918                 return arg->stype = neg_table [arg->stype];
919         case CEE_NOT:
920                 if (arg->stype >= TYPE_I4 && arg->stype <= TYPE_PTR)
921                         return arg->stype;
922                 else
923                         return arg->stype = TYPE_INV;
924         case CEE_CONV_I1:
925         case CEE_CONV_U1:
926         case CEE_CONV_I2:
927         case CEE_CONV_U2:
928         case CEE_CONV_I4:
929         case CEE_CONV_U4:
930         case CEE_CONV_OVF_I1:
931         case CEE_CONV_OVF_U1:
932         case CEE_CONV_OVF_I2:
933         case CEE_CONV_OVF_U2:
934         case CEE_CONV_OVF_I4:
935         case CEE_CONV_OVF_U4:
936         case CEE_CONV_OVF_I1_UN:
937         case CEE_CONV_OVF_U1_UN:
938         case CEE_CONV_OVF_I2_UN:
939         case CEE_CONV_OVF_U2_UN:
940         case CEE_CONV_OVF_I4_UN:
941         case CEE_CONV_OVF_U4_UN:
942                 if (arg->stype == TYPE_INV || arg->stype >= TYPE_MP)
943                         return arg->stype = TYPE_INV;
944                 return arg->stype = TYPE_I4;
945         case CEE_CONV_I:
946         case CEE_CONV_U:
947         case CEE_CONV_OVF_I:
948         case CEE_CONV_OVF_U:
949         case CEE_CONV_OVF_I_UN:
950         case CEE_CONV_OVF_U_UN:
951                 if (arg->stype == TYPE_INV || arg->stype == TYPE_VT)
952                         return arg->stype = TYPE_INV;
953                 return arg->stype = TYPE_PTR;
954         case CEE_CONV_I8:
955         case CEE_CONV_U8:
956         case CEE_CONV_OVF_I8:
957         case CEE_CONV_OVF_U8:
958         case CEE_CONV_OVF_I8_UN:
959         case CEE_CONV_OVF_U8_UN:
960                 return arg->stype = TYPE_I8;
961         case CEE_CONV_R4:
962         case CEE_CONV_R8:
963                 return arg->stype = TYPE_R8;
964         default:
965                 g_error ("opcode 0x%04x not handled in type from op", ins);
966                 break;
967         }
968         return FALSE;
969 }
970
971 static int
972 in_any_block (MonoMethodHeader *header, guint offset)
973 {
974         int i;
975         MonoExceptionClause *clause;
976
977         for (i = 0; i < header->num_clauses; ++i) {
978                 clause = &header->clauses [i];
979                 if (MONO_OFFSET_IN_CLAUSE (clause, offset))
980                         return 1;
981                 if (MONO_OFFSET_IN_HANDLER (clause, offset))
982                         return 1;
983                 /* need to check filter ... */
984         }
985         return 0;
986 }
987
988 static int
989 in_same_block (MonoMethodHeader *header, guint offset, guint target)
990 {
991         int i;
992         MonoExceptionClause *clause;
993
994         for (i = 0; i < header->num_clauses; ++i) {
995                 clause = &header->clauses [i];
996                 if (MONO_OFFSET_IN_CLAUSE (clause, offset) && !MONO_OFFSET_IN_CLAUSE (clause, target))
997                         return 0;
998                 if (MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
999                         return 0;
1000                 /* need to check filter ... */
1001         }
1002         return 1;
1003 }
1004
1005 /*
1006  * A leave can't escape a finally block 
1007  */
1008 static int
1009 is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
1010 {
1011         int i;
1012         MonoExceptionClause *clause;
1013
1014         for (i = 0; i < header->num_clauses; ++i) {
1015                 clause = &header->clauses [i];
1016                 if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
1017                         return 0;
1018                 /* need to check filter ... */
1019         }
1020         return 1;
1021 }
1022
1023 static int
1024 can_merge_stack (ILCodeDesc *a, ILCodeDesc *b)
1025 {
1026         if (!b->flags & CODE_SEEN) {
1027                 b->flags |= CODE_SEEN;
1028                 b->stack_count = a->stack_count;
1029                 /* merge types */
1030                 return 1;
1031         }
1032         if (a->stack_count != b->stack_count)
1033                 return 0;
1034         /* merge types */
1035         return 1;
1036 }
1037
1038 static int
1039 is_valid_bool_arg (ILStackDesc *arg)
1040 {
1041         switch (arg->stype) {
1042         case TYPE_I4:
1043         case TYPE_I8:
1044         case TYPE_PTR:
1045         case TYPE_MP:
1046         case TYPE_OBJ:
1047                 return TRUE;
1048         default:
1049                 return FALSE;
1050         }
1051 }
1052
1053 static int
1054 can_store_type (ILStackDesc *arg, MonoType *type)
1055 {
1056         int t = type->type;
1057         if (type->byref && arg->stype != TYPE_MP)
1058                 return FALSE;
1059 handle_enum:
1060         switch (t) {
1061         case MONO_TYPE_VOID:
1062                 return FALSE;
1063         case MONO_TYPE_I1:
1064         case MONO_TYPE_U1:
1065         case MONO_TYPE_BOOLEAN:
1066         case MONO_TYPE_I2:
1067         case MONO_TYPE_U2:
1068         case MONO_TYPE_CHAR:
1069         case MONO_TYPE_I4:
1070         case MONO_TYPE_U4:
1071                 if (arg->stype == TYPE_I4 || arg->stype == TYPE_PTR)
1072                         return TRUE;
1073                 return FALSE;
1074         case MONO_TYPE_I:
1075         case MONO_TYPE_U:
1076         case MONO_TYPE_PTR:
1077                 return TRUE;
1078         case MONO_TYPE_CLASS:
1079         case MONO_TYPE_STRING:
1080         case MONO_TYPE_OBJECT:
1081         case MONO_TYPE_SZARRAY:
1082         case MONO_TYPE_ARRAY:    
1083                 return TRUE; /* FIXME */
1084         case MONO_TYPE_I8:
1085         case MONO_TYPE_U8:
1086                 if (arg->stype == TYPE_I8)
1087                         return TRUE;
1088                 return FALSE;
1089         case MONO_TYPE_R4:
1090         case MONO_TYPE_R8:
1091                 if (arg->stype == TYPE_R8)
1092                         return TRUE;
1093                 return FALSE;
1094         case MONO_TYPE_VALUETYPE:
1095                 if (type->data.klass->enumtype) {
1096                         t = type->data.klass->enum_basetype->type;
1097                         goto handle_enum;
1098                 } else {
1099                         if (arg->type->data.klass != type->data.klass)
1100                                 return FALSE;
1101                         return TRUE;
1102                 }
1103         default:
1104                 g_error ("unknown type %02x in store type", type->type);
1105         }
1106         return FALSE;
1107 }
1108
1109 static int
1110 stind_type (int op, int type) {
1111         switch (op) {
1112         case CEE_STIND_REF:
1113                 return type == TYPE_OBJ;
1114         case CEE_STIND_I1:
1115         case CEE_STIND_I2:
1116         case CEE_STIND_I4:
1117                 return type == TYPE_I4;
1118         case CEE_STIND_I8:
1119                 return type == TYPE_I8;
1120         case CEE_STIND_R4:
1121         case CEE_STIND_R8:
1122                 return type == TYPE_R8;
1123         default:
1124                 g_assert_not_reached ();
1125         }
1126         return FALSE;
1127 }
1128
1129 /*
1130  * FIXME: need to distinguish between valid and verifiable.
1131  * Need to keep track of types on the stack.
1132  * Verify types for opcodes.
1133  */
1134 GSList*
1135 mono_method_verify (MonoMethod *method, int level)
1136 {
1137         MonoMethodHeader *header;
1138         MonoMethodSignature *signature, *csig;
1139         MonoGenericContext *generic_context = NULL;
1140         MonoMethod *cmethod;
1141         MonoClassField *field;
1142         MonoClass *klass;
1143         MonoImage *image;
1144         MonoType **params;
1145         ILStackDesc *stack;
1146         register const unsigned char *ip;
1147         register const unsigned char *end;
1148         const unsigned char *target = NULL; /* branch target */
1149         int max_args, max_stack, cur_stack, i, n, need_merge, start;
1150         guint32 token, ip_offset = 0;
1151         char *local_state = NULL;
1152         GSList *list = NULL;
1153         guint prefix = 0;
1154         ILCodeDesc *code;
1155
1156         if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1157                         (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
1158                 return NULL;
1159         }
1160         signature = method->signature;
1161         header = mono_method_get_header (method);
1162         ip = header->code;
1163         end = ip + header->code_size;
1164         max_args = signature->param_count + signature->hasthis;
1165         max_stack = header->max_stack;
1166         need_merge = cur_stack = 0;
1167         start = 1;
1168         image = method->klass->image;
1169         code = g_new0 (ILCodeDesc, header->code_size);
1170         stack = g_new0 (ILStackDesc, max_stack);
1171         if (signature->hasthis) {
1172                 params = g_new0 (MonoType*, max_args);
1173                 params [0] = &method->klass->this_arg;
1174                 memcpy (params + 1, signature->params, sizeof (MonoType*) * signature->param_count);
1175         } else {
1176                 params = signature->params;
1177         }
1178
1179         if (method->signature->is_inflated)
1180                 generic_context = ((MonoMethodInflated *) method)->context;
1181
1182         if (header->num_locals) {
1183                 local_state = g_new (char, header->num_locals);
1184                 memset (local_state, header->init_locals, header->num_locals);
1185         }
1186         /*g_print ("Method %s.%s::%s\n", method->klass->name_space, method->klass->name, method->name);*/
1187
1188         for (i = 0; i < header->num_clauses; ++i) {
1189                 MonoExceptionClause *clause = &header->clauses [i];
1190                 /* catch blocks have the exception on the stack. */
1191                 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
1192                         code [clause->handler_offset].stack_count = 1;
1193                         code [clause->handler_offset].flags |= CODE_SEEN;
1194                 }
1195         }
1196
1197         while (ip < end) {
1198                 ip_offset = ip - header->code;
1199                 if (start || !(code [ip_offset].flags & CODE_SEEN)) {
1200                         if (start) {
1201                                 /* g_print ("setting stack of IL_%04x to %d\n", ip_offset, 0); */
1202                                 cur_stack = code [ip_offset].stack_count;
1203                         } else {
1204                                 code [ip_offset].stack_count = cur_stack;
1205                         }
1206                         code [ip_offset].flags |= CODE_SEEN;
1207                 } else {
1208                         /* stack merge */
1209                         if (code [ip_offset].stack_count != cur_stack)
1210                                 ADD_INVALID (list, g_strdup_printf ("Cannot merge stack states at 0x%04x", ip_offset));
1211                 }
1212                 start = 0;
1213                 if (need_merge) {
1214                         if (!can_merge_stack (&code [ip_offset], &code [target - header->code]))
1215                                 ADD_INVALID (list, g_strdup_printf ("Cannot merge stack states at 0x%04x", ip_offset));
1216                         need_merge = 0;
1217                 }
1218 #if 0
1219                 {
1220                         char *discode;
1221                         discode = mono_disasm_code_one (NULL, method, ip, NULL);
1222                         discode [strlen (discode) - 1] = 0; /* no \n */
1223                         g_print ("%-29s (%d)\n", discode, cur_stack);
1224                         g_free (discode);
1225                 }
1226 #endif
1227
1228                 switch (*ip) {
1229                 case CEE_NOP:
1230                 case CEE_BREAK: 
1231                         ++ip;
1232                         break;
1233                 case CEE_LDARG_0:
1234                 case CEE_LDARG_1:
1235                 case CEE_LDARG_2:
1236                 case CEE_LDARG_3:
1237                         if (*ip - CEE_LDARG_0 >= max_args)
1238                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", *ip - CEE_LDARG_0, ip_offset));
1239                         CHECK_STACK_OVERFLOW ();
1240                         type_to_eval_stack_type (params [*ip - CEE_LDARG_0], stack + cur_stack, FALSE);
1241                         ++cur_stack;
1242                         ++ip;
1243                         break;
1244                 case CEE_LDLOC_0:
1245                 case CEE_LDLOC_1:
1246                 case CEE_LDLOC_2:
1247                 case CEE_LDLOC_3:
1248                         if (*ip - CEE_LDLOC_0 >= header->num_locals)
1249                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", *ip - CEE_LDLOC_0, ip_offset));
1250                         if (0 && !local_state [*ip - CEE_LDLOC_0])
1251                                 ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", *ip - CEE_LDLOC_0, ip_offset));
1252                         CHECK_STACK_OVERFLOW ();
1253                         type_to_eval_stack_type (header->locals [*ip - CEE_LDLOC_0], stack + cur_stack, FALSE);
1254                         ++cur_stack;
1255                         ++ip;
1256                         break;
1257                 case CEE_STLOC_0:
1258                 case CEE_STLOC_1:
1259                 case CEE_STLOC_2:
1260                 case CEE_STLOC_3:
1261                         if (*ip - CEE_STLOC_0 >= header->num_locals)
1262                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", *ip - CEE_STLOC_0, ip_offset));
1263                         local_state [*ip - CEE_STLOC_0] = 1;
1264                         CHECK_STACK_UNDERFLOW (1);
1265                         --cur_stack;
1266                         if (!can_store_type (stack + cur_stack, header->locals [*ip - CEE_STLOC_0]))
1267                                 ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in store at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1268                         ++ip;
1269                         break;
1270                 case CEE_LDARG_S:
1271                 case CEE_LDARGA_S:
1272                         if (ip [1] >= max_args)
1273                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", ip [1], ip_offset));
1274                         CHECK_STACK_OVERFLOW ();
1275                         type_to_eval_stack_type (params [ip [1]], stack + cur_stack, *ip == CEE_LDARGA_S);
1276                         ++cur_stack;
1277                         ip += 2;
1278                         break;
1279                 case CEE_STARG_S:
1280                         if (ip [1] >= max_args)
1281                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", ip [1], ip_offset));
1282                         CHECK_STACK_UNDERFLOW (1);
1283                         --cur_stack;
1284                         ip += 2;
1285                         break;
1286                 case CEE_LDLOC_S:
1287                 case CEE_LDLOCA_S:
1288                         if (ip [1] >= header->num_locals)
1289                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", ip [1], ip_offset));
1290                         /* no need to check if the var is initialized if the address is taken */
1291                         if (0 && *ip == CEE_LDLOC_S && !local_state [ip [1]])
1292                                 ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", ip [1], ip_offset));
1293                         CHECK_STACK_OVERFLOW ();
1294                         type_to_eval_stack_type (header->locals [ip [1]], stack + cur_stack, *ip == CEE_LDLOCA_S);
1295                         ++cur_stack;
1296                         ip += 2;
1297                         break;
1298                 case CEE_STLOC_S:
1299                         if (ip [1] >= header->num_locals)
1300                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", ip [1], ip_offset));
1301                         local_state [ip [1]] = 1;
1302                         CHECK_STACK_UNDERFLOW (1);
1303                         --cur_stack;
1304                         if (!can_store_type (stack + cur_stack, header->locals [ip [1]]))
1305                                 ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in store at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1306                         ip += 2;
1307                         break;
1308                 case CEE_LDNULL:
1309                         CHECK_STACK_OVERFLOW ();
1310                         stack [cur_stack].type = &mono_defaults.object_class->byval_arg;
1311                         stack [cur_stack].stype = TYPE_OBJ;
1312                         ++cur_stack;
1313                         ++ip;
1314                         break;
1315                 case CEE_LDC_I4_M1:
1316                 case CEE_LDC_I4_0:
1317                 case CEE_LDC_I4_1:
1318                 case CEE_LDC_I4_2:
1319                 case CEE_LDC_I4_3:
1320                 case CEE_LDC_I4_4:
1321                 case CEE_LDC_I4_5:
1322                 case CEE_LDC_I4_6:
1323                 case CEE_LDC_I4_7:
1324                 case CEE_LDC_I4_8:
1325                         CHECK_STACK_OVERFLOW ();
1326                         stack [cur_stack].type = &mono_defaults.int_class->byval_arg;
1327                         stack [cur_stack].stype = TYPE_I4;
1328                         ++cur_stack;
1329                         ++ip;
1330                         break;
1331                 case CEE_LDC_I4_S:
1332                         CHECK_STACK_OVERFLOW ();
1333                         stack [cur_stack].type = &mono_defaults.int_class->byval_arg;
1334                         stack [cur_stack].stype = TYPE_I4;
1335                         ++cur_stack;
1336                         ip += 2;
1337                         break;
1338                 case CEE_LDC_I4:
1339                         CHECK_STACK_OVERFLOW ();
1340                         stack [cur_stack].type = &mono_defaults.int_class->byval_arg;
1341                         stack [cur_stack].stype = TYPE_I4;
1342                         ++cur_stack;
1343                         ip += 5;
1344                         break;
1345                 case CEE_LDC_I8:
1346                         CHECK_STACK_OVERFLOW ();
1347                         stack [cur_stack].type = &mono_defaults.int64_class->byval_arg;
1348                         stack [cur_stack].stype = TYPE_I8;
1349                         ++cur_stack;
1350                         ip += 9;
1351                         break;
1352                 case CEE_LDC_R4:
1353                         CHECK_STACK_OVERFLOW ();
1354                         stack [cur_stack].type = &mono_defaults.double_class->byval_arg;
1355                         stack [cur_stack].stype = TYPE_R8;
1356                         ++cur_stack;
1357                         ip += 5;
1358                         break;
1359                 case CEE_LDC_R8:
1360                         CHECK_STACK_OVERFLOW ();
1361                         stack [cur_stack].type = &mono_defaults.double_class->byval_arg;
1362                         stack [cur_stack].stype = TYPE_R8;
1363                         ++cur_stack;
1364                         ip += 9;
1365                         break;
1366                 case CEE_UNUSED99: ++ip; break; /* warn/error instead? */
1367                 case CEE_DUP:
1368                         CHECK_STACK_UNDERFLOW (1);
1369                         CHECK_STACK_OVERFLOW ();
1370                         stack [cur_stack] = stack [cur_stack - 1];
1371                         ++cur_stack;
1372                         ++ip;
1373                         break;
1374                 case CEE_POP:
1375                         CHECK_STACK_UNDERFLOW (1);
1376                         --cur_stack;
1377                         ++ip;
1378                         break;
1379                 case CEE_JMP:
1380                         if (cur_stack)
1381                                 ADD_INVALID (list, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
1382                         token = read32 (ip + 1);
1383                         if (in_any_block (header, ip_offset))
1384                                 ADD_INVALID (list, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
1385                         /*
1386                          * FIXME: check signature, retval, arguments etc.
1387                          */
1388                         ip += 5;
1389                         break;
1390                 case CEE_CALL:
1391                 case CEE_CALLVIRT:
1392                         token = read32 (ip + 1);
1393                         /*
1394                          * FIXME: we could just load the signature ...
1395                          */
1396                         cmethod = mono_get_method_full (image, token, NULL, generic_context);
1397                         if (!cmethod)
1398                                 ADD_INVALID (list, g_strdup_printf ("Method 0x%08x not found at 0x%04x", token, ip_offset));
1399                         if (cmethod->signature->pinvoke) {
1400                                 csig = cmethod->signature;
1401                         } else {
1402                                 csig = mono_method_get_signature (cmethod, image, token);
1403                         }
1404
1405                         CHECK_STACK_UNDERFLOW (csig->param_count + csig->hasthis);
1406                         cur_stack -= csig->param_count + csig->hasthis;
1407                         if (csig->ret->type != MONO_TYPE_VOID) {
1408                                 CHECK_STACK_OVERFLOW ();
1409                                 type_to_eval_stack_type (csig->ret, stack + cur_stack, FALSE);
1410                                 ++cur_stack;
1411                         }
1412                         ip += 5;
1413                         break;
1414                 case CEE_CALLI:
1415                         token = read32 (ip + 1);
1416                         /*
1417                          * FIXME: check signature, retval, arguments etc.
1418                          */
1419                         ip += 5;
1420                         break;
1421                 case CEE_RET:
1422                         if (signature->ret->type != MONO_TYPE_VOID) {
1423                                 CHECK_STACK_UNDERFLOW (1);
1424                                 --cur_stack;
1425                                 if (!can_store_type (stack + cur_stack, signature->ret))
1426                                         ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in ret at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1427                         }
1428                         if (cur_stack)
1429                                 ADD_INVALID (list, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", cur_stack, ip_offset));
1430                         cur_stack = 0;
1431                         if (in_any_block (header, ip_offset))
1432                                 ADD_INVALID (list, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ip_offset));
1433                         ++ip;
1434                         start = 1;
1435                         break;
1436                 case CEE_BR_S:
1437                         target = ip + (signed char)ip [1] + 2;
1438                         if (target >= end || target < header->code)
1439                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1440                         if (!in_same_block (header, ip_offset, target - header->code))
1441                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1442                         ip += 2;
1443                         start = 1;
1444                         break;
1445                 case CEE_BRFALSE_S:
1446                 case CEE_BRTRUE_S:
1447                         target = ip + (signed char)ip [1] + 2;
1448                         if (target >= end || target < header->code)
1449                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1450                         if (!in_same_block (header, ip_offset, target - header->code))
1451                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1452                         CHECK_STACK_UNDERFLOW (1);
1453                         --cur_stack;
1454                         if (!is_valid_bool_arg (stack + cur_stack))
1455                                 ADD_INVALID (list, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1456                         ip += 2;
1457                         need_merge = 1;
1458                         break;
1459                 case CEE_BEQ_S:
1460                 case CEE_BGE_S:
1461                 case CEE_BGT_S:
1462                 case CEE_BLE_S:
1463                 case CEE_BLT_S:
1464                 case CEE_BNE_UN_S:
1465                 case CEE_BGE_UN_S:
1466                 case CEE_BGT_UN_S:
1467                 case CEE_BLE_UN_S:
1468                 case CEE_BLT_UN_S:
1469                         target = ip + (signed char)ip [1] + 2;
1470                         if (target >= end || target < header->code)
1471                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1472                         if (!in_same_block (header, ip_offset, target - header->code))
1473                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1474                         CHECK_STACK_UNDERFLOW (2);
1475                         cur_stack -= 2;
1476                         if (type_from_op (*ip, stack + cur_stack) == TYPE_INV)
1477                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1478                         ip += 2;
1479                         need_merge = 1;
1480                         break;
1481                 case CEE_BR:
1482                         target = ip + (gint32)read32 (ip + 1) + 5;
1483                         if (target >= end || target < header->code)
1484                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1485                         if (!in_same_block (header, ip_offset, target - header->code))
1486                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1487                         ip += 5;
1488                         start = 1;
1489                         break;
1490                 case CEE_BRFALSE:
1491                 case CEE_BRTRUE:
1492                         target = ip + (gint32)read32 (ip + 1) + 5;
1493                         if (target >= end || target < header->code)
1494                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1495                         if (!in_same_block (header, ip_offset, target - header->code))
1496                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1497                         CHECK_STACK_UNDERFLOW (1);
1498                         --cur_stack;
1499                         if (!is_valid_bool_arg (stack + cur_stack))
1500                                 ADD_INVALID (list, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1501                         ip += 5;
1502                         need_merge = 1;
1503                         break;
1504                 case CEE_BEQ:
1505                 case CEE_BGE:
1506                 case CEE_BGT:
1507                 case CEE_BLE:
1508                 case CEE_BLT:
1509                 case CEE_BNE_UN:
1510                 case CEE_BGE_UN:
1511                 case CEE_BGT_UN:
1512                 case CEE_BLE_UN:
1513                 case CEE_BLT_UN:
1514                         target = ip + (gint32)read32 (ip + 1) + 5;
1515                         if (target >= end || target < header->code)
1516                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1517                         if (!in_same_block (header, ip_offset, target - header->code))
1518                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1519                         CHECK_STACK_UNDERFLOW (2);
1520                         cur_stack -= 2;
1521                         if (type_from_op (*ip, stack + cur_stack) == TYPE_INV)
1522                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1523                         ip += 5;
1524                         need_merge = 1;
1525                         break;
1526                 case CEE_SWITCH:
1527                         n = read32 (ip + 1);
1528                         target = ip + sizeof (guint32) * n;
1529                         /* FIXME: check that ip is in range (and within the same exception block) */
1530                         for (i = 0; i < n; ++i)
1531                                 if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < header->code)
1532                                         ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1533                         CHECK_STACK_UNDERFLOW (1);
1534                         --cur_stack;
1535                         if (stack [cur_stack].stype != TYPE_I4)
1536                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to switch at 0x%04x", ip_offset));
1537                         ip += 5 + sizeof (guint32) * n;
1538                         break;
1539                 case CEE_LDIND_I1:
1540                 case CEE_LDIND_U1:
1541                 case CEE_LDIND_I2:
1542                 case CEE_LDIND_U2:
1543                 case CEE_LDIND_I4:
1544                 case CEE_LDIND_U4:
1545                 case CEE_LDIND_I8:
1546                 case CEE_LDIND_I:
1547                 case CEE_LDIND_R4:
1548                 case CEE_LDIND_R8:
1549                 case CEE_LDIND_REF:
1550                         CHECK_STACK_UNDERFLOW (1);
1551                         if (stack [cur_stack - 1].stype != TYPE_PTR && stack [cur_stack - 1].stype != TYPE_MP)
1552                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldind at 0x%04x", ip_offset));
1553                         stack [cur_stack - 1].stype = ldind_type [*ip - CEE_LDIND_I1];
1554                         ++ip;
1555                         break;
1556                 case CEE_STIND_REF:
1557                 case CEE_STIND_I1:
1558                 case CEE_STIND_I2:
1559                 case CEE_STIND_I4:
1560                 case CEE_STIND_I8:
1561                 case CEE_STIND_R4:
1562                 case CEE_STIND_R8:
1563                         CHECK_STACK_UNDERFLOW (2);
1564                         cur_stack -= 2;
1565                         if (stack [cur_stack].stype != TYPE_PTR && stack [cur_stack].stype != TYPE_MP)
1566                                 ADD_INVALID (list, g_strdup_printf ("Invalid pointer argument to stind at 0x%04x", ip_offset));
1567                         if (!stind_type (*ip, stack [cur_stack + 1].stype))
1568                                 ADD_INVALID (list, g_strdup_printf ("Incompatible value argument to stind at 0x%04x", ip_offset));
1569                         ++ip;
1570                         break;
1571                 case CEE_ADD:
1572                 case CEE_SUB:
1573                 case CEE_MUL:
1574                 case CEE_DIV:
1575                 case CEE_DIV_UN:
1576                 case CEE_REM:
1577                 case CEE_REM_UN:
1578                 case CEE_AND:
1579                 case CEE_OR:
1580                 case CEE_XOR:
1581                 case CEE_SHL:
1582                 case CEE_SHR:
1583                 case CEE_SHR_UN:
1584                         CHECK_STACK_UNDERFLOW (2);
1585                         --cur_stack;
1586                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1587                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1588                         ++ip;
1589                         break;
1590                 case CEE_NEG:
1591                 case CEE_NOT:
1592                 case CEE_CONV_I1:
1593                 case CEE_CONV_I2:
1594                 case CEE_CONV_I4:
1595                 case CEE_CONV_I8:
1596                 case CEE_CONV_R4:
1597                 case CEE_CONV_R8:
1598                 case CEE_CONV_U4:
1599                 case CEE_CONV_U8:
1600                         CHECK_STACK_UNDERFLOW (1);
1601                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1602                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1603                         ++ip;
1604                         break;
1605                 case CEE_CPOBJ:
1606                         token = read32 (ip + 1);
1607                         CHECK_STACK_UNDERFLOW (2);
1608                         cur_stack -= 2;
1609                         ip += 5;
1610                         break;
1611                 case CEE_LDOBJ:
1612                         token = read32 (ip + 1);
1613                         CHECK_STACK_UNDERFLOW (1);
1614                         if (stack [cur_stack - 1].stype != TYPE_MP)
1615                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldobj at 0x%04x", ip_offset));
1616                         klass = mono_class_get_full (image, token, generic_context);
1617                         if (!klass)
1618                                 ADD_INVALID (list, g_strdup_printf ("Cannot load class from token 0x%08x at 0x%04x", token, ip_offset));
1619                         if (!klass->valuetype)
1620                                 ADD_INVALID (list, g_strdup_printf ("Class is not a valuetype at 0x%04x", ip_offset));
1621                         stack [cur_stack - 1].stype = TYPE_VT;
1622                         stack [cur_stack - 1].type = &klass->byval_arg;
1623                         ip += 5;
1624                         break;
1625                 case CEE_LDSTR:
1626                         token = read32 (ip + 1);
1627                         CHECK_STACK_OVERFLOW ();
1628                         stack [cur_stack].type = &mono_defaults.string_class->byval_arg;
1629                         stack [cur_stack].stype = TYPE_OBJ;
1630                         ++cur_stack;
1631                         ip += 5;
1632                         break;
1633                 case CEE_NEWOBJ:
1634                         token = read32 (ip + 1);
1635                         /*
1636                          * FIXME: we could just load the signature ...
1637                          */
1638                         cmethod = mono_get_method_full (image, token, NULL, generic_context);
1639                         if (!cmethod)
1640                                 ADD_INVALID (list, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ip_offset));
1641                         csig = cmethod->signature;
1642                         CHECK_STACK_UNDERFLOW (csig->param_count);
1643                         cur_stack -= csig->param_count;
1644                         CHECK_STACK_OVERFLOW ();
1645                         stack [cur_stack].type = &cmethod->klass->byval_arg;
1646                         stack [cur_stack].stype = cmethod->klass->valuetype? TYPE_VT: TYPE_OBJ;
1647                         ++cur_stack;
1648                         ip += 5;
1649                         break;
1650                 case CEE_CASTCLASS:
1651                 case CEE_ISINST:
1652                         token = read32 (ip + 1);
1653                         CHECK_STACK_UNDERFLOW (1);
1654                         ip += 5;
1655                         break;
1656                 case CEE_CONV_R_UN:
1657                         CHECK_STACK_UNDERFLOW (1);
1658                         ++ip;
1659                         break;
1660                 case CEE_UNUSED58:
1661                 case CEE_UNUSED1:
1662                         ++ip; /* warn, error ? */
1663                         break;
1664                 case CEE_UNBOX:
1665                         token = read32 (ip + 1);
1666                         CHECK_STACK_UNDERFLOW (1);
1667                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1668                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument %s to unbox at 0x%04x", arg_name [stack [cur_stack - 1].stype], ip_offset));
1669                         stack [cur_stack - 1].stype = TYPE_MP;
1670                         stack [cur_stack - 1].type = NULL;
1671                         ip += 5;
1672                         break;
1673                 case CEE_THROW:
1674                         CHECK_STACK_UNDERFLOW (1);
1675                         --cur_stack;
1676                         ++ip;
1677                         start = 1;
1678                         break;
1679                 case CEE_LDFLD:
1680                         CHECK_STACK_UNDERFLOW (1);
1681                         if (stack [cur_stack - 1].stype != TYPE_OBJ && stack [cur_stack - 1].stype != TYPE_MP)
1682                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument %s to ldfld at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1683                         token = read32 (ip + 1);
1684                         field = mono_field_from_token (image, token, &klass, generic_context);
1685                         if (!field)
1686                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1687                         type_to_eval_stack_type (field->type, stack + cur_stack - 1, FALSE);
1688                         ip += 5;
1689                         break;
1690                 case CEE_LDFLDA:
1691                         CHECK_STACK_UNDERFLOW (1);
1692                         if (stack [cur_stack - 1].stype != TYPE_OBJ && stack [cur_stack - 1].stype != TYPE_MP)
1693                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldflda at 0x%04x", ip_offset));
1694                         token = read32 (ip + 1);
1695                         field = mono_field_from_token (image, token, &klass, generic_context);
1696                         if (!field)
1697                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1698                         type_to_eval_stack_type (field->type, stack + cur_stack - 1, TRUE);
1699                         ip += 5;
1700                         break;
1701                 case CEE_STFLD:
1702                         CHECK_STACK_UNDERFLOW (2);
1703                         cur_stack -= 2;
1704                         if (stack [cur_stack].stype != TYPE_OBJ && stack [cur_stack].stype != TYPE_MP)
1705                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to stfld at 0x%04x", ip_offset));
1706                         token = read32 (ip + 1);
1707                         field = mono_field_from_token (image, token, &klass, generic_context);
1708                         if (!field)
1709                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1710                         /* can_store */
1711                         ip += 5;
1712                         break;
1713                 case CEE_LDSFLD:
1714                         CHECK_STACK_OVERFLOW ();
1715                         token = read32 (ip + 1);
1716                         field = mono_field_from_token (image, token, &klass, generic_context);
1717                         if (!field)
1718                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1719                         type_to_eval_stack_type (field->type, stack + cur_stack, FALSE);
1720                         ++cur_stack;
1721                         ip += 5;
1722                         break;
1723                 case CEE_LDSFLDA:
1724                         CHECK_STACK_OVERFLOW ();
1725                         token = read32 (ip + 1);
1726                         field = mono_field_from_token (image, token, &klass, generic_context);
1727                         if (!field)
1728                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1729                         type_to_eval_stack_type (field->type, stack + cur_stack, TRUE);
1730                         ++cur_stack;
1731                         ip += 5;
1732                         break;
1733                 case CEE_STSFLD:
1734                         CHECK_STACK_UNDERFLOW (1);
1735                         --cur_stack;
1736                         token = read32 (ip + 1);
1737                         field = mono_field_from_token (image, token, &klass, generic_context);
1738                         if (!field)
1739                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1740                         /* can store */
1741                         ip += 5;
1742                         break;
1743                 case CEE_STOBJ:
1744                         CHECK_STACK_UNDERFLOW (2);
1745                         cur_stack -= 2;
1746                         token = read32 (ip + 1);
1747                         ip += 5;
1748                         break;
1749                 case CEE_CONV_OVF_I1_UN:
1750                 case CEE_CONV_OVF_I2_UN:
1751                 case CEE_CONV_OVF_I4_UN:
1752                 case CEE_CONV_OVF_I8_UN:
1753                 case CEE_CONV_OVF_U1_UN:
1754                 case CEE_CONV_OVF_U2_UN:
1755                 case CEE_CONV_OVF_U4_UN:
1756                 case CEE_CONV_OVF_U8_UN:
1757                 case CEE_CONV_OVF_I_UN:
1758                 case CEE_CONV_OVF_U_UN:
1759                         CHECK_STACK_UNDERFLOW (1);
1760                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1761                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1762                         ++ip;
1763                         break;
1764                 case CEE_BOX:
1765                         CHECK_STACK_UNDERFLOW (1);
1766                         token = read32 (ip + 1);
1767                         if (stack [cur_stack - 1].stype == TYPE_OBJ)
1768                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument %s to box at 0x%04x", arg_name [stack [cur_stack - 1].stype], ip_offset));
1769                         stack [cur_stack - 1].stype = TYPE_OBJ;
1770                         ip += 5;
1771                         break;
1772                 case CEE_NEWARR:
1773                         CHECK_STACK_UNDERFLOW (1);
1774                         token = read32 (ip + 1);
1775                         stack [cur_stack - 1].stype = TYPE_OBJ;
1776                         ip += 5;
1777                         break;
1778                 case CEE_LDLEN:
1779                         CHECK_STACK_UNDERFLOW (1);
1780                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1781                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldlen at 0x%04x", ip_offset));
1782                         stack [cur_stack - 1].type = &mono_defaults.int_class->byval_arg; /* FIXME: use a native int type */
1783                         stack [cur_stack - 1].stype = TYPE_PTR;
1784                         ++ip;
1785                         break;
1786                 case CEE_LDELEMA:
1787                         CHECK_STACK_UNDERFLOW (2);
1788                         --cur_stack;
1789                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1790                                 ADD_INVALID (list, g_strdup_printf ("Invalid array argument to ldelema at 0x%04x", ip_offset));
1791                         if (stack [cur_stack].stype != TYPE_I4 && stack [cur_stack].stype != TYPE_PTR)
1792                                 ADD_INVALID (list, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
1793                         stack [cur_stack - 1].stype = TYPE_MP;
1794                         token = read32 (ip + 1);
1795                         ip += 5;
1796                         break;
1797                 case CEE_LDELEM_I1:
1798                 case CEE_LDELEM_U1:
1799                 case CEE_LDELEM_I2:
1800                 case CEE_LDELEM_U2:
1801                 case CEE_LDELEM_I4:
1802                 case CEE_LDELEM_U4:
1803                 case CEE_LDELEM_I8:
1804                 case CEE_LDELEM_I:
1805                 case CEE_LDELEM_R4:
1806                 case CEE_LDELEM_R8:
1807                 case CEE_LDELEM_REF:
1808                         CHECK_STACK_UNDERFLOW (2);
1809                         --cur_stack;
1810                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1811                                 ADD_INVALID (list, g_strdup_printf ("Invalid array argument to ldelem at 0x%04x", ip_offset));
1812                         if (stack [cur_stack].stype != TYPE_I4 && stack [cur_stack].stype != TYPE_PTR)
1813                                 ADD_INVALID (list, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
1814                         stack [cur_stack - 1].stype = ldelem_type [*ip - CEE_LDELEM_I1];
1815                         ++ip;
1816                         break;
1817                 case CEE_STELEM_I:
1818                 case CEE_STELEM_I1:
1819                 case CEE_STELEM_I2:
1820                 case CEE_STELEM_I4:
1821                 case CEE_STELEM_I8:
1822                 case CEE_STELEM_R4:
1823                 case CEE_STELEM_R8:
1824                 case CEE_STELEM_REF:
1825                         CHECK_STACK_UNDERFLOW (3);
1826                         cur_stack -= 3;
1827                         ++ip;
1828                         break;
1829                 case CEE_LDELEM_ANY:
1830                 case CEE_STELEM_ANY:
1831                 case CEE_UNBOX_ANY:
1832                 case CEE_UNUSED5:
1833                 case CEE_UNUSED6:
1834                 case CEE_UNUSED7:
1835                 case CEE_UNUSED8:
1836                 case CEE_UNUSED9:
1837                 case CEE_UNUSED10:
1838                 case CEE_UNUSED11:
1839                 case CEE_UNUSED12:
1840                 case CEE_UNUSED13:
1841                 case CEE_UNUSED14:
1842                 case CEE_UNUSED15:
1843                 case CEE_UNUSED16:
1844                 case CEE_UNUSED17:
1845                         ++ip; /* warn, error ? */
1846                         break;
1847                 case CEE_CONV_OVF_I1:
1848                 case CEE_CONV_OVF_U1:
1849                 case CEE_CONV_OVF_I2:
1850                 case CEE_CONV_OVF_U2:
1851                 case CEE_CONV_OVF_I4:
1852                 case CEE_CONV_OVF_U4:
1853                 case CEE_CONV_OVF_I8:
1854                 case CEE_CONV_OVF_U8:
1855                         CHECK_STACK_UNDERFLOW (1);
1856                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1857                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1858                         ++ip;
1859                         break;
1860                 case CEE_UNUSED50:
1861                 case CEE_UNUSED18:
1862                 case CEE_UNUSED19:
1863                 case CEE_UNUSED20:
1864                 case CEE_UNUSED21:
1865                 case CEE_UNUSED22:
1866                 case CEE_UNUSED23:
1867                         ++ip; /* warn, error ? */
1868                         break;
1869                 case CEE_REFANYVAL:
1870                         CHECK_STACK_UNDERFLOW (1);
1871                         ++ip;
1872                         break;
1873                 case CEE_CKFINITE:
1874                         CHECK_STACK_UNDERFLOW (1);
1875                         ++ip;
1876                         break;
1877                 case CEE_UNUSED24:
1878                 case CEE_UNUSED25:
1879                         ++ip; /* warn, error ? */
1880                         break;
1881                 case CEE_MKREFANY:
1882                         CHECK_STACK_UNDERFLOW (1);
1883                         token = read32 (ip + 1);
1884                         ip += 5;
1885                         break;
1886                 case CEE_UNUSED59:
1887                 case CEE_UNUSED60:
1888                 case CEE_UNUSED61:
1889                 case CEE_UNUSED62:
1890                 case CEE_UNUSED63:
1891                 case CEE_UNUSED64:
1892                 case CEE_UNUSED65:
1893                 case CEE_UNUSED66:
1894                 case CEE_UNUSED67:
1895                         ++ip; /* warn, error ? */
1896                         break;
1897                 case CEE_LDTOKEN:
1898                         CHECK_STACK_OVERFLOW ();
1899                         token = read32 (ip + 1);
1900                         ++cur_stack;
1901                         ip += 5;
1902                         break;
1903                 case CEE_CONV_U2:
1904                 case CEE_CONV_U1:
1905                 case CEE_CONV_I:
1906                 case CEE_CONV_OVF_I:
1907                 case CEE_CONV_OVF_U:
1908                         CHECK_STACK_UNDERFLOW (1);
1909                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1910                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1911                         ++ip;
1912                         break;
1913                 case CEE_ADD_OVF:
1914                 case CEE_ADD_OVF_UN:
1915                 case CEE_MUL_OVF:
1916                 case CEE_MUL_OVF_UN:
1917                 case CEE_SUB_OVF:
1918                 case CEE_SUB_OVF_UN:
1919                         CHECK_STACK_UNDERFLOW (2);
1920                         --cur_stack;
1921                         ++ip;
1922                         break;
1923                 case CEE_ENDFINALLY:
1924                         ++ip;
1925                         start = 1;
1926                         break;
1927                 case CEE_LEAVE:
1928                         target = ip + (gint32)read32(ip + 1) + 5;
1929                         if (target >= end || target < header->code)
1930                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1931                         if (!is_correct_leave (header, ip_offset, target - header->code))
1932                                 ADD_INVALID (list, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
1933                         ip += 5;
1934                         start = 1;
1935                         break;
1936                 case CEE_LEAVE_S:
1937                         target = ip + (signed char)ip [1] + 2;
1938                         if (target >= end || target < header->code)
1939                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1940                         if (!is_correct_leave (header, ip_offset, target - header->code))
1941                                 ADD_INVALID (list, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
1942                         ip += 2;
1943                         start = 1;
1944                         break;
1945                 case CEE_STIND_I:
1946                         CHECK_STACK_UNDERFLOW (2);
1947                         cur_stack -= 2;
1948                         ++ip;
1949                         break;
1950                 case CEE_CONV_U:
1951                         CHECK_STACK_UNDERFLOW (1);
1952                         ++ip;
1953                         break;
1954                 case CEE_UNUSED26:
1955                 case CEE_UNUSED27:
1956                 case CEE_UNUSED28:
1957                 case CEE_UNUSED29:
1958                 case CEE_UNUSED30:
1959                 case CEE_UNUSED31:
1960                 case CEE_UNUSED32:
1961                 case CEE_UNUSED33:
1962                 case CEE_UNUSED34:
1963                 case CEE_UNUSED35:
1964                 case CEE_UNUSED36:
1965                 case CEE_UNUSED37:
1966                 case CEE_UNUSED38:
1967                 case CEE_UNUSED39:
1968                 case CEE_UNUSED40:
1969                 case CEE_UNUSED41:
1970                 case CEE_UNUSED42:
1971                 case CEE_UNUSED43:
1972                 case CEE_UNUSED44:
1973                 case CEE_UNUSED45:
1974                 case CEE_UNUSED46:
1975                 case CEE_UNUSED47:
1976                 case CEE_UNUSED48:
1977                         ++ip;
1978                         break;
1979                 case CEE_PREFIX7:
1980                 case CEE_PREFIX6:
1981                 case CEE_PREFIX5:
1982                 case CEE_PREFIX4:
1983                 case CEE_PREFIX3:
1984                 case CEE_PREFIX2:
1985                 case CEE_PREFIXREF:
1986                         ++ip;
1987                         break;
1988                 case CEE_PREFIX1:
1989                         ++ip;
1990                         switch (*ip) {
1991                         case CEE_ARGLIST:
1992                                 CHECK_STACK_OVERFLOW ();
1993                                 ++ip;
1994                                 break;
1995                         case CEE_CEQ:
1996                         case CEE_CGT:
1997                         case CEE_CGT_UN:
1998                         case CEE_CLT:
1999                         case CEE_CLT_UN:
2000                                 CHECK_STACK_UNDERFLOW (2);
2001                                 --cur_stack;
2002                                 if (type_from_op (256 + *ip, stack + cur_stack - 1) == TYPE_INV)
2003                                         ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0xFE 0x%02x at 0x%04x", *ip, ip_offset));
2004                                 ++ip;
2005                                 break;
2006                         case CEE_LDFTN:
2007                                 CHECK_STACK_OVERFLOW ();
2008                                 token = read32 (ip + 1);
2009                                 ip += 5;
2010                                 stack [cur_stack].stype = TYPE_PTR;
2011                                 cur_stack++;
2012                                 break;
2013                         case CEE_LDVIRTFTN:
2014                                 CHECK_STACK_UNDERFLOW (1);
2015                                 token = read32 (ip + 1);
2016                                 ip += 5;
2017                                 if (stack [cur_stack - 1].stype != TYPE_OBJ)
2018                                         ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ip_offset));
2019                                 stack [cur_stack - 1].stype = TYPE_PTR;
2020                                 break;
2021                         case CEE_UNUSED56:
2022                                 ++ip;
2023                                 break;
2024                         case CEE_LDARG:
2025                         case CEE_LDARGA:
2026                                 if (read16 (ip + 1) >= max_args)
2027                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", read16 (ip + 1), ip_offset));
2028                                 CHECK_STACK_OVERFLOW ();
2029                                 ++cur_stack;
2030                                 ip += 3;
2031                                 break;
2032                         case CEE_STARG:
2033                                 if (read16 (ip + 1) >= max_args)
2034                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", read16(ip + 1), ip_offset));
2035                                 CHECK_STACK_UNDERFLOW (1);
2036                                 --cur_stack;
2037                                 ip += 3;
2038                                 break;
2039                         case CEE_LDLOC:
2040                         case CEE_LDLOCA:
2041                                 n = read16 (ip + 1);
2042                                 if (n >= header->num_locals)
2043                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", n, ip_offset));
2044                                 /* no need to check if the var is initialized if the address is taken */
2045                                 if (0 && *ip == CEE_LDLOC && !local_state [n])
2046                                         ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", n, ip_offset));
2047                                 CHECK_STACK_OVERFLOW ();
2048                                 type_to_eval_stack_type (header->locals [n], stack + cur_stack, *ip == CEE_LDLOCA);
2049                                 ++cur_stack;
2050                                 ip += 3;
2051                                 break;
2052                         case CEE_STLOC:
2053                                 n = read16 (ip + 1);
2054                                 if (n >= header->num_locals)
2055                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", n, ip_offset));
2056                                 local_state [n] = 1;
2057                                 CHECK_STACK_UNDERFLOW (1);
2058                                 --cur_stack;
2059                                 if (!can_store_type (stack + cur_stack, header->locals [n]))
2060                                         ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in store at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
2061                                 ip += 3;
2062                                 break;
2063                         case CEE_LOCALLOC:
2064                                 if (cur_stack != 1)
2065                                         ADD_INVALID (list, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ip_offset));
2066                                 if (stack [cur_stack -1].stype != TYPE_I4 && stack [cur_stack -1].stype != TYPE_PTR)
2067                                         ADD_INVALID (list, g_strdup_printf ("Invalid argument to localloc at 0x%04x", ip_offset));
2068                                 stack [cur_stack -1].stype = TYPE_MP;
2069                                 ++ip;
2070                                 break;
2071                         case CEE_UNUSED57:
2072                                 ++ip;
2073                                 break;
2074                         case CEE_ENDFILTER:
2075                                 if (cur_stack != 1)
2076                                         ADD_INVALID (list, g_strdup_printf ("Stack must have only filter result in endfilter at 0x%04x", ip_offset));
2077                                 ++ip;
2078                                 break;
2079                         case CEE_UNALIGNED_:
2080                                 prefix |= PREFIX_UNALIGNED;
2081                                 ++ip;
2082                                 break;
2083                         case CEE_VOLATILE_:
2084                                 prefix |= PREFIX_VOLATILE;
2085                                 ++ip;
2086                                 break;
2087                         case CEE_TAIL_:
2088                                 prefix |= PREFIX_TAIL;
2089                                 ++ip;
2090                                 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
2091                                         ADD_INVALID (list, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
2092                                 break;
2093                         case CEE_INITOBJ:
2094                                 CHECK_STACK_UNDERFLOW (1);
2095                                 token = read32 (ip + 1);
2096                                 ip += 5;
2097                                 --cur_stack;
2098                                 break;
2099                         case CEE_CONSTRAINED_:
2100                                 token = read32 (ip + 1);
2101                                 ip += 5;
2102                                 break;
2103                         case CEE_CPBLK:
2104                                 CHECK_STACK_UNDERFLOW (3);
2105                                 ip++;
2106                                 break;
2107                         case CEE_INITBLK:
2108                                 CHECK_STACK_UNDERFLOW (3);
2109                                 ip++;
2110                                 break;
2111                         case CEE_NO_:
2112                                 ip += 2;
2113                                 break;
2114                         case CEE_RETHROW:
2115                                 ++ip;
2116                                 break;
2117                         case CEE_UNUSED:
2118                                 ++ip;
2119                                 break;
2120                         case CEE_SIZEOF:
2121                                 CHECK_STACK_OVERFLOW ();
2122                                 token = read32 (ip + 1);
2123                                 ip += 5;
2124                                 stack [cur_stack].type = &mono_defaults.uint_class->byval_arg;
2125                                 stack [cur_stack].stype = TYPE_I4;
2126                                 cur_stack++;
2127                                 break;
2128                         case CEE_REFANYTYPE:
2129                                 CHECK_STACK_UNDERFLOW (1);
2130                                 ++ip;
2131                                 break;
2132                         case CEE_UNUSED53:
2133                         case CEE_UNUSED54:
2134                         case CEE_UNUSED55:
2135                         case CEE_UNUSED70:
2136                                 ++ip;
2137                                 break;
2138                         }
2139                 }
2140         }
2141         /*
2142          * if ip != end we overflowed: mark as error.
2143          */
2144         if (ip != end || !start) {
2145                 ADD_INVALID (list, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
2146         }
2147 invalid_cil:
2148
2149         g_free (local_state);
2150         g_free (code);
2151         g_free (stack);
2152         if (signature->hasthis)
2153                 g_free (params);
2154         return list;
2155 }
2156
2157 typedef struct {
2158         const char *name;
2159         guint64 offset;
2160 } FieldDesc;
2161
2162 typedef struct {
2163         const char *name;
2164         const FieldDesc *fields;
2165 } ClassDesc;
2166
2167 static const FieldDesc 
2168 typebuilder_fields[] = {
2169         {"tname", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, name)},
2170         {"nspace", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, nspace)},
2171         {"parent", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, parent)},
2172         {"interfaces", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, interfaces)},
2173         {"methods", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, methods)},
2174         {"properties", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, properties)},
2175         {"fields", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, fields)},
2176         {"attrs", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, attrs)},
2177         {"table_idx", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, table_idx)},
2178         {NULL, 0}
2179 };
2180
2181 static const FieldDesc 
2182 modulebuilder_fields[] = {
2183         {"types", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, types)},
2184         {"cattrs", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, cattrs)},
2185         {"guid", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, guid)},
2186         {"table_idx", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, table_idx)},
2187         {NULL, 0}
2188 };
2189
2190 static const FieldDesc 
2191 assemblybuilder_fields[] = {
2192         {"entry_point", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, entry_point)},
2193         {"modules", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, modules)},
2194         {"name", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, name)},
2195         {"resources", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, resources)},
2196         {"version", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, version)},
2197         {"culture", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, culture)},
2198         {NULL, 0}
2199 };
2200
2201 static const FieldDesc 
2202 ctorbuilder_fields[] = {
2203         {"ilgen", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, ilgen)},
2204         {"parameters", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, parameters)},
2205         {"attrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, attrs)},
2206         {"iattrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, iattrs)},
2207         {"table_idx", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, table_idx)},
2208         {"call_conv", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, call_conv)},
2209         {"type", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, type)},
2210         {NULL, 0}
2211 };
2212
2213 static const FieldDesc 
2214 methodbuilder_fields[] = {
2215         {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, mhandle)},
2216         {"rtype", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, rtype)},
2217         {"parameters", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, parameters)},
2218         {"attrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, attrs)},
2219         {"iattrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, iattrs)},
2220         {"name", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, name)},
2221         {"table_idx", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, table_idx)},
2222         {"code", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, code)},
2223         {"ilgen", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, ilgen)},
2224         {"type", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, type)},
2225         {"pinfo", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, pinfo)},
2226         {"pi_dll", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dll)},
2227         {"pi_entry", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dllentry)},
2228         {"ncharset", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, charset)},
2229         {"native_cc", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, native_cc)},
2230         {"call_conv", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, call_conv)},
2231         {NULL, 0}
2232 };
2233
2234 static const FieldDesc 
2235 fieldbuilder_fields[] = {
2236         {"attrs", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, attrs)},
2237         {"type", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, type)},
2238         {"name", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, name)},
2239         {"def_value", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, def_value)},
2240         {"offset", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, offset)},
2241         {"table_idx", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, table_idx)},
2242         {NULL, 0}
2243 };
2244
2245 static const FieldDesc 
2246 propertybuilder_fields[] = {
2247         {"attrs", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, attrs)},
2248         {"name", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, name)},
2249         {"type", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, type)},
2250         {"parameters", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, parameters)},
2251         {"def_value", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, def_value)},
2252         {"set_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, set_method)},
2253         {"get_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, get_method)},
2254         {"table_idx", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, table_idx)},
2255         {NULL, 0}
2256 };
2257
2258 static const FieldDesc 
2259 ilgenerator_fields[] = {
2260         {"code", G_STRUCT_OFFSET (MonoReflectionILGen, code)},
2261         {"code_len", G_STRUCT_OFFSET (MonoReflectionILGen, code_len)},
2262         {"max_stack", G_STRUCT_OFFSET (MonoReflectionILGen, max_stack)},
2263         {"cur_stack", G_STRUCT_OFFSET (MonoReflectionILGen, cur_stack)},
2264         {"locals", G_STRUCT_OFFSET (MonoReflectionILGen, locals)},
2265         {"ex_handlers", G_STRUCT_OFFSET (MonoReflectionILGen, ex_handlers)},
2266         {NULL, 0}
2267 };
2268
2269 static const FieldDesc 
2270 ilexinfo_fields[] = {
2271         {"handlers", G_STRUCT_OFFSET (MonoILExceptionInfo, handlers)},
2272         {"start", G_STRUCT_OFFSET (MonoILExceptionInfo, start)},
2273         {"len", G_STRUCT_OFFSET (MonoILExceptionInfo, len)},
2274         {"end", G_STRUCT_OFFSET (MonoILExceptionInfo, label)},
2275         {NULL, 0}
2276 };
2277
2278 static const FieldDesc 
2279 ilexblock_fields[] = {
2280         {"extype", G_STRUCT_OFFSET (MonoILExceptionBlock, extype)},
2281         {"type", G_STRUCT_OFFSET (MonoILExceptionBlock, type)},
2282         {"start", G_STRUCT_OFFSET (MonoILExceptionBlock, start)},
2283         {"len", G_STRUCT_OFFSET (MonoILExceptionBlock, len)},
2284         {"filter_offset", G_STRUCT_OFFSET (MonoILExceptionBlock, filter_offset)},
2285         {NULL, 0}
2286 };
2287
2288 static const ClassDesc
2289 emit_classes_to_check [] = {
2290         {"TypeBuilder", typebuilder_fields},
2291         {"ModuleBuilder", modulebuilder_fields},
2292         {"AssemblyBuilder", assemblybuilder_fields},
2293         {"ConstructorBuilder", ctorbuilder_fields},
2294         {"MethodBuilder", methodbuilder_fields},
2295         {"FieldBuilder", fieldbuilder_fields},
2296         {"PropertyBuilder", propertybuilder_fields},
2297         {"ILGenerator", ilgenerator_fields},
2298         {"ILExceptionBlock", ilexblock_fields},
2299         {"ILExceptionInfo", ilexinfo_fields},
2300         {NULL, NULL}
2301 };
2302
2303 static const FieldDesc 
2304 monoevent_fields[] = {
2305         {"klass", G_STRUCT_OFFSET (MonoReflectionEvent, klass)},
2306         {"handle", G_STRUCT_OFFSET (MonoReflectionEvent, event)},
2307         {NULL, 0}
2308 };
2309
2310 static const FieldDesc 
2311 monoproperty_fields[] = {
2312         {"klass", G_STRUCT_OFFSET (MonoReflectionProperty, klass)},
2313         {"prop", G_STRUCT_OFFSET (MonoReflectionProperty, property)},
2314         {NULL, 0}
2315 };
2316
2317 static const FieldDesc 
2318 monofield_fields[] = {
2319         {"klass", G_STRUCT_OFFSET (MonoReflectionField, klass)},
2320         {"fhandle", G_STRUCT_OFFSET (MonoReflectionField, field)},
2321         {NULL, 0}
2322 };
2323
2324 static const FieldDesc 
2325 monomethodinfo_fields[] = {
2326         {"parent", G_STRUCT_OFFSET (MonoMethodInfo, parent)},
2327         {"ret", G_STRUCT_OFFSET (MonoMethodInfo, ret)},
2328         {"attrs", G_STRUCT_OFFSET (MonoMethodInfo, attrs)},
2329         {"iattrs", G_STRUCT_OFFSET (MonoMethodInfo, implattrs)},
2330         {NULL, 0}
2331 };
2332
2333 static const FieldDesc 
2334 monopropertyinfo_fields[] = {
2335         {"parent", G_STRUCT_OFFSET (MonoPropertyInfo, parent)},
2336         {"name", G_STRUCT_OFFSET (MonoPropertyInfo, name)},
2337         {"get_method", G_STRUCT_OFFSET (MonoPropertyInfo, get)},
2338         {"set_method", G_STRUCT_OFFSET (MonoPropertyInfo, set)},
2339         {"attrs", G_STRUCT_OFFSET (MonoPropertyInfo, attrs)},
2340         {NULL, 0}
2341 };
2342
2343 static const FieldDesc 
2344 monomethod_fields[] = {
2345         {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
2346         {NULL, 0}
2347 };
2348
2349 static const FieldDesc 
2350 monocmethod_fields[] = {
2351         {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
2352         {NULL, 0}
2353 };
2354
2355 static const FieldDesc 
2356 pinfo_fields[] = {
2357         {"ClassImpl", G_STRUCT_OFFSET (MonoReflectionParameter, ClassImpl)},
2358         {"DefaultValueImpl", G_STRUCT_OFFSET (MonoReflectionParameter, DefaultValueImpl)},
2359         {"MemberImpl", G_STRUCT_OFFSET (MonoReflectionParameter, MemberImpl)},
2360         {"NameImpl", G_STRUCT_OFFSET (MonoReflectionParameter, NameImpl)},
2361         {"PositionImpl", G_STRUCT_OFFSET (MonoReflectionParameter, PositionImpl)},
2362         {"AttrsImpl", G_STRUCT_OFFSET (MonoReflectionParameter, AttrsImpl)},
2363         {NULL, 0}
2364 };
2365
2366 static const ClassDesc
2367 reflection_classes_to_check [] = {
2368         {"MonoEvent", monoevent_fields},
2369         {"MonoProperty", monoproperty_fields},
2370         {"MonoField", monofield_fields},
2371         {"MonoMethodInfo", monomethodinfo_fields},
2372         {"MonoPropertyInfo", monopropertyinfo_fields},
2373         {"MonoMethod", monomethod_fields},
2374         {"MonoCMethod", monocmethod_fields},
2375         {"ParameterInfo", pinfo_fields},
2376         {NULL, NULL}
2377 };
2378
2379 static FieldDesc 
2380 enuminfo_fields[] = {
2381         {"utype", G_STRUCT_OFFSET (MonoEnumInfo, utype)},
2382         {"values", G_STRUCT_OFFSET (MonoEnumInfo, values)},
2383         {"names", G_STRUCT_OFFSET (MonoEnumInfo, names)},
2384         {NULL, 0}
2385 };
2386
2387 static FieldDesc 
2388 delegate_fields[] = {
2389         {"target_type", G_STRUCT_OFFSET (MonoDelegate, target_type)},
2390         {"m_target", G_STRUCT_OFFSET (MonoDelegate, target)},
2391         {"method_name", G_STRUCT_OFFSET (MonoDelegate, method_name)},
2392         {"method_ptr", G_STRUCT_OFFSET (MonoDelegate, method_ptr)},
2393         {"delegate_trampoline", G_STRUCT_OFFSET (MonoDelegate, delegate_trampoline)},
2394         {"method_info", G_STRUCT_OFFSET (MonoDelegate, method_info)},
2395         {NULL, 0}
2396 };
2397
2398 static FieldDesc 
2399 multicast_delegate_fields[] = {
2400         {"prev", G_STRUCT_OFFSET (MonoMulticastDelegate, prev)},
2401         {NULL, 0}
2402 };
2403
2404 static FieldDesc 
2405 async_result_fields[] = {
2406         {"async_state", G_STRUCT_OFFSET (MonoAsyncResult, async_state)},
2407         {"handle", G_STRUCT_OFFSET (MonoAsyncResult, handle)},
2408         {"async_delegate", G_STRUCT_OFFSET (MonoAsyncResult, async_delegate)},
2409         {"data", G_STRUCT_OFFSET (MonoAsyncResult, data)},
2410         {"sync_completed", G_STRUCT_OFFSET (MonoAsyncResult, sync_completed)},
2411         {"completed", G_STRUCT_OFFSET (MonoAsyncResult, completed)},
2412         {"endinvoke_called", G_STRUCT_OFFSET (MonoAsyncResult, endinvoke_called)},
2413         {"async_callback", G_STRUCT_OFFSET (MonoAsyncResult, async_callback)},
2414         {NULL, 0}
2415 };
2416
2417 static FieldDesc 
2418 exception_fields[] = {
2419         {"trace_ips", G_STRUCT_OFFSET (MonoException, trace_ips)},
2420         {"inner_exception", G_STRUCT_OFFSET (MonoException, inner_ex)},
2421         {"message", G_STRUCT_OFFSET (MonoException, message)},
2422         {"help_link", G_STRUCT_OFFSET (MonoException, help_link)},
2423         {"class_name", G_STRUCT_OFFSET (MonoException, class_name)},
2424         {"stack_trace", G_STRUCT_OFFSET (MonoException, stack_trace)},
2425         {"remote_stack_trace", G_STRUCT_OFFSET (MonoException, remote_stack_trace)},
2426         {"remote_stack_index", G_STRUCT_OFFSET (MonoException, remote_stack_index)},
2427         {"hresult", G_STRUCT_OFFSET (MonoException, hresult)},
2428         {"source", G_STRUCT_OFFSET (MonoException, source)},
2429         {NULL, 0}
2430 };
2431
2432 static const ClassDesc
2433 system_classes_to_check [] = {
2434         {"Exception", exception_fields},
2435         {"MonoEnumInfo", enuminfo_fields},
2436         {"Delegate", delegate_fields},
2437         {"MulticastDelegate", multicast_delegate_fields},
2438         {NULL, NULL}
2439 };
2440
2441 static FieldDesc 
2442 stack_frame_fields [] = {
2443         {"ilOffset", G_STRUCT_OFFSET (MonoStackFrame, il_offset)},
2444         {"nativeOffset", G_STRUCT_OFFSET (MonoStackFrame, native_offset)},
2445         {"methodBase", G_STRUCT_OFFSET (MonoStackFrame, method)},
2446         {"fileName", G_STRUCT_OFFSET (MonoStackFrame, filename)},
2447         {"lineNumber", G_STRUCT_OFFSET (MonoStackFrame, line)},
2448         {"columnNumber", G_STRUCT_OFFSET (MonoStackFrame, column)},
2449         {NULL, 0}
2450 };
2451
2452 static const ClassDesc
2453 system_diagnostics_classes_to_check [] = {
2454         {"StackFrame", stack_frame_fields},
2455         {NULL, NULL}
2456 };
2457
2458 static FieldDesc 
2459 mono_method_message_fields[] = {
2460         {"method", G_STRUCT_OFFSET (MonoMethodMessage, method)},
2461         {"args", G_STRUCT_OFFSET (MonoMethodMessage, args)},
2462         {"names", G_STRUCT_OFFSET (MonoMethodMessage, names)},
2463         {"arg_types", G_STRUCT_OFFSET (MonoMethodMessage, arg_types)},
2464         {"ctx", G_STRUCT_OFFSET (MonoMethodMessage, ctx)},
2465         {"rval", G_STRUCT_OFFSET (MonoMethodMessage, rval)},
2466         {"exc", G_STRUCT_OFFSET (MonoMethodMessage, exc)},
2467         {NULL, 0}
2468 };
2469
2470 static const ClassDesc
2471 messaging_classes_to_check [] = {
2472         {"AsyncResult", async_result_fields},
2473         {"MonoMethodMessage", mono_method_message_fields},
2474         {NULL, NULL}
2475 };
2476
2477 static FieldDesc 
2478 transparent_proxy_fields[] = {
2479         {"_rp", G_STRUCT_OFFSET (MonoTransparentProxy, rp)},
2480         {"_class", G_STRUCT_OFFSET (MonoTransparentProxy, remote_class)},
2481         {NULL, 0}
2482 };
2483
2484 static FieldDesc 
2485 real_proxy_fields[] = {
2486         {"class_to_proxy", G_STRUCT_OFFSET (MonoRealProxy, class_to_proxy)},
2487         {NULL, 0}
2488 };
2489
2490 static const ClassDesc
2491 proxy_classes_to_check [] = {
2492         {"TransparentProxy", transparent_proxy_fields},
2493         {"RealProxy", real_proxy_fields},
2494         {NULL, NULL}
2495 };
2496
2497 static FieldDesc 
2498 wait_handle_fields[] = {
2499         {"os_handle", G_STRUCT_OFFSET (MonoWaitHandle, handle)},
2500         {"disposed", G_STRUCT_OFFSET (MonoWaitHandle, disposed)},
2501         {NULL, 0}
2502 };
2503
2504 static FieldDesc 
2505 thread_fields[] = {
2506         {"system_thread_handle", G_STRUCT_OFFSET (MonoThread, handle)},
2507         {"current_culture", G_STRUCT_OFFSET (MonoThread, culture_info)},
2508         {"threadpool_thread", G_STRUCT_OFFSET (MonoThread, threadpool_thread)},
2509         {"state", G_STRUCT_OFFSET (MonoThread, state)},
2510         {"abort_exc", G_STRUCT_OFFSET (MonoThread, abort_exc)},
2511         {"abort_state", G_STRUCT_OFFSET (MonoThread, abort_state)},
2512         {"thread_id", G_STRUCT_OFFSET (MonoThread, tid)},
2513         {NULL, 0}
2514 };
2515
2516 static const ClassDesc
2517 threading_classes_to_check [] = {
2518         {"Thread", thread_fields},
2519         {"WaitHandle", wait_handle_fields},
2520         {NULL, NULL}
2521 };
2522
2523 static const FieldDesc
2524 cinfo_fields[] = {
2525         {"datetime_format", G_STRUCT_OFFSET (MonoCultureInfo, datetime_format)},
2526         {"number_format", G_STRUCT_OFFSET (MonoCultureInfo, number_format)},
2527         {"textinfo", G_STRUCT_OFFSET (MonoCultureInfo, textinfo)},
2528         {"name", G_STRUCT_OFFSET (MonoCultureInfo, name)},
2529         {"displayname", G_STRUCT_OFFSET (MonoCultureInfo, displayname)},
2530         {"englishname", G_STRUCT_OFFSET (MonoCultureInfo, englishname)},
2531         {"nativename", G_STRUCT_OFFSET (MonoCultureInfo, nativename)},
2532         {"iso3lang", G_STRUCT_OFFSET (MonoCultureInfo, iso3lang)},
2533         {"iso2lang", G_STRUCT_OFFSET (MonoCultureInfo, iso2lang)},
2534         {"icu_name", G_STRUCT_OFFSET (MonoCultureInfo, icu_name)},
2535         {"win3lang", G_STRUCT_OFFSET (MonoCultureInfo, win3lang)},
2536         {"compareinfo", G_STRUCT_OFFSET (MonoCultureInfo, compareinfo)},
2537         {NULL, 0}
2538 };
2539
2540 static const FieldDesc
2541 dtfinfo_fields[] = {
2542         {"_AMDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, AMDesignator)},
2543         {"_PMDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, PMDesignator)},
2544         {"_DayNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, DayNames)},
2545         {"_MonthNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, MonthNames)},
2546         {NULL, 0}
2547 };
2548
2549 static const FieldDesc
2550 nfinfo_fields[] = {
2551         {"decimalFormats", G_STRUCT_OFFSET (MonoNumberFormatInfo, decimalFormats)},
2552         {"currencySymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, currencySymbol)},
2553         {"percentSymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, percentSymbol)},
2554         {"positiveSign", G_STRUCT_OFFSET (MonoNumberFormatInfo, positiveSign)},
2555         {NULL, 0}
2556 };
2557
2558 static const FieldDesc
2559 compinfo_fields[] = {
2560         {"lcid", G_STRUCT_OFFSET (MonoCompareInfo, lcid)},
2561         {"ICU_collator", G_STRUCT_OFFSET (MonoCompareInfo, ICU_collator)},
2562         {NULL, 0}
2563 };
2564
2565 static const FieldDesc
2566 sortkey_fields[] = {
2567         {"str", G_STRUCT_OFFSET (MonoSortKey, str)},
2568         {"options", G_STRUCT_OFFSET (MonoSortKey, options)},
2569         {"key", G_STRUCT_OFFSET (MonoSortKey, key)},
2570         {"lcid", G_STRUCT_OFFSET (MonoSortKey, lcid)},
2571         {NULL, 0}
2572 };
2573
2574 static const ClassDesc
2575 globalization_classes_to_check [] = {
2576         {"CultureInfo", cinfo_fields},
2577         {"DateTimeFormatInfo", dtfinfo_fields},
2578         {"NumberFormatInfo", nfinfo_fields},
2579         {"CompareInfo", compinfo_fields},
2580         {"SortKey", sortkey_fields},
2581         {NULL, NULL}
2582 };
2583
2584 typedef struct {
2585         const char *name;
2586         const ClassDesc *types;
2587 } NameSpaceDesc;
2588
2589 static const NameSpaceDesc
2590 namespaces_to_check[] = {
2591         {"System.Runtime.Remoting.Proxies", proxy_classes_to_check},
2592         {"System.Runtime.Remoting.Messaging", messaging_classes_to_check},
2593         {"System.Reflection.Emit", emit_classes_to_check},
2594         {"System.Reflection", reflection_classes_to_check},
2595         {"System.Threading", threading_classes_to_check},
2596         {"System.Diagnostics", system_diagnostics_classes_to_check},
2597         {"System", system_classes_to_check},
2598         {"System.Globalization", globalization_classes_to_check},
2599         {NULL, NULL}
2600 };
2601
2602 static char*
2603 check_corlib (MonoImage *corlib)
2604 {
2605         MonoClass *klass;
2606         MonoClassField *field;
2607         const FieldDesc *fdesc;
2608         const ClassDesc *cdesc;
2609         const NameSpaceDesc *ndesc;
2610         gint struct_offset;
2611         GString *result = NULL;
2612
2613         for (ndesc = namespaces_to_check; ndesc->name; ++ndesc) {
2614                 for (cdesc = ndesc->types; cdesc->name; ++cdesc) {
2615                         klass = mono_class_from_name (corlib, ndesc->name, cdesc->name);
2616                         if (!klass) {
2617                                 if (!result)
2618                                         result = g_string_new ("");
2619                                 g_string_append_printf (result, "Cannot find class %s\n", cdesc->name);
2620                                 continue;
2621                         }
2622                         mono_class_init (klass);
2623                         /*
2624                          * FIXME: we should also check the size of valuetypes, or
2625                          * we're going to have trouble when we access them in arrays.
2626                          */
2627                         if (klass->valuetype)
2628                                 struct_offset = sizeof (MonoObject);
2629                         else
2630                                 struct_offset = 0;
2631                         for (fdesc = cdesc->fields; fdesc->name; ++fdesc) {
2632                                 field = mono_class_get_field_from_name (klass, fdesc->name);
2633                                 if (!field || (field->offset != (fdesc->offset + struct_offset))) {
2634                                         if (!result)
2635                                                 result = g_string_new ("");
2636                                         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));
2637                                 }
2638                         }
2639                 }
2640         }
2641         if (result)
2642                 return g_string_free (result, FALSE);
2643         return NULL;
2644 }
2645
2646 char*
2647 mono_verify_corlib () {
2648         return check_corlib (mono_defaults.corlib);
2649 }
2650
2651