Merge pull request #487 from mayerwin/patch-1
[mono.git] / mono / metadata / metadata-verify.c
index 229b35273d136175834b48c689f771c768abf628..72991f977fa567f2167123bdefe4cb6c15f6323e 100644 (file)
@@ -23,6 +23,7 @@
 #include <mono/metadata/cil-coff.h>
 #include <mono/metadata/attrdefs.h>
 #include <mono/utils/strenc.h>
+#include <mono/utils/mono-error-internals.h>
 #include <string.h>
 //#include <signal.h>
 #include <ctype.h>
@@ -137,7 +138,7 @@ const static unsigned char coded_index_desc[] = {
        5, /*tables*/
        MONO_TABLE_TYPEDEF,
        MONO_TABLE_TYPEREF,
-       MONO_TABLE_MODULE,
+       MONO_TABLE_MODULEREF,
        MONO_TABLE_METHOD,
        MONO_TABLE_TYPESPEC,
 
@@ -217,11 +218,12 @@ typedef struct {
 
 typedef struct {
        const char *data;
-       guint32 size;
+       guint32 size, token;
        GSList *errors;
        int valid;
        MonoImage *image;
        gboolean report_error;
+       gboolean report_warning;
        int stage;
 
        DataDirectory data_directories [16];
@@ -240,6 +242,21 @@ typedef struct {
                (__ctx)->errors = g_slist_prepend ((__ctx)->errors, vinfo);     \
        } while (0)
 
+#define ADD_WARNING(__ctx, __msg)      \
+       do {    \
+               if ((__ctx)->report_warning) { \
+                       ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_WARNING, MONO_EXCEPTION_INVALID_PROGRAM); \
+                       (__ctx)->valid = 0; \
+                       return; \
+               } \
+       } while (0)
+
+#define ADD_ERROR_NO_RETURN(__ctx, __msg)      \
+       do {    \
+               if ((__ctx)->report_error) \
+                       ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
+               (__ctx)->valid = 0; \
+       } while (0)
 
 #define ADD_ERROR(__ctx, __msg)        \
        do {    \
@@ -283,6 +300,16 @@ dword_align (const char *ptr)
 #endif
 }
 
+static void
+add_from_mono_error (VerifyContext *ctx, MonoError *error)
+{
+       if (mono_error_ok (error))
+               return;
+
+       ADD_ERROR (ctx, g_strdup (mono_error_get_message (error)));
+       mono_error_cleanup (error);
+}
+
 static guint32
 pe_signature_offset (VerifyContext *ctx)
 {
@@ -733,7 +760,7 @@ verify_metadata_header (VerifyContext *ctx)
 {
        int i;
        DataDirectory it = get_data_dir (ctx, CLI_HEADER_IDX);
-       guint32 offset;
+       guint32 offset, section_count;
        const char *ptr;
 
        offset = it.translated_offset;
@@ -763,13 +790,14 @@ verify_metadata_header (VerifyContext *ctx)
 
        ptr = ctx->data + offset; //move to streams header 
 
-       if (read16 (ptr + 2) < 3)
-               ADD_ERROR (ctx, g_strdup_printf ("Metadata root section must have at least 3 streams (#~, #GUID and #Blob"));
+       section_count = read16 (ptr + 2);
+       if (section_count < 2)
+               ADD_ERROR (ctx, g_strdup_printf ("Metadata root section must have at least 2 streams (#~ and #GUID)"));
 
        ptr += 4;
        offset += 4;
 
-       for (i = 0; i < 5; ++i) {
+       for (i = 0; i < section_count; ++i) {
                guint32 stream_off, stream_size;
                int string_size, stream_idx;
 
@@ -805,8 +833,12 @@ verify_metadata_header (VerifyContext *ctx)
                        stream_idx = GUID_STREAM;
                else if (!strncmp ("#~", ptr, 3))
                        stream_idx = TILDE_STREAM;
-               else
-                       ADD_ERROR (ctx, g_strdup_printf ("Metadata stream header %d invalid name %s", i, ptr));
+               else {
+                       ADD_WARNING (ctx, g_strdup_printf ("Metadata stream header %d invalid name %s", i, ptr));
+                       offset = pad4 (offset);
+                       ptr = ctx->data + offset;
+                       continue;
+               }
 
                if (ctx->metadata_streams [stream_idx].offset != 0)
                        ADD_ERROR (ctx, g_strdup_printf ("Duplicated metadata stream header %s", ptr));
@@ -822,9 +854,6 @@ verify_metadata_header (VerifyContext *ctx)
                ADD_ERROR (ctx, g_strdup_printf ("Metadata #~ stream missing"));
        if (!ctx->metadata_streams [GUID_STREAM].size)
                ADD_ERROR (ctx, g_strdup_printf ("Metadata guid stream missing"));
-       if (!ctx->metadata_streams [BLOB_STREAM].size)
-               ADD_ERROR (ctx, g_strdup_printf ("Metadata blob stream missing"));
-               
 }
 
 static void
@@ -837,11 +866,9 @@ verify_tables_schema (VerifyContext *ctx)
        guint32 count;
        int i;
 
-       //printf ("tables_area size %d offset %x %s\n", tables_area.size, tables_area.offset, ctx->image->name);
        if (tables_area.size < 24)
                ADD_ERROR (ctx, g_strdup_printf ("Table schemata size (%d) too small to for initial decoding (requires 24 bytes)", tables_area.size));
 
-       //printf ("ptr %x %x\n", ptr[4], ptr[5]);
        if (ptr [4] != 2 && ptr [4] != 1)
                ADD_ERROR (ctx, g_strdup_printf ("Invalid table schemata major version %d, expected 2", ptr [4]));
        if (ptr [5] != 0)
@@ -860,7 +887,7 @@ verify_tables_schema (VerifyContext *ctx)
                  Unused: 0x1E 0x1F 0x2D-0x3F
                  We don't care about the MS extensions.*/
                if (i == 0x3 || i == 0x5 || i == 0x7 || i == 0x13 || i == 0x16)
-                       ADD_ERROR (ctx, g_strdup_printf ("The metadata verifies doesn't support MS specific table %x", i));
+                       ADD_ERROR (ctx, g_strdup_printf ("The metadata verifier doesn't support MS specific table %x", i));
                if (i == 0x1E || i == 0x1F || i >= 0x2D)
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid table %x", i));
                ++count;
@@ -911,22 +938,31 @@ get_metadata_stream (VerifyContext *ctx, MonoStreamHeader *header)
 }
 
 static gboolean
-is_valid_string_full (VerifyContext *ctx, guint32 offset, gboolean allow_empty)
+is_valid_string_full_with_image (MonoImage *image, guint32 offset, gboolean allow_empty)
 {
-       OffsetAndSize strings = get_metadata_stream (ctx, &ctx->image->heap_strings);
+       guint32 heap_offset = (char*)image->heap_strings.data - image->raw_data;
+       guint32 heap_size = image->heap_strings.size;
+
        glong length;
-       const char *data = ctx->data + strings.offset;
+       const char *data = image->raw_data + heap_offset;
 
-       if (offset >= strings.size)
+       if (offset >= heap_size)
                return FALSE;
-       if (data + offset < data) //FIXME, use a generalized and smart unsigned add with overflow check and fix the whole thing  
+       if (CHECK_ADDP_OVERFLOW_UN (data, offset))
                return FALSE;
 
-       if (!mono_utf8_validate_and_len_with_bounds (data + offset, strings.size - offset, &length, NULL))
+       if (!mono_utf8_validate_and_len_with_bounds (data + offset, heap_size - offset, &length, NULL))
                return FALSE;
        return allow_empty || length > 0;
 }
 
+
+static gboolean
+is_valid_string_full (VerifyContext *ctx, guint32 offset, gboolean allow_empty)
+{
+       return is_valid_string_full_with_image (ctx->image, offset, allow_empty);
+}
+
 static gboolean
 is_valid_string (VerifyContext *ctx, guint32 offset)
 {
@@ -977,7 +1013,7 @@ make_coded_token (int kind, guint32 table, guint32 table_idx)
 }
 
 static gboolean
-is_valid_coded_index (VerifyContext *ctx, int token_kind, guint32 coded_token)
+is_valid_coded_index_with_image (MonoImage *image, int token_kind, guint32 coded_token)
 {
        guint32 bits = coded_index_desc [token_kind++];
        guint32 table_count = coded_index_desc [token_kind++];
@@ -992,7 +1028,13 @@ is_valid_coded_index (VerifyContext *ctx, int token_kind, guint32 coded_token)
 
        if (table == INVALID_TABLE)
                return FALSE;
-       return token <= ctx->image->tables [table].rows;
+       return token <= image->tables [table].rows;
+}
+
+static gboolean
+is_valid_coded_index (VerifyContext *ctx, int token_kind, guint32 coded_token)
+{
+       return is_valid_coded_index_with_image (ctx->image, token_kind, coded_token);
 }
 
 typedef struct {
@@ -1100,7 +1142,7 @@ decode_value (const char *_ptr, unsigned available, unsigned *value, unsigned *s
 }
 
 static gboolean
-decode_signature_header (VerifyContext *ctx, guint32 offset, int *size, const char **first_byte)
+decode_signature_header (VerifyContext *ctx, guint32 offset, guint32 *size, const char **first_byte)
 {
        MonoStreamHeader blob = ctx->image->heap_blob;
        guint32 value, enc_size;
@@ -1111,14 +1153,16 @@ decode_signature_header (VerifyContext *ctx, guint32 offset, int *size, const ch
        if (!decode_value (blob.data + offset, blob.size - offset, &value, &enc_size))
                return FALSE;
 
-       if (offset + enc_size + value < offset)
+       if (CHECK_ADD4_OVERFLOW_UN (offset, enc_size))
                return FALSE;
 
-       if (offset + enc_size + value > blob.size)
+       offset += enc_size;
+
+       if (ADD_IS_GREATER_OR_OVF (offset, value, blob.size))
                return FALSE;
 
        *size = value;
-       *first_byte = blob.data + offset + enc_size;
+       *first_byte = blob.data + offset;
        return TRUE;
 }
 
@@ -1186,7 +1230,7 @@ parse_custom_mods (VerifyContext *ctx, const char **_ptr, const char *end)
                if (!safe_read_cint (token, ptr, end))
                        FAIL (ctx, g_strdup ("CustomMod: Not enough room for the token"));
        
-               if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, token))
+               if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, token) || !get_coded_index_token (TYPEDEF_OR_REF_DESC, token))
                        FAIL (ctx, g_strdup_printf ("CustomMod: invalid TypeDefOrRef token %x", token));
        }
 
@@ -1243,9 +1287,15 @@ parse_generic_inst (VerifyContext *ctx, const char **_ptr, const char *end)
        if (!safe_read_cint (token, ptr, end))
                FAIL (ctx, g_strdup ("GenericInst: Not enough room for type token"));
 
-       if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, token))
+       if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, token) || !get_coded_index_token (TYPEDEF_OR_REF_DESC, token))
                FAIL (ctx, g_strdup_printf ("GenericInst: invalid TypeDefOrRef token %x", token));
 
