};
#undef OPDEF
-#if DISABLE_LOGGING
-#define VERIFIER_DEBUG(code)
-#else
+#ifdef MONO_VERIFIER_DEBUG
#define VERIFIER_DEBUG(code) do { code } while (0)
+#else
+#define VERIFIER_DEBUG(code)
#endif
//////////////////////////////////////////////////////////////////
vinfo->message = ( __msg ); \
(__ctx)->list = g_slist_prepend ((__ctx)->list, vinfo); \
} while (0)
-
-#define ADD_VERIFY_ERROR(__ctx, __msg) ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR)
+
+#define ADD_VERIFY_ERROR(__ctx, __msg) \
+ do { \
+ ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_ERROR); \
+ (__ctx)->valid = 0; \
+ } while (0)
+
+#define CODE_NOT_VERIFIABLE(__ctx, __msg) \
+ do { \
+ if ((__ctx)->verifiable) { \
+ ADD_VERIFY_INFO(__ctx, __msg, MONO_VERIFY_NOT_VERIFIABLE); \
+ (__ctx)->verifiable = 0; \
+ } \
+ } while (0)
+
+#define UNMASK_TYPE(type) ((type) & TYPE_MASK)
+#define IS_MANAGED_POINTER(type) (((type) & POINTER_MASK) == POINTER_MASK)
enum {
IL_CODE_FLAG_NOT_PROCESSED = 0,
int max_args;
int max_stack;
int verifiable;
+ int valid;
int code_size;
ILCodeDesc *code;
ILCodeDesc eval;
-
+
MonoType **params;
GSList *list;
-
+
int num_locals;
MonoType **locals;
-
+
int target;
-
+
guint32 ip_offset;
MonoMethodSignature *signature;
MonoMethodHeader *header;
+
+ MonoGenericContext *generic_context;
+ MonoImage *image;
+ MonoMethod *method;
} VerifyContext;
//////////////////////////////////////////////////////////////////
TYPE_I8 = 2,
TYPE_NATIVE_INT = 3,
TYPE_R8 = 4,
- /* Only used by operator tables*/
+ /* Used by operator tables to resolve pointer types (managed & unmanaged) and by unmanaged pointer types*/
TYPE_PTR = 5,
- /* Method pointer, value types and classes */
+ /* value types and classes */
TYPE_COMPLEX = 6,
/* Number of types, used to define the size of the tables*/
- TYPE_MAX = 6,
-
+ TYPE_MAX = 8, /* FIXME: This should probably be 7, but would require all the tables to be updated */
+
/* 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 pointer, unmask the value to res */
+
+ /* The stack type is a managed pointer, unmask the value to res */
POINTER_MASK = 0x100,
+
/* Controlled Mutability Manager Pointer */
CMMP_MASK = 0x200,
};
"Int64",
"Native Int",
"Float64",
+ "TYPE_PTR", /* FIXME: Give an appropriate name */
"Complex"
};
{
int i;
int found;
-
+
found = *cname == 0;
for (i = 0; i < G_N_ELEMENTS (valid_cultures); ++i) {
if (g_strcasecmp (valid_cultures [i], cname)) {
{
guint32 size;
const char *p, *blob_end;
-
+
if (blob_index >= image->heap_blob.size)
return 0;
p = mono_metadata_blob_heap (image, blob_index);
is_valid_string (MonoImage *image, guint32 str_index, int notnull)
{
const char *p, *blob_end, *res;
-
+
if (str_index >= image->heap_strings.size)
return NULL;
res = p = mono_metadata_string_heap (image, str_index);
if (t->rows > 1)
ADD_ERROR (list, g_strdup ("Assembly table may only have 0 or 1 rows"));
mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
-
+
switch (cols [MONO_ASSEMBLY_HASH_ALG]) {
case ASSEMBLY_HASH_NONE:
case ASSEMBLY_HASH_MD5:
default:
ADD_ERROR (list, g_strdup_printf ("Hash algorithm 0x%x unknown", cols [MONO_ASSEMBLY_HASH_ALG]));
}
-
+
if (!is_valid_assembly_flags (cols [MONO_ASSEMBLY_FLAGS]))
ADD_ERROR (list, g_strdup_printf ("Invalid flags in assembly: 0x%x", cols [MONO_ASSEMBLY_FLAGS]));
-
+
if (!is_valid_blob (image, cols [MONO_ASSEMBLY_PUBLIC_KEY], FALSE))
ADD_ERROR (list, g_strdup ("Assembly public key is an invalid index"));
-
+
if (!(p = is_valid_string (image, cols [MONO_ASSEMBLY_NAME], TRUE))) {
ADD_ERROR (list, g_strdup ("Assembly name is invalid"));
} else {
mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
if (!is_valid_assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]))
ADD_ERROR (list, g_strdup_printf ("Invalid flags in assemblyref row %d: 0x%x", i + 1, cols [MONO_ASSEMBLY_FLAGS]));
-
+
if (!is_valid_blob (image, cols [MONO_ASSEMBLYREF_PUBLIC_KEY], FALSE))
ADD_ERROR (list, g_strdup_printf ("AssemblyRef public key in row %d is an invalid index", i + 1));
-
+
if (!(p = is_valid_string (image, cols [MONO_ASSEMBLYREF_CULTURE], FALSE))) {
ADD_ERROR (list, g_strdup_printf ("AssemblyRef culture in row %d is invalid", i + 1));
} else {
MonoTableInfo *tdef = &image->tables [MONO_TABLE_TYPEDEF];
guint32 cols [MONO_CLASS_LAYOUT_SIZE];
guint32 value, i;
-
+
if (level & MONO_VERIFY_ERROR) {
for (i = 0; i < t->rows; ++i) {
mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
* assemblies, though (the spec claims we didn't have to, bah).
*/
/*
- * We need to check that the parent types have the samme layout
+ * We need to check that the parent types have the same layout
* type as well.
*/
}
}
}
-
+
return list;
}
guint32 cols [MONO_CONSTANT_SIZE];
guint32 value, i;
GHashTable *dups = g_hash_table_new (NULL, NULL);
-
+
for (i = 0; i < t->rows; ++i) {
mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
guint32 cols [MONO_EVENT_SIZE];
const char *p;
guint32 value, i;
-
+
for (i = 0; i < t->rows; ++i) {
mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
ADD_WARN (list, MONO_VERIFY_CLS, g_strdup_printf ("Invalid CLS name '%s` in Event row %d", p, i + 1));
}
}
-
+
if (level & MONO_VERIFY_ERROR && cols [MONO_EVENT_TYPE]) {
value = cols [MONO_EVENT_TYPE] >> MONO_TYPEDEFORREF_BITS;
switch (cols [MONO_EVENT_TYPE] & MONO_TYPEDEFORREF_MASK) {
guint32 cols [MONO_FIELD_SIZE];
const char *p;
guint32 i, flags;
-
+
for (i = 0; i < t->rows; ++i) {
mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
/*
if ((flags & FIELD_ATTRIBUTE_RT_SPECIAL_NAME) && !(flags & FIELD_ATTRIBUTE_SPECIAL_NAME))
ADD_ERROR (list, g_strdup_printf ("RTSpecialName needs also SpecialName set in Field row %d", i + 1));
/*
- * FIXME: check there is only ono owner in the respective table.
+ * FIXME: check there is only one owner in the respective table.
* if (flags & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL)
* if (flags & FIELD_ATTRIBUTE_HAS_DEFAULT)
* if (flags & FIELD_ATTRIBUTE_HAS_FIELD_RVA)
* public or private (not allowed in cls mode).
* if owner is an enum ...
*/
-
-
+
+
}
return list;
}
const char *p;
guint32 i;
GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
-
+
for (i = 0; i < t->rows; ++i) {
mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
if (level & MONO_VERIFY_ERROR) {
const char *p, *pf;
guint32 found, i, j, value;
GHashTable *dups = g_hash_table_new (g_str_hash, g_str_equal);
-
+
for (i = 0; i < t->rows; ++i) {
mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
if (!(p = is_valid_string (image, cols [MONO_MODULEREF_NAME], TRUE))) {
for (i = 0; i < header->num_clauses; ++i) {
clause = &header->clauses [i];
- if (MONO_OFFSET_IN_CLAUSE (clause, offset) && !MONO_OFFSET_IN_CLAUSE (clause, target))
+ if (MONO_OFFSET_IN_CLAUSE (clause, offset) ^ MONO_OFFSET_IN_CLAUSE (clause, target))
return 0;
- if (MONO_OFFSET_IN_HANDLER (clause, offset) && !MONO_OFFSET_IN_HANDLER (clause, target))
+ if (MONO_OFFSET_IN_HANDLER (clause, offset) ^ MONO_OFFSET_IN_HANDLER (clause, target))
return 0;
/* need to check filter ... */
}
return 1;
}
-static int
+static gboolean
is_valid_bool_arg (ILStackDesc *arg)
{
+ if (IS_MANAGED_POINTER (arg->stype))
+ return TRUE;
switch (arg->stype) {
case TYPE_I4:
case TYPE_I8:
+ case TYPE_NATIVE_INT:
case TYPE_PTR:
- case TYPE_COMPLEX:
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 !arg->type->data.generic_class->container_class->valuetype;
+ }
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 &mono_class_from_mono_type (type)->this_arg;
+}
+
+
+/*Returns the byval version of the supplied MonoType*/
+static MonoType*
+mono_type_get_type_byval (MonoType *type)
+{
+ if (!type->byref)
+ return type;
+ return &mono_class_from_mono_type (type)->byval_arg;
+}
+static MonoType*
+mono_type_from_stack_slot (ILStackDesc *slot)
+{
+ if (IS_MANAGED_POINTER (slot->stype))
+ return mono_type_get_type_byref (slot->type);
+ return slot->type;
+}
/*Stack manipulation code*/
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", size, ctx->eval.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;
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", ctx->eval.size + 1));
+ 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;
+}
+
+static gboolean
+check_unmanaged_pointer (VerifyContext *ctx, ILStackDesc *value)
+{
+ if (value->stype == 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;
+}
+
+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)
{
return ctx->eval.stack + (ctx->eval.size - distance - 1);
}
+/* 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)
+{
+ MonoType *type = mono_type_get_full (ctx->image, token, ctx->generic_context);
+
+ if (!type) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Type (0x%08x) not found at 0x%04x", token, ctx->ip_offset));
+ return NULL;
+ }
+
+ 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 NULL;
+ }
+
+ if (type->type == MONO_TYPE_TYPEDBYREF) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid use of typedbyref at 0x%04x", ctx->ip_offset));
+ return NULL;
+ }
+
+ check_unverifiable_type (ctx, type);
+ return type;
+}
+
/*operation result tables */
static void
dump_stack_value (ILStackDesc *value)
{
- VERIFIER_DEBUG ( printf ("[(%d)(%d)", value->type->type, value->stype); );
+ printf ("[(%d)(%d)", value->type->type, value->stype);
if (value->stype & CMMP_MASK)
printf ("Controled Mutability MP: ");
-
- if (value->stype & POINTER_MASK)
- printf ("Managed Pointer: ");
-
- switch (value->stype & TYPE_MASK) {
+
+ if (IS_MANAGED_POINTER (value->stype))
+ printf ("Managed Pointer to: ");
+
+ switch (UNMASK_TYPE (value->stype)) {
case TYPE_INV:
printf ("invalid type]");
return;
printf ("float64]");
return;
case TYPE_PTR:
- printf ("pointer]");
+ printf ("unmanaged pointer]");
return;
case TYPE_COMPLEX:
- printf ("complex]");
- return;
+ 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, 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);
+ return;
+ default:
+ printf ("unknown complex %d type]\n", value->type->type);
+ g_assert_not_reached ();
+ }
default:
- printf ("unknown %d type]", value->stype);
+ printf ("unknown stack %d type]\n", value->stype);
g_assert_not_reached ();
}
}
dump_stack_state (ILCodeDesc *state)
{
int i;
-
+
printf ("(%d) ", state->size);
for (i = 0; i < state->size; ++i)
dump_stack_value (state->stack + i);
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))
return FALSE;
-
+
for (i = 0; i < left->numsizes; ++i)
if (left->sizes [i] != right->sizes [i])
return FALSE;
static int
get_stack_type (MonoType *type)
{
- int t = type->type;
int mask = 0;
+ int type_kind = type->type;
if (type->byref)
mask = POINTER_MASK;
/*TODO handle CMMP_MASK */
handle_enum:
- switch (t) {
+ switch (type_kind) {
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
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:
- mask = POINTER_MASK;
- type = type->type;
- t = type->type;
- goto handle_enum;
-
+ case MONO_TYPE_TYPEDBYREF:
+ return TYPE_PTR | mask;
+
case MONO_TYPE_CLASS:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
- case MONO_TYPE_TYPEDBYREF:
-
- /* TODO verify if this case is correct */
- case MONO_TYPE_FNPTR:
-
case MONO_TYPE_GENERICINST:
return TYPE_COMPLEX | mask;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
- t = type->data.klass->enum_basetype->type;
+ type = type->data.klass->enum_basetype;
+ type_kind = type->type;
goto handle_enum;
} else
return TYPE_COMPLEX | mask;
default:
- printf ("unknown type %02x in eval stack type", type->type);
+ VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
g_assert_not_reached ();
return 0;
}
static void
set_stack_value (ILStackDesc *stack, MonoType *type, int take_addr)
{
- int t = type->type;
int mask = 0;
-
- stack->type = type;
+ int type_kind = type->type;
if (type->byref || take_addr)
mask = POINTER_MASK;
/* TODO handle CMMP_MASK */
handle_enum:
- switch (t) {
+ stack->type = type;
+
+ switch (type_kind) {
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_U:
stack->stype = TYPE_NATIVE_INT | mask;
return;
-
+
+ /*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:
- mask = POINTER_MASK;
- /* We should use the underlying pointer type */
- stack->type = type = type->type;
- t = type->type;
- goto handle_enum;
-
+ case MONO_TYPE_TYPEDBYREF:
+ stack->stype = TYPE_PTR | mask;
+ return;
+
case MONO_TYPE_CLASS:
case MONO_TYPE_STRING:
case MONO_TYPE_OBJECT:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_ARRAY:
- case MONO_TYPE_TYPEDBYREF:
-
- //TODO verify if this case is correct
- case MONO_TYPE_FNPTR:
-
+
case MONO_TYPE_GENERICINST:
+ case MONO_TYPE_VAR:
+ case MONO_TYPE_MVAR:
stack->stype = TYPE_COMPLEX | mask;
return;
case MONO_TYPE_I8:
return;
case MONO_TYPE_VALUETYPE:
if (type->data.klass->enumtype) {
- t = type->data.klass->enum_basetype->type;
+ type = type->data.klass->enum_basetype;
+ type_kind = type->type;
goto handle_enum;
} else {
stack->stype = TYPE_COMPLEX | mask;
return;
}
default:
- printf ("unknown type %02x in eval stack type", type->type);
+ VERIFIER_DEBUG ( printf ("unknown type 0x%02x in eval stack type\n", type->type); );
g_assert_not_reached ();
}
return;
}
-
/* Generics validation stuff, should be moved to another metadata/? file */
static gboolean
case MONO_TYPE_CLASS:
if (candidate->type != MONO_TYPE_CLASS)
return FALSE;
-
- printf ("verifying type class%d %d\n", target->data.klass, candidate->data.klass);
+
+ VERIFIER_DEBUG ( printf ("verifying type class %p %p\n", target->data.klass, candidate->data.klass); );
return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
case MONO_TYPE_OBJECT:
- printf("verifying object type\n");
return MONO_TYPE_IS_REFERENCE (candidate);
case MONO_TYPE_SZARRAY:
if (candidate->type != MONO_TYPE_SZARRAY)
return FALSE;
return mono_class_is_assignable_from (target->data.klass, candidate->data.klass);
-
+
case MONO_TYPE_VALUETYPE:
if (target->data.klass->enumtype) {
- target = target->data.klass->enum_basetype->type;
+ target = target->data.klass->enum_basetype;
goto handle_enum;
} else {
if (candidate->type != MONO_TYPE_VALUETYPE)
if (candidate->type != MONO_TYPE_ARRAY)
return FALSE;
return is_array_type_compatible (target, candidate);
-
+
default:
- printf ("-----unknown target type %d\n", target->type);
+ VERIFIER_DEBUG ( printf ("unknown target type %d\n", target->type); );
g_assert_not_reached ();
}
-
+
return FALSE;
}
mono_is_generic_instance_compatible (MonoGenericClass *target, MonoGenericClass *candidate, MonoGenericClass *root_candidate) {
MonoGenericContainer *container;
int i;
-
- printf ("candidate container %d\n", candidate->container_class->generic_container);
+
+ VERIFIER_DEBUG ( printf ("candidate container %p\n", candidate->container_class->generic_container); );
if (target->container_class != candidate->container_class) {
MonoType *param_class;
MonoClass *cand_class;
- printf("DIFERENT GENERIC CLASS\n");
- param_class = candidate->inst->type_argv[0];
- printf("param 0 %d\n", param_class->type);
+ VERIFIER_DEBUG ( printf ("generic class != target\n"); );
+ param_class = candidate->context.class_inst->type_argv [0];
+ VERIFIER_DEBUG ( printf ("param 0 %d\n", param_class->type); );
cand_class = candidate->container_class;
-
+
/* We must check if it's an interface type*/
if (MONO_CLASS_IS_INTERFACE (target->container_class)) {
- printf ("-----VERIFY INTERFACES\n");
-
+ VERIFIER_DEBUG ( printf ("generic type is an interface\n"); );
+
do {
int iface_count = cand_class->interface_count;
MonoClass **ifaces = cand_class->interfaces;
int i;
- printf ("type has %d interfaces\n", iface_count);
+ VERIFIER_DEBUG ( printf ("type has %d interfaces\n", iface_count); );
for (i = 0; i< iface_count; ++i) {
MonoClass *ifc = ifaces[i];
- printf ("analysing %s\n", ifc->name);
+ VERIFIER_DEBUG ( printf ("analysing %s\n", ifc->name); );
if (ifc->generic_class) {
- printf ("interface has generic info\n");
+ VERIFIER_DEBUG ( printf ("interface has generic info\n"); );
}
if (mono_is_generic_instance_compatible (target, ifc->generic_class, root_candidate)) {
- printf ("we got compatible stuff!\n");
+ VERIFIER_DEBUG ( printf ("we got compatible stuff!\n"); );
return TRUE;
}
}
-
+
cand_class = cand_class->parent;
} while (cand_class);
- printf ("don't implements an interface\n");
-
+ VERIFIER_DEBUG ( printf ("don't implements an interface\n"); );
+
} else {
- printf ("verifying upper classes\n");
-
+ VERIFIER_DEBUG ( printf ("verifying upper classes\n"); );
+
cand_class = cand_class->parent;
-
+
while (cand_class) {
- printf ("verifying parent class\n");
- printf ("name %s\n", cand_class->name);
+ VERIFIER_DEBUG ( printf ("verifying parent class name %s\n", cand_class->name); );
if (cand_class->generic_class) {
- printf ("super type has generic context\n");
+ VERIFIER_DEBUG ( printf ("super type has generic context\n"); );
/* TODO break loop if target->container_class == cand_class->generic_class->container_class */
- if (mono_is_generic_instance_compatible (target, cand_class->generic_class, root_candidate)) {
- printf ("we got compatible stuff!\n");
- return TRUE;
- }
+ return mono_is_generic_instance_compatible (target, cand_class->generic_class, root_candidate);
} else
- printf ("super class has no generic context\n");
+ VERIFIER_DEBUG ( printf ("super class has no generic context\n"); );
cand_class = cand_class->parent;
}
}
return FALSE;
}
-
+
/* now we verify if the instantiations are compatible*/
- if (target->inst == candidate->inst) {
- printf ("SAME INSTANTIATION\n");
+ if (target->context.class_inst == candidate->context.class_inst) {
+ VERIFIER_DEBUG ( printf ("generic types are compatible, both have the same instantiation\n"); );
return TRUE;
}
-
- if (target->inst->type_argc != candidate->inst->type_argc) {
- printf ("generic instantiations with diferent arg counts\n");
+
+ if (target->context.class_inst->type_argc != candidate->context.class_inst->type_argc) {
+ VERIFIER_DEBUG ( printf ("generic instantiations with different arg counts\n"); );
return FALSE;
}
-
+
//verify if open instance -- none should be
-
+
container = target->container_class->generic_container;
-
+
for (i = 0; i < container->type_argc; ++i) {
MonoGenericParam *param = container->type_params + i;
- MonoType *target_type = target->inst->type_argv [i];
- MonoType *candidate_type = candidate->inst->type_argv [i];
+ MonoType *target_type = target->context.class_inst->type_argv [i];
+ MonoType *candidate_type = candidate->context.class_inst->type_argv [i];
/* We resolve TYPE_VAR types before proceeding */
if (candidate_type->type == MONO_TYPE_VAR) {
MonoGenericParam *var_param = candidate_type->data.generic_param;
- candidate_type = root_candidate->inst->type_argv[var_param->num];
+ candidate_type = root_candidate->context.class_inst->type_argv [var_param->num];
}
if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK) == 0) {
- printf ("NO VARIANCE, VERIFY EXACT TYPE %d %d \n",target_type->type, candidate_type->type);
- if (candidate_type->type == MONO_TYPE_VAR) {
- MonoGenericParam *var_param = candidate_type->data.generic_param;
- MonoType *resolved_param = var_param->owner->context.class_inst->type_argv[var_param->num];
- printf ("VAR container %d named %s\n", var_param->owner, var_param->name);
- printf ("resolving the var param: %d\n", resolved_param->type);
- }
-
+ VERIFIER_DEBUG ( printf ("generic type have no variance flag, checking each type %d %d \n",target_type->type, candidate_type->type); );
+
if (!mono_metadata_type_equal (target_type, candidate_type))
return FALSE;
} else {
- printf ("NEED TO CHECK VARIANCE\n");
+ VERIFIER_DEBUG ( printf ("generic type has variance flag, need to perform deeper check\n"); );
/* first we check if they are the same kind */
/* byref generic params are forbiden, but better safe than sorry.*/
-
- if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) == GENERIC_PARAMETER_ATTRIBUTE_COVARIANT)
- return mono_is_generic_type_compatible (target_type, candidate_type);
- else /* the attribute must be contravariant */
- return mono_is_generic_type_compatible (candidate_type, target_type);
+
+ if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) == GENERIC_PARAMETER_ATTRIBUTE_COVARIANT) {
+ if (!mono_is_generic_type_compatible (target_type, candidate_type))
+ return FALSE;
+ /* the attribute must be contravariant */
+ } else if (!mono_is_generic_type_compatible (candidate_type, target_type))
+ return FALSE;
}
}
return TRUE;
* If strict, check for the underlying type and not the verification stack types
*/
static gboolean
-verify_stack_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict) {
- printf ("check type compat %d %d %d\n", ctx, target, candidate);
-
- if (candidate->byref) {
- if (get_stack_type (target) == TYPE_NATIVE_INT) {
- ctx->verifiable = 0;
+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)
+
+ VERIFIER_DEBUG ( printf ("checking type compatibility %p %p[%d][%d] %p[%d][%d]\n", ctx, target, target->type, target->byref, candidate, candidate->type, candidate->byref); );
+
+ /*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;
handle_enum:
switch (target->type) {
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:
+ 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 candidate->type == target->type;
- return get_stack_type (candidate) == TYPE_I4;
+ return is_native_int || is_int4;
+ return is_native_int || get_stack_type (candidate) == TYPE_I4;
+ }
case MONO_TYPE_I8:
case MONO_TYPE_U8:
- if (strict)
- return candidate->type == target->type;
- return get_stack_type (candidate) == TYPE_I8;
-
+ 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 get_stack_type (target) == TYPE_R8;
+ return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
case MONO_TYPE_I:
- case MONO_TYPE_U:
+ 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 candidate->type == target->type;
- return get_stack_type (target) == TYPE_NATIVE_INT;
-
+ 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_stack_type_compatibility (ctx, target->data.type, candidate->data.type, TRUE);
+ 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 (target);
+ right = mono_type_get_signature (candidate);
+ return mono_metadata_signature_equal (left, right) && left->call_convention == right->call_convention;
+ }
case MONO_TYPE_GENERICINST: {
MonoGenericClass *left;
MonoGenericClass *right;
- if (target->type != MONO_TYPE_GENERICINST)
+ if (candidate->type != MONO_TYPE_GENERICINST)
return FALSE;
left = target->data.generic_class;
right = candidate->data.generic_class;
- printf ("HERE I VERIFY IF THE GENERIC TYPES ARE COMPATIBLE\n");
return mono_is_generic_instance_compatible (left, right, right);
}
return candidate->type == MONO_TYPE_STRING;
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_OBJECT:
return MONO_TYPE_IS_REFERENCE (candidate);
case MONO_TYPE_SZARRAY: {
MonoClass *left;
MonoClass *right;
- if (target->type != MONO_TYPE_SZARRAY)
+ if (candidate->type != MONO_TYPE_SZARRAY)
return FALSE;
left = target->data.klass;
//TODO verify aditional checks that needs to be done
case MONO_TYPE_TYPEDBYREF:
- return candidate->type != MONO_TYPE_TYPEDBYREF;
+ return candidate->type == MONO_TYPE_TYPEDBYREF;
case MONO_TYPE_VALUETYPE:
if (target->data.klass->enumtype) {
- target = target->data.klass->enum_basetype->type;
+ target = target->data.klass->enum_basetype;
goto handle_enum;
} else {
if (candidate->type != MONO_TYPE_VALUETYPE)
return FALSE;
return target->data.klass == candidate->data.klass;
}
+
+ case MONO_TYPE_VAR:
+ if (candidate->type != MONO_TYPE_VAR)
+ return FALSE;
+ return candidate->data.generic_param->num == target->data.generic_param->num;
+
+ case MONO_TYPE_MVAR:
+ if (candidate->type != MONO_TYPE_MVAR)
+ return FALSE;
+ return candidate->data.generic_param->num == target->data.generic_param->num;
default:
- printf ("unknown store type %d\n", target->type);
+ 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
}
-/*
- TODO MONO_TYPE_PTR:
- TODO MONO_TYPE_FNPTR:
-*/
+static gboolean
+verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
+{
+ return verify_type_compatibility_full (ctx, target, candidate, FALSE);
+}
+
+
static int
-verify_type_compat (VerifyContext *ctx, MonoType *type, ILStackDesc *stack) {
- int stack_type = stack->stype;
- printf ("before stack load\n");
- printf ("check compat %d %d %d\n", ctx, stack, type);
- if (type->byref) {
- if (stack_type == TYPE_NATIVE_INT) {
- ctx->verifiable = 0;
- return TRUE;
+verify_stack_type_compatibility (VerifyContext *ctx, MonoType *type, ILStackDesc *stack)
+{
+ return verify_type_compatibility_full (ctx, type, mono_type_from_stack_slot (stack), FALSE);
+}
+
+/* implement the opcode checks*/
+static void
+push_arg (VerifyContext *ctx, unsigned int arg, int take_addr)
+{
+ 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_defaults.int32_class->byval_arg);
}
- return FALSE;
- }
-
-handle_enum:
- switch (type->type) {
- 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 stack_type == TYPE_I4;
-
- case MONO_TYPE_I8:
- case MONO_TYPE_U8:
- return stack_type == TYPE_I8;
-
- case MONO_TYPE_R4:
- case MONO_TYPE_R8:
- return stack_type == TYPE_R8;
-
- case MONO_TYPE_I:
- case MONO_TYPE_U:
- return stack_type == TYPE_NATIVE_INT;
-
- case MONO_TYPE_PTR:
- if ((stack_type & POINTER_MASK) == 0)
- return FALSE;
- stack_type &= ~POINTER_MASK;
- if (stack_type != TYPE_COMPLEX) {
- type = type->data.type;
- goto handle_enum;
- }
- return verify_stack_type_compatibility (ctx, type->data.type, stack->type, TRUE);
-
- case MONO_TYPE_GENERICINST: {
- MonoGenericClass *left;
- MonoGenericClass *right;
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
- g_assert (stack->type);
- if (stack->type->type != MONO_TYPE_GENERICINST)
- return FALSE;
- left = type->data.generic_class;
- right = stack->type->data.generic_class;
-
- printf ("HERE I VERIFY IF THE GENERIC TYPES ARE COMPATIBLE\n");
- return mono_is_generic_instance_compatible (left, right, right);
- }
-
- case MONO_TYPE_STRING:
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
- g_assert (stack->type);
- return stack->type->type == MONO_TYPE_STRING;
-
- case MONO_TYPE_CLASS:
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
- g_assert (stack->type);
- if (stack->type->type != MONO_TYPE_CLASS)
- return FALSE;
-
- return mono_class_is_assignable_from (type->data.klass, stack->type->data.klass);
-
- case MONO_TYPE_OBJECT:
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
- g_assert (stack->type);
- return MONO_TYPE_IS_REFERENCE (stack->type);
-
- case MONO_TYPE_SZARRAY: {
- MonoClass *left;
- MonoClass *right;
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
-
- g_assert (stack->type);
-
- if (stack->type->type != type->type)
- return FALSE;
- left = type->data.klass ;
- right = stack->type->data.klass;
- return mono_class_is_assignable_from (left, right);
- }
-
- case MONO_TYPE_ARRAY:
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
- g_assert (stack->type);
- if (stack->type->type != type->type)
- return FALSE;
- return is_array_type_compatible (type, stack->type);
-
- /*TODO verify aditional checks that needs to be done */
- case MONO_TYPE_TYPEDBYREF:
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
- g_assert (stack->type);
- if (stack->type->type != type->type)
- return FALSE;
- return TRUE;
-
- case MONO_TYPE_VALUETYPE:
- if (type->data.klass->enumtype) {
- type = type->data.klass->enum_basetype->type;
- goto handle_enum;
- } else {
- if (stack_type != TYPE_COMPLEX)
- return FALSE;
- g_assert (stack->type);
- if (stack->type->type != type->type)
- return FALSE;
- return stack->type->data.klass == type->data.klass;
- }
-
- default:
- printf("unknown store type %d\n", type->type);
- g_assert_not_reached ();
- return FALSE;
- }
- return 1;
-}
-
-/* implement the opcode checks*/
-static void
-push_arg (VerifyContext *ctx, unsigned int arg)
-{
- if (arg >= ctx->max_args) {
- ADD_VERIFY_ERROR(ctx, g_strdup_printf ("Method doesn't have argument %d", arg + 1));
- } else if (check_overflow (ctx)) {
- set_stack_value (stack_push (ctx), ctx->params [arg], FALSE);
- }
-}
+ } 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));
+ set_stack_value (stack_push (ctx), ctx->params [arg], take_addr);
+ }
+}
static void
-push_local (VerifyContext *ctx, guint32 arg)
+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)) {
- set_stack_value (stack_push (ctx), ctx->locals [arg], FALSE);
+ /*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));
+
+ set_stack_value (stack_push (ctx), ctx->locals [arg], take_addr);
}
}
store_arg (VerifyContext *ctx, guint32 arg)
{
ILStackDesc *value;
-
- printf ("in store arg %d -- %d\n", arg, ctx->max_args);
+
if (arg >= ctx->max_args) {
- printf ("local overflow\n");
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
+ 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);
return;
}
- printf ("ok with arg count\n");
if (check_underflow (ctx, 1)) {
- printf ("no underflow\n");
value = stack_pop (ctx);
- printf ("check compat %d %d %d %d\n", ctx, value, ctx->locals [arg], arg);
- if (!verify_type_compat (ctx, ctx->params [arg], value)) {
- printf ("not compat\n");
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible type %s in local store at 0x%04x", type_names [value->stype & TYPE_MASK], ctx->ip_offset));
- printf ("error added\n");
+ 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", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
}
}
}
store_local (VerifyContext *ctx, guint32 arg)
{
ILStackDesc *value;
- printf ("in store local %d -- %d\n", arg, ctx->num_locals);
if (arg >= ctx->num_locals) {
- printf ("local overflow\n");
ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
return;
}
- printf ("ok with local count\n");
/*TODO verify definite assigment */
if (check_underflow (ctx, 1)) {
- printf ("no underflow\n");
value = stack_pop(ctx);
- printf ("check compat %d %d %d %d\n", ctx, value, ctx->locals [arg], arg);
- if (!verify_type_compat (ctx, ctx->locals [arg], value)) {
- printf ("not compat\n");
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible type %s in local store at 0x%04x", type_names [value->stype & TYPE_MASK], ctx->ip_offset));
- printf ("error added\n");
+ if (!verify_stack_type_compatibility (ctx, ctx->locals [arg], value)) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in local store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
}
}
}
ILStackDesc *a, *b;
int idxa, idxb, complexMerge = 0;
unsigned char res;
-
+
if (!check_underflow (ctx, 2))
return;
a = stack_get (ctx, 1);
b = stack_top (ctx);
-
+
idxa = a->stype;
- if (idxa & POINTER_MASK) {
+ if (IS_MANAGED_POINTER (idxa)) {
idxa = TYPE_PTR;
complexMerge = 1;
}
-
+
idxb = b->stype;
- if (idxb & POINTER_MASK) {
+ if (IS_MANAGED_POINTER (idxb)) {
idxb = TYPE_PTR;
complexMerge = 2;
}
-
+
--idxa;
--idxb;
res = table [idxa][idxb];
- printf ("binop res %d\n", res);
- printf ("idxa %d idxb %d\n", idxa, idxb);
-
+ VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
+ VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
+
+ ctx->eval.size--;
if (res == TYPE_INV) {
- ADD_VERIFY_ERROR(ctx, g_strdup_printf (
- "Binary instruction applyed to ill formed stack (%s x %s)",
- type_names [idxa & TYPE_MASK], type_names [idxb & TYPE_MASK]));
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction applyed to ill formed stack (%s x %s)", type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)]));
return;
}
-
+
if (res & NON_VERIFIABLE_RESULT) {
- printf ("method cannot be verified\n");
- ctx->verifiable = 0;
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Binary instruction is not verifiable (%s x %s)",
+ type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)]));
+
res = res & ~NON_VERIFIABLE_RESULT;
}
} else
stack_top (ctx)->stype = res;
- ctx->eval.size--;
}
+
+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;
+ }
+
+ if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
+ 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", type_names [UNMASK_TYPE (stack_get (ctx, -1)->stype)], ctx->ip_offset));
+
+ check_unmanaged_pointer (ctx, top);
+}
+
+
static void
do_branch_op (VerifyContext *ctx, signed int delta, const unsigned char table [TYPE_MAX][TYPE_MAX])
{
int idxa, idxb;
unsigned char res;
int target = ctx->ip_offset + delta;
-
- printf ("offset %d delta %d target %d\n", ctx->ip_offset, delta, target);
-
+
+ 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;
}
+
if (!in_same_block (ctx->header, ctx->ip_offset, target)) {
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 = a->stype;
- if (idxa & POINTER_MASK)
+ if (IS_MANAGED_POINTER (idxa))
idxa = TYPE_PTR;
-
+
idxb = b->stype;
- if (idxb & POINTER_MASK)
+ if (IS_MANAGED_POINTER (idxb))
idxb = TYPE_PTR;
-
+
--idxa;
--idxb;
res = table [idxa][idxb];
- printf ("binop res %d\n", res);
- printf ("idxa %d idxb %d\n", idxa, idxb);
-
+ VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
+ VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
+
if (res == TYPE_INV) {
ADD_VERIFY_ERROR (ctx,
g_strdup_printf ("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x",
- type_names [idxa & TYPE_MASK], type_names [idxb & TYPE_MASK], ctx->ip_offset));
+ type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
} else if (res & NON_VERIFIABLE_RESULT) {
- printf ("method cannot be verified\n");
- ctx->verifiable = 0;
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare and Branch instruction is not verifiable (%s x %s) at 0x%04x",
+ type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
res = res & ~NON_VERIFIABLE_RESULT;
}
}
ILStackDesc *a, *b;
int idxa, idxb;
unsigned char res;
-
+
if (!check_underflow (ctx, 2))
return;
b = stack_pop (ctx);
a = stack_pop (ctx);
-
+
idxa = a->stype;
- if (idxa & POINTER_MASK)
+ if (IS_MANAGED_POINTER (idxa))
idxa = TYPE_PTR;
-
+
idxb = b->stype;
- if (idxb & POINTER_MASK)
+ if (IS_MANAGED_POINTER (idxb))
idxb = TYPE_PTR;
-
+
--idxa;
--idxb;
res = table [idxa][idxb];
- printf("binop res %d\n", res);
- printf("idxa %d idxb %d\n", idxa, idxb);
-
if(res == TYPE_INV) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf("Compare and Branch instruction applyed to ill formed stack (%s x %s) at 0x%04x", type_names [idxa & TYPE_MASK], type_names [idxb & TYPE_MASK], ctx->ip_offset));
- return;
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
} else if (res & NON_VERIFIABLE_RESULT) {
- printf ("method cannot be verified\n");
- ctx->verifiable = 0;
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Compare instruction is not verifiable (%s x %s) at 0x%04x",
+ type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
res = res & ~NON_VERIFIABLE_RESULT;
}
- stack_push_val (&ctx, TYPE_I4, &mono_defaults.int_class->byval_arg);
+ stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
}
-static void
-do_ret (VerifyContext *ctx)
-{
- printf ("checking ret\n");
- if (ctx->signature->ret->type != MONO_TYPE_VOID) {
- ILStackDesc *top;
- printf ("underflow\n");
- if (!check_underflow (ctx, 1))
- return;
-
- printf ("pop\n");
- top = stack_pop(ctx);
-
- printf ("check compat %d \n", ctx->signature->ret);
- if (!verify_type_compat (ctx, ctx->signature->ret, top)) {
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
- return;
- }
- }
-
- printf ("check ret\n");
+static void
+do_ret (VerifyContext *ctx)
+{
+ VERIFIER_DEBUG ( printf ("checking ret\n"); );
+ if (ctx->signature->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)) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible return value on stack with method signature ret at 0x%04x", ctx->ip_offset));
+ return;
+ }
+ }
+
+ 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))
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
+}
+
+/* FIXME: we could just load the signature instead of the whole MonoMethod
+ * TODO handle vararg calls
+ * TODO handle non virt calls to non-final virtual calls (from the verifiability clause in page 52 of partition 3)
+ * TODO handle abstract calls
+ * TODO handle calling .ctor outside one or calling the .ctor for other class but super
+ * TODO handle call invoking virtual methods (only allowed to invoke super)
+ */
+static void
+do_invoke_method (VerifyContext *ctx, int method_token)
+{
+ int param_count, i;
+ MonoMethodSignature *sig;
+ ILStackDesc *value;
+ MonoMethod *method = mono_get_method_full (ctx->image, method_token, NULL, ctx->generic_context);
+
+ if (!method) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method 0x%08x not found at 0x%04x", method_token, ctx->ip_offset));
+ return;
+ }
+
+ if (!(sig = mono_method_signature (method)))
+ sig = mono_method_get_signature (method, ctx->image, method_token);
+
+ param_count = sig->param_count + sig->hasthis;
+ if (!check_underflow (ctx, param_count))
+ return;
+
+ 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 (sig->hasthis) {
+ MonoType * type = method->klass->valuetype ? &method->klass->this_arg : &method->klass->byval_arg;
+
+ VERIFIER_DEBUG ( printf ("verifying this argument\n"); );
+ value = stack_pop (ctx);
+ if (!verify_stack_type_compatibility (ctx, type, value)) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Incompatible this argument on stack with method signature ret at 0x%04x", ctx->ip_offset));
+ return;
+ }
+ }
+ if (!mono_method_can_access_method (ctx->method, method))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method is not accessible at 0x%04x", ctx->ip_offset));
+
+ if (sig->ret->type != MONO_TYPE_VOID) {
+ if (check_overflow (ctx))
+ set_stack_value (stack_push (ctx), sig->ret, FALSE);
+ }
+
+ if (sig->ret->type == MONO_TYPE_TYPEDBYREF
+ || sig->ret->byref
+ || (sig->ret->type == MONO_TYPE_VALUETYPE && !strcmp ("System", sig->ret->data.klass->name_space) && !strcmp ("ArgIterator", sig->ret->data.klass->name)))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method returns typedbyref, byref or ArgIterator at 0x%04x", ctx->ip_offset));
+}
+
+static void
+do_push_static_field (VerifyContext *ctx, int token, gboolean take_addr)
+{
+ MonoClassField *field;
+ MonoClass *klass;
+
+ field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
+ if (!field) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
+ 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 (!mono_method_can_access_field (ctx->method, field))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
+
+ set_stack_value (stack_push (ctx), field->type, take_addr);
+}
+
+static void
+do_store_static_field (VerifyContext *ctx, int token) {
+ MonoClassField *field;
+ MonoClass *klass;
+ ILStackDesc *value;
+
+ if (!check_underflow (ctx, 1))
+ return;
+
+ value = stack_pop (ctx);
+
+ field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
+ if (!field) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot store field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
+ 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 (!mono_method_can_access_field (ctx->method, field))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
+
+ if (!verify_stack_type_compatibility (ctx, field->type, value))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in static field store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
+}
+
+static gboolean
+check_is_valid_type_for_field_ops (VerifyContext *ctx, int token, ILStackDesc *obj, MonoClassField **ret_field)
+{
+ MonoClassField *field;
+ MonoClass *klass;
+
+ /*must be one of: object type, managed pointer, unmanaged pointer (native int) or an instance of a value type */
+ if (!((obj->stype == TYPE_COMPLEX)
+ /* the managed reference must be to an object or value type */
+ || (( IS_MANAGED_POINTER (obj->stype)) && (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX))
+ || (obj->stype == TYPE_NATIVE_INT)
+ || (obj->stype == TYPE_PTR)
+ || (obj->stype == TYPE_COMPLEX))) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid argument %s to load field at 0x%04x", type_names [UNMASK_TYPE (obj->stype)], ctx->ip_offset));
+ }
+
+ field = mono_field_from_token (ctx->image, token, &klass, ctx->generic_context);
+ if (!field) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ctx->ip_offset));
+ return FALSE;
+ }
+
+ *ret_field = field;
+
+ 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 (UNMASK_TYPE (obj->stype) == TYPE_COMPLEX) {
+ MonoType *type = obj->type->byref ? &field->parent->this_arg : &field->parent->byval_arg;
+
+ if (!verify_type_compatibility (ctx, type, obj->type)) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not compatible to reference the field at 0x%04x", ctx->ip_offset));
+ }
+
+ if (!mono_method_can_access_field (ctx->method, field))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
+ }
+
+ if (!mono_method_can_access_field (ctx->method, field))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Type at stack is not accessible at 0x%04x", ctx->ip_offset));
+
+ if (obj->stype == 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));
+
+ check_unmanaged_pointer (ctx, obj);
+ return TRUE;
+}
+
+static void
+do_push_field (VerifyContext *ctx, int token, gboolean take_addr)
+{
+ ILStackDesc *obj;
+ MonoClassField *field;
+
+ if (!check_underflow (ctx, 1))
+ return;
+ obj = stack_pop (ctx);
+
+ if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
+ return;
+
+ if (take_addr && field->parent->valuetype && !IS_MANAGED_POINTER (obj->stype))
+ 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 && (ctx->method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) && !strcmp (".ctor", 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));
+
+ set_stack_value (stack_push (ctx), field->type, take_addr);
+}
+
+static void
+do_store_field (VerifyContext *ctx, int token)
+{
+ ILStackDesc *value, *obj;
+ MonoClassField *field;
+
+ if (!check_underflow (ctx, 2))
+ return;
+
+ value = stack_pop (ctx);
+ obj = stack_pop (ctx);
+
+ if (!check_is_valid_type_for_field_ops (ctx, token, obj, &field))
+ 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", type_names [UNMASK_TYPE (value->stype)], 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);
+
+ if (!type)
+ return;
+
+ if (!check_underflow (ctx, 1))
+ return;
+
+ value = stack_top (ctx);
+ /*box is a nop for reference types*/
+ if (value->stype == TYPE_COMPLEX && MONO_TYPE_IS_REFERENCE (value->type) && MONO_TYPE_IS_REFERENCE (type))
+ 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));
+
+ stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (mono_defaults.object_class));
+}
+
+static void
+do_unbox_value (VerifyContext *ctx, int klass_token)
+{
+ ILStackDesc *value;
+ MonoType *type = get_boxable_mono_type (ctx, klass_token);
+
+ if (!type)
+ return;
+
+ if (!check_underflow (ctx, 1))
+ return;
+
+ value = stack_pop (ctx);
+
+ if (value->stype != TYPE_COMPLEX || value->type->type != MONO_TYPE_OBJECT)
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type %s at stack for unbox operation at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
+
+ //TODO Pushed managed pointer is haver controled mutability (CMMP)
+ set_stack_value (stack_push (ctx), mono_type_get_type_byref (type), FALSE);
+}
+
+static void
+do_unary_math_op (VerifyContext *ctx, int op)
+{
+ ILStackDesc *value;
+ if (!check_underflow (ctx, 1))
+ return;
+ value = stack_top (ctx);
+ switch(value->stype) {
+ 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 (value->type->type == MONO_TYPE_VALUETYPE && value->type->data.klass->enumtype)
+ break;
+ default:
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for unary not at 0x%04x", ctx->ip_offset));
+ }
+}
+
+static void
+do_conversion (VerifyContext *ctx, int kind)
+{
+ ILStackDesc *value;
+ if (!check_underflow (ctx, 1))
+ return;
+ value = stack_pop (ctx);
+
+ switch(value->stype) {
+ case TYPE_I4:
+ case TYPE_I8:
+ case TYPE_NATIVE_INT:
+ case TYPE_R8:
+ break;
+ default:
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid type at stack for conversion operation at 0x%04x", ctx->ip_offset));
+ }
+
+ switch (kind) {
+ case TYPE_I4:
+ stack_push_val (ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
+ break;
+ case TYPE_I8:
+ stack_push_val (ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
+ break;
+ case TYPE_R8:
+ stack_push_val (ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
+ break;
+ case TYPE_NATIVE_INT:
+ stack_push_val (ctx, TYPE_NATIVE_INT, &mono_defaults.int_class->byval_arg);
+ break;
+ default:
+ g_error ("unknown type %02x in conversion", kind);
+
+ }
+}
+
+static void
+do_load_token (VerifyContext *ctx, int token)
+{
+ gpointer handle;
+ MonoClass *handle_class;
+ if (!check_overflow (ctx))
+ return;
+ handle = mono_ldtoken (ctx->image, token, &handle_class, ctx->generic_context);
+ if (!handle) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid token 0x%x for ldtoken at 0x%04x", token, ctx->ip_offset));
+ return;
+ }
+ stack_push_val (ctx, TYPE_COMPLEX, mono_class_get_type (handle_class));
+}
+
+static void
+do_ldobj_value (VerifyContext *ctx, int token)
+{
+ ILStackDesc *value;
+ MonoType *type = get_boxable_mono_type (ctx, token);
+
+ if (!type)
+ return;
+
+ if (!check_underflow (ctx, 1))
+ return;
+
+ value = stack_pop (ctx);
+ if (!IS_MANAGED_POINTER (value->stype)
+ && value->stype != TYPE_NATIVE_INT
+ && !(value->stype == TYPE_PTR && value->type->type != MONO_TYPE_FNPTR)) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Invalid argument %s to ldobj at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
+ return;
+ }
+
+ if (value->stype == 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 (stack_push (ctx), type, FALSE);
+}
+
+#define CTOR_REQUIRED_FLAGS (METHOD_ATTRIBUTE_SPECIAL_NAME | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_PUBLIC)
+#define CTOR_INVALID_FLAGS (METHOD_ATTRIBUTE_STATIC)
+/* TODO implement delegate verification */
+static void
+do_newobj (VerifyContext *ctx, int token)
+{
+ ILStackDesc *value;
+ int i;
+ MonoMethodSignature *sig;
+ MonoMethod *method = mono_get_method_full (ctx->image, token, NULL, ctx->generic_context);
+ if (!method) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ctx->ip_offset));
+ return;
+ }
+
+ if ((method->flags & CTOR_REQUIRED_FLAGS) != CTOR_REQUIRED_FLAGS
+ || (method->flags & CTOR_INVALID_FLAGS) != 0
+ || strcmp (".ctor", method->name)) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method from token 0x%08x not a constructor at 0x%04x", token, ctx->ip_offset));
+ return;
+ }
+
+ 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));
+
+ sig = mono_method_signature (method);
+ if (!check_underflow (ctx, sig->param_count))
+ return;
+
+ 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 (check_overflow (ctx))
+ set_stack_value (stack_push (ctx), &method->klass->byval_arg, FALSE);
+}
+
+static MonoType *
+get_indirect_op_mono_type (int opcode) {
+ switch (opcode) {
+ case CEE_LDIND_I1:
+ case CEE_LDIND_U1:
+ case CEE_STIND_I1:
+ return &mono_defaults.sbyte_class->byval_arg;
+
+ case CEE_LDIND_I2:
+ case CEE_LDIND_U2:
+ case CEE_STIND_I2:
+ return &mono_defaults.int16_class->byval_arg;
+
+ case CEE_LDIND_I4:
+ case CEE_LDIND_U4:
+ case CEE_STIND_I4:
+ return &mono_defaults.int32_class->byval_arg;
+
+ case CEE_LDIND_I8:
+ case CEE_STIND_I8:
+ return &mono_defaults.int64_class->byval_arg;
+
+ case CEE_LDIND_R4:
+ case CEE_STIND_R4:
+ return &mono_defaults.single_class->byval_arg;
+
+ case CEE_LDIND_R8:
+ case CEE_STIND_R8:
+ return &mono_defaults.double_class->byval_arg;
+
+ case CEE_LDIND_I:
+ case CEE_STIND_I:
+ return &mono_defaults.int_class->byval_arg;
+
+ case CEE_LDIND_REF:
+ case CEE_STIND_REF:
+ return &mono_defaults.object_class->byval_arg;
+
+ default:
+ g_error ("unknown opcode %02x in get_indirect_op_mono_type ", opcode);
+ return NULL;
+ }
+}
+
+static void
+do_load_indirect (VerifyContext *ctx, int opcode)
+{
+ ILStackDesc *value;
+ if (!check_underflow (ctx, 1))
+ return;
+
+ value = stack_pop (ctx);
+ if (!IS_MANAGED_POINTER (value->stype)) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Load indirect not using a manager pointer at 0x%04x", ctx->ip_offset));
+ set_stack_value (stack_push (ctx), get_indirect_op_mono_type (opcode), FALSE);
+ return;
+ }
+
+ if (opcode == CEE_LDIND_REF) {
+ if (UNMASK_TYPE (value->stype) != TYPE_COMPLEX || mono_class_from_mono_type (value->type)->valuetype)
+ 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 (stack_push (ctx), mono_type_get_type_byval (value->type), FALSE);
+ } else {
+ if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (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 (stack_push (ctx), get_indirect_op_mono_type (opcode), FALSE);
+ }
+}
+
+static void
+do_store_indirect (VerifyContext *ctx, int opcode)
+{
+ ILStackDesc *addr, *val;
+ if (!check_underflow (ctx, 2))
+ return;
+
+ val = stack_pop (ctx);
+ addr = stack_pop (ctx);
+
+ check_unmanaged_pointer (ctx, addr);
+
+ if (!IS_MANAGED_POINTER (addr->stype) && addr->stype != TYPE_PTR) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid non-pointer argument to stind at 0x%04x", ctx->ip_offset));
+ return;
+ }
+
+ if (!verify_type_compatibility_full (ctx, get_indirect_op_mono_type (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_type_compatibility_full (ctx, get_indirect_op_mono_type (opcode), mono_type_get_type_byval (val->type), FALSE))
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Invalid addr 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);
+
+ if (!type)
+ return;
+
+ if (!check_underflow (ctx, 1))
+ return;
+
+ value = stack_pop (ctx);
+ if (value->stype != TYPE_I4 && value->stype != TYPE_NATIVE_INT)
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Array size type on stack (%s) is not a verifiable type at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
+
+ set_stack_value (stack_push (ctx), mono_class_get_type (mono_array_class_get (mono_class_from_mono_type (type), 1)), FALSE);
+}
+
+static void
+do_ldlen (VerifyContext *ctx)
+{
+ ILStackDesc *value;
+
+ if (!check_underflow (ctx, 1))
+ return;
+
+ value = stack_pop (ctx);
- if (ctx->eval.size > 0) {
- printf ("stack not empty!\n");
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Stack not empty (%d) after ret at 0x%04x", ctx->eval.size, ctx->ip_offset));
- } else if (in_any_block (ctx->header, ctx->ip_offset))
- ADD_VERIFY_ERROR (ctx, g_strdup_printf ("ret cannot escape exception blocks at 0x%04x", ctx->ip_offset));
+ if (value->stype != 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_defaults.int_class->byval_arg);
}
/*Merge the stacks and perform compat checks*/
merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start)
{
int i;
-
+
if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
stack_init (ctx, to);
stack_copy (to, from);
goto end_verify;
}
- printf ("DOING STACK MERGE %d x %d\n", from->size, to->size);
-
+ VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
+
if (from->size != to->size) {
- printf ("diferent stack sizes %d x %d\n", from->size, to->size);
- ctx->verifiable = 0;
+ VERIFIER_DEBUG ( printf ("different stack sizes %d x %d\n", from->size, to->size); );
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, different sizes (%d x %d)", from->size, to->size));
goto end_verify;
}
for (i = 0; i < from->size; ++i) {
ILStackDesc *from_slot = from->stack + i;
ILStackDesc *to_slot = to->stack + i;
- int from_stype = from_slot->stype;
- int to_stype = to_slot->stype;
-
- if (from_stype != to_stype) {
- printf ("diferent stack types %d x %d\n", from_stype, to_stype);
- ctx->verifiable = 0;
- continue;
- }
-
- if (from_stype & POINTER_MASK) {
- printf ("pointer stuff\n");
- from_stype &= ~POINTER_MASK;
- to_stype &= ~POINTER_MASK;
-
- printf ("fs %d ts %d ft %d tt%d \n", from_stype, to_stype, from_slot->type, to_slot->type);
-
- if (from_slot->type && ! verify_stack_type_compatibility (ctx, to_slot->type, from_slot->type, TRUE)) {
- ctx->verifiable = 0;
- printf ("pointer types not compatible, we found it!\n");
- } else
- copy_stack_value (to_slot, from_slot);
- continue;
+
+ if (!verify_type_compatibility (ctx, mono_type_from_stack_slot (to_slot), mono_type_from_stack_slot (from_slot))) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Could not merge stacks, types not compatible at 0x%04x", ctx->ip_offset));
+ goto end_verify;
}
-
- if (from_stype == TYPE_COMPLEX) {
-
- printf ("complex types %d %d\n", to_slot->type, to_slot->type);
-
- if (!to->stack [i].type) {
- printf ("complex types must allways have an associated type\n");
- ctx->verifiable = 0;
- g_assert (0);
- } else if (!verify_type_compat (ctx, to_slot->type, from_slot)) {
- printf ("stack merge not compatible\n");
- ctx->verifiable = 0;
- } else { //TODO we need to choose the base class for merging
- copy_stack_value (to_slot, from_slot);
- }
- continue;
- }
-
+
+ /*TODO we need to choose the base class for merging reference types*/
copy_stack_value (to_slot, from_slot);
}
-
+
end_verify:
to->flags = IL_CODE_FLAG_SEEN;
}
GSList*
mono_method_verify (MonoMethod *method, int level)
{
- MonoMethodSignature *csig;
const unsigned char *ip;
const unsigned char *end;
const unsigned char *target = NULL; /* branch target */
int i, n, need_merge = 0, start = 0;
guint token, ip_offset = 0, prefix = 0;
- MonoClass *klass;
- MonoMethod *cmethod;
- MonoClassField *field;
MonoGenericContext *generic_context = NULL;
MonoImage *image;
VerifyContext ctx;
-
+ VERIFIER_DEBUG ( printf ("Verify IL for method %s %s %s\n", method->klass->name, method->klass->name, method->name); );
+
if (method->iflags & (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL | METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
(method->flags & (METHOD_ATTRIBUTE_PINVOKE_IMPL | METHOD_ATTRIBUTE_ABSTRACT))) {
return NULL;
}
-
- printf ("----\n");
memset (&ctx, 0, sizeof (VerifyContext));
ctx.signature = mono_method_signature (method);
+ if (!ctx.signature) {
+ ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method signature"));
+ return ctx.list;
+ }
ctx.header = mono_method_get_header (method);
+ if (!ctx.header) {
+ ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Could not decode method header"));
+ return ctx.list;
+ }
+ ctx.method = method;
ip = ctx.header->code;
end = ip + ctx.header->code_size;
- image = method->klass->image;
+ ctx.image = image = method->klass->image;
ctx.max_args = ctx.signature->param_count + ctx.signature->hasthis;
ctx.max_stack = ctx.header->max_stack;
- ctx.verifiable = 1;
+ ctx.verifiable = ctx.valid = 1;
ctx.code = g_new0 (ILCodeDesc, ctx.header->code_size);
ctx.code_size = ctx.header->code_size;
+
memset(ctx.code, 0, sizeof (ILCodeDesc) * ctx.header->code_size);
if (ctx.signature->hasthis) {
ctx.params = g_new0 (MonoType*, ctx.max_args);
- ctx.params [0] = &method->klass->this_arg;
+ 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;
}
if (ctx.signature->is_inflated)
- generic_context = mono_method_get_context (method);
-
- stack_init(&ctx, &ctx.eval);
+ ctx.generic_context = generic_context = mono_method_get_context (method);
-
- /* TODO implement exception entry
- for (i = 0; i < header->num_clauses; ++i) {
- MonoExceptionClause *clause = &header->clauses [i];
- // catch blocks have the exception on the stack.
+ stack_init (&ctx, &ctx.eval);
+
+
+ for (i = 0; i < ctx.header->num_clauses; ++i) {
+ MonoExceptionClause *clause = ctx.header->clauses + i;
+ /* catch blocks and filter have the exception on the stack. */
+ /* must check boundaries for handler_offset and handler_start < handler_start*/
if (clause->flags == MONO_EXCEPTION_CLAUSE_NONE) {
- code [clause->handler_offset].size = 1;
- code [clause->handler_offset].flags |= IL_CODE_FLAG_SEEN;
+ ILCodeDesc *code = ctx.code + clause->handler_offset;
+ stack_init (&ctx, code);
+ code->stack [0].stype = TYPE_COMPLEX;
+ code->stack [0].type = &clause->data.catch_class->byval_arg;
+ code->size = 1;
+ code->flags = IL_CODE_FLAG_SEEN;
}
- }*/
-
- printf ("ip %u end %u %u\n", ip, end, ctx.header->code_size);
-
-
- while (ip < end && ctx.list == NULL) {
+ else if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
+ ILCodeDesc *code = ctx.code + clause->data.filter_offset;
+ stack_init (&ctx, code);
+ code->stack [0].stype = TYPE_COMPLEX;
+ code->stack [0].type = &mono_defaults.exception_class->byval_arg;
+ code->size = 1;
+ code->flags = IL_CODE_FLAG_SEEN;
+ }
+ }
+
+ while (ip < end && ctx.valid) {
ctx.ip_offset = ip_offset = ip - ctx.header->code;
-
+
/*TODO id stack merge fails, we break, should't we - or only on errors??
TODO verify need_merge
*/
if (need_merge) {
- printf ("extra merge needed! %d \n", ctx.target);
+ VERIFIER_DEBUG ( printf ("extra merge needed! %d \n", ctx.target); );
merge_stacks (&ctx, &ctx.eval, &ctx.code [ctx.target], FALSE);
need_merge = 0;
}
merge_stacks (&ctx, &ctx.eval, &ctx.code[ip_offset], start);
start = 0;
-/*TODO rename to zero */
-#if 1
+#ifdef MONO_VERIFIER_DEBUG
{
char *discode;
discode = mono_disasm_code_one (NULL, method, ip, NULL);
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.code [ip_offset]);
#endif
switch (*ip) {
case CEE_LDARG_1:
case CEE_LDARG_2:
case CEE_LDARG_3:
- push_arg (&ctx, *ip - CEE_LDARG_0);
+ push_arg (&ctx, *ip - CEE_LDARG_0, FALSE);
++ip;
break;
-
+
+ case CEE_LDARG_S:
+ case CEE_LDARGA_S:
+ push_arg (&ctx, ip [1], *ip == CEE_LDARGA_S);
+ ip += 2;
+ 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, shift_op_table);
++ip;
break;
-
+
case CEE_POP:
if (!check_underflow (&ctx, 1))
break;
case CEE_LDLOC_2:
case CEE_LDLOC_3:
/*TODO support definite assignment verification? */
- push_local (&ctx, *ip - CEE_LDLOC_0);
+ push_local (&ctx, *ip - CEE_LDLOC_0, FALSE);
++ip;
break;
-
+
case CEE_STLOC_0:
case CEE_STLOC_1:
case CEE_STLOC_2:
case CEE_LDC_I4_6:
case CEE_LDC_I4_7:
case CEE_LDC_I4_8:
- check_overflow (&ctx);
- stack_push_val (&ctx, TYPE_I4, &mono_defaults.int_class->byval_arg);
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
++ip;
break;
-
+
case CEE_LDC_I4_S:
- check_overflow (&ctx);
- stack_push_val (&ctx, TYPE_I4, &mono_defaults.int_class->byval_arg);
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx, TYPE_I4, &mono_defaults.int32_class->byval_arg);
ip += 2;
break;
-
+
case CEE_LDC_I4:
- check_overflow (&ctx);
- stack_push_val (&ctx,TYPE_I4, &mono_defaults.int_class->byval_arg);
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx,TYPE_I4, &mono_defaults.int32_class->byval_arg);
ip += 5;
break;
-
+
case CEE_LDC_I8:
- check_overflow (&ctx);
- stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx,TYPE_I8, &mono_defaults.int64_class->byval_arg);
ip += 9;
break;
case CEE_LDC_R4:
- check_overflow (&ctx);
- stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
ip += 5;
break;
case CEE_LDC_R8:
- check_overflow (&ctx);
- stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx, TYPE_R8, &mono_defaults.double_class->byval_arg);
ip += 9;
break;
case CEE_LDNULL:
- check_overflow (&ctx);
- stack_push_val (&ctx,TYPE_COMPLEX, &mono_defaults.object_class->byval_arg);
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx,TYPE_COMPLEX, &mono_defaults.object_class->byval_arg);
++ip;
break;
case CEE_BNE_UN_S:
do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_eq_op);
ip += 2;
- need_merge =1;
+ need_merge = 1;
break;
-
+
case CEE_BGE_S:
case CEE_BGT_S:
case CEE_BLE_S:
case CEE_BLT_UN_S:
do_branch_op (&ctx, (signed char)ip [1] + 2, cmp_br_op);
ip += 2;
- need_merge =1;
+ need_merge = 1;
break;
-
+
case CEE_BEQ:
case CEE_BNE_UN:
do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_eq_op);
ip += 5;
- need_merge =1;
+ need_merge = 1;
break;
case CEE_BGE:
case CEE_BLT_UN:
do_branch_op (&ctx, (gint32)read32 (ip + 1) + 5, cmp_br_op);
ip += 5;
- need_merge =1;
+ need_merge = 1;
break;
-
-
-
-
- case CEE_LDARG_S:
- case CEE_LDARGA_S:
- if (ip [1] >= ctx.max_args)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", ip [1], ip_offset));
- check_overflow (&ctx);
- type_to_eval_stack_type (ctx.params [ip [1]], stack_push (&ctx), *ip == CEE_LDARGA_S);
- ip += 2;
- break;
case CEE_LDLOC_S:
case CEE_LDLOCA_S:
- if (ip [1] >= ctx.num_locals)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", ip [1], ip_offset));
- check_overflow (&ctx);
- type_to_eval_stack_type (ctx.locals [ip [1]], stack_push(&ctx), *ip == CEE_LDLOCA_S);
-// ++cur_stack;
+ push_local (&ctx, ip[1], *ip == CEE_LDLOCA_S);
ip += 2;
break;
- case CEE_UNUSED99: ++ip; break; /* FIXME: warn/error instead? */
- case CEE_DUP:
- check_underflow (&ctx, 1);
- check_overflow (&ctx);
-// stack [cur_stack] = stack [cur_stack - 1];
- copy_stack_value (stack_push (&ctx), stack_get (&ctx, 1));
-// ++cur_stack;
+ /* FIXME: warn/error instead? */
+ case CEE_UNUSED99:
+ ++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_get (&ctx, 1));
++ip;
break;
+ }
+
case CEE_JMP:
if (ctx.eval.size)
ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Eval stack must be empty in jmp at 0x%04x", ip_offset));
break;
case CEE_CALL:
case CEE_CALLVIRT:
- token = read32 (ip + 1);
- /*
- * FIXME: we could just load the signature ...
- */
- cmethod = mono_get_method_full (image, token, NULL, generic_context);
- if (!cmethod)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method 0x%08x not found at 0x%04x", token, ip_offset));
- if (mono_method_signature (cmethod)) {
- csig = mono_method_signature (cmethod);
- } else {
- csig = mono_method_get_signature (cmethod, image, token);
- }
-
- check_underflow (&ctx, csig->param_count + csig->hasthis);
- ctx.eval.size -= csig->param_count + csig->hasthis;
- if (csig->ret->type != MONO_TYPE_VOID) {
- check_overflow (&ctx);
- type_to_eval_stack_type (csig->ret, stack_top (&ctx), FALSE);
- ++ctx.eval.size;
- }
+ do_invoke_method (&ctx, read32 (ip + 1));
ip += 5;
break;
+
case CEE_CALLI:
token = read32 (ip + 1);
/*
ip += 2;
start = 1;
break;
+
case CEE_BRFALSE_S:
case CEE_BRTRUE_S:
- target = ip + (signed char)ip [1] + 2;
- if (target >= end || target < ctx.header->code)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
- if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
- check_underflow (&ctx, 1);
- if (!is_valid_bool_arg (stack_pop (&ctx)))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", type_names [stack_top (&ctx)->stype], ip_offset));
+ do_boolean_branch_op (&ctx, (signed char)ip [1] + 2);
ip += 2;
need_merge = 1;
break;
+
case CEE_BR:
target = ip + (gint32)read32 (ip + 1) + 5;
if (target >= end || target < ctx.header->code)
ip += 5;
start = 1;
break;
+
case CEE_BRFALSE:
case CEE_BRTRUE:
- target = ip + (gint32)read32 (ip + 1) + 5;
- if (target >= end || target < ctx.header->code)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
- if (!in_same_block (ctx.header, ip_offset, target - ctx.header->code))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target escapes out of exception block at 0x%04x", ip_offset));
- check_underflow (&ctx, 1);
-// --cur_stack;
- if (!is_valid_bool_arg (stack_pop (&ctx)))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Argument type %s not valid for brtrue/brfalse at 0x%04x", type_names [stack_top (&ctx)->stype], ip_offset));
+ do_boolean_branch_op (&ctx, (gint32)read32 (ip + 1) + 5);
ip += 5;
need_merge = 1;
break;
+
case CEE_SWITCH:
n = read32 (ip + 1);
target = ip + sizeof (guint32) * n;
for (i = 0; i < n; ++i)
if (target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) >= end || target + (gint32) read32 (ip + 5 + i * sizeof (gint32)) < ctx.header->code)
ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ip_offset));
- check_underflow (&ctx, 1);
-// --cur_stack;
+ if (!check_underflow (&ctx, 1))
+ break;
if (stack_pop (&ctx)->stype != TYPE_I4)
ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to switch at 0x%04x", ip_offset));
ip += 5 + sizeof (guint32) * n;
break;
+
case CEE_LDIND_I1:
case CEE_LDIND_U1:
case CEE_LDIND_I2:
case CEE_LDIND_R4:
case CEE_LDIND_R8:
case CEE_LDIND_REF:
- check_underflow (&ctx, 1);
- if (stack_top (&ctx)->stype != TYPE_PTR && stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldind at 0x%04x", ip_offset));
- stack_top (&ctx)->stype = ldind_type [*ip - CEE_LDIND_I1];
+ do_load_indirect (&ctx, *ip);
++ip;
break;
+
case CEE_STIND_REF:
case CEE_STIND_I1:
case CEE_STIND_I2:
case CEE_STIND_I8:
case CEE_STIND_R4:
case CEE_STIND_R8:
- check_underflow (&ctx, 2);
- ctx.eval.size -= 2;
- if (stack_top (&ctx)->stype != TYPE_PTR && stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid pointer argument to stind at 0x%04x", ip_offset));
- if (!stind_type (*ip, stack_get (&ctx, -1)->stype))
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Incompatible value argument to stind at 0x%04x", ip_offset));
+ case CEE_STIND_I:
+ do_store_indirect (&ctx, *ip);
++ip;
break;
-
- case CEE_NEG:
+
case CEE_NOT:
+ case CEE_NEG:
+ do_unary_math_op (&ctx, *ip);
+ ++ip;
+ break;
+
+ //TODO: implement proper typecheck
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_U4:
- case CEE_CONV_U8:
- check_underflow (&ctx, 1);
- if (type_from_op (*ip, stack_top (&ctx)) == TYPE_INV)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
+ 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:
token = read32 (ip + 1);
- check_underflow (&ctx, 2);
+ if (!check_underflow (&ctx, 2))
+ break;
ctx.eval.size -= 2;
ip += 5;
break;
+
case CEE_LDOBJ:
- token = read32 (ip + 1);
- check_underflow (&ctx, 1);
- if (stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldobj at 0x%04x", ip_offset));
- klass = mono_class_get_full (image, token, generic_context);
- if (!klass)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot load class from token 0x%08x at 0x%04x", token, ip_offset));
- if (!klass->valuetype)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Class is not a valuetype at 0x%04x", ip_offset));
- stack_top (&ctx)->stype = TYPE_COMPLEX;
- stack_top (&ctx)->type = &klass->byval_arg;
+ do_ldobj_value (&ctx, read32 (ip + 1));
ip += 5;
break;
+
case CEE_LDSTR:
+ /*TODO verify if token is a valid string literal*/
token = read32 (ip + 1);
- check_overflow (&ctx);
- stack_push_val (&ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
-// stack_top (&ctx)->type = &mono_defaults.string_class->byval_arg;
-// stack_top (&ctx)->stype = TYPE_COMPLEX;
-// ++cur_stack;
+ if (check_overflow (&ctx))
+ stack_push_val (&ctx, TYPE_COMPLEX, &mono_defaults.string_class->byval_arg);
ip += 5;
break;
+
case CEE_NEWOBJ:
- token = read32 (ip + 1);
- /*
- * FIXME: we could just load the signature ...
- */
- cmethod = mono_get_method_full (image, token, NULL, generic_context);
- if (!cmethod)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Constructor 0x%08x not found at 0x%04x", token, ip_offset));
- csig = mono_method_signature (cmethod);
- check_underflow (&ctx, csig->param_count);
- ctx.eval.size -= csig->param_count;
- check_overflow (&ctx);
- stack_push_val (&ctx, cmethod->klass->valuetype? TYPE_COMPLEX: TYPE_COMPLEX, &cmethod->klass->byval_arg);
-
-// stack_top (&ctx)->type = &cmethod->klass->byval_arg;
-// stack_top (&ctx)->stype = cmethod->klass->valuetype? TYPE_COMPLEX: TYPE_COMPLEX;
-// ++cur_stack;
+ do_newobj (&ctx, read32 (ip + 1));
ip += 5;
break;
+
case CEE_CASTCLASS:
case CEE_ISINST:
token = read32 (ip + 1);
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
ip += 5;
break;
- case CEE_CONV_R_UN:
- check_underflow (&ctx, 1);
- ++ip;
- break;
case CEE_UNUSED58:
case CEE_UNUSED1:
++ip; /* warn, error ? */
break;
case CEE_UNBOX:
- token = read32 (ip + 1);
- check_underflow (&ctx, 1);
- if (stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument %s to unbox at 0x%04x", type_names [stack_top (&ctx)->stype], ip_offset));
-
- stack_top (&ctx)->stype = TYPE_COMPLEX;
- stack_top (&ctx)->type = NULL;
+ do_unbox_value (&ctx, read32 (ip + 1));
ip += 5;
break;
case CEE_THROW:
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
stack_pop (&ctx);
++ip;
start = 1;
break;
+
case CEE_LDFLD:
- check_underflow (&ctx, 1);
- if (stack_top (&ctx)->stype != TYPE_COMPLEX && stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument %s to ldfld at 0x%04x", type_names [stack_top (&ctx)->stype], ip_offset));
- token = read32 (ip + 1);
- field = mono_field_from_token (image, token, &klass, generic_context);
- if (!field)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
- type_to_eval_stack_type (field->type, stack_top (&ctx), FALSE);
- ip += 5;
- break;
case CEE_LDFLDA:
- check_underflow (&ctx, 1);
- if (stack_top (&ctx)->stype != TYPE_COMPLEX && stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldflda at 0x%04x", ip_offset));
- token = read32 (ip + 1);
- field = mono_field_from_token (image, token, &klass, generic_context);
- if (!field)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
- type_to_eval_stack_type (field->type, stack_top (&ctx), TRUE);
- ip += 5;
- break;
- case CEE_STFLD:
- check_underflow (&ctx, 2);
- ctx.eval.size -= 2;
- if (stack_top (&ctx)->stype != TYPE_COMPLEX && stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to stfld at 0x%04x", ip_offset));
- token = read32 (ip + 1);
- field = mono_field_from_token (image, token, &klass, generic_context);
- if (!field)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
- /* can_store */
+ do_push_field (&ctx, read32 (ip + 1), *ip == CEE_LDFLDA);
ip += 5;
break;
+
case CEE_LDSFLD:
- check_overflow (&ctx);
- token = read32 (ip + 1);
- field = mono_field_from_token (image, token, &klass, generic_context);
- if (!field)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
- type_to_eval_stack_type (field->type, stack_top (&ctx), FALSE);
- ++ctx.eval.size;
+ case CEE_LDSFLDA:
+ do_push_static_field (&ctx, read32 (ip + 1), *ip == CEE_LDSFLDA);
ip += 5;
break;
- case CEE_LDSFLDA:
- check_overflow (&ctx);
- token = read32 (ip + 1);
- field = mono_field_from_token (image, token, &klass, generic_context);
- if (!field)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
- type_to_eval_stack_type (field->type, stack_top (&ctx), TRUE);
- ++ctx.eval.size;
+
+ case CEE_STFLD:
+ do_store_field (&ctx, read32 (ip + 1));
ip += 5;
break;
+
case CEE_STSFLD:
- check_underflow (&ctx, 1);
- stack_pop (&ctx);
-// --cur_stack;
- token = read32 (ip + 1);
- field = mono_field_from_token (image, token, &klass, generic_context);
- if (!field)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Cannot load field from token 0x%08x at 0x%04x", token, ip_offset));
- /* can store */
+ do_store_static_field (&ctx, read32 (ip + 1));
ip += 5;
break;
case CEE_STOBJ:
- check_underflow (&ctx, 2);
+ if (!check_underflow (&ctx, 2))
+ break;
ctx.eval.size -= 2;
token = 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_I8_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:
- check_underflow (&ctx, 1);
- if (type_from_op (*ip, stack_top (&ctx)) == TYPE_INV)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
+ do_conversion (&ctx, TYPE_NATIVE_INT);
++ip;
break;
+
case CEE_BOX:
- check_underflow (&ctx, 1);
- token = read32 (ip + 1);
- if ( stack_top (&ctx)->stype == TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument %s to box at 0x%04x", type_names [stack_top (&ctx)->stype], ip_offset));
- stack_top (&ctx)->stype = TYPE_COMPLEX;
+ do_box_value (&ctx, read32 (ip + 1));
ip += 5;
break;
+
case CEE_NEWARR:
- check_underflow (&ctx, 1);
- token = read32 (ip + 1);
- stack_top (&ctx)->stype = TYPE_COMPLEX;
+ do_newarr (&ctx, read32 (ip + 1));
ip += 5;
break;
+
case CEE_LDLEN:
- check_underflow (&ctx, 1);
- if (stack_top (&ctx)->stype != TYPE_COMPLEX)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid argument to ldlen at 0x%04x", ip_offset));
- stack_top (&ctx)->type = &mono_defaults.int_class->byval_arg; /* FIXME: use a native int type */
- stack_top (&ctx)->stype = TYPE_PTR;
+ do_ldlen (&ctx);
++ip;
break;
+
case CEE_LDELEMA:
- check_underflow (&ctx, 2);
+ if (check_underflow (&ctx, 2))
+ break;
--ctx.eval.size;
if (stack_top (&ctx)->stype != TYPE_COMPLEX)
ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelema at 0x%04x", ip_offset));
case CEE_LDELEM_R4:
case CEE_LDELEM_R8:
case CEE_LDELEM_REF:
- check_underflow (&ctx, 2);
+ if (!check_underflow (&ctx, 2))
+ break;
--ctx.eval.size;
if (stack_top (&ctx)->stype != TYPE_COMPLEX)
ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid array argument to ldelem at 0x%04x", ip_offset));
case CEE_STELEM_R4:
case CEE_STELEM_R8:
case CEE_STELEM_REF:
- check_underflow (&ctx, 3);
+ if (!check_underflow (&ctx, 3))
+ break;
ctx.eval.size -= 3;
++ip;
break;
case CEE_UNUSED17:
++ip; /* warn, error ? */
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:
- check_underflow (&ctx, 1);
- if (type_from_op (*ip, stack_top (&ctx)) == TYPE_INV)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
+ 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_UNUSED50:
case CEE_UNUSED18:
case CEE_UNUSED19:
++ip; /* warn, error ? */
break;
case CEE_REFANYVAL:
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
++ip;
break;
case CEE_CKFINITE:
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
++ip;
break;
case CEE_UNUSED24:
++ip; /* warn, error ? */
break;
case CEE_MKREFANY:
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
token = read32 (ip + 1);
ip += 5;
break;
++ip; /* warn, error ? */
break;
case CEE_LDTOKEN:
- check_overflow (&ctx);
- token = read32 (ip + 1);
- ++ctx.eval.size;
+ do_load_token (&ctx, read32 (ip + 1));
ip += 5;
break;
- case CEE_CONV_U2:
- case CEE_CONV_U1:
- case CEE_CONV_I:
- case CEE_CONV_OVF_I:
- case CEE_CONV_OVF_U:
- check_underflow (&ctx, 1);
- if (type_from_op (*ip, stack_top (&ctx)) == TYPE_INV)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Invalid arguments to opcode 0x%02x at 0x%04x", *ip, ip_offset));
- ++ip;
- break;
+
case CEE_ADD_OVF:
case CEE_ADD_OVF_UN:
case CEE_MUL_OVF:
case CEE_MUL_OVF_UN:
case CEE_SUB_OVF:
case CEE_SUB_OVF_UN:
- check_underflow (&ctx, 2);
+ if (!check_underflow (&ctx, 2))
+ break;
stack_pop (&ctx);
++ip;
break;
ip += 2;
start = 1;
break;
- case CEE_STIND_I:
- check_underflow (&ctx, 2);
- ctx.eval.size -= 2;
- ++ip;
- break;
- case CEE_CONV_U:
- check_underflow (&ctx, 1);
- ++ip;
- break;
case CEE_UNUSED26:
case CEE_UNUSED27:
case CEE_UNUSED28:
do_cmp_op (&ctx, cmp_br_eq_op);
++ip;
break;
-
+
case CEE_CGT:
case CEE_CGT_UN:
case CEE_CLT:
do_cmp_op (&ctx, cmp_br_op);
++ip;
break;
-
+
case CEE_STARG:
store_arg (&ctx, read16 (ip + 1) );
- --ctx.eval.size;
ip += 3;
break;
-
case CEE_ARGLIST:
check_overflow (&ctx);
++ip;
case CEE_LDFTN:
- check_overflow (&ctx);
+ if (!check_overflow (&ctx))
+ break;
token = read32 (ip + 1);
ip += 5;
stack_top (&ctx)->stype = TYPE_PTR;
ctx.eval.size++;
break;
case CEE_LDVIRTFTN:
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
token = read32 (ip + 1);
ip += 5;
if (stack_top (&ctx)->stype != TYPE_COMPLEX)
case CEE_UNUSED56:
++ip;
break;
+
case CEE_LDARG:
case CEE_LDARGA:
- if (read16 (ip + 1) >= ctx.max_args)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", read16 (ip + 1), ip_offset));
- check_overflow (&ctx);
- ++ctx.eval.size;
+ push_arg (&ctx, read16 (ip + 1), *ip == CEE_LDARGA);
ip += 3;
break;
+
case CEE_LDLOC:
case CEE_LDLOCA:
- n = read16 (ip + 1);
- if (n >= ctx.header->num_locals)
- ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", n, ip_offset));
- /* no need to check if the var is initialized if the address is taken */
-// if (0 && *ip == CEE_LDLOC && !local_state [n])
-// ADD_VERIFY_ERROR (&ctx, g_strdup_printf ("Local var %d is initialized at 0x%04x", n, ip_offset));
- check_overflow (&ctx);
- type_to_eval_stack_type (ctx.header->locals [n], stack_get (&ctx, -1), *ip == CEE_LDLOCA);
- ++ctx.eval.size;
+ push_local (&ctx, read16 (ip + 1), *ip == CEE_LDLOCA);
ip += 3;
break;
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:
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
token = read32 (ip + 1);
ip += 5;
stack_pop (&ctx);
ip += 5;
break;
case CEE_CPBLK:
- check_underflow (&ctx, 3);
+ if (!check_underflow (&ctx, 3))
+ break;
ip++;
break;
case CEE_INITBLK:
- check_underflow (&ctx, 3);
+ if (!check_underflow (&ctx, 3))
+ break;
ip++;
break;
case CEE_NO_:
++ip;
break;
case CEE_SIZEOF:
- check_overflow (&ctx);
+ if (!check_overflow (&ctx))
+ break;
token = read32 (ip + 1);
ip += 5;
- stack_top (&ctx)->type = &mono_defaults.uint_class->byval_arg;
+ stack_top (&ctx)->type = &mono_defaults.uint32_class->byval_arg;
stack_top (&ctx)->stype = TYPE_I4;
ctx.eval.size++;
break;
case CEE_REFANYTYPE:
- check_underflow (&ctx, 1);
+ if (!check_underflow (&ctx, 1))
+ break;
++ip;
break;
case CEE_UNUSED53:
}
invalid_cil:
- if (!ctx.verifiable)
- ADD_VERIFY_INFO (&ctx, g_strdup_printf("Method code is not verifiable"), MONO_VERIFY_NOT_VERIFIABLE);
if (ctx.code) {
for (i = 0; i < ctx.header->code_size; ++i) {
}
}
- g_free (ctx.eval.stack);
- g_free (ctx.code);
+ if (ctx.eval.stack)
+ g_free (ctx.eval.stack);
+ if (ctx.code)
+ g_free (ctx.code);
if (ctx.signature->hasthis)
g_free (ctx.params);