1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 // Writte by Tom Tromey <tromey@redhat.com>
17 #include <java-insns.h>
18 #include <java-interp.h>
22 #include <java/lang/Class.h>
23 #include <java/lang/VerifyError.h>
24 #include <java/lang/Throwable.h>
25 #include <java/lang/reflect/Modifier.h>
29 // * read more about when classes must be loaded
30 // * there are bugs with boolean arrays?
31 // * class loader madness
32 // * Lots and lots of debugging and testing
33 // * type representation is still ugly. look for the big switches
34 // * at least one GC problem :-(
37 // This is global because __attribute__ doesn't seem to work on static
39 static void verify_fail (char *s) __attribute__ ((__noreturn__));
41 class _Jv_BytecodeVerifier
45 static const int FLAG_INSN_START = 1;
46 static const int FLAG_BRANCH_TARGET = 2;
47 static const int FLAG_JSR_TARGET = 4;
55 // The PC corresponding to the start of the current instruction.
58 // The current state of the stack, locals, etc.
61 // We store the state at branch targets, for merging. This holds
65 // We keep a linked list of all the PCs which we must reverify.
66 // The link is done using the PC values. This is the head of the
70 // We keep some flags for each instruction. The values are the
71 // FLAG_* constants defined above.
74 // We need to keep track of which instructions can call a given
75 // subroutine. FIXME: this is inefficient. We keep a linked list
76 // of all calling `jsr's at at each jsr target.
79 // The current top of the stack, in terms of slots.
81 // The current depth of the stack. This will be larger than
82 // STACKTOP when wide types are on the stack.
85 // The bytecode itself.
86 unsigned char *bytecode;
88 _Jv_InterpException *exception;
93 _Jv_InterpMethod *current_method;
95 // This enum holds a list of tags for all the different types we
96 // need to handle. Reference types are treated specially by the
102 // The values for primitive types are chosen to correspond to values
103 // specified to newarray.
113 // Used when overwriting second word of a double or long in the
114 // local variables. Also used after merging local variable states
115 // to indicate an unusable value.
120 // Everything after `reference_type' must be a reference type.
123 unresolved_reference_type,
124 uninitialized_reference_type,
125 uninitialized_unresolved_reference_type
128 // Return the type_val corresponding to a primitive signature
129 // character. For instance `I' returns `int.class'.
130 static type_val get_type_val_for_signature (jchar sig)
160 verify_fail ("invalid signature");
165 // Return the type_val corresponding to a primitive class.
166 static type_val get_type_val_for_signature (jclass k)
168 return get_type_val_for_signature ((jchar) k->method_count);
171 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
172 // TARGET haven't been prepared.
173 static bool is_assignable_from_slow (jclass target, jclass source)
175 // This will terminate when SOURCE==Object.
178 if (source == target)
181 if (target->isPrimitive () || source->isPrimitive ())
184 // _Jv_IsAssignableFrom can handle a target which is an
185 // interface even if it hasn't been prepared.
186 if ((target->state > JV_STATE_LINKED || target->isInterface ())
187 && source->state > JV_STATE_LINKED)
188 return _Jv_IsAssignableFrom (target, source);
190 if (target->isArray ())
192 if (! source->isArray ())
194 target = target->getComponentType ();
195 source = source->getComponentType ();
197 else if (target->isInterface ())
199 for (int i = 0; i < source->interface_count; ++i)
201 // We use a recursive call because we also need to
202 // check superinterfaces.
203 if (is_assignable_from_slow (target, source->interfaces[i]))
208 else if (target == &java::lang::Object::class$)
210 else if (source->isInterface ()
211 || source == &java::lang::Object::class$)
214 source = source->getSuperclass ();
218 // This is used to keep track of which `jsr's correspond to a given
222 // PC of the instruction just after the jsr.
228 // The `type' class is used to represent a single type in the
234 // Some associated data.
237 // For a resolved reference type, this is a pointer to the class.
239 // For other reference types, this it the name of the class.
242 // This is used when constructing a new object. It is the PC of the
243 // `new' instruction which created the object. We use the special
244 // value -2 to mean that this is uninitialized, and the special
245 // value -1 for the case where the current method is itself the
249 static const int UNINIT = -2;
250 static const int SELF = -1;
252 // Basic constructor.
255 key = unsuitable_type;
260 // Make a new instance given the type tag. We assume a generic
261 // `reference_type' means Object.
266 if (key == reference_type)
267 data.klass = &java::lang::Object::class$;
271 // Make a new instance given a class.
274 key = reference_type;
279 // Make a new instance given the name of a class.
280 type (_Jv_Utf8Const *n)
282 key = unresolved_reference_type;
295 // These operators are required because libgcj can't link in
297 void *operator new[] (size_t bytes)
299 return _Jv_Malloc (bytes);
302 void operator delete[] (void *mem)
307 type& operator= (type_val k)
315 type& operator= (const type& t)
323 // Promote a numeric type.
326 if (key == boolean_type || key == char_type
327 || key == byte_type || key == short_type)
332 // If *THIS is an unresolved reference type, resolve it.
335 if (key != unresolved_reference_type
336 && key != uninitialized_unresolved_reference_type)
339 // FIXME: class loader
340 using namespace java::lang;
341 // We might see either kind of name. Sigh.
342 if (data.name->data[0] == 'L'
343 && data.name->data[data.name->length - 1] == ';')
344 data.klass = _Jv_FindClassFromSignature (data.name->data, NULL);
346 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
348 key = (key == unresolved_reference_type
350 : uninitialized_reference_type);
353 // Mark this type as the uninitialized result of `new'.
354 void set_uninitialized (int pc)
356 if (key != reference_type && key != unresolved_reference_type)
357 verify_fail ("internal error in type::uninitialized");
358 key = (key == reference_type
359 ? uninitialized_reference_type
360 : uninitialized_unresolved_reference_type);
364 // Mark this type as now initialized.
365 void set_initialized (int npc)
369 key = (key == uninitialized_reference_type
371 : unresolved_reference_type);
377 // Return true if an object of type K can be assigned to a variable
378 // of type *THIS. Handle various special cases too. Might modify
379 // *THIS or K. Note however that this does not perform numeric
381 bool compatible (type &k)
383 // Any type is compatible with the unsuitable type.
384 if (key == unsuitable_type)
387 if (key < reference_type || k.key < reference_type)
390 // The `null' type is convertible to any reference type.
391 // FIXME: is this correct for THIS?
392 if (key == null_type || k.key == null_type)
395 // Any reference type is convertible to Object. This is a special
396 // case so we don't need to unnecessarily resolve a class.
397 if (key == reference_type
398 && data.klass == &java::lang::Object::class$)
401 // An initialized type and an uninitialized type are not
403 if (isinitialized () != k.isinitialized ())
406 // Two uninitialized objects are compatible if either:
407 // * The PCs are identical, or
408 // * One PC is UNINIT.
409 if (! isinitialized ())
411 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
415 // Two unresolved types are equal if their names are the same.
418 && _Jv_equalUtf8Consts (data.name, k.data.name))
421 // We must resolve both types and check assignability.
424 return is_assignable_from_slow (data.klass, k.data.klass);
429 return key == void_type;
434 return key == long_type || key == double_type;
437 // Return number of stack or local variable slots taken by this
441 return iswide () ? 2 : 1;
444 bool isarray () const
446 // We treat null_type as not an array. This is ok based on the
447 // current uses of this method.
448 if (key == reference_type)
449 return data.klass->isArray ();
450 else if (key == unresolved_reference_type)
451 return data.name->data[0] == '[';
458 if (key != reference_type)
460 return data.klass->isInterface ();
466 if (key != reference_type)
468 using namespace java::lang::reflect;
469 return Modifier::isAbstract (data.klass->getModifiers ());
472 // Return the element type of an array.
475 // FIXME: maybe should do string manipulation here.
477 if (key != reference_type)
478 verify_fail ("programmer error in type::element_type()");
480 jclass k = data.klass->getComponentType ();
481 if (k->isPrimitive ())
482 return type (get_type_val_for_signature (k));
486 bool isreference () const
488 return key >= reference_type;
496 bool isinitialized () const
498 return (key == reference_type
500 || key == unresolved_reference_type);
503 bool isresolved () const
505 return (key == reference_type
507 || key == uninitialized_reference_type);
510 void verify_dimensions (int ndims)
512 // The way this is written, we don't need to check isarray().
513 if (key == reference_type)
515 jclass k = data.klass;
516 while (k->isArray () && ndims > 0)
518 k = k->getComponentType ();
524 // We know KEY == unresolved_reference_type.
525 char *p = data.name->data;
526 while (*p++ == '[' && ndims-- > 0)
531 verify_fail ("array type has fewer dimensions than required");
534 // Merge OLD_TYPE into this. On error throw exception.
535 bool merge (type& old_type, bool local_semantics = false)
537 bool changed = false;
538 bool refo = old_type.isreference ();
539 bool refn = isreference ();
542 if (old_type.key == null_type)
544 else if (key == null_type)
549 else if (isinitialized () != old_type.isinitialized ())
550 verify_fail ("merging initialized and uninitialized types");
553 if (! isinitialized ())
557 else if (old_type.pc == UNINIT)
559 else if (pc != old_type.pc)
560 verify_fail ("merging different uninitialized types");
564 && ! old_type.isresolved ()
565 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
567 // Types are identical.
574 jclass k = data.klass;
575 jclass oldk = old_type.data.klass;
578 while (k->isArray () && oldk->isArray ())
581 k = k->getComponentType ();
582 oldk = oldk->getComponentType ();
585 // This loop will end when we hit Object.
588 if (is_assignable_from_slow (k, oldk))
590 k = k->getSuperclass ();
596 while (arraycount > 0)
598 // FIXME: Class loader.
599 k = _Jv_GetArrayClass (k, NULL);
607 else if (refo || refn || key != old_type.key)
611 key = unsuitable_type;
615 verify_fail ("unmergeable type");
621 // This class holds all the state information we need for a given
625 // Current top of stack.
627 // Current stack depth. This is like the top of stack but it
628 // includes wide variable information.
632 // The local variables.
634 // This is used in subroutines to keep track of which local
635 // variables have been accessed.
637 // If not 0, then we are in a subroutine. The value is the PC of
638 // the subroutine's entry point. We can use 0 as an exceptional
639 // value because PC=0 can never be a subroutine.
641 // This is used to keep a linked list of all the states which
642 // require re-verification. We use the PC to keep track.
645 // INVALID marks a state which is not on the linked list of states
646 // requiring reverification.
647 static const int INVALID = -1;
648 // NO_NEXT marks the state at the end of the reverification list.
649 static const int NO_NEXT = -2;
655 local_changed = NULL;
658 state (int max_stack, int max_locals)
662 stack = new type[max_stack];
663 for (int i = 0; i < max_stack; ++i)
664 stack[i] = unsuitable_type;
665 locals = new type[max_locals];
666 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
667 for (int i = 0; i < max_locals; ++i)
669 locals[i] = unsuitable_type;
670 local_changed[i] = false;
676 state (const state *copy, int max_stack, int max_locals)
678 stack = new type[max_stack];
679 locals = new type[max_locals];
680 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
692 _Jv_Free (local_changed);
695 void *operator new[] (size_t bytes)
697 return _Jv_Malloc (bytes);
700 void operator delete[] (void *mem)
705 void *operator new (size_t bytes)
707 return _Jv_Malloc (bytes);
710 void operator delete (void *mem)
715 void copy (const state *copy, int max_stack, int max_locals)
717 stacktop = copy->stacktop;
718 stackdepth = copy->stackdepth;
719 subroutine = copy->subroutine;
720 for (int i = 0; i < max_stack; ++i)
721 stack[i] = copy->stack[i];
722 for (int i = 0; i < max_locals; ++i)
724 locals[i] = copy->locals[i];
725 local_changed[i] = copy->local_changed[i];
727 // Don't modify `next'.
730 // Modify this state to reflect entry to an exception handler.
731 void set_exception (type t, int max_stack)
736 for (int i = stacktop; i < max_stack; ++i)
737 stack[i] = unsuitable_type;
739 // FIXME: subroutine handling?
742 // Merge STATE into this state. Destructively modifies this state.
743 // Returns true if the new state was in fact changed. Will throw an
744 // exception if the states are not mergeable.
745 bool merge (state *state_old, bool ret_semantics,
748 bool changed = false;
750 // Merge subroutine states. *THIS and *STATE_OLD must be in the
751 // same subroutine. Also, recursive subroutine calls must be
753 if (subroutine == state_old->subroutine)
757 else if (subroutine == 0)
759 subroutine = state_old->subroutine;
763 verify_fail ("subroutines merged");
766 if (state_old->stacktop != stacktop)
767 verify_fail ("stack sizes differ");
768 for (int i = 0; i < state_old->stacktop; ++i)
770 if (stack[i].merge (state_old->stack[i]))
774 // Merge local variables.
775 for (int i = 0; i < max_locals; ++i)
777 if (! ret_semantics || local_changed[i])
779 if (locals[i].merge (state_old->locals[i], true))
786 // If we're in a subroutine, we must compute the union of
787 // all the changed local variables.
788 if (state_old->local_changed[i])
795 // Throw an exception if there is an uninitialized object on the
796 // stack or in a local variable. EXCEPTION_SEMANTICS controls
797 // whether we're using backwards-branch or exception-handing
799 void check_no_uninitialized_objects (int max_locals,
800 bool exception_semantics = false)
802 if (! exception_semantics)
804 for (int i = 0; i < stacktop; ++i)
805 if (stack[i].isreference () && ! stack[i].isinitialized ())
806 verify_fail ("uninitialized object on stack");
809 for (int i = 0; i < max_locals; ++i)
810 if (locals[i].isreference () && ! locals[i].isinitialized ())
811 verify_fail ("uninitialized object in local variable");
814 // Note that a local variable was accessed or modified.
815 void note_variable (int index)
818 local_changed[index] = true;
821 // Mark each `new'd object we know of that was allocated at PC as
823 void set_initialized (int pc, int max_locals)
825 for (int i = 0; i < stacktop; ++i)
826 stack[i].set_initialized (pc);
827 for (int i = 0; i < max_locals; ++i)
828 locals[i].set_initialized (pc);
834 if (current_state->stacktop <= 0)
835 verify_fail ("stack empty");
836 type r = current_state->stack[--current_state->stacktop];
837 current_state->stackdepth -= r.depth ();
838 if (current_state->stackdepth < 0)
839 verify_fail ("stack empty");
847 verify_fail ("narrow pop of wide type");
855 verify_fail ("wide pop of narrow type");
859 type pop_type (type match)
862 if (! match.compatible (t))
863 verify_fail ("incompatible type on stack");
867 void push_type (type t)
869 // If T is a numeric type like short, promote it to int.
872 int depth = t.depth ();
873 if (current_state->stackdepth + depth > current_method->max_stack)
874 verify_fail ("stack overflow");
875 current_state->stack[current_state->stacktop++] = t;
876 current_state->stackdepth += depth;
879 void set_variable (int index, type t)
881 // If T is a numeric type like short, promote it to int.
884 int depth = t.depth ();
885 if (index > current_method->max_locals - depth)
886 verify_fail ("invalid local variable");
887 current_state->locals[index] = t;
888 current_state->note_variable (index);
892 current_state->locals[index + 1] = continuation_type;
893 current_state->note_variable (index + 1);
895 if (index > 0 && current_state->locals[index - 1].iswide ())
897 current_state->locals[index - 1] = unsuitable_type;
898 // There's no need to call note_variable here.
902 type get_variable (int index, type t)
904 int depth = t.depth ();
905 if (index > current_method->max_locals - depth)
906 verify_fail ("invalid local variable");
907 if (! t.compatible (current_state->locals[index]))
908 verify_fail ("incompatible type in local variable");
911 type t (continuation_type);
912 if (! current_state->locals[index + 1].compatible (t))
913 verify_fail ("invalid local variable");
915 current_state->note_variable (index);
916 return current_state->locals[index];
919 // Make sure ARRAY is an array type and that its elements are
920 // compatible with type ELEMENT. Returns the actual element type.
921 type require_array_type (type array, type element)
923 if (! array.isarray ())
924 verify_fail ("array required");
926 type t = array.element_type ();
927 if (! element.compatible (t))
928 verify_fail ("incompatible array element type");
930 // Return T and not ELEMENT, because T might be specialized.
936 if (PC >= current_method->code_length)
937 verify_fail ("premature end of bytecode");
938 return (jint) bytecode[PC++] & 0xff;
943 jbyte b1 = get_byte ();
944 jbyte b2 = get_byte ();
945 return (jint) ((b1 << 8) | b2) & 0xffff;
950 jbyte b1 = get_byte ();
951 jbyte b2 = get_byte ();
952 jshort s = (b1 << 8) | b2;
958 jbyte b1 = get_byte ();
959 jbyte b2 = get_byte ();
960 jbyte b3 = get_byte ();
961 jbyte b4 = get_byte ();
962 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
965 int compute_jump (int offset)
967 int npc = start_PC + offset;
968 if (npc < 0 || npc >= current_method->code_length)
969 verify_fail ("branch out of range");
973 // Merge the indicated state into a new state and schedule a new PC if
974 // there is a change. If RET_SEMANTICS is true, then we are merging
975 // from a `ret' instruction into the instruction after a `jsr'. This
976 // is a special case with its own modified semantics.
977 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
980 if (states[npc] == NULL)
982 // FIXME: what if we reach this code from a `ret'?
984 states[npc] = new state (nstate, current_method->max_stack,
985 current_method->max_locals);
988 changed = nstate->merge (states[npc], ret_semantics,
989 current_method->max_stack);
991 if (changed && states[npc]->next == state::INVALID)
993 // The merge changed the state, and the new PC isn't yet on our
994 // list of PCs to re-verify.
995 states[npc]->next = next_verify_pc;
996 next_verify_pc = npc;
1000 void push_jump (int offset)
1002 int npc = compute_jump (offset);
1004 current_state->check_no_uninitialized_objects (current_method->max_stack);
1005 push_jump_merge (npc, current_state);
1008 void push_exception_jump (type t, int pc)
1010 current_state->check_no_uninitialized_objects (current_method->max_stack,
1012 state s (current_state, current_method->max_stack,
1013 current_method->max_locals);
1014 s.set_exception (t, current_method->max_stack);
1015 push_jump_merge (pc, &s);
1020 int npc = next_verify_pc;
1021 if (npc != state::NO_NEXT)
1023 next_verify_pc = states[npc]->next;
1024 states[npc]->next = state::INVALID;
1029 void invalidate_pc ()
1031 PC = state::NO_NEXT;
1034 void note_branch_target (int pc, bool is_jsr_target = false)
1036 if (pc <= PC && ! (flags[pc] & FLAG_INSN_START))
1037 verify_fail ("branch not to instruction start");
1038 flags[pc] |= FLAG_BRANCH_TARGET;
1041 // Record the jsr which called this instruction.
1042 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1044 info->next = jsr_ptrs[pc];
1045 jsr_ptrs[pc] = info;
1046 flags[pc] |= FLAG_JSR_TARGET;
1050 void skip_padding ()
1052 while ((PC % 4) > 0)
1053 if (get_byte () != 0)
1054 verify_fail ("found nonzero padding byte");
1057 // Return the subroutine to which the instruction at PC belongs.
1058 int get_subroutine (int pc)
1060 if (states[pc] == NULL)
1062 return states[pc]->subroutine;
1065 // Do the work for a `ret' instruction. INDEX is the index into the
1067 void handle_ret_insn (int index)
1069 get_variable (index, return_address_type);
1071 int csub = current_state->subroutine;
1073 verify_fail ("no subroutine");
1075 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1077 // Temporarily modify the current state so it looks like we're
1078 // in the enclosing context.
1079 current_state->subroutine = get_subroutine (subr->pc);
1081 current_state->check_no_uninitialized_objects (current_method->max_stack);
1082 push_jump_merge (subr->pc, current_state, true);
1085 current_state->subroutine = csub;
1089 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1090 // sure this subroutine isn't already on the stack.
1091 void check_nonrecursive_call (int sub, int dest)
1096 verify_fail ("recursive subroutine call");
1097 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1098 check_nonrecursive_call (get_subroutine (info->pc), dest);
1101 void handle_jsr_insn (int offset)
1103 int npc = compute_jump (offset);
1106 current_state->check_no_uninitialized_objects (current_method->max_stack);
1107 check_nonrecursive_call (current_state->subroutine, npc);
1109 // Temporarily modify the current state so that it looks like we are
1110 // in the subroutine.
1111 push_type (return_address_type);
1112 int save = current_state->subroutine;
1113 current_state->subroutine = npc;
1115 // Merge into the subroutine.
1116 push_jump_merge (npc, current_state);
1118 // Undo our modifications.
1119 current_state->subroutine = save;
1120 pop_type (return_address_type);
1123 jclass construct_primitive_array_type (type_val prim)
1129 k = JvPrimClass (boolean);
1132 k = JvPrimClass (char);
1135 k = JvPrimClass (float);
1138 k = JvPrimClass (double);
1141 k = JvPrimClass (byte);
1144 k = JvPrimClass (short);
1147 k = JvPrimClass (int);
1150 k = JvPrimClass (long);
1153 verify_fail ("unknown type in construct_primitive_array_type");
1155 k = _Jv_GetArrayClass (k, NULL);
1159 // This pass computes the location of branch targets and also
1160 // instruction starts.
1161 void branch_prepass ()
1163 flags = (char *) _Jv_Malloc (current_method->code_length);
1164 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1165 * current_method->code_length);
1167 for (int i = 0; i < current_method->code_length; ++i)
1173 bool last_was_jsr = false;
1176 while (PC < current_method->code_length)
1178 flags[PC] |= FLAG_INSN_START;
1180 // If the previous instruction was a jsr, then the next
1181 // instruction is a branch target -- the branch being the
1182 // corresponding `ret'.
1184 note_branch_target (PC);
1185 last_was_jsr = false;
1188 unsigned char opcode = bytecode[PC++];
1192 case op_aconst_null:
1329 case op_monitorenter:
1330 case op_monitorexit:
1353 case op_arraylength:
1369 case op_invokespecial:
1370 case op_invokestatic:
1371 case op_invokevirtual:
1375 case op_multianewarray:
1381 last_was_jsr = true;
1400 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1403 case op_tableswitch:
1406 note_branch_target (compute_jump (get_int ()));
1407 jint low = get_int ();
1408 jint hi = get_int ();
1410 verify_fail ("invalid tableswitch");
1411 for (int i = low; i <= hi; ++i)
1412 note_branch_target (compute_jump (get_int ()));
1416 case op_lookupswitch:
1419 note_branch_target (compute_jump (get_int ()));
1420 int npairs = get_int ();
1422 verify_fail ("too few pairs in lookupswitch");
1423 while (npairs-- > 0)
1426 note_branch_target (compute_jump (get_int ()));
1431 case op_invokeinterface:
1439 opcode = get_byte ();
1441 if (opcode == (unsigned char) op_iinc)
1447 last_was_jsr = true;
1450 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1454 verify_fail ("unrecognized instruction in branch_prepass");
1457 // See if any previous branch tried to branch to the middle of
1458 // this instruction.
1459 for (int pc = start_PC + 1; pc < PC; ++pc)
1461 if ((flags[pc] & FLAG_BRANCH_TARGET))
1462 verify_fail ("branch not to instruction start");
1466 // Verify exception handlers.
1467 for (int i = 0; i < current_method->exc_count; ++i)
1469 if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
1470 verify_fail ("exception handler not at instruction start");
1471 if (exception[i].start_pc > exception[i].end_pc)
1472 verify_fail ("exception range inverted");
1473 if (! (flags[exception[i].start_pc] & FLAG_INSN_START)
1474 || ! (flags[exception[i].start_pc] & FLAG_INSN_START))
1475 verify_fail ("exception endpoint not at instruction start");
1477 flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
1481 void check_pool_index (int index)
1483 if (index < 0 || index >= current_class->constants.size)
1484 verify_fail ("constant pool index out of range");
1487 type check_class_constant (int index)
1489 check_pool_index (index);
1490 _Jv_Constants *pool = ¤t_class->constants;
1491 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1492 return type (pool->data[index].clazz);
1493 else if (pool->tags[index] == JV_CONSTANT_Class)
1494 return type (pool->data[index].utf8);
1495 verify_fail ("expected class constant");
1498 type check_constant (int index)
1500 check_pool_index (index);
1501 _Jv_Constants *pool = ¤t_class->constants;
1502 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1503 || pool->tags[index] == JV_CONSTANT_String)
1504 return type (&java::lang::String::class$);
1505 else if (pool->tags[index] == JV_CONSTANT_Integer)
1506 return type (int_type);
1507 else if (pool->tags[index] == JV_CONSTANT_Float)
1508 return type (float_type);
1509 verify_fail ("String, int, or float constant expected");
1512 // Helper for both field and method. These are laid out the same in
1513 // the constant pool.
1514 type handle_field_or_method (int index, int expected,
1515 _Jv_Utf8Const **name,
1516 _Jv_Utf8Const **fmtype)
1518 check_pool_index (index);
1519 _Jv_Constants *pool = ¤t_class->constants;
1520 if (pool->tags[index] != expected)
1521 verify_fail ("didn't see expected constant");
1522 // Once we know we have a Fieldref or Methodref we assume that it
1523 // is correctly laid out in the constant pool. I think the code
1524 // in defineclass.cc guarantees this.
1525 _Jv_ushort class_index, name_and_type_index;
1526 _Jv_loadIndexes (&pool->data[index],
1528 name_and_type_index);
1529 _Jv_ushort name_index, desc_index;
1530 _Jv_loadIndexes (&pool->data[name_and_type_index],
1531 name_index, desc_index);
1533 *name = pool->data[name_index].utf8;
1534 *fmtype = pool->data[desc_index].utf8;
1536 return check_class_constant (class_index);
1539 // Return field's type, compute class' type if requested.
1540 type check_field_constant (int index, type *class_type = NULL)
1542 _Jv_Utf8Const *name, *field_type;
1543 type ct = handle_field_or_method (index,
1544 JV_CONSTANT_Fieldref,
1545 &name, &field_type);
1548 return type (field_type);
1551 type check_method_constant (int index, bool is_interface,
1552 _Jv_Utf8Const **method_name,
1553 _Jv_Utf8Const **method_signature)
1555 return handle_field_or_method (index,
1557 ? JV_CONSTANT_InterfaceMethodref
1558 : JV_CONSTANT_Methodref),
1559 method_name, method_signature);
1562 type get_one_type (char *&p)
1580 // FIXME! This will get collected!
1581 _Jv_Utf8Const *name = _Jv_makeUtf8Const (start, p - start);
1585 // Casting to jchar here is ok since we are looking at an ASCII
1587 type_val rt = get_type_val_for_signature (jchar (v));
1589 if (arraycount == 0)
1591 // Callers of this function eventually push their arguments on
1592 // the stack. So, promote them here.
1593 return type (rt).promote ();
1596 jclass k = construct_primitive_array_type (rt);
1597 while (--arraycount > 0)
1598 k = _Jv_GetArrayClass (k, NULL);
1602 void compute_argument_types (_Jv_Utf8Const *signature,
1605 char *p = signature->data;
1611 types[i++] = get_one_type (p);
1614 type compute_return_type (_Jv_Utf8Const *signature)
1616 char *p = signature->data;
1620 return get_one_type (p);
1623 void check_return_type (type expected)
1625 type rt = compute_return_type (current_method->self->signature);
1626 if (! expected.compatible (rt))
1627 verify_fail ("incompatible return type");
1630 void verify_instructions_0 ()
1632 current_state = new state (current_method->max_stack,
1633 current_method->max_locals);
1640 using namespace java::lang::reflect;
1641 if (! Modifier::isStatic (current_method->self->accflags))
1643 type kurr (current_class);
1644 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
1645 kurr.set_uninitialized (type::SELF);
1646 set_variable (0, kurr);
1650 if (var + _Jv_count_arguments (current_method->self->signature)
1651 > current_method->max_locals)
1652 verify_fail ("too many arguments");
1653 compute_argument_types (current_method->self->signature,
1654 ¤t_state->locals[var]);
1657 states = (state **) _Jv_Malloc (sizeof (state *)
1658 * current_method->code_length);
1659 for (int i = 0; i < current_method->code_length; ++i)
1662 next_verify_pc = state::NO_NEXT;
1666 // If the PC was invalidated, get a new one from the work list.
1667 if (PC == state::NO_NEXT)
1670 if (PC == state::INVALID)
1671 verify_fail ("saw state::INVALID");
1672 if (PC == state::NO_NEXT)
1674 // Set up the current state.
1675 *current_state = *states[PC];
1678 // Control can't fall off the end of the bytecode.
1679 if (PC >= current_method->code_length)
1680 verify_fail ("fell off end");
1682 if (states[PC] != NULL)
1684 // We've already visited this instruction. So merge the
1685 // states together. If this yields no change then we don't
1686 // have to re-verify.
1687 if (! current_state->merge (states[PC], false,
1688 current_method->max_stack))
1693 // Save a copy of it for later.
1694 states[PC]->copy (current_state, current_method->max_stack,
1695 current_method->max_locals);
1697 else if ((flags[PC] & FLAG_BRANCH_TARGET))
1699 // We only have to keep saved state at branch targets.
1700 states[PC] = new state (current_state, current_method->max_stack,
1701 current_method->max_locals);
1704 // Update states for all active exception handlers. Ordinarily
1705 // there are not many exception handlers. So we simply run
1706 // through them all.
1707 for (int i = 0; i < current_method->exc_count; ++i)
1709 if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
1711 type handler = reference_type;
1712 if (exception[i].handler_type != 0)
1713 handler = check_class_constant (exception[i].handler_type);
1714 push_exception_jump (handler, exception[i].handler_pc);
1719 unsigned char opcode = bytecode[PC++];
1725 case op_aconst_null:
1726 push_type (null_type);
1736 push_type (int_type);
1741 push_type (long_type);
1747 push_type (float_type);
1752 push_type (double_type);
1757 push_type (int_type);
1762 push_type (int_type);
1766 push_type (check_constant (get_byte ()));
1769 push_type (check_constant (get_ushort ()));
1772 push_type (check_constant (get_ushort ()));
1776 push_type (get_variable (get_byte (), int_type));
1779 push_type (get_variable (get_byte (), long_type));
1782 push_type (get_variable (get_byte (), float_type));
1785 push_type (get_variable (get_byte (), double_type));
1788 push_type (get_variable (get_byte (), reference_type));
1795 push_type (get_variable (opcode - op_iload_0, int_type));
1801 push_type (get_variable (opcode - op_lload_0, long_type));
1807 push_type (get_variable (opcode - op_fload_0, float_type));
1813 push_type (get_variable (opcode - op_dload_0, double_type));
1819 push_type (get_variable (opcode - op_aload_0, reference_type));
1822 pop_type (int_type);
1823 push_type (require_array_type (pop_type (reference_type),
1827 pop_type (int_type);
1828 push_type (require_array_type (pop_type (reference_type),
1832 pop_type (int_type);
1833 push_type (require_array_type (pop_type (reference_type),
1837 pop_type (int_type);
1838 push_type (require_array_type (pop_type (reference_type),
1842 pop_type (int_type);
1843 push_type (require_array_type (pop_type (reference_type),
1847 pop_type (int_type);
1848 require_array_type (pop_type (reference_type), byte_type);
1849 push_type (int_type);
1852 pop_type (int_type);
1853 require_array_type (pop_type (reference_type), char_type);
1854 push_type (int_type);
1857 pop_type (int_type);
1858 require_array_type (pop_type (reference_type), short_type);
1859 push_type (int_type);
1862 set_variable (get_byte (), pop_type (int_type));
1865 set_variable (get_byte (), pop_type (long_type));
1868 set_variable (get_byte (), pop_type (float_type));
1871 set_variable (get_byte (), pop_type (double_type));
1874 set_variable (get_byte (), pop_type (reference_type));
1880 set_variable (opcode - op_istore_0, pop_type (int_type));
1886 set_variable (opcode - op_lstore_0, pop_type (long_type));
1892 set_variable (opcode - op_fstore_0, pop_type (float_type));
1898 set_variable (opcode - op_dstore_0, pop_type (double_type));
1904 set_variable (opcode - op_astore_0, pop_type (reference_type));
1907 pop_type (int_type);
1908 pop_type (int_type);
1909 require_array_type (pop_type (reference_type), int_type);
1912 pop_type (long_type);
1913 pop_type (int_type);
1914 require_array_type (pop_type (reference_type), long_type);
1917 pop_type (float_type);
1918 pop_type (int_type);
1919 require_array_type (pop_type (reference_type), float_type);
1922 pop_type (double_type);
1923 pop_type (int_type);
1924 require_array_type (pop_type (reference_type), double_type);
1927 pop_type (reference_type);
1928 pop_type (int_type);
1929 require_array_type (pop_type (reference_type), reference_type);
1932 pop_type (int_type);
1933 pop_type (int_type);
1934 require_array_type (pop_type (reference_type), byte_type);
1937 pop_type (int_type);
1938 pop_type (int_type);
1939 require_array_type (pop_type (reference_type), char_type);
1942 pop_type (int_type);
1943 pop_type (int_type);
1944 require_array_type (pop_type (reference_type), short_type);
1971 type t2 = pop_raw ();
1986 type t = pop_raw ();
1999 type t1 = pop_raw ();
2017 type t1 = pop_raw ();
2020 type t2 = pop_raw ();
2038 type t3 = pop_raw ();
2076 pop_type (int_type);
2077 push_type (pop_type (int_type));
2090 pop_type (long_type);
2091 push_type (pop_type (long_type));
2098 pop_type (float_type);
2099 push_type (pop_type (float_type));
2106 pop_type (double_type);
2107 push_type (pop_type (double_type));
2113 push_type (pop_type (int_type));
2116 push_type (pop_type (long_type));
2119 push_type (pop_type (float_type));
2122 push_type (pop_type (double_type));
2125 get_variable (get_byte (), int_type);
2129 pop_type (int_type);
2130 push_type (long_type);
2133 pop_type (int_type);
2134 push_type (float_type);
2137 pop_type (int_type);
2138 push_type (double_type);
2141 pop_type (long_type);
2142 push_type (int_type);
2145 pop_type (long_type);
2146 push_type (float_type);
2149 pop_type (long_type);
2150 push_type (double_type);
2153 pop_type (float_type);
2154 push_type (int_type);
2157 pop_type (float_type);
2158 push_type (long_type);
2161 pop_type (float_type);
2162 push_type (double_type);
2165 pop_type (double_type);
2166 push_type (int_type);
2169 pop_type (double_type);
2170 push_type (long_type);
2173 pop_type (double_type);
2174 push_type (float_type);
2177 pop_type (long_type);
2178 pop_type (long_type);
2179 push_type (int_type);
2183 pop_type (float_type);
2184 pop_type (float_type);
2185 push_type (int_type);
2189 pop_type (double_type);
2190 pop_type (double_type);
2191 push_type (int_type);
2199 pop_type (int_type);
2200 push_jump (get_short ());
2208 pop_type (int_type);
2209 pop_type (int_type);
2210 push_jump (get_short ());
2214 pop_type (reference_type);
2215 pop_type (reference_type);
2216 push_jump (get_short ());
2219 push_jump (get_short ());
2223 handle_jsr_insn (get_short ());
2226 handle_ret_insn (get_byte ());
2228 case op_tableswitch:
2230 pop_type (int_type);
2232 push_jump (get_int ());
2233 jint low = get_int ();
2234 jint high = get_int ();
2235 // Already checked LOW -vs- HIGH.
2236 for (int i = low; i <= high; ++i)
2237 push_jump (get_int ());
2242 case op_lookupswitch:
2244 pop_type (int_type);
2246 push_jump (get_int ());
2247 jint npairs = get_int ();
2248 // Already checked NPAIRS >= 0.
2250 for (int i = 0; i < npairs; ++i)
2252 jint key = get_int ();
2253 if (i > 0 && key <= lastkey)
2254 verify_fail ("lookupswitch pairs unsorted");
2256 push_jump (get_int ());
2262 check_return_type (pop_type (int_type));
2266 check_return_type (pop_type (long_type));
2270 check_return_type (pop_type (float_type));
2274 check_return_type (pop_type (double_type));
2278 check_return_type (pop_type (reference_type));
2282 check_return_type (void_type);
2286 push_type (check_field_constant (get_ushort ()));
2289 pop_type (check_field_constant (get_ushort ()));
2294 type field = check_field_constant (get_ushort (), &klass);
2302 type field = check_field_constant (get_ushort (), &klass);
2308 case op_invokevirtual:
2309 case op_invokespecial:
2310 case op_invokestatic:
2311 case op_invokeinterface:
2313 _Jv_Utf8Const *method_name, *method_signature;
2315 = check_method_constant (get_ushort (),
2316 opcode == (unsigned char) op_invokeinterface,
2319 int arg_count = _Jv_count_arguments (method_signature);
2320 if (opcode == (unsigned char) op_invokeinterface)
2322 int nargs = get_byte ();
2324 verify_fail ("too few arguments to invokeinterface");
2325 if (get_byte () != 0)
2326 verify_fail ("invokeinterface dummy byte is wrong");
2327 if (nargs - 1 != arg_count)
2328 verify_fail ("wrong argument count for invokeinterface");
2331 bool is_init = false;
2332 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2335 if (opcode != (unsigned char) op_invokespecial)
2336 verify_fail ("can't invoke <init>");
2338 else if (method_name->data[0] == '<')
2339 verify_fail ("can't invoke method starting with `<'");
2341 // Pop arguments and check types.
2342 type arg_types[arg_count];
2343 compute_argument_types (method_signature, arg_types);
2344 for (int i = arg_count - 1; i >= 0; --i)
2345 pop_type (arg_types[i]);
2347 if (opcode != (unsigned char) op_invokestatic)
2349 type t = class_type;
2352 // In this case the PC doesn't matter.
2353 t.set_uninitialized (type::UNINIT);
2357 current_state->set_initialized (t.get_pc (),
2358 current_method->max_locals);
2361 type rt = compute_return_type (method_signature);
2369 type t = check_class_constant (get_ushort ());
2370 if (t.isarray () || t.isinterface () || t.isabstract ())
2371 verify_fail ("type is array, interface, or abstract");
2372 t.set_uninitialized (start_PC);
2379 int atype = get_byte ();
2380 // We intentionally have chosen constants to make this
2382 if (atype < boolean_type || atype > long_type)
2383 verify_fail ("type not primitive");
2384 pop_type (int_type);
2385 push_type (construct_primitive_array_type (type_val (atype)));
2389 pop_type (int_type);
2390 push_type (check_class_constant (get_ushort ()));
2392 case op_arraylength:
2394 type t = pop_type (reference_type);
2396 verify_fail ("array type expected");
2397 push_type (int_type);
2401 pop_type (type (&java::lang::Throwable::class$));
2405 pop_type (reference_type);
2406 push_type (check_class_constant (get_ushort ()));
2409 pop_type (reference_type);
2410 check_class_constant (get_ushort ());
2411 push_type (int_type);
2413 case op_monitorenter:
2414 pop_type (reference_type);
2416 case op_monitorexit:
2417 pop_type (reference_type);
2421 switch (get_byte ())
2424 push_type (get_variable (get_ushort (), int_type));
2427 push_type (get_variable (get_ushort (), long_type));
2430 push_type (get_variable (get_ushort (), float_type));
2433 push_type (get_variable (get_ushort (), double_type));
2436 push_type (get_variable (get_ushort (), reference_type));
2439 set_variable (get_ushort (), pop_type (int_type));
2442 set_variable (get_ushort (), pop_type (long_type));
2445 set_variable (get_ushort (), pop_type (float_type));
2448 set_variable (get_ushort (), pop_type (double_type));
2451 set_variable (get_ushort (), pop_type (reference_type));
2454 handle_ret_insn (get_short ());
2457 get_variable (get_ushort (), int_type);
2461 verify_fail ("unrecognized wide instruction");
2465 case op_multianewarray:
2467 type atype = check_class_constant (get_ushort ());
2468 int dim = get_byte ();
2470 verify_fail ("too few dimensions to multianewarray");
2471 atype.verify_dimensions (dim);
2472 for (int i = 0; i < dim; ++i)
2473 pop_type (int_type);
2479 pop_type (reference_type);
2480 push_jump (get_short ());
2483 push_jump (get_int ());
2487 handle_jsr_insn (get_int ());
2491 // Unrecognized opcode.
2492 verify_fail ("unrecognized instruction in verify_instructions_0");
2499 void verify_instructions ()
2502 verify_instructions_0 ();
2505 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2508 bytecode = m->bytecode ();
2509 exception = m->exceptions ();
2510 current_class = m->defining_class;
2517 ~_Jv_BytecodeVerifier ()
2524 _Jv_Free (jsr_ptrs);
2529 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
2531 _Jv_BytecodeVerifier v (meth);
2532 v.verify_instructions ();
2535 // FIXME: add more info, like PC, when required.
2537 verify_fail (char *s)
2540 strcpy (buf, "verification failed: ");
2542 throw new java::lang::VerifyError (JvNewStringLatin1 (buf));
2545 #endif /* INTERPRETER */