+       if (ctx->token) {
+               if (mono_metadata_token_index (ctx->token) == get_coded_index_token (TYPEDEF_OR_REF_DESC, token) &&
+                       mono_metadata_token_table (ctx->token) == get_coded_index_table (TYPEDEF_OR_REF_DESC, token))
+                       FAIL (ctx, g_strdup_printf ("Type: Recurside generic instance specification (%x). A type signature can't reference itself", ctx->token));
+       }
+
        if (!safe_read_cint (count, ptr, end))
                FAIL (ctx, g_strdup ("GenericInst: Not enough room for argument count"));
 
@@ -1253,6 +1303,9 @@ parse_generic_inst (VerifyContext *ctx, const char **_ptr, const char *end)
                FAIL (ctx, g_strdup ("GenericInst: Zero arguments generic instance"));
 
        for (i = 0; i < count; ++i) {
+               if (!parse_custom_mods (ctx, &ptr, end))
+                       FAIL (ctx, g_strdup ("Type: Failed to parse pointer custom attr"));
+
                if (!parse_type (ctx, &ptr, end))
                        FAIL (ctx, g_strdup_printf ("GenericInst: invalid generic argument %d", i + 1));
        }
@@ -1296,8 +1349,16 @@ parse_type (VerifyContext *ctx, const char **_ptr, const char *end)
                if (!safe_read_cint (token, ptr, end))
                        FAIL (ctx, g_strdup ("Type: Not enough room for the type token"));
        
-               if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, token))
+               if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, token) || !get_coded_index_token (TYPEDEF_OR_REF_DESC, token))
                        FAIL (ctx, g_strdup_printf ("Type: invalid TypeDefOrRef token %x", token));
+
+               if (!get_coded_index_token (TYPEDEF_OR_REF_DESC, token))
+                       FAIL (ctx, g_strdup_printf ("Type: zero TypeDefOrRef token %x", token));
+               if (ctx->token) {
+                       if (mono_metadata_token_index (ctx->token) == get_coded_index_token (TYPEDEF_OR_REF_DESC, token) &&
+                               mono_metadata_token_table (ctx->token) == get_coded_index_table (TYPEDEF_OR_REF_DESC, token))
+                               FAIL (ctx, g_strdup_printf ("Type: Recursive type specification (%x). A type signature can't reference itself", ctx->token));
+               }
                break;
 
        case MONO_TYPE_VAR:
@@ -1378,8 +1439,11 @@ parse_param (VerifyContext *ctx, const char **_ptr, const char *end)
        }
 
        //it's a byref, update the cursor ptr
-       if (type == MONO_TYPE_BYREF)
+       if (type == MONO_TYPE_BYREF) {
                *_ptr = ptr;
+               if (!parse_custom_mods (ctx, _ptr, end))
+                       return FALSE;
+       }
 
        return parse_type (ctx, _ptr, end);
 }
