OSDN Git Service

2006-07-13 Bryce McKinlay <mckinlay@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / interpret.cc
index 4e5e5df..b9abb01 100644 (file)
@@ -25,7 +25,6 @@ details.  */
 #include <java/lang/StringBuffer.h>
 #include <java/lang/Class.h>
 #include <java/lang/reflect/Modifier.h>
-#include <java/lang/VirtualMachineError.h>
 #include <java/lang/InternalError.h>
 #include <java/lang/NullPointerException.h>
 #include <java/lang/ArithmeticException.h>
@@ -51,10 +50,8 @@ static void throw_internal_error (const char *msg)
   __attribute__ ((__noreturn__));
 static void throw_incompatible_class_change_error (jstring msg)
   __attribute__ ((__noreturn__));
-#ifndef HANDLE_SEGV
 static void throw_null_pointer_exception ()
   __attribute__ ((__noreturn__));
-#endif
 
 static void throw_class_format_error (jstring msg)
        __attribute__ ((__noreturn__));
@@ -224,12 +221,20 @@ static jint get4(unsigned char* loc) {
 
 #define SAVE_PC() frame_desc.pc = pc
 
+// We used to define this conditionally, depending on HANDLE_SEGV.
+// However, that runs into a problem if a chunk in low memory is
+// mapped and we try to look at a field near the end of a large
+// object.  See PR 26858 for details.  It is, most likely, relatively
+// inexpensive to simply do this check always.
+#define NULLCHECK(X) \
+  do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
+
+// Note that we can still conditionally define NULLARRAYCHECK, since
+// we know that all uses of an array will first reference the length
+// field, which is first -- and thus will trigger a SEGV.
 #ifdef HANDLE_SEGV
-#define NULLCHECK(X) SAVE_PC()
 #define NULLARRAYCHECK(X) SAVE_PC()
 #else
-#define NULLCHECK(X) \
-  do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
 #define NULLARRAYCHECK(X) \
   do { SAVE_PC(); if ((X)==NULL) { throw_null_pointer_exception (); } } while (0)
 #endif
@@ -771,6 +776,8 @@ _Jv_InterpMethod::compile (const void * const *insn_targets)
       exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
       exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
       exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
+      // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
+      // during compilation.
       jclass handler
        = (_Jv_Linker::resolve_pool_entry (defining_class,
                                             exc[i].handler_type.i)).clazz;
@@ -807,8 +814,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
   // destructor so it cleans up automatically when the interpreter
   // returns.
   java::lang::Thread *thread = java::lang::Thread::currentThread();
-  _Jv_InterpFrame frame_desc (meth,
-                             (_Jv_InterpFrame **) &thread->interp_frame);
+  _Jv_InterpFrame frame_desc (meth, thread);
 
   _Jv_word stack[meth->max_stack];
   _Jv_word *sp = stack;
@@ -1135,6 +1141,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_invokevirtual:        // 0xb6
       {
+       SAVE_PC();
        int index = GET2U ();
 
        /* _Jv_Linker::resolve_pool_entry returns immediately if the
@@ -1143,31 +1150,26 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
         * the corresponding bit JV_CONSTANT_ResolvedFlag in the tag
         * directly.  For now, I don't think it is worth it.  */
 
-       SAVE_PC();
        rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
                                                   index)).rmethod;
 
        sp -= rmeth->stack_item_count;
-       // We don't use NULLCHECK here because we can't rely on that
-       // working if the method is final.  So instead we do an
-       // explicit test.
-       if (! sp[0].o)
-         {
-           //printf("invokevirtual pc = %p/%i\n", pc, meth->get_pc_val(pc));
-           throw new java::lang::NullPointerException;
-         }
 
-       if (rmeth->vtable_index == -1)
+       if (rmeth->method->accflags & Modifier::FINAL)
          {
-           // final methods do not appear in the vtable,
-           // if it does not appear in the superclass.
+           // We can't rely on NULLCHECK working if the method is final.
+           if (! sp[0].o)
+             throw_null_pointer_exception ();
+
+           // Final methods might not appear in the vtable.
            fun = (void (*)()) rmeth->method->ncode;
          }
        else
          {
+           NULLCHECK (sp[0].o);
            jobject rcv = sp[0].o;
            _Jv_VTable *table = *(_Jv_VTable**) rcv;
-           fun = (void (*)()) table->get_method (rmeth->vtable_index);
+           fun = (void (*)()) table->get_method (rmeth->method->index);
          }
 
 #ifdef DIRECT_THREADED
