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