@@ -1468,6 +1532,8 @@ parse_property_signature (VerifyContext *ctx, const char **_ptr, const char *end
                FAIL (ctx, g_strdup ("PropertySig: Could not parse property type"));
 
        for (i = 0; i < param_count; ++i) {
+               if (!parse_custom_mods (ctx, &ptr, end))
+                       FAIL (ctx, g_strdup ("Type: Failed to parse pointer custom attr"));
                if (!parse_type (ctx, &ptr, end))
                        FAIL (ctx, g_strdup_printf ("PropertySig: Error parsing arg %d", i));
        }
@@ -1555,7 +1621,7 @@ parse_locals_signature (VerifyContext *ctx, const char **_ptr, const char *end)
 static gboolean
 is_valid_field_signature (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        unsigned signature = 0;
        const char *ptr = NULL, *end;
 
@@ -1576,7 +1642,7 @@ is_valid_field_signature (VerifyContext *ctx, guint32 offset)
 static gboolean
 is_valid_method_signature (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        const char *ptr = NULL, *end;
 
        if (!decode_signature_header (ctx, offset, &size, &ptr))
@@ -1586,10 +1652,24 @@ is_valid_method_signature (VerifyContext *ctx, guint32 offset)
        return parse_method_signature (ctx, &ptr, end, FALSE, FALSE);
 }
 
+static gboolean
+is_valid_memberref_method_signature (VerifyContext *ctx, guint32 offset)
+{
+       guint32 size = 0;
+       const char *ptr = NULL, *end;
+
+       if (!decode_signature_header (ctx, offset, &size, &ptr))
+               FAIL (ctx, g_strdup ("MemberRefSig: Could not decode signature header"));
+       end = ptr + size;
+
+       return parse_method_signature (ctx, &ptr, end, TRUE, FALSE);
+}
+
+
 static gboolean
 is_valid_method_or_field_signature (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        unsigned signature = 0;
        const char *ptr = NULL, *end;
 
@@ -1608,9 +1688,9 @@ is_valid_method_or_field_signature (VerifyContext *ctx, guint32 offset)
 }
 
 static gboolean
-is_vald_cattr_blob (VerifyContext *ctx, guint32 offset)
+is_valid_cattr_blob (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        unsigned prolog = 0;
        const char *ptr = NULL, *end;
 
@@ -1626,7 +1706,328 @@ is_vald_cattr_blob (VerifyContext *ctx, guint32 offset)
 
        if (prolog != 1)
                FAIL (ctx, g_strdup_printf ("CustomAttribute: Prolog is 0x%x, expected 0x1", prolog));
-               
+
+       return TRUE;
+}
+
+static gboolean
+is_valid_cattr_type (MonoType *type)
+{
+       MonoClass *klass;
+
+       if (type->type == MONO_TYPE_OBJECT || (type->type >= MONO_TYPE_BOOLEAN && type->type <= MONO_TYPE_STRING))
+               return TRUE;
+
+       if (type->type == MONO_TYPE_VALUETYPE) {
+               klass = mono_class_from_mono_type (type);
+               return klass && klass->enumtype;
+       }
+
+       if (type->type == MONO_TYPE_CLASS)
+               return mono_class_from_mono_type (type) == mono_defaults.systemtype_class;
+
+       return FALSE;
+}
+
+static gboolean
+is_valid_ser_string_full (VerifyContext *ctx, const char **str_start, guint32 *str_len, const char **_ptr, const char *end)
+{
+       guint32 size = 0;
+       const char *ptr = *_ptr;
+
+       *str_start = NULL;
+       *str_len = 0;
+
+       if (ptr >= end)
+               FAIL (ctx, g_strdup ("CustomAttribute: Not enough room for string size"));
+
+       /*NULL string*/
+       if (*ptr == (char)0xFF) {
+               *_ptr = ptr + 1;
+               return TRUE;
+       }
+
+       if (!safe_read_cint (size, ptr, end))
+               FAIL (ctx, g_strdup ("CustomAttribute: Not enough room for string size"));
+
+       if (ADDP_IS_GREATER_OR_OVF (ptr, size, end))
+               FAIL (ctx, g_strdup ("CustomAttribute: Not enough room for string"));
+
+       *str_start = ptr;
+       *str_len = size;
+
+       *_ptr = ptr + size;
+       return TRUE;
+}
+
+static gboolean
+is_valid_ser_string (VerifyContext *ctx, const char **_ptr, const char *end)
+{
+       const char *dummy_str;
+       guint32 dummy_int;
+       return is_valid_ser_string_full (ctx, &dummy_str, &dummy_int, _ptr, end);
+}
+
+static MonoClass*
+get_enum_by_encoded_name (VerifyContext *ctx, const char **_ptr, const char *end)
+{
+       MonoType *type;
+       MonoClass *klass;
+       const char *str_start = NULL;
+       const char *ptr = *_ptr;
+       char *enum_name;
+       guint32 str_len = 0;
+
+       if (!is_valid_ser_string_full (ctx, &str_start, &str_len, &ptr, end))
+               return NULL;
+
+       /*NULL or empty string*/
+       if (str_start == NULL || str_len == 0) {
+               ADD_ERROR_NO_RETURN (ctx, g_strdup ("CustomAttribute: Null or empty enum name"));
+               return NULL;
+       }
+
+       enum_name = g_memdup (str_start, str_len + 1);
+       enum_name [str_len] = 0;
+       type = mono_reflection_type_from_name (enum_name, ctx->image);
+       if (!type) {
+               ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("CustomAttribute: Invalid enum class %s", enum_name));
+               g_free (enum_name);
+               return NULL;
+       }
+       g_free (enum_name);
+
+       klass = mono_class_from_mono_type (type);
+       if (!klass || !klass->enumtype) {
+               ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("CustomAttribute:Class %s::%s is not an enum", klass->name_space, klass->name));
+               return NULL;
+       }
+
+       *_ptr = ptr;
+       return klass;
+}
+
+static gboolean
+is_valid_fixed_param (VerifyContext *ctx, MonoType *mono_type, const char **_ptr, const char *end)
+{
+       MonoClass *klass;
+       const char *ptr = *_ptr;
+       int elem_size = 0;
+       guint32 element_count, i;
+       int type;
+
+       klass = mono_type->data.klass;
+       type = mono_type->type;
+
+handle_enum:
+       switch (type) {
+       case MONO_TYPE_BOOLEAN:
+       case MONO_TYPE_I1:
+       case MONO_TYPE_U1:
+               elem_size = 1;
+               break;
+       case MONO_TYPE_I2:
+       case MONO_TYPE_U2:
+       case MONO_TYPE_CHAR:
+               elem_size = 2;
+               break;
+       case MONO_TYPE_I4:
+       case MONO_TYPE_U4:
+       case MONO_TYPE_R4:
+               elem_size = 4;
+               break;
+       case MONO_TYPE_I8:
+       case MONO_TYPE_U8:
+       case MONO_TYPE_R8:
+               elem_size = 8;
+               break;
+
+       case MONO_TYPE_STRING:
+               *_ptr = ptr;
+               return is_valid_ser_string (ctx, _ptr, end);
+
+       case MONO_TYPE_OBJECT: {
+               unsigned sub_type = 0;
+               if (!safe_read8 (sub_type, ptr, end))
+                       FAIL (ctx, g_strdup ("CustomAttribute: Not enough room for array type"));
+
+               if (sub_type >= MONO_TYPE_BOOLEAN && sub_type <= MONO_TYPE_STRING) {
+                       type = sub_type;
+                       goto handle_enum;
+               }
+               if (sub_type == MONO_TYPE_ENUM) {
+                       klass = get_enum_by_encoded_name (ctx, &ptr, end);
+                       if (!klass)
+                               return FALSE;
+
+                       klass = klass->element_class;
+                       type = klass->byval_arg.type;
+                       goto handle_enum;
+               }
+               if (sub_type == 0x50) { /*Type*/
+                       *_ptr = ptr;
+                       return is_valid_ser_string (ctx, _ptr, end);
+               }
+               if (sub_type == MONO_TYPE_SZARRAY) {
+                       MonoType simple_type = {{0}};
+                       unsigned etype = 0;
+                       if (!safe_read8 (etype, ptr, end))
+                               FAIL (ctx, g_strdup ("CustomAttribute: Not enough room for array element type"));
+
+                       if (etype == MONO_TYPE_ENUM) {
+                               klass = get_enum_by_encoded_name (ctx, &ptr, end);
+                               if (!klass)
+                                       return FALSE;
+                       } else if (etype == 0x50 || etype == MONO_TYPE_CLASS) {
+                               klass = mono_defaults.systemtype_class;
+                       } else if ((etype >= MONO_TYPE_BOOLEAN && etype <= MONO_TYPE_STRING) || etype == 0x51) {
+                               simple_type.type = etype == 0x51 ? MONO_TYPE_OBJECT : etype;
+                               klass = mono_class_from_mono_type (&simple_type);
+                       } else
+                               FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid array element type %x", etype));
+
+                       type = MONO_TYPE_SZARRAY;
+                       goto handle_enum;
+               }
+               FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid boxed object type %x", sub_type));
+       }
+
+
+       case MONO_TYPE_CLASS:
+               if (klass != mono_defaults.systemtype_class)
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid class parameter type %s:%s ",klass->name_space, klass->name));
+               *_ptr = ptr;
+               return is_valid_ser_string (ctx, _ptr, end);
+
+       case MONO_TYPE_VALUETYPE:
+               if (!klass || !klass->enumtype)
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid valuetype parameter expected enum %s:%s ",klass->name_space, klass->name));
+
+               klass = klass->element_class;
+               type = klass->byval_arg.type;
+               goto handle_enum;
+
+       case MONO_TYPE_SZARRAY:
+               mono_type = &klass->byval_arg;
+               if (!is_valid_cattr_type (mono_type))
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid array element type %s:%s ",klass->name_space, klass->name));
+               if (!safe_read32 (element_count, ptr, end))
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid class parameter type %s:%s ",klass->name_space, klass->name));
+               if (element_count == 0xFFFFFFFFu) {
+                       *_ptr = ptr;
+                       return TRUE;
+               }
+               for (i = 0; i < element_count; ++i) {
+                       if (!is_valid_fixed_param (ctx, mono_type, &ptr, end))
+                               return FALSE;
+               }
+               *_ptr = ptr;
+               return TRUE;
+       default:
+               FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid parameter type %x ", type));
+       }
+
+       if (ADDP_IS_GREATER_OR_OVF (ptr, elem_size, end))
+               FAIL (ctx, g_strdup ("CustomAttribute: Not enough space for element"));
+       *_ptr = ptr + elem_size;
+       return TRUE;
+}
+
+static gboolean
+is_valid_cattr_content (VerifyContext *ctx, MonoMethod *ctor, const char *ptr, guint32 size)
+{
+       MonoError error;
+       unsigned prolog = 0;
+       const char *end;
+       MonoMethodSignature *sig;
+       int args, i;
+       unsigned num_named;
+
+       if (!ctor)
+               FAIL (ctx, g_strdup ("CustomAttribute: Invalid constructor"));
+
+       sig = mono_method_signature_checked (ctor, &error);
+       if (!mono_error_ok (&error)) {
+               ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("CustomAttribute: Invalid constructor signature %s", mono_error_get_message (&error)));
+               mono_error_cleanup (&error);
+               return FALSE;
+       }
+
+       if (sig->sentinelpos != -1 || sig->call_convention == MONO_CALL_VARARG)
+               FAIL (ctx, g_strdup ("CustomAttribute: Constructor cannot have VARAG signature"));
+
+       end = ptr + size;
+
+       if (!safe_read16 (prolog, ptr, end))
+               FAIL (ctx, g_strdup ("CustomAttribute: Not enough room for prolog"));
+
+       if (prolog != 1)
+               FAIL (ctx, g_strdup_printf ("CustomAttribute: Prolog is 0x%x, expected 0x1", prolog));
+
+       args = sig->param_count;
+       for (i = 0; i < args; ++i) {
+               MonoType *arg_type = sig->params [i];
+               if (!is_valid_fixed_param (ctx, arg_type, &ptr, end))
+                       return FALSE;
+       }
+
+       if (!safe_read16 (num_named, ptr, end))
+               FAIL (ctx, g_strdup ("CustomAttribute: Not enough space for num_named field"));
+
+       for (i = 0; i < num_named; ++i) {
+               MonoType *type, simple_type = {{0}};
+               unsigned kind;
+
+               if (!safe_read8 (kind, ptr, end))
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Not enough space for named parameter %d kind", i));
+               if (kind != 0x53 && kind != 0x54)
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid named parameter %d kind %x", i, kind));
+               if (!safe_read8 (kind, ptr, end))
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Not enough space for named parameter %d type", i));
+
+               if (kind >= MONO_TYPE_BOOLEAN && kind <= MONO_TYPE_STRING) {
+                       simple_type.type = kind;
+                       type = &simple_type;
+               } else if (kind == MONO_TYPE_ENUM) {
+                       MonoClass *klass = get_enum_by_encoded_name (ctx, &ptr, end);
+                       if (!klass)
+                               return FALSE;
+                       type = &klass->byval_arg;
+               } else if (kind == 0x50) {
+                       type = &mono_defaults.systemtype_class->byval_arg;
+               } else if (kind == 0x51) {
+                       type = &mono_defaults.object_class->byval_arg;
+               } else if (kind == MONO_TYPE_SZARRAY) {
+                       MonoClass *klass;
+                       unsigned etype = 0;
+                       if (!safe_read8 (etype, ptr, end))
+                               FAIL (ctx, g_strdup ("CustomAttribute: Not enough room for array element type"));
+
+                       if (etype == MONO_TYPE_ENUM) {
+                               klass = get_enum_by_encoded_name (ctx, &ptr, end);
+                               if (!klass)
+                                       return FALSE;
+                       } else if (etype == 0x50 || etype == MONO_TYPE_CLASS) {
+                               klass = mono_defaults.systemtype_class;
+                       } else if ((etype >= MONO_TYPE_BOOLEAN && etype <= MONO_TYPE_STRING) || etype == 0x51) {
+                               simple_type.type = etype == 0x51 ? MONO_TYPE_OBJECT : etype;
+                               klass = mono_class_from_mono_type (&simple_type);
+                       } else
+                               FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid array element type %x", etype));
+
+                       type = &mono_array_class_get (klass, 1)->byval_arg;
+               } else {
+                       FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid named parameter type %x", kind));
+               }
+
+               if (!is_valid_ser_string (ctx, &ptr, end))
+                       return FALSE;
+
+               if (!is_valid_fixed_param (ctx, type, &ptr, end))
+                       return FALSE;
+
+       }
+
        return TRUE;
 }
 
