+/*
+ * verify.c:
+ *
+ * Author:
+ * Mono Project (http://www.mono-project.com)
+ *
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
+ */
+#include <config.h>
#include <mono/metadata/object-internals.h>
#include <mono/metadata/verify.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/class-internals.h>
+#include <mono/metadata/security-manager.h>
+#include <mono/metadata/security-core-clr.h>
#include <mono/metadata/tokentype.h>
#include <string.h>
#include <signal.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;
+}
+
+#ifndef DISABLE_VERIFIER
/*
* Pull the list of opcodes
*/
#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); \
(__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); \
#define CODE_NOT_VERIFIABLE(__ctx, __msg) \
do { \
- if ((__ctx)->verifiable) { \
+ 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)) \
#define CODE_NOT_VERIFIABLE2(__ctx, __msg, __exception) \
do { \
- if ((__ctx)->verifiable) { \
+ 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)) \
IL_CODE_CALL_NONFINAL_VIRTUAL = 0x40,
};
+typedef enum {
+ RESULT_VALID,
+ RESULT_UNVERIFIABLE,
+ RESULT_INVALID
+} verify_result_t;
+
typedef struct {
MonoType *type;
int stype;
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;
*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;
get_stack_type (MonoType *type);
static gboolean
-mono_delegate_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2);
+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);
//////////////////////////////////////////////////////////////////
/**Signals that this is a boxed value type*/
BOXED_MASK = 0x1000,
+ /*This is an unitialized this ref*/
+ UNINIT_THIS_MASK = 0x2000,
};
static const char* const
!strcmp (namespace, type->data.klass->name_space) &&
!strcmp (name, type->data.klass->name);
}
+
+/*
+ * Returns TURE if @type is VAR or MVAR
+ */
+static gboolean
+mono_type_is_generic_argument (MonoType *type)
+{
+ return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
+}
+
/*
* mono_type_get_underlying_type_any:
*
mono_type_get_underlying_type_any (MonoType *type)
{
if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
- return type->data.klass->enum_basetype;
+ return mono_class_enum_basetype (type->data.klass);
if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype)
- return type->data.generic_class->container_class->enum_basetype;
+ return mono_class_enum_basetype (type->data.generic_class->container_class);
return type;
}
}
static gboolean
-verify_type_load_error(VerifyContext *ctx, MonoClass *klass)
+mono_class_has_default_constructor (MonoClass *klass)
+{
+ MonoMethod *method;
+ int i;
+
+ mono_class_setup_methods (klass);
+
+ for (i = 0; i < klass->method.count; ++i) {
+ method = klass->methods [i];
+ if (mono_method_is_constructor (method) &&
+ mono_method_signature (method)->param_count == 0 &&
+ (method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC)
+ return TRUE;
+ }
+ return FALSE;
+}
+
+static gboolean
+mono_class_interface_implements_interface (MonoClass *candidate, MonoClass *iface)
+{
+ int i;
+ do {
+ if (candidate == iface)
+ return TRUE;
+ mono_class_setup_interfaces (candidate);
+ for (i = 0; i < candidate->interface_count; ++i) {
+ if (candidate->interfaces [i] == iface || mono_class_interface_implements_interface (candidate->interfaces [i], iface))
+ return TRUE;
+ }
+ candidate = candidate->parent;
+ } while (candidate);
+ return FALSE;
+}
+
+/*
+ * 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 (MonoType *type, MonoGenericContext *context)
+{
+ 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 (&type->data.klass->byval_arg, context);
+ case MONO_TYPE_ARRAY:
+ return mono_type_is_valid_type_in_context (&type->data.array->eklass->byval_arg, context);
+ case MONO_TYPE_PTR:
+ return mono_type_is_valid_type_in_context (type->data.type, context);
+ 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 (inst->type_argv [i], context))
+ return FALSE;
+ break;
+ }
+ return TRUE;
+}
+
+/*This function returns NULL if the type is not instantiatable*/
+static MonoType*
+verifier_inflate_type (VerifyContext *ctx, MonoType *type, MonoGenericContext *context)
+{
+ if (!mono_type_is_valid_type_in_context (type, context))
+ return NULL;
+ return mono_class_inflate_generic_type (type, context);
+}
+/*
+ * Test if @candidate is a subtype of @target using the minimal possible information
+ * TODO move the code for non finished TypeBuilders to here.
+ */
+static gboolean
+mono_class_is_constraint_compatible (MonoClass *candidate, MonoClass *target)
+{
+ if (candidate == target)
+ return TRUE;
+ if (target == mono_defaults.object_class)
+ return TRUE;
+
+ //setup_supertypes don't mono_class_init anything
+ mono_class_setup_supertypes (candidate);
+ mono_class_setup_supertypes (target);
+
+ if (mono_class_has_parent (candidate, target))
+ return TRUE;
+
+ //if target is not a supertype it must be an interface
+ if (!MONO_CLASS_IS_INTERFACE (target))
+ return FALSE;
+
+ if (candidate->image->dynamic && !candidate->wastypebuilder) {
+ MonoReflectionTypeBuilder *tb = candidate->reflection_info;
+ int j;
+ if (tb->interfaces) {
+ for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
+ MonoReflectionType *iface = mono_array_get (tb->interfaces, MonoReflectionType*, j);
+ MonoClass *ifaceClass = mono_class_from_mono_type (iface->type);
+ if (mono_class_is_constraint_compatible (ifaceClass, target)) {
+ return TRUE;
+ }
+ }
+ }
+ return FALSE;
+ }
+ return mono_class_interface_implements_interface (candidate, target);
+}
+
+static gboolean
+is_valid_generic_instantiation (MonoGenericContainer *gc, MonoGenericContext *context, MonoGenericInst *ginst)
+{
+ int i;
+
+ if (ginst->type_argc != gc->type_argc)
+ return FALSE;
+
+ for (i = 0; i < gc->type_argc; ++i) {
+ MonoGenericParamInfo *param_info = mono_generic_container_get_param_info (gc, i);
+ MonoClass *paramClass;
+ MonoClass **constraints;
+
+ if (!param_info->constraints && !(param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK))
+ continue;
+ if (mono_type_is_generic_argument (ginst->type_argv [i]))
+ continue; //it's not our job to validate type variables
+
+ paramClass = mono_class_from_mono_type (ginst->type_argv [i]);
+
+ if (paramClass->exception_type != MONO_EXCEPTION_NONE)
+ return FALSE;
+
+ /*it's not safe to call mono_class_init from here*/
+ if (paramClass->generic_class && !paramClass->inited) {
+ if (!mono_class_is_valid_generic_instantiation (NULL, paramClass))
+ return FALSE;
+ }
+
+ if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && (!paramClass->valuetype || mono_class_is_nullable (paramClass)))
+ return FALSE;
+
+ if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && paramClass->valuetype)
+ return FALSE;
+
+ if ((param_info->flags & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !paramClass->valuetype && !mono_class_has_default_constructor (paramClass))
+ return FALSE;
+
+ if (!param_info->constraints)
+ continue;
+
+ for (constraints = param_info->constraints; *constraints; ++constraints) {
+ MonoClass *ctr = *constraints;
+ MonoType *inflated;
+
+ inflated = mono_class_inflate_generic_type (&ctr->byval_arg, context);
+ ctr = mono_class_from_mono_type (inflated);
+ mono_metadata_free_type (inflated);
+
+ if (!mono_class_is_constraint_compatible (paramClass, ctr))
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+/*
+ * Return true if @candidate is constraint compatible with @target.
+ *
+ * This means that @candidate constraints are a super set of @target constaints
+ */
+static gboolean
+mono_generic_param_is_constraint_compatible (VerifyContext *ctx, MonoGenericParam *target, MonoGenericParam *candidate, MonoGenericContext *context)
+{
+ MonoGenericParamInfo *tinfo = mono_generic_param_info (target);
+ MonoGenericParamInfo *cinfo = mono_generic_param_info (candidate);
+
+ int tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
+ int cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
+ if ((tmask & cmask) != tmask)
+ return FALSE;
+
+ if (tinfo->constraints) {
+ MonoClass **target_class, **candidate_class;
+ if (!cinfo->constraints)
+ return FALSE;
+ for (target_class = tinfo->constraints; *target_class; ++target_class) {
+ MonoClass *tc;
+ MonoType *inflated = verifier_inflate_type (ctx, &(*target_class)->byval_arg, context);
+ if (!inflated)
+ return FALSE;
+ tc = mono_class_from_mono_type (inflated);
+ mono_metadata_free_type (inflated);
+
+ for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
+ MonoClass *cc;
+ inflated = verifier_inflate_type (ctx, &(*candidate_class)->byval_arg, ctx->generic_context);
+ if (!inflated)
+ return FALSE;
+ cc = mono_class_from_mono_type (inflated);
+ mono_metadata_free_type (inflated);
+
+ if (mono_class_is_assignable_from (tc, cc))
+ 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 (gtd->generic_class)
+ gtd = gtd->generic_class->container_class;
+ gc = gtd->generic_container;
+ } else { //MVAR
+ MonoMethod *gmd = method;
+ if (method->is_inflated)
+ gmd = ((MonoMethodInflated*)method)->declaring;
+ gc = mono_method_get_generic_container (gmd);
+ }
+ if (!gc)
+ return FALSE;
+ 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)
+{
+ int i;
+ for (i = 0; i < ginst->type_argc; ++i) {
+ MonoType *type = ginst->type_argv [i];
+ if (!is_valid_type_in_context (ctx, type))
+ 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;
+
+ 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);
+
+ if (!mono_generic_param_is_constraint_compatible (ctx, target, candidate, context))
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static gboolean
+mono_method_repect_method_constraints (VerifyContext *ctx, MonoMethod *method)
+{
+ 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);
+}
+
+static gboolean
+mono_class_repect_method_constraints (VerifyContext *ctx, MonoClass *klass)
+{
+ MonoGenericClass *gklass = klass->generic_class;
+ MonoGenericInst *ginst = gklass->context.class_inst;
+ MonoGenericContainer *gc = gklass->container_class->generic_container;
+ return !gc || generic_arguments_respect_constraints (ctx, gc, &gklass->context, ginst);
+}
+
+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))
+ return FALSE;
+ return is_valid_generic_instantiation (gc, &gmethod->context, ginst);
+
+}
+
+static gboolean
+mono_class_is_valid_generic_instantiation (VerifyContext *ctx, MonoClass *klass)
+{
+ MonoGenericClass *gklass = klass->generic_class;
+ MonoGenericInst *ginst = gklass->context.class_inst;
+ MonoGenericContainer *gc = gklass->container_class->generic_container;
+ if (ctx && !is_valid_generic_instantiation_in_context (ctx, ginst))
+ return FALSE;
+ return is_valid_generic_instantiation (gc, &gklass->context, ginst);
+}
+
+static gboolean
+mono_type_is_valid_in_context (VerifyContext *ctx, MonoType *type)
+{
+ MonoClass *klass;
+
+ 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",
+ 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 (type);
mono_class_init (klass);
if (mono_loader_get_last_error () || klass->exception_type != MONO_EXCEPTION_NONE) {
+ if (klass->generic_class && !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", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
+ else
+ ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
+ return FALSE;
+ }
+
+ if (klass->exception_type != MONO_EXCEPTION_NONE || (klass->generic_class && klass->generic_class->container_class->exception_type != MONO_EXCEPTION_NONE)) {
ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Could not load type %s.%s at 0x%04x", klass->name_space, klass->name, ctx->ip_offset), MONO_EXCEPTION_TYPE_LOAD);
return FALSE;
}
- //TODO verify if the type can be inflated in the current context
+ if (!klass->generic_class)
+ 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", klass->name_space, klass->name, 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", klass->name_space, klass->name, 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, &method->klass->byval_arg))
+ 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", method->klass->name_space, method->klass->name, 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", method->klass->name_space, method->klass->name, method->name, ctx->ip_offset));
+ return RESULT_UNVERIFIABLE;
+ }
+ return RESULT_VALID;
+}
+
+
static MonoClassField*
verifier_load_field (VerifyContext *ctx, int token, MonoClass **klass, const char *opcode) {
MonoClassField *field;
}
field = mono_field_from_token (ctx->image, token, klass, ctx->generic_context);
- if (!field) {
+ if (!field || !field->parent) {
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 (!verify_type_load_error (ctx, field->parent))
+ if (!mono_type_is_valid_in_context (ctx, &field->parent->byval_arg))
return NULL;
return field;
return NULL;
}
- if (!verify_type_load_error (ctx, method->klass))
+ if (mono_method_is_valid_in_context (ctx, method) == RESULT_INVALID)
return NULL;
- //TODO verify if the method can be inflated in the current context
return method;
}
return NULL;
}
- if (!verify_type_load_error (ctx, mono_class_from_mono_type (type)))
+ if (!mono_type_is_valid_in_context (ctx, type))
return NULL;
return type;
{
return type_names [value->stype & TYPE_MASK];
}
+
+#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;
+
+ if ((value->stype & TYPE_MASK) != value->stype) {
+ gboolean first = TRUE;
+ 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");
+ 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;
+}
+
//////////////////////////////////////////////////////////////////
void
mono_free_verify_list (GSList *list)
return 1;
}
+/*This reject out PTR, FNPTR and TYPEDBYREF*/
static gboolean
check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
{
return 1;
}
+/*TODO verify if MONO_TYPE_TYPEDBYREF is not allowed here as well.*/
static gboolean
check_unverifiable_type (VerifyContext *ctx, MonoType *type)
{
static ILStackDesc *
stack_pop (VerifyContext *ctx)
{
- return ctx->eval.stack + --ctx->eval.size;
+ ILStackDesc *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;
}
-static inline ILStackDesc *
-stack_top (VerifyContext *ctx)
+/* 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)
{
- return ctx->eval.stack + (ctx->eval.size - 1);
+ return ctx->eval.stack + --ctx->eval.size;
}
-static inline ILStackDesc *
-stack_get (VerifyContext *ctx, int distance)
+static ILStackDesc *
+stack_push_stack_val (VerifyContext *ctx, ILStackDesc *value)
{
- return ctx->eval.stack + (ctx->eval.size - distance - 1);
+ 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.
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, value->type->data.generic_param->name);
+ printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
return;
case MONO_TYPE_MVAR:
- printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, value->type->data.generic_param->name);
+ printf ("complex] (method generic param !!%d - %s) ", value->type->data.generic_param->num, mono_generic_param_info (value->type->data.generic_param)->name);
return;
default: {
//should be a boxed value
static gboolean
is_array_type_compatible (MonoType *target, MonoType *candidate)
{
- int i;
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) ||
- (left->numsizes != right->numsizes) ||
- (left->numlobounds != right->numlobounds))
+ if (left->rank != right->rank)
return FALSE;
- for (i = 0; i < left->numsizes; ++i)
- if (left->sizes [i] != right->sizes [i])
- return FALSE;
-
- for (i = 0; i < left->numlobounds; ++i)
- if (left->lobounds [i] != right->lobounds [i])
- return FALSE;
-
return mono_class_is_assignable_from (left->eklass, right->eklass);
}
static void
init_stack_with_value_at_exception_boundary (VerifyContext *ctx, ILCodeDesc *code, MonoClass *klass)
{
+ MonoType *type = mono_class_inflate_generic_type (&klass->byval_arg, ctx->generic_context);
stack_init (ctx, code);
- set_stack_value (ctx, code->stack, &klass->byval_arg, FALSE);
+ 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;
}
/*Verify if type 'candidate' can be stored in type 'target'.
#define IS_ONE_OF2(T, A, B) (T == A || T == B)
MonoType *original_candidate = candidate;
- VERIFIER_DEBUG ( printf ("checking type compatibility %p %p[%x][%x] %p[%x][%x]\n", ctx, target, target->type, target->byref, candidate, candidate->type, candidate->byref); );
+ 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) {
}
case MONO_TYPE_PTR:
- if (!IS_STRICT_MODE (ctx) && IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U))
- return TRUE;
if (candidate->type != MONO_TYPE_PTR)
return FALSE;
/* check the underlying type */
}
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);
goto handle_enum;
}
- return mono_class_is_assignable_from (mono_class_from_mono_type (target), mono_class_from_mono_type (candidate));
+ target_klass = mono_class_from_mono_type (target);
+ candidate_klass = mono_class_from_mono_type (candidate);
+ if (mono_class_is_nullable (target_klass)) {
+ if (!mono_class_is_nullable (candidate_klass))
+ return FALSE;
+ return target_klass == candidate_klass;
+ }
+
+ return mono_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 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.
*/
if (candidate->type != MONO_TYPE_SZARRAY)
return FALSE;
- left = target->data.klass;
- right = candidate->data.klass;
- return mono_class_is_assignable_from(left, right);
+ left = mono_class_from_mono_type (target)->element_class;
+ right = mono_class_from_mono_type (candidate)->element_class;
+ return mono_class_is_assignable_from (left, right);
}
case MONO_TYPE_ARRAY:
case MONO_TYPE_TYPEDBYREF:
return candidate->type == MONO_TYPE_TYPEDBYREF;
- case MONO_TYPE_VALUETYPE:
- if (candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass)
+ case MONO_TYPE_VALUETYPE: {
+ MonoClass *target_klass = mono_class_from_mono_type (target);
+ MonoClass *candidate_klass = mono_class_from_mono_type (candidate);
+
+ if (target_klass == candidate_klass)
return TRUE;
if (mono_type_is_enum_type (target)) {
target = mono_type_get_underlying_type_any (target);
goto handle_enum;
}
return FALSE;
+ }
case MONO_TYPE_VAR:
if (candidate->type != MONO_TYPE_VAR)
return FALSE;
- return candidate->data.generic_param->num == target->data.generic_param->num;
+ 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 candidate->data.generic_param->num == target->data.generic_param->num;
+ 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); );
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;
+
+}
/*
* is_compatible_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
- * @type_must_be_object if TRUE @type must be System.Object, otherwise can be any reference type.
+ * @strict if TRUE candidate must be boxed compatible to the target type
*
*/
static gboolean
-is_compatible_boxed_valuetype (MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean type_must_be_object)
+is_compatible_boxed_valuetype (VerifyContext *ctx, MonoType *type, MonoType *candidate, ILStackDesc *stack, gboolean strict)
{
- if (type_must_be_object && type->type != MONO_TYPE_OBJECT)
+ if (!stack_slot_is_boxed_value (stack))
return FALSE;
- if (!type_must_be_object && !MONO_TYPE_IS_REFERENCE (type))
+ if (type->byref || candidate->byref)
return FALSE;
- return !type->byref && !candidate->byref && stack_slot_is_boxed_value (stack);
+
+ if (mono_type_is_generic_argument (candidate)) {
+ MonoGenericParam *param = get_generic_param (ctx, candidate);
+ MonoClass **class;
+ for (class = mono_generic_param_info (param)->constraints; class && *class; ++class) {
+ if (verify_type_compatibility_full (ctx, type, mono_type_get_type_byval (& (*class)->byval_arg), FALSE))
+ return TRUE;
+ }
+ }
+
+ if (mono_type_is_generic_argument (type))
+ return FALSE;
+
+ if (!strict)
+ return TRUE;
+
+ return MONO_TYPE_IS_REFERENCE (type) && mono_class_is_assignable_from (mono_class_from_mono_type (type), mono_class_from_mono_type (candidate));
}
static int
-verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
+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 (type, candidate, stack, 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);
+
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)
{
case MONO_TYPE_FNPTR:
if (candidate->type != MONO_TYPE_FNPTR)
return FALSE;
- return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate));
-
- case MONO_TYPE_GENERICINST:
- //TODO implement me
- g_assert_not_reached ();
- return FALSE;
+ return mono_delegate_signature_equal (mono_type_get_signature (target), mono_type_get_signature (candidate), FALSE);
- return candidate->type == MONO_TYPE_STRING;
-
+ case MONO_TYPE_GENERICINST: {
+ MonoClass *target_klass;
+ MonoClass *candidate_klass;
+ target_klass = mono_class_from_mono_type (target);
+ candidate_klass = mono_class_from_mono_type (candidate);
+ /*FIXME handle nullables and enum*/
+ return mono_class_is_assignable_from (target_klass, candidate_klass);
+ }
case MONO_TYPE_OBJECT:
return MONO_TYPE_IS_REFERENCE (candidate);
case MONO_TYPE_CLASS:
- if (candidate->type != MONO_TYPE_CLASS)
- return FALSE;
- return mono_class_is_assignable_from(target->data.klass, candidate->data.klass);
+ return mono_class_is_assignable_from(target->data.klass, mono_class_from_mono_type (candidate));
case MONO_TYPE_SZARRAY:
if (candidate->type != MONO_TYPE_SZARRAY)
return FALSE;
- return mono_class_is_assignable_from (target->data.array->eklass, candidate->data.array->eklass);
+ return mono_class_is_assignable_from (mono_class_from_mono_type (target)->element_class, mono_class_from_mono_type (candidate)->element_class);
case MONO_TYPE_ARRAY:
if (candidate->type != MONO_TYPE_ARRAY)
return is_array_type_compatible (target, candidate);
case MONO_TYPE_VALUETYPE:
- return candidate->type == MONO_TYPE_VALUETYPE && target->data.klass == candidate->data.klass;
+ /*FIXME handle nullables and enum*/
+ return mono_class_from_mono_type (candidate) == mono_class_from_mono_type (target);
case MONO_TYPE_VAR:
- //TODO implement me
- g_assert_not_reached ();
+ 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:
- //TODO implement me
- g_assert_not_reached ();
+ return candidate->type == MONO_TYPE_MVAR && mono_type_get_generic_param_num (target) == mono_type_get_generic_param_num (candidate);
return FALSE;
default:
* FIXME can this function be eliminated and proper metadata functionality be used?
*/
static gboolean
-mono_delegate_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
+mono_delegate_signature_equal (MonoMethodSignature *delegate_sig, MonoMethodSignature *method_sig, gboolean is_static_ldftn)
{
int i;
- if (sig1->param_count != sig2->param_count)
- return FALSE;
+ int method_offset = is_static_ldftn ? 1 : 0;
- if (sig1->generic_param_count != sig2->generic_param_count)
+ if (delegate_sig->param_count + method_offset != method_sig->param_count)
return FALSE;
-
- if (sig1->call_convention != sig2->call_convention)
+
+ if (delegate_sig->call_convention != method_sig->call_convention)
return FALSE;
- for (i = 0; i < sig1->param_count; i++) {
- MonoType *p1 = sig1->params [i];
- MonoType *p2 = sig2->params [i];
+ 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 (sig1->ret, sig2->ret))
+ if (!mono_delegate_ret_equal (delegate_sig->ret, method_sig->ret))
return FALSE;
return TRUE;
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));
invoke = mono_get_delegate_invoke (delegate);
method = funptr->method;
- if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method)))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Function pointer parameter for delegate constructor has diferent signature at 0x%04x", ctx->ip_offset));
+ 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 (invoke)->param_count + 1 == mono_method_signature (method)->param_count;
+
+ if (!mono_delegate_signature_equal (mono_method_signature (invoke), mono_method_signature (method), is_first_arg_bound)) {
+ char *fun_sig = mono_signature_get_desc (mono_method_signature (method), FALSE);
+ char *invoke_sig = mono_signature_get_desc (mono_method_signature (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:
ctx->code [ip_offset].flags |= IL_CODE_DELEGATE_SEQUENCE;
//general tests
- if (!verify_type_compatibility (ctx, &method->klass->byval_arg, value->type) && !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 (is_first_arg_bound) {
+ if (!verify_stack_type_compatibility_full (ctx, mono_method_signature (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 (!verify_stack_type_compatibility_full (ctx, &method->klass->byval_arg, 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));
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));
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));
- if (!set_stack_value (ctx, stack_push (ctx), ctx->params [arg], take_addr))
+ 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
- stack_top (ctx)->stype |= THIS_POINTER_MASK;
+ top->stype |= THIS_POINTER_MASK;
+ if (mono_method_is_constructor (ctx->method) && !ctx->super_ctor_called && !ctx->method->klass->valuetype)
+ top->stype |= UNINIT_THIS_MASK;
}
}
}
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));
- check_underflow (ctx, 1);
- stack_pop (ctx);
+ if (check_underflow (ctx, 1))
+ stack_pop (ctx);
return;
}
static void
do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
{
- ILStackDesc *a, *b;
+ ILStackDesc *a, *b, *top;
int idxa, idxb, complexMerge = 0;
unsigned char res;
if (!check_underflow (ctx, 2))
return;
- a = stack_get (ctx, 1);
- b = stack_top (ctx);
+ b = stack_pop (ctx);
+ a = stack_pop (ctx);
idxa = stack_slot_get_underlying_type (a);
if (stack_slot_is_managed_pointer (a)) {
VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
- ctx->eval.size--;
+ 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;
+ res = res & ~NON_VERIFIABLE_RESULT;
}
if (complexMerge && res == TYPE_PTR) {
if (complexMerge == 1)
- copy_stack_value (stack_top (ctx), a);
+ copy_stack_value (top, a);
else if (complexMerge == 2)
- copy_stack_value (stack_top (ctx), b);
+ 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
* This is valid acording to Patition III 1.1.4
*/
} else
- stack_top (ctx)->stype = res;
+ top->stype = res;
}
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 (stack_get (ctx, -1)), ctx->ip_offset));
+ 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 void
do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
}
switch (is_valid_cmp_branch_instruction (ctx->header, ctx->ip_offset, target)) {
- case 1:
+ 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:
if (stack_slot_is_managed_pointer (b))
idxb = TYPE_PTR;
- --idxa;
- --idxb;
- res = table [idxa][idxb];
+ 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 (stack_slot_is_managed_pointer (b))
idxb = TYPE_PTR;
- --idxa;
- --idxb;
- res = table [idxa][idxb];
+ 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) {
CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare 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));
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))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
+ 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 (sig->hasthis) {
MonoType *type = &method->klass->byval_arg;
ILStackDesc copy;
- value = stack_pop (ctx);
+
+ if (mono_method_is_constructor (method) && !method->klass->valuetype) {
+ if (!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 (method->klass != ctx->method->klass->parent && method->klass != ctx->method->klass)
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Cannot call a constructor to a type diferent that this or super at 0x%04x", ctx->ip_offset));
+
+ ctx->super_ctor_called = TRUE;
+ value = stack_pop_safe (ctx);
+ if ((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));
+ } 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
if (!verify_stack_type_compatibility (ctx, type, ©))
CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature at 0x%04x", ctx->ip_offset));
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, value->type->data.klass))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
+ if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_method_full (ctx->method, method, mono_class_from_mono_type (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))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
+ } 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 (check_overflow (ctx)) {
if (field->parent->valuetype && 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_type_compatibility (ctx, &field->parent->byval_arg, mono_type_get_type_byval (obj->type)))
+ if (!stack_slot_is_null_literal (obj) && !verify_stack_type_compatibility_full (ctx, &field->parent->byval_arg, obj, TRUE, FALSE))
CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
- if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, obj->type->data.klass))
+ if (!IS_SKIP_VISIBILITY (ctx) && !mono_method_can_access_field_full (ctx->method, field, mono_class_from_mono_type (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);
}
if (!check_underflow (ctx, 1))
return;
- obj = stack_pop (ctx);
+ obj = stack_pop_safe (ctx);
if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, take_addr ? "ldflda" : "ldfld"))
return;
return;
value = stack_pop (ctx);
- obj = stack_pop (ctx);
+ obj = stack_pop_safe (ctx);
if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field, "stfld"))
return;
{
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_top (ctx);
+ 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)) {
- value->stype |= BOXED_MASK;
+ stack_push_stack_val (ctx, value)->stype |= BOXED_MASK;
return;
}
- value = stack_pop (ctx);
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 (type);
+ if (mono_class_is_nullable (klass))
+ type = &mono_class_get_nullable_param (klass)->byval_arg;
stack_push_val (ctx, TYPE_COMPLEX | BOXED_MASK, type);
}
ILStackDesc *value;
if (!check_underflow (ctx, 1))
return;
- value = stack_top (ctx);
+ value = stack_pop (ctx);
switch (stack_slot_get_type (value)) {
case TYPE_I4:
case TYPE_I8:
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
ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
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, &((MonoClassField*)handle)->parent->byval_arg);
+ } 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, mono_class_get_type (handle_class));
}
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))
CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Token and source types of stobj don't match 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)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type token of initobj not compatible with value on stack at 0x%04x", ctx->ip_offset));
+ 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);
}
}
if (method->klass->flags & (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 (!mono_method_can_access_method_full (ctx->method, method, NULL))
- CODE_NOT_VERIFIABLE2 (ctx, g_strdup_printf ("Constructor not visible at 0x%04x", ctx->ip_offset), MONO_EXCEPTION_METHOD_ACCESS);
+ if (!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 (method);
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))
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible parameter value with function signature at 0x%04x", ctx->ip_offset));
+ 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));
ILStackDesc *value;
MonoType *type;
gboolean is_boxed;
+ gboolean do_box;
if (!check_underflow (ctx, 1))
return;
CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value for %s at 0x%04x", opcode, ctx->ip_offset));
}
- stack_push_val (ctx, TYPE_COMPLEX | (mono_class_from_mono_type (type)->valuetype || is_boxed ? BOXED_MASK : 0), type);
+ do_box = is_boxed || mono_type_is_generic_argument(type) || mono_class_from_mono_type (type)->valuetype;
+ stack_push_val (ctx, TYPE_COMPLEX | (do_box ? BOXED_MASK : 0), type);
}
static MonoType *
}
}
}
-
if (opcode == CEE_STELEM_REF) {
if (!stack_slot_is_boxed_value (value) && mono_class_from_mono_type (value->type)->valuetype)
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 && !verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (value), FALSE)) {
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid value on stack for stdelem.X at 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));
+
}
}
if (!stack_slot_is_null_literal (exception) && !(stack_slot_get_type (exception) == TYPE_COMPLEX && !mono_class_from_mono_type (exception->type)->valuetype))
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;
}
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;
}
/*
for (i = 0; i < count; ++i) {
int target = base + read32 (data + i * 4);
- if (target < 0 || target >= ctx->code_size)
+ 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:
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));
- break;
+ return;
}
merge_stacks (ctx, &ctx->eval, &ctx->code [target], FALSE, TRUE);
}
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_type_compatibility (ctx, &method->klass->byval_arg, top->type))
+
+ if (!verify_stack_type_compatibility (ctx, &method->klass->byval_arg, top))
CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Unexpected object for ldvirtftn at 0x%04x", ctx->ip_offset));
}
static void
do_localloc (VerifyContext *ctx)
{
+ ILStackDesc *top;
+
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;
return;
}
- set_stack_value (ctx, stack_top (ctx), &mono_defaults.int_class->byval_arg, FALSE);
+ /*TODO verify top type*/
+ top = stack_pop (ctx);
+
+ set_stack_value (ctx, stack_push (ctx), &mono_defaults.int_class->byval_arg, FALSE);
CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Instruction localloc in never verifiable at 0x%04x", ctx->ip_offset));
}
return;
}
- if (mono_metadata_token_index (token) >= ctx->image->heap_us.size) {
+ if (!ctx->image->dynamic && mono_metadata_token_index (token) >= ctx->image->heap_us.size) {
ADD_VERIFY_ERROR2 (ctx, g_strdup_printf ("Invalid string index %x at 0x%04x", token, ctx->ip_offset), MONO_EXCEPTION_BAD_IMAGE);
return;
}
if (!check_underflow (ctx, 1))
return;
- top = stack_top (ctx);
+ 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:
MonoClass *match_class = NULL;
// S := T then U = S (new value is compatible with current value, keep current)
- if (verify_type_compatibility (ctx, old_type, new_type)) {
+ 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_type_compatibility (ctx, new_type, old_type)) {
+ if (verify_stack_type_compatibility (ctx, new_type, old_slot)) {
copy_stack_value (old_slot, new_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 (!mono_class_from_mono_type (old_type)->valuetype
&& !mono_class_from_mono_type (new_type)->valuetype
}
}
+ mono_class_setup_interfaces (old_class);
for (j = 0; j < old_class->interface_count; ++j) {
for (k = 0; k < new_class->interface_count; ++k) {
if (mono_metadata_type_equal (&old_class->interfaces [j]->byval_arg, &new_class->interfaces [k]->byval_arg)) {
//No decent super type found, use object
match_class = mono_defaults.object_class;
goto match_found;
- } else if (is_compatible_boxed_valuetype (old_type, new_type, new_slot, FALSE) || is_compatible_boxed_valuetype (new_type, old_type, old_slot, FALSE)) {
+ } 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;
- }
+ }
- CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stack at depth %d, types not compatible old [%s] new [%s] at 0x%04x", i, stack_slot_get_name (old_slot), stack_slot_get_name (new_slot), ctx->ip_offset));
+ {
+ 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, &new_class->byval_arg, stack_slot_is_managed_pointer (old_slot));
goto end_verify;
match_found:
verify_clause_relationship (VerifyContext *ctx, MonoExceptionClause *clause, MonoExceptionClause *to_test)
{
/*clause is nested*/
- if (is_clause_nested (to_test, clause)) {
- if (to_test->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Exception clause inside filter"));
- }
+ 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;
}
}
/*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))
+ 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 (ip + size > end) {\
+ ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Code overrun starting with 0x%x at 0x%04x", *ip, ctx.ip_offset)); \
+ break; \
+ } \
+
/*
* FIXME: need to distinguish between valid and verifiable.
* Need to keep track of types on the stack.
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); );
if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
ctx.verifiable = ctx.valid = 1;
ctx.level = level;
- ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
+ ctx.code = g_new (ILCodeDesc, ctx.header->code_size);
ctx.code_size = ctx.header->code_size;
memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
ctx.num_locals = ctx.header->num_locals;
- ctx.locals = ctx.header->locals;
+ ctx.locals = g_memdup (ctx.header->locals, sizeof (MonoType*) * ctx.header->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"));
- if (ctx.signature->hasthis) {
- ctx.params = g_new0 (MonoType*, ctx.max_args);
+ ctx.params = g_new (MonoType*, ctx.max_args);
+ if (ctx.signature->hasthis)
ctx.params [0] = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
- memcpy (ctx.params + 1, ctx.signature->params, sizeof (MonoType *) * ctx.signature->param_count);
- } else {
- ctx.params = ctx.signature->params;
- }
+ 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 && (method->klass->generic_container || method->generic_container)) {
- if (method->generic_container)
- ctx.generic_context = generic_context = &method->generic_container->context;
+ if (!generic_context && (method->klass->generic_container || 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 = &method->klass->generic_container->context;
}
+ for (i = 0; i < ctx.num_locals; ++i)
+ ctx.locals [i] = mono_class_inflate_generic_type (ctx.locals [i], ctx.generic_context);
+ for (i = 0; i < ctx.max_args; ++i)
+ ctx.params [i] = mono_class_inflate_generic_type (ctx.params [i], ctx.generic_context);
stack_init (&ctx, &ctx.eval);
+ for (i = 0; i < ctx.num_locals; ++i) {
+ if (!mono_type_is_valid_in_context (&ctx, ctx.locals [i])) {
+ /*TODO use the last error message to provide better feedback. */
+ ADD_VERIFY_ERROR2 (&ctx, g_strdup_printf ("Invalid local variable %d", i), MONO_EXCEPTION_BAD_IMAGE);
+ break;
+ }
+ }
+
+ for (i = 0; i < ctx.max_args; ++i) {
+ if (!mono_type_is_valid_in_context (&ctx, ctx.params [i])) {
+ /*TODO use the last error message to provide better feedback. */
+ ADD_VERIFY_ERROR2 (&ctx, g_strdup_printf ("Invalid parameter %d", i), MONO_EXCEPTION_BAD_IMAGE);
+ 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)
+ if (clause->try_offset > ctx.code_size || 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_VERIFY_ERROR (&ctx, g_strdup_printf ("try clause out of bounds at 0x%04x", clause->try_offset));
+ if (clause->handler_offset > ctx.code_size || 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 ("try clause len <= 0 at 0x%04x", clause->try_offset));
break;
ctx.code [clause->try_offset].flags |= IL_CODE_FLAG_WAS_TARGET;
- ctx.code [clause->try_offset + clause->try_len].flags |= IL_CODE_FLAG_WAS_TARGET;
- ctx.code [clause->handler_offset + clause->handler_len].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) {
init_stack_with_value_at_exception_boundary (&ctx, ctx.code + clause->handler_offset, clause->data.catch_class);
start = 1;
}
- if (clause->try_offset == ip_offset && ctx.eval.size > 0) {
+ 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;
}
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_POP:
if (!check_underflow (&ctx, 1))
break;
- stack_pop (&ctx);
+ stack_pop_safe (&ctx);
++ip;
break;
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;
break;
case CEE_LDC_I4_S:
+ code_bounds_check (2);
if (check_overflow (&ctx))
stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
ip += 2;
break;
case CEE_LDC_I4:
+ code_bounds_check (5);
if (check_overflow (&ctx))
stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
ip += 5;
break;
case CEE_LDC_I8:
+ code_bounds_check (9);
if (check_overflow (&ctx))
stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
ip += 9;
break;
case CEE_LDC_R4:
+ code_bounds_check (5);
if (check_overflow (&ctx))
stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
ip += 5;
break;
case CEE_LDC_R8:
+ code_bounds_check (9);
if (check_overflow (&ctx))
stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
ip += 9;
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;
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;
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;
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;
case CEE_LDLOC_S:
case CEE_LDLOCA_S:
+ code_bounds_check (2);
push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
ip += 2;
break;
break;
if (!check_overflow (&ctx))
break;
- top = stack_push (&ctx);
- copy_stack_value (top, stack_get (&ctx, 1));
+ top = stack_pop_safe (&ctx);
+ copy_stack_value (stack_push (&ctx), top);
+ copy_stack_value (stack_push (&ctx), top);
++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.
*/
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.
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;
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;
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:
+ code_bounds_check (5);
n = read32 (ip + 1);
+ code_bounds_check (5 + sizeof (guint32) * n);
+
do_switch (&ctx, n, (ip + 5));
start = 1;
ip += 5 + sizeof (guint32) * n;
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_UNUSED1:
++ip; /* warn, error ? */
break;
+
case CEE_UNBOX:
+ code_bounds_check (5);
do_unbox_value (&ctx, read32 (ip + 1));
ip += 5;
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;
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;
break;
case CEE_LDELEMA:
+ code_bounds_check (5);
do_ldelema (&ctx, read32 (ip + 1));
ip += 5;
break;
break;
case CEE_LDELEM_ANY:
+ code_bounds_check (5);
do_ldelem (&ctx, *ip, read32 (ip + 1));
ip += 5;
break;
case CEE_STELEM_ANY:
+ 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_UNUSED5:
- case CEE_UNUSED6:
- case CEE_UNUSED7:
- case CEE_UNUSED8:
- case CEE_UNUSED9:
- case CEE_UNUSED10:
- case CEE_UNUSED11:
- case CEE_UNUSED12:
- case CEE_UNUSED13:
- case CEE_UNUSED14:
- case CEE_UNUSED15:
- case CEE_UNUSED16:
- case CEE_UNUSED17:
- ++ip; /* warn, error ? */
- break;
-
case CEE_CONV_OVF_I1:
case CEE_CONV_OVF_U1:
case CEE_CONV_OVF_I2:
++ip;
break;
- case CEE_UNUSED50:
- case CEE_UNUSED18:
- case CEE_UNUSED19:
- case CEE_UNUSED20:
- case CEE_UNUSED21:
- case CEE_UNUSED22:
- case CEE_UNUSED23:
- ++ip; /* warn, error ? */
- break;
case CEE_REFANYVAL:
+ code_bounds_check (5);
do_refanyval (&ctx, read32 (ip + 1));
ip += 5;
break;
++ip;
break;
- case CEE_UNUSED24:
- case CEE_UNUSED25:
- ++ip; /* warn, error ? */
- break;
-
case CEE_MKREFANY:
+ code_bounds_check (5);
do_mkrefany (&ctx, read32 (ip + 1));
ip += 5;
break;
- case CEE_UNUSED59:
- case CEE_UNUSED60:
- case CEE_UNUSED61:
- case CEE_UNUSED62:
- case CEE_UNUSED63:
- case CEE_UNUSED64:
- case CEE_UNUSED65:
- case CEE_UNUSED66:
- case CEE_UNUSED67:
- ++ip; /* warn, error ? */
- break;
case CEE_LDTOKEN:
+ code_bounds_check (5);
do_load_token (&ctx, read32 (ip + 1));
ip += 5;
break;
break;
case CEE_LEAVE:
+ code_bounds_check (5);
do_leave (&ctx, read32 (ip + 1) + 5);
ip += 5;
start = 1;
break;
case CEE_LEAVE_S:
+ code_bounds_check (2);
do_leave (&ctx, (signed char)ip [1] + 2);
ip += 2;
start = 1;
break;
-
- case CEE_UNUSED26:
- case CEE_UNUSED27:
- case CEE_UNUSED28:
- case CEE_UNUSED29:
- case CEE_UNUSED30:
- case CEE_UNUSED31:
- case CEE_UNUSED32:
- case CEE_UNUSED33:
- case CEE_UNUSED34:
- case CEE_UNUSED35:
- case CEE_UNUSED36:
- case CEE_UNUSED37:
- case CEE_UNUSED38:
- case CEE_UNUSED39:
- case CEE_UNUSED40:
- case CEE_UNUSED41:
- case CEE_UNUSED42:
- case CEE_UNUSED43:
- case CEE_UNUSED44:
- case CEE_UNUSED45:
- case CEE_UNUSED46:
- case CEE_UNUSED47:
- case CEE_UNUSED48:
- ++ip;
- break;
- case CEE_PREFIX7:
- case CEE_PREFIX6:
- case CEE_PREFIX5:
- case CEE_PREFIX4:
- case CEE_PREFIX3:
- case CEE_PREFIX2:
- case CEE_PREFIXREF:
- ++ip;
- 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;
break;
case CEE_STARG:
+ code_bounds_check (3);
store_arg (&ctx, read16 (ip + 1) );
ip += 3;
break;
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;
++ip;
break;
case CEE_UNALIGNED_:
+ code_bounds_check (2);
prefix |= PREFIX_UNALIGNED;
ip += 2;
break;
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;
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_UNUSED:
break;
case CEE_SIZEOF:
+ code_bounds_check (5);
do_sizeof (&ctx, read32 (ip + 1));
ip += 5;
break;
++ip;
break;
- case CEE_UNUSED53:
- case CEE_UNUSED54:
- case CEE_UNUSED55:
- case CEE_UNUSED70:
+ default:
+ ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid instruction FE %x at 0x%04x", *ip, ctx.ip_offset));
++ip;
- break;
}
+ 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*/
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 && !ctx.method->klass->valuetype && ctx.method->klass != mono_defaults.object_class)
+ CODE_NOT_VERIFIABLE (&ctx, g_strdup_printf ("Constructor not calling super\n"));
+
+cleanup:
if (ctx.code) {
for (i = 0; i < ctx.header->code_size; ++i) {
if (ctx.code [i].stack)
g_free (tmp->data);
g_slist_free (ctx.funptrs);
+ for (tmp = ctx.exception_types; tmp; tmp = tmp->next)
+ mono_metadata_free_type (tmp->data);
+ g_slist_free (ctx.exception_types);
+
+ for (i = 0; i < ctx.num_locals; ++i)
+ mono_metadata_free_type (ctx.locals [i]);
+ for (i = 0; i < ctx.max_args; ++i)
+ mono_metadata_free_type (ctx.params [i]);
+
if (ctx.eval.stack)
g_free (ctx.eval.stack);
if (ctx.code)
g_free (ctx.code);
- if (ctx.signature->hasthis)
- g_free (ctx.params);
+ g_free (ctx.locals);
+ g_free (ctx.params);
return ctx.list;
}
return NULL;
}
-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 true if @method needs to be verified.
*
return verify_all || (verifier_mode > MONO_VERIFIER_MODE_OFF && !klass->image->assembly->in_gac && klass->image != mono_defaults.corlib);
}
+gboolean
+mono_verifier_is_enabled_for_image (MonoImage *image)
+{
+ return verify_all || verifier_mode > MONO_VERIFIER_MODE_OFF;
+}
+
gboolean
mono_verifier_is_method_full_trust (MonoMethod *method)
{
*
* TODO This code doesn't take CAS into account.
*
- * This value is only pertinent to assembly verification and has
- * nothing to do with CoreClr security.
- *
- * Under verify_all, all code is under full trust if no verifier mode is set.
+ * Under verify_all all user code must be verifiable if no security option was set
*
*/
gboolean
mono_verifier_is_class_full_trust (MonoClass *klass)
{
- return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || klass->image->assembly->in_gac || klass->image == mono_defaults.corlib;
+ /* under CoreCLR code is trusted if it is part of the "platform" otherwise all code inside the GAC is trusted */
+ gboolean trusted_location = (mono_security_get_mode () != MONO_SECURITY_MODE_CORE_CLR) ?
+ klass->image->assembly->in_gac : mono_security_core_clr_is_platform_image (klass->image);
+
+ if (verify_all && verifier_mode == MONO_VERIFIER_MODE_OFF)
+ return trusted_location || klass->image == mono_defaults.corlib;
+ return verifier_mode < MONO_VERIFIER_MODE_VERIFIABLE || trusted_location || klass->image == mono_defaults.corlib;
}
GSList*
| (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;
+}
-gboolean
-mono_verifier_verify_class (MonoClass *class)
+static gboolean
+verify_class_for_overlapping_reference_fields (MonoClass *class)
{
- int i, j, align;
- if (!(class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT || !class->has_references)
+ int i, j;
+ gboolean is_fulltrust = mono_verifier_is_class_full_trust (class);
+ if (!((class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) || !class->has_references)
return TRUE;
//we must check for stuff overlapping reference fields
for (i = 0; i < class->field.count; ++i) {
MonoClassField *field = &class->fields [i];
- int fieldEnd = field->offset + mono_type_size (field->type, &align);
+ int fieldEnd = get_field_end (field);
gboolean is_valuetype = !MONO_TYPE_IS_REFERENCE (field->type);
- if (mono_field_is_deleted (field))
+ if (mono_field_is_deleted (field) || (field->type->attrs & FIELD_ATTRIBUTE_STATIC))
continue;
for (j = i + 1; j < class->field.count; ++j) {
MonoClassField *other = &class->fields [j];
- int otherEnd = other->offset + mono_type_size (other->type, &align);
- if (mono_field_is_deleted (other) || (is_valuetype && !MONO_TYPE_IS_REFERENCE (other->type)))
+ 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;
}
+
+/*
+ * 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 and all code must be carefull about that.
+ *
+ */
+gboolean
+mono_verifier_verify_class (MonoClass *class)
+{
+ if (class->generic_container && (class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT)
+ return FALSE;
+ if (!verify_class_for_overlapping_reference_fields (class))
+ return FALSE;
+
+ if (class->generic_class && !mono_class_is_valid_generic_instantiation (NULL, class))
+ return FALSE;
+ return TRUE;
+}
+#else
+
+gboolean
+mono_verifier_verify_class (MonoClass *class)
+{
+ /* The verifier was disabled at compile time */
+ return TRUE;
+}
+
+GSList*
+mono_method_verify_with_current_settings (MonoMethod *method, gboolean skip_visibility)
+{
+ /* 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 */
+}
+
+GSList*
+mono_image_verify_tables (MonoImage *image, int level)
+{
+ /* The verifier was disabled at compile time */
+ return NULL;
+}
+#endif