1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 // Writte by Tom Tromey <tromey@redhat.com>
17 #include <java-insns.h>
18 #include <java-interp.h>
22 #include <java/lang/Class.h>
23 #include <java/lang/VerifyError.h>
24 #include <java/lang/Throwable.h>
25 #include <java/lang/reflect/Modifier.h>
26 #include <java/lang/StringBuffer.h>
30 // * read more about when classes must be loaded
31 // * there are bugs with boolean arrays?
32 // * class loader madness
33 // * Lots and lots of debugging and testing
34 // * type representation is still ugly. look for the big switches
35 // * at least one GC problem :-(
38 // This is global because __attribute__ doesn't seem to work on static
40 static void verify_fail (char *msg, jint pc = -1)
41 __attribute__ ((__noreturn__));
43 class _Jv_BytecodeVerifier
47 static const int FLAG_INSN_START = 1;
48 static const int FLAG_BRANCH_TARGET = 2;
49 static const int FLAG_JSR_TARGET = 4;
57 // The PC corresponding to the start of the current instruction.
60 // The current state of the stack, locals, etc.
63 // We store the state at branch targets, for merging. This holds
67 // We keep a linked list of all the PCs which we must reverify.
68 // The link is done using the PC values. This is the head of the
72 // We keep some flags for each instruction. The values are the
73 // FLAG_* constants defined above.
76 // We need to keep track of which instructions can call a given
77 // subroutine. FIXME: this is inefficient. We keep a linked list
78 // of all calling `jsr's at at each jsr target.
81 // The current top of the stack, in terms of slots.
83 // The current depth of the stack. This will be larger than
84 // STACKTOP when wide types are on the stack.
87 // The bytecode itself.
88 unsigned char *bytecode;
90 _Jv_InterpException *exception;
95 _Jv_InterpMethod *current_method;
97 // This enum holds a list of tags for all the different types we
98 // need to handle. Reference types are treated specially by the
104 // The values for primitive types are chosen to correspond to values
105 // specified to newarray.
115 // Used when overwriting second word of a double or long in the
116 // local variables. Also used after merging local variable states
117 // to indicate an unusable value.
122 // Everything after `reference_type' must be a reference type.
125 unresolved_reference_type,
126 uninitialized_reference_type,
127 uninitialized_unresolved_reference_type
130 // Return the type_val corresponding to a primitive signature
131 // character. For instance `I' returns `int.class'.
132 static type_val get_type_val_for_signature (jchar sig)
165 verify_fail ("invalid signature");
170 // Return the type_val corresponding to a primitive class.
171 static type_val get_type_val_for_signature (jclass k)
173 return get_type_val_for_signature ((jchar) k->method_count);
176 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
177 // TARGET haven't been prepared.
178 static bool is_assignable_from_slow (jclass target, jclass source)
180 // This will terminate when SOURCE==Object.
183 if (source == target)
186 if (target->isPrimitive () || source->isPrimitive ())
189 // _Jv_IsAssignableFrom can handle a target which is an
190 // interface even if it hasn't been prepared.
191 if ((target->state > JV_STATE_LINKED || target->isInterface ())
192 && source->state > JV_STATE_LINKED)
193 return _Jv_IsAssignableFrom (target, source);
195 if (target->isArray ())
197 if (! source->isArray ())
199 target = target->getComponentType ();
200 source = source->getComponentType ();
202 else if (target->isInterface ())
204 for (int i = 0; i < source->interface_count; ++i)
206 // We use a recursive call because we also need to
207 // check superinterfaces.
208 if (is_assignable_from_slow (target, source->interfaces[i]))
213 else if (target == &java::lang::Object::class$)
215 else if (source->isInterface ()
216 || source == &java::lang::Object::class$)
219 source = source->getSuperclass ();
223 // This is used to keep track of which `jsr's correspond to a given
227 // PC of the instruction just after the jsr.
233 // The `type' class is used to represent a single type in the
239 // Some associated data.
242 // For a resolved reference type, this is a pointer to the class.
244 // For other reference types, this it the name of the class.
247 // This is used when constructing a new object. It is the PC of the
248 // `new' instruction which created the object. We use the special
249 // value -2 to mean that this is uninitialized, and the special
250 // value -1 for the case where the current method is itself the
254 static const int UNINIT = -2;
255 static const int SELF = -1;
257 // Basic constructor.
260 key = unsuitable_type;
265 // Make a new instance given the type tag. We assume a generic
266 // `reference_type' means Object.
271 if (key == reference_type)
272 data.klass = &java::lang::Object::class$;
276 // Make a new instance given a class.
279 key = reference_type;
284 // Make a new instance given the name of a class.
285 type (_Jv_Utf8Const *n)
287 key = unresolved_reference_type;
300 // These operators are required because libgcj can't link in
302 void *operator new[] (size_t bytes)
304 return _Jv_Malloc (bytes);
307 void operator delete[] (void *mem)
312 type& operator= (type_val k)
320 type& operator= (const type& t)
328 // Promote a numeric type.
331 if (key == boolean_type || key == char_type
332 || key == byte_type || key == short_type)
337 // If *THIS is an unresolved reference type, resolve it.
340 if (key != unresolved_reference_type
341 && key != uninitialized_unresolved_reference_type)
344 // FIXME: class loader
345 using namespace java::lang;
346 // We might see either kind of name. Sigh.
347 if (data.name->data[0] == 'L'
348 && data.name->data[data.name->length - 1] == ';')
349 data.klass = _Jv_FindClassFromSignature (data.name->data, NULL);
351 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
353 key = (key == unresolved_reference_type
355 : uninitialized_reference_type);
358 // Mark this type as the uninitialized result of `new'.
359 void set_uninitialized (int npc)
361 if (key == reference_type)
362 key = uninitialized_reference_type;
363 else if (key == unresolved_reference_type)
364 key = uninitialized_unresolved_reference_type;
366 verify_fail ("internal error in type::uninitialized");
370 // Mark this type as now initialized.
371 void set_initialized (int npc)
373 if (npc != UNINIT && pc == npc
374 && (key == uninitialized_reference_type
375 || key == uninitialized_unresolved_reference_type))
377 key = (key == uninitialized_reference_type
379 : unresolved_reference_type);
385 // Return true if an object of type K can be assigned to a variable
386 // of type *THIS. Handle various special cases too. Might modify
387 // *THIS or K. Note however that this does not perform numeric
389 bool compatible (type &k)
391 // Any type is compatible with the unsuitable type.
392 if (key == unsuitable_type)
395 if (key < reference_type || k.key < reference_type)
398 // The `null' type is convertible to any reference type.
399 // FIXME: is this correct for THIS?
400 if (key == null_type || k.key == null_type)
403 // Any reference type is convertible to Object. This is a special
404 // case so we don't need to unnecessarily resolve a class.
405 if (key == reference_type
406 && data.klass == &java::lang::Object::class$)
409 // An initialized type and an uninitialized type are not
411 if (isinitialized () != k.isinitialized ())
414 // Two uninitialized objects are compatible if either:
415 // * The PCs are identical, or
416 // * One PC is UNINIT.
417 if (! isinitialized ())
419 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
423 // Two unresolved types are equal if their names are the same.
426 && _Jv_equalUtf8Consts (data.name, k.data.name))
429 // We must resolve both types and check assignability.
432 return is_assignable_from_slow (data.klass, k.data.klass);
437 return key == void_type;
442 return key == long_type || key == double_type;
445 // Return number of stack or local variable slots taken by this
449 return iswide () ? 2 : 1;
452 bool isarray () const
454 // We treat null_type as not an array. This is ok based on the
455 // current uses of this method.
456 if (key == reference_type)
457 return data.klass->isArray ();
458 else if (key == unresolved_reference_type)
459 return data.name->data[0] == '[';
466 if (key != reference_type)
468 return data.klass->isInterface ();
474 if (key != reference_type)
476 using namespace java::lang::reflect;
477 return Modifier::isAbstract (data.klass->getModifiers ());
480 // Return the element type of an array.
483 // FIXME: maybe should do string manipulation here.
485 if (key != reference_type)
486 verify_fail ("programmer error in type::element_type()");
488 jclass k = data.klass->getComponentType ();
489 if (k->isPrimitive ())
490 return type (get_type_val_for_signature (k));
494 // Return the array type corresponding to an initialized
495 // reference. We could expand this to work for other kinds of
496 // types, but currently we don't need to.
499 // Resolving isn't ideal, because it might force us to load
500 // another class, but it's easy. FIXME?
501 if (key == unresolved_reference_type)
504 if (key == reference_type)
505 return type (_Jv_GetArrayClass (data.klass,
506 data.klass->getClassLoader ()));
508 verify_fail ("internal error in type::to_array()");
511 bool isreference () const
513 return key >= reference_type;
521 bool isinitialized () const
523 return (key == reference_type
525 || key == unresolved_reference_type);
528 bool isresolved () const
530 return (key == reference_type
532 || key == uninitialized_reference_type);
535 void verify_dimensions (int ndims)
537 // The way this is written, we don't need to check isarray().
538 if (key == reference_type)
540 jclass k = data.klass;
541 while (k->isArray () && ndims > 0)
543 k = k->getComponentType ();
549 // We know KEY == unresolved_reference_type.
550 char *p = data.name->data;
551 while (*p++ == '[' && ndims-- > 0)
556 verify_fail ("array type has fewer dimensions than required");
559 // Merge OLD_TYPE into this. On error throw exception.
560 bool merge (type& old_type, bool local_semantics = false)
562 bool changed = false;
563 bool refo = old_type.isreference ();
564 bool refn = isreference ();
567 if (old_type.key == null_type)
569 else if (key == null_type)
574 else if (isinitialized () != old_type.isinitialized ())
575 verify_fail ("merging initialized and uninitialized types");
578 if (! isinitialized ())
582 else if (old_type.pc == UNINIT)
584 else if (pc != old_type.pc)
585 verify_fail ("merging different uninitialized types");
589 && ! old_type.isresolved ()
590 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
592 // Types are identical.
599 jclass k = data.klass;
600 jclass oldk = old_type.data.klass;
603 while (k->isArray () && oldk->isArray ())
606 k = k->getComponentType ();
607 oldk = oldk->getComponentType ();
610 // This loop will end when we hit Object.
613 if (is_assignable_from_slow (k, oldk))
615 k = k->getSuperclass ();
621 while (arraycount > 0)
623 // FIXME: Class loader.
624 k = _Jv_GetArrayClass (k, NULL);
632 else if (refo || refn || key != old_type.key)
636 key = unsuitable_type;
640 verify_fail ("unmergeable type");
646 // This class holds all the state information we need for a given
650 // Current top of stack.
652 // Current stack depth. This is like the top of stack but it
653 // includes wide variable information.
657 // The local variables.
659 // This is used in subroutines to keep track of which local
660 // variables have been accessed.
662 // If not 0, then we are in a subroutine. The value is the PC of
663 // the subroutine's entry point. We can use 0 as an exceptional
664 // value because PC=0 can never be a subroutine.
666 // This is used to keep a linked list of all the states which
667 // require re-verification. We use the PC to keep track.
670 // INVALID marks a state which is not on the linked list of states
671 // requiring reverification.
672 static const int INVALID = -1;
673 // NO_NEXT marks the state at the end of the reverification list.
674 static const int NO_NEXT = -2;
680 local_changed = NULL;
683 state (int max_stack, int max_locals)
687 stack = new type[max_stack];
688 for (int i = 0; i < max_stack; ++i)
689 stack[i] = unsuitable_type;
690 locals = new type[max_locals];
691 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
692 for (int i = 0; i < max_locals; ++i)
694 locals[i] = unsuitable_type;
695 local_changed[i] = false;
701 state (const state *copy, int max_stack, int max_locals)
703 stack = new type[max_stack];
704 locals = new type[max_locals];
705 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
717 _Jv_Free (local_changed);
720 void *operator new[] (size_t bytes)
722 return _Jv_Malloc (bytes);
725 void operator delete[] (void *mem)
730 void *operator new (size_t bytes)
732 return _Jv_Malloc (bytes);
735 void operator delete (void *mem)
740 void copy (const state *copy, int max_stack, int max_locals)
742 stacktop = copy->stacktop;
743 stackdepth = copy->stackdepth;
744 subroutine = copy->subroutine;
745 for (int i = 0; i < max_stack; ++i)
746 stack[i] = copy->stack[i];
747 for (int i = 0; i < max_locals; ++i)
749 locals[i] = copy->locals[i];
750 local_changed[i] = copy->local_changed[i];
752 // Don't modify `next'.
755 // Modify this state to reflect entry to an exception handler.
756 void set_exception (type t, int max_stack)
761 for (int i = stacktop; i < max_stack; ++i)
762 stack[i] = unsuitable_type;
764 // FIXME: subroutine handling?
767 // Merge STATE into this state. Destructively modifies this state.
768 // Returns true if the new state was in fact changed. Will throw an
769 // exception if the states are not mergeable.
770 bool merge (state *state_old, bool ret_semantics,
773 bool changed = false;
775 // Merge subroutine states. *THIS and *STATE_OLD must be in the
776 // same subroutine. Also, recursive subroutine calls must be
778 if (subroutine == state_old->subroutine)
782 else if (subroutine == 0)
784 subroutine = state_old->subroutine;
788 verify_fail ("subroutines merged");
791 if (state_old->stacktop != stacktop)
792 verify_fail ("stack sizes differ");
793 for (int i = 0; i < state_old->stacktop; ++i)
795 if (stack[i].merge (state_old->stack[i]))
799 // Merge local variables.
800 for (int i = 0; i < max_locals; ++i)
802 if (! ret_semantics || local_changed[i])
804 if (locals[i].merge (state_old->locals[i], true))
811 // If we're in a subroutine, we must compute the union of
812 // all the changed local variables.
813 if (state_old->local_changed[i])
820 // Throw an exception if there is an uninitialized object on the
821 // stack or in a local variable. EXCEPTION_SEMANTICS controls
822 // whether we're using backwards-branch or exception-handing
824 void check_no_uninitialized_objects (int max_locals,
825 bool exception_semantics = false)
827 if (! exception_semantics)
829 for (int i = 0; i < stacktop; ++i)
830 if (stack[i].isreference () && ! stack[i].isinitialized ())
831 verify_fail ("uninitialized object on stack");
834 for (int i = 0; i < max_locals; ++i)
835 if (locals[i].isreference () && ! locals[i].isinitialized ())
836 verify_fail ("uninitialized object in local variable");
839 // Note that a local variable was accessed or modified.
840 void note_variable (int index)
843 local_changed[index] = true;
846 // Mark each `new'd object we know of that was allocated at PC as
848 void set_initialized (int pc, int max_locals)
850 for (int i = 0; i < stacktop; ++i)
851 stack[i].set_initialized (pc);
852 for (int i = 0; i < max_locals; ++i)
853 locals[i].set_initialized (pc);
859 if (current_state->stacktop <= 0)
860 verify_fail ("stack empty", start_PC);
861 type r = current_state->stack[--current_state->stacktop];
862 current_state->stackdepth -= r.depth ();
863 if (current_state->stackdepth < 0)
864 verify_fail ("stack empty", start_PC);
872 verify_fail ("narrow pop of wide type", start_PC);
880 verify_fail ("wide pop of narrow type", start_PC);
884 type pop_type (type match)
888 if (! match.compatible (t))
889 verify_fail ("incompatible type on stack", start_PC);
893 void push_type (type t)
895 // If T is a numeric type like short, promote it to int.
898 int depth = t.depth ();
899 if (current_state->stackdepth + depth > current_method->max_stack)
900 verify_fail ("stack overflow");
901 current_state->stack[current_state->stacktop++] = t;
902 current_state->stackdepth += depth;
905 void set_variable (int index, type t)
907 // If T is a numeric type like short, promote it to int.
910 int depth = t.depth ();
911 if (index > current_method->max_locals - depth)
912 verify_fail ("invalid local variable");
913 current_state->locals[index] = t;
914 current_state->note_variable (index);
918 current_state->locals[index + 1] = continuation_type;
919 current_state->note_variable (index + 1);
921 if (index > 0 && current_state->locals[index - 1].iswide ())
923 current_state->locals[index - 1] = unsuitable_type;
924 // There's no need to call note_variable here.
928 type get_variable (int index, type t)
930 int depth = t.depth ();
931 if (index > current_method->max_locals - depth)
932 verify_fail ("invalid local variable", start_PC);
933 if (! t.compatible (current_state->locals[index]))
934 verify_fail ("incompatible type in local variable", start_PC);
937 type t (continuation_type);
938 if (! current_state->locals[index + 1].compatible (t))
939 verify_fail ("invalid local variable", start_PC);
941 current_state->note_variable (index);
942 return current_state->locals[index];
945 // Make sure ARRAY is an array type and that its elements are
946 // compatible with type ELEMENT. Returns the actual element type.
947 type require_array_type (type array, type element)
949 if (! array.isarray ())
950 verify_fail ("array required");
952 type t = array.element_type ();
953 if (! element.compatible (t))
954 verify_fail ("incompatible array element type");
956 // Return T and not ELEMENT, because T might be specialized.
962 if (PC >= current_method->code_length)
963 verify_fail ("premature end of bytecode");
964 return (jint) bytecode[PC++] & 0xff;
969 jint b1 = get_byte ();
970 jint b2 = get_byte ();
971 return (jint) ((b1 << 8) | b2) & 0xffff;
976 jint b1 = get_byte ();
977 jint b2 = get_byte ();
978 jshort s = (b1 << 8) | b2;
984 jint b1 = get_byte ();
985 jint b2 = get_byte ();
986 jint b3 = get_byte ();
987 jint b4 = get_byte ();
988 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
991 int compute_jump (int offset)
993 int npc = start_PC + offset;
994 if (npc < 0 || npc >= current_method->code_length)
995 verify_fail ("branch out of range");
999 // Merge the indicated state into a new state and schedule a new PC if
1000 // there is a change. If RET_SEMANTICS is true, then we are merging
1001 // from a `ret' instruction into the instruction after a `jsr'. This
1002 // is a special case with its own modified semantics.
1003 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1005 bool changed = true;
1006 if (states[npc] == NULL)
1008 // FIXME: what if we reach this code from a `ret'?
1010 states[npc] = new state (nstate, current_method->max_stack,
1011 current_method->max_locals);
1014 changed = nstate->merge (states[npc], ret_semantics,
1015 current_method->max_stack);
1017 if (changed && states[npc]->next == state::INVALID)
1019 // The merge changed the state, and the new PC isn't yet on our
1020 // list of PCs to re-verify.
1021 states[npc]->next = next_verify_pc;
1022 next_verify_pc = npc;
1026 void push_jump (int offset)
1028 int npc = compute_jump (offset);
1030 current_state->check_no_uninitialized_objects (current_method->max_stack);
1031 push_jump_merge (npc, current_state);
1034 void push_exception_jump (type t, int pc)
1036 current_state->check_no_uninitialized_objects (current_method->max_stack,
1038 state s (current_state, current_method->max_stack,
1039 current_method->max_locals);
1040 s.set_exception (t, current_method->max_stack);
1041 push_jump_merge (pc, &s);
1046 int npc = next_verify_pc;
1047 if (npc != state::NO_NEXT)
1049 next_verify_pc = states[npc]->next;
1050 states[npc]->next = state::INVALID;
1055 void invalidate_pc ()
1057 PC = state::NO_NEXT;
1060 void note_branch_target (int pc, bool is_jsr_target = false)
1062 if (pc <= PC && ! (flags[pc] & FLAG_INSN_START))
1063 verify_fail ("branch not to instruction start");
1064 flags[pc] |= FLAG_BRANCH_TARGET;
1067 // Record the jsr which called this instruction.
1068 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1070 info->next = jsr_ptrs[pc];
1071 jsr_ptrs[pc] = info;
1072 flags[pc] |= FLAG_JSR_TARGET;
1076 void skip_padding ()
1078 while ((PC % 4) > 0)
1079 if (get_byte () != 0)
1080 verify_fail ("found nonzero padding byte");
1083 // Return the subroutine to which the instruction at PC belongs.
1084 int get_subroutine (int pc)
1086 if (states[pc] == NULL)
1088 return states[pc]->subroutine;
1091 // Do the work for a `ret' instruction. INDEX is the index into the
1093 void handle_ret_insn (int index)
1095 get_variable (index, return_address_type);
1097 int csub = current_state->subroutine;
1099 verify_fail ("no subroutine");
1101 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1103 // Temporarily modify the current state so it looks like we're
1104 // in the enclosing context.
1105 current_state->subroutine = get_subroutine (subr->pc);
1107 current_state->check_no_uninitialized_objects (current_method->max_stack);
1108 push_jump_merge (subr->pc, current_state, true);
1111 current_state->subroutine = csub;
1115 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1116 // sure this subroutine isn't already on the stack.
1117 void check_nonrecursive_call (int sub, int dest)
1122 verify_fail ("recursive subroutine call");
1123 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1124 check_nonrecursive_call (get_subroutine (info->pc), dest);
1127 void handle_jsr_insn (int offset)
1129 int npc = compute_jump (offset);
1132 current_state->check_no_uninitialized_objects (current_method->max_stack);
1133 check_nonrecursive_call (current_state->subroutine, npc);
1135 // Temporarily modify the current state so that it looks like we are
1136 // in the subroutine.
1137 push_type (return_address_type);
1138 int save = current_state->subroutine;
1139 current_state->subroutine = npc;
1141 // Merge into the subroutine.
1142 push_jump_merge (npc, current_state);
1144 // Undo our modifications.
1145 current_state->subroutine = save;
1146 pop_type (return_address_type);
1149 jclass construct_primitive_array_type (type_val prim)
1155 k = JvPrimClass (boolean);
1158 k = JvPrimClass (char);
1161 k = JvPrimClass (float);
1164 k = JvPrimClass (double);
1167 k = JvPrimClass (byte);
1170 k = JvPrimClass (short);
1173 k = JvPrimClass (int);
1176 k = JvPrimClass (long);
1179 verify_fail ("unknown type in construct_primitive_array_type");
1181 k = _Jv_GetArrayClass (k, NULL);
1185 // This pass computes the location of branch targets and also
1186 // instruction starts.
1187 void branch_prepass ()
1189 flags = (char *) _Jv_Malloc (current_method->code_length);
1190 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1191 * current_method->code_length);
1193 for (int i = 0; i < current_method->code_length; ++i)
1199 bool last_was_jsr = false;
1202 while (PC < current_method->code_length)
1204 flags[PC] |= FLAG_INSN_START;
1206 // If the previous instruction was a jsr, then the next
1207 // instruction is a branch target -- the branch being the
1208 // corresponding `ret'.
1210 note_branch_target (PC);
1211 last_was_jsr = false;
1214 unsigned char opcode = bytecode[PC++];
1218 case op_aconst_null:
1355 case op_monitorenter:
1356 case op_monitorexit:
1378 case op_arraylength:
1395 case op_invokespecial:
1396 case op_invokestatic:
1397 case op_invokevirtual:
1401 case op_multianewarray:
1407 last_was_jsr = true;
1426 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1429 case op_tableswitch:
1432 note_branch_target (compute_jump (get_int ()));
1433 jint low = get_int ();
1434 jint hi = get_int ();
1436 verify_fail ("invalid tableswitch", start_PC);
1437 for (int i = low; i <= hi; ++i)
1438 note_branch_target (compute_jump (get_int ()));
1442 case op_lookupswitch:
1445 note_branch_target (compute_jump (get_int ()));
1446 int npairs = get_int ();
1448 verify_fail ("too few pairs in lookupswitch", start_PC);
1449 while (npairs-- > 0)
1452 note_branch_target (compute_jump (get_int ()));
1457 case op_invokeinterface:
1465 opcode = get_byte ();
1467 if (opcode == (unsigned char) op_iinc)
1473 last_was_jsr = true;
1476 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1480 verify_fail ("unrecognized instruction in branch_prepass",
1484 // See if any previous branch tried to branch to the middle of
1485 // this instruction.
1486 for (int pc = start_PC + 1; pc < PC; ++pc)
1488 if ((flags[pc] & FLAG_BRANCH_TARGET))
1489 verify_fail ("branch to middle of instruction", pc);
1493 // Verify exception handlers.
1494 for (int i = 0; i < current_method->exc_count; ++i)
1496 if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
1497 verify_fail ("exception handler not at instruction start",
1498 exception[i].handler_pc);
1499 if (exception[i].start_pc > exception[i].end_pc)
1500 verify_fail ("exception range inverted");
1501 if (! (flags[exception[i].start_pc] & FLAG_INSN_START))
1502 verify_fail ("exception start not at instruction start",
1503 exception[i].start_pc);
1504 else if (! (flags[exception[i].end_pc] & FLAG_INSN_START))
1505 verify_fail ("exception end not at instruction start",
1506 exception[i].end_pc);
1508 flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
1512 void check_pool_index (int index)
1514 if (index < 0 || index >= current_class->constants.size)
1515 verify_fail ("constant pool index out of range", start_PC);
1518 type check_class_constant (int index)
1520 check_pool_index (index);
1521 _Jv_Constants *pool = ¤t_class->constants;
1522 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1523 return type (pool->data[index].clazz);
1524 else if (pool->tags[index] == JV_CONSTANT_Class)
1525 return type (pool->data[index].utf8);
1526 verify_fail ("expected class constant", start_PC);
1529 type check_constant (int index)
1531 check_pool_index (index);
1532 _Jv_Constants *pool = ¤t_class->constants;
1533 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1534 || pool->tags[index] == JV_CONSTANT_String)
1535 return type (&java::lang::String::class$);
1536 else if (pool->tags[index] == JV_CONSTANT_Integer)
1537 return type (int_type);
1538 else if (pool->tags[index] == JV_CONSTANT_Float)
1539 return type (float_type);
1540 verify_fail ("String, int, or float constant expected", start_PC);
1543 type check_wide_constant (int index)
1545 check_pool_index (index);
1546 _Jv_Constants *pool = ¤t_class->constants;
1547 if (pool->tags[index] == JV_CONSTANT_Long)
1548 return type (long_type);
1549 else if (pool->tags[index] == JV_CONSTANT_Double)
1550 return type (double_type);
1551 verify_fail ("long or double constant expected", start_PC);
1554 // Helper for both field and method. These are laid out the same in
1555 // the constant pool.
1556 type handle_field_or_method (int index, int expected,
1557 _Jv_Utf8Const **name,
1558 _Jv_Utf8Const **fmtype)
1560 check_pool_index (index);
1561 _Jv_Constants *pool = ¤t_class->constants;
1562 if (pool->tags[index] != expected)
1563 verify_fail ("didn't see expected constant", start_PC);
1564 // Once we know we have a Fieldref or Methodref we assume that it
1565 // is correctly laid out in the constant pool. I think the code
1566 // in defineclass.cc guarantees this.
1567 _Jv_ushort class_index, name_and_type_index;
1568 _Jv_loadIndexes (&pool->data[index],
1570 name_and_type_index);
1571 _Jv_ushort name_index, desc_index;
1572 _Jv_loadIndexes (&pool->data[name_and_type_index],
1573 name_index, desc_index);
1575 *name = pool->data[name_index].utf8;
1576 *fmtype = pool->data[desc_index].utf8;
1578 return check_class_constant (class_index);
1581 // Return field's type, compute class' type if requested.
1582 type check_field_constant (int index, type *class_type = NULL)
1584 _Jv_Utf8Const *name, *field_type;
1585 type ct = handle_field_or_method (index,
1586 JV_CONSTANT_Fieldref,
1587 &name, &field_type);
1590 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
1591 return type (field_type);
1592 return get_type_val_for_signature (field_type->data[0]);
1595 type check_method_constant (int index, bool is_interface,
1596 _Jv_Utf8Const **method_name,
1597 _Jv_Utf8Const **method_signature)
1599 return handle_field_or_method (index,
1601 ? JV_CONSTANT_InterfaceMethodref
1602 : JV_CONSTANT_Methodref),
1603 method_name, method_signature);
1606 type get_one_type (char *&p)
1624 // FIXME! This will get collected!
1625 _Jv_Utf8Const *name = _Jv_makeUtf8Const (start, p - start);
1629 // Casting to jchar here is ok since we are looking at an ASCII
1631 type_val rt = get_type_val_for_signature (jchar (v));
1633 if (arraycount == 0)
1635 // Callers of this function eventually push their arguments on
1636 // the stack. So, promote them here.
1637 return type (rt).promote ();
1640 jclass k = construct_primitive_array_type (rt);
1641 while (--arraycount > 0)
1642 k = _Jv_GetArrayClass (k, NULL);
1646 void compute_argument_types (_Jv_Utf8Const *signature,
1649 char *p = signature->data;
1655 types[i++] = get_one_type (p);
1658 type compute_return_type (_Jv_Utf8Const *signature)
1660 char *p = signature->data;
1664 return get_one_type (p);
1667 void check_return_type (type onstack)
1669 type rt = compute_return_type (current_method->self->signature);
1670 if (! rt.compatible (onstack))
1671 verify_fail ("incompatible return type", start_PC);
1674 void verify_instructions_0 ()
1676 current_state = new state (current_method->max_stack,
1677 current_method->max_locals);
1685 using namespace java::lang::reflect;
1686 if (! Modifier::isStatic (current_method->self->accflags))
1688 type kurr (current_class);
1689 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
1690 kurr.set_uninitialized (type::SELF);
1691 set_variable (0, kurr);
1695 // We have to handle wide arguments specially here.
1696 int arg_count = _Jv_count_arguments (current_method->self->signature);
1697 type arg_types[arg_count];
1698 compute_argument_types (current_method->self->signature, arg_types);
1699 for (int i = 0; i < arg_count; ++i)
1701 set_variable (var, arg_types[i]);
1703 if (arg_types[i].iswide ())
1708 states = (state **) _Jv_Malloc (sizeof (state *)
1709 * current_method->code_length);
1710 for (int i = 0; i < current_method->code_length; ++i)
1713 next_verify_pc = state::NO_NEXT;
1717 // If the PC was invalidated, get a new one from the work list.
1718 if (PC == state::NO_NEXT)
1721 if (PC == state::INVALID)
1722 verify_fail ("saw state::INVALID", start_PC);
1723 if (PC == state::NO_NEXT)
1725 // Set up the current state.
1726 *current_state = *states[PC];
1729 // Control can't fall off the end of the bytecode.
1730 if (PC >= current_method->code_length)
1731 verify_fail ("fell off end");
1733 if (states[PC] != NULL)
1735 // We've already visited this instruction. So merge the
1736 // states together. If this yields no change then we don't
1737 // have to re-verify.
1738 if (! current_state->merge (states[PC], false,
1739 current_method->max_stack))
1744 // Save a copy of it for later.
1745 states[PC]->copy (current_state, current_method->max_stack,
1746 current_method->max_locals);
1748 else if ((flags[PC] & FLAG_BRANCH_TARGET))
1750 // We only have to keep saved state at branch targets.
1751 states[PC] = new state (current_state, current_method->max_stack,
1752 current_method->max_locals);
1755 // Update states for all active exception handlers. Ordinarily
1756 // there are not many exception handlers. So we simply run
1757 // through them all.
1758 for (int i = 0; i < current_method->exc_count; ++i)
1760 if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
1762 type handler = reference_type;
1763 if (exception[i].handler_type != 0)
1764 handler = check_class_constant (exception[i].handler_type);
1765 push_exception_jump (handler, exception[i].handler_pc);
1770 unsigned char opcode = bytecode[PC++];
1776 case op_aconst_null:
1777 push_type (null_type);
1787 push_type (int_type);
1792 push_type (long_type);
1798 push_type (float_type);
1803 push_type (double_type);
1808 push_type (int_type);
1813 push_type (int_type);
1817 push_type (check_constant (get_byte ()));
1820 push_type (check_constant (get_ushort ()));
1823 push_type (check_wide_constant (get_ushort ()));
1827 push_type (get_variable (get_byte (), int_type));
1830 push_type (get_variable (get_byte (), long_type));
1833 push_type (get_variable (get_byte (), float_type));
1836 push_type (get_variable (get_byte (), double_type));
1839 push_type (get_variable (get_byte (), reference_type));
1846 push_type (get_variable (opcode - op_iload_0, int_type));
1852 push_type (get_variable (opcode - op_lload_0, long_type));
1858 push_type (get_variable (opcode - op_fload_0, float_type));
1864 push_type (get_variable (opcode - op_dload_0, double_type));
1870 push_type (get_variable (opcode - op_aload_0, reference_type));
1873 pop_type (int_type);
1874 push_type (require_array_type (pop_type (reference_type),
1878 pop_type (int_type);
1879 push_type (require_array_type (pop_type (reference_type),
1883 pop_type (int_type);
1884 push_type (require_array_type (pop_type (reference_type),
1888 pop_type (int_type);
1889 push_type (require_array_type (pop_type (reference_type),
1893 pop_type (int_type);
1894 push_type (require_array_type (pop_type (reference_type),
1898 pop_type (int_type);
1899 require_array_type (pop_type (reference_type), byte_type);
1900 push_type (int_type);
1903 pop_type (int_type);
1904 require_array_type (pop_type (reference_type), char_type);
1905 push_type (int_type);
1908 pop_type (int_type);
1909 require_array_type (pop_type (reference_type), short_type);
1910 push_type (int_type);
1913 set_variable (get_byte (), pop_type (int_type));
1916 set_variable (get_byte (), pop_type (long_type));
1919 set_variable (get_byte (), pop_type (float_type));
1922 set_variable (get_byte (), pop_type (double_type));
1925 set_variable (get_byte (), pop_type (reference_type));
1931 set_variable (opcode - op_istore_0, pop_type (int_type));
1937 set_variable (opcode - op_lstore_0, pop_type (long_type));
1943 set_variable (opcode - op_fstore_0, pop_type (float_type));
1949 set_variable (opcode - op_dstore_0, pop_type (double_type));
1955 set_variable (opcode - op_astore_0, pop_type (reference_type));
1958 pop_type (int_type);
1959 pop_type (int_type);
1960 require_array_type (pop_type (reference_type), int_type);
1963 pop_type (long_type);
1964 pop_type (int_type);
1965 require_array_type (pop_type (reference_type), long_type);
1968 pop_type (float_type);
1969 pop_type (int_type);
1970 require_array_type (pop_type (reference_type), float_type);
1973 pop_type (double_type);
1974 pop_type (int_type);
1975 require_array_type (pop_type (reference_type), double_type);
1978 pop_type (reference_type);
1979 pop_type (int_type);
1980 require_array_type (pop_type (reference_type), reference_type);
1983 pop_type (int_type);
1984 pop_type (int_type);
1985 require_array_type (pop_type (reference_type), byte_type);
1988 pop_type (int_type);
1989 pop_type (int_type);
1990 require_array_type (pop_type (reference_type), char_type);
1993 pop_type (int_type);
1994 pop_type (int_type);
1995 require_array_type (pop_type (reference_type), short_type);
2022 type t2 = pop_raw ();
2037 type t = pop_raw ();
2050 type t1 = pop_raw ();
2068 type t1 = pop_raw ();
2071 type t2 = pop_raw ();
2089 type t3 = pop_raw ();
2127 pop_type (int_type);
2128 push_type (pop_type (int_type));
2141 pop_type (long_type);
2142 push_type (pop_type (long_type));
2149 pop_type (float_type);
2150 push_type (pop_type (float_type));
2157 pop_type (double_type);
2158 push_type (pop_type (double_type));
2164 push_type (pop_type (int_type));
2167 push_type (pop_type (long_type));
2170 push_type (pop_type (float_type));
2173 push_type (pop_type (double_type));
2176 get_variable (get_byte (), int_type);
2180 pop_type (int_type);
2181 push_type (long_type);
2184 pop_type (int_type);
2185 push_type (float_type);
2188 pop_type (int_type);
2189 push_type (double_type);
2192 pop_type (long_type);
2193 push_type (int_type);
2196 pop_type (long_type);
2197 push_type (float_type);
2200 pop_type (long_type);
2201 push_type (double_type);
2204 pop_type (float_type);
2205 push_type (int_type);
2208 pop_type (float_type);
2209 push_type (long_type);
2212 pop_type (float_type);
2213 push_type (double_type);
2216 pop_type (double_type);
2217 push_type (int_type);
2220 pop_type (double_type);
2221 push_type (long_type);
2224 pop_type (double_type);
2225 push_type (float_type);
2228 pop_type (long_type);
2229 pop_type (long_type);
2230 push_type (int_type);
2234 pop_type (float_type);
2235 pop_type (float_type);
2236 push_type (int_type);
2240 pop_type (double_type);
2241 pop_type (double_type);
2242 push_type (int_type);
2250 pop_type (int_type);
2251 push_jump (get_short ());
2259 pop_type (int_type);
2260 pop_type (int_type);
2261 push_jump (get_short ());
2265 pop_type (reference_type);
2266 pop_type (reference_type);
2267 push_jump (get_short ());
2270 push_jump (get_short ());
2274 handle_jsr_insn (get_short ());
2277 handle_ret_insn (get_byte ());
2279 case op_tableswitch:
2281 pop_type (int_type);
2283 push_jump (get_int ());
2284 jint low = get_int ();
2285 jint high = get_int ();
2286 // Already checked LOW -vs- HIGH.
2287 for (int i = low; i <= high; ++i)
2288 push_jump (get_int ());
2293 case op_lookupswitch:
2295 pop_type (int_type);
2297 push_jump (get_int ());
2298 jint npairs = get_int ();
2299 // Already checked NPAIRS >= 0.
2301 for (int i = 0; i < npairs; ++i)
2303 jint key = get_int ();
2304 if (i > 0 && key <= lastkey)
2305 verify_fail ("lookupswitch pairs unsorted", start_PC);
2307 push_jump (get_int ());
2313 check_return_type (pop_type (int_type));
2317 check_return_type (pop_type (long_type));
2321 check_return_type (pop_type (float_type));
2325 check_return_type (pop_type (double_type));
2329 check_return_type (pop_type (reference_type));
2333 check_return_type (void_type);
2337 push_type (check_field_constant (get_ushort ()));
2340 pop_type (check_field_constant (get_ushort ()));
2345 type field = check_field_constant (get_ushort (), &klass);
2353 type field = check_field_constant (get_ushort (), &klass);
2359 case op_invokevirtual:
2360 case op_invokespecial:
2361 case op_invokestatic:
2362 case op_invokeinterface:
2364 _Jv_Utf8Const *method_name, *method_signature;
2366 = check_method_constant (get_ushort (),
2367 opcode == (unsigned char) op_invokeinterface,
2370 int arg_count = _Jv_count_arguments (method_signature);
2371 if (opcode == (unsigned char) op_invokeinterface)
2373 int nargs = get_byte ();
2375 verify_fail ("too few arguments to invokeinterface",
2377 if (get_byte () != 0)
2378 verify_fail ("invokeinterface dummy byte is wrong",
2380 if (nargs - 1 != arg_count)
2381 verify_fail ("wrong argument count for invokeinterface",
2385 bool is_init = false;
2386 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2389 if (opcode != (unsigned char) op_invokespecial)
2390 verify_fail ("can't invoke <init>", start_PC);
2392 else if (method_name->data[0] == '<')
2393 verify_fail ("can't invoke method starting with `<'",
2396 // Pop arguments and check types.
2397 type arg_types[arg_count];
2398 compute_argument_types (method_signature, arg_types);
2399 for (int i = arg_count - 1; i >= 0; --i)
2400 pop_type (arg_types[i]);
2402 if (opcode != (unsigned char) op_invokestatic)
2404 type t = class_type;
2407 // In this case the PC doesn't matter.
2408 t.set_uninitialized (type::UNINIT);
2412 current_state->set_initialized (t.get_pc (),
2413 current_method->max_locals);
2416 type rt = compute_return_type (method_signature);
2424 type t = check_class_constant (get_ushort ());
2425 if (t.isarray () || t.isinterface () || t.isabstract ())
2426 verify_fail ("type is array, interface, or abstract",
2428 t.set_uninitialized (start_PC);
2435 int atype = get_byte ();
2436 // We intentionally have chosen constants to make this
2438 if (atype < boolean_type || atype > long_type)
2439 verify_fail ("type not primitive", start_PC);
2440 pop_type (int_type);
2441 push_type (construct_primitive_array_type (type_val (atype)));
2445 pop_type (int_type);
2446 push_type (check_class_constant (get_ushort ()).to_array ());
2448 case op_arraylength:
2450 type t = pop_type (reference_type);
2452 verify_fail ("array type expected", start_PC);
2453 push_type (int_type);
2457 pop_type (type (&java::lang::Throwable::class$));
2461 pop_type (reference_type);
2462 push_type (check_class_constant (get_ushort ()));
2465 pop_type (reference_type);
2466 check_class_constant (get_ushort ());
2467 push_type (int_type);
2469 case op_monitorenter:
2470 pop_type (reference_type);
2472 case op_monitorexit:
2473 pop_type (reference_type);
2477 switch (get_byte ())
2480 push_type (get_variable (get_ushort (), int_type));
2483 push_type (get_variable (get_ushort (), long_type));
2486 push_type (get_variable (get_ushort (), float_type));
2489 push_type (get_variable (get_ushort (), double_type));
2492 push_type (get_variable (get_ushort (), reference_type));
2495 set_variable (get_ushort (), pop_type (int_type));
2498 set_variable (get_ushort (), pop_type (long_type));
2501 set_variable (get_ushort (), pop_type (float_type));
2504 set_variable (get_ushort (), pop_type (double_type));
2507 set_variable (get_ushort (), pop_type (reference_type));
2510 handle_ret_insn (get_short ());
2513 get_variable (get_ushort (), int_type);
2517 verify_fail ("unrecognized wide instruction", start_PC);
2521 case op_multianewarray:
2523 type atype = check_class_constant (get_ushort ());
2524 int dim = get_byte ();
2526 verify_fail ("too few dimensions to multianewarray", start_PC);
2527 atype.verify_dimensions (dim);
2528 for (int i = 0; i < dim; ++i)
2529 pop_type (int_type);
2535 pop_type (reference_type);
2536 push_jump (get_short ());
2539 push_jump (get_int ());
2543 handle_jsr_insn (get_int ());
2547 // Unrecognized opcode.
2548 verify_fail ("unrecognized instruction in verify_instructions_0",
2556 void verify_instructions ()
2559 verify_instructions_0 ();
2562 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2565 bytecode = m->bytecode ();
2566 exception = m->exceptions ();
2567 current_class = m->defining_class;
2574 ~_Jv_BytecodeVerifier ()
2581 _Jv_Free (jsr_ptrs);
2586 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
2588 _Jv_BytecodeVerifier v (meth);
2589 v.verify_instructions ();
2592 // FIXME: add more info, like PC, when required.
2594 verify_fail (char *s, jint pc)
2596 using namespace java::lang;
2597 StringBuffer *buf = new StringBuffer ();
2599 buf->append (JvNewStringLatin1 ("verification failed"));
2602 buf->append (JvNewStringLatin1 (" at PC "));
2605 buf->append (JvNewStringLatin1 (": "));
2606 buf->append (JvNewStringLatin1 (s));
2607 throw new java::lang::VerifyError (buf->toString ());
2610 #endif /* INTERPRETER */