@@ -1649,7 +2050,7 @@ is_valid_permission_set (VerifyContext *ctx, guint32 offset)
 static gboolean
 is_valid_standalonesig_blob (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        unsigned signature = 0;
        const char *ptr = NULL, *end;
 
@@ -1663,13 +2064,18 @@ is_valid_standalonesig_blob (VerifyContext *ctx, guint32 offset)
        --ptr;
        if (signature == 0x07)
                return parse_locals_signature (ctx, &ptr, end);
+
+       /*F# and managed C++ produce standalonesig for fields even thou the spec doesn't mention it.*/
+       if (signature == 0x06)
+               return parse_field (ctx, &ptr, end);
+
        return parse_method_signature (ctx, &ptr, end, TRUE, TRUE);
 }
 
 static gboolean
 is_valid_property_sig_blob (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        const char *ptr = NULL, *end;
 
        if (!decode_signature_header (ctx, offset, &size, &ptr))
@@ -1682,11 +2088,10 @@ is_valid_property_sig_blob (VerifyContext *ctx, guint32 offset)
 static gboolean
 is_valid_typespec_blob (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        const char *ptr = NULL, *end;
        unsigned type = 0;
        
-
        if (!decode_signature_header (ctx, offset, &size, &ptr))
                FAIL (ctx, g_strdup ("TypeSpec: Could not decode signature header"));
        end = ptr + size;
@@ -1712,9 +2117,9 @@ is_valid_typespec_blob (VerifyContext *ctx, guint32 offset)
 }
 
 static gboolean
-is_valid_methodspec_blog (VerifyContext *ctx, guint32 offset)
+is_valid_methodspec_blob (VerifyContext *ctx, guint32 offset)
 {
-       int size = 0;
+       guint32 size = 0;
        const char *ptr = NULL, *end;
        unsigned type = 0;
        unsigned count = 0, i;
@@ -1736,6 +2141,8 @@ is_valid_methodspec_blog (VerifyContext *ctx, guint32 offset)
                FAIL (ctx, g_strdup ("MethodSpec: Zero generic argument count"));
 
        for (i = 0; i < count; ++i) {
+               if (!parse_custom_mods (ctx, &ptr, end))
+                       return FALSE;
                if (!parse_type (ctx, &ptr, end))
                        FAIL (ctx, g_strdup_printf ("MethodSpec: Could not parse parameter %d", i + 1));
        }
@@ -1831,13 +2238,15 @@ is_valid_constant (VerifyContext *ctx, guint32 type, guint32 offset)
 #define SECTION_HEADER_INVALID_FLAGS 0x3E
 
 static gboolean
-is_valid_method_header (VerifyContext *ctx, guint32 rva)
+is_valid_method_header (VerifyContext *ctx, guint32 rva, guint32 *locals_token)
 {
        unsigned local_vars_tok, code_size, offset = mono_cli_rva_image_map (ctx->image, rva);
        unsigned header = 0;
        unsigned fat_header = 0, size = 0, max_stack;
        const char *ptr = NULL, *end;
 
+       *locals_token = 0;
+
        if (offset == INVALID_ADDRESS)
                FAIL (ctx, g_strdup ("MethodHeader: Invalid RVA"));
 
@@ -1880,6 +2289,9 @@ is_valid_method_header (VerifyContext *ctx, guint32 rva)
                        FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid local vars signature table 0x%x", ((local_vars_tok >> 24) & 0xFF)));
                if ((local_vars_tok & 0xFFFFFF) > ctx->image->tables [MONO_TABLE_STANDALONESIG].rows)   
                        FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid local vars signature points to invalid row 0x%x", local_vars_tok & 0xFFFFFF));
+               if (!(local_vars_tok & 0xFFFFFF))
+                       FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid local vars signature with zero index"));
+               *locals_token = local_vars_tok & 0xFFFFFF;
        }
 
        if (fat_header & FAT_HEADER_INVALID_FLAGS)
@@ -1914,9 +2326,14 @@ is_valid_method_header (VerifyContext *ctx, guint32 rva)
                        FAIL (ctx, g_strdup_printf ("MethodHeader: Not enough room for section content %d", section_size));
 
                if (section_header & METHOD_HEADER_SECTION_EHTABLE) {
-                       guint32 i, clauses = (section_size - 4) / (is_fat ? 24 : 12);
-                       if (clauses * (is_fat ? 24 : 12) + 4 != section_size)
-                               FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid EH section size %d, it's not of the proper size", section_size));
+                       guint32 i, clauses = section_size / (is_fat ? 24 : 12);
+                       /*
+                               LAMEIMPL: MS emits section_size without accounting for header size.
+                               Mono does as the spec says. section_size is header + section
+                               MS's peverify happily accepts both. 
+                       */
+                       if ((clauses * (is_fat ? 24 : 12) != section_size) && (clauses * (is_fat ? 24 : 12) + 4 != section_size))
+                               FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid EH section size %d, it's not of the expected size %d", section_size, clauses * (is_fat ? 24 : 12)));
 
                        /* only verify the class token is verified as the rest is done by the IL verifier*/
                        for (i = 0; i < clauses; ++i) {
@@ -1969,22 +2386,12 @@ static void
 verify_typeref_table (VerifyContext *ctx)
 {
        MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPEREF];
-       guint32 data [MONO_TYPEREF_SIZE];
-       int i;
+       MonoError error;
+       guint32 i;
 
        for (i = 0; i < table->rows; ++i) {
-               mono_metadata_decode_row (table, i, data, MONO_TYPEREF_SIZE);
-               if (!is_valid_coded_index (ctx, RES_SCOPE_DESC, data [MONO_TYPEREF_SCOPE]))
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid typeref row %d coded index 0x%08x", i, data [MONO_TYPEREF_SCOPE]));
-               
-               if (!get_coded_index_token (RES_SCOPE_DESC, data [MONO_TYPEREF_SCOPE]))
-                       ADD_ERROR (ctx, g_strdup_printf ("The metadata verifier doesn't support null ResolutionScope tokens for typeref row %d", i));
-
-               if (!data [MONO_TYPEREF_NAME] || !is_valid_non_empty_string (ctx, data [MONO_TYPEREF_NAME]))
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid typeref row %d name token 0x%08x", i, data [MONO_TYPEREF_NAME]));
-
-               if (data [MONO_TYPEREF_NAMESPACE] && !is_valid_non_empty_string (ctx, data [MONO_TYPEREF_NAMESPACE]))
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid typeref row %d namespace token 0x%08x", i, data [MONO_TYPEREF_NAMESPACE]));
+               mono_verifier_verify_typeref_row (ctx->image, i, &error);
+               add_from_mono_error (ctx, &error);
        }
 }
 
@@ -1995,7 +2402,7 @@ verify_typedef_table (VerifyContext *ctx)
 {
        MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPEDEF];
        guint32 data [MONO_TYPEDEF_SIZE];
-       guint32 fieldlist = 1, methodlist = 1;
+       guint32 fieldlist = 1, methodlist = 1, visibility;
        int i;
 
        if (table->rows == 0)
@@ -2027,6 +2434,14 @@ verify_typedef_table (VerifyContext *ctx)
                if (data [MONO_TYPEDEF_EXTENDS] && !is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, data [MONO_TYPEDEF_EXTENDS]))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d extend field coded index 0x%08x", i, data [MONO_TYPEDEF_EXTENDS]));
 
+               if (data [MONO_TYPEDEF_EXTENDS] && !get_coded_index_token (TYPEDEF_OR_REF_DESC, data [MONO_TYPEDEF_EXTENDS]))
+                       ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d zero coded extend field coded index 0x%08x", i, data [MONO_TYPEDEF_EXTENDS]));
+
+               visibility = data [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
+               if ((visibility >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visibility <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM) &&
+                       search_sorted_table (ctx, MONO_TABLE_NESTEDCLASS, MONO_NESTED_CLASS_NESTED, i + 1) == -1)
+                       ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d has nested visibility but no rows in the NestedClass table", i));
+
                if (data [MONO_TYPEDEF_FIELD_LIST] == 0)
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d FieldList be be >= 1", i));
 
@@ -2064,8 +2479,10 @@ verify_typedef_table_full (VerifyContext *ctx)
                mono_metadata_decode_row (table, i, data, MONO_TYPEDEF_SIZE);
 
                if (i == 0) {
-                       if (data [MONO_TYPEDEF_EXTENDS] != 0)
+                       /*XXX it's ok if <module> extends object, or anything at all, actually. */
+                       /*if (data [MONO_TYPEDEF_EXTENDS] != 0)
                                ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row 0 for the special <module> type must have a null extend field"));
+                       */
                        continue;
                }
 
@@ -2171,8 +2588,8 @@ verify_field_table_full (VerifyContext *ctx)
        }
 }
 
-/*bits 6,8,9,10,11,13,14,15*/
-#define INVALID_METHOD_IMPLFLAG_BITS ((1 << 6) | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 13) | (1 << 14) | (1 << 15))
+/*bits 8,9,10,11,13,14,15*/
+#define INVALID_METHOD_IMPLFLAG_BITS ((1 << 9) | (1 << 10) | (1 << 11) | (1 << 13) | (1 << 14) | (1 << 15))
 static void
 verify_method_table (VerifyContext *ctx)
 {
@@ -2221,12 +2638,14 @@ verify_method_table (VerifyContext *ctx)
                if (flags & METHOD_ATTRIBUTE_ABSTRACT) {
                        if (flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)
                                ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is Abstract and PinvokeImpl", i));
+                       if (flags & METHOD_ATTRIBUTE_FINAL)
+                               ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is Abstract and Final", i));
                        if (!(flags & METHOD_ATTRIBUTE_VIRTUAL))
                                ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is Abstract but not Virtual", i));
                }
 
                if (access == METHOD_ATTRIBUTE_COMPILER_CONTROLLED && (flags & (METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME)))
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is CompileControlled and SpecialName or RtSpecialName", i));
+                       ADD_WARNING (ctx, g_strdup_printf ("Invalid method row %d is CompileControlled and SpecialName or RtSpecialName", i));
 
                if ((flags & METHOD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & METHOD_ATTRIBUTE_SPECIAL_NAME))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is RTSpecialName but not SpecialName", i));
@@ -2249,8 +2668,12 @@ verify_method_table (VerifyContext *ctx)
                if ((flags & (METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_NEW_SLOT | METHOD_ATTRIBUTE_STRICT)) && !(flags & METHOD_ATTRIBUTE_VIRTUAL))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is (Final, NewSlot or Strict) but not Virtual", i));
 
-               if ((flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (flags & METHOD_ATTRIBUTE_VIRTUAL))
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is PinvokeImpl and Virtual", i));
+               if (flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
+                       if (flags & METHOD_ATTRIBUTE_VIRTUAL)
+                               ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is PinvokeImpl and Virtual", i));
+                       if (!(flags & METHOD_ATTRIBUTE_STATIC))
+                               ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is PinvokeImpl but not Static", i));
+               }
 
                if (!(flags & METHOD_ATTRIBUTE_ABSTRACT) && !rva && !(flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) && 
                                !(implflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && code_type != METHOD_IMPL_ATTRIBUTE_RUNTIME)
@@ -2262,8 +2685,8 @@ verify_method_table (VerifyContext *ctx)
                //TODO check signature contents
 
                if (rva) {
-                       if (flags & METHOD_ATTRIBUTE_ABSTRACT)
-                               ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d has RVA != 0 but is Abstract", i));
+                       if ((flags & (METHOD_ATTRIBUTE_ABSTRACT | METHOD_ATTRIBUTE_PINVOKE_IMPL)) || (implflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
+                               ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d has RVA != 0 but is either Abstract, InternalCall or PinvokeImpl", i));
                        if (code_type == METHOD_IMPL_ATTRIBUTE_OPTIL)
                                ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d has RVA != 0 but is CodeTypeMask is neither Native, CIL or Runtime", i));
                } else {
@@ -2304,7 +2727,7 @@ static void
 verify_method_table_full (VerifyContext *ctx)
 {
        MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHOD];
-       guint32 data [MONO_METHOD_SIZE], rva;
+       guint32 data [MONO_METHOD_SIZE], rva, locals_token;
        int i;
 
        for (i = 0; i < table->rows; ++i) {
@@ -2314,7 +2737,7 @@ verify_method_table_full (VerifyContext *ctx)
                if (!data [MONO_METHOD_SIGNATURE] || !is_valid_method_signature (ctx, data [MONO_METHOD_SIGNATURE]))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d invalid signature token 0x%08x", i, data [MONO_METHOD_SIGNATURE]));
 
-               if (rva && !is_valid_method_header (ctx, rva))
+               if (rva && !is_valid_method_header (ctx, rva, &locals_token))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d RVA points to an invalid method header", i));
        }
 }
@@ -2403,7 +2826,7 @@ verify_interfaceimpl_table (VerifyContext *ctx)
        for (i = 0; i < table->rows; ++i) {
                mono_metadata_decode_row (table, i, data, MONO_INTERFACEIMPL_SIZE);
                if (data [MONO_INTERFACEIMPL_CLASS] && data [MONO_INTERFACEIMPL_CLASS] > ctx->image->tables [MONO_TABLE_TYPEDEF].rows)
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid InterfaceImpl row %d Class field 0x%08x", i, data [MONO_TABLE_TYPEDEF]));
+                       ADD_ERROR (ctx, g_strdup_printf ("Invalid InterfaceImpl row %d Class field 0x%08x", i, data [MONO_INTERFACEIMPL_CLASS]));
 
                if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, data [MONO_INTERFACEIMPL_INTERFACE]))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid InterfaceImpl row %d Inteface field coded index 0x%08x", i, data [MONO_INTERFACEIMPL_INTERFACE]));
@@ -2491,8 +2914,8 @@ verify_cattr_table (VerifyContext *ctx)
                if (!is_valid_coded_index (ctx, HAS_CATTR_DESC, data [MONO_CUSTOM_ATTR_PARENT]))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute row %d Parent field 0x%08x", i, data [MONO_CUSTOM_ATTR_PARENT]));
 
-               if (!is_valid_coded_index (ctx, CATTR_TYPE_DESC, data [MONO_CUSTOM_ATTR_TYPE]))
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute row %d Parent field 0x%08x", i, data [MONO_CUSTOM_ATTR_PARENT]));
+               if (!is_valid_coded_index (ctx, CATTR_TYPE_DESC, data [MONO_CUSTOM_ATTR_TYPE]) || !get_coded_index_token (CATTR_TYPE_DESC, data [MONO_CUSTOM_ATTR_TYPE]))
+                       ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute row %d Type field 0x%08x", i, data [MONO_CUSTOM_ATTR_TYPE]));
 
                if (data [MONO_CUSTOM_ATTR_VALUE] && !is_valid_blob_object (ctx, data [MONO_CUSTOM_ATTR_VALUE], 0))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute row %d invalid value blob 0x%x", i, data [MONO_CUSTOM_ATTR_VALUE]));
@@ -2503,14 +2926,36 @@ static void
 verify_cattr_table_full (VerifyContext *ctx)
 {
        MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_CUSTOMATTRIBUTE];
-       guint32 data [MONO_CUSTOM_ATTR_SIZE];
+       MonoMethod *ctor;
+       const char *ptr;
+       guint32 data [MONO_CUSTOM_ATTR_SIZE], mtoken, size;
        int i;
 
        for (i = 0; i < table->rows; ++i) {
                mono_metadata_decode_row (table, i, data, MONO_CUSTOM_ATTR_SIZE);
 
-               if (!is_vald_cattr_blob (ctx, data [MONO_CUSTOM_ATTR_VALUE]))
+               if (!is_valid_cattr_blob (ctx, data [MONO_CUSTOM_ATTR_VALUE]))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute row %d Value field 0x%08x", i, data [MONO_CUSTOM_ATTR_VALUE]));
+
+               mtoken = data [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
+               switch (data [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
+               case MONO_CUSTOM_ATTR_TYPE_METHODDEF:
+                       mtoken |= MONO_TOKEN_METHOD_DEF;
+                       break;
+               case MONO_CUSTOM_ATTR_TYPE_MEMBERREF:
+                       mtoken |= MONO_TOKEN_MEMBER_REF;
+                       break;
+               default:
+                       ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute constructor row %d Token 0x%08x", i, data [MONO_CUSTOM_ATTR_TYPE]));
+               }
+
+               ctor = mono_get_method (ctx->image, mtoken, NULL);
+
+               /*This can't fail since this is checked in is_valid_cattr_blob*/
+               g_assert (decode_signature_header (ctx, data [MONO_CUSTOM_ATTR_VALUE], &size, &ptr));
+
+               if (!is_valid_cattr_content (ctx, ctor, ptr, size))
+                       ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute content row %d Value field 0x%08x", i, data [MONO_CUSTOM_ATTR_VALUE]));
        }
 }
 
@@ -2836,7 +3281,7 @@ verify_moduleref_table (VerifyContext *ctx)
                mono_metadata_decode_row (table, i, data, MONO_MODULEREF_SIZE);
 
                if (!is_valid_non_empty_string (ctx, data[MONO_MODULEREF_NAME]))
-                       ADD_ERROR (ctx, g_strdup_printf ("Invalid MethodImpl row %d Class field %08x", i, data [MONO_TABLE_TYPEDEF]));
+                       ADD_ERROR (ctx, g_strdup_printf ("Invalid ModuleRef row %d name field %08x", i, data [MONO_MODULEREF_NAME]));
        }
 }
 
@@ -2864,13 +3309,14 @@ verify_typespec_table_full (VerifyContext *ctx)
 
        for (i = 0; i < table->rows; ++i) {
                mono_metadata_decode_row (table, i, data, MONO_TYPESPEC_SIZE);
-
+               ctx->token = (i + 1) | MONO_TOKEN_TYPE_SPEC;
                if (!is_valid_typespec_blob (ctx, data [MONO_TYPESPEC_SIGNATURE]))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid TypeSpec row %d Signature field %08x", i, data [MONO_TYPESPEC_SIGNATURE]));
        }
+       ctx->token = 0;
 }
 
-#define INVALID_IMPLMAP_FLAGS_BITS ~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 6) | (1 << 8) | (1 << 9) | (1 << 10))
+#define INVALID_IMPLMAP_FLAGS_BITS ~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 12) | (1 << 13))
 static void
 verify_implmap_table (VerifyContext *ctx)
 {
@@ -2900,7 +3346,7 @@ verify_implmap_table (VerifyContext *ctx)
                if (!is_valid_non_empty_string (ctx, data [MONO_IMPLMAP_NAME]))
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d ImportName Token %x", i, data [MONO_IMPLMAP_NAME]));
 
-               if (!data [MONO_IMPLMAP_SCOPE] || data [MONO_IMPLMAP_SCOPE] > ctx->image->tables [MONO_TABLE_MODULEREF].rows + 1)
+               if (!data [MONO_IMPLMAP_SCOPE] || data [MONO_IMPLMAP_SCOPE] > ctx->image->tables [MONO_TABLE_MODULEREF].rows)
                        ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d Invalid ImportScope token %x", i, data [MONO_IMPLMAP_SCOPE]));
        }
 }
@@ -2923,7 +3369,7 @@ verify_fieldrva_table (VerifyContext *ctx)
        }
 }
 
