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 __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 like _Jv_IsAssignableFrom, but it works even if SOURCE or
244 // TARGET haven't been prepared.
245 static bool is_assignable_from_slow (jclass target, jclass source)
247 // This will terminate when SOURCE==Object.
250 if (source == target)
253 if (target->isPrimitive () || source->isPrimitive ())
256 if (target->isArray ())
258 if (! source->isArray ())
260 target = target->getComponentType ();
261 source = source->getComponentType ();
263 else if (target->isInterface ())
265 for (int i = 0; i < source->interface_count; ++i)
267 // We use a recursive call because we also need to
268 // check superinterfaces.
269 if (is_assignable_from_slow (target, source->interfaces[i]))
272 source = source->getSuperclass ();
276 // We must do this check before we check to see if SOURCE is
277 // an interface. This way we know that any interface is
278 // assignable to an Object.
279 else if (target == &java::lang::Object::class$)
281 else if (source->isInterface ())
283 for (int i = 0; i < target->interface_count; ++i)
285 // We use a recursive call because we also need to
286 // check superinterfaces.
287 if (is_assignable_from_slow (target->interfaces[i], source))
290 target = target->getSuperclass ();
294 else if (source == &java::lang::Object::class$)
297 source = source->getSuperclass ();
301 // This is used to keep track of which `jsr's correspond to a given
305 // PC of the instruction just after the jsr.
311 // This is used to keep track of which subroutine entry point
312 // corresponds to which `ret' instruction.
313 struct subr_entry_info
315 // PC of the subroutine entry point.
317 // PC of the `ret' instruction.
320 subr_entry_info *next;
323 // The `type' class is used to represent a single type in the
329 // Some associated data.
332 // For a resolved reference type, this is a pointer to the class.
334 // For other reference types, this it the name of the class.
337 // This is used when constructing a new object. It is the PC of the
338 // `new' instruction which created the object. We use the special
339 // value -2 to mean that this is uninitialized, and the special
340 // value -1 for the case where the current method is itself the
344 static const int UNINIT = -2;
345 static const int SELF = -1;
347 // Basic constructor.
350 key = unsuitable_type;
355 // Make a new instance given the type tag. We assume a generic
356 // `reference_type' means Object.
361 if (key == reference_type)
362 data.klass = &java::lang::Object::class$;
366 // Make a new instance given a class.
369 key = reference_type;
374 // Make a new instance given the name of a class.
375 type (_Jv_Utf8Const *n)
377 key = unresolved_reference_type;
390 // These operators are required because libgcj can't link in
392 void *operator new[] (size_t bytes)
394 return _Jv_Malloc (bytes);
397 void operator delete[] (void *mem)
402 type& operator= (type_val k)
410 type& operator= (const type& t)
418 // Promote a numeric type.
421 if (key == boolean_type || key == char_type
422 || key == byte_type || key == short_type)
427 // If *THIS is an unresolved reference type, resolve it.
428 void resolve (_Jv_BytecodeVerifier *verifier)
430 if (key != unresolved_reference_type
431 && key != uninitialized_unresolved_reference_type)
434 using namespace java::lang;
435 java::lang::ClassLoader *loader
436 = verifier->current_class->getClassLoaderInternal();
437 // We might see either kind of name. Sigh.
438 if (data.name->data[0] == 'L'
439 && data.name->data[data.name->length - 1] == ';')
440 data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
442 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
444 key = (key == unresolved_reference_type
446 : uninitialized_reference_type);
449 // Mark this type as the uninitialized result of `new'.
450 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
452 if (key == reference_type)
453 key = uninitialized_reference_type;
454 else if (key == unresolved_reference_type)
455 key = uninitialized_unresolved_reference_type;
457 verifier->verify_fail ("internal error in type::uninitialized");
461 // Mark this type as now initialized.
462 void set_initialized (int npc)
464 if (npc != UNINIT && pc == npc
465 && (key == uninitialized_reference_type
466 || key == uninitialized_unresolved_reference_type))
468 key = (key == uninitialized_reference_type
470 : unresolved_reference_type);
476 // Return true if an object of type K can be assigned to a variable
477 // of type *THIS. Handle various special cases too. Might modify
478 // *THIS or K. Note however that this does not perform numeric
480 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
482 // Any type is compatible with the unsuitable type.
483 if (key == unsuitable_type)
486 if (key < reference_type || k.key < reference_type)
489 // The `null' type is convertible to any initialized reference
491 if (key == null_type || k.key == null_type)
494 // Any reference type is convertible to Object. This is a special
495 // case so we don't need to unnecessarily resolve a class.
496 if (key == reference_type
497 && data.klass == &java::lang::Object::class$)
500 // An initialized type and an uninitialized type are not
502 if (isinitialized () != k.isinitialized ())
505 // Two uninitialized objects are compatible if either:
506 // * The PCs are identical, or
507 // * One PC is UNINIT.
508 if (! isinitialized ())
510 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
514 // Two unresolved types are equal if their names are the same.
517 && _Jv_equalUtf8Consts (data.name, k.data.name))
520 // We must resolve both types and check assignability.
522 k.resolve (verifier);
523 return is_assignable_from_slow (data.klass, k.data.klass);
528 return key == void_type;
533 return key == long_type || key == double_type;
536 // Return number of stack or local variable slots taken by this
540 return iswide () ? 2 : 1;
543 bool isarray () const
545 // We treat null_type as not an array. This is ok based on the
546 // current uses of this method.
547 if (key == reference_type)
548 return data.klass->isArray ();
549 else if (key == unresolved_reference_type)
550 return data.name->data[0] == '[';
556 return key == null_type;
559 bool isinterface (_Jv_BytecodeVerifier *verifier)
562 if (key != reference_type)
564 return data.klass->isInterface ();
567 bool isabstract (_Jv_BytecodeVerifier *verifier)
570 if (key != reference_type)
572 using namespace java::lang::reflect;
573 return Modifier::isAbstract (data.klass->getModifiers ());
576 // Return the element type of an array.
577 type element_type (_Jv_BytecodeVerifier *verifier)
579 // FIXME: maybe should do string manipulation here.
581 if (key != reference_type)
582 verifier->verify_fail ("programmer error in type::element_type()", -1);
584 jclass k = data.klass->getComponentType ();
585 if (k->isPrimitive ())
586 return type (verifier->get_type_val_for_signature (k));
590 // Return the array type corresponding to an initialized
591 // reference. We could expand this to work for other kinds of
592 // types, but currently we don't need to.
593 type to_array (_Jv_BytecodeVerifier *verifier)
595 // Resolving isn't ideal, because it might force us to load
596 // another class, but it's easy. FIXME?
597 if (key == unresolved_reference_type)
600 if (key == reference_type)
601 return type (_Jv_GetArrayClass (data.klass,
602 data.klass->getClassLoaderInternal()));
604 verifier->verify_fail ("internal error in type::to_array()");
607 bool isreference () const
609 return key >= reference_type;
617 bool isinitialized () const
619 return (key == reference_type
621 || key == unresolved_reference_type);
624 bool isresolved () const
626 return (key == reference_type
628 || key == uninitialized_reference_type);
631 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
633 // The way this is written, we don't need to check isarray().
634 if (key == reference_type)
636 jclass k = data.klass;
637 while (k->isArray () && ndims > 0)
639 k = k->getComponentType ();
645 // We know KEY == unresolved_reference_type.
646 char *p = data.name->data;
647 while (*p++ == '[' && ndims-- > 0)
652 verifier->verify_fail ("array type has fewer dimensions than required");
655 // Merge OLD_TYPE into this. On error throw exception.
656 bool merge (type& old_type, bool local_semantics,
657 _Jv_BytecodeVerifier *verifier)
659 bool changed = false;
660 bool refo = old_type.isreference ();
661 bool refn = isreference ();
664 if (old_type.key == null_type)
666 else if (key == null_type)
671 else if (isinitialized () != old_type.isinitialized ())
672 verifier->verify_fail ("merging initialized and uninitialized types");
675 if (! isinitialized ())
679 else if (old_type.pc == UNINIT)
681 else if (pc != old_type.pc)
682 verifier->verify_fail ("merging different uninitialized types");
686 && ! old_type.isresolved ()
687 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
689 // Types are identical.
694 old_type.resolve (verifier);
696 jclass k = data.klass;
697 jclass oldk = old_type.data.klass;
700 while (k->isArray () && oldk->isArray ())
703 k = k->getComponentType ();
704 oldk = oldk->getComponentType ();
707 // Ordinarily this terminates when we hit Object...
710 if (is_assignable_from_slow (k, oldk))
712 k = k->getSuperclass ();
715 // ... but K could have been an interface, in which
716 // case we'll end up here. We just convert this
719 k = &java::lang::Object::class$;
723 while (arraycount > 0)
725 java::lang::ClassLoader *loader
726 = verifier->current_class->getClassLoaderInternal();
727 k = _Jv_GetArrayClass (k, loader);
735 else if (refo || refn || key != old_type.key)
739 // If we're merging into an "unused" slot, then we
740 // simply accept whatever we're merging from.
741 if (key == unused_by_subroutine_type)
746 else if (old_type.key == unused_by_subroutine_type)
750 // If we already have an `unsuitable' type, then we
751 // don't need to change again.
752 else if (key != unsuitable_type)
754 key = unsuitable_type;
759 verifier->verify_fail ("unmergeable type");
765 void print (void) const
770 case boolean_type: c = 'Z'; break;
771 case byte_type: c = 'B'; break;
772 case char_type: c = 'C'; break;
773 case short_type: c = 'S'; break;
774 case int_type: c = 'I'; break;
775 case long_type: c = 'J'; break;
776 case float_type: c = 'F'; break;
777 case double_type: c = 'D'; break;
778 case void_type: c = 'V'; break;
779 case unsuitable_type: c = '-'; break;
780 case return_address_type: c = 'r'; break;
781 case continuation_type: c = '+'; break;
782 case unused_by_subroutine_type: c = '_'; break;
783 case reference_type: c = 'L'; break;
784 case null_type: c = '@'; break;
785 case unresolved_reference_type: c = 'l'; break;
786 case uninitialized_reference_type: c = 'U'; break;
787 case uninitialized_unresolved_reference_type: c = 'u'; break;
789 debug_print ("%c", c);
791 #endif /* VERIFY_DEBUG */
794 // This class holds all the state information we need for a given
798 // The current top of the stack, in terms of slots.
800 // The current depth of the stack. This will be larger than
801 // STACKTOP when wide types are on the stack.
805 // The local variables.
807 // This is used in subroutines to keep track of which local
808 // variables have been accessed.
810 // If not 0, then we are in a subroutine. The value is the PC of
811 // the subroutine's entry point. We can use 0 as an exceptional
812 // value because PC=0 can never be a subroutine.
814 // This is used to keep a linked list of all the states which
815 // require re-verification. We use the PC to keep track.
817 // We keep track of the type of `this' specially. This is used to
818 // ensure that an instance initializer invokes another initializer
819 // on `this' before returning. We must keep track of this
820 // specially because otherwise we might be confused by code which
821 // assigns to locals[0] (overwriting `this') and then returns
822 // without really initializing.
825 // INVALID marks a state which is not on the linked list of states
826 // requiring reverification.
827 static const int INVALID = -1;
828 // NO_NEXT marks the state at the end of the reverification list.
829 static const int NO_NEXT = -2;
831 // This is used to mark the stack depth at the instruction just
832 // after a `jsr' when we haven't yet processed the corresponding
833 // `ret'. See handle_jsr_insn for more information.
834 static const int NO_STACK = -1;
841 local_changed = NULL;
844 state (int max_stack, int max_locals)
849 stack = new type[max_stack];
850 for (int i = 0; i < max_stack; ++i)
851 stack[i] = unsuitable_type;
852 locals = new type[max_locals];
853 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
854 for (int i = 0; i < max_locals; ++i)
856 locals[i] = unsuitable_type;
857 local_changed[i] = false;
863 state (const state *orig, int max_stack, int max_locals,
864 bool ret_semantics = false)
866 stack = new type[max_stack];
867 locals = new type[max_locals];
868 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
869 copy (orig, max_stack, max_locals, ret_semantics);
880 _Jv_Free (local_changed);
883 void *operator new[] (size_t bytes)
885 return _Jv_Malloc (bytes);
888 void operator delete[] (void *mem)
893 void *operator new (size_t bytes)
895 return _Jv_Malloc (bytes);
898 void operator delete (void *mem)
903 void copy (const state *copy, int max_stack, int max_locals,
904 bool ret_semantics = false)
906 stacktop = copy->stacktop;
907 stackdepth = copy->stackdepth;
908 subroutine = copy->subroutine;
909 for (int i = 0; i < max_stack; ++i)
910 stack[i] = copy->stack[i];
911 for (int i = 0; i < max_locals; ++i)
913 // See push_jump_merge to understand this case.
915 locals[i] = type (copy->local_changed[i]
917 : unused_by_subroutine_type);
919 locals[i] = copy->locals[i];
920 local_changed[i] = copy->local_changed[i];
922 this_type = copy->this_type;
923 // Don't modify `next'.
926 // Modify this state to reflect entry to an exception handler.
927 void set_exception (type t, int max_stack)
932 for (int i = stacktop; i < max_stack; ++i)
933 stack[i] = unsuitable_type;
936 // Modify this state to reflect entry into a subroutine.
937 void enter_subroutine (int npc, int max_locals)
940 // Mark all items as unchanged. Each subroutine needs to keep
941 // track of its `changed' state independently. In the case of
942 // nested subroutines, this information will be merged back into
943 // parent by the `ret'.
944 for (int i = 0; i < max_locals; ++i)
945 local_changed[i] = false;
948 // Merge STATE_OLD into this state. Destructively modifies this
949 // state. Returns true if the new state was in fact changed.
950 // Will throw an exception if the states are not mergeable.
951 bool merge (state *state_old, bool ret_semantics,
952 int max_locals, _Jv_BytecodeVerifier *verifier)
954 bool changed = false;
956 // Special handling for `this'. If one or the other is
957 // uninitialized, then the merge is uninitialized.
958 if (this_type.isinitialized ())
959 this_type = state_old->this_type;
961 // Merge subroutine states. Here we just keep track of what
962 // subroutine we think we're in. We only check for a merge
963 // (which is invalid) when we see a `ret'.
964 if (subroutine == state_old->subroutine)
968 else if (subroutine == 0)
970 subroutine = state_old->subroutine;
975 // If the subroutines differ, indicate that the state
976 // changed. This is needed to detect when subroutines have
981 // Merge stacks. Special handling for NO_STACK case.
982 if (state_old->stacktop == NO_STACK)
984 // Nothing to do in this case; we don't care about modifying
987 else if (stacktop == NO_STACK)
989 stacktop = state_old->stacktop;
990 stackdepth = state_old->stackdepth;
991 for (int i = 0; i < stacktop; ++i)
992 stack[i] = state_old->stack[i];
995 else if (state_old->stacktop != stacktop)
996 verifier->verify_fail ("stack sizes differ");
999 for (int i = 0; i < state_old->stacktop; ++i)
1001 if (stack[i].merge (state_old->stack[i], false, verifier))
1006 // Merge local variables.
1007 for (int i = 0; i < max_locals; ++i)
1009 // If we're not processing a `ret', then we merge every
1010 // local variable. If we are processing a `ret', then we
1011 // only merge locals which changed in the subroutine. When
1012 // processing a `ret', STATE_OLD is the state at the point
1013 // of the `ret', and THIS is the state just after the `jsr'.
1014 if (! ret_semantics || state_old->local_changed[i])
1016 if (locals[i].merge (state_old->locals[i], true, verifier))
1018 // Note that we don't call `note_variable' here.
1019 // This change doesn't represent a real change to a
1020 // local, but rather a merge artifact. If we're in
1021 // a subroutine which is called with two
1022 // incompatible types in a slot that is unused by
1023 // the subroutine, then we don't want to mark that
1024 // variable as having been modified.
1029 // If we're in a subroutine, we must compute the union of
1030 // all the changed local variables.
1031 if (state_old->local_changed[i])
1038 // Throw an exception if there is an uninitialized object on the
1039 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1040 // whether we're using backwards-branch or exception-handing
1042 void check_no_uninitialized_objects (int max_locals,
1043 _Jv_BytecodeVerifier *verifier,
1044 bool exception_semantics = false)
1046 if (! exception_semantics)
1048 for (int i = 0; i < stacktop; ++i)
1049 if (stack[i].isreference () && ! stack[i].isinitialized ())
1050 verifier->verify_fail ("uninitialized object on stack");
1053 for (int i = 0; i < max_locals; ++i)
1054 if (locals[i].isreference () && ! locals[i].isinitialized ())
1055 verifier->verify_fail ("uninitialized object in local variable");
1057 check_this_initialized (verifier);
1060 // Ensure that `this' has been initialized.
1061 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1063 if (this_type.isreference () && ! this_type.isinitialized ())
1064 verifier->verify_fail ("`this' is uninitialized");
1067 // Set type of `this'.
1068 void set_this_type (const type &k)
1073 // Note that a local variable was modified.
1074 void note_variable (int index)
1077 local_changed[index] = true;
1080 // Mark each `new'd object we know of that was allocated at PC as
1082 void set_initialized (int pc, int max_locals)
1084 for (int i = 0; i < stacktop; ++i)
1085 stack[i].set_initialized (pc);
1086 for (int i = 0; i < max_locals; ++i)
1087 locals[i].set_initialized (pc);
1088 this_type.set_initialized (pc);
1091 // Return true if this state is the unmerged result of a `ret'.
1092 bool is_unmerged_ret_state (int max_locals) const
1094 if (stacktop == NO_STACK)
1096 for (int i = 0; i < max_locals; ++i)
1098 if (locals[i].key == unused_by_subroutine_type)
1105 void print (const char *leader, int pc,
1106 int max_stack, int max_locals) const
1108 debug_print ("%s [%4d]: [stack] ", leader, pc);
1110 for (i = 0; i < stacktop; ++i)
1112 for (; i < max_stack; ++i)
1114 debug_print (" [local] ");
1115 for (i = 0; i < max_locals; ++i)
1118 debug_print (local_changed[i] ? "+" : " ");
1120 if (subroutine == 0)
1121 debug_print (" | None");
1123 debug_print (" | %4d", subroutine);
1124 debug_print (" | %p\n", this);
1127 inline void print (const char *, int, int, int) const
1130 #endif /* VERIFY_DEBUG */
1135 if (current_state->stacktop <= 0)
1136 verify_fail ("stack empty");
1137 type r = current_state->stack[--current_state->stacktop];
1138 current_state->stackdepth -= r.depth ();
1139 if (current_state->stackdepth < 0)
1140 verify_fail ("stack empty", start_PC);
1146 type r = pop_raw ();
1148 verify_fail ("narrow pop of wide type");
1154 type r = pop_raw ();
1156 verify_fail ("wide pop of narrow type");
1160 type pop_type (type match)
1163 type t = pop_raw ();
1164 if (! match.compatible (t, this))
1165 verify_fail ("incompatible type on stack");
1169 // Pop a reference which is guaranteed to be initialized. MATCH
1170 // doesn't have to be a reference type; in this case this acts like
1172 type pop_init_ref (type match)
1174 type t = pop_raw ();
1175 if (t.isreference () && ! t.isinitialized ())
1176 verify_fail ("initialized reference required");
1177 else if (! match.compatible (t, this))
1178 verify_fail ("incompatible type on stack");
1182 // Pop a reference type or a return address.
1183 type pop_ref_or_return ()
1185 type t = pop_raw ();
1186 if (! t.isreference () && t.key != return_address_type)
1187 verify_fail ("expected reference or return address on stack");
1191 void push_type (type t)
1193 // If T is a numeric type like short, promote it to int.
1196 int depth = t.depth ();
1197 if (current_state->stackdepth + depth > current_method->max_stack)
1198 verify_fail ("stack overflow");
1199 current_state->stack[current_state->stacktop++] = t;
1200 current_state->stackdepth += depth;
1203 void set_variable (int index, type t)
1205 // If T is a numeric type like short, promote it to int.
1208 int depth = t.depth ();
1209 if (index > current_method->max_locals - depth)
1210 verify_fail ("invalid local variable");
1211 current_state->locals[index] = t;
1212 current_state->note_variable (index);
1216 current_state->locals[index + 1] = continuation_type;
1217 current_state->note_variable (index + 1);
1219 if (index > 0 && current_state->locals[index - 1].iswide ())
1221 current_state->locals[index - 1] = unsuitable_type;
1222 // There's no need to call note_variable here.
1226 type get_variable (int index, type t)
1228 int depth = t.depth ();
1229 if (index > current_method->max_locals - depth)
1230 verify_fail ("invalid local variable");
1231 if (! t.compatible (current_state->locals[index], this))
1232 verify_fail ("incompatible type in local variable");
1235 type t (continuation_type);
1236 if (! current_state->locals[index + 1].compatible (t, this))
1237 verify_fail ("invalid local variable");
1239 return current_state->locals[index];
1242 // Make sure ARRAY is an array type and that its elements are
1243 // compatible with type ELEMENT. Returns the actual element type.
1244 type require_array_type (type array, type element)
1246 // An odd case. Here we just pretend that everything went ok. If
1247 // the requested element type is some kind of reference, return
1248 // the null type instead.
1249 if (array.isnull ())
1250 return element.isreference () ? type (null_type) : element;
1252 if (! array.isarray ())
1253 verify_fail ("array required");
1255 type t = array.element_type (this);
1256 if (! element.compatible (t, this))
1258 // Special case for byte arrays, which must also be boolean
1261 if (element.key == byte_type)
1263 type e2 (boolean_type);
1264 ok = e2.compatible (t, this);
1267 verify_fail ("incompatible array element type");
1270 // Return T and not ELEMENT, because T might be specialized.
1276 if (PC >= current_method->code_length)
1277 verify_fail ("premature end of bytecode");
1278 return (jint) bytecode[PC++] & 0xff;
1283 jint b1 = get_byte ();
1284 jint b2 = get_byte ();
1285 return (jint) ((b1 << 8) | b2) & 0xffff;
1290 jint b1 = get_byte ();
1291 jint b2 = get_byte ();
1292 jshort s = (b1 << 8) | b2;
1298 jint b1 = get_byte ();
1299 jint b2 = get_byte ();
1300 jint b3 = get_byte ();
1301 jint b4 = get_byte ();
1302 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1305 int compute_jump (int offset)
1307 int npc = start_PC + offset;
1308 if (npc < 0 || npc >= current_method->code_length)
1309 verify_fail ("branch out of range", start_PC);
1313 // Merge the indicated state into the state at the branch target and
1314 // schedule a new PC if there is a change. If RET_SEMANTICS is
1315 // true, then we are merging from a `ret' instruction into the
1316 // instruction after a `jsr'. This is a special case with its own
1317 // modified semantics.
1318 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1320 bool changed = true;
1321 if (states[npc] == NULL)
1323 // There's a weird situation here. If are examining the
1324 // branch that results from a `ret', and there is not yet a
1325 // state available at the branch target (the instruction just
1326 // after the `jsr'), then we have to construct a special kind
1327 // of state at that point for future merging. This special
1328 // state has the type `unused_by_subroutine_type' in each slot
1329 // which was not modified by the subroutine.
1330 states[npc] = new state (nstate, current_method->max_stack,
1331 current_method->max_locals, ret_semantics);
1332 debug_print ("== New state in push_jump_merge\n");
1333 states[npc]->print ("New", npc, current_method->max_stack,
1334 current_method->max_locals);
1338 debug_print ("== Merge states in push_jump_merge\n");
1339 nstate->print ("Frm", start_PC, current_method->max_stack,
1340 current_method->max_locals);
1341 states[npc]->print (" To", npc, current_method->max_stack,
1342 current_method->max_locals);
1343 changed = states[npc]->merge (nstate, ret_semantics,
1344 current_method->max_locals, this);
1345 states[npc]->print ("New", npc, current_method->max_stack,
1346 current_method->max_locals);
1349 if (changed && states[npc]->next == state::INVALID)
1351 // The merge changed the state, and the new PC isn't yet on our
1352 // list of PCs to re-verify.
1353 states[npc]->next = next_verify_pc;
1354 next_verify_pc = npc;
1358 void push_jump (int offset)
1360 int npc = compute_jump (offset);
1362 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1363 push_jump_merge (npc, current_state);
1366 void push_exception_jump (type t, int pc)
1368 current_state->check_no_uninitialized_objects (current_method->max_locals,
1370 state s (current_state, current_method->max_stack,
1371 current_method->max_locals);
1372 if (current_method->max_stack < 1)
1373 verify_fail ("stack overflow at exception handler");
1374 s.set_exception (t, current_method->max_stack);
1375 push_jump_merge (pc, &s);
1380 int *prev_loc = &next_verify_pc;
1381 int npc = next_verify_pc;
1382 bool skipped = false;
1384 while (npc != state::NO_NEXT)
1386 // If the next available PC is an unmerged `ret' state, then
1387 // we aren't yet ready to handle it. That's because we would
1388 // need all kind of special cases to do so. So instead we
1389 // defer this jump until after we've processed it via a
1390 // fall-through. This has to happen because the instruction
1391 // before this one must be a `jsr'.
1392 if (! states[npc]->is_unmerged_ret_state (current_method->max_locals))
1394 *prev_loc = states[npc]->next;
1395 states[npc]->next = state::INVALID;
1400 prev_loc = &states[npc]->next;
1401 npc = states[npc]->next;
1404 // Note that we might have gotten here even when there are
1405 // remaining states to process. That can happen if we find a
1406 // `jsr' without a `ret'.
1407 return state::NO_NEXT;
1410 void invalidate_pc ()
1412 PC = state::NO_NEXT;
1415 void note_branch_target (int pc, bool is_jsr_target = false)
1417 // Don't check `pc <= PC', because we've advanced PC after
1418 // fetching the target and we haven't yet checked the next
1420 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1421 verify_fail ("branch not to instruction start", start_PC);
1422 flags[pc] |= FLAG_BRANCH_TARGET;
1425 // Record the jsr which called this instruction.
1426 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1428 info->next = jsr_ptrs[pc];
1429 jsr_ptrs[pc] = info;
1433 void skip_padding ()
1435 while ((PC % 4) > 0)
1436 if (get_byte () != 0)
1437 verify_fail ("found nonzero padding byte");
1440 // Return the subroutine to which the instruction at PC belongs.
1441 int get_subroutine (int pc)
1443 if (states[pc] == NULL)
1445 return states[pc]->subroutine;
1448 // Do the work for a `ret' instruction. INDEX is the index into the
1450 void handle_ret_insn (int index)
1452 get_variable (index, return_address_type);
1454 int csub = current_state->subroutine;
1456 verify_fail ("no subroutine");
1458 // Check to see if we've merged subroutines.
1459 subr_entry_info *entry;
1460 for (entry = entry_points; entry != NULL; entry = entry->next)
1462 if (entry->ret_pc == start_PC)
1467 entry = (subr_entry_info *) _Jv_Malloc (sizeof (subr_entry_info));
1469 entry->ret_pc = start_PC;
1470 entry->next = entry_points;
1471 entry_points = entry;
1473 else if (entry->pc != csub)
1474 verify_fail ("subroutines merged");
1476 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1478 // Temporarily modify the current state so it looks like we're
1479 // in the enclosing context.
1480 current_state->subroutine = get_subroutine (subr->pc);
1482 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1483 push_jump_merge (subr->pc, current_state, true);
1486 current_state->subroutine = csub;
1490 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1491 // sure this subroutine isn't already on the stack.
1492 void check_nonrecursive_call (int sub, int dest)
1497 verify_fail ("recursive subroutine call");
1498 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1499 check_nonrecursive_call (get_subroutine (info->pc), dest);
1502 void handle_jsr_insn (int offset)
1504 int npc = compute_jump (offset);
1507 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1508 check_nonrecursive_call (current_state->subroutine, npc);
1510 // Modify our state as appropriate for entry into a subroutine.
1511 push_type (return_address_type);
1512 push_jump_merge (npc, current_state);
1514 pop_type (return_address_type);
1516 // On entry to the subroutine, the subroutine number must be set
1517 // and the locals must be marked as cleared. We do this after
1518 // merging state so that we don't erroneously "notice" a variable
1519 // change merely on entry.
1520 states[npc]->enter_subroutine (npc, current_method->max_locals);
1522 // Indicate that we don't know the stack depth of the instruction
1523 // following the `jsr'. The idea here is that we need to merge
1524 // the local variable state across the jsr, but the subroutine
1525 // might change the stack depth, so we can't make any assumptions
1526 // about it. So we have yet another special case. We know that
1527 // at this point PC points to the instruction after the jsr.
1529 // FIXME: what if we have a jsr at the end of the code, but that
1530 // jsr has no corresponding ret? Is this verifiable, or is it
1531 // not? If it is then we need a special case here.
1532 if (PC >= current_method->code_length)
1533 verify_fail ("fell off end");
1535 current_state->stacktop = state::NO_STACK;
1536 push_jump_merge (PC, current_state);
1540 jclass construct_primitive_array_type (type_val prim)
1546 k = JvPrimClass (boolean);
1549 k = JvPrimClass (char);
1552 k = JvPrimClass (float);
1555 k = JvPrimClass (double);
1558 k = JvPrimClass (byte);
1561 k = JvPrimClass (short);
1564 k = JvPrimClass (int);
1567 k = JvPrimClass (long);
1570 // These aren't used here but we call them out to avoid
1573 case unsuitable_type:
1574 case return_address_type:
1575 case continuation_type:
1576 case unused_by_subroutine_type:
1577 case reference_type:
1579 case unresolved_reference_type:
1580 case uninitialized_reference_type:
1581 case uninitialized_unresolved_reference_type:
1583 verify_fail ("unknown type in construct_primitive_array_type");
1585 k = _Jv_GetArrayClass (k, NULL);
1589 // This pass computes the location of branch targets and also
1590 // instruction starts.
1591 void branch_prepass ()
1593 flags = (char *) _Jv_Malloc (current_method->code_length);
1594 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1595 * current_method->code_length);
1597 for (int i = 0; i < current_method->code_length; ++i)
1603 bool last_was_jsr = false;
1606 while (PC < current_method->code_length)
1608 // Set `start_PC' early so that error checking can have the
1611 flags[PC] |= FLAG_INSN_START;
1613 // If the previous instruction was a jsr, then the next
1614 // instruction is a branch target -- the branch being the
1615 // corresponding `ret'.
1617 note_branch_target (PC);
1618 last_was_jsr = false;
1620 java_opcode opcode = (java_opcode) bytecode[PC++];
1624 case op_aconst_null:
1760 case op_monitorenter:
1761 case op_monitorexit:
1769 case op_arraylength:
1801 case op_invokespecial:
1802 case op_invokestatic:
1803 case op_invokevirtual:
1807 case op_multianewarray:
1813 last_was_jsr = true;
1832 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1835 case op_tableswitch:
1838 note_branch_target (compute_jump (get_int ()));
1839 jint low = get_int ();
1840 jint hi = get_int ();
1842 verify_fail ("invalid tableswitch", start_PC);
1843 for (int i = low; i <= hi; ++i)
1844 note_branch_target (compute_jump (get_int ()));
1848 case op_lookupswitch:
1851 note_branch_target (compute_jump (get_int ()));
1852 int npairs = get_int ();
1854 verify_fail ("too few pairs in lookupswitch", start_PC);
1855 while (npairs-- > 0)
1858 note_branch_target (compute_jump (get_int ()));
1863 case op_invokeinterface:
1871 opcode = (java_opcode) get_byte ();
1873 if (opcode == op_iinc)
1879 last_was_jsr = true;
1882 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1885 // These are unused here, but we call them out explicitly
1886 // so that -Wswitch-enum doesn't complain.
1892 case op_putstatic_1:
1893 case op_putstatic_2:
1894 case op_putstatic_4:
1895 case op_putstatic_8:
1896 case op_putstatic_a:
1898 case op_getfield_2s:
1899 case op_getfield_2u:
1903 case op_getstatic_1:
1904 case op_getstatic_2s:
1905 case op_getstatic_2u:
1906 case op_getstatic_4:
1907 case op_getstatic_8:
1908 case op_getstatic_a:
1910 verify_fail ("unrecognized instruction in branch_prepass",
1914 // See if any previous branch tried to branch to the middle of
1915 // this instruction.
1916 for (int pc = start_PC + 1; pc < PC; ++pc)
1918 if ((flags[pc] & FLAG_BRANCH_TARGET))
1919 verify_fail ("branch to middle of instruction", pc);
1923 // Verify exception handlers.
1924 for (int i = 0; i < current_method->exc_count; ++i)
1926 if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
1927 verify_fail ("exception handler not at instruction start",
1928 exception[i].handler_pc.i);
1929 if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
1930 verify_fail ("exception start not at instruction start",
1931 exception[i].start_pc.i);
1932 if (exception[i].end_pc.i != current_method->code_length
1933 && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
1934 verify_fail ("exception end not at instruction start",
1935 exception[i].end_pc.i);
1937 flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
1941 void check_pool_index (int index)
1943 if (index < 0 || index >= current_class->constants.size)
1944 verify_fail ("constant pool index out of range", start_PC);
1947 type check_class_constant (int index)
1949 check_pool_index (index);
1950 _Jv_Constants *pool = ¤t_class->constants;
1951 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1952 return type (pool->data[index].clazz);
1953 else if (pool->tags[index] == JV_CONSTANT_Class)
1954 return type (pool->data[index].utf8);
1955 verify_fail ("expected class constant", start_PC);
1958 type check_constant (int index)
1960 check_pool_index (index);
1961 _Jv_Constants *pool = ¤t_class->constants;
1962 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1963 || pool->tags[index] == JV_CONSTANT_String)
1964 return type (&java::lang::String::class$);
1965 else if (pool->tags[index] == JV_CONSTANT_Integer)
1966 return type (int_type);
1967 else if (pool->tags[index] == JV_CONSTANT_Float)
1968 return type (float_type);
1969 verify_fail ("String, int, or float constant expected", start_PC);
1972 type check_wide_constant (int index)
1974 check_pool_index (index);
1975 _Jv_Constants *pool = ¤t_class->constants;
1976 if (pool->tags[index] == JV_CONSTANT_Long)
1977 return type (long_type);
1978 else if (pool->tags[index] == JV_CONSTANT_Double)
1979 return type (double_type);
1980 verify_fail ("long or double constant expected", start_PC);
1983 // Helper for both field and method. These are laid out the same in
1984 // the constant pool.
1985 type handle_field_or_method (int index, int expected,
1986 _Jv_Utf8Const **name,
1987 _Jv_Utf8Const **fmtype)
1989 check_pool_index (index);
1990 _Jv_Constants *pool = ¤t_class->constants;
1991 if (pool->tags[index] != expected)
1992 verify_fail ("didn't see expected constant", start_PC);
1993 // Once we know we have a Fieldref or Methodref we assume that it
1994 // is correctly laid out in the constant pool. I think the code
1995 // in defineclass.cc guarantees this.
1996 _Jv_ushort class_index, name_and_type_index;
1997 _Jv_loadIndexes (&pool->data[index],
1999 name_and_type_index);
2000 _Jv_ushort name_index, desc_index;
2001 _Jv_loadIndexes (&pool->data[name_and_type_index],
2002 name_index, desc_index);
2004 *name = pool->data[name_index].utf8;
2005 *fmtype = pool->data[desc_index].utf8;
2007 return check_class_constant (class_index);
2010 // Return field's type, compute class' type if requested.
2011 type check_field_constant (int index, type *class_type = NULL)
2013 _Jv_Utf8Const *name, *field_type;
2014 type ct = handle_field_or_method (index,
2015 JV_CONSTANT_Fieldref,
2016 &name, &field_type);
2019 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
2020 return type (field_type);
2021 return get_type_val_for_signature (field_type->data[0]);
2024 type check_method_constant (int index, bool is_interface,
2025 _Jv_Utf8Const **method_name,
2026 _Jv_Utf8Const **method_signature)
2028 return handle_field_or_method (index,
2030 ? JV_CONSTANT_InterfaceMethodref
2031 : JV_CONSTANT_Methodref),
2032 method_name, method_signature);
2035 type get_one_type (char *&p)
2053 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
2057 // Casting to jchar here is ok since we are looking at an ASCII
2059 type_val rt = get_type_val_for_signature (jchar (v));
2061 if (arraycount == 0)
2063 // Callers of this function eventually push their arguments on
2064 // the stack. So, promote them here.
2065 return type (rt).promote ();
2068 jclass k = construct_primitive_array_type (rt);
2069 while (--arraycount > 0)
2070 k = _Jv_GetArrayClass (k, NULL);
2074 void compute_argument_types (_Jv_Utf8Const *signature,
2077 char *p = signature->data;
2083 types[i++] = get_one_type (p);
2086 type compute_return_type (_Jv_Utf8Const *signature)
2088 char *p = signature->data;
2092 return get_one_type (p);
2095 void check_return_type (type onstack)
2097 type rt = compute_return_type (current_method->self->signature);
2098 if (! rt.compatible (onstack, this))
2099 verify_fail ("incompatible return type");
2102 // Initialize the stack for the new method. Returns true if this
2103 // method is an instance initializer.
2104 bool initialize_stack ()
2107 bool is_init = false;
2109 using namespace java::lang::reflect;
2110 if (! Modifier::isStatic (current_method->self->accflags))
2112 type kurr (current_class);
2113 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
2115 kurr.set_uninitialized (type::SELF, this);
2118 set_variable (0, kurr);
2119 current_state->set_this_type (kurr);
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);
2493 type t2 = pop_raw ();
2508 type t = pop_raw ();
2523 type t1 = pop_raw ();
2540 type t1 = pop_raw ();
2543 type t2 = pop_raw ();
2561 type t3 = pop_raw ();
2599 pop_type (int_type);
2600 push_type (pop_type (int_type));
2610 pop_type (long_type);
2611 push_type (pop_type (long_type));
2616 pop_type (int_type);
2617 push_type (pop_type (long_type));
2624 pop_type (float_type);
2625 push_type (pop_type (float_type));
2632 pop_type (double_type);
2633 push_type (pop_type (double_type));
2639 push_type (pop_type (int_type));
2642 push_type (pop_type (long_type));
2645 push_type (pop_type (float_type));
2648 push_type (pop_type (double_type));
2651 get_variable (get_byte (), int_type);
2655 pop_type (int_type);
2656 push_type (long_type);
2659 pop_type (int_type);
2660 push_type (float_type);
2663 pop_type (int_type);
2664 push_type (double_type);
2667 pop_type (long_type);
2668 push_type (int_type);
2671 pop_type (long_type);
2672 push_type (float_type);
2675 pop_type (long_type);
2676 push_type (double_type);
2679 pop_type (float_type);
2680 push_type (int_type);
2683 pop_type (float_type);
2684 push_type (long_type);
2687 pop_type (float_type);
2688 push_type (double_type);
2691 pop_type (double_type);
2692 push_type (int_type);
2695 pop_type (double_type);
2696 push_type (long_type);
2699 pop_type (double_type);
2700 push_type (float_type);
2703 pop_type (long_type);
2704 pop_type (long_type);
2705 push_type (int_type);
2709 pop_type (float_type);
2710 pop_type (float_type);
2711 push_type (int_type);
2715 pop_type (double_type);
2716 pop_type (double_type);
2717 push_type (int_type);
2725 pop_type (int_type);
2726 push_jump (get_short ());
2734 pop_type (int_type);
2735 pop_type (int_type);
2736 push_jump (get_short ());
2740 pop_type (reference_type);
2741 pop_type (reference_type);
2742 push_jump (get_short ());
2745 push_jump (get_short ());
2749 handle_jsr_insn (get_short ());
2752 handle_ret_insn (get_byte ());
2754 case op_tableswitch:
2756 pop_type (int_type);
2758 push_jump (get_int ());
2759 jint low = get_int ();
2760 jint high = get_int ();
2761 // Already checked LOW -vs- HIGH.
2762 for (int i = low; i <= high; ++i)
2763 push_jump (get_int ());
2768 case op_lookupswitch:
2770 pop_type (int_type);
2772 push_jump (get_int ());
2773 jint npairs = get_int ();
2774 // Already checked NPAIRS >= 0.
2776 for (int i = 0; i < npairs; ++i)
2778 jint key = get_int ();
2779 if (i > 0 && key <= lastkey)
2780 verify_fail ("lookupswitch pairs unsorted", start_PC);
2782 push_jump (get_int ());
2788 check_return_type (pop_type (int_type));
2792 check_return_type (pop_type (long_type));
2796 check_return_type (pop_type (float_type));
2800 check_return_type (pop_type (double_type));
2804 check_return_type (pop_init_ref (reference_type));
2808 // We only need to check this when the return type is
2809 // void, because all instance initializers return void.
2811 current_state->check_this_initialized (this);
2812 check_return_type (void_type);
2816 push_type (check_field_constant (get_ushort ()));
2819 pop_type (check_field_constant (get_ushort ()));
2824 type field = check_field_constant (get_ushort (), &klass);
2832 type field = check_field_constant (get_ushort (), &klass);
2835 // We have an obscure special case here: we can use
2836 // `putfield' on a field declared in this class, even if
2837 // `this' has not yet been initialized.
2838 if (! current_state->this_type.isinitialized ()
2839 && current_state->this_type.pc == type::SELF)
2840 klass.set_uninitialized (type::SELF, this);
2845 case op_invokevirtual:
2846 case op_invokespecial:
2847 case op_invokestatic:
2848 case op_invokeinterface:
2850 _Jv_Utf8Const *method_name, *method_signature;
2852 = check_method_constant (get_ushort (),
2853 opcode == op_invokeinterface,
2856 // NARGS is only used when we're processing
2857 // invokeinterface. It is simplest for us to compute it
2858 // here and then verify it later.
2860 if (opcode == op_invokeinterface)
2862 nargs = get_byte ();
2863 if (get_byte () != 0)
2864 verify_fail ("invokeinterface dummy byte is wrong");
2867 bool is_init = false;
2868 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2871 if (opcode != op_invokespecial)
2872 verify_fail ("can't invoke <init>");
2874 else if (method_name->data[0] == '<')
2875 verify_fail ("can't invoke method starting with `<'");
2877 // Pop arguments and check types.
2878 int arg_count = _Jv_count_arguments (method_signature);
2879 type arg_types[arg_count];
2880 compute_argument_types (method_signature, arg_types);
2881 for (int i = arg_count - 1; i >= 0; --i)
2883 // This is only used for verifying the byte for
2885 nargs -= arg_types[i].depth ();
2886 pop_init_ref (arg_types[i]);
2889 if (opcode == op_invokeinterface
2891 verify_fail ("wrong argument count for invokeinterface");
2893 if (opcode != op_invokestatic)
2895 type t = class_type;
2898 // In this case the PC doesn't matter.
2899 t.set_uninitialized (type::UNINIT, this);
2901 type raw = pop_raw ();
2903 if (! is_init && ! raw.isinitialized ())
2905 // This is a failure.
2907 else if (is_init && raw.isnull ())
2911 else if (t.compatible (raw, this))
2915 else if (opcode == op_invokeinterface)
2917 // This is a hack. We might have merged two
2918 // items and gotten `Object'. This can happen
2919 // because we don't keep track of where merges
2920 // come from. This is safe as long as the
2921 // interpreter checks interfaces at runtime.
2922 type obj (&java::lang::Object::class$);
2923 ok = raw.compatible (obj, this);
2927 verify_fail ("incompatible type on stack");
2930 current_state->set_initialized (raw.get_pc (),
2931 current_method->max_locals);
2934 type rt = compute_return_type (method_signature);
2942 type t = check_class_constant (get_ushort ());
2943 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2944 verify_fail ("type is array, interface, or abstract");
2945 t.set_uninitialized (start_PC, this);
2952 int atype = get_byte ();
2953 // We intentionally have chosen constants to make this
2955 if (atype < boolean_type || atype > long_type)
2956 verify_fail ("type not primitive", start_PC);
2957 pop_type (int_type);
2958 push_type (construct_primitive_array_type (type_val (atype)));
2962 pop_type (int_type);
2963 push_type (check_class_constant (get_ushort ()).to_array (this));
2965 case op_arraylength:
2967 type t = pop_init_ref (reference_type);
2968 if (! t.isarray () && ! t.isnull ())
2969 verify_fail ("array type expected");
2970 push_type (int_type);
2974 pop_type (type (&java::lang::Throwable::class$));
2978 pop_init_ref (reference_type);
2979 push_type (check_class_constant (get_ushort ()));
2982 pop_init_ref (reference_type);
2983 check_class_constant (get_ushort ());
2984 push_type (int_type);
2986 case op_monitorenter:
2987 pop_init_ref (reference_type);
2989 case op_monitorexit:
2990 pop_init_ref (reference_type);
2994 switch (get_byte ())
2997 push_type (get_variable (get_ushort (), int_type));
3000 push_type (get_variable (get_ushort (), long_type));
3003 push_type (get_variable (get_ushort (), float_type));
3006 push_type (get_variable (get_ushort (), double_type));
3009 push_type (get_variable (get_ushort (), reference_type));
3012 set_variable (get_ushort (), pop_type (int_type));
3015 set_variable (get_ushort (), pop_type (long_type));
3018 set_variable (get_ushort (), pop_type (float_type));
3021 set_variable (get_ushort (), pop_type (double_type));
3024 set_variable (get_ushort (), pop_init_ref (reference_type));
3027 handle_ret_insn (get_short ());
3030 get_variable (get_ushort (), int_type);
3034 verify_fail ("unrecognized wide instruction", start_PC);
3038 case op_multianewarray:
3040 type atype = check_class_constant (get_ushort ());
3041 int dim = get_byte ();
3043 verify_fail ("too few dimensions to multianewarray", start_PC);
3044 atype.verify_dimensions (dim, this);
3045 for (int i = 0; i < dim; ++i)
3046 pop_type (int_type);
3052 pop_type (reference_type);
3053 push_jump (get_short ());
3056 push_jump (get_int ());
3060 handle_jsr_insn (get_int ());
3063 // These are unused here, but we call them out explicitly
3064 // so that -Wswitch-enum doesn't complain.
3070 case op_putstatic_1:
3071 case op_putstatic_2:
3072 case op_putstatic_4:
3073 case op_putstatic_8:
3074 case op_putstatic_a:
3076 case op_getfield_2s:
3077 case op_getfield_2u:
3081 case op_getstatic_1:
3082 case op_getstatic_2s:
3083 case op_getstatic_2u:
3084 case op_getstatic_4:
3085 case op_getstatic_8:
3086 case op_getstatic_a:
3088 // Unrecognized opcode.
3089 verify_fail ("unrecognized instruction in verify_instructions_0",
3097 void verify_instructions ()
3100 verify_instructions_0 ();
3103 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3105 // We just print the text as utf-8. This is just for debugging
3107 debug_print ("--------------------------------\n");
3108 debug_print ("-- Verifying method `%s'\n", m->self->name->data);
3111 bytecode = m->bytecode ();
3112 exception = m->exceptions ();
3113 current_class = m->defining_class;
3119 entry_points = NULL;
3122 ~_Jv_BytecodeVerifier ()
3131 for (int i = 0; i < current_method->code_length; ++i)
3133 if (jsr_ptrs[i] != NULL)
3135 subr_info *info = jsr_ptrs[i];
3136 while (info != NULL)
3138 subr_info *next = info->next;
3144 _Jv_Free (jsr_ptrs);
3147 while (utf8_list != NULL)
3149 linked_utf8 *n = utf8_list->next;
3150 _Jv_Free (utf8_list->val);
3151 _Jv_Free (utf8_list);
3155 while (entry_points != NULL)
3157 subr_entry_info *next = entry_points->next;
3158 _Jv_Free (entry_points);
3159 entry_points = next;
3165 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3167 _Jv_BytecodeVerifier v (meth);
3168 v.verify_instructions ();
3170 #endif /* INTERPRETER */