2003-12-10 Bernie Solomon <bernard@ugsolutions.com>
[mono.git] / mono / metadata / verify.c
1
2 #include <mono/metadata/object.h>
3 #include <mono/metadata/verify.h>
4 #include <mono/metadata/opcodes.h>
5 #include <mono/metadata/tabledefs.h>
6 #include <mono/metadata/reflection.h>
7 #include <mono/metadata/debug-helpers.h>
8 #include <mono/metadata/mono-endian.h>
9 #include <mono/metadata/metadata.h>
10 #include <mono/metadata/tokentype.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <ctype.h>
14
15 #define MONO_CORLIB_VERSION 1
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] >> HASCONSTANT_BITS;
360                         switch (cols [MONO_CONSTANT_PARENT] & HASCONSTANT_MASK) {
361                         case 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 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 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] >> TYPEDEFORREF_BITS;
449                         switch (cols [MONO_EVENT_TYPE] & TYPEDEFORREF_MASK) {
450                         case 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 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 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         MonoMethod *cmethod;
1140         MonoClassField *field;
1141         MonoClass *klass;
1142         MonoImage *image;
1143         MonoType **params;
1144         ILStackDesc *stack;
1145         register const unsigned char *ip;
1146         register const unsigned char *end;
1147         const unsigned char *target; /* branch target */
1148         int max_args, max_stack, cur_stack, i, n, need_merge, start;
1149         guint32 token, ip_offset;
1150         char *local_state = NULL;
1151         GSList *list = NULL;
1152         guint prefix = 0;
1153         ILCodeDesc *code;
1154
1155         if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1156                         (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
1157                 return NULL;
1158         }
1159         signature = method->signature;
1160         header = ((MonoMethodNormal *)method)->header;
1161         ip = header->code;
1162         end = ip + header->code_size;
1163         max_args = signature->param_count + signature->hasthis;
1164         max_stack = header->max_stack;
1165         need_merge = cur_stack = 0;
1166         start = 1;
1167         image = method->klass->image;
1168         code = g_new0 (ILCodeDesc, header->code_size);
1169         stack = g_new0 (ILStackDesc, max_stack);
1170         if (signature->hasthis) {
1171                 params = g_new0 (MonoType*, max_args);
1172                 params [0] = &method->klass->this_arg;
1173                 memcpy (params + 1, signature->params, sizeof (MonoType*) * signature->param_count);
1174         } else {
1175                 params = signature->params;
1176         }
1177
1178         if (header->num_locals) {
1179                 local_state = g_new (char, header->num_locals);
1180                 memset (local_state, header->init_locals, header->num_locals);
1181         }
1182         /*g_print ("Method %s.%s::%s\n", method->klass->name_space, method->klass->name, method->name);*/
1183
1184         for (i = 0; i < header->num_clauses; ++i) {
1185                 MonoExceptionClause *clause = &header->clauses [i];
1186                 /* catch blocks have the exception on the stack. */
1187                 if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
1188                         code [clause->handler_offset].stack_count = 1;
1189                         code [clause->handler_offset].flags |= CODE_SEEN;
1190                 }
1191         }
1192
1193         while (ip < end) {
1194                 ip_offset = ip - header->code;
1195                 if (start || !(code [ip_offset].flags & CODE_SEEN)) {
1196                         if (start) {
1197                                 /* g_print ("setting stack of IL_%04x to %d\n", ip_offset, 0); */
1198                                 cur_stack = code [ip_offset].stack_count;
1199                         } else {
1200                                 code [ip_offset].stack_count = cur_stack;
1201                         }
1202                         code [ip_offset].flags |= CODE_SEEN;
1203                 } else {
1204                         /* stack merge */
1205                         if (code [ip_offset].stack_count != cur_stack)
1206                                 ADD_INVALID (list, g_strdup_printf ("Cannot merge stack states at 0x%04x", ip_offset));
1207                 }
1208                 start = 0;
1209                 if (need_merge) {
1210                         if (!can_merge_stack (&code [ip_offset], &code [target - header->code]))
1211                                 ADD_INVALID (list, g_strdup_printf ("Cannot merge stack states at 0x%04x", ip_offset));
1212                         need_merge = 0;
1213                 }
1214 #if 0
1215                 {
1216                         char *discode;
1217                         discode = mono_disasm_code_one (NULL, method, ip, NULL);
1218                         discode [strlen (discode) - 1] = 0; /* no \n */
1219                         g_print ("%-29s (%d)\n", discode, cur_stack);
1220                         g_free (discode);
1221                 }
1222 #endif
1223
1224                 switch (*ip) {
1225                 case CEE_NOP:
1226                 case CEE_BREAK: 
1227                         ++ip;
1228                         break;
1229                 case CEE_LDARG_0:
1230                 case CEE_LDARG_1:
1231                 case CEE_LDARG_2:
1232                 case CEE_LDARG_3:
1233                         if (*ip - CEE_LDARG_0 >= max_args)
1234                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", *ip - CEE_LDARG_0, ip_offset));
1235                         CHECK_STACK_OVERFLOW ();
1236                         type_to_eval_stack_type (params [*ip - CEE_LDARG_0], stack + cur_stack, FALSE);
1237                         ++cur_stack;
1238                         ++ip;
1239                         break;
1240                 case CEE_LDLOC_0:
1241                 case CEE_LDLOC_1:
1242                 case CEE_LDLOC_2:
1243                 case CEE_LDLOC_3:
1244                         if (*ip - CEE_LDLOC_0 >= header->num_locals)
1245                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", *ip - CEE_LDLOC_0, ip_offset));
1246                         if (0 && !local_state [*ip - CEE_LDLOC_0])
1247                                 ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", *ip - CEE_LDLOC_0, ip_offset));
1248                         CHECK_STACK_OVERFLOW ();
1249                         type_to_eval_stack_type (header->locals [*ip - CEE_LDLOC_0], stack + cur_stack, FALSE);
1250                         ++cur_stack;
1251                         ++ip;
1252                         break;
1253                 case CEE_STLOC_0:
1254                 case CEE_STLOC_1:
1255                 case CEE_STLOC_2:
1256                 case CEE_STLOC_3:
1257                         if (*ip - CEE_STLOC_0 >= header->num_locals)
1258                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", *ip - CEE_STLOC_0, ip_offset));
1259                         local_state [*ip - CEE_STLOC_0] = 1;
1260                         CHECK_STACK_UNDERFLOW (1);
1261                         --cur_stack;
1262                         if (!can_store_type (stack + cur_stack, header->locals [*ip - CEE_STLOC_0]))
1263                                 ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in store at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1264                         ++ip;
1265                         break;
1266                 case CEE_LDARG_S:
1267                 case CEE_LDARGA_S:
1268                         if (ip [1] >= max_args)
1269                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", ip [1], ip_offset));
1270                         CHECK_STACK_OVERFLOW ();
1271                         type_to_eval_stack_type (params [ip [1]], stack + cur_stack, *ip == CEE_LDARGA_S);
1272                         ++cur_stack;
1273                         ip += 2;
1274                         break;
1275                 case CEE_STARG_S:
1276                         if (ip [1] >= max_args)
1277                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", ip [1], ip_offset));
1278                         CHECK_STACK_UNDERFLOW (1);
1279                         --cur_stack;
1280                         ip += 2;
1281                         break;
1282                 case CEE_LDLOC_S:
1283                 case CEE_LDLOCA_S:
1284                         if (ip [1] >= header->num_locals)
1285                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", ip [1], ip_offset));
1286                         /* no need to check if the var is initialized if the address is taken */
1287                         if (0 && *ip == CEE_LDLOC_S && !local_state [ip [1]])
1288                                 ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", ip [1], ip_offset));
1289                         CHECK_STACK_OVERFLOW ();
1290                         type_to_eval_stack_type (header->locals [ip [1]], stack + cur_stack, *ip == CEE_LDLOCA_S);
1291                         ++cur_stack;
1292                         ip += 2;
1293                         break;
1294                 case CEE_STLOC_S:
1295                         if (ip [1] >= header->num_locals)
1296                                 ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", ip [1], ip_offset));
1297                         local_state [ip [1]] = 1;
1298                         CHECK_STACK_UNDERFLOW (1);
1299                         --cur_stack;
1300                         if (!can_store_type (stack + cur_stack, header->locals [ip [1]]))
1301                                 ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in store at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1302                         ip += 2;
1303                         break;
1304                 case CEE_LDNULL:
1305                         CHECK_STACK_OVERFLOW ();
1306                         stack [cur_stack].type = &mono_defaults.object_class->byval_arg;
1307                         stack [cur_stack].stype = TYPE_OBJ;
1308                         ++cur_stack;
1309                         ++ip;
1310                         break;
1311                 case CEE_LDC_I4_M1:
1312                 case CEE_LDC_I4_0:
1313                 case CEE_LDC_I4_1:
1314                 case CEE_LDC_I4_2:
1315                 case CEE_LDC_I4_3:
1316                 case CEE_LDC_I4_4:
1317                 case CEE_LDC_I4_5:
1318                 case CEE_LDC_I4_6:
1319                 case CEE_LDC_I4_7:
1320                 case CEE_LDC_I4_8:
1321                         CHECK_STACK_OVERFLOW ();
1322                         stack [cur_stack].type = &mono_defaults.int_class->byval_arg;
1323                         stack [cur_stack].stype = TYPE_I4;
1324                         ++cur_stack;
1325                         ++ip;
1326                         break;
1327                 case CEE_LDC_I4_S:
1328                         CHECK_STACK_OVERFLOW ();
1329                         stack [cur_stack].type = &mono_defaults.int_class->byval_arg;
1330                         stack [cur_stack].stype = TYPE_I4;
1331                         ++cur_stack;
1332                         ip += 2;
1333                         break;
1334                 case CEE_LDC_I4:
1335                         CHECK_STACK_OVERFLOW ();
1336                         stack [cur_stack].type = &mono_defaults.int_class->byval_arg;
1337                         stack [cur_stack].stype = TYPE_I4;
1338                         ++cur_stack;
1339                         ip += 5;
1340                         break;
1341                 case CEE_LDC_I8:
1342                         CHECK_STACK_OVERFLOW ();
1343                         stack [cur_stack].type = &mono_defaults.int64_class->byval_arg;
1344                         stack [cur_stack].stype = TYPE_I8;
1345                         ++cur_stack;
1346                         ip += 9;
1347                         break;
1348                 case CEE_LDC_R4:
1349                         CHECK_STACK_OVERFLOW ();
1350                         stack [cur_stack].type = &mono_defaults.double_class->byval_arg;
1351                         stack [cur_stack].stype = TYPE_R8;
1352                         ++cur_stack;
1353                         ip += 5;
1354                         break;
1355                 case CEE_LDC_R8:
1356                         CHECK_STACK_OVERFLOW ();
1357                         stack [cur_stack].type = &mono_defaults.double_class->byval_arg;
1358                         stack [cur_stack].stype = TYPE_R8;
1359                         ++cur_stack;
1360                         ip += 9;
1361                         break;
1362                 case CEE_UNUSED99: ++ip; break; /* warn/error instead? */
1363                 case CEE_DUP:
1364                         CHECK_STACK_UNDERFLOW (1);
1365                         CHECK_STACK_OVERFLOW ();
1366                         stack [cur_stack] = stack [cur_stack - 1];
1367                         ++cur_stack;
1368                         ++ip;
1369                         break;
1370                 case CEE_POP:
1371                         CHECK_STACK_UNDERFLOW (1);
1372                         --cur_stack;
1373                         ++ip;
1374                         break;
1375                 case CEE_JMP:
1376                         if (cur_stack)
1377                                 ADD_INVALID (list, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
1378                         token = read32 (ip + 1);
1379                         if (in_any_block (header, ip_offset))
1380                                 ADD_INVALID (list, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
1381                         /*
1382                          * FIXME: check signature, retval, arguments etc.
1383                          */
1384                         ip += 5;
1385                         break;
1386                 case CEE_CALL:
1387                 case CEE_CALLVIRT:
1388                         token = read32 (ip + 1);
1389                         /*
1390                          * FIXME: we could just load the signature ...
1391                          */
1392                         cmethod = mono_get_method (image, token, NULL);
1393                         if (!cmethod)
1394                                 ADD_INVALID (list, g_strdup_printf ("Method 0x%08x not found at 0x%04x", token, ip_offset));
1395                         csig = cmethod->signature;
1396                         CHECK_STACK_UNDERFLOW (csig->param_count + csig->hasthis);
1397                         cur_stack -= csig->param_count + csig->hasthis;
1398                         if (csig->ret->type != MONO_TYPE_VOID) {
1399                                 CHECK_STACK_OVERFLOW ();
1400                                 type_to_eval_stack_type (csig->ret, stack + cur_stack, FALSE);
1401                                 ++cur_stack;
1402                         }
1403                         ip += 5;
1404                         break;
1405                 case CEE_CALLI:
1406                         token = read32 (ip + 1);
1407                         /*
1408                          * FIXME: check signature, retval, arguments etc.
1409                          */
1410                         ip += 5;
1411                         break;
1412                 case CEE_RET:
1413                         if (signature->ret->type != MONO_TYPE_VOID) {
1414                                 CHECK_STACK_UNDERFLOW (1);
1415                                 --cur_stack;
1416                                 if (!can_store_type (stack + cur_stack, signature->ret))
1417                                         ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in ret at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1418                         }
1419                         if (cur_stack)
1420                                 ADD_INVALID (list, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", cur_stack, ip_offset));
1421                         cur_stack = 0;
1422                         if (in_any_block (header, ip_offset))
1423                                 ADD_INVALID (list, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ip_offset));
1424                         ++ip;
1425                         start = 1;
1426                         break;
1427                 case CEE_BR_S:
1428                         target = ip + (signed char)ip [1] + 2;
1429                         if (target >= end || target < header->code)
1430                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1431                         if (!in_same_block (header, ip_offset, target - header->code))
1432                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1433                         ip += 2;
1434                         start = 1;
1435                         break;
1436                 case CEE_BRFALSE_S:
1437                 case CEE_BRTRUE_S:
1438                         target = ip + (signed char)ip [1] + 2;
1439                         if (target >= end || target < header->code)
1440                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1441                         if (!in_same_block (header, ip_offset, target - header->code))
1442                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1443                         CHECK_STACK_UNDERFLOW (1);
1444                         --cur_stack;
1445                         if (!is_valid_bool_arg (stack + cur_stack))
1446                                 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));
1447                         ip += 2;
1448                         need_merge = 1;
1449                         break;
1450                 case CEE_BEQ_S:
1451                 case CEE_BGE_S:
1452                 case CEE_BGT_S:
1453                 case CEE_BLE_S:
1454                 case CEE_BLT_S:
1455                 case CEE_BNE_UN_S:
1456                 case CEE_BGE_UN_S:
1457                 case CEE_BGT_UN_S:
1458                 case CEE_BLE_UN_S:
1459                 case CEE_BLT_UN_S:
1460                         target = ip + (signed char)ip [1] + 2;
1461                         if (target >= end || target < header->code)
1462                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1463                         if (!in_same_block (header, ip_offset, target - header->code))
1464                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1465                         CHECK_STACK_UNDERFLOW (2);
1466                         cur_stack -= 2;
1467                         if (type_from_op (*ip, stack + cur_stack) == TYPE_INV)
1468                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1469                         ip += 2;
1470                         need_merge = 1;
1471                         break;
1472                 case CEE_BR:
1473                         target = ip + (gint32)read32 (ip + 1) + 5;
1474                         if (target >= end || target < header->code)
1475                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1476                         if (!in_same_block (header, ip_offset, target - header->code))
1477                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1478                         ip += 5;
1479                         start = 1;
1480                         break;
1481                 case CEE_BRFALSE:
1482                 case CEE_BRTRUE:
1483                         target = ip + (gint32)read32 (ip + 1) + 5;
1484                         if (target >= end || target < header->code)
1485                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1486                         if (!in_same_block (header, ip_offset, target - header->code))
1487                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1488                         CHECK_STACK_UNDERFLOW (1);
1489                         --cur_stack;
1490                         if (!is_valid_bool_arg (stack + cur_stack))
1491                                 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));
1492                         ip += 5;
1493                         need_merge = 1;
1494                         break;
1495                 case CEE_BEQ:
1496                 case CEE_BGE:
1497                 case CEE_BGT:
1498                 case CEE_BLE:
1499                 case CEE_BLT:
1500                 case CEE_BNE_UN:
1501                 case CEE_BGE_UN:
1502                 case CEE_BGT_UN:
1503                 case CEE_BLE_UN:
1504                 case CEE_BLT_UN:
1505                         target = ip + (gint32)read32 (ip + 1) + 5;
1506                         if (target >= end || target < header->code)
1507                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1508                         if (!in_same_block (header, ip_offset, target - header->code))
1509                                 ADD_INVALID (list, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
1510                         CHECK_STACK_UNDERFLOW (2);
1511                         cur_stack -= 2;
1512                         if (type_from_op (*ip, stack + cur_stack) == TYPE_INV)
1513                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1514                         ip += 5;
1515                         need_merge = 1;
1516                         break;
1517                 case CEE_SWITCH:
1518                         n = read32 (ip + 1);
1519                         target = ip + sizeof (guint32) * n;
1520                         /* FIXME: check that ip is in range (and within the same exception block) */
1521                         for (i = 0; i < n; ++i)
1522                                 if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < header->code)
1523                                         ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1524                         CHECK_STACK_UNDERFLOW (1);
1525                         --cur_stack;
1526                         if (stack [cur_stack].stype != TYPE_I4)
1527                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to switch at 0x%04x", ip_offset));
1528                         ip += 5 + sizeof (guint32) * n;
1529                         break;
1530                 case CEE_LDIND_I1:
1531                 case CEE_LDIND_U1:
1532                 case CEE_LDIND_I2:
1533                 case CEE_LDIND_U2:
1534                 case CEE_LDIND_I4:
1535                 case CEE_LDIND_U4:
1536                 case CEE_LDIND_I8:
1537                 case CEE_LDIND_I:
1538                 case CEE_LDIND_R4:
1539                 case CEE_LDIND_R8:
1540                 case CEE_LDIND_REF:
1541                         CHECK_STACK_UNDERFLOW (1);
1542                         if (stack [cur_stack - 1].stype != TYPE_PTR && stack [cur_stack - 1].stype != TYPE_MP)
1543                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldind at 0x%04x", ip_offset));
1544                         stack [cur_stack - 1].stype = ldind_type [*ip - CEE_LDIND_I1];
1545                         ++ip;
1546                         break;
1547                 case CEE_STIND_REF:
1548                 case CEE_STIND_I1:
1549                 case CEE_STIND_I2:
1550                 case CEE_STIND_I4:
1551                 case CEE_STIND_I8:
1552                 case CEE_STIND_R4:
1553                 case CEE_STIND_R8:
1554                         CHECK_STACK_UNDERFLOW (2);
1555                         cur_stack -= 2;
1556                         if (stack [cur_stack].stype != TYPE_PTR && stack [cur_stack].stype != TYPE_MP)
1557                                 ADD_INVALID (list, g_strdup_printf ("Invalid pointer argument to stind at 0x%04x", ip_offset));
1558                         if (!stind_type (*ip, stack [cur_stack + 1].stype))
1559                                 ADD_INVALID (list, g_strdup_printf ("Incompatible value argument to stind at 0x%04x", ip_offset));
1560                         ++ip;
1561                         break;
1562                 case CEE_ADD:
1563                 case CEE_SUB:
1564                 case CEE_MUL:
1565                 case CEE_DIV:
1566                 case CEE_DIV_UN:
1567                 case CEE_REM:
1568                 case CEE_REM_UN:
1569                 case CEE_AND:
1570                 case CEE_OR:
1571                 case CEE_XOR:
1572                 case CEE_SHL:
1573                 case CEE_SHR:
1574                 case CEE_SHR_UN:
1575                         CHECK_STACK_UNDERFLOW (2);
1576                         --cur_stack;
1577                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1578                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1579                         ++ip;
1580                         break;
1581                 case CEE_NEG:
1582                 case CEE_NOT:
1583                 case CEE_CONV_I1:
1584                 case CEE_CONV_I2:
1585                 case CEE_CONV_I4:
1586                 case CEE_CONV_I8:
1587                 case CEE_CONV_R4:
1588                 case CEE_CONV_R8:
1589                 case CEE_CONV_U4:
1590                 case CEE_CONV_U8:
1591                         CHECK_STACK_UNDERFLOW (1);
1592                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1593                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1594                         ++ip;
1595                         break;
1596                 case CEE_CPOBJ:
1597                         token = read32 (ip + 1);
1598                         CHECK_STACK_UNDERFLOW (2);
1599                         cur_stack -= 2;
1600                         ip += 5;
1601                         break;
1602                 case CEE_LDOBJ:
1603                         token = read32 (ip + 1);
1604                         CHECK_STACK_UNDERFLOW (1);
1605                         if (stack [cur_stack - 1].stype != TYPE_MP)
1606                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldobj at 0x%04x", ip_offset));
1607                         klass = mono_class_get (image, token);
1608                         if (!klass)
1609                                 ADD_INVALID (list, g_strdup_printf ("Cannot load class from token 0x%08x at 0x%04x", token, ip_offset));
1610                         if (!klass->valuetype)
1611                                 ADD_INVALID (list, g_strdup_printf ("Class is not a valuetype at 0x%04x", ip_offset));
1612                         stack [cur_stack - 1].stype = TYPE_VT;
1613                         stack [cur_stack - 1].type = &klass->byval_arg;
1614                         ip += 5;
1615                         break;
1616                 case CEE_LDSTR:
1617                         token = read32 (ip + 1);
1618                         CHECK_STACK_OVERFLOW ();
1619                         stack [cur_stack].type = &mono_defaults.string_class->byval_arg;
1620                         stack [cur_stack].stype = TYPE_OBJ;
1621                         ++cur_stack;
1622                         ip += 5;
1623                         break;
1624                 case CEE_NEWOBJ:
1625                         token = read32 (ip + 1);
1626                         /*
1627                          * FIXME: we could just load the signature ...
1628                          */
1629                         cmethod = mono_get_method (image, token, NULL);
1630                         if (!cmethod)
1631                                 ADD_INVALID (list, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ip_offset));
1632                         csig = cmethod->signature;
1633                         CHECK_STACK_UNDERFLOW (csig->param_count);
1634                         cur_stack -= csig->param_count;
1635                         CHECK_STACK_OVERFLOW ();
1636                         stack [cur_stack].type = &cmethod->klass->byval_arg;
1637                         stack [cur_stack].stype = cmethod->klass->valuetype? TYPE_VT: TYPE_OBJ;
1638                         ++cur_stack;
1639                         ip += 5;
1640                         break;
1641                 case CEE_CASTCLASS:
1642                 case CEE_ISINST:
1643                         token = read32 (ip + 1);
1644                         CHECK_STACK_UNDERFLOW (1);
1645                         ip += 5;
1646                         break;
1647                 case CEE_CONV_R_UN:
1648                         CHECK_STACK_UNDERFLOW (1);
1649                         ++ip;
1650                         break;
1651                 case CEE_UNUSED58:
1652                 case CEE_UNUSED1:
1653                         ++ip; /* warn, error ? */
1654                         break;
1655                 case CEE_UNBOX:
1656                         token = read32 (ip + 1);
1657                         CHECK_STACK_UNDERFLOW (1);
1658                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1659                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument %s to unbox at 0x%04x", arg_name [stack [cur_stack - 1].stype], ip_offset));
1660                         stack [cur_stack - 1].stype = TYPE_MP;
1661                         stack [cur_stack - 1].type = NULL;
1662                         ip += 5;
1663                         break;
1664                 case CEE_THROW:
1665                         CHECK_STACK_UNDERFLOW (1);
1666                         --cur_stack;
1667                         ++ip;
1668                         start = 1;
1669                         break;
1670                 case CEE_LDFLD:
1671                         CHECK_STACK_UNDERFLOW (1);
1672                         if (stack [cur_stack - 1].stype != TYPE_OBJ && stack [cur_stack - 1].stype != TYPE_MP)
1673                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument %s to ldfld at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
1674                         token = read32 (ip + 1);
1675                         field = mono_field_from_token (image, token, &klass);
1676                         if (!field)
1677                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1678                         type_to_eval_stack_type (field->type, stack + cur_stack - 1, FALSE);
1679                         ip += 5;
1680                         break;
1681                 case CEE_LDFLDA:
1682                         CHECK_STACK_UNDERFLOW (1);
1683                         if (stack [cur_stack - 1].stype != TYPE_OBJ && stack [cur_stack - 1].stype != TYPE_MP)
1684                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldflda at 0x%04x", ip_offset));
1685                         token = read32 (ip + 1);
1686                         field = mono_field_from_token (image, token, &klass);
1687                         if (!field)
1688                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1689                         type_to_eval_stack_type (field->type, stack + cur_stack - 1, TRUE);
1690                         ip += 5;
1691                         break;
1692                 case CEE_STFLD:
1693                         CHECK_STACK_UNDERFLOW (2);
1694                         cur_stack -= 2;
1695                         if (stack [cur_stack].stype != TYPE_OBJ && stack [cur_stack].stype != TYPE_MP)
1696                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to stfld at 0x%04x", ip_offset));
1697                         token = read32 (ip + 1);
1698                         field = mono_field_from_token (image, token, &klass);
1699                         if (!field)
1700                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1701                         /* can_store */
1702                         ip += 5;
1703                         break;
1704                 case CEE_LDSFLD:
1705                         CHECK_STACK_OVERFLOW ();
1706                         token = read32 (ip + 1);
1707                         field = mono_field_from_token (image, token, &klass);
1708                         if (!field)
1709                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1710                         type_to_eval_stack_type (field->type, stack + cur_stack, FALSE);
1711                         ++cur_stack;
1712                         ip += 5;
1713                         break;
1714                 case CEE_LDSFLDA:
1715                         CHECK_STACK_OVERFLOW ();
1716                         token = read32 (ip + 1);
1717                         field = mono_field_from_token (image, token, &klass);
1718                         if (!field)
1719                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1720                         type_to_eval_stack_type (field->type, stack + cur_stack, TRUE);
1721                         ++cur_stack;
1722                         ip += 5;
1723                         break;
1724                 case CEE_STSFLD:
1725                         CHECK_STACK_UNDERFLOW (1);
1726                         --cur_stack;
1727                         token = read32 (ip + 1);
1728                         field = mono_field_from_token (image, token, &klass);
1729                         if (!field)
1730                                 ADD_INVALID (list, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
1731                         /* can store */
1732                         ip += 5;
1733                         break;
1734                 case CEE_STOBJ:
1735                         CHECK_STACK_UNDERFLOW (2);
1736                         cur_stack -= 2;
1737                         token = read32 (ip + 1);
1738                         ip += 5;
1739                         break;
1740                 case CEE_CONV_OVF_I1_UN:
1741                 case CEE_CONV_OVF_I2_UN:
1742                 case CEE_CONV_OVF_I4_UN:
1743                 case CEE_CONV_OVF_I8_UN:
1744                 case CEE_CONV_OVF_U1_UN:
1745                 case CEE_CONV_OVF_U2_UN:
1746                 case CEE_CONV_OVF_U4_UN:
1747                 case CEE_CONV_OVF_U8_UN:
1748                 case CEE_CONV_OVF_I_UN:
1749                 case CEE_CONV_OVF_U_UN:
1750                         CHECK_STACK_UNDERFLOW (1);
1751                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1752                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1753                         ++ip;
1754                         break;
1755                 case CEE_BOX:
1756                         CHECK_STACK_UNDERFLOW (1);
1757                         token = read32 (ip + 1);
1758                         if (stack [cur_stack - 1].stype == TYPE_OBJ)
1759                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument %s to box at 0x%04x", arg_name [stack [cur_stack - 1].stype], ip_offset));
1760                         stack [cur_stack - 1].stype = TYPE_OBJ;
1761                         ip += 5;
1762                         break;
1763                 case CEE_NEWARR:
1764                         CHECK_STACK_UNDERFLOW (1);
1765                         token = read32 (ip + 1);
1766                         stack [cur_stack - 1].stype = TYPE_OBJ;
1767                         ip += 5;
1768                         break;
1769                 case CEE_LDLEN:
1770                         CHECK_STACK_UNDERFLOW (1);
1771                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1772                                 ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldlen at 0x%04x", ip_offset));
1773                         stack [cur_stack - 1].type = &mono_defaults.int_class->byval_arg; /* FIXME: use a native int type */
1774                         stack [cur_stack - 1].stype = TYPE_PTR;
1775                         ++ip;
1776                         break;
1777                 case CEE_LDELEMA:
1778                         CHECK_STACK_UNDERFLOW (2);
1779                         --cur_stack;
1780                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1781                                 ADD_INVALID (list, g_strdup_printf ("Invalid array argument to ldelema at 0x%04x", ip_offset));
1782                         if (stack [cur_stack].stype != TYPE_I4 && stack [cur_stack].stype != TYPE_PTR)
1783                                 ADD_INVALID (list, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
1784                         stack [cur_stack - 1].stype = TYPE_MP;
1785                         token = read32 (ip + 1);
1786                         ip += 5;
1787                         break;
1788                 case CEE_LDELEM_I1:
1789                 case CEE_LDELEM_U1:
1790                 case CEE_LDELEM_I2:
1791                 case CEE_LDELEM_U2:
1792                 case CEE_LDELEM_I4:
1793                 case CEE_LDELEM_U4:
1794                 case CEE_LDELEM_I8:
1795                 case CEE_LDELEM_I:
1796                 case CEE_LDELEM_R4:
1797                 case CEE_LDELEM_R8:
1798                 case CEE_LDELEM_REF:
1799                         CHECK_STACK_UNDERFLOW (2);
1800                         --cur_stack;
1801                         if (stack [cur_stack - 1].stype != TYPE_OBJ)
1802                                 ADD_INVALID (list, g_strdup_printf ("Invalid array argument to ldelem at 0x%04x", ip_offset));
1803                         if (stack [cur_stack].stype != TYPE_I4 && stack [cur_stack].stype != TYPE_PTR)
1804                                 ADD_INVALID (list, g_strdup_printf ("Array index needs to be Int32 or IntPtr at 0x%04x", ip_offset));
1805                         stack [cur_stack - 1].stype = ldelem_type [*ip - CEE_LDELEM_I1];
1806                         ++ip;
1807                         break;
1808                 case CEE_STELEM_I:
1809                 case CEE_STELEM_I1:
1810                 case CEE_STELEM_I2:
1811                 case CEE_STELEM_I4:
1812                 case CEE_STELEM_I8:
1813                 case CEE_STELEM_R4:
1814                 case CEE_STELEM_R8:
1815                 case CEE_STELEM_REF:
1816                         CHECK_STACK_UNDERFLOW (3);
1817                         cur_stack -= 3;
1818                         ++ip;
1819                         break;
1820                 case CEE_LDELEM:
1821                 case CEE_STELEM:
1822                 case CEE_UNBOX_ANY:
1823                 case CEE_UNUSED5:
1824                 case CEE_UNUSED6:
1825                 case CEE_UNUSED7:
1826                 case CEE_UNUSED8:
1827                 case CEE_UNUSED9:
1828                 case CEE_UNUSED10:
1829                 case CEE_UNUSED11:
1830                 case CEE_UNUSED12:
1831                 case CEE_UNUSED13:
1832                 case CEE_UNUSED14:
1833                 case CEE_UNUSED15:
1834                 case CEE_UNUSED16:
1835                 case CEE_UNUSED17:
1836                         ++ip; /* warn, error ? */
1837                         break;
1838                 case CEE_CONV_OVF_I1:
1839                 case CEE_CONV_OVF_U1:
1840                 case CEE_CONV_OVF_I2:
1841                 case CEE_CONV_OVF_U2:
1842                 case CEE_CONV_OVF_I4:
1843                 case CEE_CONV_OVF_U4:
1844                 case CEE_CONV_OVF_I8:
1845                 case CEE_CONV_OVF_U8:
1846                         CHECK_STACK_UNDERFLOW (1);
1847                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1848                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1849                         ++ip;
1850                         break;
1851                 case CEE_UNUSED50:
1852                 case CEE_UNUSED18:
1853                 case CEE_UNUSED19:
1854                 case CEE_UNUSED20:
1855                 case CEE_UNUSED21:
1856                 case CEE_UNUSED22:
1857                 case CEE_UNUSED23:
1858                         ++ip; /* warn, error ? */
1859                         break;
1860                 case CEE_REFANYVAL:
1861                         CHECK_STACK_UNDERFLOW (1);
1862                         ++ip;
1863                         break;
1864                 case CEE_CKFINITE:
1865                         CHECK_STACK_UNDERFLOW (1);
1866                         ++ip;
1867                         break;
1868                 case CEE_UNUSED24:
1869                 case CEE_UNUSED25:
1870                         ++ip; /* warn, error ? */
1871                         break;
1872                 case CEE_MKREFANY:
1873                         CHECK_STACK_UNDERFLOW (1);
1874                         token = read32 (ip + 1);
1875                         ip += 5;
1876                         break;
1877                 case CEE_UNUSED59:
1878                 case CEE_UNUSED60:
1879                 case CEE_UNUSED61:
1880                 case CEE_UNUSED62:
1881                 case CEE_UNUSED63:
1882                 case CEE_UNUSED64:
1883                 case CEE_UNUSED65:
1884                 case CEE_UNUSED66:
1885                 case CEE_UNUSED67:
1886                         ++ip; /* warn, error ? */
1887                         break;
1888                 case CEE_LDTOKEN:
1889                         CHECK_STACK_OVERFLOW ();
1890                         token = read32 (ip + 1);
1891                         ++cur_stack;
1892                         ip += 5;
1893                         break;
1894                 case CEE_CONV_U2:
1895                 case CEE_CONV_U1:
1896                 case CEE_CONV_I:
1897                 case CEE_CONV_OVF_I:
1898                 case CEE_CONV_OVF_U:
1899                         CHECK_STACK_UNDERFLOW (1);
1900                         if (type_from_op (*ip, stack + cur_stack - 1) == TYPE_INV)
1901                                 ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
1902                         ++ip;
1903                         break;
1904                 case CEE_ADD_OVF:
1905                 case CEE_ADD_OVF_UN:
1906                 case CEE_MUL_OVF:
1907                 case CEE_MUL_OVF_UN:
1908                 case CEE_SUB_OVF:
1909                 case CEE_SUB_OVF_UN:
1910                         CHECK_STACK_UNDERFLOW (2);
1911                         --cur_stack;
1912                         ++ip;
1913                         break;
1914                 case CEE_ENDFINALLY:
1915                         ++ip;
1916                         start = 1;
1917                         break;
1918                 case CEE_LEAVE:
1919                         target = ip + (gint32)read32(ip + 1) + 5;
1920                         if (target >= end || target < header->code)
1921                                 ADD_INVALID (list, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
1922                         if (!is_correct_leave (header, ip_offset, target - header->code))
1923                                 ADD_INVALID (list, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ip_offset));
1924                         ip += 5;
1925                         start = 1;
1926                         break;
1927                 case CEE_LEAVE_S:
1928                         target = ip + (signed char)ip [1] + 2;
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 += 2;
1934                         start = 1;
1935                         break;
1936                 case CEE_STIND_I:
1937                         CHECK_STACK_UNDERFLOW (2);
1938                         cur_stack -= 2;
1939                         ++ip;
1940                         break;
1941                 case CEE_CONV_U:
1942                         CHECK_STACK_UNDERFLOW (1);
1943                         ++ip;
1944                         break;
1945                 case CEE_UNUSED26:
1946                 case CEE_UNUSED27:
1947                 case CEE_UNUSED28:
1948                 case CEE_UNUSED29:
1949                 case CEE_UNUSED30:
1950                 case CEE_UNUSED31:
1951                 case CEE_UNUSED32:
1952                 case CEE_UNUSED33:
1953                 case CEE_UNUSED34:
1954                 case CEE_UNUSED35:
1955                 case CEE_UNUSED36:
1956                 case CEE_UNUSED37:
1957                 case CEE_UNUSED38:
1958                 case CEE_UNUSED39:
1959                 case CEE_UNUSED40:
1960                 case CEE_UNUSED41:
1961                 case CEE_UNUSED42:
1962                 case CEE_UNUSED43:
1963                 case CEE_UNUSED44:
1964                 case CEE_UNUSED45:
1965                 case CEE_UNUSED46:
1966                 case CEE_UNUSED47:
1967                 case CEE_UNUSED48:
1968                         ++ip;
1969                         break;
1970                 case CEE_PREFIX7:
1971                 case CEE_PREFIX6:
1972                 case CEE_PREFIX5:
1973                 case CEE_PREFIX4:
1974                 case CEE_PREFIX3:
1975                 case CEE_PREFIX2:
1976                 case CEE_PREFIXREF:
1977                         ++ip;
1978                         break;
1979                 case CEE_PREFIX1:
1980                         ++ip;
1981                         switch (*ip) {
1982                         case CEE_ARGLIST:
1983                                 CHECK_STACK_OVERFLOW ();
1984                                 ++ip;
1985                                 break;
1986                         case CEE_CEQ:
1987                         case CEE_CGT:
1988                         case CEE_CGT_UN:
1989                         case CEE_CLT:
1990                         case CEE_CLT_UN:
1991                                 CHECK_STACK_UNDERFLOW (2);
1992                                 --cur_stack;
1993                                 if (type_from_op (256 + *ip, stack + cur_stack - 1) == TYPE_INV)
1994                                         ADD_INVALID (list, g_strdup_printf ("Invalid arguments to opcode 0xFE 0x%02x at 0x%04x", *ip, ip_offset));
1995                                 ++ip;
1996                                 break;
1997                         case CEE_LDFTN:
1998                                 CHECK_STACK_OVERFLOW ();
1999                                 token = read32 (ip + 1);
2000                                 ip += 5;
2001                                 stack [cur_stack].stype = TYPE_PTR;
2002                                 cur_stack++;
2003                                 break;
2004                         case CEE_LDVIRTFTN:
2005                                 CHECK_STACK_UNDERFLOW (1);
2006                                 token = read32 (ip + 1);
2007                                 ip += 5;
2008                                 if (stack [cur_stack - 1].stype != TYPE_OBJ)
2009                                         ADD_INVALID (list, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ip_offset));
2010                                 stack [cur_stack - 1].stype = TYPE_PTR;
2011                                 break;
2012                         case CEE_UNUSED56:
2013                                 ++ip;
2014                                 break;
2015                         case CEE_LDARG:
2016                         case CEE_LDARGA:
2017                                 if (read16 (ip + 1) >= max_args)
2018                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", read16 (ip + 1), ip_offset));
2019                                 CHECK_STACK_OVERFLOW ();
2020                                 ++cur_stack;
2021                                 ip += 3;
2022                                 break;
2023                         case CEE_STARG:
2024                                 if (read16 (ip + 1) >= max_args)
2025                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", read16(ip + 1), ip_offset));
2026                                 CHECK_STACK_UNDERFLOW (1);
2027                                 --cur_stack;
2028                                 ip += 3;
2029                                 break;
2030                         case CEE_LDLOC:
2031                         case CEE_LDLOCA:
2032                                 n = read16 (ip + 1);
2033                                 if (n >= header->num_locals)
2034                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", n, ip_offset));
2035                                 /* no need to check if the var is initialized if the address is taken */
2036                                 if (0 && *ip == CEE_LDLOC && !local_state [n])
2037                                         ADD_INVALID (list, g_strdup_printf ("Local var %d is initialized at 0x%04x", n, ip_offset));
2038                                 CHECK_STACK_OVERFLOW ();
2039                                 type_to_eval_stack_type (header->locals [n], stack + cur_stack, *ip == CEE_LDLOCA);
2040                                 ++cur_stack;
2041                                 ip += 3;
2042                                 break;
2043                         case CEE_STLOC:
2044                                 n = read16 (ip + 1);
2045                                 if (n >= header->num_locals)
2046                                         ADD_INVALID (list, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", n, ip_offset));
2047                                 local_state [n] = 1;
2048                                 CHECK_STACK_UNDERFLOW (1);
2049                                 --cur_stack;
2050                                 if (!can_store_type (stack + cur_stack, header->locals [n]))
2051                                         ADD_INVALID (list, g_strdup_printf ("Incompatible type %s in store at 0x%04x", arg_name [stack [cur_stack].stype], ip_offset));
2052                                 ip += 3;
2053                                 break;
2054                         case CEE_LOCALLOC:
2055                                 if (cur_stack != 1)
2056                                         ADD_INVALID (list, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ip_offset));
2057                                 if (stack [cur_stack -1].stype != TYPE_I4 && stack [cur_stack -1].stype != TYPE_PTR)
2058                                         ADD_INVALID (list, g_strdup_printf ("Invalid argument to localloc at 0x%04x", ip_offset));
2059                                 stack [cur_stack -1].stype = TYPE_MP;
2060                                 ++ip;
2061                                 break;
2062                         case CEE_UNUSED57:
2063                                 ++ip;
2064                                 break;
2065                         case CEE_ENDFILTER:
2066                                 if (cur_stack != 1)
2067                                         ADD_INVALID (list, g_strdup_printf ("Stack must have only filter result in endfilter at 0x%04x", ip_offset));
2068                                 ++ip;
2069                                 break;
2070                         case CEE_UNALIGNED_:
2071                                 prefix |= PREFIX_UNALIGNED;
2072                                 ++ip;
2073                                 break;
2074                         case CEE_VOLATILE_:
2075                                 prefix |= PREFIX_VOLATILE;
2076                                 ++ip;
2077                                 break;
2078                         case CEE_TAIL_:
2079                                 prefix |= PREFIX_TAIL;
2080                                 ++ip;
2081                                 if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
2082                                         ADD_INVALID (list, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
2083                                 break;
2084                         case CEE_INITOBJ:
2085                                 CHECK_STACK_UNDERFLOW (1);
2086                                 token = read32 (ip + 1);
2087                                 ip += 5;
2088                                 --cur_stack;
2089                                 break;
2090                         case CEE_CONSTRAINED_:
2091                                 token = read32 (ip + 1);
2092                                 ip += 5;
2093                                 break;
2094                         case CEE_CPBLK:
2095                                 CHECK_STACK_UNDERFLOW (3);
2096                                 ip++;
2097                                 break;
2098                         case CEE_INITBLK:
2099                                 CHECK_STACK_UNDERFLOW (3);
2100                                 ip++;
2101                                 break;
2102                         case CEE_NO_:
2103                                 ip += 2;
2104                                 break;
2105                         case CEE_RETHROW:
2106                                 ++ip;
2107                                 break;
2108                         case CEE_UNUSED:
2109                                 ++ip;
2110                                 break;
2111                         case CEE_SIZEOF:
2112                                 CHECK_STACK_OVERFLOW ();
2113                                 token = read32 (ip + 1);
2114                                 ip += 5;
2115                                 stack [cur_stack].type = &mono_defaults.uint_class->byval_arg;
2116                                 stack [cur_stack].stype = TYPE_I4;
2117                                 cur_stack++;
2118                                 break;
2119                         case CEE_REFANYTYPE:
2120                                 CHECK_STACK_UNDERFLOW (1);
2121                                 ++ip;
2122                                 break;
2123                         case CEE_UNUSED53:
2124                         case CEE_UNUSED54:
2125                         case CEE_UNUSED55:
2126                         case CEE_UNUSED70:
2127                                 ++ip;
2128                                 break;
2129                         }
2130                 }
2131         }
2132         /*
2133          * if ip != end we overflowed: mark as error.
2134          */
2135         if (ip != end || !start) {
2136                 ADD_INVALID (list, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
2137         }
2138 invalid_cil:
2139
2140         g_free (local_state);
2141         g_free (code);
2142         g_free (stack);
2143         if (signature->hasthis)
2144                 g_free (params);
2145         return list;
2146 }
2147
2148 typedef struct {
2149         const char *name;
2150         guint64 offset;
2151 } FieldDesc;
2152
2153 typedef struct {
2154         const char *name;
2155         const FieldDesc *fields;
2156 } ClassDesc;
2157
2158 static const FieldDesc 
2159 typebuilder_fields[] = {
2160         {"tname", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, name)},
2161         {"nspace", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, nspace)},
2162         {"parent", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, parent)},
2163         {"interfaces", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, interfaces)},
2164         {"methods", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, methods)},
2165         {"properties", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, properties)},
2166         {"fields", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, fields)},
2167         {"attrs", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, attrs)},
2168         {"table_idx", G_STRUCT_OFFSET (MonoReflectionTypeBuilder, table_idx)},
2169         {NULL, 0}
2170 };
2171
2172 static const FieldDesc 
2173 modulebuilder_fields[] = {
2174         {"types", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, types)},
2175         {"cattrs", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, cattrs)},
2176         {"guid", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, guid)},
2177         {"table_idx", G_STRUCT_OFFSET (MonoReflectionModuleBuilder, table_idx)},
2178         {NULL, 0}
2179 };
2180
2181 static const FieldDesc 
2182 assemblybuilder_fields[] = {
2183         {"entry_point", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, entry_point)},
2184         {"modules", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, modules)},
2185         {"name", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, name)},
2186         {"resources", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, resources)},
2187         {"version", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, version)},
2188         {"culture", G_STRUCT_OFFSET (MonoReflectionAssemblyBuilder, culture)},
2189         {NULL, 0}
2190 };
2191
2192 static const FieldDesc 
2193 ctorbuilder_fields[] = {
2194         {"ilgen", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, ilgen)},
2195         {"parameters", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, parameters)},
2196         {"attrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, attrs)},
2197         {"iattrs", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, iattrs)},
2198         {"table_idx", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, table_idx)},
2199         {"call_conv", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, call_conv)},
2200         {"type", G_STRUCT_OFFSET (MonoReflectionCtorBuilder, type)},
2201         {NULL, 0}
2202 };
2203
2204 static const FieldDesc 
2205 methodbuilder_fields[] = {
2206         {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, mhandle)},
2207         {"rtype", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, rtype)},
2208         {"parameters", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, parameters)},
2209         {"attrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, attrs)},
2210         {"iattrs", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, iattrs)},
2211         {"name", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, name)},
2212         {"table_idx", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, table_idx)},
2213         {"code", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, code)},
2214         {"ilgen", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, ilgen)},
2215         {"type", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, type)},
2216         {"pinfo", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, pinfo)},
2217         {"pi_dll", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dll)},
2218         {"pi_entry", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, dllentry)},
2219         {"ncharset", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, charset)},
2220         {"native_cc", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, native_cc)},
2221         {"call_conv", G_STRUCT_OFFSET (MonoReflectionMethodBuilder, call_conv)},
2222         {NULL, 0}
2223 };
2224
2225 static const FieldDesc 
2226 fieldbuilder_fields[] = {
2227         {"attrs", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, attrs)},
2228         {"type", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, type)},
2229         {"name", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, name)},
2230         {"def_value", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, def_value)},
2231         {"offset", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, offset)},
2232         {"table_idx", G_STRUCT_OFFSET (MonoReflectionFieldBuilder, table_idx)},
2233         {NULL, 0}
2234 };
2235
2236 static const FieldDesc 
2237 propertybuilder_fields[] = {
2238         {"attrs", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, attrs)},
2239         {"name", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, name)},
2240         {"type", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, type)},
2241         {"parameters", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, parameters)},
2242         {"def_value", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, def_value)},
2243         {"set_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, set_method)},
2244         {"get_method", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, get_method)},
2245         {"table_idx", G_STRUCT_OFFSET (MonoReflectionPropertyBuilder, table_idx)},
2246         {NULL, 0}
2247 };
2248
2249 static const FieldDesc 
2250 ilgenerator_fields[] = {
2251         {"code", G_STRUCT_OFFSET (MonoReflectionILGen, code)},
2252         {"code_len", G_STRUCT_OFFSET (MonoReflectionILGen, code_len)},
2253         {"max_stack", G_STRUCT_OFFSET (MonoReflectionILGen, max_stack)},
2254         {"cur_stack", G_STRUCT_OFFSET (MonoReflectionILGen, cur_stack)},
2255         {"locals", G_STRUCT_OFFSET (MonoReflectionILGen, locals)},
2256         {"ex_handlers", G_STRUCT_OFFSET (MonoReflectionILGen, ex_handlers)},
2257         {NULL, 0}
2258 };
2259
2260 static const FieldDesc 
2261 ilexinfo_fields[] = {
2262         {"handlers", G_STRUCT_OFFSET (MonoILExceptionInfo, handlers)},
2263         {"start", G_STRUCT_OFFSET (MonoILExceptionInfo, start)},
2264         {"len", G_STRUCT_OFFSET (MonoILExceptionInfo, len)},
2265         {"end", G_STRUCT_OFFSET (MonoILExceptionInfo, label)},
2266         {NULL, 0}
2267 };
2268
2269 static const FieldDesc 
2270 ilexblock_fields[] = {
2271         {"extype", G_STRUCT_OFFSET (MonoILExceptionBlock, extype)},
2272         {"type", G_STRUCT_OFFSET (MonoILExceptionBlock, type)},
2273         {"start", G_STRUCT_OFFSET (MonoILExceptionBlock, start)},
2274         {"len", G_STRUCT_OFFSET (MonoILExceptionBlock, len)},
2275         {"filter_offset", G_STRUCT_OFFSET (MonoILExceptionBlock, filter_offset)},
2276         {NULL, 0}
2277 };
2278
2279 static const ClassDesc
2280 emit_classes_to_check [] = {
2281         {"TypeBuilder", typebuilder_fields},
2282         {"ModuleBuilder", modulebuilder_fields},
2283         {"AssemblyBuilder", assemblybuilder_fields},
2284         {"ConstructorBuilder", ctorbuilder_fields},
2285         {"MethodBuilder", methodbuilder_fields},
2286         {"FieldBuilder", fieldbuilder_fields},
2287         {"PropertyBuilder", propertybuilder_fields},
2288         {"ILGenerator", ilgenerator_fields},
2289         {"ILExceptionBlock", ilexblock_fields},
2290         {"ILExceptionInfo", ilexinfo_fields},
2291         {NULL, NULL}
2292 };
2293
2294 static const FieldDesc 
2295 monoevent_fields[] = {
2296         {"klass", G_STRUCT_OFFSET (MonoReflectionEvent, klass)},
2297         {"handle", G_STRUCT_OFFSET (MonoReflectionEvent, event)},
2298         {NULL, 0}
2299 };
2300
2301 static const FieldDesc 
2302 monoproperty_fields[] = {
2303         {"klass", G_STRUCT_OFFSET (MonoReflectionProperty, klass)},
2304         {"prop", G_STRUCT_OFFSET (MonoReflectionProperty, property)},
2305         {NULL, 0}
2306 };
2307
2308 static const FieldDesc 
2309 monofield_fields[] = {
2310         {"klass", G_STRUCT_OFFSET (MonoReflectionField, klass)},
2311         {"fhandle", G_STRUCT_OFFSET (MonoReflectionField, field)},
2312         {NULL, 0}
2313 };
2314
2315 static const FieldDesc 
2316 monomethodinfo_fields[] = {
2317         {"parent", G_STRUCT_OFFSET (MonoMethodInfo, parent)},
2318         {"ret", G_STRUCT_OFFSET (MonoMethodInfo, ret)},
2319         {"attrs", G_STRUCT_OFFSET (MonoMethodInfo, attrs)},
2320         {"iattrs", G_STRUCT_OFFSET (MonoMethodInfo, implattrs)},
2321         {NULL, 0}
2322 };
2323
2324 static const FieldDesc 
2325 monopropertyinfo_fields[] = {
2326         {"parent", G_STRUCT_OFFSET (MonoPropertyInfo, parent)},
2327         {"name", G_STRUCT_OFFSET (MonoPropertyInfo, name)},
2328         {"get_method", G_STRUCT_OFFSET (MonoPropertyInfo, get)},
2329         {"set_method", G_STRUCT_OFFSET (MonoPropertyInfo, set)},
2330         {"attrs", G_STRUCT_OFFSET (MonoPropertyInfo, attrs)},
2331         {NULL, 0}
2332 };
2333
2334 static const FieldDesc 
2335 monomethod_fields[] = {
2336         {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
2337         {NULL, 0}
2338 };
2339
2340 static const FieldDesc 
2341 monocmethod_fields[] = {
2342         {"mhandle", G_STRUCT_OFFSET (MonoReflectionMethod, method)},
2343         {NULL, 0}
2344 };
2345
2346 static const FieldDesc 
2347 pinfo_fields[] = {
2348         {"ClassImpl", G_STRUCT_OFFSET (MonoReflectionParameter, ClassImpl)},
2349         {"DefaultValueImpl", G_STRUCT_OFFSET (MonoReflectionParameter, DefaultValueImpl)},
2350         {"MemberImpl", G_STRUCT_OFFSET (MonoReflectionParameter, MemberImpl)},
2351         {"NameImpl", G_STRUCT_OFFSET (MonoReflectionParameter, NameImpl)},
2352         {"PositionImpl", G_STRUCT_OFFSET (MonoReflectionParameter, PositionImpl)},
2353         {"AttrsImpl", G_STRUCT_OFFSET (MonoReflectionParameter, AttrsImpl)},
2354         {NULL, 0}
2355 };
2356
2357 static const ClassDesc
2358 reflection_classes_to_check [] = {
2359         {"MonoEvent", monoevent_fields},
2360         {"MonoProperty", monoproperty_fields},
2361         {"MonoField", monofield_fields},
2362         {"MonoMethodInfo", monomethodinfo_fields},
2363         {"MonoPropertyInfo", monopropertyinfo_fields},
2364         {"MonoMethod", monomethod_fields},
2365         {"MonoCMethod", monocmethod_fields},
2366         {"ParameterInfo", pinfo_fields},
2367         {NULL, NULL}
2368 };
2369
2370 static FieldDesc 
2371 enuminfo_fields[] = {
2372         {"utype", G_STRUCT_OFFSET (MonoEnumInfo, utype)},
2373         {"values", G_STRUCT_OFFSET (MonoEnumInfo, values)},
2374         {"names", G_STRUCT_OFFSET (MonoEnumInfo, names)},
2375         {NULL, 0}
2376 };
2377
2378 static FieldDesc 
2379 delegate_fields[] = {
2380         {"target_type", G_STRUCT_OFFSET (MonoDelegate, target_type)},
2381         {"m_target", G_STRUCT_OFFSET (MonoDelegate, target)},
2382         {"method_name", G_STRUCT_OFFSET (MonoDelegate, method_name)},
2383         {"method_ptr", G_STRUCT_OFFSET (MonoDelegate, method_ptr)},
2384         {"delegate_trampoline", G_STRUCT_OFFSET (MonoDelegate, delegate_trampoline)},
2385         {"method_info", G_STRUCT_OFFSET (MonoDelegate, method_info)},
2386         {NULL, 0}
2387 };
2388
2389 static FieldDesc 
2390 multicast_delegate_fields[] = {
2391         {"prev", G_STRUCT_OFFSET (MonoMulticastDelegate, prev)},
2392         {NULL, 0}
2393 };
2394
2395 static FieldDesc 
2396 async_result_fields[] = {
2397         {"async_state", G_STRUCT_OFFSET (MonoAsyncResult, async_state)},
2398         {"handle", G_STRUCT_OFFSET (MonoAsyncResult, handle)},
2399         {"async_delegate", G_STRUCT_OFFSET (MonoAsyncResult, async_delegate)},
2400         {"data", G_STRUCT_OFFSET (MonoAsyncResult, data)},
2401         {"sync_completed", G_STRUCT_OFFSET (MonoAsyncResult, sync_completed)},
2402         {"completed", G_STRUCT_OFFSET (MonoAsyncResult, completed)},
2403         {"endinvoke_called", G_STRUCT_OFFSET (MonoAsyncResult, endinvoke_called)},
2404         {NULL, 0}
2405 };
2406
2407 static FieldDesc 
2408 exception_fields[] = {
2409         {"trace_ips", G_STRUCT_OFFSET (MonoException, trace_ips)},
2410         {"inner_exception", G_STRUCT_OFFSET (MonoException, inner_ex)},
2411         {"message", G_STRUCT_OFFSET (MonoException, message)},
2412         {"help_link", G_STRUCT_OFFSET (MonoException, help_link)},
2413         {"class_name", G_STRUCT_OFFSET (MonoException, class_name)},
2414         {"stack_trace", G_STRUCT_OFFSET (MonoException, stack_trace)},
2415         {"remote_stack_trace", G_STRUCT_OFFSET (MonoException, remote_stack_trace)},
2416         {"remote_stack_index", G_STRUCT_OFFSET (MonoException, remote_stack_index)},
2417         {"hresult", G_STRUCT_OFFSET (MonoException, hresult)},
2418         {"source", G_STRUCT_OFFSET (MonoException, source)},
2419         {NULL, 0}
2420 };
2421
2422 static const ClassDesc
2423 system_classes_to_check [] = {
2424         {"Exception", exception_fields},
2425         {"MonoEnumInfo", enuminfo_fields},
2426         {"Delegate", delegate_fields},
2427         {"MulticastDelegate", multicast_delegate_fields},
2428         {NULL, NULL}
2429 };
2430
2431 static FieldDesc 
2432 stack_frame_fields [] = {
2433         {"ilOffset", G_STRUCT_OFFSET (MonoStackFrame, il_offset)},
2434         {"nativeOffset", G_STRUCT_OFFSET (MonoStackFrame, native_offset)},
2435         {"methodBase", G_STRUCT_OFFSET (MonoStackFrame, method)},
2436         {"fileName", G_STRUCT_OFFSET (MonoStackFrame, filename)},
2437         {"lineNumber", G_STRUCT_OFFSET (MonoStackFrame, line)},
2438         {"columnNumber", G_STRUCT_OFFSET (MonoStackFrame, column)},
2439         {NULL, 0}
2440 };
2441
2442 static const ClassDesc
2443 system_diagnostics_classes_to_check [] = {
2444         {"StackFrame", stack_frame_fields},
2445         {NULL, NULL}
2446 };
2447
2448 static FieldDesc 
2449 mono_method_message_fields[] = {
2450         {"method", G_STRUCT_OFFSET (MonoMethodMessage, method)},
2451         {"args", G_STRUCT_OFFSET (MonoMethodMessage, args)},
2452         {"names", G_STRUCT_OFFSET (MonoMethodMessage, names)},
2453         {"arg_types", G_STRUCT_OFFSET (MonoMethodMessage, arg_types)},
2454         {"ctx", G_STRUCT_OFFSET (MonoMethodMessage, ctx)},
2455         {"rval", G_STRUCT_OFFSET (MonoMethodMessage, rval)},
2456         {"exc", G_STRUCT_OFFSET (MonoMethodMessage, exc)},
2457         {NULL, 0}
2458 };
2459
2460 static const ClassDesc
2461 messaging_classes_to_check [] = {
2462         {"AsyncResult", async_result_fields},
2463         {"MonoMethodMessage", mono_method_message_fields},
2464         {NULL, NULL}
2465 };
2466
2467 static FieldDesc 
2468 transparent_proxy_fields[] = {
2469         {"_rp", G_STRUCT_OFFSET (MonoTransparentProxy, rp)},
2470         {"_class", G_STRUCT_OFFSET (MonoTransparentProxy, klass)},
2471         {NULL, 0}
2472 };
2473
2474 static FieldDesc 
2475 real_proxy_fields[] = {
2476         {"class_to_proxy", G_STRUCT_OFFSET (MonoRealProxy, class_to_proxy)},
2477         {NULL, 0}
2478 };
2479
2480 static const ClassDesc
2481 proxy_classes_to_check [] = {
2482         {"TransparentProxy", transparent_proxy_fields},
2483         {"RealProxy", real_proxy_fields},
2484         {NULL, NULL}
2485 };
2486
2487 static FieldDesc 
2488 wait_handle_fields[] = {
2489         {"os_handle", G_STRUCT_OFFSET (MonoWaitHandle, handle)},
2490         {"disposed", G_STRUCT_OFFSET (MonoWaitHandle, disposed)},
2491         {NULL, 0}
2492 };
2493
2494 static FieldDesc 
2495 thread_fields[] = {
2496         {"system_thread_handle", G_STRUCT_OFFSET (MonoThread, handle)},
2497         {"current_culture", G_STRUCT_OFFSET (MonoThread, culture_info)},
2498         {"threadpool_thread", G_STRUCT_OFFSET (MonoThread, threadpool_thread)},
2499         {"state", G_STRUCT_OFFSET (MonoThread, state)},
2500         {"abort_exc", G_STRUCT_OFFSET (MonoThread, abort_exc)},
2501         {"abort_state", G_STRUCT_OFFSET (MonoThread, abort_state)},
2502         {"thread_id", G_STRUCT_OFFSET (MonoThread, tid)},
2503         {NULL, 0}
2504 };
2505
2506 static const ClassDesc
2507 threading_classes_to_check [] = {
2508         {"Thread", thread_fields},
2509         {"WaitHandle", wait_handle_fields},
2510         {NULL, NULL}
2511 };
2512
2513 static const FieldDesc
2514 cinfo_fields[] = {
2515         {"datetime_format", G_STRUCT_OFFSET (MonoCultureInfo, datetime_format)},
2516         {"number_format", G_STRUCT_OFFSET (MonoCultureInfo, number_format)},
2517         {"textinfo", G_STRUCT_OFFSET (MonoCultureInfo, textinfo)},
2518         {"name", G_STRUCT_OFFSET (MonoCultureInfo, name)},
2519         {"displayname", G_STRUCT_OFFSET (MonoCultureInfo, displayname)},
2520         {"englishname", G_STRUCT_OFFSET (MonoCultureInfo, englishname)},
2521         {"nativename", G_STRUCT_OFFSET (MonoCultureInfo, nativename)},
2522         {"iso3lang", G_STRUCT_OFFSET (MonoCultureInfo, iso3lang)},
2523         {"iso2lang", G_STRUCT_OFFSET (MonoCultureInfo, iso2lang)},
2524         {"icu_name", G_STRUCT_OFFSET (MonoCultureInfo, icu_name)},
2525         {"win3lang", G_STRUCT_OFFSET (MonoCultureInfo, win3lang)},
2526         {"compareinfo", G_STRUCT_OFFSET (MonoCultureInfo, compareinfo)},
2527         {NULL, 0}
2528 };
2529
2530 static const FieldDesc
2531 dtfinfo_fields[] = {
2532         {"_AMDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, AMDesignator)},
2533         {"_PMDesignator", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, PMDesignator)},
2534         {"_DayNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, DayNames)},
2535         {"_MonthNames", G_STRUCT_OFFSET (MonoDateTimeFormatInfo, MonthNames)},
2536         {NULL, 0}
2537 };
2538
2539 static const FieldDesc
2540 nfinfo_fields[] = {
2541         {"decimalFormats", G_STRUCT_OFFSET (MonoNumberFormatInfo, decimalFormats)},
2542         {"currencySymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, currencySymbol)},
2543         {"percentSymbol", G_STRUCT_OFFSET (MonoNumberFormatInfo, percentSymbol)},
2544         {"positiveSign", G_STRUCT_OFFSET (MonoNumberFormatInfo, positiveSign)},
2545         {NULL, 0}
2546 };
2547
2548 static const FieldDesc
2549 compinfo_fields[] = {
2550         {"lcid", G_STRUCT_OFFSET (MonoCompareInfo, lcid)},
2551         {"ICU_collator", G_STRUCT_OFFSET (MonoCompareInfo, ICU_collator)},
2552         {NULL, 0}
2553 };
2554
2555 static const FieldDesc
2556 sortkey_fields[] = {
2557         {"str", G_STRUCT_OFFSET (MonoSortKey, str)},
2558         {"options", G_STRUCT_OFFSET (MonoSortKey, options)},
2559         {"key", G_STRUCT_OFFSET (MonoSortKey, key)},
2560         {"lcid", G_STRUCT_OFFSET (MonoSortKey, lcid)},
2561         {NULL, 0}
2562 };
2563
2564 static const ClassDesc
2565 globalization_classes_to_check [] = {
2566         {"CultureInfo", cinfo_fields},
2567         {"DateTimeFormatInfo", dtfinfo_fields},
2568         {"NumberFormatInfo", nfinfo_fields},
2569         {"CompareInfo", compinfo_fields},
2570         {"SortKey", sortkey_fields},
2571         {NULL, NULL}
2572 };
2573
2574 typedef struct {
2575         const char *name;
2576         const ClassDesc *types;
2577 } NameSpaceDesc;
2578
2579 static const NameSpaceDesc
2580 namespaces_to_check[] = {
2581         {"System.Runtime.Remoting.Proxies", proxy_classes_to_check},
2582         {"System.Runtime.Remoting.Messaging", messaging_classes_to_check},
2583         {"System.Reflection.Emit", emit_classes_to_check},
2584         {"System.Reflection", reflection_classes_to_check},
2585         {"System.Threading", threading_classes_to_check},
2586         {"System.Diagnostics", system_diagnostics_classes_to_check},
2587         {"System", system_classes_to_check},
2588         {"System.Globalization", globalization_classes_to_check},
2589         {NULL, NULL}
2590 };
2591
2592 static char*
2593 check_corlib (MonoImage *corlib)
2594 {
2595         MonoClass *klass;
2596         MonoClassField *field;
2597         const FieldDesc *fdesc;
2598         const ClassDesc *cdesc;
2599         const NameSpaceDesc *ndesc;
2600         gint struct_offset;
2601         GString *result = NULL;
2602         guint32 i, cindex, version;
2603         guint32 constant_cols [MONO_CONSTANT_SIZE];     
2604
2605         /* Check corlib version */
2606         klass = mono_class_from_name (corlib, "System", "Environment");
2607         mono_class_init (klass);
2608         for (i = 0; i < klass->field.count; ++i) {
2609                 if (strcmp ("mono_corlib_version", klass->fields [i].name) == 0)
2610                         break;
2611         }
2612         if (i < klass->field.count) {
2613                 cindex = mono_metadata_get_constant_index (corlib, MONO_TOKEN_FIELD_DEF | (i + 1), 0);
2614                 if (cindex) {
2615                         mono_metadata_decode_row (&corlib->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
2616                         version = 0;
2617                         switch (constant_cols [MONO_CONSTANT_TYPE]) {
2618                         case MONO_TYPE_U1: {
2619                                 guint8 *version_ptr = (guint8*)mono_metadata_blob_heap (corlib, constant_cols [MONO_CONSTANT_VALUE]);
2620                                 version = *version_ptr;
2621                                 break;
2622                         }
2623                         case MONO_TYPE_U2: {
2624                                 guint16 *version_ptr = (guint16*)mono_metadata_blob_heap (corlib, constant_cols [MONO_CONSTANT_VALUE]);
2625                                 version = read16 (version_ptr);
2626                                 break;
2627                         }
2628                         default:
2629                                 g_assert_not_reached ();
2630                         }
2631                         if (version != MONO_CORLIB_VERSION) {
2632                                 result = g_string_new ("");
2633                                 g_string_append_printf (result, "expected corlib version %d, found %d.\n", MONO_CORLIB_VERSION, version);
2634                         }
2635                 }
2636         }
2637         else {
2638                 result = g_string_new ("");
2639                 g_string_append_printf (result, "Cannot find field System.Environment::mono_corlib_version\n");
2640         }
2641         
2642         for (ndesc = namespaces_to_check; ndesc->name; ++ndesc) {
2643                 for (cdesc = ndesc->types; cdesc->name; ++cdesc) {
2644                         klass = mono_class_from_name (corlib, ndesc->name, cdesc->name);
2645                         if (!klass) {
2646                                 if (!result)
2647                                         result = g_string_new ("");
2648                                 g_string_append_printf (result, "Cannot find class %s\n", cdesc->name);
2649                                 continue;
2650                         }
2651                         mono_class_init (klass);
2652                         /*
2653                          * FIXME: we should also check the size of valuetypes, or
2654                          * we're going to have trouble when we access them in arrays.
2655                          */
2656                         if (klass->valuetype)
2657                                 struct_offset = sizeof (MonoObject);
2658                         else
2659                                 struct_offset = 0;
2660                         for (fdesc = cdesc->fields; fdesc->name; ++fdesc) {
2661                                 field = mono_class_get_field_from_name (klass, fdesc->name);
2662                                 if (!field || (field->offset != (fdesc->offset + struct_offset))) {
2663                                         if (!result)
2664                                                 result = g_string_new ("");
2665                                         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));
2666                                 }
2667                         }
2668                 }
2669         }
2670         if (result)
2671                 return g_string_free (result, FALSE);
2672         return NULL;
2673 }
2674
2675 char*
2676 mono_verify_corlib () {
2677         return check_corlib (mono_defaults.corlib);
2678 }
2679
2680