+static ILStackDesc *
+stack_push (VerifyContext *ctx)
+{
+ return & ctx->eval.stack [ctx->eval.size++];
+}
+
+static ILStackDesc *
+stack_push_val (VerifyContext *ctx, int stype, MonoType *type)
+{
+ ILStackDesc *top = stack_push (ctx);
+ top->stype = stype;
+ top->type = type;
+ return top;
+}
+
+static ILStackDesc *
+stack_pop (VerifyContext *ctx)
+{
+ return ctx->eval.stack + --ctx->eval.size;
+}
+
+static inline ILStackDesc *
+stack_top (VerifyContext *ctx)
+{
+ return ctx->eval.stack + (ctx->eval.size - 1);
+}
+
+static inline ILStackDesc *
+stack_get (VerifyContext *ctx, int distance)
+{
+ 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 const unsigned char bin_op_table [TYPE_MAX][TYPE_MAX] = {
+ {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+};
+
+static const unsigned char add_table [TYPE_MAX][TYPE_MAX] = {
+ {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
+ {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
+ {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+};
+
+static const unsigned char sub_table [TYPE_MAX][TYPE_MAX] = {
+ {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_R8, TYPE_INV, TYPE_INV},
+ {TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_PTR | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_NATIVE_INT | NON_VERIFIABLE_RESULT, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+};
+
+static const unsigned char int_bin_op_table [TYPE_MAX][TYPE_MAX] = {
+ {TYPE_I4, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+};
+
+static const unsigned char shift_op_table [TYPE_MAX][TYPE_MAX] = {
+ {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_I8, TYPE_INV, TYPE_I8, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_NATIVE_INT, TYPE_INV, TYPE_NATIVE_INT, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+};
+
+static const unsigned char cmp_br_op [TYPE_MAX][TYPE_MAX] = {
+ {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+};
+
+static const unsigned char cmp_br_eq_op [TYPE_MAX][TYPE_MAX] = {
+ {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV},
+ {TYPE_I4, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4, TYPE_INV, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_I4 | NON_VERIFIABLE_RESULT, TYPE_INV, TYPE_I4, TYPE_INV},
+ {TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_INV, TYPE_I4},
+};
+
+/*debug helpers */
+static void
+dump_stack_value (ILStackDesc *value)
+{
+ printf ("[(%d)(%d)", value->type->type, value->stype);
+
+ if (value->stype & CMMP_MASK)
+ printf ("Controled Mutability MP: ");
+
+ if (IS_MANAGED_POINTER (value->stype))
+ printf ("Managed Pointer to: ");
+
+ switch (UNMASK_TYPE (value->stype)) {
+ case TYPE_INV:
+ printf ("invalid type]");
+ return;
+ case TYPE_I4:
+ printf ("int32]");
+ return;
+ case TYPE_I8:
+ printf ("int64]");
+ return;
+ case TYPE_NATIVE_INT:
+ printf ("native int]");
+ return;
+ case TYPE_R8:
+ printf ("float64]");
+ return;
+ case TYPE_PTR:
+ printf ("unmanaged pointer]");
+ return;
+ case TYPE_COMPLEX:
+ switch (value->type->type) {
+ case MONO_TYPE_CLASS:
+ case MONO_TYPE_VALUETYPE:
+ printf ("complex] (%s)", value->type->data.klass->name);
+ return;
+ case MONO_TYPE_STRING:
+ printf ("complex] (string)");
+ return;
+ case MONO_TYPE_OBJECT:
+ printf ("complex] (object)");
+ return;
+ case MONO_TYPE_SZARRAY:
+ printf ("complex] (%s [])", value->type->data.klass->name);
+ return;
+ case MONO_TYPE_ARRAY:
+ printf ("complex] (%s [%d %d %d])",
+ value->type->data.array->eklass->name,
+ value->type->data.array->rank,
+ value->type->data.array->numsizes,
+ value->type->data.array->numlobounds);
+ return;
+ case MONO_TYPE_GENERICINST:
+ printf ("complex] (inst of %s )", value->type->data.generic_class->container_class->name);
+ return;
+ case MONO_TYPE_VAR:
+ printf ("complex] (type generic param !%d - %s) ", value->type->data.generic_param->num, 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 stack %d type]\n", value->stype);
+ g_assert_not_reached ();
+ }
+}
+
+static void
+dump_stack_state (ILCodeDesc *state)
+{
+ int i;
+
+ printf ("(%d) ", state->size);
+ for (i = 0; i < state->size; ++i)
+ dump_stack_value (state->stack + i);
+ printf ("\n");
+}
+
+static void
+dump_context (VerifyContext *ctx, int code_size)
+{
+ int i;
+
+ for (i = 0; i < code_size; ++i) {
+ if (ctx->code [i].flags & IL_CODE_FLAG_SEEN) {
+ printf ("opcode [%d]:\n\t", i);
+ dump_stack_state (&ctx->code [i]);
+ }
+ }
+}
+
+/*Returns TRUE if candidate array type can be assigned to target.
+ *Both parameters MUST be of type MONO_TYPE_ARRAY (target->type == MONO_TYPE_ARRAY)
+ */
+static gboolean
+is_array_type_compatible (MonoType *target, MonoType *candidate)
+{
+ int i;
+ MonoArrayType *left = target->data.array;
+ MonoArrayType *right = candidate->data.array;
+
+ g_assert (target->type == MONO_TYPE_ARRAY);
+ g_assert (candidate->type == MONO_TYPE_ARRAY);
+
+
+ if ((left->rank != right->rank) ||
+ (left->numsizes != right->numsizes) ||
+ (left->numlobounds != right->numlobounds))
+ return FALSE;
+
+ for (i = 0; i < left->numsizes; ++i)
+ if (left->sizes [i] != right->sizes [i])
+ return FALSE;
+
+ for (i = 0; i < left->numlobounds; ++i)
+ if (left->lobounds [i] != right->lobounds [i])
+ return FALSE;
+
+ return mono_class_is_assignable_from (left->eklass, right->eklass);
+}
+
+static int
+get_stack_type (MonoType *type)
+{
+ int mask = 0;
+ int type_kind = type->type;
+ if (type->byref)
+ mask = POINTER_MASK;
+ /*TODO handle CMMP_MASK */
+
+handle_enum:
+ switch (type_kind) {
+ case MONO_TYPE_I1:
+ case MONO_TYPE_U1:
+ case MONO_TYPE_BOOLEAN:
+ case MONO_TYPE_I2:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_CHAR:
+ case MONO_TYPE_I4:
+ case MONO_TYPE_U4:
+ return TYPE_I4 | mask;
+
+ case MONO_TYPE_I:
+ case MONO_TYPE_U:
+ return TYPE_NATIVE_INT | mask;
+
+ /* FIXME: the spec says that you cannot have a pointer to method pointer, do we need to check this here? */
+ case MONO_TYPE_FNPTR:
+ case MONO_TYPE_PTR:
+ case MONO_TYPE_TYPEDBYREF:
+ return TYPE_PTR | mask;
+
+ case MONO_TYPE_CLASS:
+ case MONO_TYPE_STRING:
+ case MONO_TYPE_OBJECT:
+ case MONO_TYPE_SZARRAY:
+ case MONO_TYPE_ARRAY:
+ case MONO_TYPE_GENERICINST:
+ return TYPE_COMPLEX | mask;
+
+ case MONO_TYPE_I8:
+ case MONO_TYPE_U8:
+ return TYPE_I8 | mask;
+
+ case MONO_TYPE_R4:
+ case MONO_TYPE_R8:
+ return TYPE_R8 | mask;
+
+ case MONO_TYPE_VALUETYPE:
+ if (type->data.klass->enumtype) {
+ type = type->data.klass->enum_basetype;
+ type_kind = type->type;
+ goto handle_enum;
+ } else
+ return TYPE_COMPLEX | mask;
+
+ default:
+ VERIFIER_DEBUG ( printf ("unknown type %02x in eval stack type\n", type->type); );
+ g_assert_not_reached ();
+ return 0;
+ }
+}
+
+/* convert MonoType to ILStackDesc format (stype) */
+static void
+set_stack_value (ILStackDesc *stack, MonoType *type, int take_addr)
+{
+ int mask = 0;
+ int type_kind = type->type;
+
+ if (type->byref || take_addr)
+ mask = POINTER_MASK;
+ /* TODO handle CMMP_MASK */
+
+handle_enum:
+ stack->type = type;
+
+ switch (type_kind) {
+ case MONO_TYPE_I1:
+ case MONO_TYPE_U1:
+ case MONO_TYPE_BOOLEAN:
+ case MONO_TYPE_I2:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_CHAR:
+ case MONO_TYPE_I4:
+ case MONO_TYPE_U4:
+ stack->stype = TYPE_I4 | mask;
+ return;
+ case MONO_TYPE_I:
+ 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:
+ 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_GENERICINST:
+ case MONO_TYPE_VAR:
+ case MONO_TYPE_MVAR:
+ stack->stype = TYPE_COMPLEX | mask;
+ return;
+ case MONO_TYPE_I8:
+ case MONO_TYPE_U8:
+ stack->stype = TYPE_I8 | mask;
+ return;
+ case MONO_TYPE_R4:
+ case MONO_TYPE_R8:
+ stack->stype = TYPE_R8 | mask;
+ return;
+ case MONO_TYPE_VALUETYPE:
+ if (type->data.klass->enumtype) {
+ type = type->data.klass->enum_basetype;
+ type_kind = type->type;
+ goto handle_enum;
+ } else {
+ stack->stype = TYPE_COMPLEX | mask;
+ return;
+ }
+ default:
+ 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
+mono_is_generic_type_compatible (MonoType *target, MonoType *candidate)
+{
+ if (target->byref != candidate->byref)
+ return FALSE;
+
+handle_enum:
+ switch (target->type) {
+ case MONO_TYPE_STRING:
+ if (candidate->type == MONO_TYPE_STRING)
+ return TRUE;
+ return FALSE;
+
+ case MONO_TYPE_CLASS:
+ if (candidate->type != MONO_TYPE_CLASS)
+ return FALSE;
+
+ 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:
+ 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;
+ goto handle_enum;
+ } else {
+ if (candidate->type != MONO_TYPE_VALUETYPE)
+ return FALSE;
+ return candidate->data.klass == target->data.klass;
+ }
+
+ case MONO_TYPE_ARRAY:
+ if (candidate->type != MONO_TYPE_ARRAY)
+ return FALSE;
+ return is_array_type_compatible (target, candidate);
+
+ default:
+ VERIFIER_DEBUG ( printf ("unknown target type %d\n", target->type); );
+ g_assert_not_reached ();
+ }
+
+ return FALSE;
+}
+
+
+static gboolean
+mono_is_generic_instance_compatible (MonoGenericClass *target, MonoGenericClass *candidate, MonoGenericClass *root_candidate) {
+ MonoGenericContainer *container;
+ int i;
+
+ 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;
+ 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)) {
+ VERIFIER_DEBUG ( printf ("generic type is an interface\n"); );
+
+ do {
+ int iface_count = cand_class->interface_count;
+ MonoClass **ifaces = cand_class->interfaces;
+ int i;
+ VERIFIER_DEBUG ( printf ("type has %d interfaces\n", iface_count); );
+ for (i = 0; i< iface_count; ++i) {
+ MonoClass *ifc = ifaces[i];
+ VERIFIER_DEBUG ( printf ("analysing %s\n", ifc->name); );
+ if (ifc->generic_class) {
+ VERIFIER_DEBUG ( printf ("interface has generic info\n"); );
+ }
+ if (mono_is_generic_instance_compatible (target, ifc->generic_class, root_candidate)) {
+ VERIFIER_DEBUG ( printf ("we got compatible stuff!\n"); );
+ return TRUE;
+ }
+ }
+
+ cand_class = cand_class->parent;
+ } while (cand_class);
+
+ VERIFIER_DEBUG ( printf ("don't implements an interface\n"); );
+
+ } else {
+ VERIFIER_DEBUG ( printf ("verifying upper classes\n"); );
+
+ cand_class = cand_class->parent;
+
+ while (cand_class) {
+ VERIFIER_DEBUG ( printf ("verifying parent class name %s\n", cand_class->name); );
+ if (cand_class->generic_class) {
+ VERIFIER_DEBUG ( printf ("super type has generic context\n"); );
+
+ /* TODO break loop if target->container_class == cand_class->generic_class->container_class */
+ return mono_is_generic_instance_compatible (target, cand_class->generic_class, root_candidate);
+ } else
+ 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->context.class_inst == candidate->context.class_inst) {
+ VERIFIER_DEBUG ( printf ("generic types are compatible, both have the same instantiation\n"); );
+ return TRUE;
+ }
+
+ 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->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->context.class_inst->type_argv [var_param->num];
+ }
+
+ if ((param->flags & GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK) == 0) {
+ 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 {
+ 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) {
+ 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;
+}
+
+
+
+/*Verify if type 'candidate' can be stored in type 'target'.
+ *
+ * If strict, check for the underlying type and not the verification stack types
+ */
+static gboolean
+verify_type_compatibility_full (VerifyContext *ctx, MonoType *target, MonoType *candidate, gboolean strict)
+{
+#define IS_ONE_OF3(T, A, B, C) (T == A || T == B || T == C)
+#define IS_ONE_OF2(T, A, B) (T == A || T == B)
+
+ 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: {
+ gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
+ gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
+ if (strict)
+ return is_native_int || is_int4;
+ return is_native_int || get_stack_type (candidate) == TYPE_I4;
+ }
+
+ case MONO_TYPE_I8:
+ case MONO_TYPE_U8:
+ return IS_ONE_OF2 (candidate->type, MONO_TYPE_I8, MONO_TYPE_U8);
+
+ case MONO_TYPE_R4:
+ case MONO_TYPE_R8:
+ if (strict)
+ return candidate->type == target->type;
+ return IS_ONE_OF2 (candidate->type, MONO_TYPE_R4, MONO_TYPE_R8);
+
+ case MONO_TYPE_I:
+ case MONO_TYPE_U: {
+ gboolean is_native_int = IS_ONE_OF2 (candidate->type, MONO_TYPE_I, MONO_TYPE_U);
+ gboolean is_int4 = IS_ONE_OF2 (candidate->type, MONO_TYPE_I4, MONO_TYPE_U4);
+ if (strict)
+ return is_native_int || is_int4;
+ return is_native_int || get_stack_type (candidate) == TYPE_I4;
+ }
+
+ case MONO_TYPE_PTR:
+ if (candidate->type != MONO_TYPE_PTR)
+ return FALSE;
+ /* check the underlying type */
+ return verify_type_compatibility_full (ctx, target->data.type, candidate->data.type, TRUE);
+
+ case MONO_TYPE_FNPTR: {
+ MonoMethodSignature *left, *right;
+ if (candidate->type != MONO_TYPE_FNPTR)
+ return FALSE;
+
+ left = mono_type_get_signature (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 (candidate->type != MONO_TYPE_GENERICINST)
+ return FALSE;
+ left = target->data.generic_class;
+ right = candidate->data.generic_class;
+
+ return mono_is_generic_instance_compatible (left, right, right);
+ }
+
+ case MONO_TYPE_STRING:
+ return candidate->type == MONO_TYPE_STRING;
+
+ case MONO_TYPE_CLASS:
+ 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 (candidate->type != MONO_TYPE_SZARRAY)
+ return FALSE;
+
+ left = target->data.klass;
+ right = candidate->data.klass;
+ return mono_class_is_assignable_from(left, right);
+ }
+
+ case MONO_TYPE_ARRAY:
+ if (candidate->type != MONO_TYPE_ARRAY)
+ return FALSE;
+ return is_array_type_compatible (target, candidate);
+
+ //TODO verify aditional checks that needs to be done
+ case MONO_TYPE_TYPEDBYREF:
+ return candidate->type == MONO_TYPE_TYPEDBYREF;
+
+ case MONO_TYPE_VALUETYPE:
+ if (target->data.klass->enumtype) {
+ 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:
+ VERIFIER_DEBUG ( printf ("unknown store type %d\n", target->type); );
+ g_assert_not_reached ();
+ return FALSE;
+ }
+ return 1;
+#undef IS_ONE_OF3
+#undef IS_ONE_OF2
+}
+
+static gboolean
+verify_type_compatibility (VerifyContext *ctx, MonoType *target, MonoType *candidate)
+{
+ return verify_type_compatibility_full (ctx, target, candidate, FALSE);
+}
+
+
+static int
+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);
+ }
+ } 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, int take_addr)
+{
+ if (arg >= ctx->num_locals) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local %d", arg + 1));
+ } else if (check_overflow (ctx)) {
+ /*We must let the value be pushed, otherwise we would get an underflow error*/
+ check_unverifiable_type (ctx, ctx->locals [arg]);
+ if (ctx->locals [arg]->byref && take_addr)
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("ByRef of ByRef at 0x%04x", ctx->ip_offset));
+
+ set_stack_value (stack_push (ctx), ctx->locals [arg], take_addr);
+ }
+}
+
+static void
+store_arg (VerifyContext *ctx, guint32 arg)
+{
+ ILStackDesc *value;
+
+ if (arg >= ctx->max_args) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Method doesn't have argument %d at 0x%04x", arg + 1, ctx->ip_offset));
+ check_underflow (ctx, 1);
+ stack_pop (ctx);
+ return;
+ }
+
+ if (check_underflow (ctx, 1)) {
+ value = stack_pop (ctx);
+ if (!verify_stack_type_compatibility (ctx, ctx->params [arg], value)) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf ("Incompatible type %s in argument store at 0x%04x", type_names [UNMASK_TYPE (value->stype)], ctx->ip_offset));
+ }
+ }
+}
+
+static void
+store_local (VerifyContext *ctx, guint32 arg)
+{
+ ILStackDesc *value;
+ if (arg >= ctx->num_locals) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Method doesn't have local var %d at 0x%04x", arg + 1, ctx->ip_offset));
+ return;
+ }
+
+ /*TODO verify definite assigment */
+ if (check_underflow (ctx, 1)) {
+ value = stack_pop(ctx);
+ 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));
+ }
+ }
+}
+
+static void
+do_binop (VerifyContext *ctx, unsigned int opcode, const unsigned char table [TYPE_MAX][TYPE_MAX])
+{
+ 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 (IS_MANAGED_POINTER (idxa)) {
+ idxa = TYPE_PTR;
+ complexMerge = 1;
+ }
+
+ idxb = b->stype;
+ if (IS_MANAGED_POINTER (idxb)) {
+ idxb = TYPE_PTR;
+ complexMerge = 2;
+ }
+
+ --idxa;
+ --idxb;
+ res = table [idxa][idxb];
+
+ VERIFIER_DEBUG ( printf ("binop res %d\n", res); );
+ VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
+
+ ctx->eval.size--;
+ if (res == TYPE_INV) {
+ 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) {
+ 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;
+ }
+
+ if (complexMerge && res == TYPE_PTR) {
+ if (complexMerge == 1)
+ copy_stack_value (stack_top (ctx), a);
+ else if (complexMerge == 2)
+ copy_stack_value (stack_top (ctx), b);
+ /*
+ * There is no need to merge the type of two pointers.
+ * The only valid operation is subtraction, that returns a native
+ * int as result and can be used with any 2 pointer kinds.
+ * This is valid acording to Patition III 1.1.4
+ */
+ } else
+ stack_top (ctx)->stype = res;
+
+}
+
+
+static void
+do_boolean_branch_op (VerifyContext *ctx, int delta)
+{
+ int target = ctx->ip_offset + delta;
+ ILStackDesc *top;
+
+ VERIFIER_DEBUG ( printf ("boolean branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
+
+ if (target < 0 || target >= ctx->code_size) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Boolean branch target out of code at 0x%04x", ctx->ip_offset));
+ return;
+ }
+
+ 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])
+{
+ ILStackDesc *a, *b;
+ int idxa, idxb;
+ unsigned char res;
+ int target = ctx->ip_offset + delta;
+
+ VERIFIER_DEBUG ( printf ("branch offset %d delta %d target %d\n", ctx->ip_offset, delta, target); );
+
+ if (target < 0 || target >= ctx->code_size) {
+ ADD_VERIFY_ERROR (ctx, g_strdup_printf ("Branch target out of code at 0x%04x", ctx->ip_offset));
+ return;
+ }
+
+ 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 (IS_MANAGED_POINTER (idxa))
+ idxa = TYPE_PTR;
+
+ idxb = b->stype;
+ if (IS_MANAGED_POINTER (idxb))
+ idxb = TYPE_PTR;
+
+ --idxa;
+ --idxb;
+ res = table [idxa][idxb];
+
+ VERIFIER_DEBUG ( printf ("branch res %d\n", res); );
+ VERIFIER_DEBUG ( printf ("idxa %d idxb %d\n", idxa, idxb); );
+
+ if (res == TYPE_INV) {
+ ADD_VERIFY_ERROR (ctx,
+ g_strdup_printf ("Compare and Branch 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) {
+ 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;
+ }
+}
+
+static void
+do_cmp_op (VerifyContext *ctx, const unsigned char table [TYPE_MAX][TYPE_MAX])
+{
+ 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 (IS_MANAGED_POINTER (idxa))
+ idxa = TYPE_PTR;
+
+ idxb = b->stype;
+ if (IS_MANAGED_POINTER (idxb))
+ idxb = TYPE_PTR;
+
+ --idxa;
+ --idxb;
+ res = table [idxa][idxb];
+
+ if(res == TYPE_INV) {
+ CODE_NOT_VERIFIABLE (ctx, g_strdup_printf("Compare instruction applyed to ill formed stack (%s x %s) at 0x%04x", type_names [UNMASK_TYPE (idxa)], type_names [UNMASK_TYPE (idxb)], ctx->ip_offset));
+ } else if (res & NON_VERIFIABLE_RESULT) {
+ 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.int32_class->byval_arg);
+}
+
+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 (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*/
+static void
+merge_stacks (VerifyContext *ctx, ILCodeDesc *from, ILCodeDesc *to, int start)
+{
+ int i;
+
+ if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
+ stack_init (ctx, to);
+
+ if (start) {
+ if (to->flags == IL_CODE_FLAG_NOT_PROCESSED)
+ from->size = 0;
+ else
+ stack_copy (&ctx->eval, to);
+ goto end_verify;
+ } else if (to->flags == IL_CODE_FLAG_NOT_PROCESSED) {
+ stack_copy (to, from);
+ goto end_verify;
+ }
+ VERIFIER_DEBUG ( printf ("performing stack merge %d x %d\n", from->size, to->size); );
+
+ if (from->size != to->size) {
+ VERIFIER_DEBUG ( printf ("different stack sizes %d x %d\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;
+
+ 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;
+ }
+
+ /*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;
+}
+
+
+/*
+ * FIXME: need to distinguish between valid and verifiable.
+ * Need to keep track of types on the stack.
+ * Verify types for opcodes.
+ */
+GSList*
+mono_method_verify (MonoMethod *method, int level)
+{
+ 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;
+ 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;
+ }
+
+ 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;
+ 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 = 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);
+
+
+ ctx.num_locals = ctx.header->num_locals;
+ ctx.locals = ctx.header->locals;
+
+
+ if (ctx.signature->hasthis) {
+ ctx.params = g_new0 (MonoType*, ctx.max_args);
+ 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)
+ ctx.generic_context = generic_context = mono_method_get_context (method);
+
+ 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) {
+ 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;
+ }
+ 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) {
+ 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;
+
+#ifdef MONO_VERIFIER_DEBUG
+ {
+ char *discode;
+ discode = mono_disasm_code_one (NULL, method, ip, NULL);
+ discode [strlen (discode) - 1] = 0; /* no \n */
+ g_print ("[%d] %-29s (%d)\n", ip_offset, discode, ctx.eval.size);
+ g_free (discode);
+ }
+ dump_stack_state (&ctx.code [ip_offset]);
+#endif
+
+ switch (*ip) {
+ case CEE_NOP:
+ case CEE_BREAK:
+ ++ip;