1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001, 2002, 2003 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 __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
133 using namespace java::lang;
134 StringBuffer *buf = new StringBuffer ();
136 buf->append (JvNewStringLatin1 ("verification failed"));
141 buf->append (JvNewStringLatin1 (" at PC "));
145 _Jv_InterpMethod *method = current_method;
146 buf->append (JvNewStringLatin1 (" in "));
147 buf->append (current_class->getName());
148 buf->append ((jchar) ':');
149 buf->append (JvNewStringUTF (method->get_method()->name->data));
150 buf->append ((jchar) '(');
151 buf->append (JvNewStringUTF (method->get_method()->signature->data));
152 buf->append ((jchar) ')');
154 buf->append (JvNewStringLatin1 (": "));
155 buf->append (JvNewStringLatin1 (s));
156 throw new java::lang::VerifyError (buf->toString ());
159 // This enum holds a list of tags for all the different types we
160 // need to handle. Reference types are treated specially by the
166 // The values for primitive types are chosen to correspond to values
167 // specified to newarray.
177 // Used when overwriting second word of a double or long in the
178 // local variables. Also used after merging local variable states
179 // to indicate an unusable value.
184 // There is an obscure special case which requires us to note when
185 // a local variable has not been used by a subroutine. See
186 // push_jump_merge for more information.
187 unused_by_subroutine_type,
189 // Everything after `reference_type' must be a reference type.
192 unresolved_reference_type,
193 uninitialized_reference_type,
194 uninitialized_unresolved_reference_type
197 // Return the type_val corresponding to a primitive signature
198 // character. For instance `I' returns `int.class'.
199 type_val get_type_val_for_signature (jchar sig)
232 verify_fail ("invalid signature");
237 // Return the type_val corresponding to a primitive class.
238 type_val get_type_val_for_signature (jclass k)
240 return get_type_val_for_signature ((jchar) k->method_count);
243 // This is used to keep track of which `jsr's correspond to a given
247 // PC of the instruction just after the jsr.
253 // This is used to keep track of which subroutine entry point
254 // corresponds to which `ret' instruction.
255 struct subr_entry_info
257 // PC of the subroutine entry point.
259 // PC of the `ret' instruction.
262 subr_entry_info *next;
265 // The `type' class is used to represent a single type in the
271 // Some associated data.
274 // For a resolved reference type, this is a pointer to the class.
276 // For other reference types, this it the name of the class.
279 // This is used when constructing a new object. It is the PC of the
280 // `new' instruction which created the object. We use the special
281 // value -2 to mean that this is uninitialized, and the special
282 // value -1 for the case where the current method is itself the
286 static const int UNINIT = -2;
287 static const int SELF = -1;
289 // Basic constructor.
292 key = unsuitable_type;
297 // Make a new instance given the type tag. We assume a generic
298 // `reference_type' means Object.
303 if (key == reference_type)
304 data.klass = &java::lang::Object::class$;
308 // Make a new instance given a class.
311 key = reference_type;
316 // Make a new instance given the name of a class.
317 type (_Jv_Utf8Const *n)
319 key = unresolved_reference_type;
332 // These operators are required because libgcj can't link in
334 void *operator new[] (size_t bytes)
336 return _Jv_Malloc (bytes);
339 void operator delete[] (void *mem)
344 type& operator= (type_val k)
352 type& operator= (const type& t)
360 // Promote a numeric type.
363 if (key == boolean_type || key == char_type
364 || key == byte_type || key == short_type)
369 // If *THIS is an unresolved reference type, resolve it.
370 void resolve (_Jv_BytecodeVerifier *verifier)
372 if (key != unresolved_reference_type
373 && key != uninitialized_unresolved_reference_type)
376 using namespace java::lang;
377 java::lang::ClassLoader *loader
378 = verifier->current_class->getClassLoaderInternal();
379 // We might see either kind of name. Sigh.
380 if (data.name->data[0] == 'L'
381 && data.name->data[data.name->length - 1] == ';')
382 data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
384 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
386 key = (key == unresolved_reference_type
388 : uninitialized_reference_type);
391 // Mark this type as the uninitialized result of `new'.
392 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
394 if (key == reference_type)
395 key = uninitialized_reference_type;
396 else if (key == unresolved_reference_type)
397 key = uninitialized_unresolved_reference_type;
399 verifier->verify_fail ("internal error in type::uninitialized");
403 // Mark this type as now initialized.
404 void set_initialized (int npc)
406 if (npc != UNINIT && pc == npc
407 && (key == uninitialized_reference_type
408 || key == uninitialized_unresolved_reference_type))
410 key = (key == uninitialized_reference_type
412 : unresolved_reference_type);
418 // Return true if an object of type K can be assigned to a variable
419 // of type *THIS. Handle various special cases too. Might modify
420 // *THIS or K. Note however that this does not perform numeric
422 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
424 // Any type is compatible with the unsuitable type.
425 if (key == unsuitable_type)
428 if (key < reference_type || k.key < reference_type)
431 // The `null' type is convertible to any initialized reference
433 if (key == null_type || k.key == null_type)
436 // Any reference type is convertible to Object. This is a special
437 // case so we don't need to unnecessarily resolve a class.
438 if (key == reference_type
439 && data.klass == &java::lang::Object::class$)
442 // An initialized type and an uninitialized type are not
444 if (isinitialized () != k.isinitialized ())
447 // Two uninitialized objects are compatible if either:
448 // * The PCs are identical, or
449 // * One PC is UNINIT.
450 if (! isinitialized ())
452 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
456 // Two unresolved types are equal if their names are the same.
459 && _Jv_equalUtf8Consts (data.name, k.data.name))
462 // We must resolve both types and check assignability.
464 k.resolve (verifier);
465 return _Jv_IsAssignableFrom (data.klass, k.data.klass);
470 return key == void_type;
475 return key == long_type || key == double_type;
478 // Return number of stack or local variable slots taken by this
482 return iswide () ? 2 : 1;
485 bool isarray () const
487 // We treat null_type as not an array. This is ok based on the
488 // current uses of this method.
489 if (key == reference_type)
490 return data.klass->isArray ();
491 else if (key == unresolved_reference_type)
492 return data.name->data[0] == '[';
498 return key == null_type;
501 bool isinterface (_Jv_BytecodeVerifier *verifier)
504 if (key != reference_type)
506 return data.klass->isInterface ();
509 bool isabstract (_Jv_BytecodeVerifier *verifier)
512 if (key != reference_type)
514 using namespace java::lang::reflect;
515 return Modifier::isAbstract (data.klass->getModifiers ());
518 // Return the element type of an array.
519 type element_type (_Jv_BytecodeVerifier *verifier)
521 // FIXME: maybe should do string manipulation here.
523 if (key != reference_type)
524 verifier->verify_fail ("programmer error in type::element_type()", -1);
526 jclass k = data.klass->getComponentType ();
527 if (k->isPrimitive ())
528 return type (verifier->get_type_val_for_signature (k));
532 // Return the array type corresponding to an initialized
533 // reference. We could expand this to work for other kinds of
534 // types, but currently we don't need to.
535 type to_array (_Jv_BytecodeVerifier *verifier)
537 // Resolving isn't ideal, because it might force us to load
538 // another class, but it's easy. FIXME?
539 if (key == unresolved_reference_type)
542 if (key == reference_type)
543 return type (_Jv_GetArrayClass (data.klass,
544 data.klass->getClassLoaderInternal()));
546 verifier->verify_fail ("internal error in type::to_array()");
549 bool isreference () const
551 return key >= reference_type;
559 bool isinitialized () const
561 return (key == reference_type
563 || key == unresolved_reference_type);
566 bool isresolved () const
568 return (key == reference_type
570 || key == uninitialized_reference_type);
573 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
575 // The way this is written, we don't need to check isarray().
576 if (key == reference_type)
578 jclass k = data.klass;
579 while (k->isArray () && ndims > 0)
581 k = k->getComponentType ();
587 // We know KEY == unresolved_reference_type.
588 char *p = data.name->data;
589 while (*p++ == '[' && ndims-- > 0)
594 verifier->verify_fail ("array type has fewer dimensions than required");
597 // Merge OLD_TYPE into this. On error throw exception.
598 bool merge (type& old_type, bool local_semantics,
599 _Jv_BytecodeVerifier *verifier)
601 bool changed = false;
602 bool refo = old_type.isreference ();
603 bool refn = isreference ();
606 if (old_type.key == null_type)
608 else if (key == null_type)
613 else if (isinitialized () != old_type.isinitialized ())
614 verifier->verify_fail ("merging initialized and uninitialized types");
617 if (! isinitialized ())
621 else if (old_type.pc == UNINIT)
623 else if (pc != old_type.pc)
624 verifier->verify_fail ("merging different uninitialized types");
628 && ! old_type.isresolved ()
629 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
631 // Types are identical.
636 old_type.resolve (verifier);
638 jclass k = data.klass;
639 jclass oldk = old_type.data.klass;
642 while (k->isArray () && oldk->isArray ())
645 k = k->getComponentType ();
646 oldk = oldk->getComponentType ();
649 // Ordinarily this terminates when we hit Object...
652 if (_Jv_IsAssignableFrom (k, oldk))
654 k = k->getSuperclass ();
657 // ... but K could have been an interface, in which
658 // case we'll end up here. We just convert this
661 k = &java::lang::Object::class$;
665 while (arraycount > 0)
667 java::lang::ClassLoader *loader
668 = verifier->current_class->getClassLoaderInternal();
669 k = _Jv_GetArrayClass (k, loader);
677 else if (refo || refn || key != old_type.key)
681 // If we're merging into an "unused" slot, then we
682 // simply accept whatever we're merging from.
683 if (key == unused_by_subroutine_type)
688 else if (old_type.key == unused_by_subroutine_type)
692 // If we already have an `unsuitable' type, then we
693 // don't need to change again.
694 else if (key != unsuitable_type)
696 key = unsuitable_type;
701 verifier->verify_fail ("unmergeable type");
707 void print (void) const
712 case boolean_type: c = 'Z'; break;
713 case byte_type: c = 'B'; break;
714 case char_type: c = 'C'; break;
715 case short_type: c = 'S'; break;
716 case int_type: c = 'I'; break;
717 case long_type: c = 'J'; break;
718 case float_type: c = 'F'; break;
719 case double_type: c = 'D'; break;
720 case void_type: c = 'V'; break;
721 case unsuitable_type: c = '-'; break;
722 case return_address_type: c = 'r'; break;
723 case continuation_type: c = '+'; break;
724 case unused_by_subroutine_type: c = '_'; break;
725 case reference_type: c = 'L'; break;
726 case null_type: c = '@'; break;
727 case unresolved_reference_type: c = 'l'; break;
728 case uninitialized_reference_type: c = 'U'; break;
729 case uninitialized_unresolved_reference_type: c = 'u'; break;
731 debug_print ("%c", c);
733 #endif /* VERIFY_DEBUG */
736 // This class holds all the state information we need for a given
740 // The current top of the stack, in terms of slots.
742 // The current depth of the stack. This will be larger than
743 // STACKTOP when wide types are on the stack.
747 // The local variables.
749 // This is used in subroutines to keep track of which local
750 // variables have been accessed.
752 // If not 0, then we are in a subroutine. The value is the PC of
753 // the subroutine's entry point. We can use 0 as an exceptional
754 // value because PC=0 can never be a subroutine.
756 // This is used to keep a linked list of all the states which
757 // require re-verification. We use the PC to keep track.
759 // We keep track of the type of `this' specially. This is used to
760 // ensure that an instance initializer invokes another initializer
761 // on `this' before returning. We must keep track of this
762 // specially because otherwise we might be confused by code which
763 // assigns to locals[0] (overwriting `this') and then returns
764 // without really initializing.
766 // This is a list of all subroutines that have been seen at this
767 // point. Ordinarily this is NULL; it is only allocated and used
768 // in relatively weird situations involving non-ret exit from a
769 // subroutine. We have to keep track of this in this way to avoid
770 // endless recursion in these cases.
771 subr_info *seen_subrs;
773 // INVALID marks a state which is not on the linked list of states
774 // requiring reverification.
775 static const int INVALID = -1;
776 // NO_NEXT marks the state at the end of the reverification list.
777 static const int NO_NEXT = -2;
779 // This is used to mark the stack depth at the instruction just
780 // after a `jsr' when we haven't yet processed the corresponding
781 // `ret'. See handle_jsr_insn for more information.
782 static const int NO_STACK = -1;
789 local_changed = NULL;
793 state (int max_stack, int max_locals)
798 stack = new type[max_stack];
799 for (int i = 0; i < max_stack; ++i)
800 stack[i] = unsuitable_type;
801 locals = new type[max_locals];
802 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
804 for (int i = 0; i < max_locals; ++i)
806 locals[i] = unsuitable_type;
807 local_changed[i] = false;
813 state (const state *orig, int max_stack, int max_locals,
814 bool ret_semantics = false)
816 stack = new type[max_stack];
817 locals = new type[max_locals];
818 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
820 copy (orig, max_stack, max_locals, ret_semantics);
831 _Jv_Free (local_changed);
835 void *operator new[] (size_t bytes)
837 return _Jv_Malloc (bytes);
840 void operator delete[] (void *mem)
845 void *operator new (size_t bytes)
847 return _Jv_Malloc (bytes);
850 void operator delete (void *mem)
857 subr_info *info = seen_subrs;
860 subr_info *next = info->next;
866 void copy (const state *copy, int max_stack, int max_locals,
867 bool ret_semantics = false)
869 stacktop = copy->stacktop;
870 stackdepth = copy->stackdepth;
871 subroutine = copy->subroutine;
872 for (int i = 0; i < max_stack; ++i)
873 stack[i] = copy->stack[i];
874 for (int i = 0; i < max_locals; ++i)
876 // See push_jump_merge to understand this case.
878 locals[i] = type (copy->local_changed[i]
880 : unused_by_subroutine_type);
882 locals[i] = copy->locals[i];
883 local_changed[i] = copy->local_changed[i];
887 if (copy->seen_subrs)
889 for (subr_info *info = seen_subrs; info != NULL; info = info->next)
895 this_type = copy->this_type;
896 // Don't modify `next'.
899 // Modify this state to reflect entry to an exception handler.
900 void set_exception (type t, int max_stack)
905 for (int i = stacktop; i < max_stack; ++i)
906 stack[i] = unsuitable_type;
909 // Modify this state to reflect entry into a subroutine.
910 void enter_subroutine (int npc, int max_locals)
913 // Mark all items as unchanged. Each subroutine needs to keep
914 // track of its `changed' state independently. In the case of
915 // nested subroutines, this information will be merged back into
916 // parent by the `ret'.
917 for (int i = 0; i < max_locals; ++i)
918 local_changed[i] = false;
921 // Indicate that we've been in this this subroutine.
922 void add_subr (int pc)
924 subr_info *n = (subr_info *) _Jv_Malloc (sizeof (subr_info));
926 n->next = seen_subrs;
930 // Merge STATE_OLD into this state. Destructively modifies this
931 // state. Returns true if the new state was in fact changed.
932 // Will throw an exception if the states are not mergeable.
933 bool merge (state *state_old, bool ret_semantics,
934 int max_locals, _Jv_BytecodeVerifier *verifier)
936 bool changed = false;
938 // Special handling for `this'. If one or the other is
939 // uninitialized, then the merge is uninitialized.
940 if (this_type.isinitialized ())
941 this_type = state_old->this_type;
943 // Merge subroutine states. Here we just keep track of what
944 // subroutine we think we're in. We only check for a merge
945 // (which is invalid) when we see a `ret'.
946 if (subroutine == state_old->subroutine)
950 else if (subroutine == 0)
952 subroutine = state_old->subroutine;
957 // If the subroutines differ, and we haven't seen this
958 // subroutine before, indicate that the state changed. This
959 // is needed to detect when subroutines have merged.
961 for (subr_info *info = seen_subrs; info != NULL; info = info->next)
963 if (info->pc == state_old->subroutine)
971 add_subr (state_old->subroutine);
976 // Merge stacks. Special handling for NO_STACK case.
977 if (state_old->stacktop == NO_STACK)
979 // Nothing to do in this case; we don't care about modifying
982 else if (stacktop == NO_STACK)
984 stacktop = state_old->stacktop;
985 stackdepth = state_old->stackdepth;
986 for (int i = 0; i < stacktop; ++i)
987 stack[i] = state_old->stack[i];
990 else if (state_old->stacktop != stacktop)
991 verifier->verify_fail ("stack sizes differ");
994 for (int i = 0; i < state_old->stacktop; ++i)
996 if (stack[i].merge (state_old->stack[i], false, verifier))
1001 // Merge local variables.
1002 for (int i = 0; i < max_locals; ++i)
1004 // If we're not processing a `ret', then we merge every
1005 // local variable. If we are processing a `ret', then we
1006 // only merge locals which changed in the subroutine. When
1007 // processing a `ret', STATE_OLD is the state at the point
1008 // of the `ret', and THIS is the state just after the `jsr'.
1009 if (! ret_semantics || state_old->local_changed[i])
1011 if (locals[i].merge (state_old->locals[i], true, verifier))
1013 // Note that we don't call `note_variable' here.
1014 // This change doesn't represent a real change to a
1015 // local, but rather a merge artifact. If we're in
1016 // a subroutine which is called with two
1017 // incompatible types in a slot that is unused by
1018 // the subroutine, then we don't want to mark that
1019 // variable as having been modified.
1024 // If we're in a subroutine, we must compute the union of
1025 // all the changed local variables.
1026 if (state_old->local_changed[i])
1033 // Throw an exception if there is an uninitialized object on the
1034 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1035 // whether we're using backwards-branch or exception-handing
1037 void check_no_uninitialized_objects (int max_locals,
1038 _Jv_BytecodeVerifier *verifier,
1039 bool exception_semantics = false)
1041 if (! exception_semantics)
1043 for (int i = 0; i < stacktop; ++i)
1044 if (stack[i].isreference () && ! stack[i].isinitialized ())
1045 verifier->verify_fail ("uninitialized object on stack");
1048 for (int i = 0; i < max_locals; ++i)
1049 if (locals[i].isreference () && ! locals[i].isinitialized ())
1050 verifier->verify_fail ("uninitialized object in local variable");
1052 check_this_initialized (verifier);
1055 // Ensure that `this' has been initialized.
1056 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1058 if (this_type.isreference () && ! this_type.isinitialized ())
1059 verifier->verify_fail ("`this' is uninitialized");
1062 // Set type of `this'.
1063 void set_this_type (const type &k)
1068 // Note that a local variable was modified.
1069 void note_variable (int index)
1072 local_changed[index] = true;
1075 // Mark each `new'd object we know of that was allocated at PC as
1077 void set_initialized (int pc, int max_locals)
1079 for (int i = 0; i < stacktop; ++i)
1080 stack[i].set_initialized (pc);
1081 for (int i = 0; i < max_locals; ++i)
1082 locals[i].set_initialized (pc);
1083 this_type.set_initialized (pc);
1086 // Return true if this state is the unmerged result of a `ret'.
1087 bool is_unmerged_ret_state (int max_locals) const
1089 if (stacktop == NO_STACK)
1091 for (int i = 0; i < max_locals; ++i)
1093 if (locals[i].key == unused_by_subroutine_type)
1100 void print (const char *leader, int pc,
1101 int max_stack, int max_locals) const
1103 debug_print ("%s [%4d]: [stack] ", leader, pc);
1105 for (i = 0; i < stacktop; ++i)
1107 for (; i < max_stack; ++i)
1109 debug_print (" [local] ");
1110 for (i = 0; i < max_locals; ++i)
1113 debug_print (local_changed[i] ? "+" : " ");
1115 if (subroutine == 0)
1116 debug_print (" | None");
1118 debug_print (" | %4d", subroutine);
1119 debug_print (" | %p\n", this);
1122 inline void print (const char *, int, int, int) const
1125 #endif /* VERIFY_DEBUG */
1130 if (current_state->stacktop <= 0)
1131 verify_fail ("stack empty");
1132 type r = current_state->stack[--current_state->stacktop];
1133 current_state->stackdepth -= r.depth ();
1134 if (current_state->stackdepth < 0)
1135 verify_fail ("stack empty", start_PC);
1141 type r = pop_raw ();
1143 verify_fail ("narrow pop of wide type");
1147 type pop_type (type match)
1150 type t = pop_raw ();
1151 if (! match.compatible (t, this))
1152 verify_fail ("incompatible type on stack");
1156 // Pop a reference which is guaranteed to be initialized. MATCH
1157 // doesn't have to be a reference type; in this case this acts like
1159 type pop_init_ref (type match)
1161 type t = pop_raw ();
1162 if (t.isreference () && ! t.isinitialized ())
1163 verify_fail ("initialized reference required");
1164 else if (! match.compatible (t, this))
1165 verify_fail ("incompatible type on stack");
1169 // Pop a reference type or a return address.
1170 type pop_ref_or_return ()
1172 type t = pop_raw ();
1173 if (! t.isreference () && t.key != return_address_type)
1174 verify_fail ("expected reference or return address on stack");
1178 void push_type (type t)
1180 // If T is a numeric type like short, promote it to int.
1183 int depth = t.depth ();
1184 if (current_state->stackdepth + depth > current_method->max_stack)
1185 verify_fail ("stack overflow");
1186 current_state->stack[current_state->stacktop++] = t;
1187 current_state->stackdepth += depth;
1190 void set_variable (int index, type t)
1192 // If T is a numeric type like short, promote it to int.
1195 int depth = t.depth ();
1196 if (index > current_method->max_locals - depth)
1197 verify_fail ("invalid local variable");
1198 current_state->locals[index] = t;
1199 current_state->note_variable (index);
1203 current_state->locals[index + 1] = continuation_type;
1204 current_state->note_variable (index + 1);
1206 if (index > 0 && current_state->locals[index - 1].iswide ())
1208 current_state->locals[index - 1] = unsuitable_type;
1209 // There's no need to call note_variable here.
1213 type get_variable (int index, type t)
1215 int depth = t.depth ();
1216 if (index > current_method->max_locals - depth)
1217 verify_fail ("invalid local variable");
1218 if (! t.compatible (current_state->locals[index], this))
1219 verify_fail ("incompatible type in local variable");
1222 type t (continuation_type);
1223 if (! current_state->locals[index + 1].compatible (t, this))
1224 verify_fail ("invalid local variable");
1226 return current_state->locals[index];
1229 // Make sure ARRAY is an array type and that its elements are
1230 // compatible with type ELEMENT. Returns the actual element type.
1231 type require_array_type (type array, type element)
1233 // An odd case. Here we just pretend that everything went ok. If
1234 // the requested element type is some kind of reference, return
1235 // the null type instead.
1236 if (array.isnull ())
1237 return element.isreference () ? type (null_type) : element;
1239 if (! array.isarray ())
1240 verify_fail ("array required");
1242 type t = array.element_type (this);
1243 if (! element.compatible (t, this))
1245 // Special case for byte arrays, which must also be boolean
1248 if (element.key == byte_type)
1250 type e2 (boolean_type);
1251 ok = e2.compatible (t, this);
1254 verify_fail ("incompatible array element type");
1257 // Return T and not ELEMENT, because T might be specialized.
1263 if (PC >= current_method->code_length)
1264 verify_fail ("premature end of bytecode");
1265 return (jint) bytecode[PC++] & 0xff;
1270 jint b1 = get_byte ();
1271 jint b2 = get_byte ();
1272 return (jint) ((b1 << 8) | b2) & 0xffff;
1277 jint b1 = get_byte ();
1278 jint b2 = get_byte ();
1279 jshort s = (b1 << 8) | b2;
1285 jint b1 = get_byte ();
1286 jint b2 = get_byte ();
1287 jint b3 = get_byte ();
1288 jint b4 = get_byte ();
1289 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1292 int compute_jump (int offset)
1294 int npc = start_PC + offset;
1295 if (npc < 0 || npc >= current_method->code_length)
1296 verify_fail ("branch out of range", start_PC);
1300 // Merge the indicated state into the state at the branch target and
1301 // schedule a new PC if there is a change. If RET_SEMANTICS is
1302 // true, then we are merging from a `ret' instruction into the
1303 // instruction after a `jsr'. This is a special case with its own
1304 // modified semantics.
1305 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1307 bool changed = true;
1308 if (states[npc] == NULL)
1310 // There's a weird situation here. If are examining the
1311 // branch that results from a `ret', and there is not yet a
1312 // state available at the branch target (the instruction just
1313 // after the `jsr'), then we have to construct a special kind
1314 // of state at that point for future merging. This special
1315 // state has the type `unused_by_subroutine_type' in each slot
1316 // which was not modified by the subroutine.
1317 states[npc] = new state (nstate, current_method->max_stack,
1318 current_method->max_locals, ret_semantics);
1319 debug_print ("== New state in push_jump_merge\n");
1320 states[npc]->print ("New", npc, current_method->max_stack,
1321 current_method->max_locals);
1325 debug_print ("== Merge states in push_jump_merge\n");
1326 nstate->print ("Frm", start_PC, current_method->max_stack,
1327 current_method->max_locals);
1328 states[npc]->print (" To", npc, current_method->max_stack,
1329 current_method->max_locals);
1330 changed = states[npc]->merge (nstate, ret_semantics,
1331 current_method->max_locals, this);
1332 states[npc]->print ("New", npc, current_method->max_stack,
1333 current_method->max_locals);
1336 if (changed && states[npc]->next == state::INVALID)
1338 // The merge changed the state, and the new PC isn't yet on our
1339 // list of PCs to re-verify.
1340 states[npc]->next = next_verify_pc;
1341 next_verify_pc = npc;
1345 void push_jump (int offset)
1347 int npc = compute_jump (offset);
1349 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1350 push_jump_merge (npc, current_state);
1353 void push_exception_jump (type t, int pc)
1355 current_state->check_no_uninitialized_objects (current_method->max_locals,
1357 state s (current_state, current_method->max_stack,
1358 current_method->max_locals);
1359 if (current_method->max_stack < 1)
1360 verify_fail ("stack overflow at exception handler");
1361 s.set_exception (t, current_method->max_stack);
1362 push_jump_merge (pc, &s);
1367 int *prev_loc = &next_verify_pc;
1368 int npc = next_verify_pc;
1370 while (npc != state::NO_NEXT)
1372 // If the next available PC is an unmerged `ret' state, then
1373 // we aren't yet ready to handle it. That's because we would
1374 // need all kind of special cases to do so. So instead we
1375 // defer this jump until after we've processed it via a
1376 // fall-through. This has to happen because the instruction
1377 // before this one must be a `jsr'.
1378 if (! states[npc]->is_unmerged_ret_state (current_method->max_locals))
1380 *prev_loc = states[npc]->next;
1381 states[npc]->next = state::INVALID;
1385 prev_loc = &states[npc]->next;
1386 npc = states[npc]->next;
1389 // Note that we might have gotten here even when there are
1390 // remaining states to process. That can happen if we find a
1391 // `jsr' without a `ret'.
1392 return state::NO_NEXT;
1395 void invalidate_pc ()
1397 PC = state::NO_NEXT;
1400 void note_branch_target (int pc, bool is_jsr_target = false)
1402 // Don't check `pc <= PC', because we've advanced PC after
1403 // fetching the target and we haven't yet checked the next
1405 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1406 verify_fail ("branch not to instruction start", start_PC);
1407 flags[pc] |= FLAG_BRANCH_TARGET;
1410 // Record the jsr which called this instruction.
1411 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1413 info->next = jsr_ptrs[pc];
1414 jsr_ptrs[pc] = info;
1418 void skip_padding ()
1420 while ((PC % 4) > 0)
1421 if (get_byte () != 0)
1422 verify_fail ("found nonzero padding byte");
1425 // Return the subroutine to which the instruction at PC belongs.
1426 int get_subroutine (int pc)
1428 if (states[pc] == NULL)
1430 return states[pc]->subroutine;
1433 // Do the work for a `ret' instruction. INDEX is the index into the
1435 void handle_ret_insn (int index)
1437 get_variable (index, return_address_type);
1439 int csub = current_state->subroutine;
1441 verify_fail ("no subroutine");
1443 // Check to see if we've merged subroutines.
1444 subr_entry_info *entry;
1445 for (entry = entry_points; entry != NULL; entry = entry->next)
1447 if (entry->ret_pc == start_PC)
1452 entry = (subr_entry_info *) _Jv_Malloc (sizeof (subr_entry_info));
1454 entry->ret_pc = start_PC;
1455 entry->next = entry_points;
1456 entry_points = entry;
1458 else if (entry->pc != csub)
1459 verify_fail ("subroutines merged");
1461 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1463 // We might be returning to a `jsr' that is at the end of the
1464 // bytecode. This is ok if we never return from the called
1465 // subroutine, but if we see this here it is an error.
1466 if (subr->pc >= current_method->code_length)
1467 verify_fail ("fell off end");
1469 // Temporarily modify the current state so it looks like we're
1470 // in the enclosing context.
1471 current_state->subroutine = get_subroutine (subr->pc);
1473 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1474 push_jump_merge (subr->pc, current_state, true);
1477 current_state->subroutine = csub;
1481 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1482 // sure this subroutine isn't already on the stack.
1483 void check_nonrecursive_call (int sub, int dest)
1488 verify_fail ("recursive subroutine call");
1489 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1490 check_nonrecursive_call (get_subroutine (info->pc), dest);
1493 void handle_jsr_insn (int offset)
1495 int npc = compute_jump (offset);
1498 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1499 check_nonrecursive_call (current_state->subroutine, npc);
1501 // Modify our state as appropriate for entry into a subroutine.
1502 push_type (return_address_type);
1503 push_jump_merge (npc, current_state);
1505 pop_type (return_address_type);
1507 // On entry to the subroutine, the subroutine number must be set
1508 // and the locals must be marked as cleared. We do this after
1509 // merging state so that we don't erroneously "notice" a variable
1510 // change merely on entry.
1511 states[npc]->enter_subroutine (npc, current_method->max_locals);
1513 // Indicate that we don't know the stack depth of the instruction
1514 // following the `jsr'. The idea here is that we need to merge
1515 // the local variable state across the jsr, but the subroutine
1516 // might change the stack depth, so we can't make any assumptions
1517 // about it. So we have yet another special case. We know that
1518 // at this point PC points to the instruction after the jsr. Note
1519 // that it is ok to have a `jsr' at the end of the bytecode,
1520 // provided that the called subroutine never returns. So, we have
1521 // a special case here and another one when we handle the ret.
1522 if (PC < current_method->code_length)
1524 current_state->stacktop = state::NO_STACK;
1525 push_jump_merge (PC, current_state);
1530 jclass construct_primitive_array_type (type_val prim)
1536 k = JvPrimClass (boolean);
1539 k = JvPrimClass (char);
1542 k = JvPrimClass (float);
1545 k = JvPrimClass (double);
1548 k = JvPrimClass (byte);
1551 k = JvPrimClass (short);
1554 k = JvPrimClass (int);
1557 k = JvPrimClass (long);
1560 // These aren't used here but we call them out to avoid
1563 case unsuitable_type:
1564 case return_address_type:
1565 case continuation_type:
1566 case unused_by_subroutine_type:
1567 case reference_type:
1569 case unresolved_reference_type:
1570 case uninitialized_reference_type:
1571 case uninitialized_unresolved_reference_type:
1573 verify_fail ("unknown type in construct_primitive_array_type");
1575 k = _Jv_GetArrayClass (k, NULL);
1579 // This pass computes the location of branch targets and also
1580 // instruction starts.
1581 void branch_prepass ()
1583 flags = (char *) _Jv_Malloc (current_method->code_length);
1584 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1585 * current_method->code_length);
1587 for (int i = 0; i < current_method->code_length; ++i)
1593 bool last_was_jsr = false;
1596 while (PC < current_method->code_length)
1598 // Set `start_PC' early so that error checking can have the
1601 flags[PC] |= FLAG_INSN_START;
1603 // If the previous instruction was a jsr, then the next
1604 // instruction is a branch target -- the branch being the
1605 // corresponding `ret'.
1607 note_branch_target (PC);
1608 last_was_jsr = false;
1610 java_opcode opcode = (java_opcode) bytecode[PC++];
1614 case op_aconst_null:
1750 case op_monitorenter:
1751 case op_monitorexit:
1759 case op_arraylength:
1791 case op_invokespecial:
1792 case op_invokestatic:
1793 case op_invokevirtual:
1797 case op_multianewarray:
1803 last_was_jsr = true;
1822 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1825 case op_tableswitch:
1828 note_branch_target (compute_jump (get_int ()));
1829 jint low = get_int ();
1830 jint hi = get_int ();
1832 verify_fail ("invalid tableswitch", start_PC);
1833 for (int i = low; i <= hi; ++i)
1834 note_branch_target (compute_jump (get_int ()));
1838 case op_lookupswitch:
1841 note_branch_target (compute_jump (get_int ()));
1842 int npairs = get_int ();
1844 verify_fail ("too few pairs in lookupswitch", start_PC);
1845 while (npairs-- > 0)
1848 note_branch_target (compute_jump (get_int ()));
1853 case op_invokeinterface:
1861 opcode = (java_opcode) get_byte ();
1863 if (opcode == op_iinc)
1869 last_was_jsr = true;
1872 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1875 // These are unused here, but we call them out explicitly
1876 // so that -Wswitch-enum doesn't complain.
1882 case op_putstatic_1:
1883 case op_putstatic_2:
1884 case op_putstatic_4:
1885 case op_putstatic_8:
1886 case op_putstatic_a:
1888 case op_getfield_2s:
1889 case op_getfield_2u:
1893 case op_getstatic_1:
1894 case op_getstatic_2s:
1895 case op_getstatic_2u:
1896 case op_getstatic_4:
1897 case op_getstatic_8:
1898 case op_getstatic_a:
1900 verify_fail ("unrecognized instruction in branch_prepass",
1904 // See if any previous branch tried to branch to the middle of
1905 // this instruction.
1906 for (int pc = start_PC + 1; pc < PC; ++pc)
1908 if ((flags[pc] & FLAG_BRANCH_TARGET))
1909 verify_fail ("branch to middle of instruction", pc);
1913 // Verify exception handlers.
1914 for (int i = 0; i < current_method->exc_count; ++i)
1916 if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
1917 verify_fail ("exception handler not at instruction start",
1918 exception[i].handler_pc.i);
1919 if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
1920 verify_fail ("exception start not at instruction start",
1921 exception[i].start_pc.i);
1922 if (exception[i].end_pc.i != current_method->code_length
1923 && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
1924 verify_fail ("exception end not at instruction start",
1925 exception[i].end_pc.i);
1927 flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
1931 void check_pool_index (int index)
1933 if (index < 0 || index >= current_class->constants.size)
1934 verify_fail ("constant pool index out of range", start_PC);
1937 type check_class_constant (int index)
1939 check_pool_index (index);
1940 _Jv_Constants *pool = ¤t_class->constants;
1941 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1942 return type (pool->data[index].clazz);
1943 else if (pool->tags[index] == JV_CONSTANT_Class)
1944 return type (pool->data[index].utf8);
1945 verify_fail ("expected class constant", start_PC);
1948 type check_constant (int index)
1950 check_pool_index (index);
1951 _Jv_Constants *pool = ¤t_class->constants;
1952 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1953 || pool->tags[index] == JV_CONSTANT_String)
1954 return type (&java::lang::String::class$);
1955 else if (pool->tags[index] == JV_CONSTANT_Integer)
1956 return type (int_type);
1957 else if (pool->tags[index] == JV_CONSTANT_Float)
1958 return type (float_type);
1959 verify_fail ("String, int, or float constant expected", start_PC);
1962 type check_wide_constant (int index)
1964 check_pool_index (index);
1965 _Jv_Constants *pool = ¤t_class->constants;
1966 if (pool->tags[index] == JV_CONSTANT_Long)
1967 return type (long_type);
1968 else if (pool->tags[index] == JV_CONSTANT_Double)
1969 return type (double_type);
1970 verify_fail ("long or double constant expected", start_PC);
1973 // Helper for both field and method. These are laid out the same in
1974 // the constant pool.
1975 type handle_field_or_method (int index, int expected,
1976 _Jv_Utf8Const **name,
1977 _Jv_Utf8Const **fmtype)
1979 check_pool_index (index);
1980 _Jv_Constants *pool = ¤t_class->constants;
1981 if (pool->tags[index] != expected)
1982 verify_fail ("didn't see expected constant", start_PC);
1983 // Once we know we have a Fieldref or Methodref we assume that it
1984 // is correctly laid out in the constant pool. I think the code
1985 // in defineclass.cc guarantees this.
1986 _Jv_ushort class_index, name_and_type_index;
1987 _Jv_loadIndexes (&pool->data[index],
1989 name_and_type_index);
1990 _Jv_ushort name_index, desc_index;
1991 _Jv_loadIndexes (&pool->data[name_and_type_index],
1992 name_index, desc_index);
1994 *name = pool->data[name_index].utf8;
1995 *fmtype = pool->data[desc_index].utf8;
1997 return check_class_constant (class_index);
2000 // Return field's type, compute class' type if requested.
2001 type check_field_constant (int index, type *class_type = NULL)
2003 _Jv_Utf8Const *name, *field_type;
2004 type ct = handle_field_or_method (index,
2005 JV_CONSTANT_Fieldref,
2006 &name, &field_type);
2009 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
2010 return type (field_type);
2011 return get_type_val_for_signature (field_type->data[0]);
2014 type check_method_constant (int index, bool is_interface,
2015 _Jv_Utf8Const **method_name,
2016 _Jv_Utf8Const **method_signature)
2018 return handle_field_or_method (index,
2020 ? JV_CONSTANT_InterfaceMethodref
2021 : JV_CONSTANT_Methodref),
2022 method_name, method_signature);
2025 type get_one_type (char *&p)
2043 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
2047 // Casting to jchar here is ok since we are looking at an ASCII
2049 type_val rt = get_type_val_for_signature (jchar (v));
2051 if (arraycount == 0)
2053 // Callers of this function eventually push their arguments on
2054 // the stack. So, promote them here.
2055 return type (rt).promote ();
2058 jclass k = construct_primitive_array_type (rt);
2059 while (--arraycount > 0)
2060 k = _Jv_GetArrayClass (k, NULL);
2064 void compute_argument_types (_Jv_Utf8Const *signature,
2067 char *p = signature->data;
2073 types[i++] = get_one_type (p);
2076 type compute_return_type (_Jv_Utf8Const *signature)
2078 char *p = signature->data;
2082 return get_one_type (p);
2085 void check_return_type (type onstack)
2087 type rt = compute_return_type (current_method->self->signature);
2088 if (! rt.compatible (onstack, this))
2089 verify_fail ("incompatible return type");
2092 // Initialize the stack for the new method. Returns true if this
2093 // method is an instance initializer.
2094 bool initialize_stack ()
2097 bool is_init = _Jv_equalUtf8Consts (current_method->self->name,
2099 bool is_clinit = _Jv_equalUtf8Consts (current_method->self->name,
2102 using namespace java::lang::reflect;
2103 if (! Modifier::isStatic (current_method->self->accflags))
2105 type kurr (current_class);
2108 kurr.set_uninitialized (type::SELF, this);
2112 verify_fail ("<clinit> method must be static");
2113 set_variable (0, kurr);
2114 current_state->set_this_type (kurr);
2120 verify_fail ("<init> method must be non-static");
2123 // We have to handle wide arguments specially here.
2124 int arg_count = _Jv_count_arguments (current_method->self->signature);
2125 type arg_types[arg_count];
2126 compute_argument_types (current_method->self->signature, arg_types);
2127 for (int i = 0; i < arg_count; ++i)
2129 set_variable (var, arg_types[i]);
2131 if (arg_types[i].iswide ())
2138 void verify_instructions_0 ()
2140 current_state = new state (current_method->max_stack,
2141 current_method->max_locals);
2146 // True if we are verifying an instance initializer.
2147 bool this_is_init = initialize_stack ();
2149 states = (state **) _Jv_Malloc (sizeof (state *)
2150 * current_method->code_length);
2151 for (int i = 0; i < current_method->code_length; ++i)
2154 next_verify_pc = state::NO_NEXT;
2158 // If the PC was invalidated, get a new one from the work list.
2159 if (PC == state::NO_NEXT)
2162 if (PC == state::INVALID)
2163 verify_fail ("can't happen: saw state::INVALID");
2164 if (PC == state::NO_NEXT)
2166 debug_print ("== State pop from pending list\n");
2167 // Set up the current state.
2168 current_state->copy (states[PC], current_method->max_stack,
2169 current_method->max_locals);
2173 // Control can't fall off the end of the bytecode. We
2174 // only need to check this in the fall-through case,
2175 // because branch bounds are checked when they are
2177 if (PC >= current_method->code_length)
2178 verify_fail ("fell off end");
2180 // We only have to do this checking in the situation where
2181 // control flow falls through from the previous
2182 // instruction. Otherwise merging is done at the time we
2184 if (states[PC] != NULL)
2186 // We've already visited this instruction. So merge
2187 // the states together. If this yields no change then
2188 // we don't have to re-verify. However, if the new
2189 // state is an the result of an unmerged `ret', we
2190 // must continue through it.
2191 debug_print ("== Fall through merge\n");
2192 states[PC]->print ("Old", PC, current_method->max_stack,
2193 current_method->max_locals);
2194 current_state->print ("Cur", PC, current_method->max_stack,
2195 current_method->max_locals);
2196 if (! current_state->merge (states[PC], false,
2197 current_method->max_locals, this)
2198 && ! states[PC]->is_unmerged_ret_state (current_method->max_locals))
2200 debug_print ("== Fall through optimization\n");
2204 // Save a copy of it for later.
2205 states[PC]->copy (current_state, current_method->max_stack,
2206 current_method->max_locals);
2207 current_state->print ("New", PC, current_method->max_stack,
2208 current_method->max_locals);
2212 // We only have to keep saved state at branch targets. If
2213 // we're at a branch target and the state here hasn't been set
2214 // yet, we set it now.
2215 if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
2217 states[PC] = new state (current_state, current_method->max_stack,
2218 current_method->max_locals);
2221 // Set this before handling exceptions so that debug output is
2225 // Update states for all active exception handlers. Ordinarily
2226 // there are not many exception handlers. So we simply run
2227 // through them all.
2228 for (int i = 0; i < current_method->exc_count; ++i)
2230 if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
2232 type handler (&java::lang::Throwable::class$);
2233 if (exception[i].handler_type.i != 0)
2234 handler = check_class_constant (exception[i].handler_type.i);
2235 push_exception_jump (handler, exception[i].handler_pc.i);
2239 current_state->print (" ", PC, current_method->max_stack,
2240 current_method->max_locals);
2241 java_opcode opcode = (java_opcode) bytecode[PC++];
2247 case op_aconst_null:
2248 push_type (null_type);
2258 push_type (int_type);
2263 push_type (long_type);
2269 push_type (float_type);
2274 push_type (double_type);
2279 push_type (int_type);
2284 push_type (int_type);
2288 push_type (check_constant (get_byte ()));
2291 push_type (check_constant (get_ushort ()));
2294 push_type (check_wide_constant (get_ushort ()));
2298 push_type (get_variable (get_byte (), int_type));
2301 push_type (get_variable (get_byte (), long_type));
2304 push_type (get_variable (get_byte (), float_type));
2307 push_type (get_variable (get_byte (), double_type));
2310 push_type (get_variable (get_byte (), reference_type));
2317 push_type (get_variable (opcode - op_iload_0, int_type));
2323 push_type (get_variable (opcode - op_lload_0, long_type));
2329 push_type (get_variable (opcode - op_fload_0, float_type));
2335 push_type (get_variable (opcode - op_dload_0, double_type));
2341 push_type (get_variable (opcode - op_aload_0, reference_type));
2344 pop_type (int_type);
2345 push_type (require_array_type (pop_init_ref (reference_type),
2349 pop_type (int_type);
2350 push_type (require_array_type (pop_init_ref (reference_type),
2354 pop_type (int_type);
2355 push_type (require_array_type (pop_init_ref (reference_type),
2359 pop_type (int_type);
2360 push_type (require_array_type (pop_init_ref (reference_type),
2364 pop_type (int_type);
2365 push_type (require_array_type (pop_init_ref (reference_type),
2369 pop_type (int_type);
2370 require_array_type (pop_init_ref (reference_type), byte_type);
2371 push_type (int_type);
2374 pop_type (int_type);
2375 require_array_type (pop_init_ref (reference_type), char_type);
2376 push_type (int_type);
2379 pop_type (int_type);
2380 require_array_type (pop_init_ref (reference_type), short_type);
2381 push_type (int_type);
2384 set_variable (get_byte (), pop_type (int_type));
2387 set_variable (get_byte (), pop_type (long_type));
2390 set_variable (get_byte (), pop_type (float_type));
2393 set_variable (get_byte (), pop_type (double_type));
2396 set_variable (get_byte (), pop_ref_or_return ());
2402 set_variable (opcode - op_istore_0, pop_type (int_type));
2408 set_variable (opcode - op_lstore_0, pop_type (long_type));
2414 set_variable (opcode - op_fstore_0, pop_type (float_type));
2420 set_variable (opcode - op_dstore_0, pop_type (double_type));
2426 set_variable (opcode - op_astore_0, pop_ref_or_return ());
2429 pop_type (int_type);
2430 pop_type (int_type);
2431 require_array_type (pop_init_ref (reference_type), int_type);
2434 pop_type (long_type);
2435 pop_type (int_type);
2436 require_array_type (pop_init_ref (reference_type), long_type);
2439 pop_type (float_type);
2440 pop_type (int_type);
2441 require_array_type (pop_init_ref (reference_type), float_type);
2444 pop_type (double_type);
2445 pop_type (int_type);
2446 require_array_type (pop_init_ref (reference_type), double_type);
2449 pop_type (reference_type);
2450 pop_type (int_type);
2451 require_array_type (pop_init_ref (reference_type), reference_type);
2454 pop_type (int_type);
2455 pop_type (int_type);
2456 require_array_type (pop_init_ref (reference_type), byte_type);
2459 pop_type (int_type);
2460 pop_type (int_type);
2461 require_array_type (pop_init_ref (reference_type), char_type);
2464 pop_type (int_type);
2465 pop_type (int_type);
2466 require_array_type (pop_init_ref (reference_type), short_type);
2473 type t = pop_raw ();
2497 type t2 = pop_raw ();
2512 type t = pop_raw ();
2527 type t1 = pop_raw ();
2544 type t1 = pop_raw ();
2547 type t2 = pop_raw ();
2565 type t3 = pop_raw ();
2603 pop_type (int_type);
2604 push_type (pop_type (int_type));
2614 pop_type (long_type);
2615 push_type (pop_type (long_type));
2620 pop_type (int_type);
2621 push_type (pop_type (long_type));
2628 pop_type (float_type);
2629 push_type (pop_type (float_type));
2636 pop_type (double_type);
2637 push_type (pop_type (double_type));
2643 push_type (pop_type (int_type));
2646 push_type (pop_type (long_type));
2649 push_type (pop_type (float_type));
2652 push_type (pop_type (double_type));
2655 get_variable (get_byte (), int_type);
2659 pop_type (int_type);
2660 push_type (long_type);
2663 pop_type (int_type);
2664 push_type (float_type);
2667 pop_type (int_type);
2668 push_type (double_type);
2671 pop_type (long_type);
2672 push_type (int_type);
2675 pop_type (long_type);
2676 push_type (float_type);
2679 pop_type (long_type);
2680 push_type (double_type);
2683 pop_type (float_type);
2684 push_type (int_type);
2687 pop_type (float_type);
2688 push_type (long_type);
2691 pop_type (float_type);
2692 push_type (double_type);
2695 pop_type (double_type);
2696 push_type (int_type);
2699 pop_type (double_type);
2700 push_type (long_type);
2703 pop_type (double_type);
2704 push_type (float_type);
2707 pop_type (long_type);
2708 pop_type (long_type);
2709 push_type (int_type);
2713 pop_type (float_type);
2714 pop_type (float_type);
2715 push_type (int_type);
2719 pop_type (double_type);
2720 pop_type (double_type);
2721 push_type (int_type);
2729 pop_type (int_type);
2730 push_jump (get_short ());
2738 pop_type (int_type);
2739 pop_type (int_type);
2740 push_jump (get_short ());
2744 pop_type (reference_type);
2745 pop_type (reference_type);
2746 push_jump (get_short ());
2749 push_jump (get_short ());
2753 handle_jsr_insn (get_short ());
2756 handle_ret_insn (get_byte ());
2758 case op_tableswitch:
2760 pop_type (int_type);
2762 push_jump (get_int ());
2763 jint low = get_int ();
2764 jint high = get_int ();
2765 // Already checked LOW -vs- HIGH.
2766 for (int i = low; i <= high; ++i)
2767 push_jump (get_int ());
2772 case op_lookupswitch:
2774 pop_type (int_type);
2776 push_jump (get_int ());
2777 jint npairs = get_int ();
2778 // Already checked NPAIRS >= 0.
2780 for (int i = 0; i < npairs; ++i)
2782 jint key = get_int ();
2783 if (i > 0 && key <= lastkey)
2784 verify_fail ("lookupswitch pairs unsorted", start_PC);
2786 push_jump (get_int ());
2792 check_return_type (pop_type (int_type));
2796 check_return_type (pop_type (long_type));
2800 check_return_type (pop_type (float_type));
2804 check_return_type (pop_type (double_type));
2808 check_return_type (pop_init_ref (reference_type));
2812 // We only need to check this when the return type is
2813 // void, because all instance initializers return void.
2815 current_state->check_this_initialized (this);
2816 check_return_type (void_type);
2820 push_type (check_field_constant (get_ushort ()));
2823 pop_type (check_field_constant (get_ushort ()));
2828 type field = check_field_constant (get_ushort (), &klass);
2836 type field = check_field_constant (get_ushort (), &klass);
2839 // We have an obscure special case here: we can use
2840 // `putfield' on a field declared in this class, even if
2841 // `this' has not yet been initialized.
2842 if (! current_state->this_type.isinitialized ()
2843 && current_state->this_type.pc == type::SELF)
2844 klass.set_uninitialized (type::SELF, this);
2849 case op_invokevirtual:
2850 case op_invokespecial:
2851 case op_invokestatic:
2852 case op_invokeinterface:
2854 _Jv_Utf8Const *method_name, *method_signature;
2856 = check_method_constant (get_ushort (),
2857 opcode == op_invokeinterface,
2860 // NARGS is only used when we're processing
2861 // invokeinterface. It is simplest for us to compute it
2862 // here and then verify it later.
2864 if (opcode == op_invokeinterface)
2866 nargs = get_byte ();
2867 if (get_byte () != 0)
2868 verify_fail ("invokeinterface dummy byte is wrong");
2871 bool is_init = false;
2872 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2875 if (opcode != op_invokespecial)
2876 verify_fail ("can't invoke <init>");
2878 else if (method_name->data[0] == '<')
2879 verify_fail ("can't invoke method starting with `<'");
2881 // Pop arguments and check types.
2882 int arg_count = _Jv_count_arguments (method_signature);
2883 type arg_types[arg_count];
2884 compute_argument_types (method_signature, arg_types);
2885 for (int i = arg_count - 1; i >= 0; --i)
2887 // This is only used for verifying the byte for
2889 nargs -= arg_types[i].depth ();
2890 pop_init_ref (arg_types[i]);
2893 if (opcode == op_invokeinterface
2895 verify_fail ("wrong argument count for invokeinterface");
2897 if (opcode != op_invokestatic)
2899 type t = class_type;
2902 // In this case the PC doesn't matter.
2903 t.set_uninitialized (type::UNINIT, this);
2905 type raw = pop_raw ();
2907 if (! is_init && ! raw.isinitialized ())
2909 // This is a failure.
2911 else if (is_init && raw.isnull ())
2915 else if (t.compatible (raw, this))
2919 else if (opcode == op_invokeinterface)
2921 // This is a hack. We might have merged two
2922 // items and gotten `Object'. This can happen
2923 // because we don't keep track of where merges
2924 // come from. This is safe as long as the
2925 // interpreter checks interfaces at runtime.
2926 type obj (&java::lang::Object::class$);
2927 ok = raw.compatible (obj, this);
2931 verify_fail ("incompatible type on stack");
2934 current_state->set_initialized (raw.get_pc (),
2935 current_method->max_locals);
2938 type rt = compute_return_type (method_signature);
2946 type t = check_class_constant (get_ushort ());
2947 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2948 verify_fail ("type is array, interface, or abstract");
2949 t.set_uninitialized (start_PC, this);
2956 int atype = get_byte ();
2957 // We intentionally have chosen constants to make this
2959 if (atype < boolean_type || atype > long_type)
2960 verify_fail ("type not primitive", start_PC);
2961 pop_type (int_type);
2962 push_type (construct_primitive_array_type (type_val (atype)));
2966 pop_type (int_type);
2967 push_type (check_class_constant (get_ushort ()).to_array (this));
2969 case op_arraylength:
2971 type t = pop_init_ref (reference_type);
2972 if (! t.isarray () && ! t.isnull ())
2973 verify_fail ("array type expected");
2974 push_type (int_type);
2978 pop_type (type (&java::lang::Throwable::class$));
2982 pop_init_ref (reference_type);
2983 push_type (check_class_constant (get_ushort ()));
2986 pop_init_ref (reference_type);
2987 check_class_constant (get_ushort ());
2988 push_type (int_type);
2990 case op_monitorenter:
2991 pop_init_ref (reference_type);
2993 case op_monitorexit:
2994 pop_init_ref (reference_type);
2998 switch (get_byte ())
3001 push_type (get_variable (get_ushort (), int_type));
3004 push_type (get_variable (get_ushort (), long_type));
3007 push_type (get_variable (get_ushort (), float_type));
3010 push_type (get_variable (get_ushort (), double_type));
3013 push_type (get_variable (get_ushort (), reference_type));
3016 set_variable (get_ushort (), pop_type (int_type));
3019 set_variable (get_ushort (), pop_type (long_type));
3022 set_variable (get_ushort (), pop_type (float_type));
3025 set_variable (get_ushort (), pop_type (double_type));
3028 set_variable (get_ushort (), pop_init_ref (reference_type));
3031 handle_ret_insn (get_short ());
3034 get_variable (get_ushort (), int_type);
3038 verify_fail ("unrecognized wide instruction", start_PC);
3042 case op_multianewarray:
3044 type atype = check_class_constant (get_ushort ());
3045 int dim = get_byte ();
3047 verify_fail ("too few dimensions to multianewarray", start_PC);
3048 atype.verify_dimensions (dim, this);
3049 for (int i = 0; i < dim; ++i)
3050 pop_type (int_type);
3056 pop_type (reference_type);
3057 push_jump (get_short ());
3060 push_jump (get_int ());
3064 handle_jsr_insn (get_int ());
3067 // These are unused here, but we call them out explicitly
3068 // so that -Wswitch-enum doesn't complain.
3074 case op_putstatic_1:
3075 case op_putstatic_2:
3076 case op_putstatic_4:
3077 case op_putstatic_8:
3078 case op_putstatic_a:
3080 case op_getfield_2s:
3081 case op_getfield_2u:
3085 case op_getstatic_1:
3086 case op_getstatic_2s:
3087 case op_getstatic_2u:
3088 case op_getstatic_4:
3089 case op_getstatic_8:
3090 case op_getstatic_a:
3092 // Unrecognized opcode.
3093 verify_fail ("unrecognized instruction in verify_instructions_0",
3101 void verify_instructions ()
3104 verify_instructions_0 ();
3107 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3109 // We just print the text as utf-8. This is just for debugging
3111 debug_print ("--------------------------------\n");
3112 debug_print ("-- Verifying method `%s'\n", m->self->name->data);
3115 bytecode = m->bytecode ();
3116 exception = m->exceptions ();
3117 current_class = m->defining_class;
3123 entry_points = NULL;
3126 ~_Jv_BytecodeVerifier ()
3135 for (int i = 0; i < current_method->code_length; ++i)
3137 if (jsr_ptrs[i] != NULL)
3139 subr_info *info = jsr_ptrs[i];
3140 while (info != NULL)
3142 subr_info *next = info->next;
3148 _Jv_Free (jsr_ptrs);
3151 while (utf8_list != NULL)
3153 linked_utf8 *n = utf8_list->next;
3154 _Jv_Free (utf8_list->val);
3155 _Jv_Free (utf8_list);
3159 while (entry_points != NULL)
3161 subr_entry_info *next = entry_points->next;
3162 _Jv_Free (entry_points);
3163 entry_points = next;
3169 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3171 _Jv_BytecodeVerifier v (meth);
3172 v.verify_instructions ();
3174 #endif /* INTERPRETER */