OSDN Git Service

2003-05-27 Michael Koch <konqueror@gmx.de>
[pf3gnuchains/gcc-fork.git] / libjava / verify.cc
index 8d235e7..4a6ca45 100644 (file)
@@ -1,6 +1,6 @@
 // defineclass.cc - defining a class from .class format.
 
-/* Copyright (C) 2001, 2002  Free Software Foundation
+/* Copyright (C) 2001, 2002, 2003  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -90,12 +90,6 @@ private:
   // be many `ret' instructions, so a linked list is ok.
   subr_entry_info *entry_points;
 
-  // The current top of the stack, in terms of slots.
-  int stacktop;
-  // The current depth of the stack.  This will be larger than
-  // STACKTOP when wide types are on the stack.
-  int stackdepth;
-
   // The bytecode itself.
   unsigned char *bytecode;
   // The exceptions.
@@ -134,6 +128,34 @@ private:
     return r;
   }
 
+  __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
+  {
+    using namespace java::lang;
+    StringBuffer *buf = new StringBuffer ();
+
+    buf->append (JvNewStringLatin1 ("verification failed"));
+    if (pc == -1)
+      pc = start_PC;
+    if (pc != -1)
+      {
+       buf->append (JvNewStringLatin1 (" at PC "));
+       buf->append (pc);
+      }
+
+    _Jv_InterpMethod *method = current_method;
+    buf->append (JvNewStringLatin1 (" in "));
+    buf->append (current_class->getName());
+    buf->append ((jchar) ':');
+    buf->append (JvNewStringUTF (method->get_method()->name->data));
+    buf->append ((jchar) '(');
+    buf->append (JvNewStringUTF (method->get_method()->signature->data));
+    buf->append ((jchar) ')');
+
+    buf->append (JvNewStringLatin1 (": "));
+    buf->append (JvNewStringLatin1 (s));
+    throw new java::lang::VerifyError (buf->toString ());
+  }
+
   // This enum holds a list of tags for all the different types we
   // need to handle.  Reference types are treated specially by the
   // type class.
@@ -231,9 +253,6 @@ private:
        if (target->isPrimitive () || source->isPrimitive ())
          return false;
 
-       // Check array case first because we can have an array whose
-       // component type is not prepared; _Jv_IsAssignableFrom
-       // doesn't handle this correctly.
        if (target->isArray ())
          {
            if (! source->isArray ())
@@ -241,11 +260,6 @@ private:
            target = target->getComponentType ();
            source = source->getComponentType ();
          }
-       // _Jv_IsAssignableFrom can handle a target which is an
-       // interface even if it hasn't been prepared.
-       else if ((target->state > JV_STATE_LINKED || target->isInterface ())
-                && source->state > JV_STATE_LINKED)
-         return _Jv_IsAssignableFrom (target, source);
        else if (target->isInterface ())
          {
            for (int i = 0; i < source->interface_count; ++i)
@@ -259,10 +273,25 @@ private:
            if (source == NULL)
              return false;
          }
+       // We must do this check before we check to see if SOURCE is
+       // an interface.  This way we know that any interface is
+       // assignable to an Object.
        else if (target == &java::lang::Object::class$)
          return true;
-       else if (source->isInterface ()
-                || source == &java::lang::Object::class$)
+       else if (source->isInterface ())
+         {
+           for (int i = 0; i < target->interface_count; ++i)
+             {
+               // We use a recursive call because we also need to
+               // check superinterfaces.
+               if (is_assignable_from_slow (target->interfaces[i], source))
+                 return true;
+             }
+           target = target->getSuperclass ();
+           if (target == NULL)
+             return false;
+         }
+       else if (source == &java::lang::Object::class$)
          return false;
        else
          source = source->getSuperclass ();
@@ -404,7 +433,7 @@ private:
 
       using namespace java::lang;
       java::lang::ClassLoader *loader
-       = verifier->current_class->getClassLoader();
+       = verifier->current_class->getClassLoaderInternal();
       // We might see either kind of name.  Sigh.
       if (data.name->data[0] == 'L'
          && data.name->data[data.name->length - 1] == ';')
@@ -457,8 +486,8 @@ private:
       if (key < reference_type || k.key < reference_type)
        return key == k.key;
 
-      // The `null' type is convertible to any reference type.
-      // FIXME: is this correct for THIS?
+      // The `null' type is convertible to any initialized reference
+      // type.
       if (key == null_type || k.key == null_type)
        return true;
 
@@ -570,7 +599,7 @@ private:
 
       if (key == reference_type)
        return type (_Jv_GetArrayClass (data.klass,
-                                       data.klass->getClassLoader ()));
+                                       data.klass->getClassLoaderInternal()));
       else
        verifier->verify_fail ("internal error in type::to_array()");
     }
@@ -675,21 +704,26 @@ private:
                      oldk = oldk->getComponentType ();
                    }
 
-                 // This loop will end when we hit Object.
-                 while (true)
+                 // Ordinarily this terminates when we hit Object...
+                 while (k != NULL)
                    {
                      if (is_assignable_from_slow (k, oldk))
                        break;
                      k = k->getSuperclass ();
                      changed = true;
                    }
+                 // ... but K could have been an interface, in which
+                 // case we'll end up here.  We just convert this
+                 // into Object.
+                 if (k == NULL)
+                   k = &java::lang::Object::class$;
 
                  if (changed)
                    {
                      while (arraycount > 0)
                        {
                          java::lang::ClassLoader *loader
-                           = verifier->current_class->getClassLoader();
+                           = verifier->current_class->getClassLoaderInternal();
                          k = _Jv_GetArrayClass (k, loader);
                          --arraycount;
                        }
@@ -761,10 +795,10 @@ private:
   // location.
   struct state
   {
-    // Current top of stack.
+    // The current top of the stack, in terms of slots.
     int stacktop;
-    // Current stack depth.  This is like the top of stack but it
-    // includes wide variable information.
+    // The current depth of the stack.  This will be larger than
+    // STACKTOP when wide types are on the stack.
     int stackdepth;
     // The stack.
     type *stack;
@@ -787,6 +821,12 @@ private:
     // assigns to locals[0] (overwriting `this') and then returns
     // without really initializing.
     type this_type;
+    // This is a list of all subroutines that have been seen at this
+    // point.  Ordinarily this is NULL; it is only allocated and used
+    // in relatively weird situations involving non-ret exit from a
+    // subroutine.  We have to keep track of this in this way to avoid
+    // endless recursion in these cases.
+    subr_info *seen_subrs;
 
     // INVALID marks a state which is not on the linked list of states
     // requiring reverification.
@@ -794,12 +834,18 @@ private:
     // NO_NEXT marks the state at the end of the reverification list.
     static const int NO_NEXT = -2;
 
+    // This is used to mark the stack depth at the instruction just
+    // after a `jsr' when we haven't yet processed the corresponding
+    // `ret'.  See handle_jsr_insn for more information.
+    static const int NO_STACK = -1;
+
     state ()
       : this_type ()
     {
       stack = NULL;
       locals = NULL;
       local_changed = NULL;
+      seen_subrs = NULL;
     }
 
     state (int max_stack, int max_locals)
@@ -812,6 +858,7 @@ private:
        stack[i] = unsuitable_type;
       locals = new type[max_locals];
       local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
+      seen_subrs = NULL;
       for (int i = 0; i < max_locals; ++i)
        {
          locals[i] = unsuitable_type;
@@ -827,6 +874,7 @@ private:
       stack = new type[max_stack];
       locals = new type[max_locals];
       local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
+      seen_subrs = NULL;
       copy (orig, max_stack, max_locals, ret_semantics);
       next = INVALID;
     }
@@ -839,6 +887,7 @@ private:
        delete[] locals;
       if (local_changed)
        _Jv_Free (local_changed);
+      clean_subrs ();
     }
 
     void *operator new[] (size_t bytes)
@@ -861,6 +910,17 @@ private:
       _Jv_Free (mem);
     }
 
+    void clean_subrs ()
+    {
+      subr_info *info = seen_subrs;
+      while (info != NULL)
+       {
+         subr_info *next = info->next;
+         _Jv_Free (info);
+         info = next;
+       }
+    }
+
     void copy (const state *copy, int max_stack, int max_locals,
               bool ret_semantics = false)
     {
@@ -880,6 +940,16 @@ private:
            locals[i] = copy->locals[i];
          local_changed[i] = copy->local_changed[i];
        }
+
+      clean_subrs ();
+      if (copy->seen_subrs)
+       {
+         for (subr_info *info = seen_subrs; info != NULL; info = info->next)
+           add_subr (info->pc);
+       }
+      else
+       seen_subrs = NULL;
+
       this_type = copy->this_type;
       // Don't modify `next'.
     }
@@ -892,8 +962,6 @@ private:
       stack[0] = t;
       for (int i = stacktop; i < max_stack; ++i)
        stack[i] = unsuitable_type;
-
-      // FIXME: subroutine handling?
     }
 
     // Modify this state to reflect entry into a subroutine.
@@ -908,6 +976,15 @@ private:
        local_changed[i] = false;
     }
 
+    // Indicate that we've been in this this subroutine.
+    void add_subr (int pc)
+    {
+      subr_info *n = (subr_info *) _Jv_Malloc (sizeof (subr_info));
+      n->pc = pc;
+      n->next = seen_subrs;
+      seen_subrs = n;
+    }
+
     // Merge STATE_OLD into this state.  Destructively modifies this
     // state.  Returns true if the new state was in fact changed.
     // Will throw an exception if the states are not mergeable.
@@ -935,19 +1012,48 @@ private:
        }
       else
        {
-         // If the subroutines differ, indicate that the state
-         // changed.  This is needed to detect when subroutines have
-         // merged.
-         changed = true;
+         // If the subroutines differ, and we haven't seen this
+         // subroutine before, indicate that the state changed.  This
+         // is needed to detect when subroutines have merged.
+         bool found = false;
+         for (subr_info *info = seen_subrs; info != NULL; info = info->next)
+           {
+             if (info->pc == state_old->subroutine)
+               {
+                 found = true;
+                 break;
+               }
+           }
+         if (! found)
+           {
+             add_subr (state_old->subroutine);
+             changed = true;
+           }
        }
 
-      // Merge stacks.
-      if (state_old->stacktop != stacktop)
+      // Merge stacks.  Special handling for NO_STACK case.
+      if (state_old->stacktop == NO_STACK)
+       {
+         // Nothing to do in this case; we don't care about modifying
+         // the old state.
+       }
+      else if (stacktop == NO_STACK)
+       {
+         stacktop = state_old->stacktop;
+         stackdepth = state_old->stackdepth;
+         for (int i = 0; i < stacktop; ++i)
+           stack[i] = state_old->stack[i];
+         changed = true;
+       }
+      else if (state_old->stacktop != stacktop)
        verifier->verify_fail ("stack sizes differ");
-      for (int i = 0; i < state_old->stacktop; ++i)
+      else
        {
-         if (stack[i].merge (state_old->stack[i], false, verifier))
-           changed = true;
+         for (int i = 0; i < state_old->stacktop; ++i)
+           {
+             if (stack[i].merge (state_old->stack[i], false, verifier))
+               changed = true;
+           }
        }
 
       // Merge local variables.
@@ -962,8 +1068,14 @@ private:
            {
              if (locals[i].merge (state_old->locals[i], true, verifier))
                {
+                 // Note that we don't call `note_variable' here.
+                 // This change doesn't represent a real change to a
+                 // local, but rather a merge artifact.  If we're in
+                 // a subroutine which is called with two
+                 // incompatible types in a slot that is unused by
+                 // the subroutine, then we don't want to mark that
+                 // variable as having been modified.
                  changed = true;
-                 note_variable (i);
                }
            }
 
@@ -1032,6 +1144,8 @@ private:
     // Return true if this state is the unmerged result of a `ret'.
     bool is_unmerged_ret_state (int max_locals) const
     {
+      if (stacktop == NO_STACK)
+       return true;
       for (int i = 0; i < max_locals; ++i)
        {
          if (locals[i].key == unused_by_subroutine_type)
@@ -1052,7 +1166,10 @@ private:
        debug_print (".");
       debug_print ("    [local] ");
       for (i = 0; i < max_locals; ++i)
-       locals[i].print ();
+       {
+         locals[i].print ();
+         debug_print (local_changed[i] ? "+" : " ");
+       }
       if (subroutine == 0)
        debug_print ("   | None");
       else
@@ -1085,14 +1202,6 @@ private:
     return r;
   }
 
-  type pop64 ()
-  {
-    type r = pop_raw ();
-    if (! r.iswide ())
-      verify_fail ("wide pop of narrow type");
-    return r;
-  }
-
   type pop_type (type match)
   {
     match.promote ();
@@ -1102,6 +1211,19 @@ private:
     return t;
   }
 
+  // Pop a reference which is guaranteed to be initialized.  MATCH
+  // doesn't have to be a reference type; in this case this acts like
+  // pop_type.
+  type pop_init_ref (type match)
+  {
+    type t = pop_raw ();
+    if (t.isreference () && ! t.isinitialized ())
+      verify_fail ("initialized reference required");
+    else if (! match.compatible (t, this))
+      verify_fail ("incompatible type on stack");
+    return t;
+  }
+
   // Pop a reference type or a return address.
   type pop_ref_or_return ()
   {
@@ -1166,9 +1288,11 @@ private:
   // compatible with type ELEMENT.  Returns the actual element type.
   type require_array_type (type array, type element)
   {
-    // An odd case.  Here we just pretend that everything went ok.
+    // An odd case.  Here we just pretend that everything went ok.  If
+    // the requested element type is some kind of reference, return
+    // the null type instead.
     if (array.isnull ())
-      return element;
+      return element.isreference () ? type (null_type) : element;
 
     if (! array.isarray ())
       verify_fail ("array required");
@@ -1300,7 +1424,6 @@ private:
   {
     int *prev_loc = &next_verify_pc;
     int npc = next_verify_pc;
-    bool skipped = false;
 
     while (npc != state::NO_NEXT)
       {
@@ -1317,15 +1440,13 @@ private:
            return npc;
          }
 
-       skipped = true;
        prev_loc = &states[npc]->next;
        npc = states[npc]->next;
       }
 
-    // If we've skipped states and there is nothing else, that's a
-    // bug.
-    if (skipped)
-      verify_fail ("pop_jump: can't happen");
+    // Note that we might have gotten here even when there are
+    // remaining states to process.  That can happen if we find a
+    // `jsr' without a `ret'.
     return state::NO_NEXT;
   }
 
@@ -1397,6 +1518,12 @@ private:
 
     for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
       {
+       // We might be returning to a `jsr' that is at the end of the
+       // bytecode.  This is ok if we never return from the called
+       // subroutine, but if we see this here it is an error.
+       if (subr->pc >= current_method->code_length)
+         verify_fail ("fell off end");
+
        // Temporarily modify the current state so it looks like we're
        // in the enclosing context.
        current_state->subroutine = get_subroutine (subr->pc);
@@ -1429,12 +1556,10 @@ private:
       current_state->check_no_uninitialized_objects (current_method->max_locals, this);
     check_nonrecursive_call (current_state->subroutine, npc);
 
-    // Create a new state and modify it as appropriate for entry into
-    // a subroutine.  We're writing this in a weird way because,
-    // unfortunately, push_type only works on the current state.
+    // Modify our state as appropriate for entry into a subroutine.
     push_type (return_address_type);
     push_jump_merge (npc, current_state);
-    // Clean up the weirdness.
+    // Clean up.
     pop_type (return_address_type);
 
     // On entry to the subroutine, the subroutine number must be set
@@ -1442,6 +1567,22 @@ private:
     // merging state so that we don't erroneously "notice" a variable
     // change merely on entry.
     states[npc]->enter_subroutine (npc, current_method->max_locals);
+
+    // Indicate that we don't know the stack depth of the instruction
+    // following the `jsr'.  The idea here is that we need to merge
+    // the local variable state across the jsr, but the subroutine
+    // might change the stack depth, so we can't make any assumptions
+    // about it.  So we have yet another special case.  We know that
+    // at this point PC points to the instruction after the jsr.  Note
+    // that it is ok to have a `jsr' at the end of the bytecode,
+    // provided that the called subroutine never returns.  So, we have
+    // a special case here and another one when we handle the ret.
+    if (PC < current_method->code_length)
+      {
+       current_state->stacktop = state::NO_STACK;
+       push_jump_merge (PC, current_state);
+      }
+    invalidate_pc ();
   }
 
   jclass construct_primitive_array_type (type_val prim)
@@ -1473,6 +1614,19 @@ private:
       case long_type:
        k = JvPrimClass (long);
        break;
+
+      // These aren't used here but we call them out to avoid
+      // warnings.
+      case void_type:
+      case unsuitable_type:
+      case return_address_type:
+      case continuation_type:
+      case unused_by_subroutine_type:
+      case reference_type:
+      case null_type:
+      case unresolved_reference_type:
+      case uninitialized_reference_type:
+      case uninitialized_unresolved_reference_type:
       default:
        verify_fail ("unknown type in construct_primitive_array_type");
       }
@@ -1776,6 +1930,30 @@ private:
            note_branch_target (compute_jump (get_int ()), last_was_jsr);
            break;
 
+         // These are unused here, but we call them out explicitly
+         // so that -Wswitch-enum doesn't complain.
+         case op_putfield_1:
+         case op_putfield_2:
+         case op_putfield_4:
+         case op_putfield_8:
+         case op_putfield_a:
+         case op_putstatic_1:
+         case op_putstatic_2:
+         case op_putstatic_4:
+         case op_putstatic_8:
+         case op_putstatic_a:
+         case op_getfield_1:
+         case op_getfield_2s:
+         case op_getfield_2u:
+         case op_getfield_4:
+         case op_getfield_8:
+         case op_getfield_a:
+         case op_getstatic_1:
+         case op_getstatic_2s:
+         case op_getstatic_2u:
+         case op_getstatic_4:
+         case op_getstatic_8:
+         case op_getstatic_a:
          default:
            verify_fail ("unrecognized instruction in branch_prepass",
                         start_PC);
@@ -1793,18 +1971,18 @@ private:
     // Verify exception handlers.
     for (int i = 0; i < current_method->exc_count; ++i)
       {
-       if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
+       if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
          verify_fail ("exception handler not at instruction start",
-                      exception[i].handler_pc);
-       if (! (flags[exception[i].start_pc] & FLAG_INSN_START))
+                      exception[i].handler_pc.i);
+       if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
          verify_fail ("exception start not at instruction start",
-                      exception[i].start_pc);
-       if (exception[i].end_pc != current_method->code_length
-           && ! (flags[exception[i].end_pc] & FLAG_INSN_START))
+                      exception[i].start_pc.i);
+       if (exception[i].end_pc.i != current_method->code_length
+           && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
          verify_fail ("exception end not at instruction start",
-                      exception[i].end_pc);
+                      exception[i].end_pc.i);
 
-       flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
+       flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
       }
   }
 
@@ -1974,21 +2152,31 @@ private:
   bool initialize_stack ()
   {
     int var = 0;
-    bool is_init = false;
+    bool is_init = _Jv_equalUtf8Consts (current_method->self->name,
+                                       gcj::init_name);
+    bool is_clinit = _Jv_equalUtf8Consts (current_method->self->name,
+                                         gcj::clinit_name);
 
     using namespace java::lang::reflect;
     if (! Modifier::isStatic (current_method->self->accflags))
       {
        type kurr (current_class);
-       if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
+       if (is_init)
          {
            kurr.set_uninitialized (type::SELF, this);
            is_init = true;
          }
+       else if (is_clinit)
+         verify_fail ("<clinit> method must be static");
        set_variable (0, kurr);
        current_state->set_this_type (kurr);
        ++var;
       }
+    else
+      {
+       if (is_init)
+         verify_fail ("<init> method must be non-static");
+      }
 
     // We have to handle wide arguments specially here.
     int arg_count = _Jv_count_arguments (current_method->self->signature);
@@ -2033,6 +2221,7 @@ private:
              verify_fail ("can't happen: saw state::INVALID");
            if (PC == state::NO_NEXT)
              break;
+           debug_print ("== State pop from pending list\n");
            // Set up the current state.
            current_state->copy (states[PC], current_method->max_stack,
                                 current_method->max_locals);
@@ -2096,12 +2285,12 @@ private:
        // through them all.
        for (int i = 0; i < current_method->exc_count; ++i)
          {
-           if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
+           if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
              {
                type handler (&java::lang::Throwable::class$);
-               if (exception[i].handler_type != 0)
-                 handler = check_class_constant (exception[i].handler_type);
-               push_exception_jump (handler, exception[i].handler_pc);
+               if (exception[i].handler_type.i != 0)
+                 handler = check_class_constant (exception[i].handler_type.i);
+               push_exception_jump (handler, exception[i].handler_pc.i);
              }
          }
 
@@ -2211,42 +2400,42 @@ private:
            break;
          case op_iaload:
            pop_type (int_type);
-           push_type (require_array_type (pop_type (reference_type),
+           push_type (require_array_type (pop_init_ref (reference_type),
                                           int_type));
            break;
          case op_laload:
            pop_type (int_type);
-           push_type (require_array_type (pop_type (reference_type),
+           push_type (require_array_type (pop_init_ref (reference_type),
                                           long_type));
            break;
          case op_faload:
            pop_type (int_type);
-           push_type (require_array_type (pop_type (reference_type),
+           push_type (require_array_type (pop_init_ref (reference_type),
                                           float_type));
            break;
          case op_daload:
            pop_type (int_type);
-           push_type (require_array_type (pop_type (reference_type),
+           push_type (require_array_type (pop_init_ref (reference_type),
                                           double_type));
            break;
          case op_aaload:
            pop_type (int_type);
-           push_type (require_array_type (pop_type (reference_type),
+           push_type (require_array_type (pop_init_ref (reference_type),
                                           reference_type));
            break;
          case op_baload:
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), byte_type);
+           require_array_type (pop_init_ref (reference_type), byte_type);
            push_type (int_type);
            break;
          case op_caload:
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), char_type);
+           require_array_type (pop_init_ref (reference_type), char_type);
            push_type (int_type);
            break;
          case op_saload:
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), short_type);
+           require_array_type (pop_init_ref (reference_type), short_type);
            push_type (int_type);
            break;
          case op_istore:
@@ -2297,48 +2486,52 @@ private:
          case op_iastore:
            pop_type (int_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), int_type);
+           require_array_type (pop_init_ref (reference_type), int_type);
            break;
          case op_lastore:
            pop_type (long_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), long_type);
+           require_array_type (pop_init_ref (reference_type), long_type);
            break;
          case op_fastore:
            pop_type (float_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), float_type);
+           require_array_type (pop_init_ref (reference_type), float_type);
            break;
          case op_dastore:
            pop_type (double_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), double_type);
+           require_array_type (pop_init_ref (reference_type), double_type);
            break;
          case op_aastore:
            pop_type (reference_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), reference_type);
+           require_array_type (pop_init_ref (reference_type), reference_type);
            break;
          case op_bastore:
            pop_type (int_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), byte_type);
+           require_array_type (pop_init_ref (reference_type), byte_type);
            break;
          case op_castore:
            pop_type (int_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), char_type);
+           require_array_type (pop_init_ref (reference_type), char_type);
            break;
          case op_sastore:
            pop_type (int_type);
            pop_type (int_type);
-           require_array_type (pop_type (reference_type), short_type);
+           require_array_type (pop_init_ref (reference_type), short_type);
            break;
          case op_pop:
            pop32 ();
            break;
          case op_pop2:
-           pop64 ();
+           {
+             type t = pop_raw ();
+             if (! t.iswide ())
+               pop32 ();
+           }
            break;
          case op_dup:
            {
@@ -2670,7 +2863,7 @@ private:
            invalidate_pc ();
            break;
          case op_areturn:
-           check_return_type (pop_type (reference_type));
+           check_return_type (pop_init_ref (reference_type));
            invalidate_pc ();
            break;
          case op_return:
@@ -2752,7 +2945,7 @@ private:
                  // This is only used for verifying the byte for
                  // invokeinterface.
                  nargs -= arg_types[i].depth ();
-                 pop_type (arg_types[i]);
+                 pop_init_ref (arg_types[i]);
                }
 
              if (opcode == op_invokeinterface
@@ -2767,9 +2960,36 @@ private:
                      // In this case the PC doesn't matter.
                      t.set_uninitialized (type::UNINIT, this);
                    }
-                 t = pop_type (t);
+                 type raw = pop_raw ();
+                 bool ok = false;
+                 if (! is_init && ! raw.isinitialized ())
+                   {
+                     // This is a failure.
+                   }
+                 else if (is_init && raw.isnull ())
+                   {
+                     // Another failure.
+                   }
+                 else if (t.compatible (raw, this))
+                   {
+                     ok = true;
+                   }
+                 else if (opcode == op_invokeinterface)
+                   {
+                     // This is a hack.  We might have merged two
+                     // items and gotten `Object'.  This can happen
+                     // because we don't keep track of where merges
+                     // come from.  This is safe as long as the
+                     // interpreter checks interfaces at runtime.
+                     type obj (&java::lang::Object::class$);
+                     ok = raw.compatible (obj, this);
+                   }
+
+                 if (! ok)
+                   verify_fail ("incompatible type on stack");
+
                  if (is_init)
-                   current_state->set_initialized (t.get_pc (),
+                   current_state->set_initialized (raw.get_pc (),
                                                    current_method->max_locals);
                }
 
@@ -2806,7 +3026,7 @@ private:
            break;
          case op_arraylength:
            {
-             type t = pop_type (reference_type);
+             type t = pop_init_ref (reference_type);
              if (! t.isarray () && ! t.isnull ())
                verify_fail ("array type expected");
              push_type (int_type);
@@ -2817,19 +3037,19 @@ private:
            invalidate_pc ();
            break;
          case op_checkcast:
-           pop_type (reference_type);
+           pop_init_ref (reference_type);
            push_type (check_class_constant (get_ushort ()));
            break;
          case op_instanceof:
-           pop_type (reference_type);
+           pop_init_ref (reference_type);
            check_class_constant (get_ushort ());
            push_type (int_type);
            break;
          case op_monitorenter:
-           pop_type (reference_type);
+           pop_init_ref (reference_type);
            break;
          case op_monitorexit:
-           pop_type (reference_type);
+           pop_init_ref (reference_type);
            break;
          case op_wide:
            {
@@ -2863,7 +3083,7 @@ private:
                  set_variable (get_ushort (), pop_type (double_type));
                  break;
                case op_astore:
-                 set_variable (get_ushort (), pop_type (reference_type));
+                 set_variable (get_ushort (), pop_init_ref (reference_type));
                  break;
                case op_ret:
                  handle_ret_insn (get_short ());
@@ -2902,6 +3122,30 @@ private:
            handle_jsr_insn (get_int ());
            break;
 
+         // These are unused here, but we call them out explicitly
+         // so that -Wswitch-enum doesn't complain.
+         case op_putfield_1:
+         case op_putfield_2:
+         case op_putfield_4:
+         case op_putfield_8:
+         case op_putfield_a:
+         case op_putstatic_1:
+         case op_putstatic_2:
+         case op_putstatic_4:
+         case op_putstatic_8:
+         case op_putstatic_a:
+         case op_getfield_1:
+         case op_getfield_2s:
+         case op_getfield_2u:
+         case op_getfield_4:
+         case op_getfield_8:
+         case op_getfield_a:
+         case op_getstatic_1:
+         case op_getstatic_2s:
+         case op_getstatic_2u:
+         case op_getstatic_4:
+         case op_getstatic_8:
+         case op_getstatic_a:
          default:
            // Unrecognized opcode.
            verify_fail ("unrecognized instruction in verify_instructions_0",
@@ -2910,34 +3154,6 @@ private:
       }
   }
 
-  __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
-  {
-    using namespace java::lang;
-    StringBuffer *buf = new StringBuffer ();
-
-    buf->append (JvNewStringLatin1 ("verification failed"));
-    if (pc == -1)
-      pc = start_PC;
-    if (pc != -1)
-      {
-       buf->append (JvNewStringLatin1 (" at PC "));
-       buf->append (pc);
-      }
-
-    _Jv_InterpMethod *method = current_method;
-    buf->append (JvNewStringLatin1 (" in "));
-    buf->append (current_class->getName());
-    buf->append ((jchar) ':');
-    buf->append (JvNewStringUTF (method->get_method()->name->data));
-    buf->append ((jchar) '(');
-    buf->append (JvNewStringUTF (method->get_method()->signature->data));
-    buf->append ((jchar) ')');
-
-    buf->append (JvNewStringLatin1 (": "));
-    buf->append (JvNewStringLatin1 (s));
-    throw new java::lang::VerifyError (buf->toString ());
-  }
-
 public:
 
   void verify_instructions ()