-#define INVALID_ASSEMBLY_FLAGS_BITS ~((1 << 0) | (1 << 8) | (1 << 14) | (1 << 15))
+#define INVALID_ASSEMBLY_FLAGS_BITS ~((1 << 0) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 14) | (1 << 15))
 static void
 verify_assembly_table (VerifyContext *ctx)
 {
@@ -2955,7 +3401,7 @@ verify_assembly_table (VerifyContext *ctx)
        }
 }
 
-#define INVALID_ASSEMBLYREF_FLAGS_BITS ~(1)
+#define INVALID_ASSEMBLYREF_FLAGS_BITS ~((1 << 0) | (1 << 8) | (1 << 14) | (1 << 15))
 static void
 verify_assemblyref_table (VerifyContext *ctx)
 {
@@ -3168,7 +3614,7 @@ verify_method_spec_table_full (VerifyContext *ctx)
        for (i = 0; i < table->rows; ++i) {
                mono_metadata_decode_row (table, i, data, MONO_METHODSPEC_SIZE);
 
-               if (!is_valid_methodspec_blog (ctx, data [MONO_METHODSPEC_SIGNATURE]))
+               if (!is_valid_methodspec_blob (ctx, data [MONO_METHODSPEC_SIGNATURE]))
                        ADD_ERROR (ctx, g_strdup_printf ("MethodSpec table row %d has invalid Instantiation token %08x", i, data [MONO_METHODSPEC_SIGNATURE]));
        }
 }
