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 // * 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 *msg, jint pc = -1)
40 __attribute__ ((__noreturn__));
42 class _Jv_BytecodeVerifier
46 static const int FLAG_INSN_START = 1;
47 static const int FLAG_BRANCH_TARGET = 2;
48 static const int FLAG_JSR_TARGET = 4;
56 // The PC corresponding to the start of the current instruction.
59 // The current state of the stack, locals, etc.
62 // We store the state at branch targets, for merging. This holds
66 // We keep a linked list of all the PCs which we must reverify.
67 // The link is done using the PC values. This is the head of the
71 // We keep some flags for each instruction. The values are the
72 // FLAG_* constants defined above.
75 // We need to keep track of which instructions can call a given
76 // subroutine. FIXME: this is inefficient. We keep a linked list
77 // of all calling `jsr's at at each jsr target.
80 // The current top of the stack, in terms of slots.
82 // The current depth of the stack. This will be larger than
83 // STACKTOP when wide types are on the stack.
86 // The bytecode itself.
87 unsigned char *bytecode;
89 _Jv_InterpException *exception;
94 _Jv_InterpMethod *current_method;
96 // This enum holds a list of tags for all the different types we
97 // need to handle. Reference types are treated specially by the
103 // The values for primitive types are chosen to correspond to values
104 // specified to newarray.
114 // Used when overwriting second word of a double or long in the
115 // local variables. Also used after merging local variable states
116 // to indicate an unusable value.
121 // Everything after `reference_type' must be a reference type.
124 unresolved_reference_type,
125 uninitialized_reference_type,
126 uninitialized_unresolved_reference_type
129 // Return the type_val corresponding to a primitive signature
130 // character. For instance `I' returns `int.class'.
131 static type_val get_type_val_for_signature (jchar sig)
164 verify_fail ("invalid signature");
169 // Return the type_val corresponding to a primitive class.
170 static type_val get_type_val_for_signature (jclass k)
172 return get_type_val_for_signature ((jchar) k->method_count);
175 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
176 // TARGET haven't been prepared.
177 static bool is_assignable_from_slow (jclass target, jclass source)
179 // This will terminate when SOURCE==Object.
182 if (source == target)
185 if (target->isPrimitive () || source->isPrimitive ())
188 // _Jv_IsAssignableFrom can handle a target which is an
189 // interface even if it hasn't been prepared.
190 if ((target->state > JV_STATE_LINKED || target->isInterface ())
191 && source->state > JV_STATE_LINKED)
192 return _Jv_IsAssignableFrom (target, source);
194 if (target->isArray ())
196 if (! source->isArray ())
198 target = target->getComponentType ();
199 source = source->getComponentType ();
201 else if (target->isInterface ())
203 for (int i = 0; i < source->interface_count; ++i)
205 // We use a recursive call because we also need to
206 // check superinterfaces.
207 if (is_assignable_from_slow (target, source->interfaces[i]))
212 else if (target == &java::lang::Object::class$)
214 else if (source->isInterface ()
215 || source == &java::lang::Object::class$)
218 source = source->getSuperclass ();
222 // This is used to keep track of which `jsr's correspond to a given
226 // PC of the instruction just after the jsr.
232 // The `type' class is used to represent a single type in the
238 // Some associated data.
241 // For a resolved reference type, this is a pointer to the class.
243 // For other reference types, this it the name of the class.
246 // This is used when constructing a new object. It is the PC of the
247 // `new' instruction which created the object. We use the special
248 // value -2 to mean that this is uninitialized, and the special
249 // value -1 for the case where the current method is itself the
253 static const int UNINIT = -2;
254 static const int SELF = -1;
256 // Basic constructor.
259 key = unsuitable_type;
264 // Make a new instance given the type tag. We assume a generic
265 // `reference_type' means Object.
270 if (key == reference_type)
271 data.klass = &java::lang::Object::class$;
275 // Make a new instance given a class.
278 key = reference_type;
283 // Make a new instance given the name of a class.
284 type (_Jv_Utf8Const *n)
286 key = unresolved_reference_type;
299 // These operators are required because libgcj can't link in
301 void *operator new[] (size_t bytes)
303 return _Jv_Malloc (bytes);
306 void operator delete[] (void *mem)
311 type& operator= (type_val k)
319 type& operator= (const type& t)
327 // Promote a numeric type.
330 if (key == boolean_type || key == char_type
331 || key == byte_type || key == short_type)
336 // If *THIS is an unresolved reference type, resolve it.
339 if (key != unresolved_reference_type
340 && key != uninitialized_unresolved_reference_type)
343 // FIXME: class loader
344 using namespace java::lang;
345 // We might see either kind of name. Sigh.
346 if (data.name->data[0] == 'L'
347 && data.name->data[data.name->length - 1] == ';')
348 data.klass = _Jv_FindClassFromSignature (data.name->data, NULL);
350 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
352 key = (key == unresolved_reference_type
354 : uninitialized_reference_type);
357 // Mark this type as the uninitialized result of `new'.
358 void set_uninitialized (int npc)
360 if (key == reference_type)
361 key = uninitialized_reference_type;
362 else if (key == unresolved_reference_type)
363 key = uninitialized_unresolved_reference_type;
365 verify_fail ("internal error in type::uninitialized");
369 // Mark this type as now initialized.
370 void set_initialized (int npc)
372 if (npc != UNINIT && pc == npc
373 && (key == uninitialized_reference_type
374 || key == uninitialized_unresolved_reference_type))
376 key = (key == uninitialized_reference_type
378 : unresolved_reference_type);
384 // Return true if an object of type K can be assigned to a variable
385 // of type *THIS. Handle various special cases too. Might modify
386 // *THIS or K. Note however that this does not perform numeric
388 bool compatible (type &k)
390 // Any type is compatible with the unsuitable type.
391 if (key == unsuitable_type)
394 if (key < reference_type || k.key < reference_type)
397 // The `null' type is convertible to any reference type.
398 // FIXME: is this correct for THIS?
399 if (key == null_type || k.key == null_type)
402 // Any reference type is convertible to Object. This is a special
403 // case so we don't need to unnecessarily resolve a class.
404 if (key == reference_type
405 && data.klass == &java::lang::Object::class$)
408 // An initialized type and an uninitialized type are not
410 if (isinitialized () != k.isinitialized ())
413 // Two uninitialized objects are compatible if either:
414 // * The PCs are identical, or
415 // * One PC is UNINIT.
416 if (! isinitialized ())
418 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
422 // Two unresolved types are equal if their names are the same.
425 && _Jv_equalUtf8Consts (data.name, k.data.name))
428 // We must resolve both types and check assignability.
431 return is_assignable_from_slow (data.klass, k.data.klass);
436 return key == void_type;
441 return key == long_type || key == double_type;
444 // Return number of stack or local variable slots taken by this
448 return iswide () ? 2 : 1;
451 bool isarray () const
453 // We treat null_type as not an array. This is ok based on the
454 // current uses of this method.
455 if (key == reference_type)
456 return data.klass->isArray ();
457 else if (key == unresolved_reference_type)
458 return data.name->data[0] == '[';
465 if (key != reference_type)
467 return data.klass->isInterface ();
473 if (key != reference_type)
475 using namespace java::lang::reflect;
476 return Modifier::isAbstract (data.klass->getModifiers ());
479 // Return the element type of an array.
482 // FIXME: maybe should do string manipulation here.
484 if (key != reference_type)
485 verify_fail ("programmer error in type::element_type()");
487 jclass k = data.klass->getComponentType ();
488 if (k->isPrimitive ())
489 return type (get_type_val_for_signature (k));
493 // Return the array type corresponding to an initialized
494 // reference. We could expand this to work for other kinds of
495 // types, but currently we don't need to.
498 // Resolving isn't ideal, because it might force us to load
499 // another class, but it's easy. FIXME?
500 if (key == unresolved_reference_type)
503 if (key == reference_type)
504 return type (_Jv_GetArrayClass (data.klass,
505 data.klass->getClassLoader ()));
507 verify_fail ("internal error in type::to_array()");
510 bool isreference () const
512 return key >= reference_type;
520 bool isinitialized () const
522 return (key == reference_type
524 || key == unresolved_reference_type);
527 bool isresolved () const
529 return (key == reference_type
531 || key == uninitialized_reference_type);
534 void verify_dimensions (int ndims)
536 // The way this is written, we don't need to check isarray().
537 if (key == reference_type)
539 jclass k = data.klass;
540 while (k->isArray () && ndims > 0)
542 k = k->getComponentType ();
548 // We know KEY == unresolved_reference_type.
549 char *p = data.name->data;
550 while (*p++ == '[' && ndims-- > 0)
555 verify_fail ("array type has fewer dimensions than required");
558 // Merge OLD_TYPE into this. On error throw exception.
559 bool merge (type& old_type, bool local_semantics = false)
561 bool changed = false;
562 bool refo = old_type.isreference ();
563 bool refn = isreference ();
566 if (old_type.key == null_type)
568 else if (key == null_type)
573 else if (isinitialized () != old_type.isinitialized ())
574 verify_fail ("merging initialized and uninitialized types");
577 if (! isinitialized ())
581 else if (old_type.pc == UNINIT)
583 else if (pc != old_type.pc)
584 verify_fail ("merging different uninitialized types");
588 && ! old_type.isresolved ()
589 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
591 // Types are identical.
598 jclass k = data.klass;
599 jclass oldk = old_type.data.klass;
602 while (k->isArray () && oldk->isArray ())
605 k = k->getComponentType ();
606 oldk = oldk->getComponentType ();
609 // This loop will end when we hit Object.
612 if (is_assignable_from_slow (k, oldk))
614 k = k->getSuperclass ();
620 while (arraycount > 0)
622 // FIXME: Class loader.
623 k = _Jv_GetArrayClass (k, NULL);
631 else if (refo || refn || key != old_type.key)
635 key = unsuitable_type;
639 verify_fail ("unmergeable type");
645 // This class holds all the state information we need for a given
649 // Current top of stack.
651 // Current stack depth. This is like the top of stack but it
652 // includes wide variable information.
656 // The local variables.
658 // This is used in subroutines to keep track of which local
659 // variables have been accessed.
661 // If not 0, then we are in a subroutine. The value is the PC of
662 // the subroutine's entry point. We can use 0 as an exceptional
663 // value because PC=0 can never be a subroutine.
665 // This is used to keep a linked list of all the states which
666 // require re-verification. We use the PC to keep track.
669 // INVALID marks a state which is not on the linked list of states
670 // requiring reverification.
671 static const int INVALID = -1;
672 // NO_NEXT marks the state at the end of the reverification list.
673 static const int NO_NEXT = -2;
679 local_changed = NULL;
682 state (int max_stack, int max_locals)
686 stack = new type[max_stack];
687 for (int i = 0; i < max_stack; ++i)
688 stack[i] = unsuitable_type;
689 locals = new type[max_locals];
690 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
691 for (int i = 0; i < max_locals; ++i)
693 locals[i] = unsuitable_type;
694 local_changed[i] = false;
700 state (const state *copy, int max_stack, int max_locals)
702 stack = new type[max_stack];
703 locals = new type[max_locals];
704 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
716 _Jv_Free (local_changed);
719 void *operator new[] (size_t bytes)
721 return _Jv_Malloc (bytes);
724 void operator delete[] (void *mem)
729 void *operator new (size_t bytes)
731 return _Jv_Malloc (bytes);
734 void operator delete (void *mem)
739 void copy (const state *copy, int max_stack, int max_locals)
741 stacktop = copy->stacktop;
742 stackdepth = copy->stackdepth;
743 subroutine = copy->subroutine;
744 for (int i = 0; i < max_stack; ++i)
745 stack[i] = copy->stack[i];
746 for (int i = 0; i < max_locals; ++i)
748 locals[i] = copy->locals[i];
749 local_changed[i] = copy->local_changed[i];
751 // Don't modify `next'.
754 // Modify this state to reflect entry to an exception handler.
755 void set_exception (type t, int max_stack)
760 for (int i = stacktop; i < max_stack; ++i)
761 stack[i] = unsuitable_type;
763 // FIXME: subroutine handling?
766 // Merge STATE into this state. Destructively modifies this state.
767 // Returns true if the new state was in fact changed. Will throw an
768 // exception if the states are not mergeable.
769 bool merge (state *state_old, bool ret_semantics,
772 bool changed = false;
774 // Merge subroutine states. *THIS and *STATE_OLD must be in the
775 // same subroutine. Also, recursive subroutine calls must be
777 if (subroutine == state_old->subroutine)
781 else if (subroutine == 0)
783 subroutine = state_old->subroutine;
787 verify_fail ("subroutines merged");
790 if (state_old->stacktop != stacktop)
791 verify_fail ("stack sizes differ");
792 for (int i = 0; i < state_old->stacktop; ++i)
794 if (stack[i].merge (state_old->stack[i]))
798 // Merge local variables.
799 for (int i = 0; i < max_locals; ++i)
801 if (! ret_semantics || local_changed[i])
803 if (locals[i].merge (state_old->locals[i], true))
810 // If we're in a subroutine, we must compute the union of
811 // all the changed local variables.
812 if (state_old->local_changed[i])
819 // Throw an exception if there is an uninitialized object on the
820 // stack or in a local variable. EXCEPTION_SEMANTICS controls
821 // whether we're using backwards-branch or exception-handing
823 void check_no_uninitialized_objects (int max_locals,
824 bool exception_semantics = false)
826 if (! exception_semantics)
828 for (int i = 0; i < stacktop; ++i)
829 if (stack[i].isreference () && ! stack[i].isinitialized ())
830 verify_fail ("uninitialized object on stack");
833 for (int i = 0; i < max_locals; ++i)
834 if (locals[i].isreference () && ! locals[i].isinitialized ())
835 verify_fail ("uninitialized object in local variable");
838 // Note that a local variable was accessed or modified.
839 void note_variable (int index)
842 local_changed[index] = true;
845 // Mark each `new'd object we know of that was allocated at PC as
847 void set_initialized (int pc, int max_locals)
849 for (int i = 0; i < stacktop; ++i)
850 stack[i].set_initialized (pc);
851 for (int i = 0; i < max_locals; ++i)
852 locals[i].set_initialized (pc);
858 if (current_state->stacktop <= 0)
859 verify_fail ("stack empty", start_PC);
860 type r = current_state->stack[--current_state->stacktop];
861 current_state->stackdepth -= r.depth ();
862 if (current_state->stackdepth < 0)
863 verify_fail ("stack empty", start_PC);
871 verify_fail ("narrow pop of wide type", start_PC);
879 verify_fail ("wide pop of narrow type", start_PC);
883 type pop_type (type match)
887 if (! match.compatible (t))
888 verify_fail ("incompatible type on stack", start_PC);
892 void push_type (type t)
894 // If T is a numeric type like short, promote it to int.
897 int depth = t.depth ();
898 if (current_state->stackdepth + depth > current_method->max_stack)
899 verify_fail ("stack overflow");
900 current_state->stack[current_state->stacktop++] = t;
901 current_state->stackdepth += depth;
904 void set_variable (int index, type t)
906 // If T is a numeric type like short, promote it to int.
909 int depth = t.depth ();
910 if (index > current_method->max_locals - depth)
911 verify_fail ("invalid local variable");
912 current_state->locals[index] = t;
913 current_state->note_variable (index);
917 current_state->locals[index + 1] = continuation_type;
918 current_state->note_variable (index + 1);
920 if (index > 0 && current_state->locals[index - 1].iswide ())
922 current_state->locals[index - 1] = unsuitable_type;
923 // There's no need to call note_variable here.
927 type get_variable (int index, type t)
929 int depth = t.depth ();
930 if (index > current_method->max_locals - depth)
931 verify_fail ("invalid local variable", start_PC);
932 if (! t.compatible (current_state->locals[index]))
933 verify_fail ("incompatible type in local variable", start_PC);
936 type t (continuation_type);
937 if (! current_state->locals[index + 1].compatible (t))
938 verify_fail ("invalid local variable", start_PC);
940 current_state->note_variable (index);
941 return current_state->locals[index];
944 // Make sure ARRAY is an array type and that its elements are
945 // compatible with type ELEMENT. Returns the actual element type.
946 type require_array_type (type array, type element)
948 if (! array.isarray ())
949 verify_fail ("array required");
951 type t = array.element_type ();
952 if (! element.compatible (t))
954 // Special case for byte arrays, which must also be boolean
957 if (element.key == byte_type)
959 type e2 (boolean_type);
960 ok = e2.compatible (t);
963 verify_fail ("incompatible array element type");
966 // Return T and not ELEMENT, because T might be specialized.
972 if (PC >= current_method->code_length)
973 verify_fail ("premature end of bytecode");
974 return (jint) bytecode[PC++] & 0xff;
979 jint b1 = get_byte ();
980 jint b2 = get_byte ();
981 return (jint) ((b1 << 8) | b2) & 0xffff;
986 jint b1 = get_byte ();
987 jint b2 = get_byte ();
988 jshort s = (b1 << 8) | b2;
994 jint b1 = get_byte ();
995 jint b2 = get_byte ();
996 jint b3 = get_byte ();
997 jint b4 = get_byte ();
998 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1001 int compute_jump (int offset)
1003 int npc = start_PC + offset;
1004 if (npc < 0 || npc >= current_method->code_length)
1005 verify_fail ("branch out of range", start_PC);
1009 // Merge the indicated state into a new state and schedule a new PC if
1010 // there is a change. If RET_SEMANTICS is true, then we are merging
1011 // from a `ret' instruction into the instruction after a `jsr'. This
1012 // is a special case with its own modified semantics.
1013 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1015 bool changed = true;
1016 if (states[npc] == NULL)
1018 // FIXME: what if we reach this code from a `ret'?
1020 states[npc] = new state (nstate, current_method->max_stack,
1021 current_method->max_locals);
1024 changed = nstate->merge (states[npc], ret_semantics,
1025 current_method->max_stack);
1027 if (changed && states[npc]->next == state::INVALID)
1029 // The merge changed the state, and the new PC isn't yet on our
1030 // list of PCs to re-verify.
1031 states[npc]->next = next_verify_pc;
1032 next_verify_pc = npc;
1036 void push_jump (int offset)
1038 int npc = compute_jump (offset);
1040 current_state->check_no_uninitialized_objects (current_method->max_locals);
1041 push_jump_merge (npc, current_state);
1044 void push_exception_jump (type t, int pc)
1046 current_state->check_no_uninitialized_objects (current_method->max_locals,
1048 state s (current_state, current_method->max_stack,
1049 current_method->max_locals);
1050 s.set_exception (t, current_method->max_stack);
1051 push_jump_merge (pc, &s);
1056 int npc = next_verify_pc;
1057 if (npc != state::NO_NEXT)
1059 next_verify_pc = states[npc]->next;
1060 states[npc]->next = state::INVALID;
1065 void invalidate_pc ()
1067 PC = state::NO_NEXT;
1070 void note_branch_target (int pc, bool is_jsr_target = false)
1072 if (pc <= PC && ! (flags[pc] & FLAG_INSN_START))
1073 verify_fail ("branch not to instruction start");
1074 flags[pc] |= FLAG_BRANCH_TARGET;
1077 // Record the jsr which called this instruction.
1078 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1080 info->next = jsr_ptrs[pc];
1081 jsr_ptrs[pc] = info;
1082 flags[pc] |= FLAG_JSR_TARGET;
1086 void skip_padding ()
1088 while ((PC % 4) > 0)
1089 if (get_byte () != 0)
1090 verify_fail ("found nonzero padding byte");
1093 // Return the subroutine to which the instruction at PC belongs.
1094 int get_subroutine (int pc)
1096 if (states[pc] == NULL)
1098 return states[pc]->subroutine;
1101 // Do the work for a `ret' instruction. INDEX is the index into the
1103 void handle_ret_insn (int index)
1105 get_variable (index, return_address_type);
1107 int csub = current_state->subroutine;
1109 verify_fail ("no subroutine");
1111 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1113 // Temporarily modify the current state so it looks like we're
1114 // in the enclosing context.
1115 current_state->subroutine = get_subroutine (subr->pc);
1117 current_state->check_no_uninitialized_objects (current_method->max_locals);
1118 push_jump_merge (subr->pc, current_state, true);
1121 current_state->subroutine = csub;
1125 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1126 // sure this subroutine isn't already on the stack.
1127 void check_nonrecursive_call (int sub, int dest)
1132 verify_fail ("recursive subroutine call");
1133 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1134 check_nonrecursive_call (get_subroutine (info->pc), dest);
1137 void handle_jsr_insn (int offset)
1139 int npc = compute_jump (offset);
1142 current_state->check_no_uninitialized_objects (current_method->max_locals);
1143 check_nonrecursive_call (current_state->subroutine, npc);
1145 // Temporarily modify the current state so that it looks like we are
1146 // in the subroutine.
1147 push_type (return_address_type);
1148 int save = current_state->subroutine;
1149 current_state->subroutine = npc;
1151 // Merge into the subroutine.
1152 push_jump_merge (npc, current_state);
1154 // Undo our modifications.
1155 current_state->subroutine = save;
1156 pop_type (return_address_type);
1159 jclass construct_primitive_array_type (type_val prim)
1165 k = JvPrimClass (boolean);
1168 k = JvPrimClass (char);
1171 k = JvPrimClass (float);
1174 k = JvPrimClass (double);
1177 k = JvPrimClass (byte);
1180 k = JvPrimClass (short);
1183 k = JvPrimClass (int);
1186 k = JvPrimClass (long);
1189 verify_fail ("unknown type in construct_primitive_array_type");
1191 k = _Jv_GetArrayClass (k, NULL);
1195 // This pass computes the location of branch targets and also
1196 // instruction starts.
1197 void branch_prepass ()
1199 flags = (char *) _Jv_Malloc (current_method->code_length);
1200 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1201 * current_method->code_length);
1203 for (int i = 0; i < current_method->code_length; ++i)
1209 bool last_was_jsr = false;
1212 while (PC < current_method->code_length)
1214 flags[PC] |= FLAG_INSN_START;
1216 // If the previous instruction was a jsr, then the next
1217 // instruction is a branch target -- the branch being the
1218 // corresponding `ret'.
1220 note_branch_target (PC);
1221 last_was_jsr = false;
1224 java_opcode opcode = (java_opcode) bytecode[PC++];
1228 case op_aconst_null:
1364 case op_monitorenter:
1365 case op_monitorexit:
1373 case op_arraylength:
1405 case op_invokespecial:
1406 case op_invokestatic:
1407 case op_invokevirtual:
1411 case op_multianewarray:
1417 last_was_jsr = true;
1436 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1439 case op_tableswitch:
1442 note_branch_target (compute_jump (get_int ()));
1443 jint low = get_int ();
1444 jint hi = get_int ();
1446 verify_fail ("invalid tableswitch", start_PC);
1447 for (int i = low; i <= hi; ++i)
1448 note_branch_target (compute_jump (get_int ()));
1452 case op_lookupswitch:
1455 note_branch_target (compute_jump (get_int ()));
1456 int npairs = get_int ();
1458 verify_fail ("too few pairs in lookupswitch", start_PC);
1459 while (npairs-- > 0)
1462 note_branch_target (compute_jump (get_int ()));
1467 case op_invokeinterface:
1475 opcode = (java_opcode) get_byte ();
1477 if (opcode == op_iinc)
1483 last_was_jsr = true;
1486 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1490 verify_fail ("unrecognized instruction in branch_prepass",
1494 // See if any previous branch tried to branch to the middle of
1495 // this instruction.
1496 for (int pc = start_PC + 1; pc < PC; ++pc)
1498 if ((flags[pc] & FLAG_BRANCH_TARGET))
1499 verify_fail ("branch to middle of instruction", pc);
1503 // Verify exception handlers.
1504 for (int i = 0; i < current_method->exc_count; ++i)
1506 if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
1507 verify_fail ("exception handler not at instruction start",
1508 exception[i].handler_pc);
1509 if (exception[i].start_pc > exception[i].end_pc)
1510 verify_fail ("exception range inverted");
1511 if (! (flags[exception[i].start_pc] & FLAG_INSN_START))
1512 verify_fail ("exception start not at instruction start",
1513 exception[i].start_pc);
1514 else if (! (flags[exception[i].end_pc] & FLAG_INSN_START))
1515 verify_fail ("exception end not at instruction start",
1516 exception[i].end_pc);
1518 flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
1522 void check_pool_index (int index)
1524 if (index < 0 || index >= current_class->constants.size)
1525 verify_fail ("constant pool index out of range", start_PC);
1528 type check_class_constant (int index)
1530 check_pool_index (index);
1531 _Jv_Constants *pool = ¤t_class->constants;
1532 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1533 return type (pool->data[index].clazz);
1534 else if (pool->tags[index] == JV_CONSTANT_Class)
1535 return type (pool->data[index].utf8);
1536 verify_fail ("expected class constant", start_PC);
1539 type check_constant (int index)
1541 check_pool_index (index);
1542 _Jv_Constants *pool = ¤t_class->constants;
1543 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1544 || pool->tags[index] == JV_CONSTANT_String)
1545 return type (&java::lang::String::class$);
1546 else if (pool->tags[index] == JV_CONSTANT_Integer)
1547 return type (int_type);
1548 else if (pool->tags[index] == JV_CONSTANT_Float)
1549 return type (float_type);
1550 verify_fail ("String, int, or float constant expected", start_PC);
1553 type check_wide_constant (int index)
1555 check_pool_index (index);
1556 _Jv_Constants *pool = ¤t_class->constants;
1557 if (pool->tags[index] == JV_CONSTANT_Long)
1558 return type (long_type);
1559 else if (pool->tags[index] == JV_CONSTANT_Double)
1560 return type (double_type);
1561 verify_fail ("long or double constant expected", start_PC);
1564 // Helper for both field and method. These are laid out the same in
1565 // the constant pool.
1566 type handle_field_or_method (int index, int expected,
1567 _Jv_Utf8Const **name,
1568 _Jv_Utf8Const **fmtype)
1570 check_pool_index (index);
1571 _Jv_Constants *pool = ¤t_class->constants;
1572 if (pool->tags[index] != expected)
1573 verify_fail ("didn't see expected constant", start_PC);
1574 // Once we know we have a Fieldref or Methodref we assume that it
1575 // is correctly laid out in the constant pool. I think the code
1576 // in defineclass.cc guarantees this.
1577 _Jv_ushort class_index, name_and_type_index;
1578 _Jv_loadIndexes (&pool->data[index],
1580 name_and_type_index);
1581 _Jv_ushort name_index, desc_index;
1582 _Jv_loadIndexes (&pool->data[name_and_type_index],
1583 name_index, desc_index);
1585 *name = pool->data[name_index].utf8;
1586 *fmtype = pool->data[desc_index].utf8;
1588 return check_class_constant (class_index);
1591 // Return field's type, compute class' type if requested.
1592 type check_field_constant (int index, type *class_type = NULL)
1594 _Jv_Utf8Const *name, *field_type;
1595 type ct = handle_field_or_method (index,
1596 JV_CONSTANT_Fieldref,
1597 &name, &field_type);
1600 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
1601 return type (field_type);
1602 return get_type_val_for_signature (field_type->data[0]);
1605 type check_method_constant (int index, bool is_interface,
1606 _Jv_Utf8Const **method_name,
1607 _Jv_Utf8Const **method_signature)
1609 return handle_field_or_method (index,
1611 ? JV_CONSTANT_InterfaceMethodref
1612 : JV_CONSTANT_Methodref),
1613 method_name, method_signature);
1616 type get_one_type (char *&p)
1634 // FIXME! This will get collected!
1635 _Jv_Utf8Const *name = _Jv_makeUtf8Const (start, p - start);
1639 // Casting to jchar here is ok since we are looking at an ASCII
1641 type_val rt = get_type_val_for_signature (jchar (v));
1643 if (arraycount == 0)
1645 // Callers of this function eventually push their arguments on
1646 // the stack. So, promote them here.
1647 return type (rt).promote ();
1650 jclass k = construct_primitive_array_type (rt);
1651 while (--arraycount > 0)
1652 k = _Jv_GetArrayClass (k, NULL);
1656 void compute_argument_types (_Jv_Utf8Const *signature,
1659 char *p = signature->data;
1665 types[i++] = get_one_type (p);
1668 type compute_return_type (_Jv_Utf8Const *signature)
1670 char *p = signature->data;
1674 return get_one_type (p);
1677 void check_return_type (type onstack)
1679 type rt = compute_return_type (current_method->self->signature);
1680 if (! rt.compatible (onstack))
1681 verify_fail ("incompatible return type", start_PC);
1684 void verify_instructions_0 ()
1686 current_state = new state (current_method->max_stack,
1687 current_method->max_locals);
1695 using namespace java::lang::reflect;
1696 if (! Modifier::isStatic (current_method->self->accflags))
1698 type kurr (current_class);
1699 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
1700 kurr.set_uninitialized (type::SELF);
1701 set_variable (0, kurr);
1705 // We have to handle wide arguments specially here.
1706 int arg_count = _Jv_count_arguments (current_method->self->signature);
1707 type arg_types[arg_count];
1708 compute_argument_types (current_method->self->signature, arg_types);
1709 for (int i = 0; i < arg_count; ++i)
1711 set_variable (var, arg_types[i]);
1713 if (arg_types[i].iswide ())
1718 states = (state **) _Jv_Malloc (sizeof (state *)
1719 * current_method->code_length);
1720 for (int i = 0; i < current_method->code_length; ++i)
1723 next_verify_pc = state::NO_NEXT;
1727 // If the PC was invalidated, get a new one from the work list.
1728 if (PC == state::NO_NEXT)
1731 if (PC == state::INVALID)
1732 verify_fail ("saw state::INVALID", start_PC);
1733 if (PC == state::NO_NEXT)
1735 // Set up the current state.
1736 *current_state = *states[PC];
1739 // Control can't fall off the end of the bytecode.
1740 if (PC >= current_method->code_length)
1741 verify_fail ("fell off end");
1743 if (states[PC] != NULL)
1745 // We've already visited this instruction. So merge the
1746 // states together. If this yields no change then we don't
1747 // have to re-verify.
1748 if (! current_state->merge (states[PC], false,
1749 current_method->max_stack))
1754 // Save a copy of it for later.
1755 states[PC]->copy (current_state, current_method->max_stack,
1756 current_method->max_locals);
1758 else if ((flags[PC] & FLAG_BRANCH_TARGET))
1760 // We only have to keep saved state at branch targets.
1761 states[PC] = new state (current_state, current_method->max_stack,
1762 current_method->max_locals);
1765 // Update states for all active exception handlers. Ordinarily
1766 // there are not many exception handlers. So we simply run
1767 // through them all.
1768 for (int i = 0; i < current_method->exc_count; ++i)
1770 if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
1772 type handler = reference_type;
1773 if (exception[i].handler_type != 0)
1774 handler = check_class_constant (exception[i].handler_type);
1775 push_exception_jump (handler, exception[i].handler_pc);
1780 java_opcode opcode = (java_opcode) bytecode[PC++];
1786 case op_aconst_null:
1787 push_type (null_type);
1797 push_type (int_type);
1802 push_type (long_type);
1808 push_type (float_type);
1813 push_type (double_type);
1818 push_type (int_type);
1823 push_type (int_type);
1827 push_type (check_constant (get_byte ()));
1830 push_type (check_constant (get_ushort ()));
1833 push_type (check_wide_constant (get_ushort ()));
1837 push_type (get_variable (get_byte (), int_type));
1840 push_type (get_variable (get_byte (), long_type));
1843 push_type (get_variable (get_byte (), float_type));
1846 push_type (get_variable (get_byte (), double_type));
1849 push_type (get_variable (get_byte (), reference_type));
1856 push_type (get_variable (opcode - op_iload_0, int_type));
1862 push_type (get_variable (opcode - op_lload_0, long_type));
1868 push_type (get_variable (opcode - op_fload_0, float_type));
1874 push_type (get_variable (opcode - op_dload_0, double_type));
1880 push_type (get_variable (opcode - op_aload_0, 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 push_type (require_array_type (pop_type (reference_type),
1903 pop_type (int_type);
1904 push_type (require_array_type (pop_type (reference_type),
1908 pop_type (int_type);
1909 require_array_type (pop_type (reference_type), byte_type);
1910 push_type (int_type);
1913 pop_type (int_type);
1914 require_array_type (pop_type (reference_type), char_type);
1915 push_type (int_type);
1918 pop_type (int_type);
1919 require_array_type (pop_type (reference_type), short_type);
1920 push_type (int_type);
1923 set_variable (get_byte (), pop_type (int_type));
1926 set_variable (get_byte (), pop_type (long_type));
1929 set_variable (get_byte (), pop_type (float_type));
1932 set_variable (get_byte (), pop_type (double_type));
1935 set_variable (get_byte (), pop_type (reference_type));
1941 set_variable (opcode - op_istore_0, pop_type (int_type));
1947 set_variable (opcode - op_lstore_0, pop_type (long_type));
1953 set_variable (opcode - op_fstore_0, pop_type (float_type));
1959 set_variable (opcode - op_dstore_0, pop_type (double_type));
1965 set_variable (opcode - op_astore_0, pop_type (reference_type));
1968 pop_type (int_type);
1969 pop_type (int_type);
1970 require_array_type (pop_type (reference_type), int_type);
1973 pop_type (long_type);
1974 pop_type (int_type);
1975 require_array_type (pop_type (reference_type), long_type);
1978 pop_type (float_type);
1979 pop_type (int_type);
1980 require_array_type (pop_type (reference_type), float_type);
1983 pop_type (double_type);
1984 pop_type (int_type);
1985 require_array_type (pop_type (reference_type), double_type);
1988 pop_type (reference_type);
1989 pop_type (int_type);
1990 require_array_type (pop_type (reference_type), reference_type);
1993 pop_type (int_type);
1994 pop_type (int_type);
1995 require_array_type (pop_type (reference_type), byte_type);
1998 pop_type (int_type);
1999 pop_type (int_type);
2000 require_array_type (pop_type (reference_type), char_type);
2003 pop_type (int_type);
2004 pop_type (int_type);
2005 require_array_type (pop_type (reference_type), short_type);
2032 type t2 = pop_raw ();
2047 type t = pop_raw ();
2060 type t1 = pop_raw ();
2078 type t1 = pop_raw ();
2081 type t2 = pop_raw ();
2099 type t3 = pop_raw ();
2137 pop_type (int_type);
2138 push_type (pop_type (int_type));
2148 pop_type (long_type);
2149 push_type (pop_type (long_type));
2154 pop_type (int_type);
2155 push_type (pop_type (long_type));
2162 pop_type (float_type);
2163 push_type (pop_type (float_type));
2170 pop_type (double_type);
2171 push_type (pop_type (double_type));
2177 push_type (pop_type (int_type));
2180 push_type (pop_type (long_type));
2183 push_type (pop_type (float_type));
2186 push_type (pop_type (double_type));
2189 get_variable (get_byte (), int_type);
2193 pop_type (int_type);
2194 push_type (long_type);
2197 pop_type (int_type);
2198 push_type (float_type);
2201 pop_type (int_type);
2202 push_type (double_type);
2205 pop_type (long_type);
2206 push_type (int_type);
2209 pop_type (long_type);
2210 push_type (float_type);
2213 pop_type (long_type);
2214 push_type (double_type);
2217 pop_type (float_type);
2218 push_type (int_type);
2221 pop_type (float_type);
2222 push_type (long_type);
2225 pop_type (float_type);
2226 push_type (double_type);
2229 pop_type (double_type);
2230 push_type (int_type);
2233 pop_type (double_type);
2234 push_type (long_type);
2237 pop_type (double_type);
2238 push_type (float_type);
2241 pop_type (long_type);
2242 pop_type (long_type);
2243 push_type (int_type);
2247 pop_type (float_type);
2248 pop_type (float_type);
2249 push_type (int_type);
2253 pop_type (double_type);
2254 pop_type (double_type);
2255 push_type (int_type);
2263 pop_type (int_type);
2264 push_jump (get_short ());
2272 pop_type (int_type);
2273 pop_type (int_type);
2274 push_jump (get_short ());
2278 pop_type (reference_type);
2279 pop_type (reference_type);
2280 push_jump (get_short ());
2283 push_jump (get_short ());
2287 handle_jsr_insn (get_short ());
2290 handle_ret_insn (get_byte ());
2292 case op_tableswitch:
2294 pop_type (int_type);
2296 push_jump (get_int ());
2297 jint low = get_int ();
2298 jint high = get_int ();
2299 // Already checked LOW -vs- HIGH.
2300 for (int i = low; i <= high; ++i)
2301 push_jump (get_int ());
2306 case op_lookupswitch:
2308 pop_type (int_type);
2310 push_jump (get_int ());
2311 jint npairs = get_int ();
2312 // Already checked NPAIRS >= 0.
2314 for (int i = 0; i < npairs; ++i)
2316 jint key = get_int ();
2317 if (i > 0 && key <= lastkey)
2318 verify_fail ("lookupswitch pairs unsorted", start_PC);
2320 push_jump (get_int ());
2326 check_return_type (pop_type (int_type));
2330 check_return_type (pop_type (long_type));
2334 check_return_type (pop_type (float_type));
2338 check_return_type (pop_type (double_type));
2342 check_return_type (pop_type (reference_type));
2346 check_return_type (void_type);
2350 push_type (check_field_constant (get_ushort ()));
2353 pop_type (check_field_constant (get_ushort ()));
2358 type field = check_field_constant (get_ushort (), &klass);
2366 type field = check_field_constant (get_ushort (), &klass);
2372 case op_invokevirtual:
2373 case op_invokespecial:
2374 case op_invokestatic:
2375 case op_invokeinterface:
2377 _Jv_Utf8Const *method_name, *method_signature;
2379 = check_method_constant (get_ushort (),
2380 opcode == op_invokeinterface,
2383 int arg_count = _Jv_count_arguments (method_signature);
2384 if (opcode == op_invokeinterface)
2386 int nargs = get_byte ();
2388 verify_fail ("too few arguments to invokeinterface",
2390 if (get_byte () != 0)
2391 verify_fail ("invokeinterface dummy byte is wrong",
2393 if (nargs - 1 != arg_count)
2394 verify_fail ("wrong argument count for invokeinterface",
2398 bool is_init = false;
2399 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2402 if (opcode != op_invokespecial)
2403 verify_fail ("can't invoke <init>", start_PC);
2405 else if (method_name->data[0] == '<')
2406 verify_fail ("can't invoke method starting with `<'",
2409 // Pop arguments and check types.
2410 type arg_types[arg_count];
2411 compute_argument_types (method_signature, arg_types);
2412 for (int i = arg_count - 1; i >= 0; --i)
2413 pop_type (arg_types[i]);
2415 if (opcode != op_invokestatic)
2417 type t = class_type;
2420 // In this case the PC doesn't matter.
2421 t.set_uninitialized (type::UNINIT);
2425 current_state->set_initialized (t.get_pc (),
2426 current_method->max_locals);
2429 type rt = compute_return_type (method_signature);
2437 type t = check_class_constant (get_ushort ());
2438 if (t.isarray () || t.isinterface () || t.isabstract ())
2439 verify_fail ("type is array, interface, or abstract",
2441 t.set_uninitialized (start_PC);
2448 int atype = get_byte ();
2449 // We intentionally have chosen constants to make this
2451 if (atype < boolean_type || atype > long_type)
2452 verify_fail ("type not primitive", start_PC);
2453 pop_type (int_type);
2454 push_type (construct_primitive_array_type (type_val (atype)));
2458 pop_type (int_type);
2459 push_type (check_class_constant (get_ushort ()).to_array ());
2461 case op_arraylength:
2463 type t = pop_type (reference_type);
2465 verify_fail ("array type expected", start_PC);
2466 push_type (int_type);
2470 pop_type (type (&java::lang::Throwable::class$));
2474 pop_type (reference_type);
2475 push_type (check_class_constant (get_ushort ()));
2478 pop_type (reference_type);
2479 check_class_constant (get_ushort ());
2480 push_type (int_type);
2482 case op_monitorenter:
2483 pop_type (reference_type);
2485 case op_monitorexit:
2486 pop_type (reference_type);
2490 switch (get_byte ())
2493 push_type (get_variable (get_ushort (), int_type));
2496 push_type (get_variable (get_ushort (), long_type));
2499 push_type (get_variable (get_ushort (), float_type));
2502 push_type (get_variable (get_ushort (), double_type));
2505 push_type (get_variable (get_ushort (), reference_type));
2508 set_variable (get_ushort (), pop_type (int_type));
2511 set_variable (get_ushort (), pop_type (long_type));
2514 set_variable (get_ushort (), pop_type (float_type));
2517 set_variable (get_ushort (), pop_type (double_type));
2520 set_variable (get_ushort (), pop_type (reference_type));
2523 handle_ret_insn (get_short ());
2526 get_variable (get_ushort (), int_type);
2530 verify_fail ("unrecognized wide instruction", start_PC);
2534 case op_multianewarray:
2536 type atype = check_class_constant (get_ushort ());
2537 int dim = get_byte ();
2539 verify_fail ("too few dimensions to multianewarray", start_PC);
2540 atype.verify_dimensions (dim);
2541 for (int i = 0; i < dim; ++i)
2542 pop_type (int_type);
2548 pop_type (reference_type);
2549 push_jump (get_short ());
2552 push_jump (get_int ());
2556 handle_jsr_insn (get_int ());
2560 // Unrecognized opcode.
2561 verify_fail ("unrecognized instruction in verify_instructions_0",
2569 void verify_instructions ()
2572 verify_instructions_0 ();
2575 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2578 bytecode = m->bytecode ();
2579 exception = m->exceptions ();
2580 current_class = m->defining_class;
2587 ~_Jv_BytecodeVerifier ()
2594 _Jv_Free (jsr_ptrs);
2599 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
2601 _Jv_BytecodeVerifier v (meth);
2602 v.verify_instructions ();
2605 // FIXME: add more info, like PC, when required.
2607 verify_fail (char *s, jint pc)
2609 using namespace java::lang;
2610 StringBuffer *buf = new StringBuffer ();
2612 buf->append (JvNewStringLatin1 ("verification failed"));
2615 buf->append (JvNewStringLatin1 (" at PC "));
2618 buf->append (JvNewStringLatin1 (": "));
2619 buf->append (JvNewStringLatin1 (s));
2620 throw new java::lang::VerifyError (buf->toString ());
2623 #endif /* INTERPRETER */