tree.h: Add missing checks on some macros; make formatting more consistent.
[official-gcc.git] / libjava / verify.cc
blob3b042e9963f1e64c4ef7068f3afe4fc13d353f28
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
9 details. */
11 // Writte by Tom Tromey <tromey@redhat.com>
13 #include <config.h>
15 #include <jvm.h>
16 #include <gcj/cni.h>
17 #include <java-insns.h>
18 #include <java-interp.h>
20 #ifdef INTERPRETER
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>
29 // TO DO
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
38 // methods.
39 static void verify_fail (char *msg, jint pc = -1)
40 __attribute__ ((__noreturn__));
42 class _Jv_BytecodeVerifier
44 private:
46 static const int FLAG_INSN_START = 1;
47 static const int FLAG_BRANCH_TARGET = 2;
48 static const int FLAG_JSR_TARGET = 4;
50 struct state;
51 struct type;
52 struct subr_info;
54 // The current PC.
55 int PC;
56 // The PC corresponding to the start of the current instruction.
57 int start_PC;
59 // The current state of the stack, locals, etc.
60 state *current_state;
62 // We store the state at branch targets, for merging. This holds
63 // such states.
64 state **states;
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
68 // list.
69 int next_verify_pc;
71 // We keep some flags for each instruction. The values are the
72 // FLAG_* constants defined above.
73 char *flags;
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.
78 subr_info **jsr_ptrs;
80 // The current top of the stack, in terms of slots.
81 int stacktop;
82 // The current depth of the stack. This will be larger than
83 // STACKTOP when wide types are on the stack.
84 int stackdepth;
86 // The bytecode itself.
87 unsigned char *bytecode;
88 // The exceptions.
89 _Jv_InterpException *exception;
91 // Defining class.
92 jclass current_class;
93 // This method.
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
98 // type class.
99 enum type_val
101 void_type,
103 // The values for primitive types are chosen to correspond to values
104 // specified to newarray.
105 boolean_type = 4,
106 char_type = 5,
107 float_type = 6,
108 double_type = 7,
109 byte_type = 8,
110 short_type = 9,
111 int_type = 10,
112 long_type = 11,
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.
117 unsuitable_type,
118 return_address_type,
119 continuation_type,
121 // Everything after `reference_type' must be a reference type.
122 reference_type,
123 null_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)
133 type_val rt;
134 switch (sig)
136 case 'Z':
137 rt = boolean_type;
138 break;
139 case 'B':
140 rt = byte_type;
141 break;
142 case 'C':
143 rt = char_type;
144 break;
145 case 'S':
146 rt = short_type;
147 break;
148 case 'I':
149 rt = int_type;
150 break;
151 case 'J':
152 rt = long_type;
153 break;
154 case 'F':
155 rt = float_type;
156 break;
157 case 'D':
158 rt = double_type;
159 break;
160 case 'V':
161 rt = void_type;
162 break;
163 default:
164 verify_fail ("invalid signature");
166 return rt;
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.
180 while (true)
182 if (source == target)
183 return true;
185 if (target->isPrimitive () || source->isPrimitive ())
186 return false;
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 ())
197 return false;
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]))
208 return true;
210 return false;
212 else if (target == &java::lang::Object::class$)
213 return true;
214 else if (source->isInterface ()
215 || source == &java::lang::Object::class$)
216 return false;
217 else
218 source = source->getSuperclass ();
222 // This is used to keep track of which `jsr's correspond to a given
223 // jsr target.
224 struct subr_info
226 // PC of the instruction just after the jsr.
227 int pc;
228 // Link.
229 subr_info *next;
232 // The `type' class is used to represent a single type in the
233 // verifier.
234 struct type
236 // The type.
237 type_val key;
238 // Some associated data.
239 union
241 // For a resolved reference type, this is a pointer to the class.
242 jclass klass;
243 // For other reference types, this it the name of the class.
244 _Jv_Utf8Const *name;
245 } data;
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
250 // <init> method.
251 int pc;
253 static const int UNINIT = -2;
254 static const int SELF = -1;
256 // Basic constructor.
257 type ()
259 key = unsuitable_type;
260 data.klass = NULL;
261 pc = UNINIT;
264 // Make a new instance given the type tag. We assume a generic
265 // `reference_type' means Object.
266 type (type_val k)
268 key = k;
269 data.klass = NULL;
270 if (key == reference_type)
271 data.klass = &java::lang::Object::class$;
272 pc = UNINIT;
275 // Make a new instance given a class.
276 type (jclass klass)
278 key = reference_type;
279 data.klass = klass;
280 pc = UNINIT;
283 // Make a new instance given the name of a class.
284 type (_Jv_Utf8Const *n)
286 key = unresolved_reference_type;
287 data.name = n;
288 pc = UNINIT;
291 // Copy constructor.
292 type (const type &t)
294 key = t.key;
295 data = t.data;
296 pc = t.pc;
299 // These operators are required because libgcj can't link in
300 // -lstdc++.
301 void *operator new[] (size_t bytes)
303 return _Jv_Malloc (bytes);
306 void operator delete[] (void *mem)
308 _Jv_Free (mem);
311 type& operator= (type_val k)
313 key = k;
314 data.klass = NULL;
315 pc = UNINIT;
316 return *this;
319 type& operator= (const type& t)
321 key = t.key;
322 data = t.data;
323 pc = t.pc;
324 return *this;
327 // Promote a numeric type.
328 type &promote ()
330 if (key == boolean_type || key == char_type
331 || key == byte_type || key == short_type)
332 key = int_type;
333 return *this;
336 // If *THIS is an unresolved reference type, resolve it.
337 void resolve ()
339 if (key != unresolved_reference_type
340 && key != uninitialized_unresolved_reference_type)
341 return;
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);
349 else
350 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
351 false, NULL);
352 key = (key == unresolved_reference_type
353 ? 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;
364 else
365 verify_fail ("internal error in type::uninitialized");
366 pc = npc;
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
377 ? reference_type
378 : unresolved_reference_type);
379 pc = UNINIT;
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
387 // promotion.
388 bool compatible (type &k)
390 // Any type is compatible with the unsuitable type.
391 if (key == unsuitable_type)
392 return true;
394 if (key < reference_type || k.key < reference_type)
395 return key == k.key;
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)
400 return true;
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$)
406 return true;
408 // An initialized type and an uninitialized type are not
409 // compatible.
410 if (isinitialized () != k.isinitialized ())
411 return false;
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)
419 return false;
422 // Two unresolved types are equal if their names are the same.
423 if (! isresolved ()
424 && ! k.isresolved ()
425 && _Jv_equalUtf8Consts (data.name, k.data.name))
426 return true;
428 // We must resolve both types and check assignability.
429 resolve ();
430 k.resolve ();
431 return is_assignable_from_slow (data.klass, k.data.klass);
434 bool isvoid () const
436 return key == void_type;
439 bool iswide () const
441 return key == long_type || key == double_type;
444 // Return number of stack or local variable slots taken by this
445 // type.
446 int depth () const
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] == '[';
459 return false;
462 bool isinterface ()
464 resolve ();
465 if (key != reference_type)
466 return false;
467 return data.klass->isInterface ();
470 bool isabstract ()
472 resolve ();
473 if (key != reference_type)
474 return false;
475 using namespace java::lang::reflect;
476 return Modifier::isAbstract (data.klass->getModifiers ());
479 // Return the element type of an array.
480 type element_type ()
482 // FIXME: maybe should do string manipulation here.
483 resolve ();
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));
490 return type (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.
496 type to_array ()
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)
501 resolve ();
503 if (key == reference_type)
504 return type (_Jv_GetArrayClass (data.klass,
505 data.klass->getClassLoader ()));
506 else
507 verify_fail ("internal error in type::to_array()");
510 bool isreference () const
512 return key >= reference_type;
515 int get_pc () const
517 return pc;
520 bool isinitialized () const
522 return (key == reference_type
523 || key == null_type
524 || key == unresolved_reference_type);
527 bool isresolved () const
529 return (key == reference_type
530 || key == null_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 ();
543 --ndims;
546 else
548 // We know KEY == unresolved_reference_type.
549 char *p = data.name->data;
550 while (*p++ == '[' && ndims-- > 0)
554 if (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 ();
564 if (refo && refn)
566 if (old_type.key == null_type)
568 else if (key == null_type)
570 *this = old_type;
571 changed = true;
573 else if (isinitialized () != old_type.isinitialized ())
574 verify_fail ("merging initialized and uninitialized types");
575 else
577 if (! isinitialized ())
579 if (pc == UNINIT)
580 pc = old_type.pc;
581 else if (old_type.pc == UNINIT)
583 else if (pc != old_type.pc)
584 verify_fail ("merging different uninitialized types");
587 if (! isresolved ()
588 && ! old_type.isresolved ()
589 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
591 // Types are identical.
593 else
595 resolve ();
596 old_type.resolve ();
598 jclass k = data.klass;
599 jclass oldk = old_type.data.klass;
601 int arraycount = 0;
602 while (k->isArray () && oldk->isArray ())
604 ++arraycount;
605 k = k->getComponentType ();
606 oldk = oldk->getComponentType ();
609 // This loop will end when we hit Object.
610 while (true)
612 if (is_assignable_from_slow (k, oldk))
613 break;
614 k = k->getSuperclass ();
615 changed = true;
618 if (changed)
620 while (arraycount > 0)
622 // FIXME: Class loader.
623 k = _Jv_GetArrayClass (k, NULL);
624 --arraycount;
626 data.klass = k;
631 else if (refo || refn || key != old_type.key)
633 if (local_semantics)
635 key = unsuitable_type;
636 changed = true;
638 else
639 verify_fail ("unmergeable type");
641 return changed;
645 // This class holds all the state information we need for a given
646 // location.
647 struct state
649 // Current top of stack.
650 int stacktop;
651 // Current stack depth. This is like the top of stack but it
652 // includes wide variable information.
653 int stackdepth;
654 // The stack.
655 type *stack;
656 // The local variables.
657 type *locals;
658 // This is used in subroutines to keep track of which local
659 // variables have been accessed.
660 bool *local_changed;
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.
664 int 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.
667 int next;
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;
675 state ()
677 stack = NULL;
678 locals = NULL;
679 local_changed = NULL;
682 state (int max_stack, int max_locals)
684 stacktop = 0;
685 stackdepth = 0;
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;
696 next = INVALID;
697 subroutine = 0;
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);
705 *this = *copy;
706 next = INVALID;
709 ~state ()
711 if (stack)
712 delete[] stack;
713 if (locals)
714 delete[] locals;
715 if (local_changed)
716 _Jv_Free (local_changed);
719 void *operator new[] (size_t bytes)
721 return _Jv_Malloc (bytes);
724 void operator delete[] (void *mem)
726 _Jv_Free (mem);
729 void *operator new (size_t bytes)
731 return _Jv_Malloc (bytes);
734 void operator delete (void *mem)
736 _Jv_Free (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)
757 stackdepth = 1;
758 stacktop = 1;
759 stack[0] = t;
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,
770 int max_locals)
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
776 // avoided.
777 if (subroutine == state_old->subroutine)
779 // Nothing.
781 else if (subroutine == 0)
783 subroutine = state_old->subroutine;
784 changed = true;
786 else
787 verify_fail ("subroutines merged");
789 // Merge stacks.
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]))
795 changed = true;
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))
805 changed = true;
806 note_variable (i);
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])
813 note_variable (i);
816 return changed;
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
822 // semantics.
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)
841 if (subroutine > 0)
842 local_changed[index] = true;
845 // Mark each `new'd object we know of that was allocated at PC as
846 // initialized.
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);
856 type pop_raw ()
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);
864 return r;
867 type pop32 ()
869 type r = pop_raw ();
870 if (r.iswide ())
871 verify_fail ("narrow pop of wide type", start_PC);
872 return r;
875 type pop64 ()
877 type r = pop_raw ();
878 if (! r.iswide ())
879 verify_fail ("wide pop of narrow type", start_PC);
880 return r;
883 type pop_type (type match)
885 match.promote ();
886 type t = pop_raw ();
887 if (! match.compatible (t))
888 verify_fail ("incompatible type on stack", start_PC);
889 return t;
892 void push_type (type t)
894 // If T is a numeric type like short, promote it to int.
895 t.promote ();
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.
907 t.promote ();
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);
915 if (depth == 2)
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);
934 if (depth == 2)
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
955 // arrays.
956 bool ok = true;
957 if (element.key == byte_type)
959 type e2 (boolean_type);
960 ok = e2.compatible (t);
962 if (! ok)
963 verify_fail ("incompatible array element type");
966 // Return T and not ELEMENT, because T might be specialized.
967 return t;
970 jint get_byte ()
972 if (PC >= current_method->code_length)
973 verify_fail ("premature end of bytecode");
974 return (jint) bytecode[PC++] & 0xff;
977 jint get_ushort ()
979 jint b1 = get_byte ();
980 jint b2 = get_byte ();
981 return (jint) ((b1 << 8) | b2) & 0xffff;
984 jint get_short ()
986 jint b1 = get_byte ();
987 jint b2 = get_byte ();
988 jshort s = (b1 << 8) | b2;
989 return (jint) s;
992 jint get_int ()
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);
1006 return npc;
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);
1023 else
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);
1039 if (npc < PC)
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,
1047 true);
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);
1054 int pop_jump ()
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;
1062 return npc;
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;
1075 if (is_jsr_target)
1077 // Record the jsr which called this instruction.
1078 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1079 info->pc = PC;
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)
1097 return 0;
1098 return states[pc]->subroutine;
1101 // Do the work for a `ret' instruction. INDEX is the index into the
1102 // local variables.
1103 void handle_ret_insn (int index)
1105 get_variable (index, return_address_type);
1107 int csub = current_state->subroutine;
1108 if (csub == 0)
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);
1116 if (subr->pc < 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;
1122 invalidate_pc ();
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)
1129 if (sub == 0)
1130 return;
1131 if (sub == 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);
1141 if (npc < PC)
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)
1161 jclass k = NULL;
1162 switch (prim)
1164 case boolean_type:
1165 k = JvPrimClass (boolean);
1166 break;
1167 case char_type:
1168 k = JvPrimClass (char);
1169 break;
1170 case float_type:
1171 k = JvPrimClass (float);
1172 break;
1173 case double_type:
1174 k = JvPrimClass (double);
1175 break;
1176 case byte_type:
1177 k = JvPrimClass (byte);
1178 break;
1179 case short_type:
1180 k = JvPrimClass (short);
1181 break;
1182 case int_type:
1183 k = JvPrimClass (int);
1184 break;
1185 case long_type:
1186 k = JvPrimClass (long);
1187 break;
1188 default:
1189 verify_fail ("unknown type in construct_primitive_array_type");
1191 k = _Jv_GetArrayClass (k, NULL);
1192 return k;
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)
1205 flags[i] = 0;
1206 jsr_ptrs[i] = NULL;
1209 bool last_was_jsr = false;
1211 PC = 0;
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'.
1219 if (last_was_jsr)
1220 note_branch_target (PC);
1221 last_was_jsr = false;
1223 start_PC = PC;
1224 java_opcode opcode = (java_opcode) bytecode[PC++];
1225 switch (opcode)
1227 case op_nop:
1228 case op_aconst_null:
1229 case op_iconst_m1:
1230 case op_iconst_0:
1231 case op_iconst_1:
1232 case op_iconst_2:
1233 case op_iconst_3:
1234 case op_iconst_4:
1235 case op_iconst_5:
1236 case op_lconst_0:
1237 case op_lconst_1:
1238 case op_fconst_0:
1239 case op_fconst_1:
1240 case op_fconst_2:
1241 case op_dconst_0:
1242 case op_dconst_1:
1243 case op_iload_0:
1244 case op_iload_1:
1245 case op_iload_2:
1246 case op_iload_3:
1247 case op_lload_0:
1248 case op_lload_1:
1249 case op_lload_2:
1250 case op_lload_3:
1251 case op_fload_0:
1252 case op_fload_1:
1253 case op_fload_2:
1254 case op_fload_3:
1255 case op_dload_0:
1256 case op_dload_1:
1257 case op_dload_2:
1258 case op_dload_3:
1259 case op_aload_0:
1260 case op_aload_1:
1261 case op_aload_2:
1262 case op_aload_3:
1263 case op_iaload:
1264 case op_laload:
1265 case op_faload:
1266 case op_daload:
1267 case op_aaload:
1268 case op_baload:
1269 case op_caload:
1270 case op_saload:
1271 case op_istore_0:
1272 case op_istore_1:
1273 case op_istore_2:
1274 case op_istore_3:
1275 case op_lstore_0:
1276 case op_lstore_1:
1277 case op_lstore_2:
1278 case op_lstore_3:
1279 case op_fstore_0:
1280 case op_fstore_1:
1281 case op_fstore_2:
1282 case op_fstore_3:
1283 case op_dstore_0:
1284 case op_dstore_1:
1285 case op_dstore_2:
1286 case op_dstore_3:
1287 case op_astore_0:
1288 case op_astore_1:
1289 case op_astore_2:
1290 case op_astore_3:
1291 case op_iastore:
1292 case op_lastore:
1293 case op_fastore:
1294 case op_dastore:
1295 case op_aastore:
1296 case op_bastore:
1297 case op_castore:
1298 case op_sastore:
1299 case op_pop:
1300 case op_pop2:
1301 case op_dup:
1302 case op_dup_x1:
1303 case op_dup_x2:
1304 case op_dup2:
1305 case op_dup2_x1:
1306 case op_dup2_x2:
1307 case op_swap:
1308 case op_iadd:
1309 case op_isub:
1310 case op_imul:
1311 case op_idiv:
1312 case op_irem:
1313 case op_ishl:
1314 case op_ishr:
1315 case op_iushr:
1316 case op_iand:
1317 case op_ior:
1318 case op_ixor:
1319 case op_ladd:
1320 case op_lsub:
1321 case op_lmul:
1322 case op_ldiv:
1323 case op_lrem:
1324 case op_lshl:
1325 case op_lshr:
1326 case op_lushr:
1327 case op_land:
1328 case op_lor:
1329 case op_lxor:
1330 case op_fadd:
1331 case op_fsub:
1332 case op_fmul:
1333 case op_fdiv:
1334 case op_frem:
1335 case op_dadd:
1336 case op_dsub:
1337 case op_dmul:
1338 case op_ddiv:
1339 case op_drem:
1340 case op_ineg:
1341 case op_i2b:
1342 case op_i2c:
1343 case op_i2s:
1344 case op_lneg:
1345 case op_fneg:
1346 case op_dneg:
1347 case op_i2l:
1348 case op_i2f:
1349 case op_i2d:
1350 case op_l2i:
1351 case op_l2f:
1352 case op_l2d:
1353 case op_f2i:
1354 case op_f2l:
1355 case op_f2d:
1356 case op_d2i:
1357 case op_d2l:
1358 case op_d2f:
1359 case op_lcmp:
1360 case op_fcmpl:
1361 case op_fcmpg:
1362 case op_dcmpl:
1363 case op_dcmpg:
1364 case op_monitorenter:
1365 case op_monitorexit:
1366 case op_ireturn:
1367 case op_lreturn:
1368 case op_freturn:
1369 case op_dreturn:
1370 case op_areturn:
1371 case op_return:
1372 case op_athrow:
1373 case op_arraylength:
1374 break;
1376 case op_bipush:
1377 case op_ldc:
1378 case op_iload:
1379 case op_lload:
1380 case op_fload:
1381 case op_dload:
1382 case op_aload:
1383 case op_istore:
1384 case op_lstore:
1385 case op_fstore:
1386 case op_dstore:
1387 case op_astore:
1388 case op_ret:
1389 case op_newarray:
1390 get_byte ();
1391 break;
1393 case op_iinc:
1394 case op_sipush:
1395 case op_ldc_w:
1396 case op_ldc2_w:
1397 case op_getstatic:
1398 case op_getfield:
1399 case op_putfield:
1400 case op_putstatic:
1401 case op_new:
1402 case op_anewarray:
1403 case op_instanceof:
1404 case op_checkcast:
1405 case op_invokespecial:
1406 case op_invokestatic:
1407 case op_invokevirtual:
1408 get_short ();
1409 break;
1411 case op_multianewarray:
1412 get_short ();
1413 get_byte ();
1414 break;
1416 case op_jsr:
1417 last_was_jsr = true;
1418 // Fall through.
1419 case op_ifeq:
1420 case op_ifne:
1421 case op_iflt:
1422 case op_ifge:
1423 case op_ifgt:
1424 case op_ifle:
1425 case op_if_icmpeq:
1426 case op_if_icmpne:
1427 case op_if_icmplt:
1428 case op_if_icmpge:
1429 case op_if_icmpgt:
1430 case op_if_icmple:
1431 case op_if_acmpeq:
1432 case op_if_acmpne:
1433 case op_ifnull:
1434 case op_ifnonnull:
1435 case op_goto:
1436 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1437 break;
1439 case op_tableswitch:
1441 skip_padding ();
1442 note_branch_target (compute_jump (get_int ()));
1443 jint low = get_int ();
1444 jint hi = get_int ();
1445 if (low > hi)
1446 verify_fail ("invalid tableswitch", start_PC);
1447 for (int i = low; i <= hi; ++i)
1448 note_branch_target (compute_jump (get_int ()));
1450 break;
1452 case op_lookupswitch:
1454 skip_padding ();
1455 note_branch_target (compute_jump (get_int ()));
1456 int npairs = get_int ();
1457 if (npairs < 0)
1458 verify_fail ("too few pairs in lookupswitch", start_PC);
1459 while (npairs-- > 0)
1461 get_int ();
1462 note_branch_target (compute_jump (get_int ()));
1465 break;
1467 case op_invokeinterface:
1468 get_short ();
1469 get_byte ();
1470 get_byte ();
1471 break;
1473 case op_wide:
1475 opcode = (java_opcode) get_byte ();
1476 get_short ();
1477 if (opcode == op_iinc)
1478 get_short ();
1480 break;
1482 case op_jsr_w:
1483 last_was_jsr = true;
1484 // Fall through.
1485 case op_goto_w:
1486 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1487 break;
1489 default:
1490 verify_fail ("unrecognized instruction in branch_prepass",
1491 start_PC);
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 = &current_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 = &current_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 = &current_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 = &current_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],
1579 class_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);
1598 if (class_type)
1599 *class_type = ct;
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,
1610 (is_interface
1611 ? JV_CONSTANT_InterfaceMethodref
1612 : JV_CONSTANT_Methodref),
1613 method_name, method_signature);
1616 type get_one_type (char *&p)
1618 char *start = p;
1620 int arraycount = 0;
1621 while (*p == '[')
1623 ++arraycount;
1624 ++p;
1627 char v = *p++;
1629 if (v == 'L')
1631 while (*p != ';')
1632 ++p;
1633 ++p;
1634 // FIXME! This will get collected!
1635 _Jv_Utf8Const *name = _Jv_makeUtf8Const (start, p - start);
1636 return type (name);
1639 // Casting to jchar here is ok since we are looking at an ASCII
1640 // character.
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);
1653 return type (k);
1656 void compute_argument_types (_Jv_Utf8Const *signature,
1657 type *types)
1659 char *p = signature->data;
1660 // Skip `('.
1661 ++p;
1663 int i = 0;
1664 while (*p != ')')
1665 types[i++] = get_one_type (p);
1668 type compute_return_type (_Jv_Utf8Const *signature)
1670 char *p = signature->data;
1671 while (*p != ')')
1672 ++p;
1673 ++p;
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);
1689 PC = 0;
1690 start_PC = 0;
1693 int var = 0;
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);
1702 ++var;
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]);
1712 ++var;
1713 if (arg_types[i].iswide ())
1714 ++var;
1718 states = (state **) _Jv_Malloc (sizeof (state *)
1719 * current_method->code_length);
1720 for (int i = 0; i < current_method->code_length; ++i)
1721 states[i] = NULL;
1723 next_verify_pc = state::NO_NEXT;
1725 while (true)
1727 // If the PC was invalidated, get a new one from the work list.
1728 if (PC == state::NO_NEXT)
1730 PC = pop_jump ();
1731 if (PC == state::INVALID)
1732 verify_fail ("saw state::INVALID", start_PC);
1733 if (PC == state::NO_NEXT)
1734 break;
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))
1751 invalidate_pc ();
1752 continue;
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);
1779 start_PC = PC;
1780 java_opcode opcode = (java_opcode) bytecode[PC++];
1781 switch (opcode)
1783 case op_nop:
1784 break;
1786 case op_aconst_null:
1787 push_type (null_type);
1788 break;
1790 case op_iconst_m1:
1791 case op_iconst_0:
1792 case op_iconst_1:
1793 case op_iconst_2:
1794 case op_iconst_3:
1795 case op_iconst_4:
1796 case op_iconst_5:
1797 push_type (int_type);
1798 break;
1800 case op_lconst_0:
1801 case op_lconst_1:
1802 push_type (long_type);
1803 break;
1805 case op_fconst_0:
1806 case op_fconst_1:
1807 case op_fconst_2:
1808 push_type (float_type);
1809 break;
1811 case op_dconst_0:
1812 case op_dconst_1:
1813 push_type (double_type);
1814 break;
1816 case op_bipush:
1817 get_byte ();
1818 push_type (int_type);
1819 break;
1821 case op_sipush:
1822 get_short ();
1823 push_type (int_type);
1824 break;
1826 case op_ldc:
1827 push_type (check_constant (get_byte ()));
1828 break;
1829 case op_ldc_w:
1830 push_type (check_constant (get_ushort ()));
1831 break;
1832 case op_ldc2_w:
1833 push_type (check_wide_constant (get_ushort ()));
1834 break;
1836 case op_iload:
1837 push_type (get_variable (get_byte (), int_type));
1838 break;
1839 case op_lload:
1840 push_type (get_variable (get_byte (), long_type));
1841 break;
1842 case op_fload:
1843 push_type (get_variable (get_byte (), float_type));
1844 break;
1845 case op_dload:
1846 push_type (get_variable (get_byte (), double_type));
1847 break;
1848 case op_aload:
1849 push_type (get_variable (get_byte (), reference_type));
1850 break;
1852 case op_iload_0:
1853 case op_iload_1:
1854 case op_iload_2:
1855 case op_iload_3:
1856 push_type (get_variable (opcode - op_iload_0, int_type));
1857 break;
1858 case op_lload_0:
1859 case op_lload_1:
1860 case op_lload_2:
1861 case op_lload_3:
1862 push_type (get_variable (opcode - op_lload_0, long_type));
1863 break;
1864 case op_fload_0:
1865 case op_fload_1:
1866 case op_fload_2:
1867 case op_fload_3:
1868 push_type (get_variable (opcode - op_fload_0, float_type));
1869 break;
1870 case op_dload_0:
1871 case op_dload_1:
1872 case op_dload_2:
1873 case op_dload_3:
1874 push_type (get_variable (opcode - op_dload_0, double_type));
1875 break;
1876 case op_aload_0:
1877 case op_aload_1:
1878 case op_aload_2:
1879 case op_aload_3:
1880 push_type (get_variable (opcode - op_aload_0, reference_type));
1881 break;
1882 case op_iaload:
1883 pop_type (int_type);
1884 push_type (require_array_type (pop_type (reference_type),
1885 int_type));
1886 break;
1887 case op_laload:
1888 pop_type (int_type);
1889 push_type (require_array_type (pop_type (reference_type),
1890 long_type));
1891 break;
1892 case op_faload:
1893 pop_type (int_type);
1894 push_type (require_array_type (pop_type (reference_type),
1895 float_type));
1896 break;
1897 case op_daload:
1898 pop_type (int_type);
1899 push_type (require_array_type (pop_type (reference_type),
1900 double_type));
1901 break;
1902 case op_aaload:
1903 pop_type (int_type);
1904 push_type (require_array_type (pop_type (reference_type),
1905 reference_type));
1906 break;
1907 case op_baload:
1908 pop_type (int_type);
1909 require_array_type (pop_type (reference_type), byte_type);
1910 push_type (int_type);
1911 break;
1912 case op_caload:
1913 pop_type (int_type);
1914 require_array_type (pop_type (reference_type), char_type);
1915 push_type (int_type);
1916 break;
1917 case op_saload:
1918 pop_type (int_type);
1919 require_array_type (pop_type (reference_type), short_type);
1920 push_type (int_type);
1921 break;
1922 case op_istore:
1923 set_variable (get_byte (), pop_type (int_type));
1924 break;
1925 case op_lstore:
1926 set_variable (get_byte (), pop_type (long_type));
1927 break;
1928 case op_fstore:
1929 set_variable (get_byte (), pop_type (float_type));
1930 break;
1931 case op_dstore:
1932 set_variable (get_byte (), pop_type (double_type));
1933 break;
1934 case op_astore:
1935 set_variable (get_byte (), pop_type (reference_type));
1936 break;
1937 case op_istore_0:
1938 case op_istore_1:
1939 case op_istore_2:
1940 case op_istore_3:
1941 set_variable (opcode - op_istore_0, pop_type (int_type));
1942 break;
1943 case op_lstore_0:
1944 case op_lstore_1:
1945 case op_lstore_2:
1946 case op_lstore_3:
1947 set_variable (opcode - op_lstore_0, pop_type (long_type));
1948 break;
1949 case op_fstore_0:
1950 case op_fstore_1:
1951 case op_fstore_2:
1952 case op_fstore_3:
1953 set_variable (opcode - op_fstore_0, pop_type (float_type));
1954 break;
1955 case op_dstore_0:
1956 case op_dstore_1:
1957 case op_dstore_2:
1958 case op_dstore_3:
1959 set_variable (opcode - op_dstore_0, pop_type (double_type));
1960 break;
1961 case op_astore_0:
1962 case op_astore_1:
1963 case op_astore_2:
1964 case op_astore_3:
1965 set_variable (opcode - op_astore_0, pop_type (reference_type));
1966 break;
1967 case op_iastore:
1968 pop_type (int_type);
1969 pop_type (int_type);
1970 require_array_type (pop_type (reference_type), int_type);
1971 break;
1972 case op_lastore:
1973 pop_type (long_type);
1974 pop_type (int_type);
1975 require_array_type (pop_type (reference_type), long_type);
1976 break;
1977 case op_fastore:
1978 pop_type (float_type);
1979 pop_type (int_type);
1980 require_array_type (pop_type (reference_type), float_type);
1981 break;
1982 case op_dastore:
1983 pop_type (double_type);
1984 pop_type (int_type);
1985 require_array_type (pop_type (reference_type), double_type);
1986 break;
1987 case op_aastore:
1988 pop_type (reference_type);
1989 pop_type (int_type);
1990 require_array_type (pop_type (reference_type), reference_type);
1991 break;
1992 case op_bastore:
1993 pop_type (int_type);
1994 pop_type (int_type);
1995 require_array_type (pop_type (reference_type), byte_type);
1996 break;
1997 case op_castore:
1998 pop_type (int_type);
1999 pop_type (int_type);
2000 require_array_type (pop_type (reference_type), char_type);
2001 break;
2002 case op_sastore:
2003 pop_type (int_type);
2004 pop_type (int_type);
2005 require_array_type (pop_type (reference_type), short_type);
2006 break;
2007 case op_pop:
2008 pop32 ();
2009 break;
2010 case op_pop2:
2011 pop64 ();
2012 break;
2013 case op_dup:
2015 type t = pop32 ();
2016 push_type (t);
2017 push_type (t);
2019 break;
2020 case op_dup_x1:
2022 type t1 = pop32 ();
2023 type t2 = pop32 ();
2024 push_type (t1);
2025 push_type (t2);
2026 push_type (t1);
2028 break;
2029 case op_dup_x2:
2031 type t1 = pop32 ();
2032 type t2 = pop_raw ();
2033 if (! t2.iswide ())
2035 type t3 = pop32 ();
2036 push_type (t1);
2037 push_type (t3);
2039 else
2040 push_type (t1);
2041 push_type (t2);
2042 push_type (t1);
2044 break;
2045 case op_dup2:
2047 type t = pop_raw ();
2048 if (! t.iswide ())
2050 type t2 = pop32 ();
2051 push_type (t2);
2052 push_type (t);
2053 push_type (t2);
2055 push_type (t);
2057 break;
2058 case op_dup2_x1:
2060 type t1 = pop_raw ();
2061 type t2 = pop32 ();
2062 if (! t1.iswide ())
2064 type t3 = pop32 ();
2065 push_type (t2);
2066 push_type (t1);
2067 push_type (t3);
2069 else
2070 push_type (t1);
2071 push_type (t2);
2072 push_type (t1);
2074 break;
2075 case op_dup2_x2:
2077 // FIXME
2078 type t1 = pop_raw ();
2079 if (t1.iswide ())
2081 type t2 = pop_raw ();
2082 if (t2.iswide ())
2084 push_type (t1);
2085 push_type (t2);
2087 else
2089 type t3 = pop32 ();
2090 push_type (t1);
2091 push_type (t3);
2092 push_type (t2);
2094 push_type (t1);
2096 else
2098 type t2 = pop32 ();
2099 type t3 = pop_raw ();
2100 if (t3.iswide ())
2102 push_type (t2);
2103 push_type (t1);
2105 else
2107 type t4 = pop32 ();
2108 push_type (t2);
2109 push_type (t1);
2110 push_type (t4);
2112 push_type (t3);
2113 push_type (t2);
2114 push_type (t1);
2117 break;
2118 case op_swap:
2120 type t1 = pop32 ();
2121 type t2 = pop32 ();
2122 push_type (t1);
2123 push_type (t2);
2125 break;
2126 case op_iadd:
2127 case op_isub:
2128 case op_imul:
2129 case op_idiv:
2130 case op_irem:
2131 case op_ishl:
2132 case op_ishr:
2133 case op_iushr:
2134 case op_iand:
2135 case op_ior:
2136 case op_ixor:
2137 pop_type (int_type);
2138 push_type (pop_type (int_type));
2139 break;
2140 case op_ladd:
2141 case op_lsub:
2142 case op_lmul:
2143 case op_ldiv:
2144 case op_lrem:
2145 case op_land:
2146 case op_lor:
2147 case op_lxor:
2148 pop_type (long_type);
2149 push_type (pop_type (long_type));
2150 break;
2151 case op_lshl:
2152 case op_lshr:
2153 case op_lushr:
2154 pop_type (int_type);
2155 push_type (pop_type (long_type));
2156 break;
2157 case op_fadd:
2158 case op_fsub:
2159 case op_fmul:
2160 case op_fdiv:
2161 case op_frem:
2162 pop_type (float_type);
2163 push_type (pop_type (float_type));
2164 break;
2165 case op_dadd:
2166 case op_dsub:
2167 case op_dmul:
2168 case op_ddiv:
2169 case op_drem:
2170 pop_type (double_type);
2171 push_type (pop_type (double_type));
2172 break;
2173 case op_ineg:
2174 case op_i2b:
2175 case op_i2c:
2176 case op_i2s:
2177 push_type (pop_type (int_type));
2178 break;
2179 case op_lneg:
2180 push_type (pop_type (long_type));
2181 break;
2182 case op_fneg:
2183 push_type (pop_type (float_type));
2184 break;
2185 case op_dneg:
2186 push_type (pop_type (double_type));
2187 break;
2188 case op_iinc:
2189 get_variable (get_byte (), int_type);
2190 get_byte ();
2191 break;
2192 case op_i2l:
2193 pop_type (int_type);
2194 push_type (long_type);
2195 break;
2196 case op_i2f:
2197 pop_type (int_type);
2198 push_type (float_type);
2199 break;
2200 case op_i2d:
2201 pop_type (int_type);
2202 push_type (double_type);
2203 break;
2204 case op_l2i:
2205 pop_type (long_type);
2206 push_type (int_type);
2207 break;
2208 case op_l2f:
2209 pop_type (long_type);
2210 push_type (float_type);
2211 break;
2212 case op_l2d:
2213 pop_type (long_type);
2214 push_type (double_type);
2215 break;
2216 case op_f2i:
2217 pop_type (float_type);
2218 push_type (int_type);
2219 break;
2220 case op_f2l:
2221 pop_type (float_type);
2222 push_type (long_type);
2223 break;
2224 case op_f2d:
2225 pop_type (float_type);
2226 push_type (double_type);
2227 break;
2228 case op_d2i:
2229 pop_type (double_type);
2230 push_type (int_type);
2231 break;
2232 case op_d2l:
2233 pop_type (double_type);
2234 push_type (long_type);
2235 break;
2236 case op_d2f:
2237 pop_type (double_type);
2238 push_type (float_type);
2239 break;
2240 case op_lcmp:
2241 pop_type (long_type);
2242 pop_type (long_type);
2243 push_type (int_type);
2244 break;
2245 case op_fcmpl:
2246 case op_fcmpg:
2247 pop_type (float_type);
2248 pop_type (float_type);
2249 push_type (int_type);
2250 break;
2251 case op_dcmpl:
2252 case op_dcmpg:
2253 pop_type (double_type);
2254 pop_type (double_type);
2255 push_type (int_type);
2256 break;
2257 case op_ifeq:
2258 case op_ifne:
2259 case op_iflt:
2260 case op_ifge:
2261 case op_ifgt:
2262 case op_ifle:
2263 pop_type (int_type);
2264 push_jump (get_short ());
2265 break;
2266 case op_if_icmpeq:
2267 case op_if_icmpne:
2268 case op_if_icmplt:
2269 case op_if_icmpge:
2270 case op_if_icmpgt:
2271 case op_if_icmple:
2272 pop_type (int_type);
2273 pop_type (int_type);
2274 push_jump (get_short ());
2275 break;
2276 case op_if_acmpeq:
2277 case op_if_acmpne:
2278 pop_type (reference_type);
2279 pop_type (reference_type);
2280 push_jump (get_short ());
2281 break;
2282 case op_goto:
2283 push_jump (get_short ());
2284 invalidate_pc ();
2285 break;
2286 case op_jsr:
2287 handle_jsr_insn (get_short ());
2288 break;
2289 case op_ret:
2290 handle_ret_insn (get_byte ());
2291 break;
2292 case op_tableswitch:
2294 pop_type (int_type);
2295 skip_padding ();
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 ());
2302 invalidate_pc ();
2304 break;
2306 case op_lookupswitch:
2308 pop_type (int_type);
2309 skip_padding ();
2310 push_jump (get_int ());
2311 jint npairs = get_int ();
2312 // Already checked NPAIRS >= 0.
2313 jint lastkey = 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);
2319 lastkey = key;
2320 push_jump (get_int ());
2322 invalidate_pc ();
2324 break;
2325 case op_ireturn:
2326 check_return_type (pop_type (int_type));
2327 invalidate_pc ();
2328 break;
2329 case op_lreturn:
2330 check_return_type (pop_type (long_type));
2331 invalidate_pc ();
2332 break;
2333 case op_freturn:
2334 check_return_type (pop_type (float_type));
2335 invalidate_pc ();
2336 break;
2337 case op_dreturn:
2338 check_return_type (pop_type (double_type));
2339 invalidate_pc ();
2340 break;
2341 case op_areturn:
2342 check_return_type (pop_type (reference_type));
2343 invalidate_pc ();
2344 break;
2345 case op_return:
2346 check_return_type (void_type);
2347 invalidate_pc ();
2348 break;
2349 case op_getstatic:
2350 push_type (check_field_constant (get_ushort ()));
2351 break;
2352 case op_putstatic:
2353 pop_type (check_field_constant (get_ushort ()));
2354 break;
2355 case op_getfield:
2357 type klass;
2358 type field = check_field_constant (get_ushort (), &klass);
2359 pop_type (klass);
2360 push_type (field);
2362 break;
2363 case op_putfield:
2365 type klass;
2366 type field = check_field_constant (get_ushort (), &klass);
2367 pop_type (field);
2368 pop_type (klass);
2370 break;
2372 case op_invokevirtual:
2373 case op_invokespecial:
2374 case op_invokestatic:
2375 case op_invokeinterface:
2377 _Jv_Utf8Const *method_name, *method_signature;
2378 type class_type
2379 = check_method_constant (get_ushort (),
2380 opcode == op_invokeinterface,
2381 &method_name,
2382 &method_signature);
2383 int arg_count = _Jv_count_arguments (method_signature);
2384 if (opcode == op_invokeinterface)
2386 int nargs = get_byte ();
2387 if (nargs == 0)
2388 verify_fail ("too few arguments to invokeinterface",
2389 start_PC);
2390 if (get_byte () != 0)
2391 verify_fail ("invokeinterface dummy byte is wrong",
2392 start_PC);
2393 if (nargs - 1 != arg_count)
2394 verify_fail ("wrong argument count for invokeinterface",
2395 start_PC);
2398 bool is_init = false;
2399 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2401 is_init = true;
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 `<'",
2407 start_PC);
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;
2418 if (is_init)
2420 // In this case the PC doesn't matter.
2421 t.set_uninitialized (type::UNINIT);
2423 t = pop_type (t);
2424 if (is_init)
2425 current_state->set_initialized (t.get_pc (),
2426 current_method->max_locals);
2429 type rt = compute_return_type (method_signature);
2430 if (! rt.isvoid ())
2431 push_type (rt);
2433 break;
2435 case op_new:
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",
2440 start_PC);
2441 t.set_uninitialized (start_PC);
2442 push_type (t);
2444 break;
2446 case op_newarray:
2448 int atype = get_byte ();
2449 // We intentionally have chosen constants to make this
2450 // valid.
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)));
2456 break;
2457 case op_anewarray:
2458 pop_type (int_type);
2459 push_type (check_class_constant (get_ushort ()).to_array ());
2460 break;
2461 case op_arraylength:
2463 type t = pop_type (reference_type);
2464 if (! t.isarray ())
2465 verify_fail ("array type expected", start_PC);
2466 push_type (int_type);
2468 break;
2469 case op_athrow:
2470 pop_type (type (&java::lang::Throwable::class$));
2471 invalidate_pc ();
2472 break;
2473 case op_checkcast:
2474 pop_type (reference_type);
2475 push_type (check_class_constant (get_ushort ()));
2476 break;
2477 case op_instanceof:
2478 pop_type (reference_type);
2479 check_class_constant (get_ushort ());
2480 push_type (int_type);
2481 break;
2482 case op_monitorenter:
2483 pop_type (reference_type);
2484 break;
2485 case op_monitorexit:
2486 pop_type (reference_type);
2487 break;
2488 case op_wide:
2490 switch (get_byte ())
2492 case op_iload:
2493 push_type (get_variable (get_ushort (), int_type));
2494 break;
2495 case op_lload:
2496 push_type (get_variable (get_ushort (), long_type));
2497 break;
2498 case op_fload:
2499 push_type (get_variable (get_ushort (), float_type));
2500 break;
2501 case op_dload:
2502 push_type (get_variable (get_ushort (), double_type));
2503 break;
2504 case op_aload:
2505 push_type (get_variable (get_ushort (), reference_type));
2506 break;
2507 case op_istore:
2508 set_variable (get_ushort (), pop_type (int_type));
2509 break;
2510 case op_lstore:
2511 set_variable (get_ushort (), pop_type (long_type));
2512 break;
2513 case op_fstore:
2514 set_variable (get_ushort (), pop_type (float_type));
2515 break;
2516 case op_dstore:
2517 set_variable (get_ushort (), pop_type (double_type));
2518 break;
2519 case op_astore:
2520 set_variable (get_ushort (), pop_type (reference_type));
2521 break;
2522 case op_ret:
2523 handle_ret_insn (get_short ());
2524 break;
2525 case op_iinc:
2526 get_variable (get_ushort (), int_type);
2527 get_short ();
2528 break;
2529 default:
2530 verify_fail ("unrecognized wide instruction", start_PC);
2533 break;
2534 case op_multianewarray:
2536 type atype = check_class_constant (get_ushort ());
2537 int dim = get_byte ();
2538 if (dim < 1)
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);
2543 push_type (atype);
2545 break;
2546 case op_ifnull:
2547 case op_ifnonnull:
2548 pop_type (reference_type);
2549 push_jump (get_short ());
2550 break;
2551 case op_goto_w:
2552 push_jump (get_int ());
2553 invalidate_pc ();
2554 break;
2555 case op_jsr_w:
2556 handle_jsr_insn (get_int ());
2557 break;
2559 default:
2560 // Unrecognized opcode.
2561 verify_fail ("unrecognized instruction in verify_instructions_0",
2562 start_PC);
2567 public:
2569 void verify_instructions ()
2571 branch_prepass ();
2572 verify_instructions_0 ();
2575 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2577 current_method = m;
2578 bytecode = m->bytecode ();
2579 exception = m->exceptions ();
2580 current_class = m->defining_class;
2582 states = NULL;
2583 flags = NULL;
2584 jsr_ptrs = NULL;
2587 ~_Jv_BytecodeVerifier ()
2589 if (states)
2590 _Jv_Free (states);
2591 if (flags)
2592 _Jv_Free (flags);
2593 if (jsr_ptrs)
2594 _Jv_Free (jsr_ptrs);
2598 void
2599 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
2601 _Jv_BytecodeVerifier v (meth);
2602 v.verify_instructions ();
2605 // FIXME: add more info, like PC, when required.
2606 static void
2607 verify_fail (char *s, jint pc)
2609 using namespace java::lang;
2610 StringBuffer *buf = new StringBuffer ();
2612 buf->append (JvNewStringLatin1 ("verification failed"));
2613 if (pc != -1)
2615 buf->append (JvNewStringLatin1 (" at PC "));
2616 buf->append (pc);
2618 buf->append (JvNewStringLatin1 (": "));
2619 buf->append (JvNewStringLatin1 (s));
2620 throw new java::lang::VerifyError (buf->toString ());
2623 #endif /* INTERPRETER */