@@ -3184,7 +3630,7 @@ verify_generic_param_constraint_table (VerifyContext *ctx)
                mono_metadata_decode_row (table, i, data, MONO_GENPARCONSTRAINT_SIZE);
 
                if (!data [MONO_GENPARCONSTRAINT_GENERICPAR] || data [MONO_GENPARCONSTRAINT_GENERICPAR] > ctx->image->tables [MONO_TABLE_GENERICPARAM].rows)
-                       ADD_ERROR (ctx, g_strdup_printf ("GenericParamConstraint table row %d has invalid Owner token %08x", i, data [MONO_TABLE_GENERICPARAM]));
+                       ADD_ERROR (ctx, g_strdup_printf ("GenericParamConstraint table row %d has invalid Owner token %08x", i, data [MONO_GENPARCONSTRAINT_GENERICPAR]));
 
                if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, data [MONO_GENPARCONSTRAINT_CONSTRAINT]))
                        ADD_ERROR (ctx, g_strdup_printf ("GenericParamConstraint table row %d has invalid Constraint token %08x", i, data [MONO_GENPARCONSTRAINT_CONSTRAINT]));
@@ -3194,6 +3640,109 @@ verify_generic_param_constraint_table (VerifyContext *ctx)
        }
 }
 
+
+typedef struct {
+       const char *name;
+       const char *name_space;
+       guint32 resolution_scope;
+} TypeDefUniqueId;
+
+static guint
+typedef_hash (gconstpointer _key)
+{
+       const TypeDefUniqueId *key = _key;
+       return g_str_hash (key->name) ^ g_str_hash (key->name_space) ^ key->resolution_scope; /*XXX better salt the int key*/
+}
+
+static gboolean
+typedef_equals (gconstpointer _a, gconstpointer _b)
+{
+       const TypeDefUniqueId *a = _a;
+       const TypeDefUniqueId *b = _b;
+       return !strcmp (a->name, b->name) && !strcmp (a->name_space, b->name_space) && a->resolution_scope == b->resolution_scope;
+}
+
+static void
+verify_typedef_table_global_constraints (VerifyContext *ctx)
+{
+       int i;
+       guint32 data [MONO_TYPEDEF_SIZE];
+       guint32 nested_data [MONO_NESTED_CLASS_SIZE];
+       MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPEDEF];
+       MonoTableInfo *nested_table = &ctx->image->tables [MONO_TABLE_NESTEDCLASS];
+       GHashTable *unique_types = g_hash_table_new_full (&typedef_hash, &typedef_equals, g_free, NULL);
+
+       for (i = 0; i < table->rows; ++i) {
+               guint visibility;
+               TypeDefUniqueId *type = g_new (TypeDefUniqueId, 1);
+               mono_metadata_decode_row (table, i, data, MONO_TYPEDEF_SIZE);
+
+               type->name = mono_metadata_string_heap (ctx->image, data [MONO_TYPEDEF_NAME]);
+               type->name_space = mono_metadata_string_heap (ctx->image, data [MONO_TYPEDEF_NAMESPACE]);
+               type->resolution_scope = 0;
+
+               visibility = data [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
+               if (visibility >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visibility <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM) {
+                       int res = search_sorted_table (ctx, MONO_TABLE_NESTEDCLASS, MONO_NESTED_CLASS_NESTED, i + 1);
+                       g_assert (res >= 0);
+
+                       mono_metadata_decode_row (nested_table, res, nested_data, MONO_NESTED_CLASS_SIZE);
+                       type->resolution_scope = nested_data [MONO_NESTED_CLASS_ENCLOSING];
+               }
+
+               if (g_hash_table_lookup (unique_types, type)) {
+                       ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("TypeDef table row %d has duplicate for tuple (%s,%s,%x)", i, type->name, type->name_space, type->resolution_scope));
+                       g_hash_table_destroy (unique_types);
+                       g_free (type);
+                       return;
+               }
+               g_hash_table_insert (unique_types, type, GUINT_TO_POINTER (1));
+       }
+
+       g_hash_table_destroy (unique_types);
+}
+
+static void
+verify_typeref_table_global_constraints (VerifyContext *ctx)
+{
+       int i;
+       guint32 data [MONO_TYPEREF_SIZE];
+       MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPEREF];
+       GHashTable *unique_types = g_hash_table_new_full (&typedef_hash, &typedef_equals, g_free, NULL);
+
+       for (i = 0; i < table->rows; ++i) {
+               TypeDefUniqueId *type = g_new (TypeDefUniqueId, 1);
+               mono_metadata_decode_row (table, i, data, MONO_TYPEREF_SIZE);
+
+               type->resolution_scope = data [MONO_TYPEREF_SCOPE];
+               type->name = mono_metadata_string_heap (ctx->image, data [MONO_TYPEREF_NAME]);
+               type->name_space = mono_metadata_string_heap (ctx->image, data [MONO_TYPEREF_NAMESPACE]);
+
+               if (g_hash_table_lookup (unique_types, type)) {
+                       ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("TypeRef table row %d has duplicate for tuple (%s,%s,%x)", i, type->name, type->name_space, type->resolution_scope));
+                       g_hash_table_destroy (unique_types);
+                       g_free (type);
+                       return;
+               }
+               g_hash_table_insert (unique_types, type, GUINT_TO_POINTER (1));
+       }
+
+       g_hash_table_destroy (unique_types);
+}
+
+static void
+verify_tables_data_global_constraints (VerifyContext *ctx)
+{
+       verify_typedef_table_global_constraints (ctx);
+}
+
+static void
+verify_tables_data_global_constraints_full (VerifyContext *ctx)
+{
+       verify_typeref_table (ctx);
+       verify_typeref_table_global_constraints (ctx);
+}
+
 static void
 verify_tables_data (VerifyContext *ctx)
 {
@@ -3221,8 +3770,9 @@ verify_tables_data (VerifyContext *ctx)
 
        verify_module_table (ctx);
        CHECK_ERROR ();
+       /*Obfuscators love to place broken stuff in the typeref table
        verify_typeref_table (ctx);
-       CHECK_ERROR ();
+       CHECK_ERROR ();*/
        verify_typedef_table (ctx);
        CHECK_ERROR ();
        verify_field_table (ctx);
@@ -3284,14 +3834,17 @@ verify_tables_data (VerifyContext *ctx)
        verify_method_spec_table (ctx);
        CHECK_ERROR ();
        verify_generic_param_constraint_table (ctx);
+       CHECK_ERROR ();
+       verify_tables_data_global_constraints (ctx);
 }
 
 static void
