1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 // Writte by Tom Tromey <tromey@redhat.com>
17 #include <java-insns.h>
18 #include <java-interp.h>
22 #include <java/lang/Class.h>
23 #include <java/lang/VerifyError.h>
24 #include <java/lang/Throwable.h>
25 #include <java/lang/reflect/Modifier.h>
29 // * read more about when classes must be loaded
30 // * there are bugs with boolean arrays?
31 // * class loader madness
32 // * Lots and lots of debugging and testing
33 // * type representation is still ugly. look for the big switches
34 // * at least one GC problem :-(
37 // This is global because __attribute__ doesn't seem to work on static
39 static void verify_fail (char *s) __attribute__ ((__noreturn__));
41 class _Jv_BytecodeVerifier
45 static const int FLAG_INSN_START = 1;
46 static const int FLAG_BRANCH_TARGET = 2;
47 static const int FLAG_JSR_TARGET = 4;
55 // The PC corresponding to the start of the current instruction.
58 // The current state of the stack, locals, etc.
61 // We store the state at branch targets, for merging. This holds
65 // We keep a linked list of all the PCs which we must reverify.
66 // The link is done using the PC values. This is the head of the
70 // We keep some flags for each instruction. The values are the
71 // FLAG_* constants defined above.
74 // We need to keep track of which instructions can call a given
75 // subroutine. FIXME: this is inefficient. We keep a linked list
76 // of all calling `jsr's at at each jsr target.
79 // The current top of the stack, in terms of slots.
81 // The current depth of the stack. This will be larger than
82 // STACKTOP when wide types are on the stack.
85 // The bytecode itself.
86 unsigned char *bytecode;
88 _Jv_InterpException *exception;
93 _Jv_InterpMethod *current_method;
95 // This enum holds a list of tags for all the different types we
96 // need to handle. Reference types are treated specially by the
102 // The values for primitive types are chosen to correspond to values
103 // specified to newarray.
113 // Used when overwriting second word of a double or long in the
114 // local variables. Also used after merging local variable states
115 // to indicate an unusable value.
120 // Everything after `reference_type' must be a reference type.
123 unresolved_reference_type,
124 uninitialized_reference_type,
125 uninitialized_unresolved_reference_type
128 // Return the type_val corresponding to a primitive signature
129 // character. For instance `I' returns `int.class'.
130 static type_val get_type_val_for_signature (jchar sig)
160 verify_fail ("invalid signature");
165 // Return the type_val corresponding to a primitive class.
166 static type_val get_type_val_for_signature (jclass k)
168 return get_type_val_for_signature ((jchar) k->method_count);
171 // This is used to keep track of which `jsr's correspond to a given
175 // PC of the instruction just after the jsr.
181 // The `type' class is used to represent a single type in the
187 // Some associated data.
190 // For a resolved reference type, this is a pointer to the class.
192 // For other reference types, this it the name of the class.
195 // This is used when constructing a new object. It is the PC of the
196 // `new' instruction which created the object. We use the special
197 // value -2 to mean that this is uninitialized, and the special
198 // value -1 for the case where the current method is itself the
202 static const int UNINIT = -2;
203 static const int SELF = -1;
205 // Basic constructor.
208 key = unsuitable_type;
213 // Make a new instance given the type tag. We assume a generic
214 // `reference_type' means Object.
219 if (key == reference_type)
220 data.klass = &java::lang::Object::class$;
224 // Make a new instance given a class.
227 key = reference_type;
232 // Make a new instance given the name of a class.
233 type (_Jv_Utf8Const *n)
235 key = unresolved_reference_type;
248 // These operators are required because libgcj can't link in
250 void *operator new[] (size_t bytes)
252 return _Jv_Malloc (bytes);
255 void operator delete[] (void *mem)
260 type& operator= (type_val k)
268 type& operator= (const type& t)
276 // Promote a numeric type.
279 if (key == boolean_type || key == char_type
280 || key == byte_type || key == short_type)
284 // If *THIS is an unresolved reference type, resolve it.
287 if (key != unresolved_reference_type
288 && key != uninitialized_unresolved_reference_type)
291 // FIXME: class loader
292 using namespace java::lang;
293 // We might see either kind of name. Sigh.
294 if (data.name->data[0] == 'L'
295 && data.name->data[data.name->length - 1] == ';')
296 data.klass = _Jv_FindClassFromSignature (data.name->data, NULL);
298 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
300 key = (key == unresolved_reference_type
302 : uninitialized_reference_type);
305 // Mark this type as the uninitialized result of `new'.
306 void set_uninitialized (int pc)
308 if (key != reference_type && key != unresolved_reference_type)
309 verify_fail ("internal error in type::uninitialized");
310 key = (key == reference_type
311 ? uninitialized_reference_type
312 : uninitialized_unresolved_reference_type);
316 // Mark this type as now initialized.
317 void set_initialized (int npc)
321 key = (key == uninitialized_reference_type
323 : unresolved_reference_type);
329 // Return true if an object of type K can be assigned to a variable
330 // of type *THIS. Handle various special cases too. Might modify
331 // *THIS or K. Note however that this does not perform numeric
333 bool compatible (type &k)
335 // Any type is compatible with the unsuitable type.
336 if (key == unsuitable_type)
339 if (key < reference_type || k.key < reference_type)
342 // The `null' type is convertible to any reference type.
343 // FIXME: is this correct for THIS?
344 if (key == null_type || k.key == null_type)
347 // Any reference type is convertible to Object. This is a special
348 // case so we don't need to unnecessarily resolve a class.
349 if (key == reference_type
350 && data.klass == &java::lang::Object::class$)
353 // An initialized type and an uninitialized type are not
355 if (isinitialized () != k.isinitialized ())
358 // Two uninitialized objects are compatible if either:
359 // * The PCs are identical, or
360 // * One PC is UNINIT.
361 if (! isinitialized ())
363 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
367 // Two unresolved types are equal if their names are the same.
370 && _Jv_equalUtf8Consts (data.name, k.data.name))
373 // We must resolve both types and check assignability.
376 // Use _Jv_IsAssignableFrom to avoid premature class
378 return _Jv_IsAssignableFrom (data.klass, k.data.klass);
383 return key == void_type;
388 return key == long_type || key == double_type;
391 // Return number of stack or local variable slots taken by this
395 return iswide () ? 2 : 1;
398 bool isarray () const
400 // We treat null_type as not an array. This is ok based on the
401 // current uses of this method.
402 if (key == reference_type)
403 return data.klass->isArray ();
404 else if (key == unresolved_reference_type)
405 return data.name->data[0] == '[';
412 if (key != reference_type)
414 return data.klass->isInterface ();
420 if (key != reference_type)
422 using namespace java::lang::reflect;
423 return Modifier::isAbstract (data.klass->getModifiers ());
426 // Return the element type of an array.
429 // FIXME: maybe should do string manipulation here.
431 if (key != reference_type)
432 verify_fail ("programmer error in type::element_type()");
434 jclass k = data.klass->getComponentType ();
435 if (k->isPrimitive ())
436 return type (get_type_val_for_signature (k));
440 bool isreference () const
442 return key >= reference_type;
450 bool isinitialized () const
452 return (key == reference_type
454 || key == unresolved_reference_type);
457 bool isresolved () const
459 return (key == reference_type
461 || key == uninitialized_reference_type);
464 void verify_dimensions (int ndims)
466 // The way this is written, we don't need to check isarray().
467 if (key == reference_type)
469 jclass k = data.klass;
470 while (k->isArray () && ndims > 0)
472 k = k->getComponentType ();
478 // We know KEY == unresolved_reference_type.
479 char *p = data.name->data;
480 while (*p++ == '[' && ndims-- > 0)
485 verify_fail ("array type has fewer dimensions than required");
488 // Merge OLD_TYPE into this. On error throw exception.
489 bool merge (type& old_type, bool local_semantics = false)
491 bool changed = false;
492 bool refo = old_type.isreference ();
493 bool refn = isreference ();
496 if (old_type.key == null_type)
498 else if (key == null_type)
503 else if (isinitialized () != old_type.isinitialized ())
504 verify_fail ("merging initialized and uninitialized types");
507 if (! isinitialized ())
511 else if (old_type.pc == UNINIT)
513 else if (pc != old_type.pc)
514 verify_fail ("merging different uninitialized types");
518 && ! old_type.isresolved ()
519 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
521 // Types are identical.
528 jclass k = data.klass;
529 jclass oldk = old_type.data.klass;
532 while (k->isArray () && oldk->isArray ())
535 k = k->getComponentType ();
536 oldk = oldk->getComponentType ();
539 // This loop will end when we hit Object.
542 // Use _Jv_IsAssignableFrom to avoid premature
543 // class initialization.
544 if (_Jv_IsAssignableFrom (k, oldk))
546 k = k->getSuperclass ();
552 while (arraycount > 0)
554 // FIXME: Class loader.
555 k = _Jv_GetArrayClass (k, NULL);
563 else if (refo || refn || key != old_type.key)
567 key = unsuitable_type;
571 verify_fail ("unmergeable type");
577 // This class holds all the state information we need for a given
581 // Current top of stack.
583 // Current stack depth. This is like the top of stack but it
584 // includes wide variable information.
588 // The local variables.
590 // This is used in subroutines to keep track of which local
591 // variables have been accessed.
593 // If not 0, then we are in a subroutine. The value is the PC of
594 // the subroutine's entry point. We can use 0 as an exceptional
595 // value because PC=0 can never be a subroutine.
597 // This is used to keep a linked list of all the states which
598 // require re-verification. We use the PC to keep track.
601 // INVALID marks a state which is not on the linked list of states
602 // requiring reverification.
603 static const int INVALID = -1;
604 // NO_NEXT marks the state at the end of the reverification list.
605 static const int NO_NEXT = -2;
611 local_changed = NULL;
614 state (int max_stack, int max_locals)
618 stack = new type[max_stack];
619 for (int i = 0; i < max_stack; ++i)
620 stack[i] = unsuitable_type;
621 locals = new type[max_locals];
622 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
623 for (int i = 0; i < max_locals; ++i)
625 locals[i] = unsuitable_type;
626 local_changed[i] = false;
632 state (const state *copy, int max_stack, int max_locals)
634 stack = new type[max_stack];
635 locals = new type[max_locals];
636 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
648 _Jv_Free (local_changed);
651 void *operator new[] (size_t bytes)
653 return _Jv_Malloc (bytes);
656 void operator delete[] (void *mem)
661 void *operator new (size_t bytes)
663 return _Jv_Malloc (bytes);
666 void operator delete (void *mem)
671 void copy (const state *copy, int max_stack, int max_locals)
673 stacktop = copy->stacktop;
674 stackdepth = copy->stackdepth;
675 subroutine = copy->subroutine;
676 for (int i = 0; i < max_stack; ++i)
677 stack[i] = copy->stack[i];
678 for (int i = 0; i < max_locals; ++i)
680 locals[i] = copy->locals[i];
681 local_changed[i] = copy->local_changed[i];
683 // Don't modify `next'.
686 // Modify this state to reflect entry to an exception handler.
687 void set_exception (type t, int max_stack)
692 for (int i = stacktop; i < max_stack; ++i)
693 stack[i] = unsuitable_type;
695 // FIXME: subroutine handling?
698 // Merge STATE into this state. Destructively modifies this state.
699 // Returns true if the new state was in fact changed. Will throw an
700 // exception if the states are not mergeable.
701 bool merge (state *state_old, bool ret_semantics,
704 bool changed = false;
706 // Merge subroutine states. *THIS and *STATE_OLD must be in the
707 // same subroutine. Also, recursive subroutine calls must be
709 if (subroutine == state_old->subroutine)
713 else if (subroutine == 0)
715 subroutine = state_old->subroutine;
719 verify_fail ("subroutines merged");
722 if (state_old->stacktop != stacktop)
723 verify_fail ("stack sizes differ");
724 for (int i = 0; i < state_old->stacktop; ++i)
726 if (stack[i].merge (state_old->stack[i]))
730 // Merge local variables.
731 for (int i = 0; i < max_locals; ++i)
733 if (! ret_semantics || local_changed[i])
735 if (locals[i].merge (state_old->locals[i], true))
742 // If we're in a subroutine, we must compute the union of
743 // all the changed local variables.
744 if (state_old->local_changed[i])
751 // Throw an exception if there is an uninitialized object on the
752 // stack or in a local variable. EXCEPTION_SEMANTICS controls
753 // whether we're using backwards-branch or exception-handing
755 void check_no_uninitialized_objects (int max_locals,
756 bool exception_semantics = false)
758 if (! exception_semantics)
760 for (int i = 0; i < stacktop; ++i)
761 if (stack[i].isreference () && ! stack[i].isinitialized ())
762 verify_fail ("uninitialized object on stack");
765 for (int i = 0; i < max_locals; ++i)
766 if (locals[i].isreference () && ! locals[i].isinitialized ())
767 verify_fail ("uninitialized object in local variable");
770 // Note that a local variable was accessed or modified.
771 void note_variable (int index)
774 local_changed[index] = true;
777 // Mark each `new'd object we know of that was allocated at PC as
779 void set_initialized (int pc, int max_locals)
781 for (int i = 0; i < stacktop; ++i)
782 stack[i].set_initialized (pc);
783 for (int i = 0; i < max_locals; ++i)
784 locals[i].set_initialized (pc);
790 if (current_state->stacktop <= 0)
791 verify_fail ("stack empty");
792 type r = current_state->stack[--current_state->stacktop];
793 current_state->stackdepth -= r.depth ();
794 if (current_state->stackdepth < 0)
795 verify_fail ("stack empty");
803 verify_fail ("narrow pop of wide type");
811 verify_fail ("wide pop of narrow type");
815 type pop_type (type match)
818 if (! match.compatible (t))
819 verify_fail ("incompatible type on stack");
823 void push_type (type t)
825 // If T is a numeric type like short, promote it to int.
828 int depth = t.depth ();
829 if (current_state->stackdepth + depth > current_method->max_stack)
830 verify_fail ("stack overflow");
831 current_state->stack[current_state->stacktop++] = t;
832 current_state->stackdepth += depth;
835 void set_variable (int index, type t)
837 // If T is a numeric type like short, promote it to int.
840 int depth = t.depth ();
841 if (index > current_method->max_locals - depth)
842 verify_fail ("invalid local variable");
843 current_state->locals[index] = t;
844 current_state->note_variable (index);
848 current_state->locals[index + 1] = continuation_type;
849 current_state->note_variable (index + 1);
851 if (index > 0 && current_state->locals[index - 1].iswide ())
853 current_state->locals[index - 1] = unsuitable_type;
854 // There's no need to call note_variable here.
858 type get_variable (int index, type t)
860 int depth = t.depth ();
861 if (index > current_method->max_locals - depth)
862 verify_fail ("invalid local variable");
863 if (! t.compatible (current_state->locals[index]))
864 verify_fail ("incompatible type in local variable");
867 type t (continuation_type);
868 if (! current_state->locals[index + 1].compatible (t))
869 verify_fail ("invalid local variable");
871 current_state->note_variable (index);
872 return current_state->locals[index];
875 // Make sure ARRAY is an array type and that its elements are
876 // compatible with type ELEMENT. Returns the actual element type.
877 type require_array_type (type array, type element)
879 if (! array.isarray ())
880 verify_fail ("array required");
882 type t = array.element_type ();
883 if (! element.compatible (t))
884 verify_fail ("incompatible array element type");
886 // Return T and not ELEMENT, because T might be specialized.
892 if (PC >= current_method->code_length)
893 verify_fail ("premature end of bytecode");
894 return (jint) bytecode[PC++] & 0xff;
899 jbyte b1 = get_byte ();
900 jbyte b2 = get_byte ();
901 return (jint) ((b1 << 8) | b2) & 0xffff;
906 jbyte b1 = get_byte ();
907 jbyte b2 = get_byte ();
908 jshort s = (b1 << 8) | b2;
914 jbyte b1 = get_byte ();
915 jbyte b2 = get_byte ();
916 jbyte b3 = get_byte ();
917 jbyte b4 = get_byte ();
918 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
921 int compute_jump (int offset)
923 int npc = start_PC + offset;
924 if (npc < 0 || npc >= current_method->code_length)
925 verify_fail ("branch out of range");
929 // Merge the indicated state into a new state and schedule a new PC if
930 // there is a change. If RET_SEMANTICS is true, then we are merging
931 // from a `ret' instruction into the instruction after a `jsr'. This
932 // is a special case with its own modified semantics.
933 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
936 if (states[npc] == NULL)
938 // FIXME: what if we reach this code from a `ret'?
940 states[npc] = new state (nstate, current_method->max_stack,
941 current_method->max_locals);
944 changed = nstate->merge (states[npc], ret_semantics,
945 current_method->max_stack);
947 if (changed && states[npc]->next == state::INVALID)
949 // The merge changed the state, and the new PC isn't yet on our
950 // list of PCs to re-verify.
951 states[npc]->next = next_verify_pc;
952 next_verify_pc = npc;
956 void push_jump (int offset)
958 int npc = compute_jump (offset);
960 current_state->check_no_uninitialized_objects (current_method->max_stack);
961 push_jump_merge (npc, current_state);
964 void push_exception_jump (type t, int pc)
966 current_state->check_no_uninitialized_objects (current_method->max_stack,
968 state s (current_state, current_method->max_stack,
969 current_method->max_locals);
970 s.set_exception (t, current_method->max_stack);
971 push_jump_merge (pc, &s);
976 int npc = next_verify_pc;
977 if (npc != state::NO_NEXT)
979 next_verify_pc = states[npc]->next;
980 states[npc]->next = state::INVALID;
985 void invalidate_pc ()
990 void note_branch_target (int pc, bool is_jsr_target = false)
992 if (pc <= PC && ! (flags[pc] & FLAG_INSN_START))
993 verify_fail ("branch not to instruction start");
994 flags[pc] |= FLAG_BRANCH_TARGET;
997 // Record the jsr which called this instruction.
998 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1000 info->next = jsr_ptrs[pc];
1001 jsr_ptrs[pc] = info;
1002 flags[pc] |= FLAG_JSR_TARGET;
1006 void skip_padding ()
1008 while ((PC % 4) > 0)
1009 if (get_byte () != 0)
1010 verify_fail ("found nonzero padding byte");
1013 // Return the subroutine to which the instruction at PC belongs.
1014 int get_subroutine (int pc)
1016 if (states[pc] == NULL)
1018 return states[pc]->subroutine;
1021 // Do the work for a `ret' instruction. INDEX is the index into the
1023 void handle_ret_insn (int index)
1025 get_variable (index, return_address_type);
1027 int csub = current_state->subroutine;
1029 verify_fail ("no subroutine");
1031 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1033 // Temporarily modify the current state so it looks like we're
1034 // in the enclosing context.
1035 current_state->subroutine = get_subroutine (subr->pc);
1037 current_state->check_no_uninitialized_objects (current_method->max_stack);
1038 push_jump_merge (subr->pc, current_state, true);
1041 current_state->subroutine = csub;
1045 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1046 // sure this subroutine isn't already on the stack.
1047 void check_nonrecursive_call (int sub, int dest)
1052 verify_fail ("recursive subroutine call");
1053 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1054 check_nonrecursive_call (get_subroutine (info->pc), dest);
1057 void handle_jsr_insn (int offset)
1059 int npc = compute_jump (offset);
1062 current_state->check_no_uninitialized_objects (current_method->max_stack);
1063 check_nonrecursive_call (current_state->subroutine, npc);
1065 // Temporarily modify the current state so that it looks like we are
1066 // in the subroutine.
1067 push_type (return_address_type);
1068 int save = current_state->subroutine;
1069 current_state->subroutine = npc;
1071 // Merge into the subroutine.
1072 push_jump_merge (npc, current_state);
1074 // Undo our modifications.
1075 current_state->subroutine = save;
1076 pop_type (return_address_type);
1079 jclass construct_primitive_array_type (type_val prim)
1085 k = JvPrimClass (boolean);
1088 k = JvPrimClass (char);
1091 k = JvPrimClass (float);
1094 k = JvPrimClass (double);
1097 k = JvPrimClass (byte);
1100 k = JvPrimClass (short);
1103 k = JvPrimClass (int);
1106 k = JvPrimClass (long);
1109 verify_fail ("unknown type in construct_primitive_array_type");
1111 k = _Jv_GetArrayClass (k, NULL);
1115 // This pass computes the location of branch targets and also
1116 // instruction starts.
1117 void branch_prepass ()
1119 flags = (char *) _Jv_Malloc (current_method->code_length);
1120 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1121 * current_method->code_length);
1123 for (int i = 0; i < current_method->code_length; ++i)
1129 bool last_was_jsr = false;
1132 while (PC < current_method->code_length)
1134 flags[PC] |= FLAG_INSN_START;
1136 // If the previous instruction was a jsr, then the next
1137 // instruction is a branch target -- the branch being the
1138 // corresponding `ret'.
1140 note_branch_target (PC);
1141 last_was_jsr = false;
1144 unsigned char opcode = bytecode[PC++];
1148 case op_aconst_null:
1285 case op_monitorenter:
1286 case op_monitorexit:
1309 case op_arraylength:
1325 case op_invokespecial:
1326 case op_invokestatic:
1327 case op_invokevirtual:
1331 case op_multianewarray:
1337 last_was_jsr = true;
1356 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1359 case op_tableswitch:
1362 note_branch_target (compute_jump (get_int ()));
1363 jint low = get_int ();
1364 jint hi = get_int ();
1366 verify_fail ("invalid tableswitch");
1367 for (int i = low; i <= hi; ++i)
1368 note_branch_target (compute_jump (get_int ()));
1372 case op_lookupswitch:
1375 note_branch_target (compute_jump (get_int ()));
1376 int npairs = get_int ();
1378 verify_fail ("too few pairs in lookupswitch");
1379 while (npairs-- > 0)
1382 note_branch_target (compute_jump (get_int ()));
1387 case op_invokeinterface:
1395 opcode = get_byte ();
1397 if (opcode == (unsigned char) op_iinc)
1403 last_was_jsr = true;
1406 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1410 verify_fail ("unrecognized instruction in branch_prepass");
1413 // See if any previous branch tried to branch to the middle of
1414 // this instruction.
1415 for (int pc = start_PC + 1; pc < PC; ++pc)
1417 if ((flags[pc] & FLAG_BRANCH_TARGET))
1418 verify_fail ("branch not to instruction start");
1422 // Verify exception handlers.
1423 for (int i = 0; i < current_method->exc_count; ++i)
1425 if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
1426 verify_fail ("exception handler not at instruction start");
1427 if (exception[i].start_pc > exception[i].end_pc)
1428 verify_fail ("exception range inverted");
1429 if (! (flags[exception[i].start_pc] & FLAG_INSN_START)
1430 || ! (flags[exception[i].start_pc] & FLAG_INSN_START))
1431 verify_fail ("exception endpoint not at instruction start");
1433 flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
1437 void check_pool_index (int index)
1439 if (index < 0 || index >= current_class->constants.size)
1440 verify_fail ("constant pool index out of range");
1443 type check_class_constant (int index)
1445 check_pool_index (index);
1446 _Jv_Constants *pool = ¤t_class->constants;
1447 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1448 return type (pool->data[index].clazz);
1449 else if (pool->tags[index] == JV_CONSTANT_Class)
1450 return type (pool->data[index].utf8);
1451 verify_fail ("expected class constant");
1454 type check_constant (int index)
1456 check_pool_index (index);
1457 _Jv_Constants *pool = ¤t_class->constants;
1458 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1459 || pool->tags[index] == JV_CONSTANT_String)
1460 return type (&java::lang::String::class$);
1461 else if (pool->tags[index] == JV_CONSTANT_Integer)
1462 return type (int_type);
1463 else if (pool->tags[index] == JV_CONSTANT_Float)
1464 return type (float_type);
1465 verify_fail ("String, int, or float constant expected");
1468 // Helper for both field and method. These are laid out the same in
1469 // the constant pool.
1470 type handle_field_or_method (int index, int expected,
1471 _Jv_Utf8Const **name,
1472 _Jv_Utf8Const **fmtype)
1474 check_pool_index (index);
1475 _Jv_Constants *pool = ¤t_class->constants;
1476 if (pool->tags[index] != expected)
1477 verify_fail ("didn't see expected constant");
1478 // Once we know we have a Fieldref or Methodref we assume that it
1479 // is correctly laid out in the constant pool. I think the code
1480 // in defineclass.cc guarantees this.
1481 _Jv_ushort class_index, name_and_type_index;
1482 _Jv_loadIndexes (&pool->data[index],
1484 name_and_type_index);
1485 _Jv_ushort name_index, desc_index;
1486 _Jv_loadIndexes (&pool->data[name_and_type_index],
1487 name_index, desc_index);
1489 *name = pool->data[name_index].utf8;
1490 *fmtype = pool->data[desc_index].utf8;
1492 return check_class_constant (class_index);
1495 // Return field's type, compute class' type if requested.
1496 type check_field_constant (int index, type *class_type = NULL)
1498 _Jv_Utf8Const *name, *field_type;
1499 type ct = handle_field_or_method (index,
1500 JV_CONSTANT_Fieldref,
1501 &name, &field_type);
1504 return type (field_type);
1507 type check_method_constant (int index, bool is_interface,
1508 _Jv_Utf8Const **method_name,
1509 _Jv_Utf8Const **method_signature)
1511 return handle_field_or_method (index,
1513 ? JV_CONSTANT_InterfaceMethodref
1514 : JV_CONSTANT_Methodref),
1515 method_name, method_signature);
1518 type get_one_type (char *&p)
1536 // FIXME! This will get collected!
1537 _Jv_Utf8Const *name = _Jv_makeUtf8Const (start, p - start);
1541 // Casting to jchar here is ok since we are looking at an ASCII
1543 type_val rt = get_type_val_for_signature (jchar (v));
1545 if (arraycount == 0)
1548 jclass k = construct_primitive_array_type (rt);
1549 while (--arraycount > 0)
1550 k = _Jv_GetArrayClass (k, NULL);
1554 void compute_argument_types (_Jv_Utf8Const *signature,
1557 char *p = signature->data;
1563 types[i++] = get_one_type (p);
1566 type compute_return_type (_Jv_Utf8Const *signature)
1568 char *p = signature->data;
1572 return get_one_type (p);
1575 void check_return_type (type expected)
1577 type rt = compute_return_type (current_method->self->signature);
1578 if (! expected.compatible (rt))
1579 verify_fail ("incompatible return type");
1582 void verify_instructions_0 ()
1584 current_state = new state (current_method->max_stack,
1585 current_method->max_locals);
1592 using namespace java::lang::reflect;
1593 if (! Modifier::isStatic (current_method->self->accflags))
1595 type kurr (current_class);
1596 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
1597 kurr.set_uninitialized (type::SELF);
1598 set_variable (0, kurr);
1602 if (var + _Jv_count_arguments (current_method->self->signature)
1603 > current_method->max_locals)
1604 verify_fail ("too many arguments");
1605 compute_argument_types (current_method->self->signature,
1606 ¤t_state->locals[var]);
1609 states = (state **) _Jv_Malloc (sizeof (state *)
1610 * current_method->code_length);
1611 for (int i = 0; i < current_method->code_length; ++i)
1614 next_verify_pc = state::NO_NEXT;
1618 // If the PC was invalidated, get a new one from the work list.
1619 if (PC == state::NO_NEXT)
1622 if (PC == state::INVALID)
1623 verify_fail ("saw state::INVALID");
1624 if (PC == state::NO_NEXT)
1626 // Set up the current state.
1627 *current_state = *states[PC];
1630 // Control can't fall off the end of the bytecode.
1631 if (PC >= current_method->code_length)
1632 verify_fail ("fell off end");
1634 if (states[PC] != NULL)
1636 // We've already visited this instruction. So merge the
1637 // states together. If this yields no change then we don't
1638 // have to re-verify.
1639 if (! current_state->merge (states[PC], false,
1640 current_method->max_stack))
1645 // Save a copy of it for later.
1646 states[PC]->copy (current_state, current_method->max_stack,
1647 current_method->max_locals);
1649 else if ((flags[PC] & FLAG_BRANCH_TARGET))
1651 // We only have to keep saved state at branch targets.
1652 states[PC] = new state (current_state, current_method->max_stack,
1653 current_method->max_locals);
1656 // Update states for all active exception handlers. Ordinarily
1657 // there are not many exception handlers. So we simply run
1658 // through them all.
1659 for (int i = 0; i < current_method->exc_count; ++i)
1661 if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
1663 type handler = reference_type;
1664 if (exception[i].handler_type != 0)
1665 handler = check_class_constant (exception[i].handler_type);
1666 push_exception_jump (handler, exception[i].handler_pc);
1671 unsigned char opcode = bytecode[PC++];
1677 case op_aconst_null:
1678 push_type (null_type);
1688 push_type (int_type);
1693 push_type (long_type);
1699 push_type (float_type);
1704 push_type (double_type);
1709 push_type (int_type);
1714 push_type (int_type);
1718 push_type (check_constant (get_byte ()));
1721 push_type (check_constant (get_ushort ()));
1724 push_type (check_constant (get_ushort ()));
1728 push_type (get_variable (get_byte (), int_type));
1731 push_type (get_variable (get_byte (), long_type));
1734 push_type (get_variable (get_byte (), float_type));
1737 push_type (get_variable (get_byte (), double_type));
1740 push_type (get_variable (get_byte (), reference_type));
1747 push_type (get_variable (opcode - op_iload_0, int_type));
1753 push_type (get_variable (opcode - op_lload_0, long_type));
1759 push_type (get_variable (opcode - op_fload_0, float_type));
1765 push_type (get_variable (opcode - op_dload_0, double_type));
1771 push_type (get_variable (opcode - op_aload_0, reference_type));
1774 pop_type (int_type);
1775 push_type (require_array_type (pop_type (reference_type),
1779 pop_type (int_type);
1780 push_type (require_array_type (pop_type (reference_type),
1784 pop_type (int_type);
1785 push_type (require_array_type (pop_type (reference_type),
1789 pop_type (int_type);
1790 push_type (require_array_type (pop_type (reference_type),
1794 pop_type (int_type);
1795 push_type (require_array_type (pop_type (reference_type),
1799 pop_type (int_type);
1800 require_array_type (pop_type (reference_type), byte_type);
1801 push_type (int_type);
1804 pop_type (int_type);
1805 require_array_type (pop_type (reference_type), char_type);
1806 push_type (int_type);
1809 pop_type (int_type);
1810 require_array_type (pop_type (reference_type), short_type);
1811 push_type (int_type);
1814 set_variable (get_byte (), pop_type (int_type));
1817 set_variable (get_byte (), pop_type (long_type));
1820 set_variable (get_byte (), pop_type (float_type));
1823 set_variable (get_byte (), pop_type (double_type));
1826 set_variable (get_byte (), pop_type (reference_type));
1832 set_variable (opcode - op_istore_0, pop_type (int_type));
1838 set_variable (opcode - op_lstore_0, pop_type (long_type));
1844 set_variable (opcode - op_fstore_0, pop_type (float_type));
1850 set_variable (opcode - op_dstore_0, pop_type (double_type));
1856 set_variable (opcode - op_astore_0, pop_type (reference_type));
1859 pop_type (int_type);
1860 pop_type (int_type);
1861 require_array_type (pop_type (reference_type), int_type);
1864 pop_type (long_type);
1865 pop_type (int_type);
1866 require_array_type (pop_type (reference_type), long_type);
1869 pop_type (float_type);
1870 pop_type (int_type);
1871 require_array_type (pop_type (reference_type), float_type);
1874 pop_type (double_type);
1875 pop_type (int_type);
1876 require_array_type (pop_type (reference_type), double_type);
1879 pop_type (reference_type);
1880 pop_type (int_type);
1881 require_array_type (pop_type (reference_type), reference_type);
1884 pop_type (int_type);
1885 pop_type (int_type);
1886 require_array_type (pop_type (reference_type), byte_type);
1889 pop_type (int_type);
1890 pop_type (int_type);
1891 require_array_type (pop_type (reference_type), char_type);
1894 pop_type (int_type);
1895 pop_type (int_type);
1896 require_array_type (pop_type (reference_type), short_type);
1923 type t2 = pop_raw ();
1938 type t = pop_raw ();
1951 type t1 = pop_raw ();
1969 type t1 = pop_raw ();
1972 type t2 = pop_raw ();
1990 type t3 = pop_raw ();
2028 pop_type (int_type);
2029 push_type (pop_type (int_type));
2042 pop_type (long_type);
2043 push_type (pop_type (long_type));
2050 pop_type (float_type);
2051 push_type (pop_type (float_type));
2058 pop_type (double_type);
2059 push_type (pop_type (double_type));
2065 push_type (pop_type (int_type));
2068 push_type (pop_type (long_type));
2071 push_type (pop_type (float_type));
2074 push_type (pop_type (double_type));
2077 get_variable (get_byte (), int_type);
2081 pop_type (int_type);
2082 push_type (long_type);
2085 pop_type (int_type);
2086 push_type (float_type);
2089 pop_type (int_type);
2090 push_type (double_type);
2093 pop_type (long_type);
2094 push_type (int_type);
2097 pop_type (long_type);
2098 push_type (float_type);
2101 pop_type (long_type);
2102 push_type (double_type);
2105 pop_type (float_type);
2106 push_type (int_type);
2109 pop_type (float_type);
2110 push_type (long_type);
2113 pop_type (float_type);
2114 push_type (double_type);
2117 pop_type (double_type);
2118 push_type (int_type);
2121 pop_type (double_type);
2122 push_type (long_type);
2125 pop_type (double_type);
2126 push_type (float_type);
2129 pop_type (long_type);
2130 pop_type (long_type);
2131 push_type (int_type);
2135 pop_type (float_type);
2136 pop_type (float_type);
2137 push_type (int_type);
2141 pop_type (double_type);
2142 pop_type (double_type);
2143 push_type (int_type);
2151 pop_type (int_type);
2152 push_jump (get_short ());
2160 pop_type (int_type);
2161 pop_type (int_type);
2162 push_jump (get_short ());
2166 pop_type (reference_type);
2167 pop_type (reference_type);
2168 push_jump (get_short ());
2171 push_jump (get_short ());
2175 handle_jsr_insn (get_short ());
2178 handle_ret_insn (get_byte ());
2180 case op_tableswitch:
2182 pop_type (int_type);
2184 push_jump (get_int ());
2185 jint low = get_int ();
2186 jint high = get_int ();
2187 // Already checked LOW -vs- HIGH.
2188 for (int i = low; i <= high; ++i)
2189 push_jump (get_int ());
2194 case op_lookupswitch:
2196 pop_type (int_type);
2198 push_jump (get_int ());
2199 jint npairs = get_int ();
2200 // Already checked NPAIRS >= 0.
2202 for (int i = 0; i < npairs; ++i)
2204 jint key = get_int ();
2205 if (i > 0 && key <= lastkey)
2206 verify_fail ("lookupswitch pairs unsorted");
2208 push_jump (get_int ());
2214 check_return_type (pop_type (int_type));
2218 check_return_type (pop_type (long_type));
2222 check_return_type (pop_type (float_type));
2226 check_return_type (pop_type (double_type));
2230 check_return_type (pop_type (reference_type));
2234 check_return_type (void_type);
2238 push_type (check_field_constant (get_ushort ()));
2241 pop_type (check_field_constant (get_ushort ()));
2246 type field = check_field_constant (get_ushort (), &klass);
2254 type field = check_field_constant (get_ushort (), &klass);
2260 case op_invokevirtual:
2261 case op_invokespecial:
2262 case op_invokestatic:
2263 case op_invokeinterface:
2265 _Jv_Utf8Const *method_name, *method_signature;
2267 = check_method_constant (get_ushort (),
2268 opcode == (unsigned char) op_invokeinterface,
2271 int arg_count = _Jv_count_arguments (method_signature);
2272 if (opcode == (unsigned char) op_invokeinterface)
2274 int nargs = get_byte ();
2276 verify_fail ("too few arguments to invokeinterface");
2277 if (get_byte () != 0)
2278 verify_fail ("invokeinterface dummy byte is wrong");
2279 if (nargs - 1 != arg_count)
2280 verify_fail ("wrong argument count for invokeinterface");
2283 bool is_init = false;
2284 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2287 if (opcode != (unsigned char) op_invokespecial)
2288 verify_fail ("can't invoke <init>");
2290 else if (method_name->data[0] == '<')
2291 verify_fail ("can't invoke method starting with `<'");
2293 // Pop arguments and check types.
2294 type arg_types[arg_count];
2295 compute_argument_types (method_signature, arg_types);
2296 for (int i = arg_count - 1; i >= 0; --i)
2297 pop_type (arg_types[i]);
2299 if (opcode != (unsigned char) op_invokestatic)
2301 type t = class_type;
2304 // In this case the PC doesn't matter.
2305 t.set_uninitialized (type::UNINIT);
2309 current_state->set_initialized (t.get_pc (),
2310 current_method->max_locals);
2313 type rt = compute_return_type (method_signature);
2321 type t = check_class_constant (get_ushort ());
2322 if (t.isarray () || t.isinterface () || t.isabstract ())
2323 verify_fail ("type is array, interface, or abstract");
2324 t.set_uninitialized (start_PC);
2331 int atype = get_byte ();
2332 // We intentionally have chosen constants to make this
2334 if (atype < boolean_type || atype > long_type)
2335 verify_fail ("type not primitive");
2336 pop_type (int_type);
2337 push_type (construct_primitive_array_type (type_val (atype)));
2341 pop_type (int_type);
2342 push_type (check_class_constant (get_ushort ()));
2344 case op_arraylength:
2346 type t = pop_type (reference_type);
2348 verify_fail ("array type expected");
2349 push_type (int_type);
2353 pop_type (type (&java::lang::Throwable::class$));
2357 pop_type (reference_type);
2358 push_type (check_class_constant (get_ushort ()));
2361 pop_type (reference_type);
2362 check_class_constant (get_ushort ());
2363 push_type (int_type);
2365 case op_monitorenter:
2366 pop_type (reference_type);
2368 case op_monitorexit:
2369 pop_type (reference_type);
2373 switch (get_byte ())
2376 push_type (get_variable (get_ushort (), int_type));
2379 push_type (get_variable (get_ushort (), long_type));
2382 push_type (get_variable (get_ushort (), float_type));
2385 push_type (get_variable (get_ushort (), double_type));
2388 push_type (get_variable (get_ushort (), reference_type));
2391 set_variable (get_ushort (), pop_type (int_type));
2394 set_variable (get_ushort (), pop_type (long_type));
2397 set_variable (get_ushort (), pop_type (float_type));
2400 set_variable (get_ushort (), pop_type (double_type));
2403 set_variable (get_ushort (), pop_type (reference_type));
2406 handle_ret_insn (get_short ());
2409 get_variable (get_ushort (), int_type);
2413 verify_fail ("unrecognized wide instruction");
2417 case op_multianewarray:
2419 type atype = check_class_constant (get_ushort ());
2420 int dim = get_byte ();
2422 verify_fail ("too few dimensions to multianewarray");
2423 atype.verify_dimensions (dim);
2424 for (int i = 0; i < dim; ++i)
2425 pop_type (int_type);
2431 pop_type (reference_type);
2432 push_jump (get_short ());
2435 push_jump (get_int ());
2439 handle_jsr_insn (get_int ());
2443 // Unrecognized opcode.
2444 verify_fail ("unrecognized instruction in verify_instructions_0");
2451 void verify_instructions ()
2454 verify_instructions_0 ();
2457 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2460 bytecode = m->bytecode ();
2461 exception = m->exceptions ();
2462 current_class = m->defining_class;
2469 ~_Jv_BytecodeVerifier ()
2476 _Jv_Free (jsr_ptrs);
2481 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
2483 _Jv_BytecodeVerifier v (meth);
2484 v.verify_instructions ();
2487 // FIXME: add more info, like PC, when required.
2489 verify_fail (char *s)
2492 strcpy (buf, "verification failed: ");
2494 throw new java::lang::VerifyError (JvNewStringLatin1 (buf));
2497 #endif /* INTERPRETER */