1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001, 2002 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 // Written by Tom Tromey <tromey@redhat.com>
13 // Define VERIFY_DEBUG to enable debugging output.
19 #include <java-insns.h>
20 #include <java-interp.h>
24 #include <java/lang/Class.h>
25 #include <java/lang/VerifyError.h>
26 #include <java/lang/Throwable.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/StringBuffer.h>
32 #endif /* VERIFY_DEBUG */
35 static void debug_print (const char *fmt, ...)
36 __attribute__ ((format (printf, 1, 2)));
39 debug_print (const char *fmt, ...)
44 vfprintf (stderr, fmt, ap);
46 #endif /* VERIFY_DEBUG */
49 class _Jv_BytecodeVerifier
53 static const int FLAG_INSN_START = 1;
54 static const int FLAG_BRANCH_TARGET = 2;
59 struct subr_entry_info;
64 // The PC corresponding to the start of the current instruction.
67 // The current state of the stack, locals, etc.
70 // We store the state at branch targets, for merging. This holds
74 // We keep a linked list of all the PCs which we must reverify.
75 // The link is done using the PC values. This is the head of the
79 // We keep some flags for each instruction. The values are the
80 // FLAG_* constants defined above.
83 // We need to keep track of which instructions can call a given
84 // subroutine. FIXME: this is inefficient. We keep a linked list
85 // of all calling `jsr's at at each jsr target.
88 // We keep a linked list of entries which map each `ret' instruction
89 // to its unique subroutine entry point. We expect that there won't
90 // be many `ret' instructions, so a linked list is ok.
91 subr_entry_info *entry_points;
93 // The bytecode itself.
94 unsigned char *bytecode;
96 _Jv_InterpException *exception;
101 _Jv_InterpMethod *current_method;
103 // A linked list of utf8 objects we allocate. This is really ugly,
104 // but without this our utf8 objects would be collected.
105 linked_utf8 *utf8_list;
113 _Jv_Utf8Const *make_utf8_const (char *s, int len)
115 _Jv_Utf8Const *val = _Jv_makeUtf8Const (s, len);
116 _Jv_Utf8Const *r = (_Jv_Utf8Const *) _Jv_Malloc (sizeof (_Jv_Utf8Const)
119 r->length = val->length;
121 memcpy (r->data, val->data, val->length + 1);
123 linked_utf8 *lu = (linked_utf8 *) _Jv_Malloc (sizeof (linked_utf8));
125 lu->next = utf8_list;
131 // This enum holds a list of tags for all the different types we
132 // need to handle. Reference types are treated specially by the
138 // The values for primitive types are chosen to correspond to values
139 // specified to newarray.
149 // Used when overwriting second word of a double or long in the
150 // local variables. Also used after merging local variable states
151 // to indicate an unusable value.
156 // There is an obscure special case which requires us to note when
157 // a local variable has not been used by a subroutine. See
158 // push_jump_merge for more information.
159 unused_by_subroutine_type,
161 // Everything after `reference_type' must be a reference type.
164 unresolved_reference_type,
165 uninitialized_reference_type,
166 uninitialized_unresolved_reference_type
169 // Return the type_val corresponding to a primitive signature
170 // character. For instance `I' returns `int.class'.
171 type_val get_type_val_for_signature (jchar sig)
204 verify_fail ("invalid signature");
209 // Return the type_val corresponding to a primitive class.
210 type_val get_type_val_for_signature (jclass k)
212 return get_type_val_for_signature ((jchar) k->method_count);
215 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
216 // TARGET haven't been prepared.
217 static bool is_assignable_from_slow (jclass target, jclass source)
219 // This will terminate when SOURCE==Object.
222 if (source == target)
225 if (target->isPrimitive () || source->isPrimitive ())
228 if (target->isArray ())
230 if (! source->isArray ())
232 target = target->getComponentType ();
233 source = source->getComponentType ();
235 else if (target->isInterface ())
237 for (int i = 0; i < source->interface_count; ++i)
239 // We use a recursive call because we also need to
240 // check superinterfaces.
241 if (is_assignable_from_slow (target, source->interfaces[i]))
244 source = source->getSuperclass ();
248 // We must do this check before we check to see if SOURCE is
249 // an interface. This way we know that any interface is
250 // assignable to an Object.
251 else if (target == &java::lang::Object::class$)
253 else if (source->isInterface ())
255 for (int i = 0; i < target->interface_count; ++i)
257 // We use a recursive call because we also need to
258 // check superinterfaces.
259 if (is_assignable_from_slow (target->interfaces[i], source))
262 target = target->getSuperclass ();
266 else if (source == &java::lang::Object::class$)
269 source = source->getSuperclass ();
273 // This is used to keep track of which `jsr's correspond to a given
277 // PC of the instruction just after the jsr.
283 // This is used to keep track of which subroutine entry point
284 // corresponds to which `ret' instruction.
285 struct subr_entry_info
287 // PC of the subroutine entry point.
289 // PC of the `ret' instruction.
292 subr_entry_info *next;
295 // The `type' class is used to represent a single type in the
301 // Some associated data.
304 // For a resolved reference type, this is a pointer to the class.
306 // For other reference types, this it the name of the class.
309 // This is used when constructing a new object. It is the PC of the
310 // `new' instruction which created the object. We use the special
311 // value -2 to mean that this is uninitialized, and the special
312 // value -1 for the case where the current method is itself the
316 static const int UNINIT = -2;
317 static const int SELF = -1;
319 // Basic constructor.
322 key = unsuitable_type;
327 // Make a new instance given the type tag. We assume a generic
328 // `reference_type' means Object.
333 if (key == reference_type)
334 data.klass = &java::lang::Object::class$;
338 // Make a new instance given a class.
341 key = reference_type;
346 // Make a new instance given the name of a class.
347 type (_Jv_Utf8Const *n)
349 key = unresolved_reference_type;
362 // These operators are required because libgcj can't link in
364 void *operator new[] (size_t bytes)
366 return _Jv_Malloc (bytes);
369 void operator delete[] (void *mem)
374 type& operator= (type_val k)
382 type& operator= (const type& t)
390 // Promote a numeric type.
393 if (key == boolean_type || key == char_type
394 || key == byte_type || key == short_type)
399 // If *THIS is an unresolved reference type, resolve it.
400 void resolve (_Jv_BytecodeVerifier *verifier)
402 if (key != unresolved_reference_type
403 && key != uninitialized_unresolved_reference_type)
406 using namespace java::lang;
407 java::lang::ClassLoader *loader
408 = verifier->current_class->getClassLoader();
409 // We might see either kind of name. Sigh.
410 if (data.name->data[0] == 'L'
411 && data.name->data[data.name->length - 1] == ';')
412 data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
414 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
416 key = (key == unresolved_reference_type
418 : uninitialized_reference_type);
421 // Mark this type as the uninitialized result of `new'.
422 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
424 if (key == reference_type)
425 key = uninitialized_reference_type;
426 else if (key == unresolved_reference_type)
427 key = uninitialized_unresolved_reference_type;
429 verifier->verify_fail ("internal error in type::uninitialized");
433 // Mark this type as now initialized.
434 void set_initialized (int npc)
436 if (npc != UNINIT && pc == npc
437 && (key == uninitialized_reference_type
438 || key == uninitialized_unresolved_reference_type))
440 key = (key == uninitialized_reference_type
442 : unresolved_reference_type);
448 // Return true if an object of type K can be assigned to a variable
449 // of type *THIS. Handle various special cases too. Might modify
450 // *THIS or K. Note however that this does not perform numeric
452 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
454 // Any type is compatible with the unsuitable type.
455 if (key == unsuitable_type)
458 if (key < reference_type || k.key < reference_type)
461 // The `null' type is convertible to any reference type.
462 if (key == null_type || k.key == null_type)
465 // Any reference type is convertible to Object. This is a special
466 // case so we don't need to unnecessarily resolve a class.
467 if (key == reference_type
468 && data.klass == &java::lang::Object::class$)
471 // An initialized type and an uninitialized type are not
473 if (isinitialized () != k.isinitialized ())
476 // Two uninitialized objects are compatible if either:
477 // * The PCs are identical, or
478 // * One PC is UNINIT.
479 if (! isinitialized ())
481 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
485 // Two unresolved types are equal if their names are the same.
488 && _Jv_equalUtf8Consts (data.name, k.data.name))
491 // We must resolve both types and check assignability.
493 k.resolve (verifier);
494 return is_assignable_from_slow (data.klass, k.data.klass);
499 return key == void_type;
504 return key == long_type || key == double_type;
507 // Return number of stack or local variable slots taken by this
511 return iswide () ? 2 : 1;
514 bool isarray () const
516 // We treat null_type as not an array. This is ok based on the
517 // current uses of this method.
518 if (key == reference_type)
519 return data.klass->isArray ();
520 else if (key == unresolved_reference_type)
521 return data.name->data[0] == '[';
527 return key == null_type;
530 bool isinterface (_Jv_BytecodeVerifier *verifier)
533 if (key != reference_type)
535 return data.klass->isInterface ();
538 bool isabstract (_Jv_BytecodeVerifier *verifier)
541 if (key != reference_type)
543 using namespace java::lang::reflect;
544 return Modifier::isAbstract (data.klass->getModifiers ());
547 // Return the element type of an array.
548 type element_type (_Jv_BytecodeVerifier *verifier)
550 // FIXME: maybe should do string manipulation here.
552 if (key != reference_type)
553 verifier->verify_fail ("programmer error in type::element_type()", -1);
555 jclass k = data.klass->getComponentType ();
556 if (k->isPrimitive ())
557 return type (verifier->get_type_val_for_signature (k));
561 // Return the array type corresponding to an initialized
562 // reference. We could expand this to work for other kinds of
563 // types, but currently we don't need to.
564 type to_array (_Jv_BytecodeVerifier *verifier)
566 // Resolving isn't ideal, because it might force us to load
567 // another class, but it's easy. FIXME?
568 if (key == unresolved_reference_type)
571 if (key == reference_type)
572 return type (_Jv_GetArrayClass (data.klass,
573 data.klass->getClassLoader ()));
575 verifier->verify_fail ("internal error in type::to_array()");
578 bool isreference () const
580 return key >= reference_type;
588 bool isinitialized () const
590 return (key == reference_type
592 || key == unresolved_reference_type);
595 bool isresolved () const
597 return (key == reference_type
599 || key == uninitialized_reference_type);
602 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
604 // The way this is written, we don't need to check isarray().
605 if (key == reference_type)
607 jclass k = data.klass;
608 while (k->isArray () && ndims > 0)
610 k = k->getComponentType ();
616 // We know KEY == unresolved_reference_type.
617 char *p = data.name->data;
618 while (*p++ == '[' && ndims-- > 0)
623 verifier->verify_fail ("array type has fewer dimensions than required");
626 // Merge OLD_TYPE into this. On error throw exception.
627 bool merge (type& old_type, bool local_semantics,
628 _Jv_BytecodeVerifier *verifier)
630 bool changed = false;
631 bool refo = old_type.isreference ();
632 bool refn = isreference ();
635 if (old_type.key == null_type)
637 else if (key == null_type)
642 else if (isinitialized () != old_type.isinitialized ())
643 verifier->verify_fail ("merging initialized and uninitialized types");
646 if (! isinitialized ())
650 else if (old_type.pc == UNINIT)
652 else if (pc != old_type.pc)
653 verifier->verify_fail ("merging different uninitialized types");
657 && ! old_type.isresolved ()
658 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
660 // Types are identical.
665 old_type.resolve (verifier);
667 jclass k = data.klass;
668 jclass oldk = old_type.data.klass;
671 while (k->isArray () && oldk->isArray ())
674 k = k->getComponentType ();
675 oldk = oldk->getComponentType ();
678 // Ordinarily this terminates when we hit Object...
681 if (is_assignable_from_slow (k, oldk))
683 k = k->getSuperclass ();
686 // ... but K could have been an interface, in which
687 // case we'll end up here. We just convert this
690 k = &java::lang::Object::class$;
694 while (arraycount > 0)
696 java::lang::ClassLoader *loader
697 = verifier->current_class->getClassLoader();
698 k = _Jv_GetArrayClass (k, loader);
706 else if (refo || refn || key != old_type.key)
710 // If we're merging into an "unused" slot, then we
711 // simply accept whatever we're merging from.
712 if (key == unused_by_subroutine_type)
717 else if (old_type.key == unused_by_subroutine_type)
721 // If we already have an `unsuitable' type, then we
722 // don't need to change again.
723 else if (key != unsuitable_type)
725 key = unsuitable_type;
730 verifier->verify_fail ("unmergeable type");
736 void print (void) const
741 case boolean_type: c = 'Z'; break;
742 case byte_type: c = 'B'; break;
743 case char_type: c = 'C'; break;
744 case short_type: c = 'S'; break;
745 case int_type: c = 'I'; break;
746 case long_type: c = 'J'; break;
747 case float_type: c = 'F'; break;
748 case double_type: c = 'D'; break;
749 case void_type: c = 'V'; break;
750 case unsuitable_type: c = '-'; break;
751 case return_address_type: c = 'r'; break;
752 case continuation_type: c = '+'; break;
753 case unused_by_subroutine_type: c = '_'; break;
754 case reference_type: c = 'L'; break;
755 case null_type: c = '@'; break;
756 case unresolved_reference_type: c = 'l'; break;
757 case uninitialized_reference_type: c = 'U'; break;
758 case uninitialized_unresolved_reference_type: c = 'u'; break;
760 debug_print ("%c", c);
762 #endif /* VERIFY_DEBUG */
765 // This class holds all the state information we need for a given
769 // The current top of the stack, in terms of slots.
771 // The current depth of the stack. This will be larger than
772 // STACKTOP when wide types are on the stack.
776 // The local variables.
778 // This is used in subroutines to keep track of which local
779 // variables have been accessed.
781 // If not 0, then we are in a subroutine. The value is the PC of
782 // the subroutine's entry point. We can use 0 as an exceptional
783 // value because PC=0 can never be a subroutine.
785 // This is used to keep a linked list of all the states which
786 // require re-verification. We use the PC to keep track.
788 // We keep track of the type of `this' specially. This is used to
789 // ensure that an instance initializer invokes another initializer
790 // on `this' before returning. We must keep track of this
791 // specially because otherwise we might be confused by code which
792 // assigns to locals[0] (overwriting `this') and then returns
793 // without really initializing.
796 // INVALID marks a state which is not on the linked list of states
797 // requiring reverification.
798 static const int INVALID = -1;
799 // NO_NEXT marks the state at the end of the reverification list.
800 static const int NO_NEXT = -2;
802 // This is used to mark the stack depth at the instruction just
803 // after a `jsr' when we haven't yet processed the corresponding
804 // `ret'. See handle_jsr_insn for more information.
805 static const int NO_STACK = -1;
812 local_changed = NULL;
815 state (int max_stack, int max_locals)
820 stack = new type[max_stack];
821 for (int i = 0; i < max_stack; ++i)
822 stack[i] = unsuitable_type;
823 locals = new type[max_locals];
824 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
825 for (int i = 0; i < max_locals; ++i)
827 locals[i] = unsuitable_type;
828 local_changed[i] = false;
834 state (const state *orig, int max_stack, int max_locals,
835 bool ret_semantics = false)
837 stack = new type[max_stack];
838 locals = new type[max_locals];
839 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
840 copy (orig, max_stack, max_locals, ret_semantics);
851 _Jv_Free (local_changed);
854 void *operator new[] (size_t bytes)
856 return _Jv_Malloc (bytes);
859 void operator delete[] (void *mem)
864 void *operator new (size_t bytes)
866 return _Jv_Malloc (bytes);
869 void operator delete (void *mem)
874 void copy (const state *copy, int max_stack, int max_locals,
875 bool ret_semantics = false)
877 stacktop = copy->stacktop;
878 stackdepth = copy->stackdepth;
879 subroutine = copy->subroutine;
880 for (int i = 0; i < max_stack; ++i)
881 stack[i] = copy->stack[i];
882 for (int i = 0; i < max_locals; ++i)
884 // See push_jump_merge to understand this case.
886 locals[i] = type (copy->local_changed[i]
888 : unused_by_subroutine_type);
890 locals[i] = copy->locals[i];
891 local_changed[i] = copy->local_changed[i];
893 this_type = copy->this_type;
894 // Don't modify `next'.
897 // Modify this state to reflect entry to an exception handler.
898 void set_exception (type t, int max_stack)
903 for (int i = stacktop; i < max_stack; ++i)
904 stack[i] = unsuitable_type;
907 // Modify this state to reflect entry into a subroutine.
908 void enter_subroutine (int npc, int max_locals)
911 // Mark all items as unchanged. Each subroutine needs to keep
912 // track of its `changed' state independently. In the case of
913 // nested subroutines, this information will be merged back into
914 // parent by the `ret'.
915 for (int i = 0; i < max_locals; ++i)
916 local_changed[i] = false;
919 // Merge STATE_OLD into this state. Destructively modifies this
920 // state. Returns true if the new state was in fact changed.
921 // Will throw an exception if the states are not mergeable.
922 bool merge (state *state_old, bool ret_semantics,
923 int max_locals, _Jv_BytecodeVerifier *verifier)
925 bool changed = false;
927 // Special handling for `this'. If one or the other is
928 // uninitialized, then the merge is uninitialized.
929 if (this_type.isinitialized ())
930 this_type = state_old->this_type;
932 // Merge subroutine states. Here we just keep track of what
933 // subroutine we think we're in. We only check for a merge
934 // (which is invalid) when we see a `ret'.
935 if (subroutine == state_old->subroutine)
939 else if (subroutine == 0)
941 subroutine = state_old->subroutine;
946 // If the subroutines differ, indicate that the state
947 // changed. This is needed to detect when subroutines have
952 // Merge stacks. Special handling for NO_STACK case.
953 if (state_old->stacktop == NO_STACK)
955 // Nothing to do in this case; we don't care about modifying
958 else if (stacktop == NO_STACK)
960 stacktop = state_old->stacktop;
961 stackdepth = state_old->stackdepth;
962 for (int i = 0; i < stacktop; ++i)
963 stack[i] = state_old->stack[i];
966 else if (state_old->stacktop != stacktop)
967 verifier->verify_fail ("stack sizes differ");
970 for (int i = 0; i < state_old->stacktop; ++i)
972 if (stack[i].merge (state_old->stack[i], false, verifier))
977 // Merge local variables.
978 for (int i = 0; i < max_locals; ++i)
980 // If we're not processing a `ret', then we merge every
981 // local variable. If we are processing a `ret', then we
982 // only merge locals which changed in the subroutine. When
983 // processing a `ret', STATE_OLD is the state at the point
984 // of the `ret', and THIS is the state just after the `jsr'.
985 if (! ret_semantics || state_old->local_changed[i])
987 if (locals[i].merge (state_old->locals[i], true, verifier))
989 // Note that we don't call `note_variable' here.
990 // This change doesn't represent a real change to a
991 // local, but rather a merge artifact. If we're in
992 // a subroutine which is called with two
993 // incompatible types in a slot that is unused by
994 // the subroutine, then we don't want to mark that
995 // variable as having been modified.
1000 // If we're in a subroutine, we must compute the union of
1001 // all the changed local variables.
1002 if (state_old->local_changed[i])
1009 // Throw an exception if there is an uninitialized object on the
1010 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1011 // whether we're using backwards-branch or exception-handing
1013 void check_no_uninitialized_objects (int max_locals,
1014 _Jv_BytecodeVerifier *verifier,
1015 bool exception_semantics = false)
1017 if (! exception_semantics)
1019 for (int i = 0; i < stacktop; ++i)
1020 if (stack[i].isreference () && ! stack[i].isinitialized ())
1021 verifier->verify_fail ("uninitialized object on stack");
1024 for (int i = 0; i < max_locals; ++i)
1025 if (locals[i].isreference () && ! locals[i].isinitialized ())
1026 verifier->verify_fail ("uninitialized object in local variable");
1028 check_this_initialized (verifier);
1031 // Ensure that `this' has been initialized.
1032 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1034 if (this_type.isreference () && ! this_type.isinitialized ())
1035 verifier->verify_fail ("`this' is uninitialized");
1038 // Set type of `this'.
1039 void set_this_type (const type &k)
1044 // Note that a local variable was modified.
1045 void note_variable (int index)
1048 local_changed[index] = true;
1051 // Mark each `new'd object we know of that was allocated at PC as
1053 void set_initialized (int pc, int max_locals)
1055 for (int i = 0; i < stacktop; ++i)
1056 stack[i].set_initialized (pc);
1057 for (int i = 0; i < max_locals; ++i)
1058 locals[i].set_initialized (pc);
1059 this_type.set_initialized (pc);
1062 // Return true if this state is the unmerged result of a `ret'.
1063 bool is_unmerged_ret_state (int max_locals) const
1065 if (stacktop == NO_STACK)
1067 for (int i = 0; i < max_locals; ++i)
1069 if (locals[i].key == unused_by_subroutine_type)
1076 void print (const char *leader, int pc,
1077 int max_stack, int max_locals) const
1079 debug_print ("%s [%4d]: [stack] ", leader, pc);
1081 for (i = 0; i < stacktop; ++i)
1083 for (; i < max_stack; ++i)
1085 debug_print (" [local] ");
1086 for (i = 0; i < max_locals; ++i)
1089 debug_print (local_changed[i] ? "+" : " ");
1091 if (subroutine == 0)
1092 debug_print (" | None");
1094 debug_print (" | %4d", subroutine);
1095 debug_print (" | %p\n", this);
1098 inline void print (const char *, int, int, int) const
1101 #endif /* VERIFY_DEBUG */
1106 if (current_state->stacktop <= 0)
1107 verify_fail ("stack empty");
1108 type r = current_state->stack[--current_state->stacktop];
1109 current_state->stackdepth -= r.depth ();
1110 if (current_state->stackdepth < 0)
1111 verify_fail ("stack empty", start_PC);
1117 type r = pop_raw ();
1119 verify_fail ("narrow pop of wide type");
1125 type r = pop_raw ();
1127 verify_fail ("wide pop of narrow type");
1131 type pop_type (type match)
1134 type t = pop_raw ();
1135 if (! match.compatible (t, this))
1136 verify_fail ("incompatible type on stack");
1140 // Pop a reference type or a return address.
1141 type pop_ref_or_return ()
1143 type t = pop_raw ();
1144 if (! t.isreference () && t.key != return_address_type)
1145 verify_fail ("expected reference or return address on stack");
1149 void push_type (type t)
1151 // If T is a numeric type like short, promote it to int.
1154 int depth = t.depth ();
1155 if (current_state->stackdepth + depth > current_method->max_stack)
1156 verify_fail ("stack overflow");
1157 current_state->stack[current_state->stacktop++] = t;
1158 current_state->stackdepth += depth;
1161 void set_variable (int index, type t)
1163 // If T is a numeric type like short, promote it to int.
1166 int depth = t.depth ();
1167 if (index > current_method->max_locals - depth)
1168 verify_fail ("invalid local variable");
1169 current_state->locals[index] = t;
1170 current_state->note_variable (index);
1174 current_state->locals[index + 1] = continuation_type;
1175 current_state->note_variable (index + 1);
1177 if (index > 0 && current_state->locals[index - 1].iswide ())
1179 current_state->locals[index - 1] = unsuitable_type;
1180 // There's no need to call note_variable here.
1184 type get_variable (int index, type t)
1186 int depth = t.depth ();
1187 if (index > current_method->max_locals - depth)
1188 verify_fail ("invalid local variable");
1189 if (! t.compatible (current_state->locals[index], this))
1190 verify_fail ("incompatible type in local variable");
1193 type t (continuation_type);
1194 if (! current_state->locals[index + 1].compatible (t, this))
1195 verify_fail ("invalid local variable");
1197 return current_state->locals[index];
1200 // Make sure ARRAY is an array type and that its elements are
1201 // compatible with type ELEMENT. Returns the actual element type.
1202 type require_array_type (type array, type element)
1204 // An odd case. Here we just pretend that everything went ok. If
1205 // the requested element type is some kind of reference, return
1206 // the null type instead.
1207 if (array.isnull ())
1208 return element.isreference () ? type (null_type) : element;
1210 if (! array.isarray ())
1211 verify_fail ("array required");
1213 type t = array.element_type (this);
1214 if (! element.compatible (t, this))
1216 // Special case for byte arrays, which must also be boolean
1219 if (element.key == byte_type)
1221 type e2 (boolean_type);
1222 ok = e2.compatible (t, this);
1225 verify_fail ("incompatible array element type");
1228 // Return T and not ELEMENT, because T might be specialized.
1234 if (PC >= current_method->code_length)
1235 verify_fail ("premature end of bytecode");
1236 return (jint) bytecode[PC++] & 0xff;
1241 jint b1 = get_byte ();
1242 jint b2 = get_byte ();
1243 return (jint) ((b1 << 8) | b2) & 0xffff;
1248 jint b1 = get_byte ();
1249 jint b2 = get_byte ();
1250 jshort s = (b1 << 8) | b2;
1256 jint b1 = get_byte ();
1257 jint b2 = get_byte ();
1258 jint b3 = get_byte ();
1259 jint b4 = get_byte ();
1260 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1263 int compute_jump (int offset)
1265 int npc = start_PC + offset;
1266 if (npc < 0 || npc >= current_method->code_length)
1267 verify_fail ("branch out of range", start_PC);
1271 // Merge the indicated state into the state at the branch target and
1272 // schedule a new PC if there is a change. If RET_SEMANTICS is
1273 // true, then we are merging from a `ret' instruction into the
1274 // instruction after a `jsr'. This is a special case with its own
1275 // modified semantics.
1276 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1278 bool changed = true;
1279 if (states[npc] == NULL)
1281 // There's a weird situation here. If are examining the
1282 // branch that results from a `ret', and there is not yet a
1283 // state available at the branch target (the instruction just
1284 // after the `jsr'), then we have to construct a special kind
1285 // of state at that point for future merging. This special
1286 // state has the type `unused_by_subroutine_type' in each slot
1287 // which was not modified by the subroutine.
1288 states[npc] = new state (nstate, current_method->max_stack,
1289 current_method->max_locals, ret_semantics);
1290 debug_print ("== New state in push_jump_merge\n");
1291 states[npc]->print ("New", npc, current_method->max_stack,
1292 current_method->max_locals);
1296 debug_print ("== Merge states in push_jump_merge\n");
1297 nstate->print ("Frm", start_PC, current_method->max_stack,
1298 current_method->max_locals);
1299 states[npc]->print (" To", npc, current_method->max_stack,
1300 current_method->max_locals);
1301 changed = states[npc]->merge (nstate, ret_semantics,
1302 current_method->max_locals, this);
1303 states[npc]->print ("New", npc, current_method->max_stack,
1304 current_method->max_locals);
1307 if (changed && states[npc]->next == state::INVALID)
1309 // The merge changed the state, and the new PC isn't yet on our
1310 // list of PCs to re-verify.
1311 states[npc]->next = next_verify_pc;
1312 next_verify_pc = npc;
1316 void push_jump (int offset)
1318 int npc = compute_jump (offset);
1320 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1321 push_jump_merge (npc, current_state);
1324 void push_exception_jump (type t, int pc)
1326 current_state->check_no_uninitialized_objects (current_method->max_locals,
1328 state s (current_state, current_method->max_stack,
1329 current_method->max_locals);
1330 if (current_method->max_stack < 1)
1331 verify_fail ("stack overflow at exception handler");
1332 s.set_exception (t, current_method->max_stack);
1333 push_jump_merge (pc, &s);
1338 int *prev_loc = &next_verify_pc;
1339 int npc = next_verify_pc;
1340 bool skipped = false;
1342 while (npc != state::NO_NEXT)
1344 // If the next available PC is an unmerged `ret' state, then
1345 // we aren't yet ready to handle it. That's because we would
1346 // need all kind of special cases to do so. So instead we
1347 // defer this jump until after we've processed it via a
1348 // fall-through. This has to happen because the instruction
1349 // before this one must be a `jsr'.
1350 if (! states[npc]->is_unmerged_ret_state (current_method->max_locals))
1352 *prev_loc = states[npc]->next;
1353 states[npc]->next = state::INVALID;
1358 prev_loc = &states[npc]->next;
1359 npc = states[npc]->next;
1362 // Note that we might have gotten here even when there are
1363 // remaining states to process. That can happen if we find a
1364 // `jsr' without a `ret'.
1365 return state::NO_NEXT;
1368 void invalidate_pc ()
1370 PC = state::NO_NEXT;
1373 void note_branch_target (int pc, bool is_jsr_target = false)
1375 // Don't check `pc <= PC', because we've advanced PC after
1376 // fetching the target and we haven't yet checked the next
1378 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1379 verify_fail ("branch not to instruction start", start_PC);
1380 flags[pc] |= FLAG_BRANCH_TARGET;
1383 // Record the jsr which called this instruction.
1384 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1386 info->next = jsr_ptrs[pc];
1387 jsr_ptrs[pc] = info;
1391 void skip_padding ()
1393 while ((PC % 4) > 0)
1394 if (get_byte () != 0)
1395 verify_fail ("found nonzero padding byte");
1398 // Return the subroutine to which the instruction at PC belongs.
1399 int get_subroutine (int pc)
1401 if (states[pc] == NULL)
1403 return states[pc]->subroutine;
1406 // Do the work for a `ret' instruction. INDEX is the index into the
1408 void handle_ret_insn (int index)
1410 get_variable (index, return_address_type);
1412 int csub = current_state->subroutine;
1414 verify_fail ("no subroutine");
1416 // Check to see if we've merged subroutines.
1417 subr_entry_info *entry;
1418 for (entry = entry_points; entry != NULL; entry = entry->next)
1420 if (entry->ret_pc == start_PC)
1425 entry = (subr_entry_info *) _Jv_Malloc (sizeof (subr_entry_info));
1427 entry->ret_pc = start_PC;
1428 entry->next = entry_points;
1429 entry_points = entry;
1431 else if (entry->pc != csub)
1432 verify_fail ("subroutines merged");
1434 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1436 // Temporarily modify the current state so it looks like we're
1437 // in the enclosing context.
1438 current_state->subroutine = get_subroutine (subr->pc);
1440 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1441 push_jump_merge (subr->pc, current_state, true);
1444 current_state->subroutine = csub;
1448 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1449 // sure this subroutine isn't already on the stack.
1450 void check_nonrecursive_call (int sub, int dest)
1455 verify_fail ("recursive subroutine call");
1456 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1457 check_nonrecursive_call (get_subroutine (info->pc), dest);
1460 void handle_jsr_insn (int offset)
1462 int npc = compute_jump (offset);
1465 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1466 check_nonrecursive_call (current_state->subroutine, npc);
1468 // Modify our state as appropriate for entry into a subroutine.
1469 push_type (return_address_type);
1470 push_jump_merge (npc, current_state);
1472 pop_type (return_address_type);
1474 // On entry to the subroutine, the subroutine number must be set
1475 // and the locals must be marked as cleared. We do this after
1476 // merging state so that we don't erroneously "notice" a variable
1477 // change merely on entry.
1478 states[npc]->enter_subroutine (npc, current_method->max_locals);
1480 // Indicate that we don't know the stack depth of the instruction
1481 // following the `jsr'. The idea here is that we need to merge
1482 // the local variable state across the jsr, but the subroutine
1483 // might change the stack depth, so we can't make any assumptions
1484 // about it. So we have yet another special case. We know that
1485 // at this point PC points to the instruction after the jsr.
1487 // FIXME: what if we have a jsr at the end of the code, but that
1488 // jsr has no corresponding ret? Is this verifiable, or is it
1489 // not? If it is then we need a special case here.
1490 if (PC >= current_method->code_length)
1491 verify_fail ("fell off end");
1493 current_state->stacktop = state::NO_STACK;
1494 push_jump_merge (PC, current_state);
1498 jclass construct_primitive_array_type (type_val prim)
1504 k = JvPrimClass (boolean);
1507 k = JvPrimClass (char);
1510 k = JvPrimClass (float);
1513 k = JvPrimClass (double);
1516 k = JvPrimClass (byte);
1519 k = JvPrimClass (short);
1522 k = JvPrimClass (int);
1525 k = JvPrimClass (long);
1528 // These aren't used here but we call them out to avoid
1531 case unsuitable_type:
1532 case return_address_type:
1533 case continuation_type:
1534 case unused_by_subroutine_type:
1535 case reference_type:
1537 case unresolved_reference_type:
1538 case uninitialized_reference_type:
1539 case uninitialized_unresolved_reference_type:
1541 verify_fail ("unknown type in construct_primitive_array_type");
1543 k = _Jv_GetArrayClass (k, NULL);
1547 // This pass computes the location of branch targets and also
1548 // instruction starts.
1549 void branch_prepass ()
1551 flags = (char *) _Jv_Malloc (current_method->code_length);
1552 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1553 * current_method->code_length);
1555 for (int i = 0; i < current_method->code_length; ++i)
1561 bool last_was_jsr = false;
1564 while (PC < current_method->code_length)
1566 // Set `start_PC' early so that error checking can have the
1569 flags[PC] |= FLAG_INSN_START;
1571 // If the previous instruction was a jsr, then the next
1572 // instruction is a branch target -- the branch being the
1573 // corresponding `ret'.
1575 note_branch_target (PC);
1576 last_was_jsr = false;
1578 java_opcode opcode = (java_opcode) bytecode[PC++];
1582 case op_aconst_null:
1718 case op_monitorenter:
1719 case op_monitorexit:
1727 case op_arraylength:
1759 case op_invokespecial:
1760 case op_invokestatic:
1761 case op_invokevirtual:
1765 case op_multianewarray:
1771 last_was_jsr = true;
1790 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1793 case op_tableswitch:
1796 note_branch_target (compute_jump (get_int ()));
1797 jint low = get_int ();
1798 jint hi = get_int ();
1800 verify_fail ("invalid tableswitch", start_PC);
1801 for (int i = low; i <= hi; ++i)
1802 note_branch_target (compute_jump (get_int ()));
1806 case op_lookupswitch:
1809 note_branch_target (compute_jump (get_int ()));
1810 int npairs = get_int ();
1812 verify_fail ("too few pairs in lookupswitch", start_PC);
1813 while (npairs-- > 0)
1816 note_branch_target (compute_jump (get_int ()));
1821 case op_invokeinterface:
1829 opcode = (java_opcode) get_byte ();
1831 if (opcode == op_iinc)
1837 last_was_jsr = true;
1840 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1843 // These are unused here, but we call them out explicitly
1844 // so that -Wswitch-enum doesn't complain.
1850 case op_putstatic_1:
1851 case op_putstatic_2:
1852 case op_putstatic_4:
1853 case op_putstatic_8:
1854 case op_putstatic_a:
1856 case op_getfield_2s:
1857 case op_getfield_2u:
1861 case op_getstatic_1:
1862 case op_getstatic_2s:
1863 case op_getstatic_2u:
1864 case op_getstatic_4:
1865 case op_getstatic_8:
1866 case op_getstatic_a:
1868 verify_fail ("unrecognized instruction in branch_prepass",
1872 // See if any previous branch tried to branch to the middle of
1873 // this instruction.
1874 for (int pc = start_PC + 1; pc < PC; ++pc)
1876 if ((flags[pc] & FLAG_BRANCH_TARGET))
1877 verify_fail ("branch to middle of instruction", pc);
1881 // Verify exception handlers.
1882 for (int i = 0; i < current_method->exc_count; ++i)
1884 if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
1885 verify_fail ("exception handler not at instruction start",
1886 exception[i].handler_pc.i);
1887 if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
1888 verify_fail ("exception start not at instruction start",
1889 exception[i].start_pc.i);
1890 if (exception[i].end_pc.i != current_method->code_length
1891 && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
1892 verify_fail ("exception end not at instruction start",
1893 exception[i].end_pc.i);
1895 flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
1899 void check_pool_index (int index)
1901 if (index < 0 || index >= current_class->constants.size)
1902 verify_fail ("constant pool index out of range", start_PC);
1905 type check_class_constant (int index)
1907 check_pool_index (index);
1908 _Jv_Constants *pool = ¤t_class->constants;
1909 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1910 return type (pool->data[index].clazz);
1911 else if (pool->tags[index] == JV_CONSTANT_Class)
1912 return type (pool->data[index].utf8);
1913 verify_fail ("expected class constant", start_PC);
1916 type check_constant (int index)
1918 check_pool_index (index);
1919 _Jv_Constants *pool = ¤t_class->constants;
1920 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1921 || pool->tags[index] == JV_CONSTANT_String)
1922 return type (&java::lang::String::class$);
1923 else if (pool->tags[index] == JV_CONSTANT_Integer)
1924 return type (int_type);
1925 else if (pool->tags[index] == JV_CONSTANT_Float)
1926 return type (float_type);
1927 verify_fail ("String, int, or float constant expected", start_PC);
1930 type check_wide_constant (int index)
1932 check_pool_index (index);
1933 _Jv_Constants *pool = ¤t_class->constants;
1934 if (pool->tags[index] == JV_CONSTANT_Long)
1935 return type (long_type);
1936 else if (pool->tags[index] == JV_CONSTANT_Double)
1937 return type (double_type);
1938 verify_fail ("long or double constant expected", start_PC);
1941 // Helper for both field and method. These are laid out the same in
1942 // the constant pool.
1943 type handle_field_or_method (int index, int expected,
1944 _Jv_Utf8Const **name,
1945 _Jv_Utf8Const **fmtype)
1947 check_pool_index (index);
1948 _Jv_Constants *pool = ¤t_class->constants;
1949 if (pool->tags[index] != expected)
1950 verify_fail ("didn't see expected constant", start_PC);
1951 // Once we know we have a Fieldref or Methodref we assume that it
1952 // is correctly laid out in the constant pool. I think the code
1953 // in defineclass.cc guarantees this.
1954 _Jv_ushort class_index, name_and_type_index;
1955 _Jv_loadIndexes (&pool->data[index],
1957 name_and_type_index);
1958 _Jv_ushort name_index, desc_index;
1959 _Jv_loadIndexes (&pool->data[name_and_type_index],
1960 name_index, desc_index);
1962 *name = pool->data[name_index].utf8;
1963 *fmtype = pool->data[desc_index].utf8;
1965 return check_class_constant (class_index);
1968 // Return field's type, compute class' type if requested.
1969 type check_field_constant (int index, type *class_type = NULL)
1971 _Jv_Utf8Const *name, *field_type;
1972 type ct = handle_field_or_method (index,
1973 JV_CONSTANT_Fieldref,
1974 &name, &field_type);
1977 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
1978 return type (field_type);
1979 return get_type_val_for_signature (field_type->data[0]);
1982 type check_method_constant (int index, bool is_interface,
1983 _Jv_Utf8Const **method_name,
1984 _Jv_Utf8Const **method_signature)
1986 return handle_field_or_method (index,
1988 ? JV_CONSTANT_InterfaceMethodref
1989 : JV_CONSTANT_Methodref),
1990 method_name, method_signature);
1993 type get_one_type (char *&p)
2011 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
2015 // Casting to jchar here is ok since we are looking at an ASCII
2017 type_val rt = get_type_val_for_signature (jchar (v));
2019 if (arraycount == 0)
2021 // Callers of this function eventually push their arguments on
2022 // the stack. So, promote them here.
2023 return type (rt).promote ();
2026 jclass k = construct_primitive_array_type (rt);
2027 while (--arraycount > 0)
2028 k = _Jv_GetArrayClass (k, NULL);
2032 void compute_argument_types (_Jv_Utf8Const *signature,
2035 char *p = signature->data;
2041 types[i++] = get_one_type (p);
2044 type compute_return_type (_Jv_Utf8Const *signature)
2046 char *p = signature->data;
2050 return get_one_type (p);
2053 void check_return_type (type onstack)
2055 type rt = compute_return_type (current_method->self->signature);
2056 if (! rt.compatible (onstack, this))
2057 verify_fail ("incompatible return type");
2060 // Initialize the stack for the new method. Returns true if this
2061 // method is an instance initializer.
2062 bool initialize_stack ()
2065 bool is_init = false;
2067 using namespace java::lang::reflect;
2068 if (! Modifier::isStatic (current_method->self->accflags))
2070 type kurr (current_class);
2071 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
2073 kurr.set_uninitialized (type::SELF, this);
2076 set_variable (0, kurr);
2077 current_state->set_this_type (kurr);
2081 // We have to handle wide arguments specially here.
2082 int arg_count = _Jv_count_arguments (current_method->self->signature);
2083 type arg_types[arg_count];
2084 compute_argument_types (current_method->self->signature, arg_types);
2085 for (int i = 0; i < arg_count; ++i)
2087 set_variable (var, arg_types[i]);
2089 if (arg_types[i].iswide ())
2096 void verify_instructions_0 ()
2098 current_state = new state (current_method->max_stack,
2099 current_method->max_locals);
2104 // True if we are verifying an instance initializer.
2105 bool this_is_init = initialize_stack ();
2107 states = (state **) _Jv_Malloc (sizeof (state *)
2108 * current_method->code_length);
2109 for (int i = 0; i < current_method->code_length; ++i)
2112 next_verify_pc = state::NO_NEXT;
2116 // If the PC was invalidated, get a new one from the work list.
2117 if (PC == state::NO_NEXT)
2120 if (PC == state::INVALID)
2121 verify_fail ("can't happen: saw state::INVALID");
2122 if (PC == state::NO_NEXT)
2124 debug_print ("== State pop from pending list\n");
2125 // Set up the current state.
2126 current_state->copy (states[PC], current_method->max_stack,
2127 current_method->max_locals);
2131 // Control can't fall off the end of the bytecode. We
2132 // only need to check this in the fall-through case,
2133 // because branch bounds are checked when they are
2135 if (PC >= current_method->code_length)
2136 verify_fail ("fell off end");
2138 // We only have to do this checking in the situation where
2139 // control flow falls through from the previous
2140 // instruction. Otherwise merging is done at the time we
2142 if (states[PC] != NULL)
2144 // We've already visited this instruction. So merge
2145 // the states together. If this yields no change then
2146 // we don't have to re-verify. However, if the new
2147 // state is an the result of an unmerged `ret', we
2148 // must continue through it.
2149 debug_print ("== Fall through merge\n");
2150 states[PC]->print ("Old", PC, current_method->max_stack,
2151 current_method->max_locals);
2152 current_state->print ("Cur", PC, current_method->max_stack,
2153 current_method->max_locals);
2154 if (! current_state->merge (states[PC], false,
2155 current_method->max_locals, this)
2156 && ! states[PC]->is_unmerged_ret_state (current_method->max_locals))
2158 debug_print ("== Fall through optimization\n");
2162 // Save a copy of it for later.
2163 states[PC]->copy (current_state, current_method->max_stack,
2164 current_method->max_locals);
2165 current_state->print ("New", PC, current_method->max_stack,
2166 current_method->max_locals);
2170 // We only have to keep saved state at branch targets. If
2171 // we're at a branch target and the state here hasn't been set
2172 // yet, we set it now.
2173 if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
2175 states[PC] = new state (current_state, current_method->max_stack,
2176 current_method->max_locals);
2179 // Set this before handling exceptions so that debug output is
2183 // Update states for all active exception handlers. Ordinarily
2184 // there are not many exception handlers. So we simply run
2185 // through them all.
2186 for (int i = 0; i < current_method->exc_count; ++i)
2188 if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
2190 type handler (&java::lang::Throwable::class$);
2191 if (exception[i].handler_type.i != 0)
2192 handler = check_class_constant (exception[i].handler_type.i);
2193 push_exception_jump (handler, exception[i].handler_pc.i);
2197 current_state->print (" ", PC, current_method->max_stack,
2198 current_method->max_locals);
2199 java_opcode opcode = (java_opcode) bytecode[PC++];
2205 case op_aconst_null:
2206 push_type (null_type);
2216 push_type (int_type);
2221 push_type (long_type);
2227 push_type (float_type);
2232 push_type (double_type);
2237 push_type (int_type);
2242 push_type (int_type);
2246 push_type (check_constant (get_byte ()));
2249 push_type (check_constant (get_ushort ()));
2252 push_type (check_wide_constant (get_ushort ()));
2256 push_type (get_variable (get_byte (), int_type));
2259 push_type (get_variable (get_byte (), long_type));
2262 push_type (get_variable (get_byte (), float_type));
2265 push_type (get_variable (get_byte (), double_type));
2268 push_type (get_variable (get_byte (), reference_type));
2275 push_type (get_variable (opcode - op_iload_0, int_type));
2281 push_type (get_variable (opcode - op_lload_0, long_type));
2287 push_type (get_variable (opcode - op_fload_0, float_type));
2293 push_type (get_variable (opcode - op_dload_0, double_type));
2299 push_type (get_variable (opcode - op_aload_0, reference_type));
2302 pop_type (int_type);
2303 push_type (require_array_type (pop_type (reference_type),
2307 pop_type (int_type);
2308 push_type (require_array_type (pop_type (reference_type),
2312 pop_type (int_type);
2313 push_type (require_array_type (pop_type (reference_type),
2317 pop_type (int_type);
2318 push_type (require_array_type (pop_type (reference_type),
2322 pop_type (int_type);
2323 push_type (require_array_type (pop_type (reference_type),
2327 pop_type (int_type);
2328 require_array_type (pop_type (reference_type), byte_type);
2329 push_type (int_type);
2332 pop_type (int_type);
2333 require_array_type (pop_type (reference_type), char_type);
2334 push_type (int_type);
2337 pop_type (int_type);
2338 require_array_type (pop_type (reference_type), short_type);
2339 push_type (int_type);
2342 set_variable (get_byte (), pop_type (int_type));
2345 set_variable (get_byte (), pop_type (long_type));
2348 set_variable (get_byte (), pop_type (float_type));
2351 set_variable (get_byte (), pop_type (double_type));
2354 set_variable (get_byte (), pop_ref_or_return ());
2360 set_variable (opcode - op_istore_0, pop_type (int_type));
2366 set_variable (opcode - op_lstore_0, pop_type (long_type));
2372 set_variable (opcode - op_fstore_0, pop_type (float_type));
2378 set_variable (opcode - op_dstore_0, pop_type (double_type));
2384 set_variable (opcode - op_astore_0, pop_ref_or_return ());
2387 pop_type (int_type);
2388 pop_type (int_type);
2389 require_array_type (pop_type (reference_type), int_type);
2392 pop_type (long_type);
2393 pop_type (int_type);
2394 require_array_type (pop_type (reference_type), long_type);
2397 pop_type (float_type);
2398 pop_type (int_type);
2399 require_array_type (pop_type (reference_type), float_type);
2402 pop_type (double_type);
2403 pop_type (int_type);
2404 require_array_type (pop_type (reference_type), double_type);
2407 pop_type (reference_type);
2408 pop_type (int_type);
2409 require_array_type (pop_type (reference_type), reference_type);
2412 pop_type (int_type);
2413 pop_type (int_type);
2414 require_array_type (pop_type (reference_type), byte_type);
2417 pop_type (int_type);
2418 pop_type (int_type);
2419 require_array_type (pop_type (reference_type), char_type);
2422 pop_type (int_type);
2423 pop_type (int_type);
2424 require_array_type (pop_type (reference_type), short_type);
2451 type t2 = pop_raw ();
2466 type t = pop_raw ();
2481 type t1 = pop_raw ();
2498 type t1 = pop_raw ();
2501 type t2 = pop_raw ();
2519 type t3 = pop_raw ();
2557 pop_type (int_type);
2558 push_type (pop_type (int_type));
2568 pop_type (long_type);
2569 push_type (pop_type (long_type));
2574 pop_type (int_type);
2575 push_type (pop_type (long_type));
2582 pop_type (float_type);
2583 push_type (pop_type (float_type));
2590 pop_type (double_type);
2591 push_type (pop_type (double_type));
2597 push_type (pop_type (int_type));
2600 push_type (pop_type (long_type));
2603 push_type (pop_type (float_type));
2606 push_type (pop_type (double_type));
2609 get_variable (get_byte (), int_type);
2613 pop_type (int_type);
2614 push_type (long_type);
2617 pop_type (int_type);
2618 push_type (float_type);
2621 pop_type (int_type);
2622 push_type (double_type);
2625 pop_type (long_type);
2626 push_type (int_type);
2629 pop_type (long_type);
2630 push_type (float_type);
2633 pop_type (long_type);
2634 push_type (double_type);
2637 pop_type (float_type);
2638 push_type (int_type);
2641 pop_type (float_type);
2642 push_type (long_type);
2645 pop_type (float_type);
2646 push_type (double_type);
2649 pop_type (double_type);
2650 push_type (int_type);
2653 pop_type (double_type);
2654 push_type (long_type);
2657 pop_type (double_type);
2658 push_type (float_type);
2661 pop_type (long_type);
2662 pop_type (long_type);
2663 push_type (int_type);
2667 pop_type (float_type);
2668 pop_type (float_type);
2669 push_type (int_type);
2673 pop_type (double_type);
2674 pop_type (double_type);
2675 push_type (int_type);
2683 pop_type (int_type);
2684 push_jump (get_short ());
2692 pop_type (int_type);
2693 pop_type (int_type);
2694 push_jump (get_short ());
2698 pop_type (reference_type);
2699 pop_type (reference_type);
2700 push_jump (get_short ());
2703 push_jump (get_short ());
2707 handle_jsr_insn (get_short ());
2710 handle_ret_insn (get_byte ());
2712 case op_tableswitch:
2714 pop_type (int_type);
2716 push_jump (get_int ());
2717 jint low = get_int ();
2718 jint high = get_int ();
2719 // Already checked LOW -vs- HIGH.
2720 for (int i = low; i <= high; ++i)
2721 push_jump (get_int ());
2726 case op_lookupswitch:
2728 pop_type (int_type);
2730 push_jump (get_int ());
2731 jint npairs = get_int ();
2732 // Already checked NPAIRS >= 0.
2734 for (int i = 0; i < npairs; ++i)
2736 jint key = get_int ();
2737 if (i > 0 && key <= lastkey)
2738 verify_fail ("lookupswitch pairs unsorted", start_PC);
2740 push_jump (get_int ());
2746 check_return_type (pop_type (int_type));
2750 check_return_type (pop_type (long_type));
2754 check_return_type (pop_type (float_type));
2758 check_return_type (pop_type (double_type));
2762 check_return_type (pop_type (reference_type));
2766 // We only need to check this when the return type is
2767 // void, because all instance initializers return void.
2769 current_state->check_this_initialized (this);
2770 check_return_type (void_type);
2774 push_type (check_field_constant (get_ushort ()));
2777 pop_type (check_field_constant (get_ushort ()));
2782 type field = check_field_constant (get_ushort (), &klass);
2790 type field = check_field_constant (get_ushort (), &klass);
2793 // We have an obscure special case here: we can use
2794 // `putfield' on a field declared in this class, even if
2795 // `this' has not yet been initialized.
2796 if (! current_state->this_type.isinitialized ()
2797 && current_state->this_type.pc == type::SELF)
2798 klass.set_uninitialized (type::SELF, this);
2803 case op_invokevirtual:
2804 case op_invokespecial:
2805 case op_invokestatic:
2806 case op_invokeinterface:
2808 _Jv_Utf8Const *method_name, *method_signature;
2810 = check_method_constant (get_ushort (),
2811 opcode == op_invokeinterface,
2814 // NARGS is only used when we're processing
2815 // invokeinterface. It is simplest for us to compute it
2816 // here and then verify it later.
2818 if (opcode == op_invokeinterface)
2820 nargs = get_byte ();
2821 if (get_byte () != 0)
2822 verify_fail ("invokeinterface dummy byte is wrong");
2825 bool is_init = false;
2826 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2829 if (opcode != op_invokespecial)
2830 verify_fail ("can't invoke <init>");
2832 else if (method_name->data[0] == '<')
2833 verify_fail ("can't invoke method starting with `<'");
2835 // Pop arguments and check types.
2836 int arg_count = _Jv_count_arguments (method_signature);
2837 type arg_types[arg_count];
2838 compute_argument_types (method_signature, arg_types);
2839 for (int i = arg_count - 1; i >= 0; --i)
2841 // This is only used for verifying the byte for
2843 nargs -= arg_types[i].depth ();
2844 pop_type (arg_types[i]);
2847 if (opcode == op_invokeinterface
2849 verify_fail ("wrong argument count for invokeinterface");
2851 if (opcode != op_invokestatic)
2853 type t = class_type;
2856 // In this case the PC doesn't matter.
2857 t.set_uninitialized (type::UNINIT, this);
2859 type raw = pop_raw ();
2861 if (t.compatible (raw, this))
2865 else if (opcode == op_invokeinterface)
2867 // This is a hack. We might have merged two
2868 // items and gotten `Object'. This can happen
2869 // because we don't keep track of where merges
2870 // come from. This is safe as long as the
2871 // interpreter checks interfaces at runtime.
2872 type obj (&java::lang::Object::class$);
2873 ok = raw.compatible (obj, this);
2877 verify_fail ("incompatible type on stack");
2880 current_state->set_initialized (raw.get_pc (),
2881 current_method->max_locals);
2884 type rt = compute_return_type (method_signature);
2892 type t = check_class_constant (get_ushort ());
2893 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2894 verify_fail ("type is array, interface, or abstract");
2895 t.set_uninitialized (start_PC, this);
2902 int atype = get_byte ();
2903 // We intentionally have chosen constants to make this
2905 if (atype < boolean_type || atype > long_type)
2906 verify_fail ("type not primitive", start_PC);
2907 pop_type (int_type);
2908 push_type (construct_primitive_array_type (type_val (atype)));
2912 pop_type (int_type);
2913 push_type (check_class_constant (get_ushort ()).to_array (this));
2915 case op_arraylength:
2917 type t = pop_type (reference_type);
2918 if (! t.isarray () && ! t.isnull ())
2919 verify_fail ("array type expected");
2920 push_type (int_type);
2924 pop_type (type (&java::lang::Throwable::class$));
2928 pop_type (reference_type);
2929 push_type (check_class_constant (get_ushort ()));
2932 pop_type (reference_type);
2933 check_class_constant (get_ushort ());
2934 push_type (int_type);
2936 case op_monitorenter:
2937 pop_type (reference_type);
2939 case op_monitorexit:
2940 pop_type (reference_type);
2944 switch (get_byte ())
2947 push_type (get_variable (get_ushort (), int_type));
2950 push_type (get_variable (get_ushort (), long_type));
2953 push_type (get_variable (get_ushort (), float_type));
2956 push_type (get_variable (get_ushort (), double_type));
2959 push_type (get_variable (get_ushort (), reference_type));
2962 set_variable (get_ushort (), pop_type (int_type));
2965 set_variable (get_ushort (), pop_type (long_type));
2968 set_variable (get_ushort (), pop_type (float_type));
2971 set_variable (get_ushort (), pop_type (double_type));
2974 set_variable (get_ushort (), pop_type (reference_type));
2977 handle_ret_insn (get_short ());
2980 get_variable (get_ushort (), int_type);
2984 verify_fail ("unrecognized wide instruction", start_PC);
2988 case op_multianewarray:
2990 type atype = check_class_constant (get_ushort ());
2991 int dim = get_byte ();
2993 verify_fail ("too few dimensions to multianewarray", start_PC);
2994 atype.verify_dimensions (dim, this);
2995 for (int i = 0; i < dim; ++i)
2996 pop_type (int_type);
3002 pop_type (reference_type);
3003 push_jump (get_short ());
3006 push_jump (get_int ());
3010 handle_jsr_insn (get_int ());
3013 // These are unused here, but we call them out explicitly
3014 // so that -Wswitch-enum doesn't complain.
3020 case op_putstatic_1:
3021 case op_putstatic_2:
3022 case op_putstatic_4:
3023 case op_putstatic_8:
3024 case op_putstatic_a:
3026 case op_getfield_2s:
3027 case op_getfield_2u:
3031 case op_getstatic_1:
3032 case op_getstatic_2s:
3033 case op_getstatic_2u:
3034 case op_getstatic_4:
3035 case op_getstatic_8:
3036 case op_getstatic_a:
3038 // Unrecognized opcode.
3039 verify_fail ("unrecognized instruction in verify_instructions_0",
3045 __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
3047 using namespace java::lang;
3048 StringBuffer *buf = new StringBuffer ();
3050 buf->append (JvNewStringLatin1 ("verification failed"));
3055 buf->append (JvNewStringLatin1 (" at PC "));
3059 _Jv_InterpMethod *method = current_method;
3060 buf->append (JvNewStringLatin1 (" in "));
3061 buf->append (current_class->getName());
3062 buf->append ((jchar) ':');
3063 buf->append (JvNewStringUTF (method->get_method()->name->data));
3064 buf->append ((jchar) '(');
3065 buf->append (JvNewStringUTF (method->get_method()->signature->data));
3066 buf->append ((jchar) ')');
3068 buf->append (JvNewStringLatin1 (": "));
3069 buf->append (JvNewStringLatin1 (s));
3070 throw new java::lang::VerifyError (buf->toString ());
3075 void verify_instructions ()
3078 verify_instructions_0 ();
3081 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3083 // We just print the text as utf-8. This is just for debugging
3085 debug_print ("--------------------------------\n");
3086 debug_print ("-- Verifying method `%s'\n", m->self->name->data);
3089 bytecode = m->bytecode ();
3090 exception = m->exceptions ();
3091 current_class = m->defining_class;
3097 entry_points = NULL;
3100 ~_Jv_BytecodeVerifier ()
3109 for (int i = 0; i < current_method->code_length; ++i)
3111 if (jsr_ptrs[i] != NULL)
3113 subr_info *info = jsr_ptrs[i];
3114 while (info != NULL)
3116 subr_info *next = info->next;
3122 _Jv_Free (jsr_ptrs);
3125 while (utf8_list != NULL)
3127 linked_utf8 *n = utf8_list->next;
3128 _Jv_Free (utf8_list->val);
3129 _Jv_Free (utf8_list);
3133 while (entry_points != NULL)
3135 subr_entry_info *next = entry_points->next;
3136 _Jv_Free (entry_points);
3137 entry_points = next;
3143 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3145 _Jv_BytecodeVerifier v (meth);
3146 v.verify_instructions ();
3148 #endif /* INTERPRETER */