-init_verify_context (VerifyContext *ctx, MonoImage *image, GSList **error_list)
+init_verify_context (VerifyContext *ctx, MonoImage *image, gboolean report_error)
 {
        memset (ctx, 0, sizeof (VerifyContext));
        ctx->image = image;
-       ctx->report_error = error_list != NULL;
+       ctx->report_error = report_error;
+       ctx->report_warning = FALSE; //export this setting in the API
        ctx->valid = 1;
        ctx->size = image->raw_data_len;
        ctx->data = image->raw_data;
@@ -3308,6 +3861,18 @@ cleanup_context (VerifyContext *ctx, GSList **error_list)
        return ctx->valid;      
 }
 
+static gboolean
+cleanup_context_checked (VerifyContext *ctx, MonoError *error)
+{
+       g_free (ctx->sections);
+       if (ctx->errors) {
+               MonoVerifyInfo *info = ctx->errors->data;
+               mono_error_set_bad_image (error, ctx->image, "%s", info->message);
+               mono_free_verify_list (ctx->errors);
+       }
+       return ctx->valid;
+}
+
 gboolean
 mono_verifier_verify_pe_data (MonoImage *image, GSList **error_list)
 {
@@ -3316,7 +3881,7 @@ mono_verifier_verify_pe_data (MonoImage *image, GSList **error_list)
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_PE;
 
        verify_msdos_header (&ctx);
@@ -3346,7 +3911,7 @@ mono_verifier_verify_cli_data (MonoImage *image, GSList **error_list)
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_CLI;
 
        verify_cli_header (&ctx);
@@ -3379,7 +3944,7 @@ mono_verifier_verify_table_data (MonoImage *image, GSList **error_list)
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
 
        verify_tables_data (&ctx);
@@ -3399,7 +3964,7 @@ mono_verifier_verify_full_table_data (MonoImage *image, GSList **error_list)
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
 
        verify_typedef_table_full (&ctx);
@@ -3423,6 +3988,8 @@ mono_verifier_verify_full_table_data (MonoImage *image, GSList **error_list)
        verify_typespec_table_full (&ctx);
        CHECK_STATE ();
        verify_method_spec_table_full (&ctx);
+       CHECK_STATE ();
+       verify_tables_data_global_constraints_full (&ctx);
 
 cleanup:
        return cleanup_context (&ctx, error_list);
@@ -3436,7 +4003,7 @@ mono_verifier_verify_field_signature (MonoImage *image, guint32 offset, GSList *
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
 
        is_valid_field_signature (&ctx, offset);
@@ -3447,44 +4014,68 @@ gboolean
 mono_verifier_verify_method_header (MonoImage *image, guint32 offset, GSList **error_list)
 {
        VerifyContext ctx;
+       guint32 locals_token;
 
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
 
-       is_valid_method_header (&ctx, offset);
+       is_valid_method_header (&ctx, offset, &locals_token);
+       if (locals_token) {
+               guint32 sig_offset = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_STANDALONESIG], locals_token - 1, MONO_STAND_ALONE_SIGNATURE);
+               is_valid_standalonesig_blob (&ctx, sig_offset);
+       }
+
        return cleanup_context (&ctx, error_list);
 }
 
 gboolean
-mono_verifier_verify_method_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_method_signature (MonoImage *image, guint32 offset, MonoError *error)
 {
        VerifyContext ctx;
 
+       mono_error_init (error);
+
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, TRUE);
        ctx.stage = STAGE_TABLES;
 
        is_valid_method_signature (&ctx, offset);
+       /*XXX This returns a bad image exception, it might be the case that the right exception is method load.*/
+       return cleanup_context_checked (&ctx, error);
+}
+
+gboolean
+mono_verifier_verify_memberref_method_signature (MonoImage *image, guint32 offset, GSList **error_list)
+{
+       VerifyContext ctx;
+
+       if (!mono_verifier_is_enabled_for_image (image))
+               return TRUE;
+
+       init_verify_context (&ctx, image, error_list != NULL);
+       ctx.stage = STAGE_TABLES;
+
+       is_valid_memberref_method_signature (&ctx, offset);
        return cleanup_context (&ctx, error_list);
 }
 
 gboolean
-mono_verifier_verify_memberref_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_memberref_field_signature (MonoImage *image, guint32 offset, GSList **error_list)
 {
        VerifyContext ctx;
 
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
 
-       is_valid_method_or_field_signature (&ctx, offset);
+       is_valid_field_signature (&ctx, offset);
        return cleanup_context (&ctx, error_list);
 }
 
