[BTLS]: Certificate cleanups and fixes. (#3706)
[mono.git] / mono / dis / main.c
index 6a96e4329e1c85db0164f0e0263b2c15791aeb06..53de00ae71577c4a902472c6a2448a648608bf62 100644 (file)
@@ -11,6 +11,7 @@
  *   Structs are not being labeled as `valuetype' classes
  *   
  *   How are fields with literals mapped to constants?
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 #include <config.h>
 #include <stdio.h>
@@ -31,6 +32,7 @@
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/utils/bsearch.h>
+#include <mono/utils/mono-counters.h>
 
 static void     setup_filter          (MonoImage *image);
 static gboolean should_include_type   (int idx);
@@ -663,6 +665,7 @@ dis_locals (MonoImage *m, MonoMethodHeader *mh, const char *ptr)
 static void
 dis_code (MonoImage *m, guint32 token, guint32 rva, MonoGenericContainer *container)
 {
+       MonoError error;
        MonoMethodHeader *mh;
        const char *ptr = mono_image_rva_map (m, rva);
        const char *loc;
@@ -678,7 +681,7 @@ dis_code (MonoImage *m, guint32 token, guint32 rva, MonoGenericContainer *contai
                g_free (override);
        }
 
-       mh = mono_metadata_parse_mh_full (m, container, ptr);
+       mh = mono_metadata_parse_mh_full (m, container, ptr, &error);
        entry_point = mono_image_get_entry_point (m);
        if (entry_point && mono_metadata_token_index (entry_point) && mono_metadata_token_table (entry_point) == MONO_TABLE_METHOD) {
                loc = mono_metadata_locate_token (m, entry_point);
@@ -698,6 +701,8 @@ dis_code (MonoImage *m, guint32 token, guint32 rva, MonoGenericContainer *contai
   hex_dump (mh->code + mh->code_size, 0, 64);
 */
                mono_metadata_free_mh (mh);
+       } else {
+               mono_error_cleanup (&error);
        }
 }
 
@@ -958,6 +963,7 @@ dis_property_methods (MonoImage *m, guint32 prop, MonoGenericContainer *containe
 static char*
 dis_property_signature (MonoImage *m, guint32 prop_idx, MonoGenericContainer *container)
 {
+       MonoError error;
        MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
        const char *ptr;
        guint32 pcount, i;
@@ -980,8 +986,13 @@ dis_property_signature (MonoImage *m, guint32 prop_idx, MonoGenericContainer *co
                g_string_append (res, "instance ");
        ptr++;
        pcount = mono_metadata_decode_value (ptr, &ptr);
-       type = mono_metadata_parse_type_full (m, container, 0, ptr, &ptr);
-       blurb = dis_stringify_type (m, type, TRUE);
+       type = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, &error);
+       if (type) {
+               blurb = dis_stringify_type (m, type, TRUE);
+       } else {
+               blurb = g_strdup_printf ("Invalid type due to %s", mono_error_get_message (&error));
+               mono_error_cleanup (&error);
+       }
        if (prop_flags & 0x0200)
                g_string_append (res, "specialname ");
        if (prop_flags & 0x0400)
@@ -993,8 +1004,14 @@ dis_property_signature (MonoImage *m, guint32 prop_idx, MonoGenericContainer *co
        for (i = 0; i < pcount; i++) {
                if (i)
                        g_string_append (res, ", ");
-               param = mono_metadata_parse_type_full (m, container, 0, ptr, &ptr);
-               blurb = dis_stringify_param (m, param);
+               param = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, &error);
+               if (type) {
+                       blurb = dis_stringify_param (m, param);
+               } else {
+                       blurb = g_strdup_printf ("Invalid type due to %s", mono_error_get_message (&error));
+                       mono_error_cleanup (&error);
+               }
+
                g_string_append (res, blurb);
                g_free (blurb);
        }
@@ -1515,13 +1532,19 @@ dis_data (MonoImage *m)
        MonoType *type;
 
        for (i = 0; i < t->rows; i++) {
+               MonoError error;
                mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
                rva = mono_image_rva_map (m, cols [MONO_FIELD_RVA_RVA]);
                sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
                mono_metadata_decode_value (sig, &sig);
                /* FIELD signature == 0x06 */
                g_assert (*sig == 0x06);
-               type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
+               type = mono_metadata_parse_type_checked (m, NULL, 0, FALSE, sig + 1, &sig, &error);
+               if (!type) {
+                       fprintf (output, "// invalid field %d due to %s\n", i, mono_error_get_message (&error));
+                       mono_error_cleanup (&error);
+                       continue;
+               }
                mono_class_init (mono_class_from_mono_type (type));
                size = mono_type_size (type, &align);
 
@@ -1595,7 +1618,7 @@ struct {
  *
  * Disassembles the @file file.
  */
-static void
+static int
 disassemble_file (const char *file)
 {
        MonoImageOpenStatus status;
@@ -1604,7 +1627,7 @@ disassemble_file (const char *file)
        img = mono_image_open (file, &status);
        if (!img) {
                fprintf (stderr, "Error while trying to process %s\n", file);
-               return;
+               return 1;
        } else {
                /* FIXME: is this call necessary? */
                mono_assembly_load_from_full (img, file, &status, FALSE);
@@ -1637,6 +1660,7 @@ disassemble_file (const char *file)
        }
        
        mono_image_close (img);
+       return 0;
 }
 
 typedef struct {
@@ -1940,9 +1964,16 @@ usage (void)
        exit (1);
 }
 
+static void
+thread_state_init (MonoThreadUnwindState *ctx)
+{
+}
+
 int
 main (int argc, char *argv [])
 {
+       MonoThreadInfoRuntimeCallbacks ticallbacks;
+
        GList *input_files = NULL, *l;
        int i, j;
 
@@ -1993,6 +2024,15 @@ main (int argc, char *argv [])
        if (input_files == NULL)
                usage ();
 
+       CHECKED_MONO_INIT ();
+       mono_counters_init ();
+       memset (&ticallbacks, 0, sizeof (ticallbacks));
+       ticallbacks.thread_state_init = thread_state_init;
+#ifndef HOST_WIN32
+       mono_w32handle_init ();
+#endif
+       mono_threads_runtime_init (&ticallbacks);
+
        mono_install_assembly_load_hook (monodis_assembly_load_hook, NULL);
        mono_install_assembly_search_hook (monodis_assembly_search_hook, NULL);
 
@@ -2006,12 +2046,14 @@ main (int argc, char *argv [])
 
                mono_install_assembly_preload_hook (monodis_preload, GUINT_TO_POINTER (FALSE));
 
-               disassemble_file (filename);
+               return disassemble_file (filename);
        } else {
                mono_init (argv [0]);
 
+               i = 0;
                for (l = input_files; l; l = l->next)
-                       disassemble_file ((const char *)l->data);
+                       if (disassemble_file ((const char *)l->data) == 1) i = 1;
+               return i;
        }
 
        return 0;