+++ /dev/null
-/**
- * \file
- * Metadata verfication support
- *
- * Author:
- * Mono Project (http://www.mono-project.com)
- *
- * Copyright (C) 2005-2008 Novell, Inc. (http://www.novell.com)
- * Licensed under the MIT license. See LICENSE file in the project root for full license information.
- */
-#include <config.h>
-#include <mono/metadata/exception-internals.h>
-#include <mono/metadata/object-internals.h>
-#include <mono/metadata/verify.h>
-#include <mono/metadata/verify-internals.h>
-#include <mono/metadata/opcodes.h>
-#include <mono/metadata/tabledefs.h>
-#include <mono/metadata/reflection.h>
-#include <mono/metadata/reflection-internals.h>
-#include <mono/metadata/debug-helpers.h>
-#include <mono/metadata/mono-endian.h>
-#include <mono/metadata/metadata.h>
-#include <mono/metadata/metadata-internals.h>
-#include <mono/metadata/class-internals.h>
-#include <mono/metadata/class-init.h>
-#include <mono/metadata/tokentype.h>
-#include <mono/metadata/security-manager.h>
-#include <mono/metadata/security-core-clr.h>
-#include <mono/metadata/cil-coff.h>
-#include <mono/metadata/attrdefs.h>
-#include <mono/utils/strenc.h>
-#include <mono/utils/mono-error-internals.h>
-#include <mono/utils/bsearch.h>
-#include <string.h>
-#include <ctype.h>
-
-#ifndef DISABLE_VERIFIER
-/*
- TODO add fail fast mode
- TODO verify the entry point RVA and content.
- TODO add section relocation support
- TODO verify the relocation table, since we really don't use, no need so far.
- TODO do full PECOFF resources verification
- TODO verify in the CLI header entry point and resources
- TODO implement null token typeref validation
- TODO verify table wide invariants for typedef (sorting and uniqueness)
- TODO implement proper authenticode data directory validation
- TODO verify properties that require multiple tables to be valid
- FIXME use subtraction based bounds checking to avoid overflows
- FIXME get rid of metadata_streams and other fields from VerifyContext
-*/
-
-#ifdef MONO_VERIFIER_DEBUG
-#define VERIFIER_DEBUG(code) do { code; } while (0)
-#else
-#define VERIFIER_DEBUG(code)
-#endif
-
-#define INVALID_OFFSET ((guint32)-1)
-#define INVALID_ADDRESS 0xffffffff
-
-enum {
- STAGE_PE,
- STAGE_CLI,
- STAGE_TABLES
-};
-
-enum {
- IMPORT_TABLE_IDX = 1,
- RESOURCE_TABLE_IDX = 2,
- CERTIFICATE_TABLE_IDX = 4,
- RELOCATION_TABLE_IDX = 5,
- IAT_IDX = 12,
- CLI_HEADER_IDX = 14,
-};
-
-enum {
- STRINGS_STREAM,
- USER_STRINGS_STREAM,
- BLOB_STREAM,
- GUID_STREAM,
- TILDE_STREAM
-};
-
-
-#define INVALID_TABLE (0xFF)
-/*format: number of bits, number of tables, tables{n. tables} */
-const static unsigned char coded_index_desc[] = {
-#define TYPEDEF_OR_REF_DESC (0)
- 2, /*bits*/
- 3, /*tables*/
- MONO_TABLE_TYPEDEF,
- MONO_TABLE_TYPEREF,
- MONO_TABLE_TYPESPEC,
-
-#define HAS_CONSTANT_DESC (TYPEDEF_OR_REF_DESC + 5)
- 2, /*bits*/
- 3, /*tables*/
- MONO_TABLE_FIELD,
- MONO_TABLE_PARAM,
- MONO_TABLE_PROPERTY,
-
-#define HAS_CATTR_DESC (HAS_CONSTANT_DESC + 5)
- 5, /*bits*/
- 20, /*tables*/
- MONO_TABLE_METHOD,
- MONO_TABLE_FIELD,
- MONO_TABLE_TYPEREF,
- MONO_TABLE_TYPEDEF,
- MONO_TABLE_PARAM,
- MONO_TABLE_INTERFACEIMPL,
- MONO_TABLE_MEMBERREF,
- MONO_TABLE_MODULE,
- MONO_TABLE_DECLSECURITY,
- MONO_TABLE_PROPERTY,
- MONO_TABLE_EVENT,
- MONO_TABLE_STANDALONESIG,
- MONO_TABLE_MODULEREF,
- MONO_TABLE_TYPESPEC,
- MONO_TABLE_ASSEMBLY,
- MONO_TABLE_ASSEMBLYREF,
- MONO_TABLE_FILE,
- MONO_TABLE_EXPORTEDTYPE,
- MONO_TABLE_MANIFESTRESOURCE,
- MONO_TABLE_GENERICPARAM,
-
-#define HAS_FIELD_MARSHAL_DESC (HAS_CATTR_DESC + 22)
- 1, /*bits*/
- 2, /*tables*/
- MONO_TABLE_FIELD,
- MONO_TABLE_PARAM,
-
-#define HAS_DECL_SECURITY_DESC (HAS_FIELD_MARSHAL_DESC + 4)
- 2, /*bits*/
- 3, /*tables*/
- MONO_TABLE_TYPEDEF,
- MONO_TABLE_METHOD,
- MONO_TABLE_ASSEMBLY,
-
-#define MEMBERREF_PARENT_DESC (HAS_DECL_SECURITY_DESC + 5)
- 3, /*bits*/
- 5, /*tables*/
- MONO_TABLE_TYPEDEF,
- MONO_TABLE_TYPEREF,
- MONO_TABLE_MODULEREF,
- MONO_TABLE_METHOD,
- MONO_TABLE_TYPESPEC,
-
-#define HAS_SEMANTICS_DESC (MEMBERREF_PARENT_DESC + 7)
- 1, /*bits*/
- 2, /*tables*/
- MONO_TABLE_EVENT,
- MONO_TABLE_PROPERTY,
-
-#define METHODDEF_OR_REF_DESC (HAS_SEMANTICS_DESC + 4)
- 1, /*bits*/
- 2, /*tables*/
- MONO_TABLE_METHOD,
- MONO_TABLE_MEMBERREF,
-
-#define MEMBER_FORWARDED_DESC (METHODDEF_OR_REF_DESC + 4)
- 1, /*bits*/
- 2, /*tables*/
- MONO_TABLE_FIELD,
- MONO_TABLE_METHOD,
-
-#define IMPLEMENTATION_DESC (MEMBER_FORWARDED_DESC + 4)
- 2, /*bits*/
- 3, /*tables*/
- MONO_TABLE_FILE,
- MONO_TABLE_ASSEMBLYREF,
- MONO_TABLE_EXPORTEDTYPE,
-
-#define CATTR_TYPE_DESC (IMPLEMENTATION_DESC + 5)
- 3, /*bits*/
- 5, /*tables*/
- INVALID_TABLE,
- INVALID_TABLE,
- MONO_TABLE_METHOD,
- MONO_TABLE_MEMBERREF,
- INVALID_TABLE,
-
-#define RES_SCOPE_DESC (CATTR_TYPE_DESC + 7)
- 2, /*bits*/
- 4, /*tables*/
- MONO_TABLE_MODULE,
- MONO_TABLE_MODULEREF,
- MONO_TABLE_ASSEMBLYREF,
- MONO_TABLE_TYPEREF,
-
-#define TYPE_OR_METHODDEF_DESC (RES_SCOPE_DESC + 6)
- 1, /*bits*/
- 2, /*tables*/
- MONO_TABLE_TYPEDEF,
- MONO_TABLE_METHOD
-};
-
-typedef struct {
- guint32 rva;
- guint32 size;
- guint32 translated_offset;
-} DataDirectory;
-
-typedef struct {
- guint32 offset;
- guint32 size;
-} OffsetAndSize;
-
-typedef struct {
- guint32 baseRVA;
- guint32 baseOffset;
- guint32 size;
- guint32 rellocationsRVA;
- guint16 numberOfRelocations;
-} SectionHeader;
-
-typedef struct {
- guint32 row_count;
- guint32 row_size;
- guint32 offset;
-} TableInfo;
-
-typedef struct {
- const char *data;
- guint32 size, token;
- GSList *errors;
- int valid;
- MonoImage *image;
- gboolean report_error;
- gboolean report_warning;
- int stage;
-
- // Mono really only requires 15 here, but verifies the extra is zeroed.
- DataDirectory data_directories [16];
- guint32 section_count;
- SectionHeader *sections;
- guint pe64; // short name for PE32+; actual PE64 proposal was rejected
-
- OffsetAndSize metadata_streams [5]; //offset from begin of the image
-} VerifyContext;
-
-#define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
- do { \
- MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
- vinfo->info.status = __status; \
- vinfo->info.message = ( __msg); \
- vinfo->exception_type = (__exception); \
- (__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 { \
- if ((__ctx)->report_error) \
- ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
- (__ctx)->valid = 0; \
- return; \
- } while (0)
-
-#define FAIL(__ctx, __msg) \
- do { \
- if ((__ctx)->report_error) \
- ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
- (__ctx)->valid = 0; \
- return FALSE; \
- } while (0)
-
-#define CHECK_STATE() do { if (!ctx.valid) goto cleanup; } while (0)
-
-#define CHECK_ERROR() do { if (!ctx->valid) return; } while (0)
-
-#define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
-#define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
-
-#if SIZEOF_VOID_P == 4
-#define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
-#else
-#define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
-#endif
-
-#define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
-#define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
-
-static const char *
-dword_align (const char *ptr)
-{
-#if SIZEOF_VOID_P == 8
- return (const char *) (((guint64) (ptr + 3)) & ~3);
-#else
- return (const char *) (((guint32) (ptr + 3)) & ~3);
-#endif
-}
-
-static void
-add_from_mono_error (VerifyContext *ctx, MonoError *error)
-{
- if (is_ok (error))
- return;
-
- ADD_ERROR (ctx, g_strdup (mono_error_get_message (error)));
- mono_error_cleanup (error);
-}
-
-static guint32
-pe_signature_offset (VerifyContext *ctx)
-{
- return read32 (ctx->data + 0x3c);
-}
-
-static guint32
-pe_header_offset (VerifyContext *ctx)
-{
- return read32 (ctx->data + 0x3c) + 4;
-}
-
-static gboolean
-bounds_check_virtual_address (VerifyContext *ctx, guint32 rva, guint32 size)
-{
- int i;
-
- if (rva + size < rva) //overflow
- return FALSE;
-
- if (ctx->stage > STAGE_PE) {
- MonoCLIImageInfo *iinfo = ctx->image->image_info;
- const int top = iinfo->cli_section_count;
- MonoSectionTable *tables = iinfo->cli_section_tables;
- int i;
-
- for (i = 0; i < top; i++) {
- guint32 base = tables->st_virtual_address;
- guint32 end = base + tables->st_raw_data_size;
-
- if (rva >= base && rva + size <= end)
- return TRUE;
-
- /*if ((addr >= tables->st_virtual_address) &&
- (addr < tables->st_virtual_address + tables->st_raw_data_size)){
-
- return addr - tables->st_virtual_address + tables->st_raw_data_ptr;
- }*/
- tables++;
- }
- return FALSE;
- }
-
- if (!ctx->sections)
- return FALSE;
-
- for (i = 0; i < ctx->section_count; ++i) {
- guint32 base = ctx->sections [i].baseRVA;
- guint32 end = ctx->sections [i].baseRVA + ctx->sections [i].size;
- if (rva >= base && rva + size <= end)
- return TRUE;
- }
- return FALSE;
-}
-
-static gboolean
-bounds_check_datadir (DataDirectory *dir, guint32 offset, guint32 size)
-{
- if (dir->translated_offset > offset)
- return FALSE;
- if (dir->size < size)
- return FALSE;
- return offset + size <= dir->translated_offset + dir->size;
-}
-
-static gboolean
-bounds_check_offset (OffsetAndSize *off, guint32 offset, guint32 size)
-{
- if (off->offset > offset)
- return FALSE;
-
- if (off->size < size)
- return FALSE;
-
- return offset + size <= off->offset + off->size;
-}
-
-static guint32
-translate_rva (VerifyContext *ctx, guint32 rva)
-{
- int i;
-
- if (ctx->stage > STAGE_PE)
- return mono_cli_rva_image_map (ctx->image, rva);
-
- if (!ctx->sections)
- return FALSE;
-
- for (i = 0; i < ctx->section_count; ++i) {
- guint32 base = ctx->sections [i].baseRVA;
- guint32 end = ctx->sections [i].baseRVA + ctx->sections [i].size;
- if (rva >= base && rva <= end) {
- guint32 res = (rva - base) + ctx->sections [i].baseOffset;
- /* double check */
- return res >= ctx->size ? INVALID_OFFSET : res;
- }
- }
-
- return INVALID_OFFSET;
-}
-
-static void
-verify_msdos_header (VerifyContext *ctx)
-{
- guint32 lfanew;
- if (ctx->size < 128)
- ADD_ERROR (ctx, g_strdup ("Not enough space for the MS-DOS header"));
- if (ctx->data [0] != 0x4d || ctx->data [1] != 0x5a)
- ADD_ERROR (ctx, g_strdup ("Invalid MS-DOS watermark"));
- lfanew = pe_signature_offset (ctx);
- if (lfanew > ctx->size - 4)
- ADD_ERROR (ctx, g_strdup ("MS-DOS lfanew offset points to outside of the file"));
-}
-
-static void
-verify_pe_header (VerifyContext *ctx)
-{
- guint32 offset = pe_signature_offset (ctx);
- const char *pe_header = ctx->data + offset;
- if (pe_header [0] != 'P' || pe_header [1] != 'E' ||pe_header [2] != 0 ||pe_header [3] != 0)
- ADD_ERROR (ctx, g_strdup ("Invalid PE header watermark"));
- pe_header += 4;
- offset += 4;
-
- if (offset > ctx->size - 20)
- ADD_ERROR (ctx, g_strdup ("File with truncated pe header"));
-}
-
-static void
-verify_pe_optional_header (VerifyContext *ctx)
-{
- guint32 offset = pe_header_offset (ctx);
- guint32 header_size, file_alignment;
- const char *pe_header = ctx->data + offset;
- const char *pe_optional_header = pe_header + 20;
-
- header_size = read16 (pe_header + 16);
- offset += 20;
-
- if (header_size < 2) /*must be at least 2 or we won't be able to read magic*/
- ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
-
- if (offset > ctx->size - header_size || header_size > ctx->size)
- ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
-
- const guint16 magic = read16 (pe_optional_header);
-
- if (magic == 0x20B) {
- // Some fields are the same location for PE32 and PE32+.
- // A few are offset by 4, 8, or 12, but we do not use them.
- // Others are offset by 16.
- // Some are missing.
- ctx->pe64 = 16;
- } else if (magic != 0x10b)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid optional header magic %d", magic));
-
- // Much of this is over-verification.
- //
- // File align and section align do not matter to Mono.
- //
- // Mono requires at least 15 data directories. More than that are ignored,
- // except to require zeros in the 16th.
- //
- // Mono requires at least 216 (or pe64:232) size.
-
- /* LAMESPEC MS plays around this value and ignore it during validation
- if (read32 (pe_optional_header + 28) != 0x400000)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Image base %x", read32 (pe_optional_header + 28)));*/
- if (read32 (pe_optional_header + 32) != 0x2000)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Section Aligmnent %x", read32 (pe_optional_header + 32)));
- file_alignment = read32 (pe_optional_header + 36);
- if (file_alignment != 0x200 && file_alignment != 0x1000)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid file Aligmnent %x", file_alignment));
- /* All the junk in the middle is irrelevant, specially for mono. */
-
- if (header_size != 224 + ctx->pe64)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid optional header size %d", header_size));
-
- const guint number_of_rvas_and_sizes = read32 (pe_optional_header + 92 + ctx->pe64);
-
- // Data directories beyond 15 do not matter to mono.
- if (number_of_rvas_and_sizes > 0x10)
- ADD_ERROR (ctx, g_strdup_printf ("Too many data directories %x", number_of_rvas_and_sizes));
-}
-
-static void
-load_section_table (VerifyContext *ctx)
-{
- int i;
- SectionHeader *sections;
- guint32 offset = pe_header_offset (ctx);
- const char *ptr = ctx->data + offset;
- guint16 num_sections = ctx->section_count = read16 (ptr + 2);
-
- const guint optional_header_size = read16 (ptr + 16);
- offset += optional_header_size + 20;
- ptr += optional_header_size + 20;
-
- if (num_sections * 40 > ctx->size - offset)
- ADD_ERROR (ctx, g_strdup ("Invalid PE optional header size"));
-
- sections = ctx->sections = g_new0 (SectionHeader, num_sections);
- for (i = 0; i < num_sections; ++i) {
- sections [i].size = read32 (ptr + 8);
- sections [i].baseRVA = read32 (ptr + 12);
- sections [i].baseOffset = read32 (ptr + 20);
- sections [i].rellocationsRVA = read32 (ptr + 24);
- sections [i].numberOfRelocations = read16 (ptr + 32);
- ptr += 40;
- }
-
- ptr = ctx->data + offset; /*reset it to the beggining*/
- for (i = 0; i < num_sections; ++i) {
- guint32 raw_size, flags;
- if (sections [i].baseOffset == 0)
- ADD_ERROR (ctx, g_strdup ("Metadata verifier doesn't handle sections with intialized data only"));
- if (sections [i].baseOffset >= ctx->size)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid PointerToRawData %x points beyond EOF", sections [i].baseOffset));
- if (sections [i].size > ctx->size - sections [i].baseOffset)
- ADD_ERROR (ctx, g_strdup ("Invalid VirtualSize points beyond EOF"));
-
- raw_size = read32 (ptr + 16);
- if (raw_size < sections [i].size)
- ADD_ERROR (ctx, g_strdup ("Metadata verifier doesn't handle sections with SizeOfRawData < VirtualSize"));
-
- if (raw_size > ctx->size - sections [i].baseOffset)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid SizeOfRawData %x points beyond EOF", raw_size));
-
- if (sections [i].rellocationsRVA || sections [i].numberOfRelocations)
- ADD_ERROR (ctx, g_strdup_printf ("Metadata verifier doesn't handle section relocation"));
-
- flags = read32 (ptr + 36);
- /*TODO 0xFE0000E0 is all flags from cil-coff.h OR'd. Make it a less magical number*/
- if (flags == 0 || (flags & ~0xFE0000E0) != 0)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid section flags %x", flags));
-
- ptr += 40;
- }
-}
-
-static gboolean
-is_valid_data_directory (int i)
-{
- /*LAMESPEC 4 == certificate 6 == debug, MS uses both*/
- return i == 1 || i == 2 || i == 5 || i == 12 || i == 14 || i == 4 || i == 6;
-}
-
-static void
-load_data_directories (VerifyContext *ctx)
-{
- guint32 offset = pe_header_offset (ctx) + 116 + ctx->pe64;
- const char *ptr = ctx->data + offset;
- int i;
-
- for (i = 0; i < 16; ++i, ptr += 8) {
- guint32 rva = read32 (ptr);
- guint32 size = read32 (ptr + 4);
-
- /*LAMESPEC the authenticode data directory format is different. We don't support CAS, so lets ignore for now.*/
- if (i == CERTIFICATE_TABLE_IDX)
- continue;
-
- if ((rva != 0 || size != 0) && !is_valid_data_directory (i))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid data directory %d", i));
-
- if (rva != 0 && !bounds_check_virtual_address (ctx, rva, size))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid data directory %d rva/size pair %x/%x", i, rva, size));
-
- ctx->data_directories [i].rva = rva;
- ctx->data_directories [i].size = size;
- ctx->data_directories [i].translated_offset = translate_rva (ctx, rva);
- }
-}
-
-#define SIZE_OF_MSCOREE (sizeof ("mscoree.dll"))
-
-#define SIZE_OF_CORMAIN (sizeof ("_CorExeMain"))
-
-static void
-verify_hint_name_table (VerifyContext *ctx, guint32 import_rva, const char *table_name)
-{
- const char *ptr;
- guint32 hint_table_rva;
-
- import_rva = translate_rva (ctx, import_rva);
- g_assert (import_rva != INVALID_OFFSET);
-
- hint_table_rva = read32 (ctx->data + import_rva);
- if (!bounds_check_virtual_address (ctx, hint_table_rva, SIZE_OF_CORMAIN + 2))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Hint/Name rva %d for %s", hint_table_rva, table_name));
-
- hint_table_rva = translate_rva (ctx, hint_table_rva);
- g_assert (hint_table_rva != INVALID_OFFSET);
- ptr = ctx->data + hint_table_rva + 2;
-
- if (memcmp ("_CorExeMain", ptr, SIZE_OF_CORMAIN) && memcmp ("_CorDllMain", ptr, SIZE_OF_CORMAIN))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Hint / Name: '%s'", ptr));
-}
-
-static void
-verify_import_table (VerifyContext *ctx)
-{
- DataDirectory it = ctx->data_directories [IMPORT_TABLE_IDX];
- guint32 offset = it.translated_offset;
- const char *ptr = ctx->data + offset;
- guint32 name_rva, ilt_rva, iat_rva;
-
- g_assert (offset != INVALID_OFFSET);
-
- if (it.size < 40)
- ADD_ERROR (ctx, g_strdup_printf ("Import table size %d is smaller than 40", it.size));
-
- ilt_rva = read32 (ptr);
- if (ilt_rva && !bounds_check_virtual_address (ctx, ilt_rva, 8))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Lookup Table rva %x", ilt_rva));
-
- name_rva = read32 (ptr + 12);
- if (name_rva && !bounds_check_virtual_address (ctx, name_rva, SIZE_OF_MSCOREE))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Table Name rva %x", name_rva));
-
- iat_rva = read32 (ptr + 16);
- if (iat_rva) {
- if (!bounds_check_virtual_address (ctx, iat_rva, 8))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Address Table rva %x", iat_rva));
-
- if (iat_rva != ctx->data_directories [IAT_IDX].rva)
- ADD_ERROR (ctx, g_strdup_printf ("Import Address Table rva %x different from data directory entry %x", read32 (ptr + 16), ctx->data_directories [IAT_IDX].rva));
- }
-
- if (name_rva) {
- name_rva = translate_rva (ctx, name_rva);
- g_assert (name_rva != INVALID_OFFSET);
- ptr = ctx->data + name_rva;
-
- if (memcmp ("mscoree.dll", ptr, SIZE_OF_MSCOREE))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Table Name: '%s'", ptr));
- }
-
- if (ilt_rva) {
- verify_hint_name_table (ctx, ilt_rva, "Import Lookup Table");
- CHECK_ERROR ();
- }
-
- if (iat_rva)
- verify_hint_name_table (ctx, iat_rva, "Import Address Table");
-}
-
-static void
-verify_resources_table (VerifyContext *ctx)
-{
- DataDirectory it = ctx->data_directories [RESOURCE_TABLE_IDX];
- guint32 offset;
- guint16 named_entries, id_entries;
- const char *ptr;
-
- if (it.rva == 0)
- return;
-
- if (it.size < 16)
- ADD_ERROR (ctx, g_strdup_printf ("Resource section is too small, must be at least 16 bytes long but it's %d long", it.size));
-
- offset = it.translated_offset;
- ptr = ctx->data + offset;
-
- g_assert (offset != INVALID_OFFSET);
-
- named_entries = read16 (ptr + 12);
- id_entries = read16 (ptr + 14);
-
- if ((named_entries + id_entries) * 8 + 16 > it.size)
- ADD_ERROR (ctx, g_strdup_printf ("Resource section is too small, the number of entries (%d) doesn't fit on it's size %d", named_entries + id_entries, it.size));
-
- /* XXX at least one unmanaged resource is added due to a call to AssemblyBuilder::DefineVersionInfoResource ()
- if (named_entries || id_entries)
- ADD_ERROR (ctx, g_strdup_printf ("The metadata verifier doesn't support full verification of PECOFF resources"));
- */
-}
-
-/*----------nothing from here on can use data_directory---*/
-
-static DataDirectory
-get_data_dir (VerifyContext *ctx, int idx)
-{
- MonoCLIImageInfo *iinfo = ctx->image->image_info;
- MonoPEDirEntry *entry= &iinfo->cli_header.datadir.pe_export_table;
- DataDirectory res;
-
- entry += idx;
- res.rva = entry->rva;
- res.size = entry->size;
- res.translated_offset = translate_rva (ctx, res.rva);
- return res;
-
-}
-static void
-verify_cli_header (VerifyContext *ctx)
-{
- DataDirectory it = get_data_dir (ctx, CLI_HEADER_IDX);
- guint32 offset;
- const char *ptr;
- int i;
-
- if (it.rva == 0)
- ADD_ERROR (ctx, g_strdup_printf ("CLI header missing"));
-
- if (it.size != 72)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid cli header size in data directory %d must be 72", it.size));
-
- offset = it.translated_offset;
- ptr = ctx->data + offset;
-
- g_assert (offset != INVALID_OFFSET);
-
- if (read16 (ptr) != 72)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid cli header size %d must be 72", read16 (ptr)));
-
- if (!bounds_check_virtual_address (ctx, read32 (ptr + 8), read32 (ptr + 12)))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid medatata section rva/size pair %x/%x", read32 (ptr + 8), read32 (ptr + 12)));
-
-
- if (!read32 (ptr + 8) || !read32 (ptr + 12))
- ADD_ERROR (ctx, g_strdup_printf ("Missing medatata section in the CLI header"));
-
- if ((read32 (ptr + 16) & ~0x0003000B) != 0)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid CLI header flags"));
-
- ptr += 24;
- for (i = 0; i < 6; ++i) {
- guint32 rva = read32 (ptr);
- guint32 size = read32 (ptr + 4);
-
- if (rva != 0 && !bounds_check_virtual_address (ctx, rva, size))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid cli section %i rva/size pair %x/%x", i, rva, size));
-
- ptr += 8;
-
- if (rva && i > 1)
- ADD_ERROR (ctx, g_strdup_printf ("Metadata verifier doesn't support cli header section %d", i));
- }
-}
-
-static guint32
-pad4 (guint32 offset)
-{
- if (offset & 0x3) //pad to the next 4 byte boundary
- offset = (offset & ~0x3) + 4;
- return offset;
-}
-
-static void
-verify_metadata_header (VerifyContext *ctx)
-{
- int i;
- DataDirectory it = get_data_dir (ctx, CLI_HEADER_IDX);
- guint32 offset, section_count;
- const char *ptr;
-
- offset = it.translated_offset;
- ptr = ctx->data + offset;
- g_assert (offset != INVALID_OFFSET);
-
- //build a directory entry for the metadata root
- ptr += 8;
- it.rva = read32 (ptr);
- ptr += 4;
- it.size = read32 (ptr);
- it.translated_offset = offset = translate_rva (ctx, it.rva);
-
- ptr = ctx->data + offset;
- g_assert (offset != INVALID_OFFSET);
-
- if (it.size < 20)
- ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small %d (at least 20 bytes required for initial decoding)", it.size));
-
- if (read32 (ptr) != 0x424A5342)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid metadata signature, expected 0x424A5342 but got %08x", read32 (ptr)));
-
- offset = pad4 (offset + 16 + read32 (ptr + 12));
-
- if (!bounds_check_datadir (&it, offset, 4))
- ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small %d (at least %d bytes required for flags decoding)", it.size, offset + 4 - it.translated_offset));
-
- ptr = ctx->data + offset; //move to streams header
-
- 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 < section_count; ++i) {
- guint32 stream_off, stream_size;
- int string_size, stream_idx;
-
- if (!bounds_check_datadir (&it, offset, 8))
- ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small for initial decode of stream header %d, missing %d bytes", i, offset + 9 - it.translated_offset));
-
- stream_off = it.translated_offset + read32 (ptr);
- stream_size = read32 (ptr + 4);
-
- if (!bounds_check_datadir (&it, stream_off, stream_size))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid stream header %d offset/size pair %x/%x", 0, stream_off, stream_size));
-
- ptr += 8;
- offset += 8;
-
- for (string_size = 0; string_size < 32; ++string_size) {
- if (!bounds_check_datadir (&it, offset++, 1))
- ADD_ERROR (ctx, g_strdup_printf ("Metadata root section is too small to decode stream header %d name", i));
- if (!ptr [string_size])
- break;
- }
-
- if (ptr [string_size])
- ADD_ERROR (ctx, g_strdup_printf ("Metadata stream header %d name larger than 32 bytes", i));
-
- if (!strncmp ("#Strings", ptr, 9))
- stream_idx = STRINGS_STREAM;
- else if (!strncmp ("#US", ptr, 4))
- stream_idx = USER_STRINGS_STREAM;
- else if (!strncmp ("#Blob", ptr, 6))
- stream_idx = BLOB_STREAM;
- else if (!strncmp ("#GUID", ptr, 6))
- stream_idx = GUID_STREAM;
- else if (!strncmp ("#~", ptr, 3))
- stream_idx = TILDE_STREAM;
- 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));
-
- ctx->metadata_streams [stream_idx].offset = stream_off;
- ctx->metadata_streams [stream_idx].size = stream_size;
-
- offset = pad4 (offset);
- ptr = ctx->data + offset;
- }
-
- if (!ctx->metadata_streams [TILDE_STREAM].size)
- 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"));
-}
-
-static void
-verify_tables_schema (VerifyContext *ctx)
-{
- OffsetAndSize tables_area = ctx->metadata_streams [TILDE_STREAM];
- unsigned offset = tables_area.offset;
- const char *ptr = ctx->data + offset;
- guint64 valid_tables;
- guint32 count;
- int i;
-
- 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));
-
- 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)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid table schemata minor version %d, expected 0", ptr [5]));
-
- if ((ptr [6] & ~0x7) != 0)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid table schemata heap sizes 0x%02x, only bits 0, 1 and 2 can be set", ((unsigned char *) ptr) [6]));
-
- valid_tables = read64 (ptr + 8);
- count = 0;
- for (i = 0; i < 64; ++i) {
- if (!(valid_tables & ((guint64)1 << i)))
- continue;
-
- /*MS Extensions: 0x3 0x5 0x7 0x13 0x16
- 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 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;
- }
-
- if (tables_area.size < 24 + count * 4)
- ADD_ERROR (ctx, g_strdup_printf ("Table schemata size (%d) too small to for decoding row counts (requires %d bytes)", tables_area.size, 24 + count * 4));
- ptr += 24;
-
- for (i = 0; i < 64; ++i) {
- if (valid_tables & ((guint64)1 << i)) {
- guint32 row_count = read32 (ptr);
- if (row_count > (1 << 24) - 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Table %d row count: %d. Mono only supports 16777215 rows", i, row_count));
- ptr += 4;
- }
- }
-}
-
-/*----------nothing from here on can use data_directory or metadata_streams ---*/
-
-static guint32
-get_col_offset (VerifyContext *ctx, int table, int column)
-{
- guint32 bitfield = ctx->image->tables [table].size_bitfield;
- guint32 offset = 0;
-
- while (column-- > 0)
- offset += mono_metadata_table_size (bitfield, column);
-
- return offset;
-}
-
-static guint32
-get_col_size (VerifyContext *ctx, int table, int column)
-{
- return mono_metadata_table_size (ctx->image->tables [table].size_bitfield, column);
-}
-
-static OffsetAndSize
-get_metadata_stream (VerifyContext *ctx, MonoStreamHeader *header)
-{
- OffsetAndSize res;
- res.offset = header->data - ctx->data;
- res.size = header->size;
-
- return res;
-}
-
-static gboolean
-is_valid_string_full_with_image (MonoImage *image, guint32 offset, gboolean allow_empty)
-{
- guint32 heap_offset = (char*)image->heap_strings.data - image->raw_data;
- guint32 heap_size = image->heap_strings.size;
-
- glong length;
- const char *data = image->raw_data + heap_offset;
-
- if (offset >= heap_size)
- return FALSE;
- if (CHECK_ADDP_OVERFLOW_UN (data, offset))
- return FALSE;
-
- 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)
-{
- return is_valid_string_full (ctx, offset, TRUE);
-}
-
-static gboolean
-is_valid_non_empty_string (VerifyContext *ctx, guint32 offset)
-{
- return is_valid_string_full (ctx, offset, FALSE);
-}
-
-static gboolean
-is_valid_guid (VerifyContext *ctx, guint32 offset)
-{
- OffsetAndSize guids = get_metadata_stream (ctx, &ctx->image->heap_guid);
- return guids.size >= 8 && guids.size - 8 >= offset;
-}
-
-static guint32
-get_coded_index_token (int token_kind, guint32 coded_token)
-{
- guint32 bits = coded_index_desc [token_kind];
- return coded_token >> bits;
-}
-
-static guint32
-get_coded_index_table (int kind, guint32 coded_token)
-{
- guint32 idx, bits = coded_index_desc [kind];
- kind += 2;
- idx = coded_token & ((1 << bits) - 1);
- return coded_index_desc [kind + idx];
-}
-
-static guint32
-make_coded_token (int kind, guint32 table, guint32 table_idx)
-{
- guint32 bits = coded_index_desc [kind++];
- guint32 tables = coded_index_desc [kind++];
- guint32 i;
- for (i = 0; i < tables; ++i) {
- if (coded_index_desc [kind++] == table)
- return ((table_idx + 1) << bits) | i;
- }
- g_assert_not_reached ();
- return -1;
-}
-
-static gboolean
-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++];
- guint32 table = coded_token & ((1 << bits) - 1);
- guint32 token = coded_token >> bits;
-
- if (table >= table_count)
- return FALSE;
-
- /*token_kind points to the first table idx*/
- table = coded_index_desc [token_kind + table];
-
- if (table == INVALID_TABLE)
- return FALSE;
- 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 {
- guint32 token;
- guint32 col_size;
- guint32 col_offset;
- MonoTableInfo *table;
-} RowLocator;
-
-static int
-token_locator (const void *a, const void *b)
-{
- RowLocator *loc = (RowLocator *)a;
- unsigned const char *row = (unsigned const char *)b;
- guint32 token = loc->col_size == 2 ? read16 (row + loc->col_offset) : read32 (row + loc->col_offset);
-
- VERIFIER_DEBUG ( printf ("\tfound token %x at idx %d\n", token, ((const char*)row - loc->table->base) / loc->table->row_size) );
- return (int)loc->token - (int)token;
-}
-
-static int
-search_sorted_table (VerifyContext *ctx, int table, int column, guint32 coded_token)
-{
- MonoTableInfo *tinfo = &ctx->image->tables [table];
- RowLocator locator;
- const char *res, *base;
- locator.token = coded_token;
- locator.col_offset = get_col_offset (ctx, table, column);
- locator.col_size = get_col_size (ctx, table, column);
- locator.table = tinfo;
-
- base = tinfo->base;
-
- VERIFIER_DEBUG ( printf ("looking token %x table %d col %d rsize %d roff %d\n", coded_token, table, column, locator.col_size, locator.col_offset) );
- res = (const char *)mono_binary_search (&locator, base, tinfo->rows, tinfo->row_size, token_locator);
- if (!res)
- return -1;
-
- return (res - base) / tinfo->row_size;
-}
-
-/*WARNING: This function doesn't verify if the strings @offset points to a valid string*/
-static const char*
-get_string_ptr (VerifyContext *ctx, guint offset)
-{
- return ctx->image->heap_strings.data + offset;
-}
-
-/*WARNING: This function doesn't verify if the strings @offset points to a valid string*/
-static int
-string_cmp (VerifyContext *ctx, const char *str, guint offset)
-{
- if (offset == 0)
- return strcmp (str, "");
-
- return strcmp (str, get_string_ptr (ctx, offset));
-}
-
-static gboolean
-mono_verifier_is_corlib (MonoImage *image)
-{
- return image->module_name && !strcmp ("mscorlib.dll", image->module_name);
-}
-
-static gboolean
-typedef_is_system_object (VerifyContext *ctx, guint32 *data)
-{
- return mono_verifier_is_corlib (ctx->image) && !string_cmp (ctx, "System", data [MONO_TYPEDEF_NAMESPACE]) && !string_cmp (ctx, "Object", data [MONO_TYPEDEF_NAME]);
-}
-
-static gboolean
-decode_value (const char *_ptr, unsigned available, unsigned *value, unsigned *size)
-{
- unsigned char b;
- const unsigned char *ptr = (const unsigned char *)_ptr;
-
- if (!available)
- return FALSE;
-
- b = *ptr;
- *value = *size = 0;
-
- if ((b & 0x80) == 0) {
- *size = 1;
- *value = b;
- } else if ((b & 0x40) == 0) {
- if (available < 2)
- return FALSE;
- *size = 2;
- *value = ((b & 0x3f) << 8 | ptr [1]);
- } else {
- if (available < 4)
- return FALSE;
- *size = 4;
- *value = ((b & 0x1f) << 24) |
- (ptr [1] << 16) |
- (ptr [2] << 8) |
- ptr [3];
- }
-
- return TRUE;
-}
-
-static gboolean
-decode_signature_header (VerifyContext *ctx, guint32 offset, guint32 *size, const char **first_byte)
-{
- MonoStreamHeader blob = ctx->image->heap_blob;
- guint32 value, enc_size;
-
- if (offset >= blob.size)
- return FALSE;
-
- if (!decode_value (blob.data + offset, blob.size - offset, &value, &enc_size))
- return FALSE;
-
- if (CHECK_ADD4_OVERFLOW_UN (offset, enc_size))
- return FALSE;
-
- offset += enc_size;
-
- if (ADD_IS_GREATER_OR_OVF (offset, value, blob.size))
- return FALSE;
-
- *size = value;
- *first_byte = blob.data + offset;
- return TRUE;
-}
-
-static gboolean
-safe_read (const char **_ptr, const char *limit, unsigned *dest, int size)
-{
- const char *ptr = *_ptr;
- if (ptr + size > limit)
- return FALSE;
- switch (size) {
- case 1:
- *dest = *((guint8*)ptr);
- ++ptr;
- break;
- case 2:
- *dest = read16 (ptr);
- ptr += 2;
- break;
- case 4:
- *dest = read32 (ptr);
- ptr += 4;
- break;
- }
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-safe_read_compressed_int (const char **_ptr, const char *limit, unsigned *dest)
-{
- unsigned size = 0;
- const char *ptr = *_ptr;
- gboolean res = decode_value (ptr, limit - ptr, dest, &size);
- *_ptr = ptr + size;
- return res;
-}
-
-#define safe_read8(VAR, PTR, LIMIT) safe_read (&PTR, LIMIT, &VAR, 1)
-#define safe_read_cint(VAR, PTR, LIMIT) safe_read_compressed_int (&PTR, LIMIT, &VAR)
-#define safe_read16(VAR, PTR, LIMIT) safe_read (&PTR, LIMIT, &VAR, 2)
-#define safe_read32(VAR, PTR, LIMIT) safe_read (&PTR, LIMIT, &VAR, 4)
-
-static gboolean
-parse_type (VerifyContext *ctx, const char **_ptr, const char *end);
-
-static gboolean
-parse_method_signature (VerifyContext *ctx, const char **_ptr, const char *end, gboolean allow_sentinel, gboolean allow_unmanaged);
-
-static gboolean
-parse_custom_mods (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- const char *ptr = *_ptr;
- unsigned type = 0;
- unsigned token = 0;
-
- while (TRUE) {
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("CustomMod: Not enough room for the type"));
-
- if (type != MONO_TYPE_CMOD_REQD && type != MONO_TYPE_CMOD_OPT) {
- --ptr;
- break;
- }
-
- 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) || !get_coded_index_token (TYPEDEF_OR_REF_DESC, token))
- FAIL (ctx, g_strdup_printf ("CustomMod: invalid TypeDefOrRef token %x", token));
- }
-
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-parse_array_shape (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- const char *ptr = *_ptr;
- unsigned val = 0;
- unsigned size, num, i;
-
- if (!safe_read8 (val, ptr, end))
- FAIL (ctx, g_strdup ("ArrayShape: Not enough room for Rank"));
-
- if (val == 0)
- FAIL (ctx, g_strdup ("ArrayShape: Invalid shape with zero Rank"));
-
- if (!safe_read_cint (size, ptr, end))
- FAIL (ctx, g_strdup ("ArrayShape: Not enough room for NumSizes"));
-
- for (i = 0; i < size; ++i) {
- if (!safe_read_cint (num, ptr, end))
- FAIL (ctx, g_strdup_printf ("ArrayShape: Not enough room for Size of rank %d", i + 1));
- }
-
- if (!safe_read_cint (size, ptr, end))
- FAIL (ctx, g_strdup ("ArrayShape: Not enough room for NumLoBounds"));
-
- for (i = 0; i < size; ++i) {
- if (!safe_read_cint (num, ptr, end))
- FAIL (ctx, g_strdup_printf ("ArrayShape: Not enough room for LoBound of rank %d", i + 1));
- }
-
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-parse_generic_inst (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- const char *ptr = *_ptr;
- unsigned type;
- unsigned count, token, i;
-
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("GenericInst: Not enough room for kind"));
-
- if (type != MONO_TYPE_CLASS && type != MONO_TYPE_VALUETYPE)
- FAIL (ctx, g_strdup_printf ("GenericInst: Invalid GenericInst kind %x\n", type));
-
- 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) || !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"));
-
- if (count == 0)
- 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));
- }
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-parse_type (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- const char *ptr = *_ptr;
- unsigned type;
- unsigned token = 0;
-
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("Type: Not enough room for the type"));
-
- if (!((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_PTR) ||
- (type >= MONO_TYPE_VALUETYPE && type <= MONO_TYPE_GENERICINST) ||
- (type >= MONO_TYPE_I && type <= MONO_TYPE_U) ||
- (type >= MONO_TYPE_FNPTR && type <= MONO_TYPE_MVAR)))
- FAIL (ctx, g_strdup_printf ("Type: Invalid type kind %x\n", type));
-
- switch (type) {
- case MONO_TYPE_PTR:
- if (!parse_custom_mods (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("Type: Failed to parse pointer custom attr"));
-
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("Type: Not enough room to parse the pointer type"));
-
- if (type != MONO_TYPE_VOID) {
- --ptr;
- if (!parse_type (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("Type: Could not parse pointer type"));
- }
- break;
-
- case MONO_TYPE_VALUETYPE:
- case MONO_TYPE_CLASS:
- 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) || !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:
- case MONO_TYPE_MVAR:
- if (!safe_read_cint (token, ptr, end))
- FAIL (ctx, g_strdup ("Type: Not enough room for to decode generic argument number"));
- break;
-
- case MONO_TYPE_ARRAY:
- if (!parse_type (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("Type: Could not parse array type"));
- if (!parse_array_shape (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("Type: Could not parse array shape"));
- break;
-
- case MONO_TYPE_GENERICINST:
- if (!parse_generic_inst (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("Type: Could not parse generic inst"));
- break;
-
- case MONO_TYPE_FNPTR:
- if (!parse_method_signature (ctx, &ptr, end, TRUE, TRUE))
- FAIL (ctx, g_strdup ("Type: Could not parse method pointer signature"));
- break;
-
- case MONO_TYPE_SZARRAY:
- if (!parse_custom_mods (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("Type: Failed to parse array element custom attr"));
- if (!parse_type (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("Type: Could not parse array type"));
- break;
- }
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-parse_return_type (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- const char *ptr;
- unsigned type = 0;
-
- if (!parse_custom_mods (ctx, _ptr, end))
- return FALSE;
-
- ptr = *_ptr;
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("ReturnType: Not enough room for the type"));
-
- if (type == MONO_TYPE_VOID || type == MONO_TYPE_TYPEDBYREF) {
- *_ptr = ptr;
- return TRUE;
- }
-
- //it's a byref, update the cursor ptr
- if (type == MONO_TYPE_BYREF)
- *_ptr = ptr;
-
- return parse_type (ctx, _ptr, end);
-}
-
-static gboolean
-parse_param (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- const char *ptr;
- unsigned type = 0;
-
- if (!parse_custom_mods (ctx, _ptr, end))
- return FALSE;
-
- ptr = *_ptr;
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("Param: Not enough room for the type"));
-
- if (type == MONO_TYPE_TYPEDBYREF) {
- *_ptr = ptr;
- return TRUE;
- }
-
- //it's a byref, update the cursor ptr
- if (type == MONO_TYPE_BYREF) {
- *_ptr = ptr;
- if (!parse_custom_mods (ctx, _ptr, end))
- return FALSE;
- }
-
- return parse_type (ctx, _ptr, end);
-}
-
-static gboolean
-parse_method_signature (VerifyContext *ctx, const char **_ptr, const char *end, gboolean allow_sentinel, gboolean allow_unmanaged)
-{
- unsigned cconv = 0;
- unsigned param_count = 0, gparam_count = 0, type = 0, i;
- const char *ptr = *_ptr;
- gboolean saw_sentinel = FALSE;
-
- if (!safe_read8 (cconv, ptr, end))
- FAIL (ctx, g_strdup ("MethodSig: Not enough room for the call conv"));
-
- if (cconv & 0x80)
- FAIL (ctx, g_strdup ("MethodSig: CallConv has 0x80 set"));
-
- if (allow_unmanaged) {
- if ((cconv & 0x0F) > MONO_CALL_VARARG)
- FAIL (ctx, g_strdup_printf ("MethodSig: CallConv is not valid, it's %x", cconv & 0x0F));
- } else if ((cconv & 0x0F) != MONO_CALL_DEFAULT && (cconv & 0x0F) != MONO_CALL_VARARG)
- FAIL (ctx, g_strdup_printf ("MethodSig: CallConv is not Default or Vararg, it's %x", cconv & 0x0F));
-
- if ((cconv & 0x10) && !safe_read_cint (gparam_count, ptr, end))
- FAIL (ctx, g_strdup ("MethodSig: Not enough room for the generic param count"));
-
- if ((cconv & 0x10) && gparam_count == 0)
- FAIL (ctx, g_strdup ("MethodSig: Signature with generics but zero arity"));
-
- if (allow_unmanaged && (cconv & 0x10))
- FAIL (ctx, g_strdup ("MethodSig: Standalone signature with generic params"));
-
- if (!safe_read_cint (param_count, ptr, end))
- FAIL (ctx, g_strdup ("MethodSig: Not enough room for the param count"));
-
- if (!parse_return_type (ctx, &ptr, end))
- FAIL (ctx, g_strdup ("MethodSig: Error parsing return type"));
-
- for (i = 0; i < param_count; ++i) {
- if (allow_sentinel) {
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup_printf ("MethodSig: Not enough room for param %d type", i));
-
- if (type == MONO_TYPE_SENTINEL) {
- if ((cconv & 0x0F) != MONO_CALL_VARARG)
- FAIL (ctx, g_strdup ("MethodSig: Found sentinel but signature is not vararg"));
-
- if (saw_sentinel)
- FAIL (ctx, g_strdup ("MethodSig: More than one sentinel type"));
-
- saw_sentinel = TRUE;
- } else {
- --ptr;
- }
- }
-
- if (!parse_param (ctx, &ptr, end))
- FAIL (ctx, g_strdup_printf ("MethodSig: Error parsing arg %d", i));
- }
-
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-parse_property_signature (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- unsigned type = 0;
- unsigned sig = 0;
- unsigned param_count = 0, i;
- const char *ptr = *_ptr;
-
- if (!safe_read8 (sig, ptr, end))
- FAIL (ctx, g_strdup ("PropertySig: Not enough room for signature"));
-
- if (sig != 0x08 && sig != 0x28)
- FAIL (ctx, g_strdup_printf ("PropertySig: Signature is not 0x28 or 0x08: %x", sig));
-
- if (!safe_read_cint (param_count, ptr, end))
- FAIL (ctx, g_strdup ("PropertySig: Not enough room for the param count"));
-
- if (!parse_custom_mods (ctx, &ptr, end))
- return FALSE;
-
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("PropertySig: Not enough room for the type"));
-
- //check if it's a byref. safe_read8 did update ptr, so we rollback if it's not a byref
- if (type != MONO_TYPE_BYREF)
- --ptr;
-
- if (!parse_type (ctx, &ptr, 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));
- }
-
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-parse_field (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- const char *ptr = *_ptr;
- unsigned signature = 0;
-
- if (!safe_read8 (signature, ptr, end))
- FAIL (ctx, g_strdup ("Field: Not enough room for field signature"));
-
- if (signature != 0x06)
- FAIL (ctx, g_strdup_printf ("Field: Invalid signature 0x%x, must be 6", signature));
-
- if (!parse_custom_mods (ctx, &ptr, end))
- return FALSE;
-
- if (safe_read8 (signature, ptr, end)) {
- if (signature != MONO_TYPE_BYREF)
- --ptr;
- }
- *_ptr = ptr;
-
- return parse_type (ctx, _ptr, end);
-}
-
-static gboolean
-parse_locals_signature (VerifyContext *ctx, const char **_ptr, const char *end)
-{
- unsigned sig = 0;
- unsigned locals_count = 0, i;
- const char *ptr = *_ptr;
-
- if (!safe_read8 (sig, ptr, end))
- FAIL (ctx, g_strdup ("LocalsSig: Not enough room for signature"));
-
- if (sig != 0x07)
- FAIL (ctx, g_strdup_printf ("LocalsSig: Signature is not 0x28 or 0x08: %x", sig));
-
- if (!safe_read_cint (locals_count, ptr, end))
- FAIL (ctx, g_strdup ("LocalsSig: Not enough room for the param count"));
-
- /* LAMEIMPL: MS sometimes generates empty local signatures and its verifier is ok with.
- if (locals_count == 0)
- FAIL (ctx, g_strdup ("LocalsSig: Signature with zero locals"));
- */
-
- for (i = 0; i < locals_count; ++i) {
- if (!safe_read8 (sig, ptr, end))
- FAIL (ctx, g_strdup ("LocalsSig: Not enough room for type"));
-
- while (sig == MONO_TYPE_CMOD_REQD || sig == MONO_TYPE_CMOD_OPT || sig == MONO_TYPE_PINNED) {
- if (sig != MONO_TYPE_PINNED && !parse_custom_mods (ctx, &ptr, end))
- FAIL (ctx, g_strdup_printf ("LocalsSig: Error parsing local %d", i));
- if (!safe_read8 (sig, ptr, end))
- FAIL (ctx, g_strdup ("LocalsSig: Not enough room for type"));
- }
-
- if (sig == MONO_TYPE_BYREF) {
- if (!safe_read8 (sig, ptr, end))
- FAIL (ctx, g_strdup_printf ("Type: Not enough room for byref type for local %d", i));
- if (sig == MONO_TYPE_TYPEDBYREF)
- FAIL (ctx, g_strdup_printf ("Type: Invalid type typedref& for local %d", i));
- }
-
- if (sig == MONO_TYPE_TYPEDBYREF)
- continue;
-
- --ptr;
-
- if (!parse_type (ctx, &ptr, end))
- FAIL (ctx, g_strdup_printf ("LocalsSig: Error parsing local %d", i));
- }
-
- *_ptr = ptr;
- return TRUE;
-}
-
-static gboolean
-is_valid_field_signature (VerifyContext *ctx, guint32 offset)
-{
- guint32 size = 0;
- unsigned signature = 0;
- const char *ptr = NULL, *end;
-
- if (!decode_signature_header (ctx, offset, &size, &ptr))
- FAIL (ctx, g_strdup ("FieldSig: Could not decode signature header"));
- end = ptr + size;
-
- if (!safe_read8 (signature, ptr, end))
- FAIL (ctx, g_strdup ("FieldSig: Not enough room for the signature"));
-
- if (signature != 6)
- FAIL (ctx, g_strdup_printf ("FieldSig: Invalid signature %x", signature));
- --ptr;
-
- return parse_field (ctx, &ptr, end);
-}
-
-static gboolean
-is_valid_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 ("MethodSig: Could not decode signature header"));
- end = ptr + size;
-
- 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)
-{
- guint32 size = 0;
- unsigned signature = 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;
-
- if (!safe_read8 (signature, ptr, end))
- FAIL (ctx, g_strdup ("MemberRefSig: Not enough room for the call conv"));
- --ptr;
-
- if (signature == 0x06)
- return parse_field (ctx, &ptr, end);
-
- return parse_method_signature (ctx, &ptr, end, TRUE, FALSE);
-}
-
-static gboolean
-is_valid_cattr_blob (VerifyContext *ctx, guint32 offset)
-{
- guint32 size = 0;
- unsigned prolog = 0;
- const char *ptr = NULL, *end;
-
- if (!offset)
- return TRUE;
-
- if (!decode_signature_header (ctx, offset, &size, &ptr))
- FAIL (ctx, g_strdup ("CustomAttribute: Could not decode signature header"));
- 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));
-
- 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_internal (type);
- return klass && m_class_is_enumtype (klass);
- }
-
- if (type->type == MONO_TYPE_CLASS)
- return mono_class_from_mono_type_internal (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"));
-
-MONO_DISABLE_WARNING (4310) // cast truncates constant value
- /*NULL string*/
- if (*ptr == (char)0xFF) {
- *_ptr = ptr + 1;
- return TRUE;
- }
-MONO_RESTORE_WARNING
-
- 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)
-{
- ERROR_DECL (error);
- MonoType *type;
- MonoClass *klass;
- const char *str_start = NULL;
- const char *ptr = *_ptr;
- char *enum_name;
- guint32 str_len = 0;
- MonoAssemblyLoadContext *alc = mono_domain_ambient_alc (mono_domain_get ());
-
- 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 = (char *)g_memdup (str_start, str_len + 1);
- enum_name [str_len] = 0;
- type = mono_reflection_type_from_name_checked (enum_name, alc, ctx->image, error);
- if (!type || !is_ok (error)) {
- ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("CustomAttribute: Invalid enum class %s, due to %s", enum_name, mono_error_get_message (error)));
- g_free (enum_name);
- mono_error_cleanup (error);
- return NULL;
- }
- g_free (enum_name);
-
- klass = mono_class_from_mono_type_internal (type);
- if (!klass || !m_class_is_enumtype (klass)) {
- ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("CustomAttribute:Class %s::%s is not an enum", m_class_get_name_space (klass), m_class_get_name (klass)));
- 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 = m_class_get_element_class (klass);
- type = m_class_get_byval_arg (klass)->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 : (MonoTypeEnum)etype;
- klass = mono_class_from_mono_type_internal (&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 && m_class_is_enumtype (klass)) {
- klass = m_class_get_element_class (klass);
- type = m_class_get_byval_arg (klass)->type;
- goto handle_enum;
- }
-
- if (klass != mono_defaults.systemtype_class)
- FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid class parameter type %s:%s ",m_class_get_name_space (klass), m_class_get_name (klass)));
- *_ptr = ptr;
- return is_valid_ser_string (ctx, _ptr, end);
-
- case MONO_TYPE_VALUETYPE:
- if (!klass || !m_class_is_enumtype (klass))
- FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid valuetype parameter expected enum %s:%s ",m_class_get_name_space (klass), m_class_get_name (klass)));
-
- klass = m_class_get_element_class (klass);
- type = m_class_get_byval_arg (klass)->type;
- goto handle_enum;
-
- case MONO_TYPE_SZARRAY:
- mono_type = m_class_get_byval_arg (klass);
- if (!is_valid_cattr_type (mono_type))
- FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid array element type %s:%s ",m_class_get_name_space (klass), m_class_get_name (klass)));
- if (!safe_read32 (element_count, ptr, end))
- FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid class parameter type %s:%s ",m_class_get_name_space (klass), m_class_get_name (klass)));
- 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)
-{
- ERROR_DECL (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 (!is_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 = (MonoTypeEnum)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 = m_class_get_byval_arg (klass);
- } else if (kind == 0x50) {
- type = m_class_get_byval_arg (mono_defaults.systemtype_class);
- } else if (kind == 0x51) {
- type = mono_get_object_type ();
- } 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 : (MonoTypeEnum)etype;
- klass = mono_class_from_mono_type_internal (&simple_type);
- } else
- FAIL (ctx, g_strdup_printf ("CustomAttribute: Invalid array element type %x", etype));
-
- type = m_class_get_byval_arg (mono_class_create_array (klass, 1));
- } 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;
-}
-
-static gboolean
-is_valid_marshal_spec (VerifyContext *ctx, guint32 offset)
-{
- OffsetAndSize blob = get_metadata_stream (ctx, &ctx->image->heap_blob);
- //TODO do proper verification
- return blob.size >= 1 && blob.size - 1 >= offset;
-}
-
-static gboolean
-is_valid_permission_set (VerifyContext *ctx, guint32 offset)
-{
- OffsetAndSize blob = get_metadata_stream (ctx, &ctx->image->heap_blob);
- //TODO do proper verification
- return blob.size >= 1 && blob.size - 1 >= offset;
-}
-
-static gboolean
-is_valid_standalonesig_blob (VerifyContext *ctx, guint32 offset)
-{
- guint32 size = 0;
- unsigned signature = 0;
- const char *ptr = NULL, *end;
-
- if (!decode_signature_header (ctx, offset, &size, &ptr))
- FAIL (ctx, g_strdup ("StandAloneSig: Could not decode signature header"));
- end = ptr + size;
-
- if (!safe_read8 (signature, ptr, end))
- FAIL (ctx, g_strdup ("StandAloneSig: Not enough room for the call conv"));
-
- --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)
-{
- guint32 size = 0;
- const char *ptr = NULL, *end;
-
- if (!decode_signature_header (ctx, offset, &size, &ptr))
- FAIL (ctx, g_strdup ("PropertySig: Could not decode signature header"));
- end = ptr + size;
-
- return parse_property_signature (ctx, &ptr, end);
-}
-
-static gboolean
-is_valid_typespec_blob (VerifyContext *ctx, guint32 offset)
-{
- 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;
-
- if (!parse_custom_mods (ctx, &ptr, end))
- return FALSE;
-
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("TypeSpec: Not enough room for type"));
-
- if (type == MONO_TYPE_BYREF) {
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("TypeSpec: Not enough room for byref type"));
- if (type == MONO_TYPE_TYPEDBYREF)
- FAIL (ctx, g_strdup ("TypeSpec: Invalid type typedref&"));
- }
-
- if (type == MONO_TYPE_TYPEDBYREF)
- return TRUE;
-
- --ptr;
- return parse_type (ctx, &ptr, end);
-}
-
-static gboolean
-is_valid_methodspec_blob (VerifyContext *ctx, guint32 offset)
-{
- guint32 size = 0;
- const char *ptr = NULL, *end;
- unsigned type = 0;
- unsigned count = 0, i;
-
- if (!decode_signature_header (ctx, offset, &size, &ptr))
- FAIL (ctx, g_strdup ("MethodSpec: Could not decode signature header"));
- end = ptr + size;
-
- if (!safe_read8 (type, ptr, end))
- FAIL (ctx, g_strdup ("MethodSpec: Not enough room for call convention"));
-
- if (type != 0x0A)
- FAIL (ctx, g_strdup_printf ("MethodSpec: Invalid call convention 0x%x, expected 0x0A", type));
-
- if (!safe_read_cint (count, ptr, end))
- FAIL (ctx, g_strdup ("MethodSpec: Not enough room for parameter count"));
-
- if (!count)
- 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));
- }
- return TRUE;
-}
-
-static gboolean
-is_valid_blob_object (VerifyContext *ctx, guint32 offset, guint32 minsize)
-{
- OffsetAndSize blob = get_metadata_stream (ctx, &ctx->image->heap_blob);
- guint32 entry_size, bytes;
-
- if (blob.size < offset)
- return FALSE;
-
- if (!decode_value (ctx->data + offset + blob.offset, blob.size - blob.offset, &entry_size, &bytes))
- return FALSE;
-
- if (entry_size < minsize)
- return FALSE;
-
- if (CHECK_ADD4_OVERFLOW_UN (entry_size, bytes))
- return FALSE;
- entry_size += bytes;
-
- return !ADD_IS_GREATER_OR_OVF (offset, entry_size, blob.size);
-}
-
-static gboolean
-is_valid_constant (VerifyContext *ctx, guint32 type, guint32 offset)
-{
- OffsetAndSize blob = get_metadata_stream (ctx, &ctx->image->heap_blob);
- guint32 size, entry_size, bytes;
-
- if (blob.size < offset)
- FAIL (ctx, g_strdup ("ContantValue: invalid offset"));
-
- if (!decode_value (ctx->data + offset + blob.offset, blob.size - blob.offset, &entry_size, &bytes))
- FAIL (ctx, g_strdup ("ContantValue: not enough space to decode size"));
-
- if (type == MONO_TYPE_STRING) {
- //String is encoded as: compressed_int:len len *bytes
- offset += bytes;
-
- if (ADD_IS_GREATER_OR_OVF (offset, entry_size, blob.size))
- FAIL (ctx, g_strdup_printf ("ContantValue: not enough space for string, required %d but got %d", entry_size * 2, blob.size - offset));
-
- return TRUE;
- }
-
- switch (type) {
- case MONO_TYPE_BOOLEAN:
- case MONO_TYPE_I1:
- case MONO_TYPE_U1:
- size = 1;
- break;
- case MONO_TYPE_CHAR:
- case MONO_TYPE_I2:
- case MONO_TYPE_U2:
- size = 2;
- break;
- case MONO_TYPE_I4:
- case MONO_TYPE_U4:
- case MONO_TYPE_R4:
- case MONO_TYPE_CLASS:
- size = 4;
- break;
-
- case MONO_TYPE_I8:
- case MONO_TYPE_U8:
- case MONO_TYPE_R8:
- size = 8;
- break;
- default:
- g_assert_not_reached ();
- }
-
- if (size != entry_size)
- FAIL (ctx, g_strdup_printf ("ContantValue: Expected size %d but got %d", size, entry_size));
-
- offset += bytes;
-
- if (ADD_IS_GREATER_OR_OVF (offset, size, blob.size))
- FAIL (ctx, g_strdup_printf ("ContantValue: Not enough room for constant, required %d but have %d", size, blob.size - offset));
-
- if (type == MONO_TYPE_CLASS && read32 (ctx->data + blob.offset + offset))
- FAIL (ctx, g_strdup_printf ("ContantValue: Type is class but value is not null"));
- return TRUE;
-}
-
-#define FAT_HEADER_INVALID_FLAGS ~(0x3 | 0x8 | 0x10 | 0xF000)
-//only 0x01, 0x40 and 0x80 are allowed
-#define SECTION_HEADER_INVALID_FLAGS 0x3E
-
-static gboolean
-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"));
-
- ptr = ctx->data + offset;
- end = ctx->data + ctx->size; /*no worries if it spawns multiple sections*/
-
- if (!safe_read8 (header, ptr, end))
- FAIL (ctx, g_strdup ("MethodHeader: Not enough room for header"));
-
- switch (header & 0x3) {
- case 0:
- case 1:
- FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid header type 0x%x", header & 0x3));
- case 2:
- header >>= 2;
- if (ADDP_IS_GREATER_OR_OVF (ptr, header, end))
- FAIL (ctx, g_strdup_printf ("MethodHeader: Not enough room for method body. Required %d, but only %d is available", header, (int)(end - ptr)));
- return TRUE;
- }
- //FAT HEADER
- --ptr;
- if (!safe_read16 (fat_header, ptr, end))
- FAIL (ctx, g_strdup ("MethodHeader: Not enough room for fat header"));
-
- size = (fat_header >> 12) & 0xF;
- if (size != 3)
- FAIL (ctx, g_strdup ("MethodHeader: header size must be 3"));
-
- if (!safe_read16 (max_stack, ptr, end))
- FAIL (ctx, g_strdup ("MethodHeader: Not enough room for max stack"));
-
- if (!safe_read32 (code_size, ptr, end))
- FAIL (ctx, g_strdup ("MethodHeader: Not enough room for code size"));
-
- if (!safe_read32 (local_vars_tok, ptr, end))
- FAIL (ctx, g_strdup ("MethodHeader: Not enough room for local vars tok"));
-
- if (local_vars_tok) {
- if (((local_vars_tok >> 24) & 0xFF) != 0x11)
- 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)
- FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid fat signature flags %x", fat_header & FAT_HEADER_INVALID_FLAGS));
-
- if (ADDP_IS_GREATER_OR_OVF (ptr, code_size, end))
- FAIL (ctx, g_strdup_printf ("MethodHeader: Not enough room for code %d", code_size));
-
- if (!(fat_header & 0x08))
- return TRUE;
-
- ptr += code_size;
-
- do {
- unsigned section_header = 0, section_size = 0;
- gboolean is_fat;
-
- ptr = dword_align (ptr);
- if (!safe_read32 (section_header, ptr, end))
- FAIL (ctx, g_strdup ("MethodHeader: Not enough room for data section header"));
-
- if (section_header & SECTION_HEADER_INVALID_FLAGS)
- FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid section header flags 0x%x", section_header & SECTION_HEADER_INVALID_FLAGS));
-
- is_fat = (section_header & METHOD_HEADER_SECTION_FAT_FORMAT) != 0;
- section_size = (section_header >> 8) & (is_fat ? 0xFFFFFF : 0xFF);
-
- if (section_size < 4)
- FAIL (ctx, g_strdup_printf ("MethodHeader: Section size too small"));
-
- if (ADDP_IS_GREATER_OR_OVF (ptr, section_size - 4, end)) /*must be section_size -4 as ptr was incremented by safe_read32*/
- 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 / (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) {
- unsigned flags = *(unsigned char*)ptr;
- unsigned class_token = 0;
- ptr += (is_fat ? 20 : 8);
- if (!safe_read32 (class_token, ptr, end))
- FAIL (ctx, g_strdup_printf ("MethodHeader: Not enough room for section %d", i));
- if (flags == MONO_EXCEPTION_CLAUSE_NONE && class_token) {
- guint table = mono_metadata_token_table (class_token);
- if (table != MONO_TABLE_TYPEREF && table != MONO_TABLE_TYPEDEF && table != MONO_TABLE_TYPESPEC)
- FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid section %d class token table %x", i, table));
- if (mono_metadata_token_index (class_token) > ctx->image->tables [table].rows)
- FAIL (ctx, g_strdup_printf ("MethodHeader: Invalid section %d class token index %x", i, mono_metadata_token_index (class_token)));
- }
- }
- }
-
- if (!(section_header & METHOD_HEADER_SECTION_MORE_SECTS))
- break;
- } while (1);
- return TRUE;
-}
-
-static void
-verify_module_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_MODULE];
- guint32 data [MONO_MODULE_SIZE];
-
- if (table->rows != 1)
- ADD_ERROR (ctx, g_strdup_printf ("Module table must have exactly one row, but have %d", table->rows));
-
- mono_metadata_decode_row (table, 0, data, MONO_MODULE_SIZE);
-
- if (!is_valid_non_empty_string (ctx, data [MONO_MODULE_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Module has an invalid name, string index 0x%08x", data [MONO_MODULE_NAME]));
-
- if (!is_valid_guid (ctx, data [MONO_MODULE_MVID]))
- ADD_ERROR (ctx, g_strdup_printf ("Module has an invalid Mvid, guid index %x", data [MONO_MODULE_MVID]));
-
- if (data [MONO_MODULE_ENC] != 0)
- ADD_ERROR (ctx, g_strdup_printf ("Module has a non zero Enc field %x", data [MONO_MODULE_ENC]));
-
- if (data [MONO_MODULE_ENCBASE] != 0)
- ADD_ERROR (ctx, g_strdup_printf ("Module has a non zero EncBase field %x", data [MONO_MODULE_ENCBASE]));
-}
-
-static void
-verify_typeref_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPEREF];
- ERROR_DECL (error);
- guint32 i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_verifier_verify_typeref_row (ctx->image, i, error);
- add_from_mono_error (ctx, error);
- }
-}
-
-/*bits 9,11,14,15,19,21,24-31 */
-#define INVALID_TYPEDEF_FLAG_BITS ((1 << 6) | (1 << 9) | (1 << 15) | (1 << 19) | (1 << 21) | 0xFF000000)
-static void
-verify_typedef_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPEDEF];
- guint32 data [MONO_TYPEDEF_SIZE];
- guint32 fieldlist = 1, methodlist = 1, visibility;
- int i;
-
- if (table->rows == 0)
- ADD_ERROR (ctx, g_strdup_printf ("Typedef table must have exactly at least one row"));
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_TYPEDEF_SIZE);
- if (data [MONO_TYPEDEF_FLAGS] & INVALID_TYPEDEF_FLAG_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d invalid flags field 0x%08x rejected bits: 0x%08x", i, data [MONO_TYPEDEF_FLAGS], data [MONO_TYPEDEF_FLAGS] & INVALID_TYPEDEF_FLAG_BITS));
-
- if ((data [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_LAYOUT_MASK) == 0x18)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d invalid class layout 0x18", i));
-
- if ((data [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == 0x30000)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d mono doesn't support custom string format", i));
-
- if ((data [MONO_TYPEDEF_FLAGS] & 0xC00000) != 0)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d mono doesn't support custom string format", i));
-
- if ((data [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_INTERFACE) && (data [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_ABSTRACT) == 0)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d for interface type must be abstract", i));
-
- if (!data [MONO_TYPEDEF_NAME] || !is_valid_non_empty_string (ctx, data [MONO_TYPEDEF_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d invalid name token %08x", i, data [MONO_TYPEDEF_NAME]));
-
- if (data [MONO_TYPEREF_NAMESPACE] && !is_valid_non_empty_string (ctx, data [MONO_TYPEREF_NAMESPACE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d invalid namespace token %08x", i, data [MONO_TYPEREF_NAMESPACE]));
-
- 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));
-
- if (data [MONO_TYPEDEF_FIELD_LIST] > ctx->image->tables [MONO_TABLE_FIELD].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d FieldList rowid 0x%08x is out of range", i, data [MONO_TYPEDEF_FIELD_LIST]));
-
- if (data [MONO_TYPEDEF_FIELD_LIST] < fieldlist)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d FieldList rowid 0x%08x can't be smaller than of previous row 0x%08x", i, data [MONO_TYPEDEF_FIELD_LIST], fieldlist));
-
- if (data [MONO_TYPEDEF_METHOD_LIST] == 0)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d MethodList be be >= 1", i));
-
- if (data [MONO_TYPEDEF_METHOD_LIST] > ctx->image->tables [MONO_TABLE_METHOD].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d MethodList rowid 0x%08x is out of range", i, data [MONO_TYPEDEF_METHOD_LIST]));
-
- if (data [MONO_TYPEDEF_METHOD_LIST] < methodlist)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d MethodList rowid 0x%08x can't be smaller than of previous row 0x%08x", i, data [MONO_TYPEDEF_METHOD_LIST], methodlist));
-
- fieldlist = data [MONO_TYPEDEF_FIELD_LIST];
- methodlist = data [MONO_TYPEDEF_METHOD_LIST];
- }
-}
-
-static void
-verify_typedef_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPEDEF];
- guint32 data [MONO_TYPEDEF_SIZE];
- int i;
-
- if (table->rows == 0)
- ADD_ERROR (ctx, g_strdup_printf ("Typedef table must have exactly at least one row"));
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_TYPEDEF_SIZE);
-
- if (i == 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;
- }
-
- if (data [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_INTERFACE) {
- if (data [MONO_TYPEDEF_EXTENDS])
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d for interface type must have a null extend field", i));
- } else {
- gboolean is_sys_obj = typedef_is_system_object (ctx, data);
- gboolean has_parent = get_coded_index_token (TYPEDEF_OR_REF_DESC, data [MONO_TYPEDEF_EXTENDS]) != 0;
-
- if (is_sys_obj) {
- if (has_parent)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d for System.Object must have a null extend field", i));
- } else {
- if (!has_parent) {
- ADD_ERROR (ctx, g_strdup_printf ("Invalid typedef row %d for non-interface type must have a non-null extend field", i));
- }
- }
- }
- }
-}
-
-/*bits 3,11,14 */
-#define INVALID_FIELD_FLAG_BITS ((1 << 3) | (1 << 11) | (1 << 14))
-static void
-verify_field_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_FIELD];
- guint32 data [MONO_FIELD_SIZE], flags, module_field_list;
- int i;
-
- module_field_list = (guint32)-1;
- if (ctx->image->tables [MONO_TABLE_TYPEDEF].rows > 1) {
- MonoTableInfo *type = &ctx->image->tables [MONO_TABLE_TYPEDEF];
- module_field_list = mono_metadata_decode_row_col (type, 1, MONO_TYPEDEF_FIELD_LIST);
- }
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_FIELD_SIZE);
- flags = data [MONO_FIELD_FLAGS];
-
- if (flags & INVALID_FIELD_FLAG_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d invalid flags field 0x%08x", i, flags));
-
- if ((flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == 0x7)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d invalid field visibility 0x7", i));
-
- if ((flags & (FIELD_ATTRIBUTE_LITERAL | FIELD_ATTRIBUTE_INIT_ONLY)) == (FIELD_ATTRIBUTE_LITERAL | FIELD_ATTRIBUTE_INIT_ONLY))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d cannot be InitOnly and Literal at the same time", i));
-
- if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d is RTSpecialName but not SpecialName", i));
-
- if ((flags & FIELD_ATTRIBUTE_LITERAL) && !(flags & FIELD_ATTRIBUTE_STATIC))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d is Literal but not Static", i));
-
- if ((flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL) &&
- search_sorted_table (ctx, MONO_TABLE_FIELDMARSHAL, MONO_FIELD_MARSHAL_PARENT, make_coded_token (HAS_FIELD_MARSHAL_DESC, MONO_TABLE_FIELD, i)) == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d has FieldMarshal but there is no corresponding row in the FieldMarshal table", i));
-
- if ((flags & FIELD_ATTRIBUTE_HAS_DEFAULT) &&
- search_sorted_table (ctx, MONO_TABLE_CONSTANT, MONO_CONSTANT_PARENT, make_coded_token (HAS_CONSTANT_DESC, MONO_TABLE_FIELD, i)) == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d has Default but there is no corresponding row in the Constant table", i));
-
- if ((flags & FIELD_ATTRIBUTE_LITERAL) &&
- search_sorted_table (ctx, MONO_TABLE_CONSTANT, MONO_CONSTANT_PARENT, make_coded_token (HAS_CONSTANT_DESC, MONO_TABLE_FIELD, i)) == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d is Literal but there is no corresponding row in the Constant table", i));
-
- if ((flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA) &&
- search_sorted_table (ctx, MONO_TABLE_FIELDRVA, MONO_FIELD_RVA_FIELD, i + 1) == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d has Default but there is no corresponding row in the Constant table", i));
-
- if (!data [MONO_FIELD_NAME] || !is_valid_non_empty_string (ctx, data [MONO_FIELD_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d invalid name token %08x", i, data [MONO_FIELD_NAME]));
-
- if (data [MONO_FIELD_SIGNATURE] && !is_valid_blob_object (ctx, data [MONO_FIELD_SIGNATURE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d invalid signature blob token 0x%x", i, data [MONO_FIELD_SIGNATURE]));
-
- //TODO verify contant flag
-
- if (i + 1 < module_field_list) {
- guint32 access = flags & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
- if (!(flags & FIELD_ATTRIBUTE_STATIC))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d is a global variable but is not static", i));
- if (access != FIELD_ATTRIBUTE_COMPILER_CONTROLLED && access != FIELD_ATTRIBUTE_PRIVATE && access != FIELD_ATTRIBUTE_PUBLIC)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d is a global variable but have wrong visibility %x", i, access));
- }
- }
-}
-
-static void
-verify_field_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_FIELD];
- guint32 data [MONO_FIELD_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_FIELD_SIZE);
-
- if (!data [MONO_FIELD_SIGNATURE] || !is_valid_field_signature (ctx, data [MONO_FIELD_SIGNATURE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid field row %d invalid signature token %08x", i, data [MONO_FIELD_SIGNATURE]));
- }
-}
-
-/*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)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHOD];
- guint32 data [MONO_METHOD_SIZE], flags, implflags, rva, module_method_list, access, code_type;
- guint32 paramlist = 1;
- gboolean is_ctor, is_cctor;
- const char *name;
- int i;
-
- module_method_list = (guint32)-1;
- if (ctx->image->tables [MONO_TABLE_TYPEDEF].rows > 1) {
- MonoTableInfo *type = &ctx->image->tables [MONO_TABLE_TYPEDEF];
- module_method_list = mono_metadata_decode_row_col (type, 1, MONO_TYPEDEF_METHOD_LIST);
- }
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_METHOD_SIZE);
- rva = data [MONO_METHOD_RVA];
- implflags = data [MONO_METHOD_IMPLFLAGS];
- flags = data [MONO_METHOD_FLAGS];
- access = flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
- code_type = implflags & METHOD_IMPL_ATTRIBUTE_CODE_TYPE_MASK;
-
-
- if (implflags & INVALID_METHOD_IMPLFLAG_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d invalid implflags field 0x%08x", i, implflags));
-
- if (access == 0x7)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d invalid MemberAccessMask 0x7", i));
-
- if (!data [MONO_METHOD_NAME] || !is_valid_non_empty_string (ctx, data [MONO_METHOD_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d invalid name field 0x%08x", i, data [MONO_METHOD_NAME]));
-
- name = get_string_ptr (ctx, data [MONO_METHOD_NAME]);
- is_ctor = !strcmp (".ctor", name);
- is_cctor = !strcmp (".cctor", name);
-
- if ((is_ctor || is_cctor) &&
- search_sorted_table (ctx, MONO_TABLE_GENERICPARAM, MONO_GENERICPARAM_OWNER, make_coded_token (TYPE_OR_METHODDEF_DESC, MONO_TABLE_METHOD, i)) != -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d .ctor or .cctor has generic param", i));
-
- if ((flags & METHOD_ATTRIBUTE_STATIC) && (flags & (METHOD_ATTRIBUTE_FINAL | METHOD_ATTRIBUTE_VIRTUAL | METHOD_ATTRIBUTE_NEW_SLOT)))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is static and (final, virtual or new slot)", i));
-
- 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_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));
-
- //XXX no checks against cas stuff 10,11,12,13)
-
- //TODO check iface with .ctor (15,16)
-
- if (i + 1 < module_method_list) {
- if (!(flags & METHOD_ATTRIBUTE_STATIC))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is a global method but not Static", i));
- if (flags & (METHOD_ATTRIBUTE_ABSTRACT | METHOD_ATTRIBUTE_VIRTUAL))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is a global method but is Abstract or Virtual", i));
- if (access == METHOD_ATTRIBUTE_FAMILY || access == METHOD_ATTRIBUTE_FAM_AND_ASSEM || access == METHOD_ATTRIBUTE_FAM_OR_ASSEM)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is a global method but not CompilerControled, Public, Private or Assembly", i));
- }
-
- //TODO check valuetype for synchronized
-
- 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) {
- 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)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is not Abstract and neither PinvokeImpl, Runtime, InternalCall or with RVA != 0", i));
-
- if (access == METHOD_ATTRIBUTE_COMPILER_CONTROLLED && !(rva || (flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is CompilerControlled but neither RVA != 0 or PinvokeImpl", i));
-
- //TODO check signature contents
-
- if (rva) {
- 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 {
- if (!(flags & (METHOD_ATTRIBUTE_ABSTRACT | METHOD_ATTRIBUTE_PINVOKE_IMPL)) && !(implflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && code_type != METHOD_IMPL_ATTRIBUTE_RUNTIME)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d has RVA = 0 but neither Abstract, InternalCall, Runtime or PinvokeImpl", i));
- }
-
- if ((flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
- if (rva)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is PinvokeImpl but has RVA != 0", i));
- if (search_sorted_table (ctx, MONO_TABLE_IMPLMAP, MONO_IMPLMAP_MEMBER, make_coded_token (MEMBER_FORWARDED_DESC, MONO_TABLE_METHOD, i)) == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is PinvokeImpl but has no row in the ImplMap table", i));
- }
- if (flags & METHOD_ATTRIBUTE_RT_SPECIAL_NAME && !is_ctor && !is_cctor)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is RtSpecialName but not named .ctor or .cctor", i));
-
- if ((is_ctor || is_cctor) && !(flags & METHOD_ATTRIBUTE_RT_SPECIAL_NAME))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d is named .ctor or .cctor but is not RtSpecialName", i));
-
- if (data [MONO_METHOD_SIGNATURE] && !is_valid_blob_object (ctx, data [MONO_METHOD_SIGNATURE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d invalid signature blob token 0x%x", i, data [MONO_METHOD_SIGNATURE]));
-
- if (data [MONO_METHOD_PARAMLIST] == 0)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d ParamList be be >= 1", i));
-
- if (data [MONO_METHOD_PARAMLIST] < paramlist)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d ParamList rowid 0x%08x can't be smaller than of previous row 0x%08x", i, data [MONO_METHOD_PARAMLIST], paramlist));
-
- if (data [MONO_METHOD_PARAMLIST] > ctx->image->tables [MONO_TABLE_PARAM].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d ParamList rowid 0x%08x is out of range", i, data [MONO_METHOD_PARAMLIST]));
-
- paramlist = data [MONO_METHOD_PARAMLIST];
-
- }
-}
-
-static void
-verify_method_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHOD];
- guint32 data [MONO_METHOD_SIZE], rva, locals_token;
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_METHOD_SIZE);
- rva = data [MONO_METHOD_RVA];
-
- 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, &locals_token))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid method row %d RVA points to an invalid method header", i));
- }
-}
-
-static guint32
-get_next_param_count (VerifyContext *ctx, guint32 *current_method)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHOD];
- guint32 row = *current_method;
- guint32 paramlist, tmp;
-
-
- paramlist = mono_metadata_decode_row_col (table, row++, MONO_METHOD_PARAMLIST);
- while (row < table->rows) {
- tmp = mono_metadata_decode_row_col (table, row, MONO_METHOD_PARAMLIST);
- if (tmp > paramlist) {
- *current_method = row;
- return tmp - paramlist;
- }
- ++row;
- }
-
- /*no more methods, all params apply to the last one*/
- *current_method = table->rows;
- return (guint32)-1;
-}
-
-
-#define INVALID_PARAM_FLAGS_BITS ((1 << 2) | (1 << 3) | (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 11) | (1 << 14) | (1 << 15))
-static void
-verify_param_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_PARAM];
- guint32 data [MONO_PARAM_SIZE], flags, sequence = 0, remaining_params, current_method = 0;
- gboolean first_param = TRUE;
- int i;
-
- if (ctx->image->tables [MONO_TABLE_METHOD].rows == 0) {
- if (table->rows > 0)
- ADD_ERROR (ctx, g_strdup ("Param table has rows while the method table has zero"));
- return;
- }
-
- remaining_params = get_next_param_count (ctx, ¤t_method);
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_PARAM_SIZE);
- flags = data [MONO_PARAM_FLAGS];
-
- if (flags & INVALID_PARAM_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid param row %d bad Flags value 0x%08x", i, flags));
-
- if (search_sorted_table (ctx, MONO_TABLE_CONSTANT, MONO_CONSTANT_PARENT, make_coded_token (HAS_CONSTANT_DESC, MONO_TABLE_PARAM, i)) == -1) {
- if (flags & PARAM_ATTRIBUTE_HAS_DEFAULT)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid param row %d HasDefault = 1 but no owned row in Contant table", i));
- } else {
- if (!(flags & PARAM_ATTRIBUTE_HAS_DEFAULT))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid param row %d HasDefault = 0 but has owned row in Contant table", i));
- }
-
- if ((flags & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) && search_sorted_table (ctx, MONO_TABLE_FIELDMARSHAL, MONO_FIELD_MARSHAL_PARENT, make_coded_token (HAS_FIELD_MARSHAL_DESC, MONO_TABLE_PARAM, i)) == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid param row %d HasFieldMarshal = 1 but no owned row in FieldMarshal table", i));
-
- if (!is_valid_string (ctx, data [MONO_PARAM_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid param row %d Name = 1 bad token 0x%08x", i, data [MONO_PARAM_NAME]));
-
- if (!first_param && data [MONO_PARAM_SEQUENCE] <= sequence)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid param row %d sequece = %d previus param has %d", i, data [MONO_PARAM_SEQUENCE], sequence));
-
- first_param = FALSE;
- sequence = data [MONO_PARAM_SEQUENCE];
- if (--remaining_params == 0) {
- remaining_params = get_next_param_count (ctx, ¤t_method);
- first_param = TRUE;
- }
- }
-}
-
-static void
-verify_interfaceimpl_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_INTERFACEIMPL];
- guint32 data [MONO_INTERFACEIMPL_SIZE];
- int i;
-
- 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_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]));
-
- if (!get_coded_index_token (TYPEDEF_OR_REF_DESC, data [MONO_INTERFACEIMPL_INTERFACE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid InterfaceImpl row %d Inteface field is null", i));
- }
-}
-
-static void
-verify_memberref_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_MEMBERREF];
- guint32 data [MONO_MEMBERREF_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_MEMBERREF_SIZE);
-
- if (!is_valid_coded_index (ctx, MEMBERREF_PARENT_DESC, data [MONO_MEMBERREF_CLASS]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MemberRef row %d Class field coded index 0x%08x", i, data [MONO_MEMBERREF_CLASS]));
-
- if (!get_coded_index_token (MEMBERREF_PARENT_DESC, data [MONO_MEMBERREF_CLASS]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MemberRef row %d Class field coded is null", i));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_MEMBERREF_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MemberRef row %d Name field coded is invalid or empty 0x%08x", i, data [MONO_MEMBERREF_NAME]));
-
- if (data [MONO_MEMBERREF_SIGNATURE] && !is_valid_blob_object (ctx, data [MONO_MEMBERREF_SIGNATURE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MemberRef row %d invalid signature blob token 0x%x", i, data [MONO_MEMBERREF_SIGNATURE]));
- }
-}
-
-
-static void
-verify_memberref_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_MEMBERREF];
- guint32 data [MONO_MEMBERREF_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_MEMBERREF_SIZE);
-
- if (!is_valid_method_or_field_signature (ctx, data [MONO_MEMBERREF_SIGNATURE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MemberRef row %d Signature field 0x%08x", i, data [MONO_MEMBERREF_SIGNATURE]));
- }
-}
-
-static void
-verify_constant_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_CONSTANT];
- guint32 data [MONO_CONSTANT_SIZE], type;
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_CONSTANT_SIZE);
- type = data [MONO_CONSTANT_TYPE];
-
- if (!((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_STRING) || type == MONO_TYPE_CLASS))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Constant row %d Type field 0x%08x", i, type));
-
- if (!is_valid_coded_index (ctx, HAS_CONSTANT_DESC, data [MONO_CONSTANT_PARENT]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Constant row %d Parent field coded index 0x%08x", i, data [MONO_CONSTANT_PARENT]));
-
- if (!get_coded_index_token (HAS_CONSTANT_DESC, data [MONO_CONSTANT_PARENT]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Constant row %d Parent field coded is null", i));
-
- if (!is_valid_constant (ctx, type, data [MONO_CONSTANT_VALUE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Constant row %d Value field 0x%08x", i, data [MONO_CONSTANT_VALUE]));
- }
-}
-
-static void
-verify_cattr_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_CUSTOMATTRIBUTE];
- guint32 data [MONO_CUSTOM_ATTR_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_CUSTOM_ATTR_SIZE);
-
- 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]) || !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]));
- }
-}
-
-static void
-verify_cattr_table_full (VerifyContext *ctx)
-{
- ERROR_DECL (error);
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_CUSTOMATTRIBUTE];
- 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_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_checked (ctx->image, mtoken, NULL, NULL, error);
-
- if (!ctor) {
- ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute content row %d Could not load ctor due to %s", i, mono_error_get_message (error)));
- mono_error_cleanup (error);
- }
-
- /*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)) {
- char *ctor_name = mono_method_full_name (ctor, TRUE);
- ADD_ERROR (ctx, g_strdup_printf ("Invalid CustomAttribute content row %d Value field 0x%08x ctor: %s", i, data [MONO_CUSTOM_ATTR_VALUE], ctor_name));
- g_free (ctor_name);
- }
- }
-}
-
-static void
-verify_field_marshal_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_FIELDMARSHAL];
- guint32 data [MONO_FIELD_MARSHAL_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_FIELD_MARSHAL_SIZE);
-
- if (!is_valid_coded_index (ctx, HAS_FIELD_MARSHAL_DESC, data [MONO_FIELD_MARSHAL_PARENT]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldMarshal row %d Parent field 0x%08x", i, data [MONO_FIELD_MARSHAL_PARENT]));
-
- if (!get_coded_index_token (HAS_FIELD_MARSHAL_DESC, data [MONO_FIELD_MARSHAL_PARENT]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldMarshal row %d Parent field is null", i));
-
- if (!data [MONO_FIELD_MARSHAL_NATIVE_TYPE])
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldMarshal row %d NativeType field is null", i));
-
- if (!is_valid_blob_object (ctx, data [MONO_FIELD_MARSHAL_NATIVE_TYPE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldMarshal row %d invalid NativeType blob 0x%x", i, data [MONO_FIELD_MARSHAL_NATIVE_TYPE]));
- }
-}
-
-static void
-verify_field_marshal_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_FIELDMARSHAL];
- guint32 data [MONO_FIELD_MARSHAL_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_FIELD_MARSHAL_SIZE);
-
- if (!is_valid_marshal_spec (ctx, data [MONO_FIELD_MARSHAL_NATIVE_TYPE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldMarshal row %d NativeType field 0x%08x", i, data [MONO_FIELD_MARSHAL_NATIVE_TYPE]));
- }
-}
-
-static void
-verify_decl_security_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_DECLSECURITY];
- guint32 data [MONO_DECL_SECURITY_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_DECL_SECURITY_SIZE);
-
- if (!is_valid_coded_index (ctx, HAS_DECL_SECURITY_DESC, data [MONO_DECL_SECURITY_PARENT]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid DeclSecurity row %d Parent field 0x%08x", i, data [MONO_DECL_SECURITY_PARENT]));
-
- if (!get_coded_index_token (HAS_DECL_SECURITY_DESC, data [MONO_DECL_SECURITY_PARENT]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid DeclSecurity row %d Parent field is null", i));
-
- if (!data [MONO_DECL_SECURITY_PERMISSIONSET])
- ADD_ERROR (ctx, g_strdup_printf ("Invalid DeclSecurity row %d PermissionSet field is null", i));
- }
-}
-
-static void
-verify_decl_security_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_DECLSECURITY];
- guint32 data [MONO_DECL_SECURITY_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_DECL_SECURITY_SIZE);
-
- if (!is_valid_permission_set (ctx, data [MONO_DECL_SECURITY_PERMISSIONSET]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid DeclSecurity row %d PermissionSet field 0x%08x", i, data [MONO_DECL_SECURITY_PERMISSIONSET]));
- }
-}
-
-static void
-verify_class_layout_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_CLASSLAYOUT];
- guint32 data [MONO_CLASS_LAYOUT_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_CLASS_LAYOUT_SIZE);
-
- if (!data [MONO_CLASS_LAYOUT_PARENT] || data[MONO_CLASS_LAYOUT_PARENT] > ctx->image->tables [MONO_TABLE_TYPEDEF].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ClassLayout row %d Parent field 0x%08x", i, data [MONO_TABLE_TYPEDEF]));
-
- switch (data [MONO_CLASS_LAYOUT_PACKING_SIZE]) {
- case 0:
- case 1:
- case 2:
- case 4:
- case 8:
- case 16:
- case 32:
- case 64:
- case 128:
- break;
- default:
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ClassLayout row %d Packing field %d", i, data [MONO_CLASS_LAYOUT_PACKING_SIZE]));
- }
- }
-}
-
-static void
-verify_field_layout_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_FIELDLAYOUT];
- guint32 data [MONO_FIELD_LAYOUT_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_FIELD_LAYOUT_SIZE);
-
- if (!data [MONO_FIELD_LAYOUT_FIELD] || data[MONO_FIELD_LAYOUT_FIELD] > ctx->image->tables [MONO_TABLE_FIELD].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldLayout row %d Field field 0x%08x", i, data [MONO_FIELD_LAYOUT_FIELD]));
- }
-}
-
-static void
-verify_standalonesig_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_STANDALONESIG];
- guint32 data [MONO_STAND_ALONE_SIGNATURE_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_STAND_ALONE_SIGNATURE_SIZE);
-
- if (data [MONO_STAND_ALONE_SIGNATURE] && !is_valid_blob_object (ctx, data [MONO_STAND_ALONE_SIGNATURE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid StandAloneSig row %d invalid signature 0x%x", i, data [MONO_STAND_ALONE_SIGNATURE]));
- }
-}
-
-static void
-verify_standalonesig_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_STANDALONESIG];
- guint32 data [MONO_STAND_ALONE_SIGNATURE_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_STAND_ALONE_SIGNATURE_SIZE);
-
- if (!is_valid_standalonesig_blob (ctx, data [MONO_STAND_ALONE_SIGNATURE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid StandAloneSig row %d Signature field 0x%08x", i, data [MONO_STAND_ALONE_SIGNATURE]));
- }
-}
-
-static void
-verify_eventmap_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_EVENTMAP];
- guint32 data [MONO_EVENT_MAP_SIZE], eventlist = 0;
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_EVENT_MAP_SIZE);
-
- if (!data [MONO_EVENT_MAP_PARENT] || data [MONO_EVENT_MAP_PARENT] > ctx->image->tables [MONO_TABLE_TYPEDEF].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid EventMap row %d Parent field 0x%08x", i, data [MONO_EVENT_MAP_PARENT]));
-
- if (!data [MONO_EVENT_MAP_EVENTLIST] || data [MONO_EVENT_MAP_EVENTLIST] <= eventlist)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid EventMap row %d EventList field %d", i, data [MONO_EVENT_MAP_EVENTLIST]));
-
- eventlist = data [MONO_EVENT_MAP_EVENTLIST];
- }
-}
-
-#define INVALID_EVENT_FLAGS_BITS ~((1 << 9) | (1 << 10))
-static void
-verify_event_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_EVENT];
- guint32 data [MONO_EVENT_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_EVENT_SIZE);
-
- if (data [MONO_EVENT_FLAGS] & INVALID_EVENT_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Event row %d EventFlags field %08x", i, data [MONO_EVENT_FLAGS]));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_EVENT_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Event row %d Name field %08x", i, data [MONO_EVENT_NAME]));
-
- if (!is_valid_coded_index (ctx, TYPEDEF_OR_REF_DESC, data [MONO_EVENT_TYPE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Event row %d EventType field %08x", i, data [MONO_EVENT_TYPE]));
- }
-}
-
-static void
-verify_event_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_EVENT];
- MonoTableInfo *sema_table = &ctx->image->tables [MONO_TABLE_METHODSEMANTICS];
- guint32 data [MONO_EVENT_SIZE], sema_data [MONO_METHOD_SEMA_SIZE], token;
- gboolean found_add, found_remove;
- int i, idx;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_EVENT_SIZE);
-
- token = make_coded_token (HAS_SEMANTICS_DESC, MONO_TABLE_EVENT, i);
- idx = search_sorted_table (ctx, MONO_TABLE_METHODSEMANTICS, MONO_METHOD_SEMA_ASSOCIATION, token);
- if (idx == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Event row %d has no AddOn or RemoveOn associated methods", i));
-
- //first we move to the first row for this event
- while (idx > 0) {
- if (mono_metadata_decode_row_col (sema_table, idx - 1, MONO_METHOD_SEMA_ASSOCIATION) != token)
- break;
- --idx;
- }
- //now move forward looking for AddOn and RemoveOn rows
- found_add = found_remove = FALSE;
- while (idx < sema_table->rows) {
- mono_metadata_decode_row (sema_table, idx, sema_data, MONO_METHOD_SEMA_SIZE);
- if (sema_data [MONO_METHOD_SEMA_ASSOCIATION] != token)
- break;
- if (sema_data [MONO_METHOD_SEMA_SEMANTICS] & METHOD_SEMANTIC_ADD_ON)
- found_add = TRUE;
- if (sema_data [MONO_METHOD_SEMA_SEMANTICS] & METHOD_SEMANTIC_REMOVE_ON)
- found_remove = TRUE;
- if (found_add && found_remove)
- break;
- ++idx;
- }
-
- if (!found_add)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Event row %d has no AddOn associated method", i));
- if (!found_remove)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Event row %d has no RemoveOn associated method", i));
- }
-}
-
-static void
-verify_propertymap_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_PROPERTYMAP];
- guint32 data [MONO_PROPERTY_MAP_SIZE], propertylist = 0;
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_PROPERTY_MAP_SIZE);
-
- if (!data [MONO_PROPERTY_MAP_PARENT] || data [MONO_PROPERTY_MAP_PARENT] > ctx->image->tables [MONO_TABLE_TYPEDEF].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid PropertyMap row %d Parent field 0x%08x", i, data [MONO_PROPERTY_MAP_PARENT]));
-
- if (!data [MONO_PROPERTY_MAP_PROPERTY_LIST] || data [MONO_PROPERTY_MAP_PROPERTY_LIST] <= propertylist)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid PropertyMap row %d PropertyList field %d", i, data [MONO_PROPERTY_MAP_PROPERTY_LIST]));
-
- propertylist = data [MONO_PROPERTY_MAP_PROPERTY_LIST];
- }
-}
-
-#define INVALID_PROPERTY_FLAGS_BITS ~((1 << 9) | (1 << 10) | (1 << 12))
-static void
-verify_property_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_PROPERTY];
- guint32 data [MONO_PROPERTY_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_PROPERTY_SIZE);
-
- if (data [MONO_PROPERTY_FLAGS] & INVALID_PROPERTY_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Property row %d PropertyFlags field %08x", i, data [MONO_PROPERTY_FLAGS]));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_PROPERTY_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Property row %d Name field %08x", i, data [MONO_PROPERTY_NAME]));
-
- if (!is_valid_property_sig_blob (ctx, data [MONO_PROPERTY_TYPE]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Property row %d Type field %08x", i, data [MONO_PROPERTY_TYPE]));
-
- if ((data [MONO_PROPERTY_FLAGS] & PROPERTY_ATTRIBUTE_HAS_DEFAULT) &&
- search_sorted_table (ctx, MONO_TABLE_CONSTANT, MONO_CONSTANT_PARENT, make_coded_token (HAS_CONSTANT_DESC, MONO_TABLE_PROPERTY, i)) == -1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid Property row %d has HasDefault but there is no corresponding row in the Constant table", i));
-
- }
-}
-
-static void
-verify_methodimpl_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHODIMPL];
- guint32 data [MONO_METHODIMPL_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_METHODIMPL_SIZE);
-
- if (!data [MONO_METHODIMPL_CLASS] || data [MONO_METHODIMPL_CLASS] > ctx->image->tables [MONO_TABLE_TYPEDEF].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MethodImpl row %d Class field %08x", i, data [MONO_TABLE_TYPEDEF]));
-
- if (!get_coded_index_token (METHODDEF_OR_REF_DESC, data [MONO_METHODIMPL_BODY]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MethodImpl row %d MethodBody field %08x", i, data [MONO_METHODIMPL_BODY]));
-
- if (!is_valid_coded_index (ctx, METHODDEF_OR_REF_DESC, data [MONO_METHODIMPL_BODY]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MethodImpl row %d MethodBody field %08x", i, data [MONO_METHODIMPL_BODY]));
-
- if (!get_coded_index_token (METHODDEF_OR_REF_DESC, data [MONO_METHODIMPL_DECLARATION]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MethodImpl row %d MethodDeclaration field %08x", i, data [MONO_METHODIMPL_DECLARATION]));
-
- if (!is_valid_coded_index (ctx, METHODDEF_OR_REF_DESC, data [MONO_METHODIMPL_DECLARATION]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid MethodImpl row %d MethodDeclaration field %08x", i, data [MONO_METHODIMPL_DECLARATION]));
- }
-}
-
-static void
-verify_moduleref_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_MODULEREF];
- guint32 data [MONO_MODULEREF_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- 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 ModuleRef row %d name field %08x", i, data [MONO_MODULEREF_NAME]));
- }
-}
-
-static void
-verify_typespec_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPESPEC];
- guint32 data [MONO_TYPESPEC_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_TYPESPEC_SIZE);
-
- if (data [MONO_TYPESPEC_SIGNATURE] && !is_valid_blob_object (ctx, data [MONO_TYPESPEC_SIGNATURE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid TypeSpec row %d Signature field %08x", i, data [MONO_TYPESPEC_SIGNATURE]));
- }
-}
-
-static void
-verify_typespec_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_TYPESPEC];
- guint32 data [MONO_TYPESPEC_SIZE];
- int i;
-
- 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 << 4) | (1 << 5) | (1 << 6) | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 12) | (1 << 13))
-static void
-verify_implmap_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_IMPLMAP];
- guint32 data [MONO_IMPLMAP_SIZE], cconv;
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_IMPLMAP_SIZE);
-
- if (data [MONO_IMPLMAP_FLAGS] & INVALID_IMPLMAP_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d Flags field %08x", i, data [MONO_IMPLMAP_FLAGS]));
-
- cconv = data [MONO_IMPLMAP_FLAGS] & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
- if (cconv == 0 || cconv == 0x0600 || cconv == 0x0700)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d Invalid call conv field %x", i, cconv));
-
- if (!is_valid_coded_index (ctx, MEMBER_FORWARDED_DESC, data [MONO_IMPLMAP_MEMBER]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d Invalid MemberForward token %x", i, data [MONO_IMPLMAP_MEMBER]));
-
- if (get_coded_index_table (MEMBER_FORWARDED_DESC, data [MONO_IMPLMAP_MEMBER]) != MONO_TABLE_METHOD)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d only methods are supported token %x", i, data [MONO_IMPLMAP_MEMBER]));
-
- if (!get_coded_index_token (MEMBER_FORWARDED_DESC, data [MONO_IMPLMAP_MEMBER]))
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d null token", i));
-
- 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)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid ImplMap row %d Invalid ImportScope token %x", i, data [MONO_IMPLMAP_SCOPE]));
- }
-}
-
-static void
-verify_fieldrva_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_FIELDRVA];
- guint32 data [MONO_FIELD_RVA_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_FIELD_RVA_SIZE);
-
- if (!data [MONO_FIELD_RVA_RVA] || mono_cli_rva_image_map (ctx->image, data [MONO_FIELD_RVA_RVA]) == INVALID_ADDRESS)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldRVA row %d RVA %08x", i, data [MONO_FIELD_RVA_RVA]));
-
- if (!data [MONO_FIELD_RVA_FIELD] || data [MONO_FIELD_RVA_FIELD] > ctx->image->tables [MONO_TABLE_FIELD].rows + 1)
- ADD_ERROR (ctx, g_strdup_printf ("Invalid FieldRVA row %d Field %08x", i, data [MONO_FIELD_RVA_FIELD]));
- }
-}
-
-#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)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_ASSEMBLY];
- guint32 data [MONO_ASSEMBLY_SIZE], hash;
- int i;
-
- if (table->rows > 1)
- ADD_ERROR (ctx, g_strdup_printf ("Assembly table can have zero or one rows, but now %d", table->rows));
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_ASSEMBLY_SIZE);
-
- hash = data [MONO_ASSEMBLY_HASH_ALG];
- if (!(hash == 0 || hash == 0x8003 || hash == 0x8004))
- ADD_ERROR (ctx, g_strdup_printf ("Assembly table row %d has invalid HashAlgId %x", i, hash));
-
- if (data [MONO_ASSEMBLY_FLAGS] & INVALID_ASSEMBLY_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("Assembly table row %d has invalid Flags %08x", i, data [MONO_ASSEMBLY_FLAGS]));
-
- if (data [MONO_ASSEMBLY_PUBLIC_KEY] && !is_valid_blob_object (ctx, data [MONO_ASSEMBLY_PUBLIC_KEY], 1))
- ADD_ERROR (ctx, g_strdup_printf ("Assembly table row %d has invalid PublicKey %08x", i, data [MONO_ASSEMBLY_FLAGS]));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_ASSEMBLY_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("Assembly table row %d has invalid Name %08x", i, data [MONO_ASSEMBLY_NAME]));
-
- if (data [MONO_ASSEMBLY_CULTURE] && !is_valid_string (ctx, data [MONO_ASSEMBLY_CULTURE]))
- ADD_ERROR (ctx, g_strdup_printf ("Assembly table row %d has invalid Culture %08x", i, data [MONO_ASSEMBLY_CULTURE]));
- }
-}
-
-#define INVALID_ASSEMBLYREF_FLAGS_BITS ~((1 << 0) | (1 << 8) | (1 << 14) | (1 << 15))
-static void
-verify_assemblyref_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_ASSEMBLYREF];
- guint32 data [MONO_ASSEMBLYREF_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_ASSEMBLYREF_SIZE);
-
- if (data [MONO_ASSEMBLYREF_FLAGS] & INVALID_ASSEMBLYREF_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("AssemblyRef table row %d has invalid Flags %08x", i, data [MONO_ASSEMBLYREF_FLAGS]));
-
- if (data [MONO_ASSEMBLYREF_PUBLIC_KEY] && !is_valid_blob_object (ctx, data [MONO_ASSEMBLYREF_PUBLIC_KEY], 1))
- ADD_ERROR (ctx, g_strdup_printf ("AssemblyRef table row %d has invalid PublicKeyOrToken %08x", i, data [MONO_ASSEMBLYREF_PUBLIC_KEY]));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_ASSEMBLYREF_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("AssemblyRef table row %d has invalid Name %08x", i, data [MONO_ASSEMBLYREF_NAME]));
-
- if (data [MONO_ASSEMBLYREF_CULTURE] && !is_valid_string (ctx, data [MONO_ASSEMBLYREF_CULTURE]))
- ADD_ERROR (ctx, g_strdup_printf ("AssemblyRef table row %d has invalid Culture %08x", i, data [MONO_ASSEMBLYREF_CULTURE]));
-
- if (data [MONO_ASSEMBLYREF_HASH_VALUE] && !is_valid_blob_object (ctx, data [MONO_ASSEMBLYREF_HASH_VALUE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("AssemblyRef table row %d has invalid HashValue %08x", i, data [MONO_ASSEMBLYREF_HASH_VALUE]));
- }
-}
-
-#define INVALID_FILE_FLAGS_BITS ~(1)
-static void
-verify_file_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_FILE];
- guint32 data [MONO_FILE_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_FILE_SIZE);
-
- if (data [MONO_FILE_FLAGS] & INVALID_FILE_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("File table row %d has invalid Flags %08x", i, data [MONO_FILE_FLAGS]));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_FILE_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("File table row %d has invalid Name %08x", i, data [MONO_FILE_NAME]));
-
- if (!data [MONO_FILE_HASH_VALUE] || !is_valid_blob_object (ctx, data [MONO_FILE_HASH_VALUE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("File table row %d has invalid HashValue %08x", i, data [MONO_FILE_HASH_VALUE]));
- }
-}
-
-#define INVALID_EXPORTED_TYPE_FLAGS_BITS (INVALID_TYPEDEF_FLAG_BITS & ~TYPE_ATTRIBUTE_FORWARDER)
-static void
-verify_exportedtype_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_EXPORTEDTYPE];
- guint32 data [MONO_EXP_TYPE_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_EXP_TYPE_SIZE);
-
- if (data [MONO_EXP_TYPE_FLAGS] & INVALID_EXPORTED_TYPE_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("ExportedType table row %d has invalid Flags %08x", i, data [MONO_EXP_TYPE_FLAGS]));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_EXP_TYPE_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("ExportedType table row %d has invalid TypeName %08x", i, data [MONO_FILE_NAME]));
-
- if (data [MONO_EXP_TYPE_NAMESPACE] && !is_valid_string (ctx, data [MONO_EXP_TYPE_NAMESPACE]))
- ADD_ERROR (ctx, g_strdup_printf ("ExportedType table row %d has invalid TypeNamespace %08x", i, data [MONO_EXP_TYPE_NAMESPACE]));
-
- if (!is_valid_coded_index (ctx, IMPLEMENTATION_DESC, data [MONO_EXP_TYPE_IMPLEMENTATION]))
- ADD_ERROR (ctx, g_strdup_printf ("ExportedType table row %d has invalid Implementation token %08x", i, data [MONO_EXP_TYPE_IMPLEMENTATION]));
-
- if (!get_coded_index_token (IMPLEMENTATION_DESC, data [MONO_EXP_TYPE_IMPLEMENTATION]))
- ADD_ERROR (ctx, g_strdup_printf ("ExportedType table row %d has null Implementation token", i));
-
- /*nested type can't have a namespace*/
- if (get_coded_index_table (IMPLEMENTATION_DESC, data [MONO_EXP_TYPE_IMPLEMENTATION]) == MONO_TABLE_EXPORTEDTYPE && data [MONO_EXP_TYPE_NAMESPACE])
- ADD_ERROR (ctx, g_strdup_printf ("ExportedType table row %d has denotes a nested type but has a non null TypeNamespace", i));
- }
-}
-
-#define INVALID_MANIFEST_RESOURCE_FLAGS_BITS ~((1 << 0) | (1 << 1) | (1 << 2))
-static void
-verify_manifest_resource_table (VerifyContext *ctx)
-{
- MonoCLIImageInfo *iinfo = ctx->image->image_info;
- MonoCLIHeader *ch = &iinfo->cli_cli_header;
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_MANIFESTRESOURCE];
- guint32 data [MONO_MANIFEST_SIZE], impl_table, token, resources_size;
- int i;
-
- resources_size = ch->ch_resources.size;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_MANIFEST_SIZE);
-
- if (data [MONO_MANIFEST_FLAGS] & INVALID_MANIFEST_RESOURCE_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("ManifestResource table row %d has invalid Flags %08x", i, data [MONO_MANIFEST_FLAGS]));
-
- if (data [MONO_MANIFEST_FLAGS] != 1 && data [MONO_MANIFEST_FLAGS] != 2)
- ADD_ERROR (ctx, g_strdup_printf ("ManifestResource table row %d has invalid Flags VisibilityMask %08x", i, data [MONO_MANIFEST_FLAGS]));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_MANIFEST_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("ManifestResource table row %d has invalid Name %08x", i, data [MONO_MANIFEST_NAME]));
-
- if (!is_valid_coded_index (ctx, IMPLEMENTATION_DESC, data [MONO_MANIFEST_IMPLEMENTATION]))
- ADD_ERROR (ctx, g_strdup_printf ("ManifestResource table row %d has invalid Implementation token %08x", i, data [MONO_MANIFEST_IMPLEMENTATION]));
-
- impl_table = get_coded_index_table (IMPLEMENTATION_DESC, data [MONO_MANIFEST_IMPLEMENTATION]);
- token = get_coded_index_token (IMPLEMENTATION_DESC, data [MONO_MANIFEST_IMPLEMENTATION]);
-
- if (impl_table == MONO_TABLE_EXPORTEDTYPE)
- ADD_ERROR (ctx, g_strdup_printf ("ManifestResource table row %d has invalid Implementation token table %08x", i, get_coded_index_table (IMPLEMENTATION_DESC, data [MONO_MANIFEST_IMPLEMENTATION])));
-
- if (impl_table == MONO_TABLE_FILE && token && data [MONO_MANIFEST_OFFSET])
- ADD_ERROR (ctx, g_strdup_printf ("ManifestResource table row %d points to a file but has non-zero offset", i));
-
- if (!token && data [MONO_MANIFEST_OFFSET] >= resources_size)
- ADD_ERROR (ctx, g_strdup_printf ("ManifestResource table row %d invalid Offset field %08x ", i, data [MONO_MANIFEST_OFFSET]));
- }
-}
-
-static void
-verify_nested_class_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_NESTEDCLASS];
- guint32 data [MONO_NESTED_CLASS_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_NESTED_CLASS_SIZE);
-
- if (!data [MONO_NESTED_CLASS_NESTED] || data [MONO_NESTED_CLASS_NESTED] > ctx->image->tables [MONO_TABLE_TYPEDEF].rows)
- ADD_ERROR (ctx, g_strdup_printf ("NestedClass table row %d has invalid NestedClass token %08x", i, data [MONO_NESTED_CLASS_NESTED]));
- if (!data [MONO_NESTED_CLASS_ENCLOSING] || data [MONO_NESTED_CLASS_ENCLOSING] > ctx->image->tables [MONO_TABLE_TYPEDEF].rows)
- ADD_ERROR (ctx, g_strdup_printf ("NestedClass table row %d has invalid EnclosingClass token %08x", i, data [MONO_NESTED_CLASS_ENCLOSING]));
- if (data [MONO_NESTED_CLASS_ENCLOSING] == data [MONO_NESTED_CLASS_NESTED])
- ADD_ERROR (ctx, g_strdup_printf ("NestedClass table row %d has same token for NestedClass and EnclosingClass %08x", i, data [MONO_NESTED_CLASS_ENCLOSING]));
- }
-}
-
-#define INVALID_GENERIC_PARAM_FLAGS_BITS ~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4))
-static void
-verify_generic_param_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_GENERICPARAM];
- guint32 data [MONO_GENERICPARAM_SIZE], token, last_token = 0;
- int i, param_number = 0;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_GENERICPARAM_SIZE);
-
- if (data [MONO_GENERICPARAM_FLAGS] & INVALID_GENERIC_PARAM_FLAGS_BITS)
- ADD_ERROR (ctx, g_strdup_printf ("GenericParam table row %d has invalid Flags token %08x", i, data [MONO_GENERICPARAM_FLAGS]));
-
- if ((data [MONO_GENERICPARAM_FLAGS] & MONO_GEN_PARAM_VARIANCE_MASK) == 0x3)
- ADD_ERROR (ctx, g_strdup_printf ("GenericParam table row %d has invalid VarianceMask 0x3", i));
-
- if (!is_valid_non_empty_string (ctx, data [MONO_GENERICPARAM_NAME]))
- ADD_ERROR (ctx, g_strdup_printf ("GenericParam table row %d has invalid Name token %08x", i, data [MONO_GENERICPARAM_NAME]));
-
- token = data [MONO_GENERICPARAM_OWNER];
-
- if (!is_valid_coded_index (ctx, TYPE_OR_METHODDEF_DESC, token))
- ADD_ERROR (ctx, g_strdup_printf ("GenericParam table row %d has invalid Owner token %08x", i, token));
-
- if (!get_coded_index_token (TYPE_OR_METHODDEF_DESC, token))
- ADD_ERROR (ctx, g_strdup_printf ("GenericParam table row %d has null Owner token", i));
-
- if (token != last_token) {
- param_number = 0;
- last_token = token;
- }
-
- if (data [MONO_GENERICPARAM_NUMBER] != param_number)
- ADD_ERROR (ctx, g_strdup_printf ("GenericParam table row %d Number is out of order %d expected %d", i, data [MONO_GENERICPARAM_NUMBER], param_number));
-
- ++param_number;
- }
-}
-
-static void
-verify_method_spec_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHODSPEC];
- guint32 data [MONO_METHODSPEC_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_METHODSPEC_SIZE);
-
- if (!is_valid_coded_index (ctx, METHODDEF_OR_REF_DESC, data [MONO_METHODSPEC_METHOD]))
- ADD_ERROR (ctx, g_strdup_printf ("MethodSpec table row %d has invalid Method token %08x", i, data [MONO_METHODSPEC_METHOD]));
-
- if (!get_coded_index_token (METHODDEF_OR_REF_DESC, data [MONO_METHODSPEC_METHOD]))
- ADD_ERROR (ctx, g_strdup_printf ("MethodSpec table row %d has null Method token", i));
-
- if (data [MONO_METHODSPEC_SIGNATURE] && !is_valid_blob_object (ctx, data [MONO_METHODSPEC_SIGNATURE], 1))
- ADD_ERROR (ctx, g_strdup_printf ("MethodSpec table row %d has invalid signature token %08x", i, data [MONO_METHODSPEC_SIGNATURE]));
- }
-}
-
-static void
-verify_method_spec_table_full (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHODSPEC];
- guint32 data [MONO_METHODSPEC_SIZE];
- int i;
-
- for (i = 0; i < table->rows; ++i) {
- mono_metadata_decode_row (table, i, data, MONO_METHODSPEC_SIZE);
-
- 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]));
- }
-}
-
-static void
-verify_generic_param_constraint_table (VerifyContext *ctx)
-{
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
- guint32 data [MONO_GENPARCONSTRAINT_SIZE];
- int i;
- guint32 last_owner = 0, last_constraint = 0;
-
- for (i = 0; i < table->rows; ++i) {
- 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_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]));
-
- if (!get_coded_index_token (TYPEDEF_OR_REF_DESC, data [MONO_GENPARCONSTRAINT_CONSTRAINT]))
- ADD_ERROR (ctx, g_strdup_printf ("GenericParamConstraint table row %d has null Constraint token", i));
-
- if (last_owner > data [MONO_GENPARCONSTRAINT_GENERICPAR])
- ADD_ERROR (ctx, g_strdup_printf ("GenericParamConstraint table row %d is not properly sorted. Previous value of the owner column is 0x%08x current value is 0x%08x", i, last_owner, data [MONO_GENPARCONSTRAINT_GENERICPAR]));
-
- if (last_owner == data [MONO_GENPARCONSTRAINT_GENERICPAR]) {
- if (last_constraint == data [MONO_GENPARCONSTRAINT_CONSTRAINT])
- ADD_ERROR (ctx, g_strdup_printf ("GenericParamConstraint table row %d has duplicate constraint 0x%08x", i, last_constraint));
- } else {
- last_owner = data [MONO_GENPARCONSTRAINT_GENERICPAR];
- }
- last_constraint = data [MONO_GENPARCONSTRAINT_CONSTRAINT];
- }
-}
-
-
-typedef struct {
- const char *name;
- const char *name_space;
- guint32 resolution_scope;
-} TypeDefUniqueId;
-
-static guint
-typedef_hash (gconstpointer _key)
-{
- const TypeDefUniqueId *key = (const TypeDefUniqueId *)_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 = (const TypeDefUniqueId *)_a;
- const TypeDefUniqueId *b = (const TypeDefUniqueId *)_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);
-}
-
-typedef struct {
- guint32 klass;
- guint32 method_declaration;
-} MethodImplUniqueId;
-
-static guint
-methodimpl_hash (gconstpointer _key)
-{
- const MethodImplUniqueId *key = (const MethodImplUniqueId *)_key;
- return key->klass ^ key->method_declaration;
-}
-
-static gboolean
-methodimpl_equals (gconstpointer _a, gconstpointer _b)
-{
- const MethodImplUniqueId *a = (const MethodImplUniqueId *)_a;
- const MethodImplUniqueId *b = (const MethodImplUniqueId *)_b;
- return a->klass == b->klass && a->method_declaration == b->method_declaration;
-}
-
-static void
-verify_methodimpl_table_global_constraints (VerifyContext *ctx)
-{
- int i;
- guint32 data [MONO_METHODIMPL_SIZE];
- MonoTableInfo *table = &ctx->image->tables [MONO_TABLE_METHODIMPL];
- GHashTable *unique_impls = g_hash_table_new_full (&methodimpl_hash, &methodimpl_equals, g_free, NULL);
-
- for (i = 0; i < table->rows; ++i) {
- MethodImplUniqueId *impl = g_new (MethodImplUniqueId, 1);
- mono_metadata_decode_row (table, i, data, MONO_METHODIMPL_SIZE);
-
- impl->klass = data [MONO_METHODIMPL_CLASS];
- impl->method_declaration = data [MONO_METHODIMPL_DECLARATION];
-
- if (g_hash_table_lookup (unique_impls, impl)) {
- ADD_ERROR_NO_RETURN (ctx, g_strdup_printf ("MethodImpl table row %d has duplicate for tuple (0x%x, 0x%x)", i, impl->klass, impl->method_declaration));
- g_hash_table_destroy (unique_impls);
- g_free (impl);
- return;
- }
- g_hash_table_insert (unique_impls, impl, GUINT_TO_POINTER (1));
- }
-
- g_hash_table_destroy (unique_impls);
-}
-
-
-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);
- verify_methodimpl_table_global_constraints (ctx);
-}
-
-static void
-verify_tables_data (VerifyContext *ctx)
-{
- OffsetAndSize tables_area = get_metadata_stream (ctx, &ctx->image->heap_tables);
- guint32 size = 0, tables_offset;
- int i;
-
- for (i = 0; i < 0x2D; ++i) {
- MonoTableInfo *table = &ctx->image->tables [i];
- guint32 tmp_size;
- tmp_size = size + (guint32)table->row_size * (guint32)table->rows;
- if (tmp_size < size) {
- size = 0;
- break;
- }
- size = tmp_size;
- }
-
- if (size == 0)
- ADD_ERROR (ctx, g_strdup_printf ("table space is either empty or overflowed"));
-
- tables_offset = ctx->image->tables_base - ctx->data;
- if (!bounds_check_offset (&tables_area, tables_offset, size))
- ADD_ERROR (ctx, g_strdup_printf ("Tables data require %d bytes but the only %d are available in the #~ stream", size, tables_area.size - (tables_offset - tables_area.offset)));
-
- verify_module_table (ctx);
- CHECK_ERROR ();
- /*Obfuscators love to place broken stuff in the typeref table
- verify_typeref_table (ctx);
- CHECK_ERROR ();*/
- verify_typedef_table (ctx);
- CHECK_ERROR ();
- verify_field_table (ctx);
- CHECK_ERROR ();
- verify_method_table (ctx);
- CHECK_ERROR ();
- verify_param_table (ctx);
- CHECK_ERROR ();
- verify_interfaceimpl_table (ctx);
- CHECK_ERROR ();
- verify_memberref_table (ctx);
- CHECK_ERROR ();
- verify_constant_table (ctx);
- CHECK_ERROR ();
- verify_cattr_table (ctx);
- CHECK_ERROR ();
- verify_field_marshal_table (ctx);
- CHECK_ERROR ();
- verify_decl_security_table (ctx);
- CHECK_ERROR ();
- verify_class_layout_table (ctx);
- CHECK_ERROR ();
- verify_field_layout_table (ctx);
- CHECK_ERROR ();
- verify_standalonesig_table (ctx);
- CHECK_ERROR ();
- verify_eventmap_table (ctx);
- CHECK_ERROR ();
- verify_event_table (ctx);
- CHECK_ERROR ();
- verify_propertymap_table (ctx);
- CHECK_ERROR ();
- verify_property_table (ctx);
- CHECK_ERROR ();
- verify_methodimpl_table (ctx);
- CHECK_ERROR ();
- verify_moduleref_table (ctx);
- CHECK_ERROR ();
- verify_typespec_table (ctx);
- CHECK_ERROR ();
- verify_implmap_table (ctx);
- CHECK_ERROR ();
- verify_fieldrva_table (ctx);
- CHECK_ERROR ();
- verify_assembly_table (ctx);
- CHECK_ERROR ();
- verify_assemblyref_table (ctx);
- CHECK_ERROR ();
- verify_file_table (ctx);
- CHECK_ERROR ();
- verify_exportedtype_table (ctx);
- CHECK_ERROR ();
- verify_manifest_resource_table (ctx);
- CHECK_ERROR ();
- verify_nested_class_table (ctx);
- CHECK_ERROR ();
- verify_generic_param_table (ctx);
- CHECK_ERROR ();
- 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)
-{
- memset (ctx, 0, sizeof (VerifyContext));
- ctx->image = image;
- ctx->report_error = TRUE;
- ctx->report_warning = FALSE; //export this setting in the API
- ctx->valid = 1;
- ctx->size = image->raw_data_len;
- ctx->data = image->raw_data;
-}
-
-static gboolean
-cleanup_context (VerifyContext *ctx, MonoError *error)
-{
- g_free (ctx->sections);
- if (ctx->errors) {
- MonoVerifyInfo *info = (MonoVerifyInfo *)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, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image) && !mono_verifier_is_enabled_for_pe_only())
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_PE;
-
- verify_msdos_header (&ctx);
- CHECK_STATE();
- verify_pe_header (&ctx);
- CHECK_STATE();
- verify_pe_optional_header (&ctx);
- CHECK_STATE();
- load_section_table (&ctx);
- CHECK_STATE();
- load_data_directories (&ctx);
- CHECK_STATE();
- verify_import_table (&ctx);
- CHECK_STATE();
- /*No need to check the IAT directory entry, it's content is indirectly verified by verify_import_table*/
- verify_resources_table (&ctx);
-
-cleanup:
- return cleanup_context (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_cli_data (MonoImage *image, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_CLI;
-
- verify_cli_header (&ctx);
- CHECK_STATE();
- verify_metadata_header (&ctx);
- CHECK_STATE();
- verify_tables_schema (&ctx);
-
-cleanup:
- return cleanup_context (&ctx, error);
-}
-
-
-/*
- * Verifies basic table constraints such as global table invariants (sorting, field monotonicity, etc).
- * Other verification checks are meant to be done lazily by the runtime. Those include:
- * blob items (signatures, method headers, custom attributes, etc)
- * type semantics related
- * vtable related
- * stuff that should not block other pieces from running such as bad types/methods/fields/etc.
- *
- * The whole idea is that if this succeed the runtime is free to play around safely but any complex
- * operation still need more checking.
- */
-gboolean
-mono_verifier_verify_table_data (MonoImage *image, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- verify_tables_data (&ctx);
-
- return cleanup_context (&ctx, error);
-}
-
-
-/*
- * Verifies all other constraints.
- */
-gboolean
-mono_verifier_verify_full_table_data (MonoImage *image, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- verify_typedef_table_full (&ctx);
- CHECK_STATE ();
- verify_field_table_full (&ctx);
- CHECK_STATE ();
- verify_method_table_full (&ctx);
- CHECK_STATE ();
- verify_memberref_table_full (&ctx);
- CHECK_STATE ();
- verify_cattr_table_full (&ctx);
- CHECK_STATE ();
- verify_field_marshal_table_full (&ctx);
- CHECK_STATE ();
- verify_decl_security_table_full (&ctx);
- CHECK_STATE ();
- verify_standalonesig_table_full (&ctx);
- CHECK_STATE ();
- verify_event_table_full (&ctx);
- CHECK_STATE ();
- 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);
-}
-
-gboolean
-mono_verifier_verify_field_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- is_valid_field_signature (&ctx, offset);
- return cleanup_context (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_method_header (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
- guint32 locals_token;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- 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);
-}
-
-gboolean
-mono_verifier_verify_method_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- 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 (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_memberref_method_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- is_valid_memberref_method_signature (&ctx, offset);
- return cleanup_context (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_memberref_field_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- is_valid_field_signature (&ctx, offset);
- return cleanup_context (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_standalone_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- is_valid_standalonesig_blob (&ctx, offset);
- return cleanup_context (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_typespec_signature (MonoImage *image, guint32 offset, guint32 token, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
- ctx.token = token;
-
- is_valid_typespec_blob (&ctx, offset);
- return cleanup_context (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_methodspec_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- is_valid_methodspec_blob (&ctx, offset);
- return cleanup_context (&ctx, error);
-}
-
-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, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- verify_user_string (&ctx, offset);
-
- return cleanup_context (&ctx, error);
-}
-
-gboolean
-mono_verifier_verify_cattr_blob (MonoImage *image, guint32 offset, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- is_valid_cattr_blob (&ctx, offset);
-
- return cleanup_context (&ctx, error);
-}
-
-
-//FIXME this should raise a System.Reflection.CustomAttributeFormatException
-gboolean
-mono_verifier_verify_cattr_content (MonoImage *image, MonoMethod *ctor, const guchar *data, guint32 size, MonoError *error)
-{
- VerifyContext ctx;
-
- error_init (error);
-
- if (!mono_verifier_is_enabled_for_image (image))
- return TRUE;
-
- init_verify_context (&ctx, image);
- ctx.stage = STAGE_TABLES;
-
- is_valid_cattr_content (&ctx, ctor, (const char*)data, size);
-
- return cleanup_context (&ctx, error);
-}
-
-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_internal (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->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];
-
- 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];
-
- 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 = mono_method_from_method_def_or_ref (image, data [MONO_METHODIMPL_BODY], NULL, error);
- if (!body)
- return FALSE;
-
- declaration = mono_method_from_method_def_or_ref (image, data [MONO_METHODIMPL_DECLARATION], NULL, error);
- if (!declaration)
- 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, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_cli_data (MonoImage *image, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_pe_data (MonoImage *image, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_full_table_data (MonoImage *image, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_field_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_method_header (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_method_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_standalone_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_typespec_signature (MonoImage *image, guint32 offset, guint32 token, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_methodspec_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_string_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_cattr_blob (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_cattr_content (MonoImage *image, MonoMethod *ctor, const guchar *data, guint32 size, MonoError *error)
-{
- error_init (error);
- 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)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_methodimpl_row (MonoImage *image, guint32 row, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_memberref_method_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-gboolean
-mono_verifier_verify_memberref_field_signature (MonoImage *image, guint32 offset, MonoError *error)
-{
- error_init (error);
- return TRUE;
-}
-
-#endif /* DISABLE_VERIFIER */
#include <string.h>
#include <ctype.h>
-static MiniVerifierMode verifier_mode = MONO_VERIFIER_MODE_OFF;
-static gboolean verify_all = FALSE;
-
-/*
- * Set the desired level of checks for the verfier.
- *
- */
-void
-mono_verifier_set_mode (MiniVerifierMode mode)
-{
- verifier_mode = mode;
-}
-
-void
-mono_verifier_enable_verify_all ()
-{
- verify_all = TRUE;
-}
-
/*
* Returns TURE if @type is VAR or MVAR
*/
return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
}
-#ifndef DISABLE_VERIFIER
-/*
- * Pull the list of opcodes
- */
-#define OPDEF(a,b,c,d,e,f,g,h,i,j) \
- a = i,
-
-enum {
-#include "mono/cil/opcode.def"
- LAST = 0xff
-};
-#undef OPDEF
-
-#ifdef MONO_VERIFIER_DEBUG
-#define VERIFIER_DEBUG(code) do { code } while (0)
-#else
-#define VERIFIER_DEBUG(code)
-#endif
-
-//////////////////////////////////////////////////////////////////
-#define IS_STRICT_MODE(ctx) (((ctx)->level & MONO_VERIFY_NON_STRICT) == 0)
-#define IS_FAIL_FAST_MODE(ctx) (((ctx)->level & MONO_VERIFY_FAIL_FAST) == MONO_VERIFY_FAIL_FAST)
-#define IS_SKIP_VISIBILITY(ctx) (((ctx)->level & MONO_VERIFY_SKIP_VISIBILITY) == MONO_VERIFY_SKIP_VISIBILITY)
-#define IS_REPORT_ALL_ERRORS(ctx) (((ctx)->level & MONO_VERIFY_REPORT_ALL_ERRORS) == MONO_VERIFY_REPORT_ALL_ERRORS)
-#define CLEAR_PREFIX(ctx, prefix) do { (ctx)->prefix_set &= ~(prefix); } while (0)
-#define ADD_VERIFY_INFO(__ctx, __msg, __status, __exception) \
- do { \
- MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
- vinfo->info.status = __status; \
- vinfo->info.message = ( __msg ); \
- vinfo->exception_type = (__exception); \
- (__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
- } while (0)
-
-//TODO support MONO_VERIFY_REPORT_ALL_ERRORS
-#define ADD_VERIFY_ERROR(__ctx, __msg) \
- do { \
- ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, MONO_EXCEPTION_INVALID_PROGRAM); \
- (__ctx)->valid = 0; \
- } while (0)
-
-#define CODE_NOT_VERIFIABLE(__ctx, __msg) \
- do { \
- if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
- ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, MONO_EXCEPTION_UNVERIFIABLE_IL); \
- (__ctx)->verifiable = 0; \
- if (IS_FAIL_FAST_MODE (__ctx)) \
- (__ctx)->valid = 0; \
- } \
- } while (0)
-
-#define ADD_VERIFY_ERROR2(__ctx, __msg, __exception) \
- do { \
- ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR, __exception); \
- (__ctx)->valid = 0; \
- } while (0)
-
-#define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
- do { \
- if ((__ctx)->verifiable || IS_REPORT_ALL_ERRORS (__ctx)) { \
- ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE, __exception); \
- (__ctx)->verifiable = 0; \
- if (IS_FAIL_FAST_MODE (__ctx)) \
- (__ctx)->valid = 0; \
- } \
- } while (0)
-
-#define CHECK_ADD4_OVERFLOW_UN(a, b) ((guint32)(0xFFFFFFFFU) - (guint32)(b) < (guint32)(a))
-#define CHECK_ADD8_OVERFLOW_UN(a, b) ((guint64)(0xFFFFFFFFFFFFFFFFUL) - (guint64)(b) < (guint64)(a))
-
-#if SIZEOF_VOID_P == 4
-#define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD4_OVERFLOW_UN(a, b)
-#else
-#define CHECK_ADDP_OVERFLOW_UN(a,b) CHECK_ADD8_OVERFLOW_UN(a, b)
-#endif
-
-#define ADDP_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADDP_OVERFLOW_UN (a, b))
-#define ADD_IS_GREATER_OR_OVF(a, b, c) (((a) + (b) > (c)) || CHECK_ADD4_OVERFLOW_UN (a, b))
-
-/*Flags to be used with ILCodeDesc::flags */
-enum {
- /*Instruction has not been processed.*/
- IL_CODE_FLAG_NOT_PROCESSED = 0,
- /*Instruction was decoded by mono_method_verify loop.*/
- IL_CODE_FLAG_SEEN = 1,
- /*Instruction was target of a branch or is at a protected block boundary.*/
- IL_CODE_FLAG_WAS_TARGET = 2,
- /*Used by stack_init to avoid double initialize each entry.*/
- IL_CODE_FLAG_STACK_INITED = 4,
- /*Used by merge_stacks to decide if it should just copy the eval stack.*/
- IL_CODE_STACK_MERGED = 8,
- /*This instruction is part of the delegate construction sequence, it cannot be target of a branch.*/
- IL_CODE_DELEGATE_SEQUENCE = 0x10,
- /*This is a delegate created from a ldftn to a non final virtual method*/
- IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL = 0x20,
- /*This is a call to a non final virtual method*/
- IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
-};
-
-typedef enum {
- RESULT_VALID,
- RESULT_UNVERIFIABLE,
- RESULT_INVALID
-} verify_result_t;
-
-typedef struct {
- MonoType *type;
- int stype;
- MonoMethod *method;
-} ILStackDesc;
-
-
-typedef struct {
- ILStackDesc *stack;
- guint16 size, max_size;
- guint16 flags;
-} ILCodeDesc;
-
-typedef struct {
- int max_args;
- int max_stack;
- int verifiable;
- int valid;
- int level;
-
- int code_size;
- ILCodeDesc *code;
- ILCodeDesc eval;
-
- MonoType **params;
- GSList *list;
- /*Allocated fnptr MonoType that should be freed by us.*/
- GSList *funptrs;
- /*Type dup'ed exception types from catch blocks.*/
- GSList *exception_types;
-
- int num_locals;
- MonoType **locals;
- char *locals_verification_state;
-
- /*TODO get rid of target here, need_merge in mono_method_verify and hoist the merging code in the branching code*/
- int target;
-
- guint32 ip_offset;
- MonoMethodSignature *signature;
- MonoMethodHeader *header;
-
- MonoGenericContext *generic_context;
- MonoImage *image;
- MonoMethod *method;
-
- /*This flag helps solving a corner case of delegate verification in that you cannot have a "starg 0"
- *on a method that creates a delegate for a non-final virtual method using ldftn*/
- gboolean has_this_store;
-
- /*This flag is used to control if the contructor of the parent class has been called.
- *If the this pointer is pushed on the eval stack and it's a reference type constructor and
- * super_ctor_called is false, the uninitialized flag is set on the pushed value.
- *
- * Poping an uninitialized this ptr from the eval stack is an unverifiable operation unless
- * the safe variant is used. Only a few opcodes can use it : dup, pop, ldfld, stfld and call to a constructor.
- */
- gboolean super_ctor_called;
-
- guint32 prefix_set;
- gboolean has_flags;
- MonoType *constrained_type;
-} VerifyContext;
-
-static void
-merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external);
-
-static int
-get_stack_type (MonoType *type);
-
-static gboolean
-mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn);
-
-static gboolean
-mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass);
-
-static gboolean
-mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method);
-
-static MonoGenericParam*
-verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type);
-
-static gboolean
-verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate);
-//////////////////////////////////////////////////////////////////
-
-
-
-enum {
- TYPE_INV = 0, /* leave at 0. */
- TYPE_I4 = 1,
- TYPE_I8 = 2,
- TYPE_NATIVE_INT = 3,
- TYPE_R8 = 4,
- /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
- TYPE_PTR = 5,
- /* value types and classes */
- TYPE_COMPLEX = 6,
- /* Number of types, used to define the size of the tables*/
- TYPE_MAX = 6,
-
- /* Used by tables to signal that a result is not verifiable*/
- NON_VERIFIABLE_RESULT = 0x80,
-
- /*Mask used to extract just the type, excluding flags */
- TYPE_MASK = 0x0F,
-
- /* The stack type is a managed pointer, unmask the value to res */
- POINTER_MASK = 0x100,
-
- /*Stack type with the pointer mask*/
- RAW_TYPE_MASK = 0x10F,
-
- /* Controlled Mutability Manager Pointer */
- CMMP_MASK = 0x200,
-
- /* The stack type is a null literal*/
- NULL_LITERAL_MASK = 0x400,
-
- /**Used by ldarg.0 and family to let delegate verification happens.*/
- THIS_POINTER_MASK = 0x800,
-
- /**Signals that this is a boxed value type*/
- BOXED_MASK = 0x1000,
-
- /*This is an unitialized this ref*/
- UNINIT_THIS_MASK = 0x2000,
-
- /* This is a safe to return byref */
- SAFE_BYREF_MASK = 0x4000,
-};
-
-static const char* const
-type_names [TYPE_MAX + 1] = {
- "Invalid",
- "Int32",
- "Int64",
- "Native Int",
- "Float64",
- "Native Pointer",
- "Complex"
-};
-
-enum {
- PREFIX_UNALIGNED = 1,
- PREFIX_VOLATILE = 2,
- PREFIX_TAIL = 4,
- PREFIX_CONSTRAINED = 8,
- PREFIX_READONLY = 16
-};
-//////////////////////////////////////////////////////////////////
-
-#ifdef ENABLE_VERIFIER_STATS
-
-#define _MEM_ALLOC(amt) do { allocated_memory += (amt); working_set += (amt); } while (0)
-#define _MEM_FREE(amt) do { working_set -= (amt); } while (0)
-
-static int allocated_memory;
-static int working_set;
-static int max_allocated_memory;
-static int max_working_set;
-static int total_allocated_memory;
-
-static void
-finish_collect_stats (void)
-{
- max_allocated_memory = MAX (max_allocated_memory, allocated_memory);
- max_working_set = MAX (max_working_set, working_set);
- total_allocated_memory += allocated_memory;
- allocated_memory = working_set = 0;
-}
-
-static void
-init_verifier_stats (void)
-{
- static gboolean inited;
- if (!inited) {
- inited = TRUE;
- mono_counters_register ("Maximum memory allocated during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_allocated_memory);
- mono_counters_register ("Maximum memory used during verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &max_working_set);
- mono_counters_register ("Total memory allocated for verification", MONO_COUNTER_METADATA | MONO_COUNTER_INT, &total_allocated_memory);
- }
-}
-
-#else
-
-#define _MEM_ALLOC(amt) do {} while (0)
-#define _MEM_FREE(amt) do { } while (0)
-
-#define finish_collect_stats()
-#define init_verifier_stats()
-
-#endif
-
-
-//////////////////////////////////////////////////////////////////
-
-/*
- * Verify if @token refers to a valid row on int's table.
- */
-static gboolean
-token_bounds_check (MonoImage *image, guint32 token)
-{
- if (image_is_dynamic (image))
- return mono_dynamic_image_is_valid_token ((MonoDynamicImage*)image, token);
- return image->tables [mono_metadata_token_table (token)].rows >= mono_metadata_token_index (token) && mono_metadata_token_index (token) > 0;
-}
-
-static MonoType *
-mono_type_create_fnptr_from_mono_method (VerifyContext *ctx, MonoMethod *method)
-{
- MonoType *res = g_new0 (MonoType, 1);
- _MEM_ALLOC (sizeof (MonoType));
-
- //FIXME use mono_method_get_signature_full
- res->data.method = mono_method_signature_internal (method);
- res->type = MONO_TYPE_FNPTR;
- ctx->funptrs = g_slist_prepend (ctx->funptrs, res);
- return res;
-}
-
-/*
- * mono_type_is_enum_type:
- *
- * Returns: TRUE if @type is an enum type.
- */
-static gboolean
-mono_type_is_enum_type (MonoType *type)
-{
- if (type->type == MONO_TYPE_VALUETYPE && m_class_is_enumtype (type->data.klass))
- return TRUE;
- if (type->type == MONO_TYPE_GENERICINST && m_class_is_enumtype (type->data.generic_class->container_class))
- return TRUE;
- return FALSE;
-}
-
-/*
- * mono_type_is_value_type:
- *
- * Returns: TRUE if @type is named after @namespace.@name.
- *
- */
-static gboolean
-mono_type_is_value_type (MonoType *type, const char *namespace_, const char *name)
-{
- return type->type == MONO_TYPE_VALUETYPE &&
- !strcmp (namespace_, m_class_get_name_space (type->data.klass)) &&
- !strcmp (name, m_class_get_name (type->data.klass));
-}
-
-/*
- * mono_type_get_underlying_type_any:
- *
- * This functions is just like mono_type_get_underlying_type but it doesn't care if the type is byref.
- *
- * Returns the underlying type of @type regardless if it is byref or not.
- */
-static MonoType*
-mono_type_get_underlying_type_any (MonoType *type)
-{
- if (type->type == MONO_TYPE_VALUETYPE && m_class_is_enumtype (type->data.klass))
- return mono_class_enum_basetype_internal (type->data.klass);
- if (type->type == MONO_TYPE_GENERICINST && m_class_is_enumtype (type->data.generic_class->container_class))
- return mono_class_enum_basetype_internal (type->data.generic_class->container_class);
- return type;
-}
-
-static G_GNUC_UNUSED const char*
-mono_type_get_stack_name (MonoType *type)
-{
- return type_names [get_stack_type (type) & TYPE_MASK];
-}
-
-/*
- * Verify if @type is valid for the given @ctx verification context.
- * this function checks for VAR and MVAR types that are invalid under the current verifier,
- */
-static gboolean
-mono_type_is_valid_type_in_context_full (MonoType *type, MonoGenericContext *context, gboolean check_gtd)
-{
- int i;
- MonoGenericInst *inst;
-
- switch (type->type) {
- case MONO_TYPE_VAR:
- case MONO_TYPE_MVAR:
- if (!context)
- return FALSE;
- inst = type->type == MONO_TYPE_VAR ? context->class_inst : context->method_inst;
- if (!inst || mono_type_get_generic_param_num (type) >= inst->type_argc)
- return FALSE;
- break;
- case MONO_TYPE_SZARRAY:
- return mono_type_is_valid_type_in_context_full (m_class_get_byval_arg (type->data.klass), context, check_gtd);
- case MONO_TYPE_ARRAY:
- return mono_type_is_valid_type_in_context_full (m_class_get_byval_arg (type->data.array->eklass), context, check_gtd);
- case MONO_TYPE_PTR:
- return mono_type_is_valid_type_in_context_full (type->data.type, context, check_gtd);
- case MONO_TYPE_GENERICINST:
- inst = type->data.generic_class->context.class_inst;
- if (!inst->is_open)
- break;
- for (i = 0; i < inst->type_argc; ++i)
- if (!mono_type_is_valid_type_in_context_full (inst->type_argv [i], context, check_gtd))
- return FALSE;
- break;
- case MONO_TYPE_CLASS:
- case MONO_TYPE_VALUETYPE: {
- MonoClass *klass = type->data.klass;
- MonoType *klass_byval_arg = m_class_get_byval_arg (klass);
- /*
- * It's possible to encode generic'sh types in such a way that they disguise themselves as class or valuetype.
- * Fixing the type decoding is really tricky since under some cases this behavior is needed, for example, to
- * have a 'class' type pointing to a 'genericinst' class.
- *
- * For the runtime these non canonical (weird) encodings work fine, the worst they can cause is some
- * reflection oddities which are harmless - to security at least.
- */
- if (klass_byval_arg->type != type->type)
- return mono_type_is_valid_type_in_context_full (klass_byval_arg, context, check_gtd);
-
- if (check_gtd && mono_class_is_gtd (klass))
- return FALSE;
- break;
- }
- default:
- break;
- }
- return TRUE;
-}
-
-static gboolean
-mono_type_is_valid_type_in_context (MonoType *type, MonoGenericContext *context)
-{
- return mono_type_is_valid_type_in_context_full (type, context, FALSE);
-}
-
-/*This function returns NULL if the type is not instantiatable*/
-static MonoType*
-verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
-{
- ERROR_DECL (error);
- MonoType *result;
-
- result = mono_class_inflate_generic_type_checked (type, context, error);
- if (!is_ok (error)) {
- mono_error_cleanup (error);
- return NULL;
- }
- return result;
-}
-
-/**
- * mono_generic_param_is_constraint_compatible:
- *
- * \returns TRUE if \p candidate is constraint compatible with \p target.
- *
- * This means that \p candidate constraints are a super set of \p target constaints
- */
-static gboolean
-mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoClass *candidate_param_class, MonoGenericContext *context)
-{
- MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
- MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
- MonoClass **candidate_class;
- gboolean class_constraint_satisfied = FALSE;
- gboolean valuetype_constraint_satisfied = FALSE;
-
- int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
- int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
-
- if (cinfo->constraints) {
- for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
- MonoClass *cc;
- MonoType *inflated = verifier_inflate_type (ctx, m_class_get_byval_arg (*candidate_class), ctx->generic_context);
- if (!inflated)
- return FALSE;
- cc = mono_class_from_mono_type_internal (inflated);
- mono_metadata_free_type (inflated);
-
- if (mono_type_is_reference (m_class_get_byval_arg (cc)) && !MONO_CLASS_IS_INTERFACE_INTERNAL (cc))
- class_constraint_satisfied = TRUE;
- else if (!mono_type_is_reference (m_class_get_byval_arg (cc)) && !MONO_CLASS_IS_INTERFACE_INTERNAL (cc))
- valuetype_constraint_satisfied = TRUE;
- }
- }
- class_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0;
- valuetype_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) != 0;
-
- if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && !class_constraint_satisfied)
- return FALSE;
- if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && !valuetype_constraint_satisfied)
- return FALSE;
- if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !((cmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) ||
- valuetype_constraint_satisfied)) {
- return FALSE;
- }
-
-
- if (tinfo->constraints) {
- MonoClass **target_class;
- for (target_class = tinfo->constraints; *target_class; ++target_class) {
- MonoClass *tc;
- MonoType *inflated = verifier_inflate_type (ctx, m_class_get_byval_arg (*target_class), context);
- if (!inflated)
- return FALSE;
- tc = mono_class_from_mono_type_internal (inflated);
- mono_metadata_free_type (inflated);
-
- /*
- * A constraint from @target might inflate into @candidate itself and in that case we don't need
- * check it's constraints since it satisfy the constraint by itself.
- */
- if (mono_metadata_type_equal (m_class_get_byval_arg (tc), m_class_get_byval_arg (candidate_param_class)))
- continue;
-
- if (!cinfo->constraints)
- return FALSE;
-
- for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
- MonoClass *cc;
- inflated = verifier_inflate_type (ctx, m_class_get_byval_arg (*candidate_class), ctx->generic_context);
- if (!inflated)
- return FALSE;
- cc = mono_class_from_mono_type_internal (inflated);
- mono_metadata_free_type (inflated);
-
- if (verifier_class_is_assignable_from (tc, cc))
- break;
-
- /*
- * This happens when we have the following:
- *
- * Bar<K> where K : IFace
- * Foo<T, U> where T : U where U : IFace
- * ...
- * Bar<T> <- T here satisfy K constraint transitively through to U's constraint
- *
- */
- if (mono_type_is_generic_argument (m_class_get_byval_arg (cc))) {
- MonoGenericParam *other_candidate = verifier_get_generic_param_from_type (ctx, m_class_get_byval_arg (cc));
-
- if (mono_generic_param_is_constraint_compatible (ctx, target, other_candidate, cc, context)) {
- break;
- }
- }
- }
- if (!*candidate_class)
- return FALSE;
- }
- }
- return TRUE;
-}
-
-static MonoGenericParam*
-verifier_get_generic_param_from_type (VerifyContext *ctx, MonoType *type)
-{
- MonoGenericContainer *gc;
- MonoMethod *method = ctx->method;
- int num;
-
- num = mono_type_get_generic_param_num (type);
-
- if (type->type == MONO_TYPE_VAR) {
- MonoClass *gtd = method->klass;
- if (mono_class_is_ginst (gtd))
- gtd = mono_class_get_generic_class (gtd)->container_class;
- gc = mono_class_try_get_generic_container (gtd);
- } else { //MVAR
- MonoMethod *gmd = method;
- if (method->is_inflated)
- gmd = ((MonoMethodInflated*)method)->declaring;
- gc = mono_method_get_generic_container (gmd);
- }
- if (!gc)
- return NULL;
- return mono_generic_container_get_param (gc, num);
-}
-
-
-
-/*
- * Verify if @type is valid for the given @ctx verification context.
- * this function checks for VAR and MVAR types that are invalid under the current verifier,
- * This means that it either
- */
-static gboolean
-is_valid_type_in_context (VerifyContext *ctx, MonoType *type)
-{
- return mono_type_is_valid_type_in_context (type, ctx->generic_context);
-}
-
-static gboolean
-is_valid_generic_instantiation_in_context (VerifyContext *ctx, MonoGenericInst *ginst, gboolean check_gtd)
-{
- int i;
- for (i = 0; i < ginst->type_argc; ++i) {
- MonoType *type = ginst->type_argv [i];
-
- if (!mono_type_is_valid_type_in_context_full (type, ctx->generic_context, TRUE))
- return FALSE;
- }
- return TRUE;
-}
-
-static gboolean
-generic_arguments_respect_constraints (VerifyContext *ctx, MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
-{
- int i;
- for (i = 0; i < ginst->type_argc; ++i) {
- MonoType *type = ginst->type_argv [i];
- MonoGenericParam *target = mono_generic_container_get_param (gc, i);
- MonoGenericParam *candidate;
- MonoClass *candidate_class;
-
- if (!mono_type_is_generic_argument (type))
- continue;
-
- if (!is_valid_type_in_context (ctx, type))
- return FALSE;
-
- candidate = verifier_get_generic_param_from_type (ctx, type);
- candidate_class = mono_class_from_mono_type_internal (type);
-
- if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, candidate_class, context))
- return FALSE;
- }
- return TRUE;
-}
-static gboolean
-mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
+GSList*
+mono_method_verify (MonoMethod *method, int level)
{
- MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
- MonoGenericInst *ginst = gmethod->context.method_inst;
- MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
- return !gc || generic_arguments_respect_constraints (ctx, gc, &gmethod->context, ginst);
+ /* The verifier was disabled at compile time */
+ return NULL;
}
-static gboolean
-mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
+void
+mono_free_verify_list (GSList *list)
{
- MonoGenericClass *gklass = mono_class_get_generic_class (klass);
- MonoGenericInst *ginst = gklass->context.class_inst;
- MonoGenericContainer *gc = mono_class_get_generic_container (gklass->container_class);
- return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
+ /* The verifier was disabled at compile time */
+ /* will always be null if verifier is disabled */
}
-static gboolean
-mono_method_is_valid_generic_instantiation (VerifyContext *ctx, MonoMethod *method)
-{
- MonoMethodInflated *gmethod = (MonoMethodInflated *)method;
- MonoGenericInst *ginst = gmethod->context.method_inst;
- MonoGenericContainer *gc = mono_method_get_generic_container (gmethod->declaring);
- if (!gc) /*non-generic inflated method - it's part of a generic type */
- return TRUE;
- if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
- return FALSE;
- return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
-}
-static gboolean
-mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
+char*
+mono_verify_corlib (void)
{
- MonoGenericClass *gklass = mono_class_get_generic_class (klass);
- MonoGenericInst *ginst = gklass->context.class_inst;
- MonoGenericContainer *gc = mono_class_get_generic_container (gklass->container_class);
- if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst, TRUE))
- return FALSE;
- return is_valid_generic_instantiation (gc, &gklass->context, ginst);
+ return NULL;
}
-
-static gboolean
-mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
-{
- MonoClass *klass;
-
- if (type == NULL) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid null type at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return FALSE;
- }
-
- if (!is_valid_type_in_context (ctx, type)) {
- char *str = mono_type_full_name (type);
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type (%s%s) (argument out of range or %s is not generic) at 0x%04x",
- str [0] == '!' ? "" : type->type == MONO_TYPE_VAR ? "!" : "!!",
- str,
- type->type == MONO_TYPE_VAR ? "class" : "method",
- ctx->ip_offset),
- MONO_EXCEPTION_BAD_IMAGE);
- g_free (str);
- return FALSE;
- }
-
- klass = mono_class_from_mono_type_internal (type);
- mono_class_init_internal (klass);
- if (mono_class_has_failure (klass)) {
- if (mono_class_is_ginst (klass) && !mono_class_is_valid_generic_instantiation (NULL, klass))
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic instantiation of type %s.%s at 0x%04x", m_class_get_name_space (klass), m_class_get_name (klass), ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
- else
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", m_class_get_name_space (klass), m_class_get_name (klass), ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
- return FALSE;
- }
-
- if (mono_class_is_ginst (klass) && mono_class_has_failure (mono_class_get_generic_class (klass)->container_class)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", m_class_get_name_space (klass), m_class_get_name (klass), ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
- return FALSE;
- }
-
- if (!mono_class_is_ginst (klass))
- return TRUE;
-
- if (!mono_class_is_valid_generic_instantiation (ctx, klass)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s at 0x%04x", m_class_get_name_space (klass), m_class_get_name (klass), ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
- return FALSE;
- }
-
- if (!mono_class_repect_method_constraints (ctx, klass)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic type instantiation of type %s.%s (generic args don't respect target's constraints) at 0x%04x", m_class_get_name_space (klass), m_class_get_name (klass), ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
- return FALSE;
- }
-
- return TRUE;
-}
-
-static verify_result_t
-mono_method_is_valid_in_context (VerifyContext *ctx, MonoMethod *method)
-{
- if (!mono_type_is_valid_in_context (ctx, m_class_get_byval_arg (method->klass)))
- return RESULT_INVALID;
-
- if (!method->is_inflated)
- return RESULT_VALID;
-
- if (!mono_method_is_valid_generic_instantiation (ctx, method)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s at 0x%04x", m_class_get_name_space (method->klass), m_class_get_name (method->klass), method->name, ctx->ip_offset), MONO_EXCEPTION_UNVERIFIABLE_IL);
- return RESULT_INVALID;
- }
-
- if (!mono_method_repect_method_constraints (ctx, method)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid generic method instantiation of method %s.%s::%s (generic args don't respect target's constraints) at 0x%04x", m_class_get_name_space (method->klass), m_class_get_name (method->klass), method->name, ctx->ip_offset));
- return RESULT_UNVERIFIABLE;
- }
- return RESULT_VALID;
-}
-
-
-static MonoClassField*
-verifier_load_field (VerifyContext *ctx, int token, MonoClass **out_klass, const char *opcode) {
- ERROR_DECL (error);
- MonoClassField *field;
- MonoClass *klass = NULL;
-
- if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
- field = (MonoClassField *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
- klass = field ? field->parent : NULL;
- } else {
- if (!IS_FIELD_DEF_OR_REF (token) || !token_bounds_check (ctx->image, token)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid field token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return NULL;
- }
-
- field = mono_field_from_token_checked (ctx->image, token, &klass, ctx->generic_context, error);
- mono_error_cleanup (error); /*FIXME don't swallow the error */
- }
-
- if (!field || !field->parent || !klass) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load field from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return NULL;
- }
-
- if (!mono_type_is_valid_in_context (ctx, m_class_get_byval_arg (klass)))
- return NULL;
-
- if (mono_field_get_flags (field) & FIELD_ATTRIBUTE_LITERAL) {
- char *type_name = mono_type_get_full_name (field->parent);
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot reference literal field %s::%s at 0x%04x", type_name, field->name, ctx->ip_offset));
- g_free (type_name);
- return NULL;
- }
-
- *out_klass = klass;
- return field;
-}
-
-static MonoMethod*
-verifier_load_method (VerifyContext *ctx, int token, const char *opcode) {
- MonoMethod* method;
-
-
- if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
- method = (MonoMethod *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
- } else {
- ERROR_DECL (error);
- if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid method token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return NULL;
- }
-
- method = mono_get_method_checked (ctx->image, token, NULL, ctx->generic_context, error);
- mono_error_cleanup (error); /* FIXME don't swallow this error */
- }
-
- if (!method) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load method from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return NULL;
- }
-
- if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
- return NULL;
-
- return method;
-}
-
-static MonoType*
-verifier_load_type (VerifyContext *ctx, int token, const char *opcode) {
- MonoType* type;
-
- if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
- MonoClass *klass = (MonoClass *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
- type = klass ? m_class_get_byval_arg (klass) : NULL;
- } else {
- ERROR_DECL (error);
- if (!IS_TYPE_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid type token 0x%08x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return NULL;
- }
- type = mono_type_get_checked (ctx->image, token, ctx->generic_context, error);
- mono_error_cleanup (error); /*FIXME don't swallow the error */
- }
-
- if (!type) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Cannot load type from token 0x%08x for %s at 0x%04x", token, opcode, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return NULL;
- }
-
- if (!mono_type_is_valid_in_context (ctx, type))
- return NULL;
-
- return type;
-}
-
-
-/* stack_slot_get_type:
- *
- * Returns the stack type of @value. This value includes POINTER_MASK.
- *
- * Use this function to checks that account for a managed pointer.
- */
-static gint32
-stack_slot_get_type (ILStackDesc *value)
-{
- return value->stype & RAW_TYPE_MASK;
-}
-
-/* stack_slot_get_underlying_type:
- *
- * Returns the stack type of @value. This value does not include POINTER_MASK.
- *
- * Use this function is cases where the fact that the value could be a managed pointer is
- * irrelevant. For example, field load doesn't care about this fact of type on stack.
- */
-static gint32
-stack_slot_get_underlying_type (ILStackDesc *value)
-{
- return value->stype & TYPE_MASK;
-}
-
-/* stack_slot_is_managed_pointer:
- *
- * Returns TRUE is @value is a managed pointer.
- */
-static gboolean
-stack_slot_is_managed_pointer (ILStackDesc *value)
-{
- return (value->stype & POINTER_MASK) == POINTER_MASK;
-}
-
-/* stack_slot_is_managed_mutability_pointer:
- *
- * Returns TRUE is @value is a managed mutability pointer.
- */
-static G_GNUC_UNUSED gboolean
-stack_slot_is_managed_mutability_pointer (ILStackDesc *value)
-{
- return (value->stype & CMMP_MASK) == CMMP_MASK;
-}
-
-/* stack_slot_is_null_literal:
- *
- * Returns TRUE is @value is the null literal.
- */
-static gboolean
-stack_slot_is_null_literal (ILStackDesc *value)
-{
- return (value->stype & NULL_LITERAL_MASK) == NULL_LITERAL_MASK;
-}
-
-
-/* stack_slot_is_this_pointer:
- *
- * Returns TRUE is @value is the this literal
- */
-static gboolean
-stack_slot_is_this_pointer (ILStackDesc *value)
-{
- return (value->stype & THIS_POINTER_MASK) == THIS_POINTER_MASK;
-}
-
-/* stack_slot_is_boxed_value:
- *
- * Returns TRUE is @value is a boxed value
- */
-static gboolean
-stack_slot_is_boxed_value (ILStackDesc *value)
-{
- return (value->stype & BOXED_MASK) == BOXED_MASK;
-}
-
-/* stack_slot_is_safe_byref:
- *
- * Returns TRUE is @value is a safe byref
- */
-static gboolean
-stack_slot_is_safe_byref (ILStackDesc *value)
-{
- return (value->stype & SAFE_BYREF_MASK) == SAFE_BYREF_MASK;
-}
-
-static const char *
-stack_slot_get_name (ILStackDesc *value)
-{
- return type_names [value->stype & TYPE_MASK];
-}
-
-enum {
- SAFE_BYREF_LOCAL = 1,
- UNSAFE_BYREF_LOCAL = 2
-};
-static gboolean
-local_is_safe_byref (VerifyContext *ctx, unsigned int arg)
-{
- return ctx->locals_verification_state [arg] == SAFE_BYREF_LOCAL;
-}
-
-static gboolean
-local_is_unsafe_byref (VerifyContext *ctx, unsigned int arg)
-{
- return ctx->locals_verification_state [arg] == UNSAFE_BYREF_LOCAL;
-}
-
-#define APPEND_WITH_PREDICATE(PRED,NAME) do {\
- if (PRED (value)) { \
- if (!first) \
- g_string_append (str, ", "); \
- g_string_append (str, NAME); \
- first = FALSE; \
- } } while (0)
-
-static char*
-stack_slot_stack_type_full_name (ILStackDesc *value)
-{
- GString *str = g_string_new ("");
- char *result;
- gboolean has_pred = FALSE, first = TRUE;
-
- if ((value->stype & TYPE_MASK) != value->stype) {
- g_string_append(str, "[");
- APPEND_WITH_PREDICATE (stack_slot_is_this_pointer, "this");
- APPEND_WITH_PREDICATE (stack_slot_is_boxed_value, "boxed");
- APPEND_WITH_PREDICATE (stack_slot_is_null_literal, "null");
- APPEND_WITH_PREDICATE (stack_slot_is_managed_mutability_pointer, "cmmp");
- APPEND_WITH_PREDICATE (stack_slot_is_managed_pointer, "mp");
- APPEND_WITH_PREDICATE (stack_slot_is_safe_byref, "safe-byref");
- has_pred = TRUE;
- }
-
- if (mono_type_is_generic_argument (value->type) && !stack_slot_is_boxed_value (value)) {
- if (!has_pred)
- g_string_append(str, "[");
- if (!first)
- g_string_append (str, ", ");
- g_string_append (str, "unboxed");
- has_pred = TRUE;
- }
-
- if (has_pred)
- g_string_append(str, "] ");
-
- g_string_append (str, stack_slot_get_name (value));
- result = str->str;
- g_string_free (str, FALSE);
- return result;
-}
-
-static char*
-stack_slot_full_name (ILStackDesc *value)
-{
- char *type_name = mono_type_full_name (value->type);
- char *stack_name = stack_slot_stack_type_full_name (value);
- char *res = g_strdup_printf ("%s (%s)", type_name, stack_name);
- g_free (type_name);
- g_free (stack_name);
- return res;
-}
-
-//////////////////////////////////////////////////////////////////
-
-/**
- * mono_free_verify_list:
- */
-void
-mono_free_verify_list (GSList *list)
-{
- MonoVerifyInfoExtended *info;
- GSList *tmp;
-
- for (tmp = list; tmp; tmp = tmp->next) {
- info = (MonoVerifyInfoExtended *)tmp->data;
- g_free (info->info.message);
- g_free (info);
- }
- g_slist_free (list);
-}
-
-#define ADD_ERROR(list,msg) \
- do { \
- MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
- vinfo->info.status = MONO_VERIFY_ERROR; \
- vinfo->info.message = (msg); \
- (list) = g_slist_prepend ((list), vinfo); \
- } while (0)
-
-#define ADD_WARN(list,code,msg) \
- do { \
- MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
- vinfo->info.status = (code); \
- vinfo->info.message = (msg); \
- (list) = g_slist_prepend ((list), vinfo); \
- } while (0)
-
-#define ADD_INVALID(list,msg) \
- do { \
- MonoVerifyInfoExtended *vinfo = g_new (MonoVerifyInfoExtended, 1); \
- vinfo->status = MONO_VERIFY_ERROR; \
- vinfo->message = (msg); \
- (list) = g_slist_prepend ((list), vinfo); \
- /*G_BREAKPOINT ();*/ \
- goto invalid_cil; \
- } while (0)
-
-#define CHECK_STACK_UNDERFLOW(num) \
- do { \
- if (cur_stack < (num)) \
- ADD_INVALID (list, g_strdup_printf ("Stack underflow at 0x%04x (%d items instead of %d)", ip_offset, cur_stack, (num))); \
- } while (0)
-
-#define CHECK_STACK_OVERFLOW() \
- do { \
- if (cur_stack >= max_stack) \
- ADD_INVALID (list, g_strdup_printf ("Maxstack exceeded at 0x%04x", ip_offset)); \
- } while (0)
-
-
-static int
-in_any_block (MonoMethodHeader *header, guint offset)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < header->num_clauses; ++i) {
- clause = &header->clauses [i];
- if (MONO_OFFSET_IN_CLAUSE (clause, offset))
- return 1;
- if (MONO_OFFSET_IN_HANDLER (clause, offset))
- return 1;
- if (MONO_OFFSET_IN_FILTER (clause, offset))
- return 1;
- }
- return 0;
-}
-
-/*
- * in_any_exception_block:
- *
- * Returns TRUE is @offset is part of any exception clause (filter, handler, catch, finally or fault).
- */
-static gboolean
-in_any_exception_block (MonoMethodHeader *header, guint offset)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < header->num_clauses; ++i) {
- clause = &header->clauses [i];
- if (MONO_OFFSET_IN_HANDLER (clause, offset))
- return TRUE;
- if (MONO_OFFSET_IN_FILTER (clause, offset))
- return TRUE;
- }
- return FALSE;
-}
-
-/*
- * is_valid_branch_instruction:
- *
- * Verify if it's valid to perform a branch from @offset to @target.
- * This should be used with br and brtrue/false.
- * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
- * The major difference from other similiar functions is that branching into a
- * finally/fault block is invalid instead of just unverifiable.
- */
-static int
-is_valid_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < header->num_clauses; ++i) {
- clause = &header->clauses [i];
- /*branching into a finally block is invalid*/
- if ((clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY || clause->flags == MONO_EXCEPTION_CLAUSE_FAULT) &&
- !MONO_OFFSET_IN_HANDLER (clause, offset) &&
- MONO_OFFSET_IN_HANDLER (clause, target))
- return 2;
-
- if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
- return 1;
- if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
- return 1;
- if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
- return 1;
- }
- return 0;
-}
-
-/*
- * is_valid_cmp_branch_instruction:
- *
- * Verify if it's valid to perform a branch from @offset to @target.
- * This should be used with binary comparison branching instruction, like beq, bge and similars.
- * It returns 0 if valid, 1 for unverifiable and 2 for invalid.
- *
- * The major differences from other similar functions are that most errors lead to invalid
- * code and only branching out of finally, filter or fault clauses is unverifiable.
- */
-static int
-is_valid_cmp_branch_instruction (MonoMethodHeader *header, guint offset, guint target)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < header->num_clauses; ++i) {
- clause = &header->clauses [i];
- /*branching out of a handler or finally*/
- if (clause->flags != MONO_EXCEPTION_CLAUSE_NONE &&
- MONO_OFFSET_IN_HANDLER (clause, offset) &&
- !MONO_OFFSET_IN_HANDLER (clause, target))
- return 1;
-
- if (clause->try_offset != target && (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target)))
- return 2;
- if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
- return 2;
- if (MONO_OFFSET_IN_FILTER (clause, offset) ^ MONO_OFFSET_IN_FILTER (clause, target))
- return 2;
- }
- return 0;
-}
-
-/*
- * A leave can't escape a finally block
- */
-static int
-is_correct_leave (MonoMethodHeader *header, guint offset, guint target)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < header->num_clauses; ++i) {
- clause = &header->clauses [i];
- if (clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY && MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
- return 0;
- if (MONO_OFFSET_IN_FILTER (clause, offset))
- return 0;
- }
- return 1;
-}
-
-/*
- * A rethrow can't happen outside of a catch handler.
- */
-static int
-is_correct_rethrow (MonoMethodHeader *header, guint offset)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < header->num_clauses; ++i) {
- clause = &header->clauses [i];
- if (MONO_OFFSET_IN_HANDLER (clause, offset))
- return 1;
- }
- return 0;
-}
-
-/*
- * An endfinally can't happen outside of a finally/fault handler.
- */
-static int
-is_correct_endfinally (MonoMethodHeader *header, guint offset)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < header->num_clauses; ++i) {
- clause = &header->clauses [i];
- if (MONO_OFFSET_IN_HANDLER (clause, offset) && (clause->flags == MONO_EXCEPTION_CLAUSE_FAULT || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY))
- return 1;
- }
- return 0;
-}
-
-
-/*
- * An endfilter can only happens inside a filter clause.
- * In non-strict mode filter is allowed inside the handler clause too
- */
-static MonoExceptionClause *
-is_correct_endfilter (VerifyContext *ctx, guint offset)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < ctx->header->num_clauses; ++i) {
- clause = &ctx->header->clauses [i];
- if (clause->flags != MONO_EXCEPTION_CLAUSE_FILTER)
- continue;
- if (MONO_OFFSET_IN_FILTER (clause, offset))
- return clause;
- if (!IS_STRICT_MODE (ctx) && MONO_OFFSET_IN_HANDLER (clause, offset))
- return clause;
- }
- return NULL;
-}
-
-
-/*
- * Non-strict endfilter can happens inside a try block or any handler block
- */
-static int
-is_unverifiable_endfilter (VerifyContext *ctx, guint offset)
-{
- int i;
- MonoExceptionClause *clause;
-
- for (i = 0; i < ctx->header->num_clauses; ++i) {
- clause = &ctx->header->clauses [i];
- if (MONO_OFFSET_IN_CLAUSE (clause, offset))
- return 1;
- }
- return 0;
-}
-
-static gboolean
-is_valid_bool_arg (ILStackDesc *arg)
-{
- if (stack_slot_is_managed_pointer (arg) || stack_slot_is_boxed_value (arg) || stack_slot_is_null_literal (arg))
- return TRUE;
-
-
- switch (stack_slot_get_underlying_type (arg)) {
- case TYPE_I4:
- case TYPE_I8:
- case TYPE_NATIVE_INT:
- case TYPE_PTR:
- return TRUE;
- case TYPE_COMPLEX:
- g_assert (arg->type);
- switch (arg->type->type) {
- case MONO_TYPE_CLASS:
- case MONO_TYPE_STRING:
- case MONO_TYPE_OBJECT:
- case MONO_TYPE_SZARRAY:
- case MONO_TYPE_ARRAY:
- case MONO_TYPE_FNPTR:
- case MONO_TYPE_PTR:
- return TRUE;
- case MONO_TYPE_GENERICINST:
- /*We need to check if the container class
- * of the generic type is a valuetype, iow:
- * is it a "class Foo<T>" or a "struct Foo<T>"?
- */
- return !m_class_is_valuetype (arg->type->data.generic_class->container_class);
- default:
- return FALSE;
- }
- default:
- return FALSE;
- }
-}
-
-
-/*Type manipulation helper*/
-
-/*Returns the byref version of the supplied MonoType*/
-static MonoType*
-mono_type_get_type_byref (MonoType *type)
-{
- if (type->byref)
- return type;
- return m_class_get_this_arg (mono_class_from_mono_type_internal (type));
-}
-
-
-/*Returns the byval version of the supplied MonoType*/
-static MonoType*
-mono_type_get_type_byval (MonoType *type)
-{
- if (!type->byref)
- return type;
- return m_class_get_byval_arg (mono_class_from_mono_type_internal (type));
-}
-
-static MonoType*
-mono_type_from_stack_slot (ILStackDesc *slot)
-{
- if (stack_slot_is_managed_pointer (slot))
- return mono_type_get_type_byref (slot->type);
- return slot->type;
-}
-
-/*Stack manipulation code*/
-
-static void
-ensure_stack_size (ILCodeDesc *stack, int required)
-{
- int new_size = 8;
- ILStackDesc *tmp;
-
- if (required < stack->max_size)
- return;
-
- /* We don't have to worry about the exponential growth since stack_copy prune unused space */
- new_size = MAX (8, MAX (required, stack->max_size * 2));
-
- g_assert (new_size >= stack->size);
- g_assert (new_size >= required);
-
- tmp = g_new0 (ILStackDesc, new_size);
- _MEM_ALLOC (sizeof (ILStackDesc) * new_size);
-
- if (stack->stack) {
- if (stack->size)
- memcpy (tmp, stack->stack, stack->size * sizeof (ILStackDesc));
- g_free (stack->stack);
- _MEM_FREE (sizeof (ILStackDesc) * stack->max_size);
- }
-
- stack->stack = tmp;
- stack->max_size = new_size;
-}
-
-static void
-stack_init (VerifyContext *ctx, ILCodeDesc *state)
-{
- if (state->flags & IL_CODE_FLAG_STACK_INITED)
- return;
- state->size = state->max_size = 0;
- state->flags |= IL_CODE_FLAG_STACK_INITED;
-}
-
-static void
-stack_copy (ILCodeDesc *to, ILCodeDesc *from)
-{
- ensure_stack_size (to, from->size);
- to->size = from->size;
-
- /*stack copy happens at merge points, which have small stacks*/
- if (from->size)
- memcpy (to->stack, from->stack, sizeof (ILStackDesc) * from->size);
-}
-
-static void
-copy_stack_value (ILStackDesc *to, ILStackDesc *from)
-{
- to->stype = from->stype;
- to->type = from->type;
- to->method = from->method;
-}
-
-static int
-check_underflow (VerifyContext *ctx, int size)
-{
- if (ctx->eval.size < size) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack underflow, required %d, but have %d at 0x%04x", size, ctx->eval.size, ctx->ip_offset));
- return 0;
- }
- return 1;
-}
-
-static int
-check_overflow (VerifyContext *ctx)
-{
- if (ctx->eval.size >= ctx->max_stack) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have stack-depth %d at 0x%04x", ctx->eval.size + 1, ctx->ip_offset));
- return 0;
- }
- return 1;
-}
-
-/*This reject out PTR, FNPTR and TYPEDBYREF*/
-static gboolean
-check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
-{
- if (stack_slot_get_type (value) == TYPE_PTR) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
- return 0;
- }
- return 1;
-}
-
-/*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
-static gboolean
-check_unverifiable_type (VerifyContext *ctx, MonoType *type)
-{
- if (type->type == MONO_TYPE_PTR || type->type == MONO_TYPE_FNPTR) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unmanaged pointer is not a verifiable type at 0x%04x", ctx->ip_offset));
- return 0;
- }
- return 1;
-}
-
-static ILStackDesc *
-stack_push (VerifyContext *ctx)
-{
- g_assert (ctx->eval.size < ctx->max_stack);
- g_assert (ctx->eval.size <= ctx->eval.max_size);
-
- ensure_stack_size (&ctx->eval, ctx->eval.size + 1);
-
- return & ctx->eval.stack [ctx->eval.size++];
-}
-
-static ILStackDesc *
-stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
-{
- ILStackDesc *top = stack_push (ctx);
- top->stype = stype;
- top->type = type;
- return top;
-}
-
-static ILStackDesc *
-stack_pop (VerifyContext *ctx)
-{
- ILStackDesc *ret;
- g_assert (ctx->eval.size > 0);
- ret = ctx->eval.stack + --ctx->eval.size;
- if ((ret->stype & UNINIT_THIS_MASK) == UNINIT_THIS_MASK)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Found use of uninitialized 'this ptr' ref at 0x%04x", ctx->ip_offset));
- return ret;
-}
-
-/* This function allows to safely pop an unititialized this ptr from
- * the eval stack without marking the method as unverifiable.
- */
-static ILStackDesc *
-stack_pop_safe (VerifyContext *ctx)
-{
- g_assert (ctx->eval.size > 0);
- return ctx->eval.stack + --ctx->eval.size;
-}
-
-/*Positive number distance from stack top. [0] is stack top, [1] is the one below*/
-static ILStackDesc*
-stack_peek (VerifyContext *ctx, int distance)
-{
- g_assert (ctx->eval.size - distance > 0);
- return ctx->eval.stack + (ctx->eval.size - 1 - distance);
-}
-
-static ILStackDesc *
-stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
-{
- ILStackDesc *top = stack_push (ctx);
- copy_stack_value (top, value);
- return top;
-}
-
-/* Returns the MonoType associated with the token, or NULL if it is invalid.
- *
- * A boxable type can be either a reference or value type, but cannot be a byref type or an unmanaged pointer
- * */
-static MonoType*
-get_boxable_mono_type (VerifyContext* ctx, int token, const char *opcode)
-{
- MonoType *type;
- MonoClass *klass;
-
- if (!(type = verifier_load_type (ctx, token, opcode)))
- return NULL;
-
- if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type for %s at 0x%04x", opcode, ctx->ip_offset));
- return NULL;
- }
-
- if (type->type == MONO_TYPE_VOID) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type for %s at 0x%04x", opcode, ctx->ip_offset));
- return NULL;
- }
-
- if (type->type == MONO_TYPE_TYPEDBYREF)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref for %s at 0x%04x", opcode, ctx->ip_offset));
-
- if (!(klass = mono_class_from_mono_type_internal (type)))
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not retrieve type token for %s at 0x%04x", opcode, ctx->ip_offset));
-
- if (mono_class_is_gtd (klass) && type->type != MONO_TYPE_GENERICINST)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the generic type definition in a boxable type position for %s at 0x%04x", opcode, ctx->ip_offset));
-
- check_unverifiable_type (ctx, type);
- return type;
-}
-
-
-/*operation result tables */
-
-static const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
- {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
- {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
- {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
-};
-
-static const unsigned char add_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
- {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char sub_ovf_un_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-static const unsigned char bin_ovf_table [TYPE_MAX][TYPE_MAX] = {
- {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
- {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
-};
-
-#ifdef MONO_VERIFIER_DEBUG
-
-/*debug helpers */
-static void
-dump_stack_value (ILStackDesc *value)
-{
- printf ("[(%x)(%x)", value->type->type, value->stype);
-
- if (stack_slot_is_this_pointer (value))
- printf ("[this] ");
-
- if (stack_slot_is_boxed_value (value))
- printf ("[boxed] ");
-
- if (stack_slot_is_null_literal (value))
- printf ("[null] ");
-
- if (stack_slot_is_managed_mutability_pointer (value))
- printf ("Controled Mutability MP: ");
-
- if (stack_slot_is_managed_pointer (value))
- printf ("Managed Pointer to: ");
-
- if (stack_slot_is_safe_byref (value))
- printf ("Safe ByRef to: ");
-
- switch (stack_slot_get_underlying_type (value)) {
- case TYPE_INV:
- printf ("invalid type]");
- return;
- case TYPE_I4:
- printf ("int32]");
- return;
- case TYPE_I8:
- printf ("int64]");
- return;
- case TYPE_NATIVE_INT:
- printf ("native int]");
- return;
- case TYPE_R8:
- printf ("float64]");
- return;
- case TYPE_PTR:
- printf ("unmanaged pointer]");
- return;
- case TYPE_COMPLEX:
- switch (value->type->type) {
- case MONO_TYPE_CLASS:
- case MONO_TYPE_VALUETYPE:
- printf ("complex] (%s)", value->type->data.klass->name);
- return;
- case MONO_TYPE_STRING:
- printf ("complex] (string)");
- return;
- case MONO_TYPE_OBJECT:
- printf ("complex] (object)");
- return;
- case MONO_TYPE_SZARRAY:
- printf ("complex] (%s [])", value->type->data.klass->name);
- return;
- case MONO_TYPE_ARRAY:
- printf ("complex] (%s [%d %d %d])",
- value->type->data.array->eklass->name,
- value->type->data.array->rank,
- value->type->data.array->numsizes,
- value->type->data.array->numlobounds);
- return;
- case MONO_TYPE_GENERICINST:
- printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
- return;
- case MONO_TYPE_VAR:
- printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_name (value->type->data.generic_param));
- return;
- case MONO_TYPE_MVAR:
- printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_name (value->type->data.generic_param));
- return;
- default: {
- //should be a boxed value
- char * name = mono_type_full_name (value->type);
- printf ("complex] %s", name);
- g_free (name);
- return;
- }
- }
- default:
- printf ("unknown stack %x type]\n", value->stype);
- g_assert_not_reached ();
- }
-}
-
-static void
-dump_stack_state (ILCodeDesc *state)
-{
- int i;
-
- printf ("(%d) ", state->size);
- for (i = 0; i < state->size; ++i)
- dump_stack_value (state->stack + i);
- printf ("\n");
-}
-#endif
-
-/**
- * is_array_type_compatible:
- *
- * Returns TRUE if candidate array type can be assigned to target.
- *
- * Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
- */
-static gboolean
-is_array_type_compatible (MonoType *target, MonoType *candidate)
-{
- MonoArrayType *left = target->data.array;
- MonoArrayType *right = candidate->data.array;
-
- g_assert (target->type == MONO_TYPE_ARRAY);
- g_assert (candidate->type == MONO_TYPE_ARRAY);
-
- if (left->rank != right->rank)
- return FALSE;
-
- return verifier_class_is_assignable_from (left->eklass, right->eklass);
-}
-
-static int
-get_stack_type (MonoType *type)
-{
- int mask = 0;
- int type_kind = type->type;
- if (type->byref)
- mask = POINTER_MASK;
- /*TODO handle CMMP_MASK */
-
-handle_enum:
- switch (type_kind) {
- case MONO_TYPE_I1:
- case MONO_TYPE_U1:
- case MONO_TYPE_BOOLEAN:
- case MONO_TYPE_I2:
- case MONO_TYPE_U2:
- case MONO_TYPE_CHAR:
- case MONO_TYPE_I4:
- case MONO_TYPE_U4:
- return TYPE_I4 | mask;
-
- case MONO_TYPE_I:
- case MONO_TYPE_U:
- return TYPE_NATIVE_INT | mask;
-
- /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
- case MONO_TYPE_FNPTR:
- case MONO_TYPE_PTR:
- case MONO_TYPE_TYPEDBYREF:
- return TYPE_PTR | mask;
-
- case MONO_TYPE_VAR:
- case MONO_TYPE_MVAR:
-
- case MONO_TYPE_CLASS:
- case MONO_TYPE_STRING:
- case MONO_TYPE_OBJECT:
- case MONO_TYPE_SZARRAY:
- case MONO_TYPE_ARRAY:
- return TYPE_COMPLEX | mask;
-
- case MONO_TYPE_I8:
- case MONO_TYPE_U8:
- return TYPE_I8 | mask;
-
- case MONO_TYPE_R4:
- case MONO_TYPE_R8:
- return TYPE_R8 | mask;
-
- case MONO_TYPE_GENERICINST:
- case MONO_TYPE_VALUETYPE:
- if (mono_type_is_enum_type (type)) {
- type = mono_type_get_underlying_type_any (type);
- if (!type)
- return FALSE;
- type_kind = type->type;
- goto handle_enum;
- } else {
- return TYPE_COMPLEX | mask;
- }
-
- default:
- return TYPE_INV;
- }
-}
-
-/* convert MonoType to ILStackDesc format (stype) */
-static gboolean
-set_stack_value (VerifyContext *ctx, ILStackDesc *stack, MonoType *type, int take_addr)
-{
- int mask = 0;
- int type_kind = type->type;
-
- if (type->byref || take_addr)
- mask = POINTER_MASK;
- /* TODO handle CMMP_MASK */
-
-handle_enum:
- stack->type = type;
-
- switch (type_kind) {
- case MONO_TYPE_I1:
- case MONO_TYPE_U1:
- case MONO_TYPE_BOOLEAN:
- case MONO_TYPE_I2:
- case MONO_TYPE_U2:
- case MONO_TYPE_CHAR:
- case MONO_TYPE_I4:
- case MONO_TYPE_U4:
- stack->stype = TYPE_I4 | mask;
- break;
- case MONO_TYPE_I:
- case MONO_TYPE_U:
- stack->stype = TYPE_NATIVE_INT | mask;
- break;
-
- /*FIXME: Do we need to check if it's a pointer to the method pointer? The spec says it' illegal to have that.*/
- case MONO_TYPE_FNPTR:
- case MONO_TYPE_PTR:
- case MONO_TYPE_TYPEDBYREF:
- stack->stype = TYPE_PTR | mask;
- break;
-
- case MONO_TYPE_CLASS:
- case MONO_TYPE_STRING:
- case MONO_TYPE_OBJECT:
- case MONO_TYPE_SZARRAY:
- case MONO_TYPE_ARRAY:
-
- case MONO_TYPE_VAR:
- case MONO_TYPE_MVAR:
- stack->stype = TYPE_COMPLEX | mask;
- break;
-
- case MONO_TYPE_I8:
- case MONO_TYPE_U8:
- stack->stype = TYPE_I8 | mask;
- break;
- case MONO_TYPE_R4:
- case MONO_TYPE_R8:
- stack->stype = TYPE_R8 | mask;
- break;
- case MONO_TYPE_GENERICINST:
- case MONO_TYPE_VALUETYPE:
- if (mono_type_is_enum_type (type)) {
- MonoType *utype = mono_type_get_underlying_type_any (type);
- if (!utype) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve underlying type of %x at %d", type->type, ctx->ip_offset));
- return FALSE;
- }
- type = utype;
- type_kind = type->type;
- goto handle_enum;
- } else {
- stack->stype = TYPE_COMPLEX | mask;
- break;
- }
- default:
- VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Illegal value set on stack 0x%02x at %d", type->type, ctx->ip_offset));
- return FALSE;
- }
- return TRUE;
-}
-
-/*
- * init_stack_with_value_at_exception_boundary:
- *
- * Initialize the stack and push a given type.
- * The instruction is marked as been on the exception boundary.
- */
-static void
-init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
-{
- ERROR_DECL (error);
- MonoType *type = mono_class_inflate_generic_type_checked (m_class_get_byval_arg (klass), ctx->generic_context, error);
-
- if (!is_ok (error)) {
- char *name = mono_type_get_full_name (klass);
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid class %s used for exception", name));
- g_free (name);
- mono_error_cleanup (error);
- return;
- }
-
- if (!ctx->max_stack) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack overflow at 0x%04x", ctx->ip_offset));
- return;
- }
-
- stack_init (ctx, code);
- ensure_stack_size (code, 1);
- set_stack_value (ctx, code->stack, type, FALSE);
- ctx->exception_types = g_slist_prepend (ctx->exception_types, type);
- code->size = 1;
- code->flags |= IL_CODE_FLAG_WAS_TARGET;
- if (mono_type_is_generic_argument (type))
- code->stack->stype |= BOXED_MASK;
-}
-/* Class lazy loading functions */
-static GENERATE_GET_CLASS_WITH_CACHE (ienumerable, "System.Collections.Generic", "IEnumerable`1")
-static GENERATE_GET_CLASS_WITH_CACHE (icollection, "System.Collections.Generic", "ICollection`1")
-static GENERATE_GET_CLASS_WITH_CACHE (ireadonly_list, "System.Collections.Generic", "IReadOnlyList`1")
-static GENERATE_GET_CLASS_WITH_CACHE (ireadonly_collection, "System.Collections.Generic", "IReadOnlyCollection`1")
-
-
-static MonoClass*
-get_ienumerable_class (void)
-{
- return mono_class_get_ienumerable_class ();
-}
-
-static MonoClass*
-get_icollection_class (void)
-{
- return mono_class_get_icollection_class ();
-}
-
-static MonoClass*
-get_ireadonlylist_class (void)
-{
- return mono_class_get_ireadonly_list_class ();
-}
-
-static MonoClass*
-get_ireadonlycollection_class (void)
-{
- return mono_class_get_ireadonly_collection_class ();
-}
-
-static MonoClass*
-inflate_class_one_arg (MonoClass *gtype, MonoClass *arg0)
-{
- MonoType *args [1];
- args [0] = m_class_get_byval_arg (arg0);
-
- return mono_class_bind_generic_parameters (gtype, 1, args, FALSE);
-}
-
-static gboolean
-verifier_inflate_and_check_compat (MonoClass *target, MonoClass *gtd, MonoClass *arg)
-{
- MonoClass *tmp;
- if (!(tmp = inflate_class_one_arg (gtd, arg)))
- return FALSE;
- if (mono_class_is_variant_compatible (target, tmp, TRUE))
- return TRUE;
- return FALSE;
-}
-
-static gboolean
-verifier_class_is_assignable_from (MonoClass *target, MonoClass *candidate)
-{
- MonoClass *iface_gtd;
-
- if (target == candidate)
- return TRUE;
-
- if (mono_class_has_variant_generic_params (target)) {
- if (MONO_CLASS_IS_INTERFACE_INTERNAL (target)) {
- if (MONO_CLASS_IS_INTERFACE_INTERNAL (candidate) && mono_class_is_variant_compatible (target, candidate, TRUE))
- return TRUE;
-
- if (m_class_get_rank (candidate) == 1) {
- MonoClass *candidate_element_class = m_class_get_element_class (candidate);
- if (verifier_inflate_and_check_compat (target, mono_defaults.generic_ilist_class, candidate_element_class))
- return TRUE;
- if (verifier_inflate_and_check_compat (target, get_icollection_class (), candidate_element_class))
- return TRUE;
- if (verifier_inflate_and_check_compat (target, get_ienumerable_class (), candidate_element_class))
- return TRUE;
- if (verifier_inflate_and_check_compat (target, get_ireadonlylist_class (), candidate_element_class))
- return TRUE;
- if (verifier_inflate_and_check_compat (target, get_ireadonlycollection_class (), candidate_element_class))
- return TRUE;
- } else {
- ERROR_DECL (error);
- int i;
- while (candidate && candidate != mono_defaults.object_class) {
- mono_class_setup_interfaces (candidate, error);
- if (!is_ok (error)) {
- mono_error_cleanup (error);
- return FALSE;
- }
-
- /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
- guint16 candidate_interface_offsets_count = m_class_get_interface_offsets_count (candidate);
- MonoClass **candidate_interfaces_packed = m_class_get_interfaces_packed (candidate);
- for (i = 0; i < candidate_interface_offsets_count; ++i) {
- MonoClass *iface = candidate_interfaces_packed [i];
- if (mono_class_is_variant_compatible (target, iface, TRUE))
- return TRUE;
- }
-
- guint16 candidate_interface_count = m_class_get_interface_count (candidate);
- MonoClass **candidate_interfaces = m_class_get_interfaces (candidate);
- for (i = 0; i < candidate_interface_count; ++i) {
- MonoClass *iface = candidate_interfaces [i];
- if (mono_class_is_variant_compatible (target, iface, TRUE))
- return TRUE;
- }
- candidate = m_class_get_parent (candidate);
- }
- }
- } else if (m_class_is_delegate (target)) {
- if (mono_class_is_variant_compatible (target, candidate, TRUE))
- return TRUE;
- }
- return FALSE;
- }
-
- if (mono_class_is_assignable_from_internal (target, candidate))
- return TRUE;
-
- if (!MONO_CLASS_IS_INTERFACE_INTERNAL (target) || !mono_class_is_ginst (target) || m_class_get_rank (candidate) != 1)
- return FALSE;
-
- iface_gtd = mono_class_get_generic_class (target)->container_class;
- if (iface_gtd != mono_defaults.generic_ilist_class && iface_gtd != get_icollection_class () && iface_gtd != get_ienumerable_class ())
- return FALSE;
-
- target = mono_class_from_mono_type_internal (mono_class_get_generic_class (target)->context.class_inst->type_argv [0]);
- candidate = m_class_get_element_class (candidate);
-
- return TRUE;
-}
-
-/*Verify if type 'candidate' can be stored in type 'target'.
- *
- * If strict, check for the underlying type and not the verification stack types
- */
-static gboolean
-verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
-{
-#define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
-#define IS_ONE_OF2(T, A, B) (T == A || T == B)
-
- MonoType *original_candidate = candidate;
- VERIFIER_DEBUG ( printf ("checking type compatibility %s x %s strict %d\n", mono_type_full_name (target), mono_type_full_name (candidate), strict); );
-
- /*only one is byref */
- if (candidate->byref ^ target->byref) {
- /* converting from native int to byref*/
- if (get_stack_type (candidate) == TYPE_NATIVE_INT && target->byref) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("using byref native int at 0x%04x", ctx->ip_offset));
- return TRUE;
- }
- return FALSE;
- }
- strict |= target->byref;
- /*From now on we don't care about byref anymore, so it's ok to discard it here*/
- candidate = mono_type_get_underlying_type_any (candidate);
-
-handle_enum:
- switch (target->type) {
- case MONO_TYPE_VOID:
- return candidate->type == MONO_TYPE_VOID;
- case MONO_TYPE_I1:
- case MONO_TYPE_U1:
- case MONO_TYPE_BOOLEAN:
- if (strict)
- return IS_ONE_OF3 (candidate->type, MONO_TYPE_I1, MONO_TYPE_U1, MONO_TYPE_BOOLEAN);
- case MONO_TYPE_I2:
- case MONO_TYPE_U2:
- case MONO_TYPE_CHAR:
- if (strict)
- return IS_ONE_OF3 (candidate->type, MONO_TYPE_I2, MONO_TYPE_U2, MONO_TYPE_CHAR);
- case MONO_TYPE_I4:
- case MONO_TYPE_U4: {
- gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
- gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
- if (strict)
- return is_native_int || is_int4;
- return is_native_int || get_stack_type (candidate) == TYPE_I4;
- }
-
- case MONO_TYPE_I8:
- case MONO_TYPE_U8:
- return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
-
- case MONO_TYPE_R4:
- case MONO_TYPE_R8:
- if (strict)
- return candidate->type == target->type;
- return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
-
- case MONO_TYPE_I:
- case MONO_TYPE_U: {
- gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
- gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
- if (strict)
- return is_native_int || is_int4;
- return is_native_int || get_stack_type (candidate) == TYPE_I4;
- }
-
- case MONO_TYPE_PTR:
- if (candidate->type != MONO_TYPE_PTR)
- return FALSE;
- /* check the underlying type */
- return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
-
- case MONO_TYPE_FNPTR: {
- MonoMethodSignature *left, *right;
- if (candidate->type != MONO_TYPE_FNPTR)
- return FALSE;
-
- left = mono_type_get_signature_internal (target);
- right = mono_type_get_signature_internal (candidate);
- return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
- }
-
- case MONO_TYPE_GENERICINST: {
- MonoClass *target_klass;
- MonoClass *candidate_klass;
- if (mono_type_is_enum_type (target)) {
- target = mono_type_get_underlying_type_any (target);
- if (!target)
- return FALSE;
- goto handle_enum;
- }
- /*
- * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
- * to take boxing status into account.
- */
- if (mono_type_is_generic_argument (original_candidate))
- return FALSE;
-
- target_klass = mono_class_from_mono_type_internal (target);
- candidate_klass = mono_class_from_mono_type_internal (candidate);
- if (mono_class_is_nullable (target_klass)) {
- if (!mono_class_is_nullable (candidate_klass))
- return FALSE;
- return target_klass == candidate_klass;
- }
- return verifier_class_is_assignable_from (target_klass, candidate_klass);
- }
-
- case MONO_TYPE_STRING:
- return candidate->type == MONO_TYPE_STRING;
-
- case MONO_TYPE_CLASS:
- /*
- * VAR / MVAR compatibility must be checked by verify_stack_type_compatibility
- * to take boxing status into account.
- */
- if (mono_type_is_generic_argument (original_candidate))
- return FALSE;
-
- if (candidate->type == MONO_TYPE_VALUETYPE)
- return FALSE;
-
- /* If candidate is an enum it should return true for System.Enum and supertypes.
- * That's why here we use the original type and not the underlying type.
- */
- return verifier_class_is_assignable_from (target->data.klass, mono_class_from_mono_type_internal (original_candidate));
-
- case MONO_TYPE_OBJECT:
- return MONO_TYPE_IS_REFERENCE (candidate);
-
- case MONO_TYPE_SZARRAY: {
- MonoClass *left;
- MonoClass *right;
- if (candidate->type != MONO_TYPE_SZARRAY)
- return FALSE;
-
- left = mono_class_from_mono_type_internal (target);
- right = mono_class_from_mono_type_internal (candidate);
-
- return verifier_class_is_assignable_from (left, right);
- }
-
- case MONO_TYPE_ARRAY:
- if (candidate->type != MONO_TYPE_ARRAY)
- return FALSE;
- return is_array_type_compatible (target, candidate);
-
- case MONO_TYPE_TYPEDBYREF:
- return candidate->type == MONO_TYPE_TYPEDBYREF;
-
- case MONO_TYPE_VALUETYPE: {
- MonoClass *target_klass;
- MonoClass *candidate_klass;
-
- if (candidate->type == MONO_TYPE_CLASS)
- return FALSE;
-
- target_klass = mono_class_from_mono_type_internal (target);
- candidate_klass = mono_class_from_mono_type_internal (candidate);
- if (target_klass == candidate_klass)
- return TRUE;
- if (mono_type_is_enum_type (target)) {
- target = mono_type_get_underlying_type_any (target);
- if (!target)
- return FALSE;
- goto handle_enum;
- }
- return FALSE;
- }
-
- case MONO_TYPE_VAR:
- if (candidate->type != MONO_TYPE_VAR)
- return FALSE;
- return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
-
- case MONO_TYPE_MVAR:
- if (candidate->type != MONO_TYPE_MVAR)
- return FALSE;
- return mono_type_get_generic_param_num (candidate) == mono_type_get_generic_param_num (target);
-
- default:
- VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
- g_assert_not_reached ();
- return FALSE;
- }
- return 1;
-#undef IS_ONE_OF3
-#undef IS_ONE_OF2
-}
-
-static gboolean
-verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
-{
- return verify_type_compatibility_full (ctx, target, candidate, FALSE);
-}
-
-/*
- * Returns the generic param bound to the context been verified.
- *
- */
-static MonoGenericParam*
-get_generic_param (VerifyContext *ctx, MonoType *param)
-{
- guint16 param_num = mono_type_get_generic_param_num (param);
- if (param->type == MONO_TYPE_VAR) {
- if (!ctx->generic_context->class_inst || ctx->generic_context->class_inst->type_argc <= param_num) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic type argument %d", param_num));
- return NULL;
- }
- return ctx->generic_context->class_inst->type_argv [param_num]->data.generic_param;
- }
-
- /*param must be a MVAR */
- if (!ctx->generic_context->method_inst || ctx->generic_context->method_inst->type_argc <= param_num) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid generic method argument %d", param_num));
- return NULL;
- }
- return ctx->generic_context->method_inst->type_argv [param_num]->data.generic_param;
-
-}
-
-static gboolean
-recursive_boxed_constraint_type_check (VerifyContext *ctx, MonoType *type, MonoClass *constraint_class, int recursion_level)
-{
- MonoType *constraint_type = m_class_get_byval_arg (constraint_class);
- if (recursion_level <= 0)
- return FALSE;
-
- if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (constraint_type), FALSE))
- return TRUE;
-
- if (mono_type_is_generic_argument (constraint_type)) {
- MonoGenericParam *param = get_generic_param (ctx, constraint_type);
- MonoClass **klass;
- if (!param)
- return FALSE;
- for (klass = mono_generic_param_info (param)->constraints; klass && *klass; ++klass) {
- if (recursive_boxed_constraint_type_check (ctx, type, *klass, recursion_level - 1))
- return TRUE;
- }
- }
- return FALSE;
-}
-
-/**
- * is_compatible_boxed_valuetype:
- *
- * Returns: TRUE if @candidate / @stack is a valid boxed valuetype.
- *
- * @type The source type. It it tested to be of the proper type.
- * @candidate type of the boxed valuetype.
- * @stack stack slot of the boxed valuetype, separate from @candidade since one could be changed before calling this function
- * @strict if TRUE candidate must be boxed compatible to the target type
- *
- */
-static gboolean
-is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
-{
- if (!stack_slot_is_boxed_value (stack))
- return FALSE;
- if (type->byref || candidate->byref)
- return FALSE;
-
- if (mono_type_is_generic_argument (candidate)) {
- MonoGenericParam *param = get_generic_param (ctx, candidate);
- MonoClass **klass;
- if (!param)
- return FALSE;
-
- for (klass = mono_generic_param_info (param)->constraints; klass && *klass; ++klass) {
- /*256 should be enough since there can't be more than 255 generic arguments.*/
- if (recursive_boxed_constraint_type_check (ctx, type, *klass, 256))
- return TRUE;
- }
- }
-
- if (mono_type_is_generic_argument (type))
- return FALSE;
-
- if (!strict)
- return TRUE;
-
- return MONO_TYPE_IS_REFERENCE (type) && verifier_class_is_assignable_from (mono_class_from_mono_type_internal (type), mono_class_from_mono_type_internal (candidate));
-}
-
-static int
-verify_stack_type_compatibility_full (VerifyContext *ctx, MonoType *type, ILStackDesc *stack, gboolean drop_byref, gboolean valuetype_must_be_boxed)
-{
- MonoType *candidate = mono_type_from_stack_slot (stack);
- if (MONO_TYPE_IS_REFERENCE (type) && !type->byref && stack_slot_is_null_literal (stack))
- return TRUE;
-
- if (is_compatible_boxed_valuetype (ctx, type, candidate, stack, TRUE))
- return TRUE;
-
- if (valuetype_must_be_boxed && !stack_slot_is_boxed_value (stack) && !MONO_TYPE_IS_REFERENCE (candidate))
- return FALSE;
-
- if (!valuetype_must_be_boxed && stack_slot_is_boxed_value (stack))
- return FALSE;
-
- if (drop_byref)
- return verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (candidate), FALSE);
-
- /* Handle how Roslyn emit fixed statements by encoding it as byref */
- if (type->byref && candidate->byref && (type->type == MONO_TYPE_I) && !mono_type_is_reference (candidate)) {
- if (!IS_STRICT_MODE (ctx))
- return TRUE;
- }
-
- return verify_type_compatibility_full (ctx, type, candidate, FALSE);
-}
-
-static int
-verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
-{
- return verify_stack_type_compatibility_full (ctx, type, stack, FALSE, FALSE);
-}
-
-static gboolean
-mono_delegate_type_equal (MonoType *target, MonoType *candidate)
-{
- if (candidate->byref ^ target->byref)
- return FALSE;
-
- switch (target->type) {
- case MONO_TYPE_VOID:
- case MONO_TYPE_I1:
- case MONO_TYPE_U1:
- case MONO_TYPE_BOOLEAN:
- case MONO_TYPE_I2:
- case MONO_TYPE_U2:
- case MONO_TYPE_CHAR:
- case MONO_TYPE_I4:
- case MONO_TYPE_U4:
- case MONO_TYPE_I8:
- case MONO_TYPE_U8:
- case MONO_TYPE_R4:
- case MONO_TYPE_R8:
- case MONO_TYPE_I:
- case MONO_TYPE_U:
- case MONO_TYPE_STRING:
- case MONO_TYPE_TYPEDBYREF:
- return candidate->type == target->type;
-
- case MONO_TYPE_PTR:
- if (candidate->type != MONO_TYPE_PTR)
- return FALSE;
- return mono_delegate_type_equal (target->data.type, candidate->data.type);
-
- case MONO_TYPE_FNPTR:
- if (candidate->type != MONO_TYPE_FNPTR)
- return FALSE;
- return mono_delegate_signature_equal (mono_type_get_signature_internal (target), mono_type_get_signature_internal (candidate), FALSE);
-
- case MONO_TYPE_GENERICINST: {
- MonoClass *target_klass;
- MonoClass *candidate_klass;
- target_klass = mono_class_from_mono_type_internal (target);
- candidate_klass = mono_class_from_mono_type_internal (candidate);
- /*FIXME handle nullables and enum*/
- return verifier_class_is_assignable_from (target_klass, candidate_klass);
- }
- case MONO_TYPE_OBJECT:
- return MONO_TYPE_IS_REFERENCE (candidate);
-
- case MONO_TYPE_CLASS:
- return verifier_class_is_assignable_from(target->data.klass, mono_class_from_mono_type_internal (candidate));
-
- case MONO_TYPE_SZARRAY:
- if (candidate->type != MONO_TYPE_SZARRAY)
- return FALSE;
- return verifier_class_is_assignable_from (m_class_get_element_class (mono_class_from_mono_type_internal (target)), m_class_get_element_class (mono_class_from_mono_type_internal (candidate)));
-
- case MONO_TYPE_ARRAY:
- if (candidate->type != MONO_TYPE_ARRAY)
- return FALSE;
- return is_array_type_compatible (target, candidate);
-
- case MONO_TYPE_VALUETYPE:
- /*FIXME handle nullables and enum*/
- return mono_class_from_mono_type_internal (candidate) == mono_class_from_mono_type_internal (target);
-
- case MONO_TYPE_VAR:
- return candidate->type == MONO_TYPE_VAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
- return FALSE;
-
- case MONO_TYPE_MVAR:
- return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
- return FALSE;
-
- default:
- VERIFIER_DEBUG ( printf ("Unknown type %d. Implement me!\n", target->type); );
- g_assert_not_reached ();
- return FALSE;
- }
-}
-
-static gboolean
-mono_delegate_param_equal (MonoType *delegate, MonoType *method)
-{
- if (mono_metadata_type_equal_full (delegate, method, TRUE))
- return TRUE;
-
- return mono_delegate_type_equal (method, delegate);
-}
-
-static gboolean
-mono_delegate_ret_equal (MonoType *delegate, MonoType *method)
-{
- if (mono_metadata_type_equal_full (delegate, method, TRUE))
- return TRUE;
-
- return mono_delegate_type_equal (delegate, method);
-}
-
-/*
- * mono_delegate_signature_equal:
- *
- * Compare two signatures in the way expected by delegates.
- *
- * This function only exists due to the fact that it should ignore the 'has_this' part of the signature.
- *
- * FIXME can this function be eliminated and proper metadata functionality be used?
- */
-static gboolean
-mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
-{
- int i;
- int method_offset = is_static_ldftn ? 1 : 0;
-
- if (delegate_sig->param_count + method_offset != method_sig->param_count)
- return FALSE;
-
- if (delegate_sig->call_convention != method_sig->call_convention)
- return FALSE;
-
- for (i = 0; i < delegate_sig->param_count; i++) {
- MonoType *p1 = delegate_sig->params [i];
- MonoType *p2 = method_sig->params [i + method_offset];
-
- if (!mono_delegate_param_equal (p1, p2))
- return FALSE;
- }
-
- if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
- return FALSE;
-
- return TRUE;
-}
-
-gboolean
-mono_verifier_is_signature_compatible (MonoMethodSignature *target, MonoMethodSignature *candidate)
-{
- return mono_delegate_signature_equal (target, candidate, FALSE);
-}
-
-/*
- * verify_ldftn_delegate:
- *
- * Verify properties of ldftn based delegates.
- */
-static void
-verify_ldftn_delegate (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
-{
- MonoMethod *method = funptr->method;
-
- /*ldftn non-final virtuals only allowed if method is not static,
- * the object is a this arg (comes from a ldarg.0), and there is no starg.0.
- * This rules doesn't apply if the object on stack is a boxed valuetype.
- */
- if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !mono_class_is_sealed (method->klass) && !stack_slot_is_boxed_value (value)) {
- /*A stdarg 0 must not happen, we fail here only in fail fast mode to avoid double error reports*/
- if (IS_FAIL_FAST_MODE (ctx) && ctx->has_this_store)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", ctx->ip_offset));
-
- /*current method must not be static*/
- if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid ldftn with virtual function at 0x%04x", ctx->ip_offset));
-
- /*value is the this pointer, loaded using ldarg.0 */
- if (!stack_slot_is_this_pointer (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object argument, it is not the this pointer, to ldftn with virtual method at 0x%04x", ctx->ip_offset));
-
- ctx->code [ctx->ip_offset].flags |= IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL;
- }
-}
-
-/*
- * verify_delegate_compatibility:
- *
- * Verify delegate creation sequence.
- *
- */
-static void
-verify_delegate_compatibility (VerifyContext *ctx, MonoClass *delegate, ILStackDesc *value, ILStackDesc *funptr)
-{
-#define IS_VALID_OPCODE(offset, opcode) (ip [ip_offset - offset] == opcode && (ctx->code [ip_offset - offset].flags & IL_CODE_FLAG_SEEN))
-#define IS_LOAD_FUN_PTR(kind) (IS_VALID_OPCODE (6, CEE_PREFIX1) && ip [ip_offset - 5] == kind)
-
- MonoMethod *invoke, *method;
- const guint8 *ip = ctx->header->code;
- guint32 ip_offset = ctx->ip_offset;
- gboolean is_static_ldftn = FALSE, is_first_arg_bound = FALSE;
-
- if (stack_slot_get_type (funptr) != TYPE_PTR || !funptr->method) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid function pointer parameter for delegate constructor at 0x%04x", ctx->ip_offset));
- return;
- }
-
- invoke = mono_get_delegate_invoke_internal (delegate);
- method = funptr->method;
-
- if (!method || !mono_method_signature_internal (method)) {
- char *name = mono_type_get_full_name (delegate);
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid method on stack to create delegate %s construction at 0x%04x", name, ctx->ip_offset));
- g_free (name);
- return;
- }
-
- if (!invoke || !mono_method_signature_internal (invoke)) {
- char *name = mono_type_get_full_name (delegate);
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Delegate type %s with bad Invoke method at 0x%04x", name, ctx->ip_offset));
- g_free (name);
- return;
- }
-
- is_static_ldftn = (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) && method->flags & METHOD_ATTRIBUTE_STATIC;
-
- if (is_static_ldftn)
- is_first_arg_bound = mono_method_signature_internal (invoke)->param_count + 1 == mono_method_signature_internal (method)->param_count;
-
- if (!mono_delegate_signature_equal (mono_method_signature_internal (invoke), mono_method_signature_internal (method), is_first_arg_bound)) {
- char *fun_sig = mono_signature_get_desc (mono_method_signature_internal (method), FALSE);
- char *invoke_sig = mono_signature_get_desc (mono_method_signature_internal (invoke), FALSE);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer signature '%s' doesn't match delegate's signature '%s' at 0x%04x", fun_sig, invoke_sig, ctx->ip_offset));
- g_free (fun_sig);
- g_free (invoke_sig);
- }
-
- /*
- * Delegate code sequences:
- * [-6] ldftn token
- * newobj ...
- *
- *
- * [-7] dup
- * [-6] ldvirtftn token
- * newobj ...
- *
- * ldftn sequence:*/
- if (ip_offset > 5 && IS_LOAD_FUN_PTR (CEE_LDFTN)) {
- verify_ldftn_delegate (ctx, delegate, value, funptr);
- } else if (ip_offset > 6 && IS_VALID_OPCODE (7, CEE_DUP) && IS_LOAD_FUN_PTR (CEE_LDVIRTFTN)) {
- ctx->code [ip_offset - 6].flags |= IL_CODE_DELEGATE_SEQUENCE;
- }else {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid code sequence for delegate creation at 0x%04x", ctx->ip_offset));
- }
- ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
-
- //general tests
- if (is_first_arg_bound) {
- if (mono_method_signature_internal (method)->param_count == 0 || !verify_stack_type_compatibility_full (ctx, mono_method_signature_internal (method)->params [0], value, FALSE, TRUE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
- } else {
- if (method->flags & METHOD_ATTRIBUTE_STATIC) {
- if (!stack_slot_is_null_literal (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Non-null this args used with static function for delegate creation at 0x%04x", ctx->ip_offset));
- } else {
- if (!verify_stack_type_compatibility_full (ctx, m_class_get_byval_arg (method->klass), value, FALSE, TRUE) && !stack_slot_is_null_literal (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("This object not compatible with function pointer for delegate creation at 0x%04x", ctx->ip_offset));
- }
- }
-
- if (stack_slot_get_type (value) != TYPE_COMPLEX)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid first parameter for delegate creation at 0x%04x", ctx->ip_offset));
-
-#undef IS_VALID_OPCODE
-#undef IS_LOAD_FUN_PTR
-}
-
-static gboolean
-is_this_arg_of_struct_instance_method (unsigned int arg, VerifyContext *ctx)
-{
- if (arg != 0)
- return FALSE;
- if (ctx->method->flags & METHOD_ATTRIBUTE_STATIC)
- return FALSE;
- if (!m_class_is_valuetype (ctx->method->klass))
- return FALSE;
- return TRUE;
-}
-
-/* implement the opcode checks*/
-static void
-push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
-{
- ILStackDesc *top;
-
- if (arg >= ctx->max_args) {
- if (take_addr)
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
- else {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
- if (check_overflow (ctx)) //FIXME: what sane value could we ever push?
- stack_push_val (ctx, TYPE_I4, mono_get_int32_type ());
- }
- } else if (check_overflow (ctx)) {
- /*We must let the value be pushed, otherwise we would get an underflow error*/
- check_unverifiable_type (ctx, ctx->params [arg]);
- if (ctx->params [arg]->byref && take_addr)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
- top = stack_push (ctx);
- if (!set_stack_value (ctx, top, ctx->params [arg], take_addr))
- return;
-
- if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC)) {
- if (take_addr)
- ctx->has_this_store = TRUE;
- else
- top->stype |= THIS_POINTER_MASK;
- if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !m_class_is_valuetype (ctx->method->klass))
- top->stype |= UNINIT_THIS_MASK;
- }
- if (!take_addr && ctx->params [arg]->byref && !is_this_arg_of_struct_instance_method (arg, ctx))
- top->stype |= SAFE_BYREF_MASK;
- }
-}
-
-static void
-push_local (VerifyContext *ctx, guint32 arg, int take_addr)
-{
- if (arg >= ctx->num_locals) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
- } else if (check_overflow (ctx)) {
- /*We must let the value be pushed, otherwise we would get an underflow error*/
- check_unverifiable_type (ctx, ctx->locals [arg]);
- if (ctx->locals [arg]->byref && take_addr)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
-
- ILStackDesc *value = stack_push (ctx);
- set_stack_value (ctx, value, ctx->locals [arg], take_addr);
- if (local_is_safe_byref (ctx, arg))
- value->stype |= SAFE_BYREF_MASK;
- }
-}
-
-static void
-store_arg (VerifyContext *ctx, guint32 arg)
-{
- ILStackDesc *value;
-
- if (arg >= ctx->max_args) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
- if (check_underflow (ctx, 1))
- stack_pop (ctx);
- return;
- }
-
- if (check_underflow (ctx, 1)) {
- value = stack_pop (ctx);
- if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
- }
- }
- if (arg == 0 && !(ctx->method->flags & METHOD_ATTRIBUTE_STATIC))
- ctx->has_this_store = 1;
-}
-
-static void
-store_local (VerifyContext *ctx, guint32 arg)
-{
- ILStackDesc *value;
- if (arg >= ctx->num_locals) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
- return;
- }
-
- /*TODO verify definite assigment */
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
- if (ctx->locals [arg]->byref) {
- if (stack_slot_is_managed_mutability_pointer (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly managed reference when storing on a local variable at 0x%04x", ctx->ip_offset));
-
- if (local_is_safe_byref (ctx, arg) && !stack_slot_is_safe_byref (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot store an unsafe ret byref to a local that was previously stored a save ret byref value at 0x%04x", ctx->ip_offset));
-
- if (stack_slot_is_safe_byref (value) && !local_is_unsafe_byref (ctx, arg))
- ctx->locals_verification_state [arg] |= SAFE_BYREF_LOCAL;
-
- if (!stack_slot_is_safe_byref (value))
- ctx->locals_verification_state [arg] |= UNSAFE_BYREF_LOCAL;
-
- }
- if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
- char *expected = mono_type_full_name (ctx->locals [arg]);
- char *found = stack_slot_full_name (value);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type '%s' on stack cannot be stored to local %d with type '%s' at 0x%04x",
- found,
- arg,
- expected,
- ctx->ip_offset));
- g_free (expected);
- g_free (found);
- }
-}
-
-/*FIXME add and sub needs special care here*/
-static void
-do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
-{
- ILStackDesc *a, *b, *top;
- int idxa, idxb, complexMerge = 0;
- unsigned char res;
-
- if (!check_underflow (ctx, 2))
- return;
- b = stack_pop (ctx);
- a = stack_pop (ctx);
-
- idxa = stack_slot_get_underlying_type (a);
- if (stack_slot_is_managed_pointer (a)) {
- idxa = TYPE_PTR;
- complexMerge = 1;
- }
-
- idxb = stack_slot_get_underlying_type (b);
- if (stack_slot_is_managed_pointer (b)) {
- idxb = TYPE_PTR;
- complexMerge = 2;
- }
-
- --idxa;
- --idxb;
- res = table [idxa][idxb];
-
- VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
- VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
-
- top = stack_push (ctx);
- if (res == TYPE_INV) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction applyed to ill formed stack (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
- copy_stack_value (top, a);
- return;
- }
-
- if (res & NON_VERIFIABLE_RESULT) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)", stack_slot_get_name (a), stack_slot_get_name (b)));
-
- res = res & ~NON_VERIFIABLE_RESULT;
- }
-
- if (complexMerge && res == TYPE_PTR) {
- if (complexMerge == 1)
- copy_stack_value (top, a);
- else if (complexMerge == 2)
- copy_stack_value (top, b);
- /*
- * There is no need to merge the type of two pointers.
- * The only valid operation is subtraction, that returns a native
- * int as result and can be used with any 2 pointer kinds.
- * This is valid acording to Patition III 1.1.4
- */
- } else
- top->stype = res;
-
-}
-
-
-static void
-do_boolean_branch_op (VerifyContext *ctx, int delta)
-{
- int target = ctx->ip_offset + delta;
- ILStackDesc *top;
-
- VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
-
- if (target < 0 || target >= ctx->code_size) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
- return;
- }
-
- switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
- case 1:
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
- break;
- case 2:
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
- return;
- }
-
- ctx->target = target;
-
- if (!check_underflow (ctx, 1))
- return;
-
- top = stack_pop (ctx);
- if (!is_valid_bool_arg (top))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
-
- check_unmanaged_pointer (ctx, top);
-}
-
-static gboolean
-stack_slot_is_complex_type_not_reference_type (ILStackDesc *slot)
-{
- return stack_slot_get_type (slot) == TYPE_COMPLEX && !MONO_TYPE_IS_REFERENCE (slot->type) && !stack_slot_is_boxed_value (slot);
-}
-
-static gboolean
-stack_slot_is_reference_value (ILStackDesc *slot)
-{
- return stack_slot_get_type (slot) == TYPE_COMPLEX && (MONO_TYPE_IS_REFERENCE (slot->type) || stack_slot_is_boxed_value (slot));
-}
-
-static void
-do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
-{
- ILStackDesc *a, *b;
- int idxa, idxb;
- unsigned char res;
- int target = ctx->ip_offset + delta;
-
- VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
-
- if (target < 0 || target >= ctx->code_size) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
- return;
- }
-
- switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
- case 1: /*FIXME use constants and not magic numbers.*/
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
- break;
- case 2:
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
- return;
- }
-
- ctx->target = target;
-
- if (!check_underflow (ctx, 2))
- return;
-
- b = stack_pop (ctx);
- a = stack_pop (ctx);
-
- idxa = stack_slot_get_underlying_type (a);
- if (stack_slot_is_managed_pointer (a))
- idxa = TYPE_PTR;
-
- idxb = stack_slot_get_underlying_type (b);
- if (stack_slot_is_managed_pointer (b))
- idxb = TYPE_PTR;
-
- if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
- res = TYPE_INV;
- } else {
- --idxa;
- --idxb;
- res = table [idxa][idxb];
- }
-
- VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
- VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
-
- if (res == TYPE_INV) {
- CODE_NOT_VERIFIABLE (ctx,
- g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
- } else if (res & NON_VERIFIABLE_RESULT) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
- res = res & ~NON_VERIFIABLE_RESULT;
- }
-}
-
-static void
-do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX], guint32 opcode)
-{
- ILStackDesc *a, *b;
- int idxa, idxb;
- unsigned char res;
-
- if (!check_underflow (ctx, 2))
- return;
- b = stack_pop (ctx);
- a = stack_pop (ctx);
-
- if (opcode == CEE_CGT_UN) {
- if ((stack_slot_is_reference_value (a) && stack_slot_is_null_literal (b)) ||
- (stack_slot_is_reference_value (b) && stack_slot_is_null_literal (a))) {
- stack_push_val (ctx, TYPE_I4, mono_get_int32_type ());
- return;
- }
- }
-
- idxa = stack_slot_get_underlying_type (a);
- if (stack_slot_is_managed_pointer (a))
- idxa = TYPE_PTR;
-
- idxb = stack_slot_get_underlying_type (b);
- if (stack_slot_is_managed_pointer (b))
- idxb = TYPE_PTR;
-
- if (stack_slot_is_complex_type_not_reference_type (a) || stack_slot_is_complex_type_not_reference_type (b)) {
- res = TYPE_INV;
- } else {
- --idxa;
- --idxb;
- res = table [idxa][idxb];
- }
-
- if(res == TYPE_INV) {
- char *left_type = stack_slot_full_name (a);
- char *right_type = stack_slot_full_name (b);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", left_type, right_type, ctx->ip_offset));
- g_free (left_type);
- g_free (right_type);
- } else if (res & NON_VERIFIABLE_RESULT) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x", stack_slot_get_name (a), stack_slot_get_name (b), ctx->ip_offset));
- res = res & ~NON_VERIFIABLE_RESULT;
- }
- stack_push_val (ctx, TYPE_I4, mono_get_int32_type ());
-}
-
-static void
-do_ret (VerifyContext *ctx)
-{
- MonoType *ret = ctx->signature->ret;
- VERIFIER_DEBUG ( printf ("checking ret\n"); );
- if (ret->type != MONO_TYPE_VOID) {
- ILStackDesc *top;
- if (!check_underflow (ctx, 1))
- return;
-
- top = stack_pop(ctx);
-
- if (!verify_stack_type_compatibility (ctx, ctx->signature->ret, top)) {
- char *ret_type = mono_type_full_name (ctx->signature->ret);
- char *stack_type = stack_slot_full_name (top);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature, expected '%s' but got '%s' at 0x%04x", ret_type, stack_type, ctx->ip_offset));
- g_free (stack_type);
- g_free (ret_type);
- return;
- }
-
- if (ret->byref && !stack_slot_is_safe_byref (top))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref and return value is not a safe-to-return-byref at 0x%04x", ctx->ip_offset));
-
- if (ret->type == MONO_TYPE_TYPEDBYREF || mono_type_is_value_type (ret, "System", "ArgIterator") || mono_type_is_value_type (ret, "System", "RuntimeArgumentHandle"))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns byref, TypedReference, ArgIterator or RuntimeArgumentHandle at 0x%04x", ctx->ip_offset));
- }
-
- if (ctx->eval.size > 0) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
- }
- if (in_any_block (ctx->header, ctx->ip_offset))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
-}
-
-/*
- * FIXME we need to fix the case of a non-virtual instance method defined in the parent but call using a token pointing to a subclass.
- * This is illegal but mono_get_method_full decoded it.
- * TODO handle calling .ctor outside one or calling the .ctor for other class but super
- */
-static void
-do_invoke_method (VerifyContext *ctx, int method_token, gboolean virtual_)
-{
- ERROR_DECL (error);
- int param_count, i;
- MonoMethodSignature *sig;
- ILStackDesc *value;
- MonoMethod *method;
- gboolean virt_check_this = FALSE;
- gboolean constrained = ctx->prefix_set & PREFIX_CONSTRAINED;
-
- if (!(method = verifier_load_method (ctx, method_token, virtual_ ? "callvirt" : "call")))
- return;
-
- if (virtual_) {
- CLEAR_PREFIX (ctx, PREFIX_CONSTRAINED);
-
- if (m_class_is_valuetype (method->klass)) // && !constrained ???
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with valuetype method at 0x%04x", ctx->ip_offset));
-
- if ((method->flags & METHOD_ATTRIBUTE_STATIC))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use callvirtual with static method at 0x%04x", ctx->ip_offset));
-
- } else {
- if (method->flags & METHOD_ATTRIBUTE_ABSTRACT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use call with an abstract method at 0x%04x", ctx->ip_offset));
-
- if ((method->flags & METHOD_ATTRIBUTE_VIRTUAL) && !(method->flags & METHOD_ATTRIBUTE_FINAL) && !mono_class_is_sealed (method->klass)) {
- virt_check_this = TRUE;
- ctx->code [ctx->ip_offset].flags |= IL_CODE_CALL_NONFINAL_VIRTUAL;
- }
- }
-
- if (!(sig = mono_method_get_signature_checked (method, ctx->image, method_token, ctx->generic_context, error))) {
- mono_error_cleanup (error);
- sig = mono_method_get_signature_checked (method, ctx->image, method_token, NULL, error);
- }
-
- if (!sig) {
- char *name = mono_type_get_full_name (method->klass);
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not resolve signature of %s:%s at 0x%04x due to: %s", name, method->name, ctx->ip_offset, mono_error_get_message (error)));
- mono_error_cleanup (error);
- g_free (name);
- return;
- }
-
- param_count = sig->param_count + sig->hasthis;
- if (!check_underflow (ctx, param_count))
- return;
-
- gboolean is_safe_byref_call = TRUE;
-
- for (i = sig->param_count - 1; i >= 0; --i) {
- VERIFIER_DEBUG ( printf ("verifying argument %d\n", i); );
- value = stack_pop (ctx);
- if (!verify_stack_type_compatibility (ctx, sig->params[i], value)) {
- char *stack_name = stack_slot_full_name (value);
- char *sig_name = mono_type_full_name (sig->params [i]);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter with function signature: Calling method with signature (%s) but for argument %d there is a (%s) on stack at 0x%04x", sig_name, i, stack_name, ctx->ip_offset));
- g_free (stack_name);
- g_free (sig_name);
- }
-
- if (stack_slot_is_managed_mutability_pointer (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of %s at 0x%04x", virtual_ ? "callvirt" : "call", ctx->ip_offset));
-
- if ((ctx->prefix_set & PREFIX_TAIL) && stack_slot_is_managed_pointer (value)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot pass a byref argument to a tail %s at 0x%04x", virtual_ ? "callvirt" : "call", ctx->ip_offset));
- return;
- }
- if (stack_slot_is_managed_pointer (value) && !stack_slot_is_safe_byref (value))
- is_safe_byref_call = FALSE;
- }
-
- if (sig->hasthis) {
- MonoType *type = m_class_get_byval_arg (method->klass);
- ILStackDesc copy;
-
- if (mono_method_is_constructor (method) && !m_class_is_valuetype (method->klass)) {
- if (IS_STRICT_MODE (ctx) && !mono_method_is_constructor (ctx->method))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor outside one at 0x%04x", ctx->ip_offset));
- if (IS_STRICT_MODE (ctx) && method->klass != m_class_get_parent (ctx->method->klass) && method->klass != ctx->method->klass)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor of a type different from this or super at 0x%04x", ctx->ip_offset));
-
- ctx->super_ctor_called = TRUE;
- value = stack_pop_safe (ctx);
- if (IS_STRICT_MODE (ctx) && (value->stype & THIS_POINTER_MASK) != THIS_POINTER_MASK)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid 'this ptr' argument for constructor at 0x%04x", ctx->ip_offset));
- if (!(value->stype & UNINIT_THIS_MASK))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Calling the base constructor on an initialized this pointer at 0x%04x", ctx->ip_offset));
- } else {
- value = stack_pop (ctx);
- }
-
- copy_stack_value (©, value);
- //TODO we should extract this to a 'drop_byref_argument' and use everywhere
- //Other parts of the code suffer from the same issue of
- copy.type = mono_type_get_type_byval (copy.type);
- copy.stype &= ~POINTER_MASK;
-
- if (virt_check_this && !stack_slot_is_this_pointer (value) && !(m_class_is_valuetype (method->klass) || stack_slot_is_boxed_value (value)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use the call opcode with a non-final virtual method on an object different than the 'this' pointer at 0x%04x", ctx->ip_offset));
-
- if (constrained && virtual_) {
- if (!stack_slot_is_managed_pointer (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object is not a managed pointer for a constrained call at 0x%04x", ctx->ip_offset));
- if (!mono_metadata_type_equal_full (mono_type_get_type_byval (value->type), mono_type_get_underlying_type (ctx->constrained_type), TRUE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Object not compatible with constrained type at 0x%04x", ctx->ip_offset));
- copy.stype |= BOXED_MASK;
- copy.type = ctx->constrained_type;
- } else {
- if (stack_slot_is_managed_pointer (value) && !m_class_is_valuetype (mono_class_from_mono_type_internal (value->type)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a reference type using a managed pointer to the this arg at 0x%04x", ctx->ip_offset));
-
- if (!virtual_ && m_class_is_valuetype (mono_class_from_mono_type_internal (value->type)) && !m_class_is_valuetype (method->klass) && !stack_slot_is_boxed_value (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a valuetype baseclass at 0x%04x", ctx->ip_offset));
-
- if (virtual_ && m_class_is_valuetype (mono_class_from_mono_type_internal (value->type)) && !stack_slot_is_boxed_value (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a valuetype with callvirt at 0x%04x", ctx->ip_offset));
-
- if (m_class_is_valuetype (method->klass) && (stack_slot_is_boxed_value (value) || !stack_slot_is_managed_pointer (value)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a boxed or literal valuetype to call a valuetype method at 0x%04x", ctx->ip_offset));
- }
- if (!verify_stack_type_compatibility (ctx, type, ©)) {
- char *expected = mono_type_full_name (type);
- char *effective = stack_slot_full_name (©);
- char *method_name = mono_method_full_name (method, TRUE);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature expected '%s' but got '%s' for a call to '%s' at 0x%04x",
- expected, effective, method_name, ctx->ip_offset));
- g_free (method_name);
- g_free (effective);
- g_free (expected);
- }
-
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type_internal (value->type))) {
- char *name = mono_method_full_name (method, TRUE);
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
- g_free (name);
- }
-
- } else if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
- char *name = mono_method_full_name (method, TRUE);
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method %s is not accessible at 0x%04x", name, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
- g_free (name);
- }
-
- if (sig->ret->type != MONO_TYPE_VOID) {
- if (!mono_type_is_valid_in_context (ctx, sig->ret))
- return;
-
- if (check_overflow (ctx)) {
- value = stack_push (ctx);
- set_stack_value (ctx, value, sig->ret, FALSE);
- if ((ctx->prefix_set & PREFIX_READONLY) && m_class_get_rank (method->klass) && !strcmp (method->name, "Address")) {
- ctx->prefix_set &= ~PREFIX_READONLY;
- value->stype |= CMMP_MASK;
- }
- if (sig->ret->byref && is_safe_byref_call)
- value->stype |= SAFE_BYREF_MASK;
- }
- }
-
- if ((ctx->prefix_set & PREFIX_TAIL)) {
- if (!mono_delegate_ret_equal (mono_method_signature_internal (ctx->method)->ret, sig->ret))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call with incompatible return type at 0x%04x", ctx->ip_offset));
- if (ctx->header->code [ctx->ip_offset + 5] != CEE_RET)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Tail call not followed by ret at 0x%04x", ctx->ip_offset));
- }
-
-}
-
-static void
-do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
-{
- MonoClassField *field;
- MonoClass *klass;
- if (!check_overflow (ctx))
- return;
- if (!take_addr)
- CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
-
- if (!(field = verifier_load_field (ctx, token, &klass, take_addr ? "ldsflda" : "ldsfld")))
- return;
-
- if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load non static field at 0x%04x", ctx->ip_offset));
- return;
- }
- /*taking the address of initonly field only works from the static constructor */
- if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
- !(field->parent == ctx->method->klass && (ctx->method->flags & (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_STATIC)) && !strcmp (".cctor", ctx->method->name)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
-
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
-
- ILStackDesc *value = stack_push (ctx);
- set_stack_value (ctx, value, field->type, take_addr);
- if (take_addr)
- value->stype |= SAFE_BYREF_MASK;
-}
-
-static void
-do_store_static_field (VerifyContext *ctx, int token) {
- MonoClassField *field;
- MonoClass *klass;
- ILStackDesc *value;
- CLEAR_PREFIX (ctx, PREFIX_VOLATILE);
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
-
- if (!(field = verifier_load_field (ctx, token, &klass, "stsfld")))
- return;
-
- if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store non static field at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (field->type->type == MONO_TYPE_TYPEDBYREF) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type in store static field at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
-
- if (!verify_stack_type_compatibility (ctx, field->type, value)) {
- char *stack_name = stack_slot_full_name (value);
- char *field_name = mono_type_full_name (field->type);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type in static field store expected '%s' but found '%s' at 0x%04x",
- field_name, stack_name, ctx->ip_offset));
- g_free (field_name);
- g_free (stack_name);
- }
-}
-
-static gboolean
-check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field, const char *opcode)
-{
- MonoClassField *field;
- MonoClass *klass;
- gboolean is_pointer;
-
- /*must be a reference type, a managed pointer, an unamanaged pointer, or a valuetype*/
- if (!(field = verifier_load_field (ctx, token, &klass, opcode)))
- return FALSE;
-
- *ret_field = field;
- //the value on stack is going to be used as a pointer
- is_pointer = stack_slot_get_type (obj) == TYPE_PTR || (stack_slot_get_type (obj) == TYPE_NATIVE_INT && !get_stack_type (m_class_get_byval_arg (field->parent)));
-
- if (field->type->type == MONO_TYPE_TYPEDBYREF) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Typedbyref field is an unverfiable type at 0x%04x", ctx->ip_offset));
- return FALSE;
- }
- g_assert (obj->type);
-
- /*The value on the stack must be a subclass of the defining type of the field*/
- /* we need to check if we can load the field from the stack value*/
- if (is_pointer) {
- if (stack_slot_get_underlying_type (obj) == TYPE_NATIVE_INT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Native int is not a verifiable type to reference a field at 0x%04x", ctx->ip_offset));
-
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, NULL))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
- } else {
- if (!m_class_is_valuetype (field->parent) && stack_slot_is_managed_pointer (obj))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a managed pointer to a reference type and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
-
- /*a value type can be loaded from a value or a managed pointer, but not a boxed object*/
- if (m_class_is_valuetype (field->parent) && stack_slot_is_boxed_value (obj))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is a boxed valuetype and is not compatible to reference the field at 0x%04x", ctx->ip_offset));
-
- if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, m_class_get_byval_arg (field->parent), obj, TRUE, FALSE)) {
- char *found = stack_slot_full_name (obj);
- char *expected = mono_type_full_name (m_class_get_byval_arg (field->parent));
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected type '%s' but found '%s' referencing the 'this' argument at 0x%04x", expected, found, ctx->ip_offset));
- g_free (found);
- g_free (expected);
- }
-
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type_internal (obj->type)))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_FIELD_ACCESS);
- }
-
- check_unmanaged_pointer (ctx, obj);
- return TRUE;
-}
-
-static void
-do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
-{
- ILStackDesc *obj;
- MonoClassField *field;
- gboolean is_safe_byref = FALSE;
-
- if (!take_addr)
- CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
-
- if (!check_underflow (ctx, 1))
- return;
- obj = stack_pop_safe (ctx);
-
- if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
- return;
-
- if (take_addr && m_class_is_valuetype (field->parent) && !stack_slot_is_managed_pointer (obj))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a temporary value-type at 0x%04x", ctx->ip_offset));
-
- if (take_addr && (field->type->attrs & FIELD_ATTRIBUTE_INIT_ONLY) &&
- !(field->parent == ctx->method->klass && mono_method_is_constructor (ctx->method)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot take the address of a init-only field at 0x%04x", ctx->ip_offset));
-
- //must do it here cuz stack_push will return the same slot as obj above
- is_safe_byref = take_addr && (stack_slot_is_reference_value (obj) || stack_slot_is_safe_byref (obj));
-
- ILStackDesc *value = stack_push (ctx);
- set_stack_value (ctx, value, field->type, take_addr);
-
- if (is_safe_byref)
- value->stype |= SAFE_BYREF_MASK;
-}
-
-static void
-do_store_field (VerifyContext *ctx, int token)
-{
- ILStackDesc *value, *obj;
- MonoClassField *field;
- CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
-
- if (!check_underflow (ctx, 2))
- return;
-
- value = stack_pop (ctx);
- obj = stack_pop_safe (ctx);
-
- if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
- return;
-
- if (!verify_stack_type_compatibility (ctx, field->type, value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in field store at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
-}
-
-/*TODO proper handle for Nullable<T>*/
-static void
-do_box_value (VerifyContext *ctx, int klass_token)
-{
- ILStackDesc *value;
- MonoType *type = get_boxable_mono_type (ctx, klass_token, "box");
- MonoClass *klass;
-
- if (!type)
- return;
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
- /*box is a nop for reference types*/
-
- if (stack_slot_get_underlying_type (value) == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type)) {
- stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
- return;
- }
-
-
- if (!verify_stack_type_compatibility (ctx, type, value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for boxing operation at 0x%04x", ctx->ip_offset));
-
- klass = mono_class_from_mono_type_internal (type);
- if (mono_class_is_nullable (klass))
- type = m_class_get_byval_arg (mono_class_get_nullable_param_internal (klass));
- stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
-}
-
-static void
-do_unbox_value (VerifyContext *ctx, int klass_token)
-{
- ILStackDesc *value;
- MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox");
-
- if (!type)
- return;
-
- if (!check_underflow (ctx, 1))
- return;
-
- if (!m_class_is_valuetype (mono_class_from_mono_type_internal (type)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid reference type for unbox at 0x%04x", ctx->ip_offset));
-
- value = stack_pop (ctx);
-
- /*Value should be: a boxed valuetype or a reference type*/
- if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
- (stack_slot_is_boxed_value (value) || !m_class_is_valuetype (mono_class_from_mono_type_internal (value->type)))))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
-
- set_stack_value (ctx, value = stack_push (ctx), mono_type_get_type_byref (type), FALSE);
- value->stype |= CMMP_MASK;
-}
-
-static void
-do_unbox_any (VerifyContext *ctx, int klass_token)
-{
- ILStackDesc *value;
- MonoType *type = get_boxable_mono_type (ctx, klass_token, "unbox.any");
-
- if (!type)
- return;
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
-
- /*Value should be: a boxed valuetype or a reference type*/
- if (!(stack_slot_get_type (value) == TYPE_COMPLEX &&
- (stack_slot_is_boxed_value (value) || !m_class_is_valuetype (mono_class_from_mono_type_internal (value->type)))))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox.any operation at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
-
- set_stack_value (ctx, stack_push (ctx), type, FALSE);
-}
-
-static void
-do_unary_math_op (VerifyContext *ctx, int op)
-{
- ILStackDesc *value;
- if (!check_underflow (ctx, 1))
- return;
- value = stack_pop (ctx);
- switch (stack_slot_get_type (value)) {
- case TYPE_I4:
- case TYPE_I8:
- case TYPE_NATIVE_INT:
- break;
- case TYPE_R8:
- if (op == CEE_NEG)
- break;
- case TYPE_COMPLEX: /*only enums are ok*/
- if (mono_type_is_enum_type (value->type))
- break;
- default:
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
- }
- stack_push_stack_val (ctx, value);
-}
-
-static void
-do_conversion (VerifyContext *ctx, int kind)
-{
- ILStackDesc *value;
- if (!check_underflow (ctx, 1))
- return;
- value = stack_pop (ctx);
-
- switch (stack_slot_get_type (value)) {
- case TYPE_I4:
- case TYPE_I8:
- case TYPE_NATIVE_INT:
- case TYPE_R8:
- break;
- default:
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type (%s) at stack for conversion operation. Numeric type expected at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
- }
-
- switch (kind) {
- case TYPE_I4:
- stack_push_val (ctx, TYPE_I4, mono_get_int32_type ());
- break;
- case TYPE_I8:
- stack_push_val (ctx,TYPE_I8, m_class_get_byval_arg (mono_defaults.int64_class));
- break;
- case TYPE_R8:
- stack_push_val (ctx, TYPE_R8, m_class_get_byval_arg (mono_defaults.double_class));
- break;
- case TYPE_NATIVE_INT:
- stack_push_val (ctx, TYPE_NATIVE_INT, mono_get_int_type ());
- break;
- default:
- g_error ("unknown type %02x in conversion", kind);
-
- }
-}
-
-static void
-do_load_token (VerifyContext *ctx, int token)
-{
- ERROR_DECL (error);
- gpointer handle;
- MonoClass *handle_class;
- if (!check_overflow (ctx))
- return;
-
- if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
- handle = mono_method_get_wrapper_data (ctx->method, token);
- handle_class = (MonoClass *)mono_method_get_wrapper_data (ctx->method, token + 1);
- if (handle_class == mono_defaults.typehandle_class)
- handle = m_class_get_byval_arg ((MonoClass*)handle);
- } else {
- switch (token & 0xff000000) {
- case MONO_TOKEN_TYPE_DEF:
- case MONO_TOKEN_TYPE_REF:
- case MONO_TOKEN_TYPE_SPEC:
- case MONO_TOKEN_FIELD_DEF:
- case MONO_TOKEN_METHOD_DEF:
- case MONO_TOKEN_METHOD_SPEC:
- case MONO_TOKEN_MEMBER_REF:
- if (!token_bounds_check (ctx->image, token)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Table index out of range 0x%x for token %x for ldtoken at 0x%04x", mono_metadata_token_index (token), token, ctx->ip_offset));
- return;
- }
- break;
- default:
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid table 0x%x for token 0x%x for ldtoken at 0x%04x", mono_metadata_token_table (token), token, ctx->ip_offset));
- return;
- }
-
- handle = mono_ldtoken_checked (ctx->image, token, &handle_class, ctx->generic_context, error);
- }
-
- if (!handle) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x due to %s", token, ctx->ip_offset, mono_error_get_message (error)));
- mono_error_cleanup (error);
- return;
- }
- if (handle_class == mono_defaults.typehandle_class) {
- mono_type_is_valid_in_context (ctx, (MonoType*)handle);
- } else if (handle_class == mono_defaults.methodhandle_class) {
- mono_method_is_valid_in_context (ctx, (MonoMethod*)handle);
- } else if (handle_class == mono_defaults.fieldhandle_class) {
- mono_type_is_valid_in_context (ctx, m_class_get_byval_arg (((MonoClassField*)handle)->parent));
- } else {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid ldtoken type %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- }
- stack_push_val (ctx, TYPE_COMPLEX, m_class_get_byval_arg (handle_class));
-}
-
-static void
-do_ldobj_value (VerifyContext *ctx, int token)
-{
- ILStackDesc *value;
- MonoType *type = get_boxable_mono_type (ctx, token, "ldobj");
- CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
-
- if (!type)
- return;
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
- if (!stack_slot_is_managed_pointer (value)
- && stack_slot_get_type (value) != TYPE_NATIVE_INT
- && !(stack_slot_get_type (value) == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
- return;
- }
-
- if (stack_slot_get_type (value) == TYPE_NATIVE_INT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Using native pointer to ldobj at 0x%04x", ctx->ip_offset));
-
- /*We have a byval on the stack, but the comparison must be strict. */
- if (!verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (value->type), TRUE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldojb operation at 0x%04x", ctx->ip_offset));
-
- set_stack_value (ctx, stack_push (ctx), type, FALSE);
-}
-
-static void
-do_stobj (VerifyContext *ctx, int token)
-{
- ILStackDesc *dest, *src;
- MonoType *type = get_boxable_mono_type (ctx, token, "stobj");
- CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
-
- if (!type)
- return;
-
- if (!check_underflow (ctx, 2))
- return;
-
- src = stack_pop (ctx);
- dest = stack_pop (ctx);
-
- if (stack_slot_is_managed_mutability_pointer (dest))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stobj at 0x%04x", ctx->ip_offset));
-
- if (!stack_slot_is_managed_pointer (dest))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of stobj operation at 0x%04x", ctx->ip_offset));
-
- if (stack_slot_is_boxed_value (src) && !MONO_TYPE_IS_REFERENCE (src->type) && !MONO_TYPE_IS_REFERENCE (type))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
-
- if (!verify_stack_type_compatibility (ctx, type, src)) {
- char *type_name = mono_type_full_name (type);
- char *src_name = stack_slot_full_name (src);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token '%s' and source '%s' of stobj don't match ' at 0x%04x", type_name, src_name, ctx->ip_offset));
- g_free (type_name);
- g_free (src_name);
- }
-
- if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of stobj don't match at 0x%04x", ctx->ip_offset));
-}
-
-static void
-do_cpobj (VerifyContext *ctx, int token)
-{
- ILStackDesc *dest, *src;
- MonoType *type = get_boxable_mono_type (ctx, token, "cpobj");
- if (!type)
- return;
-
- if (!check_underflow (ctx, 2))
- return;
-
- src = stack_pop (ctx);
- dest = stack_pop (ctx);
-
- if (!stack_slot_is_managed_pointer (src))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid source of cpobj operation at 0x%04x", ctx->ip_offset));
-
- if (!stack_slot_is_managed_pointer (dest))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid destination of cpobj operation at 0x%04x", ctx->ip_offset));
-
- if (stack_slot_is_managed_mutability_pointer (dest))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with cpobj at 0x%04x", ctx->ip_offset));
-
- if (!verify_type_compatibility (ctx, type, mono_type_get_type_byval (src->type)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of cpobj don't match at 0x%04x", ctx->ip_offset));
-
- if (!verify_type_compatibility (ctx, mono_type_get_type_byval (dest->type), type))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Destination and token types of cpobj don't match at 0x%04x", ctx->ip_offset));
-}
-
-static void
-do_initobj (VerifyContext *ctx, int token)
-{
- ILStackDesc *obj;
- MonoType *stack, *type = get_boxable_mono_type (ctx, token, "initobj");
- if (!type)
- return;
-
- if (!check_underflow (ctx, 1))
- return;
-
- obj = stack_pop (ctx);
-
- if (!stack_slot_is_managed_pointer (obj))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid object address for initobj at 0x%04x", ctx->ip_offset));
-
- if (stack_slot_is_managed_mutability_pointer (obj))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with initobj at 0x%04x", ctx->ip_offset));
-
- stack = mono_type_get_type_byval (obj->type);
- if (MONO_TYPE_IS_REFERENCE (stack)) {
- if (!verify_type_compatibility (ctx, stack, type))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
- else if (IS_STRICT_MODE (ctx) && !mono_metadata_type_equal (type, stack))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
- } else if (!verify_type_compatibility (ctx, stack, type)) {
- char *expected_name = mono_type_full_name (type);
- char *stack_name = mono_type_full_name (stack);
-
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Initobj %s not compatible with value on stack %s at 0x%04x", expected_name, stack_name, ctx->ip_offset));
- g_free (expected_name);
- g_free (stack_name);
- }
-}
-
-static void
-do_newobj (VerifyContext *ctx, int token)
-{
- ILStackDesc *value;
- int i;
- MonoMethodSignature *sig;
- MonoMethod *method;
- gboolean is_delegate = FALSE;
-
- if (!(method = verifier_load_method (ctx, token, "newobj")))
- return;
-
- if (!mono_method_is_constructor (method)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
- return;
- }
-
- if (mono_class_get_flags (method->klass) & (TYPE_ATTRIBUTE_ABSTRACT | TYPE_ATTRIBUTE_INTERFACE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Trying to instantiate an abstract or interface type at 0x%04x", ctx->ip_offset));
-
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL)) {
- char *from = mono_method_full_name (ctx->method, TRUE);
- char *to = mono_method_full_name (method, TRUE);
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor %s not visible from %s at 0x%04x", to, from, ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
- g_free (from);
- g_free (to);
- }
-
- //FIXME use mono_method_get_signature_full
- sig = mono_method_signature_internal (method);
- if (!sig) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature to newobj at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (!sig->hasthis) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid constructor signature missing hasthis at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (!check_underflow (ctx, sig->param_count))
- return;
-
- is_delegate = m_class_get_parent (method->klass) == mono_defaults.multicastdelegate_class;
-
- if (is_delegate) {
- ILStackDesc *funptr;
- //first arg is object, second arg is fun ptr
- if (sig->param_count != 2) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid delegate constructor at 0x%04x", ctx->ip_offset));
- return;
- }
- funptr = stack_pop (ctx);
- value = stack_pop (ctx);
- verify_delegate_compatibility (ctx, method->klass, value, funptr);
- } else {
- for (i = sig->param_count - 1; i >= 0; --i) {
- VERIFIER_DEBUG ( printf ("verifying constructor argument %d\n", i); );
- value = stack_pop (ctx);
- if (!verify_stack_type_compatibility (ctx, sig->params [i], value)) {
- char *stack_name = stack_slot_full_name (value);
- char *sig_name = mono_type_full_name (sig->params [i]);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with constructor signature: %s X %s at 0x%04x", sig_name, stack_name, ctx->ip_offset));
- g_free (stack_name);
- g_free (sig_name);
- }
-
- if (stack_slot_is_managed_mutability_pointer (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer as argument of newobj at 0x%04x", ctx->ip_offset));
- }
- }
-
- if (check_overflow (ctx))
- set_stack_value (ctx, stack_push (ctx), m_class_get_byval_arg (method->klass), FALSE);
-}
-
-static void
-do_cast (VerifyContext *ctx, int token, const char *opcode) {
- ILStackDesc *value;
- MonoType *type;
- gboolean is_boxed;
- gboolean do_box;
-
- if (!check_underflow (ctx, 1))
- return;
-
- if (!(type = get_boxable_mono_type (ctx, token, opcode)))
- return;
-
- if (type->byref) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid %s type at 0x%04x", opcode, ctx->ip_offset));
- return;
- }
-
- value = stack_pop (ctx);
- is_boxed = stack_slot_is_boxed_value (value);
-
- if (stack_slot_is_managed_pointer (value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
- else if (!MONO_TYPE_IS_REFERENCE (value->type) && !is_boxed) {
- char *name = stack_slot_full_name (value);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a reference type on stack for %s but found %s at 0x%04x", opcode, name, ctx->ip_offset));
- g_free (name);
- }
-
- switch (value->type->type) {
- case MONO_TYPE_FNPTR:
- case MONO_TYPE_PTR:
- case MONO_TYPE_TYPEDBYREF:
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
- default:
- break;
- }
-
- do_box = is_boxed || mono_type_is_generic_argument(type) || m_class_is_valuetype (mono_class_from_mono_type_internal (type));
- stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
-}
-
-static MonoType *
-mono_type_from_opcode (int opcode) {
- switch (opcode) {
- case CEE_LDIND_I1:
- case CEE_LDIND_U1:
- case CEE_STIND_I1:
- case CEE_LDELEM_I1:
- case CEE_LDELEM_U1:
- case CEE_STELEM_I1:
- return m_class_get_byval_arg (mono_defaults.sbyte_class);
-
- case CEE_LDIND_I2:
- case CEE_LDIND_U2:
- case CEE_STIND_I2:
- case CEE_LDELEM_I2:
- case CEE_LDELEM_U2:
- case CEE_STELEM_I2:
- return m_class_get_byval_arg (mono_defaults.int16_class);
-
- case CEE_LDIND_I4:
- case CEE_LDIND_U4:
- case CEE_STIND_I4:
- case CEE_LDELEM_I4:
- case CEE_LDELEM_U4:
- case CEE_STELEM_I4:
- return mono_get_int32_type ();
-
- case CEE_LDIND_I8:
- case CEE_STIND_I8:
- case CEE_LDELEM_I8:
- case CEE_STELEM_I8:
- return m_class_get_byval_arg (mono_defaults.int64_class);
-
- case CEE_LDIND_R4:
- case CEE_STIND_R4:
- case CEE_LDELEM_R4:
- case CEE_STELEM_R4:
- return m_class_get_byval_arg (mono_defaults.single_class);
-
- case CEE_LDIND_R8:
- case CEE_STIND_R8:
- case CEE_LDELEM_R8:
- case CEE_STELEM_R8:
- return m_class_get_byval_arg (mono_defaults.double_class);
-
- case CEE_LDIND_I:
- case CEE_STIND_I:
- case CEE_LDELEM_I:
- case CEE_STELEM_I:
- return mono_get_int_type ();
-
- case CEE_LDIND_REF:
- case CEE_STIND_REF:
- case CEE_LDELEM_REF:
- case CEE_STELEM_REF:
- return mono_get_object_type ();
-
- default:
- g_error ("unknown opcode %02x in mono_type_from_opcode ", opcode);
- return NULL;
- }
-}
-
-static void
-do_load_indirect (VerifyContext *ctx, int opcode)
-{
- ILStackDesc *value;
- CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
- if (!stack_slot_is_managed_pointer (value)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
- set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
- return;
- }
-
- if (opcode == CEE_LDIND_REF) {
- if (stack_slot_get_underlying_type (value) != TYPE_COMPLEX || m_class_is_valuetype (mono_class_from_mono_type_internal (value->type)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind_ref expected object byref operation at 0x%04x", ctx->ip_offset));
- set_stack_value (ctx, stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
- } else {
- if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (value->type), TRUE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for ldind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
- set_stack_value (ctx, stack_push (ctx), mono_type_from_opcode (opcode), FALSE);
- }
-}
-
-static void
-do_store_indirect (VerifyContext *ctx, int opcode)
-{
- ILStackDesc *addr, *val;
- CLEAR_PREFIX (ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
-
- if (!check_underflow (ctx, 2))
- return;
-
- val = stack_pop (ctx);
- addr = stack_pop (ctx);
-
- check_unmanaged_pointer (ctx, addr);
-
- if (!stack_slot_is_managed_pointer (addr) && stack_slot_get_type (addr) != TYPE_PTR) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (stack_slot_is_managed_mutability_pointer (addr)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with stind at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (!verify_type_compatibility_full (ctx, mono_type_from_opcode (opcode), mono_type_get_type_byval (addr->type), TRUE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
-
- if (!verify_stack_type_compatibility (ctx, mono_type_from_opcode (opcode), val))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value type at stack for stind 0x%x operation at 0x%04x", opcode, ctx->ip_offset));
-}
-
-static void
-do_newarr (VerifyContext *ctx, int token)
-{
- ILStackDesc *value;
- MonoType *type = get_boxable_mono_type (ctx, token, "newarr");
-
- if (!type)
- return;
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
- if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Array size type on stack (%s) is not a verifiable type at 0x%04x", stack_slot_get_name (value), ctx->ip_offset));
-
- set_stack_value (ctx, stack_push (ctx), m_class_get_byval_arg (mono_class_create_array (mono_class_from_mono_type_internal (type), 1)), FALSE);
-}
-
-/*FIXME handle arrays that are not 0-indexed*/
-static void
-do_ldlen (VerifyContext *ctx)
-{
- ILStackDesc *value;
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
-
- if (stack_slot_get_type (value) != TYPE_COMPLEX || value->type->type != MONO_TYPE_SZARRAY)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type for ldlen at 0x%04x", ctx->ip_offset));
-
- stack_push_val (ctx, TYPE_NATIVE_INT, mono_get_int_type ());
-}
-
-/*FIXME handle arrays that are not 0-indexed*/
-/*FIXME handle readonly prefix and CMMP*/
-static void
-do_ldelema (VerifyContext *ctx, int klass_token)
-{
- ILStackDesc *index, *array, *res;
- MonoType *type = get_boxable_mono_type (ctx, klass_token, "ldelema");
- gboolean valid;
-
- if (!type)
- return;
-
- if (!check_underflow (ctx, 2))
- return;
-
- index = stack_pop (ctx);
- array = stack_pop (ctx);
-
- if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelema is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
-
- if (!stack_slot_is_null_literal (array)) {
- if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelema at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
- else {
- if (get_stack_type (type) == TYPE_I4 || get_stack_type (type) == TYPE_NATIVE_INT) {
- valid = verify_type_compatibility_full (ctx, type, m_class_get_byval_arg (array->type->data.klass), TRUE);
- } else {
- valid = mono_metadata_type_equal (type, m_class_get_byval_arg (array->type->data.klass));
- }
- if (!valid)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelema at 0x%04x", ctx->ip_offset));
- }
- }
-
- res = stack_push (ctx);
- set_stack_value (ctx, res, type, TRUE);
- if (ctx->prefix_set & PREFIX_READONLY) {
- ctx->prefix_set &= ~PREFIX_READONLY;
- res->stype |= CMMP_MASK;
- }
-
- res->stype |= SAFE_BYREF_MASK;
-}
-
-/*
- * FIXME handle arrays that are not 0-indexed
- * FIXME handle readonly prefix and CMMP
- */
-static void
-do_ldelem (VerifyContext *ctx, int opcode, int token)
-{
-#define IS_ONE_OF2(T, A, B) (T == A || T == B)
- ILStackDesc *index, *array;
- MonoType *type;
- if (!check_underflow (ctx, 2))
- return;
-
- if (opcode == CEE_LDELEM) {
- if (!(type = verifier_load_type (ctx, token, "ldelem.any"))) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
- return;
- }
- } else {
- type = mono_type_from_opcode (opcode);
- }
-
- index = stack_pop (ctx);
- array = stack_pop (ctx);
-
- if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for ldelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
-
- if (!stack_slot_is_null_literal (array)) {
- if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for ldelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
- else {
- if (opcode == CEE_LDELEM_REF) {
- if (m_class_is_valuetype (array->type->data.klass))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for ldelem.ref 0x%04x", ctx->ip_offset));
- type = m_class_get_byval_arg (array->type->data.klass);
- } else {
- MonoType *candidate = m_class_get_byval_arg (array->type->data.klass);
- if (IS_STRICT_MODE (ctx)) {
- MonoType *underlying_type = mono_type_get_underlying_type_any (type);
- MonoType *underlying_candidate = mono_type_get_underlying_type_any (candidate);
- if ((IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I, MONO_TYPE_U)) ||
- (IS_ONE_OF2 (underlying_candidate->type, MONO_TYPE_I4, MONO_TYPE_U4) && IS_ONE_OF2 (underlying_type->type, MONO_TYPE_I, MONO_TYPE_U)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
- }
- if (!verify_type_compatibility_full (ctx, type, candidate, TRUE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for ldelem.X at 0x%04x", ctx->ip_offset));
- }
- }
- }
-
- set_stack_value (ctx, stack_push (ctx), type, FALSE);
-#undef IS_ONE_OF2
-}
-
-/*
- * FIXME handle arrays that are not 0-indexed
- */
-static void
-do_stelem (VerifyContext *ctx, int opcode, int token)
-{
- ILStackDesc *index, *array, *value;
- MonoType *type;
- if (!check_underflow (ctx, 3))
- return;
-
- if (opcode == CEE_STELEM) {
- if (!(type = verifier_load_type (ctx, token, "stelem.any"))) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
- return;
- }
- } else {
- type = mono_type_from_opcode (opcode);
- }
-
- value = stack_pop (ctx);
- index = stack_pop (ctx);
- array = stack_pop (ctx);
-
- if (stack_slot_get_type (index) != TYPE_I4 && stack_slot_get_type (index) != TYPE_NATIVE_INT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Index type(%s) for stdelem.X is not an int or a native int at 0x%04x", stack_slot_get_name (index), ctx->ip_offset));
-
- if (!stack_slot_is_null_literal (array)) {
- if (stack_slot_get_type (array) != TYPE_COMPLEX || array->type->type != MONO_TYPE_SZARRAY) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type(%s) for stelem.X at 0x%04x", stack_slot_get_name (array), ctx->ip_offset));
- } else {
- if (opcode == CEE_STELEM_REF) {
- if (m_class_is_valuetype (array->type->data.klass))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
- } else if (!verify_type_compatibility_full (ctx, m_class_get_byval_arg (array->type->data.klass), type, TRUE)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid array type on stack for stdelem.X at 0x%04x", ctx->ip_offset));
- }
- }
- }
- if (opcode == CEE_STELEM_REF) {
- if (!stack_slot_is_boxed_value (value) && m_class_is_valuetype (mono_class_from_mono_type_internal (value->type)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value is not a reference type for stelem.ref 0x%04x", ctx->ip_offset));
- } else if (opcode != CEE_STELEM_REF) {
- if (!verify_stack_type_compatibility (ctx, type, value))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 0x%04x", ctx->ip_offset));
-
- if (stack_slot_is_boxed_value (value) && !MONO_TYPE_IS_REFERENCE (value->type) && !MONO_TYPE_IS_REFERENCE (type))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use stobj with a boxed source value that is not a reference type at 0x%04x", ctx->ip_offset));
-
- }
-}
-
-static void
-do_throw (VerifyContext *ctx)
-{
- ILStackDesc *exception;
- if (!check_underflow (ctx, 1))
- return;
- exception = stack_pop (ctx);
-
- if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !m_class_is_valuetype (mono_class_from_mono_type_internal (exception->type))))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type at 0x%04x", ctx->ip_offset));
-
- if (mono_type_is_generic_argument (exception->type) && !stack_slot_is_boxed_value (exception)) {
- char *name = mono_type_full_name (exception->type);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type on stack for throw, expected reference type but found unboxed %s at 0x%04x ", name, ctx->ip_offset));
- g_free (name);
- }
- /*The stack is left empty after a throw*/
- ctx->eval.size = 0;
-}
-
-
-static void
-do_endfilter (VerifyContext *ctx)
-{
- MonoExceptionClause *clause;
-
- if (IS_STRICT_MODE (ctx)) {
- if (ctx->eval.size != 1)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack size must have one item for endfilter at 0x%04x", ctx->ip_offset));
-
- if (ctx->eval.size >= 1 && stack_slot_get_type (stack_pop (ctx)) != TYPE_I4)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Stack item type is not an int32 for endfilter at 0x%04x", ctx->ip_offset));
- }
-
- if ((clause = is_correct_endfilter (ctx, ctx->ip_offset))) {
- if (IS_STRICT_MODE (ctx)) {
- if (ctx->ip_offset != clause->handler_offset - 2)
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
- } else {
- if ((ctx->ip_offset != clause->handler_offset - 2) && !MONO_OFFSET_IN_HANDLER (clause, ctx->ip_offset))
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter is not the last instruction of the filter clause at 0x%04x", ctx->ip_offset));
- }
- } else {
- if (IS_STRICT_MODE (ctx) && !is_unverifiable_endfilter (ctx, ctx->ip_offset))
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
- else
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("endfilter outside filter clause at 0x%04x", ctx->ip_offset));
- }
-
- ctx->eval.size = 0;
-}
-
-static void
-do_leave (VerifyContext *ctx, int delta)
-{
- int target = ((gint32)ctx->ip_offset) + delta;
- if (target >= ctx->code_size || target < 0)
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
-
- if (!is_correct_leave (ctx->header, ctx->ip_offset, target))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Leave not allowed in finally block at 0x%04x", ctx->ip_offset));
- ctx->eval.size = 0;
- ctx->target = target;
-}
-
-/*
- * do_static_branch:
- *
- * Verify br and br.s opcodes.
- */
-static void
-do_static_branch (VerifyContext *ctx, int delta)
-{
- int target = ctx->ip_offset + delta;
- if (target < 0 || target >= ctx->code_size) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("branch target out of code at 0x%04x", ctx->ip_offset));
- return;
- }
-
- switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
- case 1:
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
- break;
- case 2:
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ctx->ip_offset));
- break;
- }
-
- ctx->target = target;
-}
-
-static void
-do_switch (VerifyContext *ctx, int count, const unsigned char *data)
-{
- int i, base = ctx->ip_offset + 5 + count * 4;
- ILStackDesc *value;
-
- if (!check_underflow (ctx, 1))
- return;
-
- value = stack_pop (ctx);
-
- if (stack_slot_get_type (value) != TYPE_I4 && stack_slot_get_type (value) != TYPE_NATIVE_INT)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ctx->ip_offset));
-
- for (i = 0; i < count; ++i) {
- int target = base + read32 (data + i * 4);
-
- if (target < 0 || target >= ctx->code_size) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x out of code at 0x%04x", i, ctx->ip_offset));
- return;
- }
-
- switch (is_valid_branch_instruction (ctx->header, ctx->ip_offset, target)) {
- case 1:
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
- break;
- case 2:
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Switch target %x escapes out of exception block at 0x%04x", i, ctx->ip_offset));
- return;
- }
- merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
- }
-}
-
-static void
-do_load_function_ptr (VerifyContext *ctx, guint32 token, gboolean virtual_)
-{
- ILStackDesc *top;
- MonoMethod *method;
-
- if (virtual_ && !check_underflow (ctx, 1))
- return;
-
- if (!virtual_ && !check_overflow (ctx))
- return;
-
- if (ctx->method->wrapper_type != MONO_WRAPPER_NONE) {
- method = (MonoMethod *)mono_method_get_wrapper_data (ctx->method, (guint32)token);
- if (!method) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return;
- }
- } else {
- if (!IS_METHOD_DEF_OR_REF_OR_SPEC (token) || !token_bounds_check (ctx->image, token)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid token %x for ldftn at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return;
- }
-
- if (!(method = verifier_load_method (ctx, token, virtual_ ? "ldvirtfrn" : "ldftn")))
- return;
- }
-
- if (mono_method_is_constructor (method))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldftn with a constructor at 0x%04x", ctx->ip_offset));
-
- if (virtual_) {
- ILStackDesc *top = stack_pop (ctx);
-
- if (stack_slot_get_type (top) != TYPE_COMPLEX || top->type->type == MONO_TYPE_VALUETYPE)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument to ldvirtftn at 0x%04x", ctx->ip_offset));
-
- if (method->flags & METHOD_ATTRIBUTE_STATIC)
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use ldvirtftn with a constructor at 0x%04x", ctx->ip_offset));
-
- if (!verify_stack_type_compatibility (ctx, m_class_get_byval_arg (method->klass), top))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
- }
-
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, NULL))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Loaded method is not visible for ldftn/ldvirtftn at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
-
- top = stack_push_val(ctx, TYPE_PTR, mono_type_create_fnptr_from_mono_method (ctx, method));
- top->method = method;
-}
-
-static void
-do_sizeof (VerifyContext *ctx, int token)
-{
- MonoType *type;
-
- if (!(type = verifier_load_type (ctx, token, "sizeof")))
- return;
-
- if (type->byref && type->type != MONO_TYPE_TYPEDBYREF) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of byref type at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (type->type == MONO_TYPE_VOID) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid use of void type at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (check_overflow (ctx))
- set_stack_value (ctx, stack_push (ctx), m_class_get_byval_arg (mono_defaults.uint32_class), FALSE);
-}
-
-/* Stack top can be of any type, the runtime doesn't care and treat everything as an int. */
-static void
-do_localloc (VerifyContext *ctx)
-{
- if (ctx->eval.size != 1) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
- return;
- }
-
- if (in_any_exception_block (ctx->header, ctx->ip_offset)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack must have only size item in localloc at 0x%04x", ctx->ip_offset));
- return;
- }
-
- /*TODO verify top type*/
- /* top = */ stack_pop (ctx);
-
- set_stack_value (ctx, stack_push (ctx), mono_get_int_type (), FALSE);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
-}
-
-static void
-do_ldstr (VerifyContext *ctx, guint32 token)
-{
- ERROR_DECL (error);
- if (ctx->method->wrapper_type == MONO_WRAPPER_NONE && !image_is_dynamic (ctx->image)) {
- if (mono_metadata_token_code (token) != MONO_TOKEN_STRING) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string token %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
- return;
- }
-
- if (!mono_verifier_verify_string_signature (ctx->image, mono_metadata_token_index (token), error)) {
- ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x due to: %s", token, ctx->ip_offset, mono_error_get_message (error)), MONO_EXCEPTION_BAD_IMAGE);
- mono_error_cleanup (error);
- return;
- }
- }
-
- if (check_overflow (ctx))
- stack_push_val (ctx, TYPE_COMPLEX, m_class_get_byval_arg (mono_defaults.string_class));
-}
-
-static void
-do_refanyval (VerifyContext *ctx, int token)
-{
- ILStackDesc *top;
- MonoType *type;
- if (!check_underflow (ctx, 1))
- return;
-
- if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
- return;
-
- top = stack_pop (ctx);
-
- if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanyval, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
-
- set_stack_value (ctx, stack_push (ctx), type, TRUE);
-}
-
-static void
-do_refanytype (VerifyContext *ctx)
-{
- ILStackDesc *top;
-
- if (!check_underflow (ctx, 1))
- return;
-
- top = stack_pop (ctx);
-
- if (top->stype != TYPE_PTR || top->type->type != MONO_TYPE_TYPEDBYREF)
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected a typedref as argument for refanytype, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
-
- set_stack_value (ctx, stack_push (ctx), m_class_get_byval_arg (mono_defaults.typehandle_class), FALSE);
-
-}
-
-static void
-do_mkrefany (VerifyContext *ctx, int token)
-{
- ILStackDesc *top;
- MonoType *type;
- if (!check_underflow (ctx, 1))
- return;
-
- if (!(type = get_boxable_mono_type (ctx, token, "refanyval")))
- return;
-
- top = stack_pop (ctx);
-
- if (stack_slot_is_managed_mutability_pointer (top))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot use a readonly pointer with mkrefany at 0x%04x", ctx->ip_offset));
-
- if (!stack_slot_is_managed_pointer (top)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Expected a managed pointer for mkrefany, but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
- }else {
- MonoType *stack_type = mono_type_get_type_byval (top->type);
- if (MONO_TYPE_IS_REFERENCE (type) && !mono_metadata_type_equal (type, stack_type))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
-
- if (!MONO_TYPE_IS_REFERENCE (type) && !verify_type_compatibility_full (ctx, type, stack_type, TRUE))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type not compatible for mkrefany at 0x%04x", ctx->ip_offset));
- }
-
- set_stack_value (ctx, stack_push (ctx), m_class_get_byval_arg (mono_defaults.typed_reference_class), FALSE);
-}
-
-static void
-do_ckfinite (VerifyContext *ctx)
-{
- ILStackDesc *top;
- if (!check_underflow (ctx, 1))
- return;
-
- top = stack_pop (ctx);
-
- if (stack_slot_get_underlying_type (top) != TYPE_R8)
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Expected float32 or float64 on stack for ckfinit but found %s at 0x%04x", stack_slot_get_name (top), ctx->ip_offset));
- stack_push_stack_val (ctx, top);
-}
-/*
- * merge_stacks:
- * Merge the stacks and perform compat checks. The merge check if types of @from are mergeable with type of @to
- *
- * @from holds new values for a given control path
- * @to holds the current values of a given control path
- *
- * TODO we can eliminate the from argument as all callers pass &ctx->eval
- */
-static void
-merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, gboolean start, gboolean external)
-{
- ERROR_DECL (error);
- int i, j;
- stack_init (ctx, to);
-
- if (start) {
- if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
- from->size = 0;
- else
- stack_copy (&ctx->eval, to);
- goto end_verify;
- } else if (!(to->flags & IL_CODE_STACK_MERGED)) {
- stack_copy (to, &ctx->eval);
- goto end_verify;
- }
- VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
-
- if (from->size != to->size) {
- VERIFIER_DEBUG ( printf ("different stack sizes %d x %d at 0x%04x\n", from->size, to->size, ctx->ip_offset); );
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d) at 0x%04x", from->size, to->size, ctx->ip_offset));
- goto end_verify;
- }
-
- //FIXME we need to preserve CMMP attributes
- //FIXME we must take null literals into consideration.
- for (i = 0; i < from->size; ++i) {
- ILStackDesc *new_slot = from->stack + i;
- ILStackDesc *old_slot = to->stack + i;
- MonoType *new_type = mono_type_from_stack_slot (new_slot);
- MonoType *old_type = mono_type_from_stack_slot (old_slot);
- MonoClass *old_class = mono_class_from_mono_type_internal (old_type);
- MonoClass *new_class = mono_class_from_mono_type_internal (new_type);
- MonoClass *match_class = NULL;
-
- // check for safe byref before the next steps override new_slot
- if (stack_slot_is_safe_byref (old_slot) ^ stack_slot_is_safe_byref (new_slot)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot merge stack at depth %d byref types are safe byref incompatible at %0x04x ", i, ctx->ip_offset));
- goto end_verify;
- }
-
- // S := T then U = S (new value is compatible with current value, keep current)
- if (verify_stack_type_compatibility (ctx, old_type, new_slot)) {
- copy_stack_value (new_slot, old_slot);
- continue;
- }
-
- // T := S then U = T (old value is compatible with current value, use new)
- if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
- copy_stack_value (old_slot, new_slot);
- continue;
- }
-
- /*Both slots are the same boxed valuetype. Simply copy it.*/
- if (stack_slot_is_boxed_value (old_slot) &&
- stack_slot_is_boxed_value (new_slot) &&
- mono_metadata_type_equal (old_type, new_type)) {
- copy_stack_value (new_slot, old_slot);
- continue;
- }
-
- if (mono_type_is_generic_argument (old_type) || mono_type_is_generic_argument (new_type)) {
- char *old_name = stack_slot_full_name (old_slot);
- char *new_name = stack_slot_full_name (new_slot);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
- g_free (old_name);
- g_free (new_name);
- goto end_verify;
- }
-
- //both are reference types, use closest common super type
- if (!m_class_is_valuetype (mono_class_from_mono_type_internal (old_type))
- && !m_class_is_valuetype (mono_class_from_mono_type_internal (new_type))
- && !stack_slot_is_managed_pointer (old_slot)
- && !stack_slot_is_managed_pointer (new_slot)) {
-
- mono_class_setup_supertypes (old_class);
- mono_class_setup_supertypes (new_class);
-
- MonoClass **old_class_supertypes = m_class_get_supertypes (old_class);
- MonoClass **new_class_supertypes = m_class_get_supertypes (new_class);
- for (j = MIN (m_class_get_idepth (old_class), m_class_get_idepth (new_class)) - 1; j > 0; --j) {
- if (mono_metadata_type_equal (m_class_get_byval_arg (old_class_supertypes [j]), m_class_get_byval_arg (new_class_supertypes [j]))) {
- match_class = old_class_supertypes [j];
- goto match_found;
- }
- }
-
- mono_class_setup_interfaces (old_class, error);
- if (!is_ok (error)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot merge stacks due to a TypeLoadException %s at 0x%04x", mono_error_get_message (error), ctx->ip_offset));
- mono_error_cleanup (error);
- goto end_verify;
- }
- mono_class_setup_interfaces (new_class, error);
- if (!is_ok (error)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot merge stacks due to a TypeLoadException %s at 0x%04x", mono_error_get_message (error), ctx->ip_offset));
- mono_error_cleanup (error);
- goto end_verify;
- }
-
- /* if old class is an interface that new class implements */
- if (mono_class_is_interface (old_class)) {
- if (verifier_class_is_assignable_from (old_class, new_class)) {
- match_class = old_class;
- goto match_found;
- }
- MonoClass **old_class_interfaces = m_class_get_interfaces (old_class);
- guint16 old_class_interface_count = m_class_get_interface_count (old_class);
- for (j = 0; j < old_class_interface_count; ++j) {
- if (verifier_class_is_assignable_from (old_class_interfaces [j], new_class)) {
- match_class = old_class_interfaces [j];
- goto match_found;
- }
- }
- }
-
- if (mono_class_is_interface (new_class)) {
- if (verifier_class_is_assignable_from (new_class, old_class)) {
- match_class = new_class;
- goto match_found;
- }
- MonoClass **new_class_interfaces = m_class_get_interfaces (new_class);
- guint16 new_class_interface_count = m_class_get_interface_count (new_class);
- for (j = 0; j < new_class_interface_count; ++j) {
- if (verifier_class_is_assignable_from (new_class_interfaces [j], old_class)) {
- match_class = new_class_interfaces [j];
- goto match_found;
- }
- }
- }
-
- //No decent super type found, use object
- match_class = mono_defaults.object_class;
- goto match_found;
- } else if (is_compatible_boxed_valuetype (ctx,old_type, new_type, new_slot, FALSE) || is_compatible_boxed_valuetype (ctx, new_type, old_type, old_slot, FALSE)) {
- match_class = mono_defaults.object_class;
- goto match_found;
- }
-
- {
- char *old_name = stack_slot_full_name (old_slot);
- char *new_name = stack_slot_full_name (new_slot);
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible: %s X %s at 0x%04x", i, old_name, new_name, ctx->ip_offset));
- g_free (old_name);
- g_free (new_name);
- }
- set_stack_value (ctx, old_slot, m_class_get_byval_arg (new_class), stack_slot_is_managed_pointer (old_slot));
- goto end_verify;
-
-match_found:
- g_assert (match_class);
- set_stack_value (ctx, old_slot, m_class_get_byval_arg (match_class), stack_slot_is_managed_pointer (old_slot));
- set_stack_value (ctx, new_slot, m_class_get_byval_arg (match_class), stack_slot_is_managed_pointer (old_slot));
- continue;
- }
-
-end_verify:
- if (external)
- to->flags |= IL_CODE_FLAG_WAS_TARGET;
- to->flags |= IL_CODE_STACK_MERGED;
-}
-
-#define HANDLER_START(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER ? (clause)->data.filter_offset : clause->handler_offset)
-#define IS_CATCH_OR_FILTER(clause) ((clause)->flags == MONO_EXCEPTION_CLAUSE_FILTER || (clause)->flags == MONO_EXCEPTION_CLAUSE_NONE)
-
-/**
- * is_clause_in_range :
- *
- * Returns TRUE if either the protected block or the handler of @clause is in the @start - @end range.
- */
-static gboolean
-is_clause_in_range (MonoExceptionClause *clause, guint32 start, guint32 end)
-{
- if (clause->try_offset >= start && clause->try_offset < end)
- return TRUE;
- if (HANDLER_START (clause) >= start && HANDLER_START (clause) < end)
- return TRUE;
- return FALSE;
-}
-
-/**
- * is_clause_inside_range :
- *
- * Returns TRUE if @clause lies completely inside the @start - @end range.
- */
-static gboolean
-is_clause_inside_range (MonoExceptionClause *clause, guint32 start, guint32 end)
-{
- if (clause->try_offset < start || (clause->try_offset + clause->try_len) > end)
- return FALSE;
- if (HANDLER_START (clause) < start || (clause->handler_offset + clause->handler_len) > end)
- return FALSE;
- return TRUE;
-}
-
-/**
- * is_clause_nested :
- *
- * Returns TRUE if @nested is nested in @clause.
- */
-static gboolean
-is_clause_nested (MonoExceptionClause *clause, MonoExceptionClause *nested)
-{
- if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (nested, clause->data.filter_offset, clause->handler_offset))
- return TRUE;
- return is_clause_inside_range (nested, clause->try_offset, clause->try_offset + clause->try_len) ||
- is_clause_inside_range (nested, clause->handler_offset, clause->handler_offset + clause->handler_len);
-}
-
-/* Test the relationship between 2 exception clauses. Follow P.1 12.4.2.7 of ECMA
- * the each pair of exception must have the following properties:
- * - one is fully nested on another (the outer must not be a filter clause) (the nested one must come earlier)
- * - completely disjoin (none of the 3 regions of each entry overlap with the other 3)
- * - mutual protection (protected block is EXACT the same, handlers are disjoin and all handler are catch or all handler are filter)
- */
-static void
-verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
-{
- /*clause is nested*/
- if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER && is_clause_inside_range (clause, to_test->data.filter_offset, to_test->handler_offset)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
- return;
- }
-
- /*wrong nesting order.*/
- if (is_clause_nested (clause, to_test)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Nested exception clause appears after enclosing clause"));
- return;
- }
-
- /*mutual protection*/
- if (clause->try_offset == to_test->try_offset && clause->try_len == to_test->try_len) {
- /*handlers are not disjoint*/
- if (is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception handlers overlap"));
- return;
- }
- /* handlers are not catch or filter */
- if (!IS_CATCH_OR_FILTER (clause) || !IS_CATCH_OR_FILTER (to_test)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses with shared protected block are neither catch or filter"));
- return;
- }
- /*OK*/
- return;
- }
-
- /*not completelly disjoint*/
- if ((is_clause_in_range (to_test, clause->try_offset, clause->try_offset + clause->try_len) ||
- is_clause_in_range (to_test, HANDLER_START (clause), clause->handler_offset + clause->handler_len)) && !is_clause_nested (to_test, clause))
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clauses overlap"));
-}
-
-#define code_bounds_check(size) \
- if (ADDP_IS_GREATER_OR_OVF (ip, size, end)) {\
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
- break; \
- } \
-
-static gboolean
-mono_opcode_is_prefix (int op)
-{
- switch (op) {
- case MONO_CEE_UNALIGNED_:
- case MONO_CEE_VOLATILE_:
- case MONO_CEE_TAIL_:
- case MONO_CEE_CONSTRAINED_:
- case MONO_CEE_READONLY_:
- return TRUE;
- }
- return FALSE;
-}
-
-/*
- * FIXME: need to distinguish between valid and verifiable.
- * Need to keep track of types on the stack.
- */
-
-/**
- * mono_method_verify:
- * Verify types for opcodes.
- */
-GSList*
-mono_method_verify (MonoMethod *method, int level)
-{
- ERROR_DECL (error);
- const unsigned char *ip, *code_start;
- const unsigned char *end;
- MonoSimpleBasicBlock *bb = NULL, *original_bb = NULL;
-
- int i, n, need_merge = 0, start = 0;
- guint ip_offset = 0, prefix = 0;
- MonoGenericContext *generic_context = NULL;
- MonoImage *image;
- VerifyContext ctx;
- GSList *tmp;
- VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name_space, method->klass->name, method->name); );
-
- init_verifier_stats ();
-
- if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
- (method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
- return NULL;
- }
-
- // Disable for now
- if (TRUE)
- return NULL;
-
- memset (&ctx, 0, sizeof (VerifyContext));
-
- //FIXME use mono_method_get_signature_full
- ctx.signature = mono_method_signature_internal (method);
- if (!ctx.signature) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
-
- finish_collect_stats ();
- return ctx.list;
- }
- if (!method->is_generic && !mono_class_is_gtd (method->klass) && ctx.signature->has_type_parameters) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method and signature don't match in terms of genericity"));
- finish_collect_stats ();
- return ctx.list;
- }
-
- ctx.header = mono_method_get_header_checked (method, error);
- if (!ctx.header) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header due to %s", mono_error_get_message (error)));
- mono_error_cleanup (error);
- finish_collect_stats ();
- return ctx.list;
- }
- ctx.method = method;
- code_start = ip = ctx.header->code;
- end = ip + ctx.header->code_size;
- ctx.image = image = m_class_get_image (method->klass);
-
-
- ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
- ctx.max_stack = ctx.header->max_stack;
- ctx.verifiable = ctx.valid = 1;
- ctx.level = level;
-
- ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
- ctx.code_size = ctx.header->code_size;
- _MEM_ALLOC (sizeof (ILCodeDesc) * ctx.header->code_size);
-
- memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
-
- ctx.num_locals = ctx.header->num_locals;
- ctx.locals = (MonoType **)g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->num_locals);
- _MEM_ALLOC (sizeof (MonoType*) * ctx.header->num_locals);
- ctx.locals_verification_state = g_new0 (char, ctx.num_locals);
-
- if (ctx.num_locals > 0 && !ctx.header->init_locals)
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Method with locals variable but without init locals set"));
-
- ctx.params = g_new (MonoType*, ctx.max_args);
- _MEM_ALLOC (sizeof (MonoType*) * ctx.max_args);
-
- if (ctx.signature->hasthis)
- ctx.params [0] = m_class_is_valuetype (method->klass) ? m_class_get_this_arg (method->klass) : m_class_get_byval_arg (method->klass);
- memcpy (ctx.params + ctx.signature->hasthis, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
-
- if (ctx.signature->is_inflated)
- ctx.generic_context = generic_context = mono_method_get_context (method);
-
- if (!generic_context && (mono_class_is_gtd (method->klass) || method->is_generic)) {
- if (method->is_generic)
- ctx.generic_context = generic_context = &(mono_method_get_generic_container (method)->context);
- else
- ctx.generic_context = generic_context = &mono_class_get_generic_container (method->klass)->context;
- }
-
- for (i = 0; i < ctx.num_locals; ++i) {
- MonoType *uninflated = ctx.locals [i];
- ctx.locals [i] = mono_class_inflate_generic_type_checked (ctx.locals [i], ctx.generic_context, error);
- if (!is_ok (error)) {
- char *name = mono_type_full_name (ctx.locals [i] ? ctx.locals [i] : uninflated);
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %d of type %s", i, name));
- g_free (name);
- mono_error_cleanup (error);
- /* we must not free (in cleanup) what was not yet allocated (but only copied) */
- ctx.num_locals = i;
- ctx.max_args = 0;
- goto cleanup;
- }
- }
- for (i = 0; i < ctx.max_args; ++i) {
- MonoType *uninflated = ctx.params [i];
- ctx.params [i] = mono_class_inflate_generic_type_checked (ctx.params [i], ctx.generic_context, error);
- if (!is_ok (error)) {
- char *name = mono_type_full_name (ctx.params [i] ? ctx.params [i] : uninflated);
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %d of type %s", i, name));
- g_free (name);
- mono_error_cleanup (error);
- /* we must not free (in cleanup) what was not yet allocated (but only copied) */
- ctx.max_args = i;
- goto cleanup;
- }
- }
- stack_init (&ctx, &ctx.eval);
-
- for (i = 0; i < ctx.num_locals; ++i) {
- if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i]))
- break;
- if (get_stack_type (ctx.locals [i]) == TYPE_INV) {
- char *name = mono_type_full_name (ctx.locals [i]);
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid local %i of type %s", i, name));
- g_free (name);
- break;
- }
-
- }
-
- for (i = 0; i < ctx.max_args; ++i) {
- if (!mono_type_is_valid_in_context (&ctx, ctx.params [i]))
- break;
-
- if (get_stack_type (ctx.params [i]) == TYPE_INV) {
- char *name = mono_type_full_name (ctx.params [i]);
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid parameter %i of type %s", i, name));
- g_free (name);
- break;
- }
- }
-
- if (!ctx.valid)
- goto cleanup;
-
- for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
- MonoExceptionClause *clause = ctx.header->clauses + i;
- VERIFIER_DEBUG (printf ("clause try %x len %x filter at %x handler at %x len %x\n", clause->try_offset, clause->try_len, clause->data.filter_offset, clause->handler_offset, clause->handler_len); );
-
- if (clause->try_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, ctx.code_size))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
-
- if (clause->try_len <= 0)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause len <= 0 at 0x%04x", clause->try_offset));
-
- if (clause->handler_offset > ctx.code_size || ADD_IS_GREATER_OR_OVF (clause->handler_offset, clause->handler_len, ctx.code_size))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause out of bounds at 0x%04x", clause->try_offset));
-
- if (clause->handler_len <= 0)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("handler clause len <= 0 at 0x%04x", clause->try_offset));
-
- if (clause->try_offset < clause->handler_offset && ADD_IS_GREATER_OR_OVF (clause->try_offset, clause->try_len, HANDLER_START (clause)))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("try block (at 0x%04x) includes handler block (at 0x%04x)", clause->try_offset, clause->handler_offset));
-
- if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
- if (clause->data.filter_offset > ctx.code_size)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause out of bounds at 0x%04x", clause->try_offset));
-
- if (clause->data.filter_offset >= clause->handler_offset)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("filter clause must come before the handler clause at 0x%04x", clause->data.filter_offset));
- }
-
- for (n = i + 1; n < ctx.header->num_clauses && ctx.valid; ++n)
- verify_clause_relationship (&ctx, clause, ctx.header->clauses + n);
-
- if (!ctx.valid)
- break;
-
- ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
- if (clause->try_offset + clause->try_len < ctx.code_size)
- ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
- if (clause->handler_offset + clause->handler_len < ctx.code_size)
- ctx.code [clause->handler_offset + clause->handler_len].flags |= IL_CODE_FLAG_WAS_TARGET;
-
- if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
- if (!clause->data.catch_class) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Catch clause %d with invalid type", i));
- break;
- }
- if (!mono_type_is_valid_in_context (&ctx, m_class_get_byval_arg (clause->data.catch_class)))
- break;
-
- init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
- }
- else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
- init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->data.filter_offset, mono_defaults.exception_class);
- init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, mono_defaults.exception_class);
- }
- }
-
- if (!ctx.valid)
- goto cleanup;
-
- original_bb = bb = mono_basic_block_split (method, error, ctx.header);
- if (!is_ok (error)) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid branch target: %s", mono_error_get_message (error)));
- mono_error_cleanup (error);
- goto cleanup;
- }
- g_assert (bb);
-
- while (ip < end && ctx.valid) {
- int op_size;
- ip_offset = (guint) (ip - code_start);
- {
- const unsigned char *ip_copy = ip;
- MonoOpcodeEnum op;
-
- if (ip_offset > bb->end) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block at [0x%04x] targets middle instruction at 0x%04x", bb->end, ip_offset));
- goto cleanup;
- }
-
- if (ip_offset == bb->end)
- bb = bb->next;
-
- op_size = mono_opcode_value_and_size (&ip_copy, end, &op);
- if (op_size == -1) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ip_offset));
- goto cleanup;
- }
-
- if (ADD_IS_GREATER_OR_OVF (ip_offset, op_size, bb->end)) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets middle of instruction at 0x%04x", ip_offset));
- goto cleanup;
- }
-
- /*Last Instruction*/
- if (ip_offset + op_size == bb->end && mono_opcode_is_prefix (op)) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or EH block targets between prefix '%s' and instruction at 0x%04x", mono_opcode_name (op), ip_offset));
- goto cleanup;
- }
- }
-
- ctx.ip_offset = ip_offset = (guint) (ip - code_start);
-
- /*We need to check against fallthrou in and out of protected blocks.
- * For fallout we check the once a protected block ends, if the start flag is not set.
- * Likewise for fallthru in, we check if ip is the start of a protected block and start is not set
- * TODO convert these checks to be done using flags and not this loop
- */
- for (i = 0; i < ctx.header->num_clauses && ctx.valid; ++i) {
- MonoExceptionClause *clause = ctx.header->clauses + i;
-
- if ((clause->try_offset + clause->try_len == ip_offset) && start == 0) {
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru off try block at 0x%04x", ip_offset));
- start = 1;
- }
-
- if ((clause->handler_offset + clause->handler_len == ip_offset) && start == 0) {
- if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
- else
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallout of handler block at 0x%04x", ip_offset));
- start = 1;
- }
-
- if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER && clause->handler_offset == ip_offset && start == 0) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("fallout of filter block at 0x%04x", ip_offset));
- start = 1;
- }
-
- if (clause->handler_offset == ip_offset && start == 0) {
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("fallthru handler block at 0x%04x", ip_offset));
- start = 1;
- }
-
- if (clause->try_offset == ip_offset && ctx.eval.size > 0 && start == 0) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Try to enter try block with a non-empty stack at 0x%04x", ip_offset));
- start = 1;
- }
- }
-
- /*This must be done after fallthru detection otherwise it won't happen.*/
- if (bb->dead) {
- /*FIXME remove this once we move all bad branch checking code to use BB only*/
- ctx.code [ip_offset].flags |= IL_CODE_FLAG_SEEN;
- ip += op_size;
- continue;
- }
-
- if (!ctx.valid)
- break;
-
- if (need_merge) {
- VERIFIER_DEBUG ( printf ("extra merge needed! 0x%04x \n", ctx.target); );
- merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE, TRUE);
- need_merge = 0;
- }
- merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start, FALSE);
- start = 0;
-
- /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
-#ifdef MONO_VERIFIER_DEBUG
- {
- char *discode;
- discode = mono_disasm_code_one (NULL, method, ip, NULL);
- discode [strlen (discode) - 1] = 0; /* no \n */
- g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
- g_free (discode);
- }
- dump_stack_state (&ctx.code [ip_offset]);
- dump_stack_state (&ctx.eval);
-#endif
-
- switch (*ip) {
- case CEE_NOP:
- case CEE_BREAK:
- ++ip;
- break;
-
- case CEE_LDARG_0:
- case CEE_LDARG_1:
- case CEE_LDARG_2:
- case CEE_LDARG_3:
- push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
- ++ip;
- break;
-
- case CEE_LDARG_S:
- case CEE_LDARGA_S:
- code_bounds_check (2);
- push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
- ip += 2;
- break;
-
- case CEE_ADD_OVF_UN:
- do_binop (&ctx, *ip, add_ovf_un_table);
- ++ip;
- break;
-
- case CEE_SUB_OVF_UN:
- do_binop (&ctx, *ip, sub_ovf_un_table);
- ++ip;
- break;
-
- case CEE_ADD_OVF:
- case CEE_SUB_OVF:
- case CEE_MUL_OVF:
- case CEE_MUL_OVF_UN:
- do_binop (&ctx, *ip, bin_ovf_table);
- ++ip;
- break;
-
- case CEE_ADD:
- do_binop (&ctx, *ip, add_table);
- ++ip;
- break;
-
- case CEE_SUB:
- do_binop (&ctx, *ip, sub_table);
- ++ip;
- break;
-
- case CEE_MUL:
- case CEE_DIV:
- case CEE_REM:
- do_binop (&ctx, *ip, bin_op_table);
- ++ip;
- break;
-
- case CEE_AND:
- case CEE_DIV_UN:
- case CEE_OR:
- case CEE_REM_UN:
- case CEE_XOR:
- do_binop (&ctx, *ip, int_bin_op_table);
- ++ip;
- break;
-
- case CEE_SHL:
- case CEE_SHR:
- case CEE_SHR_UN:
- do_binop (&ctx, *ip, shift_op_table);
- ++ip;
- break;
-
- case CEE_POP:
- if (!check_underflow (&ctx, 1))
- break;
- stack_pop_safe (&ctx);
- ++ip;
- break;
-
- case CEE_RET:
- do_ret (&ctx);
- ++ip;
- start = 1;
- break;
-
- case CEE_LDLOC_0:
- case CEE_LDLOC_1:
- case CEE_LDLOC_2:
- case CEE_LDLOC_3:
- /*TODO support definite assignment verification? */
- push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
- ++ip;
- break;
-
- case CEE_STLOC_0:
- case CEE_STLOC_1:
- case CEE_STLOC_2:
- case CEE_STLOC_3:
- store_local (&ctx, *ip - CEE_STLOC_0);
- ++ip;
- break;
-
- case CEE_STLOC_S:
- code_bounds_check (2);
- store_local (&ctx, ip [1]);
- ip += 2;
- break;
-
- case CEE_STARG_S:
- code_bounds_check (2);
- store_arg (&ctx, ip [1]);
- ip += 2;
- break;
-
- case CEE_LDC_I4_M1:
- case CEE_LDC_I4_0:
- case CEE_LDC_I4_1:
- case CEE_LDC_I4_2:
- case CEE_LDC_I4_3:
- case CEE_LDC_I4_4:
- case CEE_LDC_I4_5:
- case CEE_LDC_I4_6:
- case CEE_LDC_I4_7:
- case CEE_LDC_I4_8:
- if (check_overflow (&ctx))
- stack_push_val (&ctx, TYPE_I4, mono_get_int32_type ());
- ++ip;
- break;
-
- case CEE_LDC_I4_S:
- code_bounds_check (2);
- if (check_overflow (&ctx))
- stack_push_val (&ctx, TYPE_I4, mono_get_int32_type ());
- ip += 2;
- break;
-
- case CEE_LDC_I4:
- code_bounds_check (5);
- if (check_overflow (&ctx))
- stack_push_val (&ctx,TYPE_I4, mono_get_int32_type ());
- ip += 5;
- break;
-
- case CEE_LDC_I8:
- code_bounds_check (9);
- if (check_overflow (&ctx))
- stack_push_val (&ctx,TYPE_I8, m_class_get_byval_arg (mono_defaults.int64_class));
- ip += 9;
- break;
-
- case CEE_LDC_R4:
- code_bounds_check (5);
- if (check_overflow (&ctx))
- stack_push_val (&ctx, TYPE_R8, m_class_get_byval_arg (mono_defaults.double_class));
- ip += 5;
- break;
-
- case CEE_LDC_R8:
- code_bounds_check (9);
- if (check_overflow (&ctx))
- stack_push_val (&ctx, TYPE_R8, m_class_get_byval_arg (mono_defaults.double_class));
- ip += 9;
- break;
-
- case CEE_LDNULL:
- if (check_overflow (&ctx))
- stack_push_val (&ctx, TYPE_COMPLEX | NULL_LITERAL_MASK, mono_get_object_type ());
- ++ip;
- break;
-
- case CEE_BEQ_S:
- case CEE_BNE_UN_S:
- code_bounds_check (2);
- do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
- ip += 2;
- need_merge = 1;
- break;
-
- case CEE_BGE_S:
- case CEE_BGT_S:
- case CEE_BLE_S:
- case CEE_BLT_S:
- case CEE_BGE_UN_S:
- case CEE_BGT_UN_S:
- case CEE_BLE_UN_S:
- case CEE_BLT_UN_S:
- code_bounds_check (2);
- do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
- ip += 2;
- need_merge = 1;
- break;
-
- case CEE_BEQ:
- case CEE_BNE_UN:
- code_bounds_check (5);
- do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
- ip += 5;
- need_merge = 1;
- break;
-
- case CEE_BGE:
- case CEE_BGT:
- case CEE_BLE:
- case CEE_BLT:
- case CEE_BGE_UN:
- case CEE_BGT_UN:
- case CEE_BLE_UN:
- case CEE_BLT_UN:
- code_bounds_check (5);
- do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
- ip += 5;
- need_merge = 1;
- break;
-
- case CEE_LDLOC_S:
- case CEE_LDLOCA_S:
- code_bounds_check (2);
- push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
- ip += 2;
- break;
-
- case CEE_UNUSED99:
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
- ++ip;
- break;
-
- case CEE_DUP: {
- ILStackDesc *top;
- if (!check_underflow (&ctx, 1))
- break;
- if (!check_overflow (&ctx))
- break;
- top = stack_push (&ctx);
- copy_stack_value (top, stack_peek (&ctx, 1));
- ++ip;
- break;
- }
-
- case CEE_JMP:
- code_bounds_check (5);
- if (ctx.eval.size)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
- /* token = read32 (ip + 1); */
- if (in_any_block (ctx.header, ip_offset))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("jmp cannot escape exception blocks at 0x%04x", ip_offset));
-
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction jmp is not verifiable at 0x%04x", ctx.ip_offset));
- /*
- * FIXME: check signature, retval, arguments etc.
- */
- ip += 5;
- break;
- case CEE_CALL:
- case CEE_CALLVIRT:
- code_bounds_check (5);
- do_invoke_method (&ctx, read32 (ip + 1), *ip == CEE_CALLVIRT);
- ip += 5;
- break;
-
- case CEE_CALLI:
- code_bounds_check (5);
- /* token = read32 (ip + 1); */
- /*
- * FIXME: check signature, retval, arguments etc.
- * FIXME: check requirements for tail call
- */
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Intruction calli is not verifiable at 0x%04x", ctx.ip_offset));
- ip += 5;
- break;
- case CEE_BR_S:
- code_bounds_check (2);
- do_static_branch (&ctx, (signed char)ip [1] + 2);
- need_merge = 1;
- ip += 2;
- start = 1;
- break;
-
- case CEE_BRFALSE_S:
- case CEE_BRTRUE_S:
- code_bounds_check (2);
- do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
- ip += 2;
- need_merge = 1;
- break;
-
- case CEE_BR:
- code_bounds_check (5);
- do_static_branch (&ctx, (gint32)read32 (ip + 1) + 5);
- need_merge = 1;
- ip += 5;
- start = 1;
- break;
-
- case CEE_BRFALSE:
- case CEE_BRTRUE:
- code_bounds_check (5);
- do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
- ip += 5;
- need_merge = 1;
- break;
-
- case CEE_SWITCH: {
- guint32 entries;
- code_bounds_check (5);
- entries = read32 (ip + 1);
-
- if (entries > 0xFFFFFFFFU / sizeof (guint32))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Too many switch entries %x at 0x%04x", entries, ctx.ip_offset));
-
- ip += 5;
- code_bounds_check (sizeof (guint32) * entries);
-
- do_switch (&ctx, entries, ip);
- ip += sizeof (guint32) * entries;
- break;
- }
- case CEE_LDIND_I1:
- case CEE_LDIND_U1:
- case CEE_LDIND_I2:
- case CEE_LDIND_U2:
- case CEE_LDIND_I4:
- case CEE_LDIND_U4:
- case CEE_LDIND_I8:
- case CEE_LDIND_I:
- case CEE_LDIND_R4:
- case CEE_LDIND_R8:
- case CEE_LDIND_REF:
- do_load_indirect (&ctx, *ip);
- ++ip;
- break;
-
- case CEE_STIND_REF:
- case CEE_STIND_I1:
- case CEE_STIND_I2:
- case CEE_STIND_I4:
- case CEE_STIND_I8:
- case CEE_STIND_R4:
- case CEE_STIND_R8:
- case CEE_STIND_I:
- do_store_indirect (&ctx, *ip);
- ++ip;
- break;
-
- case CEE_NOT:
- case CEE_NEG:
- do_unary_math_op (&ctx, *ip);
- ++ip;
- break;
-
- case CEE_CONV_I1:
- case CEE_CONV_I2:
- case CEE_CONV_I4:
- case CEE_CONV_U1:
- case CEE_CONV_U2:
- case CEE_CONV_U4:
- do_conversion (&ctx, TYPE_I4);
- ++ip;
- break;
-
- case CEE_CONV_I8:
- case CEE_CONV_U8:
- do_conversion (&ctx, TYPE_I8);
- ++ip;
- break;
-
- case CEE_CONV_R4:
- case CEE_CONV_R8:
- case CEE_CONV_R_UN:
- do_conversion (&ctx, TYPE_R8);
- ++ip;
- break;
-
- case CEE_CONV_I:
- case CEE_CONV_U:
- do_conversion (&ctx, TYPE_NATIVE_INT);
- ++ip;
- break;
-
- case CEE_CPOBJ:
- code_bounds_check (5);
- do_cpobj (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_LDOBJ:
- code_bounds_check (5);
- do_ldobj_value (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_LDSTR:
- code_bounds_check (5);
- do_ldstr (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_NEWOBJ:
- code_bounds_check (5);
- do_newobj (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_CASTCLASS:
- case CEE_ISINST:
- code_bounds_check (5);
- do_cast (&ctx, read32 (ip + 1), *ip == CEE_CASTCLASS ? "castclass" : "isinst");
- ip += 5;
- break;
-
- case CEE_UNUSED58:
- case CEE_UNUSED1:
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
- ++ip;
- break;
-
- case CEE_UNBOX:
- code_bounds_check (5);
- do_unbox_value (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_THROW:
- do_throw (&ctx);
- start = 1;
- ++ip;
- break;
-
- case CEE_LDFLD:
- case CEE_LDFLDA:
- code_bounds_check (5);
- do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
- ip += 5;
- break;
-
- case CEE_LDSFLD:
- case CEE_LDSFLDA:
- code_bounds_check (5);
- do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
- ip += 5;
- break;
-
- case CEE_STFLD:
- code_bounds_check (5);
- do_store_field (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_STSFLD:
- code_bounds_check (5);
- do_store_static_field (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_STOBJ:
- code_bounds_check (5);
- do_stobj (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_CONV_OVF_I1_UN:
- case CEE_CONV_OVF_I2_UN:
- case CEE_CONV_OVF_I4_UN:
- case CEE_CONV_OVF_U1_UN:
- case CEE_CONV_OVF_U2_UN:
- case CEE_CONV_OVF_U4_UN:
- do_conversion (&ctx, TYPE_I4);
- ++ip;
- break;
-
- case CEE_CONV_OVF_I8_UN:
- case CEE_CONV_OVF_U8_UN:
- do_conversion (&ctx, TYPE_I8);
- ++ip;
- break;
-
- case CEE_CONV_OVF_I_UN:
- case CEE_CONV_OVF_U_UN:
- do_conversion (&ctx, TYPE_NATIVE_INT);
- ++ip;
- break;
-
- case CEE_BOX:
- code_bounds_check (5);
- do_box_value (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_NEWARR:
- code_bounds_check (5);
- do_newarr (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_LDLEN:
- do_ldlen (&ctx);
- ++ip;
- break;
-
- case CEE_LDELEMA:
- code_bounds_check (5);
- do_ldelema (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_LDELEM_I1:
- case CEE_LDELEM_U1:
- case CEE_LDELEM_I2:
- case CEE_LDELEM_U2:
- case CEE_LDELEM_I4:
- case CEE_LDELEM_U4:
- case CEE_LDELEM_I8:
- case CEE_LDELEM_I:
- case CEE_LDELEM_R4:
- case CEE_LDELEM_R8:
- case CEE_LDELEM_REF:
- do_ldelem (&ctx, *ip, 0);
- ++ip;
- break;
-
- case CEE_STELEM_I:
- case CEE_STELEM_I1:
- case CEE_STELEM_I2:
- case CEE_STELEM_I4:
- case CEE_STELEM_I8:
- case CEE_STELEM_R4:
- case CEE_STELEM_R8:
- case CEE_STELEM_REF:
- do_stelem (&ctx, *ip, 0);
- ++ip;
- break;
-
- case CEE_LDELEM:
- code_bounds_check (5);
- do_ldelem (&ctx, *ip, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_STELEM:
- code_bounds_check (5);
- do_stelem (&ctx, *ip, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_UNBOX_ANY:
- code_bounds_check (5);
- do_unbox_any (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_CONV_OVF_I1:
- case CEE_CONV_OVF_U1:
- case CEE_CONV_OVF_I2:
- case CEE_CONV_OVF_U2:
- case CEE_CONV_OVF_I4:
- case CEE_CONV_OVF_U4:
- do_conversion (&ctx, TYPE_I4);
- ++ip;
- break;
-
- case CEE_CONV_OVF_I8:
- case CEE_CONV_OVF_U8:
- do_conversion (&ctx, TYPE_I8);
- ++ip;
- break;
-
- case CEE_CONV_OVF_I:
- case CEE_CONV_OVF_U:
- do_conversion (&ctx, TYPE_NATIVE_INT);
- ++ip;
- break;
-
- case CEE_REFANYVAL:
- code_bounds_check (5);
- do_refanyval (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_CKFINITE:
- do_ckfinite (&ctx);
- ++ip;
- break;
-
- case CEE_MKREFANY:
- code_bounds_check (5);
- do_mkrefany (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_LDTOKEN:
- code_bounds_check (5);
- do_load_token (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_ENDFINALLY:
- if (!is_correct_endfinally (ctx.header, ip_offset))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("endfinally must be used inside a finally/fault handler at 0x%04x", ctx.ip_offset));
- ctx.eval.size = 0;
- start = 1;
- ++ip;
- break;
-
- case CEE_LEAVE:
- code_bounds_check (5);
- do_leave (&ctx, read32 (ip + 1) + 5);
- ip += 5;
- start = 1;
- need_merge = 1;
- break;
-
- case CEE_LEAVE_S:
- code_bounds_check (2);
- do_leave (&ctx, (signed char)ip [1] + 2);
- ip += 2;
- start = 1;
- need_merge = 1;
- break;
-
- case CEE_PREFIX1:
- code_bounds_check (2);
- ++ip;
- switch (*ip) {
- case CEE_STLOC:
- code_bounds_check (3);
- store_local (&ctx, read16 (ip + 1));
- ip += 3;
- break;
-
- case CEE_CEQ:
- do_cmp_op (&ctx, cmp_br_eq_op, *ip);
- ++ip;
- break;
-
- case CEE_CGT:
- case CEE_CGT_UN:
- case CEE_CLT:
- case CEE_CLT_UN:
- do_cmp_op (&ctx, cmp_br_op, *ip);
- ++ip;
- break;
-
- case CEE_STARG:
- code_bounds_check (3);
- store_arg (&ctx, read16 (ip + 1) );
- ip += 3;
- break;
-
-
- case CEE_ARGLIST:
- if (!check_overflow (&ctx))
- break;
- if (ctx.signature->call_convention != MONO_CALL_VARARG)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot use arglist on method without VARGARG calling convention at 0x%04x", ctx.ip_offset));
- set_stack_value (&ctx, stack_push (&ctx), m_class_get_byval_arg (mono_defaults.argumenthandle_class), FALSE);
- ++ip;
- break;
-
- case CEE_LDFTN:
- code_bounds_check (5);
- do_load_function_ptr (&ctx, read32 (ip + 1), FALSE);
- ip += 5;
- break;
-
- case CEE_LDVIRTFTN:
- code_bounds_check (5);
- do_load_function_ptr (&ctx, read32 (ip + 1), TRUE);
- ip += 5;
- break;
-
- case CEE_LDARG:
- case CEE_LDARGA:
- code_bounds_check (3);
- push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
- ip += 3;
- break;
-
- case CEE_LDLOC:
- case CEE_LDLOCA:
- code_bounds_check (3);
- push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
- ip += 3;
- break;
-
- case CEE_LOCALLOC:
- do_localloc (&ctx);
- ++ip;
- break;
-
- case CEE_UNUSED56:
- case CEE_UNUSED57:
- case CEE_UNUSED70:
- case CEE_UNUSED:
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Use of the `unused' opcode"));
- ++ip;
- break;
- case CEE_ENDFILTER:
- do_endfilter (&ctx);
- start = 1;
- ++ip;
- break;
- case CEE_UNALIGNED_:
- code_bounds_check (2);
- prefix |= PREFIX_UNALIGNED;
- ip += 2;
- break;
- case CEE_VOLATILE_:
- prefix |= PREFIX_VOLATILE;
- ++ip;
- break;
- case CEE_TAIL_:
- prefix |= PREFIX_TAIL;
- ++ip;
- if (ip < end && (*ip != CEE_CALL && *ip != CEE_CALLI && *ip != CEE_CALLVIRT))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("tail prefix must be used only with call opcodes at 0x%04x", ip_offset));
- break;
-
- case CEE_INITOBJ:
- code_bounds_check (5);
- do_initobj (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_CONSTRAINED_:
- code_bounds_check (5);
- ctx.constrained_type = get_boxable_mono_type (&ctx, read32 (ip + 1), "constrained.");
- prefix |= PREFIX_CONSTRAINED;
- ip += 5;
- break;
-
- case CEE_READONLY_:
- prefix |= PREFIX_READONLY;
- ip++;
- break;
-
- case CEE_CPBLK:
- CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
- if (!check_underflow (&ctx, 3))
- break;
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction cpblk is not verifiable at 0x%04x", ctx.ip_offset));
- ip++;
- break;
-
- case CEE_INITBLK:
- CLEAR_PREFIX (&ctx, PREFIX_UNALIGNED | PREFIX_VOLATILE);
- if (!check_underflow (&ctx, 3))
- break;
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Instruction initblk is not verifiable at 0x%04x", ctx.ip_offset));
- ip++;
- break;
-
- case CEE_NO_:
- ip += 2;
- break;
- case CEE_RETHROW:
- if (!is_correct_rethrow (ctx.header, ip_offset))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("rethrow must be used inside a catch handler at 0x%04x", ctx.ip_offset));
- ctx.eval.size = 0;
- start = 1;
- ++ip;
- break;
-
- case CEE_SIZEOF:
- code_bounds_check (5);
- do_sizeof (&ctx, read32 (ip + 1));
- ip += 5;
- break;
-
- case CEE_REFANYTYPE:
- do_refanytype (&ctx);
- ++ip;
- break;
-
- default:
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
- ++ip;
- }
- break;
-
- default:
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction %x at 0x%04x", *ip, ctx.ip_offset));
- ++ip;
- }
-
- /*TODO we can fast detect a forward branch or exception block targeting code after prefix, we should fail fast*/
- if (prefix) {
- if (!ctx.prefix_set) //first prefix
- ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
- ctx.prefix_set |= prefix;
- ctx.has_flags = TRUE;
- prefix = 0;
- } else {
- if (!ctx.has_flags)
- ctx.code [ctx.ip_offset].flags |= IL_CODE_FLAG_SEEN;
-
- if (ctx.prefix_set & PREFIX_CONSTRAINED)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after constrained prefix at 0x%04x", ctx.ip_offset));
- if (ctx.prefix_set & PREFIX_READONLY)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after readonly prefix at 0x%04x", ctx.ip_offset));
- if (ctx.prefix_set & PREFIX_VOLATILE)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after volatile prefix at 0x%04x", ctx.ip_offset));
- if (ctx.prefix_set & PREFIX_UNALIGNED)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction after unaligned prefix at 0x%04x", ctx.ip_offset));
- ctx.prefix_set = prefix = 0;
- ctx.has_flags = FALSE;
- }
- }
- /*
- * if ip != end we overflowed: mark as error.
- */
- if ((ip != end || !start) && ctx.verifiable && !ctx.list) {
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Run ahead of method code at 0x%04x", ip_offset));
- }
-
- /*We should guard against the last decoded opcode, otherwise we might add errors that doesn't make sense.*/
- for (i = 0; i < ctx.code_size && i < ip_offset; ++i) {
- if (ctx.code [i].flags & IL_CODE_FLAG_WAS_TARGET) {
- if (!(ctx.code [i].flags & IL_CODE_FLAG_SEEN))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch or exception block target middle of instruction at 0x%04x", i));
-
- if (ctx.code [i].flags & IL_CODE_DELEGATE_SEQUENCE)
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Branch to delegate code sequence at 0x%04x", i));
- }
- if ((ctx.code [i].flags & IL_CODE_LDFTN_DELEGATE_NONFINAL_VIRTUAL) && ctx.has_this_store)
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid ldftn with virtual function in method with stdarg 0 at 0x%04x", i));
-
- if ((ctx.code [i].flags & IL_CODE_CALL_NONFINAL_VIRTUAL) && ctx.has_this_store)
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Invalid call to a non-final virtual function in method with stdarg.0 or ldarga.0 at 0x%04x", i));
- }
-
- if (mono_method_is_constructor (ctx.method) && !ctx.super_ctor_called && !m_class_is_valuetype (ctx.method->klass) && ctx.method->klass != mono_defaults.object_class) {
- char *method_name = mono_method_full_name (ctx.method, TRUE);
- char *type = mono_type_get_full_name (ctx.method->klass);
- if (m_class_get_parent (ctx.method->klass) && mono_class_has_failure (m_class_get_parent (ctx.method->klass)))
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor due to a TypeLoadException on base type.", method_name, type));
- else
- CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor %s for type %s not calling base type ctor.", method_name, type));
- g_free (method_name);
- g_free (type);
- }
-
-cleanup:
- if (ctx.code) {
- for (i = 0; i < ctx.header->code_size; ++i) {
- if (ctx.code [i].stack)
- g_free (ctx.code [i].stack);
- }
- }
-
- for (tmp = ctx.funptrs; tmp; tmp = tmp->next)
- g_free (tmp->data);
- g_slist_free (ctx.funptrs);
-
- for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
- mono_metadata_free_type ((MonoType *)tmp->data);
- g_slist_free (ctx.exception_types);
-
- for (i = 0; i < ctx.num_locals; ++i) {
- if (ctx.locals [i])
- mono_metadata_free_type (ctx.locals [i]);
- }
- for (i = 0; i < ctx.max_args; ++i) {
- if (ctx.params [i])
- mono_metadata_free_type (ctx.params [i]);
- }
-
- if (ctx.eval.stack)
- g_free (ctx.eval.stack);
- if (ctx.code)
- g_free (ctx.code);
- g_free (ctx.locals);
- g_free (ctx.locals_verification_state);
- g_free (ctx.params);
- mono_basic_block_free (original_bb);
- mono_metadata_free_mh (ctx.header);
-
- finish_collect_stats ();
- return ctx.list;
-}
-
-char*
-mono_verify_corlib ()
-{
- /* This is a public API function so cannot be removed */
- return NULL;
-}
-
-/**
- * mono_verifier_is_enabled_for_method:
- * \param method the method to probe
- * \returns TRUE if \p method needs to be verified.
- */
-gboolean
-mono_verifier_is_enabled_for_method (MonoMethod *method)
-{
- return mono_verifier_is_enabled_for_class (method->klass) && (method->wrapper_type == MONO_WRAPPER_NONE || method->wrapper_type == MONO_WRAPPER_DYNAMIC_METHOD);
-}
-
-/**
- * mono_verifier_is_enabled_for_class:
- * \param klass The \c MonoClass to probe
- * \returns TRUE if \p klass need to be verified.
- */
-gboolean
-mono_verifier_is_enabled_for_class (MonoClass *klass)
-{
- MonoImage *image = m_class_get_image (klass);
- return verify_all || (verifier_mode > MONO_VERIFIER_PE_ONLY && image != mono_defaults.corlib);
-}
-
-gboolean
-mono_verifier_is_enabled_for_image (MonoImage *image)
-{
- return verify_all || verifier_mode > MONO_VERIFIER_PE_ONLY;
-}
-
-gboolean
-mono_verifier_is_enabled_for_pe_only ()
-{
- return verify_all || verifier_mode == MONO_VERIFIER_PE_ONLY;
-}
-
-/*
- * Dynamic methods are not considered full trust since if the user is trusted and need to
- * generate unsafe code, make the method skip verification - this is a known good way to do it.
- */
-gboolean
-mono_verifier_is_method_full_trust (MonoMethod *method)
-{
- return mono_verifier_is_class_full_trust (method->klass) && !method_is_dynamic (method);
-}
-
-/*
- * Returns if @klass is under full trust or not.
- *
- * TODO This code doesn't take CAS into account.
- *
- * Under verify_all all user code must be verifiable if no security option was set
- *
- */
-gboolean
-mono_verifier_is_class_full_trust (MonoClass *klass)
-{
- MonoImage *image = m_class_get_image (klass);
-
- if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
- return image == mono_defaults.corlib;
- return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || image == mono_defaults.corlib;
-}
-
-GSList*
-mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
-{
- return mono_method_verify (method,
- (verifier_mode != MONO_VERIFIER_MODE_STRICT ? MONO_VERIFY_NON_STRICT: 0)
- | (!is_fulltrust && !mono_verifier_is_method_full_trust (method) ? MONO_VERIFY_FAIL_FAST : 0)
- | (skip_visibility ? MONO_VERIFY_SKIP_VISIBILITY : 0));
-}
-
-static int
-get_field_end (MonoClassField *field)
-{
- int align;
- int size = mono_type_size (field->type, &align);
- if (size == 0)
- size = 4; /*FIXME Is this a safe bet?*/
- return size + field->offset;
-}
-
-static gboolean
-verify_class_for_overlapping_reference_fields (MonoClass *klass)
-{
- int i = 0, j;
- gpointer iter = NULL;
- MonoClassField *field;
- gboolean is_fulltrust = mono_verifier_is_class_full_trust (klass);
- /*We can't skip types with !has_references since this is calculated after we have run.*/
- if (!mono_class_is_explicit_layout (klass))
- return TRUE;
-
-
- /*We must check for stuff overlapping reference fields.
- The outer loop uses mono_class_get_fields_internal to ensure that MonoClass:fields get inited.
- */
- while ((field = mono_class_get_fields_internal (klass, &iter))) {
- int fieldEnd = get_field_end (field);
- gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
- ++i;
-
- if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
- continue;
-
- int fcount = mono_class_get_field_count (klass);
- MonoClassField *klass_fields = m_class_get_fields (klass);
- for (j = i; j < fcount; ++j) {
- MonoClassField *other = &klass_fields [j];
- int otherEnd = get_field_end (other);
- if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)) || (other->type->attrs & FIELD_ATTRIBUTE_STATIC))
- continue;
-
- if (!is_valuetype && MONO_TYPE_IS_REFERENCE (other->type) && field->offset == other->offset && is_fulltrust)
- continue;
-
- if ((otherEnd > field->offset && otherEnd <= fieldEnd) || (other->offset >= field->offset && other->offset < fieldEnd))
- return FALSE;
- }
- }
- return TRUE;
-}
-
-static guint
-field_hash (gconstpointer key)
-{
- const MonoClassField *field = (const MonoClassField *)key;
- return g_str_hash (field->name) ^ mono_metadata_type_hash (field->type); /**/
-}
-
-static gboolean
-field_equals (gconstpointer _a, gconstpointer _b)
-{
- const MonoClassField *a = (const MonoClassField *)_a;
- const MonoClassField *b = (const MonoClassField *)_b;
- return !strcmp (a->name, b->name) && mono_metadata_type_equal (a->type, b->type);
-}
-
-
-static gboolean
-verify_class_fields (MonoClass *klass)
-{
- gpointer iter = NULL;
- MonoClassField *field;
- MonoGenericContext *context = mono_class_get_context (klass);
- GHashTable *unique_fields = g_hash_table_new_full (&field_hash, &field_equals, NULL, NULL);
- if (mono_class_is_gtd (klass))
- context = &mono_class_get_generic_container (klass)->context;
-
- while ((field = mono_class_get_fields_internal (klass, &iter)) != NULL) {
- if (!mono_type_is_valid_type_in_context (field->type, context)) {
- g_hash_table_destroy (unique_fields);
- return FALSE;
- }
- if (g_hash_table_lookup (unique_fields, field)) {
- g_hash_table_destroy (unique_fields);
- return FALSE;
- }
- g_hash_table_insert (unique_fields, field, field);
- }
- g_hash_table_destroy (unique_fields);
- return TRUE;
-}
-
-static gboolean
-verify_interfaces (MonoClass *klass)
-{
- int i;
- guint16 klass_interface_count = m_class_get_interface_count (klass);
- MonoClass **klass_interfaces = m_class_get_interfaces (klass);
- for (i = 0; i < klass_interface_count; ++i) {
- MonoClass *iface = klass_interfaces [i];
- if (!mono_class_get_flags (iface))
- return FALSE;
- }
- return TRUE;
-}
-
-static gboolean
-verify_valuetype_layout_with_target (MonoClass *klass, MonoClass *target_class)
-{
- int type;
- gpointer iter = NULL;
- MonoClassField *field;
- MonoClass *field_class;
-
- if (!m_class_is_valuetype (klass))
- return TRUE;
-
- type = m_class_get_byval_arg (klass)->type;
- /*primitive type fields are not properly decoded*/
- if ((type >= MONO_TYPE_BOOLEAN && type <= MONO_TYPE_R8) || (type >= MONO_TYPE_I && type <= MONO_TYPE_U))
- return TRUE;
-
- while ((field = mono_class_get_fields_internal (klass, &iter)) != NULL) {
- if (!field->type)
- return FALSE;
-
- if (field->type->attrs & (FIELD_ATTRIBUTE_STATIC | FIELD_ATTRIBUTE_HAS_FIELD_RVA))
- continue;
-
- field_class = mono_class_get_generic_type_definition (mono_class_from_mono_type_internal (field->type));
-
- if (field_class == target_class || klass == field_class || !verify_valuetype_layout_with_target (field_class, target_class))
- return FALSE;
- }
-
- return TRUE;
-}
-
-static gboolean
-verify_valuetype_layout (MonoClass *klass)
-{
- gboolean res;
- res = verify_valuetype_layout_with_target (klass, klass);
- return res;
-}
-
-static gboolean
-recursive_mark_constraint_args (MonoBitSet *used_args, MonoGenericContainer *gc, MonoType *type)
-{
- int idx;
- MonoClass **constraints;
- MonoGenericParamInfo *param_info;
-
- g_assert (mono_type_is_generic_argument (type));
-
- idx = mono_type_get_generic_param_num (type);
- if (mono_bitset_test_fast (used_args, idx))
- return FALSE;
-
- mono_bitset_set_fast (used_args, idx);
- param_info = mono_generic_container_get_param_info (gc, idx);
-
- if (!param_info->constraints)
- return TRUE;
-
- for (constraints = param_info->constraints; *constraints; ++constraints) {
- MonoClass *ctr = *constraints;
- MonoType *constraint_type = m_class_get_byval_arg (ctr);
-
- if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
- return FALSE;
- }
- return TRUE;
-}
-
-static gboolean
-verify_generic_parameters (MonoClass *klass)
-{
- int i;
- MonoGenericContainer *gc = mono_class_get_generic_container (klass);
- MonoBitSet *used_args = mono_bitset_new (gc->type_argc, 0);
-
- for (i = 0; i < gc->type_argc; ++i) {
- MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
- MonoClass **constraints;
-
- if (!param_info->constraints)
- continue;
-
- mono_bitset_clear_all (used_args);
- mono_bitset_set_fast (used_args, i);
-
- for (constraints = param_info->constraints; *constraints; ++constraints) {
- MonoClass *ctr = *constraints;
- MonoType *constraint_type = m_class_get_byval_arg (ctr);
-
- if (!mono_class_can_access_class (klass, ctr))
- goto fail;
-
- if (!mono_type_is_valid_type_in_context (constraint_type, &gc->context))
- goto fail;
-
- if (mono_type_is_generic_argument (constraint_type) && !recursive_mark_constraint_args (used_args, gc, constraint_type))
- goto fail;
- if (mono_class_is_ginst (ctr) && !mono_class_is_valid_generic_instantiation (NULL, ctr))
- goto fail;
- }
- }
- mono_bitset_free (used_args);
- return TRUE;
-
-fail:
- mono_bitset_free (used_args);
- return FALSE;
-}
-
-/*
- * Check if the class is verifiable.
- *
- * Right now there are no conditions that make a class a valid but not verifiable. Both overlapping reference
- * field and invalid generic instantiation are fatal errors.
- *
- * This method must be safe to be called from mono_class_init_internal and all code must be carefull about that.
- *
- */
-gboolean
-mono_verifier_verify_class (MonoClass *klass)
-{
- MonoClass *klass_parent = m_class_get_parent (klass);
- /*Neither <Module>, object or ifaces have parent.*/
- if (!klass_parent &&
- klass != mono_defaults.object_class &&
- !MONO_CLASS_IS_INTERFACE_INTERNAL (klass) &&
- (!image_is_dynamic (m_class_get_image (klass)) && m_class_get_type_token (klass) != 0x2000001)) /*<Module> is the first type in the assembly*/
- return FALSE;
- if (m_class_get_parent (klass)) {
- if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass_parent))
- return FALSE;
- if (!mono_class_is_ginst (klass) && mono_class_is_gtd (klass_parent))
- return FALSE;
- if (mono_class_is_ginst (klass_parent) && !mono_class_is_ginst (klass)) {
- MonoGenericContext *context = mono_class_get_context (klass);
- if (mono_class_is_gtd (klass))
- context = &mono_class_get_generic_container (klass)->context;
- if (!mono_type_is_valid_type_in_context (m_class_get_byval_arg (klass_parent), context))
- return FALSE;
- }
- }
- if (mono_class_is_gtd (klass) && (mono_class_is_explicit_layout (klass)))
- return FALSE;
- if (mono_class_is_gtd (klass) && !verify_generic_parameters (klass))
- return FALSE;
- if (!verify_class_for_overlapping_reference_fields (klass))
- return FALSE;
- if (mono_class_is_ginst (klass) && !mono_class_is_valid_generic_instantiation (NULL, klass))
- return FALSE;
- if (!mono_class_is_ginst (klass) && !verify_class_fields (klass))
- return FALSE;
- if (m_class_is_valuetype (klass) && !verify_valuetype_layout (klass))
- return FALSE;
- if (!verify_interfaces (klass))
- return FALSE;
- return TRUE;
-}
-
-#else
-
-gboolean
-mono_verifier_verify_class (MonoClass *klass)
-{
- /* The verifier was disabled at compile time */
- return TRUE;
-}
-
-GSList*
-mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility, gboolean is_fulltrust)
-{
- /* The verifier was disabled at compile time */
- return NULL;
-}
-
-gboolean
-mono_verifier_is_class_full_trust (MonoClass *klass)
-{
- /* The verifier was disabled at compile time */
- return TRUE;
-}
-
-gboolean
-mono_verifier_is_method_full_trust (MonoMethod *method)
-{
- /* The verifier was disabled at compile time */
- return TRUE;
-}
-
-gboolean
-mono_verifier_is_enabled_for_image (MonoImage *image)
-{
- /* The verifier was disabled at compile time */
- return FALSE;
-}
-
-gboolean
-mono_verifier_is_enabled_for_class (MonoClass *klass)
-{
- /* The verifier was disabled at compile time */
- return FALSE;
-}
-
-gboolean
-mono_verifier_is_enabled_for_method (MonoMethod *method)
-{
- /* The verifier was disabled at compile time */
- return FALSE;
-}
-
-GSList*
-mono_method_verify (MonoMethod *method, int level)
-{
- /* The verifier was disabled at compile time */
- return NULL;
-}
-
-void
-mono_free_verify_list (GSList *list)
-{
- /* The verifier was disabled at compile time */
- /* will always be null if verifier is disabled */
-}
-
-#endif