@@ -3496,7 +4087,7 @@ mono_verifier_verify_standalone_signature (MonoImage *image, guint32 offset, GSL
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
 
        is_valid_standalonesig_blob (&ctx, offset);
@@ -3504,15 +4095,16 @@ mono_verifier_verify_standalone_signature (MonoImage *image, guint32 offset, GSL
 }
 
 gboolean
-mono_verifier_verify_typespec_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_typespec_signature (MonoImage *image, guint32 offset, guint32 token, GSList **error_list)
 {
        VerifyContext ctx;
 
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
+       ctx.token = token;
 
        is_valid_typespec_blob (&ctx, offset);
        return cleanup_context (&ctx, error_list);
@@ -3526,13 +4118,208 @@ mono_verifier_verify_methodspec_signature (MonoImage *image, guint32 offset, GSL
        if (!mono_verifier_is_enabled_for_image (image))
                return TRUE;
 
-       init_verify_context (&ctx, image, error_list);
+       init_verify_context (&ctx, image, error_list != NULL);
+       ctx.stage = STAGE_TABLES;
+
+       is_valid_methodspec_blob (&ctx, offset);
+       return cleanup_context (&ctx, error_list);
+}
+
+static void
+verify_user_string (VerifyContext *ctx, guint32 offset)
+{
+       OffsetAndSize heap_us = get_metadata_stream (ctx, &ctx->image->heap_us);
+       guint32 entry_size, bytes;
+
+       if (heap_us.size < offset)
+               ADD_ERROR (ctx, g_strdup ("User string offset beyond heap_us size"));
+
+       if (!decode_value (ctx->data + offset + heap_us.offset, heap_us.size - heap_us.offset, &entry_size, &bytes))
+               ADD_ERROR (ctx, g_strdup ("Could not decode user string blob size"));
+
+       if (CHECK_ADD4_OVERFLOW_UN (entry_size, bytes))
+               ADD_ERROR (ctx, g_strdup ("User string size overflow"));
+
+       entry_size += bytes;
+
+       if (ADD_IS_GREATER_OR_OVF (offset, entry_size, heap_us.size))
+               ADD_ERROR (ctx, g_strdup ("User string oveflow heap_us"));
+}
+
+gboolean
+mono_verifier_verify_string_signature (MonoImage *image, guint32 offset, GSList **error_list)
+{
+       VerifyContext ctx;
+
+       if (!mono_verifier_is_enabled_for_image (image))
+               return TRUE;
+
+       init_verify_context (&ctx, image, error_list != NULL);
        ctx.stage = STAGE_TABLES;
 
-       is_valid_methodspec_blog (&ctx, offset);
+       verify_user_string (&ctx, offset);
+
        return cleanup_context (&ctx, error_list);
 }
 
+gboolean
+mono_verifier_verify_cattr_blob (MonoImage *image, guint32 offset, GSList **error_list)
+{
+       VerifyContext ctx;
+
+       if (!mono_verifier_is_enabled_for_image (image))
+               return TRUE;
+
+       init_verify_context (&ctx, image, error_list != NULL);
+       ctx.stage = STAGE_TABLES;
+
+       is_valid_cattr_blob (&ctx, offset);
+
+       return cleanup_context (&ctx, error_list);
+}
+
+gboolean
+mono_verifier_verify_cattr_content (MonoImage *image, MonoMethod *ctor, const guchar *data, guint32 size, GSList **error_list)
+{
+       VerifyContext ctx;
+
+       if (!mono_verifier_is_enabled_for_image (image))
+               return TRUE;
+
+       init_verify_context (&ctx, image, error_list != NULL);
+       ctx.stage = STAGE_TABLES;
+
+       is_valid_cattr_content (&ctx, ctor, (const char*)data, size);
+
+       return cleanup_context (&ctx, error_list);
+}
+
+gboolean
+mono_verifier_is_sig_compatible (MonoImage *image, MonoMethod *method, MonoMethodSignature *signature)
+{
+       MonoMethodSignature *original_sig;
+       if (!mono_verifier_is_enabled_for_image (image))
+               return TRUE;
+
+       original_sig = mono_method_signature (method);
+       if (original_sig->call_convention == MONO_CALL_VARARG) {
+               if (original_sig->hasthis != signature->hasthis)
+                       return FALSE;
+               if (original_sig->call_convention != signature->call_convention)
+                       return FALSE;
+               if (original_sig->explicit_this != signature->explicit_this)
+                       return FALSE;
+               if (original_sig->call_convention != signature->call_convention)
+                       return FALSE;
+               if (original_sig->pinvoke != signature->pinvoke)
+                       return FALSE;
+               if (original_sig->sentinelpos != signature->sentinelpos)
+                       return FALSE;
+       } else if (!mono_metadata_signature_equal (signature, original_sig)) {
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+gboolean
+mono_verifier_verify_typeref_row (MonoImage *image, guint32 row, MonoError *error)
+{
+       MonoTableInfo *table = &image->tables [MONO_TABLE_TYPEREF];
+       guint32 data [MONO_TYPEREF_SIZE];
+
+       mono_error_init (error);
+
+       if (!mono_verifier_is_enabled_for_image (image))
+               return TRUE;
+
+       if (row >= table->rows) {
+               mono_error_set_bad_image (error, image, "Invalid typeref row %d - table has %d rows", row, table->rows);
+               return FALSE;
+       }
+
+       mono_metadata_decode_row (table, row, data, MONO_TYPEREF_SIZE);
+       if (!is_valid_coded_index_with_image (image, RES_SCOPE_DESC, data [MONO_TYPEREF_SCOPE])) {
+               mono_error_set_bad_image (error, image, "Invalid typeref row %d coded index 0x%08x", row, data [MONO_TYPEREF_SCOPE]);
+               return FALSE;
+       }
+
+       if (!get_coded_index_token (RES_SCOPE_DESC, data [MONO_TYPEREF_SCOPE])) {
+               mono_error_set_bad_image (error, image, "The metadata verifier doesn't support null ResolutionScope tokens for typeref row %d", row);
+               return FALSE;
+       }
+
+       if (!data [MONO_TYPEREF_NAME] || !is_valid_string_full_with_image (image, data [MONO_TYPEREF_NAME], FALSE)) {
+               mono_error_set_bad_image (error, image, "Invalid typeref row %d name token 0x%08x", row, data [MONO_TYPEREF_NAME]);
+               return FALSE;
+       }
+
+       if (data [MONO_TYPEREF_NAMESPACE] && !is_valid_string_full_with_image (image, data [MONO_TYPEREF_NAMESPACE], FALSE)) {
+               mono_error_set_bad_image (error, image, "Invalid typeref row %d namespace token 0x%08x", row, data [MONO_TYPEREF_NAMESPACE]);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+/*Perform additional verification including metadata ones*/
+gboolean
+mono_verifier_verify_methodimpl_row (MonoImage *image, guint32 row, MonoError *error)
+{
+       MonoMethod *declaration, *body;
+       MonoMethodSignature *body_sig, *decl_sig;
+       MonoTableInfo *table = &image->tables [MONO_TABLE_METHODIMPL];
+       guint32 data [MONO_METHODIMPL_SIZE];
+
+       mono_error_init (error);
+
+       if (!mono_verifier_is_enabled_for_image (image))
+               return TRUE;
+
+       if (row >= table->rows) {
+               mono_error_set_bad_image (error, image, "Invalid methodimpl row %d - table has %d rows", row, table->rows);
+               return FALSE;
+       }
+
+       mono_metadata_decode_row (table, row, data, MONO_METHODIMPL_SIZE);
+
+       body = method_from_method_def_or_ref (image, data [MONO_METHODIMPL_BODY], NULL);
+       if (!body || mono_loader_get_last_error ()) {
+               mono_loader_clear_error ();
+               mono_error_set_bad_image (error, image, "Invalid methodimpl body for row %x", row);
+               return FALSE;
+       }
+
+       declaration = method_from_method_def_or_ref (image, data [MONO_METHODIMPL_DECLARATION], NULL);
+       if (!declaration || mono_loader_get_last_error ()) {
+               mono_loader_clear_error ();
+               mono_error_set_bad_image (error, image, "Invalid methodimpl declaration for row %x", row);
+               return FALSE;
+       }
+
+       /* FIXME
+       mono_class_setup_supertypes (class);
+       if (!mono_class_has_parent (class, body->klass)) {
+               mono_error_set_bad_image (error, image, "Invalid methodimpl body doesn't belong to parent for row %x", row);
+               return FALSE;
+       }*/
+
+       if (!(body_sig = mono_method_signature_checked (body, error))) {
+               return FALSE;
+       }
+
+       if (!(decl_sig = mono_method_signature_checked (declaration, error))) {
+               return FALSE;
+       }
+
+       if (!mono_verifier_is_signature_compatible (decl_sig, body_sig)) {
+               mono_error_set_bad_image (error, image, "Invalid methodimpl body signature not compatible with declaration row %x", row);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
 #else
 gboolean
 mono_verifier_verify_table_data (MonoImage *image, GSList **error_list)
@@ -3571,31 +4358,77 @@ mono_verifier_verify_method_header (MonoImage *image, guint32 offset, GSList **e
 }
 
 gboolean
-mono_verifier_verify_method_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_method_signature (MonoImage *image, guint32 offset, MonoError *error)
 {
+       mono_error_init (error);
        return TRUE;
 }
 
 gboolean
-mono_verifier_verify_memberref_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_standalone_signature (MonoImage *image, guint32 offset, GSList **error_list)
 {
        return TRUE;
 }
 
 gboolean
-mono_verifier_verify_standalone_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_typespec_signature (MonoImage *image, guint32 offset, guint32 token, GSList **error_list)
 {
        return TRUE;
 }
 
 gboolean
-mono_verifier_verify_typespec_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_methodspec_signature (MonoImage *image, guint32 offset, GSList **error_list)
 {
        return TRUE;
 }
 
 gboolean
-mono_verifier_verify_methodspec_signature (MonoImage *image, guint32 offset, GSList **error_list)
+mono_verifier_verify_string_signature (MonoImage *image, guint32 offset, GSList **error_list)
+{
+       return TRUE;
+}
+
+gboolean
+mono_verifier_verify_cattr_blob (MonoImage *image, guint32 offset, GSList **error_list)
+{
+       return TRUE;
+}
+
+gboolean
+mono_verifier_verify_cattr_content (MonoImage *image, MonoMethod *ctor, const guchar *data, guint32 size, GSList **error_list)
+{
+       return TRUE;
+}
+
+gboolean
+mono_verifier_is_sig_compatible (MonoImage *image, MonoMethod *method, MonoMethodSignature *signature)
+{
+       return TRUE;
+}
+
+
+gboolean
+mono_verifier_verify_typeref_row (MonoImage *image, guint32 row, MonoError *error)
+{
+       mono_error_init (error);
+       return TRUE;
+}
+
+gboolean
+mono_verifier_verify_methodimpl_row (MonoImage *image, guint32 row, MonoError *error)
+{
+       mono_error_init (error);
+       return TRUE;
+}
+
+gboolean
+mono_verifier_verify_memberref_method_signature (MonoImage *image, guint32 offset, GSList **error_list)
+{
+       return TRUE;
+}
+
+gboolean
+mono_verifier_verify_memberref_field_signature (MonoImage *image, guint32 offset, GSList **error_list)
 {
        return TRUE;
 }