]> gcc.gnu.org Git - gcc.git/blobdiff - libjava/verify.cc
verify.cc (branch_prepass): Updated for change to exception handler type.
[gcc.git] / libjava / verify.cc
index c9cfb885fdf84f595ad219bd15c30102aab840be..29f0f321a7f856c41fa4267c71bb8ba72b1f3948 100644 (file)
@@ -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.
@@ -231,9 +225,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 +232,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 +245,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 ();
@@ -522,6 +523,11 @@ private:
       return false;
     }
 
+    bool isnull () const
+    {
+      return key == null_type;
+    }
+
     bool isinterface (_Jv_BytecodeVerifier *verifier)
     {
       resolve (verifier);
@@ -670,14 +676,19 @@ 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)
                    {
@@ -756,10 +767,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;
@@ -789,6 +800,11 @@ 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 ()
     {
@@ -887,8 +903,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.
@@ -936,13 +950,29 @@ private:
          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.
@@ -957,8 +987,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);
                }
            }
 
@@ -1027,6 +1063,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)
@@ -1047,7 +1085,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
@@ -1161,6 +1202,12 @@ 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.  If
+    // the requested element type is some kind of reference, return
+    // the null type instead.
+    if (array.isnull ())
+      return element.isreference () ? type (null_type) : element;
+
     if (! array.isarray ())
       verify_fail ("array required");
 
@@ -1313,10 +1360,9 @@ private:
        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;
   }
 
@@ -1420,12 +1466,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
@@ -1433,6 +1477,23 @@ 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.
+
+    // FIXME: what if we have a jsr at the end of the code, but that
+    // jsr has no corresponding ret?  Is this verifiable, or is it
+    // not?  If it is then we need a special case here.
+    if (PC >= current_method->code_length)
+      verify_fail ("fell off end");
+
+    current_state->stacktop = state::NO_STACK;
+    push_jump_merge (PC, current_state);
+    invalidate_pc ();
   }
 
   jclass construct_primitive_array_type (type_val prim)
@@ -1464,6 +1525,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");
       }
@@ -1767,6 +1841,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);
@@ -1784,18 +1882,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;
       }
   }
 
@@ -2024,6 +2122,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);
@@ -2087,12 +2186,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);
              }
          }
 
@@ -2373,6 +2472,8 @@ private:
                  push_type (t);
                  push_type (t2);
                }
+             else
+               push_type (t);
              push_type (t);
            }
            break;
@@ -2756,9 +2857,28 @@ 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 (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);
                }
 
@@ -2796,7 +2916,7 @@ private:
          case op_arraylength:
            {
              type t = pop_type (reference_type);
-             if (! t.isarray ())
+             if (! t.isarray () && ! t.isnull ())
                verify_fail ("array type expected");
              push_type (int_type);
            }
@@ -2891,6 +3011,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",
This page took 0.041628 seconds and 5 git commands to generate.