@@ -1182,28 +1184,24 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 #ifdef DIRECT_THREADED
     invokevirtual_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
-       // We don't use NULLCHECK here because we can't rely on that
-       // working if the method is final.  So instead we do an
-       // explicit test.
-       if (! sp[0].o)
-         {
-           SAVE_PC();
-           throw new java::lang::NullPointerException;
-         }
 
-       if (rmeth->vtable_index == -1)
+       if (rmeth->method->accflags & Modifier::FINAL)
          {
-           // final methods do not appear in the vtable,
-           // if it does not appear in the superclass.
+           // We can't rely on NULLCHECK working if the method is final.
+           if (! sp[0].o)
+             throw_null_pointer_exception ();
+
+           // Final methods might not appear in the vtable.
            fun = (void (*)()) rmeth->method->ncode;
          }
        else
          {
            jobject rcv = sp[0].o;
            _Jv_VTable *table = *(_Jv_VTable**) rcv;
-           fun = (void (*)()) table->get_method (rmeth->vtable_index);
+           fun = (void (*)()) table->get_method (rmeth->method->index);
          }
       }
       goto perform_invoke;
@@ -1211,8 +1209,6 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     perform_invoke:
       {
-        SAVE_PC();
-       
        /* here goes the magic again... */
        ffi_cif *cif = &rmeth->cif;
        ffi_raw *raw = (ffi_raw*) sp;
@@ -1362,6 +1358,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
       // For direct threaded we have a separate 'ldc class' operation.
     insn_ldc_class:
       {
+       SAVE_PC();
        // We could rewrite the instruction at this point.
        int index = INTVAL ();
        jobject k = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
@@ -1830,6 +1827,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_idiv:
       {
+       SAVE_PC();
        jint value2 = POPI();
        jint value1 = POPI();
        jint res = _Jv_divI (value1, value2);
@@ -1839,6 +1837,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_ldiv:
       {
+       SAVE_PC();
        jlong value2 = POPL();
        jlong value1 = POPL();
        jlong res = _Jv_divJ (value1, value2);
@@ -1848,6 +1847,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_fdiv:
       {
+       SAVE_PC();
        jfloat value2 = POPF();
        jfloat value1 = POPF();
        jfloat res = value1 / value2;
@@ -1866,6 +1866,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_irem:
       {
+       SAVE_PC();
        jint value2 = POPI();
        jint value1 =  POPI();
        jint res = _Jv_remI (value1, value2);
@@ -1875,6 +1876,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_lrem:
       {
+       SAVE_PC();
        jlong value2 = POPL();
        jlong value1 = POPL();
        jlong res = _Jv_remJ (value1, value2);
@@ -2543,6 +2545,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_getfield:
       {
+       SAVE_PC();
        jint fieldref_index = GET2U ();
        _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
        _Jv_Field *field = pool_data[fieldref_index].field;
@@ -2553,8 +2556,6 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
        jclass type = field->type;
        jint field_offset = field->u.boffset;
-       if (field_offset > 0xffff)
-         throw new java::lang::VirtualMachineError;
 
        jobject obj   = POPA();
        NULLCHECK(obj);
@@ -2659,6 +2660,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_putstatic:
       {
+       SAVE_PC();
        jint fieldref_index = GET2U ();
        _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
        _Jv_Field *field = pool_data[fieldref_index].field;
@@ -2746,6 +2748,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_putfield:
       {
+       SAVE_PC();
        jint fieldref_index = GET2U ();
        _Jv_Linker::resolve_pool_entry (meth->defining_class, fieldref_index);
        _Jv_Field *field = pool_data[fieldref_index].field;
@@ -2757,8 +2760,6 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
            (JvNewStringLatin1 ("field is static"));
 
        jint field_offset = field->u.boffset;
-       if (field_offset > 0xffff)
-         throw new java::lang::VirtualMachineError;
 
        void *newinsn = NULL;
        if (type->isPrimitive ())
@@ -2871,6 +2872,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_invokespecial:
       {
+       SAVE_PC();
        int index = GET2U ();
 
        rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
@@ -2883,7 +2885,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
        if (! sp[0].o)
          {
            SAVE_PC();
-           throw new java::lang::NullPointerException;
+           throw_null_pointer_exception ();
          }
 
        fun = (void (*)()) rmeth->method->ncode;
@@ -2900,14 +2902,14 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 #ifdef DIRECT_THREADED
     invokespecial_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
        // We don't use NULLCHECK here because we can't rely on that
        // working for <init>.  So instead we do an explicit test.
        if (! sp[0].o)
          {
-           SAVE_PC();
-           throw new java::lang::NullPointerException;
+           throw_null_pointer_exception ();
          }
        fun = (void (*)()) rmeth->method->ncode;
       }
@@ -2916,6 +2918,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_invokestatic:
       {
+       SAVE_PC();
        int index = GET2U ();
 
        rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
@@ -2937,6 +2940,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 #ifdef DIRECT_THREADED
     invokestatic_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
        fun = (void (*)()) rmeth->method->ncode;
@@ -2946,6 +2950,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_invokeinterface:
       {
+       SAVE_PC();
        int index = GET2U ();
 
        rmeth = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
@@ -2977,6 +2982,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 #ifdef DIRECT_THREADED
     invokeinterface_resolved:
       {
+       SAVE_PC();
        rmeth = (_Jv_ResolvedMethod *) AVAL ();
        sp -= rmeth->stack_item_count;
        jobject rcv = sp[0].o;
@@ -2991,6 +2997,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_new:
       {
+       SAVE_PC();
        int index = GET2U ();
        jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
                                                          index)).clazz;
@@ -3029,6 +3036,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_anewarray:
       {
+       SAVE_PC();
        int index = GET2U ();
        jclass klass = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
                                                          index)).clazz;
@@ -3164,6 +3172,7 @@ _Jv_InterpMethod::run (void *retp, ffi_raw *args, _Jv_InterpMethod *meth)
 
     insn_multianewarray:
       {
+       SAVE_PC();
        int kind_index = GET2U ();
        int dim        = GET1U ();
 
@@ -3305,17 +3314,11 @@ throw_incompatible_class_change_error (jstring msg)
   throw new java::lang::IncompatibleClassChangeError (msg);
 }
 
-#ifndef HANDLE_SEGV
-static java::lang::NullPointerException *null_pointer_exc;
 static void 
 throw_null_pointer_exception ()
 {
-  if (null_pointer_exc == NULL)
-    null_pointer_exc = new java::lang::NullPointerException;
-
-  throw null_pointer_exc;
+  throw new java::lang::NullPointerException;
 }
-#endif
 
 /* Look up source code line number for given bytecode (or direct threaded
    interpreter) PC. */
@@ -3883,7 +3886,17 @@ _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
   _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
 
   // Splitting the allocations here lets us scan reference fields and
-  // avoid scanning non-reference fields.
+  // avoid scanning non-reference fields.  How reference fields are
+  // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
+  // means that this memory will be scanned conservatively (same
+  // difference, since we know all the contents here are pointers).
+  // Then we put pointers into this memory into the 'fields'
+  // structure.  Most of these are interior pointers, which is ok (but
+  // even so the pointer to the first reference field will be used and
+  // that is not an interior pointer).  The 'fields' array is also
+  // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
+  // be scanned.  A pointer to this array is held by Class and thus
+  // seen by the collector.
   char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
   char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
 
@@ -3911,7 +3924,7 @@ _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
 
 _Jv_ResolvedMethod *
 _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
-                                         jboolean staticp, jint vtable_index)
+                                         jboolean staticp)
 {
   int arg_count = _Jv_count_arguments (method->signature, staticp);
 
@@ -3927,7 +3940,6 @@ _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
                &result->arg_types[0],
                NULL);
 
-  result->vtable_index        = vtable_index;
   result->method              = method;
   result->klass               = klass;