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